0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 ** This file is
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 an amalgamation
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65 of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 C source files
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76 from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 31 34 2e 20 20 ersion 3.6.14.
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 the individual
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74 C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68 e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61 e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73 n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69 a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69 on.** unit. Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70 mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74 timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65 possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70 files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66 provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65 5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77 commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67 mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79 is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69 ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75 le SQLite. To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68 se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20 er.** programs,
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69 you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69 le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69 te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69 ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 ng interface to
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20 ry. (If you do
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65 not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61 "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64 der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20 , you will find
0340: 61 20 63 6f 70 79 20 69 6e 20 74 68 65 20 66 69 a copy in the fi
0350: 72 73 74 0a 2a 2a 20 35 35 33 33 20 6c 69 6e 65 rst.** 5533 line
0360: 73 20 70 61 73 74 20 74 68 69 73 20 68 65 61 64 s past this head
0370: 65 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20 41 64 er comment.) Ad
0380: 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 66 69 ditional code fi
0390: 6c 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6e 65 les may be.** ne
03a0: 65 64 65 64 20 69 66 20 79 6f 75 20 77 61 6e 74 eded if you want
03b0: 20 61 20 77 72 61 70 70 65 72 20 74 6f 20 69 6e a wrapper to in
03c0: 74 65 72 66 61 63 65 20 53 51 4c 69 74 65 20 77 terface SQLite w
03d0: 69 74 68 20 79 6f 75 72 20 63 68 6f 69 63 65 20 ith your choice
03e0: 6f 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 69 6e of.** programmin
03f0: 67 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 g language. The
0400: 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 22 73 code for the "s
0410: 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e 64 2d qlite3" command-
0420: 6c 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20 69 73 line shell.** is
0430: 20 61 6c 73 6f 20 69 6e 20 61 20 73 65 70 61 72 also in a separ
0440: 61 74 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 ate file. This
0450: 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e file contains on
0460: 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 ly code for the
0470: 63 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c core.** SQLite l
0480: 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 ibrary..**.** Th
0490: 69 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 is amalgamation
04a0: 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e was generated on
04b0: 20 32 30 30 39 2d 30 35 2d 30 37 20 30 30 3a 33 2009-05-07 00:3
04c0: 36 3a 31 31 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 6:11 UTC..*/.#de
04d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 fine SQLITE_CORE
04e0: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
04f0: 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 E_AMALGAMATION 1
0500: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
0510: 50 52 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 PRIVATE.# define
0520: 20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 SQLITE_PRIVATE
0530: 73 74 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 static.#endif.#i
0540: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 fndef SQLITE_API
0550: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
0560: 5f 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a _API.#endif./***
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
0580: 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 n file sqliteInt
0590: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
05c0: 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
05d0: 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
05e0: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
05f0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
0600: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
0610: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
0620: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
0630: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
0640: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
0650: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
0660: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
0670: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
0680: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
0690: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
06a0: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
06b0: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
06c0: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
06d0: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
06e0: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
06f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0700: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0730: 2a 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 **.** Internal i
0740: 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 nterface definit
0750: 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e ions for SQLite.
0760: 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a .**.** @(#) $Id:
0770: 20 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76 20 31 sqliteInt.h,v 1
0780: 2e 38 36 38 20 32 30 30 39 2f 30 35 2f 30 34 20 .868 2009/05/04
0790: 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 6c 6b 11:42:30 danielk
07a0: 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 1977 Exp $.*/.#i
07b0: 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49 4e 54 fndef _SQLITEINT
07c0: 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c _H_.#define _SQL
07d0: 49 54 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a ITEINT_H_../*.**
07e0: 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63 6f 6e Include the con
07f0: 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61 64 65 figuration heade
0800: 72 20 6f 75 74 70 75 74 20 62 79 20 27 63 6f 6e r output by 'con
0810: 66 69 67 75 72 65 27 20 69 66 20 77 65 27 72 65 figure' if we're
0820: 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 61 75 using the.** au
0830: 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62 75 69 toconf-based bui
0840: 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 48 41 ld.*/.#ifdef _HA
0850: 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 VE_SQLITE_CONFIG
0860: 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e _H.#include "con
0870: 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a 0a 2f fig.h".#endif../
0880: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
0890: 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c 69 6d nclude sqliteLim
08a0: 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 it.h in the midd
08b0: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
08c0: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f h ***********/./
08d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
08e0: 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 egin file sqlite
08f0: 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a Limit.h ********
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0910: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
0920: 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 37 0a *.** 2007 May 7.
0930: 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
0940: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
0950: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
0960: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
0970: 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
0980: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
0990: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
09a0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
09b0: 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
09c0: 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
09d0: 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
09e0: 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
09f0: 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
0a00: 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
0a10: 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
0a20: 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
0a30: 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
0a40: 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
0a90: 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 * .** This file
0aa0: 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75 73 20 defines various
0ab0: 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74 20 53 limits of what S
0ac0: 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63 65 73 QLite can proces
0ad0: 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 s..**.** @(#) $I
0ae0: 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 d: sqliteLimit.h
0af0: 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30 31 2f ,v 1.10 2009/01/
0b00: 31 30 20 31 36 3a 31 35 3a 30 39 20 64 61 6e 69 10 16:15:09 dani
0b10: 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
0b20: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ../*.** The maxi
0b30: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 mum length of a
0b40: 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 69 6e 20 TEXT or BLOB in
0b50: 62 79 74 65 73 2e 20 20 20 54 68 69 73 20 61 6c bytes. This al
0b60: 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 20 74 68 65 so.** limits the
0b70: 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77 20 69 size of a row i
0b80: 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 n a table or ind
0b90: 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 ex..**.** The ha
0ba0: 72 64 20 6c 69 6d 69 74 20 69 73 20 74 68 65 20 rd limit is the
0bb0: 61 62 69 6c 69 74 79 20 6f 66 20 61 20 33 32 2d ability of a 32-
0bc0: 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
0bd0: 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 20 74 er.** to count t
0be0: 68 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d 31 20 he size: 2^31-1
0bf0: 6f 72 20 32 31 34 37 34 38 33 36 34 37 2e 0a 2a or 2147483647..*
0c00: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
0c10: 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20 64 65 _MAX_LENGTH.# de
0c20: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
0c30: 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 LENGTH 100000000
0c40: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
0c50: 54 68 69 73 20 69 73 20 74 68 65 20 6d 61 78 69 This is the maxi
0c60: 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a mum number of.**
0c70: 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 .** * Columns
0c80: 20 69 6e 20 61 20 74 61 62 6c 65 0a 2a 2a 20 20 in a table.**
0c90: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 * Columns in a
0ca0: 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 2a 20 n index.** *
0cb0: 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 76 69 65 Columns in a vie
0cc0: 77 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 w.** * Terms
0cd0: 69 6e 20 74 68 65 20 53 45 54 20 63 6c 61 75 73 in the SET claus
0ce0: 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73 e of an UPDATE s
0cf0: 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a tatement.** *
0d00: 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65 Terms in the re
0d10: 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 sult set of a SE
0d20: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a LECT statement.*
0d30: 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 * * Terms in
0d40: 74 68 65 20 47 52 4f 55 50 20 42 59 20 6f 72 20 the GROUP BY or
0d50: 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73 ORDER BY clauses
0d60: 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 of a SELECT sta
0d70: 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 2a 20 tement..** *
0d80: 54 65 72 6d 73 20 69 6e 20 74 68 65 20 56 41 4c Terms in the VAL
0d90: 55 45 53 20 63 6c 61 75 73 65 20 6f 66 20 61 6e UES clause of an
0da0: 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e INSERT statemen
0db0: 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64 t.**.** The hard
0dc0: 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68 65 72 upper limit her
0dd0: 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d 6f 73 e is 32676. Mos
0de0: 74 20 64 61 74 61 62 61 73 65 20 70 65 6f 70 6c t database peopl
0df0: 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c 20 79 e will.** tell y
0e00: 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77 65 6c ou that in a wel
0e10: 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 61 74 l-normalized dat
0e20: 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75 61 6c abase, you usual
0e30: 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 ly should.** not
0e40: 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61 6e 20 have more than
0e50: 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20 63 6f a dozen or so co
0e60: 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74 61 62 lumns in any tab
0e70: 6c 65 2e 20 20 41 6e 64 20 69 66 0a 2a 2a 20 74 le. And if.** t
0e80: 68 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c hat is the case,
0e90: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 there is no poi
0ea0: 6e 74 20 69 6e 20 68 61 76 69 6e 67 20 6d 6f 72 nt in having mor
0eb0: 65 20 74 68 61 6e 20 61 20 66 65 77 0a 2a 2a 20 e than a few.**
0ec0: 64 6f 7a 65 6e 20 76 61 6c 75 65 73 20 69 6e 20 dozen values in
0ed0: 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 any of the other
0ee0: 20 73 69 74 75 61 74 69 6f 6e 73 20 64 65 73 63 situations desc
0ef0: 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a ribed above..*/.
0f00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
0f10: 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65 66 69 AX_COLUMN.# defi
0f20: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f ne SQLITE_MAX_CO
0f30: 4c 55 4d 4e 20 32 30 30 30 0a 23 65 6e 64 69 66 LUMN 2000.#endif
0f40: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ../*.** The maxi
0f50: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 mum length of a
0f60: 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 single SQL state
0f70: 6d 65 6e 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a ment in bytes..*
0f80: 2a 0a 2a 2a 20 49 74 20 75 73 65 64 20 74 6f 20 *.** It used to
0f90: 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
0fa0: 20 73 65 74 74 69 6e 67 20 74 68 69 73 20 76 61 setting this va
0fb0: 6c 75 65 20 74 6f 20 7a 65 72 6f 20 77 6f 75 6c lue to zero woul
0fc0: 64 0a 2a 2a 20 74 75 72 6e 20 74 68 65 20 6c 69 d.** turn the li
0fd0: 6d 69 74 20 6f 66 66 2e 20 20 54 68 61 74 20 69 mit off. That i
0fe0: 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65 s no longer true
0ff0: 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 . It is not pos
1000: 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 74 75 72 6e sible.** to turn
1010: 20 74 68 69 73 20 6c 69 6d 69 74 20 6f 66 66 2e this limit off.
1020: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1030: 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 TE_MAX_SQL_LENGT
1040: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 H.# define SQLIT
1050: 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 E_MAX_SQL_LENGTH
1060: 20 31 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 1000000000.#end
1070: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
1080: 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61 ximum depth of a
1090: 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 n expression tre
10a0: 65 2e 20 54 68 69 73 20 69 73 20 6c 69 6d 69 74 e. This is limit
10b0: 65 64 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65 20 65 ed to .** some e
10c0: 78 74 65 6e 74 20 62 79 20 53 51 4c 49 54 45 5f xtent by SQLITE_
10d0: 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2e 20 MAX_SQL_LENGTH.
10e0: 42 75 74 20 73 6f 6d 65 74 69 6d 65 20 79 6f 75 But sometime you
10f0: 20 6d 69 67 68 74 20 0a 2a 2a 20 77 61 6e 74 20 might .** want
1100: 74 6f 20 70 6c 61 63 65 20 6d 6f 72 65 20 73 65 to place more se
1110: 76 65 72 65 20 6c 69 6d 69 74 73 20 6f 6e 20 74 vere limits on t
1120: 68 65 20 63 6f 6d 70 6c 65 78 69 74 79 20 6f 66 he complexity of
1130: 20 61 6e 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 an .** expressi
1140: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 6c 75 on..**.** A valu
1150: 65 20 6f 66 20 30 20 75 73 65 64 20 74 6f 20 6d e of 0 used to m
1160: 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 6d ean that the lim
1170: 69 74 20 77 61 73 20 6e 6f 74 20 65 6e 66 6f 72 it was not enfor
1180: 63 65 64 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 ced..** But that
1190: 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 is no longer tr
11a0: 75 65 2e 20 20 54 68 65 20 6c 69 6d 69 74 20 69 ue. The limit i
11b0: 73 20 6e 6f 77 20 73 74 72 69 63 74 6c 79 20 65 s now strictly e
11c0: 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 74 20 61 6c nforced.** at al
11d0: 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 6e l times..*/.#ifn
11e0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 def SQLITE_MAX_E
11f0: 58 50 52 5f 44 45 50 54 48 0a 23 20 64 65 66 69 XPR_DEPTH.# defi
1200: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 ne SQLITE_MAX_EX
1210: 50 52 5f 44 45 50 54 48 20 31 30 30 30 0a 23 65 PR_DEPTH 1000.#e
1220: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1230: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1240: 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d f terms in a com
1250: 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 pound SELECT sta
1260: 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 63 tement..** The c
1270: 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 66 6f ode generator fo
1280: 72 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 r compound SELEC
1290: 54 20 73 74 61 74 65 6d 65 6e 74 73 20 64 6f 65 T statements doe
12a0: 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c 20 6f s one.** level o
12b0: 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 f recursion for
12c0: 65 61 63 68 20 74 65 72 6d 2e 20 20 41 20 73 74 each term. A st
12d0: 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 63 61 6e ack overflow can
12e0: 20 72 65 73 75 6c 74 0a 2a 2a 20 69 66 20 74 68 result.** if th
12f0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d e number of term
1300: 73 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2e 20 s is too large.
1310: 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 6f In practice, mo
1320: 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 76 65 72 20 st SQL.** never
1330: 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 33 20 has more than 3
1340: 6f 72 20 34 20 74 65 72 6d 73 2e 20 20 55 73 65 or 4 terms. Use
1350: 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20 74 6f a value of 0 to
1360: 20 64 69 73 61 62 6c 65 0a 2a 2a 20 61 6e 79 20 disable.** any
1370: 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d limit on the num
1380: 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 ber of terms in
1390: 61 20 63 6f 6d 70 6f 75 6e 74 20 53 45 4c 45 43 a compount SELEC
13a0: 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 T..*/.#ifndef SQ
13b0: 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e LITE_MAX_COMPOUN
13c0: 44 5f 53 45 4c 45 43 54 0a 23 20 64 65 66 69 6e D_SELECT.# defin
13d0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d e SQLITE_MAX_COM
13e0: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 35 30 30 POUND_SELECT 500
13f0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1400: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1410: 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 69 6e 20 r of opcodes in
1420: 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a a VDBE program..
1430: 2a 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79 ** Not currently
1440: 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a 23 69 enforced..*/.#i
1450: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1460: 5f 56 44 42 45 5f 4f 50 0a 23 20 64 65 66 69 6e _VDBE_OP.# defin
1470: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 e SQLITE_MAX_VDB
1480: 45 5f 4f 50 20 32 35 30 30 30 0a 23 65 6e 64 69 E_OP 25000.#endi
1490: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 f../*.** The max
14a0: 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 imum number of a
14b0: 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 6e 20 53 rguments to an S
14c0: 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a QL function..*/.
14d0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
14e0: 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 0a AX_FUNCTION_ARG.
14f0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1500: 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 MAX_FUNCTION_ARG
1510: 20 31 32 37 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 127.#endif../*.
1520: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1530: 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f umber of in-memo
1540: 72 79 20 70 61 67 65 73 20 74 6f 20 75 73 65 20 ry pages to use
1550: 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 for the main dat
1560: 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 61 abase.** table a
1570: 6e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 nd for temporary
1580: 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 53 51 tables. The SQ
1590: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 LITE_DEFAULT_CAC
15a0: 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66 6e 64 HE_SIZE.*/.#ifnd
15b0: 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
15c0: 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 T_CACHE_SIZE.# d
15d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
15e0: 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 20 AULT_CACHE_SIZE
15f0: 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 2000.#endif.#if
1600: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
1610: 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 ULT_TEMP_CACHE_S
1620: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c IZE.# define SQL
1630: 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 ITE_DEFAULT_TEMP
1640: 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 35 30 30 _CACHE_SIZE 500
1650: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1660: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1670: 72 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 r of attached da
1680: 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 20 6d tabases. This m
1690: 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 ust be between 0
16a0: 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 20 54 68 65 .** and 30. The
16b0: 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 upper bound on
16c0: 33 30 20 69 73 20 62 65 63 61 75 73 65 20 61 20 30 is because a
16d0: 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 62 32-bit integer b
16e0: 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73 65 64 itmap.** is used
16f0: 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 74 internally to t
1700: 72 61 63 6b 20 61 74 74 61 63 68 65 64 20 64 61 rack attached da
1710: 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e tabases..*/.#ifn
1720: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 def SQLITE_MAX_A
1730: 54 54 41 43 48 45 44 0a 23 20 64 65 66 69 6e 65 TTACHED.# define
1740: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 SQLITE_MAX_ATTA
1750: 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66 0a 0a CHED 10.#endif..
1760: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
1770: 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20 3f 6e um value of a ?n
1780: 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68 61 74 nn wildcard that
1790: 20 74 68 65 20 70 61 72 73 65 72 20 77 69 6c 6c the parser will
17a0: 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 66 6e accept..*/.#ifn
17b0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 def SQLITE_MAX_V
17c0: 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a 23 ARIABLE_NUMBER.#
17d0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
17e0: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 AX_VARIABLE_NUMB
17f0: 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a 0a 2f ER 999.#endif../
1800: 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65 20 73 * Maximum page s
1810: 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65 72 20 ize. The upper
1820: 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20 76 61 bound on this va
1830: 6c 75 65 20 69 73 20 33 32 37 36 38 2e 20 20 54 lue is 32768. T
1840: 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a 20 69 his a limit.** i
1850: 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20 6e 65 mposed by the ne
1860: 63 65 73 73 69 74 79 20 6f 66 20 73 74 6f 72 69 cessity of stori
1870: 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 ng the value in
1880: 61 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 a 2-byte unsigne
1890: 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 6e 64 d integer.** and
18a0: 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 the fact that t
18b0: 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 he page size mus
18c0: 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 t be a power of
18d0: 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 2..**.** If this
18e0: 20 6c 69 6d 69 74 20 69 73 20 63 68 61 6e 67 65 limit is change
18f0: 64 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 70 d, then the comp
1900: 69 6c 65 64 20 6c 69 62 72 61 72 79 20 69 73 20 iled library is
1910: 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 69 technically.** i
1920: 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 ncompatible with
1930: 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61 an SQLite libra
1940: 72 79 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 ry compiled with
1950: 20 61 20 64 69 66 66 65 72 65 6e 74 20 6c 69 6d a different lim
1960: 69 74 2e 20 49 66 0a 2a 2a 20 61 20 70 72 6f 63 it. If.** a proc
1970: 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e ess operating on
1980: 20 61 20 64 61 74 61 62 61 73 65 20 77 69 74 68 a database with
1990: 20 61 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 a page-size of
19a0: 36 35 35 33 36 20 62 79 74 65 73 20 0a 2a 2a 20 65536 bytes .**
19b0: 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20 61 6e crashes, then an
19c0: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53 51 4c instance of SQL
19d0: 69 74 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 ite compiled wit
19e0: 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 h the default pa
19f0: 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 6c 69 6d 69 ge-size .** limi
1a00: 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 t will not be ab
1a10: 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 le to rollback t
1a20: 68 65 20 61 62 6f 72 74 65 64 20 74 72 61 6e 73 he aborted trans
1a30: 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 63 6f 75 action. This cou
1a40: 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 6f 20 64 61 ld.** lead to da
1a50: 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
1a60: 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 n..*/.#ifndef SQ
1a70: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
1a80: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ZE.# define SQLI
1a90: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
1aa0: 20 33 32 37 36 38 0a 23 65 6e 64 69 66 0a 0a 0a 32768.#endif...
1ab0: 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c /*.** The defaul
1ac0: 74 20 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61 t size of a data
1ad0: 62 61 73 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 base page..*/.#i
1ae0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
1af0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 AULT_PAGE_SIZE.#
1b00: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
1b10: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1b20: 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 23 69 66 1024.#endif.#if
1b30: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1b40: 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45 PAGE_SIZE>SQLITE
1b50: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 _MAX_PAGE_SIZE.#
1b60: 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 undef SQLITE_DE
1b70: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a FAULT_PAGE_SIZE.
1b80: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1b90: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1ba0: 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 E SQLITE_MAX_PAG
1bb0: 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 2f E_SIZE.#endif../
1bc0: 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 69 6c 79 2c *.** Ordinarily,
1bd0: 20 69 66 20 6e 6f 20 76 61 6c 75 65 20 69 73 20 if no value is
1be0: 65 78 70 6c 69 63 69 74 6c 79 20 70 72 6f 76 69 explicitly provi
1bf0: 64 65 64 2c 20 53 51 4c 69 74 65 20 63 72 65 61 ded, SQLite crea
1c00: 74 65 73 20 64 61 74 61 62 61 73 65 73 0a 2a 2a tes databases.**
1c10: 20 77 69 74 68 20 70 61 67 65 20 73 69 7a 65 20 with page size
1c20: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
1c30: 41 47 45 5f 53 49 5a 45 2e 20 48 6f 77 65 76 65 AGE_SIZE. Howeve
1c40: 72 2c 20 62 61 73 65 64 20 6f 6e 20 63 65 72 74 r, based on cert
1c50: 61 69 6e 0a 2a 2a 20 64 65 76 69 63 65 20 63 68 ain.** device ch
1c60: 61 72 61 63 74 65 72 69 73 74 69 63 73 20 28 73 aracteristics (s
1c70: 65 63 74 6f 72 2d 73 69 7a 65 20 61 6e 64 20 61 ector-size and a
1c80: 74 6f 6d 69 63 20 77 72 69 74 65 28 29 20 73 75 tomic write() su
1c90: 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c 69 74 pport),.** SQLit
1ca0: 65 20 6d 61 79 20 63 68 6f 6f 73 65 20 61 20 6c e may choose a l
1cb0: 61 72 67 65 72 20 76 61 6c 75 65 2e 20 54 68 69 arger value. Thi
1cc0: 73 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 74 68 s constant is th
1cd0: 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 0a e maximum value.
1ce0: 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 63 ** SQLite will c
1cf0: 68 6f 6f 73 65 20 6f 6e 20 69 74 73 20 6f 77 6e hoose on its own
1d00: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1d10: 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f ITE_MAX_DEFAULT_
1d20: 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 PAGE_SIZE.# defi
1d30: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 ne SQLITE_MAX_DE
1d40: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 FAULT_PAGE_SIZE
1d50: 38 31 39 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 8192.#endif.#if
1d60: 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 SQLITE_MAX_DEFAU
1d70: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c LT_PAGE_SIZE>SQL
1d80: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
1d90: 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 E.# undef SQLITE
1da0: 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
1db0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 E_SIZE.# define
1dc0: 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 SQLITE_MAX_DEFAU
1dd0: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 53 51 4c LT_PAGE_SIZE SQL
1de0: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
1df0: 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a E.#endif.../*.**
1e00: 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 Maximum number
1e10: 6f 66 20 70 61 67 65 73 20 69 6e 20 6f 6e 65 20 of pages in one
1e20: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1e30: 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 72 65 61 *.** This is rea
1e40: 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 64 65 66 lly just the def
1e50: 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 ault value for t
1e60: 68 65 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e he max_page_coun
1e70: 74 20 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 68 69 t pragma..** Thi
1e80: 73 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6c s value can be l
1e90: 6f 77 65 72 65 64 20 28 6f 72 20 72 61 69 73 65 owered (or raise
1ea0: 64 29 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 d) at run-time u
1eb0: 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a sing that the.**
1ec0: 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 max_page_count
1ed0: 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 macro..*/.#ifnde
1ee0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 f SQLITE_MAX_PAG
1ef0: 45 5f 43 4f 55 4e 54 0a 23 20 64 65 66 69 6e 65 E_COUNT.# define
1f00: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
1f10: 5f 43 4f 55 4e 54 20 31 30 37 33 37 34 31 38 32 _COUNT 107374182
1f20: 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 3.#endif../*.**
1f30: 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 Maximum length (
1f40: 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 in bytes) of the
1f50: 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 pattern in a LI
1f60: 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 KE or GLOB.** op
1f70: 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 erator..*/.#ifnd
1f80: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 ef SQLITE_MAX_LI
1f90: 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 KE_PATTERN_LENGT
1fa0: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 H.# define SQLIT
1fb0: 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 E_MAX_LIKE_PATTE
1fc0: 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a RN_LENGTH 50000.
1fd0: 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
1fe0: 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 ******* End of s
1ff0: 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a qliteLimit.h ***
2000: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2020: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
2030: 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
2040: 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
2050: 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
2060: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
2070: 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 61 ******/../* Disa
2080: 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61 72 ble nuisance war
2090: 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e 64 nings on Borland
20a0: 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 69 compilers */.#i
20b0: 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c f defined(__BORL
20c0: 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 20 ANDC__).#pragma
20d0: 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e 72 warn -rch /* unr
20e0: 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a 2f eachable code */
20f0: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 .#pragma warn -c
2100: 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 cc /* Condition
2110: 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 20 6f is always true o
2120: 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 67 r false */.#prag
2130: 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a 20 ma warn -aus /*
2140: 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20 69 Assigned value i
2150: 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a s never used */.
2160: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 73 #pragma warn -cs
2170: 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 73 u /* Comparing s
2180: 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e igned and unsign
2190: 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 ed */.#pragma wa
21a0: 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 69 rn -spa /* Suspi
21b0: 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61 72 cious pointer ar
21c0: 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e 64 ithmetic */.#end
21d0: 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66 6f if../* Needed fo
21e0: 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e 69 r various defini
21f0: 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66 6e tions... */.#ifn
2200: 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a def _GNU_SOURCE.
2210: 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53 4f # define _GNU_SO
2220: 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a URCE.#endif../*.
2230: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e 64 ** Include stand
2240: 61 72 64 20 68 65 61 64 65 72 20 66 69 6c 65 73 ard header files
2250: 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a 2f as necessary.*/
2260: 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 44 .#ifdef HAVE_STD
2270: 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c INT_H.#include <
2280: 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 stdint.h>.#endif
2290: 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e 54 .#ifdef HAVE_INT
22a0: 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64 65 TYPES_H.#include
22b0: 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23 65 <inttypes.h>.#e
22c0: 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 69 73 ndif../*. * This
22d0: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 macro is used t
22e0: 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 75 67 o "hide" some ug
22f0: 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 69 6e liness in castin
2300: 67 20 61 6e 20 69 6e 74 0a 20 2a 20 76 61 6c 75 g an int. * valu
2310: 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c 75 65 e to a ptr value
2320: 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 43 20 under the MSVC
2330: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 2e 64-bit compiler.
2340: 20 20 20 43 61 73 74 69 6e 67 0a 20 2a 20 6e 6f Casting. * no
2350: 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 73 20 n 64-bit values
2360: 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 65 73 to ptr types res
2370: 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 64 22 ults in a "hard"
2380: 20 65 72 72 6f 72 20 77 69 74 68 20 0a 20 2a 20 error with . *
2390: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 the MSVC 64-bit
23a0: 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 20 74 compiler which t
23b0: 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20 his attempts to
23c0: 61 76 6f 69 64 2e 20 20 0a 20 2a 0a 20 2a 20 41 avoid. . *. * A
23d0: 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c 65 72 simple compiler
23e0: 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 74 69 pragma or casti
23f0: 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 75 6c ng sequence coul
2400: 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 20 d not be found.
2410: 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69 * to correct thi
2420: 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 74 69 s in all situati
2430: 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d 61 63 ons, so this mac
2440: 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 63 65 ro was introduce
2450: 64 2e 0a 20 2a 0a 20 2a 20 49 74 20 63 6f 75 6c d.. *. * It coul
2460: 64 20 62 65 20 61 72 67 75 65 64 20 74 68 61 74 d be argued that
2470: 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 74 79 the intptr_t ty
2480: 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64 pe could be used
2490: 20 69 6e 20 74 68 69 73 0a 20 2a 20 63 61 73 65 in this. * case
24a0: 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 65 20 , but that type
24b0: 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
24c0: 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 on all compiler
24d0: 73 2c 20 6f 72 20 0a 20 2a 20 72 65 71 75 69 72 s, or . * requir
24e0: 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20 es the #include
24f0: 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64 of specific head
2500: 65 72 73 20 77 68 69 63 68 20 64 69 66 66 65 72 ers which differ
2510: 73 20 62 65 74 77 65 65 6e 0a 20 2a 20 70 6c 61 s between. * pla
2520: 74 66 6f 72 6d 73 2e 0a 20 2a 2f 0a 23 64 65 66 tforms.. */.#def
2530: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 ine SQLITE_INT_T
2540: 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76 6f 69 O_PTR(X) ((voi
2550: 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 d*)&((char*)0)[X
2560: 5d 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 ]).#define SQLIT
2570: 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20 E_PTR_TO_INT(X)
2580: 20 20 28 28 69 6e 74 29 28 28 28 63 68 61 72 2a ((int)(((char*
2590: 29 58 29 2d 28 63 68 61 72 2a 29 30 29 29 0a 0a )X)-(char*)0))..
25a0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 /*.** These #def
25b0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 ines should enab
25c0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 le >2GB file sup
25d0: 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 69 66 port on POSIX if
25e0: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 the.** underlyi
25f0: 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 ng operating sys
2600: 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e tem supports it.
2610: 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b If the OS lack
2620: 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 s.** large file
2630: 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74 support, or if t
2640: 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73 he OS is windows
2650: 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 , these should b
2660: 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 e no-ops..**.**
2670: 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 20 54 Ticket #2739: T
2680: 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f he _LARGEFILE_SO
2690: 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 74 20 URCE macro must
26a0: 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 61 6e appear before an
26b0: 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 6e 63 y.** system #inc
26c0: 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c 20 74 ludes. Hence, t
26d0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 his block of cod
26e0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 76 65 e must be the ve
26f0: 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f 64 65 ry first.** code
2700: 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 in all source f
2710: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 iles..**.** Larg
2720: 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 e file support c
2730: 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 an be disabled u
2740: 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 sing the -DSQLIT
2750: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 E_DISABLE_LFS sw
2760: 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 itch.** on the c
2770: 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 ompiler command
2780: 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e line. This is n
2790: 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 ecessary if you
27a0: 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a are compiling.**
27b0: 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 on a recent mac
27c0: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 48 61 hine (ex: Red Ha
27d0: 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 t 7.2) but you w
27e0: 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f ant your code to
27f0: 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f work.** on an o
2800: 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 lder machine (ex
2810: 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 2e 20 : Red Hat 6.0).
2820: 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 If you compile
2830: 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 0a 2a on Red Hat 7.2.*
2840: 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f * without this o
2850: 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e ption, LFS is en
2860: 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 able. But LFS d
2870: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e oes not exist in
2880: 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 the kernel.** i
2890: 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c 20 73 n Red Hat 6.0, s
28a0: 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 o the code won't
28b0: 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 work. Hence, f
28c0: 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 or maximum binar
28d0: 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 y.** portability
28e0: 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 you should omit
28f0: 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69 LFS..**.** Simi
2900: 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f 72 20 lar is true for
2910: 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 20 69 Mac OS X. LFS i
2920: 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 s only supported
2930: 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 20 61 on Mac OS X 9 a
2940: 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66 nd later..*/.#if
2950: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 ndef SQLITE_DISA
2960: 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65 BLE_LFS.# define
2970: 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20 _LARGE_FILE
2980: 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46 1.# ifndef _F
2990: 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a ILE_OFFSET_BITS.
29a0: 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 # define _FILE
29b0: 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a _OFFSET_BITS 64.
29c0: 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 # endif.# define
29d0: 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 _LARGEFILE_SOUR
29e0: 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a CE 1.#endif.../*
29f0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 .** The SQLITE_T
2a00: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 HREADSAFE macro
2a10: 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 must be defined
2a20: 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 as either 0 or 1
2a30: 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 ..** Older versi
2a40: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73 ons of SQLite us
2a50: 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 ed an optional T
2a60: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e HREADSAFE macro.
2a70: 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74 .** We support t
2a80: 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a hat for legacy.*
2a90: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
2aa0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
2ab0: 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 ).#if defined(TH
2ac0: 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69 READSAFE).# defi
2ad0: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ne SQLITE_THREAD
2ae0: 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a SAFE THREADSAFE.
2af0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
2b00: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
2b10: 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 1.#endif.#endif
2b20: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
2b30: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
2b40: 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 ATUS macro must
2b50: 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69 be defined as ei
2b60: 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 ther 0 or 1..**
2b70: 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 It determines wh
2b80: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
2b90: 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65 features relate
2ba0: 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f d to .** SQLITE_
2bb0: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 CONFIG_MEMSTATUS
2bc0: 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62 are available b
2bd0: 79 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 y default or not
2be0: 2e 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e . This value can
2bf0: 0a 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 .** be overridde
2c00: 6e 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 n at runtime usi
2c10: 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 ng the sqlite3_c
2c20: 6f 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a onfig() API..*/.
2c30: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
2c40: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 ITE_DEFAULT_MEMS
2c50: 54 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 TATUS).# define
2c60: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d SQLITE_DEFAULT_M
2c70: 45 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 EMSTATUS 1.#endi
2c80: 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 f../*.** Exactly
2c90: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
2ca0: 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 owing macros mus
2cb0: 74 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 t be defined in
2cc0: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 order to.** spec
2cd0: 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 ify which memory
2ce0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
2cf0: 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a ystem to use..**
2d00: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 .** SQLITE_S
2d10: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 YSTEM_MALLOC
2d20: 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 // Use nor
2d30: 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f mal system mallo
2d40: 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 c().** SQLIT
2d50: 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 E_MEMDEBUG
2d60: 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 // Debu
2d70: 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 gging version of
2d80: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 system malloc()
2d90: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d .** SQLITE_M
2da0: 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 EMORY_SIZE
2db0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 // interna
2dc0: 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a l allocator #1.*
2dd0: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 * SQLITE_MMA
2de0: 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 P_HEAP_SIZE
2df0: 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 // internal
2e00: 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 mmap() allocator
2e10: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 .** SQLITE_P
2e20: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 OW2_MEMORY_SIZE
2e30: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 // interna
2e40: 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 l power-of-two a
2e50: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 llocator.**.** I
2e60: 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 f none of the ab
2e70: 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c ove are defined,
2e80: 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 then set SQLITE
2e90: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 _SYSTEM_MALLOC a
2ea0: 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 s.** the default
2eb0: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
2ec0: 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d (SQLITE_SYSTEM_M
2ed0: 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 ALLOC)+defined(S
2ee0: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b QLITE_MEMDEBUG)+
2ef0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 \. defined(SQ
2f00: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 LITE_MEMORY_SIZE
2f10: 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 )+defined(SQLITE
2f20: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 _MMAP_HEAP_SIZE)
2f30: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 +\. defined(S
2f40: 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 QLITE_POW2_MEMOR
2f50: 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f Y_SIZE)>1.# erro
2f60: 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f r "At most one o
2f70: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
2f80: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e compile-time con
2f90: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
2fa0: 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 ns\. is allows:
2fb0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
2fc0: 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d LLOC, SQLITE_MEM
2fd0: 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 DEBUG, SQLITE_ME
2fe0: 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c MORY_SIZE,\. SQL
2ff0: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 ITE_MMAP_HEAP_SI
3000: 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f ZE, SQLITE_POW2_
3010: 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e MEMORY_SIZE".#en
3020: 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 dif.#if defined(
3030: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
3040: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 LLOC)+defined(SQ
3050: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c LITE_MEMDEBUG)+\
3060: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
3070: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 ITE_MEMORY_SIZE)
3080: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f +defined(SQLITE_
3090: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b MMAP_HEAP_SIZE)+
30a0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 \. defined(SQ
30b0: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 LITE_POW2_MEMORY
30c0: 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 _SIZE)==0.# defi
30d0: 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d ne SQLITE_SYSTEM
30e0: 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 _MALLOC 1.#endif
30f0: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ../*.** If SQLIT
3100: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 E_MALLOC_SOFT_LI
3110: 4d 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c MIT is not zero,
3120: 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 then try to kee
3130: 70 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f p the.** sizes o
3140: 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 f memory allocat
3150: 69 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 ions below this
3160: 76 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 value where poss
3170: 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 ible..*/.#if !de
3180: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c fined(SQLITE_MAL
3190: 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a LOC_SOFT_LIMIT).
31a0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
31b0: 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 MALLOC_SOFT_LIMI
31c0: 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f T 1024.#endif../
31d0: 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 *.** We need to
31e0: 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f define _XOPEN_SO
31f0: 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 URCE as follows
3200: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 in order to enab
3210: 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20 le.** recursive
3220: 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 mutexes on most
3230: 55 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42 Unix systems. B
3240: 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64 ut Mac OS X is d
3250: 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 ifferent..** The
3260: 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 _XOPEN_SOURCE d
3270: 65 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f efine causes pro
3280: 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 blems for Mac OS
3290: 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a X we are told,.
32a0: 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 ** so it is omit
32b0: 74 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20 ted there. See
32c0: 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a ticket #2673..**
32d0: 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61 .** Later we lea
32e0: 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 rn that _XOPEN_S
32f0: 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 OURCE is poorly
3300: 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a or incorrectly.*
3310: 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e * implemented on
3320: 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 some systems.
3330: 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69 So we avoid defi
3340: 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a ning it at all.*
3350: 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 * if it is alrea
3360: 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66 dy defined or if
3370: 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20 it is unneeded
3380: 62 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a because we are.*
3390: 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 * not doing a th
33a0: 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20 readsafe build.
33b0: 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a Ticket #2681..*
33c0: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 *.** See also ti
33d0: 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 cket #2741..*/.#
33e0: 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 if !defined(_XOP
33f0: 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64 EN_SOURCE) && !d
3400: 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f efined(__DARWIN_
3410: 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f _) && !defined(_
3420: 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
3430: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 ITE_THREADSAFE.#
3440: 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f define _XOPEN_
3450: 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e SOURCE 500 /* N
3460: 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 eeded to enable
3470: 70 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76 pthread recursiv
3480: 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e e mutexes */.#en
3490: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 dif../*.** The T
34a0: 43 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f CL headers are o
34b0: 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20 nly needed when
34c0: 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43 compiling the TC
34d0: 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 L bindings..*/.#
34e0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
34f0: 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 E_TCL) || define
3500: 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 d(TCLSH).# inclu
3510: 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 de <tcl.h>.#endi
3520: 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 f../*.** Many pe
3530: 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67 ople are failing
3540: 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47 to set -DNDEBUG
3550: 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e =1 when compilin
3560: 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 g SQLite..** Set
3570: 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 ting NDEBUG make
3580: 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c s the code small
3590: 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65 er and run faste
35a0: 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f r. So the follo
35b0: 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 wing.** lines ar
35c0: 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d e added to autom
35d0: 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 atically set NDE
35e0: 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d BUG unless the -
35f0: 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a DSQLITE_DEBUG=1.
3600: 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 ** option is set
3610: 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62 . Thus NDEBUG b
3620: 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e ecomes an opt-in
3630: 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20 rather than an
3640: 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 opt-out.** featu
3650: 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 re..*/.#if !defi
3660: 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 ned(NDEBUG) && !
3670: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
3680: 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 EBUG) .# define
3690: 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a NDEBUG 1.#endif.
36a0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 ./*.** The testc
36b0: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75 ase() macro is u
36c0: 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f sed to aid in co
36d0: 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20 verage testing.
36e0: 20 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 When .** doing
36f0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 coverage testing
3700: 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 , the condition
3710: 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d inside the argum
3720: 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 ent to.** testca
3730: 73 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61 se() must be eva
3740: 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65 luated both true
3750: 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 and false in or
3760: 64 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 der to.** get fu
3770: 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61 ll branch covera
3780: 67 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73 ge. The testcas
3790: 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 e() macro is ins
37a0: 65 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 erted.** to help
37b0: 20 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 ensure adequate
37c0: 20 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69 test coverage i
37d0: 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73 n places where s
37e0: 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 imple.** conditi
37f0: 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 on/decision cove
3800: 72 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 rage is inadequa
3810: 74 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 te. For example
3820: 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 , testcase().**
3830: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d can be used to m
3840: 61 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72 ake sure boundar
3850: 79 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73 y values are tes
3860: 74 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 ted. For.** bit
3870: 6d 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74 mask tests, test
3880: 63 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73 case() can be us
3890: 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ed to make sure
38a0: 65 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 each bit.** is s
38b0: 69 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75 ignificant and u
38c0: 73 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 sed at least onc
38d0: 65 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74 e. On switch st
38e0: 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 atements.** wher
38f0: 65 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73 e multiple cases
3900: 20 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 go to the same
3910: 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 block of code, t
3920: 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e estcase().** can
3930: 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c insure that all
3940: 20 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75 cases are evalu
3950: 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 ated..**.*/.#ifd
3960: 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 ef SQLITE_COVERA
3970: 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 GE_TEST.SQLITE_P
3980: 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
3990: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e lite3Coverage(in
39a0: 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73 t);.# define tes
39b0: 74 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20 tcase(X) if( X
39c0: 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 ){ sqlite3Covera
39d0: 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a ge(__LINE__); }.
39e0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 #else.# define t
39f0: 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 estcase(X).#endi
3a00: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 f../*.** The TES
3a10: 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 TONLY macro is u
3a20: 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 sed to enclose v
3a30: 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 ariable declarat
3a40: 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 ions or.** other
3a50: 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68 bits of code th
3a60: 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f at are needed to
3a70: 20 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67 support the arg
3a80: 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e uments.** within
3a90: 20 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20 testcase() and
3aa0: 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e assert() macros.
3ab0: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
3ac0: 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 (NDEBUG) || defi
3ad0: 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 ned(SQLITE_COVER
3ae0: 41 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69 AGE_TEST).# defi
3af0: 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 ne TESTONLY(X)
3b00: 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 X.#else.# define
3b10: 20 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e TESTONLY(X).#en
3b20: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 dif../*.** Somet
3b30: 69 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 imes we need a s
3b40: 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 mall amount of c
3b50: 6f 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 ode such as a va
3b60: 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a riable initializ
3b70: 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 ation.** to setu
3b80: 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 p for a later as
3b90: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
3ba0: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e . We do not wan
3bb0: 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a t this code to.*
3bc0: 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 * appear when as
3bd0: 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c sert() is disabl
3be0: 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ed. The followi
3bf0: 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 ng macro is ther
3c00: 65 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f efore.** used to
3c10: 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 contain that se
3c20: 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 tup code. The "
3c30: 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 VVA" acronym sta
3c40: 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 nds for.** "Veri
3c50: 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 fication, Valida
3c60: 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 tion, and Accred
3c70: 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 itation". In ot
3c80: 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a her words, the.*
3c90: 2a 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 * code within VV
3ca0: 41 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e A_ONLY() will on
3cb0: 6c 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 ly run during ve
3cc0: 72 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 rification proce
3cd0: 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 sses..*/.#ifndef
3ce0: 20 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 NDEBUG.# define
3cf0: 20 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a VVA_ONLY(X) X.
3d00: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 #else.# define V
3d10: 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 VA_ONLY(X).#endi
3d20: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 f../*.** The ALW
3d30: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 AYS and NEVER ma
3d40: 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f cros surround bo
3d50: 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e olean expression
3d60: 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 s which .** are
3d70: 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 intended to alwa
3d80: 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 ys be true or fa
3d90: 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c lse, respectivel
3da0: 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 y. Such.** expr
3db0: 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 essions could be
3dc0: 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 omitted from th
3dd0: 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c e code completel
3de0: 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 y. But they.**
3df0: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 are included in
3e00: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f a few cases in o
3e10: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 rder to enhance
3e20: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a the resilience.*
3e30: 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75 * of SQLite to u
3e40: 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69 nexpected behavi
3e50: 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 or - to make the
3e60: 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c code "self-heal
3e70: 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 ing".** or "duct
3e80: 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e ile" rather than
3e90: 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22 being "brittle"
3ea0: 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74 and crashing at
3eb0: 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 the first.** hi
3ec0: 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 nt of unplanned
3ed0: 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
3ee0: 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
3ef0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 ALWAYS and NEVER
3f00: 20 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64 are added for d
3f10: 65 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a efensive code..*
3f20: 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 *.** When doing
3f30: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 coverage testing
3f40: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 ALWAYS and NEVE
3f50: 52 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64 R are hard-coded
3f60: 20 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 to.** be true a
3f70: 6e 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74 nd false so that
3f80: 20 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65 the unreachable
3f90: 20 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69 code then speci
3fa0: 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 fy will.** not b
3fb0: 65 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 e counted as unt
3fc0: 65 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 ested code..*/.#
3fd0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
3fe0: 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 E_COVERAGE_TEST)
3ff0: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 .# define ALWAYS
4000: 28 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 (X) (1).# d
4010: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 efine NEVER(X)
4020: 20 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21 (0).#elif !
4030: 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a defined(NDEBUG).
4040: 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4050: 20 69 6e 74 20 73 71 6c 69 74 65 33 41 73 73 65 int sqlite3Asse
4060: 72 74 28 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 rt(void);.# defi
4070: 6e 65 20 41 4c 57 41 59 53 28 58 29 20 20 20 20 ne ALWAYS(X)
4080: 20 20 28 28 58 29 3f 31 3a 73 71 6c 69 74 65 33 ((X)?1:sqlite3
4090: 41 73 73 65 72 74 28 29 29 0a 23 20 64 65 66 69 Assert()).# defi
40a0: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20 ne NEVER(X)
40b0: 20 20 28 28 58 29 3f 73 71 6c 69 74 65 33 41 73 ((X)?sqlite3As
40c0: 73 65 72 74 28 29 3a 30 29 0a 23 65 6c 73 65 0a sert():0).#else.
40d0: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 # define ALWAYS(
40e0: 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 64 65 X) (X).# de
40f0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 fine NEVER(X)
4100: 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a (X).#endif..
4110: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 /*.** The macro
4120: 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 20 unlikely() is a
4130: 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f 75 hint that surrou
4140: 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a nds a boolean.**
4150: 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
4160: 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 is usually fals
4170: 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 e. Macro likely
4180: 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20 () surrounds.**
4190: 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 a boolean expres
41a0: 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 sion that is usu
41b0: 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43 20 ally true. GCC
41c0: 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73 is able to.** us
41d0: 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 74 6f e these hints to
41e0: 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 65 72 generate better
41f0: 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73 code, sometimes
4200: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
4210: 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a (__GNUC__) && 0.
4220: 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 # define likely(
4230: 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f X) __builtin_
4240: 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23 20 expect((X),1).#
4250: 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 define unlikely(
4260: 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 X) __builtin_ex
4270: 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c 73 pect((X),0).#els
4280: 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c e.# define likel
4290: 79 28 58 29 20 20 20 20 21 21 28 58 29 0a 23 20 y(X) !!(X).#
42a0: 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 define unlikely(
42b0: 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69 66 X) !!(X).#endif
42c0: 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
42d0: 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 * Include sqlite
42e0: 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 3.h in the middl
42f0: 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
4300: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
4310: 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4320: 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c * Begin file sql
4330: 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ite3.h *********
4340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4360: 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
4370: 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
4380: 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
4390: 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
43a0: 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
43b0: 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
43c0: 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
43d0: 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
43e0: 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
43f0: 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
4400: 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
4410: 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
4420: 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
4430: 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
4440: 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
4450: 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
4460: 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
4470: 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
4480: 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
4490: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
44d0: 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
44e0: 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 header file def
44f0: 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 ines the interfa
4500: 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 69 ce that the SQLi
4510: 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70 72 te library.** pr
4520: 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e 74 esents to client
4530: 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20 61 programs. If a
4540: 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74 72 C-function, str
4550: 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70 65 ucture, datatype
4560: 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e 74 ,.** or constant
4570: 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65 73 definition does
4580: 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 74 not appear in t
4590: 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 his file, then i
45a0: 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 75 t is.** not a pu
45b0: 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20 53 blished API of S
45c0: 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65 63 QLite, is subjec
45d0: 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 t to change with
45e0: 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20 61 out.** notice, a
45f0: 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 nd should not be
4600: 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 70 referenced by p
4610: 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73 65 rograms that use
4620: 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 SQLite..**.** S
4630: 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69 6e ome of the defin
4640: 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 itions that are
4650: 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72 65 in this file are
4660: 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22 65 marked as.** "e
4670: 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 45 xperimental". E
4680: 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 xperimental inte
4690: 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d 61 rfaces are norma
46a0: 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74 75 lly new.** featu
46b0: 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 64 res recently add
46c0: 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 57 ed to SQLite. W
46d0: 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 70 e do not anticip
46e0: 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 ate changes.** t
46f0: 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 o experimental i
4700: 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72 65 nterfaces but re
4710: 73 65 72 76 65 20 74 6f 20 6d 61 6b 65 20 6d 69 serve to make mi
4720: 6e 6f 72 20 63 68 61 6e 67 65 73 20 69 66 0a 2a nor changes if.*
4730: 2a 20 65 78 70 65 72 69 65 6e 63 65 20 66 72 6f * experience fro
4740: 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 77 69 m use "in the wi
4750: 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 63 68 ld" suggest such
4760: 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 72 75 changes are pru
4770: 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 dent..**.** The
4780: 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 75 official C-langu
4790: 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 6e 74 age API document
47a0: 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 65 ation for SQLite
47b0: 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66 is derived.** f
47c0: 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 rom comments in
47d0: 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 69 73 this file. This
47e0: 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 75 74 file is the aut
47f0: 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 72 63 horitative sourc
4800: 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c 69 e.** on how SQLi
4810: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 te interfaces ar
4820: 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 65 e suppose to ope
4830: 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rate..**.** The
4840: 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c name of this fil
4850: 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 75 72 e under configur
4860: 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 ation management
4870: 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e 69 6e is "sqlite.h.in
4880: 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 "..** The makefi
4890: 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d 69 le makes some mi
48a0: 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74 nor changes to t
48b0: 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 20 61 his file (such a
48c0: 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20 74 s inserting.** t
48d0: 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 he version numbe
48e0: 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 20 69 r) and changes i
48f0: 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c 69 ts name to "sqli
4900: 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61 72 te3.h" as.** par
4910: 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 20 70 t of the build p
4920: 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 rocess..**.** @(
4930: 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65 2e 68 #) $Id: sqlite.h
4940: 2e 69 6e 2c 76 20 31 2e 34 34 37 20 32 30 30 39 .in,v 1.447 2009
4950: 2f 30 34 2f 33 30 20 31 35 3a 35 39 3a 35 36 20 /04/30 15:59:56
4960: 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 drh Exp $.*/.#if
4970: 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f 48 5f ndef _SQLITE3_H_
4980: 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
4990: 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 3_H_.#include <s
49a0: 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a 20 tdarg.h> /*
49b0: 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64 Needed for the d
49c0: 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61 5f efinition of va_
49d0: 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d list */../*.** M
49e0: 61 6b 65 20 73 75 72 65 20 77 65 20 63 61 6e 20 ake sure we can
49f0: 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66 66 20 call this stuff
4a00: 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 from C++..*/.#if
4a10: 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 0.extern "C" {.
4a20: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 #endif.../*.** A
4a30: 64 64 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 dd the ability t
4a40: 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78 74 65 o override 'exte
4a50: 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 rn'.*/.#ifndef S
4a60: 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23 20 64 QLITE_EXTERN.# d
4a70: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 efine SQLITE_EXT
4a80: 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64 69 ERN extern.#endi
4a90: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6e f../*.** These n
4aa0: 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 20 o-op macros are
4ab0: 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f 66 used in front of
4ac0: 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20 6d interfaces to m
4ad0: 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e 74 ark those.** int
4ae0: 65 72 66 61 63 65 73 20 61 73 20 65 69 74 68 65 erfaces as eithe
4af0: 72 20 64 65 70 72 65 63 61 74 65 64 20 6f 72 20 r deprecated or
4b00: 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 4e experimental. N
4b10: 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a ew applications.
4b20: 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 ** should not us
4b30: 65 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74 e deprecated int
4b40: 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 61 72 rfaces - they ar
4b50: 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62 61 e support for ba
4b60: 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 ckwards.** compa
4b70: 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 20 tibility only.
4b80: 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69 74 Application writ
4b90: 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61 77 ers should be aw
4ba0: 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70 65 are that.** expe
4bb0: 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 rimental interfa
4bc0: 63 65 73 20 61 72 65 20 73 75 62 6a 65 63 74 20 ces are subject
4bd0: 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f 69 to change in poi
4be0: 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a nt releases..**.
4bf0: 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 ** These macros
4c00: 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 used to resolve
4c10: 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 to various kinds
4c20: 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67 of compiler mag
4c30: 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c 64 ic that.** would
4c40: 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69 6e generate warnin
4c50: 67 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20 g messages when
4c60: 74 68 65 79 20 77 65 72 65 20 75 73 65 64 2e 20 they were used.
4c70: 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f 6d But that.** com
4c80: 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64 65 piler magic ende
4c90: 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67 20 d up generating
4ca0: 73 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f 66 such a flurry of
4cb0: 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a 20 bug reports.**
4cc0: 74 68 61 74 20 77 65 20 68 61 76 65 20 74 61 6b that we have tak
4cd0: 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61 6e en it all out an
4ce0: 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20 75 d gone back to u
4cf0: 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 6e sing simple.** n
4d00: 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 oop macros..*/.#
4d10: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
4d20: 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 PRECATED.#define
4d30: 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
4d40: 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 NTAL../*.** Ensu
4d50: 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c 73 re these symbols
4d60: 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e 65 were not define
4d70: 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69 6f d by some previo
4d80: 75 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a us header file..
4d90: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
4da0: 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66 _VERSION.# undef
4db0: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a SQLITE_VERSION.
4dc0: 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
4dd0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d LITE_VERSION_NUM
4de0: 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 BER.# undef SQLI
4df0: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 TE_VERSION_NUMBE
4e00: 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 R.#endif../*.**
4e10: 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c CAPI3REF: Compil
4e20: 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 e-Time Library V
4e30: 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b ersion Numbers {
4e40: 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30 3e H10010} <S60100>
4e50: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 .**.** The SQLIT
4e60: 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53 51 E_VERSION and SQ
4e70: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d LITE_VERSION_NUM
4e80: 42 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e 0a BER #defines in.
4e90: 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e 68 ** the sqlite3.h
4ea0: 20 66 69 6c 65 20 73 70 65 63 69 66 79 20 74 68 file specify th
4eb0: 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c e version of SQL
4ec0: 69 74 65 20 77 69 74 68 20 77 68 69 63 68 0a 2a ite with which.*
4ed0: 2a 20 74 68 61 74 20 68 65 61 64 65 72 20 66 69 * that header fi
4ee0: 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 le is associated
4ef0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65 72 ..**.** The "ver
4f00: 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65 20 sion" of SQLite
4f10: 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20 74 is a string of t
4f20: 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e 5a 22 2e he form "X.Y.Z".
4f30: 0a 2a 2a 20 54 68 65 20 70 68 72 61 73 65 20 22 .** The phrase "
4f40: 61 6c 70 68 61 22 20 6f 72 20 22 62 65 74 61 22 alpha" or "beta"
4f50: 20 6d 69 67 68 74 20 62 65 20 61 70 70 65 6e 64 might be append
4f60: 65 64 20 61 66 74 65 72 20 74 68 65 20 5a 2e 0a ed after the Z..
4f70: 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 65 20 69 ** The X value i
4f80: 73 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f 6e 20 s major version
4f90: 6e 75 6d 62 65 72 20 61 6c 77 61 79 73 20 33 20 number always 3
4fa0: 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 in SQLite3..** T
4fb0: 68 65 20 58 20 76 61 6c 75 65 20 6f 6e 6c 79 20 he X value only
4fc0: 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63 changes when bac
4fd0: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 kwards compatibi
4fe0: 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 lity is.** broke
4ff0: 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 n and we intend
5000: 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62 to never break b
5010: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
5020: 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 59 bility..** The Y
5030: 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69 value is the mi
5040: 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 nor version numb
5050: 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e er and only chan
5060: 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 ges when.** ther
5070: 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 e are major feat
5080: 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 ure enhancements
5090: 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72 that are forwar
50a0: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a ds compatible.**
50b0: 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 but not backwar
50c0: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a ds compatible..*
50d0: 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20 69 73 * The Z value is
50e0: 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d the release num
50f0: 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65 ber and is incre
5100: 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 mented with.** e
5110: 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20 ach release but
5120: 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30 resets back to 0
5130: 20 77 68 65 6e 65 76 65 72 20 59 20 69 73 20 69 whenever Y is i
5140: 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a ncremented..**.*
5150: 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
5160: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 ite3_libversion(
5170: 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
5180: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 libversion_numbe
5190: 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 r()]..**.** Requ
51a0: 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 30 31 irements: [H1001
51b0: 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f 0a 23 1] [H10014].*/.#
51c0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 define SQLITE_VE
51d0: 52 53 49 4f 4e 20 20 20 20 20 20 20 20 20 22 33 RSION "3
51e0: 2e 36 2e 31 34 22 0a 23 64 65 66 69 6e 65 20 53 .6.14".#define S
51f0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
5200: 4d 42 45 52 20 20 33 30 30 36 30 31 34 0a 0a 2f MBER 3006014../
5210: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
5220: 75 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 un-Time Library
5230: 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 Version Numbers
5240: 7b 48 31 30 30 32 30 7d 20 3c 53 36 30 31 30 30 {H10020} <S60100
5250: 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 >.** KEYWORDS: s
5260: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a qlite3_version.*
5270: 2a 0a 2a 2a 20 54 68 65 73 65 20 66 65 61 74 75 *.** These featu
5280: 72 65 73 20 70 72 6f 76 69 64 65 20 74 68 65 20 res provide the
5290: 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e same information
52a0: 20 61 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f as the [SQLITE_
52b0: 56 45 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 VERSION].** and
52c0: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f [SQLITE_VERSION_
52d0: 4e 55 4d 42 45 52 5d 20 23 64 65 66 69 6e 65 73 NUMBER] #defines
52e0: 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2c 20 in the header,
52f0: 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 but are associat
5300: 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6c ed.** with the l
5310: 69 62 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f ibrary instead o
5320: 66 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c f the header fil
5330: 65 2e 20 20 43 61 75 74 69 6f 75 73 20 70 72 6f e. Cautious pro
5340: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 0a 2a grammers might.*
5350: 2a 20 69 6e 63 6c 75 64 65 20 61 20 63 68 65 63 * include a chec
5360: 6b 20 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 k in their appli
5370: 63 61 74 69 6f 6e 20 74 6f 20 76 65 72 69 66 79 cation to verify
5380: 20 74 68 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 that.** sqlite3
5390: 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 _libversion_numb
53a0: 65 72 28 29 20 61 6c 77 61 79 73 20 72 65 74 75 er() always retu
53b0: 72 6e 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a rns the value.**
53c0: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e [SQLITE_VERSION
53d0: 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 _NUMBER]..**.**
53e0: 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 The sqlite3_libv
53f0: 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f ersion() functio
5400: 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 n returns the sa
5410: 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 me information a
5420: 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 s is.** in the s
5430: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d qlite3_version[]
5440: 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 string constant
5450: 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 . The function
5460: 69 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66 is provided.** f
5470: 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c 73 20 73 or use in DLLs s
5480: 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 73 20 75 ince DLL users u
5490: 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 61 sually do not ha
54a0: 76 65 20 64 69 72 65 63 74 20 61 63 63 65 73 73 ve direct access
54b0: 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f to string.** co
54c0: 6e 73 74 61 6e 74 73 20 77 69 74 68 69 6e 20 74 nstants within t
54d0: 68 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 he DLL..**.** Re
54e0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
54f0: 30 32 31 5d 20 5b 48 31 30 30 32 32 5d 20 5b 48 021] [H10022] [H
5500: 31 30 30 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 10023].*/.SQLITE
5510: 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
5520: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b sqlite3_version[
5530: 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49 ] = SQLITE_VERSI
5540: 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 ON;.SQLITE_API c
5550: 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
5560: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f e3_libversion(vo
5570: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
5580: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 int sqlite3_libv
5590: 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f ersion_number(vo
55a0: 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 id);../*.** CAPI
55b0: 33 52 45 46 3a 20 54 65 73 74 20 54 6f 20 53 65 3REF: Test To Se
55c0: 65 20 49 66 20 54 68 65 20 4c 69 62 72 61 72 79 e If The Library
55d0: 20 49 73 20 54 68 72 65 61 64 73 61 66 65 20 7b Is Threadsafe {
55e0: 48 31 30 31 30 30 7d 20 3c 53 36 30 31 30 30 3e H10100} <S60100>
55f0: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 .**.** SQLite ca
5600: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 n be compiled wi
5610: 74 68 20 6f 72 20 77 69 74 68 6f 75 74 20 6d 75 th or without mu
5620: 74 65 78 65 73 2e 20 20 57 68 65 6e 0a 2a 2a 20 texes. When.**
5630: 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 the [SQLITE_THRE
5640: 41 44 53 41 46 45 5d 20 43 20 70 72 65 70 72 6f ADSAFE] C prepro
5650: 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 31 20 6f cessor macro 1 o
5660: 72 20 32 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 r 2, mutexes.**
5670: 61 72 65 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 are enabled and
5680: 53 51 4c 69 74 65 20 69 73 20 74 68 72 65 61 64 SQLite is thread
5690: 73 61 66 65 2e 20 20 57 68 65 6e 20 74 68 65 0a safe. When the.
56a0: 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 ** [SQLITE_THREA
56b0: 44 53 41 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 DSAFE] macro is
56c0: 30 2c 20 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 0, .** the mutex
56d0: 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 es are omitted.
56e0: 20 57 69 74 68 6f 75 74 20 74 68 65 20 6d 75 74 Without the mut
56f0: 65 78 65 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 exes, it is not
5700: 73 61 66 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 safe.** to use S
5710: 51 4c 69 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 QLite concurrent
5720: 6c 79 20 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 ly from more tha
5730: 6e 20 6f 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a n one thread..**
5740: 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 .** Enabling mut
5750: 65 78 65 73 20 69 6e 63 75 72 73 20 61 20 6d 65 exes incurs a me
5760: 61 73 75 72 61 62 6c 65 20 70 65 72 66 6f 72 6d asurable perform
5770: 61 6e 63 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a ance penalty..**
5780: 20 53 6f 20 69 66 20 73 70 65 65 64 20 69 73 20 So if speed is
5790: 6f 66 20 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 of utmost import
57a0: 61 6e 63 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 ance, it makes s
57b0: 65 6e 73 65 20 74 6f 20 64 69 73 61 62 6c 65 0a ense to disable.
57c0: 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 2e 20 ** the mutexes.
57d0: 20 42 75 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d But for maximum
57e0: 20 73 61 66 65 74 79 2c 20 6d 75 74 65 78 65 73 safety, mutexes
57f0: 20 73 68 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c should be enabl
5800: 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 ed..** The defau
5810: 6c 74 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 lt behavior is f
5820: 6f 72 20 6d 75 74 65 78 65 73 20 74 6f 20 62 65 or mutexes to be
5830: 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 enabled..**.**
5840: 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 This interface c
5850: 61 6e 20 62 65 20 75 73 65 64 20 62 79 20 61 20 an be used by a
5860: 70 72 6f 67 72 61 6d 20 74 6f 20 6d 61 6b 65 20 program to make
5870: 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a sure that the.**
5880: 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 version of SQLi
5890: 74 65 20 74 68 61 74 20 69 74 20 69 73 20 6c 69 te that it is li
58a0: 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20 77 61 nking against wa
58b0: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a s compiled with.
58c0: 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64 20 73 ** the desired s
58d0: 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 5b 53 etting of the [S
58e0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
58f0: 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 ] macro..**.** T
5900: 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6f 6e his interface on
5910: 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20 74 68 ly reports on th
5920: 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d e compile-time m
5930: 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20 utex setting.**
5940: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 of the [SQLITE_T
5950: 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61 67 2e HREADSAFE] flag.
5960: 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 If SQLite is c
5970: 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 ompiled with.**
5980: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
5990: 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78 65 73 E=1 then mutexes
59a0: 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62 79 20 are enabled by
59b0: 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a 20 63 default but.** c
59c0: 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72 20 70 an be fully or p
59d0: 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62 6c 65 artially disable
59e0: 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 d using a call t
59f0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 o [sqlite3_confi
5a00: 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74 68 65 g()].** with the
5a10: 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45 5f 43 verbs [SQLITE_C
5a20: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
5a30: 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 4e AD], [SQLITE_CON
5a40: 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d FIG_MULTITHREAD]
5a50: 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f ,.** or [SQLITE_
5a60: 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e 20 20 CONFIG_MUTEX].
5a70: 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
5a80: 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
5a90: 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c 79 20 n shows.** only
5aa0: 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6d 70 the default comp
5ab0: 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69 6e 67 ile-time setting
5ac0: 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d 74 69 , not any run-ti
5ad0: 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f me changes.** to
5ae0: 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e 0a 2a that setting..*
5af0: 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68 *.** See the [th
5b00: 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f reading mode] do
5b10: 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 cumentation for
5b20: 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
5b30: 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 mation..**.** Re
5b40: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
5b50: 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a 2a 2f 101] [H10102].*/
5b60: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
5b70: 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 sqlite3_threadsa
5b80: 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a fe(void);../*.**
5b90: 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62 CAPI3REF: Datab
5ba0: 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 48 ase Connection H
5bb0: 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d 20 3c andle {H12000} <
5bc0: 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S40200>.** KEYWO
5bd0: 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65 20 63 RDS: {database c
5be0: 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61 74 61 onnection} {data
5bf0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
5c00: 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65 }.**.** Each ope
5c10: 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 n SQLite databas
5c20: 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 e is represented
5c30: 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f by a pointer to
5c40: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a an instance of.
5c50: 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20 73 74 ** the opaque st
5c60: 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 73 ructure named "s
5c70: 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69 73 20 qlite3". It is
5c80: 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e 6b 20 useful to think
5c90: 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a 2a 2a of an sqlite3.**
5ca0: 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e 20 6f pointer as an o
5cb0: 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 71 6c bject. The [sql
5cc0: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 ite3_open()], [s
5cd0: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
5ce0: 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 , and.** [sqlite
5cf0: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 3_open_v2()] int
5d00: 65 72 66 61 63 65 73 20 61 72 65 20 69 74 73 20 erfaces are its
5d10: 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20 61 6e constructors, an
5d20: 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 d [sqlite3_close
5d30: 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20 64 65 ()].** is its de
5d40: 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65 72 65 structor. There
5d50: 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65 72 20 are many other
5d60: 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 63 68 interfaces (such
5d70: 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f as.** [sqlite3_
5d80: 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b prepare_v2()], [
5d90: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
5da0: 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e 64 0a unction()], and.
5db0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 ** [sqlite3_busy
5dc0: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f 20 6e _timeout()] to n
5dd0: 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 20 74 ame but three) t
5de0: 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20 hat are methods
5df0: 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 on an.** sqlite3
5e00: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 object..*/.type
5e10: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
5e20: 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f 2a 0a e3 sqlite3;../*.
5e30: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 34 2d ** CAPI3REF: 64-
5e40: 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 70 65 Bit Integer Type
5e50: 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31 30 31 s {H10200} <S101
5e60: 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 10>.** KEYWORDS:
5e70: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 sqlite_int64 sq
5e80: 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a 0a 2a lite_uint64.**.*
5e90: 2a 20 42 65 63 61 75 73 65 20 74 68 65 72 65 20 * Because there
5ea0: 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c 61 74 is no cross-plat
5eb0: 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70 65 63 form way to spec
5ec0: 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74 65 67 ify 64-bit integ
5ed0: 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51 4c 69 er types.** SQLi
5ee0: 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79 70 65 te includes type
5ef0: 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69 74 20 defs for 64-bit
5f00: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 signed and unsig
5f10: 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a ned integers..**
5f20: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
5f30: 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 int64 and sqlite
5f40: 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74 68 65 3_uint64 are the
5f50: 20 70 72 65 66 65 72 72 65 64 20 74 79 70 65 20 preferred type
5f60: 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 20 definitions..**
5f70: 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 The sqlite_int64
5f80: 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74 and sqlite_uint
5f90: 36 34 20 74 79 70 65 73 20 61 72 65 20 73 75 70 64 types are sup
5fa0: 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63 6b 77 ported for backw
5fb0: 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 ards.** compatib
5fc0: 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a ility only..**.*
5fd0: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
5fe0: 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32 30 32 [H10201] [H10202
5ff0: 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ].*/.#ifdef SQLI
6000: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 20 TE_INT64_TYPE.
6010: 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f 49 typedef SQLITE_I
6020: 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65 NT64_TYPE sqlite
6030: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 _int64;. typede
6040: 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 54 f unsigned SQLIT
6050: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c E_INT64_TYPE sql
6060: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 69 ite_uint64;.#eli
6070: 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 f defined(_MSC_V
6080: 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f ER) || defined(_
6090: 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 74 _BORLANDC__). t
60a0: 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 73 ypedef __int64 s
60b0: 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 qlite_int64;. t
60c0: 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 ypedef unsigned
60d0: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 __int64 sqlite_u
60e0: 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 74 int64;.#else. t
60f0: 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67 ypedef long long
6100: 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 int sqlite_int6
6110: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 4;. typedef uns
6120: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
6130: 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 int sqlite_uint6
6140: 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 4;.#endif.typede
6150: 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 f sqlite_int64 s
6160: 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 79 qlite3_int64;.ty
6170: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e pedef sqlite_uin
6180: 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 t64 sqlite3_uint
6190: 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 64;../*.** If co
61a0: 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 mpiling for a pr
61b0: 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63 ocessor that lac
61c0: 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e ks floating poin
61d0: 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 t support,.** su
61e0: 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72 bstitute integer
61f0: 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f for floating-po
6200: 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 int..*/.#ifdef S
6210: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
6220: 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 ING_POINT.# defi
6230: 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 ne double sqlite
6240: 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a 0a 3_int64.#endif..
6250: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
6260: 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62 61 Closing A Databa
6270: 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 se Connection {H
6280: 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 3e 3c 12010} <S30100><
6290: 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40200>.**.** Th
62a0: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
62b0: 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 e destructor for
62c0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f the [sqlite3] o
62d0: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 bject..**.** App
62e0: 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 lications should
62f0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
6300: 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61 ze | finalize] a
6310: 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ll [prepared sta
6320: 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20 tements].** and
6330: 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c [sqlite3_blob_cl
6340: 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c ose | close] all
6350: 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20 [BLOB handles]
6360: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a associated with.
6370: 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d ** the [sqlite3]
6380: 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f object prior to
6390: 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63 attempting to c
63a0: 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e lose the object.
63b0: 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
63c0: 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20 69 6e _next_stmt()] in
63d0: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 terface can be u
63e0: 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 61 6c sed to locate al
63f0: 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 l.** [prepared s
6400: 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73 6f 63 tatements] assoc
6410: 69 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 iated with a [da
6420: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
6430: 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e 0a 2a n] if desired..*
6440: 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65 20 6d * Typical code m
6450: 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74 ight look like t
6460: 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 his:.**.** <bloc
6470: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 kquote><pre>.**
6480: 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
6490: 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28 20 28 tmt;.** while( (
64a0: 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 5f pStmt = sqlite3_
64b0: 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20 30 29 next_stmt(db, 0)
64c0: 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62 73 70 )!=0 ){.**  
64d0: 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 ; sqlite3_fina
64e0: 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a 2a 20 lize(pStmt);.**
64f0: 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f }.** </pre></blo
6500: 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 49 ckquote>.**.** I
6510: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 f [sqlite3_close
6520: 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 ()] is invoked w
6530: 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69 hile a transacti
6540: 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a 20 74 on is open,.** t
6550: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
6560: 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 s automatically
6570: 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
6580: 2a 2a 20 54 68 65 20 43 20 70 61 72 61 6d 65 74 ** The C paramet
6590: 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 er to [sqlite3_c
65a0: 6c 6f 73 65 28 43 29 5d 20 6d 75 73 74 20 62 65 lose(C)] must be
65b0: 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 0a 2a either a NULL.*
65c0: 2a 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20 * pointer or an
65d0: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 [sqlite3] object
65e0: 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e 65 pointer obtaine
65f0: 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 d.** from [sqlit
6600: 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c e3_open()], [sql
6610: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 ite3_open16()],
6620: 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f or.** [sqlite3_o
6630: 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 6e pen_v2()], and n
6640: 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c ot previously cl
6650: 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 osed..**.** Requ
6660: 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
6670: 32 30 31 31 5d 20 5b 48 31 32 30 31 32 5d 20 5b 2011] [H12012] [
6680: 48 31 32 30 31 33 5d 20 5b 48 31 32 30 31 34 5d H12013] [H12014]
6690: 20 5b 48 31 32 30 31 35 5d 20 5b 48 31 32 30 31 [H12015] [H1201
66a0: 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 9].*/.SQLITE_API
66b0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 6f int sqlite3_clo
66c0: 73 65 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 0a se(sqlite3 *);..
66d0: 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 66 /*.** The type f
66e0: 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 or a callback fu
66f0: 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 nction..** This
6700: 69 73 20 6c 65 67 61 63 79 20 61 6e 64 20 64 65 is legacy and de
6710: 70 72 65 63 61 74 65 64 2e 20 20 49 74 20 69 73 precated. It is
6720: 20 69 6e 63 6c 75 64 65 64 20 66 6f 72 20 68 69 included for hi
6730: 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f 6d 70 storical.** comp
6740: 61 74 69 62 69 6c 69 74 79 20 61 6e 64 20 69 73 atibility and is
6750: 20 6e 6f 74 20 64 6f 63 75 6d 65 6e 74 65 64 2e not documented.
6760: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 69 6e 74 20 .*/.typedef int
6770: 28 2a 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 (*sqlite3_callba
6780: 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 ck)(void*,int,ch
6790: 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a 0a ar**, char**);..
67a0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
67b0: 4f 6e 65 2d 53 74 65 70 20 51 75 65 72 79 20 45 One-Step Query E
67c0: 78 65 63 75 74 69 6f 6e 20 49 6e 74 65 72 66 61 xecution Interfa
67d0: 63 65 20 7b 48 31 32 31 30 30 7d 20 3c 53 31 30 ce {H12100} <S10
67e0: 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 000>.**.** The s
67f0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e qlite3_exec() in
6800: 74 65 72 66 61 63 65 20 69 73 20 61 20 63 6f 6e terface is a con
6810: 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66 20 72 venient way of r
6820: 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f 72 20 6d 6f unning one or mo
6830: 72 65 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d re.** SQL statem
6840: 65 6e 74 73 20 77 69 74 68 6f 75 74 20 68 61 76 ents without hav
6850: 69 6e 67 20 74 6f 20 77 72 69 74 65 20 61 20 6c ing to write a l
6860: 6f 74 20 6f 66 20 43 20 63 6f 64 65 2e 20 20 54 ot of C code. T
6870: 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 he UTF-8 encoded
6880: 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e .** SQL statemen
6890: 74 73 20 61 72 65 20 70 61 73 73 65 64 20 69 6e ts are passed in
68a0: 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 as the second p
68b0: 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
68c0: 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a 20 54 te3_exec()..** T
68d0: 68 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 he statements ar
68e0: 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e 65 20 e evaluated one
68f0: 62 79 20 6f 6e 65 20 75 6e 74 69 6c 20 65 69 74 by one until eit
6900: 68 65 72 20 61 6e 20 65 72 72 6f 72 20 6f 72 0a her an error or.
6910: 2a 2a 20 61 6e 20 69 6e 74 65 72 72 75 70 74 20 ** an interrupt
6920: 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 is encountered,
6930: 6f 72 20 75 6e 74 69 6c 20 74 68 65 79 20 61 72 or until they ar
6940: 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 20 54 68 65 e all done. The
6950: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 3rd parameter.*
6960: 2a 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c * is an optional
6970: 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 69 callback that i
6980: 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 s invoked once f
6990: 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 61 or each row of a
69a0: 6e 79 20 71 75 65 72 79 0a 2a 2a 20 72 65 73 75 ny query.** resu
69b0: 6c 74 73 20 70 72 6f 64 75 63 65 64 20 62 79 20 lts produced by
69c0: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
69d0: 74 73 2e 20 20 54 68 65 20 35 74 68 20 70 61 72 ts. The 5th par
69e0: 61 6d 65 74 65 72 20 74 65 6c 6c 73 20 77 68 65 ameter tells whe
69f0: 72 65 0a 2a 2a 20 74 6f 20 77 72 69 74 65 20 61 re.** to write a
6a00: 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ny error message
6a10: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 s..**.** The err
6a20: 6f 72 20 6d 65 73 73 61 67 65 20 70 61 73 73 65 or message passe
6a30: 64 20 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 d back through t
6a40: 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 he 5th parameter
6a50: 20 69 73 20 68 65 6c 64 0a 2a 2a 20 69 6e 20 6d is held.** in m
6a60: 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
6a70: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
6a80: 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 61 76 6f 69 loc()]. To avoi
6a90: 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2c d a memory leak,
6aa0: 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 .** the calling
6ab0: 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 application shou
6ac0: 6c 64 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 ld call [sqlite3
6ad0: 5f 66 72 65 65 28 29 5d 20 6f 6e 20 61 6e 79 20 _free()] on any
6ae0: 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 error.** message
6af0: 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f 75 67 returned throug
6b00: 68 20 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 h the 5th parame
6b10: 74 65 72 20 77 68 65 6e 20 69 74 20 68 61 73 20 ter when it has
6b20: 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 0a 2a finished using.*
6b30: 2a 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 * the error mess
6b40: 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 age..**.** If th
6b50: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
6b60: 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d in the 2nd param
6b70: 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 6f 72 20 eter is NULL or
6b80: 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 0a an empty string.
6b90: 2a 2a 20 6f 72 20 61 20 73 74 72 69 6e 67 20 63 ** or a string c
6ba0: 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e 6c 79 20 77 ontaining only w
6bb0: 68 69 74 65 73 70 61 63 65 20 61 6e 64 20 63 6f hitespace and co
6bc0: 6d 6d 65 6e 74 73 2c 20 74 68 65 6e 20 6e 6f 20 mments, then no
6bd0: 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 SQL.** statement
6be0: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 s are evaluated
6bf0: 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
6c00: 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e is not changed.
6c10: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
6c20: 65 33 5f 65 78 65 63 28 29 20 69 6e 74 65 72 66 e3_exec() interf
6c30: 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 ace is implement
6c40: 65 64 20 69 6e 20 74 65 72 6d 73 20 6f 66 0a 2a ed in terms of.*
6c50: 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
6c60: 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 re_v2()], [sqlit
6c70: 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 e3_step()], and
6c80: 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
6c90: 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c e()]..** The sql
6ca0: 69 74 65 33 5f 65 78 65 63 28 29 20 72 6f 75 74 ite3_exec() rout
6cb0: 69 6e 65 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 ine does nothing
6cc0: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
6cd0: 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 that cannot be
6ce0: 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 done.** by [sqli
6cf0: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
6d00: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 ], [sqlite3_step
6d10: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
6d20: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 3_finalize()]..*
6d30: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 *.** The first p
6d40: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
6d50: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6d 75 73 ite3_exec()] mus
6d60: 74 20 62 65 20 61 6e 20 76 61 6c 69 64 20 61 6e t be an valid an
6d70: 64 20 6f 70 65 6e 0a 2a 2a 20 5b 64 61 74 61 62 d open.** [datab
6d80: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
6d90: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 .**.** The datab
6da0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d ase connection m
6db0: 75 73 74 20 6e 6f 74 20 62 65 20 63 6c 6f 73 65 ust not be close
6dc0: 64 20 77 68 69 6c 65 0a 2a 2a 20 5b 73 71 6c 69 d while.** [sqli
6dd0: 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72 te3_exec()] is r
6de0: 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 unning..**.** Th
6df0: 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
6e00: 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 5b 73 on should use [s
6e10: 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 74 qlite3_free()] t
6e20: 6f 20 66 72 65 65 0a 2a 2a 20 74 68 65 20 6d 65 o free.** the me
6e30: 6d 6f 72 79 20 74 68 61 74 20 2a 65 72 72 6d 73 mory that *errms
6e40: 67 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 g is left pointi
6e50: 6e 67 20 61 74 20 6f 6e 63 65 20 74 68 65 20 65 ng at once the e
6e60: 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 rror.** message
6e70: 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 is no longer nee
6e80: 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 ded..**.** The S
6e90: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 QL statement tex
6ea0: 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72 t in the 2nd par
6eb0: 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
6ec0: 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 6d 75 e3_exec()].** mu
6ed0: 73 74 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e st remain unchan
6ee0: 67 65 64 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 ged while [sqlit
6ef0: 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72 75 e3_exec()] is ru
6f00: 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 nning..**.** Req
6f10: 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
6f20: 31 32 31 30 31 5d 20 5b 48 31 32 31 30 32 5d 20 12101] [H12102]
6f30: 5b 48 31 32 31 30 34 5d 20 5b 48 31 32 31 30 35 [H12104] [H12105
6f40: 5d 20 5b 48 31 32 31 30 37 5d 20 5b 48 31 32 31 ] [H12107] [H121
6f50: 31 30 5d 20 5b 48 31 32 31 31 33 5d 20 5b 48 31 10] [H12113] [H1
6f60: 32 31 31 36 5d 0a 2a 2a 20 5b 48 31 32 31 31 39 2116].** [H12119
6f70: 5d 20 5b 48 31 32 31 32 32 5d 20 5b 48 31 32 31 ] [H12122] [H121
6f80: 32 35 5d 20 5b 48 31 32 31 33 31 5d 20 5b 48 31 25] [H12131] [H1
6f90: 32 31 33 34 5d 20 5b 48 31 32 31 33 37 5d 20 5b 2134] [H12137] [
6fa0: 48 31 32 31 33 38 5d 0a 2a 2f 0a 53 51 4c 49 54 H12138].*/.SQLIT
6fb0: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
6fc0: 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65 3_exec(. sqlite
6fd0: 33 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 3*,
6fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ff0: 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 /* An open
7000: 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f database */. co
7010: 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20 nst char *sql,
7020: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7030: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 /* SQL
7040: 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 to be evaluated
7050: 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62 */. int (*callb
7060: 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 ack)(void*,int,c
7070: 68 61 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 har**,char**),
7080: 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 /* Callback func
7090: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a tion */. void *
70a0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
70b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70c0: 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 /* 1st argu
70d0: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b ment to callback
70e0: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65 72 72 */. char **err
70f0: 6d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20 msg
7100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7110: 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 /* Error msg wr
7120: 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b itten here */.);
7130: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
7140: 3a 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b : Result Codes {
7150: 48 31 30 32 31 30 7d 20 3c 53 31 30 37 30 30 3e H10210} <S10700>
7160: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 .** KEYWORDS: SQ
7170: 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f 72 20 63 LITE_OK {error c
7180: 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63 6f 64 65 ode} {error code
7190: 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 s}.** KEYWORDS:
71a0: 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 72 {result code} {r
71b0: 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a esult codes}.**.
71c0: 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65 20 66 ** Many SQLite f
71d0: 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
71e0: 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c an integer resul
71f0: 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 t code from the
7200: 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 65 72 set shown.** her
7210: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e e in order to in
7220: 64 69 63 61 74 65 73 20 73 75 63 63 65 73 73 20 dicates success
7230: 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a or failure..**.*
7240: 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f 64 65 * New error code
7250: 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
7260: 6e 20 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e n future version
7270: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a s of SQLite..**.
7280: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 ** See also: [SQ
7290: 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 LITE_IOERR_READ
72a0: 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c | extended resul
72b0: 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 65 66 t codes].*/.#def
72c0: 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 ine SQLITE_OK
72d0: 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53 0 /* S
72e0: 75 63 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74 uccessful result
72f0: 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e 67 */./* beginning
7300: 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 -of-error-codes
7310: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7320: 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20 20 31 E_ERROR 1
7330: 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72 20 /* SQL error
7340: 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 or missing datab
7350: 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ase */.#define S
7360: 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20 QLITE_INTERNAL
7370: 20 20 20 32 20 20 20 2f 2a 20 49 6e 74 65 72 6e 2 /* Intern
7380: 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 al logic error i
7390: 6e 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65 66 n SQLite */.#def
73a0: 69 6e 65 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 ine SQLITE_PERM
73b0: 20 20 20 20 20 20 20 20 33 20 20 20 2f 2a 20 41 3 /* A
73c0: 63 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e ccess permission
73d0: 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 denied */.#defi
73e0: 6e 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 ne SQLITE_ABORT
73f0: 20 20 20 20 20 20 20 34 20 20 20 2f 2a 20 43 61 4 /* Ca
7400: 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 llback routine r
7410: 65 71 75 65 73 74 65 64 20 61 6e 20 61 62 6f 72 equested an abor
7420: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c t */.#define SQL
7430: 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20 20 ITE_BUSY
7440: 20 35 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 5 /* The data
7450: 62 61 73 65 20 66 69 6c 65 20 69 73 20 6c 6f 63 base file is loc
7460: 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ked */.#define S
7470: 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20 QLITE_LOCKED
7480: 20 20 20 36 20 20 20 2f 2a 20 41 20 74 61 62 6c 6 /* A tabl
7490: 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
74a0: 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 e is locked */.#
74b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f define SQLITE_NO
74c0: 4d 45 4d 20 20 20 20 20 20 20 20 37 20 20 20 2f MEM 7 /
74d0: 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 * A malloc() fai
74e0: 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 led */.#define S
74f0: 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 QLITE_READONLY
7500: 20 20 20 38 20 20 20 2f 2a 20 41 74 74 65 6d 70 8 /* Attemp
7510: 74 20 74 6f 20 77 72 69 74 65 20 61 20 72 65 61 t to write a rea
7520: 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a donly database *
7530: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7540: 5f 49 4e 54 45 52 52 55 50 54 20 20 20 20 39 20 _INTERRUPT 9
7550: 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74 /* Operation t
7560: 65 72 6d 69 6e 61 74 65 64 20 62 79 20 73 71 6c erminated by sql
7570: 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
7580: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7590: 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 31 30 E_IOERR 10
75a0: 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64 20 /* Some kind
75b0: 6f 66 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f of disk I/O erro
75c0: 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23 64 r occurred */.#d
75d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 efine SQLITE_COR
75e0: 52 55 50 54 20 20 20 20 20 31 31 20 20 20 2f 2a RUPT 11 /*
75f0: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 64 69 The database di
7600: 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 sk image is malf
7610: 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 ormed */.#define
7620: 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 SQLITE_NOTFOUND
7630: 20 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54 20 12 /* NOT
7640: 55 53 45 44 2e 20 54 61 62 6c 65 20 6f 72 20 72 USED. Table or r
7650: 65 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64 20 ecord not found
7660: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7670: 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 31 33 E_FULL 13
7680: 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e 20 /* Insertion
7690: 66 61 69 6c 65 64 20 62 65 63 61 75 73 65 20 64 failed because d
76a0: 61 74 61 62 61 73 65 20 69 73 20 66 75 6c 6c 20 atabase is full
76b0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
76c0: 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 31 34 E_CANTOPEN 14
76d0: 20 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 /* Unable to
76e0: 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 open the databas
76f0: 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e e file */.#defin
7700: 65 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f e SQLITE_PROTOCO
7710: 4c 20 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f 54 L 15 /* NOT
7720: 20 55 53 45 44 2e 20 44 61 74 61 62 61 73 65 20 USED. Database
7730: 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72 lock protocol er
7740: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ror */.#define S
7750: 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20 QLITE_EMPTY
7760: 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61 16 /* Databa
7770: 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 se is empty */.#
7780: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43 define SQLITE_SC
7790: 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f HEMA 17 /
77a0: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 * The database s
77b0: 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f chema changed */
77c0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
77d0: 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20 20 TOOBIG 18
77e0: 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c /* String or BL
77f0: 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a 65 20 OB exceeds size
7800: 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65 limit */.#define
7810: 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
7820: 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72 NT 19 /* Abor
7830: 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61 t due to constra
7840: 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f int violation */
7850: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7860: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20 MISMATCH 20
7870: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69 /* Data type mi
7880: 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e smatch */.#defin
7890: 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 e SQLITE_MISUSE
78a0: 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62 21 /* Lib
78b0: 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72 rary used incorr
78c0: 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 ectly */.#define
78d0: 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 SQLITE_NOLFS
78e0: 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73 22 /* Uses
78f0: 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74 OS features not
7900: 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f supported on ho
7910: 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 st */.#define SQ
7920: 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20 LITE_AUTH
7930: 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 23 /* Authori
7940: 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f zation denied */
7950: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7960: 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20 FORMAT 24
7970: 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61 /* Auxiliary da
7980: 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 tabase format er
7990: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ror */.#define S
79a0: 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 QLITE_RANGE
79b0: 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61 25 /* 2nd pa
79c0: 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
79d0: 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72 e3_bind out of r
79e0: 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ange */.#define
79f0: 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20 SQLITE_NOTADB
7a00: 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20 26 /* File
7a10: 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e opened that is n
7a20: 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69 ot a database fi
7a30: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 le */.#define SQ
7a40: 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20 LITE_ROW
7a50: 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 100 /* sqlite3
7a60: 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74 _step() has anot
7a70: 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f her row ready */
7a80: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7a90: 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20 DONE 101
7aa0: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 /* sqlite3_step
7ab0: 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 () has finished
7ac0: 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 executing */./*
7ad0: 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 end-of-error-cod
7ae0: 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 es */../*.** CAP
7af0: 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20 I3REF: Extended
7b00: 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 Result Codes {H1
7b10: 30 32 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 0220} <S10700>.*
7b20: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 * KEYWORDS: {ext
7b30: 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 ended error code
7b40: 7d 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f } {extended erro
7b50: 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 r codes}.** KEYW
7b60: 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 ORDS: {extended
7b70: 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78 result code} {ex
7b80: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
7b90: 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 des}.**.** In it
7ba0: 73 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 s default config
7bb0: 75 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 uration, SQLite
7bc0: 41 50 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 API routines ret
7bd0: 75 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e urn one of 26 in
7be0: 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 teger.** [SQLITE
7bf0: 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 _OK | result cod
7c00: 65 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 es]. However, e
7c10: 78 70 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 xperience has sh
7c20: 6f 77 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66 own that many of
7c30: 0a 2a 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74 .** these result
7c40: 20 63 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 codes are too c
7c50: 6f 61 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 oarse-grained.
7c60: 54 68 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 They do not prov
7c70: 69 64 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 ide as.** much i
7c80: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
7c90: 20 70 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f problems as pro
7ca0: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c grammers might l
7cb0: 69 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f ike. In an effo
7cc0: 72 74 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 rt to.** address
7cd0: 20 74 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 this, newer ver
7ce0: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
7cf0: 28 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 (version 3.3.8 a
7d00: 6e 64 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 nd later) includ
7d10: 65 0a 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 e.** support for
7d20: 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 additional resu
7d30: 6c 74 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 lt codes that pr
7d40: 6f 76 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 ovide more detai
7d50: 6c 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a led information.
7d60: 2a 2a 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e ** about errors.
7d70: 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 The extended re
7d80: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 sult codes are e
7d90: 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c nabled or disabl
7da0: 65 64 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64 ed.** on a per d
7db0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
7dc0: 6f 6e 20 62 61 73 69 73 20 75 73 69 6e 67 20 74 on basis using t
7dd0: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 he.** [sqlite3_e
7de0: 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 xtended_result_c
7df0: 6f 64 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a odes()] API..**.
7e00: 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 ** Some of the a
7e10: 76 61 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65 vailable extende
7e20: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 d result codes a
7e30: 72 65 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a re listed here..
7e40: 2a 2a 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63 ** One may expec
7e50: 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
7e60: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
7e70: 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78 codes will be ex
7e80: 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d pand.** over tim
7e90: 65 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 e. Software tha
7ea0: 74 20 75 73 65 73 20 65 78 74 65 6e 64 65 64 20 t uses extended
7eb0: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f result codes sho
7ec0: 75 6c 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f uld expect.** to
7ed0: 20 73 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20 see new result
7ee0: 63 6f 64 65 73 20 69 6e 20 66 75 74 75 72 65 20 codes in future
7ef0: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
7f00: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 te..**.** The SQ
7f10: 4c 49 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 LITE_OK result c
7f20: 6f 64 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 ode will never b
7f30: 65 20 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20 e extended. It
7f40: 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 will always.** b
7f50: 65 20 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a e exactly zero..
7f60: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7f70: 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20 20 20 E_IOERR_READ
7f80: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
7f90: 45 5f 49 4f 45 52 52 20 7c 20 28 31 3c 3c 38 29 E_IOERR | (1<<8)
7fa0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
7fb0: 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
7fc0: 44 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 D (SQLITE
7fd0: 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38 29 29 _IOERR | (2<<8))
7fe0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7ff0: 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20 20 20 IOERR_WRITE
8000: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8010: 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38 29 29 0a IOERR | (3<<8)).
8020: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8030: 4f 45 52 52 5f 46 53 59 4e 43 20 20 20 20 20 20 OERR_FSYNC
8040: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8050: 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29 29 0a 23 OERR | (4<<8)).#
8060: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
8070: 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 20 20 20 ERR_DIR_FSYNC
8080: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f (SQLITE_IO
8090: 45 52 52 20 7c 20 28 35 3c 3c 38 29 29 0a 23 64 ERR | (5<<8)).#d
80a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
80b0: 52 52 5f 54 52 55 4e 43 41 54 45 20 20 20 20 20 RR_TRUNCATE
80c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
80d0: 52 52 20 7c 20 28 36 3c 3c 38 29 29 0a 23 64 65 RR | (6<<8)).#de
80e0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 fine SQLITE_IOER
80f0: 52 5f 46 53 54 41 54 20 20 20 20 20 20 20 20 20 R_FSTAT
8100: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 (SQLITE_IOER
8110: 52 20 7c 20 28 37 3c 3c 38 29 29 0a 23 64 65 66 R | (7<<8)).#def
8120: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8130: 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 _UNLOCK
8140: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8150: 20 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65 66 69 | (8<<8)).#defi
8160: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ne SQLITE_IOERR_
8170: 52 44 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 RDLOCK
8180: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 (SQLITE_IOERR
8190: 7c 20 28 39 3c 3c 38 29 29 0a 23 64 65 66 69 6e | (9<<8)).#defin
81a0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 e SQLITE_IOERR_D
81b0: 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 ELETE
81c0: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
81d0: 20 28 31 30 3c 3c 38 29 29 0a 23 64 65 66 69 6e (10<<8)).#defin
81e0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 e SQLITE_IOERR_B
81f0: 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 20 20 20 LOCKED
8200: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8210: 20 28 31 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e (11<<8)).#defin
8220: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e e SQLITE_IOERR_N
8230: 4f 4d 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 OMEM
8240: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8250: 20 28 31 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e (12<<8)).#defin
8260: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 e SQLITE_IOERR_A
8270: 43 43 45 53 53 20 20 20 20 20 20 20 20 20 20 20 CCESS
8280: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8290: 20 28 31 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e (13<<8)).#defin
82a0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 e SQLITE_IOERR_C
82b0: 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
82c0: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
82d0: 20 28 31 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e (14<<8)).#defin
82e0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c e SQLITE_IOERR_L
82f0: 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 OCK
8300: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8310: 20 28 31 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e (15<<8)).#defin
8320: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 e SQLITE_IOERR_C
8330: 4c 4f 53 45 20 20 20 20 20 20 20 20 20 20 20 20 LOSE
8340: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8350: 20 28 31 36 3c 3c 38 29 29 0a 23 64 65 66 69 6e (16<<8)).#defin
8360: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 e SQLITE_IOERR_D
8370: 49 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20 IR_CLOSE
8380: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8390: 20 28 31 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e (17<<8)).#defin
83a0: 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f e SQLITE_LOCKED_
83b0: 53 48 41 52 45 44 43 41 43 48 45 20 20 20 20 20 SHAREDCACHE
83c0: 20 28 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 (SQLITE_LOCKED
83d0: 7c 20 28 31 3c 3c 38 29 20 29 0a 0a 2f 2a 0a 2a | (1<<8) )../*.*
83e0: 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67 * CAPI3REF: Flag
83f0: 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 65 6e 20 s For File Open
8400: 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48 31 30 32 Operations {H102
8410: 33 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31 30} <H11120> <H1
8420: 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 2700>.**.** Thes
8430: 65 20 62 69 74 20 76 61 6c 75 65 73 20 61 72 65 e bit values are
8440: 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
8450: 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33 72 64 20 e in the.** 3rd
8460: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
8470: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
8480: 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 2()] interface a
8490: 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74 68 nd.** in the 4th
84a0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
84b0: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f e xOpen method o
84c0: 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 f the.** [sqlite
84d0: 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 3_vfs] object..*
84e0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
84f0: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 20 _OPEN_READONLY
8500: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 0x0000000
8510: 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
8520: 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
8530: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 0x0000000
8540: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
8550: 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20 20 20 _OPEN_CREATE
8560: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 0x0000000
8570: 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 4.#define SQLITE
8580: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
8590: 4f 53 45 20 20 20 20 30 78 30 30 30 30 30 30 30 OSE 0x0000000
85a0: 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 8.#define SQLITE
85b0: 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 _OPEN_EXCLUSIVE
85c0: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 31 0x0000001
85d0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
85e0: 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 _OPEN_MAIN_DB
85f0: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 31 30 0x0000010
8600: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
8610: 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 20 20 _OPEN_TEMP_DB
8620: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30 0x0000020
8630: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
8640: 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f _OPEN_TRANSIENT_
8650: 44 42 20 20 20 20 20 30 78 30 30 30 30 30 34 30 DB 0x0000040
8660: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
8670: 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e _OPEN_MAIN_JOURN
8680: 41 4c 20 20 20 20 20 30 78 30 30 30 30 30 38 30 AL 0x0000080
8690: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
86a0: 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e _OPEN_TEMP_JOURN
86b0: 41 4c 20 20 20 20 20 30 78 30 30 30 30 31 30 30 AL 0x0000100
86c0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
86d0: 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c _OPEN_SUBJOURNAL
86e0: 20 20 20 20 20 20 20 30 78 30 30 30 30 32 30 30 0x0000200
86f0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
8700: 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 _OPEN_MASTER_JOU
8710: 52 4e 41 4c 20 20 20 30 78 30 30 30 30 34 30 30 RNAL 0x0000400
8720: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
8730: 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 20 20 _OPEN_NOMUTEX
8740: 20 20 20 20 20 20 20 30 78 30 30 30 30 38 30 30 0x0000800
8750: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
8760: 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 _OPEN_FULLMUTEX
8770: 20 20 20 20 20 20 20 30 78 30 30 30 31 30 30 30 0x0001000
8780: 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 0../*.** CAPI3RE
8790: 46 3a 20 44 65 76 69 63 65 20 43 68 61 72 61 63 F: Device Charac
87a0: 74 65 72 69 73 74 69 63 73 20 7b 48 31 30 32 34 teristics {H1024
87b0: 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 0a 2a 0} <H11120>.**.*
87c0: 2a 20 54 68 65 20 78 44 65 76 69 63 65 43 61 70 * The xDeviceCap
87d0: 61 62 69 6c 69 74 69 65 73 20 6d 65 74 68 6f 64 abilities method
87e0: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
87f0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 _io_methods].**
8800: 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73 20 61 object returns a
8810: 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
8820: 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 74 is a vector of t
8830: 68 65 20 74 68 65 73 65 0a 2a 2a 20 62 69 74 20 he these.** bit
8840: 76 61 6c 75 65 73 20 65 78 70 72 65 73 73 69 6e values expressin
8850: 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65 72 69 g I/O characteri
8860: 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d 61 73 stics of the mas
8870: 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 64 65 76 s storage.** dev
8880: 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 ice that holds t
8890: 68 65 20 66 69 6c 65 20 74 68 61 74 20 74 68 65 he file that the
88a0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
88b0: 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 72 73 20 hods].** refers
88c0: 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 to..**.** The SQ
88d0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
88e0: 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 C property means
88f0: 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 that all writes
8900: 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 of.** any size
8910: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 are atomic. The
8920: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
8930: 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a OMICnnn values.*
8940: 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 * mean that writ
8950: 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 es of blocks tha
8960: 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 t are nnn bytes
8970: 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 in size and.** a
8980: 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e re aligned to an
8990: 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20 69 address which i
89a0: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c s an integer mul
89b0: 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 tiple of.** nnn
89c0: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 are atomic. The
89d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 SQLITE_IOCAP_SA
89e0: 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 FE_APPEND value
89f0: 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 means.** that wh
8a00: 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65 6e en data is appen
8a10: 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 ded to a file, t
8a20: 68 65 20 64 61 74 61 20 69 73 20 61 70 70 65 6e he data is appen
8a30: 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 ded.** first the
8a40: 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 n the size of th
8a50: 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 e file is extend
8a60: 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 ed, never the ot
8a70: 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e her.** way aroun
8a80: 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 d. The SQLITE_I
8a90: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 OCAP_SEQUENTIAL
8aa0: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 property means t
8ab0: 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 hat.** informati
8ac0: 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f on is written to
8ad0: 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d disk in the sam
8ae0: 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 e order as calls
8af0: 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e .** to xWrite().
8b00: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
8b10: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 TE_IOCAP_ATOMIC
8b20: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 0x00000
8b30: 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 001.#define SQLI
8b40: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 TE_IOCAP_ATOMIC5
8b50: 31 32 20 20 20 20 20 20 20 30 78 30 30 30 30 30 12 0x00000
8b60: 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 002.#define SQLI
8b70: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 TE_IOCAP_ATOMIC1
8b80: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 K 0x00000
8b90: 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 004.#define SQLI
8ba0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 TE_IOCAP_ATOMIC2
8bb0: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 K 0x00000
8bc0: 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 008.#define SQLI
8bd0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 TE_IOCAP_ATOMIC4
8be0: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 K 0x00000
8bf0: 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 010.#define SQLI
8c00: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 TE_IOCAP_ATOMIC8
8c10: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 K 0x00000
8c20: 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 020.#define SQLI
8c30: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 TE_IOCAP_ATOMIC1
8c40: 36 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30 6K 0x00000
8c50: 30 34 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 040.#define SQLI
8c60: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 TE_IOCAP_ATOMIC3
8c70: 32 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30 2K 0x00000
8c80: 30 38 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 080.#define SQLI
8c90: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 TE_IOCAP_ATOMIC6
8ca0: 34 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30 4K 0x00000
8cb0: 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 100.#define SQLI
8cc0: 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 TE_IOCAP_SAFE_AP
8cd0: 50 45 4e 44 20 20 20 20 20 30 78 30 30 30 30 30 PEND 0x00000
8ce0: 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 200.#define SQLI
8cf0: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
8d00: 49 41 4c 20 20 20 20 20 20 30 78 30 30 30 30 30 IAL 0x00000
8d10: 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 400../*.** CAPI3
8d20: 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e REF: File Lockin
8d30: 67 20 4c 65 76 65 6c 73 20 7b 48 31 30 32 35 30 g Levels {H10250
8d40: 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31 31 33 } <H11120> <H113
8d50: 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 10>.**.** SQLite
8d60: 20 75 73 65 73 20 6f 6e 65 20 6f 66 20 74 68 65 uses one of the
8d70: 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 se integer value
8d80: 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a s as the second.
8d90: 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 ** argument to c
8da0: 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73 20 74 6f alls it makes to
8db0: 20 74 68 65 20 78 4c 6f 63 6b 28 29 20 61 6e 64 the xLock() and
8dc0: 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65 74 68 6f xUnlock() metho
8dd0: 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73 71 6c ds.** of an [sql
8de0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d ite3_io_methods]
8df0: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 object..*/.#def
8e00: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f ine SQLITE_LOCK_
8e10: 4e 4f 4e 45 20 20 20 20 20 20 20 20 20 20 30 0a NONE 0.
8e20: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
8e30: 4f 43 4b 5f 53 48 41 52 45 44 20 20 20 20 20 20 OCK_SHARED
8e40: 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 1.#define SQLI
8e50: 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 TE_LOCK_RESERVED
8e60: 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 2.#define
8e70: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 SQLITE_LOCK_PEND
8e80: 49 4e 47 20 20 20 20 20 20 20 33 0a 23 64 65 66 ING 3.#def
8e90: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f ine SQLITE_LOCK_
8ea0: 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 34 0a EXCLUSIVE 4.
8eb0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
8ec0: 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f 6e Synchronization
8ed0: 20 54 79 70 65 20 46 6c 61 67 73 20 7b 48 31 30 Type Flags {H10
8ee0: 32 36 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 260} <H11120>.**
8ef0: 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20 .** When SQLite
8f00: 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53 79 6e invokes the xSyn
8f10: 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e c() method of an
8f20: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f .** [sqlite3_io_
8f30: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 methods] object
8f40: 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62 69 6e it uses a combin
8f50: 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 73 ation of.** thes
8f60: 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 e integer values
8f70: 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 as the second a
8f80: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 rgument..**.** W
8f90: 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 53 hen the SQLITE_S
8fa0: 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66 6c 61 YNC_DATAONLY fla
8fb0: 67 20 69 73 20 75 73 65 64 2c 20 69 74 20 6d 65 g is used, it me
8fc0: 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ans that the.**
8fd0: 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 20 6f sync operation o
8fe0: 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66 6c 75 nly needs to flu
8ff0: 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73 73 20 sh data to mass
9000: 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64 65 0a storage. Inode.
9010: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e ** information n
9020: 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75 73 68 eed not be flush
9030: 65 64 2e 20 49 66 20 74 68 65 20 6c 6f 77 65 72 ed. If the lower
9040: 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20 74 68 four bits of th
9050: 65 20 66 6c 61 67 0a 2a 2a 20 65 71 75 61 6c 20 e flag.** equal
9060: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
9070: 41 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 AL, that means t
9080: 6f 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66 73 79 o use normal fsy
9090: 6e 63 28 29 20 73 65 6d 61 6e 74 69 63 73 2e 0a nc() semantics..
90a0: 2a 2a 20 49 66 20 74 68 65 20 6c 6f 77 65 72 20 ** If the lower
90b0: 66 6f 75 72 20 62 69 74 73 20 65 71 75 61 6c 20 four bits equal
90c0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
90d0: 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a 2a 20 , that means.**
90e0: 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20 58 20 to use Mac OS X
90f0: 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69 style fullsync i
9100: 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28 nstead of fsync(
9110: 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 )..*/.#define SQ
9120: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
9130: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 32 0a 0x00002.
9140: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
9150: 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 YNC_FULL
9160: 20 20 30 78 30 30 30 30 33 0a 23 64 65 66 69 6e 0x00003.#defin
9170: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 e SQLITE_SYNC_DA
9180: 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 30 TAONLY 0x00
9190: 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 010../*.** CAPI3
91a0: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 REF: OS Interfac
91b0: 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61 6e 64 e Open File Hand
91c0: 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53 32 30 le {H11110} <S20
91d0: 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 73 110>.**.** An [s
91e0: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a qlite3_file] obj
91f0: 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 ect represents a
9200: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20 74 n open file in t
9210: 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66 61 he OS.** interfa
9220: 63 65 20 6c 61 79 65 72 2e 20 20 49 6e 64 69 76 ce layer. Indiv
9230: 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 72 66 61 idual OS interfa
9240: 63 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ce implementatio
9250: 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 ns will.** want
9260: 74 6f 20 73 75 62 63 6c 61 73 73 20 74 68 69 73 to subclass this
9270: 20 6f 62 6a 65 63 74 20 62 79 20 61 70 70 65 6e object by appen
9280: 64 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 ding additional
9290: 66 69 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 fields.** for th
92a0: 65 69 72 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 eir own use. Th
92b0: 65 20 70 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 e pMethods entry
92c0: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
92d0: 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f an.** [sqlite3_
92e0: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 io_methods] obje
92f0: 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 ct that defines
9300: 6d 65 74 68 6f 64 73 20 66 6f 72 20 70 65 72 66 methods for perf
9310: 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 orming.** I/O op
9320: 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 erations on the
9330: 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 open file..*/.ty
9340: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
9350: 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 ite3_file sqlite
9360: 33 5f 66 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 3_file;.struct s
9370: 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 qlite3_file {.
9380: 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c const struct sql
9390: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
93a0: 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d *pMethods; /* M
93b0: 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 ethods for an op
93c0: 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f en file */.};../
93d0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
93e0: 53 20 49 6e 74 65 72 66 61 63 65 20 46 69 6c 65 S Interface File
93f0: 20 56 69 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 Virtual Methods
9400: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 32 30 7d Object {H11120}
9410: 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 <S20110>.**.**
9420: 45 76 65 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 Every file opene
9430: 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 d by the [sqlite
9440: 33 5f 76 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 3_vfs] xOpen met
9450: 68 6f 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e hod populates an
9460: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c .** [sqlite3_fil
9470: 65 5d 20 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d e] object (or, m
9480: 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 ore commonly, a
9490: 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a subclass of the.
94a0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 ** [sqlite3_file
94b0: 5d 20 6f 62 6a 65 63 74 29 20 77 69 74 68 20 61 ] object) with a
94c0: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
94d0: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
94e0: 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 object..** This
94f0: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 object defines t
9500: 68 65 20 6d 65 74 68 6f 64 73 20 75 73 65 64 20 he methods used
9510: 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f to perform vario
9520: 75 73 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a us operations.**
9530: 20 61 67 61 69 6e 73 74 20 74 68 65 20 6f 70 65 against the ope
9540: 6e 20 66 69 6c 65 20 72 65 70 72 65 73 65 6e 74 n file represent
9550: 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 ed by the [sqlit
9560: 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e e3_file] object.
9570: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 .**.** The flags
9580: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 argument to xSy
9590: 6e 63 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 nc may be one of
95a0: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f [SQLITE_SYNC_NO
95b0: 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c RMAL] or.** [SQL
95c0: 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 ITE_SYNC_FULL].
95d0: 20 54 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 The first choic
95e0: 65 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 e is the normal
95f0: 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20 fsync()..** The
9600: 73 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 second choice is
9610: 20 61 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c a Mac OS X styl
9620: 65 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 e fullsync. The
9630: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 [SQLITE_SYNC_DA
9640: 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 TAONLY].** flag
9650: 6d 61 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74 may be ORed in t
9660: 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 o indicate that
9670: 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66 only the data of
9680: 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 the file.** and
9690: 20 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e not its inode n
96a0: 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65 eeds to be synce
96b0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 d..**.** The int
96c0: 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78 eger values to x
96d0: 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f Lock() and xUnlo
96e0: 63 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a ck() are one of.
96f0: 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
9700: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e [SQLITE_LOCK_NON
9710: 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c E],.** <li> [SQL
9720: 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d ITE_LOCK_SHARED]
9730: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ,.** <li> [SQLIT
9740: 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d E_LOCK_RESERVED]
9750: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ,.** <li> [SQLIT
9760: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c E_LOCK_PENDING],
9770: 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c or.** <li> [SQL
9780: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 ITE_LOCK_EXCLUSI
9790: 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a VE]..** </ul>.**
97a0: 20 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73 xLock() increas
97b0: 65 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e es the lock. xUn
97c0: 6c 6f 63 6b 28 29 20 64 65 63 72 65 61 73 65 73 lock() decreases
97d0: 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 the lock..** Th
97e0: 65 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 e xCheckReserved
97f0: 4c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68 Lock() method ch
9800: 65 63 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79 ecks whether any
9810: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
9820: 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20 tion,.** either
9830: 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
9840: 6f 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 or in some other
9850: 20 70 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c process, is hol
9860: 64 69 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c ding a RESERVED,
9870: 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20 .** PENDING, or
9880: 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
9890: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20 n the file. It
98a0: 72 65 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20 returns true.**
98b0: 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65 if such a lock e
98c0: 78 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20 xists and false
98d0: 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a otherwise..**.**
98e0: 20 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f The xFileContro
98f0: 6c 28 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20 l() method is a
9900: 67 65 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63 generic interfac
9910: 65 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75 e that allows cu
9920: 73 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c stom.** VFS impl
9930: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64 ementations to d
9940: 69 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 irectly control
9950: 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69 an open file usi
9960: 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 ng the.** [sqlit
9970: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 e3_file_control(
9980: 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 )] interface. T
9990: 68 65 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61 he second "op" a
99a0: 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a rgument is an.**
99b0: 20 69 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e integer opcode.
99c0: 20 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 The third argu
99d0: 6d 65 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69 ment is a generi
99e0: 63 20 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64 c pointer intend
99f0: 65 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 ed to.** point t
9a00: 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 o a structure th
9a10: 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 at may contain a
9a20: 72 67 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63 rguments or spac
9a30: 65 20 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a e in which to.**
9a40: 20 77 72 69 74 65 20 72 65 74 75 72 6e 20 76 61 write return va
9a50: 6c 75 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c lues. Potential
9a60: 20 75 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43 uses for xFileC
9a70: 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62 ontrol() might b
9a80: 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 e.** functions t
9a90: 6f 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e o enable blockin
9aa0: 67 20 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d g locks with tim
9ab0: 65 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65 eouts, to change
9ac0: 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 the.** locking
9ad0: 73 74 72 61 74 65 67 79 20 28 66 6f 72 20 65 78 strategy (for ex
9ae0: 61 6d 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74 ample to use dot
9af0: 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f -file locks), to
9b00: 20 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75 inquire.** abou
9b10: 74 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 t the status of
9b20: 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72 a lock, or to br
9b30: 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e eak stale locks.
9b40: 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 The SQLite.**
9b50: 63 6f 72 65 20 72 65 73 65 72 76 65 73 20 61 6c core reserves al
9b60: 6c 20 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74 l opcodes less t
9b70: 68 61 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20 han 100 for its
9b80: 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 own use..** A [S
9b90: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b QLITE_FCNTL_LOCK
9ba0: 53 54 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20 STATE | list of
9bb0: 6f 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68 opcodes] less th
9bc0: 61 6e 20 31 30 30 20 69 73 20 61 76 61 69 6c 61 an 100 is availa
9bd0: 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 ble..** Applicat
9be0: 69 6f 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65 ions that define
9bf0: 20 61 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43 a custom xFileC
9c00: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68 ontrol method sh
9c10: 6f 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73 ould use opcodes
9c20: 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
9c30: 20 31 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f 100 to avoid co
9c40: 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 nflicts..**.** T
9c50: 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 he xSectorSize()
9c60: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 method returns
9c70: 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
9c80: 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65 of the.** device
9c90: 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20 that underlies
9ca0: 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73 the file. The s
9cb0: 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68 ector size is th
9cc0: 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69 e.** minimum wri
9cd0: 74 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 te that can be p
9ce0: 65 72 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74 erformed without
9cf0: 20 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f disturbing.** o
9d00: 74 68 65 72 20 62 79 74 65 73 20 69 6e 20 74 68 ther bytes in th
9d10: 65 20 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65 e file. The xDe
9d20: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
9d30: 69 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 ics().** method
9d40: 72 65 74 75 72 6e 73 20 61 20 62 69 74 20 76 65 returns a bit ve
9d50: 63 74 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20 ctor describing
9d60: 62 65 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65 behaviors of the
9d70: 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 .** underlying d
9d80: 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c evice:.**.** <ul
9d90: 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 >.** <li> [SQLIT
9da0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a E_IOCAP_ATOMIC].
9db0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
9dc0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d IOCAP_ATOMIC512]
9dd0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
9de0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d _IOCAP_ATOMIC1K]
9df0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
9e00: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d _IOCAP_ATOMIC2K]
9e10: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
9e20: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d _IOCAP_ATOMIC4K]
9e30: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
9e40: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d _IOCAP_ATOMIC8K]
9e50: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
9e60: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b _IOCAP_ATOMIC16K
9e70: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ].** <li> [SQLIT
9e80: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 E_IOCAP_ATOMIC32
9e90: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 K].** <li> [SQLI
9ea0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 TE_IOCAP_ATOMIC6
9eb0: 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 4K].** <li> [SQL
9ec0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
9ed0: 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b PPEND].** <li> [
9ee0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
9ef0: 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c UENTIAL].** </ul
9f00: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 >.**.** The SQLI
9f10: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 TE_IOCAP_ATOMIC
9f20: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 property means t
9f30: 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f hat all writes o
9f40: 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 f.** any size ar
9f50: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 e atomic. The S
9f60: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
9f70: 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 ICnnn values.**
9f80: 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 mean that writes
9f90: 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 of blocks that
9fa0: 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e are nnn bytes in
9fb0: 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 size and.** are
9fc0: 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 aligned to an a
9fd0: 64 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20 ddress which is
9fe0: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 an integer multi
9ff0: 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 ple of.** nnn ar
a000: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 e atomic. The S
a010: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
a020: 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 _APPEND value me
a030: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e ans.** that when
a040: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 data is appende
a050: 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 d to a file, the
a060: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 data is appende
a070: 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 d.** first then
a080: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
a090: 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 file is extended
a0a0: 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 , never the othe
a0b0: 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e r.** way around.
a0c0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 The SQLITE_IOC
a0d0: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 AP_SEQUENTIAL pr
a0e0: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 operty means tha
a0f0: 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e t.** information
a100: 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 is written to d
a110: 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 isk in the same
a120: 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a order as calls.*
a130: 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a * to xWrite()..*
a140: 2a 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20 *.** If xRead()
a150: 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 returns SQLITE_I
a160: 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
a170: 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c it must also fil
a180: 6c 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65 l.** in the unre
a190: 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 ad portions of t
a1a0: 68 65 20 62 75 66 66 65 72 20 77 69 74 68 20 7a he buffer with z
a1b0: 65 72 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61 eros. A VFS tha
a1c0: 74 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65 t.** fails to ze
a1d0: 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 ro-fill short re
a1e0: 61 64 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74 ads might seem t
a1f0: 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72 o work. However
a200: 2c 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20 ,.** failure to
a210: 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 zero-fill short
a220: 72 65 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74 reads will event
a230: 75 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a ually lead to.**
a240: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
a250: 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
a260: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
a270: 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 io_methods sqlit
a280: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 e3_io_methods;.s
a290: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f truct sqlite3_io
a2a0: 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 _methods {. int
a2b0: 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 iVersion;. int
a2c0: 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 (*xClose)(sqlit
a2d0: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 e3_file*);. int
a2e0: 20 28 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65 (*xRead)(sqlite
a2f0: 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 3_file*, void*,
a300: 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 int iAmt, sqlite
a310: 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 3_int64 iOfst);.
a320: 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28 int (*xWrite)(
a330: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 sqlite3_file*, c
a340: 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 onst void*, int
a350: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e iAmt, sqlite3_in
a360: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e t64 iOfst);. in
a370: 74 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73 t (*xTruncate)(s
a380: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 qlite3_file*, sq
a390: 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65 lite3_int64 size
a3a0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 );. int (*xSync
a3b0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c )(sqlite3_file*,
a3c0: 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69 int flags);. i
a3d0: 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28 nt (*xFileSize)(
a3e0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 sqlite3_file*, s
a3f0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 qlite3_int64 *pS
a400: 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c ize);. int (*xL
a410: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ock)(sqlite3_fil
a420: 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 e*, int);. int
a430: 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 (*xUnlock)(sqlit
a440: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
a450: 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65 int (*xCheckRe
a460: 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69 servedLock)(sqli
a470: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a te3_file*, int *
a480: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 pResOut);. int
a490: 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 (*xFileControl)(
a4a0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
a4b0: 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 nt op, void *pAr
a4c0: 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 g);. int (*xSec
a4d0: 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 torSize)(sqlite3
a4e0: 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 _file*);. int (
a4f0: 2a 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 *xDeviceCharacte
a500: 72 69 73 74 69 63 73 29 28 73 71 6c 69 74 65 33 ristics)(sqlite3
a510: 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 _file*);. /* Ad
a520: 64 69 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 ditional methods
a530: 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
a540: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
a550: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.};../*.** CA
a560: 50 49 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64 PI3REF: Standard
a570: 20 46 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 File Control Op
a580: 63 6f 64 65 73 20 7b 48 31 31 33 31 30 7d 20 3c codes {H11310} <
a590: 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30800>.**.** Th
a5a0: 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
a5b0: 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f 64 65 tants are opcode
a5c0: 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c 65 43 s for the xFileC
a5d0: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a ontrol method.**
a5e0: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
a5f0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a _io_methods] obj
a600: 65 63 74 20 61 6e 64 20 66 6f 72 20 74 68 65 20 ect and for the
a610: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f [sqlite3_file_co
a620: 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 ntrol()].** inte
a630: 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rface..**.** The
a640: 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c [SQLITE_FCNTL_L
a650: 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64 65 OCKSTATE] opcode
a660: 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 is used for deb
a670: 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a 2a ugging. This.**
a680: 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 opcode causes t
a690: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 he xFileControl
a6a0: 6d 65 74 68 6f 64 20 74 6f 20 77 72 69 74 65 20 method to write
a6b0: 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 the current stat
a6c0: 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b e of.** the lock
a6d0: 20 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 (one of [SQLITE
a6e0: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 _LOCK_NONE], [SQ
a6f0: 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 LITE_LOCK_SHARED
a700: 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f ],.** [SQLITE_LO
a710: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b 53 CK_RESERVED], [S
a720: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 QLITE_LOCK_PENDI
a730: 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f NG], or [SQLITE_
a740: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 29 LOCK_EXCLUSIVE])
a750: 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 .** into an inte
a760: 67 65 72 20 74 68 61 74 20 74 68 65 20 70 41 72 ger that the pAr
a770: 67 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 g argument point
a780: 73 20 74 6f 2e 20 54 68 69 73 20 63 61 70 61 62 s to. This capab
a790: 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73 65 64 ility.** is used
a7a0: 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 20 during testing
a7b0: 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 and only needs t
a7c0: 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 20 77 o be supported w
a7d0: 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a hen SQLITE_TEST.
a7e0: 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a ** is defined..*
a7f0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
a800: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 _FCNTL_LOCKSTATE
a810: 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
a820: 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 e SQLITE_GET_LOC
a830: 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 20 KPROXYFILE
a840: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
a850: 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
a860: 4c 45 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e LE 3.#defin
a870: 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 e SQLITE_LAST_ER
a880: 52 4e 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 RNO
a890: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 4../*.** CAPI3RE
a8a0: 46 3a 20 4d 75 74 65 78 20 48 61 6e 64 6c 65 20 F: Mutex Handle
a8b0: 7b 48 31 37 31 31 30 7d 20 3c 53 32 30 31 33 30 {H17110} <S20130
a8c0: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 >.**.** The mute
a8d0: 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 69 6e 20 x module within
a8e0: 53 51 4c 69 74 65 20 64 65 66 69 6e 65 73 20 5b SQLite defines [
a8f0: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 74 sqlite3_mutex] t
a900: 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 62 73 74 72 o be an.** abstr
a910: 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 6d act type for a m
a920: 75 74 65 78 20 6f 62 6a 65 63 74 2e 20 20 54 68 utex object. Th
a930: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6e 65 e SQLite core ne
a940: 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20 ver looks.** at
a950: 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
a960: 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 resentation of a
a970: 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 n [sqlite3_mutex
a980: 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64 ]. It only.** d
a990: 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65 eals with pointe
a9a0: 72 73 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 rs to the [sqlit
a9b0: 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 e3_mutex] object
a9c0: 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 78 65 73 20 ..**.** Mutexes
a9d0: 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e are created usin
a9e0: 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 g [sqlite3_mutex
a9f0: 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79 _alloc()]..*/.ty
aa00: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
aa10: 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69 74 ite3_mutex sqlit
aa20: 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a e3_mutex;../*.**
aa30: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e CAPI3REF: OS In
aa40: 74 65 72 66 61 63 65 20 4f 62 6a 65 63 74 20 7b terface Object {
aa50: 48 31 31 31 34 30 7d 20 3c 53 32 30 31 30 30 3e H11140} <S20100>
aa60: 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e .**.** An instan
aa70: 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ce of the sqlite
aa80: 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 64 65 66 3_vfs object def
aa90: 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 ines the interfa
aaa0: 63 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 ce between.** th
aab0: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 61 6e e SQLite core an
aac0: 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 d the underlying
aad0: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
aae0: 6d 2e 20 20 54 68 65 20 22 76 66 73 22 0a 2a 2a m. The "vfs".**
aaf0: 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 in the name of
ab00: 74 68 65 20 6f 62 6a 65 63 74 20 73 74 61 6e 64 the object stand
ab10: 73 20 66 6f 72 20 22 76 69 72 74 75 61 6c 20 66 s for "virtual f
ab20: 69 6c 65 20 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a ile system"..**.
ab30: 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 ** The value of
ab40: 74 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 the iVersion fie
ab50: 6c 64 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 ld is initially
ab60: 31 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 1 but may be lar
ab70: 67 65 72 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65 ger in.** future
ab80: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
ab90: 69 74 65 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c ite. Additional
aba0: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 fields may be a
abb0: 70 70 65 6e 64 65 64 20 74 6f 20 74 68 69 73 0a ppended to this.
abc0: 2a 2a 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74 ** object when t
abd0: 68 65 20 69 56 65 72 73 69 6f 6e 20 76 61 6c 75 he iVersion valu
abe0: 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 2e 20 e is increased.
abf0: 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 Note that the s
ac00: 74 72 75 63 74 75 72 65 0a 2a 2a 20 6f 66 20 74 tructure.** of t
ac10: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f he sqlite3_vfs o
ac20: 62 6a 65 63 74 20 63 68 61 6e 67 65 73 20 69 6e bject changes in
ac30: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
ac40: 20 62 65 74 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 between.** SQLi
ac50: 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 39 te version 3.5.9
ac60: 20 61 6e 64 20 33 2e 36 2e 30 20 61 6e 64 20 79 and 3.6.0 and y
ac70: 65 74 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20 et the iVersion
ac80: 66 69 65 6c 64 20 77 61 73 20 6e 6f 74 0a 2a 2a field was not.**
ac90: 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a modified..**.**
aca0: 20 54 68 65 20 73 7a 4f 73 46 69 6c 65 20 66 69 The szOsFile fi
acb0: 65 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 eld is the size
acc0: 6f 66 20 74 68 65 20 73 75 62 63 6c 61 73 73 65 of the subclasse
acd0: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d d [sqlite3_file]
ace0: 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 75 73 .** structure us
acf0: 65 64 20 62 79 20 74 68 69 73 20 56 46 53 2e 20 ed by this VFS.
ad00: 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 74 mxPathname is t
ad10: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
ad20: 68 20 6f 66 0a 2a 2a 20 61 20 70 61 74 68 6e 61 h of.** a pathna
ad30: 6d 65 20 69 6e 20 74 68 69 73 20 56 46 53 2e 0a me in this VFS..
ad40: 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 65 64 **.** Registered
ad50: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a sqlite3_vfs obj
ad60: 65 63 74 73 20 61 72 65 20 6b 65 70 74 20 6f 6e ects are kept on
ad70: 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 66 a linked list f
ad80: 6f 72 6d 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 ormed by.** the
ad90: 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 20 20 pNext pointer.
ada0: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 The [sqlite3_vfs
adb0: 5f 72 65 67 69 73 74 65 72 28 29 5d 0a 2a 2a 20 _register()].**
adc0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 and [sqlite3_vfs
add0: 5f 75 6e 72 65 67 69 73 74 65 72 28 29 5d 20 69 _unregister()] i
ade0: 6e 74 65 72 66 61 63 65 73 20 6d 61 6e 61 67 65 nterfaces manage
adf0: 20 74 68 69 73 20 6c 69 73 74 0a 2a 2a 20 69 6e this list.** in
ae00: 20 61 20 74 68 72 65 61 64 2d 73 61 66 65 20 77 a thread-safe w
ae10: 61 79 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 ay. The [sqlite
ae20: 33 5f 76 66 73 5f 66 69 6e 64 28 29 5d 20 69 6e 3_vfs_find()] in
ae30: 74 65 72 66 61 63 65 0a 2a 2a 20 73 65 61 72 63 terface.** searc
ae40: 68 65 73 20 74 68 65 20 6c 69 73 74 2e 20 20 4e hes the list. N
ae50: 65 69 74 68 65 72 20 74 68 65 20 61 70 70 6c 69 either the appli
ae60: 63 61 74 69 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 cation code nor
ae70: 74 68 65 20 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 the VFS.** imple
ae80: 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 mentation should
ae90: 20 75 73 65 20 74 68 65 20 70 4e 65 78 74 20 70 use the pNext p
aea0: 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
aeb0: 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 69 73 e pNext field is
aec0: 20 74 68 65 20 6f 6e 6c 79 20 66 69 65 6c 64 20 the only field
aed0: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 in the sqlite3_v
aee0: 66 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 fs.** structure
aef0: 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c that SQLite will
af00: 20 65 76 65 72 20 6d 6f 64 69 66 79 2e 20 20 53 ever modify. S
af10: 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 QLite will only
af20: 61 63 63 65 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 access.** or mod
af30: 69 66 79 20 74 68 69 73 20 66 69 65 6c 64 20 77 ify this field w
af40: 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 61 20 70 hile holding a p
af50: 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 69 63 articular static
af60: 20 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 61 mutex..** The a
af70: 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c pplication shoul
af80: 64 20 6e 65 76 65 72 20 6d 6f 64 69 66 79 20 61 d never modify a
af90: 6e 79 74 68 69 6e 67 20 77 69 74 68 69 6e 20 74 nything within t
afa0: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a he sqlite3_vfs.*
afb0: 2a 20 6f 62 6a 65 63 74 20 6f 6e 63 65 20 74 68 * object once th
afc0: 65 20 6f 62 6a 65 63 74 20 68 61 73 20 62 65 65 e object has bee
afd0: 6e 20 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a n registered..**
afe0: 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d 65 20 66 69 .** The zName fi
aff0: 65 6c 64 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 eld holds the na
b000: 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 6d 6f me of the VFS mo
b010: 64 75 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20 dule. The name
b020: 6d 75 73 74 0a 2a 2a 20 62 65 20 75 6e 69 71 75 must.** be uniqu
b030: 65 20 61 63 72 6f 73 73 20 61 6c 6c 20 56 46 53 e across all VFS
b040: 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 modules..**.**
b050: 53 51 4c 69 74 65 20 77 69 6c 6c 20 67 75 61 72 SQLite will guar
b060: 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 7a antee that the z
b070: 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 Filename paramet
b080: 65 72 20 74 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 er to xOpen.** i
b090: 73 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 20 s either a NULL
b0a0: 70 6f 69 6e 74 65 72 20 6f 72 20 73 74 72 69 6e pointer or strin
b0b0: 67 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 g obtained.** fr
b0c0: 6f 6d 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 om xFullPathname
b0d0: 28 29 2e 20 20 53 51 4c 69 74 65 20 66 75 72 74 (). SQLite furt
b0e0: 68 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 her guarantees t
b0f0: 68 61 74 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e hat.** the strin
b100: 67 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 g will be valid
b110: 61 6e 64 20 75 6e 63 68 61 6e 67 65 64 20 75 6e and unchanged un
b120: 74 69 6c 20 78 43 6c 6f 73 65 28 29 20 69 73 0a til xClose() is.
b130: 2a 2a 20 63 61 6c 6c 65 64 2e 20 42 65 63 61 75 ** called. Becau
b140: 73 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f se of the previo
b150: 75 73 20 73 65 6e 74 65 6e 73 65 2c 0a 2a 2a 20 us sentense,.**
b160: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c the [sqlite3_fil
b170: 65 5d 20 63 61 6e 20 73 61 66 65 6c 79 20 73 74 e] can safely st
b180: 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ore a pointer to
b190: 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 the.** filename
b1a0: 20 69 66 20 69 74 20 6e 65 65 64 73 20 74 6f 20 if it needs to
b1b0: 72 65 6d 65 6d 62 65 72 20 74 68 65 20 66 69 6c remember the fil
b1c0: 65 6e 61 6d 65 20 66 6f 72 20 73 6f 6d 65 20 72 ename for some r
b1d0: 65 61 73 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 eason..** If the
b1e0: 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d zFilename param
b1f0: 65 74 65 72 20 69 73 20 78 4f 70 65 6e 20 69 73 eter is xOpen is
b200: 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
b210: 74 68 65 6e 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75 then xOpen.** mu
b220: 73 74 20 69 6e 76 69 74 65 20 69 74 73 20 6f 77 st invite its ow
b230: 6e 20 74 65 6d 70 6f 72 61 72 79 20 6e 61 6d 65 n temporary name
b240: 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 20 for the file.
b250: 57 68 65 6e 65 76 65 72 20 74 68 65 20 0a 2a 2a Whenever the .**
b260: 20 78 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d xFilename param
b270: 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 69 74 20 eter is NULL it
b280: 77 69 6c 6c 20 61 6c 73 6f 20 62 65 20 74 68 65 will also be the
b290: 20 63 61 73 65 20 74 68 61 74 20 74 68 65 0a 2a case that the.*
b2a0: 2a 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 * flags paramete
b2b0: 72 20 77 69 6c 6c 20 69 6e 63 6c 75 64 65 20 5b r will include [
b2c0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
b2d0: 54 45 4f 4e 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a TEONCLOSE]..**.*
b2e0: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 * The flags argu
b2f0: 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29 20 ment to xOpen()
b300: 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 62 69 74 includes all bit
b310: 73 20 73 65 74 20 69 6e 0a 2a 2a 20 74 68 65 20 s set in.** the
b320: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 flags argument t
b330: 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f o [sqlite3_open_
b340: 76 32 28 29 5d 2e 20 20 4f 72 20 69 66 20 5b 73 v2()]. Or if [s
b350: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a qlite3_open()].*
b360: 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 * or [sqlite3_op
b370: 65 6e 31 36 28 29 5d 20 69 73 20 75 73 65 64 2c en16()] is used,
b380: 20 74 68 65 6e 20 66 6c 61 67 73 20 69 6e 63 6c then flags incl
b390: 75 64 65 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a udes at least.**
b3a0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 [SQLITE_OPEN_RE
b3b0: 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 ADWRITE] | [SQLI
b3c0: 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 2e TE_OPEN_CREATE].
b3d0: 20 0a 2a 2a 20 49 66 20 78 4f 70 65 6e 28 29 20 .** If xOpen()
b3e0: 6f 70 65 6e 73 20 61 20 66 69 6c 65 20 72 65 61 opens a file rea
b3f0: 64 2d 6f 6e 6c 79 20 74 68 65 6e 20 69 74 20 73 d-only then it s
b400: 65 74 73 20 2a 70 4f 75 74 46 6c 61 67 73 20 74 ets *pOutFlags t
b410: 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 5b 53 51 o.** include [SQ
b420: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
b430: 4c 59 5d 2e 20 20 4f 74 68 65 72 20 62 69 74 73 LY]. Other bits
b440: 20 69 6e 20 2a 70 4f 75 74 46 6c 61 67 73 20 6d in *pOutFlags m
b450: 61 79 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a ay be set..**.**
b460: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 SQLite will als
b470: 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 o add one of the
b480: 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 following flags
b490: 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 28 29 0a to the xOpen().
b4a0: 2a 2a 20 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69 ** call, dependi
b4b0: 6e 67 20 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74 ng on the object
b4c0: 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a being opened:.*
b4d0: 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
b4e0: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f > [SQLITE_OPEN_
b4f0: 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e MAIN_DB].** <li>
b500: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d [SQLITE_OPEN_M
b510: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 AIN_JOURNAL].**
b520: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
b530: 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c EN_TEMP_DB].** <
b540: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 li> [SQLITE_OPE
b550: 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a N_TEMP_JOURNAL].
b560: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 ** <li> [SQLITE
b570: 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f _OPEN_TRANSIENT_
b580: 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 DB].** <li> [SQ
b590: 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 LITE_OPEN_SUBJOU
b5a0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b RNAL].** <li> [
b5b0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 SQLITE_OPEN_MAST
b5c0: 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c ER_JOURNAL].** <
b5d0: 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 /ul>.**.** The f
b5e0: 69 6c 65 20 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e ile I/O implemen
b5f0: 74 61 74 69 6f 6e 20 63 61 6e 20 75 73 65 20 74 tation can use t
b600: 68 65 20 6f 62 6a 65 63 74 20 74 79 70 65 20 66 he object type f
b610: 6c 61 67 73 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 lags to.** chang
b620: 65 20 74 68 65 20 77 61 79 20 69 74 20 64 65 61 e the way it dea
b630: 6c 73 20 77 69 74 68 20 66 69 6c 65 73 2e 20 20 ls with files.
b640: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 For example, an
b650: 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 application.** t
b660: 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 63 61 72 hat does not car
b670: 65 20 61 62 6f 75 74 20 63 72 61 73 68 20 72 65 e about crash re
b680: 63 6f 76 65 72 79 20 6f 72 20 72 6f 6c 6c 62 61 covery or rollba
b690: 63 6b 20 6d 69 67 68 74 20 6d 61 6b 65 0a 2a 2a ck might make.**
b6a0: 20 74 68 65 20 6f 70 65 6e 20 6f 66 20 61 20 6a the open of a j
b6b0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 20 6e 6f ournal file a no
b6c0: 2d 6f 70 2e 20 20 57 72 69 74 65 73 20 74 6f 20 -op. Writes to
b6d0: 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 this journal wou
b6e0: 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 6e 6f ld.** also be no
b6f0: 2d 6f 70 73 2c 20 61 6e 64 20 61 6e 79 20 61 74 -ops, and any at
b700: 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 tempt to read th
b710: 65 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 20 e journal would
b720: 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
b730: 5f 49 4f 45 52 52 2e 20 20 4f 72 20 74 68 65 20 _IOERR. Or the
b740: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d implementation m
b750: 69 67 68 74 20 72 65 63 6f 67 6e 69 7a 65 20 74 ight recognize t
b760: 68 61 74 20 61 20 64 61 74 61 62 61 73 65 0a 2a hat a database.*
b770: 2a 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 * file will be d
b780: 6f 69 6e 67 20 70 61 67 65 2d 61 6c 69 67 6e 65 oing page-aligne
b790: 64 20 73 65 63 74 6f 72 20 72 65 61 64 73 20 61 d sector reads a
b7a0: 6e 64 20 77 72 69 74 65 73 20 69 6e 20 61 20 72 nd writes in a r
b7b0: 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64 65 72 20 61 andom.** order a
b7c0: 6e 64 20 73 65 74 20 75 70 20 69 74 73 20 49 2f nd set up its I/
b7d0: 4f 20 73 75 62 73 79 73 74 65 6d 20 61 63 63 6f O subsystem acco
b7e0: 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 rdingly..**.** S
b7f0: 51 4c 69 74 65 20 6d 69 67 68 74 20 61 6c 73 6f QLite might also
b800: 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 add one of the
b810: 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 following flags
b820: 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 to the xOpen met
b830: 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a hod:.**.** <ul>.
b840: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
b850: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
b860: 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c SE].** <li> [SQL
b870: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
b880: 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a VE].** </ul>.**.
b890: 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f ** The [SQLITE_O
b8a0: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
b8b0: 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 E] flag means th
b8c0: 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 e file should be
b8d0: 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e .** deleted when
b8e0: 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 it is closed.
b8f0: 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e The [SQLITE_OPEN
b900: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a _DELETEONCLOSE].
b910: 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 66 ** will be set f
b920: 6f 72 20 54 45 4d 50 20 20 64 61 74 61 62 61 73 or TEMP databas
b930: 65 73 2c 20 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 es, journals and
b940: 20 66 6f 72 20 73 75 62 6a 6f 75 72 6e 61 6c 73 for subjournals
b950: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c ..**.** The [SQL
b960: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
b970: 56 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 VE] flag means t
b980: 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 he file should b
b990: 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 e opened.** for
b9a0: 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 exclusive access
b9b0: 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 . This flag is
b9c0: 73 65 74 20 66 6f 72 20 61 6c 6c 20 66 69 6c 65 set for all file
b9d0: 73 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 s except.** for
b9e0: 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
b9f0: 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 e file..**.** At
ba00: 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 20 least szOsFile
ba10: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
ba20: 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 are allocated by
ba30: 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 6f SQLite.** to ho
ba40: 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 33 ld the [sqlite3
ba50: 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 65 _file] structure
ba60: 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74 passed as the t
ba70: 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 hird.** argument
ba80: 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 20 to xOpen. The
ba90: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65 xOpen method doe
baa0: 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a s not have to.**
bab0: 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 74 allocate the st
bac0: 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f 75 ructure; it shou
bad0: 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 20 ld just fill it
bae0: 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c in..**.** The fl
baf0: 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ags argument to
bb00: 78 41 63 63 65 73 73 28 29 20 6d 61 79 20 62 65 xAccess() may be
bb10: 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f [SQLITE_ACCESS_
bb20: 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20 74 65 EXISTS].** to te
bb30: 73 74 20 66 6f 72 20 74 68 65 20 65 78 69 73 74 st for the exist
bb40: 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65 2c 20 ence of a file,
bb50: 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 or [SQLITE_ACCES
bb60: 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 6f 0a S_READWRITE] to.
bb70: 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65 72 20 ** test whether
bb80: 61 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 a file is readab
bb90: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2c le and writable,
bba0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 or [SQLITE_ACCE
bbb0: 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f 20 74 SS_READ].** to t
bbc0: 65 73 74 20 77 68 65 74 68 65 72 20 61 20 66 69 est whether a fi
bbd0: 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 72 le is at least r
bbe0: 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65 20 66 eadable. The f
bbf0: 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a 20 ile can be a.**
bc00: 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a directory..**.**
bc10: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 77 SQLite will alw
bc20: 61 79 73 20 61 6c 6c 6f 63 61 74 65 20 61 74 20 ays allocate at
bc30: 6c 65 61 73 74 20 6d 78 50 61 74 68 6e 61 6d 65 least mxPathname
bc40: 2b 31 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 +1 bytes for the
bc50: 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 .** output buffe
bc60: 72 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2e r xFullPathname.
bc70: 20 20 54 68 65 20 65 78 61 63 74 20 73 69 7a 65 The exact size
bc80: 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 of the output b
bc90: 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f uffer.** is also
bca0: 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61 72 passed as a par
bcb0: 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 20 ameter to both
bcc0: 6d 65 74 68 6f 64 73 2e 20 49 66 20 74 68 65 20 methods. If the
bcd0: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a output buffer.**
bce0: 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e is not large en
bcf0: 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 5f 43 41 ough, [SQLITE_CA
bd00: 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c 64 20 62 NTOPEN] should b
bd10: 65 20 72 65 74 75 72 6e 65 64 2e 20 53 69 6e 63 e returned. Sinc
bd20: 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 68 61 6e e this is.** han
bd30: 64 6c 65 64 20 61 73 20 61 20 66 61 74 61 6c 20 dled as a fatal
bd40: 65 72 72 6f 72 20 62 79 20 53 51 4c 69 74 65 2c error by SQLite,
bd50: 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 vfs implementat
bd60: 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 6e 64 65 ions should ende
bd70: 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 65 76 65 avor.** to preve
bd80: 6e 74 20 74 68 69 73 20 62 79 20 73 65 74 74 69 nt this by setti
bd90: 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 20 74 6f ng mxPathname to
bda0: 20 61 20 73 75 66 66 69 63 69 65 6e 74 6c 79 20 a sufficiently
bdb0: 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a large value..**.
bdc0: 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 ** The xRandomne
bdd0: 73 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c 20 ss(), xSleep(),
bde0: 61 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d 65 and xCurrentTime
bdf0: 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a () interfaces.**
be00: 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c are not strictl
be10: 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20 y a part of the
be20: 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74 20 filesystem, but
be30: 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63 6c they are.** incl
be40: 75 64 65 64 20 69 6e 20 74 68 65 20 56 46 53 20 uded in the VFS
be50: 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 63 6f structure for co
be60: 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20 54 mpleteness..** T
be70: 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29 he xRandomness()
be80: 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 function attemp
be90: 74 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42 79 ts to return nBy
bea0: 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 tes bytes.** of
beb0: 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61 6e good-quality ran
bec0: 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f 75 domness into zOu
bed0: 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 t. The return v
bee0: 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 61 alue is.** the a
bef0: 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 ctual number of
bf00: 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e bytes of randomn
bf10: 65 73 73 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2a ess obtained..**
bf20: 20 54 68 65 20 78 53 6c 65 65 70 28 29 20 6d 65 The xSleep() me
bf30: 74 68 6f 64 20 63 61 75 73 65 73 20 74 68 65 20 thod causes the
bf40: 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 74 calling thread t
bf50: 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a 2a o sleep for at.*
bf60: 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d 62 * least the numb
bf70: 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e er of microsecon
bf80: 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20 78 ds given. The x
bf90: 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a 2a CurrentTime().**
bfa0: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 method returns
bfb0: 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d a Julian Day Num
bfc0: 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 72 ber for the curr
bfd0: 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d ent date and tim
bfe0: 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70 65 64 65 66 e..**.*/.typedef
bff0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
c000: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b vfs sqlite3_vfs;
c010: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
c020: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 vfs {. int iVer
c030: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 sion;
c040: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65 /* Structure ve
c050: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a rsion number */.
c060: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20 int szOsFile;
c070: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
c080: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64 ze of subclassed
c090: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f sqlite3_file */
c0a0: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d . int mxPathnam
c0b0: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d e; /* M
c0c0: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68 aximum file path
c0d0: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 name length */.
c0e0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e sqlite3_vfs *pN
c0f0: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 ext; /* Nex
c100: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 t registered VFS
c110: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
c120: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f *zName; /
c130: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76 * Name of this v
c140: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 irtual file syst
c150: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 em */. void *pA
c160: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 ppData;
c170: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
c180: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69 pplication-speci
c190: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e fic data */. in
c1a0: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 t (*xOpen)(sqlit
c1b0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
c1c0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69 har *zName, sqli
c1d0: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20 te3_file*,.
c1e0: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c int fl
c1f0: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c ags, int *pOutFl
c200: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 ags);. int (*xD
c210: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76 elete)(sqlite3_v
c220: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 fs*, const char
c230: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63 *zName, int sync
c240: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41 Dir);. int (*xA
c250: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76 ccess)(sqlite3_v
c260: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 fs*, const char
c270: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67 *zName, int flag
c280: 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 s, int *pResOut)
c290: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50 ;. int (*xFullP
c2a0: 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 athname)(sqlite3
c2b0: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 _vfs*, const cha
c2c0: 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f r *zName, int nO
c2d0: 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b ut, char *zOut);
c2e0: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70 . void *(*xDlOp
c2f0: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a en)(sqlite3_vfs*
c300: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
c310: 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 64 ilename);. void
c320: 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 6c (*xDlError)(sql
c330: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e ite3_vfs*, int n
c340: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72 Byte, char *zErr
c350: 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 28 Msg);. void (*(
c360: 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65 33 *xDlSym)(sqlite3
c370: 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f 6e _vfs*,void*, con
c380: 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c st char *zSymbol
c390: 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 ))(void);. void
c3a0: 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 71 6c (*xDlClose)(sql
c3b0: 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 64 2a ite3_vfs*, void*
c3c0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 6e 64 );. int (*xRand
c3d0: 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 33 5f omness)(sqlite3_
c3e0: 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c vfs*, int nByte,
c3f0: 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20 20 char *zOut);.
c400: 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 73 71 int (*xSleep)(sq
c410: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 lite3_vfs*, int
c420: 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 microseconds);.
c430: 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e 74 54 int (*xCurrentT
c440: 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ime)(sqlite3_vfs
c450: 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 20 69 *, double*);. i
c460: 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45 72 72 nt (*xGetLastErr
c470: 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a or)(sqlite3_vfs*
c480: 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a , int, char *);.
c490: 20 20 2f 2a 20 4e 65 77 20 66 69 65 6c 64 73 20 /* New fields
c4a0: 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 20 may be appended
c4b0: 69 6e 20 66 69 67 75 72 65 20 76 65 72 73 69 6f in figure versio
c4c0: 6e 73 2e 20 20 54 68 65 20 69 56 65 72 73 69 6f ns. The iVersio
c4d0: 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c n. ** value wil
c4e0: 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68 65 6e l increment when
c4f0: 65 76 65 72 20 74 68 69 73 20 68 61 70 70 65 6e ever this happen
c500: 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 s. */.};../*.**
c510: 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 CAPI3REF: Flags
c520: 66 6f 72 20 74 68 65 20 78 41 63 63 65 73 73 20 for the xAccess
c530: 56 46 53 20 6d 65 74 68 6f 64 20 7b 48 31 31 31 VFS method {H111
c540: 39 30 7d 20 3c 48 31 31 31 34 30 3e 0a 2a 2a 0a 90} <H11140>.**.
c550: 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 ** These integer
c560: 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20 62 constants can b
c570: 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74 68 e used as the th
c580: 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ird parameter to
c590: 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73 20 .** the xAccess
c5a0: 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73 71 method of an [sq
c5b0: 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
c5c0: 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20 64 t. {END} They d
c5d0: 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61 74 etermine.** what
c5e0: 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73 73 kind of permiss
c5f0: 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73 73 ions the xAccess
c600: 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b 69 method is looki
c610: 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57 69 74 68 20 ng for..** With
c620: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
c630: 49 53 54 53 2c 20 74 68 65 20 78 41 63 63 65 73 ISTS, the xAcces
c640: 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d 70 s method.** simp
c650: 6c 79 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 ly checks whethe
c660: 72 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 r the file exist
c670: 73 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 s..** With SQLIT
c680: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
c690: 54 45 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 TE, the xAccess
c6a0: 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73 method.** checks
c6b0: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c whether the fil
c6c0: 65 20 69 73 20 62 6f 74 68 20 72 65 61 64 61 62 e is both readab
c6d0: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2e le and writable.
c6e0: 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f .** With SQLITE_
c6f0: 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65 ACCESS_READ, the
c700: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a xAccess method.
c710: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 ** checks whethe
c720: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 r the file is re
c730: 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 adable..*/.#defi
c740: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 ne SQLITE_ACCESS
c750: 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 65 _EXISTS 0.#de
c760: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 fine SQLITE_ACCE
c770: 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a 23 SS_READWRITE 1.#
c780: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 define SQLITE_AC
c790: 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20 32 CESS_READ 2
c7a0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
c7b0: 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65 : Initialize The
c7c0: 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20 SQLite Library
c7d0: 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30 30 {H10130} <S20000
c7e0: 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S30100>.**.**
c7f0: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 The sqlite3_init
c800: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 ialize() routine
c810: 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 initializes the
c820: 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72 61 .** SQLite libra
c830: 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 ry. The sqlite3
c840: 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75 74 _shutdown() rout
c850: 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 ine.** deallocat
c860: 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 es any resources
c870: 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63 that were alloc
c880: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ated by sqlite3_
c890: 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a initialize()..**
c8a0: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 .** A call to sq
c8b0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
c8c0: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 () is an "effect
c8d0: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 ive" call if it
c8e0: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 is.** the first
c8f0: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 time sqlite3_ini
c900: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 tialize() is inv
c910: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 oked during the
c920: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 lifetime of.** t
c930: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 he process, or i
c940: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73 f it is the firs
c950: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 t time sqlite3_i
c960: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 nitialize() is i
c970: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 nvoked.** follow
c980: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
c990: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
c9a0: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63 . Only an effec
c9b0: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 tive call.** of
c9c0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
c9d0: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e ze() does any in
c9e0: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 itialization. A
c9f0: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a ll other calls.*
ca00: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e * are harmless n
ca10: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 o-ops..**.** A c
ca20: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 all to sqlite3_s
ca30: 68 75 74 64 6f 77 6e 28 29 20 69 73 20 61 6e 20 hutdown() is an
ca40: 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c "effective" call
ca50: 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69 if it is the fi
ca60: 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 rst.** call to s
ca70: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
ca80: 29 20 73 69 6e 63 65 20 74 68 65 20 6c 61 73 74 ) since the last
ca90: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
caa0: 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 ize(). Only.**
cab0: 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c an effective cal
cac0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 l to sqlite3_shu
cad0: 74 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79 tdown() does any
cae0: 20 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f deinitializatio
caf0: 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 n..** All other
cb00: 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
cb10: 5f 73 68 75 74 64 6f 77 6e 28 29 20 61 72 65 20 _shutdown() are
cb20: 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e harmless no-ops.
cb30: 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 .**.** Among oth
cb40: 65 72 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 74 er things, sqlit
cb50: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
cb60: 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 shall invoke.**
cb70: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
cb80: 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 73 ). Similarly, s
cb90: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
cba0: 29 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76 6f 6b ).** shall invok
cbb0: 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 e sqlite3_os_end
cbc0: 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ()..**.** The sq
cbd0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
cbe0: 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 () routine retur
cbf0: 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f ns [SQLITE_OK] o
cc00: 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 49 66 n success..** If
cc10: 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e for some reason
cc20: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 , sqlite3_initia
cc30: 6c 69 7a 65 28 29 20 69 73 20 75 6e 61 62 6c 65 lize() is unable
cc40: 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a to initialize.*
cc50: 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 28 70 * the library (p
cc60: 65 72 68 61 70 73 20 69 74 20 69 73 20 75 6e 61 erhaps it is una
cc70: 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ble to allocate
cc80: 61 20 6e 65 65 64 65 64 20 72 65 73 6f 75 72 63 a needed resourc
cc90: 65 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 20 6d e such.** as a m
cca0: 75 74 65 78 29 20 69 74 20 72 65 74 75 72 6e 73 utex) it returns
ccb0: 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d an [error code]
ccc0: 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c other than [SQL
ccd0: 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 ITE_OK]..**.** T
cce0: 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 he sqlite3_initi
ccf0: 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 alize() routine
cd00: 69 73 20 63 61 6c 6c 65 64 20 69 6e 74 65 72 6e is called intern
cd10: 61 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f 74 68 ally by many oth
cd20: 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 er.** SQLite int
cd30: 65 72 66 61 63 65 73 20 73 6f 20 74 68 61 74 20 erfaces so that
cd40: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75 an application u
cd50: 73 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f 74 20 sually does not
cd60: 6e 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b need to.** invok
cd70: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 e sqlite3_initia
cd80: 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 2e lize() directly.
cd90: 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 5b For example, [
cda0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a sqlite3_open()].
cdb0: 2a 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 ** calls sqlite3
cdc0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 6f _initialize() so
cdd0: 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
cde0: 61 72 79 20 77 69 6c 6c 20 62 65 20 61 75 74 6f ary will be auto
cdf0: 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69 6e 69 matically.** ini
ce00: 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 5b 73 tialized when [s
ce10: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 69 qlite3_open()] i
ce20: 73 20 63 61 6c 6c 65 64 20 69 66 20 69 74 20 68 s called if it h
ce30: 61 73 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 as not be initia
ce40: 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61 64 79 lized.** already
ce50: 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 53 . However, if S
ce60: 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
ce70: 64 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49 d with the [SQLI
ce80: 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
ce90: 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d ].** compile-tim
cea0: 65 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 20 74 e option, then t
ceb0: 68 65 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c he automatic cal
cec0: 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e ls to sqlite3_in
ced0: 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 61 72 itialize().** ar
cee0: 65 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 74 68 e omitted and th
cef0: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 e application mu
cf00: 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f st call sqlite3_
cf10: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 initialize() dir
cf20: 65 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72 20 74 ectly.** prior t
cf30: 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 o using any othe
cf40: 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 r SQLite interfa
cf50: 63 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d 75 6d ce. For maximum
cf60: 20 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a 2a 2a portability,.**
cf70: 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 it is recommend
cf80: 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 ed that applicat
cf90: 69 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e 76 6f ions always invo
cfa0: 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 ke sqlite3_initi
cfb0: 61 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72 65 63 alize().** direc
cfc0: 74 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 73 69 tly prior to usi
cfd0: 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c ng any other SQL
cfe0: 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 ite interface.
cff0: 46 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 0a Future releases.
d000: 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 ** of SQLite may
d010: 20 72 65 71 75 69 72 65 20 74 68 69 73 2e 20 20 require this.
d020: 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
d030: 74 68 65 20 62 65 68 61 76 69 6f 72 20 65 78 68 the behavior exh
d040: 69 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e 20 53 ibited.** when S
d050: 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
d060: 64 20 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 4f d with [SQLITE_O
d070: 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 6d 69 MIT_AUTOINIT] mi
d080: 67 68 74 20 62 65 63 6f 6d 65 20 74 68 65 0a 2a ght become the.*
d090: 2a 20 64 65 66 61 75 6c 74 20 62 65 68 61 76 69 * default behavi
d0a0: 6f 72 20 69 6e 20 73 6f 6d 65 20 66 75 74 75 72 or in some futur
d0b0: 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c e release of SQL
d0c0: 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ite..**.** The s
d0d0: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
d0e0: 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6f 70 routine does op
d0f0: 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 erating-system s
d100: 70 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69 74 69 pecific.** initi
d110: 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65 alization of the
d120: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e SQLite library.
d130: 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 The sqlite3_os
d140: 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74 69 6e _end().** routin
d150: 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66 e undoes the eff
d160: 65 63 74 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f ect of sqlite3_o
d170: 73 5f 69 6e 69 74 28 29 2e 20 20 54 79 70 69 63 s_init(). Typic
d180: 61 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65 72 66 al tasks.** perf
d190: 6f 72 6d 65 64 20 62 79 20 74 68 65 73 65 20 72 ormed by these r
d1a0: 6f 75 74 69 6e 65 73 20 69 6e 63 6c 75 64 65 20 outines include
d1b0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 64 65 allocation or de
d1c0: 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 allocation.** of
d1d0: 20 73 74 61 74 69 63 20 72 65 73 6f 75 72 63 65 static resource
d1e0: 73 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f s, initializatio
d1f0: 6e 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 n of global vari
d200: 61 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74 69 6e ables,.** settin
d210: 67 20 75 70 20 61 20 64 65 66 61 75 6c 74 20 5b g up a default [
d220: 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d 6f 64 sqlite3_vfs] mod
d230: 75 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e 67 20 ule, or setting
d240: 75 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c 74 20 up.** a default
d250: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 75 73 configuration us
d260: 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e ing [sqlite3_con
d270: 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 fig()]..**.** Th
d280: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 e application sh
d290: 6f 75 6c 64 20 6e 65 76 65 72 20 69 6e 76 6f 6b ould never invok
d2a0: 65 20 65 69 74 68 65 72 20 73 71 6c 69 74 65 33 e either sqlite3
d2b0: 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 6f 72 _os_init().** or
d2c0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
d2d0: 29 20 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 ) directly. The
d2e0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f application sho
d2f0: 75 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0a uld only invoke.
d300: 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 ** sqlite3_initi
d310: 61 6c 69 7a 65 28 29 20 61 6e 64 20 73 71 6c 69 alize() and sqli
d320: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 te3_shutdown().
d330: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f The sqlite3_os_
d340: 69 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65 72 66 init().** interf
d350: 61 63 65 20 69 73 20 63 61 6c 6c 65 64 20 61 75 ace is called au
d360: 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 73 tomatically by s
d370: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
d380: 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 e() and.** sqlit
d390: 65 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 20 63 e3_os_end() is c
d3a0: 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 alled by sqlite3
d3b0: 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 41 70 _shutdown(). Ap
d3c0: 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69 6d 70 propriate.** imp
d3d0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 lementations for
d3e0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
d3f0: 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f () and sqlite3_o
d400: 73 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65 20 62 s_end().** are b
d410: 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 uilt into SQLite
d420: 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 when it is comp
d430: 69 6c 65 64 20 66 6f 72 20 75 6e 69 78 2c 20 77 iled for unix, w
d440: 69 6e 64 6f 77 73 2c 20 6f 72 20 6f 73 2f 32 2e indows, or os/2.
d450: 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 74 20 66 .** When built f
d460: 6f 72 20 6f 74 68 65 72 20 70 6c 61 74 66 6f 72 or other platfor
d470: 6d 73 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53 ms (using the [S
d480: 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 QLITE_OS_OTHER=1
d490: 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a ] compile-time.*
d4a0: 2a 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70 * option) the ap
d4b0: 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 plication must s
d4c0: 75 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65 upply a suitable
d4d0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d4e0: 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f for.** sqlite3_o
d4f0: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c s_init() and sql
d500: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 ite3_os_end().
d510: 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 An application-s
d520: 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 upplied.** imple
d530: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c mentation of sql
d540: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f ite3_os_init() o
d550: 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 r sqlite3_os_end
d560: 28 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 ().** must retur
d570: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e n [SQLITE_OK] on
d580: 20 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d success and som
d590: 65 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63 e other [error c
d5a0: 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 ode] upon.** fai
d5b0: 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lure..*/.SQLITE_
d5c0: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
d5d0: 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 initialize(void)
d5e0: 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
d5f0: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
d600: 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f n(void);.SQLITE_
d610: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
d620: 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 os_init(void);.S
d630: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
d640: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 lite3_os_end(voi
d650: 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 d);../*.** CAPI3
d660: 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 REF: Configuring
d670: 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 The SQLite Libr
d680: 61 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 ary {H14100} <S2
d690: 30 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 0000><S30200>.**
d6a0: 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
d6b0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
d6c0: 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 config() interfa
d6d0: 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 ce is used to ma
d6e0: 6b 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 ke global config
d6f0: 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 uration.** chang
d700: 65 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 es to SQLite in
d710: 6f 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 51 order to tune SQ
d720: 4c 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 63 Lite to the spec
d730: 69 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a ific needs of.**
d740: 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
d750: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 . The default c
d760: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 20 onfiguration is
d770: 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 recommended for
d780: 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 most.** applicat
d790: 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69 73 ions and so this
d7a0: 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 routine is usua
d7b0: 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 lly not necessar
d7c0: 79 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f y. It is.** pro
d7d0: 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 vided to support
d7e0: 20 72 61 72 65 20 61 70 70 6c 69 63 61 74 69 6f rare applicatio
d7f0: 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c 20 ns with unusual
d800: 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 needs..**.** The
d810: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
d820: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e ) interface is n
d830: 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 ot threadsafe.
d840: 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a The application.
d850: 2a 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20 74 ** must insure t
d860: 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c hat no other SQL
d870: 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 ite interfaces a
d880: 72 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 re invoked by ot
d890: 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 her.** threads w
d8a0: 68 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e hile sqlite3_con
d8b0: 66 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 fig() is running
d8c0: 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 . Furthermore,
d8d0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
d8e0: 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 .** may only be
d8f0: 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f invoked prior to
d900: 20 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61 6c library initial
d910: 69 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a ization using.**
d920: 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 [sqlite3_initia
d930: 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 lize()] or after
d940: 20 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 shutdown by [sq
d950: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
d960: 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 ]..** Note, howe
d970: 76 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74 65 ver, that sqlite
d980: 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 3_config() can b
d990: 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74 e called as part
d9a0: 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 of the.** imple
d9b0: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 mentation of an
d9c0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
d9d0: 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f ned [sqlite3_os_
d9e0: 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 init()]..**.** T
d9f0: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
da00: 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e t to sqlite3_con
da10: 66 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74 65 fig() is an inte
da20: 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 ger.** [SQLITE_C
da30: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
da40: 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 AD | configurati
da50: 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 on option] that
da60: 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 determines.** wh
da70: 61 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53 at property of S
da80: 51 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20 63 QLite is to be c
da90: 6f 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62 73 onfigured. Subs
daa0: 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 equent arguments
dab0: 0a 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69 .** vary dependi
dac0: 6e 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 ng on the [SQLIT
dad0: 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 E_CONFIG_SINGLET
dae0: 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 HREAD | configur
daf0: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a ation option].**
db00: 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 in the first ar
db10: 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 gument..**.** Wh
db20: 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69 en a configurati
db30: 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 on option is set
db40: 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 , sqlite3_config
db50: 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 () returns [SQLI
db60: 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 TE_OK]..** If th
db70: 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e e option is unkn
db80: 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69 73 own or SQLite is
db90: 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20 74 unable to set t
dba0: 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 he option.** the
dbb0: 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 n this routine r
dbc0: 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 eturns a non-zer
dbd0: 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a o [error code]..
dbe0: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
dbf0: 74 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 ts:.** [H14103]
dc00: 5b 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32 30 [H14106] [H14120
dc10: 5d 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34 31 ] [H14123] [H141
dc20: 32 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 26] [H14129] [H1
dc30: 34 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 4132] [H14135].*
dc40: 2a 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34 31 * [H14138] [H141
dc50: 34 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 41] [H14144] [H1
dc60: 34 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 4147] [H14150] [
dc70: 48 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36 5d H14153] [H14156]
dc80: 20 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 [H14159].** [H1
dc90: 34 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 4162] [H14165] [
dca0: 48 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 H14168].*/.SQLIT
dcb0: 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 E_API SQLITE_EXP
dcc0: 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 ERIMENTAL int sq
dcd0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 lite3_config(int
dce0: 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 , ...);../*.** C
dcf0: 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 API3REF: Configu
dd00: 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e re database conn
dd10: 65 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 ections {H14200
dd20: 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 } <S20000>.** EX
dd30: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
dd40: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f The sqlite3_db_
dd50: 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 config() interfa
dd60: 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 ce is used to ma
dd70: 6b 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ke configuration
dd80: 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61 .** changes to a
dd90: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
dda0: 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 ction]. The int
ddb0: 65 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c 61 erface is simila
ddc0: 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 r to.** [sqlite3
ddd0: 5f 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 70 _config()] excep
dde0: 74 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 t that the chang
ddf0: 65 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73 69 es apply to a si
de00: 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 ngle.** [databas
de10: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 e connection] (s
de20: 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 pecified in the
de30: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 2e first argument).
de40: 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 The.** sqlite3
de50: 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 _db_config() int
de60: 65 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 erface can only
de70: 62 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61 74 be used immediat
de80: 65 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 ely after.** the
de90: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
dea0: 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 20 tion is created
deb0: 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f using [sqlite3_o
dec0: 70 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 pen()],.** [sqli
ded0: 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f te3_open16()], o
dee0: 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f r [sqlite3_open_
def0: 76 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 v2()]. .**.** T
df00: 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
df10: 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 nt to sqlite3_db
df20: 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 _config(D,V,...)
df30: 20 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 is the.** conf
df40: 69 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20 2d iguration verb -
df50: 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 an integer code
df60: 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 that indicates
df70: 77 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f what.** aspect o
df80: 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 f the [database
df90: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 connection] is b
dfa0: 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 2e eing configured.
dfb0: 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f .** The only cho
dfc0: 69 63 65 20 66 6f 72 20 74 68 69 73 20 76 61 6c ice for this val
dfd0: 75 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 ue is [SQLITE_DB
dfe0: 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
dff0: 5d 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 ]..** New verbs
e000: 61 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 are likely to be
e010: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
e020: 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c releases of SQL
e030: 69 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e ite..** Addition
e040: 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65 70 al arguments dep
e050: 65 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62 2e end on the verb.
e060: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
e070: 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d nts:.** [H14203]
e080: 20 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32 30 [H14206] [H1420
e090: 39 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 9] [H14212] [H14
e0a0: 32 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 215].*/.SQLITE_A
e0b0: 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 PI SQLITE_EXPERI
e0c0: 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 MENTAL int sqlit
e0d0: 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c e3_db_config(sql
e0e0: 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e ite3*, int op, .
e0f0: 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ..);../*.** CAPI
e100: 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 3REF: Memory All
e110: 6f 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 ocation Routines
e120: 20 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 {H10155} <S2012
e130: 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
e140: 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 AL.**.** An inst
e150: 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a ance of this obj
e160: 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 ect defines the
e170: 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 interface betwee
e180: 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 n SQLite.** and
e190: 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
e1a0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 allocation rout
e1b0: 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ines..**.** This
e1c0: 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 object is used
e1d0: 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 in only one plac
e1e0: 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 e in the SQLite
e1f0: 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 interface..** A
e200: 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e pointer to an in
e210: 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
e220: 62 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67 bject is the arg
e230: 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c ument to.** [sql
e240: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 ite3_config()] w
e250: 68 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 hen the configur
e260: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a ation option is.
e270: 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
e280: 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 G_MALLOC]. By c
e290: 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 reating an insta
e2a0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
e2b0: 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e ct.** and passin
e2c0: 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 g it to [sqlite3
e2d0: 5f 63 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e _config()] durin
e2e0: 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c g configuration,
e2f0: 20 61 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 an.** applicati
e300: 6f 6e 20 63 61 6e 20 73 70 65 63 69 66 79 20 61 on can specify a
e310: 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 n alternative me
e320: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
e330: 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 subsystem.** for
e340: 20 53 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66 SQLite to use f
e350: 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 or all of its dy
e360: 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65 namic memory nee
e370: 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 ds..**.** Note t
e380: 68 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 hat SQLite comes
e390: 20 77 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e with a built-in
e3a0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
e3b0: 72 20 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72 r that is.** per
e3c0: 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 fectly adequate
e3d0: 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c for the overwhel
e3e0: 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 ming majority of
e3f0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
e400: 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f and that this o
e410: 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 bject is only us
e420: 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d eful to a tiny m
e430: 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 inority of appli
e440: 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 cations.** with
e450: 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f specialized memo
e460: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 ry allocation re
e470: 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 quirements. Thi
e480: 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 s object is.** a
e490: 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 lso used during
e4a0: 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 testing of SQLit
e4b0: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 e in order to sp
e4c0: 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 ecify an alterna
e4d0: 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 tive.** memory a
e4e0: 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 llocator that si
e4f0: 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f mulates memory o
e500: 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e ut-of-memory con
e510: 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 ditions in.** or
e520: 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 der to verify th
e530: 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 at SQLite recove
e540: 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 rs gracefully fr
e550: 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 om such.** condi
e560: 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
e570: 20 78 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c xMalloc, xFree,
e580: 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65 and xRealloc me
e590: 74 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 thods must work
e5a0: 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c like the.** mall
e5b0: 6f 63 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e oc(), free(), an
e5c0: 64 20 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63 d realloc() func
e5d0: 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 tions from the s
e5e0: 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e tandard library.
e5f0: 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f .**.** xSize sho
e600: 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 uld return the a
e610: 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
e620: 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
e630: 74 69 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 tion.** previous
e640: 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ly obtained from
e650: 20 78 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 xMalloc or xRea
e660: 6c 6c 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 lloc. The alloc
e670: 61 74 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 ated size.** is
e680: 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 always at least
e690: 61 73 20 62 69 67 20 61 73 20 74 68 65 20 72 65 as big as the re
e6a0: 71 75 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 quested size but
e6b0: 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a may be larger..
e6c0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 **.** The xRound
e6d0: 75 70 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e up method return
e6e0: 73 20 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 s what would be
e6f0: 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 the allocated si
e700: 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 ze of.** a memor
e710: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 y allocation giv
e720: 65 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 en a particular
e730: 72 65 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 requested size.
e740: 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 Most memory.**
e750: 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 allocators round
e760: 20 75 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 up memory alloc
e770: 61 74 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 ations at least
e780: 74 6f 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 to the next mult
e790: 69 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 iple.** of 8. S
e7a0: 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 ome allocators r
e7b0: 6f 75 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 ound up to a lar
e7c0: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 ger multiple or
e7d0: 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e to a power of 2.
e7e0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 .**.** The xInit
e7f0: 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69 method initiali
e800: 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 zes the memory a
e810: 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20 llocator. (For
e820: 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d example,.** it m
e830: 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e ight allocate an
e840: 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65 y require mutexe
e850: 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20 s or initialize
e860: 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a internal data.**
e870: 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68 structures. Th
e880: 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 e xShutdown meth
e890: 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 od is invoked (i
e8a0: 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a ndirectly) by.**
e8b0: 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f [sqlite3_shutdo
e8c0: 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64 wn()] and should
e8d0: 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 deallocate any
e8e0: 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72 resources acquir
e8f0: 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20 ed.** by xInit.
e900: 20 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f The pAppData po
e910: 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
e920: 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65 the only parame
e930: 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 ter to.** xInit
e940: 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a and xShutdown..*
e950: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
e960: 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
e970: 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d hods sqlite3_mem
e980: 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 _methods;.struct
e990: 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
e9a0: 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28 hods {. void *(
e9b0: 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 *xMalloc)(int);
e9c0: 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
e9d0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e y allocation fun
e9e0: 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
e9f0: 28 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b (*xFree)(void*);
ea00: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 /* Fre
ea10: 65 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 e a prior alloca
ea20: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a tion */. void *
ea30: 28 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64 (*xRealloc)(void
ea40: 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69 *,int); /* Resi
ea50: 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e ze an allocation
ea60: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a */. int (*xSiz
ea70: 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 e)(void*);
ea80: 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 /* Return t
ea90: 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c he size of an al
eaa0: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e location */. in
eab0: 74 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e t (*xRoundup)(in
eac0: 74 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t); /*
ead0: 52 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73 74 Round up request
eae0: 20 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74 size to allocat
eaf0: 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e ion size */. in
eb00: 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a t (*xInit)(void*
eb10: 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ); /*
eb20: 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
eb30: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
eb40: 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75 */. void (*xShu
eb50: 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20 tdown)(void*);
eb60: 20 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c /* Deinitial
eb70: 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ize the memory a
eb80: 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f llocator */. vo
eb90: 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 id *pAppData;
eba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ebb0: 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69 Argument to xIni
ebc0: 74 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77 t() and xShutdow
ebd0: 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a n() */.};../*.**
ebe0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 CAPI3REF: Confi
ebf0: 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 guration Options
ec00: 20 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30 {H10160} <S2000
ec10: 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
ec20: 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 AL.**.** These c
ec30: 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 onstants are the
ec40: 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 available integ
ec50: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e er configuration
ec60: 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a options that.**
ec70: 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 can be passed a
ec80: 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
ec90: 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c ment to the [sql
eca0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 ite3_config()] i
ecb0: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
ecc0: 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f New configuratio
ecd0: 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 n options may be
ece0: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
ecf0: 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c releases of SQL
ed00: 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 ite..** Existing
ed10: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
ed20: 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 ptions might be
ed30: 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 discontinued. A
ed40: 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 pplications.** s
ed50: 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 hould check the
ed60: 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d return code from
ed70: 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 [sqlite3_config
ed80: 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ()] to make sure
ed90: 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c that.** the cal
eda0: 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b l worked. The [
edb0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
edc0: 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c ] interface will
edd0: 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e return a.** non
ede0: 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 -zero [error cod
edf0: 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 e] if a disconti
ee00: 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 nued or unsuppor
ee10: 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ted configuratio
ee20: 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 n option.** is i
ee30: 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 nvoked..**.** <d
ee40: 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 l>.** <dt>SQLITE
ee50: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
ee60: 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 READ</dt>.** <dd
ee70: 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 >There are no ar
ee80: 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 guments to this
ee90: 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 option. This op
eea0: 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a tion disables.**
eeb0: 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e all mutexing an
eec0: 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e d puts SQLite in
eed0: 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20 to a mode where
eee0: 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 it can only be u
eef0: 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67 sed.** by a sing
ef00: 6c 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a le thread.</dd>.
ef10: 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
ef20: 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 _CONFIG_MULTITHR
ef30: 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e EAD</dt>.** <dd>
ef40: 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 There are no arg
ef50: 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f uments to this o
ef60: 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 ption. This opt
ef70: 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 ion disables.**
ef80: 6d 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74 mutexing on [dat
ef90: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
efa0: 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 ] and [prepared
efb0: 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 statement] objec
efc0: 74 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 ts..** The appli
efd0: 63 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e cation is respon
efe0: 73 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c sible for serial
eff0: 69 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a izing access to.
f000: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
f010: 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 nections] and [p
f020: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
f030: 74 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20 ts]. But other
f040: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 mutexes.** are e
f050: 6e 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53 nabled so that S
f060: 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61 QLite will be sa
f070: 66 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d fe to use in a m
f080: 75 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a ulti-threaded.**
f090: 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20 environment as
f0a0: 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74 long as no two t
f0b0: 68 72 65 61 64 73 20 61 74 74 65 6d 70 74 20 74 hreads attempt t
f0c0: 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a o use the same.*
f0d0: 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
f0e0: 65 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73 ection] at the s
f0f0: 61 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74 ame time. See t
f100: 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f he [threading mo
f110: 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 de].** documenta
f120: 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f tion for additio
f130: 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
f140: 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
f150: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 SQLITE_CONFIG_SE
f160: 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a RIALIZED</dt>.**
f170: 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e <dd>There are n
f180: 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 o arguments to t
f190: 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 his option. Thi
f1a0: 73 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73 s option enables
f1b0: 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 .** all mutexes
f1c0: 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65 including the re
f1d0: 63 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78 cursive.** mutex
f1e0: 65 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 es on [database
f1f0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 connection] and
f200: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
f210: 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a ent] objects..**
f220: 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77 In this mode (w
f230: 68 69 63 68 20 69 73 20 74 68 65 20 64 65 66 61 hich is the defa
f240: 75 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20 ult when SQLite
f250: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
f260: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 .** [SQLITE_THRE
f270: 41 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53 ADSAFE=1]) the S
f280: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 QLite library wi
f290: 6c 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c ll itself serial
f2a0: 69 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f ize access.** to
f2b0: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
f2c0: 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 ctions] and [pre
f2d0: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
f2e0: 5d 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a ] so that the.**
f2f0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 application is
f300: 66 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 20 free to use the
f310: 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 same [database c
f320: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68 onnection] or th
f330: 65 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61 e.** same [prepa
f340: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 red statement] i
f350: 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 n different thre
f360: 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 ads at the same
f370: 74 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65 time..** See the
f380: 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
f390: 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 ] documentation
f3a0: 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
f3b0: 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e nformation.</dd>
f3c0: 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
f3d0: 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c E_CONFIG_MALLOC<
f3e0: 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
f3f0: 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
f400: 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
f410: 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
f420: 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
f430: 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c ance of the [sql
f440: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
f450: 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 ] structure. Th
f460: 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 e argument speci
f470: 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 fies.** alternat
f480: 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 ive low-level me
f490: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
f4a0: 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 routines to be u
f4b0: 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a sed in place of.
f4c0: 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ** the memory al
f4d0: 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 location routine
f4e0: 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c s built into SQL
f4f0: 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ite.</dd>.**.**
f500: 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
f510: 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e G_GETMALLOC</dt>
f520: 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
f530: 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
f540: 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
f550: 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
f560: 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
f570: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
f580: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 _mem_methods] st
f590: 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73 ructure. The [s
f5a0: 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
f5b0: 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 ds].** structure
f5c0: 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 is filled with
f5d0: 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 the currently de
f5e0: 66 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c fined memory all
f5f0: 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
f600: 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e ..** This option
f610: 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
f620: 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 overload the def
f630: 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ault memory allo
f640: 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e cation.** routin
f650: 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65 es with a wrappe
f660: 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f r that simulatio
f670: 6e 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ns memory alloca
f680: 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a tion failure or.
f690: 2a 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 ** tracks memory
f6a0: 20 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d usage, for exam
f6b0: 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ple.</dd>.**.**
f6c0: 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
f6d0: 47 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e G_MEMSTATUS</dt>
f6e0: 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
f6f0: 69 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65 ion takes single
f700: 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 argument of typ
f710: 65 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74 e int, interpret
f720: 65 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c ed as a .** bool
f730: 65 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c ean, which enabl
f740: 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 es or disables t
f750: 68 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 he collection of
f760: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f770: 6f 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63 on .** statistic
f780: 73 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64 s. When disabled
f790: 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 , the following
f7a0: 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
f7b0: 73 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e s become .** non
f7c0: 2d 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a -operational:.**
f7d0: 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 <ul>.** <li
f7e0: 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 > [sqlite3_memor
f7f0: 79 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c y_used()].** <
f800: 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d li> [sqlite3_mem
f810: 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d ory_highwater()]
f820: 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 .** <li> [sqli
f830: 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 te3_soft_heap_li
f840: 6d 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e mit()].** <li>
f850: 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 [sqlite3_status
f860: 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a ()].** </ul>.*
f870: 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 * </dd>.**.** <d
f880: 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
f890: 53 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 SCRATCH</dt>.**
f8a0: 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
f8b0: 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 specifies a stat
f8c0: 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 ic memory buffer
f8d0: 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e that SQLite can
f8e0: 20 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 use for.** scra
f8f0: 74 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 tch memory. The
f900: 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67 re are three arg
f910: 75 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 uments: A point
f920: 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 er to the memory
f930: 2c 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 , the.** size of
f940: 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75 each scratch bu
f950: 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74 ffer (sz), and t
f960: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66 he number of buf
f970: 66 65 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73 fers (N). The s
f980: 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 z.** argument mu
f990: 73 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 st be a multiple
f9a0: 20 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70 of 16. The sz p
f9b0: 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 arameter should
f9c0: 62 65 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a be a few bytes.*
f9d0: 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 * larger than th
f9e0: 65 20 61 63 74 75 61 6c 20 73 63 72 61 74 63 68 e actual scratch
f9f0: 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 space required
fa00: 64 75 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 due internal ove
fa10: 72 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 rhead..** The fi
fa20: 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 rst.** argument
fa30: 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 should point to
fa40: 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 an allocation of
fa50: 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 at least sz*N b
fa60: 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a ytes of memory..
fa70: 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 ** SQLite will u
fa80: 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 se no more than
fa90: 6f 6e 65 20 73 63 72 61 74 63 68 20 62 75 66 66 one scratch buff
faa0: 65 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74 er at once per t
fab0: 68 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 hread, so.** N s
fac0: 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 hould be set to
fad0: 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78 the expected max
fae0: 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 imum number of t
faf0: 68 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a hreads. The sz.
fb00: 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f ** parameter sho
fb10: 75 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20 74 uld be 6 times t
fb20: 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c he size of the l
fb30: 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20 argest database
fb40: 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 page size..** Sc
fb50: 72 61 74 63 68 20 62 75 66 66 65 72 73 20 61 72 ratch buffers ar
fb60: 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f e used as part o
fb70: 66 20 74 68 65 20 62 74 72 65 65 20 62 61 6c 61 f the btree bala
fb80: 6e 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 nce operation.
fb90: 49 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20 If.** The btree
fba0: 62 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61 balancer needs a
fbb0: 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 dditional memory
fbc0: 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 beyond what is
fbd0: 70 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73 provided by.** s
fbe0: 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20 6f cratch buffers o
fbf0: 72 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20 r if no scratch
fc00: 62 75 66 66 65 72 20 73 70 61 63 65 20 69 73 20 buffer space is
fc10: 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 specified, then
fc20: 53 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74 SQLite.** goes t
fc30: 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
fc40: 63 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74 c()] to obtain t
fc50: 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 he memory it nee
fc60: 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ds.</dd>.**.** <
fc70: 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
fc80: 5f 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a _PAGECACHE</dt>.
fc90: 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 ** <dd>This opti
fca0: 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 on specifies a s
fcb0: 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 tatic memory buf
fcc0: 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 fer that SQLite
fcd0: 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 can use for.** t
fce0: 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
fcf0: 20 63 61 63 68 65 20 77 69 74 68 20 74 68 65 20 cache with the
fd00: 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 default page cac
fd10: 68 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e he implemenation
fd20: 2e 20 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 66 . .** This conf
fd30: 69 67 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c 64 iguration should
fd40: 20 6e 6f 74 20 62 65 20 75 73 65 64 20 69 66 20 not be used if
fd50: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 an application-d
fd60: 65 66 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63 61 efine page.** ca
fd70: 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
fd80: 6f 6e 20 69 73 20 6c 6f 61 64 65 64 20 75 73 69 on is loaded usi
fd90: 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f ng the SQLITE_CO
fda0: 4e 46 49 47 5f 50 43 41 43 48 45 20 6f 70 74 69 NFIG_PCACHE opti
fdb0: 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 on..** There are
fdc0: 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 three arguments
fdd0: 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 3a to this option:
fde0: 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 A pointer to th
fdf0: 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68 65 e.** memory, the
fe00: 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 size of each pa
fe10: 67 65 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20 ge buffer (sz),
fe20: 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f and the number o
fe30: 66 20 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 f pages (N)..**
fe40: 54 68 65 20 73 7a 20 61 72 67 75 6d 65 6e 74 20 The sz argument
fe50: 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 must be a power
fe60: 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 of two between 5
fe70: 31 32 20 61 6e 64 20 33 32 37 36 38 2e 20 20 54 12 and 32768. T
fe80: 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 he first.** argu
fe90: 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e ment should poin
fea0: 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 t to an allocati
feb0: 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 on of at least s
fec0: 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d z*N bytes of mem
fed0: 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 ory..** SQLite w
fee0: 69 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f ill use the memo
fef0: 72 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 ry provided by t
ff00: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
ff10: 74 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 t to satisfy its
ff20: 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 .** memory needs
ff30: 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e for the first N
ff40: 20 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61 pages that it a
ff50: 64 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 dds to cache. I
ff60: 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 f additional.**
ff70: 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 page cache memor
ff80: 79 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f y is needed beyo
ff90: 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 nd what is provi
ffa0: 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 ded by this opti
ffb0: 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 on, then.** SQLi
ffc0: 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 te goes to [sqli
ffd0: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f te3_malloc()] fo
ffe0: 72 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c r the additional
fff0: 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a storage space..
10000 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ** The implement
10010 61 74 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 ation might use
10020 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 one or more of t
10030 68 65 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 he N buffers to
10040 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 hold .** memory
10050 61 63 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 accounting infor
10060 6d 61 74 69 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a mation. </dd>.**
10070 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
10080 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a ONFIG_HEAP</dt>.
10090 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 ** <dd>This opti
100a0 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 on specifies a s
100b0 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 tatic memory buf
100c0 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 fer that SQLite
100d0 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 will use.** for
100e0 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d all of its dynam
100f0 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ic memory alloca
10100 74 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e tion needs beyon
10110 64 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64 d those provided
10120 0a 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 .** for by [SQLI
10130 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 TE_CONFIG_SCRATC
10140 48 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 H] and [SQLITE_C
10150 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d ONFIG_PAGECACHE]
10160 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 ..** There are t
10170 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 hree arguments:
10180 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
10190 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 6e 75 6d memory, the num
101a0 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 ber of.** bytes
101b0 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 in the memory bu
101c0 66 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 ffer, and the mi
101d0 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e nimum allocation
101e0 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74 68 size. If.** th
101f0 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 e first pointer
10200 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e (the memory poin
10210 74 65 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 ter) is NULL, th
10220 65 6e 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 en SQLite revert
10230 73 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 s.** to using it
10240 73 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 s default memory
10250 20 61 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 allocator (the
10260 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 system malloc()
10270 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c implementation),
10280 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 .** undoing any
10290 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e prior invocation
102a0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 of [SQLITE_CONF
102b0 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 IG_MALLOC]. If
102c0 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f the.** memory po
102d0 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c inter is not NUL
102e0 4c 20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 L and either [SQ
102f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
10300 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 YS3] or.** [SQLI
10310 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
10320 35 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 5] are defined,
10330 74 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 then the alterna
10340 74 69 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 tive memory.** a
10350 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 llocator is enga
10360 67 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c ged to handle al
10370 6c 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d l of SQLites mem
10380 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e ory allocation n
10390 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a eeds.</dd>.**.**
103a0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
103b0 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a IG_MUTEX</dt>.**
103c0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
103d0 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 takes a single
103e0 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 argument which i
103f0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
10400 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 n.** instance of
10410 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 the [sqlite3_mu
10420 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 tex_methods] str
10430 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 67 ucture. The arg
10440 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a ument specifies.
10450 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c ** alternative l
10460 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 72 ow-level mutex r
10470 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 outines to be us
10480 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 ed in place.** t
10490 68 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 he mutex routine
104a0 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c s built into SQL
104b0 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ite.</dd>.**.**
104c0 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
104d0 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e 0a G_GETMUTEX</dt>.
104e0 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 ** <dd>This opti
104f0 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c on takes a singl
10500 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 e argument which
10510 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
10520 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 an.** instance
10530 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
10540 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 mutex_methods] s
10550 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a tructure. The.*
10560 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 * [sqlite3_mutex
10570 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 _methods].** str
10580 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 ucture is filled
10590 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e with the curren
105a0 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 75 74 65 tly defined mute
105b0 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 x routines..** T
105c0 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 his option can b
105d0 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f e used to overlo
105e0 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d ad the default m
105f0 75 74 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a utex allocation.
10600 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 ** routines with
10610 20 61 20 77 72 61 70 70 65 72 20 75 73 65 64 20 a wrapper used
10620 74 6f 20 74 72 61 63 6b 20 6d 75 74 65 78 20 75 to track mutex u
10630 73 61 67 65 20 66 6f 72 20 70 65 72 66 6f 72 6d sage for perform
10640 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e ance.** profilin
10650 67 20 6f 72 20 74 65 73 74 69 6e 67 2c 20 66 6f g or testing, fo
10660 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a r example.</dd>.
10670 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
10680 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 _CONFIG_LOOKASID
10690 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 E</dt>.** <dd>Th
106a0 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 is option takes
106b0 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 68 two arguments th
106c0 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 at determine the
106d0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f default.** memo
106e0 72 79 20 61 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f ry allcation loo
106f0 6b 61 73 69 64 65 20 6f 70 74 69 6d 69 7a 61 74 kaside optimizat
10700 69 6f 6e 2e 20 20 54 68 65 20 66 69 72 73 74 20 ion. The first
10710 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a argument is the.
10720 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 ** size of each
10730 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 lookaside buffer
10740 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 73 65 slot and the se
10750 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62 cond is the numb
10760 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 er of.** slots a
10770 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 llocated to each
10780 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
10790 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a tion.</dd>.**.**
107a0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
107b0 49 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a IG_PCACHE</dt>.*
107c0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f * <dd>This optio
107d0 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 n takes a single
107e0 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
107f0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a is a pointer to.
10800 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 ** an [sqlite3_p
10810 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f cache_methods] o
10820 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a bject. This obj
10830 65 63 74 20 73 70 65 63 69 66 69 65 73 20 74 68 ect specifies th
10840 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 e interface.** t
10850 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 o a custom page
10860 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 cache implementa
10870 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 tion. SQLite ma
10880 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 kes a copy of th
10890 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 e.** object and
108a0 75 73 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 uses it for page
108b0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c cache memory al
108c0 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a locations.</dd>.
108d0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
108e0 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 _CONFIG_GETPCACH
108f0 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 E</dt>.** <dd>Th
10900 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 is option takes
10910 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e a single argumen
10920 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 t which is a poi
10930 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 nter to an.** [s
10940 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
10950 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 thods] object.
10960 53 51 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 SQLite copies of
10970 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
10980 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 page cache imple
10990 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 mentation into t
109a0 68 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e hat object.</dd>
109b0 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a .**.** </dl>.*/.
109c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
109d0 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
109e0 41 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f AD 1 /* nil */
109f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
10a00 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 CONFIG_MULTITHRE
10a10 41 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a AD 2 /* nil *
10a20 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
10a30 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a _CONFIG_SERIALIZ
10a40 45 44 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 ED 3 /* nil
10a50 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
10a60 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 E_CONFIG_MALLOC
10a70 20 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 4 /* sql
10a80 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
10a90 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c * */.#define SQL
10aa0 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 ITE_CONFIG_GETMA
10ab0 4c 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 LLOC 5 /* s
10ac0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
10ad0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ds* */.#define S
10ae0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 QLITE_CONFIG_SCR
10af0 41 54 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a ATCH 6 /*
10b00 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 void*, int sz,
10b10 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 int N */.#define
10b20 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 SQLITE_CONFIG_P
10b30 41 47 45 43 41 43 48 45 20 20 20 20 20 37 20 20 AGECACHE 7
10b40 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a /* void*, int sz
10b50 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 , int N */.#defi
10b60 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
10b70 5f 48 45 41 50 20 20 20 20 20 20 20 20 20 20 38 _HEAP 8
10b80 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 /* void*, int
10b90 6e 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a nByte, int min *
10ba0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
10bb0 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 _CONFIG_MEMSTATU
10bc0 53 20 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c S 9 /* bool
10bd0 65 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ean */.#define S
10be0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 QLITE_CONFIG_MUT
10bf0 45 58 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a EX 10 /*
10c00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
10c10 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 ethods* */.#defi
10c20 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ne SQLITE_CONFIG
10c30 5f 47 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 _GETMUTEX 11
10c40 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 /* sqlite3_mut
10c50 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f ex_methods* */./
10c60 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c * previously SQL
10c70 49 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b ITE_CONFIG_CHUNK
10c80 41 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 ALLOC 12 which i
10c90 73 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f s now unused. */
10ca0 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .#define SQLITE
10cb0 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 _CONFIG_LOOKASID
10cc0 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 E 13 /* int
10cd0 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 int */.#define S
10ce0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 QLITE_CONFIG_PCA
10cf0 43 48 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a CHE 14 /*
10d00 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
10d10 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 methods* */.#def
10d20 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ine SQLITE_CONFI
10d30 47 5f 47 45 54 50 43 41 43 48 45 20 20 20 20 31 G_GETPCACHE 1
10d40 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 5 /* sqlite3_pc
10d50 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f ache_methods* */
10d60 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
10d70 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 : Configuration
10d80 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d Options {H10170}
10d90 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 <S20000>.** EXP
10da0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
10db0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
10dc0 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c are the availabl
10dd0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 e integer config
10de0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
10df0 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 that.** can be p
10e00 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 assed as the sec
10e10 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
10e20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f the [sqlite3_db_
10e30 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 config()] interf
10e40 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 ace..**.** New c
10e50 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
10e60 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 ions may be adde
10e70 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 d in future rele
10e80 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
10e90 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 ** Existing conf
10ea0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
10eb0 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f s might be disco
10ec0 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 ntinued. Applic
10ed0 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 ations.** should
10ee0 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 check the retur
10ef0 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c n code from [sql
10f00 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 ite3_db_config()
10f10 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 ] to make sure t
10f20 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 hat.** the call
10f30 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 worked. The [sq
10f40 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 lite3_db_config(
10f50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c )] interface wil
10f60 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f l return a.** no
10f70 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f n-zero [error co
10f80 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 de] if a discont
10f90 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f inued or unsuppo
10fa0 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 rted configurati
10fb0 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 on option.** is
10fc0 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c invoked..**.** <
10fd0 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 dl>.** <dt>SQLIT
10fe0 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 E_DBCONFIG_LOOKA
10ff0 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIDE</dt>.** <dd
11000 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b >This option tak
11010 65 73 20 74 68 72 65 65 20 61 64 64 69 74 69 6f es three additio
11020 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 nal arguments th
11030 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 at determine the
11040 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 .** [lookaside
11050 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
11060 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 ] configuration
11070 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 for the [databas
11080 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a e connection]..*
11090 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 * The first argu
110a0 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 ment (the third
110b0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
110c0 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 lite3_db_config(
110d0 29 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 )] is a.** point
110e0 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 62 er to a memory b
110f0 75 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 uffer to use for
11100 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 lookaside memor
11110 79 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a y. The first.**
11120 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 argument may be
11130 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 NULL in which c
11140 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ase SQLite will
11150 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 6c 6f 6f allocate the loo
11160 6b 61 73 69 64 65 0a 2a 2a 20 62 75 66 66 65 72 kaside.** buffer
11170 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 itself using [s
11180 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
11190 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 . The second ar
111a0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a gument is the.**
111b0 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f size of each lo
111c0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 okaside buffer s
111d0 6c 6f 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 lot and the thir
111e0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 d argument is th
111f0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 e number of.** s
11200 6c 6f 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 lots. The size
11210 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69 6e of the buffer in
11220 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
11230 65 6e 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 ent must be grea
11240 74 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 ter than.** or e
11250 71 75 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 qual to the prod
11260 75 63 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e uct of the secon
11270 64 20 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 d and third argu
11280 6d 65 6e 74 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ments.</dd>.**.*
11290 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 * </dl>.*/.#defi
112a0 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 ne SQLITE_DBCONF
112b0 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 IG_LOOKASIDE
112c0 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 1001 /* void* i
112d0 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a nt int */.../*.*
112e0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 * CAPI3REF: Enab
112f0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 le Or Disable Ex
11300 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f tended Result Co
11310 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 des {H12200} <S1
11320 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0700>.**.** The
11330 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
11340 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 _result_codes()
11350 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 routine enables
11360 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a or disables the.
11370 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 ** [extended res
11380 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 ult codes] featu
11390 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 re of SQLite. Th
113a0 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c e extended resul
113b0 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 t.** codes are d
113c0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 isabled by defau
113d0 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 lt for historica
113e0 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 l compatibility
113f0 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a considerations..
11400 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
11410 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20 ts:.** [H12201]
11420 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49 [H12202].*/.SQLI
11430 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
11440 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 e3_extended_resu
11450 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 lt_codes(sqlite3
11460 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a *, int onoff);..
11470 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
11480 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 Last Insert Rowi
11490 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 d {H12220} <S107
114a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 00>.**.** Each e
114b0 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 ntry in an SQLit
114c0 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e e table has a un
114d0 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e ique 64-bit sign
114e0 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 ed.** integer ke
114f0 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f y called the [RO
11500 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 WID | "rowid"].
11510 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 The rowid is alw
11520 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a ays available.**
11530 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 as an undeclare
11540 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 d column named R
11550 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 OWID, OID, or _R
11560 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 OWID_ as long as
11570 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 those.** names
11580 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 are not also use
11590 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 d by explicitly
115a0 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 declared columns
115b0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c . If.** the tabl
115c0 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f e has a column o
115d0 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 f type [INTEGER
115e0 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 PRIMARY KEY] the
115f0 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a n that column.**
11600 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 is another alia
11610 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e s for the rowid.
11620 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
11630 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
11640 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d [rowid] of the m
11650 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 ost recent.** su
11660 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 ccessful [INSERT
11670 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 ] into the datab
11680 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 ase from the [da
11690 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
116a0 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 n].** in the fir
116b0 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 st argument. If
116c0 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b no successful [
116d0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 INSERT]s.** have
116e0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f ever occurred o
116f0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 n that database
11700 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f connection, zero
11710 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
11720 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 .** If an [INSER
11730 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e T] occurs within
11740 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e a trigger, then
11750 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 the [rowid] of
11760 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 the inserted.**
11770 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 row is returned
11780 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 by this routine
11790 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 as long as the t
117a0 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e rigger is runnin
117b0 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 g..** But once t
117c0 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 he trigger termi
117d0 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 nates, the value
117e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
117f0 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 s routine.** rev
11800 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 erts to the last
11810 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 value inserted
11820 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 before the trigg
11830 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 er fired..**.**
11840 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 An [INSERT] that
11850 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 fails due to a
11860 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
11870 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a tion is not a.**
11880 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 successful [INS
11890 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f ERT] and does no
118a0 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c t change the val
118b0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ue returned by t
118c0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 his.** routine.
118d0 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 Thus INSERT OR
118e0 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 FAIL, INSERT OR
118f0 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f IGNORE, INSERT O
11900 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 R ROLLBACK,.** a
11910 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f nd INSERT OR ABO
11920 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 RT make no chang
11930 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e es to the return
11940 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a value of this.*
11950 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 * routine when t
11960 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 heir insertion f
11970 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 ails. When INSE
11980 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a RT OR REPLACE.**
11990 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f encounters a co
119a0 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
119b0 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 on, it does not
119c0 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e fail. The.** IN
119d0 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 SERT continues t
119e0 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 o completion aft
119f0 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 er deleting rows
11a00 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 that caused.**
11a10 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 the constraint p
11a20 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 roblem so INSERT
11a30 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c OR REPLACE will
11a40 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a always change.*
11a50 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c * the return val
11a60 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 ue of this inter
11a70 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 face..**.** For
11a80 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
11a90 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e this routine, an
11aa0 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e [INSERT] is con
11ab0 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 sidered to.** be
11ac0 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e successful even
11ad0 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71 if it is subseq
11ae0 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 uently rolled ba
11af0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ck..**.** Requir
11b00 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
11b10 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a 21] [H12223].**.
11b20 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 ** If a separate
11b30 20 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73 thread performs
11b40 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 a new [INSERT]
11b50 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 on the same.** d
11b60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
11b70 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71 on while the [sq
11b80 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
11b90 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75 t_rowid()].** fu
11ba0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
11bb0 67 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 g and thus chang
11bc0 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 es the last inse
11bd0 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74 rt [rowid],.** t
11be0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
11bf0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 turned by [sqlit
11c00 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 e3_last_insert_r
11c10 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e owid()] is.** un
11c20 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 predictable and
11c30 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 might not equal
11c40 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f either the old o
11c50 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73 r the new.** las
11c60 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d t insert [rowid]
11c70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
11c80 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
11c90 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
11ca0 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a t_rowid(sqlite3*
11cb0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
11cc0 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 EF: Count The Nu
11cd0 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 mber Of Rows Mod
11ce0 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c ified {H12240} <
11cf0 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10600>.**.** Th
11d00 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
11d10 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
11d20 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 f database rows
11d30 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 that were change
11d40 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 d.** or inserted
11d50 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 or deleted by t
11d60 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
11d70 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 completed SQL s
11d80 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74 tatement.** on t
11d90 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
11da0 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 nection] specifi
11db0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ed by the first
11dc0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e parameter..** On
11dd0 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 ly changes that
11de0 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 are directly spe
11df0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49 cified by the [I
11e00 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d NSERT], [UPDATE]
11e10 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d ,.** or [DELETE]
11e20 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 statement are c
11e30 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 ounted. Auxilia
11e40 72 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 ry changes cause
11e50 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 d by.** triggers
11e60 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 are not counted
11e70 2e 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 . Use the [sqlit
11e80 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
11e90 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ()] function.**
11ea0 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 to find the tota
11eb0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e l number of chan
11ec0 67 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 ges including ch
11ed0 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 anges caused by
11ee0 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 triggers..**.**
11ef0 43 68 61 6e 67 65 73 20 74 6f 20 61 20 76 69 65 Changes to a vie
11f00 77 20 74 68 61 74 20 61 72 65 20 73 69 6d 75 6c w that are simul
11f10 61 74 65 64 20 62 79 20 61 6e 20 5b 49 4e 53 54 ated by an [INST
11f20 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 0a EAD OF trigger].
11f30 2a 2a 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 ** are not count
11f40 65 64 2e 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74 ed. Only real t
11f50 61 62 6c 65 20 63 68 61 6e 67 65 73 20 61 72 65 able changes are
11f60 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 counted..**.**
11f70 41 20 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69 A "row change" i
11f80 73 20 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20 s a change to a
11f90 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 single row of a
11fa0 73 69 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 single table.**
11fb0 63 61 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53 caused by an INS
11fc0 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 ERT, DELETE, or
11fd0 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 UPDATE statement
11fe0 2e 20 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 . Rows that.**
11ff0 61 72 65 20 63 68 61 6e 67 65 64 20 61 73 20 73 are changed as s
12000 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 20 5b ide effects of [
12010 52 45 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 REPLACE] constra
12020 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a int resolution,.
12030 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f ** rollback, ABO
12040 52 54 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b RT processing, [
12050 44 52 4f 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20 DROP TABLE], or
12060 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 by any other.**
12070 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f mechanisms do no
12080 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72 65 63 t count as direc
12090 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a t row changes..*
120a0 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65 72 20 *.** A "trigger
120b0 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20 73 63 context" is a sc
120c0 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69 6f 6e ope of execution
120d0 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 6e 64 that begins and
120e0 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20 74 68 .** ends with th
120f0 65 20 73 63 72 69 70 74 20 6f 66 20 61 20 5b 43 e script of a [C
12100 52 45 41 54 45 20 54 52 49 47 47 45 52 20 7c 20 REATE TRIGGER |
12110 74 72 69 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f trigger]. .** Mo
12120 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 st SQL statement
12130 73 20 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 s are.** evaluat
12140 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e ed outside of an
12150 79 20 74 72 69 67 67 65 72 2e 20 20 54 68 69 73 y trigger. This
12160 20 69 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76 is the "top lev
12170 65 6c 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 el".** trigger c
12180 6f 6e 74 65 78 74 2e 20 20 49 66 20 61 20 74 72 ontext. If a tr
12190 69 67 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d igger fires from
121a0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 the top level,
121b0 61 0a 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72 a.** new trigger
121c0 20 63 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65 context is ente
121d0 72 65 64 20 66 6f 72 20 74 68 65 20 64 75 72 61 red for the dura
121e0 74 69 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 tion of that one
121f0 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75 .** trigger. Su
12200 62 74 72 69 67 67 65 72 73 20 63 72 65 61 74 65 btriggers create
12210 20 73 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 subcontexts for
12220 20 74 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e their duration.
12230 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b .**.** Calling [
12240 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 sqlite3_exec()]
12250 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 or [sqlite3_step
12260 28 29 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20 ()] recursively
12270 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 does.** not crea
12280 74 65 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 te a new trigger
12290 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 context..**.**
122a0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
122b0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
122c0 20 6f 66 20 64 69 72 65 63 74 20 72 6f 77 20 63 of direct row c
122d0 68 61 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a hanges in the.**
122e0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 most recent INS
122f0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 ERT, UPDATE, or
12300 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 DELETE statement
12310 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 within the same
12320 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 .** trigger cont
12330 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c ext..**.** Thus,
12340 20 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f when called fro
12350 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c m the top level,
12360 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
12370 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 eturns the.** nu
12380 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 mber of changes
12390 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 in the most rece
123a0 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 nt INSERT, UPDAT
123b0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 E, or DELETE.**
123c0 74 68 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72 that also occurr
123d0 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65 ed at the top le
123e0 76 65 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65 vel. Within the
123f0 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67 body of a trigg
12400 65 72 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 er,.** the sqlit
12410 65 33 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74 e3_changes() int
12420 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 63 61 erface can be ca
12430 6c 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 lled to find the
12440 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 number of.** ch
12450 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 anges in the mos
12460 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c t recently compl
12470 65 74 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44 eted INSERT, UPD
12480 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a ATE, or DELETE.*
12490 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 * statement with
124a0 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 in the body of t
124b0 68 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e he same trigger.
124c0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 .** However, the
124d0 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 number returned
124e0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
124f0 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75 e changes.** cau
12500 73 65 64 20 62 79 20 73 75 62 74 72 69 67 67 65 sed by subtrigge
12510 72 73 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68 rs since those h
12520 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f ave their own co
12530 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 ntext..**.** See
12540 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 also the [sqlit
12550 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
12560 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e ()] interface an
12570 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f d the.** [count_
12580 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e changes pragma].
12590 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
125a0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d nts:.** [H12241]
125b0 20 5b 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 [H12243].**.**
125c0 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 If a separate th
125d0 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 read makes chang
125e0 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 es on the same d
125f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
12600 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c on.** while [sql
12610 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 ite3_changes()]
12620 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 is running then
12630 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
12640 65 64 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 ed.** is unpredi
12650 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d ctable and not m
12660 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 eaningful..*/.SQ
12670 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
12680 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c ite3_changes(sql
12690 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ite3*);../*.** C
126a0 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e API3REF: Total N
126b0 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f umber Of Rows Mo
126c0 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20 dified {H12260}
126d0 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10600>.**.** T
126e0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
126f0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
12700 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 of row changes c
12710 61 75 73 65 64 20 62 79 20 5b 49 4e 53 45 52 54 aused by [INSERT
12720 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f ],.** [UPDATE] o
12730 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 r [DELETE] state
12740 6d 65 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20 ments since the
12750 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
12760 74 69 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 tion] was opened
12770 2e 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 ..** The count i
12780 6e 63 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e ncludes all chan
12790 67 65 73 20 66 72 6f 6d 20 61 6c 6c 20 0a 2a 2a ges from all .**
127a0 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 52 [CREATE TRIGGER
127b0 20 7c 20 74 72 69 67 67 65 72 5d 20 63 6f 6e 74 | trigger] cont
127c0 65 78 74 73 2e 20 20 48 6f 77 65 76 65 72 2c 0a exts. However,.
127d0 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 ** the count doe
127e0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 s not include ch
127f0 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d anges used to im
12800 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 plement [REPLACE
12810 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a ] constraints,.*
12820 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f * do rollbacks o
12830 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 r ABORT processi
12840 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 ng, or [DROP TAB
12850 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 LE] processing.
12860 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f The.** count do
12870 65 73 20 6e 6f 74 20 72 6f 77 73 20 6f 66 20 76 es not rows of v
12880 69 65 77 73 20 74 68 61 74 20 66 69 72 65 20 61 iews that fire a
12890 6e 20 5b 49 4e 53 54 45 41 44 20 4f 46 20 74 72 n [INSTEAD OF tr
128a0 69 67 67 65 72 5d 2c 20 74 68 6f 75 67 68 20 69 igger], though i
128b0 66 0a 2a 2a 20 74 68 65 20 49 4e 53 54 45 41 44 f.** the INSTEAD
128c0 20 4f 46 20 74 72 69 67 67 65 72 20 6d 61 6b 65 OF trigger make
128d0 73 20 63 68 61 6e 67 65 73 20 6f 66 20 69 74 73 s changes of its
128e0 20 6f 77 6e 2c 20 74 68 6f 73 65 20 63 68 61 6e own, those chan
128f0 67 65 73 20 61 72 65 0a 2a 2a 20 63 6f 75 6e 74 ges are.** count
12900 65 64 2e 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67 ed..** The chang
12910 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61 es are counted a
12920 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 74 s soon as the st
12930 61 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b atement that mak
12940 65 73 20 74 68 65 6d 20 69 73 0a 2a 2a 20 63 6f es them is.** co
12950 6d 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68 mpleted (when th
12960 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 e statement hand
12970 6c 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 le is passed to
12980 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
12990 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ] or.** [sqlite3
129a0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a _finalize()])..*
129b0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 *.** See also th
129c0 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 e [sqlite3_chang
129d0 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 es()] interface
129e0 61 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e and the.** [coun
129f0 74 5f 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61 t_changes pragma
12a00 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
12a10 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 36 ments:.** [H1226
12a20 31 5d 20 5b 48 31 32 32 36 33 5d 0a 2a 2a 0a 2a 1] [H12263].**.*
12a30 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 * If a separate
12a40 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 thread makes cha
12a50 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 nges on the same
12a60 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
12a70 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 tion.** while [s
12a80 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 qlite3_total_cha
12a90 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 nges()] is runni
12aa0 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 ng then the valu
12ab0 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 73 e.** returned is
12ac0 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 unpredictable a
12ad0 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 nd not meaningfu
12ae0 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 l..*/.SQLITE_API
12af0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f 74 int sqlite3_tot
12b00 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 al_changes(sqlit
12b10 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 e3*);../*.** CAP
12b20 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70 74 I3REF: Interrupt
12b30 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67 20 A Long-Running
12b40 51 75 65 72 79 20 7b 48 31 32 32 37 30 7d 20 3c Query {H12270} <
12b50 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30500>.**.** Th
12b60 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 is function caus
12b70 65 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 64 es any pending d
12b80 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f atabase operatio
12b90 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a 2a n to abort and.*
12ba0 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 20 * return at its
12bb0 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 earliest opportu
12bc0 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74 69 nity. This routi
12bd0 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 0a ne is typically.
12be0 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 ** called in res
12bf0 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72 20 ponse to a user
12c00 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20 70 action such as p
12c10 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c 22 ressing "Cancel"
12c20 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77 68 .** or Ctrl-C wh
12c30 65 72 65 20 74 68 65 20 75 73 65 72 20 77 61 6e ere the user wan
12c40 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79 20 ts a long query
12c50 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61 6c operation to hal
12c60 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 t.** immediately
12c70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73 61 ..**.** It is sa
12c80 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 fe to call this
12c90 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20 74 routine from a t
12ca0 68 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 hread different
12cb0 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72 65 from the.** thre
12cc0 61 64 20 74 68 61 74 20 69 73 20 63 75 72 72 65 ad that is curre
12cd0 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68 65 ntly running the
12ce0 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 database operat
12cf0 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a 20 ion. But it.**
12d00 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63 is not safe to c
12d10 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 all this routine
12d20 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73 with a [databas
12d30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 e connection] th
12d40 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20 at.** is closed
12d50 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 20 62 or might close b
12d60 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 69 6e efore sqlite3_in
12d70 74 65 72 72 75 70 74 28 29 20 72 65 74 75 72 6e terrupt() return
12d80 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 53 s..**.** If an S
12d90 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 QL operation is
12da0 76 65 72 79 20 6e 65 61 72 6c 79 20 66 69 6e 69 very nearly fini
12db0 73 68 65 64 20 61 74 20 74 68 65 20 74 69 6d 65 shed at the time
12dc0 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 when.** sqlite3
12dd0 5f 69 6e 74 65 72 72 75 70 74 28 29 20 69 73 20 _interrupt() is
12de0 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 74 20 called, then it
12df0 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20 61 might not have a
12e00 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a 2a 2a n opportunity.**
12e10 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70 74 to be interrupt
12e20 65 64 20 61 6e 64 20 6d 69 67 68 74 20 63 6f 6e ed and might con
12e30 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65 74 tinue to complet
12e40 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 53 51 ion..**.** An SQ
12e50 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 L operation that
12e60 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20 is interrupted
12e70 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c will return [SQL
12e80 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e 0a ITE_INTERRUPT]..
12e90 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72 72 ** If the interr
12ea0 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61 74 upted SQL operat
12eb0 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54 ion is an INSERT
12ec0 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c , UPDATE, or DEL
12ed0 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 ETE.** that is i
12ee0 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63 69 nside an explici
12ef0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 t transaction, t
12f00 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74 hen the entire t
12f10 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 ransaction.** wi
12f20 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 ll be rolled bac
12f30 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e k automatically.
12f40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
12f50 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 e3_interrupt(D)
12f60 63 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65 63 call is in effec
12f70 74 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72 72 t until all curr
12f80 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a 2a ently running.**
12f90 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
12fa0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e on [database con
12fb0 6e 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70 6c nection] D compl
12fc0 65 74 65 2e 20 20 41 6e 79 20 6e 65 77 20 53 51 ete. Any new SQ
12fd0 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 L statements.**
12fe0 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 that are started
12ff0 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 after the sqlit
13000 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 e3_interrupt() c
13010 61 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20 74 all and before t
13020 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 73 he .** running s
13030 74 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68 65 tatements reache
13040 73 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65 72 s zero are inter
13050 72 75 70 74 65 64 20 61 73 20 69 66 20 74 68 65 rupted as if the
13060 79 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72 75 y had been.** ru
13070 6e 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20 74 nning prior to t
13080 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 he sqlite3_inter
13090 72 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 4e 65 rupt() call. Ne
130a0 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 w SQL statements
130b0 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 .** that are sta
130c0 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 72 rted after the r
130d0 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 unning statement
130e0 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a count reaches z
130f0 65 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 65 ero are.** not e
13100 66 66 65 63 74 65 64 20 62 79 20 74 68 65 20 73 ffected by the s
13110 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
13120 28 29 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f ()..** A call to
13130 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
13140 70 74 28 44 29 20 74 68 61 74 20 6f 63 63 75 72 pt(D) that occur
13150 73 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 s when there are
13160 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 53 no running.** S
13170 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 QL statements is
13180 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 68 61 73 a no-op and has
13190 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53 51 no effect on SQ
131a0 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 L statements.**
131b0 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 that are started
131c0 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 after the sqlit
131d0 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 e3_interrupt() c
131e0 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a all returns..**.
131f0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
13200 0a 2a 2a 20 5b 48 31 32 32 37 31 5d 20 5b 48 31 .** [H12271] [H1
13210 32 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 2272].**.** If t
13220 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
13230 65 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77 68 ection closes wh
13240 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 ile [sqlite3_int
13250 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 69 73 20 errupt()].** is
13260 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61 64 running then bad
13270 20 74 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69 6b things will lik
13280 65 6c 79 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a 53 ely happen..*/.S
13290 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
132a0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
132b0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
132c0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 74 ** CAPI3REF: Det
132d0 65 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51 4c ermine If An SQL
132e0 20 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43 6f Statement Is Co
132f0 6d 70 6c 65 74 65 20 7b 48 31 30 35 31 30 7d 20 mplete {H10510}
13300 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S70200>.**.** T
13310 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
13320 65 20 75 73 65 66 75 6c 20 64 75 72 69 6e 67 20 e useful during
13330 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e 70 command-line inp
13340 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ut to determine
13350 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e if the.** curren
13360 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 74 tly entered text
13370 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20 61 seems to form a
13380 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 complete SQL st
13390 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69 66 atement or.** if
133a0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70 75 additional inpu
133b0 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f t is needed befo
133c0 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20 74 re sending the t
133d0 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c 69 ext into.** SQLi
133e0 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e 20 te for parsing.
133f0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
13400 72 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 return 1 if the
13410 69 6e 70 75 74 20 73 74 72 69 6e 67 0a 2a 2a 20 input string.**
13420 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 61 20 appears to be a
13430 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 61 complete SQL sta
13440 74 65 6d 65 6e 74 2e 20 20 41 20 73 74 61 74 65 tement. A state
13450 6d 65 6e 74 20 69 73 20 6a 75 64 67 65 64 20 74 ment is judged t
13460 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 o be.** complete
13470 20 69 66 20 69 74 20 65 6e 64 73 20 77 69 74 68 if it ends with
13480 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 6f 6b a semicolon tok
13490 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 20 en and is not a
134a0 70 72 65 66 69 78 20 6f 66 20 61 0a 2a 2a 20 77 prefix of a.** w
134b0 65 6c 6c 2d 66 6f 72 6d 65 64 20 43 52 45 41 54 ell-formed CREAT
134c0 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d E TRIGGER statem
134d0 65 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73 ent. Semicolons
134e0 20 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64 that are embedd
134f0 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72 ed within.** str
13500 69 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20 ing literals or
13510 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 quoted identifie
13520 72 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65 r names or comme
13530 6e 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69 nts are not.** i
13540 6e 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e ndependent token
13550 73 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74 s (they are part
13560 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e of the token in
13570 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a which they are.
13580 2a 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64 ** embedded) and
13590 20 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75 thus do not cou
135a0 6e 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e nt as a statemen
135b0 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 57 t terminator. W
135c0 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 61 6e 64 hitespace.** and
135d0 20 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20 66 comments that f
135e0 6f 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c 20 ollow the final
135f0 73 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69 67 semicolon are ig
13600 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 nored..**.** The
13610 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 se routines retu
13620 72 6e 20 30 20 69 66 20 74 68 65 20 73 74 61 74 rn 0 if the stat
13630 65 6d 65 6e 74 20 69 73 20 69 6e 63 6f 6d 70 6c ement is incompl
13640 65 74 65 2e 20 20 49 66 20 61 0a 2a 2a 20 6d 65 ete. If a.** me
13650 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
13660 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c 49 fails, then SQLI
13670 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 TE_NOMEM is retu
13680 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 rned..**.** Thes
13690 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e 6f e routines do no
136a0 74 20 70 61 72 73 65 20 74 68 65 20 53 51 4c 20 t parse the SQL
136b0 73 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73 0a statements thus.
136c0 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74 65 ** will not dete
136d0 63 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c 79 ct syntactically
136e0 20 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e 0a incorrect SQL..
136f0 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 **.** If SQLite
13700 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69 has not been ini
13710 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 5b tialized using [
13720 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
13730 7a 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a 20 ze()] prior .**
13740 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 to invoking sqli
13750 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 te3_complete16()
13760 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e then sqlite3_in
13770 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e itialize() is in
13780 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 voked.** automat
13790 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 ically by sqlite
137a0 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e 20 3_complete16().
137b0 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61 6c If that initial
137c0 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a ization fails,.*
137d0 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 * then the retur
137e0 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c n value from sql
137f0 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 ite3_complete16(
13800 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a 65 ) will be non-ze
13810 72 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 73 ro.** regardless
13820 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e of whether or n
13830 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53 51 4c ot the input SQL
13840 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a is complete..**
13850 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
13860 3a 20 5b 48 31 30 35 31 31 5d 20 5b 48 31 30 35 : [H10511] [H105
13870 31 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 12].**.** The in
13880 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f put to [sqlite3_
13890 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 74 complete()] must
138a0 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 be a zero-termi
138b0 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 38 20 73 nated.** UTF-8 s
138c0 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tring..**.** The
138d0 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 input to [sqlit
138e0 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d e3_complete16()]
138f0 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d must be a zero-
13900 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 terminated.** UT
13910 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 6e F-16 string in n
13920 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
13930 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
13940 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 int sqlite3_comp
13950 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20 lete(const char
13960 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 *sql);.SQLITE_AP
13970 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
13980 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 76 mplete16(const v
13990 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a oid *sql);../*.*
139a0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 * CAPI3REF: Regi
139b0 73 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b 20 ster A Callback
139c0 54 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 To Handle SQLITE
139d0 5f 42 55 53 59 20 45 72 72 6f 72 73 20 7b 48 31 _BUSY Errors {H1
139e0 32 33 31 30 7d 20 3c 53 34 30 34 30 30 3e 0a 2a 2310} <S40400>.*
139f0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
13a00 65 20 73 65 74 73 20 61 20 63 61 6c 6c 62 61 63 e sets a callbac
13a10 6b 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 k function that
13a20 6d 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65 64 might be invoked
13a30 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 61 6e 20 whenever.** an
13a40 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
13a50 74 6f 20 6f 70 65 6e 20 61 20 64 61 74 61 62 61 to open a databa
13a60 73 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 6e se table that an
13a70 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
13a80 6f 72 20 70 72 6f 63 65 73 73 20 68 61 73 20 6c or process has l
13a90 6f 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ocked..**.** If
13aa0 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 the busy callbac
13ab0 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 k is NULL, then
13ac0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 [SQLITE_BUSY] or
13ad0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 [SQLITE_IOERR_B
13ae0 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 69 73 20 72 65 LOCKED].** is re
13af0 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 turned immediate
13b00 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74 65 ly upon encounte
13b10 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20 49 ring the lock. I
13b20 66 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 f the busy callb
13b30 61 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 ack.** is not NU
13b40 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c LL, then the cal
13b50 6c 62 61 63 6b 20 77 69 6c 6c 20 62 65 20 69 6e lback will be in
13b60 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61 voked with two a
13b70 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 rguments..**.**
13b80 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
13b90 6e 74 20 74 6f 20 74 68 65 20 68 61 6e 64 6c 65 nt to the handle
13ba0 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 r is a copy of t
13bb0 68 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 he void* pointer
13bc0 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68 65 which.** is the
13bd0 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 third argument
13be0 74 6f 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f to sqlite3_busy_
13bf0 68 61 6e 64 6c 65 72 28 29 2e 20 20 54 68 65 20 handler(). The
13c00 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
13c10 74 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c 65 to.** the handle
13c20 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 r callback is th
13c30 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 e number of time
13c40 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 20 s that the busy
13c50 68 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20 62 handler has.** b
13c60 65 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 een invoked for
13c70 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76 65 this locking eve
13c80 6e 74 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 62 nt. If the.** b
13c90 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 usy callback ret
13ca0 75 72 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20 urns 0, then no
13cb0 61 64 64 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d additional attem
13cc0 70 74 73 20 61 72 65 20 6d 61 64 65 20 74 6f 0a pts are made to.
13cd0 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 64 61 ** access the da
13ce0 74 61 62 61 73 65 20 61 6e 64 20 5b 53 51 4c 49 tabase and [SQLI
13cf0 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c TE_BUSY] or [SQL
13d00 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
13d10 44 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a D] is returned..
13d20 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 ** If the callba
13d30 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a ck returns non-z
13d40 65 72 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65 ero, then anothe
13d50 72 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 r attempt.** is
13d60 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 made to open the
13d70 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 database for re
13d80 61 64 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79 ading and the cy
13d90 63 6c 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a cle repeats..**.
13da0 2a 2a 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 ** The presence
13db0 6f 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 of a busy handle
13dc0 72 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 r does not guara
13dd0 6e 74 65 65 20 74 68 61 74 20 69 74 20 77 69 6c ntee that it wil
13de0 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 l be invoked.**
13df0 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f when there is lo
13e00 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 49 ck contention. I
13e10 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d 69 f SQLite determi
13e20 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e nes that invokin
13e30 67 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68 61 g the busy.** ha
13e40 6e 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75 ndler could resu
13e50 6c 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b lt in a deadlock
13e60 2c 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68 65 , it will go ahe
13e70 61 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 ad and return [S
13e80 51 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 6f QLITE_BUSY].** o
13e90 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f r [SQLITE_IOERR_
13ea0 42 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61 64 BLOCKED] instead
13eb0 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 of invoking the
13ec0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a busy handler..*
13ed0 2a 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 65 * Consider a sce
13ee0 6e 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 20 nario where one
13ef0 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 process is holdi
13f00 6e 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 74 ng a read lock t
13f10 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 79 hat.** it is try
13f20 69 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 ing to promote t
13f30 6f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 o a reserved loc
13f40 6b 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f 6e k and.** a secon
13f50 64 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c d process is hol
13f60 64 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20 ding a reserved
13f70 6c 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 lock that it is
13f80 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 6f trying.** to pro
13f90 6d 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c 75 mote to an exclu
13fa0 73 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 20 sive lock. The
13fb0 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 61 first process ca
13fc0 6e 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a 20 nnot proceed.**
13fd0 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62 6c because it is bl
13fe0 6f 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 63 ocked by the sec
13ff0 6f 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f ond and the seco
14000 6e 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f nd process canno
14010 74 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 63 t.** proceed bec
14020 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b ause it is block
14030 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 2e ed by the first.
14040 20 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 73 If both proces
14050 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 ses.** invoke th
14060 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2c e busy handlers,
14070 20 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d 61 neither will ma
14080 6b 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 2e ke any progress.
14090 20 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a 20 Therefore,.**
140a0 53 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 5b SQLite returns [
140b0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f 72 SQLITE_BUSY] for
140c0 20 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 the first proce
140d0 73 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 20 ss, hoping that
140e0 74 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e 64 this.** will ind
140f0 75 63 65 20 74 68 65 20 66 69 72 73 74 20 70 72 uce the first pr
14100 6f 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 65 ocess to release
14110 20 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 61 its read lock a
14120 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 20 nd allow.** the
14130 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 74 second process t
14140 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a 2a o proceed..**.**
14150 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73 The default bus
14160 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 y callback is NU
14170 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 LL..**.** The [S
14180 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 6f QLITE_BUSY] erro
14190 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 74 r is converted t
141a0 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o [SQLITE_IOERR_
141b0 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65 6e BLOCKED].** when
141c0 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74 68 SQLite is in th
141d0 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c 61 e middle of a la
141e0 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 rge transaction
141f0 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a 2a where all the.**
14200 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e 6f changes will no
14210 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20 69 t fit into the i
14220 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 n-memory cache.
14230 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 SQLite will.**
14240 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 52 already hold a R
14250 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 ESERVED lock on
14260 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
14270 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73 0a e, but it needs.
14280 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 ** to promote th
14290 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c 55 is lock to EXCLU
142a0 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74 20 SIVE so that it
142b0 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65 0a can spill cache.
142c0 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74 68 ** pages into th
142d0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
142e0 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f 20 without harm to
142f0 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 concurrent.** re
14300 61 64 65 72 73 2e 20 20 49 66 20 69 74 20 69 73 aders. If it is
14310 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d 6f unable to promo
14320 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68 65 te the lock, the
14330 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a n the in-memory.
14340 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62 65 ** cache will be
14350 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f left in an inco
14360 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 20 61 nsistent state a
14370 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72 0a nd so the error.
14380 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d 6f ** code is promo
14390 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65 6c ted from the rel
143a0 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20 5b atively benign [
143b0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f 0a SQLITE_BUSY] to.
143c0 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76 65 ** the more seve
143d0 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 re [SQLITE_IOERR
143e0 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69 73 _BLOCKED]. This
143f0 20 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f 6d error code prom
14400 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73 20 otion.** forces
14410 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c an automatic rol
14420 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68 61 lback of the cha
14430 6e 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a 2a nges. See the.*
14440 2a 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73 74 * <a href="/cvst
14450 72 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 75 rac/wiki?p=Corru
14460 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 ptionFollowingBu
14470 73 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f 72 syError">.** Cor
14480 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 ruptionFollowing
14490 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 69 BusyError</a> wi
144a0 6b 69 20 70 61 67 65 20 66 6f 72 20 61 20 64 69 ki page for a di
144b0 73 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 0a scussion of why.
144c0 2a 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f 72 ** this is impor
144d0 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 tant..**.** Ther
144e0 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 20 e can only be a
144f0 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e 64 single busy hand
14500 6c 65 72 20 64 65 66 69 6e 65 64 20 66 6f 72 20 ler defined for
14510 65 61 63 68 0a 2a 2a 20 5b 64 61 74 61 62 61 73 each.** [databas
14520 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 e connection].
14530 53 65 74 74 69 6e 67 20 61 20 6e 65 77 20 62 75 Setting a new bu
14540 73 79 20 68 61 6e 64 6c 65 72 20 63 6c 65 61 72 sy handler clear
14550 73 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 6f 75 s any.** previou
14560 73 6c 79 20 73 65 74 20 68 61 6e 64 6c 65 72 2e sly set handler.
14570 20 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c 6c Note that call
14580 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 ing [sqlite3_bus
14590 79 5f 74 69 6d 65 6f 75 74 28 29 5d 0a 2a 2a 20 y_timeout()].**
145a0 77 69 6c 6c 20 61 6c 73 6f 20 73 65 74 20 6f 72 will also set or
145b0 20 63 6c 65 61 72 20 74 68 65 20 62 75 73 79 20 clear the busy
145c0 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 handler..**.** T
145d0 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b he busy callback
145e0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 74 61 6b 65 should not take
145f0 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 77 68 69 any actions whi
14600 63 68 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a 2a ch modify the.**
14610 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14620 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 tion that invoke
14630 64 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c d the busy handl
14640 65 72 2e 20 20 41 6e 79 20 73 75 63 68 20 61 63 er. Any such ac
14650 74 69 6f 6e 73 0a 2a 2a 20 72 65 73 75 6c 74 20 tions.** result
14660 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 in undefined beh
14670 61 76 69 6f 72 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 avior..** .** Re
14680 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
14690 48 31 32 33 31 31 5d 20 5b 48 31 32 33 31 32 5d H12311] [H12312]
146a0 20 5b 48 31 32 33 31 34 5d 20 5b 48 31 32 33 31 [H12314] [H1231
146b0 36 5d 20 5b 48 31 32 33 31 38 5d 0a 2a 2a 0a 2a 6] [H12318].**.*
146c0 2a 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65 72 * A busy handler
146d0 20 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65 20 must not close
146e0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
146f0 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 5b 70 nection.** or [p
14700 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
14710 74 5d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 t] that invoked
14720 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 the busy handler
14730 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
14740 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 int sqlite3_busy
14750 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 _handler(sqlite3
14760 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c *, int(*)(void*,
14770 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f int), void*);../
14780 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 *.** CAPI3REF: S
14790 65 74 20 41 20 42 75 73 79 20 54 69 6d 65 6f 75 et A Busy Timeou
147a0 74 20 7b 48 31 32 33 34 30 7d 20 3c 53 34 30 34 t {H12340} <S404
147b0 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 10>.**.** This r
147c0 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b 73 outine sets a [s
147d0 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 qlite3_busy_hand
147e0 6c 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64 6c ler | busy handl
147f0 65 72 5d 20 74 68 61 74 20 73 6c 65 65 70 73 0a er] that sleeps.
14800 2a 2a 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 ** for a specifi
14810 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d ed amount of tim
14820 65 20 77 68 65 6e 20 61 20 74 61 62 6c 65 20 69 e when a table i
14830 73 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20 68 s locked. The h
14840 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c 6c 20 73 andler.** will s
14850 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65 20 74 69 leep multiple ti
14860 6d 65 73 20 75 6e 74 69 6c 20 61 74 20 6c 65 61 mes until at lea
14870 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63 st "ms" millisec
14880 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67 onds of sleeping
14890 0a 2a 2a 20 68 61 76 65 20 61 63 63 75 6d 75 6c .** have accumul
148a0 61 74 65 64 2e 20 7b 48 31 32 33 34 33 7d 20 41 ated. {H12343} A
148b0 66 74 65 72 20 22 6d 73 22 20 6d 69 6c 6c 69 73 fter "ms" millis
148c0 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 econds of sleepi
148d0 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c ng,.** the handl
148e0 65 72 20 72 65 74 75 72 6e 73 20 30 20 77 68 69 er returns 0 whi
148f0 63 68 20 63 61 75 73 65 73 20 5b 73 71 6c 69 74 ch causes [sqlit
14900 65 33 5f 73 74 65 70 28 29 5d 20 74 6f 20 72 65 e3_step()] to re
14910 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f turn.** [SQLITE_
14920 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 BUSY] or [SQLITE
14930 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e _IOERR_BLOCKED].
14940 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 .**.** Calling t
14950 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 his routine with
14960 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65 73 an argument les
14970 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 s than or equal
14980 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 72 6e 73 to zero.** turns
14990 20 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68 61 off all busy ha
149a0 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ndlers..**.** Th
149b0 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 ere can only be
149c0 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 a single busy ha
149d0 6e 64 6c 65 72 20 66 6f 72 20 61 20 70 61 72 74 ndler for a part
149e0 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62 icular.** [datab
149f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
14a00 61 6e 79 20 61 6e 79 20 67 69 76 65 6e 20 6d 6f any any given mo
14a10 6d 65 6e 74 2e 20 20 49 66 20 61 6e 6f 74 68 65 ment. If anothe
14a20 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a r busy handler.*
14a30 2a 20 77 61 73 20 64 65 66 69 6e 65 64 20 20 28 * was defined (
14a40 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 using [sqlite3_b
14a50 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 29 20 usy_handler()])
14a60 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 prior to calling
14a70 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
14a80 2c 20 74 68 61 74 20 6f 74 68 65 72 20 62 75 73 , that other bus
14a90 79 20 68 61 6e 64 6c 65 72 20 69 73 20 63 6c 65 y handler is cle
14aa0 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ared..**.** Requ
14ab0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
14ac0 32 33 34 31 5d 20 5b 48 31 32 33 34 33 5d 20 5b 2341] [H12343] [
14ad0 48 31 32 33 34 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H12344].*/.SQLIT
14ae0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
14af0 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 73 3_busy_timeout(s
14b00 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73 29 qlite3*, int ms)
14b10 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
14b20 46 3a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 52 F: Convenience R
14b30 6f 75 74 69 6e 65 73 20 46 6f 72 20 52 75 6e 6e outines For Runn
14b40 69 6e 67 20 51 75 65 72 69 65 73 20 7b 48 31 32 ing Queries {H12
14b50 33 37 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 370} <S10000>.**
14b60 0a 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20 .** Definition:
14b70 41 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 6c A <b>result tabl
14b80 65 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 20 e</b> is memory
14b90 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 data structure c
14ba0 72 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a 2a reated by the.**
14bb0 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 [sqlite3_get_ta
14bc0 62 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65 ble()] interface
14bd0 2e 20 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c . A result tabl
14be0 65 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a e records the.**
14bf0 20 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20 complete query
14c00 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65 results from one
14c10 20 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73 or more queries
14c20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c ..**.** The tabl
14c30 65 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68 e conceptually h
14c40 61 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72 as a number of r
14c50 6f 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e ows and columns.
14c60 20 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 6e But.** these n
14c70 75 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20 70 umbers are not p
14c80 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c art of the resul
14c90 74 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e 20 t table itself.
14ca0 20 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 72 These.** number
14cb0 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 73 s are obtained s
14cc0 65 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 20 eparately. Let
14cd0 4e 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 N be the number
14ce0 6f 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 4d of rows.** and M
14cf0 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f be the number o
14d00 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a f columns..**.**
14d10 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 A result table
14d20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 is an array of p
14d30 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d ointers to zero-
14d40 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
14d50 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68 65 strings..** The
14d60 72 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 65 re are (N+1)*M e
14d70 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 lements in the a
14d80 72 72 61 79 2e 20 20 54 68 65 20 66 69 72 73 74 rray. The first
14d90 20 4d 20 70 6f 69 6e 74 65 72 73 20 70 6f 69 6e M pointers poin
14da0 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65 72 t.** to zero-ter
14db0 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 minated strings
14dc0 74 68 61 74 20 20 63 6f 6e 74 61 69 6e 20 74 68 that contain th
14dd0 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 e names of the c
14de0 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 olumns..** The r
14df0 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73 emaining entries
14e00 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71 75 all point to qu
14e10 65 72 79 20 72 65 73 75 6c 74 73 2e 20 20 4e 55 ery results. NU
14e20 4c 4c 20 76 61 6c 75 65 73 20 72 65 73 75 6c 74 LL values result
14e30 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69 6e .** in NULL poin
14e40 74 65 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65 72 ters. All other
14e50 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 20 74 values are in t
14e60 68 65 69 72 20 55 54 46 2d 38 20 7a 65 72 6f 2d heir UTF-8 zero-
14e70 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 terminated.** st
14e80 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 ring representat
14e90 69 6f 6e 20 61 73 20 72 65 74 75 72 6e 65 64 20 ion as returned
14ea0 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 by [sqlite3_colu
14eb0 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a mn_text()]..**.*
14ec0 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 * A result table
14ed0 20 6d 69 67 68 74 20 63 6f 6e 73 69 73 74 20 6f might consist o
14ee0 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d 65 f one or more me
14ef0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
14f00 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 ..** It is not s
14f10 61 66 65 20 74 6f 20 70 61 73 73 20 61 20 72 65 afe to pass a re
14f20 73 75 6c 74 20 74 61 62 6c 65 20 64 69 72 65 63 sult table direc
14f30 74 6c 79 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f tly to [sqlite3_
14f40 66 72 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72 65 free()]..** A re
14f50 73 75 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 6c sult table shoul
14f60 64 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 d be deallocated
14f70 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
14f80 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a free_table()]..*
14f90 2a 0a 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d 70 *.** As an examp
14fa0 6c 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 le of the result
14fb0 20 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20 73 table format, s
14fc0 75 70 70 6f 73 65 20 61 20 71 75 65 72 79 20 72 uppose a query r
14fd0 65 73 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20 66 esult.** is as f
14fe0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ollows:.**.** <b
14ff0 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
15000 2a 2a 20 20 20 20 20 20 20 20 4e 61 6d 65 20 20 ** Name
15010 20 20 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 20 | Age.**
15020 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------
15030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
15040 20 20 20 20 20 20 20 20 41 6c 69 63 65 20 20 20 Alice
15050 20 20 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 20 | 43.**
15060 20 20 20 42 6f 62 20 20 20 20 20 20 20 20 20 7c Bob |
15070 20 32 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 69 28.** Ci
15080 6e 64 79 20 20 20 20 20 20 20 7c 20 32 31 0a 2a ndy | 21.*
15090 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
150a0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 uote>.**.** Ther
150b0 65 20 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d 6e e are two column
150c0 20 28 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72 65 (M==2) and thre
150d0 65 20 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 20 e rows (N==3).
150e0 54 68 75 73 20 74 68 65 0a 2a 2a 20 72 65 73 75 Thus the.** resu
150f0 6c 74 20 74 61 62 6c 65 20 68 61 73 20 38 20 65 lt table has 8 e
15100 6e 74 72 69 65 73 2e 20 20 53 75 70 70 6f 73 65 ntries. Suppose
15110 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c the result tabl
15120 65 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 e is stored.** i
15130 6e 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 73 n an array names
15140 20 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 6e azResult. Then
15150 20 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 20 azResult holds
15160 74 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a 2a this content:.**
15170 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
15180 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 <pre>.**
15190 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 30 5d 20 azResult[0]
151a0 3d 20 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20 20 = "Name";.**
151b0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 azResult[
151c0 3b 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a 20 ;1] = "Age";.**
151d0 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
151e0 23 39 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65 22 #91;2] = "Alice"
151f0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 ;.** azRe
15200 73 75 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22 34 sult[3] = "4
15210 33 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 3";.** az
15220 52 65 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d 20 Result[4] =
15230 22 42 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20 20 "Bob";.**
15240 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 35 5d azResult[5]
15250 20 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20 20 = "28";.**
15260 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b azResult[
15270 36 5d 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a 2a 6] = "Cindy";.**
15280 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
15290 26 23 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b 0a [7] = "21";.
152a0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b ** </pre></block
152b0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 quote>.**.** The
152c0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 sqlite3_get_tab
152d0 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 76 le() function ev
152e0 61 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 6d aluates one or m
152f0 6f 72 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e ore.** semicolon
15300 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 -separated SQL s
15310 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 tatements in the
15320 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
15330 20 55 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e 67 UTF-8.** string
15340 20 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72 61 of its 2nd para
15350 6d 65 74 65 72 2e 20 20 49 74 20 72 65 74 75 72 meter. It retur
15360 6e 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c ns a result tabl
15370 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69 6e e to the.** poin
15380 74 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74 73 ter given in its
15390 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 3rd parameter..
153a0 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 **.** After the
153b0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
153c0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 has finished us
153d0 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2c 20 ing the result,
153e0 69 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61 73 it should.** pas
153f0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f s the pointer to
15400 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c the result tabl
15410 65 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 e to sqlite3_fre
15420 65 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 e_table() in ord
15430 65 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73 65 er to.** release
15440 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 the memory that
15450 20 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 20 was malloced.
15460 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77 Because of the w
15470 61 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 ay the.** [sqlit
15480 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 70 e3_malloc()] hap
15490 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c 69 pens within sqli
154a0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 2c te3_get_table(),
154b0 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 the calling.**
154c0 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f function must no
154d0 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b 73 t try to call [s
154e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 64 qlite3_free()] d
154f0 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a 2a irectly. Only.*
15500 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f * [sqlite3_free_
15510 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62 6c 65 table()] is able
15520 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 to release the
15530 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79 20 memory properly
15540 61 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a 2a and safely..**.*
15550 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 * The sqlite3_ge
15560 74 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 72 66 t_table() interf
15570 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 ace is implement
15580 65 64 20 61 73 20 61 20 77 72 61 70 70 65 72 20 ed as a wrapper
15590 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 around.** [sqlit
155a0 65 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 68 65 e3_exec()]. The
155b0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 sqlite3_get_tab
155c0 6c 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 le() routine doe
155d0 73 20 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73 s not have acces
155e0 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74 65 s.** to any inte
155f0 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 rnal data struct
15600 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 ures of SQLite.
15610 20 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 74 68 It uses only th
15620 65 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 65 e public.** inte
15630 72 66 61 63 65 20 64 65 66 69 6e 65 64 20 68 65 rface defined he
15640 72 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 65 71 re. As a conseq
15650 75 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 74 68 uence, errors th
15660 61 74 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a at occur in the.
15670 2a 2a 20 77 72 61 70 70 65 72 20 6c 61 79 65 72 ** wrapper layer
15680 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 outside of the
15690 69 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74 65 internal [sqlite
156a0 33 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c 20 61 3_exec()] call a
156b0 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 63 re not.** reflec
156c0 74 65 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e ted in subsequen
156d0 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 t calls to [sqli
156e0 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 6f te3_errcode()] o
156f0 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 r [sqlite3_errms
15700 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 g()]..**.** Requ
15710 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
15720 32 33 37 31 5d 20 5b 48 31 32 33 37 33 5d 20 5b 2371] [H12373] [
15730 48 31 32 33 37 34 5d 20 5b 48 31 32 33 37 36 5d H12374] [H12376]
15740 20 5b 48 31 32 33 37 39 5d 20 5b 48 31 32 33 38 [H12379] [H1238
15750 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 2].*/.SQLITE_API
15760 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 int sqlite3_get
15770 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 _table(. sqlite
15780 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
15790 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 /* An open datab
157a0 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ase */. const c
157b0 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f har *zSql, /
157c0 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c * SQL to be eval
157d0 75 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 uated */. char
157e0 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 ***pazResult,
157f0 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74 /* Results of t
15800 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e he query */. in
15810 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 t *pnRow,
15820 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
15830 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69 result rows wri
15840 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 tten here */. i
15850 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 nt *pnColumn,
15860 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
15870 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 f result columns
15880 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
15890 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d . char **pzErrm
158a0 73 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f sg /* Erro
158b0 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 r msg written he
158c0 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f re */.);.SQLITE_
158d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
158e0 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72 _free_table(char
158f0 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a **result);../*.
15900 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72 ** CAPI3REF: For
15910 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72 matted String Pr
15920 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 inting Functions
15930 20 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30 {H17400} <S7000
15940 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 0><S20000>.**.**
15950 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
15960 61 72 65 20 77 6f 72 6b 61 6c 69 6b 65 73 20 6f are workalikes o
15970 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22 f the "printf()"
15980 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 family of funct
15990 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 ions.** from the
159a0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 standard C libr
159b0 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ary..**.** The s
159c0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 qlite3_mprintf()
159d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70 and sqlite3_vmp
159e0 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73 rintf() routines
159f0 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20 write their.**
15a00 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d results into mem
15a10 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
15a20 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
15a30 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 c()]..** The str
15a40 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 ings returned by
15a50 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69 these two routi
15a60 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a nes should be.**
15a70 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71 released by [sq
15a80 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20 lite3_free()].
15a90 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65 Both routines re
15aa0 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 turn a.** NULL p
15ab0 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74 ointer if [sqlit
15ac0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20 e3_malloc()] is
15ad0 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 unable to alloca
15ae0 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d te enough.** mem
15af0 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ory to hold the
15b00 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 resulting string
15b10 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74 ..**.** In sqlit
15b20 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f e3_snprintf() ro
15b30 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 utine is similar
15b40 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22 to "snprintf()"
15b50 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61 from.** the sta
15b60 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e ndard C library.
15b70 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 The result is
15b80 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
15b90 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c .** buffer suppl
15ba0 69 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e ied as the secon
15bb0 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73 d parameter whos
15bc0 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20 e size is given
15bd0 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 by.** the first
15be0 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20 parameter. Note
15bf0 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f that the order o
15c00 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74 f the.** first t
15c10 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 wo parameters is
15c20 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73 reversed from s
15c30 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73 nprintf(). This
15c40 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72 is an.** histor
15c50 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68 ical accident th
15c60 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78 at cannot be fix
15c70 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b ed without break
15c80 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 ing.** backwards
15c90 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20 compatibility.
15ca0 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 Note also that
15cb0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
15cc0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
15cd0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62 pointer to its b
15ce0 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 uffer instead of
15cf0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
15d00 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74 * characters act
15d10 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e ually written in
15d20 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20 to the buffer.
15d30 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a We admit that.**
15d40 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
15d50 68 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65 haracters writte
15d60 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72 n would be a mor
15d70 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a e useful return.
15d80 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20 ** value but we
15d90 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 cannot change th
15da0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
15db0 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 of sqlite3_snpr
15dc0 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69 intf().** now wi
15dd0 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63 thout breaking c
15de0 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a ompatibility..**
15df0 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74 .** As long as t
15e00 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69 he buffer size i
15e10 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a s greater than z
15e20 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70 ero, sqlite3_snp
15e30 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61 rintf().** guara
15e40 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62 ntees that the b
15e50 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20 uffer is always
15e60 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e zero-terminated.
15e70 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 The first.** p
15e80 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20 arameter "n" is
15e90 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f the total size o
15ea0 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e f the buffer, in
15eb0 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f cluding space fo
15ec0 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65 r.** the zero te
15ed0 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68 rminator. So th
15ee0 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67 e longest string
15ef0 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d that can be com
15f00 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74 pletely.** writt
15f10 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63 en will be n-1 c
15f20 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a haracters..**.**
15f30 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
15f40 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f all implement so
15f50 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f me additional fo
15f60 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69 rmatting.** opti
15f70 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65 ons that are use
15f80 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63 ful for construc
15f90 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 ting SQL stateme
15fa0 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 nts..** All of t
15fb0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28 he usual printf(
15fc0 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 ) formatting opt
15fd0 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 ions apply. In
15fe0 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a addition, there.
15ff0 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20 ** is are "%q",
16000 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f "%Q", and "%z" o
16010 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ptions..**.** Th
16020 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b e %q option work
16030 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61 s like %s in tha
16040 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73 t it substitutes
16050 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 a null-terminat
16060 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f ed.** string fro
16070 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c m the argument l
16080 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73 ist. But %q als
16090 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20 o doubles every
160a0 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a '\'' character..
160b0 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65 ** %q is designe
160c0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
160d0 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 a string litera
160e0 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20 l. By doubling
160f0 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61 each '\''.** cha
16100 72 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65 racter it escape
16110 73 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72 s that character
16120 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74 and allows it t
16130 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e o be inserted in
16140 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 to.** the string
16150 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d ..**.** For exam
16160 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20 ple, assume the
16170 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20 string variable
16180 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74 zText contains t
16190 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ext as follows:.
161a0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
161b0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 e><pre>.** char
161c0 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20 *zText = "It's
161d0 61 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a a happy day!";.*
161e0 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
161f0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 uote>.**.** One
16200 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78 can use this tex
16210 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 t in an SQL stat
16220 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 ement as follows
16230 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
16240 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 ote><pre>.** ch
16250 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 ar *zSQL = sqlit
16260 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 e3_mprintf("INSE
16270 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 RT INTO table VA
16280 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65 LUES('%q')", zTe
16290 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 xt);.** sqlite3
162a0 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 _exec(db, zSQL,
162b0 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 0, 0, 0);.** sq
162c0 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 lite3_free(zSQL)
162d0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f ;.** </pre></blo
162e0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 ckquote>.**.** B
162f0 65 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f ecause the %q fo
16300 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75 rmat string is u
16310 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68 sed, the '\'' ch
16320 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74 aracter in zText
16330 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61 .** is escaped a
16340 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72 nd the SQL gener
16350 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f ated is as follo
16360 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ws:.**.** <block
16370 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
16380 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c INSERT INTO tabl
16390 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73 e1 VALUES('It''s
163a0 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a a happy day!').
163b0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b ** </pre></block
163c0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 quote>.**.** Thi
163d0 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48 s is correct. H
163e0 61 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e ad we used %s in
163f0 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65 stead of %q, the
16400 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a generated SQL.*
16410 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f * would have loo
16420 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a ked like this:.*
16430 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
16440 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 ><pre>.** INSER
16450 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 T INTO table1 VA
16460 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70 LUES('It's a hap
16470 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f py day!');.** </
16480 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 pre></blockquote
16490 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 >.**.** This sec
164a0 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61 ond example is a
164b0 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72 n SQL syntax err
164c0 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61 or. As a genera
164d0 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c l rule you shoul
164e0 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20 d.** always use
164f0 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73 %q instead of %s
16500 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 when inserting
16510 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69 text into a stri
16520 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a ng literal..**.*
16530 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20 * The %Q option
16540 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78 works like %q ex
16550 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64 cept it also add
16560 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 s single quotes
16570 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75 around.** the ou
16580 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74 tside of the tot
16590 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69 al string. Addi
165a0 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 tionally, if the
165b0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 parameter in th
165c0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69 e.** argument li
165d0 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 st is a NULL poi
165e0 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74 nter, %Q substit
165f0 75 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e utes the text "N
16600 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a ULL" (without.**
16610 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20 single quotes)
16620 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 in place of the
16630 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20 %Q option. So,
16640 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 for example, one
16650 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a could say:.**.*
16660 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
16670 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 re>.** char *zS
16680 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 QL = sqlite3_mpr
16690 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 intf("INSERT INT
166a0 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25 O table VALUES(%
166b0 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 Q)", zText);.**
166c0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 sqlite3_exec(db
166d0 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 , zSQL, 0, 0, 0)
166e0 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 ;.** sqlite3_fr
166f0 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 ee(zSQL);.** </p
16700 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
16710 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 .**.** The code
16720 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65 above will rende
16730 72 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20 r a correct SQL
16740 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 statement in the
16750 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c zSQL.** variabl
16760 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54 e even if the zT
16770 65 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20 ext variable is
16780 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
16790 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66 **.** The "%z" f
167a0 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e ormatting option
167b0 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c works exactly l
167c0 69 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68 ike "%s" with th
167d0 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68 e.** addition th
167e0 61 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72 at after the str
167f0 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61 ing has been rea
16800 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74 d and copied int
16810 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c o.** the result,
16820 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
16830 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 ] is called on t
16840 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e he input string.
16850 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 52 65 71 {END}.**.** Req
16860 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
16870 31 37 34 30 33 5d 20 5b 48 31 37 34 30 36 5d 20 17403] [H17406]
16880 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 53 51 4c 49 [H17407].*/.SQLI
16890 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c TE_API char *sql
168a0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e ite3_mprintf(con
168b0 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 st char*,...);.S
168c0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
168d0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 sqlite3_vmprintf
168e0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 (const char*, va
168f0 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41 _list);.SQLITE_A
16900 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 PI char *sqlite3
16910 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68 _snprintf(int,ch
16920 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
16930 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ...);../*.** CA
16940 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 PI3REF: Memory A
16950 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 79 73 llocation Subsys
16960 74 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c 53 32 tem {H17300} <S2
16970 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0000>.**.** The
16980 53 51 4c 69 74 65 20 63 6f 72 65 20 20 75 73 65 SQLite core use
16990 73 20 74 68 65 73 65 20 74 68 72 65 65 20 72 6f s these three ro
169a0 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6f utines for all o
169b0 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69 6e 74 f its own.** int
169c0 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c ernal memory all
169d0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 22 ocation needs. "
169e0 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70 72 65 Core" in the pre
169f0 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 0a 2a vious sentence.*
16a00 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 * does not inclu
16a10 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 de operating-sys
16a20 74 65 6d 20 73 70 65 63 69 66 69 63 20 56 46 53 tem specific VFS
16a30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e implementation.
16a40 20 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f 77 73 The.** Windows
16a50 20 56 46 53 20 75 73 65 73 20 6e 61 74 69 76 65 VFS uses native
16a60 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 malloc() and fr
16a70 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20 6f 70 ee() for some op
16a80 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 erations..**.**
16a90 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c The sqlite3_mall
16aa0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 oc() routine ret
16ab0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
16ac0 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 o a block.** of
16ad0 6d 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 74 20 memory at least
16ae0 4e 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 N bytes in lengt
16af0 68 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 h, where N is th
16b00 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 e parameter..**
16b10 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f If sqlite3_mallo
16b20 63 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f c() is unable to
16b30 20 6f 62 74 61 69 6e 20 73 75 66 66 69 63 69 65 obtain sufficie
16b40 6e 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d 6f 72 nt free.** memor
16b50 79 2c 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 y, it returns a
16b60 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49 NULL pointer. I
16b70 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 f the parameter
16b80 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f N to.** sqlite3_
16b90 6d 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f malloc() is zero
16ba0 20 6f 72 20 6e 65 67 61 74 69 76 65 20 74 68 65 or negative the
16bb0 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 n sqlite3_malloc
16bc0 28 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20 () returns.** a
16bd0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
16be0 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 .** Calling sqli
16bf0 74 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20 te3_free() with
16c00 61 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f a pointer previo
16c10 75 73 6c 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a usly returned.**
16c20 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c by sqlite3_mall
16c30 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f oc() or sqlite3_
16c40 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73 realloc() releas
16c50 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73 es that memory s
16c60 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69 67 o.** that it mig
16c70 68 74 20 62 65 20 72 65 75 73 65 64 2e 20 20 54 ht be reused. T
16c80 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 he sqlite3_free(
16c90 29 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 ) routine is.**
16ca0 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 63 61 a no-op if is ca
16cb0 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c lled with a NULL
16cc0 20 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 73 69 pointer. Passi
16cd0 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ng a NULL pointe
16ce0 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f r.** to sqlite3_
16cf0 66 72 65 65 28 29 20 69 73 20 68 61 72 6d 6c 65 free() is harmle
16d00 73 73 2e 20 20 41 66 74 65 72 20 62 65 69 6e 67 ss. After being
16d10 20 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 0a 2a freed, memory.*
16d20 2a 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 65 72 * should neither
16d30 20 62 65 20 72 65 61 64 20 6e 6f 72 20 77 72 69 be read nor wri
16d40 74 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 61 64 tten. Even read
16d50 69 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 20 66 ing previously f
16d60 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d reed.** memory m
16d70 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 ight result in a
16d80 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 segmentation fa
16d90 75 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 65 76 ult or other sev
16da0 65 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 4d 65 ere error..** Me
16db0 6d 6f 72 79 20 63 6f 72 72 75 70 74 69 6f 6e 2c mory corruption,
16dc0 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 a segmentation
16dd0 66 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 72 20 fault, or other
16de0 73 65 76 65 72 65 20 65 72 72 6f 72 0a 2a 2a 20 severe error.**
16df0 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 66 20 might result if
16e00 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 sqlite3_free() i
16e10 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 s called with a
16e20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 non-NULL pointer
16e30 20 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e 6f 74 that.** was not
16e40 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
16e50 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
16e60 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c or sqlite3_reall
16e70 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 oc()..**.** The
16e80 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
16e90 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 ) interface atte
16ea0 6d 70 74 73 20 74 6f 20 72 65 73 69 7a 65 20 61 mpts to resize a
16eb0 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f 72 79 .** prior memory
16ec0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 allocation to b
16ed0 65 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 e at least N byt
16ee0 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 es, where N is t
16ef0 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 he.** second par
16f00 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6d 65 6d ameter. The mem
16f10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 ory allocation t
16f20 6f 20 62 65 20 72 65 73 69 7a 65 64 20 69 73 20 o be resized is
16f30 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 the first.** par
16f40 61 6d 65 74 65 72 2e 20 20 49 66 20 74 68 65 20 ameter. If the
16f50 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
16f60 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c to sqlite3_reall
16f70 6f 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c oc().** is a NUL
16f80 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69 L pointer then i
16f90 74 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69 ts behavior is i
16fa0 64 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c dentical to call
16fb0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ing.** sqlite3_m
16fc0 61 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e alloc(N) where N
16fd0 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 is the second p
16fe0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
16ff0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a te3_realloc()..*
17000 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 * If the second
17010 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
17020 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 ite3_realloc() i
17030 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 s zero or.** neg
17040 61 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62 ative then the b
17050 65 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74 ehavior is exact
17060 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63 ly the same as c
17070 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 alling.** sqlite
17080 33 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20 3_free(P) where
17090 50 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 P is the first p
170a0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
170b0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a te3_realloc()..*
170c0 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f * sqlite3_reallo
170d0 63 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f c() returns a po
170e0 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72 inter to a memor
170f0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 y allocation.**
17100 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 of at least N by
17110 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20 4e tes in size or N
17120 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65 6e ULL if sufficien
17130 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61 76 t memory is unav
17140 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 4d ailable..** If M
17150 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 is the size of
17160 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 the prior alloca
17170 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e 28 4e tion, then min(N
17180 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 ,M) bytes.** of
17190 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 the prior alloca
171a0 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 64 20 tion are copied
171b0 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 into the beginni
171c0 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72 65 74 ng of buffer ret
171d0 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 urned.** by sqli
171e0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 61 6e te3_realloc() an
171f0 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f d the prior allo
17200 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e cation is freed.
17210 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 72 .** If sqlite3_r
17220 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 ealloc() returns
17230 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 NULL, then the
17240 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e prior allocation
17250 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65 65 64 .** is not freed
17260 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f ..**.** The memo
17270 72 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ry returned by s
17280 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
17290 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 61 6c and sqlite3_real
172a0 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c 77 61 loc().** is alwa
172b0 79 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 74 ys aligned to at
172c0 20 6c 65 61 73 74 20 61 6e 20 38 20 62 79 74 65 least an 8 byte
172d0 20 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e 44 7d boundary. {END}
172e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 .**.** The defau
172f0 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f lt implementatio
17300 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 n of the memory
17310 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
17320 73 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74 68 65 stem uses.** the
17330 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c malloc(), reall
17340 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 oc() and free()
17350 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 provided by the
17360 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 standard C libra
17370 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32 7d 20 ry..** {H17382}
17380 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69 However, if SQLi
17390 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 te is compiled w
173a0 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 ith the.** SQLIT
173b0 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d 3c 69 E_MEMORY_SIZE=<i
173c0 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65 70 72 >NNN</i> C prepr
173d0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 28 77 ocessor macro (w
173e0 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a here <i>NNN</i>.
173f0 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 ** is an integer
17400 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 63 ), then SQLite c
17410 72 65 61 74 65 20 61 20 73 74 61 74 69 63 20 61 reate a static a
17420 72 72 61 79 20 6f 66 20 61 74 20 6c 65 61 73 74 rray of at least
17430 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 .** <i>NNN</i> b
17440 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 ytes in size and
17450 20 75 73 65 73 20 74 68 61 74 20 61 72 72 61 79 uses that array
17460 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 for all of its
17470 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d 6f 72 dynamic.** memor
17480 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 y allocation nee
17490 64 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64 69 74 ds. {END} Addit
174a0 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c ional memory all
174b0 6f 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73 0a 2a ocator options.*
174c0 2a 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 * may be added i
174d0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
174e0 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69 s..**.** In SQLi
174f0 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 te version 3.5.0
17500 20 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74 20 77 and 3.5.1, it w
17510 61 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 as possible to d
17520 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c efine.** the SQL
17530 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f ITE_OMIT_MEMORY_
17540 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69 63 68 ALLOCATION which
17550 20 77 6f 75 6c 64 20 63 61 75 73 65 20 74 68 65 would cause the
17560 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70 built-in.** imp
17570 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
17580 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f hese routines to
17590 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 be omitted. Th
175a0 61 74 20 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a at capability.**
175b0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 is no longer pr
175c0 6f 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75 ovided. Only bu
175d0 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c ilt-in memory al
175e0 6c 6f 63 61 74 6f 72 73 20 63 61 6e 20 62 65 20 locators can be
175f0 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 used..**.** The
17600 57 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74 65 72 Windows OS inter
17610 66 61 63 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 face layer calls
17620 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d .** the system m
17630 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 alloc() and free
17640 28 29 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e () directly when
17650 20 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66 converting.** f
17660 69 6c 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e ilenames between
17670 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 the UTF-8 encod
17680 69 6e 67 20 75 73 65 64 20 62 79 20 53 51 4c 69 ing used by SQLi
17690 74 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76 te.** and whatev
176a0 65 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f er filename enco
176b0 64 69 6e 67 20 69 73 20 75 73 65 64 20 62 79 20 ding is used by
176c0 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 57 the particular W
176d0 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c indows.** instal
176e0 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 lation. Memory
176f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
17700 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20 s are detected,
17710 62 75 74 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 but.** they are
17720 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73 reported back as
17730 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 [SQLITE_CANTOPE
17740 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 N] or.** [SQLITE
17750 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74 _IOERR] rather t
17760 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 han [SQLITE_NOME
17770 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 M]..**.** Requir
17780 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 ements:.** [H173
17790 30 33 5d 20 5b 48 31 37 33 30 34 5d 20 5b 48 31 03] [H17304] [H1
177a0 37 33 30 35 5d 20 5b 48 31 37 33 30 36 5d 20 5b 7305] [H17306] [
177b0 48 31 37 33 31 30 5d 20 5b 48 31 37 33 31 32 5d H17310] [H17312]
177c0 20 5b 48 31 37 33 31 35 5d 20 5b 48 31 37 33 31 [H17315] [H1731
177d0 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 31 5d 20 5b 8].** [H17321] [
177e0 48 31 37 33 32 32 5d 20 5b 48 31 37 33 32 33 5d H17322] [H17323]
177f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 .**.** The point
17800 65 72 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 er arguments to
17810 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
17820 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 and [sqlite3_re
17830 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d 75 73 74 alloc()].** must
17840 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 be either NULL
17850 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 65 72 73 or else pointers
17860 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 obtained from a
17870 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 6f 63 61 prior.** invoca
17880 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 tion of [sqlite3
17890 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 _malloc()] or [s
178a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
178b0 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 6e ] that have.** n
178c0 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 6c 65 ot yet been rele
178d0 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ased..**.** The
178e0 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 application must
178f0 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 72 69 not read or wri
17900 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 0a 2a te any part of.*
17910 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d * a block of mem
17920 6f 72 79 20 61 66 74 65 72 20 69 74 20 68 61 73 ory after it has
17930 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75 been released u
17940 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 sing.** [sqlite3
17950 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b 73 71 6c _free()] or [sql
17960 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 2e ite3_realloc()].
17970 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
17980 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c oid *sqlite3_mal
17990 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
179a0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
179b0 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 2a e3_realloc(void*
179c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 , int);.SQLITE_A
179d0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
179e0 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a 2f 2a free(void*);../*
179f0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 .** CAPI3REF: Me
17a00 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72 20 53 mory Allocator S
17a10 74 61 74 69 73 74 69 63 73 20 7b 48 31 37 33 37 tatistics {H1737
17a20 30 7d 20 3c 53 33 30 32 31 30 3e 0a 2a 2a 0a 2a 0} <S30210>.**.*
17a30 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69 64 65 * SQLite provide
17a40 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e 74 65 s these two inte
17a50 72 66 61 63 65 73 20 66 6f 72 20 72 65 70 6f 72 rfaces for repor
17a60 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74 61 74 ting on the stat
17a70 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 us.** of the [sq
17a80 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c lite3_malloc()],
17a90 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
17aa0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
17ab0 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 72 6f realloc()].** ro
17ac0 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20 66 6f utines, which fo
17ad0 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 rm the built-in
17ae0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
17af0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2a 0a n subsystem..**.
17b00 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
17b10 0a 2a 2a 20 5b 48 31 37 33 37 31 5d 20 5b 48 31 .** [H17371] [H1
17b20 37 33 37 33 5d 20 5b 48 31 37 33 37 34 5d 20 5b 7373] [H17374] [
17b30 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 51 4c 49 54 H17375].*/.SQLIT
17b40 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e E_API sqlite3_in
17b50 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f t64 sqlite3_memo
17b60 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a 53 ry_used(void);.S
17b70 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
17b80 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
17b90 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 memory_highwater
17ba0 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b (int resetFlag);
17bb0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
17bc0 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d 20 : Pseudo-Random
17bd0 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f 72 Number Generator
17be0 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 30 30 {H17390} <S2000
17bf0 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 0>.**.** SQLite
17c00 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68 2d contains a high-
17c10 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d 72 quality pseudo-r
17c20 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
17c30 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75 73 erator (PRNG) us
17c40 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74 20 ed to.** select
17c50 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20 7c 20 random [ROWID |
17c60 52 4f 57 49 44 73 5d 20 77 68 65 6e 20 69 6e 73 ROWIDs] when ins
17c70 65 72 74 69 6e 67 20 6e 65 77 20 72 65 63 6f 72 erting new recor
17c80 64 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 ds into a table
17c90 74 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 that.** already
17ca0 75 73 65 73 20 74 68 65 20 6c 61 72 67 65 73 74 uses the largest
17cb0 20 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57 49 44 possible [ROWID
17cc0 5d 2e 20 20 54 68 65 20 50 52 4e 47 20 69 73 20 ]. The PRNG is
17cd0 61 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a 2a 2a also used for.**
17ce0 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 72 61 the build-in ra
17cf0 6e 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e 64 6f ndom() and rando
17d00 6d 62 6c 6f 62 28 29 20 53 51 4c 20 66 75 6e 63 mblob() SQL func
17d10 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 6e 74 tions. This int
17d20 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a 2a 2a erface allows.**
17d30 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f applications to
17d40 20 61 63 63 65 73 73 20 74 68 65 20 73 61 6d 65 access the same
17d50 20 50 52 4e 47 20 66 6f 72 20 6f 74 68 65 72 20 PRNG for other
17d60 70 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 purposes..**.**
17d70 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 A call to this r
17d80 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 4e 20 outine stores N
17d90 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e bytes of randomn
17da0 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 72 20 ess into buffer
17db0 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 P..**.** The fir
17dc0 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 st time this rou
17dd0 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 tine is invoked
17de0 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e 61 6c (either internal
17df0 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68 65 20 ly or by.** the
17e00 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74 68 65 application) the
17e10 20 50 52 4e 47 20 69 73 20 73 65 65 64 65 64 20 PRNG is seeded
17e20 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65 73 73 using randomness
17e30 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f obtained.** fro
17e40 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 m the xRandomnes
17e50 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 s method of the
17e60 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 default [sqlite3
17e70 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a _vfs] object..**
17e80 20 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65 On all subseque
17e90 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2c 20 nt invocations,
17ea0 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f the pseudo-rando
17eb0 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72 61 74 mness is generat
17ec0 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 ed.** internally
17ed0 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72 65 63 and without rec
17ee0 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b 73 71 ourse to the [sq
17ef0 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61 6e 64 lite3_vfs] xRand
17f00 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68 6f 64 omness.** method
17f10 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
17f20 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 39 32 ents:.** [H17392
17f30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
17f40 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 61 6e void sqlite3_ran
17f50 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 domness(int N, v
17f60 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 oid *P);../*.**
17f70 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c CAPI3REF: Compil
17f80 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69 7a 61 e-Time Authoriza
17f90 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b tion Callbacks {
17fa0 48 31 32 35 30 30 7d 20 3c 53 37 30 31 30 30 3e H12500} <S70100>
17fb0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
17fc0 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 20 ine registers a
17fd0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
17fe0 61 63 6b 20 77 69 74 68 20 61 20 70 61 72 74 69 ack with a parti
17ff0 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62 61 cular.** [databa
18000 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2c 20 se connection],
18010 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68 65 20 supplied in the
18020 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a first argument..
18030 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 ** The authorize
18040 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e r callback is in
18050 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61 voked as SQL sta
18060 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65 69 6e tements are bein
18070 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 62 79 g compiled.** by
18080 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
18090 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 e()] or its vari
180a0 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 ants [sqlite3_pr
180b0 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a 2a 20 epare_v2()],.**
180c0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
180d0 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 16()] and [sqlit
180e0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
180f0 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75 73 0a )]. At various.
18100 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69 6e 67 ** points during
18110 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e the compilation
18120 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c 6f 67 process, as log
18130 69 63 20 69 73 20 62 65 69 6e 67 20 63 72 65 61 ic is being crea
18140 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66 6f 72 ted.** to perfor
18150 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69 6f 6e m various action
18160 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 s, the authorize
18170 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e r callback is in
18180 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65 65 20 voked to.** see
18190 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f 6e 73 if those actions
181a0 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20 20 54 are allowed. T
181b0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
181c0 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a 2a 2a llback should.**
181d0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
181e0 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 20 74 68 65 OK] to allow the
181f0 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49 54 45 action, [SQLITE
18200 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69 73 61 _IGNORE] to disa
18210 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70 65 63 llow the.** spec
18220 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75 74 20 ific action but
18230 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20 73 74 allow the SQL st
18240 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e 74 69 atement to conti
18250 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d nue to be.** com
18260 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c 49 54 piled, or [SQLIT
18270 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75 73 65 E_DENY] to cause
18280 20 74 68 65 20 65 6e 74 69 72 65 20 53 51 4c 20 the entire SQL
18290 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a statement to be.
182a0 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69 74 68 ** rejected with
182b0 20 61 6e 20 65 72 72 6f 72 2e 20 20 49 66 20 74 an error. If t
182c0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
182d0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a llback returns.*
182e0 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 * any value othe
182f0 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 49 r than [SQLITE_I
18300 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f GNORE], [SQLITE_
18310 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f OK], or [SQLITE_
18320 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 DENY].** then th
18330 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 e [sqlite3_prepa
18340 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 re_v2()] or equi
18350 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 valent call that
18360 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20 74 68 triggered.** th
18370 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c e authorizer wil
18380 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 l fail with an e
18390 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a rror message..**
183a0 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61 6c .** When the cal
183b0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 lback returns [S
183c0 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61 74 20 QLITE_OK], that
183d0 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 means the operat
183e0 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 ion.** requested
183f0 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20 74 68 is ok. When th
18400 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 e callback retur
18410 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d ns [SQLITE_DENY]
18420 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 , the.** [sqlite
18430 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
18440 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61 or equivalent ca
18450 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 ll that triggere
18460 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69 d the.** authori
18470 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 zer will fail wi
18480 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 th an error mess
18490 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 age explaining t
184a0 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20 69 73 hat.** access is
184b0 20 64 65 6e 69 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 denied. .**.**
184c0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
184d0 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f ter to the autho
184e0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 rizer callback i
184f0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
18500 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74 third.** paramet
18510 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 er to the sqlite
18520 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
18530 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 54 68 () interface. Th
18540 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
18550 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c er.** to the cal
18560 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65 lback is an inte
18570 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59 ger [SQLITE_COPY
18580 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20 | action code]
18590 74 68 61 74 20 73 70 65 63 69 66 69 65 73 0a 2a that specifies.*
185a0 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 * the particular
185b0 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75 action to be au
185c0 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 20 74 68 thorized. The th
185d0 69 72 64 20 74 68 72 6f 75 67 68 20 73 69 78 74 ird through sixt
185e0 68 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 h parameters.**
185f0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 to the callback
18600 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 are zero-termina
18610 74 65 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 ted strings that
18620 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f contain additio
18630 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c 73 20 61 nal.** details a
18640 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20 bout the action
18650 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 to be authorized
18660 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 ..**.** If the a
18670 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53 ction code is [S
18680 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61 QLITE_READ].** a
18690 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 nd the callback
186a0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
186b0 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65 IGNORE] then the
186c0 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
186d0 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65 atement] stateme
186e0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 nt is constructe
186f0 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a d to substitute.
18700 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 ** a NULL value
18710 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 in place of the
18720 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
18730 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 t would have.**
18740 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 been read if [SQ
18750 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 LITE_OK] had bee
18760 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 n returned. The
18770 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
18780 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62 .** return can b
18790 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61 e used to deny a
187a0 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72 n untrusted user
187b0 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76 access to indiv
187c0 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 idual.** columns
187d0 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 of a table..**
187e0 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f If the action co
187f0 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 45 de is [SQLITE_DE
18800 4c 45 54 45 5d 20 61 6e 64 20 74 68 65 20 63 61 LETE] and the ca
18810 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a llback returns.*
18820 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 * [SQLITE_IGNORE
18830 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45 4c 45 ] then the [DELE
18840 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 TE] operation pr
18850 6f 63 65 65 64 73 20 62 75 74 20 74 68 65 0a 2a oceeds but the.*
18860 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74 69 * [truncate opti
18870 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64 69 73 mization] is dis
18880 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20 72 6f abled and all ro
18890 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 69 ws are deleted i
188a0 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0a 2a 2a 0a ndividually..**.
188b0 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72 ** An authorizer
188c0 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 5b 73 is used when [s
188d0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c qlite3_prepare |
188e0 20 70 72 65 70 61 72 69 6e 67 5d 0a 2a 2a 20 53 preparing].** S
188f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 66 72 QL statements fr
18900 6f 6d 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 om an untrusted
18910 73 6f 75 72 63 65 2c 20 74 6f 20 65 6e 73 75 72 source, to ensur
18920 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 e that the SQL s
18930 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 64 6f 20 tatements.** do
18940 6e 6f 74 20 74 72 79 20 74 6f 20 61 63 63 65 73 not try to acces
18950 73 20 64 61 74 61 20 74 68 65 79 20 61 72 65 20 s data they are
18960 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 not allowed to s
18970 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68 65 79 ee, or that they
18980 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 72 79 20 74 do not.** try t
18990 6f 20 65 78 65 63 75 74 65 20 6d 61 6c 69 63 69 o execute malici
189a0 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73 20 74 ous statements t
189b0 68 61 74 20 64 61 6d 61 67 65 20 74 68 65 20 64 hat damage the d
189c0 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a 2a 2a atabase. For.**
189d0 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 example, an app
189e0 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61 6c 6c lication may all
189f0 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65 6e 74 ow a user to ent
18a00 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 er arbitrary.**
18a10 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f 72 20 SQL queries for
18a20 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20 61 20 evaluation by a
18a30 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20 74 database. But t
18a40 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 he application d
18a50 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 oes.** not want
18a60 74 68 65 20 75 73 65 72 20 74 6f 20 62 65 20 61 the user to be a
18a70 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72 62 69 ble to make arbi
18a80 74 72 61 72 79 20 63 68 61 6e 67 65 73 20 74 6f trary changes to
18a90 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
18aa0 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72 . An authorizer
18ab0 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65 20 70 could then be p
18ac0 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68 69 6c ut in place whil
18ad0 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 65 6e e the.** user-en
18ae0 74 65 72 65 64 20 53 51 4c 20 69 73 20 62 65 69 tered SQL is bei
18af0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ng [sqlite3_prep
18b00 61 72 65 20 7c 20 70 72 65 70 61 72 65 64 5d 20 are | prepared]
18b10 74 68 61 74 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77 that.** disallow
18b20 73 20 65 76 65 72 79 74 68 69 6e 67 20 65 78 63 s everything exc
18b30 65 70 74 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 ept [SELECT] sta
18b40 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 41 tements..**.** A
18b50 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
18b60 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73 need to process
18b70 20 53 51 4c 20 66 72 6f 6d 20 75 6e 74 72 75 73 SQL from untrus
18b80 74 65 64 20 73 6f 75 72 63 65 73 0a 2a 2a 20 6d ted sources.** m
18b90 69 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73 69 64 ight also consid
18ba0 65 72 20 6c 6f 77 65 72 69 6e 67 20 72 65 73 6f er lowering reso
18bb0 75 72 63 65 20 6c 69 6d 69 74 73 20 75 73 69 6e urce limits usin
18bc0 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 g [sqlite3_limit
18bd0 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c 69 6d 69 74 ()].** and limit
18be0 69 6e 67 20 64 61 74 61 62 61 73 65 20 73 69 7a ing database siz
18bf0 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d 61 78 e using the [max
18c00 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 _page_count] [PR
18c10 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 61 64 64 69 AGMA].** in addi
18c20 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20 61 6e tion to using an
18c30 20 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a authorizer..**.
18c40 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 ** Only a single
18c50 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6e 20 authorizer can
18c60 62 65 20 69 6e 20 70 6c 61 63 65 20 6f 6e 20 61 be in place on a
18c70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
18c80 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 20 74 69 6d tion.** at a tim
18c90 65 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f e. Each call to
18ca0 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 sqlite3_set_aut
18cb0 68 6f 72 69 7a 65 72 20 6f 76 65 72 72 69 64 65 horizer override
18cc0 73 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 s the.** previou
18cd0 73 20 63 61 6c 6c 2e 20 20 44 69 73 61 62 6c 65 s call. Disable
18ce0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 the authorizer
18cf0 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20 61 20 by installing a
18d00 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a NULL callback..*
18d10 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 * The authorizer
18d20 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 is disabled by
18d30 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 default..**.** T
18d40 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
18d50 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f 74 20 llback must not
18d60 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 do anything that
18d70 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 will modify.**
18d80 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
18d90 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 nection that inv
18da0 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f 72 69 oked the authori
18db0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a zer callback..**
18dc0 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 Note that [sqli
18dd0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
18de0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 ] and [sqlite3_s
18df0 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 tep()] both modi
18e00 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 fy their.** data
18e10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
18e20 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 for the meaning
18e30 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 of "modify" in
18e40 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a this paragraph..
18e50 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b 73 71 6c 69 **.** When [sqli
18e60 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
18e70 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 65 ] is used to pre
18e80 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e 74 pare a statement
18e90 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 , the.** stateme
18ea0 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65 70 72 nt might be repr
18eb0 65 70 61 72 65 64 20 64 75 72 69 6e 67 20 5b 73 epared during [s
18ec0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 64 qlite3_step()] d
18ed0 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73 63 68 65 ue to a .** sche
18ee0 6d 61 20 63 68 61 6e 67 65 2e 20 20 48 65 6e 63 ma change. Henc
18ef0 65 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 e, the applicati
18f00 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73 75 72 65 on should ensure
18f10 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 72 that the.** cor
18f20 72 65 63 74 20 61 75 74 68 6f 72 69 7a 65 72 20 rect authorizer
18f30 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61 69 6e 73 callback remains
18f40 20 69 6e 20 70 6c 61 63 65 20 64 75 72 69 6e 67 in place during
18f50 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
18f60 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 ep()]..**.** Not
18f70 65 20 74 68 61 74 20 74 68 65 20 61 75 74 68 6f e that the autho
18f80 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 rizer callback i
18f90 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 64 s invoked only d
18fa0 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 uring.** [sqlite
18fb0 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20 3_prepare()] or
18fc0 69 74 73 20 76 61 72 69 61 6e 74 73 2e 20 20 41 its variants. A
18fd0 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 69 73 20 uthorization is
18fe0 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 not.** performed
18ff0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e during statemen
19000 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 69 6e 20 t evaluation in
19010 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
19020 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 61 73 20 73 , unless.** as s
19030 74 61 74 65 64 20 69 6e 20 74 68 65 20 70 72 65 tated in the pre
19040 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 2c vious paragraph,
19050 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
19060 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71 6c 69 74 invokes.** sqlit
19070 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 e3_prepare_v2()
19080 74 6f 20 72 65 70 72 65 70 61 72 65 20 61 20 73 to reprepare a s
19090 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 61 tatement after a
190a0 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a schema change..
190b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
190c0 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 30 31 5d 20 ts:.** [H12501]
190d0 5b 48 31 32 35 30 32 5d 20 5b 48 31 32 35 30 33 [H12502] [H12503
190e0 5d 20 5b 48 31 32 35 30 34 5d 20 5b 48 31 32 35 ] [H12504] [H125
190f0 30 35 5d 20 5b 48 31 32 35 30 36 5d 20 5b 48 31 05] [H12506] [H1
19100 32 35 30 37 5d 20 5b 48 31 32 35 31 30 5d 0a 2a 2507] [H12510].*
19110 2a 20 5b 48 31 32 35 31 31 5d 20 5b 48 31 32 35 * [H12511] [H125
19120 31 32 5d 20 5b 48 31 32 35 32 30 5d 20 5b 48 31 12] [H12520] [H1
19130 32 35 32 31 5d 20 5b 48 31 32 35 32 32 5d 0a 2a 2521] [H12522].*
19140 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
19150 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 sqlite3_set_aut
19160 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 horizer(. sqlit
19170 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75 e3*,. int (*xAu
19180 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f th)(void*,int,co
19190 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 nst char*,const
191a0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
191b0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a *,const char*),.
191c0 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 void *pUserDat
191d0 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 a.);../*.** CAPI
191e0 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65 72 3REF: Authorizer
191f0 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 7b 48 Return Codes {H
19200 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 3e 0a 12590} <H12500>.
19210 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 **.** The [sqlit
19220 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 e3_set_authorize
19230 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 r | authorizer c
19240 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
19250 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72 6e ] must.** return
19260 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 5f either [SQLITE_
19270 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 OK] or one of th
19280 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 ese two constant
19290 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f s in order.** to
192a0 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20 77 signal SQLite w
192b0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
192c0 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72 6d e action is perm
192d0 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65 0a itted. See the.
192e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f ** [sqlite3_set_
192f0 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 authorizer | aut
19300 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e 74 horizer document
19310 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69 74 ation] for addit
19320 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 ional.** informa
19330 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tion..*/.#define
19340 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20 31 SQLITE_DENY 1
19350 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65 20 /* Abort the
19360 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 69 SQL statement wi
19370 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 th an error */.#
19380 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 47 define SQLITE_IG
19390 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e 27 NORE 2 /* Don'
193a0 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c 20 t allow access,
193b0 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72 61 but don't genera
193c0 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 0a te an error */..
193d0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
193e0 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69 6f Authorizer Actio
193f0 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 30 7d n Codes {H12550}
19400 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 <H12500>.**.**
19410 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 The [sqlite3_set
19420 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 69 _authorizer()] i
19430 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 nterface registe
19440 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 rs a callback fu
19450 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 69 nction.** that i
19460 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 75 74 s invoked to aut
19470 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e 20 53 horize certain S
19480 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 63 74 QL statement act
19490 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 73 65 ions. The.** se
194a0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 cond parameter t
194b0 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 o the callback i
194c0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 s an integer cod
194d0 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65 73 e that specifies
194e0 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f 6e 20 .** what action
194f0 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69 is being authori
19500 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 65 20 zed. These are
19510 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 74 69 the integer acti
19520 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a 2a 2a on codes that.**
19530 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 the authorizer
19540 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 65 20 callback may be
19550 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 passed..**.** Th
19560 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 ese action code
19570 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20 77 values signify w
19580 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65 72 hat kind of oper
19590 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 0a 2a ation is to be.*
195a0 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54 * authorized. T
195b0 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20 70 he 3rd and 4th p
195c0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65 arameters to the
195d0 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a authorization.*
195e0 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 * callback funct
195f0 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72 61 ion will be para
19600 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20 64 meters or NULL d
19610 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 epending on whic
19620 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 63 6f h of these.** co
19630 64 65 73 20 69 73 20 75 73 65 64 20 61 73 20 74 des is used as t
19640 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
19650 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 70 61 ter. The 5th pa
19660 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 0a 2a rameter to the.*
19670 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c * authorizer cal
19680 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 61 6d lback is the nam
19690 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
196a0 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 e ("main", "temp
196b0 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 20 61 ",.** etc.) if a
196c0 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 65 20 pplicable. The
196d0 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 6th parameter to
196e0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 the authorizer
196f0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 74 callback.** is t
19700 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 he name of the i
19710 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 67 65 nner-most trigge
19720 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 20 69 r or view that i
19730 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
19740 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 73 20 r.** the access
19750 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 attempt or NULL
19760 69 66 20 74 68 69 73 20 61 63 63 65 73 73 20 61 if this access a
19770 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 63 74 ttempt is direct
19780 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 2d 6c ly from.** top-l
19790 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e 0a 2a evel SQL code..*
197a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
197b0 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 31 5d 20 5b s:.** [H12551] [
197c0 48 31 32 35 35 32 5d 20 5b 48 31 32 35 35 33 5d H12552] [H12553]
197d0 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 0a 2f 2a 2a [H12554].*/./**
197e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
197f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19800 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 20 2a 2a ********* 3rd **
19810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 68 20 2a ********** 4th *
19820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 **********/.#def
19830 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 ine SQLITE_CREAT
19840 45 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 E_INDEX
19850 20 31 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 1 /* Index Na
19860 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 me Table Na
19870 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
19880 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
19890 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 _TABLE
198a0 32 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 2 /* Table Nam
198b0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
198c0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
198d0 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
198e0 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 33 TEMP_INDEX 3
198f0 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 /* Index Name
19900 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 Table Name
19910 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
19920 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
19930 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 34 20 EMP_TABLE 4
19940 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
19950 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
19960 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
19970 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 SQLITE_CREATE_TE
19980 4d 50 5f 54 52 49 47 47 45 52 20 20 20 35 20 20 MP_TRIGGER 5
19990 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 /* Trigger Name
199a0 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 Table Name
199b0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
199c0 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d QLITE_CREATE_TEM
199d0 50 5f 56 49 45 57 20 20 20 20 20 20 36 20 20 20 P_VIEW 6
199e0 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 /* View Name
199f0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
19a00 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
19a10 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47 LITE_CREATE_TRIG
19a20 47 45 52 20 20 20 20 20 20 20 20 37 20 20 20 2f GER 7 /
19a30 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 * Trigger Name
19a40 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
19a50 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
19a60 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 20 ITE_CREATE_VIEW
19a70 20 20 20 20 20 20 20 20 20 20 38 20 20 20 2f 2a 8 /*
19a80 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 View Name
19a90 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
19aa0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
19ab0 54 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 TE_DELETE
19ac0 20 20 20 20 20 20 20 20 20 39 20 20 20 2f 2a 20 9 /*
19ad0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
19ae0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
19af0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
19b00 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20 20 20 20 E_DROP_INDEX
19b10 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 49 10 /* I
19b20 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 ndex Name T
19b30 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a able Name *
19b40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
19b50 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20 20 20 20 _DROP_TABLE
19b60 20 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 61 11 /* Ta
19b70 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 ble Name NU
19b80 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
19b90 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
19ba0 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 20 DROP_TEMP_INDEX
19bb0 20 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e 64 12 /* Ind
19bc0 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 ex Name Tab
19bd0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a le Name */.
19be0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
19bf0 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 ROP_TEMP_TABLE
19c00 20 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62 6c 13 /* Tabl
19c10 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c e Name NULL
19c20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
19c30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 define SQLITE_DR
19c40 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 OP_TEMP_TRIGGER
19c50 20 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67 67 14 /* Trigg
19c60 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 er Name Table
19c70 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 Name */.#d
19c80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
19c90 50 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 P_TEMP_VIEW
19ca0 20 20 31 35 20 20 20 2f 2a 20 56 69 65 77 20 4e 15 /* View N
19cb0 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 ame NULL
19cc0 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
19cd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
19ce0 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 _TRIGGER
19cf0 20 31 36 20 20 20 2f 2a 20 54 72 69 67 67 65 72 16 /* Trigger
19d00 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e Name Table N
19d10 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
19d20 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
19d30 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20 VIEW
19d40 31 37 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 17 /* View Nam
19d50 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 e NULL
19d60 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
19d70 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 ne SQLITE_INSERT
19d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
19d90 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 8 /* Table Nam
19da0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
19db0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
19dc0 65 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 20 e SQLITE_PRAGMA
19dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39 19
19de0 20 20 20 2f 2a 20 50 72 61 67 6d 61 20 4e 61 6d /* Pragma Nam
19df0 65 20 20 20 20 20 31 73 74 20 61 72 67 20 6f 72 e 1st arg or
19e00 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 NULL */.#define
19e10 20 53 51 4c 49 54 45 5f 52 45 41 44 20 20 20 20 SQLITE_READ
19e20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 20 20
19e30 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 /* Table Name
19e40 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 Column Name
19e50 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
19e60 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 20 20 20 SQLITE_SELECT
19e70 20 20 20 20 20 20 20 20 20 20 20 20 32 31 20 20 21
19e80 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 /* NULL
19e90 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
19ea0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
19eb0 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f QLITE_TRANSACTIO
19ec0 4e 20 20 20 20 20 20 20 20 20 20 32 32 20 20 20 N 22
19ed0 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 /* Operation
19ee0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
19ef0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
19f00 4c 49 54 45 5f 55 50 44 41 54 45 20 20 20 20 20 LITE_UPDATE
19f10 20 20 20 20 20 20 20 20 20 20 32 33 20 20 20 2f 23 /
19f20 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
19f30 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 Column Name
19f40 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
19f50 49 54 45 5f 41 54 54 41 43 48 20 20 20 20 20 20 ITE_ATTACH
19f60 20 20 20 20 20 20 20 20 20 32 34 20 20 20 2f 2a 24 /*
19f70 20 46 69 6c 65 6e 61 6d 65 20 20 20 20 20 20 20 Filename
19f80 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
19f90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
19fa0 54 45 5f 44 45 54 41 43 48 20 20 20 20 20 20 20 TE_DETACH
19fb0 20 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20 25 /*
19fc0 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 Database Name
19fd0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
19fe0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
19ff0 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 20 20 E_ALTER_TABLE
1a000 20 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 44 26 /* D
1a010 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 54 atabase Name T
1a020 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a able Name *
1a030 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1a040 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 _REINDEX
1a050 20 20 20 20 20 20 32 37 20 20 20 2f 2a 20 49 6e 27 /* In
1a060 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 dex Name NU
1a070 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1a080 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1a090 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 ANALYZE
1a0a0 20 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61 62 28 /* Tab
1a0b0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c le Name NUL
1a0c0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1a0d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1a0e0 52 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20 20 REATE_VTABLE
1a0f0 20 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62 6c 29 /* Tabl
1a100 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 e Name Modu
1a110 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 le Name */.#
1a120 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 define SQLITE_DR
1a130 4f 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 OP_VTABLE
1a140 20 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c 65 30 /* Table
1a150 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c Name Modul
1a160 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 e Name */.#d
1a170 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
1a180 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 CTION
1a190 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 31 /* NULL
1a1a0 20 20 20 20 20 20 20 20 20 20 46 75 6e 63 74 69 Functi
1a1b0 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a 23 64 65 on Name */.#de
1a1c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 41 56 45 fine SQLITE_SAVE
1a1d0 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 POINT
1a1e0 20 33 32 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 32 /* Operati
1a1f0 6f 6e 20 20 20 20 20 20 20 53 61 76 65 70 6f 69 on Savepoi
1a200 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a 23 64 65 66 nt Name */.#def
1a210 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 50 59 20 ine SQLITE_COPY
1a220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a230 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 0 /* No longe
1a240 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a r used */../*.**
1a250 20 43 41 50 49 33 52 45 46 3a 20 54 72 61 63 69 CAPI3REF: Traci
1a260 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c 69 6e 67 ng And Profiling
1a270 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 32 32 Functions {H122
1a280 38 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 20 80} <S60400>.**
1a290 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
1a2a0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
1a2b0 73 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 s register callb
1a2c0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 ack functions th
1a2d0 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 at can be used f
1a2e0 6f 72 0a 2a 2a 20 74 72 61 63 69 6e 67 20 61 6e or.** tracing an
1a2f0 64 20 70 72 6f 66 69 6c 69 6e 67 20 74 68 65 20 d profiling the
1a300 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 53 51 4c execution of SQL
1a310 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
1a320 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 ** The callback
1a330 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 function registe
1a340 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 red by sqlite3_t
1a350 72 61 63 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 race() is invoke
1a360 64 20 61 74 0a 2a 2a 20 76 61 72 69 6f 75 73 20 d at.** various
1a370 74 69 6d 65 73 20 77 68 65 6e 20 61 6e 20 53 51 times when an SQ
1a380 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 L statement is b
1a390 65 69 6e 67 20 72 75 6e 20 62 79 20 5b 73 71 6c eing run by [sql
1a3a0 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a ite3_step()]..**
1a3b0 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 The callback re
1a3c0 74 75 72 6e 73 20 61 20 55 54 46 2d 38 20 72 65 turns a UTF-8 re
1a3d0 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 53 ndering of the S
1a3e0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 QL statement tex
1a3f0 74 0a 2a 2a 20 61 73 20 74 68 65 20 73 74 61 74 t.** as the stat
1a400 65 6d 65 6e 74 20 66 69 72 73 74 20 62 65 67 69 ement first begi
1a410 6e 73 20 65 78 65 63 75 74 69 6e 67 2e 20 20 41 ns executing. A
1a420 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 dditional callba
1a430 63 6b 73 20 6f 63 63 75 72 0a 2a 2a 20 61 73 20 cks occur.** as
1a440 65 61 63 68 20 74 72 69 67 67 65 72 65 64 20 73 each triggered s
1a450 75 62 70 72 6f 67 72 61 6d 20 69 73 20 65 6e 74 ubprogram is ent
1a460 65 72 65 64 2e 20 20 54 68 65 20 63 61 6c 6c 62 ered. The callb
1a470 61 63 6b 73 20 66 6f 72 20 74 72 69 67 67 65 72 acks for trigger
1a480 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20 55 s.** contain a U
1a490 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d 65 6e 74 TF-8 SQL comment
1a4a0 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 that identifies
1a4b0 20 74 68 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a the trigger..**
1a4c0 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b .** The callback
1a4d0 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 function regist
1a4e0 65 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ered by sqlite3_
1a4f0 70 72 6f 66 69 6c 65 28 29 20 69 73 20 69 6e 76 profile() is inv
1a500 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 61 63 68 20 oked.** as each
1a510 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69 SQL statement fi
1a520 6e 69 73 68 65 73 2e 20 20 54 68 65 20 70 72 6f nishes. The pro
1a530 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f file callback co
1a540 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 6f 72 ntains.** the or
1a550 69 67 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e 74 iginal statement
1a560 20 74 65 78 74 20 61 6e 64 20 61 6e 20 65 73 74 text and an est
1a570 69 6d 61 74 65 20 6f 66 20 77 61 6c 6c 2d 63 6c imate of wall-cl
1a580 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 6f 66 20 68 ock time.** of h
1a590 6f 77 20 6c 6f 6e 67 20 74 68 61 74 20 73 74 61 ow long that sta
1a5a0 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 74 6f 20 72 tement took to r
1a5b0 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 un..**.** Requir
1a5c0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
1a5d0 38 31 5d 20 5b 48 31 32 32 38 32 5d 20 5b 48 31 81] [H12282] [H1
1a5e0 32 32 38 33 5d 20 5b 48 31 32 32 38 34 5d 20 5b 2283] [H12284] [
1a5f0 48 31 32 32 38 35 5d 20 5b 48 31 32 32 38 37 5d H12285] [H12287]
1a600 20 5b 48 31 32 32 38 38 5d 20 5b 48 31 32 32 38 [H12288] [H1228
1a610 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 30 5d 0a 2a 9].** [H12290].*
1a620 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
1a630 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
1a640 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74 void *sqlite3_t
1a650 72 61 63 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 race(sqlite3*, v
1a660 6f 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f 69 oid(*xTrace)(voi
1a670 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c d*,const char*),
1a680 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
1a690 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
1a6a0 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73 71 IMENTAL void *sq
1a6b0 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 73 71 lite3_profile(sq
1a6c0 6c 69 74 65 33 2a 2c 0a 20 20 20 76 6f 69 64 28 lite3*,. void(
1a6d0 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a *xProfile)(void*
1a6e0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c ,const char*,sql
1a6f0 69 74 65 33 5f 75 69 6e 74 36 34 29 2c 20 76 6f ite3_uint64), vo
1a700 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id*);../*.** CAP
1a710 49 33 52 45 46 3a 20 51 75 65 72 79 20 50 72 6f I3REF: Query Pro
1a720 67 72 65 73 73 20 43 61 6c 6c 62 61 63 6b 73 20 gress Callbacks
1a730 7b 48 31 32 39 31 30 7d 20 3c 53 36 30 34 30 30 {H12910} <S60400
1a740 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 >.**.** This rou
1a750 74 69 6e 65 20 63 6f 6e 66 69 67 75 72 65 73 20 tine configures
1a760 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 a callback funct
1a770 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a 20 70 72 6f ion - the.** pro
1a780 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2d gress callback -
1a790 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 that is invoked
1a7a0 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 periodically du
1a7b0 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a 20 72 75 6e ring long.** run
1a7c0 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 5b 73 ning calls to [s
1a7d0 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2c 20 qlite3_exec()],
1a7e0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1a7f0 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 and.** [sqlite3
1a800 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 2e 20 20 _get_table()].
1a810 41 6e 20 65 78 61 6d 70 6c 65 20 75 73 65 20 66 An example use f
1a820 6f 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 or this.** inter
1a830 66 61 63 65 20 69 73 20 74 6f 20 6b 65 65 70 20 face is to keep
1a840 61 20 47 55 49 20 75 70 64 61 74 65 64 20 64 75 a GUI updated du
1a850 72 69 6e 67 20 61 20 6c 61 72 67 65 20 71 75 65 ring a large que
1a860 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ry..**.** If the
1a870 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 progress callba
1a880 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a ck returns non-z
1a890 65 72 6f 2c 20 74 68 65 20 6f 70 65 72 61 74 69 ero, the operati
1a8a0 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 65 72 72 75 on is.** interru
1a8b0 70 74 65 64 2e 20 20 54 68 69 73 20 66 65 61 74 pted. This feat
1a8c0 75 72 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 ure can be used
1a8d0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 0a 2a to implement a.*
1a8e0 2a 20 22 43 61 6e 63 65 6c 22 20 62 75 74 74 6f * "Cancel" butto
1a8f0 6e 20 6f 6e 20 61 20 47 55 49 20 70 72 6f 67 72 n on a GUI progr
1a900 65 73 73 20 64 69 61 6c 6f 67 20 62 6f 78 2e 0a ess dialog box..
1a910 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 67 72 65 **.** The progre
1a920 73 73 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 ss handler must
1a930 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 not do anything
1a940 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 that will modify
1a950 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1a960 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
1a970 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 70 72 6f invoked the pro
1a980 67 72 65 73 73 20 68 61 6e 64 6c 65 72 2e 0a 2a gress handler..*
1a990 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c * Note that [sql
1a9a0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
1a9b0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
1a9c0 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 step()] both mod
1a9d0 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 ify their.** dat
1a9e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1a9f0 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e s for the meanin
1aa00 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e g of "modify" in
1aa10 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e this paragraph.
1aa20 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
1aa30 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 31 31 5d nts:.** [H12911]
1aa40 20 5b 48 31 32 39 31 32 5d 20 5b 48 31 32 39 31 [H12912] [H1291
1aa50 33 5d 20 5b 48 31 32 39 31 34 5d 20 5b 48 31 32 3] [H12914] [H12
1aa60 39 31 35 5d 20 5b 48 31 32 39 31 36 5d 20 5b 48 915] [H12916] [H
1aa70 31 32 39 31 37 5d 20 5b 48 31 32 39 31 38 5d 0a 12917] [H12918].
1aa80 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 **.*/.SQLITE_API
1aa90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 70 72 void sqlite3_pr
1aaa0 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 28 73 ogress_handler(s
1aab0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 69 6e qlite3*, int, in
1aac0 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 t(*)(void*), voi
1aad0 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d*);../*.** CAPI
1aae0 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67 20 41 20 3REF: Opening A
1aaf0 4e 65 77 20 44 61 74 61 62 61 73 65 20 43 6f 6e New Database Con
1ab00 6e 65 63 74 69 6f 6e 20 7b 48 31 32 37 30 30 7d nection {H12700}
1ab10 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S40200>.**.**
1ab20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f These routines o
1ab30 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61 pen an SQLite da
1ab40 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 6f 73 tabase file whos
1ab50 65 20 6e 61 6d 65 20 69 73 20 67 69 76 65 6e 20 e name is given
1ab60 62 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 by the.** filena
1ab70 6d 65 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 me argument. The
1ab80 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 filename argume
1ab90 6e 74 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 nt is interprete
1aba0 64 20 61 73 20 55 54 46 2d 38 20 66 6f 72 0a 2a d as UTF-8 for.*
1abb0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
1abc0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 and sqlite3_ope
1abd0 6e 5f 76 32 28 29 20 61 6e 64 20 61 73 20 55 54 n_v2() and as UT
1abe0 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69 F-16 in the nati
1abf0 76 65 20 62 79 74 65 0a 2a 2a 20 6f 72 64 65 72 ve byte.** order
1ac00 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 for sqlite3_ope
1ac10 6e 31 36 28 29 2e 20 41 20 5b 64 61 74 61 62 61 n16(). A [databa
1ac20 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 se connection] h
1ac30 61 6e 64 6c 65 20 69 73 20 75 73 75 61 6c 6c 79 andle is usually
1ac40 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 20 .** returned in
1ac50 2a 70 70 44 62 2c 20 65 76 65 6e 20 69 66 20 61 *ppDb, even if a
1ac60 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 n error occurs.
1ac70 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 The only except
1ac80 69 6f 6e 20 69 73 20 74 68 61 74 0a 2a 2a 20 69 ion is that.** i
1ac90 66 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61 62 f SQLite is unab
1aca0 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d le to allocate m
1acb0 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 emory to hold th
1acc0 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 e [sqlite3] obje
1acd0 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 77 69 ct,.** a NULL wi
1ace0 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e ll be written in
1acf0 74 6f 20 2a 70 70 44 62 20 69 6e 73 74 65 61 64 to *ppDb instead
1ad00 20 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f of a pointer to
1ad10 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 0a 2a the [sqlite3].*
1ad20 2a 20 6f 62 6a 65 63 74 2e 20 49 66 20 74 68 65 * object. If the
1ad30 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 database is ope
1ad40 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63 72 65 61 ned (and/or crea
1ad50 74 65 64 29 20 73 75 63 63 65 73 73 66 75 6c 6c ted) successfull
1ad60 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53 51 4c 49 y, then.** [SQLI
1ad70 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e TE_OK] is return
1ad80 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 61 ed. Otherwise a
1ad90 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 n [error code] i
1ada0 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 s returned. The
1adb0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 .** [sqlite3_err
1adc0 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 msg()] or [sqlit
1add0 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 72 e3_errmsg16()] r
1ade0 6f 75 74 69 6e 65 73 20 63 61 6e 20 62 65 20 75 outines can be u
1adf0 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 0a 2a 2a sed to obtain.**
1ae00 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 an English lang
1ae10 75 61 67 65 20 64 65 73 63 72 69 70 74 69 6f 6e uage description
1ae20 20 6f 66 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a of the error..*
1ae30 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 *.** The default
1ae40 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 encoding for th
1ae50 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 e database will
1ae60 62 65 20 55 54 46 2d 38 20 69 66 0a 2a 2a 20 73 be UTF-8 if.** s
1ae70 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72 qlite3_open() or
1ae80 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1ae90 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 () is called and
1aea0 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e 20 74 68 .** UTF-16 in th
1aeb0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 e native byte or
1aec0 64 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f 6f der if sqlite3_o
1aed0 70 65 6e 31 36 28 29 20 69 73 20 75 73 65 64 2e pen16() is used.
1aee0 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f .**.** Whether o
1aef0 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f r not an error o
1af00 63 63 75 72 73 20 77 68 65 6e 20 69 74 20 69 73 ccurs when it is
1af10 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f 75 72 63 opened, resourc
1af20 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 es.** associated
1af30 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 with the [datab
1af40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
1af50 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64 20 62 65 handle should be
1af60 20 72 65 6c 65 61 73 65 64 20 62 79 0a 2a 2a 20 released by.**
1af70 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 5b 73 passing it to [s
1af80 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 qlite3_close()]
1af90 77 68 65 6e 20 69 74 20 69 73 20 6e 6f 20 6c 6f when it is no lo
1afa0 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a 2a nger required..*
1afb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
1afc0 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e 74 65 72 _open_v2() inter
1afd0 66 61 63 65 20 77 6f 72 6b 73 20 6c 69 6b 65 20 face works like
1afe0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 0a 2a sqlite3_open().*
1aff0 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 * except that it
1b000 20 61 63 63 65 70 74 73 20 74 77 6f 20 61 64 64 accepts two add
1b010 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65 itional paramete
1b020 72 73 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 rs for additiona
1b030 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 6f 76 65 l control.** ove
1b040 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62 61 r the new databa
1b050 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 se connection.
1b060 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 The flags parame
1b070 74 65 72 20 63 61 6e 20 74 61 6b 65 20 6f 6e 65 ter can take one
1b080 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f of.** the follo
1b090 77 69 6e 67 20 74 68 72 65 65 20 76 61 6c 75 65 wing three value
1b0a0 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 6f s, optionally co
1b0b0 6d 62 69 6e 65 64 20 77 69 74 68 20 74 68 65 20 mbined with the
1b0c0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e .** [SQLITE_OPEN
1b0d0 5f 4e 4f 4d 55 54 45 58 5d 20 6f 72 20 5b 53 51 _NOMUTEX] or [SQ
1b0e0 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 LITE_OPEN_FULLMU
1b0f0 54 45 58 5d 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a TEX] flags:.**.*
1b100 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 5b 53 * <dl>.** <dt>[S
1b110 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
1b120 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NLY]</dt>.** <dd
1b130 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 >The database is
1b140 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 61 64 2d opened in read-
1b150 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 66 20 74 only mode. If t
1b160 68 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 he database does
1b170 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 not.** already
1b180 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20 exist, an error
1b190 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64 is returned.</dd
1b1a0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c >.**.** <dt>[SQL
1b1b0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
1b1c0 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TE]</dt>.** <dd>
1b1d0 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 The database is
1b1e0 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 opened for readi
1b1f0 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20 69 ng and writing i
1b200 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72 20 72 f possible, or r
1b210 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 20 69 eading.** only i
1b220 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 77 72 f the file is wr
1b230 69 74 65 20 70 72 6f 74 65 63 74 65 64 20 62 79 ite protected by
1b240 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
1b250 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74 68 65 ystem. In eithe
1b260 72 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 64 61 r.** case the da
1b270 74 61 62 61 73 65 20 6d 75 73 74 20 61 6c 72 65 tabase must alre
1b280 61 64 79 20 65 78 69 73 74 2c 20 6f 74 68 65 72 ady exist, other
1b290 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 69 73 wise an error is
1b2a0 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a returned.</dd>.
1b2b0 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 **.** <dt>[SQLIT
1b2c0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
1b2d0 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e ] | [SQLITE_OPEN
1b2e0 5f 43 52 45 41 54 45 5d 3c 2f 64 74 3e 0a 2a 2a _CREATE]</dt>.**
1b2f0 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 <dd>The databas
1b300 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 e is opened for
1b310 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 reading and writ
1b320 69 6e 67 2c 20 61 6e 64 20 69 73 20 63 72 65 61 ing, and is crea
1b330 74 65 73 20 69 74 20 69 66 0a 2a 2a 20 69 74 20 tes it if.** it
1b340 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
1b350 20 65 78 69 73 74 2e 20 54 68 69 73 20 69 73 20 exist. This is
1b360 74 68 65 20 62 65 68 61 76 69 6f 72 20 74 68 61 the behavior tha
1b370 74 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64 t is always used
1b380 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f for.** sqlite3_
1b390 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 open() and sqlit
1b3a0 65 33 5f 6f 70 65 6e 31 36 28 29 2e 3c 2f 64 64 e3_open16().</dd
1b3b0 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a >.** </dl>.**.**
1b3c0 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 If the 3rd para
1b3d0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
1b3e0 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 6e 6f _open_v2() is no
1b3f0 74 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 t one of the.**
1b400 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f combinations sho
1b410 77 6e 20 61 62 6f 76 65 20 6f 72 20 6f 6e 65 20 wn above or one
1b420 6f 66 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69 of the combinati
1b430 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 ons shown above
1b440 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 77 69 74 68 combined.** with
1b450 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 the [SQLITE_OPE
1b460 4e 5f 4e 4f 4d 55 54 45 58 5d 20 6f 72 20 5b 53 N_NOMUTEX] or [S
1b470 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d QLITE_OPEN_FULLM
1b480 55 54 45 58 5d 20 66 6c 61 67 73 2c 0a 2a 2a 20 UTEX] flags,.**
1b490 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f then the behavio
1b4a0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a r is undefined..
1b4b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 53 51 **.** If the [SQ
1b4c0 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 LITE_OPEN_NOMUTE
1b4d0 58 5d 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 X] flag is set,
1b4e0 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 then the databas
1b4f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 e connection.**
1b500 6f 70 65 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c opens in the mul
1b510 74 69 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61 ti-thread [threa
1b520 64 69 6e 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f ding mode] as lo
1b530 6e 67 20 61 73 20 74 68 65 20 73 69 6e 67 6c 65 ng as the single
1b540 2d 74 68 72 65 61 64 0a 2a 2a 20 6d 6f 64 65 20 -thread.** mode
1b550 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 has not been set
1b560 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 at compile-time
1b570 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 or start-time.
1b580 20 49 66 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 If the.** [SQLI
1b590 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 TE_OPEN_FULLMUTE
1b5a0 58 5d 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 X] flag is set t
1b5b0 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
1b5c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e connection open
1b5d0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 72 69 s.** in the seri
1b5e0 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e alized [threadin
1b5f0 67 20 6d 6f 64 65 5d 20 75 6e 6c 65 73 73 20 73 g mode] unless s
1b600 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 77 61 73 ingle-thread was
1b610 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 .** previously s
1b620 65 6c 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69 elected at compi
1b630 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 le-time or start
1b640 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 -time..**.** If
1b650 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 the filename is
1b660 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e ":memory:", then
1b670 20 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 a private, temp
1b680 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f 72 79 20 orary in-memory
1b690 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 63 database.** is c
1b6a0 72 65 61 74 65 64 20 66 6f 72 20 74 68 65 20 63 reated for the c
1b6b0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 onnection. This
1b6c0 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
1b6d0 61 73 65 20 77 69 6c 6c 20 76 61 6e 69 73 68 20 ase will vanish
1b6e0 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 when.** the data
1b6f0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1b700 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 75 74 75 is closed. Futu
1b710 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 re versions of S
1b720 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 6d QLite might.** m
1b730 61 6b 65 20 75 73 65 20 6f 66 20 61 64 64 69 74 ake use of addit
1b740 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c 20 66 69 ional special fi
1b750 6c 65 6e 61 6d 65 73 20 74 68 61 74 20 62 65 67 lenames that beg
1b760 69 6e 20 77 69 74 68 20 74 68 65 20 22 3a 22 20 in with the ":"
1b770 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 74 character..** It
1b780 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 is recommended
1b790 74 68 61 74 20 77 68 65 6e 20 61 20 64 61 74 61 that when a data
1b7a0 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 61 63 base filename ac
1b7b0 74 75 61 6c 6c 79 20 64 6f 65 73 20 62 65 67 69 tually does begi
1b7c0 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22 3a 22 20 n with.** a ":"
1b7d0 63 68 61 72 61 63 74 65 72 20 79 6f 75 20 73 68 character you sh
1b7e0 6f 75 6c 64 20 70 72 65 66 69 78 20 74 68 65 20 ould prefix the
1b7f0 66 69 6c 65 6e 61 6d 65 20 77 69 74 68 20 61 20 filename with a
1b800 70 61 74 68 6e 61 6d 65 20 73 75 63 68 20 61 73 pathname such as
1b810 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61 76 6f 69 .** "./" to avoi
1b820 64 20 61 6d 62 69 67 75 69 74 79 2e 0a 2a 2a 0a d ambiguity..**.
1b830 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 ** If the filena
1b840 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79 20 73 me is an empty s
1b850 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 70 72 tring, then a pr
1b860 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 ivate, temporary
1b870 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74 61 .** on-disk data
1b880 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72 65 base will be cre
1b890 61 74 65 64 2e 20 20 54 68 69 73 20 70 72 69 76 ated. This priv
1b8a0 61 74 65 20 64 61 74 61 62 61 73 65 20 77 69 6c ate database wil
1b8b0 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 l be.** automati
1b8c0 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 61 73 cally deleted as
1b8d0 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 61 74 soon as the dat
1b8e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1b8f0 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a is closed..**.*
1b900 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 * The fourth par
1b910 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
1b920 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 74 3_open_v2() is t
1b930 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a he name of the.*
1b940 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 * [sqlite3_vfs]
1b950 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66 69 object that defi
1b960 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e nes the operatin
1b970 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 g system interfa
1b980 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e ce that.** the n
1b990 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ew database conn
1b9a0 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 ection should us
1b9b0 65 2e 20 20 49 66 20 74 68 65 20 66 6f 75 72 74 e. If the fourt
1b9c0 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a h parameter is.*
1b9d0 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 * a NULL pointer
1b9e0 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c then the defaul
1b9f0 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 t [sqlite3_vfs]
1ba00 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 2e 0a object is used..
1ba10 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74 6f **.** <b>Note to
1ba20 20 57 69 6e 64 6f 77 73 20 75 73 65 72 73 3a 3c Windows users:<
1ba30 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f 64 69 6e /b> The encodin
1ba40 67 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 66 g used for the f
1ba50 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 ilename argument
1ba60 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f .** of sqlite3_o
1ba70 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 pen() and sqlite
1ba80 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d 75 73 74 3_open_v2() must
1ba90 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f 74 20 77 be UTF-8, not w
1baa0 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f 64 65 70 hatever.** codep
1bab0 61 67 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 age is currently
1bac0 20 64 65 66 69 6e 65 64 2e 20 20 46 69 6c 65 6e defined. Filen
1bad0 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ames containing
1bae0 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 0a 2a 2a international.**
1baf0 20 63 68 61 72 61 63 74 65 72 73 20 6d 75 73 74 characters must
1bb00 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f be converted to
1bb10 20 55 54 46 2d 38 20 70 72 69 6f 72 20 74 6f 20 UTF-8 prior to
1bb20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 69 6e 74 passing them int
1bb30 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 o.** sqlite3_ope
1bb40 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f n() or sqlite3_o
1bb50 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a 2a 20 pen_v2()..**.**
1bb60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
1bb70 20 5b 48 31 32 37 30 31 5d 20 5b 48 31 32 37 30 [H12701] [H1270
1bb80 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b 48 31 32 2] [H12703] [H12
1bb90 37 30 34 5d 20 5b 48 31 32 37 30 36 5d 20 5b 48 704] [H12706] [H
1bba0 31 32 37 30 37 5d 20 5b 48 31 32 37 30 39 5d 20 12707] [H12709]
1bbb0 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b 48 31 32 [H12711].** [H12
1bbc0 37 31 32 5d 20 5b 48 31 32 37 31 33 5d 20 5b 48 712] [H12713] [H
1bbd0 31 32 37 31 34 5d 20 5b 48 31 32 37 31 37 5d 20 12714] [H12717]
1bbe0 5b 48 31 32 37 31 39 5d 20 5b 48 31 32 37 32 31 [H12719] [H12721
1bbf0 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f 0a 53 51 ] [H12723].*/.SQ
1bc00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1bc10 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e ite3_open(. con
1bc20 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d st char *filenam
1bc30 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 e, /* Database
1bc40 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 filename (UTF-8
1bc50 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a ) */. sqlite3 *
1bc60 2a 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f *ppDb /
1bc70 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 * OUT: SQLite db
1bc80 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 handle */.);.SQ
1bc90 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1bca0 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20 20 63 ite3_open16(. c
1bcb0 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69 6c 65 6e onst void *filen
1bcc0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 ame, /* Databa
1bcd0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 se filename (UTF
1bce0 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 -16) */. sqlite
1bcf0 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20 3 **ppDb
1bd00 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 /* OUT: SQLite
1bd10 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b db handle */.);
1bd20 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1bd30 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
1bd40 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 . const char *f
1bd50 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 ilename, /* Da
1bd60 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 tabase filename
1bd70 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c (UTF-8) */. sql
1bd80 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 ite3 **ppDb,
1bd90 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c /* OUT: SQL
1bda0 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f ite db handle */
1bdb0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
1bdc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
1bdd0 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ags */. const c
1bde0 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20 har *zVfs
1bdf0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53 20 /* Name of VFS
1be00 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a 2f module to use */
1be10 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
1be20 52 45 46 3a 20 45 72 72 6f 72 20 43 6f 64 65 73 REF: Error Codes
1be30 20 41 6e 64 20 4d 65 73 73 61 67 65 73 20 7b 48 And Messages {H
1be40 31 32 38 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 12800} <S60200>.
1be50 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
1be60 33 5f 65 72 72 63 6f 64 65 28 29 20 69 6e 74 65 3_errcode() inte
1be70 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 rface returns th
1be80 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 73 75 6c e numeric [resul
1be90 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20 5b 65 t code] or.** [e
1bea0 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
1beb0 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73 ode] for the mos
1bec0 74 20 72 65 63 65 6e 74 20 66 61 69 6c 65 64 20 t recent failed
1bed0 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 20 63 61 sqlite3_* API ca
1bee0 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 ll.** associated
1bef0 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73 with a [databas
1bf00 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 49 e connection]. I
1bf10 66 20 61 20 70 72 69 6f 72 20 41 50 49 20 63 61 f a prior API ca
1bf20 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20 62 75 74 ll failed.** but
1bf30 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
1bf40 20 41 50 49 20 63 61 6c 6c 20 73 75 63 63 65 65 API call succee
1bf50 64 65 64 2c 20 74 68 65 20 72 65 74 75 72 6e 20 ded, the return
1bf60 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 73 71 value from.** sq
1bf70 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20 lite3_errcode()
1bf80 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 54 is undefined. T
1bf90 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e he sqlite3_exten
1bfa0 64 65 64 5f 65 72 72 63 6f 64 65 28 29 0a 2a 2a ded_errcode().**
1bfb0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 68 interface is th
1bfc0 65 20 73 61 6d 65 20 65 78 63 65 70 74 20 74 68 e same except th
1bfd0 61 74 20 69 74 20 61 6c 77 61 79 73 20 72 65 74 at it always ret
1bfe0 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20 5b 65 78 urns the .** [ex
1bff0 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
1c000 64 65 5d 20 65 76 65 6e 20 77 68 65 6e 20 65 78 de] even when ex
1c010 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
1c020 64 65 73 20 61 72 65 0a 2a 2a 20 64 69 73 61 62 des are.** disab
1c030 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 led..**.** The s
1c040 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 qlite3_errmsg()
1c050 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d and sqlite3_errm
1c060 73 67 31 36 28 29 20 72 65 74 75 72 6e 20 45 6e sg16() return En
1c070 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67 65 0a 2a glish-language.*
1c080 2a 20 74 65 78 74 20 74 68 61 74 20 64 65 73 63 * text that desc
1c090 72 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 2c ribes the error,
1c0a0 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 as either UTF-8
1c0b0 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73 70 65 or UTF-16 respe
1c0c0 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f ctively..** Memo
1c0d0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 65 ry to hold the e
1c0e0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 rror message str
1c0f0 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 64 20 69 ing is managed i
1c100 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 68 nternally..** Th
1c110 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f e application do
1c120 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 77 es not need to w
1c130 6f 72 72 79 20 61 62 6f 75 74 20 66 72 65 65 69 orry about freei
1c140 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a ng the result..*
1c150 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 65 * However, the e
1c160 72 72 6f 72 20 73 74 72 69 6e 67 20 6d 69 67 68 rror string migh
1c170 74 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e t be overwritten
1c180 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 or deallocated
1c190 62 79 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 by.** subsequent
1c1a0 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 65 72 20 calls to other
1c1b0 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
1c1c0 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a functions..**.*
1c1d0 2a 20 57 68 65 6e 20 74 68 65 20 73 65 72 69 61 * When the seria
1c1e0 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67 lized [threading
1c1f0 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20 75 73 65 mode] is in use
1c200 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 68 , it might be th
1c210 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 61 e.** case that a
1c220 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 6f 63 second error oc
1c230 63 75 72 73 20 6f 6e 20 61 20 73 65 70 61 72 61 curs on a separa
1c240 74 65 20 74 68 72 65 61 64 20 69 6e 20 62 65 74 te thread in bet
1c250 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74 69 6d 65 ween.** the time
1c260 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 65 72 of the first er
1c270 72 6f 72 20 61 6e 64 20 74 68 65 20 63 61 6c 6c ror and the call
1c280 20 74 6f 20 74 68 65 73 65 20 69 6e 74 65 72 66 to these interf
1c290 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e 20 74 68 aces..** When th
1c2a0 61 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 at happens, the
1c2b0 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 77 69 6c second error wil
1c2c0 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 73 69 l be reported si
1c2d0 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 69 6e 74 nce these.** int
1c2e0 65 72 66 61 63 65 73 20 61 6c 77 61 79 73 20 72 erfaces always r
1c2f0 65 70 6f 72 74 20 74 68 65 20 6d 6f 73 74 20 72 eport the most r
1c300 65 63 65 6e 74 20 72 65 73 75 6c 74 2e 20 20 54 ecent result. T
1c310 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68 69 73 2c o avoid.** this,
1c320 20 65 61 63 68 20 74 68 72 65 61 64 20 63 61 6e each thread can
1c330 20 6f 62 74 61 69 6e 20 65 78 63 6c 75 73 69 76 obtain exclusiv
1c340 65 20 75 73 65 20 6f 66 20 74 68 65 20 5b 64 61 e use of the [da
1c350 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1c360 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b n] D.** by invok
1c370 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 ing [sqlite3_mut
1c380 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71 6c 69 74 ex_enter]([sqlit
1c390 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29 29 e3_db_mutex](D))
1c3a0 20 62 65 66 6f 72 65 20 62 65 67 69 6e 6e 69 6e before beginnin
1c3b0 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44 20 61 6e g.** to use D an
1c3c0 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 d invoking [sqli
1c3d0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 5d te3_mutex_leave]
1c3e0 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 ([sqlite3_db_mut
1c3f0 65 78 5d 28 44 29 29 20 61 66 74 65 72 0a 2a 2a ex](D)) after.**
1c400 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 74 68 all calls to th
1c410 65 20 69 6e 74 65 72 66 61 63 65 73 20 6c 69 73 e interfaces lis
1c420 74 65 64 20 68 65 72 65 20 61 72 65 20 63 6f 6d ted here are com
1c430 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 pleted..**.** If
1c440 20 61 6e 20 69 6e 74 65 72 66 61 63 65 20 66 61 an interface fa
1c450 69 6c 73 20 77 69 74 68 20 53 51 4c 49 54 45 5f ils with SQLITE_
1c460 4d 49 53 55 53 45 2c 20 74 68 61 74 20 6d 65 61 MISUSE, that mea
1c470 6e 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 ns the interface
1c480 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b 65 64 20 .** was invoked
1c490 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 79 20 74 incorrectly by t
1c4a0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 he application.
1c4b0 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 In that case, t
1c4c0 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 he.** error code
1c4d0 20 61 6e 64 20 6d 65 73 73 61 67 65 20 6d 61 79 and message may
1c4e0 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 73 or may not be s
1c4f0 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 et..**.** Requir
1c500 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 38 ements:.** [H128
1c510 30 31 5d 20 5b 48 31 32 38 30 32 5d 20 5b 48 31 01] [H12802] [H1
1c520 32 38 30 33 5d 20 5b 48 31 32 38 30 37 5d 20 5b 2803] [H12807] [
1c530 48 31 32 38 30 38 5d 20 5b 48 31 32 38 30 39 5d H12808] [H12809]
1c540 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1c550 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f nt sqlite3_errco
1c560 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b de(sqlite3 *db);
1c570 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1c580 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
1c590 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 _errcode(sqlite3
1c5a0 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 *db);.SQLITE_AP
1c5b0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
1c5c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c lite3_errmsg(sql
1c5d0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 ite3*);.SQLITE_A
1c5e0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1c5f0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 qlite3_errmsg16(
1c600 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
1c610 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 * CAPI3REF: SQL
1c620 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 Statement Object
1c630 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 31 {H13000} <H1301
1c640 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
1c650 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d {prepared statem
1c660 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 73 ent} {prepared s
1c670 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a tatements}.**.**
1c680 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
1c690 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 this object repr
1c6a0 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 esents a single
1c6b0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a SQL statement..*
1c6c0 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 * This object is
1c6d0 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e variously known
1c6e0 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 20 as a "prepared
1c6f0 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 0a statement" or a.
1c700 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 4c ** "compiled SQL
1c710 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 73 statement" or s
1c720 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 74 imply as a "stat
1c730 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 ement"..**.** Th
1c740 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 74 e life of a stat
1c750 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f 65 ement object goe
1c760 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 s something like
1c770 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c this:.**.** <ol
1c780 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 65 >.** <li> Create
1c790 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e the object usin
1c7a0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 g [sqlite3_prepa
1c7b0 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 65 re_v2()] or a re
1c7c0 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 75 lated.** fu
1c7d0 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20 nction..** <li>
1c7e0 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 5b Bind values to [
1c7f0 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 5d host parameters]
1c800 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
1c810 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20 e3_bind_*().**
1c820 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e 0a interfaces..
1c830 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 20 ** <li> Run the
1c840 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b SQL by calling [
1c850 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1c860 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 one or more time
1c870 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 74 s..** <li> Reset
1c880 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75 the statement u
1c890 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 sing [sqlite3_re
1c8a0 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 62 set()] then go b
1c8b0 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 73 ack.** to s
1c8c0 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 20 tep 2. Do this
1c8d0 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 6d zero or more tim
1c8e0 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 74 es..** <li> Dest
1c8f0 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 75 roy the object u
1c900 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
1c910 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f nalize()]..** </
1c920 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 20 ol>.**.** Refer
1c930 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e to documentation
1c940 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6d on individual m
1c950 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f 72 ethods above for
1c960 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 additional.** i
1c970 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 nformation..*/.t
1c980 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
1c990 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 74 lite3_stmt sqlit
1c9a0 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20 e3_stmt;../*.**
1c9b0 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 69 CAPI3REF: Run-ti
1c9c0 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 36 me Limits {H1276
1c9d0 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a 0} <S20600>.**.*
1c9e0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
1c9f0 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a 65 allows the size
1ca00 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e 73 of various cons
1ca10 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 6d tructs to be lim
1ca20 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e ited.** on a con
1ca30 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e 65 nection by conne
1ca40 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 68 ction basis. Th
1ca50 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
1ca60 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 74 r is the.** [dat
1ca70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1ca80 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 73 ] whose limit is
1ca90 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 75 to be set or qu
1caa0 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 eried. The.** s
1cab0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
1cac0 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b 6c is one of the [l
1cad0 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 5d imit categories]
1cae0 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a 2a that define a.*
1caf0 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 74 * class of const
1cb00 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a 65 ructs to be size
1cb10 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 74 limited. The t
1cb20 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 hird parameter i
1cb30 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d s the.** new lim
1cb40 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 73 it for that cons
1cb50 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e 63 truct. The func
1cb60 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
1cb70 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a old limit..**.*
1cb80 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 6d * If the new lim
1cb90 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 65 it is a negative
1cba0 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 6d number, the lim
1cbb0 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e it is unchanged.
1cbc0 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d 69 .** For the limi
1cbd0 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 51 t category of SQ
1cbe0 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 74 LITE_LIMIT_XYZ t
1cbf0 68 65 72 65 20 69 73 20 61 20 0a 2a 2a 20 5b 6c here is a .** [l
1cc00 69 6d 69 74 73 20 7c 20 68 61 72 64 20 75 70 70 imits | hard upp
1cc10 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20 73 65 74 er bound].** set
1cc20 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74 69 by a compile-ti
1cc30 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f me C preprocesso
1cc40 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 0a 2a r macro named .*
1cc50 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53 51 4c 49 * [limits | SQLI
1cc60 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a 2a 2a 20 TE_MAX_XYZ]..**
1cc70 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 20 69 (The "_LIMIT_" i
1cc80 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 63 68 n the name is ch
1cc90 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58 5f 22 anged to "_MAX_"
1cca0 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73 20 74 .).** Attempts t
1ccb0 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c 69 6d o increase a lim
1ccc0 69 74 20 61 62 6f 76 65 20 69 74 73 20 68 61 72 it above its har
1ccd0 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 61 72 d upper bound ar
1cce0 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 74 72 e.** silently tr
1ccf0 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 20 68 uncated to the h
1cd00 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 2e ard upper limit.
1cd10 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d 65 20 .**.** Run time
1cd20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74 65 6e limits are inten
1cd30 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 61 ded for use in a
1cd40 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
1cd50 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 68 20 manage.** both
1cd60 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 72 6e their own intern
1cd70 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 al database and
1cd80 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73 20 74 also databases t
1cd90 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f 6c 6c hat are controll
1cda0 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75 73 74 ed.** by untrust
1cdb0 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 ed external sour
1cdc0 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 ces. An example
1cdd0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 69 67 application mig
1cde0 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62 20 62 ht be a.** web b
1cdf0 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73 20 rowser that has
1ce00 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73 65 its own database
1ce10 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68 69 s for storing hi
1ce20 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65 70 story and.** sep
1ce30 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73 20 arate databases
1ce40 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a 61 controlled by Ja
1ce50 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63 61 vaScript applica
1ce60 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65 64 tions downloaded
1ce70 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74 65 .** off the Inte
1ce80 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65 72 rnet. The inter
1ce90 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63 61 nal databases ca
1cea0 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a 2a n be given the.*
1ceb0 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c 74 * large, default
1cec0 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62 61 limits. Databa
1ced0 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20 65 ses managed by e
1cee0 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 20 xternal sources
1cef0 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e 20 can.** be given
1cf00 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69 6d much smaller lim
1cf10 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20 its designed to
1cf20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61 6c prevent a denial
1cf30 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20 61 of service.** a
1cf40 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70 65 ttack. Develope
1cf50 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77 61 rs might also wa
1cf60 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b 73 nt to use the [s
1cf70 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
1cf80 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74 65 rizer()].** inte
1cf90 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65 72 rface to further
1cfa0 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73 74 control untrust
1cfb0 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69 7a ed SQL. The siz
1cfc0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1cfd0 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 e.** created by
1cfe0 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63 72 an untrusted scr
1cff0 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74 61 ipt can be conta
1d000 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a ined using the.*
1d010 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e * [max_page_coun
1d020 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a 0a t] [PRAGMA]..**.
1d030 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65 20 ** New run-time
1d040 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 limit categories
1d050 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
1d060 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
1d070 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
1d080 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37 36 32 ents:.** [H12762
1d090 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48 31 32 37 ] [H12766] [H127
1d0a0 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 69].*/.SQLITE_AP
1d0b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 I int sqlite3_li
1d0c0 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e mit(sqlite3*, in
1d0d0 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 56 61 6c t id, int newVal
1d0e0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
1d0f0 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 6d EF: Run-Time Lim
1d100 69 74 20 43 61 74 65 67 6f 72 69 65 73 20 7b 48 it Categories {H
1d110 31 32 37 39 30 7d 20 3c 48 31 32 37 36 30 3e 0a 12790} <H12760>.
1d120 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 6c 69 ** KEYWORDS: {li
1d130 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 7b 6c mit category} {l
1d140 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 7d imit categories}
1d150 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e .**.** These con
1d160 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20 76 61 stants define va
1d170 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61 6e 63 rious performanc
1d180 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68 61 74 e limits.** that
1d190 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 can be lowered
1d1a0 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69 6e at run-time usin
1d1b0 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 g [sqlite3_limit
1d1c0 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79 6e 6f ()]..** The syno
1d1d0 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65 61 6e psis of the mean
1d1e0 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 ings of the vari
1d1f0 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20 73 68 ous limits is sh
1d200 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20 41 64 own below..** Ad
1d210 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1d220 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c tion is availabl
1d230 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c 20 4c e at [limits | L
1d240 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74 65 5d imits in SQLite]
1d250 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 ..**.** <dl>.**
1d260 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1d270 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 _LENGTH</dt>.**
1d280 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
1d290 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 74 72 69 size of any stri
1d2a0 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 20 74 61 ng or BLOB or ta
1d2b0 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a 2a 2a 0a ble row.<dd>.**.
1d2c0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
1d2d0 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 2f MIT_SQL_LENGTH</
1d2e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d dt>.** <dd>The m
1d2f0 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 aximum length of
1d300 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
1d310 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 t.</dd>.**.** <d
1d320 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 t>SQLITE_LIMIT_C
1d330 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 OLUMN</dt>.** <d
1d340 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 d>The maximum nu
1d350 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1d360 69 6e 20 61 20 74 61 62 6c 65 20 64 65 66 69 6e in a table defin
1d370 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 68 65 0a ition or in the.
1d380 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 ** result set of
1d390 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f 72 20 74 a [SELECT] or t
1d3a0 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1d3b0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1d3c0 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72 20 69 an index.** or i
1d3d0 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 n an ORDER BY or
1d3e0 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
1d3f0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1d400 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 >SQLITE_LIMIT_EX
1d410 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a PR_DEPTH</dt>.**
1d420 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d <dd>The maximum
1d430 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 depth of the pa
1d440 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e 79 20 rse tree on any
1d450 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e expression.</dd>
1d460 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
1d470 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 E_LIMIT_COMPOUND
1d480 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a 20 _SELECT</dt>.**
1d490 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
1d4a0 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 number of terms
1d4b0 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 in a compound SE
1d4c0 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 3c LECT statement.<
1d4d0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
1d4e0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 QLITE_LIMIT_VDBE
1d4f0 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e _OP</dt>.** <dd>
1d500 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1d510 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f er of instructio
1d520 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 ns in a virtual
1d530 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a machine program.
1d540 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 ** used to imple
1d550 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74 61 74 ment an SQL stat
1d560 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ement.</dd>.**.*
1d570 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
1d580 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c IT_FUNCTION_ARG<
1d590 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1d5a0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1d5b0 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 61 f arguments on a
1d5c0 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a function.</dd>.
1d5d0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
1d5e0 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 3c _LIMIT_ATTACHED<
1d5f0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1d600 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1d610 66 20 5b 41 54 54 41 43 48 20 7c 20 61 74 74 61 f [ATTACH | atta
1d620 63 68 65 64 20 64 61 74 61 62 61 73 65 73 5d 2e ched databases].
1d630 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
1d640 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b SQLITE_LIMIT_LIK
1d650 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 E_PATTERN_LENGTH
1d660 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
1d670 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 maximum length
1d680 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20 61 of the pattern a
1d690 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b rgument to the [
1d6a0 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b 47 4c 4f LIKE] or.** [GLO
1d6b0 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e 3c 2f 64 B] operators.</d
1d6c0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
1d6d0 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 ITE_LIMIT_VARIAB
1d6e0 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a LE_NUMBER</dt>.*
1d6f0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1d700 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 72 69 m number of vari
1d710 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 51 4c 20 ables in an SQL
1d720 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63 statement that c
1d730 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e 64 2e 3c an.** be bound.<
1d740 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f /dd>.** </dl>.*/
1d750 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1d760 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20 20 20 20 LIMIT_LENGTH
1d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d780 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
1d790 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 _LIMIT_SQL_LENGT
1d7a0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
1d7b0 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
1d7c0 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20 E_LIMIT_COLUMN
1d7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d7e0 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
1d7f0 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 TE_LIMIT_EXPR_DE
1d800 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 PTH
1d810 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 3.#define SQL
1d820 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 ITE_LIMIT_COMPOU
1d830 4e 44 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 ND_SELECT
1d840 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4.#define SQ
1d850 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f LITE_LIMIT_VDBE_
1d860 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OP
1d870 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 5.#define S
1d880 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 QLITE_LIMIT_FUNC
1d890 54 49 4f 4e 5f 41 52 47 20 20 20 20 20 20 20 20 TION_ARG
1d8a0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
1d8b0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 SQLITE_LIMIT_ATT
1d8c0 41 43 48 45 44 20 20 20 20 20 20 20 20 20 20 20 ACHED
1d8d0 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 7.#define
1d8e0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 SQLITE_LIMIT_LI
1d8f0 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 KE_PATTERN_LENGT
1d900 48 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e H 8.#defin
1d910 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 e SQLITE_LIMIT_V
1d920 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20 ARIABLE_NUMBER
1d930 20 20 20 20 20 20 20 20 20 39 0a 0a 2f 2a 0a 2a 9../*.*
1d940 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 * CAPI3REF: Comp
1d950 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 53 74 61 iling An SQL Sta
1d960 74 65 6d 65 6e 74 20 7b 48 31 33 30 31 30 7d 20 tement {H13010}
1d970 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b 45 59 57 <S10000>.** KEYW
1d980 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 61 74 65 ORDS: {SQL state
1d990 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0a 2a ment compiler}.*
1d9a0 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 74 65 20 *.** To execute
1d9b0 61 6e 20 53 51 4c 20 71 75 65 72 79 2c 20 69 74 an SQL query, it
1d9c0 20 6d 75 73 74 20 66 69 72 73 74 20 62 65 20 63 must first be c
1d9d0 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 61 20 62 ompiled into a b
1d9e0 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f 67 yte-code.** prog
1d9f0 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f 66 ram using one of
1da00 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e these routines.
1da10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
1da20 20 61 72 67 75 6d 65 6e 74 2c 20 22 64 62 22 2c argument, "db",
1da30 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65 20 is a [database
1da40 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 74 61 connection] obta
1da50 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 70 ined from a.** p
1da60 72 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 rior successful
1da70 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
1da80 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 _open()], [sqlit
1da90 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f 72 e3_open_v2()] or
1daa0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 .** [sqlite3_ope
1dab0 6e 31 36 28 29 5d 2e 20 20 54 68 65 20 64 61 74 n16()]. The dat
1dac0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1dad0 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 62 must not have b
1dae0 65 65 6e 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a een closed..**.*
1daf0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 * The second arg
1db00 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20 69 ument, "zSql", i
1db10 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 s the statement
1db20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c 20 to be compiled,
1db30 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 69 encoded.** as ei
1db40 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 ther UTF-8 or UT
1db50 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69 74 F-16. The sqlit
1db60 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e 64 e3_prepare() and
1db70 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1db80 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 _v2().** interfa
1db90 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20 61 ces use UTF-8, a
1dba0 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 nd sqlite3_prepa
1dbb0 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69 74 re16() and sqlit
1dbc0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
1dbd0 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 2e ).** use UTF-16.
1dbe0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 .**.** If the nB
1dbf0 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 yte argument is
1dc00 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 less than zero,
1dc10 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 61 then zSql is rea
1dc20 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 66 d up to the.** f
1dc30 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e irst zero termin
1dc40 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20 69 ator. If nByte i
1dc50 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 s non-negative,
1dc60 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6d then it is the m
1dc70 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 72 aximum.** number
1dc80 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64 20 of bytes read
1dc90 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65 6e from zSql. When
1dca0 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 nByte is non-ne
1dcb0 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20 7a gative, the.** z
1dcc0 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73 20 Sql string ends
1dcd0 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66 69 at either the fi
1dce0 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27 5c rst '\000' or '\
1dcf0 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65 72 u0000' character
1dd00 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74 65 or.** the nByte
1dd10 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68 65 -th byte, whiche
1dd20 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e ver comes first.
1dd30 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 6b If the caller k
1dd40 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 nows.** that the
1dd50 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e 67 supplied string
1dd60 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 is nul-terminat
1dd70 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 ed, then there i
1dd80 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65 72 s a small.** per
1dd90 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74 61 formance advanta
1dda0 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64 20 ge to be gained
1ddb0 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e 42 by passing an nB
1ddc0 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74 68 yte parameter th
1ddd0 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74 at.** is equal t
1dde0 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
1ddf0 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e 70 bytes in the inp
1de00 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e 63 ut string <i>inc
1de10 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74 68 luding</i>.** th
1de20 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 e nul-terminator
1de30 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 bytes..**.** If
1de40 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20 4e pzTail is not N
1de50 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69 6c ULL then *pzTail
1de60 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e is made to poin
1de70 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 t to the first b
1de80 79 74 65 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 yte.** past the
1de90 65 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 end of the first
1dea0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 SQL statement i
1deb0 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20 72 n zSql. These r
1dec0 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a 20 outines only.**
1ded0 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 69 72 73 compile the firs
1dee0 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a t statement in z
1def0 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20 Sql, so *pzTail
1df00 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
1df10 20 74 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d 61 to.** what rema
1df20 69 6e 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a ins uncompiled..
1df30 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69 73 **.** *ppStmt is
1df40 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 left pointing t
1df50 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70 72 o a compiled [pr
1df60 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1df70 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a 2a ] that can be.**
1df80 20 65 78 65 63 75 74 65 64 20 75 73 69 6e 67 20 executed using
1df90 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1dfa0 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 61 . If there is a
1dfb0 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74 6d 74 n error, *ppStmt
1dfc0 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 is set.** to NU
1dfd0 4c 4c 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 LL. If the inpu
1dfe0 74 20 74 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 t text contains
1dff0 6e 6f 20 53 51 4c 20 28 69 66 20 74 68 65 20 69 no SQL (if the i
1e000 6e 70 75 74 20 69 73 20 61 6e 20 65 6d 70 74 79 nput is an empty
1e010 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72 20 61 20 .** string or a
1e020 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70 comment) then *p
1e030 70 53 74 6d 74 20 69 73 20 73 65 74 20 74 6f 20 pStmt is set to
1e040 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 20 63 61 6c NULL..** The cal
1e050 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 ling procedure i
1e060 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
1e070 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 r deleting the c
1e080 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 ompiled.** SQL s
1e090 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b tatement using [
1e0a0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
1e0b0 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 ()] after it has
1e0c0 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 finished with i
1e0d0 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 20 6d 61 79 t..** ppStmt may
1e0e0 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a not be NULL..**
1e0f0 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 .** On success,
1e100 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 [SQLITE_OK] is r
1e110 65 74 75 72 6e 65 64 2c 20 6f 74 68 65 72 77 69 eturned, otherwi
1e120 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 se an [error cod
1e130 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a e] is returned..
1e140 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
1e150 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 61 3_prepare_v2() a
1e160 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 nd sqlite3_prepa
1e170 72 65 31 36 5f 76 32 28 29 20 69 6e 74 65 72 66 re16_v2() interf
1e180 61 63 65 73 20 61 72 65 0a 2a 2a 20 72 65 63 6f aces are.** reco
1e190 6d 6d 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c 20 mmended for all
1e1a0 6e 65 77 20 70 72 6f 67 72 61 6d 73 2e 20 54 68 new programs. Th
1e1b0 65 20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74 65 e two older inte
1e1c0 72 66 61 63 65 73 20 61 72 65 20 72 65 74 61 69 rfaces are retai
1e1d0 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 61 63 6b 77 ned.** for backw
1e1e0 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
1e1f0 74 79 2c 20 62 75 74 20 74 68 65 69 72 20 75 73 ty, but their us
1e200 65 20 69 73 20 64 69 73 63 6f 75 72 61 67 65 64 e is discouraged
1e210 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 22 76 32 22 ..** In the "v2"
1e220 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 interfaces, the
1e230 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
1e240 65 6e 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 ent.** that is r
1e250 65 74 75 72 6e 65 64 20 28 74 68 65 20 5b 73 71 eturned (the [sq
1e260 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 lite3_stmt] obje
1e270 63 74 29 20 63 6f 6e 74 61 69 6e 73 20 61 20 63 ct) contains a c
1e280 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 opy of the.** or
1e290 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 2e iginal SQL text.
1e2a0 20 54 68 69 73 20 63 61 75 73 65 73 20 74 68 65 This causes the
1e2b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1e2c0 5d 20 69 6e 74 65 72 66 61 63 65 20 74 6f 0a 2a ] interface to.*
1e2d0 2a 20 62 65 68 61 76 65 20 61 20 64 69 66 66 65 * behave a diffe
1e2e0 72 65 6e 74 6c 79 20 69 6e 20 74 77 6f 20 77 61 rently in two wa
1e2f0 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a ys:.**.** <ol>.*
1e300 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 20 74 68 65 * <li>.** If the
1e310 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
1e320 20 63 68 61 6e 67 65 73 2c 20 69 6e 73 74 65 61 changes, instea
1e330 64 20 6f 66 20 72 65 74 75 72 6e 69 6e 67 20 5b d of returning [
1e340 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 61 SQLITE_SCHEMA] a
1e350 73 20 69 74 0a 2a 2a 20 61 6c 77 61 79 73 20 75 s it.** always u
1e360 73 65 64 20 74 6f 20 64 6f 2c 20 5b 73 71 6c 69 sed to do, [sqli
1e370 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c te3_step()] will
1e380 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 automatically r
1e390 65 63 6f 6d 70 69 6c 65 20 74 68 65 20 53 51 4c ecompile the SQL
1e3a0 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 6e .** statement an
1e3b0 64 20 74 72 79 20 74 6f 20 72 75 6e 20 69 74 20 d try to run it
1e3c0 61 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 73 again. If the s
1e3d0 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 chema has change
1e3e0 64 20 69 6e 0a 2a 2a 20 61 20 77 61 79 20 74 68 d in.** a way th
1e3f0 61 74 20 6d 61 6b 65 73 20 74 68 65 20 73 74 61 at makes the sta
1e400 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 tement no longer
1e410 20 76 61 6c 69 64 2c 20 5b 73 71 6c 69 74 65 33 valid, [sqlite3
1e420 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 73 74 _step()] will st
1e430 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 ill.** return [S
1e440 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2e 20 20 QLITE_SCHEMA].
1e450 42 75 74 20 75 6e 6c 69 6b 65 20 74 68 65 20 6c But unlike the l
1e460 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 2c 20 egacy behavior,
1e470 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 [SQLITE_SCHEMA]
1e480 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 66 61 74 61 is.** now a fata
1e490 6c 20 65 72 72 6f 72 2e 20 20 43 61 6c 6c 69 6e l error. Callin
1e4a0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 g [sqlite3_prepa
1e4b0 72 65 5f 76 32 28 29 5d 20 61 67 61 69 6e 20 77 re_v2()] again w
1e4c0 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 20 74 68 65 ill not make the
1e4d0 0a 2a 2a 20 65 72 72 6f 72 20 67 6f 20 61 77 61 .** error go awa
1e4e0 79 2e 20 20 4e 6f 74 65 3a 20 75 73 65 20 5b 73 y. Note: use [s
1e4f0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d qlite3_errmsg()]
1e500 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 65 78 to find the tex
1e510 74 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73 t.** of the pars
1e520 69 6e 67 20 65 72 72 6f 72 20 74 68 61 74 20 72 ing error that r
1e530 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 5b 53 51 esults in an [SQ
1e540 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 72 65 74 LITE_SCHEMA] ret
1e550 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a urn..** </li>.**
1e560 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65 6e .** <li>.** When
1e570 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1e580 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 , [sqlite3_step(
1e590 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 6f )] will return o
1e5a0 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69 6c ne of the detail
1e5b0 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 ed.** [error cod
1e5c0 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 es] or [extended
1e5d0 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20 20 error codes].
1e5e0 54 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 The legacy behav
1e5f0 69 6f 72 20 77 61 73 20 74 68 61 74 0a 2a 2a 20 ior was that.**
1e600 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1e610 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74 75 would only retu
1e620 72 6e 20 61 20 67 65 6e 65 72 69 63 20 5b 53 51 rn a generic [SQ
1e630 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72 65 73 75 LITE_ERROR] resu
1e640 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 79 lt code.** and y
1e650 6f 75 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f ou would have to
1e660 20 6d 61 6b 65 20 61 20 73 65 63 6f 6e 64 20 63 make a second c
1e670 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
1e680 72 65 73 65 74 28 29 5d 20 69 6e 20 6f 72 64 65 reset()] in orde
1e690 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 r.** to find the
1e6a0 20 75 6e 64 65 72 6c 79 69 6e 67 20 63 61 75 73 underlying caus
1e6b0 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c 65 6d e of the problem
1e6c0 2e 20 57 69 74 68 20 74 68 65 20 22 76 32 22 20 . With the "v2"
1e6d0 70 72 65 70 61 72 65 0a 2a 2a 20 69 6e 74 65 72 prepare.** inter
1e6e0 66 61 63 65 73 2c 20 74 68 65 20 75 6e 64 65 72 faces, the under
1e6f0 6c 79 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f 72 lying reason for
1e700 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 72 65 the error is re
1e710 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 turned immediate
1e720 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 ly..** </li>.**
1e730 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 </ol>.**.** Requ
1e740 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
1e750 33 30 31 31 5d 20 5b 48 31 33 30 31 32 5d 20 5b 3011] [H13012] [
1e760 48 31 33 30 31 33 5d 20 5b 48 31 33 30 31 34 5d H13013] [H13014]
1e770 20 5b 48 31 33 30 31 35 5d 20 5b 48 31 33 30 31 [H13015] [H1301
1e780 36 5d 20 5b 48 31 33 30 31 39 5d 20 5b 48 31 33 6] [H13019] [H13
1e790 30 32 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 021].**.*/.SQLIT
1e7a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1e7b0 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73 71 6c 3_prepare(. sql
1e7c0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
1e7d0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1e7e0 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
1e7f0 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 st char *zSql,
1e800 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 /* SQL stat
1e810 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 ement, UTF-8 enc
1e820 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 oded */. int nB
1e830 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 yte,
1e840 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e /* Maximum len
1e850 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 gth of zSql in b
1e860 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 ytes. */. sqlit
1e870 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 e3_stmt **ppStmt
1e880 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 , /* OUT: State
1e890 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 ment handle */.
1e8a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a const char **pz
1e8b0 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a Tail /* OUT:
1e8c0 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 Pointer to unus
1e8d0 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 ed portion of zS
1e8e0 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f ql */.);.SQLITE_
1e8f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1e900 70 72 65 70 61 72 65 5f 76 32 28 0a 20 20 73 71 prepare_v2(. sq
1e910 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
1e920 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
1e930 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f e handle */. co
1e940 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 nst char *zSql,
1e950 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 /* SQL sta
1e960 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e tement, UTF-8 en
1e970 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e coded */. int n
1e980 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 Byte,
1e990 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 /* Maximum le
1e9a0 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 ngth of zSql in
1e9b0 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 bytes. */. sqli
1e9c0 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d te3_stmt **ppStm
1e9d0 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 t, /* OUT: Stat
1e9e0 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a ement handle */.
1e9f0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 const char **p
1ea00 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 zTail /* OUT
1ea10 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 : Pointer to unu
1ea20 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a sed portion of z
1ea30 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 Sql */.);.SQLITE
1ea40 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1ea50 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 _prepare16(. sq
1ea60 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
1ea70 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
1ea80 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f e handle */. co
1ea90 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 nst void *zSql,
1eaa0 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 /* SQL sta
1eab0 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 tement, UTF-16 e
1eac0 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 ncoded */. int
1ead0 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 nByte,
1eae0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c /* Maximum l
1eaf0 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e ength of zSql in
1eb00 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c bytes. */. sql
1eb10 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 ite3_stmt **ppSt
1eb20 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 mt, /* OUT: Sta
1eb30 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f tement handle */
1eb40 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a . const void **
1eb50 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 pzTail /* OU
1eb60 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e T: Pointer to un
1eb70 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 used portion of
1eb80 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 zSql */.);.SQLIT
1eb90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1eba0 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a 3_prepare16_v2(.
1ebb0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
1ebc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
1ebd0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
1ebe0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 const void *zS
1ebf0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c ql, /* SQL
1ec00 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d statement, UTF-
1ec10 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 16 encoded */.
1ec20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 int nByte,
1ec30 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
1ec40 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 um length of zSq
1ec50 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 l in bytes. */.
1ec60 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a sqlite3_stmt **
1ec70 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a ppStmt, /* OUT:
1ec80 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c Statement handl
1ec90 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 e */. const voi
1eca0 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f d **pzTail /
1ecb0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 * OUT: Pointer t
1ecc0 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e o unused portion
1ecd0 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a of zSql */.);..
1ece0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1ecf0 52 65 74 72 69 65 76 69 6e 67 20 53 74 61 74 65 Retrieving State
1ed00 6d 65 6e 74 20 53 51 4c 20 7b 48 31 33 31 30 30 ment SQL {H13100
1ed10 7d 20 3c 48 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a } <H13000>.**.**
1ed20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
1ed30 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 can be used to r
1ed40 65 74 72 69 65 76 65 20 61 20 73 61 76 65 64 20 etrieve a saved
1ed50 63 6f 70 79 20 6f 66 20 74 68 65 20 6f 72 69 67 copy of the orig
1ed60 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20 74 65 78 74 inal.** SQL text
1ed70 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 used to create
1ed80 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
1ed90 65 6d 65 6e 74 5d 20 69 66 20 74 68 61 74 20 73 ement] if that s
1eda0 74 61 74 65 6d 65 6e 74 20 77 61 73 0a 2a 2a 20 tatement was.**
1edb0 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 65 compiled using e
1edc0 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 ither [sqlite3_p
1edd0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
1ede0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
1edf0 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 16_v2()]..**.**
1ee00 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
1ee10 20 5b 48 31 33 31 30 31 5d 20 5b 48 31 33 31 30 [H13101] [H1310
1ee20 32 5d 20 5b 48 31 33 31 30 33 5d 0a 2a 2f 0a 53 2] [H13103].*/.S
1ee30 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1ee40 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 char *sqlite3_sq
1ee50 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a l(sqlite3_stmt *
1ee60 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
1ee70 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63 API3REF: Dynamic
1ee80 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65 ally Typed Value
1ee90 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d Object {H15000}
1eea0 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 <S20200>.** KEY
1eeb0 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65 WORDS: {protecte
1eec0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d d sqlite3_value}
1eed0 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 {unprotected sq
1eee0 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a lite3_value}.**.
1eef0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 ** SQLite uses t
1ef00 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 he sqlite3_value
1ef10 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65 object to repre
1ef20 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a sent all values.
1ef30 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 73 ** that can be s
1ef40 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61 62 tored in a datab
1ef50 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 ase table. SQLit
1ef60 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74 e uses dynamic t
1ef70 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65 yping.** for the
1ef80 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65 values it store
1ef90 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64 s. Values stored
1efa0 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 in sqlite3_valu
1efb0 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e e objects.** can
1efc0 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c be integers, fl
1efd0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
1efe0 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c ues, strings, BL
1eff0 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a OBs, or NULL..**
1f000 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76 .** An sqlite3_v
1f010 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20 alue object may
1f020 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 65 be either "prote
1f030 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74 cted" or "unprot
1f040 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 ected"..** Some
1f050 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69 interfaces requi
1f060 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 re a protected s
1f070 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f qlite3_value. O
1f080 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a ther interfaces.
1f090 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65 ** will accept e
1f0a0 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 65 ither a protecte
1f0b0 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 d or an unprotec
1f0c0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
1f0d0 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65 e..** Every inte
1f0e0 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 70 rface that accep
1f0f0 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ts sqlite3_value
1f100 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 arguments speci
1f110 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20 fies.** whether
1f120 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72 or not it requir
1f130 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 es a protected s
1f140 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a qlite3_value..**
1f150 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70 .** The terms "p
1f160 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22 75 rotected" and "u
1f170 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66 65 nprotected" refe
1f180 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20 r to whether or
1f190 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69 not.** a mutex i
1f1a0 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72 s held. A inter
1f1b0 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c nal mutex is hel
1f1c0 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65 d for a protecte
1f1d0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c d.** sqlite3_val
1f1e0 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f ue object but no
1f1f0 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 mutex is held f
1f200 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 or an unprotecte
1f210 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c d.** sqlite3_val
1f220 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 ue object. If S
1f230 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
1f240 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 d to be single-t
1f250 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68 hreaded.** (with
1f260 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 [SQLITE_THREADS
1f270 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 AFE=0] and with
1f280 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 [sqlite3_threads
1f290 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 afe()] returning
1f2a0 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 0).** or if SQL
1f2b0 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e ite is run in on
1f2c0 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74 e of reduced mut
1f2d0 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 ex modes .** [SQ
1f2e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 LITE_CONFIG_SING
1f2f0 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51 LETHREAD] or [SQ
1f300 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 LITE_CONFIG_MULT
1f310 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e ITHREAD].** then
1f320 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73 there is no dis
1f330 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e tinction between
1f340 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75 protected and u
1f350 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 nprotected.** sq
1f360 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
1f370 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e cts and they can
1f380 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63 68 be used interch
1f390 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76 angeably. Howev
1f3a0 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d er,.** for maxim
1f3b0 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c um code portabil
1f3c0 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d ity it is recomm
1f3d0 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 ended that appli
1f3e0 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c cations.** still
1f3f0 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e make the distin
1f400 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65 ction between be
1f410 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 tween protected
1f420 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a and unprotected.
1f430 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
1f440 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68 objects even wh
1f450 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 en not strictly
1f460 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 required..**.**
1f470 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 The sqlite3_valu
1f480 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 e objects that a
1f490 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 72 re passed as par
1f4a0 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65 ameters into the
1f4b0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
1f4c0 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69 on of [applicati
1f4d0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
1f4e0 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72 unctions] are pr
1f4f0 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20 otected..** The
1f500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
1f510 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 ject returned by
1f520 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c .** [sqlite3_col
1f530 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 umn_value()] is
1f540 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 unprotected..**
1f550 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 Unprotected sqli
1f560 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
1f570 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 s may only be us
1f580 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 ed with.** [sqli
1f590 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
1f5a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
1f5b0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a _bind_value()]..
1f5c0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
1f5d0 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c value_blob | sql
1f5e0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
1f5f0 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 )] family of.**
1f600 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69 interfaces requi
1f610 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c re protected sql
1f620 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
1f630 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ts..*/.typedef s
1f640 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65 truct Mem sqlite
1f650 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 3_value;../*.**
1f660 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75 CAPI3REF: SQL Fu
1f670 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f nction Context O
1f680 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c bject {H16001} <
1f690 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20200>.**.** Th
1f6a0 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 e context in whi
1f6b0 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 ch an SQL functi
1f6c0 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73 on executes is s
1f6d0 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 tored in an.** s
1f6e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f qlite3_context o
1f6f0 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 bject. A pointe
1f700 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f r to an sqlite3_
1f710 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a context object.*
1f720 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 73 * is always firs
1f730 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b t parameter to [
1f740 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
1f750 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ned SQL function
1f760 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 s]..** The appli
1f770 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
1f780 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c QL function impl
1f790 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 ementation will
1f7a0 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69 pass this.** poi
1f7b0 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74 nter through int
1f7c0 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 o calls to [sqli
1f7d0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c te3_result_int |
1f7e0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28 sqlite3_result(
1f7f0 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f )],.** [sqlite3_
1f800 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
1f810 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75 t()], [sqlite3_u
1f820 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 ser_data()],.**
1f830 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 [sqlite3_context
1f840 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b _db_handle()], [
1f850 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
1f860 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f ata()],.** and/o
1f870 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 r [sqlite3_set_a
1f880 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 uxdata()]..*/.ty
1f890 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
1f8a0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c ite3_context sql
1f8b0 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f ite3_context;../
1f8c0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42 *.** CAPI3REF: B
1f8d0 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f inding Values To
1f8e0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d Prepared Statem
1f8f0 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53 ents {H13500} <S
1f900 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 70300>.** KEYWOR
1f910 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 DS: {host parame
1f920 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d ter} {host param
1f930 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 eters} {host par
1f940 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 ameter name}.**
1f950 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 KEYWORDS: {SQL p
1f960 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 arameter} {SQL p
1f970 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 arameters} {para
1f980 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a meter binding}.*
1f990 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20 *.** In the SQL
1f9a0 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f strings input to
1f9b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
1f9c0 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20 e_v2()] and its
1f9d0 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 variants,.** lit
1f9e0 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70 erals may be rep
1f9f0 6c 61 63 65 64 20 62 79 20 61 20 5b 70 61 72 61 laced by a [para
1fa00 6d 65 74 65 72 5d 20 69 6e 20 6f 6e 65 20 6f 66 meter] in one of
1fa10 20 74 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a these forms:.**
1fa20 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
1fa30 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e ?.** <li> ?NN
1fa40 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a N.** <li> :VVV.
1fa50 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a ** <li> @VVV.**
1fa60 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c <li> $VVV.** <
1fa70 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 /ul>.**.** In th
1fa80 65 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d e parameter form
1fa90 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e s shown above NN
1faa0 4e 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 N is an integer
1fab0 6c 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 literal,.** and
1fac0 56 56 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d VVV is an alpha-
1fad0 6e 75 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 numeric paramete
1fae0 72 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75 r name. The valu
1faf0 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 es of these.** p
1fb00 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 arameters (also
1fb10 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 called "host par
1fb20 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 ameter names" or
1fb30 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 "SQL parameters
1fb40 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 ").** can be set
1fb50 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 using the sqlit
1fb60 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 e3_bind_*() rout
1fb70 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 ines defined her
1fb80 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 e..**.** The fir
1fb90 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 st argument to t
1fba0 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f he sqlite3_bind_
1fbb0 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 *() routines is
1fbc0 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e always.** a poin
1fbd0 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 ter to the [sqli
1fbe0 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 te3_stmt] object
1fbf0 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a returned from.*
1fc00 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
1fc10 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 re_v2()] or its
1fc20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 variants..**.**
1fc30 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
1fc40 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 ent is the index
1fc50 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 of the SQL para
1fc60 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e meter to be set.
1fc70 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 .** The leftmost
1fc80 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 SQL parameter h
1fc90 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 as an index of 1
1fca0 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 . When the same
1fcb0 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 named.** SQL pa
1fcc0 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 rameter is used
1fcd0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 more than once,
1fce0 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 second and subse
1fcf0 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 quent.** occurre
1fd00 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 73 61 nces have the sa
1fd10 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 me index as the
1fd20 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 first occurrence
1fd30 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 ..** The index f
1fd40 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 or named paramet
1fd50 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 ers can be looke
1fd60 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a d up using the.*
1fd70 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f * [sqlite3_bind_
1fd80 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 parameter_index(
1fd90 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 65 )] API if desire
1fda0 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a d. The index.**
1fdb0 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 for "?NNN" para
1fdc0 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61 meters is the va
1fdd0 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 lue of NNN..** T
1fde0 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 he NNN value mus
1fdf0 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 t be between 1 a
1fe00 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f nd the [sqlite3_
1fe10 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 limit()].** para
1fe20 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 meter [SQLITE_LI
1fe30 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
1fe40 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 BER] (default va
1fe50 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a lue: 999)..**.**
1fe60 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d The third argum
1fe70 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 ent is the value
1fe80 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 to bind to the
1fe90 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a parameter..**.**
1fea0 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e In those routin
1feb0 65 73 20 74 68 61 74 20 68 61 76 65 20 61 20 66 es that have a f
1fec0 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 ourth argument,
1fed0 69 74 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 its value is the
1fee0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 .** number of by
1fef0 74 65 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d tes in the param
1ff00 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 eter. To be cle
1ff10 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 ar: the value is
1ff20 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
1ff30 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 f <u>bytes</u> i
1ff40 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 n the value, not
1ff50 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
1ff60 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 haracters..** If
1ff70 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 the fourth para
1ff80 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 meter is negativ
1ff90 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 e, the length of
1ffa0 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a the string is.*
1ffb0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
1ffc0 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 bytes up to the
1ffd0 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 first zero termi
1ffe0 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 nator..**.** The
1fff0 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 fifth argument
20000 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f to sqlite3_bind_
20010 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f blob(), sqlite3_
20020 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 bind_text(), and
20030 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 .** sqlite3_bind
20040 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 20 64 _text16() is a d
20050 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 estructor used t
20060 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 o dispose of the
20070 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 BLOB or.** stri
20080 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 ng after SQLite
20090 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 has finished wit
200a0 68 20 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 h it. If the fif
200b0 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a th argument is.*
200c0 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 * the special va
200d0 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 lue [SQLITE_STAT
200e0 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 IC], then SQLite
200f0 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 assumes that th
20100 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e e.** information
20110 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 is in static, u
20120 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 nmanaged space a
20130 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 nd does not need
20140 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a to be freed..**
20150 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72 If the fifth ar
20160 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 gument has the v
20170 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 alue [SQLITE_TRA
20180 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a NSIENT], then.**
20190 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 SQLite makes it
201a0 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f s own private co
201b0 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 py of the data i
201c0 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f mmediately, befo
201d0 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 re.** the sqlite
201e0 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 3_bind_*() routi
201f0 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a ne returns..**.*
20200 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 * The sqlite3_bi
20210 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f nd_zeroblob() ro
20220 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c utine binds a BL
20230 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 OB of length N t
20240 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 hat.** is filled
20250 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 with zeroes. A
20260 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 zeroblob uses a
20270 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 fixed amount of
20280 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 memory.** (just
20290 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 an integer to h
202a0 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 old its size) wh
202b0 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 ile it is being
202c0 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 processed..** Ze
202d0 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 roblobs are inte
202e0 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 nded to serve as
202f0 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f placeholders fo
20300 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a r BLOBs whose.**
20310 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 content is late
20320 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a r written using.
20330 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ** [sqlite3_blob
20340 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e _open | incremen
20350 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f tal BLOB I/O] ro
20360 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 utines..** A neg
20370 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 ative value for
20380 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 the zeroblob res
20390 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c ults in a zero-l
203a0 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a ength BLOB..**.*
203b0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 * The sqlite3_bi
203c0 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 nd_*() routines
203d0 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 must be called a
203e0 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 fter.** [sqlite3
203f0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 _prepare_v2()] (
20400 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 and its variants
20410 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 ) or [sqlite3_re
20420 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 set()] and.** be
20430 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 fore [sqlite3_st
20440 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e ep()]..** Bindin
20450 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 gs are not clear
20460 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 ed by the [sqlit
20470 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 e3_reset()] rout
20480 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 ine..** Unbound
20490 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 parameters are i
204a0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 nterpreted as NU
204b0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 LL..**.** These
204c0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
204d0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 [SQLITE_OK] on s
204e0 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 uccess or an err
204f0 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e or code if.** an
20500 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ything goes wron
20510 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 g. [SQLITE_RANG
20520 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 E] is returned i
20530 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a f the parameter.
20540 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 ** index is out
20550 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 of range. [SQLI
20560 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 TE_NOMEM] is ret
20570 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 urned if malloc(
20580 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c ) fails..** [SQL
20590 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 ITE_MISUSE] migh
205a0 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 t be returned if
205b0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
205c0 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a are called on a.
205d0 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 ** virtual machi
205e0 6e 65 20 74 68 61 74 20 69 73 20 74 68 65 20 77 ne that is the w
205f0 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 rong state or wh
20600 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 ich has already
20610 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a been finalized..
20620 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 ** Detection of
20630 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 misuse is unreli
20640 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 able. Applicati
20650 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 ons should not d
20660 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 epend.** on SQLI
20670 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e TE_MISUSE return
20680 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 s. SQLITE_MISUS
20690 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f E is intended to
206a0 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 indicate a.** a
206b0 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 logic error in
206c0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e the application.
206d0 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e Future version
206e0 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 s of SQLite migh
206f0 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 t.** panic rathe
20700 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 r than return SQ
20710 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a LITE_MISUSE..**.
20720 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
20730 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
20740 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a eter_count()],.*
20750 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f * [sqlite3_bind_
20760 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 parameter_name()
20770 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
20780 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
20790 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 ndex()]..**.** R
207a0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
207b0 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35 30 39 [H13506] [H13509
207c0 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31 33 35 ] [H13512] [H135
207d0 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b 48 31 15] [H13518] [H1
207e0 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d 20 5b 3521] [H13524] [
207f0 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31 33 35 H13527].** [H135
20800 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b 48 31 30] [H13533] [H1
20810 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d 20 5b 3536] [H13539] [
20820 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34 35 5d H13542] [H13545]
20830 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33 35 35 [H13548] [H1355
20840 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 1].**.*/.SQLITE_
20850 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
20860 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 bind_blob(sqlite
20870 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
20880 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e nst void*, int n
20890 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
208a0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
208b0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 t sqlite3_bind_d
208c0 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 ouble(sqlite3_st
208d0 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 mt*, int, double
208e0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
208f0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 t sqlite3_bind_i
20900 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a nt(sqlite3_stmt*
20910 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
20920 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
20930 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 te3_bind_int64(s
20940 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
20950 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 t, sqlite3_int64
20960 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
20970 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e t sqlite3_bind_n
20980 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ull(sqlite3_stmt
20990 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
209a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
209b0 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65 bind_text(sqlite
209c0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 3_stmt*, int, co
209d0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e nst char*, int n
209e0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 , void(*)(void*)
209f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
20a00 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 t sqlite3_bind_t
20a10 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ext16(sqlite3_st
20a20 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 mt*, int, const
20a30 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 void*, int, void
20a40 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
20a50 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
20a60 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 te3_bind_value(s
20a70 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
20a80 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 t, const sqlite3
20a90 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
20aa0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
20ab0 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 _bind_zeroblob(s
20ac0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
20ad0 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a t, int n);../*.*
20ae0 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 * CAPI3REF: Numb
20af0 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 er Of SQL Parame
20b00 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53 ters {H13600} <S
20b10 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 70300>.**.** Thi
20b20 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 s routine can be
20b30 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68 used to find th
20b40 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c e number of [SQL
20b50 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 parameters].**
20b60 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 in a [prepared s
20b70 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 tatement]. SQL
20b80 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74 parameters are t
20b90 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20 okens of the.**
20ba0 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 form "?", "?NNN"
20bb0 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22 , ":AAA", "$AAA"
20bc0 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74 , or "@AAA" that
20bd0 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61 serve as.** pla
20be0 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61 ceholders for va
20bf0 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73 lues that are [s
20c00 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
20c10 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 | bound].** to
20c20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 the parameters a
20c30 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a t a later time..
20c40 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
20c50 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75 ne actually retu
20c60 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 rns the index of
20c70 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69 the largest (ri
20c80 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 ghtmost).** para
20c90 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 meter. For all f
20ca0 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e orms except ?NNN
20cb0 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 , this will corr
20cc0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a espond to the.**
20cd0 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 number of uniqu
20ce0 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49 e parameters. I
20cf0 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 f parameters of
20d00 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65 the ?NNN are use
20d10 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20 d,.** there may
20d20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c be gaps in the l
20d30 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ist..**.** See a
20d40 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 lso: [sqlite3_bi
20d50 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f nd_blob|sqlite3_
20d60 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c bind()],.** [sql
20d70 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
20d80 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 ter_name()], and
20d90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
20da0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
20db0 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 x()]..**.** Requ
20dc0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
20dd0 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3601].*/.SQLITE_
20de0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
20df0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 bind_parameter_c
20e00 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d ount(sqlite3_stm
20e10 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
20e20 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 3REF: Name Of A
20e30 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b Host Parameter {
20e40 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e H13620} <S70300>
20e50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
20e60 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ine returns a po
20e70 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d inter to the nam
20e80 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a e of the n-th.**
20e90 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d [SQL parameter]
20ea0 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 in a [prepared
20eb0 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 statement]..** S
20ec0 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 QL parameters of
20ed0 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 the form "?NNN"
20ee0 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 or ":AAA" or "@
20ef0 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a AAA" or "$AAA".*
20f00 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 * have a name wh
20f10 69 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e ich is the strin
20f20 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 g "?NNN" or ":AA
20f30 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 A" or "@AAA" or
20f40 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 "$AAA".** respec
20f50 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 tively..** In ot
20f60 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 her words, the i
20f70 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 nitial ":" or "$
20f80 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a " or "@" or "?".
20f90 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 ** is included a
20fa0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 s part of the na
20fb0 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 me..** Parameter
20fc0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f s of the form "?
20fd0 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c " without a foll
20fe0 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 owing integer ha
20ff0 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e ve no name.** an
21000 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 d are also refer
21010 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 red to as "anony
21020 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 mous parameters"
21030 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
21040 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 t host parameter
21050 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
21060 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 1, not 0..**.**
21070 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 If the value n
21080 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 is out of range
21090 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 or if the n-th p
210a0 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e arameter is.** n
210b0 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 ameless, then NU
210c0 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 LL is returned.
210d0 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 The returned st
210e0 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 ring is.** alway
210f0 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 s in UTF-8 encod
21100 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 ing even if the
21110 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 named parameter
21120 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c was.** originall
21130 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20 55 y specified as U
21140 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 TF-16 in [sqlite
21150 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 3_prepare16()] o
21160 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 r.** [sqlite3_pr
21170 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a epare16_v2()]..*
21180 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
21190 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
211a0 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 b|sqlite3_bind()
211b0 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 ],.** [sqlite3_b
211c0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f ind_parameter_co
211d0 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b unt()], and.** [
211e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
211f0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e ameter_index()].
21200 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
21210 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32 31 5d nts:.** [H13621]
21220 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
21230 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
21240 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
21250 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 r_name(sqlite3_s
21260 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a tmt*, int);../*.
21270 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64 ** CAPI3REF: Ind
21280 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65 ex Of A Paramete
21290 72 20 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e r With A Given N
212a0 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c 53 37 ame {H13640} <S7
212b0 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 0300>.**.** Retu
212c0 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 rn the index of
212d0 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 an SQL parameter
212e0 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e given its name.
212f0 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76 The.** index v
21300 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
21310 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 suitable for us
21320 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a e as the second.
21330 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 ** parameter to
21340 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
21350 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 ob|sqlite3_bind(
21360 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a 20 69 )]. A zero.** i
21370 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f s returned if no
21380 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d 65 matching parame
21390 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20 54 ter is found. T
213a0 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 he parameter.**
213b0 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69 76 name must be giv
213c0 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65 6e en in UTF-8 even
213d0 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c if the original
213e0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61 statement.** wa
213f0 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d 20 s prepared from
21400 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69 6e UTF-16 text usin
21410 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 g [sqlite3_prepa
21420 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a re16_v2()]..**.*
21430 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
21440 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 ite3_bind_blob|s
21450 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a qlite3_bind()],.
21460 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 ** [sqlite3_bind
21470 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 _parameter_count
21480 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c ()], and.** [sql
21490 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
214a0 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a ter_index()]..**
214b0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
214c0 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a 2a 2f :.** [H13641].*/
214d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
214e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
214f0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c ameter_index(sql
21500 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 ite3_stmt*, cons
21510 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a t char *zName);.
21520 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
21530 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64 69 Reset All Bindi
21540 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72 65 ngs On A Prepare
21550 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 d Statement {H13
21560 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 660} <S70300>.**
21570 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74 6f 20 .** Contrary to
21580 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66 the intuition of
21590 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f many, [sqlite3_
215a0 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f reset()] does no
215b0 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b t reset.** the [
215c0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
215d0 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e b | bindings] on
215e0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
215f0 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65 20 tement]..** Use
21600 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 this routine to
21610 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 70 reset all host p
21620 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c arameters to NUL
21630 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 L..**.** Require
21640 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 36 ments:.** [H1366
21650 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 1].*/.SQLITE_API
21660 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 int sqlite3_cle
21670 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 ar_bindings(sqli
21680 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a te3_stmt*);../*.
21690 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d ** CAPI3REF: Num
216a0 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49 ber Of Columns I
216b0 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 7b n A Result Set {
216c0 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30 30 3e H13710} <S10700>
216d0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 .**.** Return th
216e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
216f0 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c mns in the resul
21700 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 t set returned b
21710 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 y the.** [prepar
21720 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54 ed statement]. T
21730 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
21740 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20 69 rns 0 if pStmt i
21750 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74 s an SQL.** stat
21760 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20 ement that does
21770 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 20 not return data
21780 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 (for example an
21790 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a [UPDATE])..**.**
217a0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
217b0 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a 53 51 * [H13711].*/.SQ
217c0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
217d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e ite3_column_coun
217e0 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
217f0 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
21800 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 API3REF: Column
21810 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c Names In A Resul
21820 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c t Set {H13720} <
21830 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10700>.**.** Th
21840 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 ese routines ret
21850 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 urn the name ass
21860 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 igned to a parti
21870 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 cular column.**
21880 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
21890 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 t of a [SELECT]
218a0 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 statement. The
218b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
218c0 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 ame().** interfa
218d0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
218e0 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 nter to a zero-t
218f0 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 erminated UTF-8
21900 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 string.** and sq
21910 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
21920 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 e16() returns a
21930 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 pointer to a zer
21940 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 o-terminated.**
21950 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 UTF-16 string.
21960 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
21970 74 65 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 ter is the [prep
21980 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a ared statement].
21990 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ** that implemen
219a0 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 ts the [SELECT]
219b0 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 statement. The s
219c0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
219d0 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e is the.** column
219e0 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 number. The le
219f0 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 ftmost column is
21a00 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a number 0..**.**
21a10 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 The returned st
21a20 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 ring pointer is
21a30 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 valid until eith
21a40 65 72 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 er the [prepared
21a50 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 statement].** i
21a60 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b s destroyed by [
21a70 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
21a80 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 ()] or until the
21a90 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a next call to.**
21aa0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
21ab0 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 name() or sqlite
21ac0 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 3_column_name16(
21ad0 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f ) on the same co
21ae0 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 lumn..**.** If s
21af0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
21b00 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 fails during the
21b10 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 processing of e
21b20 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a ither routine.**
21b30 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 (for example du
21b40 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f ring a conversio
21b50 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 n from UTF-8 to
21b60 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a UTF-16) then a.*
21b70 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 * NULL pointer i
21b80 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
21b90 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 * The name of a
21ba0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 result column is
21bb0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
21bc0 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f e "AS" clause fo
21bd0 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e r.** that column
21be0 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e , if there is an
21bf0 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 AS clause. If
21c00 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 there is no AS c
21c10 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 lause.** then th
21c20 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f e name of the co
21c30 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 lumn is unspecif
21c40 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e ied and may chan
21c50 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 ge from.** one r
21c60 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 elease of SQLite
21c70 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a to the next..**
21c80 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
21c90 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20 5b 48 :.** [H13721] [H
21ca0 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34 5d 20 13723] [H13724]
21cb0 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37 32 36 [H13725] [H13726
21cc0 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a 53 51 ] [H13727].*/.SQ
21cd0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
21ce0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
21cf0 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 umn_name(sqlite3
21d00 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a _stmt*, int N);.
21d10 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
21d20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 void *sqlite3_c
21d30 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c olumn_name16(sql
21d40 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
21d50 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 N);../*.** CAPI3
21d60 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20 44 REF: Source Of D
21d70 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20 52 ata In A Query R
21d80 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20 3c esult {H13740} <
21d90 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10700>.**.** Th
21da0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 6f ese routines pro
21db0 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f 20 vide a means to
21dc0 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 63 determine what c
21dd0 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a olumn of what.**
21de0 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20 table in which
21df0 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 6c database a resul
21e00 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 t of a [SELECT]
21e10 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 statement comes
21e20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d from..** The nam
21e30 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
21e40 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 6f e or table or co
21e50 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 75 lumn can be retu
21e60 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 65 rned as.** eithe
21e70 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54 46 r a UTF-8 or UTF
21e80 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65 -16 string. The
21e90 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75 74 _database_ rout
21ea0 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74 ines return.** t
21eb0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 he database name
21ec0 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f , the _table_ ro
21ed0 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 utines return th
21ee0 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e e table name, an
21ef0 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f d.** the origin_
21f00 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e routines return
21f10 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 the column name
21f20 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 ..** The returne
21f30 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c 69 d string is vali
21f40 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 65 d until the [pre
21f50 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
21f60 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a is destroyed.**
21f70 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
21f80 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 finalize()] or u
21f90 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 6e ntil the same in
21fa0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 formation is req
21fb0 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20 uested.** again
21fc0 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 65 in a different e
21fd0 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 ncoding..**.** T
21fe0 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e 65 he names returne
21ff0 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69 6e d are the origin
22000 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61 al un-aliased na
22010 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 mes of the.** da
22020 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 61 tabase, table, a
22030 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a nd column..**.**
22040 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
22050 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f ent to the follo
22060 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 20 wing calls is a
22070 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
22080 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66 ent]..** These f
22090 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
220a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
220b0 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e t the Nth column
220c0 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 returned by.**
220d0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 the statement, w
220e0 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 here N is the se
220f0 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 cond function ar
22100 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 gument..**.** If
22110 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
22120 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
22130 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e 20 statement is an
22140 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a 2a expression or.**
22150 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 73 subquery and is
22160 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61 not a column va
22170 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66 lue, then all of
22180 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
22190 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e return.** NULL.
221a0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 These routine
221b0 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 72 might also retur
221c0 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f n NULL if a memo
221d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
221e0 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 ror.** occurs.
221f0 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 20 Otherwise, they
22200 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 return the name
22210 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64 20 of the attached
22220 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 0a database, table.
22230 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68 ** and column th
22240 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 20 at query result
22250 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72 61 column was extra
22260 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a cted from..**.**
22270 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74 68 As with all oth
22280 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c 20 er SQLite APIs,
22290 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64 20 those postfixed
222a0 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72 6e with "16" return
222b0 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 .** UTF-16 encod
222c0 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65 20 ed strings, the
222d0 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 other functions
222e0 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b 45 return UTF-8. {E
222f0 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ND}.**.** These
22300 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 APIs are only av
22310 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c ailable if the l
22320 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 ibrary was compi
22330 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 led with the.**
22340 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 [SQLITE_ENABLE_C
22350 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20 OLUMN_METADATA]
22360 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 C-preprocessor s
22370 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a ymbol defined..*
22380 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a 2a *.** {A13751}.**
22390 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 If two or more
223a0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 threads call one
223b0 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 73 or more of thes
223c0 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 6e e routines again
223d0 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70 st the same.** p
223e0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
223f0 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20 t and column at
22400 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68 the same time th
22410 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 en the results a
22420 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e re.** undefined.
22430 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
22440 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34 31 5d nts:.** [H13741]
22450 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33 37 34 [H13742] [H1374
22460 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48 31 33 3] [H13744] [H13
22470 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20 5b 48 745] [H13746] [H
22480 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 13748].**.** If
22490 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 two or more thre
224a0 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 ads call one or
224b0 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 more.** [sqlite3
224c0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
224d0 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d _name | column m
224e0 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61 63 etadata interfac
224f0 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 es].** for the s
22500 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 ame [prepared st
22510 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73 atement] and res
22520 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 ult column.** at
22530 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 the same time t
22540 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 hen the results
22550 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a are undefined..*
22560 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
22570 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
22580 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
22590 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
225a0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 mt*,int);.SQLITE
225b0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
225c0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
225d0 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 database_name16(
225e0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
225f0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
22600 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
22610 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f e3_column_table_
22620 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
22630 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
22640 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
22650 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
22660 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 able_name16(sqli
22670 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a te3_stmt*,int);.
22680 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
22690 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
226a0 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d olumn_origin_nam
226b0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
226c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
226d0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
226e0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 ite3_column_orig
226f0 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 in_name16(sqlite
22700 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 3_stmt*,int);../
22710 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
22720 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65 eclared Datatype
22730 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75 Of A Query Resu
22740 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53 31 30 lt {H13760} <S10
22750 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 700>.**.** The f
22760 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 irst parameter i
22770 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 s a [prepared st
22780 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 atement]..** If
22790 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 this statement i
227a0 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 s a [SELECT] sta
227b0 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 4e tement and the N
227c0 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 th column of the
227d0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65 73 .** returned res
227e0 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20 ult set of that
227f0 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74 61 [SELECT] is a ta
22800 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20 ble column (not
22810 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e an.** expression
22820 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 68 or subquery) th
22830 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 20 en the declared
22840 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c type of the tabl
22850 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72 e.** column is r
22860 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 eturned. If the
22870 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 Nth column of t
22880 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 he result set is
22890 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f an.** expressio
228a0 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20 74 n or subquery, t
228b0 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 hen a NULL point
228c0 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a er is returned..
228d0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
228e0 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73 string is always
228f0 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 20 UTF-8 encoded.
22900 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 {END}.**.** For
22910 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74 example, given t
22920 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 he database sche
22930 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 ma:.**.** CREATE
22940 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 52 TABLE t1(c1 VAR
22950 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 IANT);.**.** and
22960 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
22970 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 tatement to be c
22980 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 ompiled:.**.** S
22990 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 31 ELECT c1 + 1, c1
229a0 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 FROM t1;.**.**
229b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 75 this routine wou
229c0 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 ld return the st
229d0 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 66 ring "VARIANT" f
229e0 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72 65 or the second re
229f0 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28 sult.** column (
22a00 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c i==1), and a NUL
22a10 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 L pointer for th
22a20 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 63 e first result c
22a30 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a 2a olumn (i==0)..**
22a40 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 .** SQLite uses
22a50 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69 6d 65 dynamic run-time
22a60 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a 75 73 typing. So jus
22a70 74 20 62 65 63 61 75 73 65 20 61 20 63 6f 6c 75 t because a colu
22a80 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61 72 65 mn.** is declare
22a90 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 70 d to contain a p
22aa0 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 20 64 articular type d
22ab0 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61 oes not mean tha
22ac0 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 73 74 t the.** data st
22ad0 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 6f 6c ored in that col
22ae0 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 64 65 umn is of the de
22af0 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 53 51 clared type. SQ
22b00 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72 6f 6e Lite is.** stron
22b10 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74 20 74 gly typed, but t
22b20 68 65 20 74 79 70 69 6e 67 20 69 73 20 64 79 6e he typing is dyn
22b30 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69 63 2e amic not static.
22b40 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61 73 73 Type.** is ass
22b50 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e 64 ociated with ind
22b60 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73 2c 20 ividual values,
22b70 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63 6f 6e not with the con
22b80 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65 64 20 tainers.** used
22b90 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20 76 61 to hold those va
22ba0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 lues..**.** Requ
22bb0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
22bc0 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d 20 5b 3761] [H13762] [
22bd0 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 H13763].*/.SQLIT
22be0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
22bf0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
22c00 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 _decltype(sqlite
22c10 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 3_stmt*,int);.SQ
22c20 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
22c30 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
22c40 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 umn_decltype16(s
22c50 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 qlite3_stmt*,int
22c60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
22c70 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41 6e 20 EF: Evaluate An
22c80 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 SQL Statement {H
22c90 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 13200} <S10000>.
22ca0 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b 70 **.** After a [p
22cb0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
22cc0 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65 70 t] has been prep
22cd0 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 ared using eithe
22ce0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 r.** [sqlite3_pr
22cf0 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b epare_v2()] or [
22d00 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
22d10 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 6_v2()] or one o
22d20 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 f the legacy.**
22d30 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69 interfaces [sqli
22d40 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f te3_prepare()] o
22d50 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 r [sqlite3_prepa
22d60 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66 75 re16()], this fu
22d70 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 nction.** must b
22d80 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 e called one or
22d90 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65 76 more times to ev
22da0 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74 65 aluate the state
22db0 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
22dc0 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 62 details of the b
22dd0 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 73 ehavior of the s
22de0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e qlite3_step() in
22df0 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a 2a terface depend.*
22e00 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 65 * on whether the
22e10 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 statement was p
22e20 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 68 repared using th
22e30 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e 74 e newer "v2" int
22e40 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 74 erface.** [sqlit
22e50 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
22e60 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 and [sqlite3_pr
22e70 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 epare16_v2()] or
22e80 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 63 the older legac
22e90 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 5b y.** interface [
22ea0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
22eb0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
22ec0 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20 54 prepare16()]. T
22ed0 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a he use of the.**
22ee0 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72 66 new "v2" interf
22ef0 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 ace is recommend
22f00 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c 69 ed for new appli
22f10 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65 20 cations but the
22f20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 legacy.** interf
22f30 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 ace will continu
22f40 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 e to be supporte
22f50 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 d..**.** In the
22f60 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 legacy interface
22f70 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c , the return val
22f80 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65 ue will be eithe
22f90 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c r [SQLITE_BUSY],
22fa0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 .** [SQLITE_DONE
22fb0 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c ], [SQLITE_ROW],
22fc0 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c [SQLITE_ERROR],
22fd0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 or [SQLITE_MISU
22fe0 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 SE]..** With the
22ff0 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c "v2" interface,
23000 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65 any of the othe
23010 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 73 5d r [result codes]
23020 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 or.** [extended
23030 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 6d result codes] m
23040 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 ight be returned
23050 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 as well..**.**
23060 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6d 65 [SQLITE_BUSY] me
23070 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 61 74 ans that the dat
23080 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77 61 73 abase engine was
23090 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71 75 69 unable to acqui
230a0 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 re the.** databa
230b0 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65 65 64 se locks it need
230c0 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62 2e s to do its job.
230d0 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 If the stateme
230e0 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 54 5d nt is a [COMMIT]
230f0 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f 75 .** or occurs ou
23100 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70 6c tside of an expl
23110 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e icit transaction
23120 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20 72 , then you can r
23130 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 etry the.** stat
23140 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 73 ement. If the s
23150 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 tatement is not
23160 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 20 6f a [COMMIT] and o
23170 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 0a 2a ccurs within a.*
23180 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 * explicit trans
23190 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20 action then you
231a0 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20 should rollback
231b0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
231c0 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e before.** contin
231d0 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c uing..**.** [SQL
231e0 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20 ITE_DONE] means
231f0 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65 that the stateme
23200 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 nt has finished
23210 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63 executing.** suc
23220 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69 cessfully. sqli
23230 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c te3_step() shoul
23240 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 d not be called
23250 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 76 69 again on this vi
23260 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 rtual.** machine
23270 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 63 without first c
23280 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
23290 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 73 65 reset()] to rese
232a0 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a t the virtual.**
232b0 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 74 6f machine back to
232c0 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61 its initial sta
232d0 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 te..**.** If the
232e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 SQL statement b
232f0 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 72 65 eing executed re
23300 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 2c 20 turns any data,
23310 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 then [SQLITE_ROW
23320 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 ].** is returned
23330 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65 77 each time a new
23340 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 row of data is
23350 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63 65 73 ready for proces
23360 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20 63 sing by the.** c
23370 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75 65 aller. The value
23380 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 s may be accesse
23390 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63 6f 6c d using the [col
233a0 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 umn access funct
233b0 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74 65 ions]..** sqlite
233c0 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c 6c 3_step() is call
233d0 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74 72 ed again to retr
233e0 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72 6f ieve the next ro
233f0 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a w of data..**.**
23400 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
23410 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72 75 6e means that a run
23420 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73 75 63 -time error (suc
23430 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61 69 6e h as a constrain
23440 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 29 20 t.** violation)
23450 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 73 has occurred. s
23460 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68 qlite3_step() sh
23470 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c ould not be call
23480 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20 74 ed again on.** t
23490 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e 66 6f he VM. More info
234a0 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 66 rmation may be f
234b0 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 ound by calling
234c0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
234d0 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 )]..** With the
234e0 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 legacy interface
234f0 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69 66 69 , a more specifi
23500 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28 66 6f c error code (fo
23510 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 r example,.** [S
23520 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d QLITE_INTERRUPT]
23530 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 , [SQLITE_SCHEMA
23540 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52 52 55 ], [SQLITE_CORRU
23550 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 PT], and so fort
23560 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 62 74 h).** can be obt
23570 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 ained by calling
23580 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
23590 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 )] on the.** [pr
235a0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
235b0 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32 22 20 ]. In the "v2"
235c0 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 74 68 interface,.** th
235d0 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20 e more specific
235e0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
235f0 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 20 turned directly
23600 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 by sqlite3_step(
23610 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 )..**.** [SQLITE
23620 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73 20 74 _MISUSE] means t
23630 68 61 74 20 74 68 65 20 74 68 69 73 20 72 6f 75 hat the this rou
23640 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20 tine was called
23650 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e inappropriately.
23660 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74 20 77 .** Perhaps it w
23670 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b as called on a [
23680 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
23690 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 nt] that has.**
236a0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b 73 71 already been [sq
236b0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c lite3_finalize |
236c0 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 6f finalized] or o
236d0 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 0a 2a n one that had.*
236e0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74 * previously ret
236f0 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 45 52 urned [SQLITE_ER
23700 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f ROR] or [SQLITE_
23710 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 63 6f DONE]. Or it co
23720 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20 63 61 uld.** be the ca
23730 73 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 se that the same
23740 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
23750 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 75 73 tion is being us
23760 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a 2a 20 ed by two or.**
23770 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 74 20 more threads at
23780 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 20 the same moment
23790 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c in time..**.** <
237a0 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61 63 b>Goofy Interfac
237b0 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e 20 e Alert:</b> In
237c0 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 the legacy inter
237d0 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74 65 face, the sqlite
237e0 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 20 3_step().** API
237f0 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 always returns a
23800 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20 63 generic error c
23810 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 ode, [SQLITE_ERR
23820 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 OR], following a
23830 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68 65 ny.** error othe
23840 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 42 r than [SQLITE_B
23850 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 USY] and [SQLITE
23860 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20 6d _MISUSE]. You m
23870 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 6c ust call.** [sql
23880 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 ite3_reset()] or
23890 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
238a0 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20 74 ze()] in order t
238b0 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74 68 o find one of th
238c0 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b 65 e.** specific [e
238d0 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61 74 rror codes] that
238e0 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62 65 better describe
238f0 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 s the error..**
23900 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74 68 We admit that th
23910 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64 65 is is a goofy de
23920 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62 6c sign. The probl
23930 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78 65 em has been fixe
23940 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 76 d.** with the "v
23950 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 2" interface. I
23960 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61 6c f you prepare al
23970 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73 74 l of your SQL st
23980 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 6e atements.** usin
23990 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65 g either [sqlite
239a0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
239b0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 or [sqlite3_prep
239c0 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73 74 are16_v2()] inst
239d0 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 65 ead.** of the le
239e0 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 gacy [sqlite3_pr
239f0 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 epare()] and [sq
23a00 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 lite3_prepare16(
23a10 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a 2a )] interfaces,.*
23a20 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65 20 * then the more
23a30 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20 specific [error
23a40 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 72 codes] are retur
23a50 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 ned directly.**
23a60 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 by sqlite3_step(
23a70 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74 ). The use of t
23a80 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 he "v2" interfac
23a90 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 e is recommended
23aa0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
23ab0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32 30 32 ents:.** [H13202
23ac0 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31 35 33 ] [H15304] [H153
23ad0 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b 48 31 06] [H15308] [H1
23ae0 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 5310].*/.SQLITE_
23af0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
23b00 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d step(sqlite3_stm
23b10 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
23b20 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 3REF: Number of
23b30 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 columns in a res
23b40 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d ult set {H13770}
23b50 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
23b60 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 Returns the numb
23b70 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20 er of values in
23b80 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 the current row
23b90 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
23ba0 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 t..**.** Require
23bb0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 37 ments:.** [H1377
23bc0 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a 53 1] [H13772].*/.S
23bd0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
23be0 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 lite3_data_count
23bf0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
23c00 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Stmt);../*.** CA
23c10 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d 65 6e PI3REF: Fundamen
23c20 74 61 6c 20 44 61 74 61 74 79 70 65 73 20 7b 48 tal Datatypes {H
23c30 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30 3e 3c 10265} <S10110><
23c40 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59 57 4f S10120>.** KEYWO
23c50 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58 54 RDS: SQLITE_TEXT
23c60 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36 7d 20 .**.** {H10266}
23c70 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e 20 53 Every value in S
23c80 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20 6f 66 QLite has one of
23c90 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e 74 61 five fundamenta
23ca0 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a 2a 0a l datatypes:.**.
23cb0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
23cc0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
23cd0 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36 34 teger.** <li> 64
23ce0 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61 74 69 -bit IEEE floati
23cf0 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 0a ng point number.
23d00 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67 0a 2a ** <li> string.*
23d10 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20 3c * <li> BLOB.** <
23d20 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c li> NULL.** </ul
23d30 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 > {END}.**.** Th
23d40 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 ese constants ar
23d50 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 63 68 e codes for each
23d60 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 73 2e of those types.
23d70 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
23d80 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 58 54 the SQLITE_TEXT
23d90 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 61 6c constant was al
23da0 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 so used in SQLit
23db0 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 66 e version 2.** f
23dc0 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20 or a completely
23dd0 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e different meanin
23de0 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 g. Software tha
23df0 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 t links against
23e00 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 20 76 both.** SQLite v
23e10 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c ersion 2 and SQL
23e20 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73 68 ite version 3 sh
23e30 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45 33 ould use SQLITE3
23e40 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53 51 _TEXT, not.** SQ
23e50 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 64 LITE_TEXT..*/.#d
23e60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 efine SQLITE_INT
23e70 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e 65 20 EGER 1.#define
23e80 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 20 20 SQLITE_FLOAT
23e90 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
23ea0 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 65 66 _BLOB 4.#def
23eb0 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 ine SQLITE_NULL
23ec0 20 20 20 20 35 0a 23 69 66 64 65 66 20 53 51 4c 5.#ifdef SQL
23ed0 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 65 66 ITE_TEXT.# undef
23ee0 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 65 6c SQLITE_TEXT.#el
23ef0 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
23f00 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a 23 65 TE_TEXT 3.#e
23f10 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 51 4c ndif.#define SQL
23f20 49 54 45 33 5f 54 45 58 54 20 20 20 20 20 33 0a ITE3_TEXT 3.
23f30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
23f40 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73 20 46 Result Values F
23f50 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48 31 33 rom A Query {H13
23f60 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 800} <S10700>.**
23f70 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6c 75 KEYWORDS: {colu
23f80 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 mn access functi
23f90 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ons}.**.** These
23fa0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d 20 74 routines form t
23fb0 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 20 71 he "result set q
23fc0 75 65 72 79 22 20 69 6e 74 65 72 66 61 63 65 2e uery" interface.
23fd0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
23fe0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e 66 tines return inf
23ff0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 ormation about a
24000 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f single column o
24010 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a f the current.**
24020 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 result row of a
24030 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 65 72 query. In ever
24040 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73 74 y case the first
24050 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 argument is a p
24060 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ointer.** to the
24070 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
24080 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65 ment] that is be
24090 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 74 ing evaluated (t
240a0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 he [sqlite3_stmt
240b0 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 72 *].** that was r
240c0 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71 eturned from [sq
240d0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
240e0 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 ()] or one of it
240f0 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61 s variants).** a
24100 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 nd the second ar
24110 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e gument is the in
24120 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d dex of the colum
24130 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f n for which info
24140 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c rmation.** shoul
24150 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 d be returned.
24160 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c The leftmost col
24170 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c umn of the resul
24180 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69 6e t set has the in
24190 64 65 78 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 dex 0..**.** If
241a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
241b0 74 20 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 t does not curre
241c0 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 ntly point to a
241d0 76 61 6c 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 valid row, or if
241e0 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 the.** column i
241f0 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72 ndex is out of r
24200 61 6e 67 65 2c 20 74 68 65 20 72 65 73 75 6c 74 ange, the result
24210 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
24220 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
24230 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c may only be cal
24240 6c 65 64 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 led when the mos
24250 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f t recent call to
24260 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 .** [sqlite3_ste
24270 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 p()] has returne
24280 64 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 d [SQLITE_ROW] a
24290 6e 64 20 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 nd neither.** [s
242a0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
242b0 6e 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e nor [sqlite3_fin
242c0 61 6c 69 7a 65 28 29 5d 20 68 61 76 65 20 62 65 alize()] have be
242d0 65 6e 20 63 61 6c 6c 65 64 20 73 75 62 73 65 71 en called subseq
242e0 75 65 6e 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e uently..** If an
242f0 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 y of these routi
24300 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 61 nes are called a
24310 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 fter [sqlite3_re
24320 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 set()] or.** [sq
24330 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
24340 5d 20 6f 72 20 61 66 74 65 72 20 5b 73 71 6c 69 ] or after [sqli
24350 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 te3_step()] has
24360 72 65 74 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 returned.** some
24370 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e thing other than
24380 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 [SQLITE_ROW], t
24390 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 he results are u
243a0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 ndefined..** If
243b0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
243c0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 or [sqlite3_res
243d0 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 et()] or [sqlite
243e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 3_finalize()].**
243f0 20 61 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d are called from
24400 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 a different thr
24410 65 61 64 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 ead while any of
24420 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a these routines.
24430 2a 2a 20 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 ** are pending,
24440 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 then the results
24450 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a are undefined..
24460 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
24470 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 3_column_type()
24480 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
24490 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 the.** [SQLITE_I
244a0 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74 79 70 NTEGER | datatyp
244b0 65 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 e code] for the
244c0 69 6e 69 74 69 61 6c 20 64 61 74 61 20 74 79 70 initial data typ
244d0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 e.** of the resu
244e0 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 lt column. The
244f0 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 returned value i
24500 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 s one of [SQLITE
24510 5f 49 4e 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 _INTEGER],.** [S
24520 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 QLITE_FLOAT], [S
24530 51 4c 49 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 QLITE_TEXT], [SQ
24540 4c 49 54 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b LITE_BLOB], or [
24550 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 SQLITE_NULL]. T
24560 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 he value.** retu
24570 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
24580 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 column_type() is
24590 20 6f 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c only meaningful
245a0 20 69 66 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 if no type.** c
245b0 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 onversions have
245c0 6f 63 63 75 72 72 65 64 20 61 73 20 64 65 73 63 occurred as desc
245d0 72 69 62 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 ribed below. Af
245e0 74 65 72 20 61 20 74 79 70 65 20 63 6f 6e 76 65 ter a type conve
245f0 72 73 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 rsion,.** the va
24600 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
24610 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
24620 79 70 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e ype() is undefin
24630 65 64 2e 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 ed. Future.** v
24640 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
24650 65 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 e may change the
24660 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c behavior of sql
24670 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 ite3_column_type
24680 28 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 ().** following
24690 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f a type conversio
246a0 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 n..**.** If the
246b0 72 65 73 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 result is a BLOB
246c0 20 6f 72 20 55 54 46 2d 38 20 73 74 72 69 6e 67 or UTF-8 string
246d0 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 then the sqlite
246e0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
246f0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 .** routine retu
24700 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
24710 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 f bytes in that
24720 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a BLOB or string..
24730 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 ** If the result
24740 20 69 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 is a UTF-16 str
24750 69 6e 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 ing, then sqlite
24760 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
24770 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 converts.** the
24780 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 string to UTF-8
24790 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e and then return
247a0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
247b0 62 79 74 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 bytes..** If the
247c0 20 72 65 73 75 6c 74 20 69 73 20 61 20 6e 75 6d result is a num
247d0 65 72 69 63 20 76 61 6c 75 65 20 74 68 65 6e 20 eric value then
247e0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
247f0 79 74 65 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b ytes() uses.** [
24800 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
24810 28 29 5d 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 ()] to convert t
24820 68 61 74 20 76 61 6c 75 65 20 74 6f 20 61 20 55 hat value to a U
24830 54 46 2d 38 20 73 74 72 69 6e 67 20 61 6e 64 20 TF-8 string and
24840 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e returns.** the n
24850 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
24860 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a n that string..*
24870 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 * The value retu
24880 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e rned does not in
24890 63 6c 75 64 65 20 74 68 65 20 7a 65 72 6f 20 74 clude the zero t
248a0 65 72 6d 69 6e 61 74 6f 72 20 61 74 20 74 68 65 erminator at the
248b0 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 end.** of the s
248c0 74 72 69 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 tring. For clar
248d0 69 74 79 3a 20 74 68 65 20 76 61 6c 75 65 20 72 ity: the value r
248e0 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6e eturned is the n
248f0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 umber of.** byte
24900 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2c s in the string,
24910 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 not the number
24920 6f 66 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a of characters..*
24930 2a 0a 2a 2a 20 53 74 72 69 6e 67 73 20 72 65 74 *.** Strings ret
24940 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
24950 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 _column_text() a
24960 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nd sqlite3_colum
24970 6e 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65 n_text16(),.** e
24980 76 65 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 ven empty string
24990 73 2c 20 61 72 65 20 61 6c 77 61 79 73 20 7a 65 s, are always ze
249a0 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 ro terminated.
249b0 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 The return.** va
249c0 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 lue from sqlite3
249d0 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 _column_blob() f
249e0 6f 72 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 or a zero-length
249f0 20 42 4c 4f 42 20 69 73 20 61 6e 20 61 72 62 69 BLOB is an arbi
24a00 74 72 61 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 trary.** pointer
24a10 2c 20 70 6f 73 73 69 62 6c 79 20 65 76 65 6e 20 , possibly even
24a20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
24a30 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
24a40 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 3_column_bytes16
24a50 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 () routine is si
24a60 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 milar to sqlite3
24a70 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a _column_bytes().
24a80 2a 2a 20 62 75 74 20 6c 65 61 76 65 73 20 74 68 ** but leaves th
24a90 65 20 72 65 73 75 6c 74 20 69 6e 20 55 54 46 2d e result in UTF-
24aa0 31 36 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 16 in native byt
24ab0 65 20 6f 72 64 65 72 20 69 6e 73 74 65 61 64 20 e order instead
24ac0 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 of UTF-8..** The
24ad0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
24ae0 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 is not included
24af0 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a in this count..
24b00 2a 2a 0a 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74 **.** The object
24b10 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
24b20 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c lite3_column_val
24b30 75 65 28 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b ue()] is an.** [
24b40 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 unprotected sqli
24b50 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
24b60 74 2e 20 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 t. An unprotect
24b70 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
24b80 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f object.** may o
24b90 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68 nly be used with
24ba0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 [sqlite3_bind_v
24bb0 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c alue()] and [sql
24bc0 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 ite3_result_valu
24bd0 65 28 29 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 e()]..** If the
24be0 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c [unprotected sql
24bf0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
24c00 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a ct returned by.*
24c10 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d * [sqlite3_colum
24c20 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 75 73 n_value()] is us
24c30 65 64 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 ed in any other
24c40 77 61 79 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 way, including c
24c50 61 6c 6c 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 alls.** to routi
24c60 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 nes like [sqlite
24c70 33 5f 76 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 3_value_int()],
24c80 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
24c90 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 ext()],.** or [s
24ca0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
24cb0 65 73 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 es()], then the
24cc0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
24cd0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 fined..**.** The
24ce0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 se routines atte
24cf0 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 mpt to convert t
24d00 68 65 20 76 61 6c 75 65 20 77 68 65 72 65 20 61 he value where a
24d10 70 70 72 6f 70 72 69 61 74 65 2e 20 20 46 6f 72 ppropriate. For
24d20 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 .** example, if
24d30 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
24d40 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 46 resentation is F
24d50 4c 4f 41 54 20 61 6e 64 20 61 20 74 65 78 74 20 LOAT and a text
24d60 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71 result.** is req
24d70 75 65 73 74 65 64 2c 20 5b 73 71 6c 69 74 65 33 uested, [sqlite3
24d80 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 69 73 20 _snprintf()] is
24d90 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 used internally
24da0 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a to perform the.*
24db0 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74 * conversion aut
24dc0 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 omatically. The
24dd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 following table
24de0 20 64 65 74 61 69 6c 73 20 74 68 65 20 63 6f 6e details the con
24df0 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 versions.** that
24e00 20 61 72 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a are applied:.**
24e10 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
24e20 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 .** <table borde
24e30 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 r="1">.** <tr><t
24e40 68 3e 20 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 h> Internal<br>T
24e50 79 70 65 20 3c 74 68 3e 20 52 65 71 75 65 73 74 ype <th> Request
24e60 65 64 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 ed<br>Type <th>
24e70 20 43 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a Conversion.**.*
24e80 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c * <tr><td> NULL
24e90 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 <td> INTEGER
24ea0 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 <td> Result i
24eb0 73 20 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 s 0.** <tr><td>
24ec0 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46 NULL <td> F
24ed0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 52 65 73 LOAT <td> Res
24ee0 75 6c 74 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 ult is 0.0.** <t
24ef0 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 r><td> NULL
24f00 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c <td> TEXT <
24f10 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55 td> Result is NU
24f20 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 LL pointer.** <t
24f30 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 r><td> NULL
24f40 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c <td> BLOB <
24f50 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55 td> Result is NU
24f60 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 LL pointer.** <t
24f70 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 r><td> INTEGER
24f80 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c <td> FLOAT <
24f90 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d td> Convert from
24fa0 20 69 6e 74 65 67 65 72 20 74 6f 20 66 6c 6f 61 integer to floa
24fb0 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e t.** <tr><td> IN
24fc0 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 54 45 TEGER <td> TE
24fd0 58 54 20 20 20 20 3c 74 64 3e 20 41 53 43 49 49 XT <td> ASCII
24fe0 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 rendering of th
24ff0 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 e integer.** <tr
25000 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c ><td> INTEGER <
25010 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 td> BLOB <t
25020 64 3e 20 53 61 6d 65 20 61 73 20 49 4e 54 45 47 d> Same as INTEG
25030 45 52 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e ER->TEXT.** <tr>
25040 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 <td> FLOAT <t
25050 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 d> INTEGER <td
25060 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66 > Convert from f
25070 6c 6f 61 74 20 74 6f 20 69 6e 74 65 67 65 72 0a loat to integer.
25080 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f ** <tr><td> FLO
25090 41 54 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 AT <td> TEXT
250a0 20 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 <td> ASCII r
250b0 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 endering of the
250c0 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 float.** <tr><td
250d0 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 > FLOAT <td>
250e0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 BLOB <td> S
250f0 61 6d 65 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45 ame as FLOAT->TE
25100 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 XT.** <tr><td>
25110 54 45 58 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 TEXT <td> INT
25120 45 47 45 52 20 20 20 3c 74 64 3e 20 55 73 65 20 EGER <td> Use
25130 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 atoi().** <tr><t
25140 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e d> TEXT <td>
25150 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 FLOAT <td>
25160 55 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 Use atof().** <t
25170 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 r><td> TEXT
25180 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c <td> BLOB <
25190 74 64 3e 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a td> No change.**
251a0 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 <tr><td> BLOB
251b0 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 <td> INTEGER
251c0 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 <td> Convert t
251d0 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 o TEXT then use
251e0 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 atoi().** <tr><t
251f0 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e d> BLOB <td>
25200 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 FLOAT <td>
25210 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 Convert to TEXT
25220 74 68 65 6e 20 75 73 65 20 61 74 6f 66 28 29 0a then use atof().
25230 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f ** <tr><td> BLO
25240 42 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 B <td> TEXT
25250 20 20 20 20 3c 74 64 3e 20 41 64 64 20 61 20 7a <td> Add a z
25260 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 ero terminator i
25270 66 20 6e 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 f needed.** </ta
25280 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 ble>.** </blockq
25290 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 uote>.**.** The
252a0 74 61 62 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65 table above make
252b0 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 73 s reference to s
252c0 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 tandard C librar
252d0 79 20 66 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69 y functions atoi
252e0 28 29 0a 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29 ().** and atof()
252f0 2e 20 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e . SQLite does n
25300 6f 74 20 72 65 61 6c 6c 79 20 75 73 65 20 74 68 ot really use th
25310 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 ese functions.
25320 49 74 20 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77 It has its.** ow
25330 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 n equivalent int
25340 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20 ernal routines.
25350 20 54 68 65 20 61 74 6f 69 28 29 20 61 6e 64 20 The atoi() and
25360 61 74 6f 66 28 29 20 6e 61 6d 65 73 20 61 72 65 atof() names are
25370 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20 .** used in the
25380 74 61 62 6c 65 20 66 6f 72 20 62 72 65 76 69 74 table for brevit
25390 79 20 61 6e 64 20 62 65 63 61 75 73 65 20 74 68 y and because th
253a0 65 79 20 61 72 65 20 66 61 6d 69 6c 69 61 72 20 ey are familiar
253b0 74 6f 20 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f to most.** C pro
253c0 67 72 61 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 grammers..**.**
253d0 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 74 Note that when t
253e0 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 ype conversions
253f0 6f 63 63 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 occur, pointers
25400 72 65 74 75 72 6e 65 64 20 62 79 20 70 72 69 6f returned by prio
25410 72 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 r.** calls to sq
25420 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f lite3_column_blo
25430 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c b(), sqlite3_col
25440 75 6d 6e 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f umn_text(), and/
25450 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f or.** sqlite3_co
25460 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 6d 61 lumn_text16() ma
25470 79 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 y be invalidated
25480 2e 0a 2a 2a 20 54 79 70 65 20 63 6f 6e 76 65 72 ..** Type conver
25490 73 69 6f 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65 sions and pointe
254a0 72 20 69 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20 r invalidations
254b0 6d 69 67 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69 might occur.** i
254c0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
254d0 63 61 73 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c cases:.**.** <ul
254e0 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e >.** <li> The in
254f0 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 itial content is
25500 20 61 20 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 a BLOB and sqli
25510 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
25520 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c ) or.** sql
25530 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
25540 31 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 16() is called.
25550 20 41 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 A zero-terminat
25560 6f 72 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 or might.**
25570 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64 65 need to be adde
25580 64 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 2e d to the string.
25590 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 </li>.** <li> Th
255a0 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e e initial conten
255b0 74 20 69 73 20 55 54 46 2d 38 20 74 65 78 74 20 t is UTF-8 text
255c0 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 and sqlite3_colu
255d0 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 6f 72 0a mn_bytes16() or.
255e0 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ** sqlite3_
255f0 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 column_text16()
25600 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 is called. The
25610 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 content must be
25620 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 converted.**
25630 20 20 74 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 to UTF-16.</li
25640 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e >.** <li> The in
25650 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 itial content is
25660 20 55 54 46 2d 31 36 20 74 65 78 74 20 61 6e 64 UTF-16 text and
25670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
25680 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20 bytes() or.**
25690 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d sqlite3_colum
256a0 6e 5f 74 65 78 74 28 29 20 69 73 20 63 61 6c 6c n_text() is call
256b0 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 ed. The content
256c0 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 must be convert
256d0 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 ed.** to UT
256e0 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 F-8.</li>.** </u
256f0 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 l>.**.** Convers
25700 69 6f 6e 73 20 62 65 74 77 65 65 6e 20 55 54 46 ions between UTF
25710 2d 31 36 62 65 20 61 6e 64 20 55 54 46 2d 31 36 -16be and UTF-16
25720 6c 65 20 61 72 65 20 61 6c 77 61 79 73 20 64 6f le are always do
25730 6e 65 20 69 6e 20 70 6c 61 63 65 20 61 6e 64 20 ne in place and
25740 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 do.** not invali
25750 64 61 74 65 20 61 20 70 72 69 6f 72 20 70 6f 69 date a prior poi
25760 6e 74 65 72 2c 20 74 68 6f 75 67 68 20 6f 66 20 nter, though of
25770 63 6f 75 72 73 65 20 74 68 65 20 63 6f 6e 74 65 course the conte
25780 6e 74 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 nt of the buffer
25790 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 69 .** that the pri
257a0 6f 72 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 or pointer point
257b0 73 20 74 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 s to will have b
257c0 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f een modified. O
257d0 74 68 65 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 ther kinds.** of
257e0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 72 65 20 conversion are
257f0 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 77 68 done in place wh
25800 65 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c en it is possibl
25810 65 2c 20 62 75 74 20 73 6f 6d 65 74 69 6d 65 73 e, but sometimes
25820 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 they.** are not
25830 20 70 6f 73 73 69 62 6c 65 20 61 6e 64 20 69 6e possible and in
25840 20 74 68 6f 73 65 20 63 61 73 65 73 20 70 72 69 those cases pri
25850 6f 72 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 or pointers are
25860 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a invalidated..**.
25870 2a 2a 20 54 68 65 20 73 61 66 65 73 74 20 61 6e ** The safest an
25880 64 20 65 61 73 69 65 73 74 20 74 6f 20 72 65 6d d easiest to rem
25890 65 6d 62 65 72 20 70 6f 6c 69 63 79 20 69 73 20 ember policy is
258a0 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 to invoke these
258b0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f routines.** in o
258c0 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
258d0 69 6e 67 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 ing ways:.**.**
258e0 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c <ul>.** <li>sql
258f0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
25900 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 () followed by s
25910 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
25920 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c tes()</li>.** <
25930 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d li>sqlite3_colum
25940 6e 5f 62 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 n_blob() followe
25950 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c d by sqlite3_col
25960 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e umn_bytes()</li>
25970 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 .** <li>sqlite3
25980 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 _column_text16()
25990 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c followed by sql
259a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
259b0 73 31 36 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f s16()</li>.** </
259c0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 ul>.**.** In oth
259d0 65 72 20 77 6f 72 64 73 2c 20 79 6f 75 20 73 68 er words, you sh
259e0 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 ould call sqlite
259f0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 3_column_text(),
25a00 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 .** sqlite3_colu
25a10 6d 6e 5f 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71 mn_blob(), or sq
25a20 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
25a30 74 31 36 28 29 20 66 69 72 73 74 20 74 6f 20 66 t16() first to f
25a40 6f 72 63 65 20 74 68 65 20 72 65 73 75 6c 74 0a orce the result.
25a50 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 65 73 69 ** into the desi
25a60 72 65 64 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e red format, then
25a70 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
25a80 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f column_bytes() o
25a90 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c r.** sqlite3_col
25aa0 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 74 6f umn_bytes16() to
25ab0 20 66 69 6e 64 20 74 68 65 20 73 69 7a 65 20 6f find the size o
25ac0 66 20 74 68 65 20 72 65 73 75 6c 74 2e 20 20 44 f the result. D
25ad0 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a o not mix calls.
25ae0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ** to sqlite3_co
25af0 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 20 73 lumn_text() or s
25b00 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
25b10 6f 62 28 29 20 77 69 74 68 20 63 61 6c 6c 73 20 ob() with calls
25b20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f to.** sqlite3_co
25b30 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 2c 20 lumn_bytes16(),
25b40 61 6e 64 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63 and do not mix c
25b50 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f alls to sqlite3_
25b60 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a column_text16().
25b70 2a 2a 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f ** with calls to
25b80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
25b90 62 79 74 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 bytes()..**.** T
25ba0 68 65 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 he pointers retu
25bb0 72 6e 65 64 20 61 72 65 20 76 61 6c 69 64 20 75 rned are valid u
25bc0 6e 74 69 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 ntil a type conv
25bd0 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 ersion occurs as
25be0 0a 2a 2a 20 64 65 73 63 72 69 62 65 64 20 61 62 .** described ab
25bf0 6f 76 65 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 ove, or until [s
25c00 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f qlite3_step()] o
25c10 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 r [sqlite3_reset
25c20 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 ()] or.** [sqlit
25c30 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 e3_finalize()] i
25c40 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d s called. The m
25c50 65 6d 6f 72 79 20 73 70 61 63 65 20 75 73 65 64 emory space used
25c60 20 74 6f 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 to hold strings
25c70 0a 2a 2a 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 .** and BLOBs is
25c80 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 freed automatic
25c90 61 6c 6c 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 ally. Do <b>not
25ca0 3c 2f 62 3e 20 70 61 73 73 20 74 68 65 20 70 6f </b> pass the po
25cb0 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 0a inters returned.
25cc0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** [sqlite3_colu
25cd0 6d 6e 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c mn_blob()], [sql
25ce0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
25cf0 28 29 5d 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a ()], etc. into.*
25d00 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 * [sqlite3_free(
25d10 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d )]..**.** If a m
25d20 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
25d30 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
25d40 72 69 6e 67 20 74 68 65 20 65 76 61 6c 75 61 74 ring the evaluat
25d50 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 ion of any.** of
25d60 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c these routines,
25d70 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 a default value
25d80 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 is returned. T
25d90 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
25da0 0a 2a 2a 20 69 73 20 65 69 74 68 65 72 20 74 68 .** is either th
25db0 65 20 69 6e 74 65 67 65 72 20 30 2c 20 74 68 65 e integer 0, the
25dc0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
25dd0 6e 75 6d 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 number 0.0, or a
25de0 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 NULL.** pointer
25df0 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 . Subsequent ca
25e00 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
25e10 65 72 72 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 errcode()] will
25e20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 return.** [SQLIT
25e30 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 E_NOMEM]..**.**
25e40 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
25e50 20 5b 48 31 33 38 30 33 5d 20 5b 48 31 33 38 30 [H13803] [H1380
25e60 36 5d 20 5b 48 31 33 38 30 39 5d 20 5b 48 31 33 6] [H13809] [H13
25e70 38 31 32 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48 812] [H13815] [H
25e80 31 33 38 31 38 5d 20 5b 48 31 33 38 32 31 5d 20 13818] [H13821]
25e90 5b 48 31 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 [H13824].** [H13
25ea0 38 32 37 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 827] [H13830].*/
25eb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
25ec0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
25ed0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 column_blob(sqli
25ee0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 te3_stmt*, int i
25ef0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 Col);.SQLITE_API
25f00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c int sqlite3_col
25f10 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 umn_bytes(sqlite
25f20 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
25f30 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
25f40 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
25f50 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 n_bytes16(sqlite
25f60 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
25f70 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 l);.SQLITE_API d
25f80 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f ouble sqlite3_co
25f90 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 lumn_double(sqli
25fa0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 te3_stmt*, int i
25fb0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 Col);.SQLITE_API
25fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c int sqlite3_col
25fd0 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f umn_int(sqlite3_
25fe0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
25ff0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c ;.SQLITE_API sql
26000 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 ite3_int64 sqlit
26010 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 e3_column_int64(
26020 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
26030 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
26040 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 _API const unsig
26050 6e 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ned char *sqlite
26060 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 3_column_text(sq
26070 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
26080 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 iCol);.SQLITE_A
26090 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
260a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
260b0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d xt16(sqlite3_stm
260c0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
260d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
260e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 lite3_column_typ
260f0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
26100 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 int iCol);.SQLI
26110 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 TE_API sqlite3_v
26120 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f alue *sqlite3_co
26130 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 lumn_value(sqlit
26140 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
26150 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ol);../*.** CAPI
26160 33 52 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20 3REF: Destroy A
26170 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 Prepared Stateme
26180 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 nt Object {H1330
26190 30 7d 20 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 0} <S70300><S301
261a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
261b0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
261c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
261d0 6c 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20 led to delete a
261e0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
261f0 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 ent]..** If the
26200 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 65 78 statement was ex
26210 65 63 75 74 65 64 20 73 75 63 63 65 73 73 66 75 ecuted successfu
26220 6c 6c 79 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 lly or not execu
26230 74 65 64 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e ted at all, then
26240 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
26250 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 65 78 returned. If ex
26260 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ecution of the s
26270 74 61 74 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 tatement failed
26280 74 68 65 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f then an.** [erro
26290 72 20 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 r code] or [exte
262a0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d nded error code]
262b0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
262c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
262d0 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 can be called a
262e0 74 20 61 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 t any point duri
262f0 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e ng the execution
26300 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 of the.** [prep
26310 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e ared statement].
26320 20 20 49 66 20 74 68 65 20 76 69 72 74 75 61 6c If the virtual
26330 20 6d 61 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 machine has not
26340 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 .** completed ex
26350 65 63 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 ecution when thi
26360 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
26370 6c 65 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b led, that is lik
26380 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e e.** encounterin
26390 67 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e g an error or an
263a0 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 [sqlite3_interr
263b0 75 70 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d upt | interrupt]
263c0 2e 0a 2a 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 ..** Incomplete
263d0 75 70 64 61 74 65 73 20 6d 61 79 20 62 65 20 72 updates may be r
263e0 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 olled back and t
263f0 72 61 6e 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 ransactions canc
26400 65 6c 65 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 eled,.** dependi
26410 6e 67 20 6f 6e 20 74 68 65 20 63 69 72 63 75 6d ng on the circum
26420 73 74 61 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 stances, and the
26430 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d .** [error code]
26440 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 returned will b
26450 65 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d e [SQLITE_ABORT]
26460 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
26470 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 ents:.** [H11302
26480 5d 20 5b 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 ] [H11304].*/.SQ
26490 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
264a0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 ite3_finalize(sq
264b0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
264c0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
264d0 52 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 REF: Reset A Pre
264e0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
264f0 4f 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 Object {H13330}
26500 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S70300>.**.** T
26510 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 he sqlite3_reset
26520 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 () function is c
26530 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 alled to reset a
26540 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
26550 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 ment].** object
26560 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 back to its init
26570 69 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 ial state, ready
26580 20 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 to be re-execut
26590 65 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 ed..** Any SQL s
265a0 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c tatement variabl
265b0 65 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 es that had valu
265c0 65 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d es bound to them
265d0 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 using.** the [s
265e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
265f0 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f | sqlite3_bind_
26600 2a 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 *() API] retain
26610 74 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a their values..**
26620 20 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c Use [sqlite3_cl
26630 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 ear_bindings()]
26640 74 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e to reset the bin
26650 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 dings..**.** {H1
26660 31 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 1332} The [sqlit
26670 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 e3_reset(S)] int
26680 65 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68 erface resets th
26690 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
266a0 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 ement] S.**
266b0 20 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 back to the
266c0 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 beginning of it
266d0 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a s program..**.**
266e0 20 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 {H11334} If the
266f0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c most recent cal
26700 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 l to [sqlite3_st
26710 65 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a ep(S)] for the.*
26720 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 * [prep
26730 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
26740 53 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 S returned [SQLI
26750 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 TE_ROW] or [SQLI
26760 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 TE_DONE],.**
26770 20 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c or if [sql
26780 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 ite3_step(S)] ha
26790 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 s never before b
267a0 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c een called on S,
267b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
267c0 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 n [sqlite3_reset
267d0 28 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 (S)] returns [SQ
267e0 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK]..**.**
267f0 7b 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 {H11336} If the
26800 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c most recent call
26810 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 to [sqlite3_ste
26820 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a p(S)] for the.**
26830 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
26840 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 red statement] S
26850 20 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 indicated an er
26860 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 ror, then.**
26870 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 [sqlite3_r
26880 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 eset(S)] returns
26890 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
268a0 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a [error code]..**
268b0 0a 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 .** {H11338} The
268c0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
268d0 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f S)] interface do
268e0 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 es not change th
268f0 65 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 e values.**
26900 20 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c of any [sql
26910 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 ite3_bind_blob|b
26920 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 indings] on the
26930 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
26940 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 ent] S..*/.SQLIT
26950 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
26960 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 3_reset(sqlite3_
26970 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f stmt *pStmt);../
26980 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
26990 72 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e reate Or Redefin
269a0 65 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 e SQL Functions
269b0 7b 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 {H16100} <S20200
269c0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
269d0 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f function creatio
269e0 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b n routines}.** K
269f0 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 EYWORDS: {applic
26a00 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 ation-defined SQ
26a10 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b L function}.** K
26a20 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 EYWORDS: {applic
26a30 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 ation-defined SQ
26a40 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a L functions}.**.
26a50 2a 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e ** These two fun
26a60 63 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 ctions (collecti
26a70 76 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 vely known as "f
26a80 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e unction creation
26a90 20 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 routines").** a
26aa0 72 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 re used to add S
26ab0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 QL functions or
26ac0 61 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f aggregates or to
26ad0 20 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 redefine the be
26ae0 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 havior.** of exi
26af0 73 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 sting SQL functi
26b00 6f 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 ons or aggregate
26b10 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 s. The only dif
26b20 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 ference between
26b30 74 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 the.** two is th
26b40 61 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 at the second pa
26b50 72 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d rameter, the nam
26b60 65 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 e of the (scalar
26b70 29 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a ) function or.**
26b80 20 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 aggregate, is e
26b90 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 ncoded in UTF-8
26ba0 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 for sqlite3_crea
26bb0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e te_function() an
26bc0 64 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 d UTF-16.** for
26bd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
26be0 75 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a unction16()..**.
26bf0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 ** The first par
26c00 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 ameter is the [d
26c10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
26c20 6f 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 on] to which the
26c30 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e SQL.** function
26c40 20 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e is to be added.
26c50 20 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 If a single pr
26c60 6f 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 ogram uses more
26c70 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 than one databas
26c80 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e.** connection
26c90 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e internally, then
26ca0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d SQL functions m
26cb0 75 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 ust be added ind
26cc0 69 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 ividually to.**
26cd0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
26ce0 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nnection..**.**
26cf0 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d The second param
26d00 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 eter is the name
26d10 20 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 of the SQL func
26d20 74 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 tion to be creat
26d30 65 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e ed or.** redefin
26d40 65 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 ed. The length
26d50 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c of the name is l
26d60 69 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 imited to 255 by
26d70 74 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f tes, exclusive o
26d80 66 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 f.** the zero-te
26d90 72 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 rminator. Note
26da0 74 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 that the name le
26db0 6e 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e ngth limit is in
26dc0 20 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 bytes, not.** c
26dd0 68 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 haracters. Any
26de0 61 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 attempt to creat
26df0 65 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 e a function wit
26e00 68 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a h a longer name.
26e10 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 ** will result i
26e20 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d n [SQLITE_ERROR]
26e30 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e being returned.
26e40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 .**.** The third
26e50 20 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 parameter (nArg
26e60 29 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 ).** is the numb
26e70 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
26e80 74 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e that the SQL fun
26e90 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 ction or.** aggr
26ea0 65 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 egate takes. If
26eb0 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 this parameter i
26ec0 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e s negative, then
26ed0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
26ee0 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 n or.** aggregat
26ef0 65 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e e may take any n
26f00 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
26f10 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ts..**.** The fo
26f20 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 urth parameter,
26f30 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 eTextRep, specif
26f40 69 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c ies what.** [SQL
26f50 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 ITE_UTF8 | text
26f60 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 encoding] this S
26f70 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 QL function pref
26f80 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 ers for.** its p
26f90 61 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 arameters. Any
26fa0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 SQL function imp
26fb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 lementation shou
26fc0 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f ld be able to wo
26fd0 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 rk.** work with
26fe0 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c UTF-8, UTF-16le,
26ff0 20 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 or UTF-16be. B
27000 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e ut some implemen
27010 74 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a tations may be.*
27020 2a 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 * more efficient
27030 20 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 with one encodi
27040 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e ng than another.
27050 20 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 It is allowed
27060 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c to.** invoke sql
27070 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
27080 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 tion() or sqlite
27090 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
270a0 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a n16() multiple.*
270b0 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 * times with the
270c0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 same function b
270d0 75 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e ut with differen
270e0 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 t values of eTex
270f0 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 tRep..** When mu
27100 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 ltiple implement
27110 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 ations of the sa
27120 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 me function are
27130 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 available, SQLit
27140 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 e.** will pick t
27150 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f he one that invo
27160 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 lves the least a
27170 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f mount of data co
27180 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 nversion..** If
27190 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 there is only a
271a0 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 single implement
271b0 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 ation which does
271c0 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74 not care what t
271d0 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 ext.** encoding
271e0 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 is used, then th
271f0 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e e fourth argumen
27200 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c t should be [SQL
27210 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 ITE_ANY]..**.**
27220 54 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 The fifth parame
27230 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 ter is an arbitr
27240 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 ary pointer. Th
27250 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
27260 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74 of the.** funct
27270 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 ion can gain acc
27280 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e ess to this poin
27290 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ter using [sqlit
272a0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e e3_user_data()].
272b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e .**.** The seven
272c0 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e th, eighth and n
272d0 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c inth parameters,
272e0 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e xFunc, xStep an
272f0 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a d xFinal, are.**
27300 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c pointers to C-l
27310 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e anguage function
27320 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
27330 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
27340 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 n or.** aggregat
27350 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20 e. A scalar SQL
27360 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 function require
27370 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 s an implementat
27380 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63 ion of the xFunc
27390 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c .** callback onl
273a0 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 y, NULL pointers
273b0 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 should be passe
273c0 64 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61 d as the xStep a
273d0 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72 nd xFinal.** par
273e0 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72 ameters. An aggr
273f0 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 egate SQL functi
27400 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 on requires an i
27410 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
27420 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 xStep.** and xF
27430 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68 inal and NULL sh
27440 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66 ould be passed f
27450 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c or xFunc. To del
27460 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a ete an existing.
27470 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 ** SQL function
27480 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 or aggregate, pa
27490 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20 ss NULL for all
274a0 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63 three function c
274b0 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 allbacks..**.**
274c0 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 It is permitted
274d0 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74 to register mult
274e0 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 iple implementat
274f0 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
27500 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 .** functions wi
27510 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
27520 20 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72 but with either
27530 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65 differing numbe
27540 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e rs of.** argumen
27550 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20 ts or differing
27560 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 preferred text e
27570 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74 ncodings. SQLit
27580 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 e will use.** th
27590 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
275a0 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 most closely ma
275b0 74 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e tches the way in
275c0 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 which the.** SQ
275d0 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 L function is us
275e0 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 ed. A function
275f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 implementation w
27600 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 ith a non-negati
27610 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d ve.** nArg param
27620 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 eter is a better
27630 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 match than a fu
27640 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
27650 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 ation with.** a
27660 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 negative nArg.
27670 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 A function where
27680 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 the preferred t
27690 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 ext encoding.**
276a0 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 matches the data
276b0 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 base encoding is
276c0 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 a better.** mat
276d0 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 ch than a functi
276e0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
276f0 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 oding is differe
27700 6e 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 nt. .** A funct
27710 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e ion where the en
27720 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 coding differenc
27730 65 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 e is between UTF
27740 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 16le and UTF16be
27750 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 .** is a closer
27760 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e match than a fun
27770 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 ction where the
27780 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 encoding differe
27790 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 nce is.** betwee
277a0 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 n UTF8 and UTF16
277b0 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e ..**.** Built-in
277c0 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 functions may b
277d0 65 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 e overloaded by
277e0 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d new application-
277f0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
27800 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 s..** The first
27810 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
27820 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 ned function wit
27830 68 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f h a given name o
27840 76 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 verrides all.**
27850 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f built-in functio
27860 6e 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b ns in the same [
27870 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
27880 69 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 ion] with the sa
27890 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 me name..** Subs
278a0 65 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 equent applicati
278b0 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
278c0 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
278d0 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 name only overr
278e0 69 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 ide .** prior ap
278f0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
27900 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 d functions that
27910 20 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 are an exact ma
27920 74 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e tch for the.** n
27930 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 umber of paramet
27940 65 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 ers and preferre
27950 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a d encoding..**.*
27960 2a 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e * An application
27970 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
27980 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 n is permitted t
27990 6f 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 o call other.**
279a0 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
279b0 73 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 s. However, suc
279c0 68 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 h calls must not
279d0 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 .** close the da
279e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
279f0 6e 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f n nor finalize o
27a00 72 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 r reset the prep
27a10 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e ared.** statemen
27a20 74 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 t in which the f
27a30 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 unction is runni
27a40 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ng..**.** Requir
27a50 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 ements:.** [H161
27a60 30 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 03] [H16106] [H1
27a70 36 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 6109] [H16112] [
27a80 48 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d H16118] [H16121]
27a90 20 5b 48 31 36 31 32 34 5d 20 5b 48 31 36 31 32 [H16124] [H1612
27aa0 37 5d 0a 2a 2a 20 5b 48 31 36 31 33 30 5d 20 5b 7].** [H16130] [
27ab0 48 31 36 31 33 33 5d 20 5b 48 31 36 31 33 36 5d H16133] [H16136]
27ac0 20 5b 48 31 36 31 33 39 5d 20 5b 48 31 36 31 34 [H16139] [H1614
27ad0 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 2].*/.SQLITE_API
27ae0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 int sqlite3_cre
27af0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 ate_function(.
27b00 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 sqlite3 *db,. c
27b10 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 onst char *zFunc
27b20 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 tionName,. int
27b30 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 nArg,. int eTex
27b40 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 tRep,. void *pA
27b50 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 pp,. void (*xFu
27b60 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 nc)(sqlite3_cont
27b70 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
27b80 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 _value**),. voi
27b90 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 d (*xStep)(sqlit
27ba0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
27bb0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 sqlite3_value**)
27bc0 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 ,. void (*xFina
27bd0 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 l)(sqlite3_conte
27be0 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 xt*).);.SQLITE_A
27bf0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
27c00 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 reate_function16
27c10 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
27c20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
27c30 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 FunctionName,.
27c40 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 int nArg,. int
27c50 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 eTextRep,. void
27c60 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 *pApp,. void (
27c70 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f *xFunc)(sqlite3_
27c80 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
27c90 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 ite3_value**),.
27ca0 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 void (*xStep)(s
27cb0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
27cc0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
27cd0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 e**),. void (*x
27ce0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 Final)(sqlite3_c
27cf0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a ontext*).);../*.
27d00 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 ** CAPI3REF: Tex
27d10 74 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 t Encodings {H10
27d20 32 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 267} <S50200> <H
27d30 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 16100>.**.** The
27d40 73 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 se constant defi
27d50 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 ne integer codes
27d60 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 that represent
27d70 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 the various.** t
27d80 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 ext encodings su
27d90 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 pported by SQLit
27da0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e..*/.#define SQ
27db0 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 LITE_UTF8
27dc0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
27dd0 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 LITE_UTF16LE
27de0 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 2.#define SQ
27df0 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 LITE_UTF16BE
27e00 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 3.#define SQ
27e10 4c 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 LITE_UTF16
27e20 20 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 4 /* Use
27e30 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
27e40 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c r */.#define SQL
27e50 49 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 ITE_ANY
27e60 20 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 5 /* sqlit
27e70 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
27e80 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 on only */.#defi
27e90 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f ne SQLITE_UTF16_
27ea0 41 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a ALIGNED 8 /*
27eb0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
27ec0 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a collation only *
27ed0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 /../*.** CAPI3RE
27ee0 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75 F: Deprecated Fu
27ef0 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 nctions.** DEPRE
27f00 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 CATED.**.** Thes
27f10 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
27f20 5b 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 [deprecated]. I
27f30 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 n order to maint
27f40 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 ain.** backwards
27f50 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 compatibility w
27f60 69 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 ith older code,
27f70 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 these functions
27f80 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 continue .** to
27f90 62 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 be supported. H
27fa0 6f 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c owever, new appl
27fb0 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 ications should
27fc0 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 avoid.** the use
27fd0 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 of these functi
27fe0 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e ons. To help en
27ff0 63 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 courage people t
28000 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 o avoid.** using
28010 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
28020 2c 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 , we are not goi
28030 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 ng to tell you w
28040 68 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a hat they do..*/.
28050 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
28060 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 MIT_DEPRECATED.S
28070 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
28080 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 _DEPRECATED int
28090 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
280a0 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f e_count(sqlite3_
280b0 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 context*);.SQLIT
280c0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
280d0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
280e0 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 te3_expired(sqli
280f0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 te3_stmt*);.SQLI
28100 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 TE_API SQLITE_DE
28110 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c PRECATED int sql
28120 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 ite3_transfer_bi
28130 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 ndings(sqlite3_s
28140 74 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 tmt*, sqlite3_st
28150 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 mt*);.SQLITE_API
28160 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
28170 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 ED int sqlite3_g
28180 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f lobal_recover(vo
28190 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
281a0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
281b0 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 D void sqlite3_t
281c0 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f hread_cleanup(vo
281d0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
281e0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
281f0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 D int sqlite3_me
28200 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 mory_alarm(void(
28210 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 *)(void*,sqlite3
28220 5f 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 _int64,int),void
28230 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 *,sqlite3_int64)
28240 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
28250 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e CAPI3REF: Obtain
28260 69 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e ing SQL Function
28270 20 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 Parameter Value
28280 73 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 s {H15100} <S202
28290 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 00>.**.** The C-
282a0 6c 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 language impleme
282b0 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 ntation of SQL f
282c0 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 unctions and agg
282d0 72 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 regates uses.**
282e0 74 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 this set of inte
282f0 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 rface routines t
28300 6f 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72 o access the par
28310 61 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e ameter values on
28320 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e .** the function
28330 20 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a or aggregate..*
28340 2a 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 *.** The xFunc (
28350 66 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 for scalar funct
28360 69 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 ions) or xStep (
28370 66 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20 for aggregates)
28380 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f parameters.** to
28390 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
283a0 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 _function()] and
283b0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
283c0 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a _function16()].*
283d0 2a 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 * define callbac
283e0 6b 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ks that implemen
283f0 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 t the SQL functi
28400 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 ons and aggregat
28410 65 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 es..** The 4th p
28420 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 arameter to thes
28430 65 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 e callbacks is a
28440 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 n array of point
28450 65 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 ers to.** [prote
28460 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
28470 75 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 ue] objects. Th
28480 65 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 ere is one [sqli
28490 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
284a0 74 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 t for.** each pa
284b0 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 rameter to the S
284c0 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 QL function. Th
284d0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
284e0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 used to.** extr
284f0 61 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 act values from
28500 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c the [sqlite3_val
28510 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a ue] objects..**.
28520 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
28530 73 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 s work only with
28540 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 [protected sqli
28550 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
28560 74 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d ts..** Any attem
28570 70 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 pt to use these
28580 72 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b routines on an [
28590 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 unprotected sqli
285a0 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 te3_value].** ob
285b0 6a 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 ject results in
285c0 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 undefined behavi
285d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 or..**.** These
285e0 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 routines work ju
285f0 73 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 st like the corr
28600 65 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d esponding [colum
28610 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f n access functio
28620 6e 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 ns].** except th
28630 61 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e at these routin
28640 65 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 es take a single
28650 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 [protected sqli
28660 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
28670 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 t.** pointer ins
28680 74 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 tead of a [sqlit
28690 65 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 e3_stmt*] pointe
286a0 72 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 r and an integer
286b0 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a column number..
286c0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
286d0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 3_value_text16()
286e0 20 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 interface extra
286f0 63 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 cts a UTF-16 str
28700 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 ing.** in the na
28710 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 tive byte-order
28720 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 of the host mach
28730 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c ine. The.** sql
28740 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
28750 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 6be() and sqlite
28760 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 3_value_text16le
28770 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a () interfaces.**
28780 20 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 extract UTF-16
28790 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 strings as big-e
287a0 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 ndian and little
287b0 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 -endian respecti
287c0 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 vely..**.** The
287d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 sqlite3_value_nu
287e0 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 meric_type() int
287f0 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 erface attempts
28800 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 to apply.** nume
28810 72 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 ric affinity to
28820 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 the value. This
28830 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 means that an a
28840 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 ttempt is.** mad
28850 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 e to convert the
28860 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 value to an int
28870 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 eger or floating
28880 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 point. If.** s
28890 75 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e uch a conversion
288a0 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 is possible wit
288b0 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 hout loss of inf
288c0 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 ormation (in oth
288d0 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 er.** words, if
288e0 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 the value is a s
288f0 74 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 tring that looks
28900 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a like a number).
28910 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 ** then the conv
28920 65 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 ersion is perfor
28930 6d 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 med. Otherwise
28940 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 no conversion oc
28950 63 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 curs..** The [SQ
28960 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 LITE_INTEGER | d
28970 61 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 atatype] after c
28980 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 onversion is ret
28990 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 urned..**.** Ple
289a0 61 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c ase pay particul
289b0 61 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 ar attention to
289c0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 the fact that th
289d0 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e e pointer return
289e0 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 ed.** from [sqli
289f0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 te3_value_blob()
28a00 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 ], [sqlite3_valu
28a10 65 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a e_text()], or.**
28a20 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
28a30 74 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 text16()] can be
28a40 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 invalidated by
28a50 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c a subsequent cal
28a60 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 l to.** [sqlite3
28a70 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c _value_bytes()],
28a80 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
28a90 62 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c bytes16()], [sql
28aa0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
28ab0 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 )],.** or [sqlit
28ac0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 e3_value_text16(
28ad0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 )]..**.** These
28ae0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 routines must be
28af0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
28b00 20 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a same thread as.
28b10 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 ** the SQL funct
28b20 69 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 ion that supplie
28b30 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 d the [sqlite3_v
28b40 61 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 alue*] parameter
28b50 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 s..**.** Require
28b60 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 35 31 30 ments:.** [H1510
28b70 33 5d 20 5b 48 31 35 31 30 36 5d 20 5b 48 31 35 3] [H15106] [H15
28b80 31 30 39 5d 20 5b 48 31 35 31 31 32 5d 20 5b 48 109] [H15112] [H
28b90 31 35 31 31 35 5d 20 5b 48 31 35 31 31 38 5d 20 15115] [H15118]
28ba0 5b 48 31 35 31 32 31 5d 20 5b 48 31 35 31 32 34 [H15121] [H15124
28bb0 5d 0a 2a 2a 20 5b 48 31 35 31 32 37 5d 20 5b 48 ].** [H15127] [H
28bc0 31 35 31 33 30 5d 20 5b 48 31 35 31 33 33 5d 20 15130] [H15133]
28bd0 5b 48 31 35 31 33 36 5d 0a 2a 2f 0a 53 51 4c 49 [H15136].*/.SQLI
28be0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
28bf0 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 d *sqlite3_value
28c00 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 _blob(sqlite3_va
28c10 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
28c20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
28c30 6c 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 lue_bytes(sqlite
28c40 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
28c50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
28c60 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 3_value_bytes16(
28c70 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
28c80 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
28c90 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le sqlite3_value
28ca0 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
28cb0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
28cc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
28cd0 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 value_int(sqlite
28ce0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
28cf0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e E_API sqlite3_in
28d00 74 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 t64 sqlite3_valu
28d10 65 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f e_int64(sqlite3_
28d20 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
28d30 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e API const unsign
28d40 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 ed char *sqlite3
28d50 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 _value_text(sqli
28d60 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
28d70 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
28d80 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 id *sqlite3_valu
28d90 65 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 e_text16(sqlite3
28da0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
28db0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
28dc0 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 *sqlite3_value_t
28dd0 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f ext16le(sqlite3_
28de0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
28df0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
28e00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
28e10 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 xt16be(sqlite3_v
28e20 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
28e30 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 PI int sqlite3_v
28e40 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 alue_type(sqlite
28e50 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
28e60 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
28e70 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 3_value_numeric_
28e80 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c type(sqlite3_val
28e90 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ue*);../*.** CAP
28ea0 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 20 41 67 I3REF: Obtain Ag
28eb0 67 72 65 67 61 74 65 20 46 75 6e 63 74 69 6f 6e gregate Function
28ec0 20 43 6f 6e 74 65 78 74 20 7b 48 31 36 32 31 30 Context {H16210
28ed0 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a } <S20200>.**.**
28ee0 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 The implementat
28ef0 69 6f 6e 20 6f 66 20 61 67 67 72 65 67 61 74 65 ion of aggregate
28f00 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 75 SQL functions u
28f10 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 se this routine
28f20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 to allocate.** a
28f30 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 structure for s
28f40 74 6f 72 69 6e 67 20 74 68 65 69 72 20 73 74 61 toring their sta
28f50 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 te..**.** The fi
28f60 72 73 74 20 74 69 6d 65 20 74 68 65 20 73 71 6c rst time the sql
28f70 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
28f80 6f 6e 74 65 78 74 28 29 20 72 6f 75 74 69 6e 65 ontext() routine
28f90 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 is called for a
28fa0 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 61 .** particular a
28fb0 67 67 72 65 67 61 74 65 2c 20 53 51 4c 69 74 65 ggregate, SQLite
28fc0 20 61 6c 6c 6f 63 61 74 65 73 20 6e 42 79 74 65 allocates nByte
28fd0 73 20 6f 66 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 s of memory, zer
28fe0 6f 65 73 20 6f 75 74 20 74 68 61 74 0a 2a 2a 20 oes out that.**
28ff0 6d 65 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75 memory, and retu
29000 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
29010 20 69 74 2e 20 4f 6e 20 73 65 63 6f 6e 64 20 61 it. On second a
29020 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 nd subsequent ca
29030 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 lls to.** sqlite
29040 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 3_aggregate_cont
29050 65 78 74 28 29 20 66 6f 72 20 74 68 65 20 73 61 ext() for the sa
29060 6d 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e me aggregate fun
29070 63 74 69 6f 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 ction index,.**
29080 74 68 65 20 73 61 6d 65 20 62 75 66 66 65 72 20 the same buffer
29090 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 is returned. The
290a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
290b0 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 of the aggregate
290c0 20 63 61 6e 20 75 73 65 0a 2a 2a 20 74 68 65 20 can use.** the
290d0 72 65 74 75 72 6e 65 64 20 62 75 66 66 65 72 20 returned buffer
290e0 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 64 61 to accumulate da
290f0 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 ta..**.** SQLite
29100 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66 automatically f
29110 72 65 65 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 rees the allocat
29120 65 64 20 62 75 66 66 65 72 20 77 68 65 6e 20 74 ed buffer when t
29130 68 65 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 he aggregate.**
29140 71 75 65 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e query concludes.
29150 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
29160 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c parameter shoul
29170 64 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 d be a copy of t
29180 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 he.** [sqlite3_c
29190 6f 6e 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e ontext | SQL fun
291a0 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74 ction context] t
291b0 68 61 74 20 69 73 20 74 68 65 20 66 69 72 73 74 hat is the first
291c0 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f parameter.** to
291d0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f the callback ro
291e0 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 utine that imple
291f0 6d 65 6e 74 73 20 74 68 65 20 61 67 67 72 65 67 ments the aggreg
29200 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a ate function..**
29210 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
29220 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
29230 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 from the same th
29240 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a read in which.**
29250 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 53 the aggregate S
29260 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 QL function is r
29270 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 unning..**.** Re
29280 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
29290 48 31 36 32 31 31 5d 20 5b 48 31 36 32 31 33 5d H16211] [H16213]
292a0 20 5b 48 31 36 32 31 35 5d 20 5b 48 31 36 32 31 [H16215] [H1621
292b0 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 7].*/.SQLITE_API
292c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 void *sqlite3_a
292d0 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
292e0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
292f0 2a 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a *, int nBytes);.
29300 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
29310 20 55 73 65 72 20 44 61 74 61 20 46 6f 72 20 46 User Data For F
29320 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 32 34 30 unctions {H16240
29330 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a } <S20200>.**.**
29340 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75 73 65 The sqlite3_use
29350 72 5f 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 r_data() interfa
29360 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 ce returns a cop
29370 79 20 6f 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e y of.** the poin
29380 74 65 72 20 74 68 61 74 20 77 61 73 20 74 68 65 ter that was the
29390 20 70 55 73 65 72 44 61 74 61 20 70 61 72 61 6d pUserData param
293a0 65 74 65 72 20 28 74 68 65 20 35 74 68 20 70 61 eter (the 5th pa
293b0 72 61 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 rameter).** of t
293c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 he [sqlite3_crea
293d0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a te_function()].*
293e0 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 * and [sqlite3_c
293f0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 reate_function16
29400 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 ()] routines tha
29410 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 t originally.**
29420 72 65 67 69 73 74 65 72 65 64 20 74 68 65 20 61 registered the a
29430 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e pplication defin
29440 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e ed function. {EN
29450 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f D}.**.** This ro
29460 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 utine must be ca
29470 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 lled from the sa
29480 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 me thread in whi
29490 63 68 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 ch.** the applic
294a0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
294b0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
294c0 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 g..**.** Require
294d0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 34 ments:.** [H1624
294e0 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 3].*/.SQLITE_API
294f0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 void *sqlite3_u
29500 73 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 ser_data(sqlite3
29510 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a _context*);../*.
29520 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 ** CAPI3REF: Dat
29530 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e abase Connection
29540 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b For Functions {
29550 48 31 36 32 35 30 7d 20 3c 53 36 30 36 30 30 3e H16250} <S60600>
29560 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
29570 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 he sqlite3_conte
29580 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 20 69 xt_db_handle() i
29590 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
295a0 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 a copy of.** th
295b0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 e pointer to the
295c0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
295d0 63 74 69 6f 6e 5d 20 28 74 68 65 20 31 73 74 20 ction] (the 1st
295e0 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 parameter).** of
295f0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 the [sqlite3_cr
29600 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d eate_function()]
29610 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 .** and [sqlite3
29620 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
29630 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 16()] routines t
29640 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a hat originally.*
29650 2a 20 72 65 67 69 73 74 65 72 65 64 20 74 68 65 * registered the
29660 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 application def
29670 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a ined function..*
29680 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
29690 73 3a 0a 2a 2a 20 5b 48 31 36 32 35 33 5d 0a 2a s:.** [H16253].*
296a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
296b0 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f ite3 *sqlite3_co
296c0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
296d0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
296e0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
296f0 45 46 3a 20 46 75 6e 63 74 69 6f 6e 20 41 75 78 EF: Function Aux
29700 69 6c 69 61 72 79 20 44 61 74 61 20 7b 48 31 36 iliary Data {H16
29710 32 37 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 270} <S20200>.**
29720 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
29730 67 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 g two functions
29740 6d 61 79 20 62 65 20 75 73 65 64 20 62 79 20 73 may be used by s
29750 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 calar SQL functi
29760 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73 73 6f 63 69 ons to.** associ
29770 61 74 65 20 6d 65 74 61 64 61 74 61 20 77 69 74 ate metadata wit
29780 68 20 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 h argument value
29790 73 2e 20 49 66 20 74 68 65 20 73 61 6d 65 20 76 s. If the same v
297a0 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 74 alue is passed t
297b0 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 69 6e o.** multiple in
297c0 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 vocations of the
297d0 20 73 61 6d 65 20 53 51 4c 20 66 75 6e 63 74 69 same SQL functi
297e0 6f 6e 20 64 75 72 69 6e 67 20 71 75 65 72 79 20 on during query
297f0 65 78 65 63 75 74 69 6f 6e 2c 20 75 6e 64 65 72 execution, under
29800 0a 2a 2a 20 73 6f 6d 65 20 63 69 72 63 75 6d 73 .** some circums
29810 74 61 6e 63 65 73 20 74 68 65 20 61 73 73 6f 63 tances the assoc
29820 69 61 74 65 64 20 6d 65 74 61 64 61 74 61 20 6d iated metadata m
29830 61 79 20 62 65 20 70 72 65 73 65 72 76 65 64 2e ay be preserved.
29840 20 54 68 69 73 20 6d 61 79 0a 2a 2a 20 62 65 20 This may.** be
29850 75 73 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c used, for exampl
29860 65 2c 20 74 6f 20 61 64 64 20 61 20 72 65 67 75 e, to add a regu
29870 6c 61 72 2d 65 78 70 72 65 73 73 69 6f 6e 20 6d lar-expression m
29880 61 74 63 68 69 6e 67 20 73 63 61 6c 61 72 0a 2a atching scalar.*
29890 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 * function. The
298a0 63 6f 6d 70 69 6c 65 64 20 76 65 72 73 69 6f 6e compiled version
298b0 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61 72 20 of the regular
298c0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 74 expression is st
298d0 6f 72 65 64 20 61 73 0a 2a 2a 20 6d 65 74 61 64 ored as.** metad
298e0 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
298f0 69 74 68 20 74 68 65 20 53 51 4c 20 76 61 6c 75 ith the SQL valu
29900 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
29910 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 regular expressi
29920 6f 6e 0a 2a 2a 20 70 61 74 74 65 72 6e 2e 20 20 on.** pattern.
29930 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 The compiled reg
29940 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 ular expression
29950 63 61 6e 20 62 65 20 72 65 75 73 65 64 20 6f 6e can be reused on
29960 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 multiple.** inv
29970 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ocations of the
29980 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 73 6f same function so
29990 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e that the origin
299a0 61 6c 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e al pattern strin
299b0 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6e 65 g.** does not ne
299c0 65 64 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 ed to be recompi
299d0 6c 65 64 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f led on each invo
299e0 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cation..**.** Th
299f0 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 e sqlite3_get_au
29a00 78 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 xdata() interfac
29a10 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
29a20 74 65 72 20 74 6f 20 74 68 65 20 6d 65 74 61 64 ter to the metad
29a30 61 74 61 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 ata.** associate
29a40 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 d by the sqlite3
29a50 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 66 _set_auxdata() f
29a60 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 unction with the
29a70 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a Nth argument.**
29a80 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 61 70 value to the ap
29a90 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
29aa0 64 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 6e d function. If n
29ab0 6f 20 6d 65 74 61 64 61 74 61 20 68 61 73 20 62 o metadata has b
29ac0 65 65 6e 20 65 76 65 72 0a 2a 2a 20 62 65 65 6e een ever.** been
29ad0 20 73 65 74 20 66 6f 72 20 74 68 65 20 4e 74 68 set for the Nth
29ae0 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 argument of the
29af0 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 69 66 function, or if
29b00 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
29b10 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 70 ng.** function p
29b20 61 72 61 6d 65 74 65 72 20 68 61 73 20 63 68 61 arameter has cha
29b30 6e 67 65 64 20 73 69 6e 63 65 20 74 68 65 20 6d nged since the m
29b40 65 74 61 2d 64 61 74 61 20 77 61 73 20 73 65 74 eta-data was set
29b50 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 ,.** then sqlite
29b60 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 3_get_auxdata()
29b70 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 returns a NULL p
29b80 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
29b90 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 e sqlite3_set_au
29ba0 78 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 xdata() interfac
29bb0 65 20 73 61 76 65 73 20 74 68 65 20 6d 65 74 61 e saves the meta
29bc0 64 61 74 61 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 data.** pointed
29bd0 74 6f 20 62 79 20 69 74 73 20 33 72 64 20 70 61 to by its 3rd pa
29be0 72 61 6d 65 74 65 72 20 61 73 20 74 68 65 20 6d rameter as the m
29bf0 65 74 61 64 61 74 61 20 66 6f 72 20 74 68 65 20 etadata for the
29c00 4e 2d 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 N-th.** argument
29c10 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 of the applicat
29c20 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
29c30 74 69 6f 6e 2e 20 20 53 75 62 73 65 71 75 65 6e tion. Subsequen
29c40 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 t.** calls to sq
29c50 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
29c60 61 28 29 20 6d 69 67 68 74 20 72 65 74 75 72 6e a() might return
29c70 20 74 68 69 73 20 64 61 74 61 2c 20 69 66 20 69 this data, if i
29c80 74 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 t has.** not bee
29c90 6e 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 20 n destroyed..**
29ca0 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c If it is not NUL
29cb0 4c 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 L, SQLite will i
29cc0 6e 76 6f 6b 65 20 74 68 65 20 64 65 73 74 72 75 nvoke the destru
29cd0 63 74 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ctor.** function
29ce0 20 67 69 76 65 6e 20 62 79 20 74 68 65 20 34 74 given by the 4t
29cf0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 h parameter to s
29d00 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
29d10 74 61 28 29 20 6f 6e 0a 2a 2a 20 74 68 65 20 6d ta() on.** the m
29d20 65 74 61 64 61 74 61 20 77 68 65 6e 20 74 68 65 etadata when the
29d30 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 66 corresponding f
29d40 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 unction paramete
29d50 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 6f 72 20 r changes.** or
29d60 77 68 65 6e 20 74 68 65 20 53 51 4c 20 73 74 61 when the SQL sta
29d70 74 65 6d 65 6e 74 20 63 6f 6d 70 6c 65 74 65 73 tement completes
29d80 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 , whichever come
29d90 73 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 s first..**.** S
29da0 51 4c 69 74 65 20 69 73 20 66 72 65 65 20 74 6f QLite is free to
29db0 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 call the destru
29dc0 63 74 6f 72 20 61 6e 64 20 64 72 6f 70 20 6d 65 ctor and drop me
29dd0 74 61 64 61 74 61 20 6f 6e 20 61 6e 79 0a 2a 2a tadata on any.**
29de0 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 61 6e parameter of an
29df0 79 20 66 75 6e 63 74 69 6f 6e 20 61 74 20 61 6e y function at an
29e00 79 20 74 69 6d 65 2e 20 20 54 68 65 20 6f 6e 6c y time. The onl
29e10 79 20 67 75 61 72 61 6e 74 65 65 20 69 73 20 74 y guarantee is t
29e20 68 61 74 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 hat.** the destr
29e30 75 63 74 6f 72 20 77 69 6c 6c 20 62 65 20 63 61 uctor will be ca
29e40 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 lled before the
29e50 6d 65 74 61 64 61 74 61 20 69 73 20 64 72 6f 70 metadata is drop
29e60 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 ped..**.** In pr
29e70 61 63 74 69 63 65 2c 20 6d 65 74 61 64 61 74 61 actice, metadata
29e80 20 69 73 20 70 72 65 73 65 72 76 65 64 20 62 65 is preserved be
29e90 74 77 65 65 6e 20 66 75 6e 63 74 69 6f 6e 20 63 tween function c
29ea0 61 6c 6c 73 20 66 6f 72 0a 2a 2a 20 65 78 70 72 alls for.** expr
29eb0 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 essions that are
29ec0 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d constant at com
29ed0 70 69 6c 65 20 74 69 6d 65 2e 20 54 68 69 73 20 pile time. This
29ee0 69 6e 63 6c 75 64 65 73 20 6c 69 74 65 72 61 6c includes literal
29ef0 0a 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 53 .** values and S
29f00 51 4c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a QL variables..**
29f10 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
29f20 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 es must be calle
29f30 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 d from the same
29f40 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a thread in which.
29f50 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 ** the SQL funct
29f60 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a ion is running..
29f70 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
29f80 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 37 32 5d 20 ts:.** [H16272]
29f90 5b 48 31 36 32 37 34 5d 20 5b 48 31 36 32 37 36 [H16274] [H16276
29fa0 5d 20 5b 48 31 36 32 37 37 5d 20 5b 48 31 36 32 ] [H16277] [H162
29fb0 37 38 5d 20 5b 48 31 36 32 37 39 5d 0a 2a 2f 0a 78] [H16279].*/.
29fc0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
29fd0 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 *sqlite3_get_aux
29fe0 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e data(sqlite3_con
29ff0 74 65 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 text*, int N);.S
2a000 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2a010 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
2a020 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ta(sqlite3_conte
2a030 78 74 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64 xt*, int N, void
2a040 2a 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 *, void (*)(void
2a050 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 *));.../*.** CAP
2a060 49 33 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73 I3REF: Constants
2a070 20 44 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61 Defining Specia
2a080 6c 20 44 65 73 74 72 75 63 74 6f 72 20 42 65 68 l Destructor Beh
2a090 61 76 69 6f 72 20 7b 48 31 30 32 38 30 7d 20 3c avior {H10280} <
2a0a0 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30100>.**.** Th
2a0b0 65 73 65 20 61 72 65 20 73 70 65 63 69 61 6c 20 ese are special
2a0c0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 64 values for the d
2a0d0 65 73 74 72 75 63 74 6f 72 20 74 68 61 74 20 69 estructor that i
2a0e0 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 s passed in as t
2a0f0 68 65 0a 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75 he.** final argu
2a100 6d 65 6e 74 20 74 6f 20 72 6f 75 74 69 6e 65 73 ment to routines
2a110 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72 like [sqlite3_r
2a120 65 73 75 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 esult_blob()].
2a130 49 66 20 74 68 65 20 64 65 73 74 72 75 63 74 6f If the destructo
2a140 72 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 r.** argument is
2a150 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 SQLITE_STATIC,
2a160 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
2a170 65 20 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 e content pointe
2a180 72 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a r is constant.**
2a190 20 61 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 and will never
2a1a0 63 68 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73 change. It does
2a1b0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
2a1c0 64 65 73 74 72 6f 79 65 64 2e 20 20 54 68 65 0a destroyed. The.
2a1d0 2a 2a 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 ** SQLITE_TRANSI
2a1e0 45 4e 54 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 ENT value means
2a1f0 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 that the content
2a200 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68 61 will likely cha
2a210 6e 67 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 nge in.** the ne
2a220 61 72 20 66 75 74 75 72 65 20 61 6e 64 20 74 68 ar future and th
2a230 61 74 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 at SQLite should
2a240 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 make its own pr
2a250 69 76 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a 2a ivate copy of.**
2a260 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 the content bef
2a270 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
2a280 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 64 65 66 *.** The typedef
2a290 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f is necessary to
2a2a0 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72 6f work around pro
2a2b0 62 6c 65 6d 73 20 69 6e 20 63 65 72 74 61 69 6e blems in certain
2a2c0 0a 2a 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 .** C++ compiler
2a2d0 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 s. See ticket #
2a2e0 32 31 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 2191..*/.typedef
2a2f0 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 5f void (*sqlite3_
2a300 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 destructor_type)
2a310 28 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 (void*);.#define
2a320 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 20 SQLITE_STATIC
2a330 20 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 ((sqlite3_de
2a340 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 30 29 structor_type)0)
2a350 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
2a360 54 52 41 4e 53 49 45 4e 54 20 20 20 28 28 73 71 TRANSIENT ((sq
2a370 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 lite3_destructor
2a380 5f 74 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a _type)-1)../*.**
2a390 20 43 41 50 49 33 52 45 46 3a 20 53 65 74 74 69 CAPI3REF: Setti
2a3a0 6e 67 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66 ng The Result Of
2a3b0 20 41 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e An SQL Function
2a3c0 20 7b 48 31 36 34 30 30 7d 20 3c 53 32 30 32 30 {H16400} <S2020
2a3d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
2a3e0 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
2a3f0 20 62 79 20 74 68 65 20 78 46 75 6e 63 20 6f 72 by the xFunc or
2a400 20 78 46 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b xFinal callback
2a410 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d s that.** implem
2a420 65 6e 74 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ent SQL function
2a430 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 s and aggregates
2a440 2e 20 20 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 . See.** [sqlit
2a450 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
2a460 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 on()] and [sqlit
2a470 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
2a480 6f 6e 31 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 on16()].** for a
2a490 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
2a4a0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ation..**.** The
2a4b0 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72 se functions wor
2a4c0 6b 20 76 65 72 79 20 6d 75 63 68 20 6c 69 6b 65 k very much like
2a4d0 20 74 68 65 20 5b 70 61 72 61 6d 65 74 65 72 20 the [parameter
2a4e0 62 69 6e 64 69 6e 67 5d 20 66 61 6d 69 6c 79 20 binding] family
2a4f0 6f 66 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 of.** functions
2a500 75 73 65 64 20 74 6f 20 62 69 6e 64 20 76 61 6c used to bind val
2a510 75 65 73 20 74 6f 20 68 6f 73 74 20 70 61 72 61 ues to host para
2a520 6d 65 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 meters in prepar
2a530 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a ed statements..*
2a540 2a 20 52 65 66 65 72 20 74 6f 20 74 68 65 20 5b * Refer to the [
2a550 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 64 SQL parameter] d
2a560 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 ocumentation for
2a570 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
2a580 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 rmation..**.** T
2a590 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2a5a0 74 5f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 t_blob() interfa
2a5b0 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75 ce sets the resu
2a5c0 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 lt from.** an ap
2a5d0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2a5e0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
2a5f0 20 74 68 65 20 42 4c 4f 42 20 77 68 6f 73 65 20 the BLOB whose
2a600 63 6f 6e 74 65 6e 74 20 69 73 20 70 6f 69 6e 74 content is point
2a610 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 ed.** to by the
2a620 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
2a630 20 61 6e 64 20 77 68 69 63 68 20 69 73 20 4e 20 and which is N
2a640 62 79 74 65 73 20 6c 6f 6e 67 20 77 68 65 72 65 bytes long where
2a650 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 69 N is the.** thi
2a660 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a rd parameter..**
2a670 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2a680 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 result_zeroblob(
2a690 29 20 69 6e 74 65 72 66 61 63 65 73 20 73 65 74 ) interfaces set
2a6a0 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a the result of.*
2a6b0 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
2a6c0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
2a6d0 6f 6e 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 on to be a BLOB
2a6e0 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a containing all z
2a6f0 65 72 6f 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64 ero.** bytes and
2a700 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 N bytes in size
2a710 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 , where N is the
2a720 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e value of the 2n
2a730 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a d parameter..**.
2a740 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2a750 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 29 20 69 esult_double() i
2a760 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
2a770 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a e result from.**
2a780 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d an application-
2a790 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2a7a0 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e to be a floatin
2a7b0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 73 70 g point value sp
2a7c0 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 69 74 ecified.** by it
2a7d0 73 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a s 2nd argument..
2a7e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2a7f0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
2a800 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 and sqlite3_res
2a810 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 66 75 ult_error16() fu
2a820 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73 65 nctions.** cause
2a830 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 the implemented
2a840 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f SQL function to
2a850 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 throw an except
2a860 69 6f 6e 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 75 ion..** SQLite u
2a870 73 65 73 20 74 68 65 20 73 74 72 69 6e 67 20 70 ses the string p
2a880 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 ointed to by the
2a890 0a 2a 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 .** 2nd paramete
2a8a0 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 72 65 73 r of sqlite3_res
2a8b0 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f 72 20 73 ult_error() or s
2a8c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2a8d0 72 6f 72 31 36 28 29 0a 2a 2a 20 61 73 20 74 68 ror16().** as th
2a8e0 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 e text of an err
2a8f0 6f 72 20 6d 65 73 73 61 67 65 2e 20 20 53 51 4c or message. SQL
2a900 69 74 65 20 69 6e 74 65 72 70 72 65 74 73 20 74 ite interprets t
2a910 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 he error.** mess
2a920 61 67 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 age string from
2a930 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2a940 72 72 6f 72 28 29 20 61 73 20 55 54 46 2d 38 2e rror() as UTF-8.
2a950 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 SQLite.** inter
2a960 70 72 65 74 73 20 74 68 65 20 73 74 72 69 6e 67 prets the string
2a970 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 from sqlite3_re
2a980 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 61 sult_error16() a
2a990 73 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 s UTF-16 in nati
2a9a0 76 65 0a 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 ve.** byte order
2a9b0 2e 20 20 49 66 20 74 68 65 20 74 68 69 72 64 20 . If the third
2a9c0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
2a9d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2a9e0 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 r().** or sqlite
2a9f0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
2aa00 28 29 20 69 73 20 6e 65 67 61 74 69 76 65 20 74 () is negative t
2aa10 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 hen SQLite takes
2aa20 20 61 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a as the error.**
2aa30 20 6d 65 73 73 61 67 65 20 61 6c 6c 20 74 65 78 message all tex
2aa40 74 20 75 70 20 74 68 72 6f 75 67 68 20 74 68 65 t up through the
2aa50 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 first zero char
2aa60 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 acter..** If the
2aa70 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
2aa80 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 to sqlite3_resu
2aa90 6c 74 5f 65 72 72 6f 72 28 29 20 6f 72 0a 2a 2a lt_error() or.**
2aaa0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2aab0 65 72 72 6f 72 31 36 28 29 20 69 73 20 6e 6f 6e error16() is non
2aac0 2d 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 -negative then S
2aad0 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 61 74 QLite takes that
2aae0 20 6d 61 6e 79 0a 2a 2a 20 62 79 74 65 73 20 28 many.** bytes (
2aaf0 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73 29 20 not characters)
2ab00 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 from the 2nd par
2ab10 61 6d 65 74 65 72 20 61 73 20 74 68 65 20 65 72 ameter as the er
2ab20 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 20 ror message..**
2ab30 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
2ab40 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 lt_error() and s
2ab50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2ab60 72 6f 72 31 36 28 29 0a 2a 2a 20 72 6f 75 74 69 ror16().** routi
2ab70 6e 65 73 20 6d 61 6b 65 20 61 20 70 72 69 76 61 nes make a priva
2ab80 74 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 te copy of the e
2ab90 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 rror message tex
2aba0 74 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 79 t before.** they
2abb0 20 72 65 74 75 72 6e 2e 20 20 48 65 6e 63 65 2c return. Hence,
2abc0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e the calling fun
2abd0 63 74 69 6f 6e 20 63 61 6e 20 64 65 61 6c 6c 6f ction can deallo
2abe0 63 61 74 65 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66 cate or.** modif
2abf0 79 20 74 68 65 20 74 65 78 74 20 61 66 74 65 72 y the text after
2ac00 20 74 68 65 79 20 72 65 74 75 72 6e 20 77 69 74 they return wit
2ac10 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 54 68 hout harm..** Th
2ac20 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2ac30 5f 65 72 72 6f 72 5f 63 6f 64 65 28 29 20 66 75 _error_code() fu
2ac40 6e 63 74 69 6f 6e 20 63 68 61 6e 67 65 73 20 74 nction changes t
2ac50 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a he error code.**
2ac60 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c returned by SQL
2ac70 69 74 65 20 61 73 20 61 20 72 65 73 75 6c 74 20 ite as a result
2ac80 6f 66 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 61 of an error in a
2ac90 20 66 75 6e 63 74 69 6f 6e 2e 20 20 42 79 20 64 function. By d
2aca0 65 66 61 75 6c 74 2c 0a 2a 2a 20 74 68 65 20 65 efault,.** the e
2acb0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51 4c rror code is SQL
2acc0 49 54 45 5f 45 52 52 4f 52 2e 20 20 41 20 73 75 ITE_ERROR. A su
2acd0 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f bsequent call to
2ace0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2acf0 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71 error().** or sq
2ad00 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2ad10 6f 72 31 36 28 29 20 72 65 73 65 74 73 20 74 68 or16() resets th
2ad20 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 e error code to
2ad30 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a SQLITE_ERROR..**
2ad40 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2ad50 72 65 73 75 6c 74 5f 74 6f 6f 62 69 67 28 29 20 result_toobig()
2ad60 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65 73 interface causes
2ad70 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 SQLite to throw
2ad80 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 an error.** ind
2ad90 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 20 73 icating that a s
2ada0 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69 73 tring or BLOB is
2adb0 20 74 6f 20 6c 6f 6e 67 20 74 6f 20 72 65 70 72 to long to repr
2adc0 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 esent..**.** The
2add0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2ade0 6e 6f 6d 65 6d 28 29 20 69 6e 74 65 72 66 61 63 nomem() interfac
2adf0 65 20 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 e causes SQLite
2ae00 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f to throw an erro
2ae10 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 r.** indicating
2ae20 74 68 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c that a memory al
2ae30 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2e location failed.
2ae40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2ae50 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 29 20 e3_result_int()
2ae60 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 interface sets t
2ae70 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
2ae80 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 ** of the applic
2ae90 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2aea0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 nction to be the
2aeb0 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 32-bit signed i
2aec0 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 nteger.** value
2aed0 67 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 given in the 2nd
2aee0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 argument..** Th
2aef0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2af00 5f 69 6e 74 36 34 28 29 20 69 6e 74 65 72 66 61 _int64() interfa
2af10 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75 ce sets the retu
2af20 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 rn value.** of t
2af30 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
2af40 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
2af50 74 6f 20 62 65 20 74 68 65 20 36 34 2d 62 69 74 to be the 64-bit
2af60 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a signed integer.
2af70 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20 69 ** value given i
2af80 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d 65 n the 2nd argume
2af90 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 nt..**.** The sq
2afa0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
2afb0 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 l() interface se
2afc0 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ts the return va
2afd0 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 lue.** of the ap
2afe0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2aff0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
2b000 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 NULL..**.** The
2b010 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2b020 74 65 78 74 28 29 2c 20 73 71 6c 69 74 65 33 5f text(), sqlite3_
2b030 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 29 2c result_text16(),
2b040 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 .** sqlite3_resu
2b050 6c 74 5f 74 65 78 74 31 36 6c 65 28 29 2c 20 61 lt_text16le(), a
2b060 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c nd sqlite3_resul
2b070 74 5f 74 65 78 74 31 36 62 65 28 29 20 69 6e 74 t_text16be() int
2b080 65 72 66 61 63 65 73 0a 2a 2a 20 73 65 74 20 74 erfaces.** set t
2b090 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
2b0a0 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 of the applicati
2b0b0 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
2b0c0 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 61 20 74 ion to be.** a t
2b0d0 65 78 74 20 73 74 72 69 6e 67 20 77 68 69 63 68 ext string which
2b0e0 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 is represented
2b0f0 61 73 20 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 as UTF-8, UTF-16
2b100 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
2b110 65 72 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 er,.** UTF-16 li
2b120 74 74 6c 65 20 65 6e 64 69 61 6e 2c 20 6f 72 20 ttle endian, or
2b130 55 54 46 2d 31 36 20 62 69 67 20 65 6e 64 69 61 UTF-16 big endia
2b140 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e n, respectively.
2b150 0a 2a 2a 20 53 51 4c 69 74 65 20 74 61 6b 65 73 .** SQLite takes
2b160 20 74 68 65 20 74 65 78 74 20 72 65 73 75 6c 74 the text result
2b170 20 66 72 6f 6d 20 74 68 65 20 61 70 70 6c 69 63 from the applic
2b180 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 ation from.** th
2b190 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 e 2nd parameter
2b1a0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 of the sqlite3_r
2b1b0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 esult_text* inte
2b1c0 72 66 61 63 65 73 2e 0a 2a 2a 20 49 66 20 74 68 rfaces..** If th
2b1d0 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
2b1e0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 to the sqlite3_r
2b1f0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 esult_text* inte
2b200 72 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 65 67 rfaces.** is neg
2b210 61 74 69 76 65 2c 20 74 68 65 6e 20 53 51 4c 69 ative, then SQLi
2b220 74 65 20 74 61 6b 65 73 20 72 65 73 75 6c 74 20 te takes result
2b230 74 65 78 74 20 66 72 6f 6d 20 74 68 65 20 32 6e text from the 2n
2b240 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 d parameter.** t
2b250 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 hrough the first
2b260 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e zero character.
2b270 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 .** If the 3rd p
2b280 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
2b290 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2b2a0 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a ext* interfaces.
2b2b0 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 ** is non-negati
2b2c0 76 65 2c 20 74 68 65 6e 20 61 73 20 6d 61 6e 79 ve, then as many
2b2d0 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 bytes (not char
2b2e0 61 63 74 65 72 73 29 20 6f 66 20 74 68 65 20 74 acters) of the t
2b2f0 65 78 74 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 ext.** pointed t
2b300 6f 20 62 79 20 74 68 65 20 32 6e 64 20 70 61 72 o by the 2nd par
2b310 61 6d 65 74 65 72 20 61 72 65 20 74 61 6b 65 6e ameter are taken
2b320 20 61 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 as the applicat
2b330 69 6f 6e 2d 64 65 66 69 6e 65 64 0a 2a 2a 20 66 ion-defined.** f
2b340 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a unction result..
2b350 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 ** If the 4th pa
2b360 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 rameter to the s
2b370 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2b380 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a xt* interfaces.*
2b390 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 * or sqlite3_res
2b3a0 75 6c 74 5f 62 6c 6f 62 20 69 73 20 61 20 6e 6f ult_blob is a no
2b3b0 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 n-NULL pointer,
2b3c0 74 68 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c then SQLite call
2b3d0 73 20 74 68 61 74 0a 2a 2a 20 66 75 6e 63 74 69 s that.** functi
2b3e0 6f 6e 20 61 73 20 74 68 65 20 64 65 73 74 72 75 on as the destru
2b3f0 63 74 6f 72 20 6f 6e 20 74 68 65 20 74 65 78 74 ctor on the text
2b400 20 6f 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 or BLOB result
2b410 77 68 65 6e 20 69 74 20 68 61 73 0a 2a 2a 20 66 when it has.** f
2b420 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 inished using th
2b430 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 at result..** If
2b440 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 the 4th paramet
2b450 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 er to the sqlite
2b460 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 3_result_text* i
2b470 6e 74 65 72 66 61 63 65 73 20 6f 72 0a 2a 2a 20 nterfaces or.**
2b480 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 sqlite3_result_b
2b490 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65 63 69 lob is the speci
2b4a0 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 al constant SQLI
2b4b0 54 45 5f 53 54 41 54 49 43 2c 20 74 68 65 6e 20 TE_STATIC, then
2b4c0 53 51 4c 69 74 65 0a 2a 2a 20 61 73 73 75 6d 65 SQLite.** assume
2b4d0 73 20 74 68 61 74 20 74 68 65 20 74 65 78 74 20 s that the text
2b4e0 6f 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 69 or BLOB result i
2b4f0 73 20 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73 70 s in constant sp
2b500 61 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 ace and does not
2b510 0a 2a 2a 20 63 6f 70 79 20 74 68 65 20 69 74 20 .** copy the it
2b520 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 or call a destru
2b530 63 74 6f 72 20 77 68 65 6e 20 69 74 20 68 61 73 ctor when it has
2b540 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 finished using
2b550 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 that result..**
2b560 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d If the 4th param
2b570 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 eter to the sqli
2b580 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a te3_result_text*
2b590 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f interfaces.** o
2b5a0 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 r sqlite3_result
2b5b0 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65 _blob is the spe
2b5c0 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 cial constant SQ
2b5d0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 0a 2a LITE_TRANSIENT.*
2b5e0 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 * then SQLite ma
2b5f0 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 kes a copy of th
2b600 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 73 70 e result into sp
2b610 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ace obtained fro
2b620 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 m.** from [sqlit
2b630 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 65 66 e3_malloc()] bef
2b640 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 2e 0a ore it returns..
2b650 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2b660 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 3_result_value()
2b670 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 interface sets
2b680 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
2b690 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2b6a0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
2b6b0 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 20 74 n to be a copy t
2b6c0 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 he.** [unprotect
2b6d0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2b6e0 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69 ] object specifi
2b6f0 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 70 61 ed by the 2nd pa
2b700 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a 2a 2a rameter. The.**
2b710 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2b720 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 63 value() interfac
2b730 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f e makes a copy o
2b740 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 f the [sqlite3_v
2b750 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 61 74 alue].** so that
2b760 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 the [sqlite3_va
2b770 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 20 69 lue] specified i
2b780 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 n the parameter
2b790 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a may change or.**
2b7a0 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 be deallocated
2b7b0 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 72 65 after sqlite3_re
2b7c0 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 65 74 sult_value() ret
2b7d0 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 61 72 urns without har
2b7e0 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 63 74 m..** A [protect
2b7f0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2b800 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 6c 77 ] object may alw
2b810 61 79 73 20 62 65 20 75 73 65 64 20 77 68 65 72 ays be used wher
2b820 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 e an.** [unprote
2b830 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2b840 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 72 65 ue] object is re
2b850 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 68 65 quired, so eithe
2b860 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b 73 71 r.** kind of [sq
2b870 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
2b880 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 ect can be used
2b890 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 72 66 with this interf
2b8a0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ace..**.** If th
2b8b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
2b8c0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
2b8d0 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e hin the differen
2b8e0 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 61 6e t thread.** than
2b8f0 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 69 6e the one contain
2b900 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 ing the applicat
2b910 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2b920 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 69 76 tion that receiv
2b930 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 ed.** the [sqlit
2b940 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f 69 6e e3_context] poin
2b950 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c 74 73 ter, the results
2b960 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a are undefined..
2b970 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
2b980 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 5d 20 ts:.** [H16403]
2b990 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 30 39 [H16406] [H16409
2b9a0 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 36 34 ] [H16412] [H164
2b9b0 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 48 31 15] [H16418] [H1
2b9c0 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d 0a 2a 6421] [H16424].*
2b9d0 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 36 34 * [H16427] [H164
2b9e0 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 48 31 30] [H16433] [H1
2b9f0 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d 20 5b 6436] [H16439] [
2ba00 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 35 5d H16442] [H16445]
2ba10 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 48 31 [H16448].** [H1
2ba20 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d 20 5b 6451] [H16454] [
2ba30 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 30 5d H16457] [H16460]
2ba40 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 51 4c [H16463].*/.SQL
2ba50 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2ba60 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
2ba70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2ba80 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
2ba90 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 int, void(*)(voi
2baa0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
2bab0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2bac0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 sult_double(sqli
2bad0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f te3_context*, do
2bae0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 uble);.SQLITE_AP
2baf0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2bb00 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 esult_error(sqli
2bb10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
2bb20 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b nst char*, int);
2bb30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2bb40 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2bb50 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f error16(sqlite3_
2bb60 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 context*, const
2bb70 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c void*, int);.SQL
2bb80 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2bb90 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2bba0 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 r_toobig(sqlite3
2bbb0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 _context*);.SQLI
2bbc0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2bbd0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2bbe0 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 _nomem(sqlite3_c
2bbf0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
2bc00 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
2bc10 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
2bc20 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ode(sqlite3_cont
2bc30 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ext*, int);.SQLI
2bc40 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2bc50 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 te3_result_int(s
2bc60 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2bc70 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 int);.SQLITE_AP
2bc80 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2bc90 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 esult_int64(sqli
2bca0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 te3_context*, sq
2bcb0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 lite3_int64);.SQ
2bcc0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2bcd0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
2bce0 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 l(sqlite3_contex
2bcf0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 t*);.SQLITE_API
2bd00 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2bd10 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33 ult_text(sqlite3
2bd20 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
2bd30 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69 char*, int, voi
2bd40 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
2bd50 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2bd60 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2bd70 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 t16(sqlite3_cont
2bd80 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ext*, const void
2bd90 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 *, int, void(*)(
2bda0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
2bdb0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2bdc0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 _result_text16le
2bdd0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2bde0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
2bdf0 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 int,void(*)(void
2be00 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
2be10 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2be20 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c ult_text16be(sql
2be30 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 ite3_context*, c
2be40 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c onst void*, int,
2be50 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
2be60 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2be70 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2be80 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f value(sqlite3_co
2be90 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f ntext*, sqlite3_
2bea0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
2beb0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2bec0 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 _result_zeroblob
2bed0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2bee0 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a *, int n);../*.*
2bef0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69 * CAPI3REF: Defi
2bf00 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67 ne New Collating
2bf10 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36 Sequences {H166
2bf20 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 00} <S20300>.**.
2bf30 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f ** These functio
2bf40 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 ns are used to a
2bf50 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e dd new collation
2bf60 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68 sequences to th
2bf70 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
2bf80 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 onnection] speci
2bf90 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73 fied as the firs
2bfa0 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a t argument..**.*
2bfb0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
2bfc0 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 e new collation
2bfd0 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63 sequence is spec
2bfe0 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38 ified as a UTF-8
2bff0 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73 string.** for s
2c000 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2c010 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71 llation() and sq
2c020 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2c030 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61 lation_v2().** a
2c040 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 nd a UTF-16 stri
2c050 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 ng for sqlite3_c
2c060 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 reate_collation1
2c070 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65 6(). In all case
2c080 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73 s.** the name is
2c090 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
2c0a0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 econd function a
2c0b0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
2c0c0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
2c0d0 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 t may be one of
2c0e0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53 the constants [S
2c0f0 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 QLITE_UTF8],.**
2c100 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d [SQLITE_UTF16LE]
2c110 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 , or [SQLITE_UTF
2c120 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 16BE], indicatin
2c130 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d g that the user-
2c140 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 supplied.** rout
2c150 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 ine expects to b
2c160 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 e passed pointer
2c170 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 s to strings enc
2c180 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 oded using UTF-8
2c190 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 ,.** UTF-16 litt
2c1a0 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 le-endian, or UT
2c1b0 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c F-16 big-endian,
2c1c0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 respectively. T
2c1d0 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 he.** third argu
2c1e0 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 ment might also
2c1f0 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 be [SQLITE_UTF16
2c200 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 ] to indicate th
2c210 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 0a 2a at the routine.*
2c220 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 65 * expects pointe
2c230 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20 rs to be UTF-16
2c240 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 6e strings in the n
2c250 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
2c260 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 67 75 , or the.** argu
2c270 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 51 4c ment can be [SQL
2c280 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 ITE_UTF16_ALIGNE
2c290 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 68 65 D] if the.** the
2c2a0 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 routine expects
2c2b0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d pointers to 16-
2c2c0 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64 bit word aligned
2c2d0 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55 strings.** of U
2c2e0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 TF-16 in the nat
2c2f0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0a ive byte order..
2c300 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 **.** A pointer
2c310 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70 70 to the user supp
2c320 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75 73 lied routine mus
2c330 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 t be passed as t
2c340 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67 75 he fifth.** argu
2c350 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73 20 ment. If it is
2c360 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74 68 NULL, this is th
2c370 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74 69 e same as deleti
2c380 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ng the collation
2c390 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73 6f .** sequence (so
2c3a0 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e that SQLite can
2c3b0 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79 6d not call it anym
2c3c0 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74 69 ore)..** Each ti
2c3d0 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 me the applicati
2c3e0 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e 63 on supplied func
2c3f0 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c tion is invoked,
2c400 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a it is passed.**
2c410 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70 61 as its first pa
2c420 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20 6f rameter a copy o
2c430 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73 73 f the void* pass
2c440 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68 ed as the fourth
2c450 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
2c460 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2c470 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73 71 ollation() or sq
2c480 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2c490 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a lation16()..**.*
2c4a0 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 * The remaining
2c4b0 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 arguments to the
2c4c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 application-sup
2c4d0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 72 plied routine ar
2c4e0 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a 2a e two strings,.*
2c4f0 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e 74 * each represent
2c500 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 2c ed by a (length,
2c510 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 20 data) pair and
2c520 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 65 encoded in the e
2c530 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 20 ncoding.** that
2c540 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68 was passed as th
2c550 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
2c560 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74 when the collat
2c570 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 73 ion sequence was
2c580 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e 20 .** registered.
2c590 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c 69 {END} The appli
2c5a0 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 63 cation defined c
2c5b0 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 ollation routine
2c5c0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 should.** retur
2c5d0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f n negative, zero
2c5e0 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 or positive if
2c5f0 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67 the first string
2c600 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a 2a is less than,.*
2c610 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 * equal to, or g
2c620 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
2c630 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20 69 second string. i
2c640 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20 53 .e. (STRING1 - S
2c650 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 54 TRING2)..**.** T
2c660 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 he sqlite3_creat
2c670 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 e_collation_v2()
2c680 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 works like sqli
2c690 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
2c6a0 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74 tion().** except
2c6b0 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61 that it takes a
2c6c0 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74 n extra argument
2c6d0 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73 74 which is a dest
2c6e0 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74 68 ructor for.** th
2c6f0 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54 68 e collation. Th
2c700 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 20 e destructor is
2c710 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 called when the
2c720 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 collation is.**
2c730 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69 73 destroyed and is
2c740 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f passed a copy o
2c750 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 f the fourth par
2c760 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f 69 ameter void* poi
2c770 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 73 nter.** of the s
2c780 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2c790 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a 2a llation_v2()..**
2c7a0 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65 20 Collations are
2c7b0 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 74 destroyed when t
2c7c0 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64 64 hey are overridd
2c7d0 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c 6c en by later call
2c7e0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c s to the.** coll
2c7f0 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 66 ation creation f
2c800 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65 6e unctions or when
2c810 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
2c820 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 6c onnection] is cl
2c830 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 osed.** using [s
2c840 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e qlite3_close()].
2c850 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
2c860 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 [sqlite3_colla
2c870 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d 20 61 tion_needed()] a
2c880 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c nd [sqlite3_coll
2c890 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29 ation_needed16()
2c8a0 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
2c8b0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36 30 ments:.** [H1660
2c8c0 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 31 36 3] [H16604] [H16
2c8d0 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b 48 606] [H16609] [H
2c8e0 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 5d 20 16612] [H16615]
2c8f0 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 32 31 [H16618] [H16621
2c900 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b 48 ].** [H16624] [H
2c910 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 5d 0a 16627] [H16630].
2c920 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
2c930 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
2c940 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 _collation(. sq
2c950 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 lite3*, . const
2c960 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 char *zName, .
2c970 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
2c980 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
2c990 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
2c9a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
2c9b0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
2c9c0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
2c9d0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
2c9e0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a e_collation_v2(.
2c9f0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 sqlite3*, . c
2ca00 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
2ca10 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 , . int eTextRe
2ca20 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 p, . void*,. i
2ca30 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f nt(*xCompare)(vo
2ca40 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2ca50 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f id*,int,const vo
2ca60 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 id*),. void(*xD
2ca70 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29 estroy)(void*).)
2ca80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
2ca90 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2caa0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 collation16(. s
2cab0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 qlite3*, . cons
2cac0 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 t void *zName,.
2cad0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
2cae0 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a void*,. int(*
2caf0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c xCompare)(void*,
2cb00 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
2cb10 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 int,const void*)
2cb20 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
2cb30 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e REF: Collation N
2cb40 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20 eeded Callbacks
2cb50 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30 {H16700} <S20300
2cb60 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 >.**.** To avoid
2cb70 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 having to regis
2cb80 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f ter all collatio
2cb90 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f n sequences befo
2cba0 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a re a database.**
2cbb0 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20 can be used, a
2cbc0 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 single callback
2cbd0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 function may be
2cbe0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 registered with
2cbf0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 the.** [database
2cc00 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 connection] to
2cc10 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 be called whenev
2cc20 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 er an undefined
2cc30 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 collation.** seq
2cc40 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65 uence is require
2cc50 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
2cc60 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69 function is regi
2cc70 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 stered using the
2cc80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 sqlite3_collati
2cc90 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c on_needed() API,
2cca0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70 .** then it is p
2ccb0 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20 assed the names
2ccc0 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c of undefined col
2ccd0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 lation sequences
2cce0 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65 as strings.** e
2ccf0 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e ncoded in UTF-8.
2cd00 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c {H16703} If sql
2cd10 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
2cd20 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65 eeded16() is use
2cd30 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 d,.** the names
2cd40 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54 are passed as UT
2cd50 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 F-16 in machine
2cd60 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
2cd70 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 r..** A call to
2cd80 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 either function
2cd90 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69 replaces any exi
2cda0 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a sting callback..
2cdb0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 **.** When the c
2cdc0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
2cdd0 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72 ed, the first ar
2cde0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73 gument passed is
2cdf0 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68 a copy.** of th
2ce00 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
2ce10 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c t to sqlite3_col
2ce20 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 lation_needed()
2ce30 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f or.** sqlite3_co
2ce40 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 llation_needed16
2ce50 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 (). The second
2ce60 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
2ce70 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
2ce80 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69 ection. The thi
2ce90 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f rd argument is o
2cea0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54 ne of [SQLITE_UT
2ceb0 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46 F8], [SQLITE_UTF
2cec0 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 16BE],.** or [SQ
2ced0 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69 LITE_UTF16LE], i
2cee0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f ndicating the mo
2cef0 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72 st desirable for
2cf00 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 m of the collati
2cf10 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66 on.** sequence f
2cf20 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64 unction required
2cf30 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 . The fourth pa
2cf40 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e rameter is the n
2cf50 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 ame of the.** re
2cf60 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e quired collation
2cf70 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a sequence..**.**
2cf80 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 The callback fu
2cf90 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 nction should re
2cfa0 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72 gister the desir
2cfb0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69 ed collation usi
2cfc0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ng.** [sqlite3_c
2cfd0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 reate_collation(
2cfe0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 )], [sqlite3_cre
2cff0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 ate_collation16(
2d000 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 )], or.** [sqlit
2d010 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2d020 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a ion_v2()]..**.**
2d030 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
2d040 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 36 37 * [H16702] [H167
2d050 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f 0a 04] [H16706].*/.
2d060 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2d070 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
2d080 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74 _needed(. sqlit
2d090 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 0a e3*, . void*, .
2d0a0 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c void(*)(void*,
2d0b0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 sqlite3*,int eTe
2d0c0 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 xtRep,const char
2d0d0 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *).);.SQLITE_API
2d0e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c int sqlite3_col
2d0f0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 lation_needed16(
2d100 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 . sqlite3*, .
2d110 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29 void*,. void(*)
2d120 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c (void*,sqlite3*,
2d130 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e int eTextRep,con
2d140 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a st void*).);../*
2d150 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 20 .** Specify the
2d160 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 79 key for an encry
2d170 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 20 pted database.
2d180 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f This routine sho
2d190 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 uld be.** called
2d1a0 20 72 69 67 68 74 20 61 66 74 65 72 20 73 71 6c right after sql
2d1b0 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a ite3_open()..**.
2d1c0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 ** The code to i
2d1d0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 mplement this AP
2d1e0 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 I is not availab
2d1f0 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 le in the public
2d200 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 release.** of S
2d210 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 QLite..*/.SQLITE
2d220 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2d230 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20 _key(. sqlite3
2d240 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
2d250 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
2d260 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64 se to be rekeyed
2d270 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
2d280 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
2d290 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 /* The key
2d2a0 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 */.);../*.** Cha
2d2b0 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 61 nge the key on a
2d2c0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e n open database.
2d2d0 20 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 If the current
2d2e0 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 database is not
2d2f0 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 74 .** encrypted, t
2d300 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
2d310 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 49 66 encrypt it. If
2d320 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77 pNew==0 or nNew
2d330 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61 ==0, the.** data
2d340 62 61 73 65 20 69 73 20 64 65 63 72 79 70 74 65 base is decrypte
2d350 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 d..**.** The cod
2d360 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 e to implement t
2d370 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61 his API is not a
2d380 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 vailable in the
2d390 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a public release.*
2d3a0 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a * of SQLite..*/.
2d3b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2d3c0 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 20 qlite3_rekey(.
2d3d0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
2d3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2d3f0 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65 * Database to be
2d400 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f rekeyed */. co
2d410 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
2d420 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 int nKey /*
2d430 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29 The new key */.)
2d440 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2d450 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 63 75 F: Suspend Execu
2d460 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74 tion For A Short
2d470 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d 20 3c Time {H10530} <
2d480 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40410>.**.** Th
2d490 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 e sqlite3_sleep(
2d4a0 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65 ) function cause
2d4b0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 s the current th
2d4c0 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20 read to suspend
2d4d0 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f 72 execution.** for
2d4e0 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 6d 62 at least a numb
2d4f0 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e er of millisecon
2d500 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 ds specified in
2d510 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a its parameter..*
2d520 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72 *.** If the oper
2d530 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65 ating system doe
2d540 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 6c s not support sl
2d550 65 65 70 20 72 65 71 75 65 73 74 73 20 77 69 74 eep requests wit
2d560 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 h.** millisecond
2d570 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e time resolution
2d580 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d 65 20 , then the time
2d590 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64 20 will be rounded
2d5a0 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 61 up to.** the nea
2d5b0 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 68 65 rest second. The
2d5c0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 number of milli
2d5d0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
2d5e0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 actually.** req
2d5f0 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 uested from the
2d600 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
2d610 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
2d620 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 .** SQLite imple
2d630 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 65 72 ments this inter
2d640 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 face by calling
2d650 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a 20 the xSleep().**
2d660 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65 method of the de
2d670 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 fault [sqlite3_v
2d680 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a fs] object..**.*
2d690 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 * Requirements:
2d6a0 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 33 36 [H10533] [H10536
2d6b0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2d6c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 int sqlite3_slee
2d6d0 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 p(int);../*.** C
2d6e0 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 API3REF: Name Of
2d6f0 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 The Folder Hold
2d700 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 ing Temporary Fi
2d710 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c 53 32 les {H10310} <S2
2d720 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 0000>.**.** If t
2d730 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 his global varia
2d740 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70 ble is made to p
2d750 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 oint to a string
2d760 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 which is.** the
2d770 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 name of a folde
2d780 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 r (a.k.a. direct
2d790 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 ory), then all t
2d7a0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a emporary files.*
2d7b0 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 51 4c * created by SQL
2d7c0 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 63 ite will be plac
2d7d0 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 65 63 ed in that direc
2d7e0 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 20 76 tory. If this v
2d7f0 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 ariable.** is a
2d800 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 NULL pointer, th
2d810 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72 en SQLite perfor
2d820 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 ms a search for
2d830 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a an appropriate.*
2d840 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 * temporary file
2d850 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a directory..**.*
2d860 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 * It is not safe
2d870 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69 to read or modi
2d880 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 fy this variable
2d890 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e in more than on
2d8a0 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 20 61 e.** thread at a
2d8b0 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 6e 6f time. It is no
2d8c0 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f t safe to read o
2d8d0 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 r modify this va
2d8e0 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20 5b riable.** if a [
2d8f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2d900 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 75 73 ion] is being us
2d910 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ed at the same t
2d920 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 61 74 ime in a separat
2d930 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 20 e.** thread..**
2d940 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 It is intended t
2d950 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c hat this variabl
2d960 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a e be set once.**
2d970 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 as part of proc
2d980 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ess initializati
2d990 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e on and before an
2d9a0 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 y SQLite interfa
2d9b0 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 ce.** routines h
2d9c0 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 ave been called
2d9d0 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 76 61 and that this va
2d9e0 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 75 6e riable remain un
2d9f0 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72 65 changed.** there
2da00 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 after..**.** The
2da10 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 [temp_store_dir
2da20 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 6d ectory pragma] m
2da30 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 ay modify this v
2da40 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75 73 ariable and caus
2da50 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e 74 e.** it to point
2da60 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 to memory obtai
2da70 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 ned from [sqlite
2da80 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 72 74 3_malloc]. Furt
2da90 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65 20 hermore,.** the
2daa0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 [temp_store_dire
2dab0 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61 6c ctory pragma] al
2dac0 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68 61 ways assumes tha
2dad0 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a 20 t any string.**
2dae0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 that this variab
2daf0 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 le points to is
2db00 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f held in memory o
2db10 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a 2a btained from .**
2db20 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
2db30 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d 61 ] and the pragma
2db40 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20 may attempt to
2db50 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72 79 free that memory
2db60 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 .** using [sqlit
2db70 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65 6e e3_free]..** Hen
2db80 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72 69 ce, if this vari
2db90 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65 64 able is modified
2dba0 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68 65 directly, eithe
2dbb0 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a r it should be.*
2dbc0 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20 6d * made NULL or m
2dbd0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ade to point to
2dbe0 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
2dbf0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
2dc00 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73 65 lloc].** or else
2dc10 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 the use of the
2dc20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 [temp_store_dire
2dc30 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73 68 ctory pragma] sh
2dc40 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64 2e ould be avoided.
2dc50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
2dc60 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d har *sqlite3_tem
2dc70 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 2f 2a p_directory;../*
2dc80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 .** CAPI3REF: Te
2dc90 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f 6d 6d st For Auto-Comm
2dca0 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 30 7d it Mode {H12930}
2dcb0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 <S60200>.** KEY
2dcc0 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f 6d 6d WORDS: {autocomm
2dcd0 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 it mode}.**.** T
2dce0 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 he sqlite3_get_a
2dcf0 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e 74 65 utocommit() inte
2dd00 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 6e 6f rface returns no
2dd10 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a 65 72 n-zero or.** zer
2dd20 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 64 o if the given d
2dd30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
2dd40 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f 74 20 on is or is not
2dd50 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f in autocommit mo
2dd60 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 de,.** respectiv
2dd70 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 ely. Autocommit
2dd80 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20 64 mode is on by d
2dd90 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 6f 63 efault..** Autoc
2dda0 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 64 69 ommit mode is di
2ddb0 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 45 47 sabled by a [BEG
2ddc0 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a IN] statement..*
2ddd0 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 * Autocommit mod
2dde0 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 64 20 e is re-enabled
2ddf0 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 by a [COMMIT] or
2de00 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a [ROLLBACK]..**.
2de10 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 6b 69 ** If certain ki
2de20 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 6f 63 nds of errors oc
2de30 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 6d 65 cur on a stateme
2de40 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 6c 74 nt within a mult
2de50 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 i-statement.** t
2de60 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 72 6f ransaction (erro
2de70 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b 53 51 rs including [SQ
2de80 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 51 4c LITE_FULL], [SQL
2de90 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a 20 5b ITE_IOERR],.** [
2dea0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b SQLITE_NOMEM], [
2deb0 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 61 6e SQLITE_BUSY], an
2dec0 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 d [SQLITE_INTERR
2ded0 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 0a 2a UPT]) then the.*
2dee0 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 * transaction mi
2def0 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 ght be rolled ba
2df00 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ck automatically
2df10 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20 . The only way
2df20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 20 77 to.** find out w
2df30 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 61 75 hether SQLite au
2df40 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c tomatically roll
2df50 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 61 6e ed back the tran
2df60 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a 2a 2a saction after.**
2df70 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 6f 20 an error is to
2df80 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f use this functio
2df90 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 n..**.** If anot
2dfa0 68 65 72 20 74 68 72 65 61 64 20 63 68 61 6e 67 her thread chang
2dfb0 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d 6d 69 es the autocommi
2dfc0 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 65 20 t status of the
2dfd0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
2dfe0 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 ection while thi
2dff0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e s routine is run
2e000 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 ning, then the r
2e010 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 eturn value.** i
2e020 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a s undefined..**.
2e030 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2e040 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 39 33 [H12931] [H1293
2e050 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 31 32 2] [H12933] [H12
2e060 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 934].*/.SQLITE_A
2e070 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 PI int sqlite3_g
2e080 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 71 et_autocommit(sq
2e090 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 lite3*);../*.**
2e0a0 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20 54 CAPI3REF: Find T
2e0b0 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e 64 he Database Hand
2e0c0 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65 64 le Of A Prepared
2e0d0 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 31 Statement {H131
2e0e0 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a 20} <S60600>.**.
2e0f0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64 ** The sqlite3_d
2e100 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 61 b_handle interfa
2e110 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b ce returns the [
2e120 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2e130 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 74 ion] handle.** t
2e140 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 61 o which a [prepa
2e150 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62 red statement] b
2e160 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b 64 61 elongs. The [da
2e170 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2e180 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 n].** returned b
2e190 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e y sqlite3_db_han
2e1a0 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 dle is the same
2e1b0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2e1c0 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 74 tion] that was t
2e1d0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
2e1e0 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71 6c t.** to the [sql
2e1f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
2e200 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 73 20 )] call (or its
2e210 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 20 77 variants) that w
2e220 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 72 as used to.** cr
2e230 65 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 eate the stateme
2e240 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 nt in the first
2e250 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 place..**.** Req
2e260 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33 31 uirements: [H131
2e270 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 23].*/.SQLITE_AP
2e280 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 I sqlite3 *sqlit
2e290 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c e3_db_handle(sql
2e2a0 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a ite3_stmt*);../*
2e2b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 .** CAPI3REF: Fi
2e2c0 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 65 70 nd the next prep
2e2d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 7b ared statement {
2e2e0 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 30 3e H13140} <S60600>
2e2f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
2e300 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
2e310 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
2e320 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 73 74 ext [prepared st
2e330 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 0a 2a atement] after.*
2e340 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61 74 * pStmt associat
2e350 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 ed with the [dat
2e360 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2e370 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74 ] pDb. If pStmt
2e380 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e is NULL.** then
2e390 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 this interface
2e3a0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
2e3b0 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 70 r to the first p
2e3c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2e3d0 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 t.** associated
2e3e0 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 with the databas
2e3f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 e connection pDb
2e400 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 72 65 . If no prepare
2e410 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73 d statement.** s
2e420 61 74 69 73 66 69 65 73 20 74 68 65 20 63 6f 6e atisfies the con
2e430 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 ditions of this
2e440 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 74 75 routine, it retu
2e450 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 rns NULL..**.**
2e460 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f The [database co
2e470 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e 74 65 nnection] pointe
2e480 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 74 6f r D in a call to
2e490 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 .** [sqlite3_nex
2e4a0 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d 75 73 t_stmt(D,S)] mus
2e4b0 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 6f 70 t refer to an op
2e4c0 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 en database.** c
2e4d0 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e onnection and in
2e4e0 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 73 74 particular must
2e4f0 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 not be a NULL p
2e500 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 ointer..**.** Re
2e510 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33 quirements: [H13
2e520 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 5b 48 143] [H13146] [H
2e530 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 5d 0a 13149] [H13152].
2e540 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
2e550 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 lite3_stmt *sqli
2e560 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 te3_next_stmt(sq
2e570 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 lite3 *pDb, sqli
2e580 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
2e590 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2e5a0 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 52 6f F: Commit And Ro
2e5b0 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 61 74 llback Notificat
2e5c0 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 ion Callbacks {H
2e5d0 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 3e 0a 12950} <S60400>.
2e5e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2e5f0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 20 3_commit_hook()
2e600 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
2e610 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a ers a callback.*
2e620 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 * function to be
2e630 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 invoked wheneve
2e640 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 r a transaction
2e650 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a is committed..**
2e660 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 Any callback se
2e670 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 t by a previous
2e680 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
2e690 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a commit_hook().**
2e6a0 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 for the same da
2e6b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2e6c0 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e n is overridden.
2e6d0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2e6e0 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 rollback_hook()
2e6f0 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
2e700 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a ers a callback.*
2e710 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 * function to be
2e720 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 invoked wheneve
2e730 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 r a transaction
2e740 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a is committed..**
2e750 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 Any callback se
2e760 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 t by a previous
2e770 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
2e780 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a commit_hook().**
2e790 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 for the same da
2e7a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2e7b0 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e n is overridden.
2e7c0 0a 2a 2a 20 54 68 65 20 70 41 72 67 20 61 72 67 .** The pArg arg
2e7d0 75 6d 65 6e 74 20 69 73 20 70 61 73 73 65 64 20 ument is passed
2e7e0 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 63 through to the c
2e7f0 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 allback..** If t
2e800 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 61 he callback on a
2e810 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 66 75 6e commit hook fun
2e820 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f ction returns no
2e830 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 n-zero,.** then
2e840 74 68 65 20 63 6f 6d 6d 69 74 20 69 73 20 63 6f the commit is co
2e850 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 72 nverted into a r
2e860 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 ollback..**.** I
2e870 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 f another functi
2e880 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c on was previousl
2e890 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 69 74 y registered, it
2e8a0 73 0a 2a 2a 20 70 41 72 67 20 76 61 6c 75 65 20 s.** pArg value
2e8b0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 74 is returned. Ot
2e8c0 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 73 20 herwise NULL is
2e8d0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
2e8e0 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 6d 70 The callback imp
2e8f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 lementation must
2e900 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 not do anything
2e910 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 that will modif
2e920 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 y.** the databas
2e930 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 e connection tha
2e940 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 63 61 t invoked the ca
2e950 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 20 61 63 74 llback. Any act
2e960 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 ions.** to modif
2e970 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 y the database c
2e980 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 62 onnection must b
2e990 65 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c e deferred until
2e9a0 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20 63 6f after the.** co
2e9b0 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68 65 20 mpletion of the
2e9c0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
2e9d0 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 call that trigg
2e9e0 65 72 65 64 20 74 68 65 20 63 6f 6d 6d 69 74 0a ered the commit.
2e9f0 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 68 ** or rollback h
2ea00 6f 6f 6b 20 69 6e 20 74 68 65 20 66 69 72 73 74 ook in the first
2ea10 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20 place..** Note
2ea20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 that [sqlite3_pr
2ea30 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
2ea40 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
2ea50 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 both modify the
2ea60 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 ir.** database c
2ea70 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 onnections for t
2ea80 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d he meaning of "m
2ea90 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 odify" in this p
2eaa0 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 aragraph..**.**
2eab0 52 65 67 69 73 74 65 72 69 6e 67 20 61 20 4e 55 Registering a NU
2eac0 4c 4c 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 LL function disa
2ead0 62 6c 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 bles the callbac
2eae0 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 k..**.** For the
2eaf0 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 purposes of thi
2eb00 73 20 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63 s API, a transac
2eb10 74 69 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20 tion is said to
2eb20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c have been.** rol
2eb30 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 led back if an e
2eb40 78 70 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 xplicit "ROLLBAC
2eb50 4b 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 K" statement is
2eb60 65 78 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 executed, or.**
2eb70 61 6e 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 an error or cons
2eb80 74 72 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e traint causes an
2eb90 20 69 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 implicit rollba
2eba0 63 6b 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 ck to occur..**
2ebb0 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c The rollback cal
2ebc0 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 lback is not inv
2ebd0 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 oked if a transa
2ebe0 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f ction is.** auto
2ebf0 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 matically rolled
2ec00 20 62 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 back because th
2ec10 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
2ec20 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e ction is closed.
2ec30 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b .** The rollback
2ec40 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 callback is not
2ec50 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 invoked if a tr
2ec60 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 ansaction is.**
2ec70 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 rolled back beca
2ec80 75 73 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c use a commit cal
2ec90 6c 62 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e lback returned n
2eca0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 on-zero..** <tod
2ecb0 6f 3e 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 o> Check on this
2ecc0 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 52 </todo>.**.** R
2ecd0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
2ece0 5b 48 31 32 39 35 31 5d 20 5b 48 31 32 39 35 32 [H12951] [H12952
2ecf0 5d 20 5b 48 31 32 39 35 33 5d 20 5b 48 31 32 39 ] [H12953] [H129
2ed00 35 34 5d 20 5b 48 31 32 39 35 35 5d 0a 2a 2a 20 54] [H12955].**
2ed10 5b 48 31 32 39 36 31 5d 20 5b 48 31 32 39 36 32 [H12961] [H12962
2ed20 5d 20 5b 48 31 32 39 36 33 5d 20 5b 48 31 32 39 ] [H12963] [H129
2ed30 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 64].*/.SQLITE_AP
2ed40 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
2ed50 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73 71 6c 69 commit_hook(sqli
2ed60 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 te3*, int(*)(voi
2ed70 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c d*), void*);.SQL
2ed80 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
2ed90 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 lite3_rollback_h
2eda0 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f ook(sqlite3*, vo
2edb0 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 id(*)(void *), v
2edc0 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid*);../*.** CA
2edd0 50 49 33 52 45 46 3a 20 44 61 74 61 20 43 68 61 PI3REF: Data Cha
2ede0 6e 67 65 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e nge Notification
2edf0 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 Callbacks {H129
2ee00 37 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 70} <S60400>.**.
2ee10 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75 ** The sqlite3_u
2ee20 70 64 61 74 65 5f 68 6f 6f 6b 28 29 20 69 6e 74 pdate_hook() int
2ee30 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73 erface registers
2ee40 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 a callback func
2ee50 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 tion.** with the
2ee60 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
2ee70 63 74 69 6f 6e 5d 20 69 64 65 6e 74 69 66 69 65 ction] identifie
2ee80 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 61 d by the first a
2ee90 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 62 65 rgument.** to be
2eea0 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 invoked wheneve
2eeb0 72 20 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 r a row is updat
2eec0 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 ed, inserted or
2eed0 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 deleted..** Any
2eee0 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 callback set by
2eef0 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 a previous call
2ef00 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
2ef10 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 .** for the same
2ef20 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
2ef30 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 tion is overridd
2ef40 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 en..**.** The se
2ef50 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
2ef60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
2ef70 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e e function to in
2ef80 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a 2a 20 72 voke when a.** r
2ef90 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 ow is updated, i
2efa0 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 nserted or delet
2efb0 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 ed..** The first
2efc0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
2efd0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 callback is a c
2efe0 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 64 opy of the third
2eff0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
2f000 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 sqlite3_update_h
2f010 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 ook()..** The se
2f020 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b 20 61 72 cond callback ar
2f030 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 gument is one of
2f040 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 5d [SQLITE_INSERT]
2f050 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 , [SQLITE_DELETE
2f060 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 ],.** or [SQLITE
2f070 5f 55 50 44 41 54 45 5d 2c 20 64 65 70 65 6e 64 _UPDATE], depend
2f080 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 65 72 61 ing on the opera
2f090 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 64 tion that caused
2f0a0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a the callback.**
2f0b0 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 2e 0a to be invoked..
2f0c0 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 ** The third and
2f0d0 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 fourth argument
2f0e0 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 s to the callbac
2f0f0 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65 k contain pointe
2f100 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 rs to the.** dat
2f110 61 62 61 73 65 20 61 6e 64 20 74 61 62 6c 65 20 abase and table
2f120 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 name containing
2f130 74 68 65 20 61 66 66 65 63 74 65 64 20 72 6f 77 the affected row
2f140 2e 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 63 ..** The final c
2f150 61 6c 6c 62 61 63 6b 20 70 61 72 61 6d 65 74 65 allback paramete
2f160 72 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d r is the [rowid]
2f170 20 6f 66 20 74 68 65 20 72 6f 77 2e 0a 2a 2a 20 of the row..**
2f180 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 61 In the case of a
2f190 6e 20 75 70 64 61 74 65 2c 20 74 68 69 73 20 69 n update, this i
2f1a0 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 61 66 s the [rowid] af
2f1b0 74 65 72 20 74 68 65 20 75 70 64 61 74 65 20 74 ter the update t
2f1c0 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a akes place..**.*
2f1d0 2a 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f * The update hoo
2f1e0 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 k is not invoked
2f1f0 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73 when internal s
2f200 79 73 74 65 6d 20 74 61 62 6c 65 73 20 61 72 65 ystem tables are
2f210 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 28 69 2e .** modified (i.
2f220 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 e. sqlite_master
2f230 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 65 71 75 and sqlite_sequ
2f240 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ence)..**.** The
2f250 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d 70 update hook imp
2f260 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 lementation must
2f270 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 not do anything
2f280 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 that will modif
2f290 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 y.** the databas
2f2a0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 e connection tha
2f2b0 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 70 t invoked the up
2f2c0 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 20 date hook. Any
2f2d0 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f actions.** to mo
2f2e0 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 dify the databas
2f2f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 e connection mus
2f300 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e t be deferred un
2f310 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a til after the.**
2f320 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 completion of t
2f330 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 he [sqlite3_step
2f340 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 ()] call that tr
2f350 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64 61 iggered the upda
2f360 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 65 te hook..** Note
2f370 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 that [sqlite3_p
2f380 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 repare_v2()] and
2f390 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
2f3a0 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 ] both modify th
2f3b0 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 eir.** database
2f3c0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 connections for
2f3d0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 the meaning of "
2f3e0 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 modify" in this
2f3f0 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a paragraph..**.**
2f400 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 If another func
2f410 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 tion was previou
2f420 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 sly registered,
2f430 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a 2a its pArg value.*
2f440 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 * is returned.
2f450 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 Otherwise NULL i
2f460 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
2f470 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
2f480 2a 2a 20 5b 48 31 32 39 37 31 5d 20 5b 48 31 32 ** [H12971] [H12
2f490 39 37 33 5d 20 5b 48 31 32 39 37 35 5d 20 5b 48 973] [H12975] [H
2f4a0 31 32 39 37 37 5d 20 5b 48 31 32 39 37 39 5d 20 12977] [H12979]
2f4b0 5b 48 31 32 39 38 31 5d 20 5b 48 31 32 39 38 33 [H12981] [H12983
2f4c0 5d 20 5b 48 31 32 39 38 36 5d 0a 2a 2f 0a 53 51 ] [H12986].*/.SQ
2f4d0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
2f4e0 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f qlite3_update_ho
2f4f0 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 ok(. sqlite3*,
2f500 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 . void(*)(void
2f510 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 *,int ,char cons
2f520 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a t *,char const *
2f530 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c ,sqlite3_int64),
2f540 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a . void*.);../*.
2f550 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 ** CAPI3REF: Ena
2f560 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 53 ble Or Disable S
2f570 68 61 72 65 64 20 50 61 67 65 72 20 43 61 63 68 hared Pager Cach
2f580 65 20 7b 48 31 30 33 33 30 7d 20 3c 53 33 30 39 e {H10330} <S309
2f590 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 00>.** KEYWORDS:
2f5a0 20 7b 73 68 61 72 65 64 20 63 61 63 68 65 7d 20 {shared cache}
2f5b0 7b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f {shared cache mo
2f5c0 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 de}.**.** This r
2f5d0 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f outine enables o
2f5e0 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 73 r disables the s
2f5f0 68 61 72 69 6e 67 20 6f 66 20 74 68 65 20 64 61 haring of the da
2f600 74 61 62 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 tabase cache.**
2f610 61 6e 64 20 73 63 68 65 6d 61 20 64 61 74 61 20 and schema data
2f620 73 74 72 75 63 74 75 72 65 73 20 62 65 74 77 65 structures betwe
2f630 65 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e en [database con
2f640 6e 65 63 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 nection | connec
2f650 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 tions].** to the
2f660 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 same database.
2f670 53 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c Sharing is enabl
2f680 65 64 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 ed if the argume
2f690 6e 74 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e nt is true.** an
2f6a0 64 20 64 69 73 61 62 6c 65 64 20 69 66 20 74 68 d disabled if th
2f6b0 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 e argument is fa
2f6c0 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 lse..**.** Cache
2f6d0 20 73 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 sharing is enab
2f6e0 6c 65 64 20 61 6e 64 20 64 69 73 61 62 6c 65 64 led and disabled
2f6f0 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 70 for an entire p
2f700 72 6f 63 65 73 73 2e 0a 2a 2a 20 54 68 69 73 20 rocess..** This
2f710 69 73 20 61 20 63 68 61 6e 67 65 20 61 73 20 6f is a change as o
2f720 66 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e f SQLite version
2f730 20 33 2e 35 2e 30 2e 20 49 6e 20 70 72 69 6f 72 3.5.0. In prior
2f740 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
2f750 69 74 65 2c 0a 2a 2a 20 73 68 61 72 69 6e 67 20 ite,.** sharing
2f760 77 61 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 was enabled or d
2f770 69 73 61 62 6c 65 64 20 66 6f 72 20 65 61 63 68 isabled for each
2f780 20 74 68 72 65 61 64 20 73 65 70 61 72 61 74 65 thread separate
2f790 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 ly..**.** The ca
2f7a0 63 68 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 che sharing mode
2f7b0 20 73 65 74 20 62 79 20 74 68 69 73 20 69 6e 74 set by this int
2f7c0 65 72 66 61 63 65 20 65 66 66 65 63 74 73 20 61 erface effects a
2f7d0 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a ll subsequent.**
2f7e0 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 calls to [sqlit
2f7f0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c e3_open()], [sql
2f800 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c ite3_open_v2()],
2f810 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70 and [sqlite3_op
2f820 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45 78 69 73 en16()]..** Exis
2f830 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f ting database co
2f840 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e nnections contin
2f850 75 65 20 75 73 65 20 74 68 65 20 73 68 61 72 69 ue use the shari
2f860 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68 61 74 20 ng mode.** that
2f870 77 61 73 20 69 6e 20 65 66 66 65 63 74 20 61 74 was in effect at
2f880 20 74 68 65 20 74 69 6d 65 20 74 68 65 79 20 77 the time they w
2f890 65 72 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 0a 2a ere opened..**.*
2f8a0 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 * Virtual tables
2f8b0 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 cannot be used
2f8c0 77 69 74 68 20 61 20 73 68 61 72 65 64 20 63 61 with a shared ca
2f8d0 63 68 65 2e 20 20 57 68 65 6e 20 73 68 61 72 65 che. When share
2f8e0 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 65 6e d.** cache is en
2f8f0 61 62 6c 65 64 2c 20 74 68 65 20 5b 73 71 6c 69 abled, the [sqli
2f900 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c te3_create_modul
2f910 65 28 29 5d 20 41 50 49 20 75 73 65 64 20 74 6f e()] API used to
2f920 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 76 69 72 register.** vir
2f930 74 75 61 6c 20 74 61 62 6c 65 73 20 77 69 6c 6c tual tables will
2f940 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 61 always return a
2f950 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 n error..**.** T
2f960 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
2f970 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 rns [SQLITE_OK]
2f980 69 66 20 73 68 61 72 65 64 20 63 61 63 68 65 20 if shared cache
2f990 77 61 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 was enabled or d
2f9a0 69 73 61 62 6c 65 64 0a 2a 2a 20 73 75 63 63 65 isabled.** succe
2f9b0 73 73 66 75 6c 6c 79 2e 20 20 41 6e 20 5b 65 72 ssfully. An [er
2f9c0 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
2f9d0 75 72 6e 65 64 20 6f 74 68 65 72 77 69 73 65 2e urned otherwise.
2f9e0 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 20 63 61 .**.** Shared ca
2f9f0 63 68 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 che is disabled
2fa00 62 79 20 64 65 66 61 75 6c 74 2e 20 42 75 74 20 by default. But
2fa10 74 68 69 73 20 6d 69 67 68 74 20 63 68 61 6e 67 this might chang
2fa20 65 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 72 e in.** future r
2fa30 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 eleases of SQLit
2fa40 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 e. Applications
2fa50 20 74 68 61 74 20 63 61 72 65 20 61 62 6f 75 74 that care about
2fa60 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 shared.** cache
2fa70 20 73 65 74 74 69 6e 67 20 73 68 6f 75 6c 64 20 setting should
2fa80 73 65 74 20 69 74 20 65 78 70 6c 69 63 69 74 6c set it explicitl
2fa90 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 y..**.** See Als
2faa0 6f 3a 20 20 5b 53 51 4c 69 74 65 20 53 68 61 72 o: [SQLite Shar
2fab0 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 0a 2a ed-Cache Mode].*
2fac0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2fad0 73 3a 20 5b 48 31 30 33 33 31 5d 20 5b 48 31 30 s: [H10331] [H10
2fae0 33 33 36 5d 20 5b 48 31 30 33 33 37 5d 20 5b 48 336] [H10337] [H
2faf0 31 30 33 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 10339].*/.SQLITE
2fb00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2fb10 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 _enable_shared_c
2fb20 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a ache(int);../*.*
2fb30 2a 20 43 41 50 49 33 52 45 46 3a 20 41 74 74 65 * CAPI3REF: Atte
2fb40 6d 70 74 20 54 6f 20 46 72 65 65 20 48 65 61 70 mpt To Free Heap
2fb50 20 4d 65 6d 6f 72 79 20 7b 48 31 37 33 34 30 7d Memory {H17340}
2fb60 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 <S30220>.**.**
2fb70 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 The sqlite3_rele
2fb80 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 ase_memory() int
2fb90 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 erface attempts
2fba0 74 6f 20 66 72 65 65 20 4e 20 62 79 74 65 73 0a to free N bytes.
2fbb0 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 ** of heap memor
2fbc0 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e y by deallocatin
2fbd0 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 g non-essential
2fbe0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
2fbf0 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 ns.** held by th
2fc00 65 20 64 61 74 61 62 61 73 65 20 6c 69 62 72 61 e database libra
2fc10 72 79 2e 20 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72 ry. {END} Memor
2fc20 79 20 75 73 65 64 20 74 6f 20 63 61 63 68 65 20 y used to cache
2fc30 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 database.** page
2fc40 73 20 74 6f 20 69 6d 70 72 6f 76 65 20 70 65 72 s to improve per
2fc50 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6e 20 65 formance is an e
2fc60 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 xample of non-es
2fc70 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 2e 0a sential memory..
2fc80 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 ** sqlite3_relea
2fc90 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72 65 74 75 se_memory() retu
2fca0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
2fcb0 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 f bytes actually
2fcc0 20 66 72 65 65 64 2c 0a 2a 2a 20 77 68 69 63 68 freed,.** which
2fcd0 20 6d 69 67 68 74 20 62 65 20 6d 6f 72 65 20 6f might be more o
2fce0 72 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 r less than the
2fcf0 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 64 amount requested
2fd00 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
2fd10 65 6e 74 73 3a 20 5b 48 31 37 33 34 31 5d 20 5b ents: [H17341] [
2fd20 48 31 37 33 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 H17342].*/.SQLIT
2fd30 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
2fd40 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
2fd50 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 (int);../*.** CA
2fd60 50 49 33 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 PI3REF: Impose A
2fd70 20 4c 69 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 Limit On Heap S
2fd80 69 7a 65 20 7b 48 31 37 33 35 30 7d 20 3c 53 33 ize {H17350} <S3
2fd90 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0220>.**.** The
2fda0 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 sqlite3_soft_hea
2fdb0 70 5f 6c 69 6d 69 74 28 29 20 69 6e 74 65 72 66 p_limit() interf
2fdc0 61 63 65 20 70 6c 61 63 65 73 20 61 20 22 73 6f ace places a "so
2fdd0 66 74 22 20 6c 69 6d 69 74 0a 2a 2a 20 6f 6e 20 ft" limit.** on
2fde0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 the amount of he
2fdf0 61 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d ap memory that m
2fe00 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 ay be allocated
2fe10 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 49 66 by SQLite..** If
2fe20 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c an internal all
2fe30 6f 63 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65 ocation is reque
2fe40 73 74 65 64 20 74 68 61 74 20 77 6f 75 6c 64 20 sted that would
2fe50 65 78 63 65 65 64 20 74 68 65 0a 2a 2a 20 73 6f exceed the.** so
2fe60 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2c 20 5b ft heap limit, [
2fe70 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
2fe80 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e 76 memory()] is inv
2fe90 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 6d oked one or.** m
2fea0 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 66 72 65 ore times to fre
2feb0 65 20 75 70 20 73 6f 6d 65 20 73 70 61 63 65 20 e up some space
2fec0 62 65 66 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63 before the alloc
2fed0 61 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d ation is perform
2fee0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 ed..**.** The li
2fef0 6d 69 74 20 69 73 20 63 61 6c 6c 65 64 20 22 73 mit is called "s
2ff00 6f 66 74 22 2c 20 62 65 63 61 75 73 65 20 69 66 oft", because if
2ff10 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 [sqlite3_releas
2ff20 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a 2a 20 63 e_memory()].** c
2ff30 61 6e 6e 6f 74 20 66 72 65 65 20 73 75 66 66 69 annot free suffi
2ff40 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20 74 6f 20 cient memory to
2ff50 70 72 65 76 65 6e 74 20 74 68 65 20 6c 69 6d 69 prevent the limi
2ff60 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 65 78 63 t from being exc
2ff70 65 65 64 65 64 2c 0a 2a 2a 20 74 68 65 20 6d 65 eeded,.** the me
2ff80 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 mory is allocate
2ff90 64 20 61 6e 79 77 61 79 20 61 6e 64 20 74 68 65 d anyway and the
2ffa0 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 current operati
2ffb0 6f 6e 20 70 72 6f 63 65 65 64 73 2e 0a 2a 2a 0a on proceeds..**.
2ffc0 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72 ** A negative or
2ffd0 20 7a 65 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 zero value for
2ffe0 4e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 N means that the
2fff0 72 65 20 69 73 20 6e 6f 20 73 6f 66 74 20 68 65 re is no soft he
30000 61 70 20 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 ap limit and.**
30010 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 [sqlite3_release
30020 5f 6d 65 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 _memory()] will
30030 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 only be called w
30040 68 65 6e 20 6d 65 6d 6f 72 79 20 69 73 20 65 78 hen memory is ex
30050 68 61 75 73 74 65 64 2e 0a 2a 2a 20 54 68 65 20 hausted..** The
30060 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f default value fo
30070 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 r the soft heap
30080 6c 69 6d 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a limit is zero..*
30090 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 *.** SQLite make
300a0 73 20 61 20 62 65 73 74 20 65 66 66 6f 72 74 20 s a best effort
300b0 74 6f 20 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66 to honor the sof
300c0 74 20 68 65 61 70 20 6c 69 6d 69 74 2e 0a 2a 2a t heap limit..**
300d0 20 42 75 74 20 69 66 20 74 68 65 20 73 6f 66 74 But if the soft
300e0 20 68 65 61 70 20 6c 69 6d 69 74 20 63 61 6e 6e heap limit cann
300f0 6f 74 20 62 65 20 68 6f 6e 6f 72 65 64 2c 20 65 ot be honored, e
30100 78 65 63 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a xecution will.**
30110 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 6f 75 continue withou
30120 74 20 65 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66 t error or notif
30130 69 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 ication. This i
30140 73 20 77 68 79 20 74 68 65 20 6c 69 6d 69 74 20 s why the limit
30150 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22 is.** called a "
30160 73 6f 66 74 22 20 6c 69 6d 69 74 2e 20 20 49 74 soft" limit. It
30170 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c is advisory onl
30180 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74 y..**.** Prior t
30190 6f 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e o SQLite version
301a0 20 33 2e 35 2e 30 2c 20 74 68 69 73 20 72 6f 75 3.5.0, this rou
301b0 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6e 73 74 72 tine only constr
301c0 61 69 6e 65 64 20 74 68 65 20 6d 65 6d 6f 72 79 ained the memory
301d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 .** allocated by
301e0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 a single thread
301f0 20 2d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 - the same thre
30200 61 64 20 69 6e 20 77 68 69 63 68 20 74 68 69 73 ad in which this
30210 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 75 6e 73 routine.** runs
30220 2e 20 20 42 65 67 69 6e 6e 69 6e 67 20 77 69 74 . Beginning wit
30230 68 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e h SQLite version
30240 20 33 2e 35 2e 30 2c 20 74 68 65 20 73 6f 66 74 3.5.0, the soft
30250 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 0a 2a heap limit is.*
30260 2a 20 61 70 70 6c 69 65 64 20 74 6f 20 61 6c 6c * applied to all
30270 20 74 68 72 65 61 64 73 2e 20 54 68 65 20 76 61 threads. The va
30280 6c 75 65 20 73 70 65 63 69 66 69 65 64 20 66 6f lue specified fo
30290 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 r the soft heap
302a0 6c 69 6d 69 74 0a 2a 2a 20 69 73 20 61 6e 20 75 limit.** is an u
302b0 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 pper bound on th
302c0 65 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 e total memory a
302d0 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 6c llocation for al
302e0 6c 20 74 68 72 65 61 64 73 2e 20 49 6e 0a 2a 2a l threads. In.**
302f0 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20 74 version 3.5.0 t
30300 68 65 72 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 here is no mecha
30310 6e 69 73 6d 20 66 6f 72 20 6c 69 6d 69 74 69 6e nism for limitin
30320 67 20 74 68 65 20 68 65 61 70 20 75 73 61 67 65 g the heap usage
30330 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 for.** individu
30340 61 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a al threads..**.*
30350 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
30360 2a 2a 20 5b 48 31 36 33 35 31 5d 20 5b 48 31 36 ** [H16351] [H16
30370 33 35 32 5d 20 5b 48 31 36 33 35 33 5d 20 5b 48 352] [H16353] [H
30380 31 36 33 35 34 5d 20 5b 48 31 36 33 35 35 5d 20 16354] [H16355]
30390 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a 53 51 4c 49 [H16358].*/.SQLI
303a0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
303b0 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 te3_soft_heap_li
303c0 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a mit(int);../*.**
303d0 20 43 41 50 49 33 52 45 46 3a 20 45 78 74 72 61 CAPI3REF: Extra
303e0 63 74 20 4d 65 74 61 64 61 74 61 20 41 62 6f 75 ct Metadata Abou
303f0 74 20 41 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20 t A Column Of A
30400 54 61 62 6c 65 20 7b 48 31 32 38 35 30 7d 20 3c Table {H12850} <
30410 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S60300>.**.** Th
30420 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
30430 6e 73 20 6d 65 74 61 64 61 74 61 20 61 62 6f 75 ns metadata abou
30440 74 20 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c t a specific col
30450 75 6d 6e 20 6f 66 20 61 20 73 70 65 63 69 66 69 umn of a specifi
30460 63 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 61 c.** database ta
30470 62 6c 65 20 61 63 63 65 73 73 69 62 6c 65 20 75 ble accessible u
30480 73 69 6e 67 20 74 68 65 20 5b 64 61 74 61 62 61 sing the [databa
30490 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 se connection] h
304a0 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20 andle.** passed
304b0 61 73 20 74 68 65 20 66 69 72 73 74 20 66 75 6e as the first fun
304c0 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a ction argument..
304d0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e **.** The column
304e0 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 is identified b
304f0 79 20 74 68 65 20 73 65 63 6f 6e 64 2c 20 74 68 y the second, th
30500 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 ird and fourth p
30510 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 arameters to.**
30520 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 this function. T
30530 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
30540 74 65 72 20 69 73 20 65 69 74 68 65 72 20 74 68 ter is either th
30550 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 e name of the da
30560 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e 65 2e 20 tabase.** (i.e.
30570 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f "main", "temp" o
30580 72 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 r an attached da
30590 74 61 62 61 73 65 29 20 63 6f 6e 74 61 69 6e 69 tabase) containi
305a0 6e 67 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ng the specified
305b0 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c .** table or NUL
305c0 4c 2e 20 49 66 20 69 74 20 69 73 20 4e 55 4c 4c L. If it is NULL
305d0 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74 74 61 63 , then all attac
305e0 68 65 64 20 64 61 74 61 62 61 73 65 73 20 61 72 hed databases ar
305f0 65 20 73 65 61 72 63 68 65 64 0a 2a 2a 20 66 6f e searched.** fo
30600 72 20 74 68 65 20 74 61 62 6c 65 20 75 73 69 6e r the table usin
30610 67 20 74 68 65 20 73 61 6d 65 20 61 6c 67 6f 72 g the same algor
30620 69 74 68 6d 20 75 73 65 64 20 62 79 20 74 68 65 ithm used by the
30630 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 database engine
30640 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 75 to.** resolve u
30650 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 nqualified table
30660 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a references..**.
30670 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 ** The third and
30680 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 fourth paramete
30690 72 73 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 rs to this funct
306a0 69 6f 6e 20 61 72 65 20 74 68 65 20 74 61 62 6c ion are the tabl
306b0 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 e and column.**
306c0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 65 73 69 name of the desi
306d0 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72 65 73 70 red column, resp
306e0 65 63 74 69 76 65 6c 79 2e 20 4e 65 69 74 68 65 ectively. Neithe
306f0 72 20 6f 66 20 74 68 65 73 65 20 70 61 72 61 6d r of these param
30700 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20 62 65 20 eters.** may be
30710 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4d 65 74 61 NULL..**.** Meta
30720 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 data is returned
30730 20 62 79 20 77 72 69 74 69 6e 67 20 74 6f 20 74 by writing to t
30740 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 he memory locati
30750 6f 6e 73 20 70 61 73 73 65 64 20 61 73 20 74 68 ons passed as th
30760 65 20 35 74 68 0a 2a 2a 20 61 6e 64 20 73 75 62 e 5th.** and sub
30770 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65 sequent paramete
30780 72 73 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 rs to this funct
30790 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74 68 65 73 ion. Any of thes
307a0 65 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 e arguments may
307b0 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 69 6e 20 77 be.** NULL, in w
307c0 68 69 63 68 20 63 61 73 65 20 74 68 65 20 63 6f hich case the co
307d0 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 6c 65 6d rresponding elem
307e0 65 6e 74 20 6f 66 20 6d 65 74 61 64 61 74 61 20 ent of metadata
307f0 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a is omitted..**.*
30800 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a * <blockquote>.*
30810 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d * <table border=
30820 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e "1">.** <tr><th>
30830 20 50 61 72 61 6d 65 74 65 72 20 3c 74 68 3e 20 Parameter <th>
30840 4f 75 74 70 75 74 3c 62 72 3e 54 79 70 65 20 3c Output<br>Type <
30850 74 68 3e 20 20 44 65 73 63 72 69 70 74 69 6f 6e th> Description
30860 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 .**.** <tr><td>
30870 35 74 68 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 5th <td> const c
30880 68 61 72 2a 20 3c 74 64 3e 20 44 61 74 61 20 74 har* <td> Data t
30890 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 ype.** <tr><td>
308a0 36 74 68 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 6th <td> const c
308b0 68 61 72 2a 20 3c 74 64 3e 20 4e 61 6d 65 20 6f har* <td> Name o
308c0 66 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 f default collat
308d0 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 ion sequence.**
308e0 3c 74 72 3e 3c 74 64 3e 20 37 74 68 20 3c 74 64 <tr><td> 7th <td
308f0 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20 3c 74 > int <t
30900 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d d> True if colum
30910 6e 20 68 61 73 20 61 20 4e 4f 54 20 4e 55 4c 4c n has a NOT NULL
30920 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 3c constraint.** <
30930 74 72 3e 3c 74 64 3e 20 38 74 68 20 3c 74 64 3e tr><td> 8th <td>
30940 20 69 6e 74 20 20 20 20 20 20 20 20 20 3c 74 64 int <td
30950 3e 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e > True if column
30960 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 is part of the
30970 50 52 49 4d 41 52 59 20 4b 45 59 0a 2a 2a 20 3c PRIMARY KEY.** <
30980 74 72 3e 3c 74 64 3e 20 39 74 68 20 3c 74 64 3e tr><td> 9th <td>
30990 20 69 6e 74 20 20 20 20 20 20 20 20 20 3c 74 64 int <td
309a0 3e 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e > True if column
309b0 20 69 73 20 5b 41 55 54 4f 49 4e 43 52 45 4d 45 is [AUTOINCREME
309c0 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a NT].** </table>.
309d0 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e ** </blockquote>
309e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 .**.** The memor
309f0 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 y pointed to by
30a00 74 68 65 20 63 68 61 72 61 63 74 65 72 20 70 6f the character po
30a10 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 inters returned
30a20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65 63 6c 61 for the.** decla
30a30 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 ration type and
30a40 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
30a50 63 65 20 69 73 20 76 61 6c 69 64 20 6f 6e 6c 79 ce is valid only
30a60 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a until the next.
30a70 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 53 ** call to any S
30a80 51 4c 69 74 65 20 41 50 49 20 66 75 6e 63 74 69 QLite API functi
30a90 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 on..**.** If the
30aa0 20 73 70 65 63 69 66 69 65 64 20 74 61 62 6c 65 specified table
30ab0 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 76 is actually a v
30ac0 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 iew, an [error c
30ad0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ode] is returned
30ae0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 ..**.** If the s
30af0 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 pecified column
30b00 69 73 20 22 72 6f 77 69 64 22 2c 20 22 6f 69 64 is "rowid", "oid
30b10 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20 61 " or "_rowid_" a
30b20 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e 54 45 47 45 nd an.** [INTEGE
30b30 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 R PRIMARY KEY] c
30b40 6f 6c 75 6d 6e 20 68 61 73 20 62 65 65 6e 20 65 olumn has been e
30b50 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 xplicitly declar
30b60 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74 ed, then the out
30b70 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 put.** parameter
30b80 73 20 61 72 65 20 73 65 74 20 66 6f 72 20 74 68 s are set for th
30b90 65 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 e explicitly dec
30ba0 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66 lared column. If
30bb0 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 there is no.**
30bc0 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 explicitly decla
30bd0 72 65 64 20 5b 49 4e 54 45 47 45 52 20 50 52 49 red [INTEGER PRI
30be0 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e MARY KEY] column
30bf0 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 , then the outpu
30c00 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 t.** parameters
30c10 61 72 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f are set as follo
30c20 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a ws:.**.** <pre>.
30c30 2a 2a 20 20 20 20 20 64 61 74 61 20 74 79 70 65 ** data type
30c40 3a 20 22 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 : "INTEGER".**
30c50 20 20 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 collation seq
30c60 75 65 6e 63 65 3a 20 22 42 49 4e 41 52 59 22 0a uence: "BINARY".
30c70 2a 2a 20 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a ** not null:
30c80 20 30 0a 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 0.** primar
30c90 79 20 6b 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 y key: 1.**
30ca0 61 75 74 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 auto increment:
30cb0 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 0.** </pre>.**.*
30cc0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
30cd0 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 may load one or
30ce0 6d 6f 72 65 20 73 63 68 65 6d 61 73 20 66 72 6f more schemas fro
30cf0 6d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 m database files
30d00 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 . If an.** error
30d10 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 occurs during t
30d20 68 69 73 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 his process, or
30d30 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 if the requested
30d40 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e table or column
30d50 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f .** cannot be fo
30d60 75 6e 64 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 und, an [error c
30d70 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ode] is returned
30d80 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 and an error me
30d90 73 73 61 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e ssage left.** in
30da0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
30db0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 onnection] (to b
30dc0 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69 6e e retrieved usin
30dd0 67 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 g sqlite3_errmsg
30de0 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ())..**.** This
30df0 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 API is only avai
30e00 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 lable if the lib
30e10 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 rary was compile
30e20 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 d with the.** [S
30e30 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
30e40 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d UMN_METADATA] C-
30e50 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d preprocessor sym
30e60 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a bol defined..*/.
30e70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
30e80 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c qlite3_table_col
30e90 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 umn_metadata(.
30ea0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
30eb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
30ec0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 onnection handle
30ed0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
30ee0 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 *zDbName,
30ef0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 /* Database na
30f00 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 me or NULL */.
30f10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
30f20 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 leName, /* T
30f30 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 able name */. c
30f40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 onst char *zColu
30f50 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f mnName, /* Co
30f60 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 lumn name */. c
30f70 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 har const **pzDa
30f80 74 61 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 taType, /* OU
30f90 54 50 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 TPUT: Declared d
30fa0 61 74 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 ata type */. ch
30fb0 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c ar const **pzCol
30fc0 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 lSeq, /* OUT
30fd0 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 PUT: Collation s
30fe0 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a equence name */.
30ff0 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c int *pNotNull,
31000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
31010 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 OUTPUT: True if
31020 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 NOT NULL constr
31030 61 69 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 aint exists */.
31040 20 69 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 int *pPrimaryKe
31050 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 y, /*
31060 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 OUTPUT: True if
31070 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 column part of P
31080 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 K */. int *pAut
31090 6f 69 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 oinc
310a0 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 /* OUTPUT: Tr
310b0 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 ue if column is
310c0 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a auto-increment *
310d0 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.);../*.** CAPI
310e0 33 52 45 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 3REF: Load An Ex
310f0 74 65 6e 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d tension {H12600}
31100 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20500>.**.**
31110 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6c This interface l
31120 6f 61 64 73 20 61 6e 20 53 51 4c 69 74 65 20 65 oads an SQLite e
31130 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 xtension library
31140 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 from the named
31150 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 file..**.** {H12
31160 36 30 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 601} The sqlite3
31170 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 _load_extension(
31180 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 ) interface atte
31190 6d 70 74 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a mpts to load an.
311a0 2a 2a 20 20 20 20 20 20 20 20 20 20 53 51 4c 69 ** SQLi
311b0 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 te extension lib
311c0 72 61 72 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 rary contained i
311d0 6e 20 74 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 n the file zFile
311e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d ..**.** {H12602}
311f0 20 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 The entry point
31200 20 69 73 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a is zProc..**.**
31210 20 7b 48 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 {H12603} zProc
31220 6d 61 79 20 62 65 20 30 2c 20 69 6e 20 77 68 69 may be 0, in whi
31230 63 68 20 63 61 73 65 20 74 68 65 20 6e 61 6d 65 ch case the name
31240 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 70 6f of the entry po
31250 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 int.**
31260 64 65 66 61 75 6c 74 73 20 74 6f 20 22 73 71 6c defaults to "sql
31270 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 ite3_extension_i
31280 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 nit"..**.** {H12
31290 36 30 34 7d 20 54 68 65 20 73 71 6c 69 74 65 33 604} The sqlite3
312a0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 _load_extension(
312b0 29 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c ) interface shal
312c0 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 l return.**
312d0 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d [SQLITE_OK]
312e0 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 on success and
312f0 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 [SQLITE_ERROR] i
31300 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 f something goes
31310 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 wrong..**.** {H
31320 31 32 36 30 35 7d 20 49 66 20 61 6e 20 65 72 72 12605} If an err
31330 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a or occurs and pz
31340 45 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c ErrMsg is not 0,
31350 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 then the.**
31360 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c [sqlite3_l
31370 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d oad_extension()]
31380 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c interface shall
31390 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 attempt to.**
313a0 20 20 20 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a fill *pz
313b0 45 72 72 4d 73 67 20 77 69 74 68 20 65 72 72 6f ErrMsg with erro
313c0 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 73 r message text s
313d0 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a tored in memory.
313e0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 ** obta
313f0 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 ined from [sqlit
31400 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 e3_malloc()]. {E
31410 4e 44 7d 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 ND} The calling
31420 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 function.**
31430 20 20 20 20 20 20 73 68 6f 75 6c 64 20 66 72 65 should fre
31440 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 e this memory by
31450 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 calling [sqlite
31460 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 3_free()]..**.**
31470 20 7b 48 31 32 36 30 36 7d 20 45 78 74 65 6e 73 {H12606} Extens
31480 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 ion loading must
31490 20 62 65 20 65 6e 61 62 6c 65 64 20 75 73 69 6e be enabled usin
314a0 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 g.** [s
314b0 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f qlite3_enable_lo
314c0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 ad_extension()]
314d0 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 prior to calling
314e0 20 74 68 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 this API,.**
314f0 20 20 20 20 20 20 20 6f 74 68 65 72 77 69 73 65 otherwise
31500 20 61 6e 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 an error will b
31510 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 e returned..*/.S
31520 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
31530 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e lite3_load_exten
31540 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 sion(. sqlite3
31550 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a *db, /*
31560 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73 Load the extens
31570 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61 ion into this da
31580 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
31590 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 n */. const cha
315a0 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 r *zFile, /*
315b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72 Name of the shar
315c0 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 ed library conta
315d0 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 ining extension
315e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
315f0 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e *zProc, /* En
31600 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65 72 69 try point. Deri
31610 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20 69 ved from zFile i
31620 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a f 0 */. char **
31630 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f pzErrMsg /
31640 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 * Put error mess
31650 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 age here if not
31660 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 0 */.);../*.** C
31670 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 API3REF: Enable
31680 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e Or Disable Exten
31690 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 sion Loading {H1
316a0 32 36 32 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2620} <S20500>.*
316b0 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20 74 *.** So as not t
316c0 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20 o open security
316d0 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 holes in older a
316e0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
316f0 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61 72 are.** unprepar
31700 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 ed to deal with
31710 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e extension loadin
31720 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61 6e g, and as a mean
31730 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a 2a s of disabling.*
31740 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 * extension load
31750 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75 61 ing while evalua
31760 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72 65 ting user-entere
31770 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c 6f d SQL, the follo
31780 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20 70 wing API.** is p
31790 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e 20 rovided to turn
317a0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 the [sqlite3_loa
317b0 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 6d d_extension()] m
317c0 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 20 echanism on and
317d0 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e off..**.** Exten
317e0 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 sion loading is
317f0 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20 off by default.
31800 53 65 65 20 74 69 63 6b 65 74 20 23 31 38 36 33 See ticket #1863
31810 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 31 7d ..**.** {H12621}
31820 20 43 61 6c 6c 20 74 68 65 20 73 71 6c 69 74 65 Call the sqlite
31830 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 3_enable_load_ex
31840 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69 6e tension() routin
31850 65 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 0a e with onoff==1.
31860 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 74 ** to t
31870 75 72 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f urn extension lo
31880 61 64 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c ading on and cal
31890 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d l it with onoff=
318a0 3d 30 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 20 20 =0 to turn.**
318b0 20 20 20 20 20 20 20 69 74 20 62 61 63 6b 20 6f it back o
318c0 66 66 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 ff again..**.**
318d0 7b 48 31 32 36 32 32 7d 20 45 78 74 65 6e 73 69 {H12622} Extensi
318e0 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 on loading is of
318f0 66 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2f f by default..*/
31900 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
31910 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c sqlite3_enable_l
31920 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 oad_extension(sq
31930 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f lite3 *db, int o
31940 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 noff);../*.** CA
31950 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74 69 PI3REF: Automati
31960 63 61 6c 6c 79 20 4c 6f 61 64 20 41 6e 20 45 78 cally Load An Ex
31970 74 65 6e 73 69 6f 6e 73 20 7b 48 31 32 36 34 30 tensions {H12640
31980 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a } <S20500>.**.**
31990 20 54 68 69 73 20 41 50 49 20 63 61 6e 20 62 65 This API can be
319a0 20 69 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f 67 invoked at prog
319b0 72 61 6d 20 73 74 61 72 74 75 70 20 69 6e 20 6f ram startup in o
319c0 72 64 65 72 20 74 6f 20 72 65 67 69 73 74 65 72 rder to register
319d0 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 .** one or more
319e0 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 statically linke
319f0 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 d extensions tha
31a00 74 20 77 69 6c 6c 20 62 65 20 61 76 61 69 6c 61 t will be availa
31a10 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e 65 ble.** to all ne
31a20 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e w [database conn
31a30 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d 0a ections]. {END}.
31a40 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
31a50 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e ne stores a poin
31a60 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e ter to the exten
31a70 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 sion in an array
31a80 20 74 68 61 74 20 69 73 0a 2a 2a 20 6f 62 74 61 that is.** obta
31a90 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 ined from [sqlit
31aa0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 49 e3_malloc()]. I
31ab0 66 20 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d 6f f you run a memo
31ac0 72 79 20 6c 65 61 6b 20 63 68 65 63 6b 65 72 0a ry leak checker.
31ad0 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67 72 ** on your progr
31ae0 61 6d 20 61 6e 64 20 69 74 20 72 65 70 6f 72 74 am and it report
31af0 73 20 61 20 6c 65 61 6b 20 62 65 63 61 75 73 65 s a leak because
31b00 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 2c 20 of this array,
31b10 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 74 invoke.** [sqlit
31b20 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 e3_reset_auto_ex
31b30 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 tension()] prior
31b40 20 74 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f 20 to shutdown to
31b50 66 72 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 2e free the memory.
31b60 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 31 7d 20 .**.** {H12641}
31b70 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
31b80 67 69 73 74 65 72 73 20 61 6e 20 65 78 74 65 6e gisters an exten
31b90 73 69 6f 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 sion entry point
31ba0 20 74 68 61 74 20 69 73 0a 2a 2a 20 20 20 20 20 that is.**
31bb0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c automatical
31bc0 6c 79 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 ly invoked whene
31bd0 76 65 72 20 61 20 6e 65 77 20 5b 64 61 74 61 62 ver a new [datab
31be0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a ase connection].
31bf0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 6f ** is o
31c00 70 65 6e 65 64 20 75 73 69 6e 67 20 5b 73 71 6c pened using [sql
31c10 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 ite3_open()], [s
31c20 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
31c30 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 ,.** or
31c40 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
31c50 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 2()]..**.** {H12
31c60 36 34 32 7d 20 44 75 70 6c 69 63 61 74 65 20 65 642} Duplicate e
31c70 78 74 65 6e 73 69 6f 6e 73 20 61 72 65 20 64 65 xtensions are de
31c80 74 65 63 74 65 64 20 73 6f 20 63 61 6c 6c 69 6e tected so callin
31c90 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a g this routine.*
31ca0 2a 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69 * multi
31cb0 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 20 74 ple times with t
31cc0 68 65 20 73 61 6d 65 20 65 78 74 65 6e 73 69 6f he same extensio
31cd0 6e 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a 2a n is harmless..*
31ce0 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 7d 20 54 68 *.** {H12643} Th
31cf0 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 is routine store
31d00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
31d10 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 he extension in
31d20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 20 20 20 20 an array.**
31d30 20 20 20 20 20 74 68 61 74 20 69 73 20 6f 62 74 that is obt
31d40 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 ained from [sqli
31d50 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a te3_malloc()]..*
31d60 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 7d 20 41 75 *.** {H12644} Au
31d70 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f tomatic extensio
31d80 6e 73 20 61 70 70 6c 79 20 61 63 72 6f 73 73 20 ns apply across
31d90 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a all threads..*/.
31da0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
31db0 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 qlite3_auto_exte
31dc0 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 45 6e nsion(void (*xEn
31dd0 74 72 79 50 6f 69 6e 74 29 28 76 6f 69 64 29 29 tryPoint)(void))
31de0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
31df0 46 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61 74 F: Reset Automat
31e00 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 ic Extension Loa
31e10 64 69 6e 67 20 7b 48 31 32 36 36 30 7d 20 3c 53 ding {H12660} <S
31e20 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 20500>.**.** Thi
31e30 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 s function disab
31e40 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 les all previous
31e50 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 61 75 ly registered au
31e60 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 78 74 65 6e tomatic.** exten
31e70 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20 20 49 74 sions. {END} It
31e80 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66 65 undoes the effe
31e90 63 74 20 6f 66 20 61 6c 6c 20 70 72 69 6f 72 0a ct of all prior.
31ea0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 75 74 6f ** [sqlite3_auto
31eb0 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 63 61 _extension()] ca
31ec0 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 lls..**.** {H126
31ed0 36 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 61} This functio
31ee0 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 n disables all p
31ef0 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 reviously regist
31f00 65 72 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 ered.**
31f10 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e automatic exten
31f20 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 sions..**.** {H1
31f30 32 36 36 32 7d 20 54 68 69 73 20 66 75 6e 63 74 2662} This funct
31f40 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 75 74 ion disables aut
31f50 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e omatic extension
31f60 73 20 69 6e 20 61 6c 6c 20 74 68 72 65 61 64 73 s in all threads
31f70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
31f80 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
31f90 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f et_auto_extensio
31fa0 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 2a n(void);../*.***
31fb0 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c *** EXPERIMENTAL
31fc0 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 - subject to ch
31fd0 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 ange without not
31fe0 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ice ************
31ff0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 **.**.** The int
32000 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 erface to the vi
32010 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 rtual-table mech
32020 61 6e 69 73 6d 20 69 73 20 63 75 72 72 65 6e 74 anism is current
32030 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a ly considered.**
32040 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e to be experimen
32050 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 tal. The interf
32060 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 ace might change
32070 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 in incompatible
32080 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 ways..** If thi
32090 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 s is a problem f
320a0 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 or you, do not u
320b0 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 se the interface
320c0 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a at this time..*
320d0 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 *.** When the vi
320e0 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 rtual-table mech
320f0 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 anism stabilizes
32100 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 , we will declar
32110 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 e the.** interfa
32120 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 ce fixed, suppor
32130 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c t it indefinitel
32140 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 y, and remove th
32150 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 0a is comment..*/..
32160 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 73 /*.** Structures
32170 20 75 73 65 64 20 62 79 20 74 68 65 20 76 69 72 used by the vir
32180 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 72 tual table inter
32190 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 face.*/.typedef
321a0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 struct sqlite3_v
321b0 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74 61 62 tab sqlite3_vtab
321c0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
321d0 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 sqlite3_index_i
321e0 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 nfo sqlite3_inde
321f0 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 x_info;.typedef
32200 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 struct sqlite3_v
32210 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 tab_cursor sqlit
32220 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 3b 0a e3_vtab_cursor;.
32230 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
32240 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 71 qlite3_module sq
32250 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a 0a 2f lite3_module;../
32260 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 *.** CAPI3REF: V
32270 69 72 74 75 61 6c 20 54 61 62 6c 65 20 4f 62 6a irtual Table Obj
32280 65 63 74 20 7b 48 31 38 30 30 30 7d 20 3c 53 32 ect {H18000} <S2
32290 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0400>.** KEYWORD
322a0 53 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c S: sqlite3_modul
322b0 65 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e {virtual table
322c0 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 45 58 50 45 module}.** EXPE
322d0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
322e0 68 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 73 his structure, s
322f0 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 ometimes called
32300 61 20 61 20 22 76 69 72 74 75 61 6c 20 74 61 62 a a "virtual tab
32310 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 0a 2a 2a 20 le module", .**
32320 64 65 66 69 6e 65 73 20 74 68 65 20 69 6d 70 6c defines the impl
32330 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 ementation of a
32340 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 5d [virtual tables]
32350 2e 20 20 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 . .** This stru
32360 63 74 75 72 65 20 63 6f 6e 73 69 73 74 73 20 6d cture consists m
32370 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64 73 ostly of methods
32380 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 2e for the module.
32390 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c .**.** A virtual
323a0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 69 73 table module is
323b0 20 63 72 65 61 74 65 64 20 62 79 20 66 69 6c 6c created by fill
323c0 69 6e 67 20 69 6e 20 61 20 70 65 72 73 69 73 74 ing in a persist
323d0 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 ent.** instance
323e0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
323f0 65 20 61 6e 64 20 70 61 73 73 69 6e 67 20 61 20 e and passing a
32400 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 pointer to that
32410 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 74 6f 20 5b instance.** to [
32420 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
32430 6f 64 75 6c 65 28 29 5d 20 6f 72 20 5b 73 71 6c odule()] or [sql
32440 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
32450 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a 20 54 68 65 le_v2()]..** The
32460 20 72 65 67 69 73 74 72 61 74 69 6f 6e 20 72 65 registration re
32470 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 mains valid unti
32480 6c 20 69 74 20 69 73 20 72 65 70 6c 61 63 65 64 l it is replaced
32490 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 0a by a different.
324a0 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 75 6e 74 ** module or unt
324b0 69 6c 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 il the [database
324c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 63 6c 6f connection] clo
324d0 73 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e ses. The conten
324e0 74 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 t.** of this str
324f0 75 63 74 75 72 65 20 6d 75 73 74 20 6e 6f 74 20 ucture must not
32500 63 68 61 6e 67 65 20 77 68 69 6c 65 20 69 74 20 change while it
32510 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 is registered wi
32520 74 68 0a 2a 2a 20 61 6e 79 20 64 61 74 61 62 61 th.** any databa
32530 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a se connection..*
32540 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
32550 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 6e 74 20 _module {. int
32560 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 iVersion;. int
32570 28 2a 78 43 72 65 61 74 65 29 28 73 71 6c 69 74 (*xCreate)(sqlit
32580 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c e3*, void *pAux,
32590 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
325a0 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 int argc, const
325b0 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 char *const*argv
325c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
325d0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a sqlite3_vtab **
325e0 70 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b ppVTab, char**);
325f0 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65 63 . int (*xConnec
32600 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 t)(sqlite3*, voi
32610 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 d *pAux,.
32620 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 int argc
32630 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f , const char *co
32640 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 nst*argv,.
32650 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
32660 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 _vtab **ppVTab,
32670 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 char**);. int (
32680 2a 78 42 65 73 74 49 6e 64 65 78 29 28 73 71 6c *xBestIndex)(sql
32690 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 ite3_vtab *pVTab
326a0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f , sqlite3_index_
326b0 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 20 28 2a info*);. int (*
326c0 78 44 69 73 63 6f 6e 6e 65 63 74 29 28 73 71 6c xDisconnect)(sql
326d0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 ite3_vtab *pVTab
326e0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74 );. int (*xDest
326f0 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74 61 roy)(sqlite3_vta
32700 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 b *pVTab);. int
32710 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 65 (*xOpen)(sqlite
32720 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 3_vtab *pVTab, s
32730 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
32740 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 3b 0a or **ppCursor);.
32750 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 int (*xClose)(
32760 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
32770 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 sor*);. int (*x
32780 46 69 6c 74 65 72 29 28 73 71 6c 69 74 65 33 5f Filter)(sqlite3_
32790 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 69 6e vtab_cursor*, in
327a0 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 t idxNum, const
327b0 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 20 char *idxStr,.
327c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
327d0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
327e0 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 3b 0a 20 value **argv);.
327f0 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73 71 int (*xNext)(sq
32800 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
32810 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 45 6f r*);. int (*xEo
32820 66 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f f)(sqlite3_vtab_
32830 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 cursor*);. int
32840 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 6c 69 74 (*xColumn)(sqlit
32850 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c e3_vtab_cursor*,
32860 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
32870 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 *, int);. int (
32880 2a 78 52 6f 77 69 64 29 28 73 71 6c 69 74 65 33 *xRowid)(sqlite3
32890 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73 _vtab_cursor*, s
328a0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 52 qlite3_int64 *pR
328b0 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78 owid);. int (*x
328c0 55 70 64 61 74 65 29 28 73 71 6c 69 74 65 33 5f Update)(sqlite3_
328d0 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71 6c vtab *, int, sql
328e0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20 73 ite3_value **, s
328f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 29 3b qlite3_int64 *);
32900 0a 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e 29 . int (*xBegin)
32910 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
32920 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
32930 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76 74 Sync)(sqlite3_vt
32940 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e ab *pVTab);. in
32950 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71 6c t (*xCommit)(sql
32960 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 ite3_vtab *pVTab
32970 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c 6c );. int (*xRoll
32980 62 61 63 6b 29 28 73 71 6c 69 74 65 33 5f 76 74 back)(sqlite3_vt
32990 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e ab *pVTab);. in
329a0 74 20 28 2a 78 46 69 6e 64 46 75 6e 63 74 69 6f t (*xFindFunctio
329b0 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 n)(sqlite3_vtab
329c0 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67 *pVtab, int nArg
329d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
329e0 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ame,.
329f0 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 void
32a00 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 (**pxFunc)(sqli
32a10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 te3_context*,int
32a20 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a ,sqlite3_value**
32a30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
32a40 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a void *
32a50 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 *ppArg);. int (
32a60 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c 69 74 65 *xRename)(sqlite
32a70 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 63 3_vtab *pVtab, c
32a80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 29 onst char *zNew)
32a90 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ;.};../*.** CAPI
32aa0 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 3REF: Virtual Ta
32ab0 62 6c 65 20 49 6e 64 65 78 69 6e 67 20 49 6e 66 ble Indexing Inf
32ac0 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38 31 30 30 ormation {H18100
32ad0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 } <S20400>.** KE
32ae0 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f YWORDS: sqlite3_
32af0 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 45 58 index_info.** EX
32b00 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
32b10 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 The sqlite3_ind
32b20 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 ex_info structur
32b30 65 20 61 6e 64 20 69 74 73 20 73 75 62 73 74 72 e and its substr
32b40 75 63 74 75 72 65 73 20 69 73 20 75 73 65 64 20 uctures is used
32b50 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f 72 to.** pass infor
32b60 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64 20 mation into and
32b70 72 65 63 65 69 76 65 20 74 68 65 20 72 65 70 6c receive the repl
32b80 79 20 66 72 6f 6d 20 74 68 65 20 5b 78 42 65 73 y from the [xBes
32b90 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d 65 74 68 6f tIndex].** metho
32ba0 64 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 d of a [virtual
32bb0 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 table module].
32bc0 54 68 65 20 66 69 65 6c 64 73 20 75 6e 64 65 72 The fields under
32bd0 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65 20 **Inputs** are
32be0 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74 6f the.** inputs to
32bf0 20 78 42 65 73 74 49 6e 64 65 78 20 61 6e 64 20 xBestIndex and
32c00 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 are read-only.
32c10 78 42 65 73 74 49 6e 64 65 78 20 69 6e 73 65 72 xBestIndex inser
32c20 74 73 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 ts its.** result
32c30 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75 74 s into the **Out
32c40 70 75 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a puts** fields..*
32c50 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 *.** The aConstr
32c60 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72 65 63 aint[] array rec
32c70 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61 75 73 ords WHERE claus
32c80 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 e constraints of
32c90 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a the form:.**.**
32ca0 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e 20 4f 50 20 <pre>column OP
32cb0 65 78 70 72 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a expr</pre>.**.**
32cc0 20 77 68 65 72 65 20 4f 50 20 69 73 20 3d 2c 20 where OP is =,
32cd0 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 <, <=, >
32ce0 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20 20 54 68 ;, or >=. Th
32cf0 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 e particular ope
32d00 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 74 6f 72 rator is.** stor
32d10 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e ed in aConstrain
32d20 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e 64 t[].op. The ind
32d30 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ex of the column
32d40 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a is stored in.**
32d50 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 69 aConstraint[].i
32d60 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74 72 Column. aConstr
32d70 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 aint[].usable is
32d80 20 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a 20 TRUE if the.**
32d90 65 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67 68 expr on the righ
32da0 74 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e 20 t-hand side can
32db0 62 65 20 65 76 61 6c 75 61 74 65 64 20 28 61 6e be evaluated (an
32dc0 64 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73 74 d thus the const
32dd0 72 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 62 raint.** is usab
32de0 6c 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69 66 le) and false if
32df0 20 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a it cannot..**.*
32e00 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 * The optimizer
32e10 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e automatically in
32e20 76 65 72 74 73 20 74 65 72 6d 73 20 6f 66 20 74 verts terms of t
32e30 68 65 20 66 6f 72 6d 20 22 65 78 70 72 20 4f 50 he form "expr OP
32e40 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64 20 column".** and
32e50 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 69 6d 70 makes other simp
32e60 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74 lifications to t
32e70 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
32e80 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f in an attempt to
32e90 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e 79 20 .** get as many
32ea0 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 WHERE clause ter
32eb0 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 6d ms into the form
32ec0 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 61 73 20 shown above as
32ed0 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 possible..** The
32ee0 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 aConstraint[] a
32ef0 72 72 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72 74 rray only report
32f00 73 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 s WHERE clause t
32f10 65 72 6d 73 20 69 6e 20 74 68 65 20 63 6f 72 72 erms in the corr
32f20 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61 74 ect.** form that
32f30 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 61 refer to the pa
32f40 72 74 69 63 75 6c 61 72 20 76 69 72 74 75 61 6c rticular virtual
32f50 20 74 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65 table being que
32f60 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f ried..**.** Info
32f70 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
32f80 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
32f90 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
32fa0 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45 61 OrderBy[]..** Ea
32fb0 63 68 20 74 65 72 6d 20 6f 66 20 61 4f 72 64 65 ch term of aOrde
32fc0 72 42 79 20 72 65 63 6f 72 64 73 20 61 20 63 6f rBy records a co
32fd0 6c 75 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 lumn of the ORDE
32fe0 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a R BY clause..**.
32ff0 2a 2a 20 54 68 65 20 5b 78 42 65 73 74 49 6e 64 ** The [xBestInd
33000 65 78 5d 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 ex] method must
33010 66 69 6c 6c 20 61 43 6f 6e 73 74 72 61 69 6e 74 fill aConstraint
33020 55 73 61 67 65 5b 5d 20 77 69 74 68 20 69 6e 66 Usage[] with inf
33030 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 ormation.** abou
33040 74 20 77 68 61 74 20 70 61 72 61 6d 65 74 65 72 t what parameter
33050 73 20 74 6f 20 70 61 73 73 20 74 6f 20 78 46 69 s to pass to xFi
33060 6c 74 65 72 2e 20 20 49 66 20 61 72 67 76 49 6e lter. If argvIn
33070 64 65 78 3e 30 20 74 68 65 6e 0a 2a 2a 20 74 68 dex>0 then.** th
33080 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
33090 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 e of the corresp
330a0 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 74 72 61 69 onding aConstrai
330b0 6e 74 5b 5d 20 69 73 20 65 76 61 6c 75 61 74 65 nt[] is evaluate
330c0 64 0a 2a 2a 20 61 6e 64 20 62 65 63 6f 6d 65 73 d.** and becomes
330d0 20 74 68 65 20 61 72 67 76 49 6e 64 65 78 2d 74 the argvIndex-t
330e0 68 20 65 6e 74 72 79 20 69 6e 20 61 72 67 76 2e h entry in argv.
330f0 20 20 49 66 20 61 43 6f 6e 73 74 72 61 69 6e 74 If aConstraint
33100 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 Usage[].omit.**
33110 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
33120 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 e constraint is
33130 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 66 75 assumed to be fu
33140 6c 6c 79 20 68 61 6e 64 6c 65 64 20 62 79 20 74 lly handled by t
33150 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 he.** virtual ta
33160 62 6c 65 20 61 6e 64 20 69 73 20 6e 6f 74 20 63 ble and is not c
33170 68 65 63 6b 65 64 20 61 67 61 69 6e 20 62 79 20 hecked again by
33180 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 SQLite..**.** Th
33190 65 20 69 64 78 4e 75 6d 20 61 6e 64 20 69 64 78 e idxNum and idx
331a0 50 74 72 20 76 61 6c 75 65 73 20 61 72 65 20 72 Ptr values are r
331b0 65 63 6f 72 64 65 64 20 61 6e 64 20 70 61 73 73 ecorded and pass
331c0 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b ed into the.** [
331d0 78 46 69 6c 74 65 72 5d 20 6d 65 74 68 6f 64 2e xFilter] method.
331e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 .** [sqlite3_fre
331f0 65 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 e()] is used to
33200 66 72 65 65 20 69 64 78 50 74 72 20 69 66 20 61 free idxPtr if a
33210 6e 64 20 6f 6e 6c 79 20 69 66 66 0a 2a 2a 20 6e nd only iff.** n
33220 65 65 64 54 6f 46 72 65 65 49 64 78 50 74 72 20 eedToFreeIdxPtr
33230 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 is true..**.** T
33240 68 65 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d he orderByConsum
33250 65 64 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 75 ed means that ou
33260 74 70 75 74 20 66 72 6f 6d 20 5b 78 46 69 6c 74 tput from [xFilt
33270 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 69 6c 6c er]/[xNext] will
33280 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 occur in.** the
33290 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74 correct order t
332a0 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 o satisfy the OR
332b0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f DER BY clause so
332c0 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74 that no separat
332d0 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 e.** sorting ste
332e0 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a p is required..*
332f0 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 *.** The estimat
33300 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20 edCost value is
33310 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 an estimate of t
33320 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 he cost of doing
33330 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c the.** particul
33340 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 ar lookup. A fu
33350 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62 ll scan of a tab
33360 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65 le with N entrie
33370 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a s should have.**
33380 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 a cost of N. A
33390 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f binary search o
333a0 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 f a table of N e
333b0 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 ntries should ha
333c0 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 ve a.** cost of
333d0 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f approximately lo
333e0 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 g(N)..*/.struct
333f0 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e sqlite3_index_in
33400 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74 73 fo {. /* Inputs
33410 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74 */. int nConst
33420 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 raint;
33430 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
33440 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74 72 tries in aConstr
33450 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 aint */. struct
33460 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 sqlite3_index_c
33470 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 20 20 onstraint {.
33480 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 int iColumn;
33490 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
334a0 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d 68 61 6e lumn on left-han
334b0 64 20 73 69 64 65 20 6f 66 20 63 6f 6e 73 74 72 d side of constr
334c0 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 75 6e 73 aint */. uns
334d0 69 67 6e 65 64 20 63 68 61 72 20 6f 70 3b 20 20 igned char op;
334e0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 /* Constr
334f0 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 20 2a 2f aint operator */
33500 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 . unsigned c
33510 68 61 72 20 75 73 61 62 6c 65 3b 20 20 20 20 20 har usable;
33520 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
33530 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73 constraint is us
33540 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 69 6e 74 able */. int
33550 20 69 54 65 72 6d 4f 66 66 73 65 74 3b 20 20 20 iTermOffset;
33560 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 69 /* Used i
33570 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 42 65 73 nternally - xBes
33580 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 20 69 67 tIndex should ig
33590 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f nore */. } *aCo
335a0 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20 nstraint;
335b0 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f 66 /* Table of
335c0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f WHERE clause co
335d0 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 nstraints */. i
335e0 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20 nt nOrderBy;
335f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
33600 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 ber of terms in
33610 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
33620 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 use */. struct
33630 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 sqlite3_index_or
33640 64 65 72 62 79 20 7b 0a 20 20 20 20 20 69 6e 74 derby {. int
33650 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 iColumn;
33660 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e /* Column
33670 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 number */.
33680 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 65 unsigned char de
33690 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 sc; /* Tru
336a0 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61 6c e for DESC. Fal
336b0 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0a 20 se for ASC. */.
336c0 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20 20 } *aOrderBy;
336d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
336e0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
336f0 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 74 70 75 se */. /* Outpu
33700 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 ts */. struct s
33710 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e qlite3_index_con
33720 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 7b 0a straint_usage {.
33730 20 20 20 20 69 6e 74 20 61 72 67 76 49 6e 64 65 int argvInde
33740 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 x; /*
33750 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69 6e if >0, constrain
33760 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 72 67 t is part of arg
33770 76 20 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f 0a v to xFilter */.
33780 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
33790 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a 20 r omit; /*
337a0 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 20 74 65 Do not code a te
337b0 73 74 20 66 6f 72 20 74 68 69 73 20 63 6f 6e 73 st for this cons
337c0 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a 61 traint */. } *a
337d0 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b ConstraintUsage;
337e0 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20 20 . int idxNum;
337f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
33800 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 Number used to
33810 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e 64 identify the ind
33820 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69 64 ex */. char *id
33830 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 xStr;
33840 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70 6f /* String, po
33850 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64 20 ssibly obtained
33860 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
33870 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 loc */. int nee
33880 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20 20 dToFreeIdxStr;
33890 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78 53 /* Free idxS
338a0 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 tr using sqlite3
338b0 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65 20 _free() if true
338c0 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42 79 */. int orderBy
338d0 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20 20 Consumed;
338e0 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70 75 /* True if outpu
338f0 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72 64 t is already ord
33900 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 ered */. double
33910 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b 20 estimatedCost;
33920 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 /* Estimate
33930 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 d cost of using
33940 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b this index */.};
33950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
33960 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 INDEX_CONSTRAINT
33970 5f 45 51 20 20 20 20 32 0a 23 64 65 66 69 6e 65 _EQ 2.#define
33980 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f SQLITE_INDEX_CO
33990 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 20 20 34 NSTRAINT_GT 4
339a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
339b0 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 INDEX_CONSTRAINT
339c0 5f 4c 45 20 20 20 20 38 0a 23 64 65 66 69 6e 65 _LE 8.#define
339d0 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f SQLITE_INDEX_CO
339e0 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 20 20 31 NSTRAINT_LT 1
339f0 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 6.#define SQLITE
33a00 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e _INDEX_CONSTRAIN
33a10 54 5f 47 45 20 20 20 20 33 32 0a 23 64 65 66 69 T_GE 32.#defi
33a20 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f ne SQLITE_INDEX_
33a30 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 CONSTRAINT_MATCH
33a40 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 64../*.** CAPI3
33a50 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 REF: Register A
33a60 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49 6d Virtual Table Im
33a70 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 7b 48 31 plementation {H1
33a80 38 32 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8200} <S20400>.*
33a90 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
33aa0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
33ab0 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 67 e is used to reg
33ac0 69 73 74 65 72 20 61 20 6e 65 77 20 5b 76 69 72 ister a new [vir
33ad0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c tual table modul
33ae0 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 4d 6f 64 75 e] name..** Modu
33af0 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74 20 62 65 le names must be
33b00 20 72 65 67 69 73 74 65 72 65 64 20 62 65 66 6f registered befo
33b10 72 65 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 61 re.** creating a
33b20 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20 74 61 new [virtual ta
33b30 62 6c 65 5d 20 75 73 69 6e 67 20 74 68 65 20 6d ble] using the m
33b40 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65 odule, or before
33b50 20 75 73 69 6e 67 20 61 0a 2a 2a 20 70 72 65 65 using a.** pree
33b60 78 69 73 74 69 6e 67 20 5b 76 69 72 74 75 61 6c xisting [virtual
33b70 20 74 61 62 6c 65 5d 20 66 6f 72 20 74 68 65 20 table] for the
33b80 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 module..**.** Th
33b90 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69 73 e module name is
33ba0 20 72 65 67 69 73 74 65 72 65 64 20 6f 6e 20 74 registered on t
33bb0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
33bc0 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 nection] specifi
33bd0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 69 72 ed.** by the fir
33be0 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 st parameter. T
33bf0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d he name of the m
33c00 6f 64 75 6c 65 20 69 73 20 67 69 76 65 6e 20 62 odule is given b
33c10 79 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 y the .** second
33c20 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 parameter. The
33c30 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
33c40 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
33c50 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e .** the implemen
33c60 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 76 tation of the [v
33c70 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 irtual table mod
33c80 75 6c 65 5d 2e 20 20 20 54 68 65 20 66 6f 75 72 ule]. The four
33c90 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 th.** parameter
33ca0 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 is an arbitrary
33cb0 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69 6e client data poin
33cc0 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 73 ter that is pass
33cd0 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 6e ed through.** in
33ce0 74 6f 20 74 68 65 20 5b 78 43 72 65 61 74 65 5d to the [xCreate]
33cf0 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 and [xConnect]
33d00 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 76 methods of the v
33d10 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 irtual table mod
33d20 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 20 6e 65 ule.** when a ne
33d30 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 w virtual table
33d40 69 73 20 62 65 20 62 65 69 6e 67 20 63 72 65 61 is be being crea
33d50 74 65 64 20 6f 72 20 72 65 69 6e 69 74 69 61 6c ted or reinitial
33d60 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ized..**.** This
33d70 20 69 6e 74 65 72 66 61 63 65 20 68 61 73 20 65 interface has e
33d80 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 xactly the same
33d90 65 66 66 65 63 74 20 61 73 20 63 61 6c 6c 69 6e effect as callin
33da0 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 g.** [sqlite3_cr
33db0 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 eate_module_v2()
33dc0 5d 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 63 6c ] with a NULL cl
33dd0 69 65 6e 74 20 64 61 74 61 20 64 65 73 74 72 75 ient data destru
33de0 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ctor..*/.SQLITE_
33df0 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
33e00 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 IMENTAL int sqli
33e10 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c te3_create_modul
33e20 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 e(. sqlite3 *db
33e30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
33e40 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 /* SQLite connec
33e50 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 tion to register
33e60 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a module with */.
33e70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
33e80 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ame, /*
33e90 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 Name of the modu
33ea0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 le */. const sq
33eb0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 2c lite3_module *p,
33ec0 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f /* Methods fo
33ed0 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a r the module */.
33ee0 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74 44 void *pClientD
33ef0 61 74 61 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ata /*
33f00 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20 Client data for
33f10 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 xCreate/xConnect
33f20 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.);../*.** CA
33f30 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 PI3REF: Register
33f40 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 A Virtual Table
33f50 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
33f60 7b 48 31 38 32 31 30 7d 20 3c 53 32 30 34 30 30 {H18210} <S20400
33f70 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
33f80 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 L.**.** This rou
33f90 74 69 6e 65 20 69 73 20 69 64 65 6e 74 69 63 61 tine is identica
33fa0 6c 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 l to the [sqlite
33fb0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 3_create_module(
33fc0 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a 2a 20 65 78 )] method,.** ex
33fd0 63 65 70 74 20 74 68 61 74 20 69 74 20 68 61 73 cept that it has
33fe0 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d 65 an extra parame
33ff0 74 65 72 20 74 6f 20 73 70 65 63 69 66 79 20 0a ter to specify .
34000 2a 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 ** a destructor
34010 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 function for the
34020 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69 client data poi
34030 6e 74 65 72 2e 20 20 53 51 4c 69 74 65 20 77 69 nter. SQLite wi
34040 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 ll.** invoke the
34050 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 destructor func
34060 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73 20 6e tion (if it is n
34070 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20 53 51 ot NULL) when SQ
34080 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 Lite.** no longe
34090 72 20 6e 65 65 64 73 20 74 68 65 20 70 43 6c 69 r needs the pCli
340a0 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65 72 2e entData pointer.
340b0 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 .*/.SQLITE_API
340c0 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 SQLITE_EXPERIME
340d0 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 NTAL int sqlite3
340e0 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 _create_module_v
340f0 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2(. sqlite3 *db
34100 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
34110 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 /* SQLite connec
34120 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 tion to register
34130 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a module with */.
34140 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
34150 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ame, /*
34160 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 Name of the modu
34170 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 le */. const sq
34180 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 2c lite3_module *p,
34190 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f /* Methods fo
341a0 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a r the module */.
341b0 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74 44 void *pClientD
341c0 61 74 61 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ata, /*
341d0 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20 Client data for
341e0 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 xCreate/xConnect
341f0 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 */. void(*xDes
34200 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 20 troy)(void*)
34210 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 /* Module destr
34220 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a uctor function *
34230 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.);../*.** CAPI
34240 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 3REF: Virtual Ta
34250 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a ble Instance Obj
34260 65 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 32 ect {H18010} <S2
34270 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0400>.** KEYWORD
34280 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 0a S: sqlite3_vtab.
34290 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
342a0 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b 76 69 72 **.** Every [vir
342b0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c tual table modul
342c0 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f e] implementatio
342d0 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 n uses a subclas
342e0 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c s.** of the foll
342f0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
34300 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 to describe a pa
34310 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 rticular instanc
34320 65 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 76 69 72 e.** of the [vir
34330 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20 20 45 61 tual table]. Ea
34340 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c 6c ch subclass will
34350 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64 20 .** be tailored
34360 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63 20 to the specific
34370 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f 64 needs of the mod
34380 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ule implementati
34390 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f on..** The purpo
343a0 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 65 72 se of this super
343b0 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 66 69 class is to defi
343c0 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 6c 64 ne certain field
343d0 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63 6f s that are.** co
343e0 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f 64 75 mmon to all modu
343f0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
34400 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 ns..**.** Virtua
34410 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f 64 73 l tables methods
34420 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 72 6f can set an erro
34430 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 73 73 r message by ass
34440 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 72 69 igning a.** stri
34450 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ng obtained from
34460 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 [sqlite3_mprint
34470 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 67 2e f()] to zErrMsg.
34480 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 68 6f The method sho
34490 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 72 65 uld.** take care
344a0 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f 72 20 that any prior
344b0 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64 20 string is freed
344c0 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 by a call to [sq
344d0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a 2a 2a lite3_free()].**
344e0 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67 6e prior to assign
344f0 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e 67 ing a new string
34500 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41 66 to zErrMsg. Af
34510 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d 65 ter the error me
34520 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c 69 ssage.** is deli
34530 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65 20 vered up to the
34540 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74 69 client applicati
34550 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20 77 on, the string w
34560 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 ill be automatic
34570 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62 79 ally.** freed by
34580 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 sqlite3_free()
34590 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67 20 and the zErrMsg
345a0 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a 65 field will be ze
345b0 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 roed..*/.struct
345c0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 7b 0a 20 sqlite3_vtab {.
345d0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
345e0 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 odule *pModule;
345f0 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 /* The module f
34600 6f 72 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 or this virtual
34610 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e table */. int n
34620 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
34630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
34640 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f ed internally */
34650 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
34660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
34670 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 /* Error mess
34680 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 age from sqlite3
34690 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 20 20 _mprintf() */.
346a0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
346b0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
346c0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 will typically
346d0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 add additional f
346e0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ields */.};../*.
346f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 ** CAPI3REF: Vir
34700 74 75 61 6c 20 54 61 62 6c 65 20 43 75 72 73 6f tual Table Curso
34710 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 38 30 32 r Object {H1802
34720 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 0} <S20400>.** K
34730 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 EYWORDS: sqlite3
34740 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 76 69 _vtab_cursor {vi
34750 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 rtual table curs
34760 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e or}.** EXPERIMEN
34770 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 TAL.**.** Every
34780 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d [virtual table m
34790 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 odule] implement
347a0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 ation uses a sub
347b0 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 class of the.**
347c0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
347d0 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 ure to describe
347e0 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69 cursors that poi
347f0 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b nt into the.** [
34800 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 61 virtual table] a
34810 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74 nd are used.** t
34820 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 o loop through t
34830 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
34840 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 63 . Cursors are c
34850 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 reated using the
34860 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64 .** [sqlite3_mod
34870 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 4f 70 65 ule.xOpen | xOpe
34880 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 n] method of the
34890 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 72 65 20 module and are
348a0 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 62 79 20 destroyed.** by
348b0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64 the [sqlite3_mod
348c0 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 78 43 6c ule.xClose | xCl
348d0 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 20 43 75 ose] method. Cu
348e0 73 73 6f 72 73 20 61 72 65 20 75 73 65 64 0a 2a ssors are used.*
348f0 2a 20 62 79 20 74 68 65 20 5b 78 46 69 6c 74 65 * by the [xFilte
34900 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 5b 78 45 r], [xNext], [xE
34910 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e 5d 2c 20 of], [xColumn],
34920 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 6d 65 74 and [xRowid] met
34930 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 6d hods.** of the m
34940 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d 6f 64 odule. Each mod
34950 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ule implementati
34960 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65 0a 2a on will define.*
34970 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
34980 20 61 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 a cursor struct
34990 75 72 65 20 74 6f 20 73 75 69 74 20 69 74 73 20 ure to suit its
349a0 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a own needs..**.**
349b0 20 54 68 69 73 20 73 75 70 65 72 63 6c 61 73 73 This superclass
349c0 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64 65 72 exists in order
349d0 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65 6c 64 to define field
349e0 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20 s of the cursor
349f0 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f 6d 6d that.** are comm
34a00 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c 65 6d on to all implem
34a10 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 entations..*/.st
34a20 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 ruct sqlite3_vta
34a30 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c b_cursor {. sql
34a40 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
34a50 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 ; /* Virtua
34a60 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 l table of this
34a70 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 cursor */. /* V
34a80 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 irtual table imp
34a90 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c lementations wil
34aa0 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 l typically add
34ab0 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 additional field
34ac0 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 s */.};../*.** C
34ad0 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 API3REF: Declare
34ae0 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41 The Schema Of A
34af0 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b Virtual Table {
34b00 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e H18280} <S20400>
34b10 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
34b20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 43 72 65 .**.** The [xCre
34b30 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 ate] and [xConne
34b40 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 61 ct] methods of a
34b50 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 .** [virtual tab
34b60 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 6c 6c 20 le module] call
34b70 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 0a 2a this interface.*
34b80 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74 68 65 * to declare the
34b90 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e 61 6d format (the nam
34ba0 65 73 20 61 6e 64 20 64 61 74 61 74 79 70 65 73 es and datatypes
34bb0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 29 of the columns)
34bc0 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 of.** the virtu
34bd0 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79 20 69 al tables they i
34be0 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c mplement..*/.SQL
34bf0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
34c00 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 XPERIMENTAL int
34c10 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f sqlite3_declare_
34c20 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63 vtab(sqlite3*, c
34c30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 4c 29 onst char *zSQL)
34c40 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
34c50 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75 F: Overload A Fu
34c60 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72 nction For A Vir
34c70 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 33 tual Table {H183
34c80 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 00} <S20400>.**
34c90 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
34ca0 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 ** Virtual table
34cb0 73 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c s can provide al
34cc0 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d ternative implem
34cd0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e entations of fun
34ce0 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 ctions.** using
34cf0 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69 the [xFindFuncti
34d00 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 on] method of th
34d10 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e [virtual table
34d20 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a 20 42 module]. .** B
34d30 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f ut global versio
34d40 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e 63 ns of those func
34d50 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 65 78 tions.** must ex
34d60 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ist in order to
34d70 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e 0a 2a be overloaded..*
34d80 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61 *.** This API ma
34d90 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f 62 61 kes sure a globa
34da0 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 20 66 l version of a f
34db0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 70 unction with a p
34dc0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e 61 6d articular.** nam
34dd0 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20 e and number of
34de0 70 61 72 61 6d 65 74 65 72 73 20 65 78 69 73 74 parameters exist
34df0 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68 20 66 s. If no such f
34e00 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 0a 2a unction exists.*
34e10 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 41 50 * before this AP
34e20 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 20 6e I is called, a n
34e30 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 ew function is c
34e40 72 65 61 74 65 64 2e 20 20 54 68 65 20 69 6d 70 reated. The imp
34e50 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f lementation.** o
34e60 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 69 f the new functi
34e70 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73 65 73 on always causes
34e80 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f an exception to
34e90 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53 6f 0a be thrown. So.
34ea0 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 ** the new funct
34eb0 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f 64 20 ion is not good
34ec0 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62 79 20 for anything by
34ed0 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f 6e 6c itself. Its onl
34ee0 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69 73 20 y.** purpose is
34ef0 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68 6f 6c to be a placehol
34f00 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 der function tha
34f10 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c 6f 61 t can be overloa
34f20 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b 76 69 72 ded.** by a [vir
34f30 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a 2f 0a tual table]..*/.
34f40 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
34f50 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 E_EXPERIMENTAL i
34f60 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c nt sqlite3_overl
34f70 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c oad_function(sql
34f80 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 ite3*, const cha
34f90 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e r *zFuncName, in
34fa0 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t nArg);../*.**
34fb0 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f The interface to
34fc0 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 the virtual-tab
34fd0 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 le mechanism def
34fe0 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b ined above (back
34ff0 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d up.** to a comm
35000 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 ent remarkably s
35010 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f imilar to this o
35020 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ne) is currently
35030 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 considered.** t
35040 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61 o be experimenta
35050 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 l. The interfac
35060 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 e might change i
35070 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 n incompatible w
35080 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 ays..** If this
35090 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 is a problem for
350a0 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 you, do not use
350b0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 61 the interface a
350c0 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a t this time..**.
350d0 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 72 74 ** When the virt
350e0 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e ual-table mechan
350f0 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 ism stabilizes,
35100 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 we will declare
35110 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 the.** interface
35120 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20 fixed, support
35130 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c it indefinitely,
35140 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73 and remove this
35150 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a comment..**.***
35160 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c *** EXPERIMENTAL
35170 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 - subject to ch
35180 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 ange without not
35190 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ice ************
351a0 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 **.*/../*.** CAP
351b0 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20 I3REF: A Handle
351c0 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 To An Open BLOB
351d0 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30 {H17800} <S30230
351e0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
351f0 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c BLOB handle} {BL
35200 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a OB handles}.**.*
35210 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
35220 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 this object rep
35230 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 resents an open
35240 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a BLOB on which.**
35250 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
35260 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 pen | incrementa
35270 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 l BLOB I/O] can
35280 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a be performed..**
35290 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 Objects of this
352a0 20 74 79 70 65 20 61 72 65 20 63 72 65 61 74 65 type are create
352b0 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c d by [sqlite3_bl
352c0 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e ob_open()].** an
352d0 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b d destroyed by [
352e0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
352f0 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 se()]..** The [s
35300 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 qlite3_blob_read
35310 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
35320 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 _blob_write()] i
35330 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e nterfaces.** can
35340 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 64 be used to read
35350 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 or write small
35360 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 subsections of t
35370 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 he BLOB..** The
35380 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 [sqlite3_blob_by
35390 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tes()] interface
353a0 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a returns the siz
353b0 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e e of the BLOB in
353c0 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 bytes..*/.typed
353d0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
353e0 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 3_blob sqlite3_b
353f0 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lob;../*.** CAPI
35400 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 3REF: Open A BLO
35410 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61 B For Incrementa
35420 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c l I/O {H17810} <
35430 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30230>.**.** Th
35440 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70 is interfaces op
35450 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 ens a [BLOB hand
35460 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20 le | handle] to
35470 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64 the BLOB located
35480 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c .** in row iRow,
35490 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c column zColumn,
354a0 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e table zTable in
354b0 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a database zDb;.*
354c0 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * in other words
354d0 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20 , the same BLOB
354e0 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65 that would be se
354f0 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a lected by:.**.**
35500 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45 <pre>.** SE
35510 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f LECT zColumn FRO
35520 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45 M zDb.zTable WHE
35530 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f RE [rowid] = iRo
35540 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e w;.** </pre> {EN
35550 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 D}.**.** If the
35560 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
35570 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
35580 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 the BLOB is ope
35590 6e 65 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a 20 ned for read.**
355a0 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73 73 and write access
355b0 2e 20 49 66 20 69 74 20 69 73 20 7a 65 72 6f 2c . If it is zero,
355c0 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 the BLOB is ope
355d0 6e 65 64 20 66 6f 72 20 72 65 61 64 20 61 63 63 ned for read acc
355e0 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ess..**.** Note
355f0 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 that the databas
35600 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 e name is not th
35610 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20 e filename that
35620 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 contains.** the
35630 64 61 74 61 62 61 73 65 20 62 75 74 20 72 61 74 database but rat
35640 68 65 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 her the symbolic
35650 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
35660 61 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73 abase that.** is
35670 20 61 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74 assigned when t
35680 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 he database is c
35690 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b onnected using [
356a0 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20 ATTACH]..** For
356b0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
356c0 65 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 e file, the data
356d0 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61 base name is "ma
356e0 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50 in"..** For TEMP
356f0 20 74 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74 tables, the dat
35700 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74 abase name is "t
35710 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 emp"..**.** On s
35720 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f uccess, [SQLITE_
35730 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 OK] is returned
35740 61 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f and the new [BLO
35750 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 B handle] is wri
35760 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c tten.** to *ppBl
35770 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e ob. Otherwise an
35780 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
35790 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e returned and an
357a0 79 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 0a y value written.
357b0 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 20 73 68 ** to *ppBlob sh
357c0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 ould not be used
357d0 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a by the caller..
357e0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
357f0 20 73 65 74 73 20 74 68 65 20 5b 64 61 74 61 62 sets the [datab
35800 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
35810 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d error code and m
35820 65 73 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73 essage.** access
35830 69 62 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65 ible via [sqlite
35840 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64 3_errcode()] and
35850 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
35860 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ()]..**.** If th
35870 65 20 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f e row that a BLO
35880 42 20 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 B handle points
35890 74 6f 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 to is modified b
358a0 79 20 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d y an.** [UPDATE]
358b0 2c 20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 , [DELETE], or b
358c0 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 y [ON CONFLICT]
358d0 73 69 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 side-effects.**
358e0 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 then the BLOB ha
358f0 6e 64 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61 ndle is marked a
35900 73 20 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 s "expired"..**
35910 54 68 69 73 20 69 73 20 74 72 75 65 20 69 66 20 This is true if
35920 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 any column of th
35930 65 20 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64 e row is changed
35940 2c 20 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a , even a column.
35950 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 ** other than th
35960 65 20 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 e one the BLOB h
35970 61 6e 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e andle is open on
35980 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 ..** Calls to [s
35990 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 qlite3_blob_read
359a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
359b0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 _blob_write()] f
359c0 6f 72 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20 or.** a expired
359d0 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c BLOB handle fail
359e0 20 77 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20 with an return
359f0 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f code of [SQLITE_
35a00 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 ABORT]..** Chang
35a10 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 es written into
35a20 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 a BLOB prior to
35a30 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e the BLOB expirin
35a40 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c g are not.** rol
35a50 6c 62 61 63 6b 20 62 79 20 74 68 65 20 65 78 70 lback by the exp
35a60 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 iration of the B
35a70 4c 4f 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67 LOB. Such chang
35a80 65 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c es will eventual
35a90 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 ly.** commit if
35aa0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
35ab0 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d continues to com
35ac0 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 pletion..**.** R
35ad0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
35ae0 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38 31 34 [H17813] [H17814
35af0 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 38 ] [H17816] [H178
35b00 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 31 19] [H17821] [H1
35b10 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 7824].*/.SQLITE_
35b20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
35b30 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c blob_open(. sql
35b40 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 ite3*,. const c
35b50 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 har *zDb,. cons
35b60 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a t char *zTable,.
35b70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
35b80 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33 olumn,. sqlite3
35b90 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 _int64 iRow,. i
35ba0 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 nt flags,. sqli
35bb0 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f te3_blob **ppBlo
35bc0 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 b.);../*.** CAPI
35bd0 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 4c 3REF: Close A BL
35be0 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 33 OB Handle {H1783
35bf0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
35c00 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 6e * Closes an open
35c10 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a [BLOB handle]..
35c20 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 20 **.** Closing a
35c30 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 65 BLOB shall cause
35c40 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
35c50 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d nsaction to comm
35c60 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 61 it.** if there a
35c70 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f 42 re no other BLOB
35c80 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 72 s, no pending pr
35c90 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
35ca0 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 s, and the.** da
35cb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
35cc0 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f 6d n is in [autocom
35cd0 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 mit mode]..** If
35ce0 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 72 65 any writes were
35cf0 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f made to the BLO
35d00 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 B, they might be
35d10 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 0a 2a held in cache.*
35d20 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73 * until the clos
35d30 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74 e operation if t
35d40 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 20 7b 45 hey will fit. {E
35d50 4e 44 7d 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e ND}.**.** Closin
35d60 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e g the BLOB often
35d70 20 66 6f 72 63 65 73 20 74 68 65 20 63 68 61 6e forces the chan
35d80 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 ges.** out to di
35d90 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 sk and so if any
35da0 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 I/O errors occu
35db0 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b r, they will lik
35dc0 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 ely occur.** at
35dd0 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68 the time when th
35de0 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 e BLOB is closed
35df0 2e 20 20 7b 48 31 37 38 33 33 7d 20 41 6e 79 20 . {H17833} Any
35e00 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 errors that occu
35e10 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 r during.** clos
35e20 69 6e 67 20 61 72 65 20 72 65 70 6f 72 74 65 64 ing are reported
35e30 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 as a non-zero r
35e40 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a eturn value..**.
35e50 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63 ** The BLOB is c
35e60 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f losed unconditio
35e70 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 nally. Even if
35e80 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
35e90 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 urns.** an error
35ea0 20 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 code, the BLOB
35eb0 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e is still closed.
35ec0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
35ed0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d nts:.** [H17833]
35ee0 20 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 [H17836] [H1783
35ef0 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 9].*/.SQLITE_API
35f00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f int sqlite3_blo
35f10 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f b_close(sqlite3_
35f20 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 blob *);../*.**
35f30 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e CAPI3REF: Return
35f40 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 The Size Of An
35f50 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 Open BLOB {H1784
35f60 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
35f70 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 * Returns the si
35f80 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 ze in bytes of t
35f90 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 he BLOB accessib
35fa0 6c 65 20 76 69 61 20 74 68 65 20 6f 70 65 6e 0a le via the open.
35fb0 2a 2a 20 5b 5d 42 4c 4f 42 20 68 61 6e 64 6c 65 ** []BLOB handle
35fc0 5d 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 ] in its only ar
35fd0 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 gument..**.** Re
35fe0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
35ff0 48 31 37 38 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 H17843].*/.SQLIT
36000 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
36010 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 3_blob_bytes(sql
36020 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f ite3_blob *);../
36030 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
36040 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20 41 20 ead Data From A
36050 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c BLOB Incremental
36060 6c 79 20 7b 48 31 37 38 35 30 7d 20 3c 53 33 30 ly {H17850} <S30
36070 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 230>.**.** This
36080 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
36090 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66 72 to read data fr
360a0 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 om an open [BLOB
360b0 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 0a handle] into a.
360c0 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 ** caller-suppli
360d0 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 ed buffer. N byt
360e0 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 es of data are c
360f0 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66 66 65 opied into buffe
36100 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 r Z.** from the
36110 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 open BLOB, start
36120 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f ing at offset iO
36130 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ffset..**.** If
36140 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 offset iOffset i
36150 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 s less than N by
36160 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 tes from the end
36170 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a of the BLOB,.**
36180 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
36190 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
361a0 6e 6f 20 64 61 74 61 20 69 73 20 72 65 61 64 2e no data is read.
361b0 20 20 49 66 20 4e 20 6f 72 20 69 4f 66 66 73 65 If N or iOffse
361c0 74 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 t is.** less tha
361d0 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c 49 54 45 5f n zero, [SQLITE_
361e0 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e ERROR] is return
361f0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 ed and no data i
36200 73 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e s read..**.** An
36210 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 attempt to read
36220 20 66 72 6f 6d 20 61 6e 20 65 78 70 69 72 65 64 from an expired
36230 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 [BLOB handle] f
36240 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 ails with an.**
36250 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 error code of [S
36260 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a QLITE_ABORT]..**
36270 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 .** On success,
36280 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
36290 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 urned..** Otherw
362a0 69 73 65 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 ise, an [error c
362b0 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 ode] or an [exte
362c0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d nded error code]
362d0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
362e0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
362f0 3a 0a 2a 2a 20 5b 48 31 37 38 35 33 5d 20 5b 48 :.** [H17853] [H
36300 31 37 38 35 36 5d 20 5b 48 31 37 38 35 39 5d 20 17856] [H17859]
36310 5b 48 31 37 38 36 32 5d 20 5b 48 31 37 38 36 33 [H17862] [H17863
36320 5d 20 5b 48 31 37 38 36 35 5d 20 5b 48 31 37 38 ] [H17865] [H178
36330 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 68].*/.SQLITE_AP
36340 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
36350 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f ob_read(sqlite3_
36360 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c blob *, void *Z,
36370 20 69 6e 74 20 4e 2c 20 69 6e 74 20 69 4f 66 66 int N, int iOff
36380 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 set);../*.** CAP
36390 49 33 52 45 46 3a 20 57 72 69 74 65 20 44 61 74 I3REF: Write Dat
363a0 61 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49 6e a Into A BLOB In
363b0 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 crementally {H17
363c0 38 37 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 870} <S30230>.**
363d0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
363e0 6e 20 69 73 20 75 73 65 64 20 74 6f 20 77 72 69 n is used to wri
363f0 74 65 20 64 61 74 61 20 69 6e 74 6f 20 61 6e 20 te data into an
36400 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c open [BLOB handl
36410 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c e] from a.** cal
36420 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62 75 66 ler-supplied buf
36430 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f 66 20 fer. N bytes of
36440 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 data are copied
36450 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 from the buffer
36460 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 70 Z.** into the op
36470 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e en BLOB, startin
36480 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 g at offset iOff
36490 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 set..**.** If th
364a0 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 e [BLOB handle]
364b0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
364c0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77 61 73 rst argument was
364d0 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72 0a not opened for.
364e0 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68 65 20 ** writing (the
364f0 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
36500 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to [sqlite3_blob
36510 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a 65 72 _open()] was zer
36520 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 o),.** this func
36530 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 tion returns [SQ
36540 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a LITE_READONLY]..
36550 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
36560 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 ion may only mod
36570 69 66 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ify the contents
36580 20 6f 66 20 74 68 65 20 42 4c 4f 42 3b 20 69 74 of the BLOB; it
36590 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 is.** not possi
365a0 62 6c 65 20 74 6f 20 69 6e 63 72 65 61 73 65 20 ble to increase
365b0 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 4c the size of a BL
365c0 4f 42 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 OB using this AP
365d0 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 I..** If offset
365e0 69 4f 66 66 73 65 74 20 69 73 20 6c 65 73 73 20 iOffset is less
365f0 74 68 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f than N bytes fro
36600 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 m the end of the
36610 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 BLOB,.** [SQLIT
36620 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 E_ERROR] is retu
36630 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 rned and no data
36640 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49 66 is written. If
36650 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68 N is.** less th
36660 61 6e 20 7a 65 72 6f 20 5b 53 51 4c 49 54 45 5f an zero [SQLITE_
36670 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e ERROR] is return
36680 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 ed and no data i
36690 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a s written..**.**
366a0 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77 An attempt to w
366b0 72 69 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72 rite to an expir
366c0 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d ed [BLOB handle]
366d0 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a fails with an.*
366e0 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 * error code of
366f0 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20 [SQLITE_ABORT].
36700 20 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 42 Writes to the B
36710 4c 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72 65 LOB that occurre
36720 64 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 d.** before the
36730 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 [BLOB handle] ex
36740 70 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f pired are not ro
36750 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 lled back by the
36760 0a 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f .** expiration o
36770 66 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68 f the handle, th
36780 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 ough of course t
36790 68 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67 hose changes mig
367a0 68 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 ht.** have been
367b0 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 74 overwritten by t
367c0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 he statement tha
367d0 74 20 65 78 70 69 72 65 64 20 74 68 65 20 42 4c t expired the BL
367e0 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 OB handle.** or
367f0 62 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e by other indepen
36800 64 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e dent statements.
36810 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 .**.** On succes
36820 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 s, SQLITE_OK is
36830 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 returned..** Oth
36840 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 72 72 erwise, an [err
36850 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b or code] or an [
36860 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
36870 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ode] is returned
36880 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
36890 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 37 33 ents:.** [H17873
368a0 5d 20 5b 48 31 37 38 37 34 5d 20 5b 48 31 37 38 ] [H17874] [H178
368b0 37 35 5d 20 5b 48 31 37 38 37 36 5d 20 5b 48 31 75] [H17876] [H1
368c0 37 38 37 37 5d 20 5b 48 31 37 38 37 39 5d 20 5b 7877] [H17879] [
368d0 48 31 37 38 38 32 5d 20 5b 48 31 37 38 38 35 5d H17882] [H17885]
368e0 0a 2a 2a 20 5b 48 31 37 38 38 38 5d 0a 2a 2f 0a .** [H17888].*/.
368f0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
36900 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 qlite3_blob_writ
36910 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a e(sqlite3_blob *
36920 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c , const void *z,
36930 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 int n, int iOff
36940 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 set);../*.** CAP
36950 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 46 I3REF: Virtual F
36960 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 63 ile System Objec
36970 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 30 ts {H11200} <S20
36980 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 100>.**.** A vir
36990 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d 20 tual filesystem
369a0 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71 6c (VFS) is an [sql
369b0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
369c0 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 20 .** that SQLite
369d0 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63 74 uses to interact
369e0 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e 64 .** with the und
369f0 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e erlying operatin
36a00 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 20 g system. Most
36a10 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63 6f SQLite builds co
36a20 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 6e me with a.** sin
36a30 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 gle default VFS
36a40 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 that is appropri
36a50 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73 74 ate for the host
36a60 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e 65 computer..** Ne
36a70 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 72 w VFSes can be r
36a80 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65 78 egistered and ex
36a90 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61 6e isting VFSes can
36aa0 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65 64 be unregistered
36ab0 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ..** The followi
36ac0 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 ng interfaces ar
36ad0 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a 2a e provided..**.*
36ae0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 66 * The sqlite3_vf
36af0 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66 61 s_find() interfa
36b00 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
36b10 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67 69 nter to a VFS gi
36b20 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a 2a ven its name..**
36b30 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65 20 Names are case
36b40 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 61 sensitive..** Na
36b50 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 mes are zero-ter
36b60 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 minated UTF-8 st
36b70 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65 rings..** If the
36b80 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 re is no match,
36b90 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 a NULL pointer i
36ba0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 s returned..** I
36bb0 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e 55 f zVfsName is NU
36bc0 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 LL then the defa
36bd0 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75 72 ult VFS is retur
36be0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 56 ned..**.** New V
36bf0 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74 65 FSes are registe
36c00 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33 red with sqlite3
36c10 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2e _vfs_register().
36c20 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46 53 .** Each new VFS
36c30 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 becomes the def
36c40 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65 20 ault VFS if the
36c50 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 73 makeDflt flag is
36c60 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 6d set..** The sam
36c70 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65 67 e VFS can be reg
36c80 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 istered multiple
36c90 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20 69 times without i
36ca0 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b njury..** To mak
36cb0 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56 46 e an existing VF
36cc0 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75 S into the defau
36cd0 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65 72 lt VFS, register
36ce0 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 74 it again.** wit
36cf0 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 h the makeDflt f
36d00 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77 6f lag set. If two
36d10 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65 73 different VFSes
36d20 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 6d with the.** sam
36d30 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69 73 e name are regis
36d40 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61 76 tered, the behav
36d50 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
36d60 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 69 . If a.** VFS i
36d70 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 s registered wit
36d80 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69 73 h a name that is
36d90 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 NULL or an empt
36da0 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 65 y string,.** the
36db0 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 n the behavior i
36dc0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a s undefined..**.
36dd0 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 ** Unregister a
36de0 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71 6c VFS with the sql
36df0 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 ite3_vfs_unregis
36e00 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e ter() interface.
36e10 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 .** If the defau
36e20 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67 69 lt VFS is unregi
36e30 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20 stered, another
36e40 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73 VFS is chosen as
36e50 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e .** the default.
36e60 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72 The choice for
36e70 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73 20 the new VFS is
36e80 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a 2a arbitrary..**.**
36e90 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
36ea0 2a 20 5b 48 31 31 32 30 33 5d 20 5b 48 31 31 32 * [H11203] [H112
36eb0 30 36 5d 20 5b 48 31 31 32 30 39 5d 20 5b 48 31 06] [H11209] [H1
36ec0 31 32 31 32 5d 20 5b 48 31 31 32 31 35 5d 20 5b 1212] [H11215] [
36ed0 48 31 31 32 31 38 5d 0a 2a 2f 0a 53 51 4c 49 54 H11218].*/.SQLIT
36ee0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66 E_API sqlite3_vf
36ef0 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 s *sqlite3_vfs_f
36f00 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ind(const char *
36f10 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 zVfsName);.SQLIT
36f20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
36f30 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 73 3_vfs_register(s
36f40 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 qlite3_vfs*, int
36f50 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c 49 makeDflt);.SQLI
36f60 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
36f70 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 e3_vfs_unregiste
36f80 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b r(sqlite3_vfs*);
36f90 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
36fa0 3a 20 4d 75 74 65 78 65 73 20 7b 48 31 37 30 30 : Mutexes {H1700
36fb0 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 0} <S20000>.**.*
36fc0 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 * The SQLite cor
36fd0 65 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 e uses these rou
36fe0 74 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61 64 tines for thread
36ff0 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 74 .** synchronizat
37000 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65 79 ion. Though they
37010 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f are intended fo
37020 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
37030 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f 64 e by SQLite, cod
37040 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 e that links aga
37050 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a 2a inst SQLite is.*
37060 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 75 * permitted to u
37070 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 se any of these
37080 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 routines..**.**
37090 54 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72 63 The SQLite sourc
370a0 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20 e code contains
370b0 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 multiple impleme
370c0 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 ntations.** of t
370d0 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74 69 hese mutex routi
370e0 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 72 nes. An appropr
370f0 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 iate implementat
37100 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 74 ion.** is select
37110 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
37120 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 at compile-time
37130 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
37140 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
37150 6f 6e 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c ons are availabl
37160 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 e in the SQLite
37170 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e core:.**.** <ul>
37180 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 .** <li> SQLIT
37190 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20 3c E_MUTEX_OS2.** <
371a0 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 li> SQLITE_MUT
371b0 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c 6c EX_PTHREAD.** <l
371c0 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 i> SQLITE_MUTE
371d0 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 X_W32.** <li>
371e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
371f0 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a P.** </ul>.**.**
37200 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 The SQLITE_MUTE
37210 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e 74 X_NOOP implement
37220 61 74 69 6f 6e 20 69 73 20 61 20 73 65 74 20 6f ation is a set o
37230 66 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 74 68 f routines.** th
37240 61 74 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c 20 at does no real
37250 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69 73 20 61 locking and is a
37260 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 ppropriate for u
37270 73 65 20 69 6e 0a 2a 2a 20 61 20 73 69 6e 67 6c se in.** a singl
37280 65 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 e-threaded appli
37290 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 53 51 4c cation. The SQL
372a0 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a 2a ITE_MUTEX_OS2,.*
372b0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 * SQLITE_MUTEX_P
372c0 54 48 52 45 41 44 2c 20 61 6e 64 20 53 51 4c 49 THREAD, and SQLI
372d0 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d 70 TE_MUTEX_W32 imp
372e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 lementations.**
372f0 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 are appropriate
37300 66 6f 72 20 75 73 65 20 6f 6e 20 4f 53 2f 32 2c for use on OS/2,
37310 20 55 6e 69 78 2c 20 61 6e 64 20 57 69 6e 64 6f Unix, and Windo
37320 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c ws..**.** If SQL
37330 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ite is compiled
37340 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
37350 4d 55 54 45 58 5f 41 50 50 44 45 46 20 70 72 65 MUTEX_APPDEF pre
37360 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61 63 processor.** mac
37370 72 6f 20 64 65 66 69 6e 65 64 20 28 77 69 74 68 ro defined (with
37380 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45 58 "-DSQLITE_MUTEX
37390 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 74 68 65 _APPDEF=1"), the
373a0 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69 6d n no mutex.** im
373b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
373c0 69 6e 63 6c 75 64 65 64 20 77 69 74 68 20 74 68 included with th
373d0 65 20 6c 69 62 72 61 72 79 2e 20 49 6e 20 74 68 e library. In th
373e0 69 73 20 63 61 73 65 20 74 68 65 0a 2a 2a 20 61 is case the.** a
373f0 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 pplication must
37400 73 75 70 70 6c 79 20 61 20 63 75 73 74 6f 6d 20 supply a custom
37410 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 mutex implementa
37420 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a tion using the.*
37430 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
37440 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 20 6f _MUTEX] option o
37450 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f f the sqlite3_co
37460 6e 66 69 67 28 29 20 66 75 6e 63 74 69 6f 6e 0a nfig() function.
37470 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e ** before callin
37480 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 g sqlite3_initia
37490 6c 69 7a 65 28 29 20 6f 72 20 61 6e 79 20 6f 74 lize() or any ot
374a0 68 65 72 20 70 75 62 6c 69 63 20 73 71 6c 69 74 her public sqlit
374b0 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 e3_.** function
374c0 74 68 61 74 20 63 61 6c 6c 73 20 73 71 6c 69 74 that calls sqlit
374d0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e e3_initialize().
374e0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 31 7d 20 .**.** {H17011}
374f0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
37500 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e x_alloc() routin
37510 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 e allocates a ne
37520 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 w.** mutex and r
37530 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
37540 20 74 6f 20 69 74 2e 20 7b 48 31 37 30 31 32 7d to it. {H17012}
37550 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e If it returns N
37560 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e ULL.** that mean
37570 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 s that a mutex c
37580 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f ould not be allo
37590 63 61 74 65 64 2e 20 7b 48 31 37 30 31 33 7d 20 cated. {H17013}
375a0 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 SQLite.** will u
375b0 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 nwind its stack
375c0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 and return an er
375d0 72 6f 72 2e 20 7b 48 31 37 30 31 34 7d 20 54 68 ror. {H17014} Th
375e0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f e argument.** to
375f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
37600 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 lloc() is one of
37610 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 these integer c
37620 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 onstants:.**.**
37630 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 <ul>.** <li> SQ
37640 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a LITE_MUTEX_FAST.
37650 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
37660 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a MUTEX_RECURSIVE.
37670 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
37680 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
37690 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c TER.** <li> SQL
376a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
376b0 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 _MEM.** <li> SQ
376c0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
376d0 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 C_MEM2.** <li>
376e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
376f0 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e TIC_PRNG.** <li>
37700 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
37710 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 TATIC_LRU.** <li
37720 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
37730 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c STATIC_LRU2.** <
37740 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 /ul>.**.** {H170
37750 31 35 7d 20 54 68 65 20 66 69 72 73 74 20 74 77 15} The first tw
37760 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 o constants caus
37770 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
37780 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 alloc() to creat
37790 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 e.** a new mutex
377a0 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 . The new mutex
377b0 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 is recursive wh
377c0 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f en SQLITE_MUTEX_
377d0 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 RECURSIVE.** is
377e0 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 used but not nec
377f0 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e essarily so when
37800 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
37810 53 54 20 69 73 20 75 73 65 64 2e 20 7b 45 4e 44 ST is used. {END
37820 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 }.** The mutex i
37830 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f mplementation do
37840 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
37850 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f ake a distinctio
37860 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c n.** between SQL
37870 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
37880 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d IVE and SQLITE_M
37890 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 UTEX_FAST if it
378a0 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 does.** not want
378b0 20 74 6f 2e 20 20 7b 48 31 37 30 31 36 7d 20 42 to. {H17016} B
378c0 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f ut SQLite will o
378d0 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 nly request a re
378e0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e cursive mutex in
378f0 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
37900 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 it really needs
37910 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 49 66 20 61 one. {END} If a
37920 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 faster non-recu
37930 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 rsive mutex.** i
37940 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 mplementation is
37950 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 available on th
37960 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c e host platform,
37970 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 the mutex subsy
37980 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 stem.** might re
37990 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 turn such a mute
379a0 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f x in response to
379b0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
379c0 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 ST..**.** {H1701
379d0 37 7d 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 7} The other all
379e0 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 owed parameters
379f0 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
37a00 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 _alloc() each re
37a10 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 turn.** a pointe
37a20 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 r to a static pr
37a30 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e eexisting mutex.
37a40 20 7b 45 4e 44 7d 20 20 46 6f 75 72 20 73 74 61 {END} Four sta
37a50 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a tic mutexes are.
37a60 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 ** used by the c
37a70 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f urrent version o
37a80 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 f SQLite. Futur
37a90 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
37aa0 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 Lite.** may add
37ab0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 additional stati
37ac0 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 c mutexes. Stat
37ad0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 ic mutexes are f
37ae0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
37af0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c se by SQLite onl
37b00 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 y. Applications
37b10 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
37b20 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a mutexes should.
37b30 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 ** use only the
37b40 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 dynamic mutexes
37b50 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 returned by SQLI
37b60 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 TE_MUTEX_FAST or
37b70 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 .** SQLITE_MUTEX
37b80 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a _RECURSIVE..**.*
37b90 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f 74 65 20 * {H17018} Note
37ba0 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 that if one of t
37bb0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 he dynamic mutex
37bc0 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c parameters (SQL
37bd0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a ITE_MUTEX_FAST.*
37be0 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 * or SQLITE_MUTE
37bf0 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 X_RECURSIVE) is
37c00 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 used then sqlite
37c10 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 3_mutex_alloc().
37c20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 ** returns a dif
37c30 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 ferent mutex on
37c40 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 7b 48 31 every call. {H1
37c50 37 30 33 34 7d 20 42 75 74 20 66 6f 72 20 74 68 7034} But for th
37c60 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 e static.** mute
37c70 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d x types, the sam
37c80 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 e mutex is retur
37c90 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c ned on every cal
37ca0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 l that has.** th
37cb0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 e same type numb
37cc0 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 er..**.** {H1701
37cd0 39 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 9} The sqlite3_m
37ce0 75 74 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74 utex_free() rout
37cf0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
37d00 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 a previously.**
37d10 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69 allocated dynami
37d20 63 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 30 c mutex. {H17020
37d30 7d 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 } SQLite is care
37d40 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 ful to deallocat
37d50 65 20 65 76 65 72 79 0a 2a 2a 20 64 79 6e 61 6d e every.** dynam
37d60 69 63 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 ic mutex that it
37d70 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b 41 31 37 allocates. {A17
37d80 30 32 31 7d 20 54 68 65 20 64 79 6e 61 6d 69 63 021} The dynamic
37d90 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f mutexes must no
37da0 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 65 20 77 t be in.** use w
37db0 68 65 6e 20 74 68 65 79 20 61 72 65 20 64 65 61 hen they are dea
37dc0 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31 37 30 32 llocated. {A1702
37dd0 32 7d 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f 2} Attempting to
37de0 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 73 74 deallocate a st
37df0 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 72 65 atic.** mutex re
37e00 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e sults in undefin
37e10 65 64 20 62 65 68 61 76 69 6f 72 2e 20 7b 48 31 ed behavior. {H1
37e20 37 30 32 33 7d 20 53 51 4c 69 74 65 20 6e 65 76 7023} SQLite nev
37e30 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a er deallocates.*
37e40 2a 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 * a static mutex
37e50 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 . {END}.**.** Th
37e60 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
37e70 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 enter() and sqli
37e80 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
37e90 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
37ea0 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d .** to enter a m
37eb0 75 74 65 78 2e 20 7b 48 31 37 30 32 34 7d 20 49 utex. {H17024} I
37ec0 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 f another thread
37ed0 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 is already with
37ee0 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a in the mutex,.**
37ef0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
37f00 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 nter() will bloc
37f10 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 k and sqlite3_mu
37f20 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 tex_try() will r
37f30 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
37f40 42 55 53 59 2e 20 7b 48 31 37 30 32 35 7d 20 20 BUSY. {H17025}
37f50 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
37f60 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 x_try() interfac
37f70 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 e returns [SQLIT
37f80 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75 E_OK].** upon su
37f90 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 ccessful entry.
37fa0 20 7b 48 31 37 30 32 36 7d 20 4d 75 74 65 78 65 {H17026} Mutexe
37fb0 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a s created using.
37fc0 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
37fd0 52 45 43 55 52 53 49 56 45 20 63 61 6e 20 62 65 RECURSIVE can be
37fe0 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
37ff0 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
38000 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a 2a 20 7b ame thread..** {
38010 48 31 37 30 32 37 7d 20 49 6e 20 73 75 63 68 20 H17027} In such
38020 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 cases the,.** mu
38030 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 tex must be exit
38040 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 ed an equal numb
38050 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f er of times befo
38060 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 re another threa
38070 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 d.** can enter.
38080 20 7b 41 31 37 30 32 38 7d 20 49 66 20 74 68 65 {A17028} If the
38090 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 same thread tri
380a0 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 es to enter any
380b0 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 other.** kind of
380c0 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 68 61 6e mutex more than
380d0 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 once, the behav
380e0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
380f0 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d 20 53 51 ..** {H17029} SQ
38100 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 Lite will never
38110 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20 exhibit.** such
38120 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20 behavior in its
38130 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78 own use of mutex
38140 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73 es..**.** Some s
38150 79 73 74 65 6d 73 20 28 66 6f 72 20 65 78 61 6d ystems (for exam
38160 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20 39 35 29 ple, Windows 95)
38170 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
38180 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a the operation.**
38190 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
381a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
381b0 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 y(). On those s
381c0 79 73 74 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f ystems, sqlite3_
381d0 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a 2a 20 77 mutex_try().** w
381e0 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 ill always retur
381f0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 n SQLITE_BUSY.
38200 7b 48 31 37 30 33 30 7d 20 54 68 65 20 53 51 4c {H17030} The SQL
38210 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76 ite core only ev
38220 65 72 20 75 73 65 73 0a 2a 2a 20 73 71 6c 69 74 er uses.** sqlit
38230 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61 e3_mutex_try() a
38240 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
38250 6e 20 73 6f 20 74 68 69 73 20 69 73 20 61 63 63 n so this is acc
38260 65 70 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72 eptable behavior
38270 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 33 31 7d ..**.** {H17031}
38280 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
38290 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
382a0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
382b0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
382c0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
382d0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
382e0 61 64 2e 20 20 7b 41 31 37 30 33 32 7d 20 54 68 ad. {A17032} Th
382f0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 e behavior.** is
38300 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
38310 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 e mutex is not c
38320 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
38330 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 by the.** calli
38340 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69 73 20 ng thread or is
38350 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c not currently al
38360 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31 37 30 33 located. {H1703
38370 33 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 3} SQLite will.*
38380 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 * never do eithe
38390 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 r. {END}.**.** I
383a0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 f the argument t
383b0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f o sqlite3_mutex_
383c0 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33 enter(), sqlite3
383d0 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72 _mutex_try(), or
383e0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
383f0 78 5f 6c 65 61 76 65 28 29 20 69 73 20 61 20 4e x_leave() is a N
38400 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 ULL pointer, the
38410 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 6f 75 74 n all three rout
38420 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76 65 20 61 ines.** behave a
38430 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 s no-ops..**.**
38440 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
38450 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d e3_mutex_held()]
38460 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75 and [sqlite3_mu
38470 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 2e 0a tex_notheld()]..
38480 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
38490 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c lite3_mutex *sql
384a0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
384b0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 (int);.SQLITE_AP
384c0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d I void sqlite3_m
384d0 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 utex_free(sqlite
384e0 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 3_mutex*);.SQLIT
384f0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
38500 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 e3_mutex_enter(s
38510 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a qlite3_mutex*);.
38520 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
38530 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
38540 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 (sqlite3_mutex*)
38550 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
38560 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
38570 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 leave(sqlite3_mu
38580 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tex*);../*.** CA
38590 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 4d 65 PI3REF: Mutex Me
385a0 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 thods Object {H1
385b0 37 31 32 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 7120} <S20130>.*
385c0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
385d0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
385e0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
385f0 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c re defines the l
38600 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 ow-level routine
38610 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c s.** used to all
38620 6f 63 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75 ocate and use mu
38630 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 73 75 texes..**.** Usu
38640 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c ally, the defaul
38650 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e t mutex implemen
38660 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64 tations provided
38670 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 0a 2a by SQLite are.*
38680 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20 68 6f * sufficient, ho
38690 77 65 76 65 72 20 74 68 65 20 75 73 65 72 20 68 wever the user h
386a0 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f 66 as the option of
386b0 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61 20 substituting a
386c0 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c 65 6d custom.** implem
386d0 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73 70 65 entation for spe
386e0 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f 79 6d cialized deploym
386f0 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d 73 20 ents or systems
38700 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69 74 65 for which SQLite
38710 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f .** does not pro
38720 76 69 64 65 20 61 20 73 75 69 74 61 62 6c 65 20 vide a suitable
38730 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 implementation.
38740 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 In this case, th
38750 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 61 74 65 e user.** create
38760 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 73 20 s and populates
38770 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
38780 68 69 73 20 73 74 72 75 63 74 75 72 65 20 74 6f his structure to
38790 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 pass.** to sqli
387a0 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 61 6c 6f te3_config() alo
387b0 6e 67 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c ng with the [SQL
387c0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
387d0 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 64 64 ] option..** Add
387e0 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 69 6e itionally, an in
387f0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
38800 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 tructure can be
38810 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a 20 6f 75 used as an.** ou
38820 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 77 68 tput variable wh
38830 65 6e 20 71 75 65 72 79 69 6e 67 20 74 68 65 20 en querying the
38840 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 63 system for the c
38850 75 72 72 65 6e 74 20 6d 75 74 65 78 0a 2a 2a 20 urrent mutex.**
38860 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
38870 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 using the [SQLIT
38880 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 E_CONFIG_GETMUTE
38890 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a X] option..**.**
388a0 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 The xMutexInit
388b0 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62 method defined b
388c0 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
388d0 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a is invoked as.*
388e0 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d * part of system
388f0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
38900 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 by the sqlite3_i
38910 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 75 6e 63 nitialize() func
38920 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 30 30 31 tion..** {H17001
38930 7d 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 } The xMutexInit
38940 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 62 routine shall b
38950 65 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 e called by SQLi
38960 74 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 te once for each
38970 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 20 63 61 .** effective ca
38980 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 ll to [sqlite3_i
38990 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a nitialize()]..**
389a0 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 45 6e .** The xMutexEn
389b0 64 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 d method defined
389c0 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75 by this structu
389d0 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 re is invoked as
389e0 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 .** part of syst
389f0 65 6d 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74 em shutdown by t
38a00 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 he sqlite3_shutd
38a10 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 own() function.
38a20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 The.** implement
38a30 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 ation of this me
38a40 74 68 6f 64 20 69 73 20 65 78 70 65 63 74 65 64 thod is expected
38a50 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 to release all
38a60 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72 outstanding.** r
38a70 65 73 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65 esources obtaine
38a80 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d d by the mutex m
38a90 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74 ethods implement
38aa0 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c ation, especiall
38ab0 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69 y.** those obtai
38ac0 6e 65 64 20 62 79 20 74 68 65 20 78 4d 75 74 65 ned by the xMute
38ad0 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e 20 7b 48 xInit method. {H
38ae0 31 37 30 30 33 7d 20 54 68 65 20 78 4d 75 74 65 17003} The xMute
38af0 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66 xEnd().** interf
38b00 61 63 65 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 ace shall be inv
38b10 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 oked once for ea
38b20 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ch call to [sqli
38b30 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e te3_shutdown()].
38b40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 .**.** The remai
38b50 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f ning seven metho
38b60 64 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 ds defined by th
38b70 69 73 20 73 74 72 75 63 74 75 72 65 20 28 78 4d is structure (xM
38b80 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d utexAlloc,.** xM
38b90 75 74 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78 utexFree, xMutex
38ba0 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79 Enter, xMutexTry
38bb0 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78 , xMutexLeave, x
38bc0 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 0a 2a 2a MutexHeld and.**
38bd0 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 20 xMutexNotheld)
38be0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f implement the fo
38bf0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 llowing interfac
38c00 65 73 20 28 72 65 73 70 65 63 74 69 76 65 6c 79 es (respectively
38c10 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a ):.**.** <ul>.**
38c20 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 <li> [sqlite
38c30 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 3_mutex_alloc()]
38c40 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e </li>.** <li>
38c50 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 [sqlite3_mutex
38c60 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a _free()] </li>.*
38c70 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 * <li> [sqlit
38c80 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
38c90 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 ] </li>.** <li
38ca0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 > [sqlite3_mute
38cb0 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69 3e 0a 2a x_try()] </li>.*
38cc0 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 * <li> [sqlit
38cd0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
38ce0 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 ] </li>.** <li
38cf0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 > [sqlite3_mute
38d00 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a x_held()] </li>.
38d10 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 ** <li> [sqli
38d20 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
38d30 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f d()] </li>.** </
38d40 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e ul>.**.** The on
38d50 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 ly difference is
38d60 20 74 68 61 74 20 74 68 65 20 70 75 62 6c 69 63 that the public
38d70 20 73 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e sqlite3_XXX fun
38d80 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65 ctions enumerate
38d90 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e d.** above silen
38da0 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69 tly ignore any i
38db0 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 nvocations that
38dc0 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e pass a NULL poin
38dd0 74 65 72 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f ter instead.** o
38de0 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 f a valid mutex
38df0 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c handle. The impl
38e00 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 ementations of t
38e10 68 65 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e he methods defin
38e20 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 73 74 ed.** by this st
38e30 72 75 63 74 75 72 65 20 61 72 65 20 6e 6f 74 20 ructure are not
38e40 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 required to hand
38e50 6c 65 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 le this case, th
38e60 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20 e results.** of
38e70 70 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 passing a NULL p
38e80 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 20 6f ointer instead o
38e90 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 f a valid mutex
38ea0 68 61 6e 64 6c 65 20 61 72 65 20 75 6e 64 65 66 handle are undef
38eb0 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74 ined.** (i.e. it
38ec0 20 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 74 is acceptable t
38ed0 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 69 6d 70 o provide an imp
38ee0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 lementation that
38ef0 20 73 65 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a segfaults if.**
38f00 20 69 74 20 69 73 20 70 61 73 73 65 64 20 61 20 it is passed a
38f10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a NULL pointer)..*
38f20 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
38f30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
38f40 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d ethods sqlite3_m
38f50 75 74 65 78 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 utex_methods;.st
38f60 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 ruct sqlite3_mut
38f70 65 78 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 ex_methods {. i
38f80 6e 74 20 28 2a 78 4d 75 74 65 78 49 6e 69 74 29 nt (*xMutexInit)
38f90 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a (void);. int (*
38fa0 78 4d 75 74 65 78 45 6e 64 29 28 76 6f 69 64 29 xMutexEnd)(void)
38fb0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
38fc0 78 20 2a 28 2a 78 4d 75 74 65 78 41 6c 6c 6f 63 x *(*xMutexAlloc
38fd0 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 )(int);. void (
38fe0 2a 78 4d 75 74 65 78 46 72 65 65 29 28 73 71 6c *xMutexFree)(sql
38ff0 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 ite3_mutex *);.
39000 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 45 6e void (*xMutexEn
39010 74 65 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 ter)(sqlite3_mut
39020 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 ex *);. int (*x
39030 4d 75 74 65 78 54 72 79 29 28 73 71 6c 69 74 65 MutexTry)(sqlite
39040 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 3_mutex *);. vo
39050 69 64 20 28 2a 78 4d 75 74 65 78 4c 65 61 76 65 id (*xMutexLeave
39060 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 )(sqlite3_mutex
39070 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 *);. int (*xMut
39080 65 78 48 65 6c 64 29 28 73 71 6c 69 74 65 33 5f exHeld)(sqlite3_
39090 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 mutex *);. int
390a0 28 2a 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 (*xMutexNotheld)
390b0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
390c0 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 );.};../*.** CAP
390d0 49 33 52 45 46 3a 20 4d 75 74 65 78 20 56 65 72 I3REF: Mutex Ver
390e0 69 66 69 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e ification Routin
390f0 65 73 20 7b 48 31 37 30 38 30 7d 20 3c 53 32 30 es {H17080} <S20
39100 31 33 30 3e 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 130> <S30800>.**
39110 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
39120 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 mutex_held() and
39130 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
39140 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 otheld() routine
39150 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 s.** are intende
39160 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
39170 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
39180 65 6e 74 73 2e 20 7b 48 31 37 30 38 31 7d 20 54 ents. {H17081} T
39190 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a he SQLite core.*
391a0 2a 20 6e 65 76 65 72 20 75 73 65 73 20 74 68 65 * never uses the
391b0 73 65 20 72 6f 75 74 69 6e 65 73 20 65 78 63 65 se routines exce
391c0 70 74 20 69 6e 73 69 64 65 20 61 6e 20 61 73 73 pt inside an ass
391d0 65 72 74 28 29 20 61 6e 64 20 61 70 70 6c 69 63 ert() and applic
391e0 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 64 ations.** are ad
391f0 76 69 73 65 64 20 74 6f 20 66 6f 6c 6c 6f 77 20 vised to follow
39200 74 68 65 20 6c 65 61 64 20 6f 66 20 74 68 65 20 the lead of the
39210 63 6f 72 65 2e 20 20 7b 48 31 37 30 38 32 7d 20 core. {H17082}
39220 54 68 65 20 63 6f 72 65 20 6f 6e 6c 79 0a 2a 2a The core only.**
39230 20 70 72 6f 76 69 64 65 73 20 69 6d 70 6c 65 6d provides implem
39240 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 entations for th
39250 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 68 65 ese routines whe
39260 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 n it is compiled
39270 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c .** with the SQL
39280 49 54 45 5f 44 45 42 55 47 20 66 6c 61 67 2e 20 ITE_DEBUG flag.
39290 20 7b 41 31 37 30 38 37 7d 20 45 78 74 65 72 6e {A17087} Extern
392a0 61 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 al mutex impleme
392b0 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 ntations.** are
392c0 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f only required to
392d0 20 70 72 6f 76 69 64 65 20 74 68 65 73 65 20 72 provide these r
392e0 6f 75 74 69 6e 65 73 20 69 66 20 53 51 4c 49 54 outines if SQLIT
392f0 45 5f 44 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 E_DEBUG is.** de
39300 66 69 6e 65 64 20 61 6e 64 20 69 66 20 4e 44 45 fined and if NDE
39310 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e BUG is not defin
39320 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 ed..**.** {H1708
39330 33 7d 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 3} These routine
39340 73 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 s should return
39350 74 72 75 65 20 69 66 20 74 68 65 20 6d 75 74 65 true if the mute
39360 78 20 69 6e 20 74 68 65 69 72 20 61 72 67 75 6d x in their argum
39370 65 6e 74 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f ent.** is held o
39380 72 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 73 70 r not held, resp
39390 65 63 74 69 76 65 6c 79 2c 20 62 79 20 74 68 65 ectively, by the
393a0 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 2e calling thread.
393b0 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30 38 34 7d 20 .**.** {X17084}
393c0 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 The implementati
393d0 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 on is not requir
393e0 65 64 20 74 6f 20 70 72 6f 76 69 64 65 64 20 76 ed to provided v
393f0 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 ersions of these
39400 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61 .** routines tha
39410 74 20 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2e t actually work.
39420 20 49 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e If the implemen
39430 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
39440 70 72 6f 76 69 64 65 20 77 6f 72 6b 69 6e 67 0a provide working.
39450 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 ** versions of t
39460 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 69 hese routines, i
39470 74 20 73 68 6f 75 6c 64 20 61 74 20 6c 65 61 73 t should at leas
39480 74 20 70 72 6f 76 69 64 65 20 73 74 75 62 73 20 t provide stubs
39490 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 72 that always.** r
394a0 65 74 75 72 6e 20 74 72 75 65 20 73 6f 20 74 68 eturn true so th
394b0 61 74 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 20 at one does not
394c0 67 65 74 20 73 70 75 72 69 6f 75 73 20 61 73 73 get spurious ass
394d0 65 72 74 69 6f 6e 20 66 61 69 6c 75 72 65 73 2e ertion failures.
394e0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 35 7d 20 .**.** {H17085}
394f0 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 If the argument
39500 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
39510 5f 68 65 6c 64 28 29 20 69 73 20 61 20 4e 55 4c _held() is a NUL
39520 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 0a 2a L pointer then.*
39530 2a 20 74 68 65 20 72 6f 75 74 69 6e 65 20 73 68 * the routine sh
39540 6f 75 6c 64 20 72 65 74 75 72 6e 20 31 2e 20 20 ould return 1.
39550 7b 45 4e 44 7d 20 54 68 69 73 20 73 65 65 6d 73 {END} This seems
39560 20 63 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69 counter-intuiti
39570 76 65 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61 ve since.** clea
39580 72 6c 79 20 74 68 65 20 6d 75 74 65 78 20 63 61 rly the mutex ca
39590 6e 6e 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20 nnot be held if
395a0 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 it does not exis
395b0 74 2e 20 20 42 75 74 20 74 68 65 0a 2a 2a 20 74 t. But the.** t
395c0 68 65 20 72 65 61 73 6f 6e 20 74 68 65 20 6d 75 he reason the mu
395d0 74 65 78 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 tex does not exi
395e0 73 74 20 69 73 20 62 65 63 61 75 73 65 20 74 68 st is because th
395f0 65 20 62 75 69 6c 64 20 69 73 20 6e 6f 74 0a 2a e build is not.*
39600 2a 20 75 73 69 6e 67 20 6d 75 74 65 78 65 73 2e * using mutexes.
39610 20 20 41 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 And we do not
39620 77 61 6e 74 20 74 68 65 20 61 73 73 65 72 74 28 want the assert(
39630 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 ) containing the
39640 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 .** call to sqli
39650 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
39660 20 74 6f 20 66 61 69 6c 2c 20 73 6f 20 61 20 6e to fail, so a n
39670 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 69 on-zero return i
39680 73 0a 2a 2a 20 74 68 65 20 61 70 70 72 6f 70 72 s.** the appropr
39690 69 61 74 65 20 74 68 69 6e 67 20 74 6f 20 64 6f iate thing to do
396a0 2e 20 20 7b 48 31 37 30 38 36 7d 20 54 68 65 20 . {H17086} The
396b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
396c0 74 68 65 6c 64 28 29 0a 2a 2a 20 69 6e 74 65 72 theld().** inter
396d0 66 61 63 65 20 73 68 6f 75 6c 64 20 61 6c 73 6f face should also
396e0 20 72 65 74 75 72 6e 20 31 20 77 68 65 6e 20 67 return 1 when g
396f0 69 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e iven a NULL poin
39700 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ter..*/.SQLITE_A
39710 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d PI int sqlite3_m
39720 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 utex_held(sqlite
39730 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 3_mutex*);.SQLIT
39740 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
39750 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
39760 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b sqlite3_mutex*);
39770 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
39780 3a 20 4d 75 74 65 78 20 54 79 70 65 73 20 7b 48 : Mutex Types {H
39790 31 37 30 30 31 7d 20 3c 48 31 37 30 30 30 3e 0a 17001} <H17000>.
397a0 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 **.** The [sqlit
397b0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
397c0 5d 20 69 6e 74 65 72 66 61 63 65 20 74 61 6b 65 ] interface take
397d0 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d s a single argum
397e0 65 6e 74 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 ent.** which is
397f0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 one of these int
39800 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 2e 0a eger constants..
39810 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 **.** The set of
39820 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 static mutexes
39830 6d 61 79 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 may change from
39840 6f 6e 65 20 53 51 4c 69 74 65 20 72 65 6c 65 61 one SQLite relea
39850 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 se to the.** nex
39860 74 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 t. Applications
39870 20 74 68 61 74 20 6f 76 65 72 72 69 64 65 20 74 that override t
39880 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 75 74 65 he built-in mute
39890 78 20 6c 6f 67 69 63 20 6d 75 73 74 20 62 65 0a x logic must be.
398a0 2a 2a 20 70 72 65 70 61 72 65 64 20 74 6f 20 61 ** prepared to a
398b0 63 63 6f 6d 6d 6f 64 61 74 65 20 61 64 64 69 74 ccommodate addit
398c0 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 ional static mut
398d0 65 78 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 exes..*/.#define
398e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
398f0 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 30 ST 0
39900 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
39910 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
39920 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
39930 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
39940 41 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 32 ATIC_MASTER 2
39950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
39960 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
39970 20 20 20 20 20 20 20 33 20 20 2f 2a 20 73 71 6c 3 /* sql
39980 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f ite3_malloc() */
39990 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
399a0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
399b0 32 20 20 20 20 20 20 34 20 20 2f 2a 20 4e 4f 54 2 4 /* NOT
399c0 20 55 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 USED */.#define
399d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
399e0 41 54 49 43 5f 4f 50 45 4e 20 20 20 20 20 20 34 ATIC_OPEN 4
399f0 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 /* sqlite3Btre
39a00 65 4f 70 65 6e 28 29 20 2a 2f 0a 23 64 65 66 69 eOpen() */.#defi
39a10 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
39a20 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 STATIC_PRNG
39a30 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 5 /* sqlite3_r
39a40 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 69 andom() */.#defi
39a50 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
39a60 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20 STATIC_LRU
39a70 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6 /* lru page
39a80 6c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 list */.#define
39a90 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
39aa0 54 49 43 5f 4c 52 55 32 20 20 20 20 20 20 37 20 TIC_LRU2 7
39ab0 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73 /* lru page lis
39ac0 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t */../*.** CAPI
39ad0 33 52 45 46 3a 20 52 65 74 72 69 65 76 65 20 74 3REF: Retrieve t
39ae0 68 65 20 6d 75 74 65 78 20 66 6f 72 20 61 20 64 he mutex for a d
39af0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
39b00 6f 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48 31 37 on {H17002} <H17
39b10 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 000>.**.** This
39b20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
39b30 73 20 61 20 70 6f 69 6e 74 65 72 20 74 68 65 20 s a pointer the
39b40 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 [sqlite3_mutex]
39b50 6f 62 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a 20 object that .**
39b60 73 65 72 69 61 6c 69 7a 65 73 20 61 63 63 65 73 serializes acces
39b70 73 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 s to the [databa
39b80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 67 se connection] g
39b90 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 iven in the argu
39ba0 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68 65 ment.** when the
39bb0 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
39bc0 5d 20 69 73 20 53 65 72 69 61 6c 69 7a 65 64 2e ] is Serialized.
39bd0 0a 2a 2a 20 49 66 20 74 68 65 20 5b 74 68 72 65 .** If the [thre
39be0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 ading mode] is S
39bf0 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f 72 20 ingle-thread or
39c00 4d 75 6c 74 69 2d 74 68 72 65 61 64 20 74 68 65 Multi-thread the
39c10 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e n this.** routin
39c20 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c e returns a NULL
39c30 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c pointer..*/.SQL
39c40 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
39c50 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 mutex *sqlite3_d
39c60 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 2a b_mutex(sqlite3*
39c70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
39c80 45 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f EF: Low-Level Co
39c90 6e 74 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 73 ntrol Of Databas
39ca0 65 20 46 69 6c 65 73 20 7b 48 31 31 33 30 30 7d e Files {H11300}
39cb0 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S30800>.**.**
39cc0 7b 48 31 31 33 30 31 7d 20 54 68 65 20 5b 73 71 {H11301} The [sq
39cd0 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 lite3_file_contr
39ce0 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ol()] interface
39cf0 6d 61 6b 65 73 20 61 20 64 69 72 65 63 74 20 63 makes a direct c
39d00 61 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 all to the.** xF
39d10 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f ileControl metho
39d20 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 d for the [sqlit
39d30 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f e3_io_methods] o
39d40 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 bject associated
39d50 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 .** with a parti
39d60 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 69 cular database i
39d70 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 dentified by the
39d80 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
39d90 2e 20 7b 48 31 31 33 30 32 7d 20 54 68 65 0a 2a . {H11302} The.*
39da0 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 * name of the da
39db0 74 61 62 61 73 65 20 69 73 20 74 68 65 20 6e 61 tabase is the na
39dc0 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 me assigned to t
39dd0 68 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74 he database by t
39de0 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 6c he.** <a href="l
39df0 61 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c 22 ang_attach.html"
39e00 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c 20 >ATTACH</a> SQL
39e10 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70 65 command that ope
39e20 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 ned the.** datab
39e30 61 73 65 2e 20 7b 48 31 31 33 30 33 7d 20 54 6f ase. {H11303} To
39e40 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61 69 control the mai
39e50 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c n database file,
39e60 20 75 73 65 20 74 68 65 20 6e 61 6d 65 20 22 6d use the name "m
39e70 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55 4c ain".** or a NUL
39e80 4c 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31 33 L pointer. {H113
39e90 30 34 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 04} The third an
39ea0 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 d fourth paramet
39eb0 65 72 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 ers to this rout
39ec0 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73 73 65 ine.** are passe
39ed0 64 20 64 69 72 65 63 74 6c 79 20 74 68 72 6f 75 d directly throu
39ee0 67 68 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 gh to the second
39ef0 20 61 6e 64 20 74 68 69 72 64 20 70 61 72 61 6d and third param
39f00 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65 20 eters of.** the
39f10 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 xFileControl met
39f20 68 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d 20 54 hod. {H11305} T
39f30 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
39f40 6f 66 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 of the xFileCont
39f50 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 rol.** method be
39f60 63 6f 6d 65 73 20 74 68 65 20 72 65 74 75 72 6e comes the return
39f70 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 value of this r
39f80 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 outine..**.** {H
39f90 31 31 33 30 36 7d 20 49 66 20 74 68 65 20 73 65 11306} If the se
39fa0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 cond parameter (
39fb0 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e 6f zDbName) does no
39fc0 74 20 6d 61 74 63 68 20 74 68 65 20 6e 61 6d 65 t match the name
39fd0 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e 20 of any.** open
39fe0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
39ff0 68 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 hen SQLITE_ERROR
3a000 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 7b 48 is returned. {H
3a010 31 31 33 30 37 7d 20 54 68 69 73 20 65 72 72 6f 11307} This erro
3a020 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f 74 r.** code is not
3a030 20 72 65 6d 65 6d 62 65 72 65 64 20 61 6e 64 20 remembered and
3a040 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 61 will not be reca
3a050 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 lled by [sqlite3
3a060 5f 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20 6f _errcode()].** o
3a070 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 r [sqlite3_errms
3a080 67 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20 54 g()]. {A11308} T
3a090 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 46 he underlying xF
3a0a0 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f ileControl metho
3a0b0 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 d might.** also
3a0c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
3a0d0 52 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d 20 54 ROR. {A11309} T
3a0e0 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 here is no way t
3a0f0 6f 20 64 69 73 74 69 6e 67 75 69 73 68 20 62 65 o distinguish be
3a100 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63 6f tween.** an inco
3a110 72 72 65 63 74 20 7a 44 62 4e 61 6d 65 20 61 6e rrect zDbName an
3a120 64 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f d an SQLITE_ERRO
3a130 52 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 R return from th
3a140 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 e underlying.**
3a150 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 xFileControl met
3a160 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a hod. {END}.**.**
3a170 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 See also: [SQLI
3a180 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 TE_FCNTL_LOCKSTA
3a190 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 TE].*/.SQLITE_AP
3a1a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 I int sqlite3_fi
3a1b0 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 le_control(sqlit
3a1c0 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 e3*, const char
3a1d0 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 *zDbName, int op
3a1e0 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a , void*);../*.**
3a1f0 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 CAPI3REF: Testi
3a200 6e 67 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 ng Interface {H1
3a210 31 34 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 1400} <S30800>.*
3a220 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
3a230 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 _test_control()
3a240 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 interface is use
3a250 64 20 74 6f 20 72 65 61 64 20 6f 75 74 20 69 6e d to read out in
3a260 74 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65 20 ternal.** state
3a270 6f 66 20 53 51 4c 69 74 65 20 61 6e 64 20 74 6f of SQLite and to
3a280 20 69 6e 6a 65 63 74 20 66 61 75 6c 74 73 20 69 inject faults i
3a290 6e 74 6f 20 53 51 4c 69 74 65 20 66 6f 72 20 74 nto SQLite for t
3a2a0 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 esting.** purpos
3a2b0 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 es. The first p
3a2c0 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 6f arameter is an o
3a2d0 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 68 peration code th
3a2e0 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a at determines.**
3a2f0 20 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 the number, mea
3a300 6e 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 ning, and operat
3a310 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 ion of all subse
3a320 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 quent parameters
3a330 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
3a340 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 66 6f erface is not fo
3a350 72 20 75 73 65 20 62 79 20 61 70 70 6c 69 63 61 r use by applica
3a360 74 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 74 tions. It exist
3a370 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20 s solely.** for
3a380 76 65 72 69 66 79 69 6e 67 20 74 68 65 20 63 6f verifying the co
3a390 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 rrect operation
3a3a0 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 of the SQLite li
3a3b0 62 72 61 72 79 2e 20 20 44 65 70 65 6e 64 69 6e brary. Dependin
3a3c0 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20 g.** on how the
3a3d0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69 SQLite library i
3a3e0 73 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73 s compiled, this
3a3f0 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 interface might
3a400 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a not exist..**.*
3a410 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 * The details of
3a420 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 63 the operation c
3a430 6f 64 65 73 2c 20 74 68 65 69 72 20 6d 65 61 6e odes, their mean
3a440 69 6e 67 73 2c 20 74 68 65 20 70 61 72 61 6d 65 ings, the parame
3a450 74 65 72 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b ters.** they tak
3a460 65 2c 20 61 6e 64 20 77 68 61 74 20 74 68 65 79 e, and what they
3a470 20 64 6f 20 61 72 65 20 61 6c 6c 20 73 75 62 6a do are all subj
3a480 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 ect to change wi
3a490 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a thout notice..**
3a4a0 20 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20 Unlike most of
3a4b0 74 68 65 20 53 51 4c 69 74 65 20 41 50 49 2c 20 the SQLite API,
3a4c0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
3a4d0 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 20 not guaranteed
3a4e0 74 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f to.** operate co
3a4f0 6e 73 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20 nsistently from
3a500 6f 6e 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 one release to t
3a510 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 he next..*/.SQLI
3a520 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
3a530 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 e3_test_control(
3a540 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f int op, ...);../
3a550 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 *.** CAPI3REF: T
3a560 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 esting Interface
3a570 20 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64 65 73 Operation Codes
3a580 20 7b 48 31 31 34 31 30 7d 20 3c 48 31 31 34 30 {H11410} <H1140
3a590 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 0>.**.** These c
3a5a0 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 onstants are the
3a5b0 20 76 61 6c 69 64 20 6f 70 65 72 61 74 69 6f 6e valid operation
3a5c0 20 63 6f 64 65 20 70 61 72 61 6d 65 74 65 72 73 code parameters
3a5d0 20 75 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 used.** as the
3a5e0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
3a5f0 6f 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f o [sqlite3_test_
3a600 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a control()]..**.*
3a610 2a 20 54 68 65 73 65 20 70 61 72 61 6d 65 74 65 * These paramete
3a620 72 73 20 61 6e 64 20 74 68 65 69 72 20 6d 65 61 rs and their mea
3a630 6e 69 6e 67 73 20 61 72 65 20 73 75 62 6a 65 63 nings are subjec
3a640 74 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 77 t to change.** w
3a650 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20 ithout notice.
3a660 54 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65 These values are
3a670 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 for testing pur
3a680 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 poses only..** A
3a690 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 pplications shou
3a6a0 6c 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f ld not use any o
3a6b0 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 f these paramete
3a6c0 72 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 71 rs or the.** [sq
3a6d0 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 lite3_test_contr
3a6e0 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e ol()] interface.
3a6f0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
3a700 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 TE_TESTCTRL_PRNG
3a710 5f 53 41 56 45 20 20 20 20 20 20 20 20 20 20 20 _SAVE
3a720 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 5.#define S
3a730 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 QLITE_TESTCTRL_P
3a740 52 4e 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20 RNG_RESTORE
3a750 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 6.#defin
3a760 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 e SQLITE_TESTCTR
3a770 4c 5f 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20 L_PRNG_RESET
3a780 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 7.#de
3a790 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 fine SQLITE_TEST
3a7a0 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 CTRL_BITVEC_TEST
3a7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 8.
3a7c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 #define SQLITE_T
3a7d0 45 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e ESTCTRL_FAULT_IN
3a7e0 53 54 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 STALL
3a7f0 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 9.#define SQLIT
3a800 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 E_TESTCTRL_BENIG
3a810 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 N_MALLOC_HOOKS
3a820 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51 10.#define SQ
3a830 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 LITE_TESTCTRL_PE
3a840 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 NDING_BYTE
3a850 20 20 20 20 20 20 31 31 0a 0a 2f 2a 0a 2a 2a 20 11../*.**
3a860 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 CAPI3REF: SQLite
3a870 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 Runtime Status
3a880 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 {H17200} <S60200
3a890 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
3a8a0 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 L.**.** This int
3a8b0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 erface is used t
3a8c0 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 o retrieve runti
3a8d0 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d me status inform
3a8e0 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 ation.** about t
3a8f0 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f he preformance o
3a900 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 f SQLite, and op
3a910 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 tionally to rese
3a920 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 t various.** hig
3a930 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 hwater marks. T
3a940 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
3a950 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 t is an integer
3a960 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 code for.** the
3a970 73 70 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 specific paramet
3a980 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 er to measure.
3a990 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 Recognized integ
3a9a0 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 er codes.** are
3a9b0 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c of the form [SQL
3a9c0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
3a9d0 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f Y_USED | SQLITE_
3a9e0 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 STATUS_...]..**
3a9f0 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 The current valu
3aa00 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 e of the paramet
3aa10 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 er is returned i
3aa20 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a nto *pCurrent..*
3aa30 2a 20 54 68 65 20 68 69 67 68 65 73 74 20 72 65 * The highest re
3aa40 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 corded value is
3aa50 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 returned in *pHi
3aa60 67 68 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 ghwater. If the
3aa70 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 .** resetFlag is
3aa80 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 true, then the
3aa90 68 69 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 highest record v
3aaa0 61 6c 75 65 20 69 73 20 72 65 73 65 74 20 61 66 alue is reset af
3aab0 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 ter.** *pHighwat
3aac0 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 er is written. S
3aad0 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 ome parameters d
3aae0 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 o not record the
3aaf0 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 highest.** valu
3ab00 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 e. For those pa
3ab10 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 rameters.** noth
3ab20 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 ing is written i
3ab30 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 nto *pHighwater
3ab40 61 6e 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 and the resetFla
3ab50 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a g is ignored..**
3ab60 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 Other parameter
3ab70 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 s record only th
3ab80 65 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b e highwater mark
3ab90 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 and not the cur
3aba0 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 rent.** value.
3abb0 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 For these latter
3abc0 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 parameters noth
3abd0 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 ing is written i
3abe0 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a nto *pCurrent..*
3abf0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
3ac00 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
3ac10 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 _OK on success a
3ac20 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a nd a non-zero.**
3ac30 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e [error code] on
3ac40 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 failure..**.**
3ac50 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
3ac60 74 68 72 65 61 64 73 61 66 65 20 62 75 74 20 69 threadsafe but i
3ac70 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 s not atomic. T
3ac80 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a his routine can.
3ac90 2a 2a 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 ** called while
3aca0 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 72 other threads ar
3acb0 65 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61 e running the sa
3acc0 6d 65 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20 me or different
3acd0 53 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66 SQLite.** interf
3ace0 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74 aces. However t
3acf0 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e he values return
3ad00 65 64 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20 ed in *pCurrent
3ad10 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 and.** *pHighwat
3ad20 65 72 20 72 65 66 6c 65 63 74 20 74 68 65 20 73 er reflect the s
3ad30 74 61 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20 tatus of SQLite
3ad40 61 74 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69 at different poi
3ad50 6e 74 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61 nts in time.** a
3ad60 6e 64 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c nd it is possibl
3ad70 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 e that another t
3ad80 68 72 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e hread might chan
3ad90 67 65 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 ge the parameter
3ada0 0a 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74 .** in between t
3adb0 68 65 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70 he times when *p
3adc0 43 75 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69 Current and *pHi
3add0 67 68 77 61 74 65 72 20 61 72 65 20 77 72 69 74 ghwater are writ
3ade0 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ten..**.** See a
3adf0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62 lso: [sqlite3_db
3ae00 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51 _status()].*/.SQ
3ae10 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
3ae20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 EXPERIMENTAL int
3ae30 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 sqlite3_status(
3ae40 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 int op, int *pCu
3ae50 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 rrent, int *pHig
3ae60 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 hwater, int rese
3ae70 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 tFlag);.../*.**
3ae80 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 CAPI3REF: Status
3ae90 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37 Parameters {H17
3aea0 32 35 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a 250} <H17200>.**
3aeb0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
3aec0 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 .** These intege
3aed0 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 r constants desi
3aee0 67 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 gnate various ru
3aef0 6e 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 n-time status pa
3af00 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 rameters.** that
3af10 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 can be returned
3af20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 by [sqlite3_sta
3af30 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 tus()]..**.** <d
3af40 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 l>.** <dt>SQLITE
3af50 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
3af60 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e SED</dt>.** <dd>
3af70 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 This parameter i
3af80 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d s the current am
3af90 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 ount of memory c
3afa0 68 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 hecked out.** us
3afb0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c ing [sqlite3_mal
3afc0 6c 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 loc()], either d
3afd0 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 irectly or indir
3afe0 65 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 ectly. The.** f
3aff0 69 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 igure includes c
3b000 61 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 alls made to [sq
3b010 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 lite3_malloc()]
3b020 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 by the applicati
3b030 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e on.** and intern
3b040 61 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 al memory usage
3b050 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 by the SQLite li
3b060 62 72 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 brary. Scratch
3b070 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f memory.** contro
3b080 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f lled by [SQLITE_
3b090 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 CONFIG_SCRATCH]
3b0a0 61 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 and auxiliary pa
3b0b0 67 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f ge-cache.** memo
3b0c0 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 ry controlled by
3b0d0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
3b0e0 50 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f PAGECACHE] is no
3b0f0 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a t included in.**
3b100 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e this parameter.
3b110 20 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 The amount ret
3b120 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d urned is the sum
3b130 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 of the allocati
3b140 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 on.** sizes as r
3b150 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 eported by the x
3b160 53 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b Size method in [
3b170 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
3b180 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ods].</dd>.**.**
3b190 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 <dt>SQLITE_STAT
3b1a0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f US_MALLOC_SIZE</
3b1b0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
3b1c0 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 parameter record
3b1d0 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 s the largest me
3b1e0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
3b1f0 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 request.** hande
3b200 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 d to [sqlite3_ma
3b210 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 lloc()] or [sqli
3b220 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28 te3_realloc()] (
3b230 6f 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65 or their.** inte
3b240 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73 rnal equivalents
3b250 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c ). Only the val
3b260 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 ue returned in t
3b270 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 he.** *pHighwate
3b280 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b r parameter to [
3b290 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 sqlite3_status()
3b2a0 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 ] is of interest
3b2b0 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 . .** The value
3b2c0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
3b2d0 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 e *pCurrent para
3b2e0 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e meter is undefin
3b2f0 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ed.</dd>.**.** <
3b300 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
3b310 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c _PAGECACHE_USED<
3b320 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3b330 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 parameter retur
3b340 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
3b350 20 70 61 67 65 73 20 75 73 65 64 20 6f 75 74 20 pages used out
3b360 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63 of the.** [pagec
3b370 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ache memory allo
3b380 63 61 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20 cator] that was
3b390 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 configured using
3b3a0 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e .** [SQLITE_CON
3b3b0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20 FIG_PAGECACHE].
3b3c0 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65 The.** value re
3b3d0 74 75 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67 turned is in pag
3b3e0 65 73 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73 es, not in bytes
3b3f0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
3b400 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 >SQLITE_STATUS_P
3b410 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f AGECACHE_OVERFLO
3b420 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 W</dt>.** <dd>Th
3b430 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 is parameter ret
3b440 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
3b450 6f 66 20 62 79 74 65 73 20 6f 66 20 70 61 67 65 of bytes of page
3b460 20 63 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 cache.** alloca
3b470 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 tion which could
3b480 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 not be statisfi
3b490 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 ed by the [SQLIT
3b4a0 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
3b4b0 48 45 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e HE].** buffer an
3b4c0 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 d where forced t
3b4d0 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 o overflow to [s
3b4e0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
3b4f0 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e . The.** return
3b500 65 64 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 ed value include
3b510 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 s allocations th
3b520 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 at overflowed be
3b530 63 61 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68 cause they.** wh
3b540 65 72 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74 ere too large (t
3b550 68 65 79 20 77 65 72 65 20 6c 61 72 67 65 72 20 hey were larger
3b560 74 68 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61 than the "sz" pa
3b570 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53 rameter to.** [S
3b580 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
3b590 45 43 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c ECACHE]) and all
3b5a0 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 ocations that ov
3b5b0 65 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 erflowed because
3b5c0 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73 .** no space was
3b5d0 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70 61 67 left in the pag
3b5e0 65 20 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a e cache.</dd>.**
3b5f0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3b600 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
3b610 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIZE</dt>.** <dd
3b620 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3b630 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 records the larg
3b640 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 est memory alloc
3b650 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a ation request.**
3b660 20 68 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65 handed to [page
3b670 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c cache memory all
3b680 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 ocator]. Only t
3b690 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
3b6a0 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 d in the.** *pHi
3b6b0 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 ghwater paramete
3b6c0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 r to [sqlite3_st
3b6d0 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e atus()] is of in
3b6e0 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 terest. .** The
3b6f0 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 value written i
3b700 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e nto the *pCurren
3b710 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 t parameter is u
3b720 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a ndefined.</dd>.*
3b730 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3b740 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 STATUS_SCRATCH_U
3b750 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e SED</dt>.** <dd>
3b760 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
3b770 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
3b780 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 r of allocations
3b790 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 used out of the
3b7a0 0a 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d .** [scratch mem
3b7b0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 ory allocator] c
3b7c0 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a onfigured using.
3b7d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ** [SQLITE_CONFI
3b7e0 47 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 G_SCRATCH]. The
3b7f0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
3b800 69 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e is in allocation
3b810 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 s, not.** in byt
3b820 65 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e es. Since a sin
3b830 67 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f gle thread may o
3b840 6e 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 nly have one scr
3b850 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a atch allocation.
3b860 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 ** outstanding a
3b870 74 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 t time, this par
3b880 61 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f ameter also repo
3b890 72 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rts the number o
3b8a0 66 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 f threads.** usi
3b8b0 6e 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 ng scratch memor
3b8c0 79 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 y at the same ti
3b8d0 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c me.</dd>.**.** <
3b8e0 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
3b8f0 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f _SCRATCH_OVERFLO
3b900 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 W</dt>.** <dd>Th
3b910 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 is parameter ret
3b920 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
3b930 6f 66 20 62 79 74 65 73 20 6f 66 20 73 63 72 61 of bytes of scra
3b940 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c tch memory.** al
3b950 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 location which c
3b960 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 ould not be stat
3b970 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 isfied by the [S
3b980 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 QLITE_CONFIG_SCR
3b990 41 54 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20 ATCH].** buffer
3b9a0 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 and where forced
3b9b0 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 to overflow to
3b9c0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
3b9d0 29 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a )]. The values.
3b9e0 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c ** returned incl
3b9f0 75 64 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 ude overflows be
3ba00 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65 73 cause the reques
3ba10 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 ted allocation w
3ba20 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72 as too.** larger
3ba30 20 28 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 (that is, becau
3ba40 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 se the requested
3ba50 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 allocation was
3ba60 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a larger than the.
3ba70 2a 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65 ** "sz" paramete
3ba80 72 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e r to [SQLITE_CON
3ba90 46 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e FIG_SCRATCH]) an
3baa0 64 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72 d because no scr
3bab0 61 74 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73 atch buffer.** s
3bac0 6c 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c 61 lots were availa
3bad0 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a ble..** </dd>.**
3bae0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3baf0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 TATUS_SCRATCH_SI
3bb00 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ZE</dt>.** <dd>T
3bb10 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
3bb20 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 cords the larges
3bb30 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
3bb40 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 ion request.** h
3bb50 61 6e 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63 anded to [scratc
3bb60 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 h memory allocat
3bb70 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 or]. Only the v
3bb80 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e alue returned in
3bb90 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 the.** *pHighwa
3bba0 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f ter parameter to
3bbb0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 [sqlite3_status
3bbc0 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 ()] is of intere
3bbd0 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c st. .** The val
3bbe0 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 ue written into
3bbf0 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 the *pCurrent pa
3bc00 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 rameter is undef
3bc10 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ined.</dd>.**.**
3bc20 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 <dt>SQLITE_STAT
3bc30 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c US_PARSER_STACK<
3bc40 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3bc50 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 parameter recor
3bc60 64 73 20 74 68 65 20 64 65 65 70 65 73 74 20 70 ds the deepest p
3bc70 61 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74 arser stack. It
3bc80 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e is only.** mean
3bc90 69 6e 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65 ingful if SQLite
3bca0 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 is compiled wit
3bcb0 68 20 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 h [YYTRACKMAXSTA
3bcc0 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a CKDEPTH].</dd>.*
3bcd0 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 * </dl>.**.** Ne
3bce0 77 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 w status paramet
3bcf0 65 72 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 ers may be added
3bd00 20 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 from time to ti
3bd10 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 me..*/.#define S
3bd20 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
3bd30 4f 52 59 5f 55 53 45 44 20 20 20 20 20 20 20 20 ORY_USED
3bd40 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 0.#define SQLI
3bd50 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 TE_STATUS_PAGECA
3bd60 43 48 45 5f 55 53 45 44 20 20 20 20 20 20 20 31 CHE_USED 1
3bd70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3bd80 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
3bd90 5f 4f 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 _OVERFLOW 2.#d
3bda0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 efine SQLITE_STA
3bdb0 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 TUS_SCRATCH_USED
3bdc0 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 3.#defi
3bdd0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ne SQLITE_STATUS
3bde0 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f _SCRATCH_OVERFLO
3bdf0 57 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 W 4.#define
3be00 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 SQLITE_STATUS_MA
3be10 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 LLOC_SIZE
3be20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 5.#define SQL
3be30 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 ITE_STATUS_PARSE
3be40 52 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 R_STACK
3be50 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 6.#define SQLITE
3be60 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
3be70 45 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23 E_SIZE 7.#
3be80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3be90 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a ATUS_SCRATCH_SIZ
3bea0 45 20 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a E 8../*.
3beb0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 ** CAPI3REF: Dat
3bec0 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e abase Connection
3bed0 20 53 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d Status {H17500}
3bee0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 <S60200>.** EXP
3bef0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
3bf00 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 This interface i
3bf10 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 s used to retrie
3bf20 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 ve runtime statu
3bf30 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a s information .*
3bf40 2a 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 * about a single
3bf50 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3bf60 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 ction]. The fir
3bf70 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 st argument is t
3bf80 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 he.** database c
3bf90 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 onnection object
3bfa0 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 to be interroga
3bfb0 74 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 ted. The second
3bfc0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 argument.** is
3bfd0 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f the parameter to
3bfe0 20 69 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43 interrogate. C
3bff0 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e urrently, the on
3c000 6c 79 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ly allowed value
3c010 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f .** for the seco
3c020 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nd parameter is
3c030 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 [SQLITE_DBSTATUS
3c040 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d _LOOKASIDE_USED]
3c050 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 ..** Additional
3c060 6f 70 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b options will lik
3c070 65 6c 79 20 61 70 70 65 61 72 20 69 6e 20 66 75 ely appear in fu
3c080 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 ture releases of
3c090 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 SQLite..**.** T
3c0a0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
3c0b0 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 of the requeste
3c0c0 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 77 d parameter is w
3c0d0 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 ritten into *pCu
3c0e0 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 r.** and the hig
3c0f0 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f hest instantaneo
3c100 75 73 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 us value is writ
3c110 74 65 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 ten into *pHiwtr
3c120 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73 . If.** the res
3c130 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 etFlg is true, t
3c140 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 hen the highest
3c150 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 instantaneous va
3c160 6c 75 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 lue is.** reset
3c170 62 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65 back down to the
3c180 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a current value..
3c190 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
3c1a0 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 [sqlite3_status(
3c1b0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
3c1c0 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a stmt_status()]..
3c1d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
3c1e0 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
3c1f0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 L int sqlite3_db
3c200 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a _status(sqlite3*
3c210 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 , int op, int *p
3c220 43 75 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72 Cur, int *pHiwtr
3c230 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b , int resetFlg);
3c240 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3c250 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 : Status Paramet
3c260 65 72 73 20 66 6f 72 20 64 61 74 61 62 61 73 65 ers for database
3c270 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31 connections {H1
3c280 37 35 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a 7520} <H17500>.*
3c290 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3c2a0 2a 0a 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62 *.** Status verb
3c2b0 73 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64 s for [sqlite3_d
3c2c0 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a b_status()]..**.
3c2d0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 ** <dl>.** <dt>S
3c2e0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c QLITE_DBSTATUS_L
3c2f0 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 OOKASIDE_USED</d
3c300 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
3c310 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 arameter returns
3c320 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c the number of l
3c330 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 ookaside memory
3c340 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a slots currently.
3c350 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c ** checked out.<
3c360 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f /dd>.** </dl>.*/
3c370 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3c380 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 DBSTATUS_LOOKASI
3c390 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a DE_USED 0...
3c3a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3c3b0 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 Prepared Stateme
3c3c0 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 nt Status {H1755
3c3d0 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 0} <S60200>.** E
3c3e0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a XPERIMENTAL.**.*
3c3f0 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64 20 * Each prepared
3c400 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 statement mainta
3c410 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b ins various.** [
3c420 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 SQLITE_STMTSTATU
3c430 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 S_SORT | counter
3c440 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 s] that measure
3c450 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 the number.** of
3c460 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 times it has pe
3c470 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 rformed specific
3c480 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 operations. Th
3c490 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e ese counters can
3c4a0 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d .** be used to m
3c4b0 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f onitor the perfo
3c4c0 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 rmance character
3c4d0 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 istics of the pr
3c4e0 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d epared.** statem
3c4f0 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 ents. For examp
3c500 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 le, if the numbe
3c510 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 r of table steps
3c520 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 greatly exceeds
3c530 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
3c540 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 f table searches
3c550 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c or result rows,
3c560 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 that would tend
3c570 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 to indicate.**
3c580 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65 that the prepare
3c590 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 d statement is u
3c5a0 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c sing a full tabl
3c5b0 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 e scan rather th
3c5c0 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 an.** an index.
3c5d0 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 .**.** This int
3c5e0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 erface is used t
3c5f0 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 o retrieve and r
3c600 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c eset counter val
3c610 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 ues from.** a [p
3c620 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3c630 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 t]. The first a
3c640 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 rgument is the p
3c650 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3c660 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 t.** object to b
3c670 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 e interrogated.
3c680 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
3c690 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e ment.** is an in
3c6a0 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 teger code for a
3c6b0 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 specific [SQLIT
3c6c0 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 E_STMTSTATUS_SOR
3c6d0 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 T | counter].**
3c6e0 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 to be interrogat
3c6f0 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 ed. .** The curr
3c700 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 ent value of the
3c710 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 requested count
3c720 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a er is returned..
3c730 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 46 ** If the resetF
3c740 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e lg is true, then
3c750 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 the counter is
3c760 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 reset to zero af
3c770 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 ter this.** inte
3c780 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 rface call retur
3c790 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ns..**.** See al
3c7a0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 so: [sqlite3_sta
3c7b0 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 tus()] and [sqli
3c7c0 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d te3_db_status()]
3c7d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3c7e0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
3c7f0 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TAL int sqlite3_
3c800 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 stmt_status(sqli
3c810 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f te3_stmt*, int o
3c820 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b p,int resetFlg);
3c830 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3c840 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 : Status Paramet
3c850 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 ers for prepared
3c860 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 statements {H17
3c870 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 570} <H17550>.**
3c880 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
3c890 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f .** These prepro
3c8a0 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 cessor macros de
3c8b0 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 fine integer cod
3c8c0 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 es that name cou
3c8d0 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 nter.** values a
3c8e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
3c8f0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 he [sqlite3_stmt
3c900 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 _status()] inter
3c910 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 face..** The mea
3c920 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 nings of the var
3c930 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 ious counters ar
3c940 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a e as follows:.**
3c950 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e .** <dl>.** <dt>
3c960 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 SQLITE_STMTSTATU
3c970 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c S_FULLSCAN_STEP<
3c980 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3c990 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
3c9a0 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c f times that SQL
3c9b0 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64 20 ite has stepped
3c9c0 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 forward in.** a
3c9d0 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 table as part of
3c9e0 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 a full table sc
3c9f0 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 an. Large numbe
3ca00 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e rs for this coun
3ca10 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 ter.** may indic
3ca20 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 ate opportunitie
3ca30 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 s for performanc
3ca40 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 e improvement th
3ca50 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 rough .** carefu
3ca60 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 l use of indices
3ca70 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
3ca80 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 >SQLITE_STMTSTAT
3ca90 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 US_SORT</dt>.**
3caa0 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 <dd>This is the
3cab0 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f number of sort o
3cac0 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 perations that h
3cad0 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a ave occurred..**
3cae0 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 A non-zero valu
3caf0 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 e in this counte
3cb00 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 r may indicate a
3cb10 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f n opportunity to
3cb20 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 .** improvement
3cb30 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f performance thro
3cb40 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 ugh careful use
3cb50 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e of indices.</dd>
3cb60 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a .**.** </dl>.*/.
3cb70 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3cb80 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 TMTSTATUS_FULLSC
3cb90 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 AN_STEP 1.#d
3cba0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d efine SQLITE_STM
3cbb0 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 TSTATUS_SORT
3cbc0 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2../*.
3cbd0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 73 ** CAPI3REF: Cus
3cbe0 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f tom Page Cache O
3cbf0 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d bject.** EXPERIM
3cc00 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ENTAL.**.** The
3cc10 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74 sqlite3_pcache t
3cc20 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20 ype is opaque.
3cc30 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 It is implemente
3cc40 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 d by.** the plug
3cc50 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 gable module. T
3cc60 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68 he SQLite core h
3cc70 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 as no knowledge
3cc80 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f of.** its size o
3cc90 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 r internal struc
3cca0 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64 ture and never d
3ccb0 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a eals with the.**
3ccc0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
3ccd0 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 79 object except by
3cce0 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 holding and pas
3ccf0 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a sing pointers.**
3cd00 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a to the object..
3cd10 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 **.** See [sqlit
3cd20 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
3cd30 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 s] for additiona
3cd40 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
3cd50 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
3cd60 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
3cd70 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a sqlite3_pcache;.
3cd80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3cd90 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 Application Def
3cda0 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 2e ined Page Cache.
3cdb0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
3cdc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 .**.** The [sqli
3cdd0 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c te3_config]([SQL
3cde0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
3cdf0 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 E], ...) interfa
3ce00 63 65 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74 ce can.** regist
3ce10 65 72 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 er an alternativ
3ce20 65 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 e page cache imp
3ce30 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 lementation by p
3ce40 61 73 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a assing in an .**
3ce50 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
3ce60 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
3ce70 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 methods structur
3ce80 65 2e 20 54 68 65 20 6d 61 6a 6f 72 69 74 79 20 e. The majority
3ce90 6f 66 20 74 68 65 20 0a 2a 2a 20 68 65 61 70 20 of the .** heap
3cea0 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 73 memory used by s
3ceb0 71 6c 69 74 65 20 69 73 20 75 73 65 64 20 62 79 qlite is used by
3cec0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
3ced0 74 6f 20 63 61 63 68 65 20 64 61 74 61 20 72 65 to cache data re
3cee0 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 ad .** from, or
3cef0 72 65 61 64 79 20 74 6f 20 62 65 20 77 72 69 74 ready to be writ
3cf00 74 65 6e 20 74 6f 2c 20 74 68 65 20 64 61 74 61 ten to, the data
3cf10 62 61 73 65 20 66 69 6c 65 2e 20 42 79 20 69 6d base file. By im
3cf20 70 6c 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a plementing a .**
3cf30 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 custom page cac
3cf40 68 65 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 he using this AP
3cf50 49 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f I, an applicatio
3cf60 6e 20 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f n can control mo
3cf70 72 65 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79 re .** precisely
3cf80 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d the amount of m
3cf90 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 emory consumed b
3cfa0 79 20 73 71 6c 69 74 65 2c 20 74 68 65 20 77 61 y sqlite, the wa
3cfb0 79 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 73 y in which .** s
3cfc0 61 69 64 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c aid memory is al
3cfd0 6c 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c 65 located and rele
3cfe0 61 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70 6f ased, and the po
3cff0 6c 69 63 69 65 73 20 75 73 65 64 20 74 6f 20 0a licies used to .
3d000 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78 61 ** determine exa
3d010 63 74 6c 79 20 77 68 69 63 68 20 70 61 72 74 73 ctly which parts
3d020 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 of a database f
3d030 69 6c 65 20 61 72 65 20 63 61 63 68 65 64 20 61 ile are cached a
3d040 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c nd for .** how l
3d050 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ong..**.** The c
3d060 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73 ontents of the s
3d070 74 72 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 tructure are cop
3d080 69 65 64 20 74 6f 20 61 6e 20 69 6e 74 65 72 6e ied to an intern
3d090 61 6c 20 62 75 66 66 65 72 20 62 79 20 73 71 6c al buffer by sql
3d0a0 69 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 ite.** within th
3d0b0 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 e call to [sqlit
3d0c0 65 33 5f 63 6f 6e 66 69 67 5d 2e 0a 2a 2a 0a 2a e3_config]..**.*
3d0d0 2a 20 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65 * The xInit() me
3d0e0 74 68 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f thod is called o
3d0f0 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c nce for each cal
3d100 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e l to [sqlite3_in
3d110 69 74 69 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28 itialize()].** (
3d120 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 usually only onc
3d130 65 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 e during the lif
3d140 65 74 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f etime of the pro
3d150 63 65 73 73 29 2e 20 49 74 20 69 73 20 70 61 73 cess). It is pas
3d160 73 65 64 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 sed.** a copy of
3d170 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
3d180 63 68 65 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67 che_methods.pArg
3d190 20 76 61 6c 75 65 2e 20 49 74 20 63 61 6e 20 62 value. It can b
3d1a0 65 20 75 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a e used to set.**
3d1b0 20 75 70 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 up global struc
3d1c0 74 75 72 65 73 20 61 6e 64 20 6d 75 74 65 78 65 tures and mutexe
3d1d0 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 s required by th
3d1e0 65 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 e custom page ca
3d1f0 63 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e che .** implemen
3d200 74 61 74 69 6f 6e 2e 20 54 68 65 20 78 53 68 75 tation. The xShu
3d210 74 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 tdown() method i
3d220 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 s called from wi
3d230 74 68 69 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 thin .** [sqlite
3d240 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 69 3_shutdown()], i
3d250 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
3d260 6e 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 n invokes this A
3d270 50 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 PI. It can be us
3d280 65 64 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75 ed.** to clean u
3d290 70 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e p any outstandin
3d2a0 67 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f g resources befo
3d2b0 72 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 re process shutd
3d2c0 6f 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 own, if required
3d2d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 ..**.** The xCre
3d2e0 61 74 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 ate() method is
3d2f0 75 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 used to construc
3d300 74 20 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e t a new cache in
3d310 73 74 61 6e 63 65 2e 20 54 68 65 0a 2a 2a 20 66 stance. The.** f
3d320 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20 irst parameter,
3d330 73 7a 50 61 67 65 2c 20 69 73 20 74 68 65 20 73 szPage, is the s
3d340 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
3d350 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 6d the pages that m
3d360 75 73 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 ust.** be alloca
3d370 74 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 ted by the cache
3d380 2e 20 73 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f . szPage will no
3d390 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 t be a power of
3d3a0 74 77 6f 2e 20 54 68 65 0a 2a 2a 20 73 65 63 6f two. The.** seco
3d3b0 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 62 50 75 nd argument, bPu
3d3c0 72 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65 rgeable, is true
3d3d0 20 69 66 20 74 68 65 20 63 61 63 68 65 20 62 65 if the cache be
3d3e0 69 6e 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c ing created will
3d3f0 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63 .** be used to c
3d400 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61 ache database pa
3d410 67 65 73 20 72 65 61 64 20 66 72 6f 6d 20 61 20 ges read from a
3d420 66 69 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 file stored on d
3d430 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 isk, or.** false
3d440 20 69 66 20 69 74 20 69 73 20 75 73 65 64 20 66 if it is used f
3d450 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 or an in-memory
3d460 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 database. The ca
3d470 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
3d480 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 on.** does not h
3d490 61 76 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 ave to do anythi
3d4a0 6e 67 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 ng special based
3d4b0 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 on the value of
3d4c0 20 62 50 75 72 67 65 61 62 6c 65 2c 0a 2a 2a 20 bPurgeable,.**
3d4d0 69 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 it is purely adv
3d4e0 69 73 6f 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 isory. .**.** Th
3d4f0 65 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d e xCachesize() m
3d500 65 74 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c ethod may be cal
3d510 6c 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 led at any time
3d520 62 79 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74 by SQLite to set
3d530 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 the.** suggeste
3d540 64 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d d maximum cache-
3d550 73 69 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20 size (number of
3d560 70 61 67 65 73 20 73 74 6f 72 65 64 20 62 79 29 pages stored by)
3d570 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e the cache.** in
3d580 73 74 61 6e 63 65 20 70 61 73 73 65 64 20 61 73 stance passed as
3d590 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
3d5a0 65 6e 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 ent. This is the
3d5b0 20 76 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65 value configure
3d5c0 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 d using.** the S
3d5d0 51 4c 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63 QLite "[PRAGMA c
3d5e0 61 63 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d ache_size]" comm
3d5f0 61 6e 64 2e 20 41 73 20 77 69 74 68 20 74 68 65 and. As with the
3d600 20 62 50 75 72 67 65 61 62 6c 65 20 70 61 72 61 bPurgeable para
3d610 6d 65 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d meter,.** the im
3d620 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
3d630 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
3d640 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63 do anything spec
3d650 69 61 6c 20 77 69 74 68 20 74 68 69 73 0a 2a 2a ial with this.**
3d660 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 61 64 value, it is ad
3d670 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a visory only..**.
3d680 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e ** The xPagecoun
3d690 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c t() method shoul
3d6a0 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d d return the num
3d6b0 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 ber of pages cur
3d6c0 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 rently.** stored
3d6d0 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 73 75 in the cache su
3d6e0 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 pplied as an arg
3d6f0 75 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 ument..** .** Th
3d700 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f e xFetch() metho
3d710 64 20 69 73 20 75 73 65 64 20 74 6f 20 66 65 74 d is used to fet
3d720 63 68 20 61 20 70 61 67 65 20 61 6e 64 20 72 65 ch a page and re
3d730 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
3d740 6f 20 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67 o it. .** A 'pag
3d750 65 27 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 e', in this cont
3d760 65 78 74 2c 20 69 73 20 61 20 62 75 66 66 65 72 ext, is a buffer
3d770 20 6f 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 of szPage bytes
3d780 20 61 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a aligned at an.*
3d790 2a 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 * 8-byte boundar
3d7a0 79 2e 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 y. The page to b
3d7b0 65 20 66 65 74 63 68 65 64 20 69 73 20 64 65 74 e fetched is det
3d7c0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b ermined by the k
3d7d0 65 79 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d ey. The.** mimim
3d7e0 75 6d 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 um key value is
3d7f0 31 2e 20 41 66 74 65 72 20 69 74 20 68 61 73 20 1. After it has
3d800 62 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75 been retrieved u
3d810 73 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65 sing xFetch, the
3d820 20 70 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e page .** is con
3d830 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 70 69 sidered to be pi
3d840 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 nned..**.** If t
3d850 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 he requested pag
3d860 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 e is already in
3d870 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2c 20 the page cache,
3d880 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 then a pointer t
3d890 6f 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 64 20 o.** the cached
3d8a0 62 75 66 66 65 72 20 73 68 6f 75 6c 64 20 62 65 buffer should be
3d8b0 20 72 65 74 75 72 6e 65 64 20 77 69 74 68 20 69 returned with i
3d8c0 74 73 20 63 6f 6e 74 65 6e 74 73 20 69 6e 74 61 ts contents inta
3d8d0 63 74 2e 20 49 66 20 74 68 65 0a 2a 2a 20 70 61 ct. If the.** pa
3d8e0 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 ge is not alread
3d8f0 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 y in the cache,
3d900 74 68 65 6e 20 74 68 65 20 65 78 70 65 63 74 65 then the expecte
3d910 64 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 74 d behaviour of t
3d920 68 65 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 64 he.** cache is d
3d930 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
3d940 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 value of the cr
3d950 65 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 eateFlag paramet
3d960 65 72 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 er passed.** to
3d970 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e xFetch, accordin
3d980 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 g to the followi
3d990 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 ng table:.**.**
3d9a0 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 <table border=1
3d9b0 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d width=85% align=
3d9c0 63 65 6e 74 65 72 3e 0a 2a 2a 20 20 20 3c 74 72 center>.** <tr
3d9d0 3e 3c 74 68 3e 63 72 65 61 74 65 46 6c 61 67 3c ><th>createFlag<
3d9e0 74 68 3e 45 78 70 65 63 74 65 64 20 42 65 68 61 th>Expected Beha
3d9f0 76 69 6f 75 72 0a 2a 2a 20 20 20 3c 74 72 3e 3c viour.** <tr><
3da00 74 64 3e 30 3c 74 64 3e 4e 55 4c 4c 20 73 68 6f td>0<td>NULL sho
3da10 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e uld be returned.
3da20 20 4e 6f 20 6e 65 77 20 63 61 63 68 65 20 65 6e No new cache en
3da30 74 72 79 20 69 73 20 63 72 65 61 74 65 64 2e 0a try is created..
3da40 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 31 3c 74 ** <tr><td>1<t
3da50 64 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 d>If createFlag
3da60 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74 68 69 is set to 1, thi
3da70 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 s indicates that
3da80 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 .**
3da90 20 20 20 20 53 51 4c 69 74 65 20 69 73 20 68 6f SQLite is ho
3daa0 6c 64 69 6e 67 20 70 69 6e 6e 65 64 20 70 61 67 lding pinned pag
3dab0 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 es that can be u
3dac0 6e 70 69 6e 6e 65 64 0a 2a 2a 20 20 20 20 20 20 npinned.**
3dad0 20 20 20 20 20 20 20 20 20 20 62 79 20 77 72 69 by wri
3dae0 74 69 6e 67 20 74 68 65 69 72 20 63 6f 6e 74 65 ting their conte
3daf0 6e 74 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 nts to the datab
3db00 61 73 65 20 66 69 6c 65 20 28 61 0a 2a 2a 20 20 ase file (a.**
3db10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
3db20 6c 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69 latively expensi
3db30 76 65 20 6f 70 65 72 61 74 69 6f 6e 29 2e 20 49 ve operation). I
3db40 6e 20 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e n this situation
3db50 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
3db60 20 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 cache imp
3db70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 lementation has
3db80 74 77 6f 20 63 68 6f 69 63 65 73 3a 20 69 74 20 two choices: it
3db90 63 61 6e 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2c can return NULL,
3dba0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
3dbb0 20 20 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 in which case
3dbc0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 74 74 SQLite will att
3dbd0 65 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e empt to unpin on
3dbe0 65 20 6f 72 20 6d 6f 72 65 20 0a 2a 2a 20 20 20 e or more .**
3dbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
3dc00 65 73 20 62 65 66 6f 72 65 20 72 65 2d 72 65 71 es before re-req
3dc10 75 65 73 74 69 6e 67 20 74 68 65 20 73 61 6d 65 uesting the same
3dc20 20 70 61 67 65 2c 20 6f 72 20 69 74 20 63 61 6e page, or it can
3dc30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
3dc40 20 20 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 allocate a ne
3dc50 77 20 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 w page and retur
3dc60 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 n a pointer to i
3dc70 74 2e 20 49 66 20 61 20 6e 65 77 0a 2a 2a 20 20 t. If a new.**
3dc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
3dc90 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c ge is allocated,
3dca0 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 then the first
3dcb0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 62 79 sizeof(void*) by
3dcc0 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 tes of.**
3dcd0 20 20 20 20 20 20 20 20 20 69 74 20 28 61 74 20 it (at
3dce0 6c 65 61 73 74 29 20 6d 75 73 74 20 62 65 20 7a least) must be z
3dcf0 65 72 6f 65 64 20 62 65 66 6f 72 65 20 69 74 20 eroed before it
3dd00 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
3dd10 20 20 3c 74 72 3e 3c 74 64 3e 32 3c 74 64 3e 49 <tr><td>2<td>I
3dd20 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 f createFlag is
3dd30 73 65 74 20 74 6f 20 32 2c 20 74 68 65 6e 20 53 set to 2, then S
3dd40 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 68 6f 6c QLite is not hol
3dd50 64 69 6e 67 20 61 6e 79 0a 2a 2a 20 20 20 20 20 ding any.**
3dd60 20 20 20 20 20 20 20 20 20 20 20 70 69 6e 6e 65 pinne
3dd70 64 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 d pages associat
3dd80 65 64 20 77 69 74 68 20 74 68 65 20 73 70 65 63 ed with the spec
3dd90 69 66 69 63 20 63 61 63 68 65 20 70 61 73 73 65 ific cache passe
3dda0 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 d.**
3ddb0 20 20 20 20 61 73 20 74 68 65 20 66 69 72 73 74 as the first
3ddc0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 46 65 argument to xFe
3ddd0 74 63 68 28 29 20 74 68 61 74 20 63 61 6e 20 62 tch() that can b
3dde0 65 20 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65 0a e unpinned. The.
3ddf0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
3de00 20 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e cache implemen
3de10 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 74 tation should at
3de20 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 tempt to allocat
3de30 65 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 e a new.**
3de40 20 20 20 20 20 20 20 20 20 20 63 61 63 68 65 20 cache
3de50 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e entry and return
3de60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
3de70 2e 20 41 67 61 69 6e 2c 20 74 68 65 20 66 69 72 . Again, the fir
3de80 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 st.**
3de90 20 20 20 20 20 73 69 7a 65 6f 66 28 76 6f 69 64 sizeof(void
3dea0 2a 29 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 *) bytes of the
3deb0 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 7a page should be z
3dec0 65 72 6f 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a eroed before .**
3ded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3dee0 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 it is returned.
3def0 49 66 20 74 68 65 20 78 46 65 74 63 68 28 29 20 If the xFetch()
3df00 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 4e method returns N
3df10 55 4c 4c 20 77 68 65 6e 20 0a 2a 2a 20 20 20 20 ULL when .**
3df20 20 20 20 20 20 20 20 20 20 20 20 20 63 72 65 61 crea
3df30 74 65 46 6c 61 67 3d 3d 32 2c 20 53 51 4c 69 74 teFlag==2, SQLit
3df40 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 61 e assumes that a
3df50 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
3df60 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 on .**
3df70 20 20 20 20 20 20 66 61 69 6c 65 64 20 61 6e 64 failed and
3df80 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
3df90 4e 4f 4d 45 4d 20 74 6f 20 74 68 65 20 75 73 65 NOMEM to the use
3dfa0 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a r..** </table>.*
3dfb0 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 *.** xUnpin() is
3dfc0 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 called by SQLit
3dfd0 65 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 e with a pointer
3dfe0 20 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 to a currently
3dff0 70 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 pinned page.** a
3e000 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 s its second arg
3e010 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 ument. If the th
3e020 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 ird parameter, d
3e030 69 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a iscard, is non-z
3e040 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ero,.** then the
3e050 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 page should be
3e060 65 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 evicted from the
3e070 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 cache. In this
3e080 63 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 case SQLite .**
3e090 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 assumes that the
3e0a0 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 next time the p
3e0b0 61 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 age is retrieved
3e0c0 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 from the cache
3e0d0 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 using.** the xFe
3e0e0 74 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 tch() method, it
3e0f0 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e will be zeroed.
3e100 20 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 If the discard
3e110 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 parameter is.**
3e120 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 zero, then the p
3e130 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 age is considere
3e140 64 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 d to be unpinned
3e150 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c . The cache impl
3e160 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 ementation.** ma
3e170 79 20 63 68 6f 6f 73 65 20 74 6f 20 72 65 63 6c y choose to recl
3e180 61 69 6d 20 28 66 72 65 65 20 6f 72 20 72 65 63 aim (free or rec
3e190 79 63 6c 65 29 20 75 6e 70 69 6e 6e 65 64 20 70 ycle) unpinned p
3e1a0 61 67 65 73 20 61 74 20 61 6e 79 20 74 69 6d 65 ages at any time
3e1b0 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 61 73 73 75 ..** SQLite assu
3e1c0 6d 65 73 20 74 68 61 74 20 6e 65 78 74 20 74 69 mes that next ti
3e1d0 6d 65 20 74 68 65 20 70 61 67 65 20 69 73 20 72 me the page is r
3e1e0 65 74 72 69 65 76 65 64 20 66 72 6f 6d 20 74 68 etrieved from th
3e1f0 65 20 63 61 63 68 65 0a 2a 2a 20 69 74 20 77 69 e cache.** it wi
3e200 6c 6c 20 65 69 74 68 65 72 20 62 65 20 7a 65 72 ll either be zer
3e210 6f 65 64 2c 20 6f 72 20 63 6f 6e 74 61 69 6e 20 oed, or contain
3e220 74 68 65 20 73 61 6d 65 20 64 61 74 61 20 74 68 the same data th
3e230 61 74 20 69 74 20 64 69 64 20 77 68 65 6e 20 69 at it did when i
3e240 74 0a 2a 2a 20 77 61 73 20 75 6e 70 69 6e 6e 65 t.** was unpinne
3e250 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 d..**.** The cac
3e260 68 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 he is not requir
3e270 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e ed to perform an
3e280 79 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e y reference coun
3e290 74 69 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a ting. A single .
3e2a0 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69 ** call to xUnpi
3e2b0 6e 28 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70 n() unpins the p
3e2c0 61 67 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f age regardless o
3e2d0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
3e2e0 70 72 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 prior calls .**
3e2f0 74 6f 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a to xFetch()..**.
3e300 2a 2a 20 54 68 65 20 78 52 65 6b 65 79 28 29 20 ** The xRekey()
3e310 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 method is used t
3e320 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79 o change the key
3e330 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 value associate
3e340 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61 d with the.** pa
3e350 67 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ge passed as the
3e360 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
3e370 20 66 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 from oldKey to
3e380 6e 65 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63 newKey. If the c
3e390 61 63 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 ache.** previous
3e3a0 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 ly contains an e
3e3b0 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 ntry associated
3e3c0 77 69 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 with newKey, it
3e3d0 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73 should be.** dis
3e3e0 63 61 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f carded. Any prio
3e3f0 72 20 63 61 63 68 65 20 65 6e 74 72 79 20 61 73 r cache entry as
3e400 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 sociated with ne
3e410 77 4b 65 79 20 69 73 20 67 75 61 72 61 6e 74 65 wKey is guarante
3e420 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 ed not.** to be
3e430 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 pinned..**.** Wh
3e440 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 en SQLite calls
3e450 74 68 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 the xTruncate()
3e460 6d 65 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68 method, the cach
3e470 65 20 6d 75 73 74 20 64 69 73 63 61 72 64 20 61 e must discard a
3e480 6c 6c 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63 ll.** existing c
3e490 61 63 68 65 20 65 6e 74 72 69 65 73 20 77 69 74 ache entries wit
3e4a0 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28 h page numbers (
3e4b0 6b 65 79 73 29 20 67 72 65 61 74 65 72 20 74 68 keys) greater th
3e4c0 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74 an or equal.** t
3e4d0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 o the value of t
3e4e0 68 65 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65 he iLimit parame
3e4f0 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 78 54 ter passed to xT
3e500 72 75 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e runcate(). If an
3e510 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61 y.** of these pa
3e520 67 65 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20 ges are pinned,
3e530 74 68 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69 they are implici
3e540 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 tly unpinned, me
3e550 61 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68 aning that.** th
3e560 65 79 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 ey can be safely
3e570 20 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a discarded..**.*
3e580 2a 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29 * The xDestroy()
3e590 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 method is used
3e5a0 74 6f 20 64 65 6c 65 74 65 20 61 20 63 61 63 68 to delete a cach
3e5b0 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78 e allocated by x
3e5c0 43 72 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c Create()..** All
3e5d0 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 resources assoc
3e5e0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 iated with the s
3e5f0 70 65 63 69 66 69 65 64 20 63 61 63 68 65 20 73 pecified cache s
3e600 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20 hould be freed.
3e610 41 66 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 After.** calling
3e620 20 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 the xDestroy()
3e630 6d 65 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63 method, SQLite c
3e640 6f 6e 73 69 64 65 72 73 20 74 68 65 20 5b 73 71 onsiders the [sq
3e650 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a lite3_pcache*].*
3e660 2a 20 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64 * handle invalid
3e670 2c 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75 , and will not u
3e680 73 65 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f se it with any o
3e690 74 68 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61 ther sqlite3_pca
3e6a0 63 68 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 che_methods.** f
3e6b0 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 unctions..*/.typ
3e6c0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
3e6d0 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
3e6e0 64 73 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ds sqlite3_pcach
3e6f0 65 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 e_methods;.struc
3e700 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 t sqlite3_pcache
3e710 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 _methods {. voi
3e720 64 20 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28 d *pArg;. int (
3e730 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a *xInit)(void*);.
3e740 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f void (*xShutdo
3e750 77 6e 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71 wn)(void*);. sq
3e760 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a lite3_pcache *(*
3e770 78 43 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50 xCreate)(int szP
3e780 61 67 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 age, int bPurgea
3e790 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 ble);. void (*x
3e7a0 43 61 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74 Cachesize)(sqlit
3e7b0 65 33 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20 e3_pcache*, int
3e7c0 6e 43 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69 nCachesize);. i
3e7d0 6e 74 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29 nt (*xPagecount)
3e7e0 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
3e7f0 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65 );. void *(*xFe
3e800 74 63 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61 tch)(sqlite3_pca
3e810 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b che*, unsigned k
3e820 65 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c ey, int createFl
3e830 61 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 ag);. void (*xU
3e840 6e 70 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63 npin)(sqlite3_pc
3e850 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e ache*, void*, in
3e860 74 20 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f t discard);. vo
3e870 69 64 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c id (*xRekey)(sql
3e880 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f ite3_pcache*, vo
3e890 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c id*, unsigned ol
3e8a0 64 4b 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e dKey, unsigned n
3e8b0 65 77 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 ewKey);. void (
3e8c0 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 *xTruncate)(sqli
3e8d0 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 te3_pcache*, uns
3e8e0 69 67 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 igned iLimit);.
3e8f0 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 void (*xDestroy
3e900 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 )(sqlite3_pcache
3e910 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 *);.};../*.** CA
3e920 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 PI3REF: Online B
3e930 61 63 6b 75 70 20 4f 62 6a 65 63 74 0a 2a 2a 20 ackup Object.**
3e940 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
3e950 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 ** The sqlite3_b
3e960 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 72 65 63 ackup object rec
3e970 6f 72 64 73 20 73 74 61 74 65 20 69 6e 66 6f 72 ords state infor
3e980 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e 20 mation about an
3e990 6f 6e 67 6f 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e ongoing.** onlin
3e9a0 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 e backup operati
3e9b0 6f 6e 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 on. The sqlite3
3e9c0 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 69 _backup object i
3e9d0 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 s created by.**
3e9e0 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 a call to [sqlit
3e9f0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
3ea00 5d 20 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79 ] and is destroy
3ea10 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a ed by a call to.
3ea20 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b ** [sqlite3_back
3ea30 75 70 5f 66 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a up_finish()]..**
3ea40 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 .** See Also: [U
3ea50 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 sing the SQLite
3ea60 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 Online Backup AP
3ea70 49 5d 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 I].*/.typedef st
3ea80 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63 ruct sqlite3_bac
3ea90 6b 75 70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b kup sqlite3_back
3eaa0 75 70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 up;../*.** CAPI3
3eab0 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b REF: Online Back
3eac0 75 70 20 41 50 49 2e 0a 2a 2a 20 45 58 50 45 52 up API..** EXPER
3ead0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
3eae0 69 73 20 41 50 49 20 69 73 20 75 73 65 64 20 74 is API is used t
3eaf0 6f 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 20 o overwrite the
3eb00 63 6f 6e 74 65 6e 74 73 20 6f 66 20 6f 6e 65 20 contents of one
3eb10 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74 68 database with th
3eb20 61 74 0a 2a 2a 20 6f 66 20 61 6e 6f 74 68 65 72 at.** of another
3eb30 2e 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 65 . It is useful e
3eb40 69 74 68 65 72 20 66 6f 72 20 63 72 65 61 74 69 ither for creati
3eb50 6e 67 20 62 61 63 6b 75 70 73 20 6f 66 20 64 61 ng backups of da
3eb60 74 61 62 61 73 65 73 20 6f 72 0a 2a 2a 20 66 6f tabases or.** fo
3eb70 72 20 63 6f 70 79 69 6e 67 20 69 6e 2d 6d 65 6d r copying in-mem
3eb80 6f 72 79 20 64 61 74 61 62 61 73 65 73 20 74 6f ory databases to
3eb90 20 6f 72 20 66 72 6f 6d 20 70 65 72 73 69 73 74 or from persist
3eba0 65 6e 74 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a ent files. .**.*
3ebb0 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 * See Also: [Usi
3ebc0 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e ng the SQLite On
3ebd0 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d line Backup API]
3ebe0 0a 2a 2a 0a 2a 2a 20 45 78 63 6c 75 73 69 76 65 .**.** Exclusive
3ebf0 20 61 63 63 65 73 73 20 69 73 20 72 65 71 75 69 access is requi
3ec00 72 65 64 20 74 6f 20 74 68 65 20 64 65 73 74 69 red to the desti
3ec10 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
3ec20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 64 75 72 61 for the .** dura
3ec30 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65 72 tion of the oper
3ec40 61 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 20 74 ation. However t
3ec50 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
3ec60 73 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65 se is only.** re
3ec70 61 64 2d 6c 6f 63 6b 65 64 20 77 68 69 6c 65 20 ad-locked while
3ec80 69 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 it is actually b
3ec90 65 69 6e 67 20 72 65 61 64 2c 20 69 74 20 69 73 eing read, it is
3eca0 20 6e 6f 74 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63 not locked.** c
3ecb0 6f 6e 74 69 6e 75 6f 75 73 6c 79 20 66 6f 72 20 ontinuously for
3ecc0 74 68 65 20 65 6e 74 69 72 65 20 6f 70 65 72 61 the entire opera
3ecd0 74 69 6f 6e 2e 20 54 68 75 73 2c 20 74 68 65 20 tion. Thus, the
3ece0 62 61 63 6b 75 70 20 6d 61 79 20 62 65 0a 2a 2a backup may be.**
3ecf0 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 61 20 performed on a
3ed00 6c 69 76 65 20 64 61 74 61 62 61 73 65 20 77 69 live database wi
3ed10 74 68 6f 75 74 20 70 72 65 76 65 6e 74 69 6e 67 thout preventing
3ed20 20 6f 74 68 65 72 20 75 73 65 72 73 20 66 72 6f other users fro
3ed30 6d 0a 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 m.** writing to
3ed40 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 the database for
3ed50 20 61 6e 20 65 78 74 65 6e 64 65 64 20 70 65 72 an extended per
3ed60 69 6f 64 20 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20 iod of time..**
3ed70 0a 2a 2a 20 54 6f 20 70 65 72 66 6f 72 6d 20 61 .** To perform a
3ed80 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
3ed90 6e 3a 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a n: .** <ol>.**
3eda0 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 <li><b>sqli
3edb0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
3edc0 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 )</b> is called
3edd0 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 once to initiali
3ede0 7a 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 ze the.**
3edf0 20 20 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 backup, .**
3ee00 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 <li><b>sqlite3
3ee10 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f _backup_step()</
3ee20 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65 b> is called one
3ee30 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 or more times t
3ee40 6f 20 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20 o transfer .**
3ee50 20 20 20 20 20 20 20 74 68 65 20 64 61 74 61 20 the data
3ee60 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 between the two
3ee70 64 61 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66 databases, and f
3ee80 69 6e 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c inally.** <l
3ee90 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 i><b>sqlite3_bac
3eea0 6b 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e kup_finish()</b>
3eeb0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 is called to re
3eec0 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 lease all resour
3eed0 63 65 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ces .**
3eee0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
3eef0 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
3ef00 74 69 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c tion. .** </ol
3ef10 3e 0a 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 6c >.** There shoul
3ef20 64 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 d be exactly one
3ef30 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
3ef40 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 _backup_finish()
3ef50 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75 63 for each.** suc
3ef60 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 cessful call to
3ef70 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
3ef80 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e nit()..**.** <b>
3ef90 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
3efa0 6e 69 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 nit()</b>.**.**
3efb0 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 61 72 The first two ar
3efc0 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 guments passed t
3efd0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 o [sqlite3_backu
3efe0 70 5f 69 6e 69 74 28 29 5d 20 61 72 65 20 74 68 p_init()] are th
3eff0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 68 61 e database.** ha
3f000 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 ndle associated
3f010 77 69 74 68 20 74 68 65 20 64 65 73 74 69 6e 61 with the destina
3f020 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 61 6e tion database an
3f030 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e d the database n
3f040 61 6d 65 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20 ame .** used to
3f050 61 74 74 61 63 68 20 74 68 65 20 64 65 73 74 69 attach the desti
3f060 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
3f070 74 6f 20 74 68 65 20 68 61 6e 64 6c 65 2e 20 54 to the handle. T
3f080 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 he database name
3f090 0a 2a 2a 20 69 73 20 22 6d 61 69 6e 22 20 66 6f .** is "main" fo
3f0a0 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
3f0b0 61 73 65 2c 20 22 74 65 6d 70 22 20 66 6f 72 20 ase, "temp" for
3f0c0 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 the temporary da
3f0d0 74 61 62 61 73 65 2c 20 6f 72 0a 2a 2a 20 74 68 tabase, or.** th
3f0e0 65 20 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 e name specified
3f0f0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
3f100 5b 41 54 54 41 43 48 5d 20 73 74 61 74 65 6d 65 [ATTACH] stateme
3f110 6e 74 20 69 66 20 74 68 65 20 64 65 73 74 69 6e nt if the destin
3f120 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 6e 20 61 ation is.** an a
3f130 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
3f140 2e 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 . The third and
3f150 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 fourth arguments
3f160 20 70 61 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73 passed to .** s
3f170 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e qlite3_backup_in
3f180 69 74 28 29 20 69 64 65 6e 74 69 66 79 20 74 68 it() identify th
3f190 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
3f1a0 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64 ection].** and d
3f1b0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 75 73 65 atabase name use
3f1c0 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 d.** to access t
3f1d0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
3f1e0 73 65 2e 20 54 68 65 20 76 61 6c 75 65 73 20 70 se. The values p
3f1f0 61 73 73 65 64 20 66 6f 72 20 74 68 65 20 73 6f assed for the so
3f200 75 72 63 65 20 61 6e 64 20 0a 2a 2a 20 64 65 73 urce and .** des
3f210 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 tination [databa
3f220 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 se connection] p
3f230 61 72 61 6d 65 74 65 72 73 20 6d 75 73 74 20 6e arameters must n
3f240 6f 74 20 62 65 20 74 68 65 20 73 61 6d 65 2e 0a ot be the same..
3f250 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
3f260 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 r occurs within
3f270 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
3f280 6e 69 74 28 29 2c 20 74 68 65 6e 20 4e 55 4c 4c nit(), then NULL
3f290 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 is returned.**
3f2a0 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
3f2b0 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 e and error mess
3f2c0 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f age written into
3f2d0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
3f2e0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70 onnection] .** p
3f2f0 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 assed as the fir
3f300 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 st argument. The
3f310 79 20 6d 61 79 20 62 65 20 72 65 74 72 69 65 76 y may be retriev
3f320 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 ed using the.**
3f330 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 [sqlite3_errcode
3f340 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72 ()], [sqlite3_er
3f350 72 6d 73 67 28 29 5d 2c 20 61 6e 64 20 5b 73 71 rmsg()], and [sq
3f360 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 lite3_errmsg16()
3f370 5d 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 ] functions..**
3f380 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 73 75 Otherwise, if su
3f390 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e ccessful, a poin
3f3a0 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 ter to an [sqlit
3f3b0 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 e3_backup] objec
3f3c0 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 t is.** returned
3f3d0 2e 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 6d . This pointer m
3f3e0 61 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20 ay be used with
3f3f0 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
3f400 75 70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a up_step() and.**
3f410 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
3f420 66 69 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f finish() functio
3f430 6e 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 ns to perform th
3f440 65 20 73 70 65 63 69 66 69 65 64 20 62 61 63 6b e specified back
3f450 75 70 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e up .** operation
3f460 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 ..**.** <b>sqlit
3f470 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
3f480 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 </b>.**.** Funct
3f490 69 6f 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 ion [sqlite3_bac
3f4a0 6b 75 70 5f 73 74 65 70 28 29 5d 20 69 73 20 75 kup_step()] is u
3f4b0 73 65 64 20 74 6f 20 63 6f 70 79 20 75 70 20 74 sed to copy up t
3f4c0 6f 20 6e 50 61 67 65 20 70 61 67 65 73 20 62 65 o nPage pages be
3f4d0 74 77 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73 6f tween .** the so
3f4e0 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 urce and destina
3f4f0 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 2c 20 tion databases,
3f500 77 68 65 72 65 20 6e 50 61 67 65 20 69 73 20 74 where nPage is t
3f510 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
3f520 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d .** second param
3f530 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 73 eter passed to s
3f540 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
3f550 65 70 28 29 2e 20 49 66 20 6e 50 61 67 65 20 69 ep(). If nPage i
3f560 73 20 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 s a negative.**
3f570 76 61 6c 75 65 2c 20 61 6c 6c 20 72 65 6d 61 69 value, all remai
3f580 6e 69 6e 67 20 73 6f 75 72 63 65 20 70 61 67 65 ning source page
3f590 73 20 61 72 65 20 63 6f 70 69 65 64 2e 20 49 66 s are copied. If
3f5a0 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 61 the required pa
3f5b0 67 65 73 20 61 72 65 20 0a 2a 2a 20 73 75 63 63 ges are .** succ
3f5c0 65 73 66 75 6c 6c 79 20 63 6f 70 69 65 64 2c 20 esfully copied,
3f5d0 62 75 74 20 74 68 65 72 65 20 61 72 65 20 73 74 but there are st
3f5e0 69 6c 6c 20 6d 6f 72 65 20 70 61 67 65 73 20 74 ill more pages t
3f5f0 6f 20 63 6f 70 79 20 62 65 66 6f 72 65 20 74 68 o copy before th
3f600 65 20 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20 e .** backup is
3f610 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 72 65 74 complete, it ret
3f620 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d urns [SQLITE_OK]
3f630 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 . If no error oc
3f640 63 75 72 65 64 20 61 6e 64 20 74 68 65 72 65 20 cured and there
3f650 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 .** are no more
3f660 70 61 67 65 73 20 74 6f 20 63 6f 70 79 2c 20 74 pages to copy, t
3f670 68 65 6e 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 hen [SQLITE_DONE
3f680 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 ] is returned. I
3f690 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f f an error .** o
3f6a0 63 63 75 72 73 2c 20 74 68 65 6e 20 61 6e 20 53 ccurs, then an S
3f6b0 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 QLite error code
3f6c0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 41 73 is returned. As
3f6d0 20 77 65 6c 6c 20 61 73 20 5b 53 51 4c 49 54 45 well as [SQLITE
3f6e0 5f 4f 4b 5d 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c _OK] and.** [SQL
3f6f0 49 54 45 5f 44 4f 4e 45 5d 2c 20 61 20 63 61 6c ITE_DONE], a cal
3f700 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
3f710 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 kup_step() may r
3f720 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 eturn [SQLITE_RE
3f730 41 44 4f 4e 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c ADONLY],.** [SQL
3f740 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c ITE_NOMEM], [SQL
3f750 49 54 45 5f 42 55 53 59 5d 2c 20 5b 53 51 4c 49 ITE_BUSY], [SQLI
3f760 54 45 5f 4c 4f 43 4b 45 44 5d 2c 20 6f 72 20 61 TE_LOCKED], or a
3f770 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 n.** [SQLITE_IOE
3f780 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 RR_ACCESS | SQLI
3f790 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 78 TE_IOERR_XXX] ex
3f7a0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
3f7b0 65 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c e..**.** As well
3f7c0 20 61 73 20 74 68 65 20 63 61 73 65 20 77 68 65 as the case whe
3f7d0 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 re the destinati
3f7e0 6f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 on database file
3f7f0 20 77 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 0a was opened for.
3f800 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63 ** read-only acc
3f810 65 73 73 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 ess, sqlite3_bac
3f820 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 kup_step() may r
3f830 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 eturn [SQLITE_RE
3f840 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20 74 68 ADONLY] if.** th
3f850 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 e destination is
3f860 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
3f870 74 61 62 61 73 65 20 77 69 74 68 20 61 20 64 69 tabase with a di
3f880 66 66 65 72 65 6e 74 20 70 61 67 65 20 73 69 7a fferent page siz
3f890 65 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 6f e.** from the so
3f8a0 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 0a 2a urce database..*
3f8b0 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f *.** If sqlite3_
3f8c0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 backup_step() ca
3f8d0 6e 6e 6f 74 20 6f 62 74 61 69 6e 20 61 20 72 65 nnot obtain a re
3f8e0 71 75 69 72 65 64 20 66 69 6c 65 2d 73 79 73 74 quired file-syst
3f8f0 65 6d 20 6c 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a em lock, then.**
3f900 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 the [sqlite3_bu
3f910 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 sy_handler | bus
3f920 79 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 y-handler functi
3f930 6f 6e 5d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 on].** is invoke
3f940 64 20 28 69 66 20 6f 6e 65 20 69 73 20 73 70 65 d (if one is spe
3f950 63 69 66 69 65 64 29 2e 20 49 66 20 74 68 65 20 cified). If the
3f960 0a 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 .** busy-handler
3f970 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
3f980 6f 20 62 65 66 6f 72 65 20 74 68 65 20 6c 6f 63 o before the loc
3f990 6b 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 k is available,
3f9a0 74 68 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 then .** [SQLITE
3f9b0 5f 42 55 53 59 5d 20 69 73 20 72 65 74 75 72 6e _BUSY] is return
3f9c0 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
3f9d0 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
3f9e0 68 65 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 he call to.** sq
3f9f0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
3fa00 70 28 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 p() can be retri
3fa10 65 64 20 6c 61 74 65 72 2e 20 49 66 20 74 68 65 ed later. If the
3fa20 20 73 6f 75 72 63 65 0a 2a 2a 20 5b 64 61 74 61 source.** [data
3fa30 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
3fa40 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 75 73 65 .** is being use
3fa50 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 d to write to th
3fa60 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
3fa70 65 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 62 e when sqlite3_b
3fa80 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 ackup_step().**
3fa90 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 is called, then
3faa0 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 [SQLITE_LOCKED]
3fab0 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 is returned imme
3fac0 64 69 61 74 65 6c 79 2e 20 41 67 61 69 6e 2c 20 diately. Again,
3fad0 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 in this.** case
3fae0 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 the call to sqli
3faf0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
3fb00 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 65 64 ) can be retried
3fb10 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a later on. If.**
3fb20 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 [SQLITE_IOERR_A
3fb30 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 CCESS | SQLITE_I
3fb40 4f 45 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 OERR_XXX], [SQLI
3fb50 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a TE_NOMEM], or.**
3fb60 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c [SQLITE_READONL
3fb70 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 Y] is returned,
3fb80 74 68 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69 then .** there i
3fb90 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65 s no point in re
3fba0 74 72 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 trying the call
3fbb0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
3fbc0 70 5f 73 74 65 70 28 29 2e 20 54 68 65 73 65 20 p_step(). These
3fbd0 0a 2a 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63 .** errors are c
3fbe0 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e onsidered fatal.
3fbf0 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 At this point t
3fc00 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d he application m
3fc10 75 73 74 20 61 63 63 65 70 74 20 0a 2a 2a 20 74 ust accept .** t
3fc20 68 61 74 20 74 68 65 20 62 61 63 6b 75 70 20 6f hat the backup o
3fc30 70 65 72 61 74 69 6f 6e 20 68 61 73 20 66 61 69 peration has fai
3fc40 6c 65 64 20 61 6e 64 20 70 61 73 73 20 74 68 65 led and pass the
3fc50 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
3fc60 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20 n handle .** to
3fc70 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
3fc80 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 72 up_finish() to r
3fc90 65 6c 65 61 73 65 20 61 73 73 6f 63 69 61 74 65 elease associate
3fca0 64 20 72 65 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a d resources..**.
3fcb0 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ** Following the
3fcc0 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73 first call to s
3fcd0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
3fce0 65 70 28 29 2c 20 61 6e 20 65 78 63 6c 75 73 69 ep(), an exclusi
3fcf0 76 65 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 ve lock is.** ob
3fd00 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 65 tained on the de
3fd10 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c 65 2e 20 stination file.
3fd20 49 74 20 69 73 20 6e 6f 74 20 72 65 6c 65 61 73 It is not releas
3fd30 65 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 ed until either
3fd40 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
3fd50 75 70 5f 66 69 6e 69 73 68 28 29 20 69 73 20 63 up_finish() is c
3fd60 61 6c 6c 65 64 20 6f 72 20 74 68 65 20 62 61 63 alled or the bac
3fd70 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 kup operation is
3fd80 20 63 6f 6d 70 6c 65 74 65 20 0a 2a 2a 20 61 6e complete .** an
3fd90 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 d sqlite3_backup
3fda0 5f 73 74 65 70 28 29 20 72 65 74 75 72 6e 73 20 _step() returns
3fdb0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 41 [SQLITE_DONE]. A
3fdc0 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 61 63 dditionally, eac
3fdd0 68 20 74 69 6d 65 20 0a 2a 2a 20 61 20 63 61 6c h time .** a cal
3fde0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
3fdf0 6b 75 70 5f 73 74 65 70 28 29 20 69 73 20 6d 61 kup_step() is ma
3fe00 64 65 20 61 20 5b 73 68 61 72 65 64 20 6c 6f 63 de a [shared loc
3fe10 6b 5d 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f k] is obtained o
3fe20 6e 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 n.** the source
3fe30 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
3fe40 68 69 73 20 6c 6f 63 6b 20 69 73 20 72 65 6c 65 his lock is rele
3fe50 61 73 65 64 20 62 65 66 6f 72 65 20 74 68 65 0a ased before the.
3fe60 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
3fe70 70 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 72 65 p_step() call re
3fe80 74 75 72 6e 73 2e 20 42 65 63 61 75 73 65 20 74 turns. Because t
3fe90 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
3fea0 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 se is not.** loc
3feb0 6b 65 64 20 62 65 74 77 65 65 6e 20 63 61 6c 6c ked between call
3fec0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 s to sqlite3_bac
3fed0 6b 75 70 5f 73 74 65 70 28 29 2c 20 69 74 20 6d kup_step(), it m
3fee0 61 79 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6d ay be modified m
3fef0 69 64 2d 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67 id-way.** throug
3ff00 68 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f h the backup pro
3ff10 63 65 64 75 72 65 2e 20 49 66 20 74 68 65 20 73 cedure. If the s
3ff20 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 ource database i
3ff30 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e s modified by an
3ff40 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 70 72 6f .** external pro
3ff50 63 65 73 73 20 6f 72 20 76 69 61 20 61 20 64 61 cess or via a da
3ff60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3ff70 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 n other than the
3ff80 20 6f 6e 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73 one being.** us
3ff90 65 64 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 ed by the backup
3ffa0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e operation, then
3ffb0 20 74 68 65 20 62 61 63 6b 75 70 20 77 69 6c 6c the backup will
3ffc0 20 62 65 20 74 72 61 6e 73 70 61 72 65 6e 74 6c be transparentl
3ffd0 79 0a 2a 2a 20 72 65 73 74 61 72 74 65 64 20 62 y.** restarted b
3ffe0 79 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 y the next call
3fff0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
40000 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74 68 65 p_step(). If the
40010 20 73 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61 source .** data
40020 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 base is modified
40030 20 62 79 20 74 68 65 20 75 73 69 6e 67 20 74 68 by the using th
40040 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
40050 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73 connection as is
40060 20 75 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 used.** by the
40070 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
40080 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 , then the backu
40090 70 20 64 61 74 61 62 61 73 65 20 69 73 20 74 72 p database is tr
400a0 61 6e 73 70 61 72 65 6e 74 6c 79 20 0a 2a 2a 20 ansparently .**
400b0 75 70 64 61 74 65 64 20 61 74 20 74 68 65 20 73 updated at the s
400c0 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 ame time..**.**
400d0 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 <b>sqlite3_backu
400e0 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a p_finish()</b>.*
400f0 2a 0a 2a 2a 20 4f 6e 63 65 20 73 71 6c 69 74 65 *.** Once sqlite
40100 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
40110 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 has returned [SQ
40120 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77 LITE_DONE], or w
40130 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 70 70 6c hen the .** appl
40140 69 63 61 74 69 6f 6e 20 77 69 73 68 65 73 20 74 ication wishes t
40150 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 62 61 o abandon the ba
40160 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 ckup operation,
40170 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 the [sqlite3_bac
40180 6b 75 70 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 73 kup].** object s
40190 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 hould be passed
401a0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
401b0 70 5f 66 69 6e 69 73 68 28 29 2e 20 54 68 69 73 p_finish(). This
401c0 20 72 65 6c 65 61 73 65 73 20 61 6c 6c 0a 2a 2a releases all.**
401d0 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 resources assoc
401e0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62 iated with the b
401f0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e ackup operation.
40200 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b If sqlite3_back
40210 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 68 61 73 up_step().** has
40220 20 6e 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65 not yet returne
40230 64 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c d [SQLITE_DONE],
40240 20 74 68 65 6e 20 61 6e 79 20 61 63 74 69 76 65 then any active
40250 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
40260 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 65 73 on on the.** des
40270 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
40280 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b e is rolled back
40290 2e 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 . The [sqlite3_b
402a0 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 ackup] object is
402b0 20 69 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20 invalid.** and
402c0 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 may not be used
402d0 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c following a call
402e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
402f0 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a up_finish()..**.
40300 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 ** The value ret
40310 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
40320 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 20 69 _backup_finish i
40330 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 s [SQLITE_OK] if
40340 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 no error.** occ
40350 75 72 72 65 64 2c 20 72 65 67 61 72 64 6c 65 73 urred, regardles
40360 73 20 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20 s or whether or
40370 6e 6f 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b not sqlite3_back
40380 75 70 5f 73 74 65 70 28 29 20 77 61 73 20 63 61 up_step() was ca
40390 6c 6c 65 64 0a 2a 2a 20 61 20 73 75 66 66 69 63 lled.** a suffic
403a0 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 74 ient number of t
403b0 69 6d 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 65 imes to complete
403c0 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 the backup oper
403d0 61 74 69 6f 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a ation. Or, if.**
403e0 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 an out-of-memor
403f0 79 20 63 6f 6e 64 69 74 69 6f 6e 20 6f 72 20 49 y condition or I
40400 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 O error occured
40410 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f during a call to
40420 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
40430 75 70 5f 73 74 65 70 28 29 20 74 68 65 6e 20 5b up_step() then [
40440 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 SQLITE_NOMEM] or
40450 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 an.** [SQLITE_I
40460 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 OERR_ACCESS | SQ
40470 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 LITE_IOERR_XXX]
40480 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 error code.** is
40490 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 returned. In th
404a0 69 73 20 63 61 73 65 20 74 68 65 20 65 72 72 6f is case the erro
404b0 72 20 63 6f 64 65 20 61 6e 64 20 61 6e 20 65 72 r code and an er
404c0 72 6f 72 20 6d 65 73 73 61 67 65 20 61 72 65 0a ror message are.
404d0 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 ** written to th
404e0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 e destination [d
404f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
40500 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74 on]..**.** A ret
40510 75 72 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42 urn of [SQLITE_B
40520 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f USY] or [SQLITE_
40530 4c 4f 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c LOCKED] from sql
40540 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
40550 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 () is.** not a p
40560 65 72 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61 ermanent error a
40570 6e 64 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 nd does not affe
40580 63 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ct the return va
40590 6c 75 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 lue of.** sqlite
405a0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
405b0 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 )..**.** <b>sqli
405c0 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 te3_backup_remai
405d0 6e 69 6e 67 28 29 2c 20 73 71 6c 69 74 65 33 5f ning(), sqlite3_
405e0 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
405f0 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 ()</b>.**.** Eac
40600 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 h call to sqlite
40610 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
40620 73 65 74 73 20 74 77 6f 20 76 61 6c 75 65 73 20 sets two values
40630 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c stored internall
40640 79 0a 2a 2a 20 62 79 20 61 6e 20 5b 73 71 6c 69 y.** by an [sqli
40650 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 te3_backup] obje
40660 63 74 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f ct. The number o
40670 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f f pages still to
40680 20 62 65 20 62 61 63 6b 65 64 0a 2a 2a 20 75 70 be backed.** up
40690 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 71 , which may be q
406a0 75 65 72 69 65 64 20 62 79 20 73 71 6c 69 74 65 ueried by sqlite
406b0 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 3_backup_remaini
406c0 6e 67 28 29 2c 20 61 6e 64 20 74 68 65 20 74 6f ng(), and the to
406d0 74 61 6c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 tal.** number of
406e0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f pages in the so
406f0 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 urce database fi
40700 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 le, which may be
40710 20 71 75 65 72 69 65 64 20 62 79 0a 2a 2a 20 73 queried by.** s
40720 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 qlite3_backup_pa
40730 67 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a gecount()..**.**
40740 20 54 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 The values retu
40750 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 66 75 rned by these fu
40760 6e 63 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 nctions are only
40770 20 75 70 64 61 74 65 64 20 62 79 0a 2a 2a 20 73 updated by.** s
40780 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
40790 65 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 ep(). If the sou
407a0 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
407b0 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67 20 modified during
407c0 61 20 62 61 63 6b 75 70 0a 2a 2a 20 6f 70 65 72 a backup.** oper
407d0 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 ation, then the
407e0 76 61 6c 75 65 73 20 61 72 65 20 6e 6f 74 20 75 values are not u
407f0 70 64 61 74 65 64 20 74 6f 20 61 63 63 6f 75 6e pdated to accoun
40800 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61 0a t for any extra.
40810 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65 ** pages that ne
40820 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 ed to be updated
40830 20 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 or the size of
40840 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
40850 61 73 65 20 66 69 6c 65 0a 2a 2a 20 63 68 61 6e ase file.** chan
40860 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 ging..**.** <b>C
40870 6f 6e 63 75 72 72 65 6e 74 20 55 73 61 67 65 20 oncurrent Usage
40880 6f 66 20 44 61 74 61 62 61 73 65 20 48 61 6e 64 of Database Hand
40890 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 les</b>.**.** Th
408a0 65 20 73 6f 75 72 63 65 20 5b 64 61 74 61 62 61 e source [databa
408b0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d se connection] m
408c0 61 79 20 62 65 20 75 73 65 64 20 62 79 20 74 68 ay be used by th
408d0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 6f e application fo
408e0 72 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f r other.** purpo
408f0 73 65 73 20 77 68 69 6c 65 20 61 20 62 61 63 6b ses while a back
40900 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 up operation is
40910 75 6e 64 65 72 77 61 79 20 6f 72 20 62 65 69 6e underway or bein
40920 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a g initialized..*
40930 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 * If SQLite is c
40940 6f 6d 70 69 6c 65 64 20 61 6e 64 20 63 6f 6e 66 ompiled and conf
40950 69 67 75 72 65 64 20 74 6f 20 73 75 70 70 6f 72 igured to suppor
40960 74 20 74 68 72 65 61 64 73 61 66 65 20 64 61 74 t threadsafe dat
40970 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
40980 69 6f 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 ions, then the s
40990 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 63 ource database c
409a0 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 62 65 onnection may be
409b0 20 75 73 65 64 20 63 6f 6e 63 75 72 72 65 6e 74 used concurrent
409c0 6c 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 ly.** from withi
409d0 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e n other threads.
409e0 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 .**.** However,
409f0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
40a00 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 must guarantee t
40a10 68 61 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 hat the destinat
40a20 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ion database.**
40a30 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
40a40 65 20 69 73 20 6e 6f 74 20 70 61 73 73 65 64 20 e is not passed
40a50 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 41 50 49 to any other API
40a60 20 28 62 79 20 61 6e 79 20 74 68 72 65 61 64 29 (by any thread)
40a70 20 61 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69 74 after .** sqlit
40a80 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
40a90 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 62 is called and b
40aa0 65 66 6f 72 65 20 74 68 65 20 63 6f 72 72 65 73 efore the corres
40ab0 70 6f 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f 0a ponding call to.
40ac0 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
40ad0 70 5f 66 69 6e 69 73 68 28 29 2e 20 55 6e 66 6f p_finish(). Unfo
40ae0 72 74 75 6e 61 74 65 6c 79 20 53 51 4c 69 74 65 rtunately SQLite
40af0 20 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e does not curren
40b00 74 6c 79 20 63 68 65 63 6b 0a 2a 2a 20 66 6f 72 tly check.** for
40b10 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 61 70 this, if the ap
40b20 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 75 plication does u
40b30 73 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 se the destinati
40b40 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e on [database con
40b50 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 nection].** for
40b60 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f some other purpo
40b70 73 65 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b se during a back
40b80 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 up operation, th
40b90 69 6e 67 73 20 6d 61 79 20 61 70 70 65 61 72 20 ings may appear
40ba0 74 6f 0a 2a 2a 20 77 6f 72 6b 20 63 6f 72 72 65 to.** work corre
40bb0 63 74 6c 79 20 62 75 74 20 69 6e 20 66 61 63 74 ctly but in fact
40bc0 20 62 65 20 73 75 62 74 6c 79 20 6d 61 6c 66 75 be subtly malfu
40bd0 6e 63 74 69 6f 6e 69 6e 67 2e 20 20 55 73 65 20 nctioning. Use
40be0 6f 66 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e of the.** destin
40bf0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 63 ation database c
40c00 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 onnection while
40c10 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 a backup is in p
40c20 72 6f 67 72 65 73 73 20 6d 69 67 68 74 0a 2a 2a rogress might.**
40c30 20 61 6c 73 6f 20 63 61 75 73 65 20 61 20 6d 75 also cause a mu
40c40 74 65 78 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a tex deadlock..**
40c50 0a 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c .** Furthermore,
40c60 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 5b if running in [
40c70 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 shared cache mod
40c80 65 5d 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 e], the applicat
40c90 69 6f 6e 20 6d 75 73 74 0a 2a 2a 20 67 75 61 72 ion must.** guar
40ca0 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 73 antee that the s
40cb0 68 61 72 65 64 20 63 61 63 68 65 20 75 73 65 64 hared cache used
40cc0 20 62 79 20 74 68 65 20 64 65 73 74 69 6e 61 74 by the destinat
40cd0 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ion database.**
40ce0 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 is not accessed
40cf0 77 68 69 6c 65 20 74 68 65 20 62 61 63 6b 75 70 while the backup
40d00 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 49 6e 20 is running. In
40d10 70 72 61 63 74 69 63 65 20 74 68 69 73 20 6d 65 practice this me
40d20 61 6e 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 ans.** that the
40d30 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 application must
40d40 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
40d50 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
40d60 66 69 6c 65 20 62 65 69 6e 67 20 0a 2a 2a 20 62 file being .** b
40d70 61 63 6b 65 64 20 75 70 20 74 6f 20 69 73 20 6e acked up to is n
40d80 6f 74 20 61 63 63 65 73 73 65 64 20 62 79 20 61 ot accessed by a
40d90 6e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 ny connection wi
40da0 74 68 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 thin the process
40db0 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73 74 20 74 68 ,.** not just th
40dc0 65 20 73 70 65 63 69 66 69 63 20 63 6f 6e 6e 65 e specific conne
40dd0 63 74 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 ction that was p
40de0 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 assed to sqlite3
40df0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a _backup_init()..
40e00 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 **.** The [sqlit
40e10 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 e3_backup] objec
40e20 74 20 69 74 73 65 6c 66 20 69 73 20 70 61 72 74 t itself is part
40e30 69 61 6c 6c 79 20 74 68 72 65 61 64 73 61 66 65 ially threadsafe
40e40 2e 20 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 . Multiple .** t
40e50 68 72 65 61 64 73 20 6d 61 79 20 73 61 66 65 6c hreads may safel
40e60 79 20 6d 61 6b 65 20 6d 75 6c 74 69 70 6c 65 20 y make multiple
40e70 63 6f 6e 63 75 72 72 65 6e 74 20 63 61 6c 6c 73 concurrent calls
40e80 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
40e90 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2a 20 48 6f up_step()..** Ho
40ea0 77 65 76 65 72 2c 20 74 68 65 20 73 71 6c 69 74 wever, the sqlit
40eb0 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e e3_backup_remain
40ec0 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ing() and sqlite
40ed0 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 3_backup_pagecou
40ee0 6e 74 28 29 0a 2a 2a 20 41 50 49 73 20 61 72 65 nt().** APIs are
40ef0 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 73 70 not strictly sp
40f00 65 61 6b 69 6e 67 20 74 68 72 65 61 64 73 61 66 eaking threadsaf
40f10 65 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 69 e. If they are i
40f20 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 0a 2a 2a nvoked at the.**
40f30 20 73 61 6d 65 20 74 69 6d 65 20 61 73 20 61 6e same time as an
40f40 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 other thread is
40f50 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 invoking sqlite3
40f60 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 _backup_step() i
40f70 74 20 69 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 t is.** possible
40f80 20 74 68 61 74 20 74 68 65 79 20 72 65 74 75 72 that they retur
40f90 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 73 n invalid values
40fa0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
40fb0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
40fc0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
40fd0 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a nit(. sqlite3 *
40fe0 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 20 pDest,
40ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
41000 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 Destination dat
41010 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
41020 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
41030 65 73 74 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 estName,
41040 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 /* Dest
41050 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
41060 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c 69 74 name */. sqlit
41070 65 33 20 2a 70 53 6f 75 72 63 65 2c 20 20 20 20 e3 *pSource,
41080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
41090 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 /* Source data
410a0 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 base handle */.
410b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 6f const char *zSo
410c0 75 72 63 65 4e 61 6d 65 20 20 20 20 20 20 20 20 urceName
410d0 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 /* Sourc
410e0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
410f0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 */.);.SQLITE_API
41100 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 int sqlite3_bac
41110 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 kup_step(sqlite3
41120 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 _backup *p, int
41130 6e 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 41 nPage);.SQLITE_A
41140 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
41150 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c ackup_finish(sql
41160 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b ite3_backup *p);
41170 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
41180 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 sqlite3_backup_r
41190 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 emaining(sqlite3
411a0 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c _backup *p);.SQL
411b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
411c0 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 te3_backup_pagec
411d0 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63 ount(sqlite3_bac
411e0 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 kup *p);../*.**
411f0 43 41 50 49 33 52 45 46 3a 20 55 6e 6c 6f 63 6b CAPI3REF: Unlock
41200 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 0a 2a 2a Notification.**
41210 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
41220 0a 2a 2a 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 .** When running
41230 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65 in shared-cache
41240 20 6d 6f 64 65 2c 20 61 20 64 61 74 61 62 61 73 mode, a databas
41250 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20 e operation may
41260 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e 20 fail with.** an
41270 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 [SQLITE_LOCKED]
41280 65 72 72 6f 72 20 69 66 20 74 68 65 20 72 65 71 error if the req
41290 75 69 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 uired locks on t
412a0 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 he shared-cache
412b0 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c or.** individual
412c0 20 74 61 62 6c 65 73 20 77 69 74 68 69 6e 20 74 tables within t
412d0 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 he shared-cache
412e0 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e cannot be obtain
412f0 65 64 2e 20 53 65 65 0a 2a 2a 20 5b 53 51 4c 69 ed. See.** [SQLi
41300 74 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 te Shared-Cache
41310 4d 6f 64 65 5d 20 66 6f 72 20 61 20 64 65 73 63 Mode] for a desc
41320 72 69 70 74 69 6f 6e 20 6f 66 20 73 68 61 72 65 ription of share
41330 64 2d 63 61 63 68 65 20 6c 6f 63 6b 69 6e 67 2e d-cache locking.
41340 20 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61 .** This API ma
41350 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 67 y be used to reg
41360 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b ister a callback
41370 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c that SQLite wil
41380 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a 20 77 68 65 l invoke .** whe
41390 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
413a0 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 currently holdi
413b0 6e 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20 ng the required
413c0 6c 6f 63 6b 20 72 65 6c 69 6e 71 75 69 73 68 65 lock relinquishe
413d0 73 20 69 74 2e 0a 2a 2a 20 54 68 69 73 20 41 50 s it..** This AP
413e0 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 I is only availa
413f0 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 ble if the libra
41400 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 ry was compiled
41410 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c with the.** [SQL
41420 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 ITE_ENABLE_UNLOC
41430 4b 5f 4e 4f 54 49 46 59 5d 20 43 2d 70 72 65 70 K_NOTIFY] C-prep
41440 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 rocessor symbol
41450 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 defined..**.** S
41460 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 ee Also: [Using
41470 74 68 65 20 53 51 4c 69 74 65 20 55 6e 6c 6f 63 the SQLite Unloc
41480 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 46 k Notification F
41490 65 61 74 75 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 eature]..**.** S
414a0 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b hared-cache lock
414b0 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 77 s are released w
414c0 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 hen a database c
414d0 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 63 6c 75 onnection conclu
414e0 64 65 73 0a 2a 2a 20 69 74 73 20 63 75 72 72 65 des.** its curre
414f0 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 nt transaction,
41500 65 69 74 68 65 72 20 62 79 20 63 6f 6d 6d 69 74 either by commit
41510 74 69 6e 67 20 69 74 20 6f 72 20 72 6f 6c 6c 69 ting it or rolli
41520 6e 67 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a ng it back. .**.
41530 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 6e 65 63 ** When a connec
41540 74 69 6f 6e 20 28 6b 6e 6f 77 6e 20 61 73 20 74 tion (known as t
41550 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 he blocked conne
41560 63 74 69 6f 6e 29 20 66 61 69 6c 73 20 74 6f 20 ction) fails to
41570 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 73 68 61 72 obtain a.** shar
41580 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 20 61 6e ed-cache lock an
41590 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 d SQLITE_LOCKED
415a0 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
415b0 68 65 20 63 61 6c 6c 65 72 2c 20 74 68 65 0a 2a he caller, the.*
415c0 2a 20 69 64 65 6e 74 69 74 79 20 6f 66 20 74 68 * identity of th
415d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
415e0 63 74 69 6f 6e 20 28 74 68 65 20 62 6c 6f 63 6b ction (the block
415f0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 ing connection)
41600 74 68 61 74 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b that.** has lock
41610 65 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 ed the required
41620 72 65 73 6f 75 72 63 65 20 69 73 20 73 74 6f 72 resource is stor
41630 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 41 ed internally. A
41640 66 74 65 72 20 61 6e 20 0a 2a 2a 20 61 70 70 6c fter an .** appl
41650 69 63 61 74 69 6f 6e 20 72 65 63 65 69 76 65 73 ication receives
41660 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 an SQLITE_LOCKE
41670 44 20 65 72 72 6f 72 2c 20 69 74 20 6d 61 79 20 D error, it may
41680 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 call the.** sqli
41690 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
416a0 79 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20 y() method with
416b0 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e the blocked conn
416c0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 61 73 ection handle as
416d0 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 .** the first a
416e0 72 67 75 6d 65 6e 74 20 74 6f 20 72 65 67 69 73 rgument to regis
416f0 74 65 72 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 ter for a callba
41700 63 6b 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ck that will be
41710 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 invoked.** when
41720 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e the blocking con
41730 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 nections current
41740 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
41750 63 6f 6e 63 6c 75 64 65 64 2e 20 54 68 65 0a 2a concluded. The.*
41760 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e * callback is in
41770 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 voked from withi
41780 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 n the [sqlite3_s
41790 74 65 70 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 tep] or [sqlite3
417a0 5f 63 6c 6f 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20 _close].** call
417b0 74 68 61 74 20 63 6f 6e 63 6c 75 64 65 73 20 74 that concludes t
417c0 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e he blocking conn
417d0 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 ections transact
417e0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 ion..**.** If sq
417f0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
41800 69 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ify() is called
41810 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 in a multi-threa
41820 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c ded application,
41830 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 63 .** there is a c
41840 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 62 hance that the b
41850 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
41860 6f 6e 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 on will have alr
41870 65 61 64 79 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65 eady.** conclude
41880 64 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f d its transactio
41890 6e 20 62 79 20 74 68 65 20 74 69 6d 65 20 73 71 n by the time sq
418a0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
418b0 69 66 79 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 ify() is invoked
418c0 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 68 61 70 ..** If this hap
418d0 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 pens, then the s
418e0 70 65 63 69 66 69 65 64 20 63 61 6c 6c 62 61 63 pecified callbac
418f0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d k is invoked imm
41900 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f ediately,.** fro
41910 6d 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c m within the cal
41920 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c l to sqlite3_unl
41930 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a ock_notify()..**
41940 0a 2a 2a 20 49 66 20 74 68 65 20 62 6c 6f 63 6b .** If the block
41950 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 ed connection is
41960 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f attempting to o
41970 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f btain a write-lo
41980 63 6b 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65 ck on a.** share
41990 64 2d 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61 d-cache table, a
419a0 6e 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 nd more than one
419b0 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
419c0 6e 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 n currently hold
419d0 73 0a 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b s.** a read-lock
419e0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 on the same tab
419f0 6c 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 le, then SQLite
41a00 61 72 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65 arbitrarily sele
41a10 63 74 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 cts one of .** t
41a20 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 he other connect
41a30 69 6f 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74 ions to use as t
41a40 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e he blocking conn
41a50 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ection..**.** Th
41a60 65 72 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f ere may be at mo
41a70 73 74 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f st one unlock-no
41a80 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 tify callback re
41a90 67 69 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a gistered by a .*
41aa0 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 * blocked connec
41ab0 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 tion. If sqlite3
41ac0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
41ad0 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
41ae0 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 the.** blocked c
41af0 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 onnection alread
41b00 79 20 68 61 73 20 61 20 72 65 67 69 73 74 65 72 y has a register
41b10 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 ed unlock-notify
41b20 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68 callback,.** th
41b30 65 6e 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62 en the new callb
41b40 61 63 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65 ack replaces the
41b50 20 6f 6c 64 2e 20 49 66 20 73 71 6c 69 74 65 33 old. If sqlite3
41b60 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
41b70 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 is.** called wi
41b80 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 th a NULL pointe
41b90 72 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 r as its second
41ba0 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 61 argument, then a
41bb0 6e 79 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 75 ny existing.** u
41bc0 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c nlock-notify cal
41bd0 6c 62 61 63 6b 20 69 73 20 63 61 6e 63 65 6c 6c lback is cancell
41be0 65 64 2e 20 54 68 65 20 62 6c 6f 63 6b 65 64 20 ed. The blocked
41bf0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 0a 2a 2a 20 connections .**
41c00 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
41c10 6c 6c 62 61 63 6b 20 6d 61 79 20 61 6c 73 6f 20 llback may also
41c20 62 65 20 63 61 6e 63 65 6c 65 64 20 62 79 20 63 be canceled by c
41c30 6c 6f 73 69 6e 67 20 74 68 65 20 62 6c 6f 63 6b losing the block
41c40 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e ed.** connection
41c50 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
41c60 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 close()]..**.**
41c70 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 The unlock-notif
41c80 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f y callback is no
41c90 74 20 72 65 65 6e 74 72 61 6e 74 2e 20 49 66 20 t reentrant. If
41ca0 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 an application i
41cb0 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e 79 20 73 71 nvokes.** any sq
41cc0 6c 69 74 65 33 5f 78 78 78 20 41 50 49 20 66 75 lite3_xxx API fu
41cd0 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 77 69 74 nctions from wit
41ce0 68 69 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f hin an unlock-no
41cf0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c 20 61 tify callback, a
41d00 0a 2a 2a 20 63 72 61 73 68 20 6f 72 20 64 65 61 .** crash or dea
41d10 64 6c 6f 63 6b 20 6d 61 79 20 62 65 20 74 68 65 dlock may be the
41d20 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 result..**.** U
41d30 6e 6c 65 73 73 20 64 65 61 64 6c 6f 63 6b 20 69 nless deadlock i
41d40 73 20 64 65 74 65 63 74 65 64 20 28 73 65 65 20 s detected (see
41d50 62 65 6c 6f 77 29 2c 20 73 71 6c 69 74 65 33 5f below), sqlite3_
41d60 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 unlock_notify()
41d70 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e always.** return
41d80 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a s SQLITE_OK..**.
41d90 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61 63 6b 20 49 ** <b>Callback I
41da0 6e 76 6f 63 61 74 69 6f 6e 20 44 65 74 61 69 6c nvocation Detail
41db0 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e s</b>.**.** When
41dc0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
41dd0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65 y callback is re
41de0 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 61 70 gistered, the ap
41df0 70 6c 69 63 61 74 69 6f 6e 20 70 72 6f 76 69 64 plication provid
41e00 65 73 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 es a .** single
41e10 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20 74 68 void* pointer th
41e20 61 74 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 at is passed to
41e30 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 the callback whe
41e40 6e 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e n it is invoked.
41e50 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 .** However, the
41e60 20 73 69 67 6e 61 74 75 72 65 20 6f 66 20 74 68 signature of th
41e70 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
41e80 69 6f 6e 20 61 6c 6c 6f 77 73 20 53 51 4c 69 74 ion allows SQLit
41e90 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 69 74 20 e to pass.** it
41ea0 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 an array of void
41eb0 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 * context pointe
41ec0 72 73 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 rs. The first ar
41ed0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f gument passed to
41ee0 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f .** an unlock-no
41ef0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 tify callback is
41f00 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
41f10 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 array of void*
41f20 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a 20 61 6e 64 pointers,.** and
41f30 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 the second is t
41f40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 he number of ent
41f50 72 69 65 73 20 69 6e 20 74 68 65 20 61 72 72 61 ries in the arra
41f60 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 y..**.** When a
41f70 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 blocking connect
41f80 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e ions transaction
41f90 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2c 20 74 is concluded, t
41fa0 68 65 72 65 20 6d 61 79 20 62 65 0a 2a 2a 20 6d here may be.** m
41fb0 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 62 6c 6f ore than one blo
41fc0 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 cked connection
41fd0 74 68 61 74 20 68 61 73 20 72 65 67 69 73 74 65 that has registe
41fe0 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 red for an unloc
41ff0 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c k-notify.** call
42000 62 61 63 6b 2e 20 49 66 20 74 77 6f 20 6f 72 20 back. If two or
42010 6d 6f 72 65 20 73 75 63 68 20 62 6c 6f 63 6b 65 more such blocke
42020 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 61 d connections ha
42030 76 65 20 73 70 65 63 69 66 69 65 64 20 74 68 65 ve specified the
42040 0a 2a 2a 20 73 61 6d 65 20 63 61 6c 6c 62 61 63 .** same callbac
42050 6b 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e k function, then
42060 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f instead of invo
42070 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 king the callbac
42080 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 k function.** mu
42090 6c 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 69 74 ltiple times, it
420a0 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 is invoked once
420b0 20 77 69 74 68 20 74 68 65 20 73 65 74 20 6f 66 with the set of
420c0 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 void* context p
420d0 6f 69 6e 74 65 72 73 0a 2a 2a 20 73 70 65 63 69 ointers.** speci
420e0 66 69 65 64 20 62 79 20 74 68 65 20 62 6c 6f 63 fied by the bloc
420f0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ked connections
42100 62 75 6e 64 6c 65 64 20 74 6f 67 65 74 68 65 72 bundled together
42110 20 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 2e 0a into an array..
42120 2a 2a 20 54 68 69 73 20 67 69 76 65 73 20 74 68 ** This gives th
42130 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 61 6e e application an
42140 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 opportunity to
42150 70 72 69 6f 72 69 74 69 7a 65 20 61 6e 79 20 61 prioritize any a
42160 63 74 69 6f 6e 73 20 0a 2a 2a 20 72 65 6c 61 74 ctions .** relat
42170 65 64 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 ed to the set of
42180 20 75 6e 62 6c 6f 63 6b 65 64 20 64 61 74 61 62 unblocked datab
42190 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
421a0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f .**.** <b>Deadlo
421b0 63 6b 20 44 65 74 65 63 74 69 6f 6e 3c 2f 62 3e ck Detection</b>
421c0 0a 2a 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 .**.** Assuming
421d0 74 68 61 74 20 61 66 74 65 72 20 72 65 67 69 73 that after regis
421e0 74 65 72 69 6e 67 20 66 6f 72 20 61 6e 20 75 6e tering for an un
421f0 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
42200 62 61 63 6b 20 61 20 0a 2a 2a 20 64 61 74 61 62 back a .** datab
42210 61 73 65 20 77 61 69 74 73 20 66 6f 72 20 74 68 ase waits for th
42220 65 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 e callback to be
42230 20 69 73 73 75 65 64 20 62 65 66 6f 72 65 20 74 issued before t
42240 61 6b 69 6e 67 20 61 6e 79 20 66 75 72 74 68 65 aking any furthe
42250 72 0a 2a 2a 20 61 63 74 69 6f 6e 20 28 61 20 72 r.** action (a r
42260 65 61 73 6f 6e 61 62 6c 65 20 61 73 73 75 6d 70 easonable assump
42270 74 69 6f 6e 29 2c 20 74 68 65 6e 20 75 73 69 6e tion), then usin
42280 67 20 74 68 69 73 20 41 50 49 20 6d 61 79 20 63 g this API may c
42290 61 75 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c ause the.** appl
422a0 69 63 61 74 69 6f 6e 20 74 6f 20 64 65 61 64 6c ication to deadl
422b0 6f 63 6b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 ock. For example
422c0 2c 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 , if connection
422d0 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 X is waiting for
422e0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 59 .** connection Y
422f0 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 's transaction t
42300 6f 20 62 65 20 63 6f 6e 63 6c 75 64 65 64 2c 20 o be concluded,
42310 61 6e 64 20 73 69 6d 69 6c 61 72 6c 79 20 63 6f and similarly co
42320 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 59 20 69 73 nnection.** Y is
42330 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e waiting on conn
42340 65 63 74 69 6f 6e 20 58 27 73 20 74 72 61 6e 73 ection X's trans
42350 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 6e 65 69 action, then nei
42360 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a ther connection.
42370 2a 2a 20 77 69 6c 6c 20 70 72 6f 63 65 65 64 20 ** will proceed
42380 61 6e 64 20 74 68 65 20 73 79 73 74 65 6d 20 6d and the system m
42390 61 79 20 72 65 6d 61 69 6e 20 64 65 61 64 6c 6f ay remain deadlo
423a0 63 6b 65 64 20 69 6e 64 65 66 69 6e 69 74 65 6c cked indefinitel
423b0 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 y..**.** To avoi
423c0 64 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c d this scenario,
423d0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c the sqlite3_unl
423e0 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 70 65 72 ock_notify() per
423f0 66 6f 72 6d 73 20 64 65 61 64 6c 6f 63 6b 0a 2a forms deadlock.*
42400 2a 20 64 65 74 65 63 74 69 6f 6e 2e 20 49 66 20 * detection. If
42410 61 20 67 69 76 65 6e 20 63 61 6c 6c 20 74 6f 20 a given call to
42420 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
42430 6f 74 69 66 79 28 29 20 77 6f 75 6c 64 20 70 75 otify() would pu
42440 74 20 74 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20 t the.** system
42450 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 in a deadlocked
42460 73 74 61 74 65 2c 20 74 68 65 6e 20 53 51 4c 49 state, then SQLI
42470 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 TE_LOCKED is ret
42480 75 72 6e 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 urned and no.**
42490 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
424a0 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 llback is regist
424b0 65 72 65 64 2e 20 54 68 65 20 73 79 73 74 65 6d ered. The system
424c0 20 69 73 20 73 61 69 64 20 74 6f 20 62 65 20 69 is said to be i
424d0 6e 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 65 n.** a deadlocke
424e0 64 20 73 74 61 74 65 20 69 66 20 63 6f 6e 6e 65 d state if conne
424f0 63 74 69 6f 6e 20 41 20 68 61 73 20 72 65 67 69 ction A has regi
42500 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e stered for an un
42510 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 lock-notify.** c
42520 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 allback on the c
42530 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e onclusion of con
42540 6e 65 63 74 69 6f 6e 20 42 27 73 20 74 72 61 6e nection B's tran
42550 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e saction, and con
42560 6e 65 63 74 69 6f 6e 0a 2a 2a 20 42 20 68 61 73 nection.** B has
42570 20 69 74 73 65 6c 66 20 72 65 67 69 73 74 65 72 itself register
42580 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b ed for an unlock
42590 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
425a0 20 77 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e when connection
425b0 0a 2a 2a 20 41 27 73 20 74 72 61 6e 73 61 63 74 .** A's transact
425c0 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 ion is concluded
425d0 2e 20 49 6e 64 69 72 65 63 74 20 64 65 61 64 6c . Indirect deadl
425e0 6f 63 6b 20 69 73 20 61 6c 73 6f 20 64 65 74 65 ock is also dete
425f0 63 74 65 64 2c 20 73 6f 0a 2a 2a 20 74 68 65 20 cted, so.** the
42600 73 79 73 74 65 6d 20 69 73 20 61 6c 73 6f 20 63 system is also c
42610 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 onsidered to be
42620 64 65 61 64 6c 6f 63 6b 65 64 20 69 66 20 63 6f deadlocked if co
42630 6e 6e 65 63 74 69 6f 6e 20 42 20 68 61 73 0a 2a nnection B has.*
42640 2a 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 * registered for
42650 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
42660 79 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 y callback on th
42670 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 e conclusion of
42680 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 43 27 connection.** C'
42690 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 77 s transaction, w
426a0 68 65 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 here connection
426b0 43 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20 C is waiting on
426c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 2e 20 41 6e connection A. An
426d0 79 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 6c y.** number of l
426e0 65 76 65 6c 73 20 6f 66 20 69 6e 64 69 72 65 63 evels of indirec
426f0 74 69 6f 6e 20 61 72 65 20 61 6c 6c 6f 77 65 64 tion are allowed
42700 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 54 68 65 20 22 ..**.** <b>The "
42710 44 52 4f 50 20 54 41 42 4c 45 22 20 45 78 63 65 DROP TABLE" Exce
42720 70 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 ption</b>.**.**
42730 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b When a call to [
42740 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
42750 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c returns SQLITE_L
42760 4f 43 4b 45 44 2c 20 69 74 20 69 73 20 61 6c 6d OCKED, it is alm
42770 6f 73 74 20 0a 2a 2a 20 61 6c 77 61 79 73 20 61 ost .** always a
42780 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 63 61 ppropriate to ca
42790 6c 6c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 ll sqlite3_unloc
427a0 6b 5f 6e 6f 74 69 66 79 28 29 2e 20 54 68 65 72 k_notify(). Ther
427b0 65 20 69 73 20 68 6f 77 65 76 65 72 2c 0a 2a 2a e is however,.**
427c0 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 2e 20 one exception.
427d0 57 68 65 6e 20 65 78 65 63 75 74 69 6e 67 20 61 When executing a
427e0 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 6f 72 "DROP TABLE" or
427f0 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 73 74 "DROP INDEX" st
42800 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69 atement,.** SQLi
42810 74 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 te checks if the
42820 72 65 20 61 72 65 20 61 6e 79 20 63 75 72 72 65 re are any curre
42830 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 53 ntly executing S
42840 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
42850 0a 2a 2a 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 .** that belong
42860 74 6f 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e to the same conn
42870 65 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 ection. If there
42880 20 61 72 65 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 are, SQLITE_LOC
42890 4b 45 44 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e KED is.** return
428a0 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
428b0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 22 62 6c there is no "bl
428c0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f ocking connectio
428d0 6e 22 2c 20 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a n", so invoking.
428e0 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 ** sqlite3_unloc
428f0 6b 5f 6e 6f 74 69 66 79 28 29 20 72 65 73 75 6c k_notify() resul
42900 74 73 20 69 6e 20 74 68 65 20 75 6e 6c 6f 63 6b ts in the unlock
42910 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
42920 20 62 65 69 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65 being.** invoke
42930 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 d immediately. I
42940 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
42950 6e 20 74 68 65 6e 20 72 65 2d 61 74 74 65 6d 70 n then re-attemp
42960 74 73 20 74 68 65 20 22 44 52 4f 50 20 54 41 42 ts the "DROP TAB
42970 4c 45 22 0a 2a 2a 20 6f 72 20 22 44 52 4f 50 20 LE".** or "DROP
42980 49 4e 44 45 58 22 20 71 75 65 72 79 2c 20 61 6e INDEX" query, an
42990 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 20 6d infinite loop m
429a0 69 67 68 74 20 62 65 20 74 68 65 20 72 65 73 75 ight be the resu
429b0 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61 lt..**.** One wa
429c0 79 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 y around this pr
429d0 6f 62 6c 65 6d 20 69 73 20 74 6f 20 63 68 65 63 oblem is to chec
429e0 6b 20 74 68 65 20 65 78 74 65 6e 64 65 64 20 65 k the extended e
429f0 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e rror code return
42a00 65 64 0a 2a 2a 20 62 79 20 61 6e 20 73 71 6c 69 ed.** by an sqli
42a10 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e te3_step() call.
42a20 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 62 If there is a b
42a30 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
42a40 6f 6e 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 on, then the.**
42a50 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
42a60 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 53 51 ode is set to SQ
42a70 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 LITE_LOCKED_SHAR
42a80 45 44 43 41 43 48 45 2e 20 4f 74 68 65 72 77 69 EDCACHE. Otherwi
42a90 73 65 2c 20 69 6e 0a 2a 2a 20 74 68 65 20 73 70 se, in.** the sp
42aa0 65 63 69 61 6c 20 22 44 52 4f 50 20 54 41 42 4c ecial "DROP TABL
42ab0 45 2f 49 4e 44 45 58 22 20 63 61 73 65 2c 20 74 E/INDEX" case, t
42ac0 68 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f he extended erro
42ad0 72 20 63 6f 64 65 20 69 73 20 6a 75 73 74 20 0a r code is just .
42ae0 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 ** SQLITE_LOCKED
42af0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
42b00 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f int sqlite3_unlo
42b10 63 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c ck_notify(. sql
42b20 69 74 65 33 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 ite3 *pBlocked,
42b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
42b40 20 20 20 20 20 20 20 20 20 2f 2a 20 57 61 69 74 /* Wait
42b50 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ing connection *
42b60 2f 0a 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69 /. void (*xNoti
42b70 66 79 29 28 76 6f 69 64 20 2a 2a 61 70 41 72 67 fy)(void **apArg
42b80 2c 20 69 6e 74 20 6e 41 72 67 29 2c 20 20 20 20 , int nArg),
42b90 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 /* Callback func
42ba0 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 2a tion to invoke *
42bb0 2f 0a 20 20 76 6f 69 64 20 2a 70 4e 6f 74 69 66 /. void *pNotif
42bc0 79 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 yArg
42bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
42be0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 /* Argument to p
42bf0 61 73 73 20 74 6f 20 78 4e 6f 74 69 66 79 20 2a ass to xNotify *
42c00 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f /.);../*.** Undo
42c10 20 74 68 65 20 68 61 63 6b 20 74 68 61 74 20 63 the hack that c
42c20 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74 69 6e 67 onverts floating
42c30 20 70 6f 69 6e 74 20 74 79 70 65 73 20 74 6f 20 point types to
42c40 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20 62 integer for.** b
42c50 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73 73 uilds on process
42c60 6f 72 73 20 77 69 74 68 6f 75 74 20 66 6c 6f 61 ors without floa
42c70 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f ting point suppo
42c80 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 rt..*/.#ifdef SQ
42c90 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
42ca0 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65 66 NG_POINT.# undef
42cb0 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 0a double.#endif..
42cc0 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 20 #if 0.} /* End
42cd0 6f 66 20 74 68 65 20 27 65 78 74 65 72 6e 20 22 of the 'extern "
42ce0 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 6e C"' block */.#en
42cf0 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a dif.#endif../***
42d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
42d10 6f 66 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a of sqlite3.h ***
42d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
42d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
42d60 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
42d70 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
42d80 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
42d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
42da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
42db0 75 64 65 20 68 61 73 68 2e 68 20 69 6e 20 74 68 ude hash.h in th
42dc0 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
42dd0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
42de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
42df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
42e00 6e 20 66 69 6c 65 20 68 61 73 68 2e 68 20 2a 2a n file hash.h **
42e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
42e40 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
42e50 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
42e60 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
42e70 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
42e80 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
42e90 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
42ea0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
42eb0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
42ec0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
42ed0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
42ee0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
42ef0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
42f00 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
42f10 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
42f20 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
42f30 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
42f40 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
42f50 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
42f60 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
42f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
42fb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 **.** This is th
42fc0 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f e header file fo
42fd0 72 20 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 r the generic ha
42fe0 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 sh-table impleme
42ff0 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 nation.** used i
43000 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 n SQLite..**.**
43010 24 49 64 3a 20 68 61 73 68 2e 68 2c 76 20 31 2e $Id: hash.h,v 1.
43020 31 35 20 32 30 30 39 2f 30 35 2f 30 32 20 31 33 15 2009/05/02 13
43030 3a 32 39 3a 33 38 20 64 72 68 20 45 78 70 20 24 :29:38 drh Exp $
43040 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c .*/.#ifndef _SQL
43050 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 ITE_HASH_H_.#def
43060 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 ine _SQLITE_HASH
43070 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 _H_../* Forward
43080 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 declarations of
43090 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 structures. */.t
430a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48 61 ypedef struct Ha
430b0 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64 65 66 sh Hash;.typedef
430c0 20 73 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d struct HashElem
430d0 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 HashElem;../* A
430e0 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 complete hash t
430f0 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 able is an insta
43100 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
43110 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
43120 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 ** The internals
43130 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
43140 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 re are intended
43150 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 to be opaque --
43160 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 client.** code s
43170 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 hould not attemp
43180 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d t to access or m
43190 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 odify the fields
431a0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
431b0 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 re.** directly.
431c0 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 Change this str
431d0 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 ucture only by u
431e0 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 sing the routine
431f0 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 s below..** Howe
43200 76 65 72 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 ver, some of the
43210 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e "procedures" an
43220 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f d "functions" fo
43230 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a r modifying and.
43240 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 ** accessing thi
43250 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
43260 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 really macros, s
43270 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c o we can't reall
43280 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 y make.** this s
43290 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e tructure opaque.
432a0 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65 .**.** All eleme
432b0 6e 74 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 nts of the hash
432c0 74 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 73 table are on a s
432d0 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e ingle doubly-lin
432e0 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 48 61 73 ked list..** Has
432f0 68 2e 66 69 72 73 74 20 70 6f 69 6e 74 73 20 74 h.first points t
43300 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 o the head of th
43310 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 is list..**.** T
43320 68 65 72 65 20 61 72 65 20 48 61 73 68 2e 68 74 here are Hash.ht
43330 73 69 7a 65 20 62 75 63 6b 65 74 73 2e 20 20 45 size buckets. E
43340 61 63 68 20 62 75 63 6b 65 74 20 70 6f 69 6e 74 ach bucket point
43350 73 20 74 6f 20 61 20 73 70 6f 74 20 69 6e 0a 2a s to a spot in.*
43360 2a 20 74 68 65 20 67 6c 6f 62 61 6c 20 64 6f 75 * the global dou
43370 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e bly-linked list.
43380 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f The contents o
43390 66 20 74 68 65 20 62 75 63 6b 65 74 20 61 72 65 f the bucket are
433a0 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 the.** element
433b0 70 6f 69 6e 74 65 64 20 74 6f 20 70 6c 75 73 20 pointed to plus
433c0 74 68 65 20 6e 65 78 74 20 5f 68 74 2e 63 6f 75 the next _ht.cou
433d0 6e 74 2d 31 20 65 6c 65 6d 65 6e 74 73 20 69 6e nt-1 elements in
433e0 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a the list..**.**
433f0 20 48 61 73 68 2e 68 74 73 69 7a 65 20 61 6e 64 Hash.htsize and
43400 20 48 61 73 68 2e 68 74 20 6d 61 79 20 62 65 20 Hash.ht may be
43410 7a 65 72 6f 2e 20 20 49 6e 20 74 68 61 74 20 63 zero. In that c
43420 61 73 65 20 6c 6f 6f 6b 75 70 20 69 73 20 64 6f ase lookup is do
43430 6e 65 0a 2a 2a 20 62 79 20 61 20 6c 69 6e 65 61 ne.** by a linea
43440 72 20 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 r search of the
43450 67 6c 6f 62 61 6c 20 6c 69 73 74 2e 20 20 46 6f global list. Fo
43460 72 20 73 6d 61 6c 6c 20 74 61 62 6c 65 73 2c 20 r small tables,
43470 74 68 65 20 0a 2a 2a 20 48 61 73 68 2e 68 74 20 the .** Hash.ht
43480 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72 20 61 table is never a
43490 6c 6c 6f 63 61 74 65 64 20 62 65 63 61 75 73 65 llocated because
434a0 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65 if there are fe
434b0 77 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e w elements.** in
434c0 20 74 68 65 20 74 61 62 6c 65 2c 20 69 74 20 69 the table, it i
434d0 73 20 66 61 73 74 65 72 20 74 6f 20 64 6f 20 61 s faster to do a
434e0 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20 74 linear search t
434f0 68 61 6e 20 74 6f 20 6d 61 6e 61 67 65 0a 2a 2a han to manage.**
43500 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e the hash table.
43510 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 20 .*/.struct Hash
43520 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 {. unsigned int
43530 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a htsize; /*
43540 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 Number of bucke
43550 74 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 ts in the hash t
43560 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e able */. unsign
43570 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 ed int count;
43580 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
43590 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73 entries in this
435a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 table */. Hash
435b0 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 Elem *first;
435c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 /* The fir
435d0 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 st element of th
435e0 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 e array */. str
435f0 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20 20 20 uct _ht {
43600 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 /* the ha
43610 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 sh table */.
43620 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 int count;
43630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
43640 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
43650 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a with this hash *
43660 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a /. HashElem *
43670 63 68 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 chain;
43680 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 /* Pointer to f
43690 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 68 20 irst entry with
436a0 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d this hash */. }
436b0 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 *ht;.};../* Eac
436c0 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 h element in the
436d0 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 hash table is a
436e0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
436f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 e following .**
43700 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 structure. All
43710 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f elements are sto
43720 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 red on a single
43730 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 doubly-linked li
43740 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c st..**.** Again,
43750 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
43760 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 is intended to b
43770 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74 e opaque, but it
43780 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a can't really.**
43790 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 61 75 be opaque becau
437a0 73 65 20 69 74 20 69 73 20 75 73 65 64 20 62 79 se it is used by
437b0 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 macros..*/.stru
437c0 63 74 20 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 ct HashElem {.
437d0 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 HashElem *next,
437e0 2a 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 *prev; /*
437f0 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 Next and previou
43800 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 s elements in th
43810 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 e table */. voi
43820 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 d *data;
43830 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
43840 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
43850 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a h this element *
43860 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
43870 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 pKey; int nKey;
43880 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 /* Key associat
43890 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 ed with this ele
438a0 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ment */.};../*.*
438b0 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 * Access routine
438c0 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 s. To delete, i
438d0 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 nsert a NULL poi
438e0 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nter..*/.SQLITE_
438f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
43900 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 ite3HashInit(Has
43910 68 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 h*);.SQLITE_PRIV
43920 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
43930 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 3HashInsert(Hash
43940 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 *, const char *p
43950 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 Key, int nKey, v
43960 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c oid *pData);.SQL
43970 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
43980 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e *sqlite3HashFin
43990 64 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 d(const Hash*, c
439a0 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c onst char *pKey,
439b0 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 int nKey);.SQLI
439c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
439d0 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 sqlite3HashClear
439e0 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 (Hash*);../*.**
439f0 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 Macros for loopi
43a00 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d ng over all elem
43a10 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68 20 74 ents of a hash t
43a20 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69 6f 6d able. The idiom
43a30 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 is.** like this
43a40 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 :.**.** Hash h
43a50 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 ;.** HashElem
43a60 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 *p;.** ....**
43a70 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 for(p=sqliteHa
43a80 73 68 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 shFirst(&h); p;
43a90 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 p=sqliteHashNext
43aa0 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d (p)){.** Som
43ab0 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 61 74 eStructure *pDat
43ac0 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 a = sqliteHashDa
43ad0 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f ta(p);.** //
43ae0 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 do something wi
43af0 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a th pData.** }.
43b00 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 */.#define sqlit
43b10 65 48 61 73 68 46 69 72 73 74 28 48 29 20 20 28 eHashFirst(H) (
43b20 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 (H)->first).#def
43b30 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4e 65 ine sqliteHashNe
43b40 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 xt(E) ((E)->ne
43b50 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 xt).#define sqli
43b60 74 65 48 61 73 68 44 61 74 61 28 45 29 20 20 20 teHashData(E)
43b70 28 28 45 29 2d 3e 64 61 74 61 29 0a 2f 2a 20 23 ((E)->data)./* #
43b80 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 define sqliteHas
43b90 68 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d hKey(E) ((E)-
43ba0 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f 54 20 55 53 >pKey) // NOT US
43bb0 45 44 20 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 ED */./* #define
43bc0 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 73 69 sqliteHashKeysi
43bd0 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 ze(E) ((E)->nKey
43be0 29 20 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a ) // NOT USED *
43bf0 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 /../*.** Number
43c00 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 of entries in a
43c10 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 2f 2a hash table.*/./*
43c20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 #define sqliteH
43c30 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48 ashCount(H) ((H
43c40 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54 )->count) // NOT
43c50 20 55 53 45 44 20 2a 2f 0a 0a 23 65 6e 64 69 66 USED */..#endif
43c60 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 /* _SQLITE_HASH
43c70 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
43c80 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
43c90 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ash.h **********
43ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43cc0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
43cd0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
43ce0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
43cf0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
43d00 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
43d10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
43d20 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
43d30 70 61 72 73 65 2e 68 20 69 6e 20 74 68 65 20 6d parse.h in the m
43d40 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
43d50 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
43d60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
43d70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
43d80 6c 65 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a le parse.h *****
43d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
43db0 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 ******/.#define
43dc0 54 4b 5f 53 45 4d 49 20 20 20 20 20 20 20 20 20 TK_SEMI
43dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43de0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 1.#define TK_
43df0 45 58 50 4c 41 49 4e 20 20 20 20 20 20 20 20 20 EXPLAIN
43e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e10 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 51 55 45 2.#define TK_QUE
43e20 52 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 RY
43e30 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 3.#
43e40 64 65 66 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 define TK_PLAN
43e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e60 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 4.#def
43e70 69 6e 65 20 54 4b 5f 42 45 47 49 4e 20 20 20 20 ine TK_BEGIN
43e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43e90 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 5.#define
43ea0 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 TK_TRANSACTION
43eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43ec0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 54 4b 6.#define TK
43ed0 5f 44 45 46 45 52 52 45 44 20 20 20 20 20 20 20 _DEFERRED
43ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43ef0 20 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 7.#define TK_IM
43f00 4d 45 44 49 41 54 45 20 20 20 20 20 20 20 20 20 MEDIATE
43f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 8.
43f20 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55 #define TK_EXCLU
43f30 53 49 56 45 20 20 20 20 20 20 20 20 20 20 20 20 SIVE
43f40 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 9.#de
43f50 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 fine TK_COMMIT
43f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43f70 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 10.#defin
43f80 65 20 54 4b 5f 45 4e 44 20 20 20 20 20 20 20 20 e TK_END
43f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43fa0 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 54 11.#define T
43fb0 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20 K_ROLLBACK
43fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43fd0 20 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 12.#define TK_S
43fe0 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20 AVEPOINT
43ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
44000 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4c 45 .#define TK_RELE
44010 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ASE
44020 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 14.#d
44030 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20 efine TK_TO
44040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44050 20 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 15.#defi
44060 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 ne TK_TABLE
44070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44080 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 16.#define
44090 54 4b 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 TK_CREATE
440a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
440b0 20 20 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 17.#define TK_
440c0 49 46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IF
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
440e0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 8.#define TK_NOT
440f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44100 20 20 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 19.#
44110 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53 54 53 define TK_EXISTS
44120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44130 20 20 20 20 20 20 20 20 20 32 30 0a 23 64 65 66 20.#def
44140 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20 20 20 ine TK_TEMP
44150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44160 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 21.#define
44170 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20 20 20 TK_LP
44180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44190 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 22.#define TK
441a0 5f 52 50 20 20 20 20 20 20 20 20 20 20 20 20 20 _RP
441b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
441c0 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 23.#define TK_AS
441d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
441e0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 24.
441f0 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 #define TK_COMMA
44200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44210 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 25.#de
44220 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20 20 20 fine TK_ID
44230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44240 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e 26.#defin
44250 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 e TK_INDEXED
44260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44270 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 54 27.#define T
44280 4b 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 20 K_ABORT
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
442a0 20 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 28.#define TK_A
442b0 46 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 FTER
442c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39 29
442d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c .#define TK_ANAL
442e0 59 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 YZE
442f0 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 30.#d
44300 65 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20 efine TK_ASC
44310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44320 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 69 31.#defi
44330 6e 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20 ne TK_ATTACH
44340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44350 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 32.#define
44360 54 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20 TK_BEFORE
44370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44380 20 20 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 33.#define TK_
44390 42 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 BY
443a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
443b0 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 4.#define TK_CAS
443c0 43 41 44 45 20 20 20 20 20 20 20 20 20 20 20 20 CADE
443d0 20 20 20 20 20 20 20 20 20 20 20 20 33 35 0a 23 35.#
443e0 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 54 20 20 define TK_CAST
443f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44400 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 36.#def
44410 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 ine TK_COLUMNKW
44420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44430 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 37.#define
44440 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 TK_CONFLICT
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44460 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 38.#define TK
44470 5f 44 41 54 41 42 41 53 45 20 20 20 20 20 20 20 _DATABASE
44480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44490 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 39.#define TK_DE
444a0 53 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SC
444b0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 40.
444c0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 #define TK_DETAC
444d0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
444e0 20 20 20 20 20 20 20 20 20 20 34 31 0a 23 64 65 41.#de
444f0 66 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20 fine TK_EACH
44500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44510 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 42.#defin
44520 65 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 e TK_FAIL
44530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44540 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 54 43.#define T
44550 4b 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20 20 K_FOR
44560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44570 20 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44.#define TK_I
44580 47 4e 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 GNORE
44590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35 45
445a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 .#define TK_INIT
445b0 49 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20 IALLY
445c0 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 46.#d
445d0 65 66 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 efine TK_INSTEAD
445e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
445f0 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 69 47.#defi
44600 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 ne TK_LIKE_KW
44610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44620 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20 48.#define
44630 54 4b 5f 4d 41 54 43 48 20 20 20 20 20 20 20 20 TK_MATCH
44640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44650 20 20 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49.#define TK_
44660 4b 45 59 20 20 20 20 20 20 20 20 20 20 20 20 20 KEY
44670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
44680 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 20 0.#define TK_OF
44690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
446a0 20 20 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 51.#
446b0 64 65 66 69 6e 65 20 54 4b 5f 4f 46 46 53 45 54 define TK_OFFSET
446c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
446d0 20 20 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 52.#def
446e0 69 6e 65 20 54 4b 5f 50 52 41 47 4d 41 20 20 20 ine TK_PRAGMA
446f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44700 20 20 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 53.#define
44710 20 54 4b 5f 52 41 49 53 45 20 20 20 20 20 20 20 TK_RAISE
44720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44730 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 54.#define TK
44740 5f 52 45 50 4c 41 43 45 20 20 20 20 20 20 20 20 _REPLACE
44750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44760 35 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 55.#define TK_RE
44770 53 54 52 49 43 54 20 20 20 20 20 20 20 20 20 20 STRICT
44780 20 20 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 56.
44790 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 57 20 20 #define TK_ROW
447a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
447b0 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 57.#de
447c0 66 69 6e 65 20 54 4b 5f 54 52 49 47 47 45 52 20 fine TK_TRIGGER
447d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
447e0 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 69 6e 58.#defin
447f0 65 20 54 4b 5f 56 41 43 55 55 4d 20 20 20 20 20 e TK_VACUUM
44800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44810 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 20 54 59.#define T
44820 4b 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 20 K_VIEW
44830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44840 20 36 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 60.#define TK_V
44850 49 52 54 55 41 4c 20 20 20 20 20 20 20 20 20 20 IRTUAL
44860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31 61
44870 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 49 4e .#define TK_REIN
44880 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 DEX
44890 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 64 62.#d
448a0 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d 45 20 efine TK_RENAME
448b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
448c0 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66 69 63.#defi
448d0 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 ne TK_CTIME_KW
448e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
448f0 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 64.#define
44900 54 4b 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 TK_ANY
44910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44920 20 20 36 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 65.#define TK_
44930 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OR
44940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
44950 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 44 6.#define TK_AND
44960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44970 20 20 20 20 20 20 20 20 20 20 20 20 36 37 0a 23 67.#
44980 64 65 66 69 6e 65 20 54 4b 5f 49 53 20 20 20 20 define TK_IS
44990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
449a0 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 68.#def
449b0 69 6e 65 20 54 4b 5f 42 45 54 57 45 45 4e 20 20 ine TK_BETWEEN
449c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
449d0 20 20 20 20 20 20 36 39 0a 23 64 65 66 69 6e 65 69.#define
449e0 20 54 4b 5f 49 4e 20 20 20 20 20 20 20 20 20 20 TK_IN
449f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a00 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b 70.#define TK
44a10 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 _ISNULL
44a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a30 37 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 71.#define TK_NO
44a40 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 TNULL
44a50 20 20 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 72.
44a60 23 64 65 66 69 6e 65 20 54 4b 5f 4e 45 20 20 20 #define TK_NE
44a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44a80 20 20 20 20 20 20 20 20 20 20 37 33 0a 23 64 65 73.#de
44a90 66 69 6e 65 20 54 4b 5f 45 51 20 20 20 20 20 20 fine TK_EQ
44aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ab0 20 20 20 20 20 20 20 37 34 0a 23 64 65 66 69 6e 74.#defin
44ac0 65 20 54 4b 5f 47 54 20 20 20 20 20 20 20 20 20 e TK_GT
44ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ae0 20 20 20 20 37 35 0a 23 64 65 66 69 6e 65 20 54 75.#define T
44af0 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 K_LE
44b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44b10 20 37 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 76.#define TK_L
44b20 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
44b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 77
44b40 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45 20 20 .#define TK_GE
44b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44b60 20 20 20 20 20 20 20 20 20 20 20 37 38 0a 23 64 78.#d
44b70 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50 45 20 efine TK_ESCAPE
44b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44b90 20 20 20 20 20 20 20 20 37 39 0a 23 64 65 66 69 79.#defi
44ba0 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 20 ne TK_BITAND
44bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44bc0 20 20 20 20 20 38 30 0a 23 64 65 66 69 6e 65 20 80.#define
44bd0 54 4b 5f 42 49 54 4f 52 20 20 20 20 20 20 20 20 TK_BITOR
44be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44bf0 20 20 38 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 81.#define TK_
44c00 4c 53 48 49 46 54 20 20 20 20 20 20 20 20 20 20 LSHIFT
44c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
44c20 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 53 48 2.#define TK_RSH
44c30 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20 IFT
44c40 20 20 20 20 20 20 20 20 20 20 20 20 38 33 0a 23 83.#
44c50 64 65 66 69 6e 65 20 54 4b 5f 50 4c 55 53 20 20 define TK_PLUS
44c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44c70 20 20 20 20 20 20 20 20 20 38 34 0a 23 64 65 66 84.#def
44c80 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 ine TK_MINUS
44c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ca0 20 20 20 20 20 20 38 35 0a 23 64 65 66 69 6e 65 85.#define
44cb0 20 54 4b 5f 53 54 41 52 20 20 20 20 20 20 20 20 TK_STAR
44cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44cd0 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 54 4b 86.#define TK
44ce0 5f 53 4c 41 53 48 20 20 20 20 20 20 20 20 20 20 _SLASH
44cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44d00 38 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 87.#define TK_RE
44d10 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 M
44d20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 0a 88.
44d30 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 #define TK_CONCA
44d40 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
44d50 20 20 20 20 20 20 20 20 20 20 38 39 0a 23 64 65 89.#de
44d60 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 fine TK_COLLATE
44d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44d80 20 20 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 90.#defin
44d90 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20 e TK_UMINUS
44da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44db0 20 20 20 20 39 31 0a 23 64 65 66 69 6e 65 20 54 91.#define T
44dc0 4b 5f 55 50 4c 55 53 20 20 20 20 20 20 20 20 20 K_UPLUS
44dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44de0 20 39 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 92.#define TK_B
44df0 49 54 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 ITNOT
44e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33 93
44e10 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 .#define TK_STRI
44e20 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NG
44e30 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 23 64 94.#d
44e40 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 efine TK_JOIN_KW
44e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44e60 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 95.#defi
44e70 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 ne TK_CONSTRAINT
44e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44e90 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 96.#define
44ea0 54 4b 5f 44 45 46 41 55 4c 54 20 20 20 20 20 20 TK_DEFAULT
44eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44ec0 20 20 39 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 97.#define TK_
44ed0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
44ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
44ef0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 8.#define TK_PRI
44f00 4d 41 52 59 20 20 20 20 20 20 20 20 20 20 20 20 MARY
44f10 20 20 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 99.#
44f20 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 define TK_UNIQUE
44f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44f40 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65 100.#de
44f50 66 69 6e 65 20 54 4b 5f 43 48 45 43 4b 20 20 20 fine TK_CHECK
44f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44f70 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 101.#defi
44f80 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 ne TK_REFERENCES
44f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44fa0 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 102.#define
44fb0 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 TK_AUTOINCR
44fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44fd0 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 20 54 103.#define T
44fe0 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 K_ON
44ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45000 20 31 30 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 104.#define TK_
45010 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 DELETE
45020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45030 30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 05.#define TK_UP
45040 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 DATE
45050 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 36 106
45060 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 45 .#define TK_INSE
45070 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 RT
45080 20 20 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 107.#
45090 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 20 20 define TK_SET
450a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
450b0 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65 108.#de
450c0 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 41 42 fine TK_DEFERRAB
450d0 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LE
450e0 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 66 69 109.#defi
450f0 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 20 20 ne TK_FOREIGN
45100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45110 20 20 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 110.#define
45120 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 20 20 TK_DROP
45130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45140 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 54 111.#define T
45150 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 K_UNION
45160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45170 20 31 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 112.#define TK_
45180 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 ALL
45190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
451a0 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 13.#define TK_EX
451b0 43 45 50 54 20 20 20 20 20 20 20 20 20 20 20 20 CEPT
451c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 34 114
451d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 .#define TK_INTE
451e0 52 53 45 43 54 20 20 20 20 20 20 20 20 20 20 20 RSECT
451f0 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 115.#
45200 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 43 54 define TK_SELECT
45210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45220 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 116.#de
45230 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e 43 54 fine TK_DISTINCT
45240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45250 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 66 69 117.#defi
45260 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 20 20 ne TK_DOT
45270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45280 20 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 118.#define
45290 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20 TK_FROM
452a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
452b0 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 54 119.#define T
452c0 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20 K_JOIN
452d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
452e0 20 31 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 120.#define TK_
452f0 55 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 USING
45300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45310 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 21.#define TK_OR
45320 44 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 DER
45330 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 32 122
45340 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 .#define TK_GROU
45350 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 P
45360 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 123.#
45370 64 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 define TK_HAVING
45380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45390 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 64 65 124.#de
453a0 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 fine TK_LIMIT
453b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
453c0 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 125.#defi
453d0 6e 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 ne TK_WHERE
453e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
453f0 20 20 20 20 20 31 32 36 0a 23 64 65 66 69 6e 65 126.#define
45400 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 TK_INTO
45410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45420 20 20 20 31 32 37 0a 23 64 65 66 69 6e 65 20 54 127.#define T
45430 4b 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 K_VALUES
45440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45450 20 31 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 128.#define TK_
45460 49 4e 54 45 47 45 52 20 20 20 20 20 20 20 20 20 INTEGER
45470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45480 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 29.#define TK_FL
45490 4f 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 OAT
454a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 30 130
454b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 .#define TK_BLOB
454c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
454d0 20 20 20 20 20 20 20 20 20 20 20 31 33 31 0a 23 131.#
454e0 64 65 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 define TK_REGIST
454f0 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ER
45500 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 132.#de
45510 66 69 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 fine TK_VARIABLE
45520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45530 20 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 133.#defi
45540 6e 65 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20 ne TK_CASE
45550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45560 20 20 20 20 20 31 33 34 0a 23 64 65 66 69 6e 65 134.#define
45570 20 54 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 TK_WHEN
45580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45590 20 20 20 31 33 35 0a 23 64 65 66 69 6e 65 20 54 135.#define T
455a0 4b 5f 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 K_THEN
455b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
455c0 20 31 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 136.#define TK_
455d0 45 4c 53 45 20 20 20 20 20 20 20 20 20 20 20 20 ELSE
455e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
455f0 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 37.#define TK_IN
45600 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 DEX
45610 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 38 138
45620 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 .#define TK_ALTE
45630 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
45640 20 20 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 139.#
45650 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 define TK_ADD
45660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45670 20 20 20 20 20 20 20 20 20 31 34 30 0a 23 64 65 140.#de
45680 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 fine TK_TO_TEXT
45690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
456a0 20 20 20 20 20 20 20 31 34 31 0a 23 64 65 66 69 141.#defi
456b0 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 ne TK_TO_BLOB
456c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
456d0 20 20 20 20 20 31 34 32 0a 23 64 65 66 69 6e 65 142.#define
456e0 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 TK_TO_NUMERIC
456f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45700 20 20 20 31 34 33 0a 23 64 65 66 69 6e 65 20 54 143.#define T
45710 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 K_TO_INT
45720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45730 20 31 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 144.#define TK_
45740 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20 20 20 TO_REAL
45750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
45760 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 45.#define TK_EN
45770 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20 D_OF_FILE
45780 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 36 146
45790 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 .#define TK_ILLE
457a0 47 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 GAL
457b0 20 20 20 20 20 20 20 20 20 20 20 31 34 37 0a 23 147.#
457c0 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20 define TK_SPACE
457d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
457e0 20 20 20 20 20 20 20 20 20 31 34 38 0a 23 64 65 148.#de
457f0 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 fine TK_UNCLOSED
45800 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 _STRING
45810 20 20 20 20 20 20 20 31 34 39 0a 23 64 65 66 69 149.#defi
45820 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 ne TK_FUNCTION
45830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45840 20 20 20 20 20 31 35 30 0a 23 64 65 66 69 6e 65 150.#define
45850 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 TK_COLUMN
45860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45870 20 20 20 31 35 31 0a 23 64 65 66 69 6e 65 20 54 151.#define T
45880 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20 K_AGG_FUNCTION
45890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
458a0 20 31 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 152.#define TK_
458b0 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 AGG_COLUMN
458c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
458d0 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 53.#define TK_CO
458e0 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20 NST_FUNC
458f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 34 154
45900 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
45910 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 68 * End of parse.h
45920 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
45930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45950 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
45960 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
45970 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
45980 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
45990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
459a0 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 /.#include <stdi
459b0 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 o.h>.#include <s
459c0 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 tdlib.h>.#includ
459d0 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e e <string.h>.#in
459e0 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e clude <assert.h>
459f0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64 65 .#include <stdde
45a00 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 f.h>../*.** If c
45a10 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 ompiling for a p
45a20 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 rocessor that la
45a30 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 cks floating poi
45a40 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 nt support,.** s
45a50 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 ubstitute intege
45a60 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 r for floating-p
45a70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 20 53 oint.*/.#ifdef S
45a80 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
45a90 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 ING_POINT.# defi
45aa0 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 ne double sqlite
45ab0 5f 69 6e 74 36 34 0a 23 20 64 65 66 69 6e 65 20 _int64.# define
45ac0 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 LONGDOUBLE_TYPE
45ad0 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 69 sqlite_int64.# i
45ae0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 fndef SQLITE_BIG
45af0 5f 44 42 4c 0a 23 20 20 20 64 65 66 69 6e 65 20 _DBL.# define
45b00 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 SQLITE_BIG_DBL (
45b10 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 0x7fffffffffffff
45b20 66 66 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 ff).# endif.# de
45b30 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fine SQLITE_OMIT
45b40 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20 _DATETIME_FUNCS
45b50 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 1.# define SQLIT
45b60 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23 E_OMIT_TRACE 1.#
45b70 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 undef SQLITE_MI
45b80 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 XED_ENDIAN_64BIT
45b90 5f 46 4c 4f 41 54 0a 23 65 6e 64 69 66 0a 23 69 _FLOAT.#endif.#i
45ba0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 fndef SQLITE_BIG
45bb0 5f 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51 _DBL.# define SQ
45bc0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65 LITE_BIG_DBL (1e
45bd0 39 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 99).#endif../*.*
45be0 2a 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 * OMIT_TEMPDB is
45bf0 20 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c set to 1 if SQL
45c00 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20 ITE_OMIT_TEMPDB
45c10 69 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30 is defined, or 0
45c20 0a 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48 .** afterward. H
45c30 61 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f aving this macro
45c40 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61 allows us to ca
45c50 75 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c use the C compil
45c60 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 er .** to omit c
45c70 6f 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50 ode used by TEMP
45c80 20 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20 tables without
45c90 6d 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74 messy #ifndef st
45ca0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 atements..*/.#if
45cb0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
45cc0 54 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f TEMPDB.#define O
45cd0 4d 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c MIT_TEMPDB 1.#el
45ce0 73 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f se.#define OMIT_
45cf0 54 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a TEMPDB 0.#endif.
45d00 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f ./*.** If the fo
45d10 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 llowing macro is
45d20 20 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20 set to 1, then
45d30 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 NULL values are
45d40 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69 considered.** di
45d50 73 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65 stinct when dete
45d60 72 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 rmining whether
45d70 6f 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69 or not two entri
45d80 65 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a es are the same.
45d90 2a 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69 ** in a UNIQUE i
45da0 6e 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74 ndex. This is t
45db0 68 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51 he way PostgreSQ
45dc0 4c 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20 L, Oracle, DB2,
45dd0 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 MySQL,.** OCELOT
45de0 2c 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61 , and Firebird a
45df0 6c 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51 ll work. The SQ
45e00 4c 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69 L92 spec explici
45e10 74 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a tly says this.**
45e20 20 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e is the way thin
45e30 67 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 gs are suppose t
45e40 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 o work..**.** If
45e50 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
45e60 61 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30 acro is set to 0
45e70 2c 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20 , the NULLs are
45e80 69 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a indistinct for.*
45e90 2a 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 * a UNIQUE index
45ea0 2e 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c . In this mode,
45eb0 20 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61 you can only ha
45ec0 76 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c ve a single NULL
45ed0 20 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20 entry.** for a
45ee0 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20 column declared
45ef0 55 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73 UNIQUE. This is
45f00 20 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69 the way Informi
45f10 78 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72 x and SQL Server
45f20 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 .** work..*/.#de
45f30 66 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e fine NULL_DISTIN
45f40 43 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a CT_FOR_UNIQUE 1.
45f50 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65 ./*.** The "file
45f60 20 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20 format" number
45f70 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 is an integer th
45f80 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 at is incremente
45f90 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68 d whenever.** th
45fa0 65 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c e VDBE-level fil
45fb0 65 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73 e format changes
45fc0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
45fd0 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 macros define t
45fe0 68 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c he.** the defaul
45ff0 74 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f t file format fo
46000 72 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 r new databases
46010 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 and the maximum
46020 66 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 file format.** t
46030 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 hat the library
46040 63 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 can read..*/.#de
46050 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
46060 46 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69 FILE_FORMAT 4.#i
46070 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
46080 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 AULT_FILE_FORMAT
46090 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
460a0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f _DEFAULT_FILE_FO
460b0 52 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 2f RMAT 1.#endif../
460c0 2a 0a 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64 *.** Provide a d
460d0 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 efault value for
460e0 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f SQLITE_TEMP_STO
460f0 52 45 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 RE in case it is
46100 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a not specified.*
46110 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 * on the command
46120 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 -line.*/.#ifndef
46130 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f SQLITE_TEMP_STO
46140 52 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 RE.# define SQLI
46150 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a TE_TEMP_STORE 1.
46160 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 #endif../*.** GC
46170 43 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e C does not defin
46180 65 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 e the offsetof()
46190 20 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 macro so we'll
461a0 68 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a have to do it.**
461b0 20 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 ourselves..*/.#
461c0 69 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a ifndef offsetof.
461d0 23 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 #define offsetof
461e0 28 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 (STRUCTURE,FIELD
461f0 29 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 ) ((int)((char*)
46200 26 28 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 &((STRUCTURE*)0)
46210 2d 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 ->FIELD)).#endif
46220 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f ../*.** Check to
46230 20 73 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 see if this mac
46240 68 69 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 hine uses EBCDIC
46250 2e 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 . (Yes, believe
46260 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 it or.** not, t
46270 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d here are still m
46280 61 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 achines out ther
46290 65 20 74 68 61 74 20 75 73 65 20 45 42 43 44 49 e that use EBCDI
462a0 43 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d C.).*/.#if 'A' =
462b0 3d 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e = '\301'.# defin
462c0 65 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 e SQLITE_EBCDIC
462d0 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 1.#else.# define
462e0 20 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a SQLITE_ASCII 1.
462f0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
46300 74 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 tegers of known
46310 73 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 sizes. These ty
46320 70 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 pedefs might cha
46330 6e 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 nge for architec
46340 74 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 tures.** where t
46350 68 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 he sizes very.
46360 50 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 Preprocessor mac
46370 72 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c ros are availabl
46380 65 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a e so that the.**
46390 20 74 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f types can be co
463a0 6e 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 nveniently redef
463b0 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d ined at compile-
463c0 74 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 type. Like this
463d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 :.**.**
463e0 63 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 cc '-DUINTPTR_TY
463f0 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 PE=long long int
46400 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 ' ....*/.#ifndef
46410 20 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 UINT32_TYPE.# i
46420 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 fdef HAVE_UINT32
46430 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e _T.# define UIN
46440 54 33 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f T32_TYPE uint32_
46450 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 t.# else.# defi
46460 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 ne UINT32_TYPE u
46470 6e 73 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e nsigned int.# en
46480 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 dif.#endif.#ifnd
46490 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 ef UINT16_TYPE.#
464a0 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 ifdef HAVE_UINT
464b0 31 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 16_T.# define U
464c0 49 4e 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 INT16_TYPE uint1
464d0 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 6_t.# else.# de
464e0 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 fine UINT16_TYPE
464f0 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 unsigned short
46500 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 int.# endif.#end
46510 69 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 if.#ifndef INT16
46520 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 _TYPE.# ifdef HA
46530 56 45 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 VE_INT16_T.# de
46540 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 fine INT16_TYPE
46550 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 int16_t.# else.#
46560 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 define INT16_T
46570 59 50 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 YPE short int.#
46580 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 endif.#endif.#if
46590 6e 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a ndef UINT8_TYPE.
465a0 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e # ifdef HAVE_UIN
465b0 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 T8_T.# define U
465c0 49 4e 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f INT8_TYPE uint8_
465d0 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 t.# else.# defi
465e0 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e ne UINT8_TYPE un
465f0 73 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e signed char.# en
46600 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 dif.#endif.#ifnd
46610 65 66 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 ef INT8_TYPE.# i
46620 66 64 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 fdef HAVE_INT8_T
46630 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f .# define INT8_
46640 54 59 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c TYPE int8_t.# el
46650 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 se.# define INT
46660 38 5f 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 8_TYPE signed ch
46670 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 ar.# endif.#endi
46680 66 0a 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f f.#ifndef LONGDO
46690 55 42 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 UBLE_TYPE.# defi
466a0 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 ne LONGDOUBLE_TY
466b0 50 45 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 PE long double.#
466c0 65 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 endif.typedef sq
466d0 6c 69 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 lite_int64 i64;
466e0 20 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 /* 8-by
466f0 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
46700 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c r */.typedef sql
46710 69 74 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 ite_uint64 u64;
46720 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 /* 8-byt
46730 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 e unsigned integ
46740 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 er */.typedef UI
46750 4e 54 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 NT32_TYPE u32;
46760 20 20 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 /* 4-by
46770 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 te unsigned inte
46780 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 ger */.typedef U
46790 49 4e 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 INT16_TYPE u16;
467a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 /* 2-b
467b0 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 yte unsigned int
467c0 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 eger */.typedef
467d0 49 4e 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 INT16_TYPE i16;
467e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d /* 2-
467f0 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 byte signed inte
46800 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 ger */.typedef U
46810 49 4e 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 INT8_TYPE u8;
46820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 /* 1-b
46830 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 yte unsigned int
46840 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 eger */.typedef
46850 49 4e 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 INT8_TYPE i8;
46860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d /* 1-
46870 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 byte signed inte
46880 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 ger */../*.** Ma
46890 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e cros to determin
468a0 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d 61 e whether the ma
468b0 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20 chine is big or
468c0 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a little endian,.*
468d0 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 72 * evaluated at r
468e0 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 untime..*/.#ifde
468f0 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d f SQLITE_AMALGAM
46900 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 ATION.SQLITE_PRI
46910 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 VATE const int s
46920 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 qlite3one = 1;.#
46930 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 else.SQLITE_PRIV
46940 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 ATE const int sq
46950 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 66 lite3one;.#endif
46960 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 38 .#if defined(i38
46970 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 6) || defined(__
46980 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e i386__) || defin
46990 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20 20 ed(_M_IX86)\.
469a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
469b0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 66 || def
469c0 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 7c ined(__x86_64) |
469d0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f | defined(__x86_
469e0 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 64__).# define S
469f0 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 QLITE_BIGENDIAN
46a00 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 51 0.# define SQ
46a10 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 LITE_LITTLEENDIA
46a20 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c N 1.# define SQL
46a30 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 ITE_UTF16NATIVE
46a40 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a SQLITE_UTF16LE.
46a50 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
46a60 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 QLITE_BIGENDIAN
46a70 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73 (*(char *)(&s
46a80 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a 23 qlite3one)==0).#
46a90 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c define SQLITE_L
46aa0 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 63 ITTLEENDIAN (*(c
46ab0 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f har *)(&sqlite3o
46ac0 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e 65 ne)==1).# define
46ad0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
46ae0 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 45 IVE (SQLITE_BIGE
46af0 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 46 NDIAN?SQLITE_UTF
46b00 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 31 16BE:SQLITE_UTF1
46b10 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 6LE).#endif../*.
46b20 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f 72 ** Constants for
46b30 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e 64 the largest and
46b40 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 smallest possib
46b50 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 le 64-bit signed
46b60 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 integers..** Th
46b70 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 64 ese macros are d
46b80 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20 esigned to work
46b90 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f 74 correctly on bot
46ba0 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 2d h 32-bit and 64-
46bb0 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 73 bit.** compilers
46bc0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 52 ..*/.#define LAR
46bd0 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 66 GEST_INT64 (0xf
46be0 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 30 fffffff|(((i64)0
46bf0 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 29 x7fffffff)<<32))
46c00 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 53 .#define SMALLES
46c10 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 2d T_INT64 (((i64)-
46c20 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e 54 1) - LARGEST_INT
46c30 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75 6e 64)../* .** Roun
46c40 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 6f d up a number to
46c50 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 the next larger
46c60 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20 multiple of 8.
46c70 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a This is used.**
46c80 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 65 to force 8-byte
46c90 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 34 alignment on 64
46ca0 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 72 -bit architectur
46cb0 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 es..*/.#define R
46cc0 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28 28 OUND8(x) (((
46cd0 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a x)+7)&~7)../*.**
46ce0 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 74 Round down to t
46cf0 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74 69 he nearest multi
46d00 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65 66 ple of 8.*/.#def
46d10 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28 78 ine ROUNDDOWN8(x
46d20 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a 2a ) ((x)&~7)../*.*
46d30 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 * Assert that th
46d40 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20 61 e pointer X is a
46d50 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 ligned to an 8-b
46d60 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f yte boundary..*/
46d70 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f 42 .#define EIGHT_B
46d80 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29 YTE_ALIGNMENT(X)
46d90 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 29 ((((char*)(X)
46da0 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 3d - (char*)0)&7)=
46db0 3d 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e =0)../*.** An in
46dc0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
46dd0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
46de0 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f e is used to sto
46df0 72 65 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 re the busy-hand
46e00 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 ler.** callback
46e10 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71 6c 69 for a given sqli
46e20 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a te handle. .**.*
46e30 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62 75 73 * The sqlite.bus
46e40 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65 72 20 yHandler member
46e50 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 73 74 of the sqlite st
46e60 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 ruct contains th
46e70 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c 62 61 e busy.** callba
46e80 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 ck for the datab
46e90 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61 63 68 ase handle. Each
46ea0 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20 76 69 pager opened vi
46eb0 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 a the sqlite.**
46ec0 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 65 64 handle is passed
46ed0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 73 71 a pointer to sq
46ee0 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 lite.busyHandler
46ef0 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e 64 6c . The busy-handl
46f00 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 er.** callback i
46f10 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 76 6f s currently invo
46f20 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 ked only from wi
46f30 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a 2a 2f thin pager.c..*/
46f40 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
46f50 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75 73 79 BusyHandler Busy
46f60 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63 74 20 Handler;.struct
46f70 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a 20 20 BusyHandler {.
46f80 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69 int (*xFunc)(voi
46f90 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 d *,int); /* Th
46fa0 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 e busy callback
46fb0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b */. void *pArg;
46fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46fd0 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 /* First arg to
46fe0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f busy callback */
46ff0 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 20 20 . int nBusy;
47000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
47010 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 Incremented wit
47020 68 20 65 61 63 68 20 62 75 73 79 20 63 61 6c 6c h each busy call
47030 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 */.};../*.** Na
47040 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 me of the master
47050 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e database table.
47060 20 20 54 68 65 20 6d 61 73 74 65 72 20 64 61 74 The master dat
47070 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a 20 69 abase table.** i
47080 73 20 61 20 73 70 65 63 69 61 6c 20 74 61 62 6c s a special tabl
47090 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 e that holds the
470a0 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74 72 69 names and attri
470b0 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20 butes of all.**
470c0 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e 64 20 user tables and
470d0 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64 65 66 indices..*/.#def
470e0 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d 45 20 ine MASTER_NAME
470f0 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f 6d 61 "sqlite_ma
47100 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20 54 45 ster".#define TE
47110 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 MP_MASTER_NAME
47120 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 "sqlite_temp_mas
47130 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ter"../*.** The
47140 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 65 root-page of the
47150 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 master database
47160 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 table..*/.#defi
47170 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54 20 20 ne MASTER_ROOT
47180 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 1../*.** Th
47190 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 73 63 e name of the sc
471a0 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 hema table..*/.#
471b0 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f 54 41 define SCHEMA_TA
471c0 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49 54 5f BLE(x) ((!OMIT_
471d0 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31 29 3f TEMPDB)&&(x==1)?
471e0 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 TEMP_MASTER_NAME
471f0 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f :MASTER_NAME)../
47200 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69 65 6e *.** A convenien
47210 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20 72 65 ce macro that re
47220 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
47230 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a of elements in.
47240 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a ** an array..*/.
47250 23 64 65 66 69 6e 65 20 41 72 72 61 79 53 69 7a #define ArraySiz
47260 65 28 58 29 20 20 20 20 28 28 69 6e 74 29 28 73 e(X) ((int)(s
47270 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f 66 28 izeof(X)/sizeof(
47280 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 X[0])))../*.** T
47290 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c he following val
472a0 75 65 20 61 73 20 61 20 64 65 73 74 72 75 63 74 ue as a destruct
472b0 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 or means to use
472c0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 2e sqlite3DbFree().
472d0 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 .** This is an i
472e0 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73 69 6f nternal extensio
472f0 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54 41 54 n to SQLITE_STAT
47300 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f 54 52 IC and SQLITE_TR
47310 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64 65 66 ANSIENT..*/.#def
47320 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d ine SQLITE_DYNAM
47330 49 43 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 IC ((sqlite3_d
47340 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 73 estructor_type)s
47350 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a 0a 2f qlite3DbFree)../
47360 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49 54 45 *.** When SQLITE
47370 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64 65 66 _OMIT_WSD is def
47380 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 ined, it means t
47390 68 61 74 20 74 68 65 20 74 61 72 67 65 74 20 70 hat the target p
473a0 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20 latform does.**
473b0 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72 69 74 not support Writ
473c0 61 62 6c 65 20 53 74 61 74 69 63 20 44 61 74 61 able Static Data
473d0 20 28 57 53 44 29 20 73 75 63 68 20 61 73 20 67 (WSD) such as g
473e0 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74 69 63 lobal and static
473f0 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 20 41 variables..** A
47400 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 75 73 ll variables mus
47410 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e 20 74 t either be on t
47420 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79 6e 61 he stack or dyna
47430 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
47440 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 68 65 d from.** the he
47450 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20 69 73 ap. When WSD is
47460 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20 74 68 unsupported, th
47470 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 e variable decla
47480 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65 72 65 rations scattere
47490 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75 74 20 d.** throughout
474a0 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20 the SQLite code
474b0 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f 6e 73 must become cons
474c0 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e 20 20 tants instead.
474d0 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44 0a 2a The SQLITE_WSD.*
474e0 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 * macro is used
474f0 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 for this purpose
47500 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64 20 6f . And instead o
47510 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20 74 68 f referencing th
47520 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 64 69 e variable.** di
47530 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65 20 69 rectly, we use i
47540 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73 20 61 ts constant as a
47550 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 key to lookup t
47560 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f he run-time allo
47570 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65 72 20 cated.** buffer
47580 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61 6c 20 that holds real
47590 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65 20 63 variable. The c
475a0 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73 6f 20 onstant is also
475b0 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 0a the initializer.
475c0 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e 2d 74 ** for the run-t
475d0 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 ime allocated bu
475e0 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 ffer..**.** In t
475f0 68 65 20 75 73 75 61 6c 20 63 61 73 65 20 77 68 he usual case wh
47600 65 72 65 20 57 53 44 20 69 73 20 73 75 70 70 6f ere WSD is suppo
47610 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49 54 45 rted, the SQLITE
47620 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41 4c 0a _WSD and GLOBAL.
47630 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f 6d 65 ** macros become
47640 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61 76 65 no-ops and have
47650 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61 6e 63 zero performanc
47660 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 69 66 e impact..*/.#if
47670 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
47680 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20 53 51 WSD. #define SQ
47690 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74 0a 20 LITE_WSD const.
476a0 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 #define GLOBAL(
476b0 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c 69 74 t,v) (*(t*)sqlit
476c0 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76 6f 69 e3_wsd_find((voi
476d0 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f 66 28 d*)&(v), sizeof(
476e0 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65 20 73 v))). #define s
476f0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
47700 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 ig GLOBAL(struct
47710 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 2c 20 Sqlite3Config,
47720 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29 0a 53 sqlite3Config).S
47730 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e 74 20 QLITE_API int
47740 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 74 sqlite3_wsd_init
47750 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a (int N, int J);.
47760 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76 6f 69 SQLITE_API voi
47770 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 d *sqlite3_wsd_f
47780 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69 6e 74 ind(void *K, int
47790 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 L);.#else. #de
477a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 fine SQLITE_WSD
477b0 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 . #define GLOBA
477c0 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65 66 69 L(t,v) v. #defi
477d0 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ne sqlite3Global
477e0 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f Config sqlite3Co
477f0 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a nfig.#endif../*.
47800 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
47810 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 macros are used
47820 20 74 6f 20 73 75 70 70 72 65 73 73 20 63 6f 6d to suppress com
47830 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 61 piler warnings a
47840 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74 nd to.** make it
47850 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61 6e 20 clear to human
47860 72 65 61 64 65 72 73 20 77 68 65 6e 20 61 20 66 readers when a f
47870 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 unction paramete
47880 72 20 69 73 20 64 65 6c 69 62 65 72 61 74 65 6c r is deliberatel
47890 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 73 65 y .** left unuse
478a0 64 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 d within the bod
478b0 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2e y of a function.
478c0 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20 68 61 This usually ha
478d0 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 61 20 ppens when.** a
478e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
478f0 65 64 20 76 69 61 20 61 20 66 75 6e 63 74 69 6f ed via a functio
47900 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72 20 65 n pointer. For e
47910 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a 20 69 xample the .** i
47920 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
47930 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67 61 74 an SQL aggregat
47940 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63 6b 20 e step callback
47950 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68 65 0a may not use the.
47960 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 ** parameter ind
47970 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 icating the numb
47980 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
47990 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 61 67 passed to the ag
479a0 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66 20 69 gregate,.** if i
479b0 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 69 t knows that thi
479c0 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20 65 6c s is enforced el
479d0 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 sewhere..**.** W
479e0 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 hen a function p
479f0 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 arameter is not
47a00 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69 74 68 used at all with
47a10 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 in the body of a
47a20 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74 function,.** it
47a30 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20 6e 61 is generally na
47a40 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20 6f 72 med "NotUsed" or
47a50 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f 20 6d "NotUsed2" to m
47a60 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65 6e 20 ake things even
47a70 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f 77 65 clearer..** Howe
47a80 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63 72 6f ver, these macro
47a90 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 75 73 s may also be us
47aa0 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 77 ed to suppress w
47ab0 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65 64 20 arnings related
47ac0 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 to.** parameters
47ad0 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d 61 79 that may or may
47ae0 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64 65 70 not be used dep
47af0 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c ending on compil
47b00 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a ation options..*
47b10 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 * For example th
47b20 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f ose parameters o
47b30 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73 73 65 nly used in asse
47b40 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
47b50 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63 61 73 In these.** cas
47b60 65 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 es the parameter
47b70 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73 20 70 s are named as p
47b80 65 72 20 74 68 65 20 75 73 75 61 6c 20 63 6f 6e er the usual con
47b90 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 ventions..*/.#de
47ba0 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 41 fine UNUSED_PARA
47bb0 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64 29 28 METER(x) (void)(
47bc0 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 x).#define UNUSE
47bd0 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78 2c 79 D_PARAMETER2(x,y
47be0 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 ) UNUSED_PARAMET
47bf0 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50 41 52 ER(x),UNUSED_PAR
47c00 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a AMETER(y)../*.**
47c10 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
47c20 63 65 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 ces to structure
47c30 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 s.*/.typedef str
47c40 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67 67 49 uct AggInfo AggI
47c50 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 nfo;.typedef str
47c60 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20 uct AuthContext
47c70 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74 79 70 AuthContext;.typ
47c80 65 64 65 66 20 73 74 72 75 63 74 20 42 69 74 76 edef struct Bitv
47c90 65 63 20 42 69 74 76 65 63 3b 0a 74 79 70 65 64 ec Bitvec;.typed
47ca0 65 66 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 ef struct RowSet
47cb0 20 52 6f 77 53 65 74 3b 0a 74 79 70 65 64 65 66 RowSet;.typedef
47cc0 20 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 struct CollSeq
47cd0 43 6f 6c 6c 53 65 71 3b 0a 74 79 70 65 64 65 66 CollSeq;.typedef
47ce0 20 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 43 struct Column C
47cf0 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64 65 66 20 73 olumn;.typedef s
47d00 74 72 75 63 74 20 44 62 20 44 62 3b 0a 74 79 70 truct Db Db;.typ
47d10 65 64 65 66 20 73 74 72 75 63 74 20 53 63 68 65 edef struct Sche
47d20 6d 61 20 53 63 68 65 6d 61 3b 0a 74 79 70 65 64 ma Schema;.typed
47d30 65 66 20 73 74 72 75 63 74 20 45 78 70 72 20 45 ef struct Expr E
47d40 78 70 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 xpr;.typedef str
47d50 75 63 74 20 45 78 70 72 4c 69 73 74 20 45 78 70 uct ExprList Exp
47d60 72 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 rList;.typedef s
47d70 74 72 75 63 74 20 46 4b 65 79 20 46 4b 65 79 3b truct FKey FKey;
47d80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
47d90 46 75 6e 63 44 65 66 20 46 75 6e 63 44 65 66 3b FuncDef FuncDef;
47da0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
47db0 46 75 6e 63 44 65 66 48 61 73 68 20 46 75 6e 63 FuncDefHash Func
47dc0 44 65 66 48 61 73 68 3b 0a 74 79 70 65 64 65 66 DefHash;.typedef
47dd0 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 49 struct IdList I
47de0 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 dList;.typedef s
47df0 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65 truct Index Inde
47e00 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 x;.typedef struc
47e10 74 20 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c t KeyClass KeyCl
47e20 61 73 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ass;.typedef str
47e30 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49 uct KeyInfo KeyI
47e40 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 nfo;.typedef str
47e50 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f uct Lookaside Lo
47e60 6f 6b 61 73 69 64 65 3b 0a 74 79 70 65 64 65 66 okaside;.typedef
47e70 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 struct Lookasid
47e80 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53 eSlot LookasideS
47e90 6c 6f 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 lot;.typedef str
47ea0 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c uct Module Modul
47eb0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 e;.typedef struc
47ec0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61 t NameContext Na
47ed0 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 meContext;.typed
47ee0 65 66 20 73 74 72 75 63 74 20 50 61 72 73 65 20 ef struct Parse
47ef0 50 61 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73 Parse;.typedef s
47f00 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 truct Savepoint
47f10 53 61 76 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64 Savepoint;.typed
47f20 65 66 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 ef struct Select
47f30 20 53 65 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 Select;.typedef
47f40 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 20 struct SrcList
47f50 53 72 63 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 SrcList;.typedef
47f60 20 73 74 72 75 63 74 20 53 74 72 41 63 63 75 6d struct StrAccum
47f70 20 53 74 72 41 63 63 75 6d 3b 0a 74 79 70 65 64 StrAccum;.typed
47f80 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 20 ef struct Table
47f90 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 Table;.typedef s
47fa0 74 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 truct TableLock
47fb0 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 TableLock;.typed
47fc0 65 66 20 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 ef struct Token
47fd0 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 65 66 20 73 Token;.typedef s
47fe0 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61 truct TriggerSta
47ff0 63 6b 20 54 72 69 67 67 65 72 53 74 61 63 6b 3b ck TriggerStack;
48000 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
48010 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67 TriggerStep Trig
48020 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66 gerStep;.typedef
48030 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 struct Trigger
48040 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66 Trigger;.typedef
48050 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64 struct Unpacked
48060 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52 Record UnpackedR
48070 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73 ecord;.typedef s
48080 74 72 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c truct Walker Wal
48090 6b 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ker;.typedef str
480a0 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 uct WherePlan Wh
480b0 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 erePlan;.typedef
480c0 20 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 struct WhereInf
480d0 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 o WhereInfo;.typ
480e0 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 edef struct Wher
480f0 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 eLevel WhereLeve
48100 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 l;../*.** Defer
48110 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 sourcing vdbe.h
48120 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 and btree.h unti
48130 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 38 22 l after the "u8"
48140 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 and .** "BusyHa
48150 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e ndler" typedefs.
48160 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 vdbe.h also req
48170 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 uires a few of t
48180 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 he opaque.** poi
48190 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e nter types (i.e.
481a0 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 FuncDef) define
481b0 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a d above..*/./***
481c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
481d0 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 ude btree.h in t
481e0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
481f0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
48200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
48210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
48220 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a n file btree.h *
48230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
48260 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
48270 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
48280 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
48290 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
482a0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
482b0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
482c0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
482d0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
482e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
482f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
48300 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
48310 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
48320 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
48330 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
48340 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
48350 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
48360 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
48370 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
48380 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
48390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
483d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 **.** This heade
483e0 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 r file defines t
483f0 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 he interface tha
48400 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 t the sqlite B-T
48410 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 ree file.** subs
48420 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d ystem. See comm
48430 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 ents in the sour
48440 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 ce code for a de
48450 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 tailed descripti
48460 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 on.** of what ea
48470 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 ch interface rou
48480 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a tine does..**.**
48490 20 40 28 23 29 20 24 49 64 3a 20 62 74 72 65 65 @(#) $Id: btree
484a0 2e 68 2c 76 20 31 2e 31 31 34 20 32 30 30 39 2f .h,v 1.114 2009/
484b0 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 20 64 05/04 11:42:30 d
484c0 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
484d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 52 .*/.#ifndef _BTR
484e0 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42 EE_H_.#define _B
484f0 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f TREE_H_../* TODO
48500 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 6f : This definitio
48510 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 64 n is just includ
48520 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75 ed so other modu
48530 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a les compile. It.
48540 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 ** needs to be r
48550 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 evisited..*/.#de
48560 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 fine SQLITE_N_BT
48570 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a REE_META 10../*.
48580 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73 ** If defined as
48590 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d non-zero, auto-
485a0 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 vacuum is enable
485b0 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 d by default. Ot
485c0 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 herwise.** it mu
485d0 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 st be turned on
485e0 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 for each databas
485f0 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20 e using "PRAGMA
48600 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 auto_vacuum = 1"
48610 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
48620 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
48630 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e 65 VACUUM. #define
48640 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
48650 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e AUTOVACUUM 0.#en
48660 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 52 dif..#define BTR
48670 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f EE_AUTOVACUUM_NO
48680 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 44 NE 0 /* D
48690 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61 o not do auto-va
486a0 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cuum */.#define
486b0 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
486c0 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 2f _FULL 1 /
486d0 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76 * Do full auto-v
486e0 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 acuum */.#define
486f0 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 BTREE_AUTOVACUU
48700 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 20 M_INCR 2
48710 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 /* Incremental v
48720 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 acuum */../*.**
48730 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 Forward declarat
48740 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 ions of structur
48750 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 e.*/.typedef str
48760 75 63 74 20 42 74 72 65 65 20 42 74 72 65 65 3b uct Btree Btree;
48770 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
48780 42 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f BtCursor BtCurso
48790 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
487a0 74 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61 t BtShared BtSha
487b0 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72 red;.typedef str
487c0 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 uct BtreeMutexAr
487d0 72 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 72 ray BtreeMutexAr
487e0 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ray;../*.** This
487f0 20 73 74 72 75 63 74 75 72 65 20 72 65 63 6f 72 structure recor
48800 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 74 ds all of the Bt
48810 72 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 rees that need t
48820 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 o hold.** a mute
48830 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 65 x before we ente
48840 72 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 r sqlite3VdbeExe
48850 63 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 73 c(). The Btrees
48860 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63 are.** are plac
48870 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 69 ed in aBtree[] i
48880 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 65 n order of aBtre
48890 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 20 e[]->pBt. That
488a0 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61 way,.** we can a
488b0 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75 lways lock and u
488c0 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71 nlock them all q
488d0 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 uickly..*/.struc
488e0 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 t BtreeMutexArra
488f0 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78 y {. int nMutex
48900 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 65 ;. Btree *aBtre
48910 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 e[SQLITE_MAX_ATT
48920 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 ACHED+1];.};...S
48930 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
48940 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 t sqlite3BtreeOp
48950 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 en(. const char
48960 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f *zFilename, /
48970 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 * Name of databa
48980 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 se file to open
48990 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
489a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
489b0 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 Associated data
489c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
489d0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 2c 20 20 */. Btree **,
489e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
489f0 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 Return open Btr
48a00 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ee* here */. in
48a10 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
48a20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
48a30 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 */. int vfsFlag
48a40 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s /*
48a50 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 Flags passed th
48a60 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65 rough to VFS ope
48a70 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 n */.);../* The
48a80 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
48a90 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f to sqlite3BtreeO
48aa0 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 62 pen can be the b
48ab0 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 65 itwise or of the
48ac0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 .** following va
48ad0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 lues..**.** NOTE
48ae0 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 : These values
48af0 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 must match the c
48b00 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 orresponding PAG
48b10 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a ER_ values in.**
48b20 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 pager.h..*/.#de
48b30 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f fine BTREE_OMIT_
48b40 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 JOURNAL 1 /* D
48b50 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61 o not use journa
48b60 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 l. No argument
48b70 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
48b80 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 _NO_READLOCK 2
48b90 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f /* Omit readlo
48ba0 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 cks on readonly
48bb0 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 files */.#define
48bc0 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 BTREE_MEMORY
48bd0 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 4 /* In-me
48be0 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 67 mory DB. No arg
48bf0 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 ument */.#define
48c00 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20 BTREE_READONLY
48c10 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20 8 /* Open
48c20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 the database in
48c30 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a read-only mode *
48c40 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
48c50 52 45 41 44 57 52 49 54 45 20 20 20 20 31 36 20 READWRITE 16
48c60 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74 /* Open for bot
48c70 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 h reading and wr
48c80 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 iting */.#define
48c90 20 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 20 BTREE_CREATE
48ca0 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 74 32 /* Creat
48cb0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 e the database i
48cc0 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 f it does not ex
48cd0 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 ist */..SQLITE_P
48ce0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
48cf0 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72 e3BtreeClose(Btr
48d00 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
48d10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
48d20 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a BtreeSetCacheSiz
48d30 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 e(Btree*,int);.S
48d40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
48d50 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
48d60 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 tSafetyLevel(Btr
48d70 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 ee*,int,int);.SQ
48d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
48d90 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e sqlite3BtreeSyn
48da0 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 2a cDisabled(Btree*
48db0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
48dc0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
48dd0 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 eeSetPageSize(Bt
48de0 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ree*,int,int,int
48df0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
48e00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
48e10 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 eeGetPageSize(Bt
48e20 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
48e30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
48e40 33 42 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 3BtreeMaxPageCou
48e50 6e 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a nt(Btree*,int);.
48e60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
48e70 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
48e80 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 2a etReserve(Btree*
48e90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
48ea0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
48eb0 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 eeSetAutoVacuum(
48ec0 42 74 72 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 Btree *, int);.S
48ed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
48ee0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
48ef0 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 tAutoVacuum(Btre
48f00 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e *);.SQLITE_PRI
48f10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
48f20 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 BtreeBeginTrans(
48f30 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c Btree*,int);.SQL
48f40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
48f50 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
48f60 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65 itPhaseOne(Btree
48f70 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
48f80 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f Master);.SQLITE_
48f90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
48fa0 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
48fb0 61 73 65 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a aseTwo(Btree*);.
48fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
48fd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
48fe0 6f 6d 6d 69 74 28 42 74 72 65 65 2a 29 3b 0a 53 ommit(Btree*);.S
48ff0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
49000 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f t sqlite3BtreeRo
49010 6c 6c 62 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a llback(Btree*);.
49020 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
49030 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 nt sqlite3BtreeB
49040 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65 2a 2c eginStmt(Btree*,
49050 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
49060 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
49070 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 BtreeCreateTable
49080 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 (Btree*, int*, i
49090 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 4c 49 54 nt flags);.SQLIT
490a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
490b0 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 lite3BtreeIsInTr
490c0 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c ans(Btree*);.SQL
490d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
490e0 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
490f0 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 2a ReadTrans(Btree*
49100 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
49110 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
49120 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 74 72 eeIsInBackup(Btr
49130 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
49140 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
49150 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 e3BtreeSchema(Bt
49160 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 ree *, int, void
49170 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51 (*)(void *));.SQ
49180 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
49190 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 sqlite3BtreeSch
491a0 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 emaLocked(Btree
491b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
491c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
491d0 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 reeLockTable(Btr
491e0 65 65 20 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a ee *, int, u8);.
491f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
49200 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
49210 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a avepoint(Btree *
49220 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 , int, int);..SQ
49230 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
49240 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
49250 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
49260 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
49270 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
49280 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 char *sqlite3Btr
49290 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 eeGetJournalname
492a0 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
492b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
492c0 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 lite3BtreeCopyFi
492d0 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65 le(Btree *, Btre
492e0 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 e *);..SQLITE_PR
492f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
49300 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 3BtreeIncrVacuum
49310 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54 (Btree *);../* T
49320 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 he flags paramet
49330 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 er to sqlite3Btr
49340 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61 eeCreateTable ca
49350 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65 n be the bitwise
49360 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f OR.** of the fo
49370 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a llowing flags:.*
49380 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
49390 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20 INTKEY 1
493a0 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c /* Table has onl
493b0 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 y 64-bit signed
493c0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a integer keys */.
493d0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45 #define BTREE_ZE
493e0 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a RODATA 2 /*
493f0 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20 Table has keys
49400 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a only - no data *
49410 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
49420 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20 LEAFDATA 4
49430 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69 /* Data stored i
49440 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20 n leaves only.
49450 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a Implies INTKEY *
49460 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 /..SQLITE_PRIVAT
49470 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
49480 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 eeDropTable(Btre
49490 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a e*, int, int*);.
494a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
494b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
494c0 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a learTable(Btree*
494d0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 , int, int*);.SQ
494e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
494f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
49500 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 Meta(Btree*, int
49510 20 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 idx, u32 *pValu
49520 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
49530 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
49540 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 reeUpdateMeta(Bt
49550 72 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 ree*, int idx, u
49560 33 32 20 76 61 6c 75 65 29 3b 0a 53 51 4c 49 54 32 value);.SQLIT
49570 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
49580 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 qlite3BtreeTripA
49590 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 2a llCursors(Btree*
495a0 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f , int);..SQLITE_
495b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
495c0 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a te3BtreeCursor(.
495d0 20 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 Btree*,
495e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
495f0 20 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 /* BTree
49600 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 containing table
49610 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e to open */. in
49620 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 t iTable,
49630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49640 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 /* Index of r
49650 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e oot page */. in
49660 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 t wrFlag,
49670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49680 20 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 /* 1 for writ
49690 69 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 ing. 0 for read
496a0 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 -only */. struc
496b0 74 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 t KeyInfo*,
496c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
496d0 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e /* First argumen
496e0 74 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e t to compare fun
496f0 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 ction */. BtCur
49700 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 sor *pCursor
49710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49720 2f 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 /* Space to writ
49730 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 e cursor structu
49740 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f re */.);.SQLITE_
49750 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
49760 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 te3BtreeCursorSi
49770 7a 65 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 ze(void);..SQLIT
49780 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
49790 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 lite3BtreeCloseC
497a0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 ursor(BtCursor*)
497b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
497c0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
497d0 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 eMoveto(. BtCur
497e0 73 6f 72 2a 2c 0a 20 20 63 6f 6e 73 74 20 76 6f sor*,. const vo
497f0 69 64 20 2a 70 4b 65 79 2c 0a 20 20 69 36 34 20 id *pKey,. i64
49800 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 nKey,. int bias
49810 2c 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b ,. int *pRes.);
49820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
49830 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
49840 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a MovetoUnpacked(.
49850 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 BtCursor*,. U
49860 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
49870 55 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 UnKey,. i64 int
49880 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c Key,. int bias,
49890 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a . int *pRes.);.
498a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
498b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
498c0 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 ursorHasMoved(Bt
498d0 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a Cursor*, int*);.
498e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
498f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
49900 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 elete(BtCursor*)
49910 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
49920 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
49930 65 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 eInsert(BtCursor
49940 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 *, const void *p
49950 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 Key, i64 nKey,.
49960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49980 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 const void *pDa
49990 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 ta, int nData,.
499a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
499b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
499c0 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 int nZero, int
499d0 62 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 bias, int seekRe
499e0 73 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sult);.SQLITE_PR
499f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
49a00 33 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 3BtreeFirst(BtCu
49a10 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 rsor*, int *pRes
49a20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
49a30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
49a40 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a eeLast(BtCursor*
49a50 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 , int *pRes);.SQ
49a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
49a70 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
49a80 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 t(BtCursor*, int
49a90 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f *pRes);.SQLITE_
49aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
49ab0 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 te3BtreeEof(BtCu
49ac0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
49ad0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
49ae0 65 33 42 74 72 65 65 46 6c 61 67 73 28 42 74 43 e3BtreeFlags(BtC
49af0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ursor*);.SQLITE_
49b00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
49b10 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
49b20 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
49b30 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 *pRes);.SQLITE_P
49b40 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
49b50 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 e3BtreeKeySize(B
49b60 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 tCursor*, i64 *p
49b70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Size);.SQLITE_PR
49b80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
49b90 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 3BtreeKey(BtCurs
49ba0 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c or*, u32 offset,
49bb0 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 u32 amt, void*)
49bc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
49bd0 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 sqlite3 *sqlite
49be0 33 42 74 72 65 65 43 75 72 73 6f 72 44 62 28 63 3BtreeCursorDb(c
49bf0 6f 6e 73 74 20 42 74 43 75 72 73 6f 72 2a 29 3b onst BtCursor*);
49c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
49c10 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
49c20 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 te3BtreeKeyFetch
49c30 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
49c40 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *pAmt);.SQLITE_P
49c50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 RIVATE const voi
49c60 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 d *sqlite3BtreeD
49c70 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f ataFetch(BtCurso
49c80 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a r*, int *pAmt);.
49c90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
49ca0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
49cb0 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 ataSize(BtCursor
49cc0 2a 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a *, u32 *pSize);.
49cd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
49ce0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
49cf0 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 ata(BtCursor*, u
49d00 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 32 offset, u32 a
49d10 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 mt, void*);.SQLI
49d20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
49d30 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
49d40 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 achedRowid(BtCur
49d50 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e sor*, sqlite3_in
49d60 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t64);.SQLITE_PRI
49d70 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 VATE sqlite3_int
49d80 36 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 64 sqlite3BtreeG
49d90 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 etCachedRowid(Bt
49da0 43 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 Cursor*);..SQLIT
49db0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
49dc0 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 sqlite3BtreeInte
49dd0 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65 grityCheck(Btree
49de0 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 *, int *aRoot, i
49df0 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 nt nRoot, int, i
49e00 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
49e10 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65 VATE struct Page
49e20 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 r *sqlite3BtreeP
49e30 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 ager(Btree*);..S
49e40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
49e50 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 t sqlite3BtreePu
49e60 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c tData(BtCursor*,
49e70 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 u32 offset, u32
49e80 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 amt, void*);.SQ
49e90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
49ea0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 d sqlite3BtreeCa
49eb0 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 cheOverflow(BtCu
49ec0 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f rsor *);.SQLITE_
49ed0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
49ee0 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 ite3BtreeClearCu
49ef0 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29 rsor(BtCursor *)
49f00 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
49f10 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e E_OMIT_BTREECOUN
49f20 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
49f30 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
49f40 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 eCount(BtCursor
49f50 2a 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69 *, i64 *);.#endi
49f60 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
49f70 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 _TEST.SQLITE_PRI
49f80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
49f90 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 BtreeCursorInfo(
49fa0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c BtCursor*, int*,
49fb0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
49fc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
49fd0 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73 e3BtreeCursorLis
49fe0 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 t(Btree*);.#endi
49ff0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 f../*.** If we a
4a000 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 re not using sha
4a010 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20 red cache, then
4a020 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
4a030 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 to.** use mutex
4a040 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 es to access the
4a050 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 BtShared struct
4a060 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 ures. So make t
4a070 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 he.** Enter and
4a080 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73 Leave procedures
4a090 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e no-ops..*/.#ifn
4a0a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
4a0b0 53 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c SHARED_CACHE.SQL
4a0c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4a0d0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
4a0e0 6e 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 nter(Btree*);.SQ
4a0f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
4a100 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4a110 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 EnterAll(sqlite3
4a120 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 *);.#else.# defi
4a130 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 ne sqlite3BtreeE
4a140 6e 74 65 72 28 58 29 20 0a 23 20 64 65 66 69 6e nter(X) .# defin
4a150 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e e sqlite3BtreeEn
4a160 74 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66 terAll(X).#endif
4a170 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
4a180 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
4a190 44 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c 49 D_CACHE) && SQLI
4a1a0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 51 TE_THREADSAFE.SQ
4a1b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
4a1c0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4a1d0 4c 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 Leave(Btree*);.S
4a1e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4a1f0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
4a200 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 eEnterCursor(BtC
4a210 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ursor*);.SQLITE_
4a220 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
4a230 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
4a240 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a Cursor(BtCursor*
4a250 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4a260 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
4a270 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 BtreeLeaveAll(sq
4a280 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
4a290 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
4a2a0 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
4a2b0 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 ArrayEnter(Btree
4a2c0 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 MutexArray*);.SQ
4a2d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
4a2e0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4a2f0 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 MutexArrayLeave(
4a300 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a BtreeMutexArray*
4a310 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4a320 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
4a330 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 BtreeMutexArrayI
4a340 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 nsert(BtreeMutex
4a350 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b Array*, Btree*);
4a360 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
4a370 20 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74 69 /* These routi
4a380 6e 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 73 nes are used ins
4a390 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
4a3a0 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f tements only. */
4a3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4a3c0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 int sqlite3Btr
4a3d0 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 eeHoldsMutex(Btr
4a3e0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4a3f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
4a400 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d e3BtreeHoldsAllM
4a410 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a 29 utexes(sqlite3*)
4a420 3b 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a ;.#endif.#else..
4a430 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
4a440 42 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 20 BtreeLeave(X).#
4a450 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4a460 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 reeEnterCursor(X
4a470 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
4a480 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
4a490 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 or(X).# define s
4a4a0 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
4a4b0 41 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 All(X).# define
4a4c0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
4a4d0 78 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 xArrayEnter(X).#
4a4e0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4a4f0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 treeMutexArrayLe
4a500 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 ave(X).# define
4a510 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
4a520 78 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 xArrayInsert(X,Y
4a530 29 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 )..# define sqli
4a540 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
4a550 65 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65 ex(X) 1.# define
4a560 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
4a570 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 dsAllMutexes(X)
4a580 31 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 1.#endif...#endi
4a590 66 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a f /* _BTREE_H_ *
4a5a0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
4a5b0 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e ** End of btree.
4a5c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4a5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a5f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4a600 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
4a610 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
4a620 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a in sqliteInt.h *
4a630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a640 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4a650 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e ** Include vdbe.
4a660 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
4a670 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a of sqliteInt.h *
4a680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a690 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4a6a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
4a6b0 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a be.h ***********
4a6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a6e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
4a6f0 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
4a700 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
4a710 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
4a720 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
4a730 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
4a740 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
4a750 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
4a760 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
4a770 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
4a780 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
4a790 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
4a7a0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
4a7b0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
4a7c0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
4a7d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
4a7e0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
4a7f0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
4a800 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
4a810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4a850 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 *********.** Hea
4a860 64 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 der file for the
4a870 20 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 Virtual DataBas
4a880 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a e Engine (VDBE).
4a890 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 **.** This heade
4a8a0 72 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e r defines the in
4a8b0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 terface to the v
4a8c0 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 irtual database
4a8d0 65 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 engine.** or VDB
4a8e0 45 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 E. The VDBE imp
4a8f0 6c 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 lements an abstr
4a900 61 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 act machine that
4a910 20 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c runs a.** simpl
4a920 65 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 e program to acc
4a930 65 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 ess and modify t
4a940 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 he underlying da
4a950 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 tabase..**.** $I
4a960 64 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34 d: vdbe.h,v 1.14
4a970 31 20 32 30 30 39 2f 30 34 2f 31 30 20 30 30 3a 1 2009/04/10 00:
4a980 35 36 3a 32 39 20 64 72 68 20 45 78 70 20 24 0a 56:29 drh Exp $.
4a990 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 */.#ifndef _SQLI
4a9a0 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 TE_VDBE_H_.#defi
4a9b0 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f ne _SQLITE_VDBE_
4a9c0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 H_../*.** A sing
4a9d0 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 le VDBE is an op
4a9e0 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e aque structure n
4a9f0 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e amed "Vdbe". On
4aa00 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 ly routines.** i
4aa10 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c n the source fil
4aa20 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61 e sqliteVdbe.c a
4aa30 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 re allowed to se
4aa40 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a e the insides.**
4aa50 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
4aa60 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 re..*/.typedef s
4aa70 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b truct Vdbe Vdbe;
4aa80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 ../*.** The name
4aa90 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s of the followi
4aaa0 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65 ng types declare
4aab0 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61 d in vdbeInt.h a
4aac0 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66 re required.** f
4aad0 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65 or the VdbeOp de
4aae0 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 finition..*/.typ
4aaf0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
4ab00 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74 Func VdbeFunc;.t
4ab10 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 ypedef struct Me
4ab20 6d 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 m Mem;../*.** A
4ab30 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 single instructi
4ab40 6f 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 on of the virtua
4ab50 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e l machine has an
4ab60 20 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 opcode.** and a
4ab70 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 s many as three
4ab80 6f 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 operands. The i
4ab90 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 nstruction is re
4aba0 63 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 corded.** as an
4abb0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
4abc0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
4abd0 75 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 ure:.*/.struct V
4abe0 64 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 dbeOp {. u8 opc
4abf0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ode; /*
4ac00 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 What operation
4ac10 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 to perform */.
4ac20 73 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 signed char p4ty
4ac30 70 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 pe; /* One of th
4ac40 65 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e e P4_xxx constan
4ac50 74 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 ts for p4 */. u
4ac60 38 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 8 opflags;
4ac70 20 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e /* Not curren
4ac80 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 tly used */. u8
4ac90 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 p5;
4aca0 20 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d /* Fifth param
4acb0 65 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 eter is an unsig
4acc0 6e 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f ned character */
4acd0 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 . int p1;
4ace0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
4acf0 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 operand */. int
4ad00 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 p2;
4ad10 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d /* Second param
4ad20 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 eter (often the
4ad30 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e jump destination
4ad40 29 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 ) */. int p3;
4ad50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
4ad60 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
4ad70 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 r */. union {
4ad80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f /* fo
4ad90 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f rth parameter */
4ada0 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 . int i;
4adb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
4adc0 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 nteger value if
4add0 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 p4type==P4_INT32
4ade0 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b */. void *p;
4adf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4ae00 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 * Generic pointe
4ae10 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a r */. char *z
4ae20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4ae30 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 /* Pointer to da
4ae40 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 ta for string (c
4ae50 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 har array) types
4ae60 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 */. i64 *pI6
4ae70 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 4; /
4ae80 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
4ae90 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a pe is P4_INT64 *
4aea0 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 /. double *pR
4aeb0 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 eal; /*
4aec0 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
4aed0 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 is P4_REAL */.
4aee0 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e FuncDef *pFun
4aef0 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 c; /* Use
4af00 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4af10 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 P4_FUNCDEF */.
4af20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 VdbeFunc *pVd
4af30 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 beFunc; /* Use
4af40 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4af50 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a P4_VDBEFUNC */.
4af60 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f CollSeq *pCo
4af70 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ll; /* Us
4af80 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4af90 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a s P4_COLLSEQ */.
4afa0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 Mem *pMem;
4afb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
4afc0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4afd0 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 s P4_MEM */.
4afe0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
4aff0 74 61 62 3b 20 20 20 2f 2a 20 55 73 65 64 20 77 tab; /* Used w
4b000 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 hen p4type is P4
4b010 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 _VTAB */. Key
4b020 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
4b030 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4b040 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 p4type is P4_KE
4b050 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 YINFO */. int
4b060 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 *ai;
4b070 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4b080 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e p4type is P4_IN
4b090 54 41 52 52 41 59 20 2a 2f 0a 20 20 7d 20 70 34 TARRAY */. } p4
4b0a0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
4b0b0 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 DEBUG. char *zC
4b0c0 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 omment;
4b0d0 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 /* Comment to i
4b0e0 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69 mprove readabili
4b0f0 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ty */.#endif.#if
4b100 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 def VDBE_PROFILE
4b110 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 . int cnt;
4b120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
4b130 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
4b140 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
4b150 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a was executed */.
4b160 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20 u64 cycles;
4b170 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f /* To
4b180 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65 tal time spent e
4b190 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e xecuting this in
4b1a0 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e struction */.#en
4b1b0 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 dif.};.typedef s
4b1c0 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62 truct VdbeOp Vdb
4b1d0 65 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d eOp;../*.** A sm
4b1e0 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 aller version of
4b1f0 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72 VdbeOp used for
4b200 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69 the VdbeAddOpLi
4b210 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 st() function be
4b220 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65 cause.** it take
4b230 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e s up less space.
4b240 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f .*/.struct VdbeO
4b250 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63 pList {. u8 opc
4b260 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ode; /*
4b270 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 What operation
4b280 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 to perform */.
4b290 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20 signed char p1;
4b2a0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 /* First ope
4b2b0 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64 rand */. signed
4b2c0 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a char p2; /*
4b2d0 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 Second paramete
4b2e0 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d r (often the jum
4b2f0 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a p destination) *
4b300 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 /. signed char
4b310 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64 p3; /* Third
4b320 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b parameter */.};
4b330 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b340 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f VdbeOpList VdbeO
4b350 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c pList;../*.** Al
4b360 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 lowed values of
4b370 56 64 62 65 4f 70 2e 70 33 74 79 70 65 0a 2a 2f VdbeOp.p3type.*/
4b380 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55 .#define P4_NOTU
4b390 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68 SED 0 /* Th
4b3a0 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69 e P4 parameter i
4b3b0 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64 s not used */.#d
4b3c0 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43 efine P4_DYNAMIC
4b3d0 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74 (-1) /* Point
4b3e0 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f er to a string o
4b3f0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
4b400 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 iteMalloc() */.#
4b410 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43 define P4_STATIC
4b420 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e (-2) /* Poin
4b430 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 ter to a static
4b440 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
4b450 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d e P4_COLLSEQ (-
4b460 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 4) /* P4 is a p
4b470 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c ointer to a Coll
4b480 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f Seq structure */
4b490 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43 .#define P4_FUNC
4b4a0 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34 DEF (-5) /* P4
4b4b0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
4b4c0 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 a FuncDef struc
4b4d0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4b4e0 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29 P4_KEYINFO (-6)
4b4f0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
4b500 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 nter to a KeyInf
4b510 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 o structure */.#
4b520 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55 define P4_VDBEFU
4b530 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69 NC (-7) /* P4 i
4b540 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
4b550 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74 VdbeFunc struct
4b560 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
4b570 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20 4_MEM (-8)
4b580 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
4b590 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20 ter to a Mem*
4b5a0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
4b5b0 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45 efine P4_TRANSIE
4b5c0 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73 NT (-9) /* P4 is
4b5d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4b5e0 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67 transient string
4b5f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 */.#define P4_V
4b600 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a TAB (-10) /*
4b610 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
4b620 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 to an sqlite3_v
4b630 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f tab structure */
4b640 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49 .#define P4_MPRI
4b650 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34 NTF (-11) /* P4
4b660 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74 is a string obt
4b670 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
4b680 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a e3_mprintf() */.
4b690 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20 #define P4_REAL
4b6a0 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20 (-12) /* P4
4b6b0 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 is a 64-bit floa
4b6c0 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
4b6d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 */.#define P4_I
4b6e0 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a NT64 (-13) /*
4b6f0 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 P4 is a 64-bit
4b700 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
4b710 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 /.#define P4_INT
4b720 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50 32 (-14) /* P
4b730 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69 4 is a 32-bit si
4b740 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
4b750 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52 #define P4_INTAR
4b760 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20 RAY (-15) /* P4
4b770 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33 is a vector of 3
4b780 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a 2-bit integers *
4b790 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e /../* When addin
4b7a0 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 g a P4 argument
4b7b0 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f using P4_KEYINFO
4b7c0 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 , a copy of the
4b7d0 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
4b7e0 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 e.** is made. T
4b7f0 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65 hat copy is free
4b800 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
4b810 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 is finalized. B
4b820 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 ut if the.** arg
4b830 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 ument is P4_KEYI
4b840 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 NFO_HANDOFF, the
4b850 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 passed in point
4b860 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20 er is used. It
4b870 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 still.** gets fr
4b880 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 eed when the Vdb
4b890 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 e is finalized s
4b8a0 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c o it still shoul
4b8b0 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a d be obtained.**
4b8c0 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 from a single s
4b8d0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 qliteMalloc().
4b8e0 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d But no copy is m
4b8f0 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c ade and the call
4b900 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ing.** function
4b910 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 should *not* try
4b920 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79 to free the Key
4b930 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 Info..*/.#define
4b940 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 P4_KEYINFO_HAND
4b950 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e OFF (-16).#defin
4b960 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 e P4_KEYINFO_STA
4b970 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a TIC (-17)../*.*
4b980 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e * The Vdbe.aColN
4b990 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69 ame array contai
4b9a0 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 ns 5n Mem struct
4b9b0 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73 ures, where n is
4b9c0 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 the .** number
4b9d0 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 of columns of da
4b9e0 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ta returned by t
4b9f0 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f he statement..*/
4ba00 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
4ba10 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66 _NAME 0.#def
4ba20 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c ine COLNAME_DECL
4ba30 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43 TYPE 1.#define C
4ba40 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 OLNAME_DATABASE
4ba50 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 2.#define COLNAM
4ba60 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65 E_TABLE 3.#de
4ba70 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c fine COLNAME_COL
4ba80 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53 UMN 4.#ifdef S
4ba90 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
4baa0 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 UMN_METADATA.# d
4bab0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4bac0 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a 5 /*
4bad0 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 Number of COLNA
4bae0 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a ME_xxx symbols *
4baf0 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 /.#else.# ifdef
4bb00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
4bb10 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20 TYPE.# define
4bb20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 COLNAME_N 1
4bb30 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f /* Store o
4bb40 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a nly the name */.
4bb50 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e # else.# defin
4bb60 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 e COLNAME_N
4bb70 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 2 /* Store
4bb80 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 the name and de
4bb90 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 cltype */.# endi
4bba0 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
4bbb0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 The following ma
4bbc0 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 cro converts a r
4bbd0 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20 elative address
4bbe0 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a in the p2 field.
4bbf0 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 ** of a VdbeOp s
4bc00 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 tructure into a
4bc10 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 negative number
4bc20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 so that .** sqli
4bc30 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
4bc40 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 () knows that th
4bc50 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c e address is rel
4bc60 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a ative. Calling.
4bc70 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 ** the macro aga
4bc80 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 in restores the
4bc90 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 address..*/.#def
4bca0 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 ine ADDR(X) (-1
4bcb0 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 -(X))../*.** The
4bcc0 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 makefile scans
4bcd0 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 the vdbe.c sourc
4bce0 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 e file and creat
4bcf0 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e es the "opcodes.
4bd00 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c h".** header fil
4bd10 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 e that defines a
4bd20 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 number for each
4bd30 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 opcode used by
4bd40 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a the VDBE..*/./**
4bd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
4bd60 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 lude opcodes.h i
4bd70 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
4bd80 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h *********
4bd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
4bda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
4bdb0 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e in file opcodes.
4bdc0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4bdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 ***********/./*
4bdf0 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 Automatically ge
4be00 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 nerated. Do not
4be10 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 edit */./* See
4be20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 the mkopcodeh.aw
4be30 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 k script for det
4be40 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ails */.#define
4be50 4f 50 5f 56 4e 65 78 74 20 20 20 20 20 20 20 20 OP_VNext
4be60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4be70 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
4be80 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20 e OP_Affinity
4be90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bea0 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 2.#def
4beb0 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 ine OP_Column
4bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bed0 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 3.#d
4bee0 65 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f 6f 6b efine OP_SetCook
4bef0 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ie
4bf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 4.
4bf10 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 #define OP_Seek
4bf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf40 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 5.#define OP_Rea
4bf50 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l
4bf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bf70 31 33 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 130 /* same as
4bf80 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 2a 2f 0a TK_FLOAT */.
4bf90 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75 65 #define OP_Seque
4bfa0 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 nce
4bfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bfc0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 61 76 6.#define OP_Sav
4bfd0 65 70 6f 69 6e 74 20 20 20 20 20 20 20 20 20 20 epoint
4bfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4bff0 20 20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 7.#define OP_G
4c000 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
4c010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c020 20 20 20 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 78 /* same
4c030 61 73 20 54 4b 5f 47 45 20 20 20 20 20 20 20 2a as TK_GE *
4c040 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 /.#define OP_Row
4c050 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 20 20 Key
4c060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c070 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 8.#define OP_S
4c080 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 20 Copy
4c090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c0a0 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 9.#define OP
4c0b0 5f 45 71 20 20 20 20 20 20 20 20 20 20 20 20 20 _Eq
4c0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c0d0 20 20 20 20 20 37 34 20 20 20 2f 2a 20 73 61 6d 74 /* sam
4c0e0 65 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20 20 e as TK_EQ
4c0f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f */.#define OP_O
4c100 70 65 6e 57 72 69 74 65 20 20 20 20 20 20 20 20 penWrite
4c110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c120 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 10.#define OP
4c130 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 _NotNull
4c140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c150 20 20 20 20 20 37 32 20 20 20 2f 2a 20 73 61 6d 72 /* sam
4c160 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 e as TK_NOTNULL
4c170 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 */.#define OP_I
4c180 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f
4c190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c1a0 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 11.#define OP
4c1b0 5f 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 _ToInt
4c1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c1d0 20 20 20 20 31 34 34 20 20 20 2f 2a 20 73 61 6d 144 /* sam
4c1e0 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 e as TK_TO_INT
4c1f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
4c200 74 72 69 6e 67 38 20 20 20 20 20 20 20 20 20 20 tring8
4c210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c220 20 20 20 39 34 20 20 20 2f 2a 20 73 61 6d 65 20 94 /* same
4c230 61 73 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a as TK_STRING *
4c240 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c /.#define OP_Col
4c250 6c 53 65 71 20 20 20 20 20 20 20 20 20 20 20 20 lSeq
4c260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c270 20 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 12.#define OP_O
4c280 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20 penRead
4c290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c2a0 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 13.#define OP
4c2b0 5f 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20 _Expire
4c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c2d0 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 14.#define
4c2e0 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20 OP_AutoCommit
4c2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c300 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 15.#defin
4c310 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 e OP_Gt
4c320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c330 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f 2a 75 /*
4c340 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 same as TK_GT
4c350 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4c360 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20 OP_Pagecount
4c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c380 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 16.#defin
4c390 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b e OP_IntegrityCk
4c3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c3b0 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 17.#def
4c3c0 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20 20 ine OP_Sort
4c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c3e0 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 18.#d
4c3f0 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20 efine OP_Copy
4c400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c410 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 20.
4c420 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 63 65 #define OP_Trace
4c430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
4c450 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 1.#define OP_Fun
4c460 63 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 ction
4c470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c480 20 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 22.#define OP_I
4c490 66 4e 65 67 20 20 20 20 20 20 20 20 20 20 20 20 fNeg
4c4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c4b0 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 23.#define OP
4c4c0 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 _And
4c4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c4e0 20 20 20 20 20 36 37 20 20 20 2f 2a 20 73 61 6d 67 /* sam
4c4f0 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20 20 e as TK_AND
4c500 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
4c510 75 62 74 72 61 63 74 20 20 20 20 20 20 20 20 20 ubtract
4c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c530 20 20 20 38 35 20 20 20 2f 2a 20 73 61 6d 65 20 85 /* same
4c540 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a as TK_MINUS *
4c550 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f /.#define OP_Noo
4c560 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
4c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c580 20 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 24.#define OP_R
4c590 65 74 75 72 6e 20 20 20 20 20 20 20 20 20 20 20 eturn
4c5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c5b0 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 4f 50 25.#define OP
4c5c0 5f 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20 20 _Remainder
4c5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c5e0 20 20 20 20 20 38 38 20 20 20 2f 2a 20 73 61 6d 88 /* sam
4c5f0 65 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20 20 e as TK_REM
4c600 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e */.#define OP_N
4c610 65 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 ewRowid
4c620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c630 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 26.#define OP
4c640 5f 4d 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20 _Multiply
4c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c660 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73 61 6d 86 /* sam
4c670 65 20 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20 e as TK_STAR
4c680 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 */.#define OP_V
4c690 61 72 69 61 62 6c 65 20 20 20 20 20 20 20 20 20 ariable
4c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c6b0 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 27.#define OP
4c6c0 5f 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 _String
4c6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c6e0 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 28.#define
4c6f0 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 OP_RealAffinity
4c700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c710 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 29.#defin
4c720 65 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 20 20 e OP_VRename
4c730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c740 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 30.#def
4c750 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 ine OP_ParseSche
4c760 6d 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ma
4c770 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 31.#d
4c780 65 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 efine OP_VOpen
4c790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 32.
4c7b0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 #define OP_Close
4c7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
4c7e0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 3.#define OP_Cre
4c7f0 61 74 65 49 6e 64 65 78 20 20 20 20 20 20 20 20 ateIndex
4c800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c810 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 34.#define OP_I
4c820 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20 sUnique
4c830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c840 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 35.#define OP
4c850 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 20 20 _NotFound
4c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c870 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 36.#define
4c880 4f 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 OP_Int64
4c890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c8a0 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 37.#defin
4c8b0 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20 20 e OP_MustBeInt
4c8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c8d0 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 38.#def
4c8e0 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20 20 20 20 ine OP_Halt
4c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c900 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 39.#d
4c910 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 efine OP_Rowid
4c920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c930 20 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 40.
4c940 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54 #define OP_IdxLT
4c950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
4c970 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 1.#define OP_Add
4c980 49 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 Imm
4c990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c9a0 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 42.#define OP_S
4c9b0 74 61 74 65 6d 65 6e 74 20 20 20 20 20 20 20 20 tatement
4c9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4c9d0 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 43.#define OP
4c9e0 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20 _RowData
4c9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca00 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 44.#define
4ca10 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 OP_MemMax
4ca20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca30 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 45.#defin
4ca40 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 e OP_Or
4ca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ca60 20 20 20 20 20 20 20 20 20 36 36 20 20 20 2f 2a 66 /*
4ca70 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 same as TK_OR
4ca80 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4ca90 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 OP_NotExists
4caa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cab0 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 46.#defin
4cac0 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20 e OP_Gosub
4cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cae0 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 47.#def
4caf0 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20 ine OP_Divide
4cb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb10 20 20 20 20 20 20 20 20 20 20 20 38 37 20 20 20 87
4cb20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c /* same as TK_SL
4cb30 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e ASH */.#defin
4cb40 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20 e OP_Integer
4cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb60 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 48.#def
4cb70 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 ine OP_ToNumeric
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb90 20 20 20 20 20 20 20 20 20 20 31 34 33 20 20 20 143
4cba0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
4cbb0 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69 _NUMERIC*/.#defi
4cbc0 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 ne OP_Prev
4cbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cbe0 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 49.#de
4cbf0 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 52 65 fine OP_RowSetRe
4cc00 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ad
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 50.#
4cc20 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 63 61 74 define OP_Concat
4cc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 39 89
4cc50 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4cc60 5f 43 4f 4e 43 41 54 20 20 20 2a 2f 0a 23 64 65 _CONCAT */.#de
4cc70 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 fine OP_RowSetAd
4cc80 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
4cc90 20 20 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 51.#
4cca0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 define OP_BitAnd
4ccb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 80
4ccd0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4cce0 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 _BITAND */.#de
4ccf0 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 fine OP_VColumn
4cd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cd10 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 52.#
4cd20 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 define OP_Create
4cd30 54 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 Table
4cd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33 53
4cd50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 61 73 74 .#define OP_Last
4cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cd80 35 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 54.#define OP_Se
4cd90 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 ekLe
4cda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cdb0 20 20 35 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 55.#define OP_
4cdc0 49 73 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 IsNull
4cdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cde0 20 20 20 20 37 31 20 20 20 2f 2a 20 73 61 6d 65 71 /* same
4cdf0 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 as TK_ISNULL
4ce00 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e */.#define OP_In
4ce10 63 72 56 61 63 75 75 6d 20 20 20 20 20 20 20 20 crVacuum
4ce20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce30 20 20 35 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56.#define OP_
4ce40 49 64 78 52 6f 77 69 64 20 20 20 20 20 20 20 20 IdxRowid
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce60 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 57.#define O
4ce70 50 5f 53 68 69 66 74 52 69 67 68 74 20 20 20 20 P_ShiftRight
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce90 20 20 20 20 20 20 38 33 20 20 20 2f 2a 20 73 61 83 /* sa
4cea0 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 20 me as TK_RSHIFT
4ceb0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4cec0 52 65 73 65 74 43 6f 75 6e 74 20 20 20 20 20 20 ResetCount
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cee0 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f 58.#define O
4cef0 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 20 20 20 P_ContextPush
4cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cf10 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 59.#define
4cf20 20 4f 50 5f 59 69 65 6c 64 20 20 20 20 20 20 20 OP_Yield
4cf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cf40 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69 60.#defi
4cf50 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 ne OP_DropTrigge
4cf60 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
4cf70 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 61.#de
4cf80 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 fine OP_DropInde
4cf90 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x
4cfa0 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 62.#
4cfb0 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45 20 define OP_IdxGE
4cfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33 63
4cfe0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 44 .#define OP_IdxD
4cff0 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 elete
4d000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d010 36 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61 64.#define OP_Va
4d020 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 cuum
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d040 20 20 36 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 65.#define OP_
4d050 49 66 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 IfNot
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d070 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 4f 68.#define O
4d080 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20 P_DropTable
4d090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d0a0 20 20 20 20 20 20 36 39 0a 23 64 65 66 69 6e 65 69.#define
4d0b0 20 4f 50 5f 53 65 65 6b 4c 74 20 20 20 20 20 20 OP_SeekLt
4d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d0d0 20 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 70.#defi
4d0e0 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 ne OP_MakeRecord
4d0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d100 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 64 65 79.#de
4d110 66 69 6e 65 20 4f 50 5f 54 6f 42 6c 6f 62 20 20 fine OP_ToBlob
4d120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d130 20 20 20 20 20 20 20 20 20 20 20 31 34 32 20 20 142
4d140 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
4d150 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 64 65 66 69 O_BLOB */.#defi
4d160 6e 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 ne OP_ResultRow
4d170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d180 20 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 65 90.#de
4d190 66 69 6e 65 20 4f 50 5f 44 65 6c 65 74 65 20 20 fine OP_Delete
4d1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d1b0 20 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23 91.#
4d1c0 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 46 69 6e define OP_AggFin
4d1d0 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 al
4d1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32 92
4d1f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6d 70 .#define OP_Comp
4d200 61 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20 are
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d220 39 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 95.#define OP_Sh
4d230 69 66 74 4c 65 66 74 20 20 20 20 20 20 20 20 20 iftLeft
4d240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d250 20 20 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 82 /* same a
4d260 73 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 2a 2f s TK_LSHIFT */
4d270 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 74 6f .#define OP_Goto
4d280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d2a0 39 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 61 96.#define OP_Ta
4d2b0 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 20 20 bleLock
4d2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d2d0 20 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 97.#define OP_
4d2e0 43 6c 65 61 72 20 20 20 20 20 20 20 20 20 20 20 Clear
4d2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d300 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 98.#define O
4d310 50 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 P_Le
4d320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d330 20 20 20 20 20 20 37 36 20 20 20 2f 2a 20 73 61 76 /* sa
4d340 6d 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 20 me as TK_LE
4d350 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4d360 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 VerifyCookie
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d380 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 99.#define O
4d390 50 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20 P_AggStep
4d3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d3b0 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 100.#define
4d3c0 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 OP_ToText
4d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d3e0 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20 141 /*
4d3f0 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 same as TK_TO_TE
4d400 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f XT */.#define O
4d410 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 P_Not
4d420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d430 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 73 61 19 /* sa
4d440 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 me as TK_NOT
4d450 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4d460 54 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 ToReal
4d470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d480 20 20 20 31 34 35 20 20 20 2f 2a 20 73 61 6d 65 145 /* same
4d490 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 as TK_TO_REAL
4d4a0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 */.#define OP_Se
4d4b0 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 20 20 20 20 tNumColumns
4d4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d4d0 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 101.#define OP_
4d4e0 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 Transaction
4d4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d500 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f 102.#define O
4d510 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20 P_VFilter
4d520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d530 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 103.#define
4d540 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 20 20 20 OP_Ne
4d550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d560 20 20 20 20 20 20 20 20 37 33 20 20 20 2f 2a 20 73 /*
4d570 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20 20 20 same as TK_NE
4d580 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
4d590 50 5f 56 44 65 73 74 72 6f 79 20 20 20 20 20 20 P_VDestroy
4d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d5b0 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 104.#define
4d5c0 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 20 20 OP_ContextPop
4d5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d5e0 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 105.#defi
4d5f0 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 ne OP_BitOr
4d600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d610 20 20 20 20 20 20 20 20 20 20 38 31 20 20 20 2f 81 /
4d620 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
4d630 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 OR */.#define
4d640 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20 OP_Next
4d650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d660 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 106.#defi
4d670 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20 20 ne OP_Count
4d680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d690 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 107.#de
4d6a0 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 fine OP_IdxInser
4d6b0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4d6c0 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 108.#
4d6d0 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 define OP_Lt
4d6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 77
4d700 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4d710 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _LT */.#de
4d720 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 fine OP_SeekGe
4d730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d740 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 109.#
4d750 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 define OP_Insert
4d760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d770 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 110
4d780 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74 .#define OP_Dest
4d790 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 roy
4d7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4d7b0 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 11.#define OP_Re
4d7c0 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 adCookie
4d7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d7e0 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 112.#define OP_
4d7f0 52 6f 77 53 65 74 54 65 73 74 20 20 20 20 20 20 RowSetTest
4d800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d810 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 4f 113.#define O
4d820 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20 P_LoadAnalysis
4d830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d840 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 114.#define
4d850 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 20 20 OP_Explain
4d860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d870 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 115.#defi
4d880 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c ne OP_HaltIfNull
4d890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d8a0 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 116.#de
4d8b0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 fine OP_OpenPseu
4d8c0 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 do
4d8d0 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 117.#
4d8e0 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45 70 define OP_OpenEp
4d8f0 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20 20 hemeral
4d900 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 118
4d910 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c .#define OP_Null
4d920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4d940 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 19.#define OP_Mo
4d950 76 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ve
4d960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d970 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 120.#define OP_
4d980 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 Blob
4d990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d9a0 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 121.#define O
4d9b0 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20 20 P_Add
4d9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d9d0 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61 84 /* sa
4d9e0 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 20 me as TK_PLUS
4d9f0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4da00 52 65 77 69 6e 64 20 20 20 20 20 20 20 20 20 20 Rewind
4da10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4da20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 122.#define O
4da30 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20 20 20 P_SeekGt
4da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4da50 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 123.#define
4da60 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20 OP_VBegin
4da70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4da80 20 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 124.#defi
4da90 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 20 20 ne OP_VUpdate
4daa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dab0 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 125.#de
4dac0 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f 20 20 fine OP_IfZero
4dad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dae0 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 126.#
4daf0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4e 6f 74 define OP_BitNot
4db00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4db10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33 93
4db20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4db30 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 64 65 _BITNOT */.#de
4db40 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 74 65 20 fine OP_VCreate
4db50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4db60 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 127.#
4db70 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e 64 20 define OP_Found
4db80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4db90 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38 128
4dba0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f .#define OP_IfPo
4dbb0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
4dbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4dbd0 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 29.#define OP_Nu
4dbe0 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 llRow
4dbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dc00 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 131.#define OP_
4dc10 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 20 20 Jump
4dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dc30 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 4f 132.#define O
4dc40 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20 P_Permutation
4dc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dc60 20 20 20 20 20 31 33 33 0a 0a 2f 2a 20 54 68 65 133../* The
4dc70 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 following opcod
4dc80 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76 e values are nev
4dc90 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69 er used */.#defi
4dca0 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 ne OP_NotUsed_13
4dcb0 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4
4dcc0 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 134.#de
4dcd0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
4dce0 31 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20 135
4dcf0 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 135.#
4dd00 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
4dd10 64 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20 d_136
4dd20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 136
4dd30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
4dd40 73 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20 sed_137
4dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4dd60 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 37.#define OP_No
4dd70 74 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20 tUsed_138
4dd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4dd90 20 31 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 138.#define OP_
4dda0 4e 6f 74 55 73 65 64 5f 31 33 39 20 20 20 20 20 NotUsed_139
4ddb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ddc0 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 139.#define O
4ddd0 50 5f 4e 6f 74 55 73 65 64 5f 31 34 30 20 20 20 P_NotUsed_140
4dde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ddf0 20 20 20 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72 140.../* Pr
4de00 6f 70 65 72 74 69 65 73 20 73 75 63 68 20 61 73 operties such as
4de10 20 22 6f 75 74 32 22 20 6f 72 20 22 6a 75 6d 70 "out2" or "jump
4de20 22 20 74 68 61 74 20 61 72 65 20 73 70 65 63 69 " that are speci
4de30 66 69 65 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 fied in.** comme
4de40 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 nts following th
4de50 65 20 22 63 61 73 65 22 20 66 6f 72 20 65 61 63 e "case" for eac
4de60 68 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 h opcode in the
4de70 76 64 62 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e vdbe.c.** are en
4de80 63 6f 64 65 64 20 69 6e 74 6f 20 62 69 74 76 65 coded into bitve
4de90 63 74 6f 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ctors as follows
4dea0 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 :.*/.#define OPF
4deb0 4c 47 5f 4a 55 4d 50 20 20 20 20 20 20 20 20 20 LG_JUMP
4dec0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75 0x0001 /* ju
4ded0 6d 70 3a 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d mp: P2 holds jm
4dee0 70 20 74 61 72 67 65 74 20 2a 2f 0a 23 64 65 66 p target */.#def
4def0 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 ine OPFLG_OUT2_P
4df00 52 45 52 45 4c 45 41 53 45 20 30 78 30 30 30 32 RERELEASE 0x0002
4df10 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
4df20 65 61 73 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 ease: */.#define
4df30 20 4f 50 46 4c 47 5f 49 4e 31 20 20 20 20 20 20 OPFLG_IN1
4df40 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 0x0004 /
4df50 2a 20 69 6e 31 3a 20 20 20 50 31 20 69 73 20 61 * in1: P1 is a
4df60 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 n input */.#defi
4df70 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20 ne OPFLG_IN2
4df80 20 20 20 20 20 20 20 20 20 30 78 30 30 30 38 20 0x0008
4df90 20 2f 2a 20 69 6e 32 3a 20 20 20 50 32 20 69 73 /* in2: P2 is
4dfa0 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 an input */.#de
4dfb0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20 fine OPFLG_IN3
4dfc0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 31 0x001
4dfd0 30 20 20 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20 0 /* in3: P3
4dfe0 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 is an input */.#
4dff0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 define OPFLG_OUT
4e000 33 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 3 0x0
4e010 30 32 30 20 20 2f 2a 20 6f 75 74 33 3a 20 20 50 020 /* out3: P
4e020 33 20 69 73 20 61 6e 20 6f 75 74 70 75 74 20 2a 3 is an output *
4e030 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
4e040 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f INITIALIZER {\./
4e050 2a 20 20 20 30 20 2a 2f 20 30 78 30 30 2c 20 30 * 0 */ 0x00, 0
4e060 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x01, 0x00, 0x00,
4e070 20 30 78 31 30 2c 20 30 78 30 38 2c 20 30 78 30 0x10, 0x08, 0x0
4e080 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 20 38 2, 0x00,\./* 8
4e090 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 34 2c 20 */ 0x00, 0x04,
4e0a0 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30 0x00, 0x05, 0x00
4e0b0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
4e0c0 30 30 2c 5c 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 00,\./* 16 */ 0
4e0d0 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 31 2c x02, 0x00, 0x01,
4e0e0 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30 0x04, 0x04, 0x0
4e0f0 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 5c 0a 0, 0x00, 0x05,\.
4e100 2f 2a 20 20 32 34 20 2a 2f 20 30 78 30 30 2c 20 /* 24 */ 0x00,
4e110 30 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30 30 0x04, 0x02, 0x00
4e120 2c 20 30 78 30 32 2c 20 30 78 30 34 2c 20 30 78 , 0x02, 0x04, 0x
4e130 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 33 00, 0x00,\./* 3
4e140 32 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 2 */ 0x00, 0x00,
4e150 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 31 0x02, 0x11, 0x1
4e160 31 2c 20 30 78 30 32 2c 20 30 78 30 35 2c 20 30 1, 0x02, 0x05, 0
4e170 78 30 30 2c 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 x00,\./* 40 */
4e180 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 30 34 0x02, 0x11, 0x04
4e190 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
4e1a0 30 63 2c 20 30 78 31 31 2c 20 30 78 30 31 2c 5c 0c, 0x11, 0x01,\
4e1b0 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 78 30 32 2c ./* 48 */ 0x02,
4e1c0 20 30 78 30 31 2c 20 30 78 32 31 2c 20 30 78 30 0x01, 0x21, 0x0
4e1d0 38 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 8, 0x00, 0x02, 0
4e1e0 78 30 31 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 x01, 0x11,\./*
4e1f0 35 36 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 32 56 */ 0x01, 0x02
4e200 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
4e210 30 34 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 04, 0x00, 0x00,
4e220 30 78 31 31 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 0x11,\./* 64 */
4e230 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 32 0x00, 0x00, 0x2
4e240 63 2c 20 30 78 32 63 2c 20 30 78 30 35 2c 20 30 c, 0x2c, 0x05, 0
4e250 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 35 2c x00, 0x11, 0x05,
4e260 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20 30 78 30 35 \./* 72 */ 0x05
4e270 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 , 0x15, 0x15, 0x
4e280 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 15, 0x15, 0x15,
4e290 30 78 31 35 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 0x15, 0x00,\./*
4e2a0 20 38 30 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 80 */ 0x2c, 0x2
4e2b0 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 c, 0x2c, 0x2c, 0
4e2c0 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c x2c, 0x2c, 0x2c,
4e2d0 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a 0x2c,\./* 88 *
4e2e0 2f 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 / 0x2c, 0x2c, 0x
4e2f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
4e300 30 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30 30 0x04, 0x02, 0x00
4e310 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f 20 30 78 30 ,\./* 96 */ 0x0
4e320 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 1, 0x00, 0x00, 0
4e330 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
4e340 20 30 78 30 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 0x00, 0x01,\./*
4e350 20 31 30 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 104 */ 0x00, 0x
4e360 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 00, 0x01, 0x02,
4e370 30 78 30 38 2c 20 30 78 31 31 2c 20 30 78 30 30 0x08, 0x11, 0x00
4e380 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 31 32 20 , 0x02,\./* 112
4e390 2a 2f 20 30 78 30 32 2c 20 30 78 31 35 2c 20 30 */ 0x02, 0x15, 0
4e3a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31 30 2c x00, 0x00, 0x10,
4e3b0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
4e3c0 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a 2f 20 30 78 2,\./* 120 */ 0x
4e3d0 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 20 00, 0x02, 0x01,
4e3e0 30 78 31 31 2c 20 30 78 30 30 2c 20 30 78 30 30 0x11, 0x00, 0x00
4e3f0 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 5c 0a 2f , 0x05, 0x00,\./
4e400 2a 20 31 32 38 20 2a 2f 20 30 78 31 31 2c 20 30 * 128 */ 0x11, 0
4e410 78 30 35 2c 20 30 78 30 32 2c 20 30 78 30 30 2c x05, 0x02, 0x00,
4e420 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 0x01, 0x00, 0x0
4e430 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 0, 0x00,\./* 136
4e440 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 */ 0x00, 0x00,
4e450 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
4e460 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 , 0x04, 0x04, 0x
4e470 30 34 2c 5c 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 04,\./* 144 */ 0
4e480 78 30 34 2c 20 30 78 30 34 2c 7d 0a 0a 2f 2a 2a x04, 0x04,}../**
4e490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
4e4a0 20 6f 66 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a of opcodes.h **
4e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
4e4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
4e4f0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
4e500 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 left off in vdb
4e510 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
4e520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
4e530 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65 73 20 66 .** Prototypes f
4e540 6f 72 20 74 68 65 20 56 44 42 45 20 69 6e 74 65 or the VDBE inte
4e550 72 66 61 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d rface. See comm
4e560 65 6e 74 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c ents on the impl
4e570 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f ementation.** fo
4e580 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 r a description
4e590 6f 66 20 77 68 61 74 20 65 61 63 68 20 6f 66 20 of what each of
4e5a0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 these routines d
4e5b0 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 oes..*/.SQLITE_P
4e5c0 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c RIVATE Vdbe *sql
4e5d0 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73 ite3VdbeCreate(s
4e5e0 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
4e5f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4e600 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 56 ite3VdbeAddOp0(V
4e610 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 dbe*,int);.SQLIT
4e620 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4e630 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
4e640 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a Vdbe*,int,int);.
4e650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4e660 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
4e670 64 4f 70 32 28 56 64 62 65 2a 2c 69 6e 74 2c 69 dOp2(Vdbe*,int,i
4e680 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
4e690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4e6a0 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 te3VdbeAddOp3(Vd
4e6b0 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c be*,int,int,int,
4e6c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4e6d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4e6e0 56 64 62 65 41 64 64 4f 70 34 28 56 64 62 65 2a VdbeAddOp4(Vdbe*
4e6f0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
4e700 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 ,const char *zP4
4e710 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
4e720 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4e730 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 3VdbeAddOpList(V
4e740 64 62 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 dbe*, int nOp, V
4e750 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 dbeOpList const
4e760 2a 61 4f 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 *aOp);.SQLITE_PR
4e770 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
4e780 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 e3VdbeChangeP1(V
4e790 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 dbe*, int addr,
4e7a0 69 6e 74 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f int P1);.SQLITE_
4e7b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4e7c0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 ite3VdbeChangeP2
4e7d0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
4e7e0 2c 20 69 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 , int P2);.SQLIT
4e7f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4e800 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
4e810 50 33 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 P3(Vdbe*, int ad
4e820 64 72 2c 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c dr, int P3);.SQL
4e830 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4e840 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
4e850 67 65 50 35 28 56 64 62 65 2a 2c 20 75 38 20 50 geP5(Vdbe*, u8 P
4e860 35 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 5);.SQLITE_PRIVA
4e870 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
4e880 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 dbeJumpHere(Vdbe
4e890 2a 2c 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 *, int addr);.SQ
4e8a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4e8b0 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 d sqlite3VdbeCha
4e8c0 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c ngeToNoop(Vdbe*,
4e8d0 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e int addr, int N
4e8e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4e8f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
4e900 62 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 2a beChangeP4(Vdbe*
4e910 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 , int addr, cons
4e920 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 t char *zP4, int
4e930 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 N);.SQLITE_PRIV
4e940 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
4e950 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56 64 VdbeUsesBtree(Vd
4e960 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 be*, int);.SQLIT
4e970 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 E_PRIVATE VdbeOp
4e980 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 *sqlite3VdbeGet
4e990 4f 70 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a Op(Vdbe*, int);.
4e9a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4e9b0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 nt sqlite3VdbeMa
4e9c0 6b 65 4c 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a keLabel(Vdbe*);.
4e9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4e9e0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
4e9f0 65 6c 65 74 65 28 56 64 62 65 2a 29 3b 0a 53 51 elete(Vdbe*);.SQ
4ea00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4ea10 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b d sqlite3VdbeMak
4ea20 65 52 65 61 64 79 28 56 64 62 65 2a 2c 69 6e 74 eReady(Vdbe*,int
4ea30 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
4ea40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4ea50 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e t sqlite3VdbeFin
4ea60 61 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51 alize(Vdbe*);.SQ
4ea70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4ea80 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 d sqlite3VdbeRes
4ea90 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c olveLabel(Vdbe*,
4eaa0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
4eab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4eac0 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 3VdbeCurrentAddr
4ead0 28 56 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20 (Vdbe*);.#ifdef
4eae0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
4eaf0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4eb00 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 72 id sqlite3VdbeTr
4eb10 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45 2a 29 ace(Vdbe*,FILE*)
4eb20 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f ;.#endif.SQLITE_
4eb30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4eb40 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 ite3VdbeResetSte
4eb50 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29 3b 0a pResult(Vdbe*);.
4eb60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4eb70 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 nt sqlite3VdbeRe
4eb80 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 set(Vdbe*);.SQLI
4eb90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
4eba0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 sqlite3VdbeSetNu
4ebb0 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e 74 29 mCols(Vdbe*,int)
4ebc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4ebd0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
4ebe0 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62 65 2a SetColName(Vdbe*
4ebf0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 , int, int, cons
4ec00 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64 28 2a t char *, void(*
4ec10 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
4ec20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4ec30 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 qlite3VdbeCountC
4ec40 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b 0a 53 hanges(Vdbe*);.S
4ec50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
4ec60 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64 lite3 *sqlite3Vd
4ec70 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 51 4c beDb(Vdbe*);.SQL
4ec80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4ec90 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 sqlite3VdbeSetS
4eca0 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 ql(Vdbe*, const
4ecb0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 char *z, int n,
4ecc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4ecd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4ece0 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a 2c 3VdbeSwap(Vdbe*,
4ecf0 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65 66 20 Vdbe*);..#ifdef
4ed00 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
4ed10 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a MORY_MANAGEMENT.
4ed20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4ed30 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 nt sqlite3VdbeRe
4ed40 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 leaseMemory(int)
4ed50 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f ;.#endif.SQLITE_
4ed60 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 PRIVATE Unpacked
4ed70 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 Record *sqlite3V
4ed80 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 dbeRecordUnpack(
4ed90 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63 6f 6e KeyInfo*,int,con
4eda0 73 74 20 76 6f 69 64 2a 2c 63 68 61 72 2a 2c 69 st void*,char*,i
4edb0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
4edc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
4edd0 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b VdbeDeleteUnpack
4ede0 65 64 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 edRecord(Unpacke
4edf0 64 52 65 63 6f 72 64 2a 29 3b 0a 53 51 4c 49 54 dRecord*);.SQLIT
4ee00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4ee10 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 lite3VdbeRecordC
4ee20 6f 6d 70 61 72 65 28 69 6e 74 2c 63 6f 6e 73 74 ompare(int,const
4ee30 20 76 6f 69 64 2a 2c 55 6e 70 61 63 6b 65 64 52 void*,UnpackedR
4ee40 65 63 6f 72 64 2a 29 3b 0a 0a 0a 23 69 66 6e 64 ecord*);...#ifnd
4ee50 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 ef NDEBUG.SQLITE
4ee60 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4ee70 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 sqlite3VdbeComme
4ee80 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 nt(Vdbe*, const
4ee90 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 char*, ...);.# d
4eea0 65 66 69 6e 65 20 56 64 62 65 43 6f 6d 6d 65 6e efine VdbeCommen
4eeb0 74 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 t(X) sqlite3Vdb
4eec0 65 43 6f 6d 6d 65 6e 74 20 58 0a 53 51 4c 49 54 eComment X.SQLIT
4eed0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
4eee0 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 sqlite3VdbeNoop
4eef0 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 Comment(Vdbe*, c
4ef00 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
4ef10 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e ;.# define VdbeN
4ef20 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 oopComment(X) s
4ef30 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f qlite3VdbeNoopCo
4ef40 6d 6d 65 6e 74 20 58 0a 23 65 6c 73 65 0a 23 20 mment X.#else.#
4ef50 64 65 66 69 6e 65 20 56 64 62 65 43 6f 6d 6d 65 define VdbeComme
4ef60 6e 74 28 58 29 0a 23 20 64 65 66 69 6e 65 20 56 nt(X).# define V
4ef70 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58 dbeNoopComment(X
4ef80 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ).#endif..#endif
4ef90 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
4efa0 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 68 20 * End of vdbe.h
4efb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4efc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4efd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4efe0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4eff0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
4f000 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
4f010 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
4f020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f030 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4f040 2a 20 49 6e 63 6c 75 64 65 20 70 61 67 65 72 2e * Include pager.
4f050 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
4f060 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a of sqliteInt.h *
4f070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f080 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4f090 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67 * Begin file pag
4f0a0 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.h ***********
4f0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f0d0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
4f0e0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
4f0f0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
4f100 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
4f110 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
4f120 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
4f130 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
4f140 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
4f150 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
4f160 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
4f170 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
4f180 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
4f190 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
4f1a0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
4f1b0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
4f1c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
4f1d0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
4f1e0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
4f1f0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
4f200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f240 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
4f250 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 header file def
4f260 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 ines the interfa
4f270 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 ce that the sqli
4f280 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a te page cache.**
4f290 20 73 75 62 73 79 73 74 65 6d 2e 20 20 54 68 65 subsystem. The
4f2a0 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 page cache subs
4f2b0 79 73 74 65 6d 20 72 65 61 64 73 20 61 6e 64 20 ystem reads and
4f2c0 77 72 69 74 65 73 20 61 20 66 69 6c 65 20 61 20 writes a file a
4f2d0 70 61 67 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d page.** at a tim
4f2e0 65 20 61 6e 64 20 70 72 6f 76 69 64 65 73 20 61 e and provides a
4f2f0 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 6c journal for rol
4f300 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 lback..**.** @(#
4f310 29 20 24 49 64 3a 20 70 61 67 65 72 2e 68 2c 76 ) $Id: pager.h,v
4f320 20 31 2e 31 30 31 20 32 30 30 39 2f 30 34 2f 33 1.101 2009/04/3
4f330 30 20 30 39 3a 31 30 3a 33 38 20 64 61 6e 69 65 0 09:10:38 danie
4f340 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
4f350 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f .#ifndef _PAGER_
4f360 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45 H_.#define _PAGE
4f370 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 R_H_../*.** Defa
4f380 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 ult maximum size
4f390 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 for persistent
4f3a0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41 journal files. A
4f3b0 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61 negative .** va
4f3c0 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d lue means no lim
4f3d0 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d it. This value m
4f3e0 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e ay be overridden
4f3f0 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73 using the .** s
4f400 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
4f410 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50 alSizeLimit() AP
4f420 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41 I. See also "PRA
4f430 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 GMA journal_size
4f440 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e _limit"..*/.#ifn
4f450 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
4f460 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f LT_JOURNAL_SIZE_
4f470 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20 LIMIT. #define
4f480 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a SQLITE_DEFAULT_J
4f490 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 OURNAL_SIZE_LIMI
4f4a0 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a T -1.#endif../*.
4f4b0 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64 ** The type used
4f4c0 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 to represent a
4f4d0 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 page number. Th
4f4e0 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 e first page in
4f4f0 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c a file.** is cal
4f500 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69 led page 1. 0 i
4f510 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 s used to repres
4f520 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22 ent "not a page"
4f530 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32 ..*/.typedef u32
4f540 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 Pgno;../*.** Ea
4f550 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20 ch open file is
4f560 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70 managed by a sep
4f570 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f arate instance o
4f580 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74 f the "Pager" st
4f590 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 ructure..*/.type
4f5a0 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 def struct Pager
4f5b0 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48 Pager;../*.** H
4f5c0 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70 andle type for p
4f5d0 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ages..*/.typedef
4f5e0 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62 struct PgHdr Db
4f5f0 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 Page;../*.** Pag
4f600 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d e number PAGER_M
4f610 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20 J_PGNO is never
4f620 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74 used in an SQLit
4f630 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69 e database (it i
4f640 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f s.** reserved fo
4f650 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64 r working around
4f660 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78 a windows/posix
4f670 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 incompatibility
4f680 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 ). It is.** used
4f690 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
4f6a0 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20 to signify that
4f6b0 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 the remainder of
4f6c0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
4f6d0 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64 e .** is devoted
4f6e0 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61 to storing a ma
4f6f0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
4f700 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f e - there are no
4f710 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a more pages to.*
4f720 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 * roll back. See
4f730 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 comments for fu
4f740 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 nction writeMast
4f750 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70 erJournal() in p
4f760 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64 ager.c .** for d
4f770 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 etails..*/.#defi
4f780 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f ne PAGER_MJ_PGNO
4f790 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e (x) ((Pgno)((PEN
4f7a0 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e DING_BYTE/((x)->
4f7b0 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a pageSize))+1))..
4f7c0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
4f7d0 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61 lues for the fla
4f7e0 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 gs parameter to
4f7f0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
4f800 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 ()..**.** NOTE:
4f810 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 These values mus
4f820 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 t match the corr
4f830 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f esponding BTREE_
4f840 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65 values in btree
4f850 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 .h..*/.#define P
4f860 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 AGER_OMIT_JOURNA
4f870 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 L 0x0001 /*
4f880 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c Do not use a rol
4f890 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f lback journal */
4f8a0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4e .#define PAGER_N
4f8b0 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 30 O_READLOCK 0x0
4f8c0 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 72 002 /* Omit r
4f8d0 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 eadlocks on read
4f8e0 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f only files */../
4f8f0 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 *.** Valid value
4f900 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 s for the second
4f910 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
4f920 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 ite3PagerLocking
4f930 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 Mode()..*/.#defi
4f940 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 ne PAGER_LOCKING
4f950 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 MODE_QUERY
4f960 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 -1.#define PAGER
4f970 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 _LOCKINGMODE_NOR
4f980 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69 MAL 0.#defi
4f990 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 ne PAGER_LOCKING
4f9a0 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20 MODE_EXCLUSIVE
4f9b0 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 1../*.** Valid
4f9c0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 values for the s
4f9d0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
4f9e0 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f o sqlite3PagerJo
4f9f0 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a urnalMode()..*/.
4fa00 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f #define PAGER_JO
4fa10 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 URNALMODE_QUERY
4fa20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 -1.#define
4fa30 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
4fa40 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 20 E_DELETE 0
4fa50 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 64 /* Commit by d
4fa60 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 eleting journal
4fa70 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 file */.#define
4fa80 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
4fa90 45 5f 50 45 52 53 49 53 54 20 20 20 20 20 31 20 E_PERSIST 1
4faa0 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 7a /* Commit by z
4fab0 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 eroing journal h
4fac0 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 eader */.#define
4fad0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
4fae0 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20 32 DE_OFF 2
4faf0 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d /* Journal om
4fb00 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66 69 itted. */.#defi
4fb10 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
4fb20 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20 20 MODE_TRUNCATE
4fb30 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 3 /* Commit b
4fb40 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 y truncating jou
4fb50 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 rnal */.#define
4fb60 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
4fb70 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 20 E_MEMORY 4
4fb80 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a /* In-memory j
4fb90 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a ournal file */..
4fba0 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e /*.** The remain
4fbb0 64 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65 der of this file
4fbc0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65 contains the de
4fbd0 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68 clarations of th
4fbe0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 e functions.** t
4fbf0 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 hat make up the
4fc00 50 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d Pager sub-system
4fc10 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65 API. See source
4fc20 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 code comments f
4fc30 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65 or .** a detaile
4fc40 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 d description of
4fc50 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a each routine..*
4fc60 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 /../* Open and c
4fc70 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e lose a Pager con
4fc80 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c nection. */ .SQL
4fc90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4fca0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
4fcb0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
4fcc0 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c Pager **ppPager,
4fcd0 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e const char*, in
4fce0 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 t,int,int);.SQLI
4fcf0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4fd00 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 qlite3PagerClose
4fd10 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b (Pager *pPager);
4fd20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4fd30 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
4fd40 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 ReadFileheader(P
4fd50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69 ager*, int, unsi
4fd60 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 0a 2f 2a gned char*);../*
4fd70 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 Functions used
4fd80 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 61 20 50 to configure a P
4fd90 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a ager object. */.
4fda0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4fdb0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
4fdc0 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 50 SetBusyhandler(P
4fdd0 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 28 76 6f ager*, int(*)(vo
4fde0 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a 29 3b 0a id *), void *);.
4fdf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4fe00 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
4fe10 53 65 74 52 65 69 6e 69 74 65 72 28 50 61 67 65 SetReiniter(Page
4fe20 72 2a 2c 20 76 6f 69 64 28 2a 29 28 44 62 50 61 r*, void(*)(DbPa
4fe30 67 65 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ge*));.SQLITE_PR
4fe40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4fe50 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
4fe60 65 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 29 3b e(Pager*, u16*);
4fe70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4fe80 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
4fe90 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 MaxPageCount(Pag
4fea0 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 er*, int);.SQLIT
4feb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4fec0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 qlite3PagerSetCa
4fed0 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 chesize(Pager*,
4fee0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4fef0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4ff00 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 3PagerSetSafetyL
4ff10 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 2c evel(Pager*,int,
4ff20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4ff30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4ff40 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 PagerLockingMode
4ff50 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a (Pager *, int);.
4ff60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4ff70 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a nt sqlite3PagerJ
4ff80 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72 ournalMode(Pager
4ff90 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
4ffa0 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c _PRIVATE i64 sql
4ffb0 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
4ffc0 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 20 SizeLimit(Pager
4ffd0 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f *, i64);.SQLITE_
4ffe0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
4fff0 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65 33 backup **sqlite3
50000 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 50 PagerBackupPtr(P
50010 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 ager*);../* Func
50020 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6f 62 tions used to ob
50030 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 tain and release
50040 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 page references
50050 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 . */ .SQLITE_PRI
50060 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
50070 50 61 67 65 72 41 63 71 75 69 72 65 28 50 61 67 PagerAcquire(Pag
50080 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
50090 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20 2a 2a pgno, DbPage **
500a0 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c 72 46 ppPage, int clrF
500b0 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20 73 71 lag);.#define sq
500c0 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 41 2c lite3PagerGet(A,
500d0 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61 67 65 B,C) sqlite3Page
500e0 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43 2c 30 rAcquire(A,B,C,0
500f0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
50100 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 DbPage *sqlite3
50110 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 PagerLookup(Page
50120 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 r *pPager, Pgno
50130 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pgno);.SQLITE_PR
50140 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
50150 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 e3PagerRef(DbPag
50160 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
50170 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
50180 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61 67 PagerUnref(DbPag
50190 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61 74 69 e*);../* Operati
501a0 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65 66 65 ons on page refe
501b0 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c 49 54 rences. */.SQLIT
501c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
501d0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
501e0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
501f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
50200 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
50210 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 ite(DbPage*);.SQ
50220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
50230 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 sqlite3PagerMov
50240 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62 50 epage(Pager*,DbP
50250 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a age*,Pgno,int);.
50260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
50270 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 nt sqlite3PagerP
50280 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 ageRefcount(DbPa
50290 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ge*);.SQLITE_PRI
502a0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
502b0 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44 e3PagerGetData(D
502c0 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54 bPage *); .SQLIT
502d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
502e0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
502f0 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b 20 xtra(DbPage *);
50300 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 ../* Functions u
50310 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 70 61 sed to manage pa
50320 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ger transactions
50330 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74 73 2e and savepoints.
50340 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
50350 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
50360 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67 gerPagecount(Pag
50370 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 er*, int*);.SQLI
50380 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
50390 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e qlite3PagerBegin
503a0 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65 78 46 (Pager*, int exF
503b0 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 lag, int);.SQLIT
503c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
503d0 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
503e0 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c PhaseOne(Pager*,
503f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
50400 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 ter, int);.SQLIT
50410 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
50420 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 lite3PagerSync(P
50430 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 ager *pPager);.S
50440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
50450 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
50460 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 mmitPhaseTwo(Pag
50470 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
50480 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
50490 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 PagerRollback(Pa
504a0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
504b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
504c0 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
504d0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
504e0 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 r, int n);.SQLIT
504f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
50500 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f lite3PagerSavepo
50510 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
50520 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 r, int op, int i
50530 53 61 76 65 70 6f 69 6e 74 29 3b 0a 0a 2f 2a 20 Savepoint);../*
50540 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 Functions used t
50550 6f 20 71 75 65 72 79 20 70 61 67 65 72 20 73 74 o query pager st
50560 61 74 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 ate and configur
50570 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 ation. */.SQLITE
50580 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 _PRIVATE u8 sqli
50590 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e te3PagerIsreadon
505a0 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 ly(Pager*);.SQLI
505b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
505c0 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
505d0 75 6e 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c unt(Pager*);.SQL
505e0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
505f0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 t char *sqlite3P
50600 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 agerFilename(Pag
50610 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
50620 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 VATE const sqlit
50630 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 e3_vfs *sqlite3P
50640 61 67 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b agerVfs(Pager*);
50650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
50660 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 sqlite3_file *sq
50670 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 lite3PagerFile(P
50680 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
50690 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
506a0 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a r *sqlite3PagerJ
506b0 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 ournalname(Pager
506c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
506d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
506e0 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a gerNosync(Pager*
506f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
50700 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 E void *sqlite3P
50710 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 agerTempSpace(Pa
50720 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
50730 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
50740 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 3PagerIsMemdb(Pa
50750 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 ger*);../* Funct
50760 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75 ions used to tru
50770 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62 61 ncate the databa
50780 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 se file. */.SQLI
50790 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
507a0 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
507b0 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a cateImage(Pager*
507c0 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 55 73 65 64 ,Pgno);../* Used
507d0 20 62 79 20 65 6e 63 72 79 70 74 69 6f 6e 20 65 by encryption e
507e0 78 74 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 xtensions. */.#i
507f0 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
50800 43 4f 44 45 43 0a 53 51 4c 49 54 45 5f 50 52 49 CODEC.SQLITE_PRI
50810 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
50820 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 63 te3PagerSetCodec
50830 28 50 61 67 65 72 2a 2c 76 6f 69 64 2a 28 2a 29 (Pager*,void*(*)
50840 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e (void*,void*,Pgn
50850 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a 23 o,int),void*);.#
50860 65 6e 64 69 66 0a 0a 2f 2a 20 46 75 6e 63 74 69 endif../* Functi
50870 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ons to support t
50880 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 esting and debug
50890 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 ging. */.#if !de
508a0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c fined(NDEBUG) ||
508b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
508c0 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 TEST).SQLITE_PRI
508d0 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 VATE Pgno sqli
508e0 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 te3PagerPagenumb
508f0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c er(DbPage*);.SQL
50900 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
50910 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
50920 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 writeable(DbPage
50930 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 *);.#endif.#ifde
50940 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
50950 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
50960 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 nt *sqlite3Pager
50970 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 Stats(Pager*);.S
50980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
50990 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
509a0 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 rRefdump(Pager*)
509b0 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 ;. void disable
509c0 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
509d0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f rors(void);. vo
509e0 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 id enable_simula
509f0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f ted_io_errors(vo
50a00 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 id);.#else.# def
50a10 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 ine disable_simu
50a20 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
50a30 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c ).# define enabl
50a40 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
50a50 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a rrors().#endif..
50a60 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 #endif /* _PAGER
50a70 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
50a80 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
50a90 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ager.h *********
50aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50ac0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
50ad0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
50ae0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
50af0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
50b00 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
50b10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
50b20 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
50b30 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 pcache.h in the
50b40 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
50b50 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
50b60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
50b70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
50b80 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a le pcache.h ****
50b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50bb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
50bc0 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 08 August 05.**.
50bd0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
50be0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
50bf0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
50c00 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
50c10 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
50c20 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
50c30 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
50c40 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
50c50 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
50c60 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
50c70 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
50c80 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
50c90 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
50ca0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
50cb0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
50cc0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
50cd0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
50ce0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
50cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
50d30 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 his header file
50d40 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 defines the inte
50d50 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 rface that the s
50d60 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65 qlite page cache
50d70 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a .** subsystem. .
50d80 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
50d90 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 31 39 20 pcache.h,v 1.19
50da0 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 2009/01/20 17:06
50db0 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :27 danielk1977
50dc0 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 Exp $.*/..#ifnde
50dd0 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 f _PCACHE_H_..ty
50de0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 pedef struct PgH
50df0 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 dr PgHdr;.typede
50e00 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20 f struct PCache
50e10 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 PCache;../*.** E
50e20 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 very page in the
50e30 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f cache is contro
50e40 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 lled by an insta
50e50 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
50e60 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 wing.** structur
50e70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 e..*/.struct PgH
50e80 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 dr {. void *pDa
50e90 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
50ea0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 /* Content
50eb0 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f of this page */
50ec0 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b . void *pExtra;
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ee0 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 /* Extra conte
50ef0 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 nt */. PgHdr *p
50f00 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 Dirty;
50f10 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 /* Transi
50f20 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 ent list of dirt
50f30 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e y pages */. Pgn
50f40 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 o pgno;
50f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
50f60 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 age number for t
50f70 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 his page */. Pa
50f80 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 ger *pPager;
50f90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
50fa0 54 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70 The pager this p
50fb0 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a age is part of *
50fc0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
50fd0 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 CHECK_PAGES. u3
50fe0 32 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 2 pageHash;
50ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
51000 48 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e Hash of page con
51010 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 tent */.#endif.
51020 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 u16 flags;
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51040 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 /* PGHDR flags d
51050 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a efined below */.
51060 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . /************
51070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
510a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 **********. **
510b0 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 Elements above a
510c0 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 re public. All
510d0 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 that follows is
510e0 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 private to pcach
510f0 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f e.c. ** and sho
51100 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 uld not be acces
51110 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 sed by other mod
51120 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 ules.. */. i16
51130 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
51140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
51150 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f umber of users o
51160 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 f this page */.
51170 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b PCache *pCache;
51180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51190 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 /* Cache that ow
511a0 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a ns this page */.
511b0 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 . PgHdr *pDirty
511c0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
511d0 20 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e /* Next elemen
511e0 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 t in list of dir
511f0 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 ty pages */. Pg
51200 48 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b Hdr *pDirtyPrev;
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
51220 50 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 Previous element
51230 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 in list of dirt
51240 79 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f y pages */.};../
51250 2a 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 * Bit values for
51260 20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a PgHdr.flags */.
51270 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 #define PGHDR_DI
51280 52 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20 RTY
51290 30 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 0x002 /* Page h
512a0 61 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 as changed */.#d
512b0 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 efine PGHDR_NEED
512c0 5f 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 _SYNC 0x
512d0 30 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 004 /* Fsync th
512e0 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
512f0 61 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 al before.
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51320 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 ** writing this
51330 20 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 page to the dat
51340 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 abase */.#define
51350 20 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 PGHDR_NEED_READ
51360 20 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20 0x008
51370 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e /* Content is un
51380 72 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 read */.#define
51390 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 PGHDR_REUSE_UNLI
513a0 4b 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f KELY 0x010 /
513b0 2a 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 * A hint that re
513c0 75 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 use is unlikely
513d0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 */.#define PGHDR
513e0 5f 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 _DONT_WRITE
513f0 20 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 0x020 /* Do
51400 6e 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e not write conten
51410 74 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a t to disk */../*
51420 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
51430 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 shutdown the pag
51440 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 e cache subsyste
51450 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 m */.SQLITE_PRIV
51460 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
51470 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 cacheInitialize(
51480 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
51490 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
514a0 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e e3PcacheShutdown
514b0 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 (void);../* Page
514c0 20 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 cache buffer ma
514d0 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 nagement:.** The
514e0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c se routines impl
514f0 65 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e ement SQLITE_CON
51500 46 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a FIG_PAGECACHE..*
51510 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
51520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 void sqlite3PCa
51530 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76 cheBufferSetup(v
51540 6f 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 oid *, int sz, i
51550 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 nt n);../* Creat
51560 65 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 e a new pager ca
51570 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 che..** Under me
51580 6d 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 mory stress, inv
51590 6f 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 oke xStress to t
515a0 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 ry to make pages
515b0 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 clean..** Only
515c0 63 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e clean and unpinn
515d0 65 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 ed pages can be
515e0 72 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 reclaimed..*/.SQ
515f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
51600 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f d sqlite3PcacheO
51610 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 pen(. int szPag
51620 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
51630 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
51640 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 every page */.
51650 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 int szExtra,
51660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51670 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61 /* Extra space a
51680 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 ssociated with e
51690 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ach page */. in
516a0 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 t bPurgeable,
516b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
516c0 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
516d0 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
516e0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
516f0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 tress)(void*, Pg
51700 48 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 Hdr*), /* Call t
51710 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 o try to make pa
51720 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 ges clean */. v
51730 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 oid *pStress,
51740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
51750 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 Argument to xSt
51760 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 ress */. PCache
51770 20 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 *pToInit
51780 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 /* Prea
51790 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 llocated space f
517a0 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f or the PCache */
517b0 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 .);../* Modify t
517c0 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 he page-size aft
517d0 65 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73 er the cache has
517e0 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a been created. *
517f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
51800 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
51810 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 cheSetPageSize(P
51820 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a Cache *, int);..
51830 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 /* Return the si
51840 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 ze in bytes of a
51850 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 PCache object.
51860 20 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f Used to preallo
51870 63 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 cate.** storage
51880 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 space..*/.SQLITE
51890 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
518a0 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 ite3PcacheSize(v
518b0 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 oid);../* One re
518c0 6c 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73 lease per succes
518d0 73 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 sful fetch. Pag
518e0 65 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 e is pinned unti
518f0 6c 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 l released..** R
51900 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 eference counted
51910 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 . .*/.SQLITE_PRI
51920 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
51930 50 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63 PcacheFetch(PCac
51940 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 he*, Pgno, int c
51950 72 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 reateFlag, PgHdr
51960 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
51970 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
51980 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 PcacheRelease(Pg
51990 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 Hdr*);..SQLITE_P
519a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
519b0 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 te3PcacheDrop(Pg
519c0 48 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f Hdr*); /
519d0 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 * Remove page fr
519e0 6f 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 om cache */.SQLI
519f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
51a00 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
51a10 65 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 eDirty(PgHdr*);
51a20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 /* Make sure
51a30 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 page is marked d
51a40 69 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 irty */.SQLITE_P
51a50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
51a60 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
51a70 61 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f an(PgHdr*); /
51a80 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 * Mark a single
51a90 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f page as clean */
51aa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
51ab0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
51ac0 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 heCleanAll(PCach
51ad0 65 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 e*); /* Mark
51ae0 61 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 all dirty list p
51af0 61 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f ages as clean */
51b00 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 ../* Change a pa
51b10 67 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 ge number. Used
51b20 20 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e by incr-vacuum.
51b30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
51b40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
51b50 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a cacheMove(PgHdr*
51b60 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d , Pgno);../* Rem
51b70 6f 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 ove all pages wi
51b80 74 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 th pgno>x. Rese
51b90 74 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78 t the cache if x
51ba0 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ==0 */.SQLITE_PR
51bb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
51bc0 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 e3PcacheTruncate
51bd0 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 (PCache*, Pgno x
51be0 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 );../* Get a lis
51bf0 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 t of all dirty p
51c00 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 ages in the cach
51c10 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 e, sorted by pag
51c20 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 e number */.SQLI
51c30 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 TE_PRIVATE PgHdr
51c40 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 *sqlite3PcacheD
51c50 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a irtyList(PCache*
51c60 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 );../* Reset and
51c70 20 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 close the cache
51c80 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 object */.SQLIT
51c90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
51ca0 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 qlite3PcacheClos
51cb0 65 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 e(PCache*);../*
51cc0 43 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d Clear flags from
51cd0 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 pages of the pa
51ce0 67 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 ge cache */.SQLI
51cf0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
51d00 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
51d10 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 arSyncFlags(PCac
51d20 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 he *);../* Disca
51d30 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 rd the contents
51d40 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a of the cache */.
51d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
51d60 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
51d70 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b eClear(PCache*);
51d80 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ../* Return the
51d90 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
51da0 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
51db0 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 references */.S
51dc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
51dd0 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 t sqlite3PcacheR
51de0 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 efCount(PCache*)
51df0 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 ;../* Increment
51e00 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
51e10 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 unt of an existi
51e20 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 ng page */.SQLIT
51e30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
51e40 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 qlite3PcacheRef(
51e50 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 PgHdr*);..SQLITE
51e60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
51e70 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 ite3PcachePageRe
51e80 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a fcount(PgHdr*);.
51e90 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 ./* Return the t
51ea0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
51eb0 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 ages stored in t
51ec0 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 he cache */.SQLI
51ed0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
51ee0 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
51ef0 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a count(PCache*);.
51f00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
51f10 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 20 49 74 HECK_PAGES./* It
51f20 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c erate through al
51f30 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 l dirty pages cu
51f40 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 rrently stored i
51f50 6e 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 69 n the cache. Thi
51f60 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 s.** interface i
51f70 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
51f80 20 69 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b if SQLITE_CHECK
51f90 5f 50 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 _PAGES is define
51fa0 64 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c d when the .** l
51fb0 69 62 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e ibrary is built.
51fc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
51fd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
51fe0 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 cacheIterateDirt
51ff0 79 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 y(PCache *pCache
52000 2c 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 , void (*xIter)(
52010 50 67 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 PgHdr *));.#endi
52020 66 0a 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 f../* Set and ge
52030 74 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 t the suggested
52040 63 61 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 cache-size for t
52050 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 he specified pag
52060 65 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 er-cache..**.**
52070 49 66 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 If no global max
52080 69 6d 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 imum is configur
52090 65 64 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73 ed, then the sys
520a0 74 65 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 tem attempts to
520b0 6c 69 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 limit.** the tot
520c0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
520d0 65 73 20 63 61 63 68 65 64 20 62 79 20 70 75 72 es cached by pur
520e0 67 65 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63 geable pager-cac
520f0 68 65 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a hes to the sum.*
52100 2a 20 6f 66 20 74 68 65 20 73 75 67 67 65 73 74 * of the suggest
52110 65 64 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a ed cache-sizes..
52120 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
52130 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
52140 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 acheSetCachesize
52150 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b (PCache *, int);
52160 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
52170 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 EST.SQLITE_PRIVA
52180 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
52190 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 acheGetCachesize
521a0 28 50 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 (PCache *);.#end
521b0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
521c0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f E_ENABLE_MEMORY_
521d0 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 MANAGEMENT./* Tr
521e0 79 20 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f y to return memo
521f0 72 79 20 75 73 65 64 20 62 79 20 74 68 65 20 70 ry used by the p
52200 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 cache module to
52210 74 68 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 the main memory
52220 68 65 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 heap */.SQLITE_P
52230 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
52240 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d e3PcacheReleaseM
52250 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 emory(int);.#end
52260 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
52270 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
52280 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52290 65 33 50 63 61 63 68 65 53 74 61 74 73 28 69 6e e3PcacheStats(in
522a0 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 t*,int*,int*,int
522b0 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 *);.#endif..SQLI
522c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
522d0 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 sqlite3PCacheSet
522e0 44 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a Default(void);..
522f0 23 65 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 #endif /* _PCACH
52300 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a E_H_ */../******
52310 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
52320 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a pcache.h *******
52330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52350 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
52360 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
52370 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
52380 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 t off in sqliteI
52390 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
523a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a *******/../*****
523b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
523c0 65 20 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 e os.h in the mi
523d0 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
523e0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
523f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
52400 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
52410 66 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a file os.h ******
52420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52440 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
52450 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
52460 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
52470 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
52480 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
52490 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
524a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
524b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
524c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
524d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
524e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
524f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
52500 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
52510 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
52520 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
52530 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
52540 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
52550 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
52560 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
52570 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
52580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
525a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
525b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
525c0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
525d0 20 68 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f header file (to
525e0 67 65 74 68 65 72 20 77 69 74 68 20 69 73 20 63 gether with is c
525f0 6f 6d 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 ompanion C sourc
52600 65 2d 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 e-code file.** "
52610 6f 73 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74 os.c") attempt t
52620 6f 20 61 62 73 74 72 61 63 74 20 74 68 65 20 75 o abstract the u
52630 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 nderlying operat
52640 69 6e 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68 ing system so th
52650 61 74 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 at.** the SQLite
52660 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f library will wo
52670 72 6b 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 rk on both POSIX
52680 20 61 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73 and windows sys
52690 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tems..**.** This
526a0 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 header file is
526b0 23 69 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73 #include-ed by s
526c0 71 6c 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 qliteInt.h and t
526d0 68 75 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 hus ends up.** b
526e0 65 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79 eing included by
526f0 20 65 76 65 72 79 20 73 6f 75 72 63 65 20 66 69 every source fi
52700 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f le..**.** $Id: o
52710 73 2e 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39 s.h,v 1.108 2009
52720 2f 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 /02/05 16:31:46
52730 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 drh Exp $.*/.#if
52740 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f ndef _SQLITE_OS_
52750 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 H_.#define _SQLI
52760 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 TE_OS_H_../*.**
52770 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 Figure out if we
52780 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
52790 68 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c h Unix, Windows,
527a0 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a or some other.*
527b0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 * operating syst
527c0 65 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 em. After the f
527d0 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f ollowing block o
527e0 66 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 f preprocess mac
527f0 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 ros,.** all of S
52800 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 QLITE_OS_UNIX, S
52810 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 QLITE_OS_WIN, SQ
52820 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 LITE_OS_OS2, and
52830 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 SQLITE_OS_OTHER
52840 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 .** will define
52850 64 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 d to either 1 or
52860 20 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 0. One of the
52870 66 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 four will be 1.
52880 20 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 The other .** t
52890 68 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a hree will be 0..
528a0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
528b0 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a QLITE_OS_OTHER).
528c0 23 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f # if SQLITE_OS_O
528d0 54 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 THER==1.# unde
528e0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 f SQLITE_OS_UNIX
528f0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 .# define SQLI
52900 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 TE_OS_UNIX 0.#
52910 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 undef SQLITE_OS
52920 5f 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 _WIN.# define
52930 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a SQLITE_OS_WIN 0.
52940 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 # undef SQLITE
52950 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 _OS_OS2.# defi
52960 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 ne SQLITE_OS_OS2
52970 20 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 0.# else.# un
52980 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 def SQLITE_OS_OT
52990 48 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 HER.# endif.#end
529a0 69 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 if.#if !defined(
529b0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 SQLITE_OS_UNIX)
529c0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
529d0 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 TE_OS_OTHER).# d
529e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
529f0 4f 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 OTHER 0.# ifndef
52a00 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 SQLITE_OS_WIN.#
52a10 20 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 if defined(_W
52a20 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 IN32) || defined
52a30 28 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e (WIN32) || defin
52a40 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c ed(__CYGWIN__) |
52a50 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 | defined(__MING
52a60 57 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 W32__) || define
52a70 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a d(__BORLANDC__).
52a80 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c # define SQL
52a90 49 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 ITE_OS_WIN 1.#
52aa0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
52ab0 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 _OS_UNIX 0.#
52ac0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
52ad0 53 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 S_OS2 0.# elif
52ae0 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f defined(__EMX__
52af0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 ) || defined(_OS
52b00 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 2) || defined(OS
52b10 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 2) || defined(_O
52b20 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 S2_) || defined(
52b30 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 __OS2__).# d
52b40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
52b50 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 WIN 0.# defi
52b60 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 ne SQLITE_OS_UNI
52b70 58 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 X 0.# define
52b80 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 SQLITE_OS_OS2 1
52b90 0a 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 .# else.#
52ba0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
52bb0 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 _WIN 0.# def
52bc0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e ine SQLITE_OS_UN
52bd0 49 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e IX 1.# defin
52be0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 e SQLITE_OS_OS2
52bf0 30 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 0.# endif.# els
52c00 65 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 e.# define SQLI
52c10 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 TE_OS_UNIX 0.#
52c20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
52c30 5f 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 _OS2 0.# endif.#
52c40 65 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 else.# ifndef SQ
52c50 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 LITE_OS_WIN.# d
52c60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
52c70 57 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 WIN 0.# endif.#e
52c80 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 ndif../*.** Dete
52c90 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 rmine if we are
52ca0 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e dealing with Win
52cb0 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 dowsCE - which h
52cc0 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 as a much.** red
52cd0 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 uced API..*/.#if
52ce0 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f defined(_WIN32_
52cf0 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 WCE).# define SQ
52d00 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a LITE_OS_WINCE 1.
52d10 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
52d20 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 QLITE_OS_WINCE 0
52d30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
52d40 44 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d Define the maxim
52d50 75 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d um size of a tem
52d60 70 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a porary filename.
52d70 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
52d80 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c _WIN.# include <
52d90 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 windows.h>.# def
52da0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e ine SQLITE_TEMPN
52db0 41 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 AME_SIZE (MAX_PA
52dc0 54 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c TH+50).#elif SQL
52dd0 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 ITE_OS_OS2.# if
52de0 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c (__GNUC__ > 3 ||
52df0 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 __GNUC__ == 3 &
52e00 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f & __GNUC_MINOR__
52e10 20 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 >= 3) && define
52e20 64 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 d(OS2_HIGH_MEMOR
52e30 59 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f Y).# include <o
52e40 73 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 s2safe.h> /* has
52e50 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 to be included
52e60 62 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 before os2.h for
52e70 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b linking to work
52e80 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 */.# endif.# de
52e90 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 fine INCL_DOSDAT
52ea0 45 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 ETIME.# define I
52eb0 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 NCL_DOSFILEMGR.#
52ec0 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 define INCL_DOS
52ed0 45 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 ERRORS.# define
52ee0 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 INCL_DOSMISC.# d
52ef0 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 efine INCL_DOSPR
52f00 4f 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 OCESS.# define I
52f10 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 NCL_DOSMODULEMGR
52f20 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 .# define INCL_D
52f30 4f 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 OSSEMAPHORES.# i
52f40 6e 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 nclude <os2.h>.#
52f50 20 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e include <uconv.
52f60 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 h>.# define SQLI
52f70 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 TE_TEMPNAME_SIZE
52f80 20 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 (CCHMAXPATHCOMP
52f90 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
52fa0 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 SQLITE_TEMPNAME
52fb0 5f 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 _SIZE 200.#endif
52fc0 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f ../* If the SET_
52fd0 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 FULLSYNC macro i
52fe0 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 s not defined ab
52ff0 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 ove, then make i
53000 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a t.** a no-op.*/.
53010 23 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c #ifndef SET_FULL
53020 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 SYNC.# define SE
53030 54 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a T_FULLSYNC(x,y).
53040 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
53050 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f e default size o
53060 66 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a f a disk sector.
53070 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
53080 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
53090 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 _SIZE.# define S
530a0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
530b0 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 CTOR_SIZE 512.#e
530c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 ndif../*.** Temp
530d0 6f 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 orary files are
530e0 6e 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 named starting w
530f0 69 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 ith this prefix
53100 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 followed by 16 r
53110 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 andom.** alphanu
53120 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 meric characters
53130 2c 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 , and no file ex
53140 74 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 tension. They ar
53150 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a e stored in the.
53160 2a 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 ** OS's standard
53170 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
53180 64 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 directory, and a
53190 72 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 re deleted prior
531a0 20 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 to exit..** If
531b0 73 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 sqlite is being
531c0 65 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 embedded in anot
531d0 68 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 her program, you
531e0 20 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 may wish to cha
531f0 6e 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 nge the.** prefi
53200 78 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 x to reflect you
53210 72 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 r program's name
53220 2c 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 , so that if you
53230 72 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a r program exits.
53240 2a 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 ** prematurely,
53250 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 old temporary fi
53260 6c 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c les can be easil
53270 79 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 y identified. Th
53280 69 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a is can be done.*
53290 2a 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 * using -DSQLITE
532a0 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 _TEMP_FILE_PREFI
532b0 58 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 X=myprefix_ on t
532c0 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d he compiler comm
532d0 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 and line..**.**
532e0 32 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 2006-10-31: The
532f0 20 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 default prefix
53300 75 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 used to be "sqli
53310 74 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a te_". But then.
53320 2a 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 ** Mcafee starte
53330 64 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 d using SQLite i
53340 6e 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 n their anti-vir
53350 75 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 us product and i
53360 74 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 t.** started put
53370 74 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 ting files with
53380 74 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d the "sqlite" nam
53390 65 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 e in the c:/temp
533a0 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 folder..** This
533b0 20 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 annoyed many wi
533c0 6e 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 ndows users. Th
533d0 6f 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 ose users would
533e0 74 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f then do a .** Go
533f0 6f 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 ogle search for
53400 22 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 "sqlite", find t
53410 68 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d he telephone num
53420 62 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 bers of the.** d
53430 65 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 evelopers and ca
53440 6c 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 ll to wake them
53450 75 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 up at night and
53460 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 complain..** For
53470 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 this reason, th
53480 65 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 e default name p
53490 72 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 refix is changed
534a0 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 to be "sqlite"
534b0 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b .** spelled back
534c0 77 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 wards. So the t
534d0 65 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 emp files are st
534e0 69 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 ill identified,
534f0 62 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 but.** anybody s
53500 6d 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 mart enough to f
53510 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f igure out the co
53520 64 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c de is also likel
53530 79 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 y smart.** enoug
53540 68 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 h to know that c
53550 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c alling the devel
53560 6f 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 oper will not he
53570 6c 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 lp get rid.** of
53580 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 the file..*/.#i
53590 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d fndef SQLITE_TEM
535a0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 P_FILE_PREFIX.#
535b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
535c0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 MP_FILE_PREFIX "
535d0 65 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a etilqs_".#endif.
535e0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
535f0 77 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 wing values may
53600 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 be passed as the
53610 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
53620 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 to.** sqlite3Os
53630 4c 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 Lock(). The vari
53640 6f 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 ous locks exhibi
53650 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 t the following
53660 73 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a semantics:.**.**
53670 20 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 SHARED: Any
53680 6e 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 number of proces
53690 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 ses may hold a S
536a0 48 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c HARED lock simul
536b0 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 taneously..** RE
536c0 53 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c SERVED: A singl
536d0 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f e process may ho
536e0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f ld a RESERVED lo
536f0 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a ck on a file at.
53700 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e ** an
53710 79 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 y time. Other pr
53720 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 ocesses may hold
53730 20 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 and obtain new
53740 53 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a SHARED locks..**
53750 20 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 PENDING: A si
53760 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 ngle process may
53770 20 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 hold a PENDING
53780 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 lock on a file a
53790 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
537a0 61 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 any one time. Ex
537b0 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f isting SHARED lo
537c0 63 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c cks may persist,
537d0 20 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 but no new.**
537e0 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 SHARED
537f0 20 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 locks may be ob
53800 74 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 tained by other
53810 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 processes..** EX
53820 43 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c CLUSIVE: An EXCL
53830 55 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c USIVE lock precl
53840 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c udes all other l
53850 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 ocks..**.** PEND
53860 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 ING_LOCK may not
53870 20 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 be passed direc
53880 74 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 tly to sqlite3Os
53890 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c Lock(). Instead,
538a0 20 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 a.** process th
538b0 61 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 at requests an E
538c0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 XCLUSIVE lock ma
538d0 79 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 y actually obtai
538e0 6e 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c n a PENDING.** l
538f0 6f 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 ock. This can be
53900 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 upgraded to an
53910 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 EXCLUSIVE lock b
53920 79 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 y a subsequent c
53930 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
53940 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 3OsLock()..*/.#d
53950 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 efine NO_LOCK
53960 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
53970 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 SHARED_LOCK
53980 31 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 1.#define RESERV
53990 45 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 ED_LOCK 2.#def
539a0 69 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ine PENDING_LOCK
539b0 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 3.#define EX
539c0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a CLUSIVE_LOCK 4.
539d0 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b ./*.** File Lock
539e0 69 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 ing Notes: (Mos
539f0 74 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 tly about window
53a00 73 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 s but also some
53a10 69 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a info for Unix).*
53a20 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 *.** We cannot u
53a30 73 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 se LockFileEx()
53a40 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 or UnlockFileEx(
53a50 29 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 ) on Win95/98/ME
53a60 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 because.** thos
53a70 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
53a80 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 not available.
53a90 53 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c So we use only L
53aa0 6f 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a ockFile() and.**
53ab0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a UnlockFile()..*
53ac0 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 *.** LockFile()
53ad0 70 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 prevents not jus
53ae0 74 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c t writing but al
53af0 73 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 so reading by ot
53b00 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a her processes..*
53b10 2a 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 * A SHARED_LOCK
53b20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c is obtained by l
53b30 6f 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 ocking a single
53b40 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 randomly-chosen
53b50 0a 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 .** byte out of
53b60 61 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 a specific range
53b70 20 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c of bytes. The l
53b80 6f 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 ock byte is obta
53b90 69 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 ined at .** rand
53ba0 6f 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 om so two separa
53bb0 74 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 te readers can p
53bc0 72 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 robably access t
53bd0 68 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a he file at the .
53be0 2a 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e ** same time, un
53bf0 6c 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e less they are un
53c00 6c 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 lucky and choose
53c10 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 the same lock b
53c20 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 yte..** An EXCLU
53c30 53 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 SIVE_LOCK is obt
53c40 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 ained by locking
53c50 20 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 all bytes in th
53c60 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 e range..** Ther
53c70 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e e can only be on
53c80 65 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 e writer. A RES
53c90 45 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 ERVED_LOCK is ob
53ca0 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e tained by lockin
53cb0 67 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 g.** a single by
53cc0 74 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 te of the file t
53cd0 68 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 hat is designate
53ce0 64 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 d as the reserve
53cf0 64 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 d lock byte..**
53d00 41 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 A PENDING_LOCK i
53d10 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
53d20 63 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 cking a designat
53d30 65 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e ed byte differen
53d40 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 t from.** the RE
53d50 53 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 SERVED_LOCK byte
53d60 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 ..**.** On WinNT
53d70 2f 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 /2K/XP systems,
53d80 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 LockFileEx() and
53d90 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 UnlockFileEx()
53da0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a are available,.*
53db0 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 * which means we
53dc0 20 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f can use reader/
53dd0 77 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 writer locks. W
53de0 68 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 hen reader/write
53df0 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 r locks.** are u
53e00 73 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 sed, the lock is
53e10 20 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 placed on the s
53e20 61 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 ame range of byt
53e30 65 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a es that is used.
53e40 2a 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 ** for probabili
53e50 73 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 stic locking in
53e60 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 Win95/98/ME. He
53e70 6e 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 nce, the locking
53e80 20 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 scheme.** will
53e90 73 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d support two or m
53ea0 6f 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 ore Win95 reader
53eb0 73 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 s or two or more
53ec0 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a WinNT readers..
53ed0 2a 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 ** But a single
53ee0 57 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c Win95 reader wil
53ef0 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 l lock out all W
53f00 69 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 inNT readers and
53f10 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e a single.** Win
53f20 4e 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c NT reader will l
53f30 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 ock out all othe
53f40 72 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e r Win95 readers.
53f50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
53f60 77 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 wing #defines sp
53f70 65 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 ecify the range
53f80 6f 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f of bytes used fo
53f90 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 r locking..** SH
53fa0 41 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 ARED_SIZE is the
53fb0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
53fc0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 available in th
53fd0 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 e pool from whic
53fe0 68 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 h.** a random by
53ff0 74 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 te is selected f
54000 6f 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b or a shared lock
54010 2e 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 . The pool of b
54020 79 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 ytes for.** shar
54030 65 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 ed locks begins
54040 61 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e at SHARED_FIRST.
54050 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 .**.** The same
54060 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
54070 79 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 y and.** byte ra
54080 6e 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f nges are used fo
54090 72 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 r Unix. This le
540a0 61 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f aves open the po
540b0 73 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 ssiblity of havi
540c0 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e ng.** clients on
540d0 20 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 win95, winNT, a
540e0 6e 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b nd unix all talk
540f0 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ing to the same
54100 73 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 shared file.** a
54110 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 nd all locking c
54120 6f 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f orrectly. To do
54130 20 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 so would requir
54140 65 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 e that samba (or
54150 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f whatever.** too
54160 6c 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 l is being used
54170 66 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 for file sharing
54180 29 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 ) implements loc
54190 6b 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 ks correctly bet
541a0 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 ween.** windows
541b0 61 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 and unix. I'm g
541c0 75 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e uessing that isn
541d0 27 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 't likely to hap
541e0 70 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 pen, but by.** u
541f0 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f sing the same lo
54200 63 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 cking range we a
54210 72 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e re at least open
54220 20 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c to the possibil
54230 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 ity..**.** Locki
54240 6e 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 ng in windows is
54250 20 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 manditory. For
54260 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 this reason, we
54270 20 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a cannot store.**
54280 20 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 actual data in
54290 74 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66 the bytes used f
542a0 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 or locking. The
542b0 20 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c pager never all
542c0 6f 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 ocates.** the pa
542d0 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 ges involved in
542e0 6c 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 locking therefor
542f0 65 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 e. SHARED_SIZE
54300 69 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a is selected so.*
54310 2a 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 * that all locks
54320 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 will fit on a s
54330 69 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 ingle page even
54340 61 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 at the minimum p
54350 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e age size..** PEN
54360 44 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 DING_BYTE define
54370 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 s the beginning
54380 6f 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 of the locks. B
54390 79 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e y default PENDIN
543a0 47 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 G_BYTE.** is set
543b0 20 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 high so that we
543c0 20 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 don't have to a
543d0 6c 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 llocate an unuse
543e0 64 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a d page except.**
543f0 20 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 for very large
54400 64 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 databases. But
54410 6f 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 one should test
54420 74 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e the page skippin
54430 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 g logic .** by s
54440 65 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 etting PENDING_B
54450 59 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e YTE low and runn
54460 69 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 ing the entire r
54470 65 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e egression suite.
54480 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 .**.** Changing
54490 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e the value of PEN
544a0 44 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 DING_BYTE result
544b0 73 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e s in a subtly in
544c0 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 compatible.** fi
544d0 6c 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 le format. Depe
544e0 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 nding on how it
544f0 69 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 is changed, you
54500 6d 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 might not notice
54510 0a 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 .** the incompat
54520 69 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 ibility right aw
54530 61 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 ay, even running
54540 20 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 a full regressi
54550 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 on test..** The
54560 64 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e default location
54570 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 of PENDING_BYTE
54580 20 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79 is the first by
54590 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 te past the.** 1
545a0 47 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a GB boundary..**.
545b0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 */.#define PENDI
545c0 4e 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c NG_BYTE sql
545d0 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a ite3PendingByte.
545e0 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 #define RESERVED
545f0 5f 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 _BYTE (PENDI
54600 4e 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 NG_BYTE+1).#defi
54610 6e 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 ne SHARED_FIRST
54620 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 (PENDING_BY
54630 54 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 TE+2).#define SH
54640 41 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 ARED_SIZE
54650 35 31 30 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 510../* .** Func
54660 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 tions for access
54670 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ing sqlite3_file
54680 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c methods .*/.SQL
54690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
546a0 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 sqlite3OsClose(s
546b0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 qlite3_file*);.S
546c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
546d0 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 t sqlite3OsRead(
546e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 sqlite3_file*, v
546f0 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 oid*, int amt, i
54700 36 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 64 offset);.SQLI
54710 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
54720 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 qlite3OsWrite(sq
54730 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e lite3_file*, con
54740 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d st void*, int am
54750 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a t, i64 offset);.
54760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
54770 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e nt sqlite3OsTrun
54780 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c cate(sqlite3_fil
54790 65 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53 e*, i64 size);.S
547a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
547b0 74 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 t sqlite3OsSync(
547c0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
547d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
547e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
547f0 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 sFileSize(sqlite
54800 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53 3_file*, i64 *pS
54810 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ize);.SQLITE_PRI
54820 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
54830 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 OsLock(sqlite3_f
54840 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ile*, int);.SQLI
54850 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
54860 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 qlite3OsUnlock(s
54870 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
54880 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
54890 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
548a0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
548b0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
548c0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
548d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
548e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 E int sqlite3OsF
548f0 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 ileControl(sqlit
54900 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 e3_file*,int,voi
54910 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c d*);.#define SQL
54920 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 ITE_FCNTL_DB_UNC
54930 48 41 4e 47 45 44 20 30 78 63 61 30 39 33 66 61 HANGED 0xca093fa
54940 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 0.SQLITE_PRIVATE
54950 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 65 int sqlite3OsSe
54960 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 ctorSize(sqlite3
54970 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49 _file *id);.SQLI
54980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
54990 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
549a0 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 aracteristics(sq
549b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b lite3_file *id);
549c0 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f ../* .** Functio
549d0 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 ns for accessing
549e0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 sqlite3_vfs met
549f0 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f hods .*/.SQLITE_
54a00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
54a10 74 65 33 4f 73 4f 70 65 6e 28 73 71 6c 69 74 65 te3OsOpen(sqlite
54a20 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
54a30 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 har *, sqlite3_f
54a40 69 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a ile*, int, int *
54a50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
54a60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 E int sqlite3OsD
54a70 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 elete(sqlite3_vf
54a80 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 s *, const char
54a90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
54aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
54ab0 74 65 33 4f 73 41 63 63 65 73 73 28 73 71 6c 69 te3OsAccess(sqli
54ac0 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 te3_vfs *, const
54ad0 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e char *, int, in
54ae0 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c t *pResOut);.SQL
54af0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54b00 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 sqlite3OsFullPat
54b10 68 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 hname(sqlite3_vf
54b20 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 s *, const char
54b30 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b *, int, char *);
54b40 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
54b50 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
54b60 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ION.SQLITE_PRIVA
54b70 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
54b80 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 OsDlOpen(sqlite3
54b90 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 _vfs *, const ch
54ba0 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ar *);.SQLITE_PR
54bb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
54bc0 65 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 e3OsDlError(sqli
54bd0 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 te3_vfs *, int,
54be0 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f char *);.SQLITE_
54bf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 PRIVATE void (*s
54c00 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 qlite3OsDlSym(sq
54c10 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f 69 lite3_vfs *, voi
54c20 64 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 d *, const char
54c30 2a 29 29 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 *))(void);.SQLIT
54c40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
54c50 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 qlite3OsDlClose(
54c60 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 sqlite3_vfs *, v
54c70 6f 69 64 20 2a 29 3b 0a 23 65 6e 64 69 66 20 2f oid *);.#endif /
54c80 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f * SQLITE_OMIT_LO
54c90 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a AD_EXTENSION */.
54ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
54cb0 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 nt sqlite3OsRand
54cc0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 omness(sqlite3_v
54cd0 66 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 fs *, int, char
54ce0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
54cf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
54d00 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
54d10 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 s *, int);.SQLIT
54d20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
54d30 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
54d40 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a me(sqlite3_vfs *
54d50 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a , double*);../*.
54d60 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 66 ** Convenience f
54d70 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f 70 65 unctions for ope
54d80 6e 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ning and closing
54d90 20 66 69 6c 65 73 20 75 73 69 6e 67 20 0a 2a 2a files using .**
54da0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
54db0 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 ) to obtain spac
54dc0 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2d 68 e for the file-h
54dd0 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e andle structure.
54de0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
54df0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
54e00 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 OpenMalloc(sqlit
54e10 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 e3_vfs *, const
54e20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f char *, sqlite3_
54e30 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74 file **, int,int
54e40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
54e50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
54e60 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 CloseFree(sqlite
54e70 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64 3_file *);..#end
54e80 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 if /* _SQLITE_OS
54e90 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
54ea0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f ******* End of o
54eb0 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.h ************
54ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54ee0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54ef0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
54f00 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
54f10 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
54f20 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
54f30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54f40 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
54f50 6d 75 74 65 78 2e 68 20 69 6e 20 74 68 65 20 6d mutex.h in the m
54f60 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
54f70 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
54f80 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54f90 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
54fa0 6c 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a le mutex.h *****
54fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54fd0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
54fe0 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 07 August 28.**.
54ff0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
55000 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
55010 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
55020 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
55030 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
55040 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
55050 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
55060 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
55070 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
55080 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
55090 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
550a0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
550b0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
550c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
550d0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
550e0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
550f0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
55100 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
55110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
55150 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
55160 61 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 ains the common
55170 68 65 61 64 65 72 20 66 6f 72 20 61 6c 6c 20 6d header for all m
55180 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 utex implementat
55190 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c ions..** The sql
551a0 69 74 65 49 6e 74 2e 68 20 68 65 61 64 65 72 20 iteInt.h header
551b0 23 69 6e 63 6c 75 64 65 73 20 74 68 69 73 20 66 #includes this f
551c0 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 69 ile so that it i
551d0 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 s available.** t
551e0 6f 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 69 6c o all source fil
551f0 65 73 2e 20 20 57 65 20 62 72 65 61 6b 20 69 74 es. We break it
55200 20 6f 75 74 20 69 6e 20 61 6e 20 65 66 66 6f 72 out in an effor
55210 74 20 74 6f 20 6b 65 65 70 20 74 68 65 20 63 6f t to keep the co
55220 64 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f 72 67 de.** better org
55230 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f anized..**.** NO
55240 54 45 3a 20 20 73 6f 75 72 63 65 20 66 69 6c 65 TE: source file
55250 73 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23 s should *not* #
55260 69 6e 63 6c 75 64 65 20 74 68 69 73 20 68 65 61 include this hea
55270 64 65 72 20 66 69 6c 65 20 64 69 72 65 63 74 6c der file directl
55280 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66 69 6c y..** Source fil
55290 65 73 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75 es should #inclu
552a0 64 65 20 74 68 65 20 73 71 6c 69 74 65 49 6e 74 de the sqliteInt
552b0 2e 68 20 66 69 6c 65 20 61 6e 64 20 6c 65 74 20 .h file and let
552c0 74 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69 6e 63 that file.** inc
552d0 6c 75 64 65 20 74 68 69 73 20 6f 6e 65 20 69 6e lude this one in
552e0 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 directly..**.**
552f0 24 49 64 3a 20 6d 75 74 65 78 2e 68 2c 76 20 31 $Id: mutex.h,v 1
55300 2e 39 20 32 30 30 38 2f 31 30 2f 30 37 20 31 35 .9 2008/10/07 15
55310 3a 32 35 3a 34 38 20 64 72 68 20 45 78 70 20 24 :25:48 drh Exp $
55320 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75 .*/.../*.** Figu
55330 72 65 20 6f 75 74 20 77 68 61 74 20 76 65 72 73 re out what vers
55340 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 64 65 20 ion of the code
55350 74 6f 20 75 73 65 2e 20 20 54 68 65 20 63 68 6f to use. The cho
55360 69 63 65 73 20 61 72 65 0a 2a 2a 0a 2a 2a 20 20 ices are.**.**
55370 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d SQLITE_MUTEX_OM
55380 49 54 20 20 20 20 20 20 20 20 20 4e 6f 20 6d 75 IT No mu
55390 74 65 78 20 6c 6f 67 69 63 2e 20 20 4e 6f 74 20 tex logic. Not
553a0 65 76 65 6e 20 73 74 75 62 73 2e 20 20 54 68 65 even stubs. The
553b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
553c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
553d0 6d 75 74 65 78 65 73 20 69 6d 70 6c 65 6d 65 6e mutexes implemen
553e0 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6f tion cannot be o
553f0 76 65 72 72 69 64 64 65 6e 0a 2a 2a 20 20 20 20 verridden.**
55400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55410 20 20 20 20 20 20 20 20 20 61 74 20 73 74 61 72 at star
55420 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 t-time..**.**
55430 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
55440 50 20 20 20 20 20 20 20 20 20 46 6f 72 20 73 69 P For si
55450 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 ngle-threaded ap
55460 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 4e 6f 0a plications. No.
55470 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
55480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
55490 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 utual exclusion
554a0 69 73 20 70 72 6f 76 69 64 65 64 2e 20 20 42 75 is provided. Bu
554b0 74 20 74 68 69 73 0a 2a 2a 20 20 20 20 20 20 20 t this.**
554c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
554d0 20 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 61 implementa
554e0 74 69 6f 6e 20 63 61 6e 20 62 65 20 6f 76 65 72 tion can be over
554f0 72 69 64 64 65 6e 20 61 74 0a 2a 2a 20 20 20 20 ridden at.**
55500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55510 20 20 20 20 20 20 20 20 20 73 74 61 72 74 2d 74 start-t
55520 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c ime..**.** SQL
55530 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
55540 44 53 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 DS For multi
55550 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 -threaded applic
55560 61 74 69 6f 6e 73 20 6f 6e 20 55 6e 69 78 2e 0a ations on Unix..
55570 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
55580 55 54 45 58 5f 57 33 32 20 20 20 20 20 20 20 20 UTEX_W32
55590 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 For multi-thre
555a0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
555b0 73 20 6f 6e 20 57 69 6e 33 32 2e 0a 2a 2a 0a 2a s on Win32..**.*
555c0 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 * SQLITE_MUTEX
555d0 5f 4f 53 32 20 20 20 20 20 20 20 20 20 20 46 6f _OS2 Fo
555e0 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 r multi-threaded
555f0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e applications on
55600 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 66 20 21 53 OS/2..*/.#if !S
55610 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
55620 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
55630 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65 6e 64 _MUTEX_OMIT.#end
55640 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 if.#if SQLITE_TH
55650 52 45 41 44 53 41 46 45 20 26 26 20 21 64 65 66 READSAFE && !def
55660 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 ined(SQLITE_MUTE
55670 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20 53 51 X_NOOP).# if SQ
55680 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20 LITE_OS_UNIX.#
55690 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
556a0 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a 23 MUTEX_PTHREADS.#
556b0 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 elif SQLITE_OS
556c0 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69 6e 65 _WIN.# define
556d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
556e0 32 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 45 2.# elif SQLITE
556f0 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 20 64 65 66 _OS_OS2.# def
55700 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
55710 5f 4f 53 32 0a 23 20 20 65 6c 73 65 0a 23 20 20 _OS2.# else.#
55720 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
55730 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 20 65 6e MUTEX_NOOP.# en
55740 64 69 66 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 dif.#endif..#ifd
55750 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ef SQLITE_MUTEX_
55760 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 OMIT./*.** If th
55770 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6d is is a no-op im
55780 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 69 6d plementation, im
55790 70 6c 65 6d 65 6e 74 20 65 76 65 72 79 74 68 69 plement everythi
557a0 6e 67 20 61 73 20 6d 61 63 72 6f 73 2e 0a 2a 2f ng as macros..*/
557b0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
557c0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 58 29 20 _mutex_alloc(X)
557d0 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 ((sqlite3_mut
557e0 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 ex*)8).#define s
557f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 qlite3_mutex_fre
55800 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c e(X).#define sql
55810 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
55820 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 (X).#define sqli
55830 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 58 29 te3_mutex_try(X)
55840 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a SQLITE_OK.
55850 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f #define sqlite3_
55860 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 0a 23 mutex_leave(X).#
55870 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d define sqlite3_m
55880 75 74 65 78 5f 68 65 6c 64 28 58 29 20 20 20 20 utex_held(X)
55890 20 31 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 1.#define sqlit
558a0 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
558b0 28 58 29 20 20 31 0a 23 64 65 66 69 6e 65 20 73 (X) 1.#define s
558c0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
558d0 28 58 29 20 20 20 20 20 20 28 28 73 71 6c 69 74 (X) ((sqlit
558e0 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 e3_mutex*)8).#de
558f0 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 fine sqlite3Mute
55900 78 49 6e 69 74 28 29 20 20 20 20 20 20 20 20 53 xInit() S
55910 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 QLITE_OK.#define
55920 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 sqlite3MutexEnd
55930 28 29 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 ().#endif /* def
55940 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
55950 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a 2f 2a 2a 2a _MUTEX) */../***
55960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
55970 6f 66 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a of mutex.h *****
55980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
559b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
559c0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
559d0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
559e0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a **********/.../*
55a00 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 .** Each databas
55a10 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63 e file to be acc
55a20 65 73 73 65 64 20 62 79 20 74 68 65 20 73 79 73 essed by the sys
55a30 74 65 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e tem is an instan
55a40 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ce.** of the fol
55a50 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
55a60 2e 20 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72 . There are nor
55a70 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65 mally two of the
55a80 73 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a se structures.**
55a90 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 in the sqlite.a
55aa0 44 62 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62 Db[] array. aDb
55ab0 5b 30 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20 [0] is the main
55ac0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
55ad0 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20 74 d.** aDb[1] is t
55ae0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
55af0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 65 used to hold te
55b00 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 mporary tables.
55b10 20 41 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 Additional.** d
55b20 61 74 61 62 61 73 65 73 20 6d 61 79 20 62 65 20 atabases may be
55b30 61 74 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 74 72 attached..*/.str
55b40 75 63 74 20 44 62 20 7b 0a 20 20 63 68 61 72 20 uct Db {. char
55b50 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
55b60 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 /* Name of this
55b70 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 74 database */. Bt
55b80 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 ree *pBt;
55b90 20 20 20 2f 2a 20 54 68 65 20 42 2a 54 72 65 65 /* The B*Tree
55ba0 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 structure for t
55bb0 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c his database fil
55bc0 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e e */. u8 inTran
55bd0 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 s; /* 0
55be0 3a 20 6e 6f 74 20 77 72 69 74 61 62 6c 65 2e 20 : not writable.
55bf0 20 31 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 2e 1: Transaction.
55c00 20 20 32 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20 2: Checkpoint
55c10 2a 2f 0a 20 20 75 38 20 73 61 66 65 74 79 5f 6c */. u8 safety_l
55c20 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 48 6f 77 evel; /* How
55c30 20 61 67 67 72 65 73 73 69 76 65 20 61 74 20 73 aggressive at s
55c40 79 6e 63 69 6e 67 20 64 61 74 61 20 74 6f 20 64 yncing data to d
55c50 69 73 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 isk */. void *p
55c60 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Aux;
55c70 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 /* Auxiliary
55c80 64 61 74 61 2e 20 20 55 73 75 61 6c 6c 79 20 4e data. Usually N
55c90 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ULL */. void (*
55ca0 78 46 72 65 65 41 75 78 29 28 76 6f 69 64 2a 29 xFreeAux)(void*)
55cb0 3b 20 20 2f 2a 20 52 6f 75 74 69 6e 65 20 74 6f ; /* Routine to
55cc0 20 66 72 65 65 20 70 41 75 78 20 2a 2f 0a 20 20 free pAux */.
55cd0 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b Schema *pSchema;
55ce0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
55cf0 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 65 to database sche
55d00 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61 ma (possibly sha
55d10 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a red) */.};../*.*
55d20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
55d30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
55d40 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 20 tructure stores
55d50 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d a database schem
55d60 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 a..**.** If ther
55d70 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c e are no virtual
55d80 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 tables configur
55d90 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d ed in this schem
55da0 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 a, the.** Schema
55db0 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 .db variable is
55dc0 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 set to NULL. Aft
55dd0 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 72 er the first vir
55de0 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 tual table.** ha
55df0 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74 s been added, it
55e00 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 is set to point
55e10 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
55e20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 connection .**
55e30 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 used to create t
55e40 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f he connection. O
55e50 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 nce a virtual ta
55e60 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 ble has been.**
55e70 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 68 added to the Sch
55e80 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 6e ema structure an
55e90 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20 d the Schema.db
55ea0 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 variable populat
55eb0 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 ed, .** only tha
55ec0 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 t database conne
55ed0 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 ction may use th
55ee0 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70 e Schema to prep
55ef0 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e are .** statemen
55f00 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 ts..*/.struct Sc
55f10 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 hema {. int sch
55f20 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a ema_cookie; /*
55f30 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 Database schema
55f40 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 version number
55f50 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f for this file */
55f60 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b . Hash tblHash;
55f70 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 /* All t
55f80 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79 ables indexed by
55f90 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 name */. Hash
55fa0 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 20 idxHash;
55fb0 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 /* All (named) i
55fc0 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 62 ndices indexed b
55fd0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 y name */. Hash
55fe0 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 20 trigHash;
55ff0 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73 /* All triggers
56000 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 indexed by name
56010 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 */. Table *pSe
56020 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 qTab; /* Th
56030 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 e sqlite_sequenc
56040 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 e table used by
56050 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f AUTOINCREMENT */
56060 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d 61 . u8 file_forma
56070 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d t; /* Schem
56080 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e a format version
56090 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a for this file *
560a0 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 /. u8 enc;
560b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 /* Text
560c0 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 encoding used b
560d0 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 y this database
560e0 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 */. u16 flags;
560f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
56100 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 gs associated wi
56110 74 68 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a th this schema *
56120 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73 69 /. int cache_si
56130 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ze; /* Numb
56140 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 75 er of pages to u
56150 73 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 se in the cache
56160 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
56170 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
56180 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 BLE. sqlite3 *d
56190 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22 4f b; /* "O
561a0 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f 6e wner" connection
561b0 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61 62 . See comment ab
561c0 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b ove */.#endif.};
561d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 ../*.** These ma
561e0 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 cros can be used
561f0 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f to test, set, o
56200 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 r clear bits in
56210 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 73 the .** Db.flags
56220 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 field..*/.#defi
56230 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 ne DbHasProperty
56240 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28 44 (D,I,P) (((D
56250 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d )->aDb[I].pSchem
56260 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 a->flags&(P))==(
56270 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48 61 P)).#define DbHa
56280 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c 49 sAnyProperty(D,I
56290 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62 5b ,P) (((D)->aDb[
562a0 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 I].pSchema->flag
562b0 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 s&(P))!=0).#defi
562c0 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 ne DbSetProperty
562d0 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29 2d (D,I,P) (D)-
562e0 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d >aDb[I].pSchema-
562f0 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 >flags|=(P).#def
56300 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70 65 ine DbClearPrope
56310 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44 29 rty(D,I,P) (D)
56320 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 ->aDb[I].pSchema
56330 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f ->flags&=~(P)../
56340 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
56350 75 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e 66 ues for the DB.f
56360 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a lags field..**.*
56370 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61 4c * The DB_SchemaL
56380 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20 73 65 oaded flag is se
56390 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 t after the data
563a0 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 base schema has
563b0 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e 74 been.** read int
563c0 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 o internal hash
563d0 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 42 tables..**.** DB
563e0 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d 65 _UnresetViews me
563f0 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 ans that one or
56400 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76 65 20 more views have
56410 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61 column names tha
56420 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 66 t.** have been f
56430 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 74 illed out. If t
56440 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 he schema change
56450 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20 s, these column
56460 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20 63 names might.** c
56470 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 68 hanges and so th
56480 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 64 e view will need
56490 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a 2f to be reset..*/
564a0 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68 65 .#define DB_Sche
564b0 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30 30 maLoaded 0x00
564c0 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 01 /* The schem
564d0 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 a has been loade
564e0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f d */.#define DB_
564f0 55 6e 72 65 73 65 74 56 69 65 77 73 20 20 20 20 UnresetViews
56500 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65 20 0x0002 /* Some
56510 76 69 65 77 73 20 68 61 76 65 20 64 65 66 69 6e views have defin
56520 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 ed column names
56530 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45 6d */.#define DB_Em
56540 70 74 79 20 20 20 20 20 20 20 20 20 20 20 30 78 pty 0x
56550 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 0004 /* The fil
56560 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e 67 e is empty (leng
56570 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a 0a th 0 bytes) */..
56580 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 /*.** The number
56590 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b 69 of different ki
565a0 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74 68 nds of things th
565b0 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 65 at can be limite
565c0 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 d.** using the s
565d0 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20 69 qlite3_limit() i
565e0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 nterface..*/.#de
565f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c 49 fine SQLITE_N_LI
56600 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d 49 MIT (SQLITE_LIMI
56610 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
56620 52 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b R+1)../*.** Look
56630 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 aside malloc is
56640 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73 a set of fixed-s
56650 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74 ize buffers that
56660 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 can be used.**
56670 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c to satisfy small
56680 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 transient memor
56690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
566a0 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 uests for object
566b0 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 s.** associated
566c0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
566d0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
566e0 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 ction. The use
566f0 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 of.** lookaside
56700 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 malloc provides
56710 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65 a significant pe
56720 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 rformance enhanc
56730 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 ement.** (approx
56740 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e 10%) by avoidin
56750 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f g numerous mallo
56760 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20 c/free requests
56770 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a while parsing.**
56780 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e SQL statements.
56790 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 .**.** The Looka
567a0 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68 side structure h
567b0 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69 olds configurati
567c0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 on information a
567d0 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b bout the.** look
567e0 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 aside malloc sub
567f0 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76 system. Each av
56800 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 ailable memory a
56810 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 llocation in.**
56820 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 the lookaside su
56830 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65 bsystem is store
56840 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d on a linked li
56850 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 st of LookasideS
56860 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a lot.** objects..
56870 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 **.** Lookaside
56880 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 allocations are
56890 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72 only allowed for
568a0 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 objects that ar
568b0 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 e associated.**
568c0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
568d0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
568e0 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73 ction. Hence, s
568f0 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f chema informatio
56900 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73 n cannot.** be s
56910 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69 tored in lookasi
56920 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68 de because in sh
56930 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20 ared cache mode
56940 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 the schema infor
56950 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61 mation.** is sha
56960 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 red by multiple
56970 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
56980 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65 ions. Therefore
56990 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a , while parsing.
569a0 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d ** schema inform
569b0 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61 ation, the Looka
569c0 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c side.bEnabled fl
569d0 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f ag is cleared so
569e0 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 that.** lookasi
569f0 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 de allocations a
56a00 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63 re not used to c
56a10 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68 onstruct the sch
56a20 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a ema objects..*/.
56a30 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 struct Lookaside
56a40 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20 {. u16 sz;
56a50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
56a60 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66 Size of each buf
56a70 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
56a80 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 u8 bEnabled;
56a90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c /* Fal
56aa0 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65 se to disable ne
56ab0 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f w lookaside allo
56ac0 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20 cations */. u8
56ad0 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 bMalloced;
56ae0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
56af0 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20 pStart obtained
56b00 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
56b10 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e loc() */. int n
56b20 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Out;
56b30 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
56b40 62 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c buffers currentl
56b50 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f y checked out */
56b60 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 . int mxOut;
56b70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69 /* Hi
56b80 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 ghwater mark for
56b90 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 nOut */. Looka
56ba0 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b sideSlot *pFree;
56bb0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 /* List of av
56bc0 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72 73 20 ailable buffers
56bd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 */. void *pStar
56be0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
56bf0 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 76 First byte of av
56c00 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 ailable memory s
56c10 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a pace */. void *
56c20 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 pEnd;
56c30 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
56c40 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76 61 69 past end of avai
56c50 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d lable space */.}
56c60 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 ;.struct Lookasi
56c70 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 deSlot {. Looka
56c80 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b sideSlot *pNext;
56c90 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 /* Next buff
56ca0 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 6f er in the list o
56cb0 66 20 66 72 65 65 20 62 75 66 66 65 72 73 20 2a f free buffers *
56cc0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 /.};../*.** A ha
56cd0 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e sh table for fun
56ce0 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e ction definition
56cf0 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 s..**.** Hash ea
56d00 63 68 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 ch FuncDef struc
56d10 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 ture into one of
56d20 20 74 68 65 20 46 75 6e 63 44 65 66 48 61 73 68 the FuncDefHash
56d30 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 .a[] slots..** C
56d40 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e ollisions are on
56d50 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70 48 61 the FuncDef.pHa
56d60 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 sh chain..*/.str
56d70 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20 uct FuncDefHash
56d80 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 {. FuncDef *a[2
56d90 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 61 73 3]; /* Has
56da0 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 h table for func
56db0 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a tions */.};../*.
56dc0 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 ** Each database
56dd0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
56de0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
56df0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
56e00 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 * The sqlite.las
56e10 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20 74 tRowid records t
56e20 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 he last insert r
56e30 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64 20 62 owid generated b
56e40 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 y an.** insert s
56e50 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 tatement. Inser
56e60 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e ts on views do n
56e70 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 76 61 ot affect its va
56e80 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 lue. Each.** tr
56e90 69 67 67 65 72 20 68 61 73 20 69 74 73 20 6f 77 igger has its ow
56ea0 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 n context, so th
56eb0 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61 6e at lastRowid can
56ec0 20 62 65 20 75 70 64 61 74 65 64 20 69 6e 73 69 be updated insi
56ed0 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61 de.** triggers a
56ee0 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20 70 72 s usual. The pr
56ef0 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77 69 6c evious value wil
56f00 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 6f 6e l be restored on
56f10 63 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a ce the trigger.*
56f20 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 * exits. Upon e
56f30 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f 72 65 ntering a before
56f40 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 20 74 or instead of t
56f50 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 rigger, lastRowi
56f60 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 d is no.** longe
56f70 72 20 28 73 69 6e 63 65 20 61 66 74 65 72 20 76 r (since after v
56f80 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 ersion 2.8.12) r
56f90 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a eset to -1..**.*
56fa0 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 * The sqlite.nCh
56fb0 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f ange does not co
56fc0 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69 74 68 unt changes with
56fd0 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 in triggers and
56fe0 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 keeps no.** cont
56ff0 65 78 74 2e 20 20 49 74 20 69 73 20 72 65 73 65 ext. It is rese
57000 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 73 71 t at start of sq
57010 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 lite3_exec..** T
57020 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e he sqlite.lsChan
57030 67 65 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 ge represents th
57040 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e e number of chan
57050 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 ges made by the
57060 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 last.** insert,
57070 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 update, or delet
57080 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 e statement. It
57090 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e remains constan
570a0 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 t throughout the
570b0 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 .** length of a
570c0 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 statement and is
570d0 20 74 68 65 6e 20 75 70 64 61 74 65 64 20 62 79 then updated by
570e0 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 OP_SetCounts.
570f0 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f It keeps a.** co
57100 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75 73 74 ntext stack just
57110 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 like lastRowid
57120 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 75 6e so that the coun
57130 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 t of changes.**
57140 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 within a trigger
57150 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 is not seen out
57160 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72 side the trigger
57170 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20 76 69 . Changes to vi
57180 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 ews do not.** af
57190 66 65 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f fect the value o
571a0 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 f lsChange..** T
571b0 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68 61 6e he sqlite.csChan
571c0 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f ge keeps track o
571d0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
571e0 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65 73 20 current changes
571f0 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 (since.** the la
57200 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e st statement) an
57210 64 20 69 73 20 75 73 65 64 20 74 6f 20 75 70 64 d is used to upd
57220 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 ate sqlite_lsCha
57230 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d nge..**.** The m
57240 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20 ember variables
57250 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 sqlite.errCode,
57260 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 sqlite.zErrMsg a
57270 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 nd sqlite.zErrMs
57280 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 g16.** store the
57290 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 most recent err
572a0 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 or code and, if
572b0 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 applicable, stri
572c0 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 ng. The.** inter
572d0 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c nal function sql
572e0 69 74 65 33 45 72 72 6f 72 28 29 20 69 73 20 75 ite3Error() is u
572f0 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 73 65 sed to set these
57300 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f variables.** co
57310 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 nsistently..*/.s
57320 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 7b 0a truct sqlite3 {.
57330 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
57340 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 Vfs;
57350 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 /* OS Interface
57360 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 */. int nDb;
57370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57380 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
57390 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 backends current
573a0 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 ly in use */. D
573b0 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20 20 20 b *aDb;
573c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
573d0 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a All backends */.
573e0 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
573f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57400 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73 /* Miscellaneous
57410 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f flags. See belo
57420 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 w */. int openF
57430 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
57440 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 /* Flags pa
57450 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f ssed to sqlite3_
57460 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 vfs.xOpen() */.
57470 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 int errCode;
57480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
57490 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
574a0 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45 ror code (SQLITE
574b0 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 _*) */. int err
574c0 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 Mask;
574d0 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75 /* & resu
574e0 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 74 68 lt codes with th
574f0 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e is before return
57500 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f ing */. u8 auto
57510 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20 Commit;
57520 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75 /* The au
57530 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 to-commit flag.
57540 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f */. u8 temp_sto
57550 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 re;
57560 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a /* 1: file 2:
57570 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75 memory 0: defau
57580 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f lt */. u8 mallo
57590 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20 cFailed;
575a0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
575b0 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20 we have seen a
575c0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a malloc failure *
575d0 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d /. u8 dfltLockM
575e0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ode;
575f0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63 /* Default loc
57600 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74 king-mode for at
57610 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 tached dbs */.
57620 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f u8 dfltJournalMo
57630 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a de; /*
57640 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c Default journal
57650 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 mode for attach
57660 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e ed dbs */. sign
57670 65 64 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f ed char nextAuto
57680 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 vac; /* Aut
57690 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61 66 74 ovac setting aft
576a0 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 er VACUUM if >=0
576b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 50 61 */. int nextPa
576c0 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 gesize;
576d0 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a 65 20 /* Pagesize
576e0 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 after VACUUM if
576f0 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 >0 */. int nTab
57700 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
57710 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
57720 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 of tables in the
57730 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 43 database */. C
57740 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c ollSeq *pDfltCol
57750 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 l; /*
57760 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c The default coll
57770 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 28 ating sequence (
57780 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 36 34 BINARY) */. i64
57790 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 lastRowid;
577a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f /* RO
577b0 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 63 65 WID of most rece
577c0 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65 20 61 nt insert (see a
577d0 62 6f 76 65 29 20 2a 2f 0a 20 20 69 36 34 20 70 bove) */. i64 p
577e0 72 69 6f 72 4e 65 77 52 6f 77 69 64 3b 20 20 20 riorNewRowid;
577f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 /* Last
57800 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65 72 61 randomly genera
57810 74 65 64 20 52 4f 57 49 44 20 2a 2f 0a 20 20 75 ted ROWID */. u
57820 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 32 magic;
57830 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
57840 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 Magic number for
57850 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 79 20 detect library
57860 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 misuse */. int
57870 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 nChange;
57880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
57890 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
578a0 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 qlite3_changes()
578b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c */. int nTotal
578c0 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 Change;
578d0 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 /* Value ret
578e0 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
578f0 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
57900 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
57910 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 tex *mutex;
57920 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f /* Connectio
57930 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 n mutex */. int
57940 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4e aLimit[SQLITE_N
57950 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 _LIMIT]; /* Li
57960 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 mits */. struct
57970 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 6f sqlite3InitInfo
57980 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 { /* Infor
57990 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 72 69 mation used duri
579a0 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ng initializatio
579b0 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 n */. int iDb
579c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
579d0 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 63 /* When bac
579e0 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 74 69 k is being initi
579f0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e alized */. in
57a00 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 20 t newTnum;
57a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f /* Roo
57a20 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 62 tpage of table b
57a30 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 eing initialized
57a40 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 79 3b */. u8 busy;
57a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57a60 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 63 /* TRUE if c
57a70 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 6c urrently initial
57a80 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20 69 6e 69 izing */. } ini
57a90 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 t;. int nExtens
57aa0 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 ion;
57ab0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
57ac0 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e loaded extension
57ad0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 s */. void **aE
57ae0 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 xtension;
57af0 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 /* Array of
57b00 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 shared library
57b10 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 handles */. str
57b20 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b uct Vdbe *pVdbe;
57b30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 /* Li
57b40 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72 st of active vir
57b50 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f tual machines */
57b60 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62 . int activeVdb
57b70 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 eCnt;
57b80 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 /* Number of VD
57b90 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78 BEs currently ex
57ba0 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 ecuting */. int
57bb0 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20 writeVdbeCnt;
57bc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
57bd0 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56 mber of active V
57be0 44 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72 DBEs that are wr
57bf0 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 iting */. void
57c00 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c (*xTrace)(void*,
57c10 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20 const char*);
57c20 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75 /* Trace fu
57c30 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
57c40 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 *pTraceArg;
57c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57c60 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
57c70 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 t to the trace f
57c80 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
57c90 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f d (*xProfile)(vo
57ca0 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c id*,const char*,
57cb0 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c u64); /* Profil
57cc0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ing function */.
57cd0 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 void *pProfile
57ce0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
57cf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
57d00 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 rgument to profi
57d10 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 le function */.
57d20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 void *pCommitAr
57d30 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
57d40 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
57d50 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 o xCommitCallbac
57d60 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 k() */ . int
57d70 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 (*xCommitCallbac
57d80 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a k)(void*); /*
57d90 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 Invoked at ever
57da0 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 y commit. */. v
57db0 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 oid *pRollbackAr
57dc0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
57dd0 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 /* Argument to
57de0 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 xRollbackCallbac
57df0 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 k() */ . void
57e00 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c (*xRollbackCall
57e10 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a back)(void*); /*
57e20 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 Invoked at ever
57e30 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 y commit. */. v
57e40 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b oid *pUpdateArg;
57e50 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74 . void (*xUpdat
57e60 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a eCallback)(void*
57e70 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 ,int, const char
57e80 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 *,const char*,sq
57e90 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 lite_int64);. v
57ea0 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 oid(*xCollNeeded
57eb0 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a )(void*,sqlite3*
57ec0 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f ,int eTextRep,co
57ed0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f nst char*);. vo
57ee0 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 id(*xCollNeeded1
57ef0 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 6)(void*,sqlite3
57f00 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 *,int eTextRep,c
57f10 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 onst void*);. v
57f20 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 oid *pCollNeeded
57f30 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 Arg;. sqlite3_v
57f40 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20 alue *pErr;
57f50 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
57f60 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
57f70 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 e */. char *zEr
57f80 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 rMsg;
57f90 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
57fa0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
57fb0 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 e (UTF-8 encoded
57fc0 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 ) */. char *zEr
57fd0 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20 rMsg16;
57fe0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 /* Most rec
57ff0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 ent error messag
58000 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 e (UTF-16 encode
58010 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a d) */. union {.
58020 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 volatile int
58030 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20 isInterrupted;
58040 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74 /* True if sqlit
58050 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73 e3_interrupt has
58060 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a been called */.
58070 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 double notUs
58080 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 ed1;
58090 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d /* Spacer */. }
580a0 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 u1;. Lookaside
580b0 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 lookaside;
580c0 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 /* Lookasid
580d0 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 e malloc configu
580e0 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 ration */.#ifnde
580f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
58100 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e THORIZATION. in
58110 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a t (*xAuth)(void*
58120 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
58130 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
58140 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
58150 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 har*);.
58160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58170 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73 /* Access
58180 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 authorization f
58190 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
581a0 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20 d *pAuthArg;
581b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 /* 1s
581c0 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
581d0 65 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75 e access auth fu
581e0 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 nction */.#endif
581f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
58200 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 OMIT_PROGRESS_CA
58210 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 LLBACK. int (*x
58220 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a Progress)(void *
58230 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 ); /* The pr
58240 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 ogress callback
58250 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 */. void *pProg
58260 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20 ressArg;
58270 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
58280 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 o the progress c
58290 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 allback */. int
582a0 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 nProgressOps;
582b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
582c0 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 mber of opcodes
582d0 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c for progress cal
582e0 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a lback */.#endif.
582f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
58300 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
58310 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b . Hash aModule;
58320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58330 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79 /* populated by
58340 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
58350 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 module() */. Ta
58360 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20 ble *pVTab;
58370 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 /* v
58380 74 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20 tab with active
58390 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d Connect/Create m
583a0 65 74 68 6f 64 20 2a 2f 0a 20 20 73 71 6c 69 74 ethod */. sqlit
583b0 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61 6e e3_vtab **aVTran
583c0 73 3b 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 s; /* Virt
583d0 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20 ual tables with
583e0 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e open transaction
583f0 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 s */. int nVTra
58400 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ns;
58410 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 /* Allocate
58420 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e d size of aVTran
58430 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 46 75 s */.#endif. Fu
58440 6e 63 44 65 66 48 61 73 68 20 61 46 75 6e 63 3b ncDefHash aFunc;
58450 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
58460 61 73 68 20 74 61 62 6c 65 20 6f 66 20 63 6f 6e ash table of con
58470 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e nection function
58480 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 43 6f 6c s */. Hash aCol
58490 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 lSeq;
584a0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f 6c 6c /* All coll
584b0 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 ating sequences
584c0 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c 65 72 */. BusyHandler
584d0 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20 20 20 busyHandler;
584e0 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c 6c 62 /* Busy callb
584f0 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 62 75 73 ack */. int bus
58500 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20 20 20 yTimeout;
58510 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 68 /* Busy h
58520 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74 2c 20 andler timeout,
58530 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 44 62 20 in msec */. Db
58540 61 44 62 53 74 61 74 69 63 5b 32 5d 3b 20 20 20 aDbStatic[2];
58550 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
58560 61 74 69 63 20 73 70 61 63 65 20 66 6f 72 20 74 atic space for t
58570 68 65 20 32 20 64 65 66 61 75 6c 74 20 62 61 63 he 2 default bac
58580 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 kends */.#ifdef
58590 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 73 71 6c SQLITE_SSE. sql
585a0 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 65 74 63 ite3_stmt *pFetc
585b0 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 h; /* Us
585c0 65 64 20 62 79 20 53 53 45 20 74 6f 20 66 65 74 ed by SSE to fet
585d0 63 68 20 73 74 6f 72 65 64 20 73 74 61 74 65 6d ch stored statem
585e0 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 ents */.#endif.
585f0 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 Savepoint *pSav
58600 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f epoint; /
58610 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 * List of active
58620 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 savepoints */.
58630 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b int nSavepoint;
58640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
58650 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d * Number of non-
58660 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 transaction save
58670 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 points */. int
58680 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 nStatement;
58690 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
586a0 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73 74 ber of nested st
586b0 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 atement-transact
586c0 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69 73 ions */. u8 is
586d0 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
586e0 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75 65 oint; /* True
586f0 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 if the outermos
58700 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 t savepoint is a
58710 20 54 53 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 TS */..#ifdef S
58720 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c QLITE_ENABLE_UNL
58730 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f 2a 20 OCK_NOTIFY. /*
58740 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 The following va
58750 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c 6c 20 riables are all
58760 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 protected by the
58770 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 0a STATIC_MASTER .
58780 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f 74 20 ** mutex, not
58790 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 by sqlite3.mutex
587a0 2e 20 54 68 65 79 20 61 72 65 20 75 73 65 64 20 . They are used
587b0 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 by code in notif
587c0 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 y.c. . **. **
587d0 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f When X.pUnlockCo
587e0 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 nnection==Y, tha
587f0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58 20 69 t means that X i
58800 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20 59 20 s waiting for Y
58810 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73 to. ** unlock s
58820 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 70 72 o that it can pr
58830 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a oceed.. **. **
58840 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e When X.pBlockin
58850 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 gConnection==Y,
58860 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
58870 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 58 something that X
58880 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72 69 65 tried. ** trie
58890 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74 6c 79 d to do recently
588a0 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20 failed with an
588b0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 SQLITE_LOCKED er
588c0 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63 6b 73 ror due to locks
588d0 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 59 2e . ** held by Y.
588e0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 . */. sqlite3
588f0 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 *pBlockingConnec
58900 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74 tion; /* Connect
58910 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 64 20 ion that caused
58920 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f SQLITE_LOCKED */
58930 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55 6e 6c . sqlite3 *pUnl
58940 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20 ockConnection;
58950 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e /* Conn
58960 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63 68 20 ection to watch
58970 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 for unlock */.
58980 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 72 67 void *pUnlockArg
58990 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
589a0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
589b0 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 t to xUnlockNoti
589c0 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 fy */. void (*x
589d0 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 76 6f UnlockNotify)(vo
589e0 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a id **, int); /*
589f0 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 20 63 Unlock notify c
58a00 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c allback */. sql
58a10 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f 63 6b ite3 *pNextBlock
58a20 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 ed; /* Ne
58a30 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c xt in list of al
58a40 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 l blocked connec
58a50 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a tions */.#endif.
58a60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 };../*.** A macr
58a70 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 o to discover th
58a80 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 e encoding of a
58a90 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 database..*/.#de
58aa0 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 28 64 fine ENC(db) ((d
58ab0 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 b)->aDb[0].pSche
58ac0 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 ma->enc)../*.**
58ad0 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 Possible values
58ae0 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 66 for the sqlite.f
58af0 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62 2e 66 lags and or Db.f
58b00 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a lags fields..**.
58b10 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66 6c 61 ** On sqlite.fla
58b20 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 49 gs, the SQLITE_I
58b30 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d 65 61 nTrans value mea
58b40 6e 73 20 74 68 61 74 20 77 65 20 68 61 76 65 0a ns that we have.
58b50 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20 42 45 ** executed a BE
58b60 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c 61 67 GIN. On Db.flag
58b70 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e s, SQLITE_InTran
58b80 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74 65 6d s means a statem
58b90 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ent.** transacti
58ba0 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f 6e 20 on is active on
58bb0 74 68 61 74 20 70 61 72 74 69 63 75 6c 61 72 20 that particular
58bc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
58bd0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
58be0 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 20 _VdbeTrace
58bf0 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 54 0x00000001 /* T
58c00 72 75 65 20 74 6f 20 74 72 61 63 65 20 56 44 42 rue to trace VDB
58c10 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 E execution */.#
58c20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e define SQLITE_In
58c30 54 72 61 6e 73 20 20 20 20 20 20 20 20 30 78 30 Trans 0x0
58c40 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72 75 65 0000008 /* True
58c50 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73 61 63 if in a transac
58c60 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tion */.#define
58c70 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
58c80 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 31 30 nges 0x00000010
58c90 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 64 /* Uncommitted
58ca0 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 61 6e Hash table chan
58cb0 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ges */.#define S
58cc0 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d QLITE_FullColNam
58cd0 65 73 20 20 20 30 78 30 30 30 30 30 30 32 30 20 es 0x00000020
58ce0 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 6f /* Show full co
58cf0 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 45 lumn names on SE
58d00 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20 LECT */.#define
58d10 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e SQLITE_ShortColN
58d20 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 34 30 ames 0x00000040
58d30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74 20 /* Show short
58d40 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f columns names */
58d50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
58d60 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 20 30 CountRows 0
58d70 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 43 6f x00000080 /* Co
58d80 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 65 64 unt rows changed
58d90 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 by INSERT, */.
58da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 44 45 /* DE
58dd0 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20 LETE, or UPDATE
58de0 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 and return */.
58df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58e10 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 68 65 /* the
58e20 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 20 63 count using a c
58e30 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 allback. */.#def
58e40 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 ine SQLITE_NullC
58e50 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 30 30 allback 0x0000
58e60 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 0100 /* Invoke
58e70 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 the callback onc
58e80 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 20 20 e if the */.
58e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58eb0 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 75 6c /* resul
58ec0 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 20 2a t set is empty *
58ed0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
58ee0 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20 20 _SqlTrace
58ef0 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 44 0x00000200 /* D
58f00 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c 20 61 ebug print SQL a
58f10 73 20 69 74 20 65 78 65 63 75 74 65 73 20 2a 2f s it executes */
58f20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
58f30 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 20 30 VdbeListing 0
58f40 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 44 65 x00000400 /* De
58f50 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f 66 20 bug listings of
58f60 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 2a 2f VDBE programs */
58f70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
58f80 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 20 30 WriteSchema 0
58f90 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 4f 4b x00000800 /* OK
58fa0 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c 49 54 to update SQLIT
58fb0 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64 65 66 E_MASTER */.#def
58fc0 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 ine SQLITE_NoRea
58fd0 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30 30 30 dlock 0x0000
58fe0 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c 6f 63 1000 /* Readloc
58ff0 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77 ks are omitted w
59000 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 hen .
59010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
59030 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65 61 64 * accessing read
59040 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73 20 -only databases
59050 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
59060 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 E_IgnoreChecks
59070 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 0x00002000 /*
59080 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63 Do not enforce c
59090 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 heck constraints
590a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
590b0 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 TE_ReadUncommitt
590c0 65 64 20 30 78 30 30 30 30 34 30 30 30 20 2f 2a ed 0x00004000 /*
590d0 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 For shared-cach
590e0 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e e mode */.#defin
590f0 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 e SQLITE_LegacyF
59100 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30 38 30 ileFmt 0x000080
59110 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 00 /* Create ne
59120 77 20 64 61 74 61 62 61 73 65 73 20 69 6e 20 66 w databases in f
59130 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69 ormat 1 */.#defi
59140 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 ne SQLITE_FullFS
59150 79 6e 63 20 20 20 20 20 20 30 78 30 30 30 31 30 ync 0x00010
59160 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 000 /* Use full
59170 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61 fsync on the ba
59180 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 ckend */.#define
59190 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 SQLITE_LoadExte
591a0 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30 30 30 nsion 0x0002000
591b0 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 0 /* Enable loa
591c0 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a d_extension */..
591d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
591e0 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20 30 78 ecoveryMode 0x
591f0 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49 67 6e 00040000 /* Ign
59200 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72 6f 72 ore schema error
59210 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
59220 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 65 20 ITE_SharedCache
59230 20 20 20 30 78 30 30 30 38 30 30 30 30 20 20 2f 0x00080000 /
59240 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 * Cache sharing
59250 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 64 is enabled */.#d
59260 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 6d efine SQLITE_Com
59270 6d 69 74 42 75 73 79 20 20 20 20 20 30 78 30 30 mitBusy 0x00
59280 32 30 30 30 30 30 20 20 2f 2a 20 49 6e 20 74 68 200000 /* In th
59290 65 20 70 72 6f 63 65 73 73 20 6f 66 20 63 6f 6d e process of com
592a0 6d 69 74 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 mitting */.#defi
592b0 6e 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 ne SQLITE_Revers
592c0 65 4f 72 64 65 72 20 20 20 30 78 30 30 34 30 30 eOrder 0x00400
592d0 30 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20 000 /* Reverse
592e0 75 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54 unordered SELECT
592f0 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 s */../*.** Poss
59300 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 ible values for
59310 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 the sqlite.magic
59320 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 20 6e field..** The n
59330 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69 umbers are obtai
59340 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e ned at random an
59350 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 69 61 d have no specia
59360 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 65 72 l meaning, other
59370 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 20 64 .** than being d
59380 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f 6e 65 istinct from one
59390 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 another..*/.#de
593a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 fine SQLITE_MAGI
593b0 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 30 32 C_OPEN 0xa02
593c0 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 62 61 9a697 /* Databa
593d0 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 23 64 se is open */.#d
593e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 efine SQLITE_MAG
593f0 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 39 66 IC_CLOSED 0x9f
59400 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 61 62 3c2d33 /* Datab
59410 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 2a 2f ase is closed */
59420 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
59430 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 20 30 MAGIC_SICK 0
59440 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 45 72 x4b771290 /* Er
59450 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 6e 67 ror and awaiting
59460 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 69 6e close */.#defin
59470 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 e SQLITE_MAGIC_B
59480 55 53 59 20 20 20 20 20 30 78 66 30 33 62 37 39 USY 0xf03b79
59490 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 06 /* Database
594a0 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 currently in use
594b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
594c0 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 20 20 TE_MAGIC_ERROR
594d0 20 20 30 78 62 35 33 35 37 39 33 30 20 20 2f 2a 0xb5357930 /*
594e0 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 An SQLITE_MISUS
594f0 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 E error occurred
59500 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 */../*.** Each
59510 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 SQL function is
59520 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e defined by an in
59530 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
59540 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 llowing.** struc
59550 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 65 72 ture. A pointer
59560 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
59570 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 re is stored in
59580 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 6e 63 the sqlite.aFunc
59590 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 .** hash table.
595a0 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 66 When multiple f
595b0 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 74 68 unctions have th
595c0 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 68 65 e same name, the
595d0 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 70 hash table.** p
595e0 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65 oints to a linke
595f0 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20 d list of these
59600 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 structures..*/.s
59610 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 7b 0a truct FuncDef {.
59620 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 20 20 i16 nArg;
59630 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
59640 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 of arguments.
59650 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d 69 74 -1 means unlimit
59660 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 65 66 ed */. u8 iPref
59670 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Enc; /*
59680 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 Preferred text e
59690 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 45 5f ncoding (SQLITE_
596a0 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 42 45 UTF8, 16LE, 16BE
596b0 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 3b ) */. u8 flags;
596c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
596d0 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 ome combination
596e0 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 2a of SQLITE_FUNC_*
596f0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 */. void *pUse
59700 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 55 73 rData; /* Us
59710 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 74 65 er data paramete
59720 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a r */. FuncDef *
59730 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e pNext; /* N
59740 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 ext function wit
59750 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 h same name */.
59760 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 void (*xFunc)(s
59770 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
59780 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
59790 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c 61 72 e**); /* Regular
597a0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
597b0 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c oid (*xStep)(sql
597c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
597d0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
597e0 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 74 65 *); /* Aggregate
597f0 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 64 20 step */. void
59800 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c (*xFinalize)(sql
59810 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 20 ite3_context*);
59820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
59830 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 6e 61 * Aggregate fina
59840 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 lizer */. char
59850 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
59860 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66 20 74 /* SQL name of t
59870 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a he function. */.
59880 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61 73 68 FuncDef *pHash
59890 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 77 ; /* Next w
598a0 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 ith a different
598b0 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73 61 6d name but the sam
598c0 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a e hash */.};../*
598d0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c .** Possible val
598e0 75 65 73 20 66 6f 72 20 46 75 6e 63 44 65 66 2e ues for FuncDef.
598f0 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 flags.*/.#define
59900 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b SQLITE_FUNC_LIK
59910 45 20 20 20 20 20 30 78 30 31 20 2f 2a 20 43 61 E 0x01 /* Ca
59920 6e 64 69 64 61 74 65 20 66 6f 72 20 74 68 65 20 ndidate for the
59930 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f LIKE optimizatio
59940 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
59950 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 20 20 20 ITE_FUNC_CASE
59960 20 20 30 78 30 32 20 2f 2a 20 43 61 73 65 2d 73 0x02 /* Case-s
59970 65 6e 73 69 74 69 76 65 20 4c 49 4b 45 2d 74 79 ensitive LIKE-ty
59980 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 pe function */.#
59990 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
599a0 4e 43 5f 45 50 48 45 4d 20 20 20 20 30 78 30 34 NC_EPHEM 0x04
599b0 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 2e 20 20 /* Ephemeral.
599c0 44 65 6c 65 74 65 20 77 69 74 68 20 56 44 42 45 Delete with VDBE
599d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
599e0 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
599f0 20 30 78 30 38 20 2f 2a 20 73 71 6c 69 74 65 33 0x08 /* sqlite3
59a00 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 GetFuncCollSeq()
59a10 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 might be called
59a20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
59a30 54 45 5f 46 55 4e 43 5f 50 52 49 56 41 54 45 20 TE_FUNC_PRIVATE
59a40 20 30 78 31 30 20 2f 2a 20 41 6c 6c 6f 77 65 64 0x10 /* Allowed
59a50 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 for internal us
59a60 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e e only */.#defin
59a70 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f e SQLITE_FUNC_CO
59a80 55 4e 54 20 20 20 20 30 78 32 30 20 2f 2a 20 42 UNT 0x20 /* B
59a90 75 69 6c 74 2d 69 6e 20 63 6f 75 6e 74 28 2a 29 uilt-in count(*)
59aa0 20 61 67 67 72 65 67 61 74 65 20 2a 2f 0a 0a 2f aggregate */../
59ab0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
59ac0 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73 2c ng three macros,
59ad0 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49 4b FUNCTION(), LIK
59ae0 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47 52 EFUNC() and AGGR
59af0 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20 75 EGATE() are.** u
59b00 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 sed to create th
59b10 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20 66 e initializers f
59b20 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 or the FuncDef s
59b30 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a tructures..**.**
59b40 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d FUNCTION(zNam
59b50 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 e, nArg, iArg, b
59b60 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20 20 NC, xFunc).**
59b70 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 Used to create
59b80 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 a scalar functi
59b90 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 on definition of
59ba0 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d a function zNam
59bb0 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65 6d e .** implem
59bc0 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63 74 ented by C funct
59bd0 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20 61 ion xFunc that a
59be0 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 ccepts nArg argu
59bf0 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20 20 ments. The.**
59c00 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 value passed a
59c10 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20 74 s iArg is cast t
59c20 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64 20 o a (void*) and
59c30 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a made available.*
59c40 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73 65 * as the use
59c50 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f r-data (sqlite3_
59c60 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f 72 user_data()) for
59c70 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 49 the function. I
59c80 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d 65 f .** argume
59c90 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c 20 nt bNC is true,
59ca0 74 68 65 6e 20 74 68 65 20 46 75 6e 63 44 65 66 then the FuncDef
59cb0 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 20 66 6c 61 .needCollate fla
59cc0 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 g is set..**.**
59cd0 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d AGGREGATE(zNam
59ce0 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 e, nArg, iArg, b
59cf0 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 NC, xStep, xFina
59d00 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 l).** Used t
59d10 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67 72 o create an aggr
59d20 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 64 egate function d
59d30 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 6d efinition implem
59d40 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 ented by.**
59d50 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 the C functions
59d60 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c xStep and xFinal
59d70 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75 72 . The first four
59d80 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 20 parameters.**
59d90 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 are interpret
59da0 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 77 ed in the same w
59db0 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ay as the first
59dc0 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 4 parameters to.
59dd0 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ** FUNCTION(
59de0 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 )..**.** LIKEF
59df0 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c UNC(zName, nArg,
59e00 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a 2a pArg, flags).**
59e10 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 Used to cre
59e20 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e ate a scalar fun
59e30 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e ction definition
59e40 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a of a function z
59e50 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68 61 Name .** tha
59e60 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 61 t accepts nArg a
59e70 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73 20 rguments and is
59e80 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 61 implemented by a
59e90 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 call to C .**
59ea0 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 function like
59eb0 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20 70 Func. Argument p
59ec0 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 Arg is cast to a
59ed0 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d 61 (void *) and ma
59ee0 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c 61 de.** availa
59ef0 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63 74 ble as the funct
59f00 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28 73 ion user-data (s
59f10 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
59f20 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 ()). The.**
59f30 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76 61 FuncDef.flags va
59f40 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f riable is set to
59f50 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 the value passe
59f60 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a 2a d as the flags.*
59f70 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 2e * parameter.
59f80 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e 43 .*/.#define FUNC
59f90 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 TION(zName, nArg
59fa0 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 , iArg, bNC, xFu
59fb0 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 nc) \. {nArg, S
59fc0 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a QLITE_UTF8, bNC*
59fd0 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 8, SQLITE_INT_TO
59fe0 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78 _PTR(iArg), 0, x
59ff0 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 Func, 0, 0, #zNa
5a000 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53 me, 0}.#define S
5a010 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d TR_FUNCTION(zNam
5a020 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62 e, nArg, pArg, b
5a030 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b NC, xFunc) \. {
5a040 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 nArg, SQLITE_UTF
5a050 38 2c 20 62 4e 43 2a 38 2c 20 70 41 72 67 2c 20 8, bNC*8, pArg,
5a060 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 0, xFunc, 0, 0,
5a070 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 #zName, 0}.#defi
5a080 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d ne LIKEFUNC(zNam
5a090 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66 6c e, nArg, arg, fl
5a0a0 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 ags) \. {nArg,
5a0b0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c 61 SQLITE_UTF8, fla
5a0c0 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67 2c gs, (void *)arg,
5a0d0 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 0, likeFunc, 0,
5a0e0 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 0, #zName, 0}.#
5a0f0 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 45 define AGGREGATE
5a100 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 (zName, nArg, ar
5a110 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 g, nc, xStep, xF
5a120 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c inal) \. {nArg,
5a130 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63 SQLITE_UTF8, nc
5a140 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 *8, SQLITE_INT_T
5a150 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30 O_PTR(arg), 0, 0
5a160 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23 , xStep,xFinal,#
5a170 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20 zName,0}../*.**
5a180 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76 65 All current save
5a190 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72 65 points are store
5a1a0 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d in a linked li
5a1b0 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a 2a st starting at.*
5a1c0 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 * sqlite3.pSavep
5a1d0 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 20 oint. The first
5a1e0 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 6c element in the l
5a1f0 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 20 ist is the most
5a200 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65 6e recently.** open
5a210 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 61 ed savepoint. Sa
5a220 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 64 vepoints are add
5a230 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 62 ed to the list b
5a240 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f 50 y the vdbe.** OP
5a250 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 _Savepoint instr
5a260 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 uction..*/.struc
5a270 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 t Savepoint {.
5a280 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
5a290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2a0 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e 74 /* Savepoint
5a2b0 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d 69 name (nul-termi
5a2c0 6e 61 74 65 64 29 20 2a 2f 0a 20 20 53 61 76 65 nated) */. Save
5a2d0 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20 point *pNext;
5a2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2f0 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 6f /* Parent savepo
5a300 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a int (if any) */.
5a310 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f };../*.** The fo
5a320 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 64 llowing are used
5a330 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 as the second p
5a340 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
5a350 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a te3Savepoint(),.
5a360 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 31 ** and as the P1
5a370 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
5a380 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e OP_Savepoint in
5a390 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 struction..*/.#d
5a3a0 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f efine SAVEPOINT_
5a3b0 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 65 BEGIN 0.#de
5a3c0 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 fine SAVEPOINT_R
5a3d0 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 66 ELEASE 1.#def
5a3e0 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f ine SAVEPOINT_RO
5a3f0 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a LLBACK 2.../*.
5a400 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d ** Each SQLite m
5a410 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 odule (virtual t
5a420 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 able definition)
5a430 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 is defined by a
5a440 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 n.** instance of
5a450 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
5a460 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 tructure, stored
5a470 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e in the sqlite3.
5a480 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 aModule.** hash
5a490 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 table..*/.struct
5a4a0 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 Module {. cons
5a4b0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
5a4c0 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 *pModule;
5a4d0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 /* Callback poi
5a4e0 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 nters */. const
5a4f0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
5a500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a510 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 /* Name passed t
5a520 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 o create_module(
5a530 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 ) */. void *pAu
5a540 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
5a550 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
5a560 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 Aux passed to cr
5a570 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f eate_module() */
5a580 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 . void (*xDestr
5a590 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 oy)(void *);
5a5a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c /* Modul
5a5b0 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e e destructor fun
5a5c0 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ction */.};../*.
5a5d0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ** information a
5a5e0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e bout each column
5a5f0 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 of an SQL table
5a600 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 is held in an i
5a610 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 nstance.** of th
5a620 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f is structure..*/
5a630 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b .struct Column {
5a640 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
5a650 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
5a660 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 his column */.
5a670 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 Expr *pDflt;
5a680 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 /* Default valu
5a690 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e e of this column
5a6a0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 */. char *zTyp
5a6b0 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 e; /* Data t
5a6c0 79 70 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c ype for this col
5a6d0 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a umn */. char *z
5a6e0 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c Coll; /* Col
5a6f0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e lating sequence.
5a700 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 If NULL, use t
5a710 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 he default */.
5a720 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 u8 notNull;
5a730 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 /* True if ther
5a740 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 e is a NOT NULL
5a750 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 constraint */.
5a760 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 u8 isPrimKey;
5a770 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
5a780 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 column is part
5a790 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b of the PRIMARY K
5a7a0 45 59 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 EY */. char aff
5a7b0 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 inity; /* One
5a7c0 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 of the SQLITE_AF
5a7d0 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a F_... values */.
5a7e0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
5a7f0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
5a800 0a 20 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20 . u8 isHidden;
5a810 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
5a820 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 his column is 'h
5a830 69 64 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 idden' */.#endif
5a840 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f .};../*.** A "Co
5a850 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65 llating Sequence
5a860 22 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 " is defined by
5a870 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
5a880 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
5a890 73 74 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 structure. Conce
5a8a0 70 74 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 ptually, a colla
5a8b0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f ting sequence co
5a8c0 6e 73 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 nsists of a name
5a8d0 20 61 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 and.** a compar
5a8e0 69 73 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 ison routine tha
5a8f0 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72 t defines the or
5a900 64 65 72 20 6f 66 20 74 68 61 74 20 73 65 71 75 der of that sequ
5a910 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 ence..**.** Ther
5a920 65 20 6d 61 79 20 74 77 6f 20 73 65 70 61 72 61 e may two separa
5a930 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f te implementatio
5a940 6e 73 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 ns of the collat
5a950 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e ion function, on
5a960 65 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 e.** that proces
5a970 73 65 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d ses text in UTF-
5a980 38 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 8 encoding (Coll
5a990 53 65 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e Seq.xCmp) and an
5a9a0 6f 74 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 other that.** pr
5a9b0 6f 63 65 73 73 65 73 20 74 65 78 74 20 65 6e 63 ocesses text enc
5a9c0 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28 oded in UTF-16 (
5a9d0 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c CollSeq.xCmp16),
5a9e0 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69 using the machi
5a9f0 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 ne.** native byt
5aa00 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20 e order. When a
5aa10 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
5aa20 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 ce is invoked, S
5aa30 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a QLite selects.**
5aa40 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61 the version tha
5aa50 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74 t will require t
5aa60 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69 he least expensi
5aa70 76 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 ve encoding.** t
5aa80 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 ranslations, if
5aa90 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 any..**.** The C
5aaa0 6f 6c 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d ollSeq.pUser mem
5aab0 62 65 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 ber variable is
5aac0 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d 65 74 an extra paramet
5aad0 65 72 20 74 68 61 74 20 70 61 73 73 65 64 20 69 er that passed i
5aae0 6e 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 n.** as the firs
5aaf0 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
5ab00 65 20 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 e UTF-8 comparis
5ab10 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d on function, xCm
5ab20 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 p..** CollSeq.pU
5ab30 73 65 72 31 36 20 69 73 20 74 68 65 20 65 71 75 ser16 is the equ
5ab40 69 76 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 ivalent for the
5ab50 55 54 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f UTF-16 compariso
5ab60 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 n function,.** x
5ab70 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 Cmp16..**.** If
5ab80 62 6f 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d both CollSeq.xCm
5ab90 70 20 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 p and CollSeq.xC
5aba0 6d 70 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 mp16 are NULL, i
5abb0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 t means that the
5abc0 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 .** collating se
5abd0 71 75 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69 quence is undefi
5abe0 6e 65 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75 ned. Indices bu
5abf0 69 6c 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 ilt on an undefi
5ac00 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 ned.** collating
5ac10 20 73 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f sequence may no
5ac20 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 t be read or wri
5ac30 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tten..*/.struct
5ac40 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 CollSeq {. char
5ac50 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
5ac60 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
5ac70 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5ac80 6e 63 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 nce, UTF-8 encod
5ac90 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 ed */. u8 enc;
5aca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5acb0 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 Text encoding h
5acc0 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29 andled by xCmp()
5acd0 20 2a 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20 */. u8 type;
5ace0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
5acf0 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 ne of the SQLITE
5ad00 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 _COLL_... values
5ad10 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 below */. void
5ad20 20 2a 70 55 73 65 72 3b 20 20 20 20 20 20 20 20 *pUser;
5ad30 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d /* First argum
5ad40 65 6e 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f ent to xCmp() */
5ad50 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 . int (*xCmp)(v
5ad60 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 oid*,int, const
5ad70 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 void*, int, cons
5ad80 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 t void*);. void
5ad90 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b (*xDel)(void*);
5ada0 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 /* Destructor
5adb0 66 6f 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a for pUser */.};.
5adc0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
5add0 61 6c 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 alues of CollSeq
5ade0 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e .type:.*/.#defin
5adf0 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 e SQLITE_COLL_BI
5ae00 4e 41 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20 NARY 1 /* The
5ae10 64 65 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 default memcmp()
5ae20 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5ae30 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 nce */.#define S
5ae40 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 QLITE_COLL_NOCAS
5ae50 45 20 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69 E 2 /* The bui
5ae60 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c lt-in NOCASE col
5ae70 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
5ae80 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5ae90 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 E_COLL_REVERSE 3
5aea0 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 /* The built-i
5aeb0 6e 20 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 n REVERSE collat
5aec0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ing sequence */.
5aed0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
5aee0 4f 4c 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f OLL_USER 0 /
5aef0 2a 20 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72 * Any other user
5af00 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 -defined collati
5af10 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a ng sequence */..
5af20 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 /*.** A sort ord
5af30 65 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 er can be either
5af40 20 41 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f ASC or DESC..*/
5af50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5af60 53 4f 5f 41 53 43 20 20 20 20 20 20 20 30 20 20 SO_ASC 0
5af70 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e /* Sort in ascen
5af80 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 ding order */.#d
5af90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f efine SQLITE_SO_
5afa0 44 45 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20 DESC 1 /*
5afb0 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e Sort in ascendin
5afc0 67 20 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a g order */../*.*
5afd0 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 * Column affinit
5afe0 79 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 y types..**.** T
5aff0 68 65 73 65 20 75 73 65 64 20 74 6f 20 68 61 76 hese used to hav
5b000 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 e mnemonic name
5b010 6c 69 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c like 'i' for SQL
5b020 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 ITE_AFF_INTEGER
5b030 61 6e 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 and.** 't' for S
5b040 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 QLITE_AFF_TEXT.
5b050 20 42 75 74 20 77 65 20 63 61 6e 20 73 61 76 65 But we can save
5b060 20 61 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20 a little space
5b070 61 6e 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 and improve.** t
5b080 68 65 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c he speed a littl
5b090 65 20 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 e by numbering t
5b0a0 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 he values consec
5b0b0 75 74 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a utively. .**.**
5b0c0 20 42 75 74 20 72 61 74 68 65 72 20 74 68 61 6e But rather than
5b0d0 20 73 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 start with 0 or
5b0e0 20 31 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 1, we begin wit
5b0f0 68 20 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 h 'a'. That way
5b100 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 ,.** when multip
5b110 6c 65 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 le affinity type
5b120 73 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 s are concatenat
5b130 65 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 ed into a string
5b140 20 61 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 and.** used as
5b150 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 the P4 operand,
5b160 74 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 they will be mor
5b170 65 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a e readable..**.*
5b180 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 * Note also that
5b190 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 the numeric typ
5b1a0 65 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 es are grouped t
5b1b0 6f 67 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 ogether so that
5b1c0 74 65 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 testing.** for a
5b1d0 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 numeric type is
5b1e0 20 61 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 a single compar
5b1f0 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ison..*/.#define
5b200 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
5b210 20 20 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 'a'.#define
5b220 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 SQLITE_AFF_NONE
5b230 20 20 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 'b'.#define
5b240 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 SQLITE_AFF_NUME
5b250 52 49 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 RIC 'c'.#define
5b260 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
5b270 47 45 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 GER 'd'.#define
5b280 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
5b290 20 20 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 'e'..#defin
5b2a0 65 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 e sqlite3IsNumer
5b2b0 69 63 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 icAffinity(X) (
5b2c0 28 58 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f (X)>=SQLITE_AFF_
5b2d0 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 NUMERIC)../*.**
5b2e0 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d The SQLITE_AFF_M
5b2f0 41 53 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 ASK values masks
5b300 20 6f 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 off the signifi
5b310 63 61 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a cant bits of an.
5b320 2a 2a 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 ** affinity valu
5b330 65 2e 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 e. .*/.#define S
5b340 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 QLITE_AFF_MASK
5b350 20 20 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 0x67../*.** A
5b360 64 64 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 dditional bit va
5b370 6c 75 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 lues that can be
5b380 20 4f 52 65 64 20 77 69 74 68 20 61 6e 20 61 66 ORed with an af
5b390 66 69 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a finity without.*
5b3a0 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61 * changing the a
5b3b0 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 ffinity..*/.#def
5b3c0 69 6e 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 ine SQLITE_JUMPI
5b3d0 46 4e 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a FNULL 0x08 /*
5b3e0 20 6a 75 6d 70 73 20 69 66 20 65 69 74 68 65 72 jumps if either
5b3f0 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
5b400 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5b410 54 45 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20 TE_STOREP2
5b420 30 78 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 0x10 /* Store r
5b430 65 73 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d esult in reg[P2]
5b440 20 72 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d rather than jum
5b450 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 p */../*.** Each
5b460 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 72 65 SQL table is re
5b470 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d presented in mem
5b480 6f 72 79 20 62 79 20 61 6e 20 69 6e 73 74 61 6e ory by an instan
5b490 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c ce of the.** fol
5b4a0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
5b4b0 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e ..**.** Table.zN
5b4c0 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ame is the name
5b4d0 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 of the table. T
5b4e0 68 65 20 63 61 73 65 20 6f 66 20 74 68 65 20 6f he case of the o
5b4f0 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 riginal.** CREAT
5b500 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
5b510 74 20 69 73 20 73 74 6f 72 65 64 2c 20 62 75 74 t is stored, but
5b520 20 63 61 73 65 20 69 73 20 6e 6f 74 20 73 69 67 case is not sig
5b530 6e 69 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 nificant for.**
5b540 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a comparisons..**.
5b550 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 ** Table.nCol is
5b560 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
5b570 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 olumns in this t
5b580 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f able. Table.aCo
5b590 6c 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 l is a.** pointe
5b5a0 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 r to an array of
5b5b0 20 43 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 Column structur
5b5c0 65 73 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 es, one for each
5b5d0 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 column..**.** I
5b5e0 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 f the table has
5b5f0 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 an INTEGER PRIMA
5b600 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 RY KEY, then Tab
5b610 6c 65 2e 69 50 4b 65 79 20 69 73 20 74 68 65 20 le.iPKey is the
5b620 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 index of.** the
5b630 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 column that is t
5b640 68 61 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 72 hat key. Other
5b650 77 69 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 wise Table.iPKey
5b660 20 69 73 20 6e 65 67 61 74 69 76 65 2e 20 20 4e is negative. N
5b670 6f 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 ote.** that the
5b680 64 61 74 61 74 79 70 65 20 6f 66 20 74 68 65 20 datatype of the
5b690 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 PRIMARY KEY must
5b6a0 20 62 65 20 49 4e 54 45 47 45 52 20 66 6f 72 20 be INTEGER for
5b6b0 74 68 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a this field to.**
5b6c0 20 62 65 20 73 65 74 2e 20 20 41 6e 20 49 4e 54 be set. An INT
5b6d0 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
5b6e0 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 is used as the
5b6f0 72 6f 77 69 64 20 66 6f 72 20 65 61 63 68 20 72 rowid for each r
5b700 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 ow of.** the tab
5b710 6c 65 2e 20 20 49 66 20 61 20 74 61 62 6c 65 20 le. If a table
5b720 68 61 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 has no INTEGER P
5b730 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e RIMARY KEY, then
5b740 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a a random rowid.
5b750 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 ** is generated
5b760 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 for each row of
5b770 74 68 65 20 74 61 62 6c 65 2e 20 20 54 46 5f 48 the table. TF_H
5b780 61 73 50 72 69 6d 61 72 79 4b 65 79 20 69 73 20 asPrimaryKey is
5b790 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 set if.** the ta
5b7a0 62 6c 65 20 68 61 73 20 61 6e 79 20 50 52 49 4d ble has any PRIM
5b7b0 41 52 59 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 ARY KEY, INTEGER
5b7c0 20 6f 72 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a or otherwise..*
5b7d0 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 *.** Table.tnum
5b7e0 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 is the page numb
5b7f0 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 er for the root
5b800 42 54 72 65 65 20 70 61 67 65 20 6f 66 20 74 68 BTree page of th
5b810 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a e table in the.*
5b820 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e * database file.
5b830 20 20 49 66 20 54 61 62 6c 65 2e 69 44 62 20 69 If Table.iDb i
5b840 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
5b850 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c he database tabl
5b860 65 20 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 e backend.** in
5b870 73 71 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 sqlite.aDb[]. 0
5b880 20 69 73 20 66 6f 72 20 74 68 65 20 6d 61 69 6e is for the main
5b890 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 31 20 database and 1
5b8a0 69 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 is for the file
5b8b0 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 that.** holds te
5b8c0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 mporary tables a
5b8d0 6e 64 20 69 6e 64 69 63 65 73 2e 20 20 49 66 20 nd indices. If
5b8e0 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 69 73 20 TF_Ephemeral is
5b8f0 73 65 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 set.** then the
5b900 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 table is stored
5b910 69 6e 20 61 20 66 69 6c 65 20 74 68 61 74 20 69 in a file that i
5b920 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 s automatically
5b930 64 65 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20 deleted.** when
5b940 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 the VDBE cursor
5b950 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 to the table is
5b960 63 6c 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 73 closed. In this
5b970 20 63 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d case Table.tnum
5b980 20 0a 2a 2a 20 72 65 66 65 72 73 20 56 44 42 45 .** refers VDBE
5b990 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 cursor number t
5b9a0 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 74 61 hat holds the ta
5b9b0 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f ble open, not to
5b9c0 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 the root.** pag
5b9d0 65 20 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 e number. Trans
5b9e0 69 65 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20 ient tables are
5b9f0 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 used to hold the
5ba00 20 72 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a results of a.**
5ba10 20 73 75 62 2d 71 75 65 72 79 20 74 68 61 74 20 sub-query that
5ba20 61 70 70 65 61 72 73 20 69 6e 73 74 65 61 64 20 appears instead
5ba30 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 of a real table
5ba40 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d name in the FROM
5ba50 20 63 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61 clause .** of a
5ba60 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
5ba70 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 t..*/.struct Tab
5ba80 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a le {. sqlite3 *
5ba90 64 62 4d 65 6d 3b 20 20 20 20 20 20 2f 2a 20 44 dbMem; /* D
5baa0 42 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 65 B connection use
5bab0 64 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 d for lookaside
5bac0 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 2a 2f 0a allocations. */.
5bad0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
5bae0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
5baf0 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 f the table or v
5bb00 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b iew */. int iPK
5bb10 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ey; /*
5bb20 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 If not negative
5bb30 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 , use aCol[iPKey
5bb40 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79 ] as the primary
5bb50 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 key */. int nC
5bb60 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ol; /
5bb70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
5bb80 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c mns in this tabl
5bb90 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 e */. Column *a
5bba0 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 Col; /* I
5bbb0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
5bbc0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a each column */.
5bbd0 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b Index *pIndex;
5bbe0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
5bbf0 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e f SQL indexes on
5bc00 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a this table. */.
5bc10 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 int tnum;
5bc20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42 /* Root B
5bc30 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68 Tree node for th
5bc40 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f is table (see no
5bc50 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53 te above) */. S
5bc60 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
5bc70 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 /* NULL for
5bc80 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20 tables. Points
5bc90 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 66 to definition if
5bca0 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31 a view. */. u1
5bcb0 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 6 nRef;
5bcc0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5bcd0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 pointers to this
5bce0 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74 Table */. u8 t
5bcf0 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 abFlags;
5bd00 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a /* Mask of TF_*
5bd10 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 values */. u8
5bd20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 20 20 keyConf;
5bd30 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 /* What to do
5bd40 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71 75 in case of uniqu
5bd50 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f eness conflict o
5bd60 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 46 4b 65 n iPKey */. FKe
5bd70 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20 y *pFKey;
5bd80 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 /* Linked list
5bd90 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 of all foreign
5bda0 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74 61 62 keys in this tab
5bdb0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 le */. char *zC
5bdc0 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f 2a 20 olAff; /*
5bdd0 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 String defining
5bde0 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 the affinity of
5bdf0 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 each column */.#
5be00 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
5be10 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70 72 20 IT_CHECK. Expr
5be20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 *pCheck;
5be30 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61 6c /* The AND of al
5be40 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 l CHECK constrai
5be50 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 nts */.#endif.#i
5be60 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
5be70 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 69 T_ALTERTABLE. i
5be80 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b nt addColOffset;
5be90 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
5bea0 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
5beb0 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20 mt to add a new
5bec0 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 column */.#endif
5bed0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5bee0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
5bef0 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 E. Module *pMod
5bf00 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
5bf10 74 65 72 20 74 6f 20 74 68 65 20 69 6d 70 6c 65 ter to the imple
5bf20 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
5bf30 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c module */. sql
5bf40 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
5bf50 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 ; /* Pointer to
5bf60 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74 61 the module insta
5bf70 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f nce */. int nMo
5bf80 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a duleArg; /*
5bf90 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d Number of argum
5bfa0 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75 ents to the modu
5bfb0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 le */. char **a
5bfc0 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20 zModuleArg; /*
5bfd0 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 Text of all modu
5bfe0 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20 le args. [0] is
5bff0 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23 module name */.#
5c000 65 6e 64 69 66 0a 20 20 54 72 69 67 67 65 72 20 endif. Trigger
5c010 2a 70 54 72 69 67 67 65 72 3b 20 20 20 2f 2a 20 *pTrigger; /*
5c020 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 List of triggers
5c030 20 73 74 6f 72 65 64 20 69 6e 20 70 53 63 68 65 stored in pSche
5c040 6d 61 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a ma */. Schema *
5c050 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 pSchema; /*
5c060 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74 Schema that cont
5c070 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c 65 20 ains this table
5c080 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 78 */. Table *pNex
5c090 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78 tZombie; /* Nex
5c0a0 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 2e 70 t on the Parse.p
5c0b0 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 20 2a ZombieTab list *
5c0c0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f /.};../*.** Allo
5c0d0 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 54 wed values for T
5c0e0 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f abe.tabFlags..*/
5c0f0 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65 61 64 .#define TF_Read
5c100 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78 30 31 only 0x01
5c110 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79 /* Read-only
5c120 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f system table */
5c130 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70 68 65 .#define TF_Ephe
5c140 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78 30 32 meral 0x02
5c150 20 20 20 20 2f 2a 20 41 6e 20 65 70 68 65 6d 65 /* An epheme
5c160 72 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 ral table */.#de
5c170 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69 6d 61 fine TF_HasPrima
5c180 72 79 4b 65 79 20 20 20 30 78 30 34 20 20 20 20 ryKey 0x04
5c190 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61 20 70 /* Table has a p
5c1a0 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64 rimary key */.#d
5c1b0 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63 efine TF_Autoinc
5c1c0 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20 20 20 rement 0x08
5c1d0 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72 69 6d /* Integer prim
5c1e0 61 72 79 20 6b 65 79 20 69 73 20 61 75 74 6f 69 ary key is autoi
5c1f0 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 ncrement */.#def
5c200 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c 20 20 ine TF_Virtual
5c210 20 20 20 20 20 20 20 30 78 31 30 20 20 20 20 2f 0x10 /
5c220 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c 20 74 * Is a virtual t
5c230 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 able */.#define
5c240 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74 61 20 TF_NeedMetadata
5c250 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 61 43 0x20 /* aC
5c260 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61 ol[].zType and a
5c270 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 Col[].pColl miss
5c280 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 ing */..../*.**
5c290 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74 Test to see whet
5c2a0 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 61 62 her or not a tab
5c2b0 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 le is a virtual
5c2c0 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a table. This is.
5c2d0 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d 61 63 ** done as a mac
5c2e0 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 ro so that it wi
5c2f0 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 ll be optimized
5c300 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75 61 6c out when virtual
5c310 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70 6f 72 .** table suppor
5c320 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f t is omitted fro
5c330 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a m the build..*/.
5c340 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
5c350 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
5c360 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72 .# define IsVir
5c370 74 75 61 6c 28 58 29 20 20 20 20 20 20 28 28 28 tual(X) (((
5c380 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 X)->tabFlags & T
5c390 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29 0a 23 F_Virtual)!=0).#
5c3a0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65 define IsHidde
5c3b0 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d nColumn(X) ((X)-
5c3c0 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c 73 65 >isHidden).#else
5c3d0 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72 .# define IsVir
5c3e0 74 75 61 6c 28 58 29 20 20 20 20 20 20 30 0a 23 tual(X) 0.#
5c3f0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65 define IsHidde
5c400 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e nColumn(X) 0.#en
5c410 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 dif../*.** Each
5c420 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
5c430 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69 6e 73 traint is an ins
5c440 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
5c450 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
5c460 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67 ..**.** A foreig
5c470 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 69 61 n key is associa
5c480 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 61 62 ted with two tab
5c490 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f 6d 22 les. The "from"
5c4a0 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 table is.** the
5c4b0 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 table that cont
5c4c0 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e ains the REFEREN
5c4d0 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 74 20 CES clause that
5c4e0 63 72 65 61 74 65 73 20 74 68 65 20 66 6f 72 65 creates the fore
5c4f0 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65 ign.** key. The
5c500 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73 20 74 "to" table is t
5c510 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 he table that is
5c520 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 52 45 named in the RE
5c530 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 2e FERENCES clause.
5c540 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 69 .** Consider thi
5c550 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a s example:.**.**
5c560 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
5c570 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20 20 20 E ex1(.**
5c580 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 a INTEGER PRIMAR
5c590 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20 Y KEY,.**
5c5a0 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53 54 52 b INTEGER CONSTR
5c5b0 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52 45 4e AINT fk1 REFEREN
5c5c0 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20 20 20 CES ex2(x).**
5c5d0 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 );.**.** For f
5c5e0 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b 31 22 oreign key "fk1"
5c5f0 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 , the from-table
5c600 20 69 73 20 22 65 78 31 22 20 61 6e 64 20 74 68 is "ex1" and th
5c610 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 22 65 e to-table is "e
5c620 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 x2"..**.** Each
5c630 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 REFERENCES claus
5c640 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69 e generates an i
5c650 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
5c660 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
5c670 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 re.** which is a
5c680 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 66 ttached to the f
5c690 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68 65 20 rom-table. The
5c6a0 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f to-table need no
5c6b0 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20 t exist when.**
5c6c0 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 the from-table i
5c6d0 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 s created. The
5c6e0 65 78 69 73 74 65 6e 63 65 20 6f 66 20 74 68 65 existence of the
5c6f0 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74 to-table is not
5c700 20 63 68 65 63 6b 65 64 2e 0a 2a 2f 0a 73 74 72 checked..*/.str
5c710 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 uct FKey {. Tab
5c720 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f le *pFrom; /
5c730 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 * The table that
5c740 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 contains the RE
5c750 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 FERENCES clause
5c760 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 */. FKey *pNext
5c770 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 From; /* Next f
5c780 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 oreign key in pF
5c790 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a rom */. char *z
5c7a0 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 To; /* Na
5c7b0 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 me of table that
5c7c0 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 the key points
5c7d0 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c to */. int nCol
5c7e0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
5c7f0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
5c800 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 n this key */.
5c810 75 38 20 69 73 44 65 66 65 72 72 65 64 3b 20 20 u8 isDeferred;
5c820 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 6e /* True if con
5c830 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69 6e 67 straint checking
5c840 20 69 73 20 64 65 66 65 72 72 65 64 20 74 69 6c is deferred til
5c850 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 75 38 l COMMIT */. u8
5c860 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20 20 20 updateConf;
5c870 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 /* How to resolv
5c880 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 e conflicts that
5c890 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41 54 45 occur on UPDATE
5c8a0 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74 65 43 */. u8 deleteC
5c8b0 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 onf; /* How t
5c8c0 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 o resolve confli
5c8d0 63 74 73 20 74 68 61 74 20 6f 63 63 75 72 20 6f cts that occur o
5c8e0 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 n DELETE */. u8
5c8f0 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20 20 20 insertConf;
5c900 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 /* How to resolv
5c910 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 e conflicts that
5c920 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45 52 54 occur on INSERT
5c930 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f */. struct sCo
5c940 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 lMap { /* Mappi
5c950 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e ng of columns in
5c960 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e pFrom to column
5c970 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 s in zTo */.
5c980 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 int iFrom;
5c990 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
5c9a0 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a olumn in pFrom *
5c9b0 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c /. char *zCol
5c9c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ; /* Name
5c9d0 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 of column in zT
5c9e0 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50 52 49 o. If 0 use PRI
5c9f0 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 MARY KEY */. }
5ca00 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 aCol[1];
5ca10 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 /* One entry for
5ca20 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f each of nCol co
5ca30 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a lumn s */.};../*
5ca40 0a 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f .** SQLite suppo
5ca50 72 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 rts many differe
5ca60 6e 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c nt ways to resol
5ca70 76 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a ve a constraint.
5ca80 2a 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 ** error. ROLLB
5ca90 41 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d ACK processing m
5caa0 65 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 eans that a cons
5cab0 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
5cac0 0a 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f .** causes the o
5cad0 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 peration in proc
5cae0 65 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 ess to fail and
5caf0 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 for the current
5cb00 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 transaction.** t
5cb10 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b o be rolled back
5cb20 2e 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 . ABORT process
5cb30 69 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 ing means the op
5cb40 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 eration in proce
5cb50 73 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 ss.** fails and
5cb60 61 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 any prior change
5cb70 73 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 s from that one
5cb80 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 operation are ba
5cb90 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 cked out,.** but
5cba0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
5cbb0 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 is not rolled b
5cbc0 61 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 ack. FAIL proce
5cbd0 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 ssing means that
5cbe0 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f .** the operatio
5cbf0 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 n in progress st
5cc00 6f 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ops and returns
5cc10 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 an error code.
5cc20 42 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 But prior.** cha
5cc30 6e 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 nges due to the
5cc40 73 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 same operation a
5cc50 72 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 re not backed ou
5cc60 74 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 t and no rollbac
5cc70 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 k.** occurs. IG
5cc80 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 NORE means that
5cc90 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 the particular r
5cca0 6f 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74 ow that caused t
5ccb0 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a he constraint.**
5ccc0 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e error is not in
5ccd0 73 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 serted or update
5cce0 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 d. Processing c
5ccf0 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 ontinues and no
5cd00 65 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 error.** is retu
5cd10 72 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d rned. REPLACE m
5cd20 65 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 eans that preexi
5cd30 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 sting database r
5cd40 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a ows that caused.
5cd50 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 ** a UNIQUE cons
5cd60 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
5cd70 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 are removed so
5cd80 74 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 that the new ins
5cd90 65 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 ert or.** update
5cda0 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 can proceed. P
5cdb0 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e rocessing contin
5cdc0 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 ues and no error
5cdd0 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a is reported..**
5cde0 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 .** RESTRICT, SE
5cdf0 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 TNULL, and CASCA
5ce00 44 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 DE actions apply
5ce10 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e only to foreign
5ce20 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 keys..** RESTRI
5ce30 43 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 CT is the same a
5ce40 73 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 s ABORT for IMME
5ce50 44 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 DIATE foreign ke
5ce60 79 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 ys and the.** sa
5ce70 6d 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 me as ROLLBACK f
5ce80 6f 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 or DEFERRED keys
5ce90 2e 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 . SETNULL means
5cea0 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 that the foreig
5ceb0 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 n.** key is set
5cec0 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 to NULL. CASCAD
5ced0 45 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 E means that a D
5cee0 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 ELETE or UPDATE
5cef0 6f 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 of the.** refere
5cf00 6e 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 nced table row i
5cf10 73 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 s propagated int
5cf20 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 o the row that h
5cf30 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 olds the.** fore
5cf40 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 ign key..** .**
5cf50 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 The following sy
5cf60 6d 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 mbolic values ar
5cf70 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 e used to record
5cf80 20 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f which type.** o
5cf90 66 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 f action to take
5cfa0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f ..*/.#define OE_
5cfb0 4e 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 None 0 /*
5cfc0 54 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 There is no cons
5cfd0 74 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 traint to check
5cfe0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f */.#define OE_Ro
5cff0 6c 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 llback 1 /* Fa
5d000 69 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e il the operation
5d010 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 and rollback th
5d020 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f e transaction */
5d030 0a 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 .#define OE_Abor
5d040 74 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b t 2 /* Back
5d050 20 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 out changes but
5d060 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 do no rollback
5d070 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 transaction */.#
5d080 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 define OE_Fail
5d090 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 3 /* Stop t
5d0a0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 he operation but
5d0b0 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 leave all prior
5d0c0 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 changes */.#def
5d0d0 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 ine OE_Ignore
5d0e0 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 4 /* Ignore th
5d0f0 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 e error. Do not
5d100 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 do the INSERT or
5d110 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 UPDATE */.#defi
5d120 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 ne OE_Replace 5
5d130 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 /* Delete exi
5d140 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 sting record, th
5d150 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 en do INSERT or
5d160 55 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 UPDATE */..#defi
5d170 6e 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 ne OE_Restrict 6
5d180 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 /* OE_Abort f
5d190 6f 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 or IMMEDIATE, OE
5d1a0 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 _Rollback for DE
5d1b0 46 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e FERRED */.#defin
5d1c0 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 e OE_SetNull 7
5d1d0 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 /* Set the for
5d1e0 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 eign key value t
5d1f0 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e o NULL */.#defin
5d200 65 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 e OE_SetDflt 8
5d210 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 /* Set the for
5d220 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 eign key value t
5d230 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f o its default */
5d240 0a 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 .#define OE_Casc
5d250 61 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 ade 9 /* Casc
5d260 61 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 ade the changes
5d270 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 */..#define OE_D
5d280 65 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 efault 99 /* D
5d290 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64 o whatever the d
5d2a0 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 efault action is
5d2b0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 */.../*.** An i
5d2c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
5d2d0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
5d2e0 72 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 re is passed as
5d2f0 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 the first.** arg
5d300 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
5d310 56 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 VdbeKeyCompare a
5d320 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f nd is used to co
5d330 6e 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f ntrol the .** co
5d340 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 mparison of the
5d350 74 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a two index keys..
5d360 2a 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 */.struct KeyInf
5d370 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 o {. sqlite3 *d
5d380 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 b; /* The
5d390 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
5d3a0 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 tion */. u8 enc
5d3b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
5d3c0 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d Text encoding -
5d3d0 20 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 one of the TEXT
5d3e0 5f 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a _Utf* values */.
5d3f0 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 u16 nField;
5d400 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
5d410 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 of entries in aC
5d420 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 oll[] */. u8 *a
5d430 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f SortOrder; /
5d440 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 * If defined an
5d450 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 aSortOrder[i] is
5d460 20 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 true, sort DESC
5d470 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 */. CollSeq *a
5d480 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c Coll[1]; /* Col
5d490 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
5d4a0 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 for each term of
5d4b0 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a the key */.};..
5d4c0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
5d4d0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
5d4e0 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c ng structure hol
5d4f0 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ds information a
5d500 62 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 bout a.** single
5d510 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 index record th
5d520 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 at has already b
5d530 65 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 een parsed out i
5d540 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a nto individual.*
5d550 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 * values..**.**
5d560 41 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f A record is an o
5d570 62 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 bject that conta
5d580 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ins one or more
5d590 66 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a fields of data..
5d5a0 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 ** Records are u
5d5b0 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 sed to store the
5d5c0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 content of a ta
5d5d0 62 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 ble row and to s
5d5e0 74 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 tore.** the key
5d5f0 6f 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 of an index. A
5d600 62 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 blob encoding of
5d610 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 a record is cre
5d620 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f ated by.** the O
5d630 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 P_MakeRecord opc
5d640 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 ode of the VDBE
5d650 61 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 and is disassemb
5d660 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 led by the.** OP
5d670 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a _Column opcode..
5d680 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 **.** This struc
5d690 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 ture holds a rec
5d6a0 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 ord that has alr
5d6b0 65 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 eady been disass
5d6c0 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 embled.** into i
5d6d0 74 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 ts constituent f
5d6e0 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 ields..*/.struct
5d6f0 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
5d700 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 {. KeyInfo *pKe
5d710 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 yInfo; /* Colla
5d720 74 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 tion and sort-or
5d730 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 der information
5d740 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b */. u16 nField;
5d750 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5d760 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
5d770 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 apMem[] */. u1
5d780 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 6 flags;
5d790 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 /* Boolean set
5d7a0 74 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 tings. UNPACKED
5d7b0 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 _... below */.
5d7c0 69 36 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20 i64 rowid;
5d7d0 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55 /* Used by U
5d7e0 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 NPACKED_PREFIX_S
5d7f0 45 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a EARCH */. Mem *
5d800 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f aMem; /
5d810 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a * Values */.};..
5d820 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
5d830 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 lues of Unpacked
5d840 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a Record.flags.*/.
5d850 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
5d860 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 20 20 30 _NEED_FREE 0
5d870 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 x0001 /* Memory
5d880 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 is from sqlite3
5d890 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 Malloc() */.#def
5d8a0 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 ine UNPACKED_NEE
5d8b0 44 5f 44 45 53 54 52 4f 59 20 20 30 78 30 30 30 D_DESTROY 0x000
5d8c0 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 2 /* apMem[]s s
5d8d0 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 hould all be des
5d8e0 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e troyed */.#defin
5d8f0 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 e UNPACKED_IGNOR
5d900 45 5f 52 4f 57 49 44 20 20 30 78 30 30 30 34 20 E_ROWID 0x0004
5d910 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c /* Ignore trail
5d920 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 ing rowid on key
5d930 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 1 */.#define UNP
5d940 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 ACKED_INCRKEY
5d950 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 0x0008 /* M
5d960 61 6b 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20 ake this key an
5d970 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a epsilon larger *
5d980 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b /.#define UNPACK
5d990 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 ED_PREFIX_MATCH
5d9a0 20 30 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72 0x0010 /* A pr
5d9b0 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f efix match is co
5d9c0 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 nsidered OK */.#
5d9d0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
5d9e0 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 30 78 PREFIX_SEARCH 0x
5d9f0 30 30 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69 0020 /* A prefi
5da00 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 x match is consi
5da10 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a dered OK */../*.
5da20 2a 2a 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65 ** Each SQL inde
5da30 78 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 x is represented
5da40 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e in memory by an
5da50 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
5da60 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
5da70 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
5da80 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 he columns of th
5da90 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 72 65 e table that are
5daa0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 to be indexed a
5dab0 72 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 re described.**
5dac0 62 79 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b by the aiColumn[
5dad0 5d 20 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20 ] field of this
5dae0 73 74 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20 structure. For
5daf0 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 example, suppose
5db00 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 68 65 20 .** we have the
5db10 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 following table
5db20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a and index:.**.**
5db30 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
5db40 45 20 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32 E Ex1(c1 int, c2
5db50 20 69 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a int, c3 text);.
5db60 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e ** CREATE IN
5db70 44 45 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63 DEX Ex2 ON Ex1(c
5db80 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 3,c1);.**.** In
5db90 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 the Table struct
5dba0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 45 ure describing E
5dbb0 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 x1, nCol==3 beca
5dbc0 75 73 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a use there are.**
5dbd0 20 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 three columns i
5dbe0 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e n the table. In
5dbf0 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 the Index struc
5dc00 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a ture describing.
5dc10 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d ** Ex2, nColumn=
5dc20 3d 32 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68 =2 since 2 of th
5dc30 65 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 e 3 columns of E
5dc40 78 31 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a x1 are indexed..
5dc50 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 ** The value of
5dc60 61 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 aiColumn is {2,
5dc70 30 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 0}. aiColumn[0]
5dc80 3d 3d 32 20 62 65 63 61 75 73 65 20 74 68 65 20 ==2 because the
5dc90 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e .** first column
5dca0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 to be indexed (
5dcb0 63 33 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 c3) has an index
5dcc0 20 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f of 2 in Ex1.aCo
5dcd0 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f l[]..** The seco
5dce0 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 nd column to be
5dcf0 69 6e 64 65 78 65 64 20 28 63 31 29 20 68 61 73 indexed (c1) has
5dd00 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69 an index of 0 i
5dd10 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c n.** Ex1.aCol[],
5dd20 20 68 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c hence Ex2.aiCol
5dd30 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a umn[1]==0..**.**
5dd40 20 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 The Index.onErr
5dd50 6f 72 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69 or field determi
5dd60 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e nes whether or n
5dd70 6f 74 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 ot the indexed c
5dd80 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 olumns.** must b
5dd90 65 20 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61 e unique and wha
5dda0 74 20 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20 t to do if they
5ddb0 61 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 are not. When I
5ddc0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f ndex.onError=OE_
5ddd0 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e None,.** it mean
5dde0 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 s this is not a
5ddf0 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f unique index. O
5de00 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 61 therwise it is a
5de10 20 75 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a unique index.**
5de20 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
5de30 66 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 f Index.onError
5de40 69 6e 64 69 63 61 74 65 20 74 68 65 20 77 68 69 indicate the whi
5de50 63 68 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f ch conflict reso
5de60 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 lution .** algor
5de70 69 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 ithm to employ w
5de80 68 65 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d henever an attem
5de90 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e pt is made to in
5dea0 73 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 sert a non-uniqu
5deb0 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f e.** element..*/
5dec0 0a 73 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a .struct Index {.
5ded0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
5dee0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
5def0 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e is index */. in
5df00 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f t nColumn; /
5df10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
5df20 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 mns in the table
5df30 20 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e used by this in
5df40 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 dex */. int *ai
5df50 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 Column; /* Whi
5df60 63 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 ch columns are u
5df70 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 sed by this inde
5df80 78 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a x. 1st is 0 */.
5df90 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f unsigned *aiRo
5dfa0 77 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 wEst; /* Result
5dfb0 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e of ANALYZE: Est.
5dfc0 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 rows selected b
5dfd0 79 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f y each column */
5dfe0 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 . Table *pTable
5dff0 3b 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 ; /* The SQL t
5e000 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 able being index
5e010 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d ed */. int tnum
5e020 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 ; /* Page
5e030 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 containing root
5e040 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 of this index i
5e050 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
5e060 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b */. u8 onError;
5e070 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 /* OE_Abor
5e080 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 t, OE_Ignore, OE
5e090 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f _Replace, or OE_
5e0a0 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 None */. u8 aut
5e0b0 6f 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 oIndex; /* Tr
5e0c0 75 65 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74 ue if is automat
5e0d0 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28 ically created (
5e0e0 65 78 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a ex: by UNIQUE) *
5e0f0 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 /. char *zColAf
5e100 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 f; /* String d
5e110 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 efining the affi
5e120 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c nity of each col
5e130 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a umn */. Index *
5e140 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 pNext; /* The
5e150 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f next index asso
5e160 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
5e170 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 same table */.
5e180 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b Schema *pSchema;
5e190 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 /* Schema conta
5e1a0 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 ining this index
5e1b0 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f */. u8 *aSortO
5e1c0 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 rder; /* Array
5e1d0 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 of size Index.nC
5e1e0 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 olumn. True==DES
5e1f0 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f C, False==ASC */
5e200 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c . char **azColl
5e210 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 ; /* Array of
5e220 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
5e230 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 ce names for ind
5e240 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ex */.};../*.**
5e250 45 61 63 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e Each token comin
5e260 67 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 65 78 g out of the lex
5e270 65 72 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 er is an instanc
5e280 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 e of.** this str
5e290 75 63 74 75 72 65 2e 20 20 54 6f 6b 65 6e 73 20 ucture. Tokens
5e2a0 61 72 65 20 61 6c 73 6f 20 75 73 65 64 20 61 73 are also used as
5e2b0 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 part of an expr
5e2c0 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f ession..**.** No
5e2d0 74 65 20 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 te if Token.z==0
5e2e0 20 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20 then Token.dyn
5e2f0 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 72 65 20 and Token.n are
5e300 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 2a 2a undefined and.**
5e310 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72 61 6e may contain ran
5e320 64 6f 6d 20 76 61 6c 75 65 73 2e 20 20 44 6f 20 dom values. Do
5e330 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 61 73 73 not make any ass
5e340 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74 20 54 umptions about T
5e350 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20 oken.dyn.** and
5e360 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b Token.n when Tok
5e370 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75 en.z==0..*/.stru
5e380 63 74 20 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e ct Token {. con
5e390 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
5e3a0 20 2a 7a 3b 20 2f 2a 20 54 65 78 74 20 6f 66 20 *z; /* Text of
5e3b0 74 68 65 20 74 6f 6b 65 6e 2e 20 20 4e 6f 74 20 the token. Not
5e3c0 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 21 NULL-terminated!
5e3d0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 64 */. unsigned d
5e3e0 79 6e 20 20 20 20 3a 20 31 3b 20 20 20 20 2f 2a yn : 1; /*
5e3f0 20 54 72 75 65 20 66 6f 72 20 6d 61 6c 6c 6f 63 True for malloc
5e400 65 64 20 6d 65 6d 6f 72 79 2c 20 66 61 6c 73 65 ed memory, false
5e410 20 66 6f 72 20 73 74 61 74 69 63 20 2a 2f 0a 20 for static */.
5e420 20 75 6e 73 69 67 6e 65 64 20 71 75 6f 74 65 64 unsigned quoted
5e430 20 3a 20 31 3b 20 20 20 20 2f 2a 20 54 72 75 65 : 1; /* True
5e440 20 69 66 20 74 6f 6b 65 6e 20 73 74 69 6c 6c 20 if token still
5e450 68 61 73 20 69 74 73 20 71 75 6f 74 65 73 20 2a has its quotes *
5e460 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e 20 20 /. unsigned n
5e470 20 20 20 20 3a 20 33 30 3b 20 20 20 2f 2a 20 4e : 30; /* N
5e480 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
5e490 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f 6b 65 ers in this toke
5e4a0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 n */.};../*.** A
5e4b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
5e4c0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e is structure con
5e4d0 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f tains informatio
5e4e0 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 n needed to gene
5e4f0 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 rate.** code for
5e500 20 61 20 53 45 4c 45 43 54 20 74 68 61 74 20 63 a SELECT that c
5e510 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 ontains aggregat
5e520 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a e functions..**.
5e530 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d 3d 54 ** If Expr.op==T
5e540 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f 72 20 K_AGG_COLUMN or
5e550 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 TK_AGG_FUNCTION
5e560 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67 49 6e then Expr.pAggIn
5e570 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 fo is a.** point
5e580 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 er to this struc
5e590 74 75 72 65 2e 20 20 54 68 65 20 45 78 70 72 2e ture. The Expr.
5e5a0 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 69 73 iColumn field is
5e5b0 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a the index in.**
5e5c0 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 5d 20 AggInfo.aCol[]
5e5d0 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 or AggInfo.aFunc
5e5e0 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f [] of informatio
5e5f0 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 n needed to gene
5e600 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 rate.** code for
5e610 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a that node..**.*
5e620 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70 * AggInfo.pGroup
5e630 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f 2e 61 By and AggInfo.a
5e640 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 6e 74 Func.pExpr point
5e650 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74 68 69 to fields withi
5e660 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 n the.** origina
5e670 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 l Select structu
5e680 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 re that describe
5e690 73 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 s the SELECT sta
5e6a0 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 0a 2a tement. These.*
5e6b0 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f 74 20 * fields do not
5e6c0 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 need to be freed
5e6d0 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 74 69 when deallocati
5e6e0 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73 ng the AggInfo s
5e6f0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 tructure..*/.str
5e700 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a 20 20 uct AggInfo {.
5e710 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b 20 20 u8 directMode;
5e720 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 /* Direc
5e730 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f 64 65 t rendering mode
5e740 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 74 61 means take data
5e750 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 20 20 directly.
5e760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e770 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f 75 72 ** from sour
5e780 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 65 72 ce tables rather
5e790 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 75 6d than from accum
5e7a0 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 38 20 ulators */. u8
5e7b0 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 20 20 useSortingIdx;
5e7c0 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 65 63 /* In direc
5e7d0 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 6e 63 t mode, referenc
5e7e0 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e e the sorting in
5e7f0 64 65 78 20 72 61 74 68 65 72 0a 20 20 20 20 20 dex rather.
5e800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e810 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 ** than the
5e820 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f source table */
5e830 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64 . int sortingId
5e840 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 x; /* Cu
5e850 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 rsor number of t
5e860 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 he sorting index
5e870 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
5e880 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a pGroupBy; /*
5e890 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c The group by cl
5e8a0 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 ause */. int nS
5e8b0 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 20 20 ortingColumn;
5e8c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
5e8d0 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f olumns in the so
5e8e0 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 rting index */.
5e8f0 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f struct AggInfo_
5e900 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f 72 20 col { /* For
5e910 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 65 64 each column used
5e920 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62 6c 65 in source table
5e930 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a s */. Table *
5e940 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 pTab;
5e950 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 62 6c /* Source tabl
5e960 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 61 e */. int iTa
5e970 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ble;
5e980 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 /* Cursor numb
5e990 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 er of the source
5e9a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e table */. in
5e9b0 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 t iColumn;
5e9c0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e /* Column
5e9d0 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 number within t
5e9e0 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 he source table
5e9f0 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74 */. int iSort
5ea00 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 erColumn;
5ea10 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 /* Column number
5ea20 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20 in the sorting
5ea30 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 index */. int
5ea40 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 iMem;
5ea50 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
5ea60 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 61 63 location that ac
5ea70 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61 74 6f ts as accumulato
5ea80 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 r */. Expr *p
5ea90 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
5eaa0 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 /* The origina
5eab0 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a l expression */.
5eac0 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 6e 74 } *aCol;. int
5ead0 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 nColumn;
5eae0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5eaf0 66 20 75 73 65 64 20 65 6e 74 72 69 65 73 20 69 f used entries i
5eb00 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e n aCol[] */. in
5eb10 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 3b 20 t nColumnAlloc;
5eb20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
5eb30 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 of slots allocat
5eb40 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 2a 2f ed for aCol[] */
5eb50 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 6c 61 . int nAccumula
5eb60 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 tor; /* Nu
5eb70 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
5eb80 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f 75 67 that show throug
5eb90 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e h to the output.
5eba0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
5ebb0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 64 ** Ad
5ebc0 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73 ditional columns
5ebd0 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 20 61 are used only a
5ebe0 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a s parameters to.
5ebf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ec00 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 67 67 ** agg
5ec10 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
5ec20 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 */. struct Agg
5ec30 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 2f 2a Info_func { /*
5ec40 20 46 6f 72 20 65 61 63 68 20 61 67 67 72 65 67 For each aggreg
5ec50 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ate function */.
5ec60 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b Expr *pExpr;
5ec70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5ec80 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 6f 64 Expression encod
5ec90 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69 6f 6e ing the function
5eca0 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 */. FuncDef
5ecb0 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 *pFunc;
5ecc0 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67 61 74 /* The aggregat
5ecd0 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 e function imple
5ece0 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 mentation */.
5ecf0 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 int iMem;
5ed00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
5ed10 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 ory location tha
5ed20 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 t acts as accumu
5ed30 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74 lator */. int
5ed40 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20 iDistinct;
5ed50 20 20 20 20 20 20 2f 2a 20 45 70 68 65 6d 65 72 /* Ephemer
5ed60 61 6c 20 74 61 62 6c 65 20 75 73 65 64 20 74 6f al table used to
5ed70 20 65 6e 66 6f 72 63 65 20 44 49 53 54 49 4e 43 enforce DISTINC
5ed80 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e 63 3b T */. } *aFunc;
5ed90 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20 20 20 . int nFunc;
5eda0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5edb0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
5edc0 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 in aFunc[] */.
5edd0 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63 3b 20 int nFuncAlloc;
5ede0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5edf0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
5ee00 61 74 65 64 20 66 6f 72 20 61 46 75 6e 63 5b 5d ated for aFunc[]
5ee10 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 */.};../*.** Ea
5ee20 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78 ch node of an ex
5ee30 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 pression in the
5ee40 70 61 72 73 65 20 74 72 65 65 20 69 73 20 61 6e parse tree is an
5ee50 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 instance.** of
5ee60 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a this structure..
5ee70 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73 **.** Expr.op is
5ee80 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65 the opcode. The
5ee90 20 69 6e 74 65 67 65 72 20 70 61 72 73 65 72 20 integer parser
5eea0 74 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20 token codes are
5eeb0 72 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63 reused.** as opc
5eec0 6f 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65 odes here. For e
5eed0 78 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73 xample, the pars
5eee0 65 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45 er defines TK_GE
5eef0 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 to be an intege
5ef00 72 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73 r.** code repres
5ef10 65 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20 enting the ">="
5ef20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 operator. This s
5ef30 61 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 ame integer code
5ef40 20 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f is reused.** to
5ef50 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 67 represent the g
5ef60 72 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65 reater-than-or-e
5ef70 71 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72 qual-to operator
5ef80 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 in the expressi
5ef90 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a on.** tree..**.*
5efa0 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
5efb0 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69 ion is an SQL li
5efc0 74 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45 teral (TK_INTEGE
5efd0 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f R, TK_FLOAT, TK_
5efe0 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f BLOB, .** or TK_
5eff0 53 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78 STRING), then Ex
5f000 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e pr.token contain
5f010 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 s the text of th
5f020 65 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49 e SQL literal. I
5f030 66 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73 f.** the express
5f040 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c ion is a variabl
5f050 65 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c e (TK_VARIABLE),
5f060 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e then Expr.token
5f070 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a contains the .*
5f080 2a 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e * variable name.
5f090 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 Finally, if the
5f0a0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
5f0b0 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28 n SQL function (
5f0c0 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a TK_FUNCTION),.**
5f0d0 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e then Expr.token
5f0e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 contains the na
5f0f0 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 me of the functi
5f100 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 on..**.** Expr.p
5f110 52 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 Right and Expr.p
5f120 4c 65 66 74 20 61 72 65 20 74 68 65 20 6c 65 66 Left are the lef
5f130 74 20 61 6e 64 20 72 69 67 68 74 20 73 75 62 65 t and right sube
5f140 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a xpressions of a.
5f150 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 ** binary operat
5f160 6f 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f or. Either or bo
5f170 74 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a th may be NULL..
5f180 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69 **.** Expr.x.pLi
5f190 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 st is a list of
5f1a0 61 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65 arguments if the
5f1b0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
5f1c0 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a n SQL function,.
5f1d0 2a 2a 20 61 20 43 41 53 45 20 65 78 70 72 65 73 ** a CASE expres
5f1e0 73 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78 sion or an IN ex
5f1f0 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 pression of the
5f200 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 form "<lhs> IN (
5f210 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a <y>, <z>...)"..*
5f220 2a 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 * Expr.x.pSelect
5f230 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65 20 is used if the
5f240 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
5f250 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e sub-select or an
5f260 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a expression of.*
5f270 2a 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 * the form "<lhs
5f280 3e 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e > IN (SELECT ...
5f290 29 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49 )". If the EP_xI
5f2a0 73 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 73 sSelect bit is s
5f2b0 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70 et in the.** Exp
5f2c0 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 r.flags mask, th
5f2d0 65 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 en Expr.x.pSelec
5f2e0 74 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65 t is valid. Othe
5f2f0 72 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c rwise, Expr.x.pL
5f300 69 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64 ist is .** valid
5f310 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 ..**.** An expre
5f320 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 ssion of the for
5f330 6d 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 m ID or ID.ID re
5f340 66 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e fers to a column
5f350 20 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 in a table..**
5f360 46 6f 72 20 73 75 63 68 20 65 78 70 72 65 73 73 For such express
5f370 69 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 ions, Expr.op is
5f380 20 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d set to TK_COLUM
5f390 4e 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c N and Expr.iTabl
5f3a0 65 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 e is.** the inte
5f3b0 67 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 ger cursor numbe
5f3c0 72 20 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 r of a VDBE curs
5f3d0 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 or pointing to t
5f3e0 68 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a hat table and.**
5f3f0 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 Expr.iColumn is
5f400 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 the column numb
5f410 65 72 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 er for the speci
5f420 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 fic column. If
5f430 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f the.** expressio
5f440 6e 20 69 73 20 75 73 65 64 20 61 73 20 61 20 72 n is used as a r
5f450 65 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 esult in an aggr
5f460 65 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 egate SELECT, th
5f470 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 en the.** value
5f480 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 is also stored i
5f490 6e 20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 n the Expr.iAgg
5f4a0 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 column in the ag
5f4b0 67 72 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a gregate so that.
5f4c0 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 ** it can be acc
5f4d0 65 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 essed after all
5f4e0 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20 63 aggregates are c
5f4f0 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 omputed..**.** I
5f500 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
5f510 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76 is an unbound v
5f520 61 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28 ariable marker (
5f530 61 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20 a question mark
5f540 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f .** character '?
5f550 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 ' in the origina
5f560 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20 l SQL) then the
5f570 45 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64 Expr.iTable hold
5f580 73 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 s the index .**
5f590 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 number for that
5f5a0 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 variable..**.**
5f5b0 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
5f5c0 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 n is a subquery
5f5d0 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d then Expr.iColum
5f5e0 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 n holds an integ
5f5f0 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e er.** register n
5f600 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 umber containing
5f610 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 the result of t
5f620 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 66 he subquery. If
5f630 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 the.** subquery
5f640 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e gives a constan
5f650 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69 t result, then i
5f660 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66 Table is -1. If
5f670 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a the subquery.**
5f680 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 65 gives a differe
5f690 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 66 nt answer at dif
5f6a0 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72 ferent times dur
5f6b0 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72 ing statement pr
5f6c0 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e ocessing.** then
5f6d0 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 61 iTable is the a
5f6e0 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 72 ddress of a subr
5f6f0 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70 outine that comp
5f700 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 72 utes the subquer
5f710 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 y..**.** If the
5f720 45 78 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 Expr is of type
5f730 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 OP_Column, and t
5f740 68 65 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 he table it is s
5f750 65 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a electing from.**
5f760 20 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 is a disk table
5f770 20 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 or the "old.*"
5f780 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 pseudo-table, th
5f790 65 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 en pTab points t
5f7a0 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 o the.** corresp
5f7b0 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 onding table def
5f7c0 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 inition..**.** A
5f7d0 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a LLOCATION NOTES:
5f7e0 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65 .**.** Expr obje
5f7f0 63 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f cts can use a lo
5f800 74 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63 t of memory spac
5f810 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 73 63 e in database sc
5f820 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c hema. To.** hel
5f830 70 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 p reduce memory
5f840 72 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f requirements, so
5f850 6d 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20 metimes an Expr
5f860 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a object will be.*
5f870 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e * truncated. An
5f880 64 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 d to reduce the
5f890 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 number of memory
5f8a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f allocations, so
5f8b0 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f metimes.** two o
5f8c0 72 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65 r more Expr obje
5f8d0 63 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 cts will be stor
5f8e0 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d ed in a single m
5f8f0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
5f900 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 ,.** together wi
5f910 74 68 20 45 78 70 72 2e 74 6f 6b 65 6e 20 61 6e th Expr.token an
5f920 64 2f 6f 72 20 45 78 70 72 2e 73 70 61 6e 20 73 d/or Expr.span s
5f930 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 trings..**.** If
5f940 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 2c the EP_Reduced,
5f950 20 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 2c 20 61 EP_SpanToken, a
5f960 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 nd EP_TokenOnly
5f970 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 77 68 flags are set wh
5f980 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f 62 en.** an Expr ob
5f990 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61 74 65 ject is truncate
5f9a0 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64 75 d. When EP_Redu
5f9b0 63 65 64 20 69 73 20 73 65 74 2c 20 74 68 65 6e ced is set, then
5f9c0 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69 6c all.** the chil
5f9d0 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 69 d Expr objects i
5f9e0 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66 74 n the Expr.pLeft
5f9f0 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 and Expr.pRight
5fa00 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72 65 subtrees.** are
5fa10 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
5fa20 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f 72 n the same memor
5fa30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e y allocation. N
5fa40 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 ote, however, th
5fa50 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72 65 at.** the subtre
5fa60 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c 69 es in Expr.x.pLi
5fa70 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65 st or Expr.x.pSe
5fa80 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79 73 20 lect are always
5fa90 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61 6c separately.** al
5faa0 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64 6c located, regardl
5fab0 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f ess of whether o
5fac0 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65 64 r not EP_Reduced
5fad0 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72 75 is set..*/.stru
5fae0 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f ct Expr {. u8 o
5faf0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
5fb00 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 /* Operation
5fb10 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 performed by thi
5fb20 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 s node */. char
5fb30 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 affinity;
5fb40 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
5fb50 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ty of the column
5fb60 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 or 0 if not a c
5fb70 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 56 56 41 5f 4f olumn */. VVA_O
5fb80 4e 4c 59 28 75 38 20 76 76 61 46 6c 61 67 73 3b NLY(u8 vvaFlags;
5fb90 29 20 2f 2a 20 46 6c 61 67 73 20 75 73 65 64 20 ) /* Flags used
5fba0 66 6f 72 20 56 56 26 41 20 6f 6e 6c 79 2e 20 20 for VV&A only.
5fbb0 45 56 56 41 5f 2a 20 62 65 6c 6f 77 2e 20 2a 2f EVVA_* below. */
5fbc0 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 . u16 flags;
5fbd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72 /* Var
5fbe0 69 6f 75 73 20 66 6c 61 67 73 2e 20 20 45 50 5f ious flags. EP_
5fbf0 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 * See below */.
5fc00 20 54 6f 6b 65 6e 20 74 6f 6b 65 6e 3b 20 20 20 Token token;
5fc10 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 /* An op
5fc20 65 72 61 6e 64 20 74 6f 6b 65 6e 20 2a 2f 0a 0a erand token */..
5fc30 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 54 /* If the EP_T
5fc40 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 okenOnly flag is
5fc50 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 set in the Expr
5fc60 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 .flags mask, the
5fc70 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 n no. ** space
5fc80 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 is allocated for
5fc90 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f the fields belo
5fca0 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e w this point. An
5fcb0 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a attempt to. **
5fcc0 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c access them wil
5fcd0 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 l result in a se
5fce0 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e gfault or malfun
5fcf0 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a ction. . ******
5fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
5fd40 0a 0a 20 20 54 6f 6b 65 6e 20 73 70 61 6e 3b 20 .. Token span;
5fd50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
5fd60 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 mplete text of t
5fd70 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f he expression */
5fd80 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 .. /* If the EP
5fd90 5f 53 70 61 6e 54 6f 6b 65 6e 20 66 6c 61 67 20 _SpanToken flag
5fda0 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 is set in the Ex
5fdb0 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 pr.flags mask, t
5fdc0 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 hen no. ** spac
5fdd0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 e is allocated f
5fde0 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 or the fields be
5fdf0 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 low this point.
5fe00 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 An attempt to.
5fe10 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 ** access them w
5fe20 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ill result in a
5fe30 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 segfault or malf
5fe40 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a unction. . ****
5fe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5fe90 2a 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 */.. Expr *pLef
5fea0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
5feb0 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a Left subnode */.
5fec0 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20 Expr *pRight;
5fed0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 /* Righ
5fee0 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 t subnode */. u
5fef0 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c nion {. ExprL
5ff00 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 ist *pList;
5ff10 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67 75 /* Function argu
5ff20 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65 78 ments or in "<ex
5ff30 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 pr> IN (<expr-li
5ff40 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65 st)" */. Sele
5ff50 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 ct *pSelect;
5ff60 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75 62 /* Used for sub
5ff70 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c 65 -selects and "<e
5ff80 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74 xpr> IN (<select
5ff90 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 >)" */. } x;.
5ffa0 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 CollSeq *pColl;
5ffb0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
5ffc0 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66 20 llation type of
5ffd0 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 the column or 0
5ffe0 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 */.. /* If the
5fff0 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 EP_Reduced flag
60000 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 is set in the Ex
60010 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 pr.flags mask, t
60020 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 hen no. ** spac
60030 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 e is allocated f
60040 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 or the fields be
60050 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 low this point.
60060 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 An attempt to.
60070 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 ** access them w
60080 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ill result in a
60090 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 segfault or malf
600a0 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a unction.. *****
600b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
600f0 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c /.. int iTable,
60100 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 iColumn; /* W
60110 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d hen op==TK_COLUM
60120 4e 2c 20 74 68 65 6e 20 74 68 69 73 20 65 78 70 N, then this exp
60130 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20 74 68 65 r node means the
60140 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
60150 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 43 6f ** iCo
60160 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64 20 6f 66 lumn-th field of
60170 20 74 68 65 20 69 54 61 62 6c 65 2d 74 68 20 74 the iTable-th t
60180 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67 67 49 6e able. */. AggIn
60190 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 fo *pAggInfo;
601a0 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f /* Used by TK_
601b0 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 AGG_COLUMN and T
601c0 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a K_AGG_FUNCTION *
601d0 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b 20 20 20 /. int iAgg;
601e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 /* Wh
601f0 69 63 68 20 65 6e 74 72 79 20 69 6e 20 70 41 67 ich entry in pAg
60200 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72 gInfo->aCol[] or
60210 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 ->aFunc[] */.
60220 69 6e 74 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 int iRightJoinTa
60230 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 45 50 5f ble; /* If EP_
60240 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 20 72 69 FromJoin, the ri
60250 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 ght table of the
60260 20 6a 6f 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 join */. Table
60270 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 *pTab;
60280 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20 54 /* Table for T
60290 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 K_COLUMN express
602a0 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 51 4c ions. */.#if SQL
602b0 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 ITE_MAX_EXPR_DEP
602c0 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 69 67 TH>0. int nHeig
602d0 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ht; /*
602e0 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 20 74 Height of the t
602f0 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68 ree headed by th
60300 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 is node */.#endi
60310 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 f.};../*.** The
60320 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 following are th
60330 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 e meanings of bi
60340 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 ts in the Expr.f
60350 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
60360 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f define EP_FromJo
60370 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 in 0x0001 /*
60380 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e Originated in ON
60390 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 or USING clause
603a0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 64 of a join */.#d
603b0 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 20 20 efine EP_Agg
603c0 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43 0x0002 /* C
603d0 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d ontains one or m
603e0 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 66 75 ore aggregate fu
603f0 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 nctions */.#defi
60400 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 20 ne EP_Resolved
60410 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44 73 20 0x0004 /* IDs
60420 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 have been resolv
60430 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f ed to COLUMNs */
60440 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 72 6f .#define EP_Erro
60450 72 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f r 0x0008 /
60460 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e * Expression con
60470 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 tains one or mor
60480 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 e errors */.#def
60490 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 74 20 ine EP_Distinct
604a0 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 67 67 0x0010 /* Agg
604b0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
604c0 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 6b 65 with DISTINCT ke
604d0 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65 yword */.#define
604e0 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 20 30 EP_VarSelect 0
604f0 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c 65 63 x0020 /* pSelec
60500 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 64 2c t is correlated,
60510 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 2a 2f not constant */
60520 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 62 6c 51 .#define EP_DblQ
60530 75 6f 74 65 64 20 20 30 78 30 30 34 30 20 20 2f uoted 0x0040 /
60540 2a 20 74 6f 6b 65 6e 2e 7a 20 77 61 73 20 6f 72 * token.z was or
60550 69 67 69 6e 61 6c 6c 79 20 69 6e 20 22 2e 2e 2e iginally in "...
60560 22 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f " */.#define EP_
60570 49 6e 66 69 78 46 75 6e 63 20 20 30 78 30 30 38 InfixFunc 0x008
60580 30 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 0 /* True for a
60590 6e 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e n infix function
605a0 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 65 74 : LIKE, GLOB, et
605b0 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f c */.#define EP_
605c0 45 78 70 43 6f 6c 6c 61 74 65 20 30 78 30 31 30 ExpCollate 0x010
605d0 30 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 0 /* Collating
605e0 73 65 71 75 65 6e 63 65 20 73 70 65 63 69 66 69 sequence specifi
605f0 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 2a 2f ed explicitly */
60600 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 6e 79 41 .#define EP_AnyA
60610 66 66 20 20 20 20 20 30 78 30 32 30 30 20 20 2f ff 0x0200 /
60620 2a 20 43 61 6e 20 74 61 6b 65 20 61 20 63 61 63 * Can take a cac
60630 68 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e hed column of an
60640 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 23 64 y affinity */.#d
60650 65 66 69 6e 65 20 45 50 5f 46 69 78 65 64 44 65 efine EP_FixedDe
60660 73 74 20 20 30 78 30 34 30 30 20 20 2f 2a 20 52 st 0x0400 /* R
60670 65 73 75 6c 74 20 6e 65 65 64 65 64 20 69 6e 20 esult needed in
60680 61 20 73 70 65 63 69 66 69 63 20 72 65 67 69 73 a specific regis
60690 74 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ter */.#define E
606a0 50 5f 49 6e 74 56 61 6c 75 65 20 20 20 30 78 30 P_IntValue 0x0
606b0 38 30 30 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 800 /* Integer
606c0 76 61 6c 75 65 20 63 6f 6e 74 61 69 6e 65 64 20 value contained
606d0 69 6e 20 69 54 61 62 6c 65 20 2a 2f 0a 23 64 65 in iTable */.#de
606e0 66 69 6e 65 20 45 50 5f 78 49 73 53 65 6c 65 63 fine EP_xIsSelec
606f0 74 20 20 30 78 31 30 30 30 20 20 2f 2a 20 78 2e t 0x1000 /* x.
60700 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 pSelect is valid
60710 20 28 6f 74 68 65 72 77 69 73 65 20 78 2e 70 4c (otherwise x.pL
60720 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23 64 65 66 ist is) */..#def
60730 69 6e 65 20 45 50 5f 52 65 64 75 63 65 64 20 20 ine EP_Reduced
60740 20 20 30 78 32 30 30 30 20 20 2f 2a 20 45 78 70 0x2000 /* Exp
60750 72 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 r struct is EXPR
60760 5f 52 45 44 55 43 45 44 53 49 5a 45 20 62 79 74 _REDUCEDSIZE byt
60770 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 es only */.#defi
60780 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 ne EP_TokenOnly
60790 20 30 78 34 30 30 30 20 20 2f 2a 20 45 78 70 72 0x4000 /* Expr
607a0 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 5f struct is EXPR_
607b0 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 62 79 TOKENONLYSIZE by
607c0 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 tes only */.#def
607d0 69 6e 65 20 45 50 5f 53 70 61 6e 54 6f 6b 65 6e ine EP_SpanToken
607e0 20 20 30 78 38 30 30 30 20 20 2f 2a 20 45 78 70 0x8000 /* Exp
607f0 72 20 73 69 7a 65 20 69 73 20 45 58 50 52 5f 53 r size is EXPR_S
60800 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 20 62 79 74 PANTOKENSIZE byt
60810 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 es */../*.** The
60820 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 following are t
60830 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 he meanings of b
60840 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e its in the Expr.
60850 76 76 61 46 6c 61 67 73 20 66 69 65 6c 64 2e 0a vvaFlags field..
60860 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 ** This informat
60870 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ion is only used
60880 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 when SQLite is
60890 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a compiled with.**
608a0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65 SQLITE_DEBUG de
608b0 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 fined..*/.#ifnde
608c0 66 20 4e 44 45 42 55 47 0a 23 64 65 66 69 6e 65 f NDEBUG.#define
608d0 20 45 56 56 41 5f 52 65 61 64 4f 6e 6c 79 54 6f EVVA_ReadOnlyTo
608e0 6b 65 6e 20 20 30 78 30 31 20 20 2f 2a 20 45 78 ken 0x01 /* Ex
608f0 70 72 2e 74 6f 6b 65 6e 2e 7a 20 69 73 20 72 65 pr.token.z is re
60900 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 ad-only */.#endi
60910 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d f../*.** These m
60920 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 acros can be use
60930 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 d to test, set,
60940 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e or clear bits in
60950 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 6c the .** Expr.fl
60960 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 ags field..*/.#d
60970 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 6f efine ExprHasPro
60980 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28 perty(E,P) (
60990 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 ((E)->flags&(P))
609a0 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 45 ==(P)).#define E
609b0 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
609c0 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e 66 y(E,P) (((E)->f
609d0 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 lags&(P))!=0).#d
609e0 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 6f efine ExprSetPro
609f0 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28 perty(E,P) (
60a00 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 E)->flags|=(P).#
60a10 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 72 define ExprClear
60a20 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 Property(E,P)
60a30 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 (E)->flags&=~(P)
60a40 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 ../*.** Macros t
60a50 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
60a60 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
60a70 72 65 71 75 69 72 65 64 20 62 79 20 61 20 6e 6f required by a no
60a80 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 73 74 rmal Expr .** st
60a90 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20 73 74 ruct, an Expr st
60aa0 72 75 63 74 20 77 69 74 68 20 74 68 65 20 45 50 ruct with the EP
60ab0 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 73 65 _Reduced flag se
60ac0 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73 20 t in Expr.flags
60ad0 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 72 20 .** and an Expr
60ae0 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20 struct with the
60af0 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 EP_TokenOnly fla
60b00 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e g set..*/.#defin
60b10 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 20 e EXPR_FULLSIZE
60b20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 sizeof
60b30 28 45 78 70 72 29 20 20 20 20 20 20 20 20 20 20 (Expr)
60b40 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 2a 2f /* Full size */
60b50 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 52 45 .#define EXPR_RE
60b60 44 55 43 45 44 53 49 5a 45 20 20 20 20 20 20 20 DUCEDSIZE
60b70 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 69 offsetof(Expr,i
60b80 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d 6d 6f Table) /* Commo
60b90 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23 64 n features */.#d
60ba0 65 66 69 6e 65 20 45 58 50 52 5f 53 50 41 4e 54 efine EXPR_SPANT
60bb0 4f 4b 45 4e 53 49 5a 45 20 20 20 20 20 20 6f 66 OKENSIZE of
60bc0 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c 65 66 fsetof(Expr,pLef
60bd0 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20 66 65 t) /* Fewer fe
60be0 61 74 75 72 65 73 20 2a 2f 0a 23 64 65 66 69 6e atures */.#defin
60bf0 65 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 e EXPR_TOKENONLY
60c00 53 49 5a 45 20 20 20 20 20 20 6f 66 66 73 65 74 SIZE offset
60c10 6f 66 28 45 78 70 72 2c 73 70 61 6e 29 20 20 20 of(Expr,span)
60c20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 /* Smallest pos
60c30 73 69 62 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 sible */../*.**
60c40 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 Flags passed to
60c50 74 68 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 the sqlite3ExprD
60c60 75 70 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 53 up() function. S
60c70 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f ee the header co
60c80 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f 76 65 20 mment .** above
60c90 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 sqlite3ExprDup()
60ca0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f for details..*/
60cb0 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44 55 50 .#define EXPRDUP
60cc0 5f 52 45 44 55 43 45 20 20 20 20 20 20 20 20 20 _REDUCE
60cd0 30 78 30 30 30 31 20 20 2f 2a 20 55 73 65 64 20 0x0001 /* Used
60ce0 72 65 64 75 63 65 64 2d 73 69 7a 65 20 45 78 70 reduced-size Exp
60cf0 72 20 6e 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 r nodes */.#defi
60d00 6e 65 20 45 58 50 52 44 55 50 5f 53 50 41 4e 20 ne EXPRDUP_SPAN
60d10 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 32 0x0002
60d20 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 /* Make a copy
60d30 20 6f 66 20 45 78 70 72 2e 73 70 61 6e 20 2a 2f of Expr.span */
60d40 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f ../*.** A list o
60d50 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 f expressions.
60d60 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 Each expression
60d70 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 may optionally h
60d80 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 ave a.** name.
60d90 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d An expr/name com
60da0 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 bination can be
60db0 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 used in several
60dc0 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 ways, such.** as
60dd0 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78 the list of "ex
60de0 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 73 pr AS ID" fields
60df0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 following a "SE
60e00 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a LECT" or in the.
60e10 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d ** list of "ID =
60e20 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20 expr" items in
60e30 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69 an UPDATE. A li
60e40 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e st of expression
60e50 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 s can.** also be
60e60 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72 67 used as the arg
60e70 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 ument to a funct
60e80 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61 ion, in which ca
60e90 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a se the a.zName.*
60ea0 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 * field is not u
60eb0 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 sed..*/.struct E
60ec0 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 xprList {. int
60ed0 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 nExpr;
60ee0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
60ef0 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 expressions on t
60f00 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 he list */. int
60f10 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 nAlloc;
60f20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
60f30 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 entries allocat
60f40 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e ed below */. in
60f50 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20 t iECursor;
60f60 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 /* VDBE Cur
60f70 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 sor associated w
60f80 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 73 ith this ExprLis
60f90 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 t */. struct Ex
60fa0 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 prList_item {.
60fb0 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 Expr *pExpr;
60fc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
60fd0 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 list of expressi
60fe0 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 ons */. char
60ff0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
61000 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 /* Token assoc
61010 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 iated with this
61020 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 expression */.
61030 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 u8 sortOrder;
61040 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f /* 1 fo
61050 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 r DESC or 0 for
61060 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f ASC */. u8 do
61070 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ne;
61080 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 /* A flag to i
61090 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f ndicate when pro
610a0 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 cessing is finis
610b0 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 hed */. u16 i
610c0 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
610d0 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 /* For ORDER B
610e0 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 Y, column number
610f0 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a in result set *
61100 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 /. u16 iAlias
61110 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
61120 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 Index into Parse
61130 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e .aAlias[] for zN
61140 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 ame */. } *a;
61150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
61160 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 /* One entry for
61170 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e each expression
61180 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
61190 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
611a0 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 s structure can
611b0 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c 69 hold a simple li
611c0 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 st of identifier
611d0 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 68 s,.** such as th
611e0 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 69 e list "a,b,c" i
611f0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
61200 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a statements:.**.*
61210 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e * INSERT IN
61220 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c 55 TO t(a,b,c) VALU
61230 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20 ES ...;.**
61240 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 78 CREATE INDEX idx
61250 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a ON t(a,b,c);.**
61260 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49 CREATE TRI
61270 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 45 GGER trig BEFORE
61280 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c 62 UPDATE ON t(a,b
61290 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 ,c) ...;.**.** T
612a0 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 20 he IdList.a.idx
612b0 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 68 field is used wh
612c0 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 65 en the IdList re
612d0 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 73 presents the lis
612e0 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e t of.** column n
612f0 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 62 ames after a tab
61300 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e le name in an IN
61310 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 SERT statement.
61320 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e In the statemen
61330 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 t.**.** INSE
61340 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 RT INTO t(a,b,c)
61350 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61 ....**.** If "a
61360 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 6f " is the k-th co
61370 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 74 lumn of table "t
61380 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e 61 ", then IdList.a
61390 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73 [0].idx==k..*/.s
613a0 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a 20 truct IdList {.
613b0 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 struct IdList_i
613c0 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a tem {. char *
613d0 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e zName; /* N
613e0 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e 74 ame of the ident
613f0 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 ifier */. int
61400 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f idx; /
61410 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20 * Index in some
61420 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20 Table.aCol[] of
61430 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a a column named z
61440 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a Name */. } *a;.
61450 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 int nId;
61460 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
61470 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 74 identifiers on t
61480 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 he list */. int
61490 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a nAlloc; /*
614a0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
614b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
614c0 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b a[] below */.};
614d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d ../*.** The bitm
614e0 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 66 ask datatype def
614f0 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73 ined below is us
61500 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f ed for various o
61510 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a ptimizations..**
61520 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69 .** Changing thi
61530 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20 s from a 64-bit
61540 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65 to a 32-bit type
61550 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62 limits the numb
61560 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 er of.** tables
61570 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20 in a join to 32
61580 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 20 instead of 64.
61590 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 75 But it also redu
615a0 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 ces the size.**
615b0 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 62 of the library b
615c0 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 69 y 738 bytes on i
615d0 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 x86..*/.typedef
615e0 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a u64 Bitmask;../*
615f0 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f .** The number o
61600 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d f bits in a Bitm
61610 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e ask. "BMS" mean
61620 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 22 s "BitMask Size"
61630 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53 ..*/.#define BMS
61640 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 ((int)(sizeof(
61650 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a Bitmask)*8))../*
61660 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
61670 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 g structure desc
61680 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 63 ribes the FROM c
61690 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 lause of a SELEC
616a0 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 T statement..**
616b0 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 75 Each table or su
616c0 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 bquery in the FR
616d0 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73 OM clause is a s
616e0 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 20 eparate element
616f0 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73 of.** the SrcLis
61700 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a t.a[] array..**.
61710 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69 ** With the addi
61720 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65 tion of multiple
61730 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 database suppor
61740 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t, the following
61750 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 61 structure.** ca
61760 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 n also be used t
61770 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72 o describe a par
61780 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 75 ticular table su
61790 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 ch as the table
617a0 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66 that.** is modif
617b0 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54 ied by an INSERT
617c0 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 , DELETE, or UPD
617d0 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ATE statement.
617e0 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c 2c In standard SQL,
617f0 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c 65 .** such a table
61800 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 6c must be a simpl
61810 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 74 e name: ID. But
61820 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 20 in SQLite, the
61830 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77 table can.** now
61840 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 be identified b
61850 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 6d y a database nam
61860 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 74 e, a dot, then t
61870 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49 he table name: I
61880 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 D.ID..**.** The
61890 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 20 jointype starts
618a0 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 20 out showing the
618b0 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 65 join type betwee
618c0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 n the current ta
618d0 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e ble.** and the n
618e0 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 ext table on the
618f0 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 73 list. The pars
61900 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c 69 er builds the li
61910 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20 st this way..**
61920 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c 69 But sqlite3SrcLi
61930 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 stShiftJoinType(
61940 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 74 ) later shifts t
61950 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20 he jointypes so
61960 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69 that each.** joi
61970 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 20 ntype expresses
61980 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e the join between
61990 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 the table and t
619a0 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 6c he previous tabl
619b0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63 e..*/.struct Src
619c0 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 72 List {. i16 nSr
619d0 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d c; /* Num
619e0 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f 72 ber of tables or
619f0 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 subqueries in t
61a00 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a he FROM clause *
61a10 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20 /. i16 nAlloc;
61a20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
61a30 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
61a40 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77 ted in a[] below
61a50 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 */. struct Src
61a60 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 List_item {.
61a70 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b char *zDatabase;
61a80 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 /* Name of dat
61a90 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 abase holding th
61aa0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 is table */.
61ab0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
61ac0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
61ad0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68 table */. ch
61ae0 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20 ar *zAlias;
61af0 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 20 /* The "B" part
61b00 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 68 of a "A AS B" ph
61b10 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20 rase. zName is
61b20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 54 the "A" */. T
61b30 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 able *pTab;
61b40 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c 65 /* An SQL table
61b50 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
61b60 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53 o zName */. S
61b70 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
61b80 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 /* A SELECT sta
61b90 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 70 tement used in p
61ba0 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20 lace of a table
61bb0 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 69 name */. u8 i
61bc0 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a sPopulated; /*
61bd0 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 Temporary table
61be0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
61bf0 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 6c SELECT is popul
61c00 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a ated */. u8 j
61c10 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f 2a ointype; /*
61c20 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65 Type of join be
61c30 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 20 tween this able
61c40 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 and the previous
61c50 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e */. u8 notIn
61c60 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 dexed; /* Tru
61c70 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 e if there is a
61c80 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 NOT INDEXED clau
61c90 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 se */. int iC
61ca0 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 ursor; /* T
61cb0 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e he VDBE cursor n
61cc0 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 umber used to ac
61cd0 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 cess this table
61ce0 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e */. Expr *pOn
61cf0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
61d00 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a ON clause of a j
61d10 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 oin */. IdLis
61d20 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 t *pUsing; /*
61d30 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 The USING clause
61d40 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 of a join */.
61d50 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 Bitmask colUse
61d60 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c d; /* Bit N (1<
61d70 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d <N) set if colum
61d80 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 75 n N of pTab is u
61d90 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 sed */. char
61da0 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20 *zIndex; /*
61db0 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d 20 Identifier from
61dc0 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 6e "INDEXED BY <zIn
61dd0 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a dex>" clause */.
61de0 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 Index *pInde
61df0 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 x; /* Index s
61e00 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 70 tructure corresp
61e10 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78 onding to zIndex
61e20 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20 , if any */. }
61e30 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 a[1];
61e40 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 /* One entry f
61e50 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 69 or each identifi
61e60 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a er on the list *
61e70 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d /.};../*.** Perm
61e80 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 20 itted values of
61e90 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f the SrcList.a.jo
61ea0 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a intype field.*/.
61eb0 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52 #define JT_INNER
61ec0 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 2f 0x0001 /
61ed0 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e * Any kind of in
61ee0 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69 ner or cross joi
61ef0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f n */.#define JT_
61f00 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 32 CROSS 0x0002
61f10 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20 /* Explicit
61f20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53 use of the CROSS
61f30 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 keyword */.#def
61f40 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 ine JT_NATURAL
61f50 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72 0x0004 /* Tr
61f60 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61 ue for a "natura
61f70 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 l" join */.#defi
61f80 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20 ne JT_LEFT
61f90 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66 0x0008 /* Lef
61fa0 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a t outer join */.
61fb0 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 54 #define JT_RIGHT
61fc0 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 2f 0x0010 /
61fd0 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a 6f * Right outer jo
61fe0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
61ff0 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 32 _OUTER 0x002
62000 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 54 0 /* The "OUT
62010 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 70 ER" keyword is p
62020 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e resent */.#defin
62030 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 30 e JT_ERROR 0
62040 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e x0040 /* unkn
62050 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 own or unsupport
62060 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a ed join type */.
62070 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50 ../*.** A WhereP
62080 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 lan object holds
62090 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 information tha
620a0 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c 6f t describes a lo
620b0 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 79 okup.** strategy
620c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a ..**.** This obj
620d0 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ect is intended
620e0 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 74 to be opaque out
620f0 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 72 side of the wher
62100 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49 e.c module..** I
62110 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 65 t is included he
62120 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20 re only so that
62130 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 69 that compiler wi
62140 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20 ll know how big
62150 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20 it.** is. None
62160 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 6e of the fields in
62170 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 6f this object sho
62180 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 73 uld be used outs
62190 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68 ide of.** the wh
621a0 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a ere.c module..**
621b0 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 75 .** Within the u
621c0 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e nion, pIdx is on
621d0 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 ly used when wsF
621e0 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 58 lags&WHERE_INDEX
621f0 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70 ED is true..** p
62200 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65 Term is only use
62210 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57 d when wsFlags&W
62220 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73 HERE_MULTI_OR is
62230 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 61 true. And pVta
62240 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 bIdx.** is only
62250 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 used when wsFlag
62260 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 s&WHERE_VIRTUALT
62270 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 49 ABLE is true. I
62280 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a 2a t is never the.*
62290 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 65 * case that more
622a0 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 than one of the
622b0 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 se conditions is
622c0 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 true..*/.struct
622d0 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75 WherePlan {. u
622e0 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 20 32 wsFlags;
622f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
62300 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 74 WHERE_* flags t
62310 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 hat describe the
62320 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 75 strategy */. u
62330 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20 32 nEq;
62340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
62350 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f Number of == co
62360 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75 nstraints */. u
62370 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78 nion {. Index
62380 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 *pIdx;
62390 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
623a0 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 4e ex when WHERE_IN
623b0 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a 2f DEXED is true */
623c0 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 72 . struct Wher
623d0 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 eTerm *pTerm;
623e0 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 /* WHERE cla
623f0 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d use term for OR-
62400 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 71 search */. sq
62410 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f lite3_index_info
62420 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a 20 *pVtabIdx; /*
62430 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e Virtual table in
62440 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 dex to use */.
62450 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 } u;.};../*.** F
62460 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 6c or each nested l
62470 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 63 oop in a WHERE c
62480 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61 lause implementa
62490 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 49 tion, the WhereI
624a0 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 nfo.** structure
624b0 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 contains a sing
624c0 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 le instance of t
624d0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
624e0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a This structure.*
624f0 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f * is intended to
62500 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 20 be private the
62510 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 the where.c modu
62520 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f le and should no
62530 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f t be.** access o
62540 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f 74 r modified by ot
62550 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a her modules..**.
62560 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 ** The pIdxInfo
62570 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f field is used to
62580 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62 help pick the b
62590 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a est index on a.*
625a0 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e * virtual table.
625b0 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70 The pIdxInfo p
625c0 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 ointer contains
625d0 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f indexing.** info
625e0 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 rmation for the
625f0 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 68 i-th table in th
62600 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 65 e FROM clause be
62610 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 2e fore reordering.
62620 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 78 .** All the pIdx
62630 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 72 Info pointers ar
62640 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 65 e freed by where
62650 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 68 InfoFree() in wh
62660 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 ere.c..** All ot
62670 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 her information
62680 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 72 in the i-th Wher
62690 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f eLevel object fo
626a0 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 r the i-th table
626b0 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 63 .** after FROM c
626c0 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a lause ordering..
626d0 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4c */.struct WhereL
626e0 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c evel {. WherePl
626f0 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f an plan; /
62700 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 * query plan for
62710 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66 this element of
62720 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
62730 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a */. int iLeftJ
62740 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d oin; /* M
62750 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 emory cell used
62760 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46 to implement LEF
62770 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a T OUTER JOIN */.
62780 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 20 int iTabCur;
62790 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
627a0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 DBE cursor used
627b0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 61 to access the ta
627c0 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 ble */. int iId
627d0 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f xCur; /
627e0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
627f0 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 r used to access
62800 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61 pIdx */. int a
62810 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20 ddrBrk;
62820 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f /* Jump here to
62830 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 break out of th
62840 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 e loop */. int
62850 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20 addrNxt;
62860 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
62870 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 74 o start the next
62880 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 IN combination
62890 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e */. int addrCon
628a0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 t; /* Ju
628b0 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 mp here to conti
628c0 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 nue with the nex
628d0 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a t loop cycle */.
628e0 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 3b int addrFirst;
628f0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
62900 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 instruction of
62910 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20 interior of the
62920 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 72 loop */. u8 iFr
62930 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 om;
62940 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 /* Which entry i
62950 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
62960 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35 e */. u8 op, p5
62970 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
62980 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f 66 Opcode and P5 of
62990 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74 the opcode that
629a0 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a ends the loop *
629b0 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b 20 /. int p1, p2;
629c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
629d0 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 63 rands of the opc
629e0 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 73 ode used to ends
629f0 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 the loop */. u
62a00 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 nion {
62a10 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /* Informat
62a20 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 73 ion that depends
62a30 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 on plan.wsFlags
62a40 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 7b */. struct {
62a50 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20 . int nIn;
62a60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
62a70 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
62a80 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a s in aInLoop[] *
62a90 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49 /. struct I
62aa0 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 20 nLoop {.
62ab0 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 int iCur;
62ac0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 /* The VD
62ad0 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 62 BE cursor used b
62ae0 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 74 y this IN operat
62af0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e or */. in
62b00 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 20 t addrInTop;
62b10 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 /* Top of t
62b20 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 he IN loop */.
62b30 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20 } *aInLoop;
62b40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 /* Inf
62b50 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 ormation about e
62b60 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f 70 ach nested IN op
62b70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20 erator */. }
62b80 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 in;
62b90 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
62ba0 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 plan.wsFlags&WH
62bb0 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20 ERE_IN_ABLE */.
62bc0 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 } u;.. /* The
62bd0 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 following field
62be0 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 is really not pa
62bf0 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e rt of the curren
62c00 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 t level. But.
62c10 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 ** we need a pla
62c20 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74 ce to cache virt
62c30 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 ual table index
62c40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
62c50 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 each. ** virtua
62c60 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 l table in the F
62c70 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74 ROM clause and t
62c80 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74 he WhereLevel st
62c90 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 ructure is. **
62ca0 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 a convenient pla
62cb0 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 ce since there i
62cc0 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c s one WhereLevel
62cd0 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63 for each FROM c
62ce0 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 lause. ** eleme
62cf0 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 nt.. */. sqlit
62d00 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 e3_index_info *p
62d10 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 IdxInfo; /* Ind
62d20 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 ex info for n-th
62d30 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f source table */
62d40 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 .};../*.** Flags
62d50 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 appropriate for
62d60 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20 the wctrlFlags
62d70 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c parameter of sql
62d80 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
62d90 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 .** and the Wher
62da0 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73 eInfo.wctrlFlags
62db0 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 member..*/.#def
62dc0 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 ine WHERE_ORDERB
62dd0 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30 Y_NORMAL 0x000
62de0 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64 0 /* No-op */.#d
62df0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 efine WHERE_ORDE
62e00 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30 RBY_MIN 0x0
62e10 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 001 /* ORDER BY
62e20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d processing for m
62e30 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 in() func */.#de
62e40 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 fine WHERE_ORDER
62e50 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30 BY_MAX 0x00
62e60 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 02 /* ORDER BY p
62e70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61 rocessing for ma
62e80 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 x() func */.#def
62e90 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 ine WHERE_ONEPAS
62ea0 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30 S_DESIRED 0x000
62eb0 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20 4 /* Want to do
62ec0 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f one-pass UPDATE/
62ed0 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e DELETE */.#defin
62ee0 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 e WHERE_DUPLICAT
62ef0 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20 ES_OK 0x0008
62f00 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20 /* Ok to return
62f10 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20 a row more than
62f20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 once */.#define
62f30 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 WHERE_OMIT_OPEN
62f40 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f 2a 0x0010 /*
62f50 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72 Table cursor ar
62f60 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a e already open *
62f70 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
62f80 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20 OMIT_CLOSE
62f90 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20 0x0020 /* Omit
62fa0 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 26 close of table &
62fb0 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a index cursors *
62fc0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
62fd0 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 20 FORCE_TABLE
62fe0 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f 0x0040 /* Do no
62ff0 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f t use an index-o
63000 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f nly search */../
63010 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 63 *.** The WHERE c
63020 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 lause processing
63030 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 6f routine has two
63040 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a halves. The.**
63050 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 73 first part does
63060 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
63070 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64 e WHERE loop and
63080 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 the second.** h
63090 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 69 alf does the tai
630a0 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c l of the WHERE l
630b0 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63 oop. An instanc
630c0 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 e of.** this str
630d0 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 6e ucture is return
630e0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ed by the first
630f0 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 0a half and passed.
63100 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 6f ** into the seco
63110 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 20 nd half to give
63120 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e some continuity.
63130 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 .*/.struct Where
63140 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a Info {. Parse *
63150 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a pParse; /*
63160 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 Parsing and cod
63170 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
63180 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77 63 text */. u16 wc
63190 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 2f trlFlags; /
631a0 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c * Flags original
631b0 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c ly passed to sql
631c0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
631d0 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61 */. u8 okOnePa
631e0 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b ss; /* Ok
631f0 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73 73 to use one-pass
63200 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 55 algorithm for U
63210 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 PDATE or DELETE
63220 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 */. SrcList *pT
63230 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 abList;
63240 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 /* List of t
63250 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 ables in the joi
63260 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b n */. int iTop;
63270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63280 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72 /* The ver
63290 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 y beginning of t
632a0 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f he WHERE loop */
632b0 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 . int iContinue
632c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
632d0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
632e0 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 o continue with
632f0 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 next record */.
63300 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20 int iBreak;
63310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63320 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 /* Jump here to
63330 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 break out of the
63340 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e loop */. int n
63350 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 Level;
63360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
63370 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f ber of nested lo
63380 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57 op */. struct W
63390 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b hereClause *pWC;
633a0 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 /* Decomp
633b0 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 osition of the W
633c0 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
633d0 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d WhereLevel a[1]
633e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
633f0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
63400 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c bout each nest l
63410 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a oop in WHERE */.
63420 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 };../*.** A Name
63430 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20 Context defines
63440 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 a context in whi
63450 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 ch to resolve ta
63460 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a ble and column.*
63470 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f * names. The co
63480 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f ntext consists o
63490 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c f a list of tabl
634a0 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74 es (the pSrcList
634b0 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 ) field and.** a
634c0 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 list of named e
634d0 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 xpression (pELis
634e0 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65 t). The named e
634f0 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d xpression list m
63500 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 ay.** be NULL.
63510 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70 The pSrc corresp
63520 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d onds to the FROM
63530 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c clause of a SEL
63540 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 ECT or.** to the
63550 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65 table being ope
63560 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45 rated on by INSE
63570 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 RT, UPDATE, or D
63580 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70 ELETE. The.** p
63590 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 EList correspond
635a0 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 s to the result
635b0 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 set of a SELECT
635c0 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a and is NULL for.
635d0 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 ** other stateme
635e0 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 nts..**.** NameC
635f0 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e ontexts can be n
63600 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73 ested. When res
63610 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68 olving names, th
63620 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a e inner-most .**
63630 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72 context is sear
63640 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20 ched first. If
63650 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e no match is foun
63660 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65 d, the next oute
63670 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 r.** context is
63680 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65 checked. If the
63690 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d re is still no m
636a0 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63 atch, the next c
636b0 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65 ontext.** is che
636c0 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63 cked. This proc
636d0 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e ess continues un
636e0 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74 til either a mat
636f0 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f ch is found.** o
63700 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61 r all contexts a
63710 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20 re check. When
63720 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 a match is found
63730 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65 , the nRef membe
63740 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 r of.** the cont
63750 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ext containing t
63760 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72 he match is incr
63770 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 emented. .**.**
63780 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65 Each subquery ge
63790 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e ts a new NameCon
637a0 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74 text. The pNext
637b0 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f field points to
637c0 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 the.** NameCont
637d0 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e ext in the paren
637e0 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74 t query. Thus t
637f0 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63 he process of sc
63800 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 anning the.** Na
63810 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 meContext list c
63820 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 orresponds to se
63830 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20 arching through
63840 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74 successively out
63850 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 er.** subqueries
63860 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d looking for a m
63870 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 atch..*/.struct
63880 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 NameContext {.
63890 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 Parse *pParse;
638a0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 /* The pars
638b0 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 er */. SrcList
638c0 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20 *pSrcList; /*
638d0 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c One or more tabl
638e0 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c es used to resol
638f0 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78 ve names */. Ex
63900 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
63910 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c /* Optional l
63920 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 ist of named exp
63930 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e ressions */. in
63940 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
63950 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
63960 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62 names resolved b
63970 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a y this context *
63980 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 /. int nErr;
63990 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
639a0 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 er of errors enc
639b0 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72 ountered while r
639c0 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a esolving names *
639d0 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b /. u8 allowAgg;
639e0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 /* Aggr
639f0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
63a00 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a allowed here */.
63a10 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20 u8 hasAgg;
63a20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
63a30 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 f aggregates are
63a40 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73 seen */. u8 is
63a50 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20 Check;
63a60 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c /* True if resol
63a70 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20 ving names in a
63a80 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 CHECK constraint
63a90 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68 */. int nDepth
63aa0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 ; /* De
63ab0 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20 pth of subquery
63ac0 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72 recursion. 1 for
63ad0 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f no recursion */
63ae0 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 . AggInfo *pAgg
63af0 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 Info; /* Infor
63b00 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67 mation about agg
63b10 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20 regates at this
63b20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 level */. NameC
63b30 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20 ontext *pNext;
63b40 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61 /* Next outer na
63b50 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c me context. NUL
63b60 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20 L for outermost
63b70 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
63b80 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
63b90 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
63ba0 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c ure contains all
63bb0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
63bc0 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61 needed to genera
63bd0 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 te code for a si
63be0 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 ngle SELECT stat
63bf0 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 ement..**.** nLi
63c00 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31 mit is set to -1
63c10 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
63c20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e LIMIT clause. n
63c30 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f Offset is set to
63c40 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 0..** If there
63c50 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 is a LIMIT claus
63c60 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65 e, the parser se
63c70 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65 ts nLimit to the
63c80 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a value of the.**
63c90 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 limit and nOffs
63ca0 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 et to the value
63cb0 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f of the offset (o
63cc0 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 r 0 if there is
63cd0 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20 not.** offset).
63ce0 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e But later on, n
63cf0 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 Limit and nOffse
63d00 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d t become the mem
63d10 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a ory locations.**
63d20 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61 in the VDBE tha
63d30 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d t record the lim
63d40 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f it and offset co
63d50 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 unters..**.** ad
63d60 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 drOpenEphm[] ent
63d70 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 ries contain the
63d80 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f address of OP_O
63d90 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 penEphemeral opc
63da0 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61 odes..** These a
63db0 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65 ddresses must be
63dc0 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20 stored so that
63dd0 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61 we can go back a
63de0 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 nd fill in.** th
63df0 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 e P4_KEYINFO and
63e00 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c P2 parameters l
63e10 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74 ater. Neither t
63e20 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a he KeyInfo nor.*
63e30 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
63e40 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61 columns in P2 ca
63e50 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 n be computed at
63e60 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a the same time.*
63e70 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e * as the OP_Open
63e80 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e Ephm instruction
63e90 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73 is coded becaus
63ea0 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 e not.** enough
63eb0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
63ec0 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 t the compound q
63ed0 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 uery is known at
63ee0 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 that point..**
63ef0 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 The KeyInfo for
63f00 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 addrOpenTran[0]
63f10 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 and [1] contains
63f20 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
63f30 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20 nces.** for the
63f40 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65 result set. The
63f50 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 KeyInfo for add
63f60 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e rOpenTran[2] con
63f70 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a tains collating.
63f80 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72 ** sequences for
63f90 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
63fa0 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ause..*/.struct
63fb0 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c Select {. ExprL
63fc0 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 ist *pEList;
63fd0 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20 /* The fields
63fe0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f of the result */
63ff0 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 . u8 op;
64000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
64010 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b of: TK_UNION TK
64020 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43 _ALL TK_INTERSEC
64030 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20 T TK_EXCEPT */.
64040 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 char affinity;
64050 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52 /* MakeR
64060 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20 ecord with this
64070 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54 affinity for SRT
64080 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65 _Set */. u16 se
64090 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 lFlags;
640a0 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a /* Various SF_*
640b0 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63 values */. Src
640c0 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 List *pSrc;
640d0 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 /* The FROM
640e0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
640f0 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 *pWhere;
64100 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 /* The WHERE
64110 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
64120 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 List *pGroupBy;
64130 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 /* The GROUP
64140 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 BY clause */. E
64150 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 xpr *pHaving;
64160 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 /* The HAV
64170 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ING clause */.
64180 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
64190 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 By; /* The OR
641a0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
641b0 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f . Select *pPrio
641c0 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 r; /* Pri
641d0 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 or select in a c
641e0 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 ompound select s
641f0 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 tatement */. Se
64200 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 lect *pNext;
64210 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c /* Next sel
64220 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20 ect to the left
64230 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f in a compound */
64240 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68 . Select *pRigh
64250 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 tmost; /* Rig
64260 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 ht-most select i
64270 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c n a compound sel
64280 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f ect statement */
64290 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b . Expr *pLimit;
642a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d /* LIM
642b0 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e IT expression. N
642c0 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 ULL means not us
642d0 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ed. */. Expr *p
642e0 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
642f0 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 /* OFFSET expres
64300 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 sion. NULL means
64310 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 not used. */.
64320 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 int iLimit, iOff
64330 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 set; /* Memory
64340 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 registers holdi
64350 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45 ng LIMIT & OFFSE
64360 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 T counters */.
64370 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d int addrOpenEphm
64380 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 [3]; /* OP_Ope
64390 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72 nEphem opcodes r
643a0 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73 elated to this s
643b0 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a elect */.};../*.
643c0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
643d0 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c s for Select.sel
643e0 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22 Flags. The "SF"
643f0 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66 prefix stands f
64400 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c or.** "Select Fl
64410 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ag"..*/.#define
64420 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20 SF_Distinct
64430 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 0x0001 /* Ou
64440 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44 tput should be D
64450 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 ISTINCT */.#defi
64460 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 ne SF_Resolved
64470 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 0x0002 /*
64480 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76 Identifiers hav
64490 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 e been resolved
644a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67 */.#define SF_Ag
644b0 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78 gregate 0x
644c0 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 0004 /* Contain
644d0 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 s aggregate func
644e0 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 tions */.#define
644f0 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 SF_UsesEphemera
64500 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55 l 0x0008 /* U
64510 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65 ses the OpenEphe
64520 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a meral opcode */.
64530 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e #define SF_Expan
64540 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31 ded 0x001
64550 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 0 /* sqlite3Sel
64560 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c ectExpand() call
64570 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 ed on this */.#d
64580 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65 efine SF_HasType
64590 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20 Info 0x0020
645a0 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72 /* FROM subquer
645b0 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d ies have Table m
645c0 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a etadata */.../*.
645d0 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f ** The results o
645e0 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62 f a select can b
645f0 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e e distributed in
64600 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20 several ways.
64610 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65 The.** "SRT" pre
64620 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43 fix means "SELEC
64630 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a T Result Type"..
64640 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55 */.#define SRT_U
64650 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f nion 1 /
64660 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 * Store result a
64670 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 s keys in an ind
64680 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 ex */.#define SR
64690 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32 T_Except 2
646a0 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75 /* Remove resu
646b0 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20 lt from a UNION
646c0 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 index */.#define
646d0 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 SRT_Exists
646e0 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 3 /* Store 1
646f0 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 if the result is
64700 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64 not empty */.#d
64710 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72 efine SRT_Discar
64720 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20 d 4 /* Do
64730 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73 not save the res
64740 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f ults anywhere */
64750 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 ../* The ORDER B
64760 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f Y clause is igno
64770 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 red for all of t
64780 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 he above */.#def
64790 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 ine IgnorableOrd
647a0 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65 erby(X) ((X->eDe
647b0 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64 st)<=SRT_Discard
647c0 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f )..#define SRT_O
647d0 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f utput 5 /
647e0 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f * Output each ro
647f0 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23 w of result */.#
64800 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 define SRT_Mem
64810 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6 /* St
64820 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ore result in a
64830 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23 memory cell */.#
64840 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20 define SRT_Set
64850 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74 7 /* St
64860 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b ore results as k
64870 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 eys in an index
64880 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54 */.#define SRT_T
64890 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f able 8 /
648a0 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 * Store result a
648b0 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61 s data with an a
648c0 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a utomatic rowid *
648d0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70 /.#define SRT_Ep
648e0 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a hemTab 9 /*
648f0 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e Create transien
64900 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20 t tab and store
64910 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a like SRT_Table *
64920 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f /.#define SRT_Co
64930 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a routine 10 /*
64940 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67 Generate a sing
64950 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 le row of result
64960 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 */../*.** A str
64970 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63 ucture used to c
64980 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68 ustomize the beh
64990 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 avior of sqlite3
649a0 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a Select(). See.**
649b0 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 comments above
649c0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 sqlite3Select()
649d0 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a for details..*/.
649e0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
649f0 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74 electDest Select
64a00 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c Dest;.struct Sel
64a10 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65 ectDest {. u8 e
64a20 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Dest; /*
64a30 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 How to dispose
64a40 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a of the results *
64a50 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b /. u8 affinity;
64a60 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 /* Affinit
64a70 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73 y used when eDes
64a80 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 t==SRT_Set */.
64a90 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20 int iParm;
64aa0 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72 /* A parameter
64ab0 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65 used by the eDe
64ac0 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 st disposal meth
64ad0 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d od */. int iMem
64ae0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 ; /* Bas
64af0 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 e register where
64b00 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69 results are wri
64b10 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d tten */. int nM
64b20 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e em; /* N
64b30 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 umber of registe
64b40 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a rs allocated */.
64b50 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f };../*.** Size o
64b60 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 f the column cac
64b70 68 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 he.*/.#ifndef SQ
64b80 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 0a LITE_N_COLCACHE.
64b90 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
64ba0 4e 5f 43 4f 4c 43 41 43 48 45 20 31 30 0a 23 65 N_COLCACHE 10.#e
64bb0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 ndif../*.** An S
64bc0 51 4c 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 QL parser contex
64bd0 74 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68 t. A copy of th
64be0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
64bf0 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a passed through.*
64c00 2a 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 * the parser and
64c10 20 64 6f 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 down into all t
64c20 68 65 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e he parser action
64c30 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 routine in orde
64c40 72 20 74 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72 r to.** carry ar
64c50 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ound information
64c60 20 74 68 61 74 20 69 73 20 67 6c 6f 62 61 6c 20 that is global
64c70 74 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 to the entire pa
64c80 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 rse..**.** The s
64c90 74 72 75 63 74 75 72 65 20 69 73 20 64 69 76 69 tructure is divi
64ca0 64 65 64 20 69 6e 74 6f 20 74 77 6f 20 70 61 72 ded into two par
64cb0 74 73 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 ts. When the pa
64cc0 72 73 65 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a rser and code.**
64cd0 20 67 65 6e 65 72 61 74 65 20 63 61 6c 6c 20 74 generate call t
64ce0 68 65 6d 73 65 6c 76 65 73 20 72 65 63 75 72 73 hemselves recurs
64cf0 69 76 65 6c 79 2c 20 74 68 65 20 66 69 72 73 74 ively, the first
64d00 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 74 72 part of the str
64d10 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e ucture.** is con
64d20 73 74 61 6e 74 20 62 75 74 20 74 68 65 20 73 65 stant but the se
64d30 63 6f 6e 64 20 70 61 72 74 20 69 73 20 72 65 73 cond part is res
64d40 65 74 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e et at the beginn
64d50 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a ing and end of.*
64d60 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e * each recursion
64d70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62 ..**.** The nTab
64d80 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c leLock and aTabl
64d90 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 73 20 eLock variables
64da0 61 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 are only used if
64db0 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 the shared-cach
64dc0 65 20 0a 2a 2a 20 66 65 61 74 75 72 65 20 69 73 e .** feature is
64dd0 20 65 6e 61 62 6c 65 64 20 28 69 66 20 73 71 6c enabled (if sql
64de0 69 74 65 33 54 73 64 28 29 2d 3e 75 73 65 53 68 ite3Tsd()->useSh
64df0 61 72 65 64 44 61 74 61 20 69 73 20 74 72 75 65 aredData is true
64e00 29 2e 20 54 68 65 79 20 61 72 65 0a 2a 2a 20 75 ). They are.** u
64e10 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 sed to store the
64e20 20 73 65 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f set of table-lo
64e30 63 6b 73 20 72 65 71 75 69 72 65 64 20 62 79 20 cks required by
64e40 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 the statement be
64e50 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e ing.** compiled.
64e60 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 Function sqlite
64e70 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20 3TableLock() is
64e80 75 73 65 64 20 74 6f 20 61 64 64 20 65 6e 74 72 used to add entr
64e90 69 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 ies to the.** li
64ea0 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 st..*/.struct Pa
64eb0 72 73 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 rse {. sqlite3
64ec0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
64ed0 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 The main databas
64ee0 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
64ef0 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
64f00 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
64f10 63 6f 64 65 20 66 72 6f 6d 20 65 78 65 63 75 74 code from execut
64f20 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ion */. char *z
64f30 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 2f 2a ErrMsg; /*
64f40 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 An error messag
64f50 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 56 64 e */. Vdbe *pVd
64f60 62 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 be; /* A
64f70 6e 20 65 6e 67 69 6e 65 20 66 6f 72 20 65 78 65 n engine for exe
64f80 63 75 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 cuting database
64f90 62 79 74 65 63 6f 64 65 20 2a 2f 0a 20 20 75 38 bytecode */. u8
64fa0 20 63 6f 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20 colNamesSet;
64fb0 20 20 20 2f 2a 20 54 52 55 45 20 61 66 74 65 72 /* TRUE after
64fc0 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 OP_ColumnName h
64fd0 61 73 20 62 65 65 6e 20 69 73 73 75 65 64 20 74 as been issued t
64fe0 6f 20 70 56 64 62 65 20 2a 2f 0a 20 20 75 38 20 o pVdbe */. u8
64ff0 6e 61 6d 65 43 6c 61 73 68 3b 20 20 20 20 20 20 nameClash;
65000 20 20 2f 2a 20 41 20 70 65 72 6d 61 6e 65 6e 74 /* A permanent
65010 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6c 61 73 table name clas
65020 68 65 73 20 77 69 74 68 20 74 65 6d 70 20 74 61 hes with temp ta
65030 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 38 ble name */. u8
65040 20 63 68 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 checkSchema;
65050 20 20 20 2f 2a 20 43 61 75 73 65 73 20 73 63 68 /* Causes sch
65060 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b ema cookie check
65070 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 after an error
65080 2a 2f 0a 20 20 75 38 20 6e 65 73 74 65 64 3b 20 */. u8 nested;
65090 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
650a0 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 ber of nested ca
650b0 6c 6c 73 20 74 6f 20 74 68 65 20 70 61 72 73 65 lls to the parse
650c0 72 2f 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 r/code generator
650d0 20 2a 2f 0a 20 20 75 38 20 70 61 72 73 65 45 72 */. u8 parseEr
650e0 72 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 ror; /* Tr
650f0 75 65 20 61 66 74 65 72 20 61 20 70 61 72 73 69 ue after a parsi
65100 6e 67 20 65 72 72 6f 72 2e 20 20 54 69 63 6b 65 ng error. Ticke
65110 74 20 23 31 37 39 34 20 2a 2f 0a 20 20 75 38 20 t #1794 */. u8
65120 6e 54 65 6d 70 52 65 67 3b 20 20 20 20 20 20 20 nTempReg;
65130 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
65140 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 emporary registe
65150 72 73 20 69 6e 20 61 54 65 6d 70 52 65 67 5b 5d rs in aTempReg[]
65160 20 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 70 49 6e */. u8 nTempIn
65170 55 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 Use; /* Nu
65180 6d 62 65 72 20 6f 66 20 61 54 65 6d 70 52 65 67 mber of aTempReg
65190 5b 5d 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 [] currently che
651a0 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e cked out */. in
651b0 74 20 61 54 65 6d 70 52 65 67 5b 38 5d 3b 20 20 t aTempReg[8];
651c0 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 61 72 /* Holding ar
651d0 65 61 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 ea for temporary
651e0 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 registers */.
651f0 69 6e 74 20 6e 52 61 6e 67 65 52 65 67 3b 20 20 int nRangeReg;
65200 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
65210 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 72 65 the temporary re
65220 67 69 73 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a gister block */.
65230 20 20 69 6e 74 20 69 52 61 6e 67 65 52 65 67 3b int iRangeReg;
65240 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
65250 72 65 67 69 73 74 65 72 20 69 6e 20 74 65 6d 70 register in temp
65260 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 62 orary register b
65270 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 lock */. int nE
65280 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f rr; /
65290 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f * Number of erro
652a0 72 73 20 73 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 rs seen */. int
652b0 20 6e 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 nTab;
652c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
652d0 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 reviously alloca
652e0 74 65 64 20 56 44 42 45 20 63 75 72 73 6f 72 73 ted VDBE cursors
652f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 */. int nMem;
65300 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
65310 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 mber of memory c
65320 65 6c 6c 73 20 75 73 65 64 20 73 6f 20 66 61 72 ells used so far
65330 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 74 3b 20 */. int nSet;
65340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
65350 6d 62 65 72 20 6f 66 20 73 65 74 73 20 75 73 65 mber of sets use
65360 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e d so far */. in
65370 74 20 63 6b 42 61 73 65 3b 20 20 20 20 20 20 20 t ckBase;
65380 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73 /* Base regis
65390 74 65 72 20 6f 66 20 64 61 74 61 20 64 75 72 69 ter of data duri
653a0 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 ng check constra
653b0 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 ints */. int iC
653c0 61 63 68 65 4c 65 76 65 6c 3b 20 20 20 20 20 2f acheLevel; /
653d0 2a 20 43 6f 6c 43 61 63 68 65 20 76 61 6c 69 64 * ColCache valid
653e0 20 77 68 65 6e 20 61 43 6f 6c 43 61 63 68 65 5b when aColCache[
653f0 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43 61 63 68 65 ].iLevel<=iCache
65400 4c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 Level */. int i
65410 43 61 63 68 65 43 6e 74 3b 20 20 20 20 20 20 20 CacheCnt;
65420 2f 2a 20 43 6f 75 6e 74 65 72 20 75 73 65 64 20 /* Counter used
65430 74 6f 20 67 65 6e 65 72 61 74 65 20 61 43 6f 6c to generate aCol
65440 43 61 63 68 65 5b 5d 2e 6c 72 75 20 76 61 6c 75 Cache[].lru valu
65450 65 73 20 2a 2f 0a 20 20 75 38 20 6e 43 6f 6c 43 es */. u8 nColC
65460 61 63 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 ache; /*
65470 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
65480 73 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 s in the column
65490 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 69 43 cache */. u8 iC
654a0 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 olCache;
654b0 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 20 6f 66 /* Next entry of
654c0 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 72 65 the cache to re
654d0 70 6c 61 63 65 20 2a 2f 0a 20 20 73 74 72 75 63 place */. struc
654e0 74 20 79 43 6f 6c 43 61 63 68 65 20 7b 0a 20 20 t yColCache {.
654f0 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 int iTable;
65500 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
65510 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a cursor number *
65520 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d /. int iColum
65530 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 n; /* T
65540 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 able column numb
65550 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 61 66 66 er */. u8 aff
65560 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 Change;
65570 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
65580 72 65 67 69 73 74 65 72 20 68 61 73 20 68 61 64 register has had
65590 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 an affinity cha
655a0 6e 67 65 20 2a 2f 0a 20 20 20 20 75 38 20 74 65 nge */. u8 te
655b0 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 mpReg;
655c0 20 2f 2a 20 69 52 65 67 20 69 73 20 61 20 74 65 /* iReg is a te
655d0 6d 70 20 72 65 67 69 73 74 65 72 20 74 68 61 74 mp register that
655e0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72 65 needs to be fre
655f0 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c ed */. int iL
65600 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 evel;
65610 2f 2a 20 4e 65 73 74 69 6e 67 20 6c 65 76 65 6c /* Nesting level
65620 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 65 67 */. int iReg
65630 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
65640 20 52 65 67 20 77 69 74 68 20 76 61 6c 75 65 20 Reg with value
65650 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 2e 20 of this column.
65660 30 20 6d 65 61 6e 73 20 6e 6f 6e 65 2e 20 2a 2f 0 means none. */
65670 0a 20 20 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 . int lru;
65680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
65690 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73 65 ast recently use
656a0 64 20 65 6e 74 72 79 20 68 61 73 20 74 68 65 20 d entry has the
656b0 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20 2a smallest value *
656c0 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 63 68 65 5b /. } aColCache[
656d0 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
656e0 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 E]; /* One for
656f0 65 61 63 68 20 63 6f 6c 75 6d 6e 20 63 61 63 68 each column cach
65700 65 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 e entry */. u32
65710 20 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 writeMask;
65720 20 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 /* Start a wri
65730 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te transaction o
65740 6e 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 n these database
65750 73 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 s */. u32 cooki
65760 65 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 eMask; /* B
65770 69 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 itmask of schema
65780 20 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61 verified databa
65790 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f ses */. int coo
657a0 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a kieGoto; /*
657b0 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 Address of OP_G
657c0 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 oto to cookie ve
657d0 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e rifier subroutin
657e0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 e */. int cooki
657f0 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 eValue[SQLITE_MA
65800 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 X_ATTACHED+2];
65810 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f /* Values of coo
65820 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a kies to verify *
65830 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
65840 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
65850 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c HE. int nTableL
65860 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ock; /* N
65870 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 umber of locks i
65880 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a n aTableLock */.
65890 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 TableLock *aTa
658a0 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 bleLock; /* Requ
658b0 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 ired table locks
658c0 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 for shared-cach
658d0 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 e mode */.#endif
658e0 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b . int regRowid;
658f0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
65900 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 ter holding rowi
65910 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c d of CREATE TABL
65920 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 E entry */. int
65930 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 regRoot;
65940 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f /* Register ho
65950 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 lding root page
65960 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f number for new o
65970 62 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 bjects */.. /*
65980 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e Above is constan
65990 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 t between recurs
659a0 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 ions. Below is
659b0 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 reset before and
659c0 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 after. ** each
659d0 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 recursion */..
659e0 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 int nVar;
659f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
65a00 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 of '?' variables
65a10 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c seen in the SQL
65a20 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 so far */. int
65a30 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 nVarExpr;
65a40 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
65a50 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 sed slots in apV
65a60 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e arExpr[] */. in
65a70 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b t nVarExprAlloc;
65a80 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
65a90 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 allocated slots
65aa0 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a in apVarExpr[] *
65ab0 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 /. Expr **apVar
65ac0 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e Expr; /* Poin
65ad0 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 ters to :aaa and
65ae0 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 $aaaa wildcard
65af0 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 expressions */.
65b00 20 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 int nAlias;
65b10 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
65b20 6f 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c of aliased resul
65b30 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f t set columns */
65b40 0a 20 20 69 6e 74 20 6e 41 6c 69 61 73 41 6c 6c . int nAliasAll
65b50 6f 63 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 oc; /* Numbe
65b60 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 r of allocated s
65b70 6c 6f 74 73 20 66 6f 72 20 61 41 6c 69 61 73 5b lots for aAlias[
65b80 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 41 6c 69 ] */. int *aAli
65b90 61 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 as; /* R
65ba0 65 67 69 73 74 65 72 20 75 73 65 64 20 74 6f 20 egister used to
65bb0 68 6f 6c 64 20 61 6c 69 61 73 65 64 20 72 65 73 hold aliased res
65bc0 75 6c 74 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c ult */. u8 expl
65bd0 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ain; /*
65be0 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50 True if the EXP
65bf0 4c 41 49 4e 20 66 6c 61 67 20 69 73 20 66 6f 75 LAIN flag is fou
65c00 6e 64 20 6f 6e 20 74 68 65 20 71 75 65 72 79 20 nd on the query
65c10 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 45 72 72 54 */. Token sErrT
65c20 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a 20 54 68 65 oken; /* The
65c30 20 74 6f 6b 65 6e 20 61 74 20 77 68 69 63 68 20 token at which
65c40 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72 the error occurr
65c50 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4e ed */. Token sN
65c60 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 ameToken; /*
65c70 54 6f 6b 65 6e 20 77 69 74 68 20 75 6e 71 75 61 Token with unqua
65c80 6c 69 66 69 65 64 20 73 63 68 65 6d 61 20 6f 62 lified schema ob
65c90 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 ject name */. T
65ca0 6f 6b 65 6e 20 73 4c 61 73 74 54 6f 6b 65 6e 3b oken sLastToken;
65cb0 20 20 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 /* The last
65cc0 74 6f 6b 65 6e 20 70 61 72 73 65 64 20 2a 2f 0a token parsed */.
65cd0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
65ce0 71 6c 3b 20 20 20 20 2f 2a 20 41 6c 6c 20 53 51 ql; /* All SQ
65cf0 4c 20 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 L text */. cons
65d00 74 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 t char *zTail;
65d10 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 /* All SQL text
65d20 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 past the last s
65d30 65 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 emicolon parsed
65d40 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 */. Table *pNew
65d50 54 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 Table; /* A t
65d60 61 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 able being const
65d70 72 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 ructed by CREATE
65d80 20 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 TABLE */. Trig
65d90 67 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 ger *pNewTrigger
65da0 3b 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 ; /* Trigger
65db0 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 under construct
65dc0 20 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 by a CREATE TRI
65dd0 47 47 45 52 20 2a 2f 0a 20 20 54 72 69 67 67 65 GGER */. Trigge
65de0 72 53 74 61 63 6b 20 2a 74 72 69 67 53 74 61 63 rStack *trigStac
65df0 6b 3b 20 20 2f 2a 20 54 72 69 67 67 65 72 20 61 k; /* Trigger a
65e00 63 74 69 6f 6e 73 20 62 65 69 6e 67 20 63 6f 64 ctions being cod
65e10 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ed */. const ch
65e20 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 ar *zAuthContext
65e30 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72 ; /* The 6th par
65e40 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41 ameter to db->xA
65e50 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f uth callbacks */
65e60 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
65e70 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
65e80 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20 E. Token sArg;
65e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
65ea0 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 * Complete text
65eb0 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75 of a module argu
65ec0 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63 ment */. u8 dec
65ed0 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 20 lareVtab;
65ee0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
65ef0 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64 inside sqlite3_d
65f00 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f eclare_vtab() */
65f10 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b . int nVtabLock
65f20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
65f30 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 Number of virtu
65f40 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 al tables to loc
65f50 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61 k */. Table **a
65f60 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 pVtabLock;
65f70 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
65f80 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e virtual tables n
65f90 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a eeding locking *
65fa0 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e /.#endif. int n
65fb0 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 Height;
65fc0 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e /* Expression
65fd0 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 tree height of
65fe0 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 current sub-sele
65ff0 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 ct */. Table *p
66000 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20 ZombieTab;
66010 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 /* List of Table
66020 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 objects to dele
66030 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 te after code ge
66040 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 n */.};..#ifdef
66050 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
66060 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69 UALTABLE. #defi
66070 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 ne IN_DECLARE_VT
66080 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 AB 0.#else. #de
66090 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f fine IN_DECLARE_
660a0 56 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65 VTAB (pParse->de
660b0 63 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69 clareVtab).#endi
660c0 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 f../*.** An inst
660d0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
660e0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
660f0 63 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20 can be declared
66100 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75 on a stack and u
66110 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 sed.** to save t
66120 68 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f he Parse.zAuthCo
66130 6e 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74 ntext value so t
66140 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 hat it can be re
66150 73 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f stored later..*/
66160 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 .struct AuthCont
66170 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 ext {. const ch
66180 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 ar *zAuthContext
66190 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64 ; /* Put saved
661a0 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 Parse.zAuthCont
661b0 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 ext here */. Pa
661c0 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 rse *pParse;
661d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
661e0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 Parse structure
661f0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 */.};../*.** Bi
66200 74 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 tfield flags for
66210 20 50 35 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f P5 value in OP_
66220 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 Insert and OP_De
66230 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lete.*/.#define
66240 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 OPFLAG_NCHANGE
66250 20 20 31 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 1 /* Set to
66260 20 75 70 64 61 74 65 20 64 62 2d 3e 6e 43 68 61 update db->nCha
66270 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f nge */.#define O
66280 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 PFLAG_LASTROWID
66290 20 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 2 /* Set to
662a0 75 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 update db->lastR
662b0 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 owid */.#define
662c0 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 OPFLAG_ISUPDATE
662d0 20 20 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f 4 /* This O
662e0 50 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73 P_Insert is an s
662f0 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 ql UPDATE */.#de
66300 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 fine OPFLAG_APPE
66310 4e 44 20 20 20 20 20 38 20 20 20 20 2f 2a 20 54 ND 8 /* T
66320 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f his is likely to
66330 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f be an append */
66340 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
66350 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 31 36 USESEEKRESULT 16
66360 20 20 20 20 2f 2a 20 54 72 79 20 74 6f 20 61 76 /* Try to av
66370 6f 69 64 20 61 20 73 65 65 6b 20 69 6e 20 42 74 oid a seek in Bt
66380 72 65 65 49 6e 73 65 72 74 28 29 20 2a 2f 0a 0a reeInsert() */..
66390 2f 2a 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 /*. * Each trigg
663a0 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 er present in th
663b0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
663c0 61 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 a is stored as a
663d0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a n instance of. *
663e0 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 2e struct Trigger.
663f0 20 0a 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 . *. * Pointers
66400 20 74 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 to instances of
66410 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 struct Trigger
66420 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 are stored in tw
66430 6f 20 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e o ways.. * 1. In
66440 20 74 68 65 20 22 74 72 69 67 48 61 73 68 22 20 the "trigHash"
66450 68 61 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 hash table (part
66460 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a of the sqlite3*
66470 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 that represents
66480 20 74 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 the . * data
66490 62 61 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f base). This allo
664a0 77 73 20 54 72 69 67 67 65 72 20 73 74 72 75 63 ws Trigger struc
664b0 74 75 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 tures to be retr
664c0 69 65 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 ieved by name..
664d0 2a 20 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 * 2. All trigger
664e0 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
664f0 68 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 h a single table
66500 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c form a linked l
66510 69 73 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 ist, using the.
66520 2a 20 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 * pNext membe
66530 72 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 r of struct Trig
66540 67 65 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 ger. A pointer t
66550 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d o the first elem
66560 65 6e 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 ent of the. *
66570 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 linked list is
66580 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 stored as the "p
66590 54 72 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 Trigger" member
665a0 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 of the associate
665b0 64 0a 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 d. * struct T
665c0 61 62 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 able.. *. * The
665d0 22 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 "step_list" memb
665e0 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 er points to the
665f0 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
66600 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a f a linked list.
66610 20 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 * containing th
66620 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 e SQL statements
66630 20 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68 specified as th
66640 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 e trigger progra
66650 6d 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 m.. */.struct Tr
66660 69 67 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a igger {. char *
66670 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 name;
66680 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 /* The name of
66690 20 74 68 65 20 74 72 69 67 67 65 72 20 20 20 20 the trigger
666a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
666b0 20 20 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 */. char *t
666c0 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 able;
666d0 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 /* The table or
666e0 20 76 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 view to which t
666f0 68 65 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 he trigger appli
66700 65 73 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 es */. u8 op;
66710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66720 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c /* One of TK_DEL
66730 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 ETE, TK_UPDATE,
66740 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 TK_INSERT
66750 20 20 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b */. u8 tr_tm;
66760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
66770 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 * One of TRIGGER
66780 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 _BEFORE, TRIGGER
66790 5f 41 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 _AFTER */. Expr
667a0 20 2a 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 *pWhen;
667b0 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 /* The WHEN
667c0 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 clause of the ex
667d0 70 72 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 pression (may be
667e0 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 NULL) */. IdLi
667f0 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 st *pColumns;
66800 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
66810 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c s an UPDATE OF <
66820 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 column-list> tri
66830 67 67 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 gger,.
66840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66850 20 20 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c the <column-l
66860 69 73 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 ist> is stored h
66870 65 72 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e ere */. Token n
66880 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 ameToken;
66890 20 2f 2a 20 54 6f 6b 65 6e 20 63 6f 6e 74 61 69 /* Token contai
668a0 6e 69 6e 67 20 7a 4e 61 6d 65 2e 20 55 73 65 20 ning zName. Use
668b0 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 20 6f during parsing o
668c0 6e 6c 79 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 nly */. Schema
668d0 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 *pSchema;
668e0 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 /* Schema conta
668f0 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 ining the trigge
66900 72 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 r */. Schema *p
66910 54 61 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f TabSchema; /
66920 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e * Schema contain
66930 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f ing the table */
66940 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a . TriggerStep *
66950 73 74 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 step_list; /* Li
66960 6e 6b 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 nk list of trigg
66970 65 72 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 er program steps
66980 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a */.
66990 20 20 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 Trigger *pNext
669a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 ; /* Nex
669b0 74 20 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 t trigger associ
669c0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61 ated with the ta
669d0 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ble */.};../*.**
669e0 20 41 20 74 72 69 67 67 65 72 20 69 73 20 65 69 A trigger is ei
669f0 74 68 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 ther a BEFORE or
66a00 20 61 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 an AFTER trigge
66a10 72 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e r. The followin
66a20 67 20 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 g constants.** d
66a30 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 etermine which.
66a40 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
66a50 61 72 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 are multiple tri
66a60 67 67 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 ggers, you might
66a70 20 6f 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 of some BEFORE
66a80 61 6e 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a and some AFTER..
66a90 2a 2a 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 ** In that cases
66aa0 2c 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 , the constants
66ab0 62 65 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 below can be ORe
66ac0 64 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 d together..*/.#
66ad0 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 define TRIGGER_B
66ae0 45 46 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 EFORE 1.#define
66af0 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 TRIGGER_AFTER
66b00 20 32 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 2../*. * An ins
66b10 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 tance of struct
66b20 54 72 69 67 67 65 72 53 74 65 70 20 69 73 20 75 TriggerStep is u
66b30 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 sed to store a s
66b40 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d ingle SQL statem
66b50 65 6e 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 ent. * that is a
66b60 20 70 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 part of a trigg
66b70 65 72 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a er-program. . *.
66b80 20 2a 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 * Instances of
66b90 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 struct TriggerSt
66ba0 65 70 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e ep are stored in
66bb0 20 61 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 a singly linked
66bc0 20 6c 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a list (linked. *
66bd0 20 75 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 using the "pNex
66be0 74 22 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 t" member) refer
66bf0 65 6e 63 65 64 20 62 79 20 74 68 65 20 22 73 74 enced by the "st
66c00 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 ep_list" member
66c10 6f 66 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 of the . * assoc
66c20 69 61 74 65 64 20 73 74 72 75 63 74 20 54 72 69 iated struct Tri
66c30 67 67 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 gger instance. T
66c40 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 he first element
66c50 20 6f 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c of the linked l
66c60 69 73 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 ist is. * the fi
66c70 72 73 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 rst step of the
66c80 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e trigger-program.
66c90 0a 20 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 . * . * The "op"
66ca0 20 6d 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 member indicate
66cb0 73 20 77 68 65 74 68 65 72 20 74 68 69 73 20 69 s whether this i
66cc0 73 20 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 s a "DELETE", "I
66cd0 4e 53 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 NSERT", "UPDATE"
66ce0 20 6f 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 or. * "SELECT"
66cf0 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d statement. The m
66d00 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f eanings of the o
66d10 74 68 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 ther members is
66d20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 determined by th
66d30 65 20 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 e . * value of "
66d40 6f 70 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a op" as follows:.
66d50 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f *. * (op == TK_
66d60 49 4e 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e INSERT). * orcon
66d70 66 20 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 f -> stores t
66d80 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 he ON CONFLICT a
66d90 6c 67 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c lgorithm. * pSel
66da0 65 63 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 ect -> If this
66db0 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e is an INSERT IN
66dc0 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e TO ... SELECT ..
66dd0 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 . statement, the
66de0 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 n. *
66df0 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 this stores a
66e00 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 pointer to the S
66e10 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
66e20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e Otherwise NULL.
66e30 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e . * target ->
66e40 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 A token holding
66e50 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
66e60 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 table to insert
66e70 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c into.. * pExprL
66e80 69 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69 ist -> If this i
66e90 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f s an INSERT INTO
66ea0 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 ... VALUES ...
66eb0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a statement, then.
66ec0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
66ed0 74 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75 this stores valu
66ee0 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 es to be inserte
66ef0 64 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c d. Otherwise NUL
66f00 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20 L.. * pIdList
66f10 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e -> If this is an
66f20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e INSERT INTO ...
66f30 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e (<column-names>
66f40 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a ) VALUES ... . *
66f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 st
66f60 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 atement, then th
66f70 69 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f is stores the co
66f80 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 lumn-names to be
66f90 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 . *
66fa0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a inserted into..
66fb0 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f *. * (op == TK_
66fc0 44 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65 DELETE). * targe
66fd0 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 t -> A token
66fe0 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 holding the name
66ff0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
67000 20 64 65 6c 65 74 65 20 66 72 6f 6d 2e 0a 20 2a delete from.. *
67010 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 pWhere -> Th
67020 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f e WHERE clause o
67030 66 20 74 68 65 20 44 45 4c 45 54 45 20 73 74 61 f the DELETE sta
67040 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 tement if one is
67050 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 specified.. *
67060 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 Othe
67070 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a rwise NULL.. * .
67080 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 * (op == TK_UPD
67090 41 54 45 29 0a 20 2a 20 74 61 72 67 65 74 20 20 ATE). * target
670a0 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c -> A token hol
670b0 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 ding the name of
670c0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 75 70 the table to up
670d0 64 61 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a date rows of.. *
670e0 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 pWhere -> Th
670f0 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f e WHERE clause o
67100 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 f the UPDATE sta
67110 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 tement if one is
67120 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 specified.. *
67130 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 Othe
67140 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 rwise NULL.. * p
67150 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 ExprList -> A li
67160 73 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e st of the column
67170 73 20 74 6f 20 75 70 64 61 74 65 20 61 6e 64 20 s to update and
67180 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 the expressions
67190 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 20 20 20 to update. *
671a0 20 20 20 20 20 20 20 20 20 20 74 68 65 6d 20 74 them t
671b0 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 33 55 70 o. See sqlite3Up
671c0 64 61 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 date() documenta
671d0 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 tion of "pChange
671e0 73 22 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 s". *
671f0 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 argument.. *
67200 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 . */.struct Trig
67210 67 65 72 53 74 65 70 20 7b 0a 20 20 69 6e 74 20 gerStep {. int
67220 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 op;
67230 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 /* One of TK_DE
67240 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c LETE, TK_UPDATE,
67250 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 TK_INSERT, TK_S
67260 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 6f ELECT */. int o
67270 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 rconf;
67280 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 /* OE_Rollback e
67290 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 tc. */. Trigger
672a0 20 2a 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a *pTrig; /*
672b0 20 54 68 65 20 74 72 69 67 67 65 72 20 74 68 61 The trigger tha
672c0 74 20 74 68 69 73 20 73 74 65 70 20 69 73 20 61 t this step is a
672d0 20 70 61 72 74 20 6f 66 20 2a 2f 0a 0a 20 20 53 part of */.. S
672e0 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
672f0 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 /* Valid for
67300 20 53 45 4c 45 43 54 20 61 6e 64 20 73 6f 6d 65 SELECT and some
67310 74 69 6d 65 73 20 0a 20 20 20 20 20 20 20 20 20 times .
67320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67330 20 49 4e 53 45 52 54 20 73 74 65 70 73 20 28 77 INSERT steps (w
67340 68 65 6e 20 70 45 78 70 72 4c 69 73 74 20 3d 3d hen pExprList ==
67350 20 30 29 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 0) */. Token t
67360 61 72 67 65 74 3b 20 20 20 20 20 20 20 20 2f 2a arget; /*
67370 20 56 61 6c 69 64 20 66 6f 72 20 44 45 4c 45 54 Valid for DELET
67380 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 E, UPDATE, INSER
67390 54 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 T steps */. Exp
673a0 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 r *pWhere;
673b0 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 44 /* Valid for D
673c0 45 4c 45 54 45 2c 20 55 50 44 41 54 45 20 73 74 ELETE, UPDATE st
673d0 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 eps */. ExprLis
673e0 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f 2a t *pExprList; /*
673f0 20 56 61 6c 69 64 20 66 6f 72 20 55 50 44 41 54 Valid for UPDAT
67400 45 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 E statements and
67410 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20 20 20 20 sometimes .
67420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67430 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 73 74 INSERT st
67440 65 70 73 20 28 77 68 65 6e 20 70 53 65 6c 65 63 eps (when pSelec
67450 74 20 3d 3d 20 30 29 20 20 20 20 20 20 20 20 20 t == 0)
67460 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 49 64 */. IdList *pId
67470 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c List; /* Val
67480 69 64 20 66 6f 72 20 49 4e 53 45 52 54 20 73 74 id for INSERT st
67490 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 20 2a 2f atements only */
674a0 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a . TriggerStep *
674b0 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 pNext; /* Next
674c0 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 in the link-list
674d0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 */. TriggerSte
674e0 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 p *pLast; /* La
674f0 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 st element in li
67500 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 nk-list. Valid f
67510 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 or 1st elem only
67520 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 20 2a 20 41 6e */.};../*. * An
67530 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72 instance of str
67540 75 63 74 20 54 72 69 67 67 65 72 53 74 61 63 6b uct TriggerStack
67550 20 73 74 6f 72 65 73 20 69 6e 66 6f 72 6d 61 74 stores informat
67560 69 6f 6e 20 72 65 71 75 69 72 65 64 20 64 75 72 ion required dur
67570 69 6e 67 20 63 6f 64 65 0a 20 2a 20 67 65 6e 65 ing code. * gene
67580 72 61 74 69 6f 6e 20 6f 66 20 61 20 73 69 6e 67 ration of a sing
67590 6c 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 le trigger progr
675a0 61 6d 2e 20 57 68 69 6c 65 20 74 68 65 20 74 72 am. While the tr
675b0 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 69 73 igger program is
675c0 20 62 65 69 6e 67 0a 20 2a 20 63 6f 64 65 64 2c being. * coded,
675d0 20 69 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 its associated
675e0 54 72 69 67 67 65 72 53 74 61 63 6b 20 69 6e 73 TriggerStack ins
675f0 74 61 6e 63 65 20 69 73 20 70 6f 69 6e 74 65 64 tance is pointed
67600 20 74 6f 20 62 79 20 74 68 65 0a 20 2a 20 22 70 to by the. * "p
67610 54 72 69 67 67 65 72 53 74 61 63 6b 22 20 6d 65 TriggerStack" me
67620 6d 62 65 72 20 6f 66 20 74 68 65 20 50 61 72 73 mber of the Pars
67630 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a e structure.. *.
67640 20 2a 20 54 68 65 20 70 54 61 62 20 6d 65 6d 62 * The pTab memb
67650 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 er points to the
67660 20 74 61 62 6c 65 20 74 68 61 74 20 74 72 69 67 table that trig
67670 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 63 gers are being c
67680 6f 64 65 64 20 6f 6e 2e 20 54 68 65 20 0a 20 2a oded on. The . *
67690 20 6e 65 77 49 64 78 20 6d 65 6d 62 65 72 20 63 newIdx member c
676a0 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65 ontains the inde
676b0 78 20 6f 66 20 74 68 65 20 76 64 62 65 20 63 75 x of the vdbe cu
676c0 72 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 rsor that points
676d0 20 61 74 20 74 68 65 20 74 65 6d 70 0a 20 2a 20 at the temp. *
676e0 74 61 62 6c 65 20 74 68 61 74 20 73 74 6f 72 65 table that store
676f0 73 20 74 68 65 20 6e 65 77 2e 2a 20 72 65 66 65 s the new.* refe
67700 72 65 6e 63 65 73 2e 20 49 66 20 6e 65 77 2e 2a rences. If new.*
67710 20 72 65 66 65 72 65 6e 63 65 73 20 61 72 65 20 references are
67720 6e 6f 74 20 76 61 6c 69 64 0a 20 2a 20 66 6f 72 not valid. * for
67730 20 74 68 65 20 74 72 69 67 67 65 72 20 62 65 69 the trigger bei
67740 6e 67 20 63 6f 64 65 64 20 28 66 6f 72 20 65 78 ng coded (for ex
67750 61 6d 70 6c 65 20 61 6e 20 4f 4e 20 44 45 4c 45 ample an ON DELE
67760 54 45 20 74 72 69 67 67 65 72 29 2c 20 74 68 65 TE trigger), the
67770 6e 20 6e 65 77 49 64 78 0a 20 2a 20 69 73 20 73 n newIdx. * is s
67780 65 74 20 74 6f 20 2d 31 2e 20 54 68 65 20 6f 6c et to -1. The ol
67790 64 49 64 78 20 6d 65 6d 62 65 72 20 69 73 20 61 dIdx member is a
677a0 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 6e 65 77 49 nalogous to newI
677b0 64 78 2c 20 66 6f 72 20 6f 6c 64 2e 2a 20 72 65 dx, for old.* re
677c0 66 65 72 65 6e 63 65 73 2e 0a 20 2a 0a 20 2a 20 ferences.. *. *
677d0 54 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 The ON CONFLICT
677e0 70 6f 6c 69 63 79 20 74 6f 20 62 65 20 75 73 65 policy to be use
677f0 64 20 66 6f 72 20 74 68 65 20 74 72 69 67 67 65 d for the trigge
67800 72 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 r program steps
67810 69 73 20 73 74 6f 72 65 64 20 0a 20 2a 20 61 73 is stored . * as
67820 20 74 68 65 20 6f 72 63 6f 6e 66 20 6d 65 6d 62 the orconf memb
67830 65 72 2e 20 49 66 20 74 68 69 73 20 69 73 20 4f er. If this is O
67840 45 5f 44 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 E_Default, then
67850 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 the ON CONFLICT
67860 63 6c 61 75 73 65 20 0a 20 2a 20 73 70 65 63 69 clause . * speci
67870 66 69 65 64 20 66 6f 72 20 69 6e 64 69 76 69 64 fied for individ
67880 75 61 6c 20 74 72 69 67 67 65 72 73 20 73 74 65 ual triggers ste
67890 70 73 20 69 73 20 75 73 65 64 2e 0a 20 2a 0a 20 ps is used.. *.
678a0 2a 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 * struct Trigger
678b0 53 74 61 63 6b 20 68 61 73 20 61 20 22 70 4e 65 Stack has a "pNe
678c0 78 74 22 20 6d 65 6d 62 65 72 2c 20 74 6f 20 61 xt" member, to a
678d0 6c 6c 6f 77 20 6c 69 6e 6b 65 64 20 6c 69 73 74 llow linked list
678e0 73 20 74 6f 20 62 65 0a 20 2a 20 63 6f 6e 73 74 s to be. * const
678f0 72 75 63 74 65 64 2e 20 57 68 65 6e 20 63 6f 64 ructed. When cod
67900 69 6e 67 20 6e 65 73 74 65 64 20 74 72 69 67 67 ing nested trigg
67910 65 72 73 20 28 74 72 69 67 67 65 72 73 20 66 69 ers (triggers fi
67920 72 65 64 20 62 79 20 6f 74 68 65 72 20 74 72 69 red by other tri
67930 67 67 65 72 73 29 0a 20 2a 20 65 61 63 68 20 6e ggers). * each n
67940 65 73 74 65 64 20 74 72 69 67 67 65 72 20 73 74 ested trigger st
67950 6f 72 65 73 20 69 74 73 20 70 61 72 65 6e 74 20 ores its parent
67960 74 72 69 67 67 65 72 27 73 20 54 72 69 67 67 65 trigger's Trigge
67970 72 53 74 61 63 6b 20 61 73 20 74 68 65 20 22 70 rStack as the "p
67980 4e 65 78 74 22 20 0a 20 2a 20 70 6f 69 6e 74 65 Next" . * pointe
67990 72 2e 20 4f 6e 63 65 20 74 68 65 20 6e 65 73 74 r. Once the nest
679a0 65 64 20 74 72 69 67 67 65 72 20 68 61 73 20 62 ed trigger has b
679b0 65 65 6e 20 63 6f 64 65 64 2c 20 74 68 65 20 70 een coded, the p
679c0 4e 65 78 74 20 76 61 6c 75 65 20 69 73 20 72 65 Next value is re
679d0 73 74 6f 72 65 64 0a 20 2a 20 74 6f 20 74 68 65 stored. * to the
679e0 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 20 6d pTriggerStack m
679f0 65 6d 62 65 72 20 6f 66 20 74 68 65 20 50 61 72 ember of the Par
67a00 73 65 20 73 74 75 63 74 75 72 65 20 61 6e 64 20 se stucture and
67a10 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 70 61 coding of the pa
67a20 72 65 6e 74 0a 20 2a 20 74 72 69 67 67 65 72 20 rent. * trigger
67a30 63 6f 6e 74 69 6e 75 65 73 2e 0a 20 2a 0a 20 2a continues.. *. *
67a40 20 42 65 66 6f 72 65 20 61 20 6e 65 73 74 65 64 Before a nested
67a50 20 74 72 69 67 67 65 72 20 69 73 20 63 6f 64 65 trigger is code
67a60 64 2c 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 d, the linked li
67a70 73 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 st pointed to by
67a80 20 74 68 65 20 0a 20 2a 20 70 54 72 69 67 67 65 the . * pTrigge
67a90 72 53 74 61 63 6b 20 69 73 20 73 63 61 6e 6e 65 rStack is scanne
67aa0 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 d to ensure that
67ab0 20 74 68 65 20 74 72 69 67 67 65 72 20 69 73 20 the trigger is
67ac0 6e 6f 74 20 61 62 6f 75 74 20 74 6f 20 62 65 20 not about to be
67ad0 63 6f 64 65 64 0a 20 2a 20 72 65 63 75 72 73 69 coded. * recursi
67ae0 76 65 6c 79 2e 20 49 66 20 74 68 69 73 20 63 6f vely. If this co
67af0 6e 64 69 74 69 6f 6e 20 69 73 20 64 65 74 65 63 ndition is detec
67b00 74 65 64 2c 20 74 68 65 20 6e 65 73 74 65 64 20 ted, the nested
67b10 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 63 trigger is not c
67b20 6f 64 65 64 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 oded.. */.struct
67b30 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 7b 0a TriggerStack {.
67b40 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
67b50 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 /* Table
67b60 74 68 61 74 20 74 72 69 67 67 65 72 73 20 61 72 that triggers ar
67b70 65 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e e currently bein
67b80 67 20 63 6f 64 65 64 20 6f 6e 20 2a 2f 0a 20 20 g coded on */.
67b90 69 6e 74 20 6e 65 77 49 64 78 3b 20 20 20 20 20 int newIdx;
67ba0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
67bb0 20 76 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20 vdbe cursor to
67bc0 22 6e 65 77 22 20 74 65 6d 70 20 74 61 62 6c 65 "new" temp table
67bd0 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 */. int oldIdx
67be0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e ; /* In
67bf0 64 65 78 20 6f 66 20 76 64 62 65 20 63 75 72 73 dex of vdbe curs
67c00 6f 72 20 74 6f 20 22 6f 6c 64 22 20 74 65 6d 70 or to "old" temp
67c10 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 33 32 20 table */. u32
67c20 6e 65 77 43 6f 6c 4d 61 73 6b 3b 0a 20 20 75 33 newColMask;. u3
67c30 32 20 6f 6c 64 43 6f 6c 4d 61 73 6b 3b 0a 20 20 2 oldColMask;.
67c40 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 int orconf;
67c50 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
67c60 6f 72 63 6f 6e 66 20 70 6f 6c 69 63 79 20 2a 2f orconf policy */
67c70 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d . int ignoreJum
67c80 70 3b 20 20 20 20 20 20 2f 2a 20 77 68 65 72 65 p; /* where
67c90 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 to jump to for
67ca0 61 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 a RAISE(IGNORE)
67cb0 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 */. Trigger *pT
67cc0 72 69 67 67 65 72 3b 20 20 20 2f 2a 20 54 68 65 rigger; /* The
67cd0 20 74 72 69 67 67 65 72 20 63 75 72 72 65 6e 74 trigger current
67ce0 6c 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a ly being coded *
67cf0 2f 0a 20 20 54 72 69 67 67 65 72 53 74 61 63 6b /. TriggerStack
67d00 20 2a 70 4e 65 78 74 3b 20 2f 2a 20 4e 65 78 74 *pNext; /* Next
67d10 20 74 72 69 67 67 65 72 20 64 6f 77 6e 20 6f 6e trigger down on
67d20 20 74 68 65 20 74 72 69 67 67 65 72 20 73 74 61 the trigger sta
67d30 63 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ck */.};../*.**
67d40 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 The following st
67d50 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
67d60 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 information use
67d70 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 d by the sqliteF
67d80 69 78 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 ix....** routine
67d90 73 20 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 s as they walk t
67da0 68 65 20 70 61 72 73 65 20 74 72 65 65 20 74 6f he parse tree to
67db0 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 make database r
67dc0 65 66 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 eferences.** exp
67dd0 6c 69 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 licit. .*/.type
67de0 64 65 66 20 73 74 72 75 63 74 20 44 62 46 69 78 def struct DbFix
67df0 65 72 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 er DbFixer;.stru
67e00 63 74 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 ct DbFixer {. P
67e10 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
67e20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e /* The parsin
67e30 67 20 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f g context. Erro
67e40 72 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 r messages writt
67e50 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e en here */. con
67e60 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 st char *zDb;
67e70 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c /* Make sure al
67e80 6c 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f l objects are co
67e90 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 ntained in this
67ea0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f database */. co
67eb0 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b nst char *zType;
67ec0 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 /* Type of the
67ed0 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 container - use
67ee0 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 d for error mess
67ef0 61 67 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ages */. const
67f00 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a Token *pName; /*
67f10 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e Name of the con
67f20 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f tainer - used fo
67f30 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 r error messages
67f40 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
67f50 20 6f 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 objected used t
67f60 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 o accumulate the
67f70 20 74 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e text of a strin
67f80 67 20 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f g where we.** do
67f90 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 not necessarily
67fa0 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 know how big th
67fb0 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 e string will be
67fc0 20 69 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a in the end..*/.
67fd0 73 74 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 struct StrAccum
67fe0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
67ff0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 /* Opti
68000 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f onal database fo
68010 72 20 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 r lookaside. Ca
68020 6e 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 n be NULL */. c
68030 68 61 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 har *zBase;
68040 20 20 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c /* A base al
68050 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 location. Not f
68060 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 rom malloc. */.
68070 20 63 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 char *zText;
68080 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 /* The str
68090 69 6e 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f ing collected so
680a0 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e far */. int n
680b0 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f Char; /
680c0 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 * Length of the
680d0 73 74 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f string so far */
680e0 0a 20 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 . int nAlloc;
680f0 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e /* Amoun
68100 74 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 t of space alloc
68110 61 74 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f ated in zText */
68120 0a 20 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b . int mxAlloc;
68130 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
68140 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e um allowed strin
68150 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 g length */. u8
68160 20 20 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b mallocFailed;
68170 20 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 /* Becomes tr
68180 75 65 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 ue if any memory
68190 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
681a0 73 20 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d s */. u8 useM
681b0 61 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 alloc; /* T
681c0 72 75 65 20 69 66 20 7a 54 65 78 74 20 69 73 20 rue if zText is
681d0 65 6e 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e enlargeable usin
681e0 67 20 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 g realloc */. u
681f0 38 20 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 8 tooBig;
68200 20 20 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 /* Becomes t
68210 72 75 65 20 69 66 20 73 74 72 69 6e 67 20 73 69 rue if string si
68220 7a 65 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74 ze exceeds limit
68230 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 s */.};../*.** A
68240 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
68250 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 structure is us
68260 65 64 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 ed to communicat
68270 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a e information.**
68280 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 from sqlite3Ini
68290 74 20 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 t and OP_ParseSc
682a0 68 65 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71 hema into the sq
682b0 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 lite3InitCallbac
682c0 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 k..*/.typedef st
682d0 72 75 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 ruct {. sqlite3
682e0 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
682f0 54 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 The database bei
68300 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a ng initialized *
68310 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 /. int iDb;
68320 20 20 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 /* 0 for
68330 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 main database.
68340 20 31 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 1 for TEMP, 2..
68350 20 66 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f for ATTACHed */
68360 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d . char **pzErrM
68370 73 67 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 sg; /* Error
68380 6d 65 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 message stored h
68390 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b ere */. int rc;
683a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
683b0 52 65 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 Result code stor
683c0 65 64 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 ed here */.} Ini
683d0 74 44 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 tData;../*.** St
683e0 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 ructure containi
683f0 6e 67 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 ng global config
68400 75 72 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 uration data for
68410 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
68420 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ary..**.** This
68430 73 74 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63 structure also c
68440 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 ontains some sta
68450 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a te information..
68460 2a 2f 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65 */.struct Sqlite
68470 33 43 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 3Config {. int
68480 62 4d 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20 bMemstat;
68490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
684a0 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 True to enable
684b0 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f memory status */
684c0 0a 20 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 . int bCoreMute
684d0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
684e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
684f0 65 6e 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 enable core mute
68500 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 xing */. int bF
68510 75 6c 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20 ullMutex;
68520 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
68530 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 rue to enable fu
68540 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 ll mutexing */.
68550 20 69 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 int mxStrlen;
68560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68570 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 /* Maximum st
68580 72 69 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 ring length */.
68590 20 69 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 int szLookaside
685a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
685b0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f /* Default lo
685c0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 okaside buffer s
685d0 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f ize */. int nLo
685e0 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 okaside;
685f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
68600 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 fault lookaside
68610 62 75 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a buffer count */.
68620 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 sqlite3_mem_me
68630 74 68 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 thods m;
68640 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c /* Low-level
68650 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
68660 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a on interface */.
68670 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
68680 6d 65 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 methods mutex;
68690 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c /* Low-level
686a0 20 6d 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 mutex interface
686b0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 */. sqlite3_pc
686c0 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 ache_methods pca
686d0 63 68 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c che; /* Low-l
686e0 65 76 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20 evel page-cache
686f0 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 interface */. v
68700 6f 69 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 oid *pHeap;
68710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68720 20 2f 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 /* Heap storage
68730 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 space */. int
68740 6e 48 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 nHeap;
68750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
68760 20 53 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d Size of pHeap[]
68770 20 2a 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c */. int mnReq,
68780 20 6d 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 mxReq;
68790 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 /* Min a
687a0 6e 64 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 nd max heap requ
687b0 65 73 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 ests sizes */.
687c0 76 6f 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 void *pScratch;
687d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
687e0 20 20 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d /* Scratch mem
687f0 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 ory */. int szS
68800 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 cratch;
68810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
68820 7a 65 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 ze of each scrat
68830 63 68 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 ch buffer */. i
68840 6e 74 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 nt nScratch;
68850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68860 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 /* Number of sc
68870 72 61 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f ratch buffers */
68880 0a 20 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 . void *pPage;
68890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
688a0 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 /* Page cac
688b0 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 he memory */. i
688c0 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 nt szPage;
688d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
688e0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 /* Size of each
688f0 20 70 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d page in pPage[]
68900 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b */. int nPage;
68910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68920 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
68930 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 r of pages in pP
68940 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d age[] */. int m
68950 78 50 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20 xParserStack;
68960 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
68970 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 maximum depth of
68980 20 74 68 65 20 70 61 72 73 65 72 20 73 74 61 63 the parser stac
68990 6b 20 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 k */. int share
689a0 64 43 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 dCacheEnabled;
689b0 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 /* true
689c0 20 69 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 if shared-cache
689d0 20 6d 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f mode enabled */
689e0 0a 20 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 . /* The above
689f0 6d 69 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c might be initial
68a00 69 7a 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f ized to non-zero
68a10 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
68a20 20 6e 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a need to always.
68a30 20 20 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 ** initially b
68a40 65 20 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e e zero, however.
68a50 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 */. int isInit
68a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
68a70 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
68a80 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 after initializa
68a90 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 tion has finishe
68aa0 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f d */. int inPro
68ab0 67 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 gress;
68ac0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
68ad0 20 77 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a while initializ
68ae0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 ation in progres
68af0 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61 6c s */. int isMal
68b00 6c 6f 63 49 6e 69 74 3b 20 20 20 20 20 20 20 20 locInit;
68b10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
68b20 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73 after malloc is
68b30 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
68b40 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
68b50 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 20 *pInitMutex;
68b60 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 /* Mutex use
68b70 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 d by sqlite3_ini
68b80 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69 tialize() */. i
68b90 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 nt nRefInitMutex
68ba0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
68bb0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
68bc0 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 65 ers of pInitMute
68bd0 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 x */.};../*.** C
68be0 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 70 ontext pointer p
68bf0 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f 75 assed down throu
68c00 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c 6b gh the tree-walk
68c10 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b ..*/.struct Walk
68c20 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78 er {. int (*xEx
68c30 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b prCallback)(Walk
68c40 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20 20 er*, Expr*);
68c50 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 /* Callback for
68c60 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a expressions */.
68c70 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74 43 int (*xSelectC
68c80 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a allback)(Walker*
68c90 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43 ,Select*); /* C
68ca0 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c 45 allback for SELE
68cb0 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a CTs */. Parse *
68cc0 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
68cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68ce0 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e /* Parser con
68cf0 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f text. */. unio
68d00 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 n {
68d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68d20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 64 /* Extra d
68d30 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b ata for callback
68d40 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 */. NameCont
68d50 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 20 ext *pNC;
68d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68d70 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e /* Naming con
68d80 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 text */. int
68d90 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
68da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68db0 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
68dc0 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 r value */. } u
68dd0 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 ;.};../* Forward
68de0 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f declarations */
68df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
68e00 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 int sqlite3WalkE
68e10 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 xpr(Walker*, Exp
68e20 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
68e30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
68e40 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b alkExprList(Walk
68e50 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b er*, ExprList*);
68e60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
68e70 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 int sqlite3WalkS
68e80 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53 elect(Walker*, S
68e90 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
68ea0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
68eb0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 te3WalkSelectExp
68ec0 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 r(Walker*, Selec
68ed0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
68ee0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
68ef0 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61 alkSelectFrom(Wa
68f00 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b lker*, Select*);
68f10 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63 ../*.** Return c
68f20 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72 ode from the par
68f30 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67 20 se-tree walking
68f40 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20 74 primitives and t
68f50 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b heir.** callback
68f60 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 s..*/.#define WR
68f70 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30 20 C_Continue 0
68f80 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f /* Continue do
68f90 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e wn into children
68fa0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f */.#define WRC_
68fb0 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20 20 Prune 1
68fc0 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e /* Omit children
68fd0 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77 61 but continue wa
68fe0 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a lking siblings *
68ff0 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 62 /.#define WRC_Ab
69000 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f 2a ort 2 /*
69010 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 Abandon the tre
69020 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a e walk */../*.**
69030 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f Assuming zIn po
69040 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
69050 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d t byte of a UTF-
69060 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 8 character,.**
69070 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 advance zIn to p
69080 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 oint to the firs
69090 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 t byte of the ne
690a0 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 xt UTF-8 charact
690b0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 er..*/.#define S
690c0 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 QLITE_SKIP_UTF8(
690d0 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20 zIn) {
690e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
690f0 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 if( (*(zIn++))
69100 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 >=0xc0 ){
69110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
69120 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 \. whi
69130 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 le( (*zIn & 0xc0
69140 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b )==0x80 ){ zIn++
69150 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 ; }
69160 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 \. }
69170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
69180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
69190 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a \.}../*
691a0 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43 .** The SQLITE_C
691b0 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 ORRUPT_BKPT macr
691c0 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 o can be either
691d0 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 a constant (for
691e0 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 production.** bu
691f0 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74 ilds) or a funct
69200 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 ion call (for de
69210 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74 bugging). If it
69220 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 is a function c
69230 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 all,.** it allow
69240 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 s the operator t
69250 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 o set a breakpoi
69260 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 77 nt at the spot w
69270 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a here database.**
69280 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 corruption is f
69290 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a irst detected..*
692a0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
692b0 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
692c0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
692d0 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b e3Corrupt(void);
692e0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
692f0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71 _CORRUPT_BKPT sq
69300 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23 lite3Corrupt().#
69310 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
69320 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
69330 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 T SQLITE_CORRUPT
69340 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
69350 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
69360 72 6f 73 20 6d 69 6d 69 63 20 74 68 65 20 73 74 ros mimic the st
69370 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 66 andard library f
69380 75 6e 63 74 69 6f 6e 73 20 74 6f 75 70 70 65 72 unctions toupper
69390 28 29 2c 0a 2a 2a 20 69 73 73 70 61 63 65 28 29 (),.** isspace()
693a0 2c 20 69 73 61 6c 6e 75 6d 28 29 2c 20 69 73 64 , isalnum(), isd
693b0 69 67 69 74 28 29 20 61 6e 64 20 69 73 78 64 69 igit() and isxdi
693c0 67 69 74 28 29 2c 20 72 65 73 70 65 63 74 69 76 git(), respectiv
693d0 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 73 71 6c 69 ely. The.** sqli
693e0 74 65 20 76 65 72 73 69 6f 6e 73 20 6f 6e 6c 79 te versions only
693f0 20 77 6f 72 6b 20 66 6f 72 20 41 53 43 49 49 20 work for ASCII
69400 63 68 61 72 61 63 74 65 72 73 2c 20 72 65 67 61 characters, rega
69410 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 rdless of locale
69420 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
69430 54 45 5f 41 53 43 49 49 0a 23 20 64 65 66 69 6e TE_ASCII.# defin
69440 65 20 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 e sqlite3Toupper
69450 28 78 29 20 20 28 28 78 29 26 7e 28 73 71 6c 69 (x) ((x)&~(sqli
69460 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 te3CtypeMap[(uns
69470 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 igned char)(x)]&
69480 30 78 32 30 29 29 0a 23 20 64 65 66 69 6e 65 20 0x20)).# define
69490 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 78 sqlite3Isspace(x
694a0 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 ) (sqlite3Ctyp
694b0 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 eMap[(unsigned c
694c0 68 61 72 29 28 78 29 5d 26 30 78 30 31 29 0a 23 har)(x)]&0x01).#
694d0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
694e0 73 61 6c 6e 75 6d 28 78 29 20 20 20 28 73 71 6c salnum(x) (sql
694f0 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e ite3CtypeMap[(un
69500 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d signed char)(x)]
69510 26 30 78 30 36 29 0a 23 20 64 65 66 69 6e 65 20 &0x06).# define
69520 73 71 6c 69 74 65 33 49 73 61 6c 70 68 61 28 78 sqlite3Isalpha(x
69530 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 ) (sqlite3Ctyp
69540 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 eMap[(unsigned c
69550 68 61 72 29 28 78 29 5d 26 30 78 30 32 29 0a 23 har)(x)]&0x02).#
69560 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
69570 73 64 69 67 69 74 28 78 29 20 20 20 28 73 71 6c sdigit(x) (sql
69580 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e ite3CtypeMap[(un
69590 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d signed char)(x)]
695a0 26 30 78 30 34 29 0a 23 20 64 65 66 69 6e 65 20 &0x04).# define
695b0 73 71 6c 69 74 65 33 49 73 78 64 69 67 69 74 28 sqlite3Isxdigit(
695c0 78 29 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 x) (sqlite3Ctyp
695d0 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 eMap[(unsigned c
695e0 68 61 72 29 28 78 29 5d 26 30 78 30 38 29 0a 23 har)(x)]&0x08).#
695f0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 define sqlite3T
69600 6f 6c 6f 77 65 72 28 78 29 20 20 20 28 73 71 6c olower(x) (sql
69610 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 ite3UpperToLower
69620 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 [(unsigned char)
69630 28 78 29 5d 29 0a 23 65 6c 73 65 0a 23 20 69 6e (x)]).#else.# in
69640 63 6c 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0a clude <ctype.h>.
69650 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
69660 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f 75 Toupper(x) tou
69670 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63 pper((unsigned c
69680 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e har)(x)).# defin
69690 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 e sqlite3Isspace
696a0 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28 75 (x) isspace((u
696b0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 nsigned char)(x)
696c0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
696d0 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 69 e3Isalnum(x) i
696e0 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65 64 salnum((unsigned
696f0 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 char)(x)).# def
69700 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70 ine sqlite3Isalp
69710 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61 28 ha(x) isalpha(
69720 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
69730 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c x)).# define sql
69740 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 ite3Isdigit(x)
69750 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67 6e isdigit((unsign
69760 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
69770 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 efine sqlite3Isx
69780 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69 67 digit(x) isxdig
69790 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 it((unsigned cha
697a0 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 r)(x)).# define
697b0 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 sqlite3Tolower(x
697c0 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e 73 ) tolower((uns
697d0 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a igned char)(x)).
697e0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
697f0 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 ternal function
69800 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 prototypes.*/.SQ
69810 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
69820 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
69830 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f const char *, co
69840 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nst char *);.SQL
69850 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
69860 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 sqlite3StrNICmp(
69870 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f const char *, co
69880 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 nst char *, int)
69890 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
698a0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 int sqlite3IsNu
698b0 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a mber(const char*
698c0 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c , int*, u8);.SQL
698d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
698e0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
698f0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 53 const char*);..S
69900 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
69910 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 t sqlite3MallocI
69920 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 nit(void);.SQLIT
69930 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
69940 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 qlite3MallocEnd(
69950 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
69960 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
69970 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a te3Malloc(int);.
69980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
69990 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c oid *sqlite3Mall
699a0 6f 63 5a 65 72 6f 28 69 6e 74 29 3b 0a 53 51 4c ocZero(int);.SQL
699b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
699c0 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *sqlite3DbMallo
699d0 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 2a 2c 20 cZero(sqlite3*,
699e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
699f0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
69a00 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 e3DbMallocRaw(sq
69a10 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
69a20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
69a30 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44 r *sqlite3DbStrD
69a40 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 up(sqlite3*,cons
69a50 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
69a60 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
69a70 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
69a80 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
69a90 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 har*, int);.SQLI
69aa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
69ab0 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 *sqlite3Realloc(
69ac0 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c void*, int);.SQL
69ad0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
69ae0 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c *sqlite3DbReall
69af0 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 ocOrFree(sqlite3
69b00 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29 *, void *, int)
69b10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
69b20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
69b30 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 Realloc(sqlite3
69b40 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b *, void *, int);
69b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69b60 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 void sqlite3DbFr
69b70 65 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 ee(sqlite3*, voi
69b80 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
69b90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
69ba0 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 2a 29 allocSize(void*)
69bb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
69bc0 20 69 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61 int sqlite3DbMa
69bd0 6c 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33 llocSize(sqlite3
69be0 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 *, void*);.SQLIT
69bf0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
69c00 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 sqlite3ScratchMa
69c10 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
69c20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
69c30 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 qlite3ScratchFre
69c40 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 e(void*);.SQLITE
69c50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
69c60 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 qlite3PageMalloc
69c70 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
69c80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
69c90 65 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 2a e3PageFree(void*
69ca0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
69cb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
69cc0 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 mSetDefault(void
69cd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
69ce0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 E void sqlite3Be
69cf0 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 nignMallocHooks(
69d00 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 2c 20 void (*)(void),
69d10 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 29 3b void (*)(void));
69d20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69d30 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 int sqlite3Memor
69d40 79 41 6c 61 72 6d 28 76 6f 69 64 20 28 2a 29 28 yAlarm(void (*)(
69d50 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 void*, sqlite3_i
69d60 6e 74 36 34 2c 20 69 6e 74 29 2c 20 76 6f 69 64 nt64, int), void
69d70 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
69d80 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
69d90 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 E_ENABLE_MEMSYS3
69da0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
69db0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
69dc0 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 m_methods *sqlit
69dd0 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 e3MemGetMemsys3(
69de0 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 void);.#endif.#i
69df0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
69e00 4c 45 5f 4d 45 4d 53 59 53 35 0a 53 51 4c 49 54 LE_MEMSYS5.SQLIT
69e10 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
69e20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
69e30 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 ods *sqlite3MemG
69e40 65 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 3b etMemsys5(void);
69e50 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 .#endif...#ifnde
69e60 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f f SQLITE_MUTEX_O
69e70 4d 49 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 MIT.SQLITE_PRIVA
69e80 54 45 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 TE sqlite3_mut
69e90 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 ex_methods *sqli
69ea0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
69eb0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
69ec0 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 5f IVATE sqlite3_
69ed0 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d 75 mutex *sqlite3Mu
69ee0 74 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 texAlloc(int);.S
69ef0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
69f00 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 int sqlite3Mutex
69f10 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 Init(void);.SQLI
69f20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
69f30 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 sqlite3MutexEnd
69f40 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a (void);.#endif..
69f50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
69f60 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 nt sqlite3Status
69f70 56 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51 4c 49 Value(int);.SQLI
69f80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
69f90 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
69fa0 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 (int, int);.SQLI
69fb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
69fc0 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 sqlite3StatusSet
69fd0 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c (int, int);..SQL
69fe0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
69ff0 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 sqlite3IsNaN(dou
6a000 62 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 ble);..SQLITE_PR
6a010 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6a020 65 33 56 58 50 72 69 6e 74 66 28 53 74 72 41 63 e3VXPrintf(StrAc
6a030 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 cum*, int, const
6a040 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 char*, va_list)
6a050 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6a060 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 char *sqlite3MP
6a070 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c 63 rintf(sqlite3*,c
6a080 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
6a090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6a0a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d char *sqlite3VM
6a0b0 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c Printf(sqlite3*,
6a0c0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f const char*, va_
6a0d0 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 list);.SQLITE_PR
6a0e0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
6a0f0 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c 69 te3MAppendf(sqli
6a100 74 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 te3*,char*,const
6a110 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 69 66 char*,...);.#if
6a120 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
6a130 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 TEST) || defined
6a140 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 (SQLITE_DEBUG).S
6a150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6a160 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 void sqlite3Debu
6a170 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 gPrintf(const ch
6a180 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69 ar*, ...);.#endi
6a190 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 f.#if defined(SQ
6a1a0 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 LITE_TEST).SQLIT
6a1b0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
6a1c0 20 2a 73 71 6c 69 74 65 33 54 65 73 74 54 65 78 *sqlite3TestTex
6a1d0 74 54 6f 50 74 72 28 63 6f 6e 73 74 20 63 68 61 tToPtr(const cha
6a1e0 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 r*);.#endif.SQLI
6a1f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6a200 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
6a210 28 63 68 61 72 20 2a 2a 2c 20 73 71 6c 69 74 65 (char **, sqlite
6a220 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 3*, const char*,
6a230 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ...);.SQLITE_PR
6a240 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6a250 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 e3ErrorMsg(Parse
6a260 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
6a270 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ...);.SQLITE_PRI
6a280 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6a290 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 3ErrorClear(Pars
6a2a0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
6a2b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 ATE int sqlite3D
6a2c0 65 71 75 6f 74 65 28 63 68 61 72 2a 29 3b 0a 53 equote(char*);.S
6a2d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6a2e0 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 t sqlite3Keyword
6a2f0 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 Code(const unsig
6a300 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b ned char*, int);
6a310 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6a320 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61 int sqlite3RunPa
6a330 72 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f 6e rser(Parse*, con
6a340 73 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 2a st char*, char *
6a350 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6a360 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 TE void sqlite3F
6a370 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 73 inishCoding(Pars
6a380 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
6a390 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 ATE int sqlite3G
6a3a0 65 74 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a etTempReg(Parse*
6a3b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a3c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
6a3d0 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 leaseTempReg(Par
6a3e0 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 se*,int);.SQLITE
6a3f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6a400 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 ite3GetTempRange
6a410 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Parse*,int);.SQ
6a420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6a430 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 d sqlite3Release
6a440 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a TempRange(Parse*
6a450 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
6a460 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
6a470 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69 sqlite3Expr(sqli
6a480 74 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a te3*, int, Expr*
6a490 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 , Expr*, const T
6a4a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6a4b0 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
6a4c0 69 74 65 33 50 45 78 70 72 28 50 61 72 73 65 2a ite3PExpr(Parse*
6a4d0 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 , int, Expr*, Ex
6a4e0 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e pr*, const Token
6a4f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6a500 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
6a510 52 65 67 69 73 74 65 72 45 78 70 72 28 50 61 72 RegisterExpr(Par
6a520 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c se*,Token*);.SQL
6a530 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
6a540 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 *sqlite3ExprAnd
6a550 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c (sqlite3*,Expr*,
6a560 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
6a570 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6a580 69 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 70 ite3ExprSpan(Exp
6a590 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a r*,Token*,Token*
6a5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a5b0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
6a5c0 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73 xprFunction(Pars
6a5d0 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f e*,ExprList*, To
6a5e0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6a5f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6a600 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e e3ExprAssignVarN
6a610 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45 78 umber(Parse*, Ex
6a620 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
6a630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6a640 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69 74 3ExprClear(sqlit
6a650 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c e3*, Expr*);.SQL
6a660 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6a670 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
6a680 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 te(sqlite3*, Exp
6a690 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6a6a0 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 ATE ExprList *sq
6a6b0 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 lite3ExprListApp
6a6c0 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72 4c end(Parse*,ExprL
6a6d0 69 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65 6e ist*,Expr*,Token
6a6e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6a6f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6a700 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 xprListDelete(sq
6a710 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 lite3*, ExprList
6a720 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6a730 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e TE int sqlite3In
6a740 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 it(sqlite3*, cha
6a750 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r**);.SQLITE_PRI
6a760 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6a770 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 InitCallback(voi
6a780 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c d*, int, char**,
6a790 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 char**);.SQLITE
6a7a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6a7b0 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 lite3Pragma(Pars
6a7c0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a e*,Token*,Token*
6a7d0 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 ,Token*,int);.SQ
6a7e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6a7f0 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e d sqlite3ResetIn
6a800 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c ternalSchema(sql
6a810 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ite3*, int);.SQL
6a820 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6a830 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 sqlite3BeginPar
6a840 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a se(Parse*,int);.
6a850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6a860 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 oid sqlite3Commi
6a870 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 tInternalChanges
6a880 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
6a890 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 TE_PRIVATE Table
6a8a0 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 *sqlite3ResultS
6a8b0 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 etOfSelect(Parse
6a8c0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 *,Select*);.SQLI
6a8d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6a8e0 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 sqlite3OpenMaste
6a8f0 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20 rTable(Parse *,
6a900 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6a910 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6a920 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 73 3StartTable(Pars
6a930 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a e*,Token*,Token*
6a940 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
6a950 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6a960 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 E void sqlite3Ad
6a970 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54 dColumn(Parse*,T
6a980 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6a990 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6a9a0 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 te3AddNotNull(Pa
6a9b0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 rse*, int);.SQLI
6a9c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6a9d0 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 sqlite3AddPrimar
6a9e0 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 yKey(Parse*, Exp
6a9f0 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 rList*, int, int
6aa00 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6aa10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6aa20 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 te3AddCheckConst
6aa30 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 raint(Parse*, Ex
6aa40 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
6aa50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6aa60 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 3AddColumnType(P
6aa70 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 arse*,Token*);.S
6aa80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6aa90 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 id sqlite3AddDef
6aaa0 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a aultValue(Parse*
6aab0 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ,Expr*);.SQLITE_
6aac0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6aad0 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 ite3AddCollateTy
6aae0 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e pe(Parse*, Token
6aaf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6ab00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6ab10 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 ndTable(Parse*,T
6ab20 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c oken*,Token*,Sel
6ab30 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 ect*);..SQLITE_P
6ab40 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 RIVATE Bitvec *s
6ab50 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
6ab60 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f te(u32);.SQLITE_
6ab70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ab80 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 te3BitvecTest(Bi
6ab90 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c tvec*, u32);.SQL
6aba0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6abb0 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
6abc0 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a (Bitvec*, u32);.
6abd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6abe0 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 oid sqlite3Bitve
6abf0 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20 cClear(Bitvec*,
6ac00 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u32);.SQLITE_PRI
6ac10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6ac20 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 42 3BitvecDestroy(B
6ac30 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f itvec*);.SQLITE_
6ac40 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 PRIVATE u32 sqli
6ac50 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 69 te3BitvecSize(Bi
6ac60 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tvec*);.SQLITE_P
6ac70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6ac80 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 e3BitvecBuiltinT
6ac90 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a est(int,int*);..
6aca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 52 SQLITE_PRIVATE R
6acb0 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52 6f owSet *sqlite3Ro
6acc0 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65 33 wSetInit(sqlite3
6acd0 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e *, void*, unsign
6ace0 65 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f ed int);.SQLITE_
6acf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6ad00 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 ite3RowSetClear(
6ad10 52 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49 54 45 RowSet*);.SQLITE
6ad20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6ad30 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 lite3RowSetInser
6ad40 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 29 3b t(RowSet*, i64);
6ad50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ad60 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 int sqlite3RowSe
6ad70 74 54 65 73 74 28 52 6f 77 53 65 74 2a 2c 20 75 tTest(RowSet*, u
6ad80 38 20 69 42 61 74 63 68 2c 20 69 36 34 29 3b 0a 8 iBatch, i64);.
6ad90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6ada0 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 nt sqlite3RowSet
6adb0 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 Next(RowSet*, i6
6adc0 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 4*);..SQLITE_PRI
6add0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6ade0 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72 73 3CreateView(Pars
6adf0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a e*,Token*,Token*
6ae00 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c ,Token*,Select*,
6ae10 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21 int,int);..#if !
6ae20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
6ae30 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
6ae40 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
6ae50 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a T_VIRTUALTABLE).
6ae60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6ae70 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77 int sqlite3View
6ae80 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 GetColumnNames(P
6ae90 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 arse*,Table*);.#
6aea0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 else.# define sq
6aeb0 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 lite3ViewGetColu
6aec0 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a 23 mnNames(A,B) 0.#
6aed0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
6aee0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6aef0 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73 e3DropTable(Pars
6af00 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e e*, SrcList*, in
6af10 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6af20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6af30 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 ite3DeleteTable(
6af40 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Table*);.SQLITE_
6af50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6af60 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65 ite3Insert(Parse
6af70 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 *, SrcList*, Exp
6af80 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c rList*, Select*,
6af90 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a IdList*, int);.
6afa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6afb0 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 oid *sqlite3Arra
6afc0 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 yAllocate(sqlite
6afd0 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 3*,void*,int,int
6afe0 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 ,int*,int*,int*)
6aff0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b000 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 IdList *sqlite3
6b010 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c IdListAppend(sql
6b020 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 ite3*, IdList*,
6b030 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6b040 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6b050 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 te3IdListIndex(I
6b060 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 dList*,const cha
6b070 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6b080 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
6b090 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 ite3SrcListEnlar
6b0a0 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 ge(sqlite3*, Src
6b0b0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 List*, int, int)
6b0c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b0d0 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
6b0e0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 3SrcListAppend(s
6b0f0 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 qlite3*, SrcList
6b100 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
6b110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6b120 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 TE SrcList *sqli
6b130 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 te3SrcListAppend
6b140 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c FromTerm(Parse*,
6b150 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e SrcList*, Token
6b160 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 *, Token*,.
6b170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b190 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a Token*, Select*
6b1a0 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a , Expr*, IdList*
6b1b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b1c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
6b1d0 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 cListIndexedBy(P
6b1e0 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 arse *, SrcList
6b1f0 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c *, Token *);.SQL
6b200 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6b210 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 sqlite3IndexedBy
6b220 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 Lookup(Parse *,
6b230 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
6b240 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tem *);.SQLITE_P
6b250 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6b260 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a te3SrcListShiftJ
6b270 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a oinType(SrcList*
6b280 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b290 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
6b2a0 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f cListAssignCurso
6b2b0 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 rs(Parse*, SrcLi
6b2c0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
6b2d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6b2e0 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 3IdListDelete(sq
6b2f0 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 lite3*, IdList*)
6b300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6b310 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 void sqlite3Src
6b320 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 ListDelete(sqlit
6b330 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a e3*, SrcList*);.
6b340 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6b350 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 oid sqlite3Creat
6b360 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f eIndex(Parse*,To
6b370 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c ken*,Token*,SrcL
6b380 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 ist*,ExprList*,i
6b390 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 nt,Token*,.
6b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b3b0 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 Token*, int,
6b3c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6b3d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6b3e0 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 3DropIndex(Parse
6b3f0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 *, SrcList*, int
6b400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b410 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c E int sqlite3Sel
6b420 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 ect(Parse*, Sele
6b430 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a ct*, SelectDest*
6b440 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b450 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 E Select *sqlite
6b460 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65 3SelectNew(Parse
6b470 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c *,ExprList*,SrcL
6b480 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c ist*,Expr*,ExprL
6b490 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 ist*,.
6b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
6b4b0 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 xpr*,ExprList*,i
6b4c0 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b nt,Expr*,Expr*);
6b4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6b4e0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 void sqlite3Sele
6b4f0 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 ctDelete(sqlite3
6b500 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
6b510 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
6b520 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 e *sqlite3SrcLis
6b530 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20 tLookup(Parse*,
6b540 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 SrcList*);.SQLIT
6b550 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6b560 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 lite3IsReadOnly(
6b570 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
6b580 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6b590 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6b5a0 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65 3OpenTable(Parse
6b5b0 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74 *, int iCur, int
6b5c0 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e iDb, Table*, in
6b5d0 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 t);.#if defined(
6b5e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 SQLITE_ENABLE_UP
6b5f0 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 DATE_DELETE_LIMI
6b600 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 T) && !defined(S
6b610 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
6b620 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 ERY).SQLITE_PRIV
6b630 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
6b640 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73 3LimitWhere(Pars
6b650 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 e *, SrcList *,
6b660 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74 Expr *, ExprList
6b670 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 *, Expr *, Expr
6b680 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e *, char *);.#en
6b690 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
6b6a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
6b6b0 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a eleteFrom(Parse*
6b6c0 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 , SrcList*, Expr
6b6d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6b6e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
6b6f0 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 pdate(Parse*, Sr
6b700 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 cList*, ExprList
6b710 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a *, Expr*, int);.
6b720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 SQLITE_PRIVATE W
6b730 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 hereInfo *sqlite
6b740 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73 3WhereBegin(Pars
6b750 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 e*, SrcList*, Ex
6b760 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c pr*, ExprList**,
6b770 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u16);.SQLITE_PR
6b780 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6b790 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 e3WhereEnd(Where
6b7a0 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Info*);.SQLITE_P
6b7b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6b7c0 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c e3ExprCodeGetCol
6b7d0 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c umn(Parse*, Tabl
6b7e0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e e*, int, int, in
6b7f0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6b800 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6b810 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 ite3ExprCodeMove
6b820 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
6b830 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6b840 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6b850 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 ite3ExprCodeCopy
6b860 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
6b870 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
6b880 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6b890 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f ite3ExprCacheSto
6b8a0 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 re(Parse*, int,
6b8b0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
6b8c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6b8d0 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
6b8e0 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ush(Parse*);.SQL
6b8f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6b900 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
6b910 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 ePop(Parse*, int
6b920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6b930 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
6b940 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 prCacheRemove(Pa
6b950 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 rse*, int);.SQLI
6b960 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6b970 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
6b980 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 Clear(Parse*);.S
6b990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6b9a0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
6b9b0 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
6b9c0 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 e(Parse*, int, i
6b9d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6b9e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6b9f0 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 ExprHardCopy(Par
6ba00 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 se*,int,int);.SQ
6ba10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ba20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
6ba30 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
6ba40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6ba50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6ba60 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 ExprCodeTemp(Par
6ba70 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a se*, Expr*, int*
6ba80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ba90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
6baa0 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 rCodeTarget(Pars
6bab0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b e*, Expr*, int);
6bac0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6bad0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
6bae0 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 odeAndCache(Pars
6baf0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b e*, Expr*, int);
6bb00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6bb10 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
6bb20 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 CodeConstants(Pa
6bb30 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 rse*, Expr*);.SQ
6bb40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6bb50 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
6bb60 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c ExprList(Parse*,
6bb70 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c ExprList*, int,
6bb80 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6bb90 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6bba0 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72 e3ExprIfTrue(Par
6bbb0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c se*, Expr*, int,
6bbc0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6bbd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6bbe0 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61 e3ExprIfFalse(Pa
6bbf0 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
6bc00 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6bc10 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 RIVATE Table *sq
6bc20 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 lite3FindTable(s
6bc30 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
6bc40 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a ar*, const char*
6bc50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6bc60 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 E Table *sqlite3
6bc70 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 LocateTable(Pars
6bc80 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f e*,int isView,co
6bc90 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 nst char*, const
6bca0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
6bcb0 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 PRIVATE Index *s
6bcc0 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 qlite3FindIndex(
6bcd0 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
6bce0 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
6bcf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6bd00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
6bd10 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
6bd20 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 ble(sqlite3*,int
6bd30 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 ,const char*);.S
6bd40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6bd50 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b id sqlite3Unlink
6bd60 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73 AndDeleteIndex(s
6bd70 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 qlite3*,int,cons
6bd80 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
6bd90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6bda0 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73 lite3Vacuum(Pars
6bdb0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
6bdc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
6bdd0 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c unVacuum(char**,
6bde0 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 sqlite3*);.SQLI
6bdf0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
6be00 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d *sqlite3NameFrom
6be10 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 Token(sqlite3*,
6be20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
6be30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6be40 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 te3ExprCompare(E
6be50 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 xpr*, Expr*);.SQ
6be60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6be70 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 d sqlite3ExprAna
6be80 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e lyzeAggregates(N
6be90 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 ameContext*, Exp
6bea0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6beb0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6bec0 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 ExprAnalyzeAggLi
6bed0 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c st(NameContext*,
6bee0 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 ExprList*);.SQLI
6bef0 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 TE_PRIVATE Vdbe
6bf00 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 *sqlite3GetVdbe(
6bf10 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6bf20 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
6bf30 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70 lite3CreateIdExp
6bf40 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 r(Parse *, const
6bf50 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
6bf60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6bf70 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 ite3PrngSaveStat
6bf80 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f e(void);.SQLITE_
6bf90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6bfa0 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 ite3PrngRestoreS
6bfb0 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 tate(void);.SQLI
6bfc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6bfd0 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 sqlite3PrngReset
6bfe0 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c State(void);.SQL
6bff0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6c000 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
6c010 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 All(sqlite3*);.S
6c020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6c030 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 id sqlite3CodeVe
6c040 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65 rifySchema(Parse
6c050 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6c060 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6c070 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 ite3BeginTransac
6c080 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 tion(Parse*, int
6c090 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c0a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
6c0b0 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 mmitTransaction(
6c0c0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6c0d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6c0e0 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e ite3RollbackTran
6c0f0 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b saction(Parse*);
6c100 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6c110 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 void sqlite3Save
6c120 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e point(Parse*, in
6c130 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 t, Token*);.SQLI
6c140 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6c150 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 sqlite3CloseSave
6c160 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a points(sqlite3 *
6c170 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c180 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
6c190 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 rIsConstant(Expr
6c1a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6c1b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
6c1c0 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a prIsConstantNotJ
6c1d0 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 oin(Expr*);.SQLI
6c1e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6c1f0 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
6c200 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 tantOrFunction(E
6c210 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6c220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6c230 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 3ExprIsInteger(E
6c240 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c xpr*, int*);.SQL
6c250 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6c260 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63 sqlite3IsRowid(c
6c270 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
6c280 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6c290 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 sqlite3Generate
6c2a0 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 2a RowDelete(Parse*
6c2b0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 , Table*, int, i
6c2c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
6c2d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6c2e0 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 lite3GenerateRow
6c2f0 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 73 IndexDelete(Pars
6c300 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
6c310 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int*);.SQLITE_P
6c320 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6c330 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b e3GenerateIndexK
6c340 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 78 ey(Parse*, Index
6c350 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
6c360 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c370 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 E void sqlite3Ge
6c380 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 nerateConstraint
6c390 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 61 Checks(Parse*,Ta
6c3a0 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20 ble*,int,int,.
6c3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c3d0 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c int*,int,int,
6c3e0 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 int,int,int*);.S
6c3f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6c400 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 id sqlite3Comple
6c410 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61 72 73 teInsertion(Pars
6c420 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
6c430 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74 2c int, int*, int,
6c440 20 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 int,int,int);.S
6c450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6c460 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 t sqlite3OpenTab
6c470 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72 leAndIndices(Par
6c480 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 se*, Table*, int
6c490 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6c4a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6c4b0 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
6c4c0 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 ration(Parse*, i
6c4d0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
6c4e0 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
6c4f0 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71 qlite3ExprDup(sq
6c500 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 lite3*,Expr*,int
6c510 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c520 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f E void sqlite3To
6c530 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 2a kenCopy(sqlite3*
6c540 2c 54 6f 6b 65 6e 2a 2c 63 6f 6e 73 74 20 54 6f ,Token*,const To
6c550 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6c560 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a IVATE ExprList *
6c570 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
6c580 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 up(sqlite3*,Expr
6c590 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 List*,int);.SQLI
6c5a0 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 TE_PRIVATE SrcLi
6c5b0 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 st *sqlite3SrcLi
6c5c0 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 stDup(sqlite3*,S
6c5d0 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 rcList*,int);.SQ
6c5e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c LITE_PRIVATE IdL
6c5f0 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 ist *sqlite3IdLi
6c600 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49 stDup(sqlite3*,I
6c610 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f dList*);.SQLITE_
6c620 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a PRIVATE Select *
6c630 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 sqlite3SelectDup
6c640 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 (sqlite3*,Select
6c650 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
6c660 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6c670 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 te3FuncDefInsert
6c680 28 46 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 (FuncDefHash*, F
6c690 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 uncDef*);.SQLITE
6c6a0 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 _PRIVATE FuncDef
6c6b0 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e *sqlite3FindFun
6c6c0 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 ction(sqlite3*,c
6c6d0 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 onst char*,int,i
6c6e0 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 nt,u8,int);.SQLI
6c6f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6c700 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 sqlite3RegisterB
6c710 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 uiltinFunctions(
6c720 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
6c730 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6c740 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 qlite3RegisterDa
6c750 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 teTimeFunctions(
6c760 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
6c770 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6c780 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c e3RegisterGlobal
6c790 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b Functions(void);
6c7a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
6c7b0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
6c7c0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
6c7d0 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 3SafetyOn(sqlite
6c7e0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.SQLITE_PRIV
6c7f0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
6c800 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 3SafetyOff(sqlit
6c810 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 e3*);.#else.# de
6c820 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 fine sqlite3Safe
6c830 74 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 tyOn(A) 0.# defi
6c840 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ne sqlite3Safety
6c850 4f 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a Off(A) 0.#endif.
6c860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6c870 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 nt sqlite3Safety
6c880 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a CheckOk(sqlite3*
6c890 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6c8a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
6c8b0 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b etyCheckSickOrOk
6c8c0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
6c8d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6c8e0 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f sqlite3ChangeCoo
6c8f0 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 kie(Parse*, int)
6c900 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 ;..#if !defined(
6c910 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
6c920 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
6c930 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
6c940 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 R).SQLITE_PRIVAT
6c950 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 E void sqlite3Ma
6c960 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 terializeView(Pa
6c970 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 rse*, Table*, Ex
6c980 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 pr*, int);.#endi
6c990 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
6c9a0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 E_OMIT_TRIGGER.S
6c9b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6c9c0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 void sqlite3Begi
6c9d0 6e 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c nTrigger(Parse*,
6c9e0 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 Token*,Token*,i
6c9f0 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 nt,int,IdList*,S
6ca00 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 rcList*,.
6ca10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ca20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 Expr*,int, i
6ca30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6ca40 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
6ca50 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 e3FinishTrigger(
6ca60 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 Parse*, TriggerS
6ca70 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 tep*, Token*);.S
6ca80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6ca90 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 void sqlite3Drop
6caa0 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 Trigger(Parse*,
6cab0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a SrcList*, int);.
6cac0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6cad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f void sqlite3Dro
6cae0 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73 pTriggerPtr(Pars
6caf0 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 e*, Trigger*);.S
6cb00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6cb10 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 Trigger *sqlite3
6cb20 54 72 69 67 67 65 72 73 45 78 69 73 74 28 50 61 TriggersExist(Pa
6cb30 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 rse *, Table*, i
6cb40 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 nt, ExprList*, i
6cb50 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 nt *pMask);.SQLI
6cb60 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
6cb70 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 gger *sqlite3Tri
6cb80 67 67 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a ggerList(Parse *
6cb90 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 , Table *);.SQLI
6cba0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
6cbb0 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 sqlite3CodeRowT
6cbc0 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 rigger(Parse*, T
6cbd0 72 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 rigger *, int, E
6cbe0 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 xprList*, int, T
6cbf0 61 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 able *,.
6cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cc10 20 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e int, int, in
6cc20 74 2c 20 69 6e 74 2c 20 75 33 32 2a 2c 20 75 33 t, int, u32*, u3
6cc30 32 2a 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69 2*);. void sqli
6cc40 74 65 56 69 65 77 54 72 69 67 67 65 72 73 28 50 teViewTriggers(P
6cc50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 arse*, Table*, E
6cc60 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c xpr*, int, ExprL
6cc70 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
6cc80 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
6cc90 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 ite3DeleteTrigge
6cca0 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 20 rStep(sqlite3*,
6ccb0 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 TriggerStep*);.S
6ccc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6ccd0 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c TriggerStep *sql
6cce0 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 ite3TriggerSelec
6ccf0 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 53 tStep(sqlite3*,S
6cd00 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
6cd10 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 PRIVATE Trigge
6cd20 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 rStep *sqlite3Tr
6cd30 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 iggerInsertStep(
6cd40 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c sqlite3*,Token*,
6cd50 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 IdList*,.
6cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cd80 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 ExprList*,Sele
6cd90 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ct*,int);.SQLITE
6cda0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
6cdb0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 erStep *sqlite3T
6cdc0 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70 riggerUpdateStep
6cdd0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a (sqlite3*,Token*
6cde0 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 72 ,ExprList*, Expr
6cdf0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6ce00 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 PRIVATE Trigge
6ce10 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 rStep *sqlite3Tr
6ce20 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 iggerDeleteStep(
6ce30 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c sqlite3*,Token*,
6ce40 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
6ce50 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6ce60 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 qlite3DeleteTrig
6ce70 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 ger(sqlite3*, Tr
6ce80 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f igger*);.SQLITE_
6ce90 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6cea0 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
6ceb0 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c eleteTrigger(sql
6cec0 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 ite3*,int,const
6ced0 63 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 char*);.#else.#
6cee0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 define sqlite3Tr
6cef0 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c iggersExist(B,C,
6cf00 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e D,E,F) 0.# defin
6cf10 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 e sqlite3DeleteT
6cf20 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 rigger(A,B).# de
6cf30 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 fine sqlite3Drop
6cf40 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a TriggerPtr(A,B).
6cf50 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6cf60 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
6cf70 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 rigger(A,B,C).#
6cf80 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f define sqlite3Co
6cf90 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 deRowTrigger(A,B
6cfa0 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a ,C,D,E,F,G,H,I,J
6cfb0 2c 4b 2c 4c 29 20 30 0a 23 20 64 65 66 69 6e 65 ,K,L) 0.# define
6cfc0 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c sqlite3TriggerL
6cfd0 69 73 74 28 58 2c 20 59 29 20 30 0a 23 65 6e 64 ist(X, Y) 0.#end
6cfe0 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
6cff0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f TE int sqlite3Jo
6d000 69 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 inType(Parse*, T
6d010 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 oken*, Token*, T
6d020 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6d030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6d040 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e te3CreateForeign
6d050 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 Key(Parse*, Expr
6d060 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 List*, Token*, E
6d070 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a xprList*, int);.
6d080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6d090 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72 oid sqlite3Defer
6d0a0 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 ForeignKey(Parse
6d0b0 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 *, int);.#ifndef
6d0c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
6d0d0 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 HORIZATION.SQLIT
6d0e0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
6d0f0 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 sqlite3AuthRead
6d100 28 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 (Parse*,Expr*,Sc
6d110 68 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b hema*,SrcList*);
6d120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6d130 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 int sqlite3Aut
6d140 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e hCheck(Parse*,in
6d150 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 t, const char*,
6d160 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e const char*, con
6d170 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
6d180 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
6d190 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 sqlite3AuthCont
6d1a0 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20 extPush(Parse*,
6d1b0 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f AuthContext*, co
6d1c0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
6d1d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
6d1e0 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e d sqlite3AuthCon
6d1f0 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 textPop(AuthCont
6d200 65 78 74 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 ext*);.#else.# d
6d210 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 efine sqlite3Aut
6d220 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 hRead(a,b,c,d).#
6d230 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 define sqlite3A
6d240 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 uthCheck(a,b,c,d
6d250 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b ,e) SQLITE_OK
6d260 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6d270 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 3AuthContextPush
6d280 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 (a,b,c).# define
6d290 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 sqlite3AuthCont
6d2a0 65 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69 extPop(a) ((voi
6d2b0 64 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 d)(a)).#endif.SQ
6d2c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6d2d0 64 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 d sqlite3Attach(
6d2e0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 Parse*, Expr*, E
6d2f0 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 xpr*, Expr*);.SQ
6d300 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6d310 64 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 d sqlite3Detach(
6d320 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a Parse*, Expr*);.
6d330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6d340 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 nt sqlite3BtreeF
6d350 61 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c actory(const sql
6d360 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
6d370 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
6d380 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
6d390 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 int omit
6d3a0 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 Journal, int nCa
6d3b0 63 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 che, int flags,
6d3c0 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29 Btree **ppBtree)
6d3d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6d3e0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49 int sqlite3FixI
6d3f0 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61 nit(DbFixer*, Pa
6d400 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 rse*, int, const
6d410 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f char*, const To
6d420 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6d430 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6d440 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69 3FixSrcList(DbFi
6d450 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b xer*, SrcList*);
6d460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6d470 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 int sqlite3FixSe
6d480 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53 lect(DbFixer*, S
6d490 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
6d4a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6d4b0 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78 te3FixExpr(DbFix
6d4c0 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c er*, Expr*);.SQL
6d4d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6d4e0 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 sqlite3FixExprLi
6d4f0 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 st(DbFixer*, Exp
6d500 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f rList*);.SQLITE_
6d510 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6d520 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 te3FixTriggerSte
6d530 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67 p(DbFixer*, Trig
6d540 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 gerStep*);.SQLIT
6d550 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6d560 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 lite3AtoF(const
6d570 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a char *z, double*
6d580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6d590 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 E int sqlite3Get
6d5a0 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 Int32(const char
6d5b0 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 *, int*);.SQLIT
6d5c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6d5d0 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 lite3FitsIn64Bit
6d5e0 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 s(const char *,
6d5f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6d600 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6d610 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e Utf16ByteLen(con
6d620 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 st void *pData,
6d630 69 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 int nChar);.SQLI
6d640 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6d650 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 qlite3Utf8CharLe
6d660 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 n(const char *pD
6d670 61 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b ata, int nByte);
6d680 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6d690 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 int sqlite3Utf8R
6d6a0 65 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 ead(const u8*, c
6d6b0 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a onst u8**);../*.
6d6c0 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 ** Routines to r
6d6d0 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 ead and write va
6d6e0 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
6d6f0 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 tegers. These u
6d700 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 sed to.** be def
6d710 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 ined locally, bu
6d720 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 t now we use the
6d730 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 varint routines
6d740 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a in the util.c.*
6d750 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 * file. Code sh
6d760 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 ould use the MAC
6d770 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 RO forms below,
6d780 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20 as the Varint32
6d790 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 versions.** are
6d7a0 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20 coded to assume
6d7b0 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20 the single byte
6d7c0 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 case is already
6d7d0 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a handled (which .
6d7e0 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 ** the MACRO for
6d7f0 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 m does)..*/.SQLI
6d800 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6d810 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 qlite3PutVarint(
6d820 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 unsigned char*,
6d830 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u64);.SQLITE_PRI
6d840 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6d850 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 PutVarint32(unsi
6d860 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29 gned char*, u32)
6d870 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6d880 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 u8 sqlite3GetVa
6d890 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 rint(const unsig
6d8a0 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 ned char *, u64
6d8b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6d8c0 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 TE u8 sqlite3Get
6d8d0 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 Varint32(const u
6d8e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 nsigned char *,
6d8f0 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 u32 *);.SQLITE_P
6d900 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6d910 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 e3VarintLen(u64
6d920 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 v);../*.** The h
6d930 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72 eader of a recor
6d940 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 d consists of a
6d950 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c sequence variabl
6d960 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
6d970 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 s..** These inte
6d980 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 gers are almost
6d990 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 always small and
6d9a0 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20 are encoded as
6d9b0 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a a single byte..*
6d9c0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
6d9d0 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61 macros take adva
6d9e0 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20 ntage this fact
6d9f0 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73 to provide a fas
6da00 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 t encode.** and
6da10 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e decode of the in
6da20 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f tegers in a reco
6da30 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 rd header. It i
6da40 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 s faster for the
6da50 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 common.** case
6da60 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 where the intege
6da70 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 r is a single by
6da80 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 te. It is a lit
6da90 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 tle slower when
6daa0 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 the.** integer i
6dab0 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 s two or more by
6dac0 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c tes. But overal
6dad0 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a l it is faster..
6dae0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
6daf0 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 ing expressions
6db00 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a are equivalent:.
6db10 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 **.** x = sq
6db20 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 lite3GetVarint32
6db30 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 ( A, &B );.**
6db40 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74 x = sqlite3Put
6db50 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 Varint32( A, B )
6db60 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 ;.**.** x =
6db70 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 getVarint32( A,
6db80 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 B );.** x =
6db90 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 putVarint32( A,
6dba0 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 B );.**.*/.#defi
6dbb0 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41 ne getVarint32(A
6dbc0 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c ,B) (u8)((*(A)<
6dbd0 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29 (u8)0x80) ? ((B)
6dbe0 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20 = (u32)*(A)),1
6dbf0 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 : sqlite3GetVari
6dc00 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a nt32((A), (u32 *
6dc10 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 )&(B))).#define
6dc20 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 putVarint32(A,B)
6dc30 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29 (u8)(((u32)(B)
6dc40 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a <(u32)0x80) ? (*
6dc50 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 (A) = (unsigned
6dc60 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71 char)(B)),1 : sq
6dc70 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 lite3PutVarint32
6dc80 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66 ((A), (B))).#def
6dc90 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20 ine getVarint
6dca0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
6dcb0 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 t.#define putVar
6dcc0 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 int sqlite3Pu
6dcd0 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 tVarint...SQLITE
6dce0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6dcf0 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 lite3IndexAffini
6dd00 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e tyStr(Vdbe *, In
6dd10 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 dex *);.SQLITE_P
6dd20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6dd30 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 te3TableAffinity
6dd40 53 74 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c Str(Vdbe *, Tabl
6dd50 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e *);.SQLITE_PRI
6dd60 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 VATE char sqlite
6dd70 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
6dd80 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
6dd90 61 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 ar aff2);.SQLITE
6dda0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6ddb0 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 ite3IndexAffinit
6ddc0 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c yOk(Expr *pExpr,
6ddd0 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 char idx_affini
6dde0 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ty);.SQLITE_PRIV
6ddf0 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
6de00 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 ExprAffinity(Exp
6de10 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 r *pExpr);.SQLIT
6de20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6de30 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 lite3Atoi64(cons
6de40 74 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a t char*, i64*);.
6de50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6de60 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 oid sqlite3Error
6de70 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 (sqlite3*, int,
6de80 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 const char*,...)
6de90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6dea0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 void *sqlite3He
6deb0 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a xToBlob(sqlite3*
6dec0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c , const char *z,
6ded0 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f int n);.SQLITE_
6dee0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6def0 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 te3TwoPartName(P
6df00 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c arse *, Token *,
6df10 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 Token *, Token
6df20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
6df30 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
6df40 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e sqlite3ErrStr(in
6df50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6df60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 TE int sqlite3Re
6df70 61 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a adSchema(Parse *
6df80 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f pParse);.SQLITE_
6df90 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
6dfa0 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c *sqlite3FindColl
6dfb0 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 Seq(sqlite3*,u8
6dfc0 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 enc, const char
6dfd0 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 *,int,int);.SQLI
6dfe0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
6dff0 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 eq *sqlite3Locat
6e000 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a eCollSeq(Parse *
6e010 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 pParse, const ch
6e020 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e ar *zName, int n
6e030 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Name);.SQLITE_PR
6e040 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
6e050 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 qlite3ExprCollSe
6e060 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c q(Parse *pParse,
6e070 20 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 Expr *pExpr);.S
6e080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
6e090 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 pr *sqlite3ExprS
6e0a0 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 etColl(Parse *pP
6e0b0 61 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f arse, Expr *, To
6e0c0 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ken *);.SQLITE_P
6e0d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6e0e0 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 e3CheckCollSeq(P
6e0f0 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 arse *, CollSeq
6e100 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e110 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 TE int sqlite3Ch
6e120 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 eckObjectName(Pa
6e130 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 rse *, const cha
6e140 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r *);.SQLITE_PRI
6e150 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6e160 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
6e170 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b sqlite3 *, int);
6e180 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
6e190 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
6e1a0 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 73 71 ite3ValueText(sq
6e1b0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 lite3_value*, u8
6e1c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e1d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c E int sqlite3Val
6e1e0 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f ueBytes(sqlite3_
6e1f0 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c value*, u8);.SQL
6e200 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6e210 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
6e220 53 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 Str(sqlite3_valu
6e230 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 e*, int, const v
6e240 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20 oid *,u8, .
6e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e260 20 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a void(*)(void*
6e270 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ));.SQLITE_PRIVA
6e280 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
6e290 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 alueFree(sqlite3
6e2a0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
6e2b0 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
6e2c0 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 _value *sqlite3V
6e2d0 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 alueNew(sqlite3
6e2e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e2f0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
6e300 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33 Utf16to8(sqlite3
6e310 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c *, const void*,
6e320 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6e330 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6e340 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 3ValueFromExpr(s
6e350 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a qlite3 *, Expr *
6e360 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65 , u8, u8, sqlite
6e370 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 3_value **);.SQL
6e380 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6e390 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 sqlite3ValueApp
6e3a0 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74 lyAffinity(sqlit
6e3b0 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20 e3_value *, u8,
6e3c0 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c u8);.#ifndef SQL
6e3d0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
6e3e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e3f0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
6e400 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 har sqlite3Upper
6e410 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 ToLower[];.SQLIT
6e420 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
6e430 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 unsigned char sq
6e440 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 5d 3b lite3CtypeMap[];
6e450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e460 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
6e470 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 t Sqlite3Config
6e480 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53 sqlite3Config;.S
6e490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 QLITE_PRIVATE SQ
6e4a0 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 LITE_WSD FuncDef
6e4b0 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 Hash sqlite3Glob
6e4c0 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c alFunctions;.SQL
6e4d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6e4e0 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 sqlite3PendingBy
6e4f0 74 65 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 te;.#endif.SQLIT
6e500 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6e510 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f qlite3RootPageMo
6e520 76 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e ved(Db*, int, in
6e530 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6e540 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
6e550 65 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54 eindex(Parse*, T
6e560 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a oken*, Token*);.
6e570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6e580 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 oid sqlite3Alter
6e590 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 Functions(sqlite
6e5a0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.SQLITE_PRIV
6e5b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6e5c0 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 AlterRenameTable
6e5d0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 (Parse*, SrcList
6e5e0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
6e5f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6e600 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 qlite3GetToken(c
6e610 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
6e620 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 ar *, int *);.SQ
6e630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6e640 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 d sqlite3NestedP
6e650 61 72 73 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e arse(Parse*, con
6e660 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a st char*, ...);.
6e670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6e680 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 oid sqlite3Expir
6e690 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 ePreparedStateme
6e6a0 6e 74 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 nts(sqlite3*);.S
6e6b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6e6c0 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 id sqlite3CodeSu
6e6d0 62 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a 2c bselect(Parse *,
6e6e0 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20 69 6e Expr *, int, in
6e6f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6e700 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
6e710 65 6c 65 63 74 50 72 65 70 28 50 61 72 73 65 2a electPrep(Parse*
6e720 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 , Select*, NameC
6e730 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 ontext*);.SQLITE
6e740 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6e750 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
6e760 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 ames(NameContext
6e770 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
6e780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6e790 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c qlite3ResolveSel
6e7a0 65 63 74 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c ectNames(Parse*,
6e7b0 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f Select*, NameCo
6e7c0 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f ntext*);.SQLITE_
6e7d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6e7e0 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 te3ResolveOrderG
6e7f0 72 6f 75 70 42 79 28 50 61 72 73 65 2a 2c 20 53 roupBy(Parse*, S
6e800 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69 73 74 elect*, ExprList
6e810 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b *, const char*);
6e820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e830 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75 void sqlite3Colu
6e840 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a mnDefault(Vdbe *
6e850 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 29 3b , Table *, int);
6e860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e870 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 void sqlite3Alte
6e880 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e rFinishAddColumn
6e890 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 (Parse *, Token
6e8a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6e8b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
6e8c0 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 lterBeginAddColu
6e8d0 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c mn(Parse *, SrcL
6e8e0 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ist *);.SQLITE_P
6e8f0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a RIVATE CollSeq *
6e900 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 sqlite3GetCollSe
6e910 71 28 73 71 6c 69 74 65 33 2a 2c 20 43 6f 6c 6c q(sqlite3*, Coll
6e920 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 Seq *, const cha
6e930 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 r *, int);.SQLIT
6e940 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 E_PRIVATE char s
6e950 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 qlite3AffinityTy
6e960 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 pe(const Token*)
6e970 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e980 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 void sqlite3Ana
6e990 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b lyze(Parse*, Tok
6e9a0 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 en*, Token*);.SQ
6e9b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6e9c0 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 sqlite3InvokeBu
6e9d0 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61 syHandler(BusyHa
6e9e0 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ndler*);.SQLITE_
6e9f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ea00 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 te3FindDb(sqlite
6ea10 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 3*, Token*);.SQL
6ea20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6ea30 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d sqlite3FindDbNam
6ea40 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e e(sqlite3 *, con
6ea50 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 st char *);.SQLI
6ea60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6ea70 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f qlite3AnalysisLo
6ea80 61 64 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 ad(sqlite3*,int
6ea90 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 iDB);.SQLITE_PRI
6eaa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6eab0 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 3DefaultRowEst(I
6eac0 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ndex*);.SQLITE_P
6ead0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6eae0 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 te3RegisterLikeF
6eaf0 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 unctions(sqlite3
6eb00 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6eb10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6eb20 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f te3IsLikeFunctio
6eb30 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a n(sqlite3*,Expr*
6eb40 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 ,int*,char*);.SQ
6eb50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6eb60 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d d sqlite3Minimum
6eb70 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65 FileFormat(Parse
6eb80 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
6eb90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6eba0 64 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 d sqlite3SchemaF
6ebb0 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a 53 51 4c ree(void *);.SQL
6ebc0 49 54 45 5f 50 52 49 56 41 54 45 20 53 63 68 65 ITE_PRIVATE Sche
6ebd0 6d 61 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d ma *sqlite3Schem
6ebe0 61 47 65 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 aGet(sqlite3 *,
6ebf0 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Btree *);.SQLITE
6ec00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6ec10 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
6ec20 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 x(sqlite3 *db, S
6ec30 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c 49 54 45 chema *);.SQLITE
6ec40 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f _PRIVATE KeyInfo
6ec50 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 *sqlite3IndexKe
6ec60 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 2c 20 49 yinfo(Parse *, I
6ec70 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f ndex *);.SQLITE_
6ec80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ec90 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 73 71 te3CreateFunc(sq
6eca0 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 lite3 *, const c
6ecb0 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c har *, int, int,
6ecc0 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76 6f 69 64 void *, . void
6ecd0 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e (*)(sqlite3_con
6ece0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 text*,int,sqlite
6ecf0 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 3_value **),. v
6ed00 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f oid (*)(sqlite3_
6ed10 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
6ed20 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 20 ite3_value **),
6ed30 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 void (*)(sqlite3
6ed40 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 53 51 4c _context*));.SQL
6ed50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6ed60 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 73 sqlite3ApiExit(s
6ed70 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 29 qlite3 *db, int)
6ed80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ed90 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e int sqlite3Open
6eda0 54 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 TempDatabase(Par
6edb0 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 se *);..SQLITE_P
6edc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6edd0 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
6ede0 53 74 72 41 63 63 75 6d 2a 2c 20 63 68 61 72 2a StrAccum*, char*
6edf0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
6ee00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6ee10 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
6ee20 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d 2a Append(StrAccum*
6ee30 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 ,const char*,int
6ee40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ee50 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53 E char *sqlite3S
6ee60 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 74 trAccumFinish(St
6ee70 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 rAccum*);.SQLITE
6ee80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6ee90 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 lite3StrAccumRes
6eea0 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 et(StrAccum*);.S
6eeb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6eec0 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 id sqlite3Select
6eed0 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44 DestInit(SelectD
6eee0 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a est*,int,int);..
6eef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ef00 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 oid sqlite3Backu
6ef10 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33 pRestart(sqlite3
6ef20 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49 _backup *);.SQLI
6ef30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6ef40 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
6ef50 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ate(sqlite3_back
6ef60 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73 up *, Pgno, cons
6ef70 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t u8 *);../*.**
6ef80 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f The interface to
6ef90 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 the LEMON-gener
6efa0 61 74 65 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 ated parser.*/.S
6efb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6efc0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 id *sqlite3Parse
6efd0 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 rAlloc(void*(*)(
6efe0 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 size_t));.SQLITE
6eff0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6f000 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28 lite3ParserFree(
6f010 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 void*, void(*)(v
6f020 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 oid*));.SQLITE_P
6f030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f040 74 65 33 50 61 72 73 65 72 28 76 6f 69 64 2a 2c te3Parser(void*,
6f050 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 int, Token, Par
6f060 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 59 59 54 se*);.#ifdef YYT
6f070 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 RACKMAXSTACKDEPT
6f080 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 H.SQLITE_PRIVATE
6f090 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 int sqlite3Pa
6f0a0 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f rserStackPeak(vo
6f0b0 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 id*);.#endif..SQ
6f0c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6f0d0 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 sqlite3AutoLoad
6f0e0 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 Extensions(sqlit
6f0f0 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 e3*);.#ifndef SQ
6f100 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
6f110 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f XTENSION.SQLITE_
6f120 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6f130 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e qlite3CloseExten
6f140 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b sions(sqlite3*);
6f150 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
6f160 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 sqlite3CloseExte
6f170 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69 66 nsions(X).#endif
6f180 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
6f190 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
6f1a0 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 HE.SQLITE_PRIVAT
6f1b0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
6f1c0 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65 20 TableLock(Parse
6f1d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 2c *, int, int, u8,
6f1e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a const char *);.
6f1f0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
6f200 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
6f210 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 (v,w,x,y,z).#end
6f220 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
6f230 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
6f240 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
6f250 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 te3Utf8To8(unsig
6f260 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 ned char*);.#end
6f270 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
6f280 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
6f290 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 73 71 BLE.# define sq
6f2a0 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 58 lite3VtabClear(X
6f2b0 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 ).# define sqli
6f2c0 74 65 33 56 74 61 62 53 79 6e 63 28 58 2c 59 29 te3VtabSync(X,Y)
6f2d0 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 65 SQLITE_OK.# de
6f2e0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
6f2f0 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20 64 Rollback(X).# d
6f300 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
6f310 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64 65 bCommit(X).# de
6f320 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
6f330 49 6e 53 79 6e 63 28 64 62 29 20 30 0a 23 65 6c InSync(db) 0.#el
6f340 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 se.SQLITE_PRIVAT
6f350 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 E void sqlite
6f360 33 56 74 61 62 43 6c 65 61 72 28 54 61 62 6c 65 3VtabClear(Table
6f370 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f380 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 TE int sqlite
6f390 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74 65 3VtabSync(sqlite
6f3a0 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 29 3b 3 *db, char **);
6f3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f3c0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 int sqlite3Vt
6f3d0 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 abRollback(sqlit
6f3e0 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f e3 *db);.SQLITE_
6f3f0 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 PRIVATE int s
6f400 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 qlite3VtabCommit
6f410 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 (sqlite3 *db);.#
6f420 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
6f430 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 28 VtabInSync(db) (
6f440 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 (db)->nVTrans>0
6f450 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61 6e 73 && (db)->aVTrans
6f460 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 ==0).#endif.SQLI
6f470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f480 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 sqlite3VtabMakeW
6f490 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 ritable(Parse*,T
6f4a0 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 able*);.SQLITE_P
6f4b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f4c0 74 65 33 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69 te3VtabLock(sqli
6f4d0 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49 te3_vtab*);.SQLI
6f4e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f4f0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 sqlite3VtabUnloc
6f500 6b 28 73 71 6c 69 74 65 33 2a 2c 20 73 71 6c 69 k(sqlite3*, sqli
6f510 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49 te3_vtab*);.SQLI
6f520 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f530 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e sqlite3VtabBegin
6f540 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f Parse(Parse*, To
6f550 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f ken*, Token*, To
6f560 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6f570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f580 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 e3VtabFinishPars
6f590 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a e(Parse*, Token*
6f5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f5b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 E void sqlite3Vt
6f5c0 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a abArgInit(Parse*
6f5d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f5e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 E void sqlite3Vt
6f5f0 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 abArgExtend(Pars
6f600 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c e*, Token*);.SQL
6f610 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6f620 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 sqlite3VtabCallC
6f630 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 reate(sqlite3*,
6f640 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 int, const char
6f650 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c *, char **);.SQL
6f660 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6f670 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 sqlite3VtabCallC
6f680 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54 onnect(Parse*, T
6f690 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 able*);.SQLITE_P
6f6a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6f6b0 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f e3VtabCallDestro
6f6c0 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c y(sqlite3*, int,
6f6d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a const char *);.
6f6e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6f6f0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 nt sqlite3VtabBe
6f700 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 73 gin(sqlite3 *, s
6f710 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a qlite3_vtab *);.
6f720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 SQLITE_PRIVATE F
6f730 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 uncDef *sqlite3V
6f740 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 tabOverloadFunct
6f750 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 ion(sqlite3 *,Fu
6f760 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 ncDef*, int nArg
6f770 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6f780 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6f790 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 lite3InvalidFunc
6f7a0 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e tion(sqlite3_con
6f7b0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 text*,int,sqlite
6f7c0 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 3_value**);.SQLI
6f7d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6f7e0 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 qlite3TransferBi
6f7f0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 ndings(sqlite3_s
6f800 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73 tmt *, sqlite3_s
6f810 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tmt *);.SQLITE_P
6f820 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6f830 65 33 52 65 70 72 65 70 61 72 65 28 56 64 62 65 e3Reprepare(Vdbe
6f840 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f850 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6f860 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 xprListCheckLeng
6f870 74 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c th(Parse*, ExprL
6f880 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ist*, const char
6f890 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f8a0 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 TE CollSeq *sqli
6f8b0 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 te3BinaryCompare
6f8c0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c CollSeq(Parse *,
6f8d0 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20 2a 29 Expr *, Expr *)
6f8e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f8f0 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 6d 70 int sqlite3Temp
6f900 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73 InMemory(const s
6f910 71 6c 69 74 65 33 2a 29 3b 0a 0a 0a 0a 2f 2a 0a qlite3*);..../*.
6f920 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75 ** Available fau
6f930 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53 lt injectors. S
6f940 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65 hould be numbere
6f950 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 d beginning with
6f960 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 0..*/.#define S
6f970 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 QLITE_FAULTINJEC
6f980 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30 TOR_MALLOC 0
6f990 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
6f9a0 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f FAULTINJECTOR_CO
6f9b0 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a UNT 1../*.*
6f9c0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 * The interface
6f9d0 74 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66 to the code in f
6f9e0 61 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20 ault.c used for
6f9f0 69 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e identifying "ben
6fa00 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 ign".** malloc f
6fa10 61 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73 ailures. This is
6fa20 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66 only present if
6fa30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 SQLITE_OMIT_BUI
6fa40 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20 LTIN_TEST.** is
6fa50 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a not defined..*/.
6fa60 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
6fa70 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
6fa80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fa90 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 void sqlite3Be
6faa0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
6fab0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
6fac0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
6fad0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
6fae0 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 loc(void);.#else
6faf0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
6fb00 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
6fb10 6c 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20 loc(). #define
6fb20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
6fb30 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a Malloc().#endif.
6fb40 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 .#define IN_INDE
6fb50 58 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20 X_ROWID
6fb60 20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 1.#define IN_I
6fb70 4e 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20 NDEX_EPH
6fb80 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49 2.#define I
6fb90 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20 N_INDEX_INDEX
6fba0 20 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45 3.SQLITE
6fbb0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6fbc0 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 ite3FindInIndex(
6fbd0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c Parse *, Expr *,
6fbe0 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20 int*);..#ifdef
6fbf0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
6fc00 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54 OMIC_WRITE.SQLIT
6fc10 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
6fc20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 sqlite3JournalOp
6fc30 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a en(sqlite3_vfs *
6fc40 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
6fc50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20 sqlite3_file *,
6fc60 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
6fc70 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
6fc80 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 sqlite3JournalSi
6fc90 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ze(sqlite3_vfs *
6fca0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fcb0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a E int sqlite3J
6fcc0 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c ournalCreate(sql
6fcd0 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 ite3_file *);.#e
6fce0 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
6fcf0 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 lite3JournalSize
6fd00 28 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e (pVfs) ((pVfs)->
6fd10 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66 szOsFile).#endif
6fd20 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
6fd30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
6fd40 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 JournalOpen(sqli
6fd50 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c te3_file *);.SQL
6fd60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6fd70 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
6fd80 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c lSize(void);.SQL
6fd90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6fda0 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
6fdb0 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 nal(sqlite3_file
6fdc0 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 *);..#if SQLITE
6fdd0 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e _MAX_EXPR_DEPTH>
6fde0 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 0.SQLITE_PRIVATE
6fdf0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 void sqlite3E
6fe00 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 xprSetHeight(Par
6fe10 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
6fe20 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 *p);.SQLITE_PRI
6fe30 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6fe40 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 e3SelectExprHeig
6fe50 68 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51 ht(Select *);.SQ
6fe60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
6fe70 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 nt sqlite3ExprCh
6fe80 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a eckHeight(Parse*
6fe90 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20 , int);.#else.
6fea0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 #define sqlite3E
6feb0 78 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79 xprSetHeight(x,y
6fec0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
6fed0 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 te3SelectExprHei
6fee0 67 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69 ght(x) 0. #defi
6fef0 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 ne sqlite3ExprCh
6ff00 65 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23 eckHeight(x,y).#
6ff10 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
6ff20 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
6ff30 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 3Get4byte(const
6ff40 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u8*);.SQLITE_PRI
6ff50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6ff60 33 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75 3Put4byte(u8*, u
6ff70 33 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 32);..#ifdef SQL
6ff80 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 ITE_ENABLE_UNLOC
6ff90 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45 5f K_NOTIFY.SQLITE_
6ffa0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
6ffb0 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e qlite3Connection
6ffc0 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 Blocked(sqlite3
6ffd0 2a 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 *, sqlite3 *);.S
6ffe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6fff0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e void sqlite3Conn
70000 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 ectionUnlocked(s
70010 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c qlite3 *db);.SQL
70020 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
70030 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 id sqlite3Connec
70040 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74 tionClosed(sqlit
70050 65 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a 20 e3 *db);.#else.
70060 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
70070 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 ConnectionBlocke
70080 64 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 d(x,y). #define
70090 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 sqlite3Connecti
700a0 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20 20 onUnlocked(x).
700b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 #define sqlite3C
700c0 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 onnectionClosed(
700d0 78 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 x).#endif...#ifd
700e0 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 23 69 ef SQLITE_SSE.#i
700f0 6e 63 6c 75 64 65 20 22 73 73 65 49 6e 74 2e 68 nclude "sseInt.h
70100 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ".#endif..#ifdef
70110 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 SQLITE_DEBUG.SQ
70120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
70130 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 oid sqlite3Parse
70140 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68 rTrace(FILE*, ch
70150 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f ar *);.#endif../
70160 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 *.** If the SQLI
70170 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43 TE_ENABLE IOTRAC
70180 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68 E exists then th
70190 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c e global variabl
701a0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72 e.** sqlite3IoTr
701b0 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ace is a pointer
701c0 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b to a printf-lik
701d0 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 e routine used t
701e0 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74 o.** print I/O t
701f0 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e racing messages.
70200 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 .*/.#ifdef SQLI
70210 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 TE_ENABLE_IOTRAC
70220 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41 E.# define IOTRA
70230 43 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74 CE(A) if( sqlit
70240 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c e3IoTrace ){ sql
70250 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d ite3IoTrace A; }
70260 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70270 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
70280 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 beIOTraceSql(Vdb
70290 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
702a0 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 ATE void (*sqlit
702b0 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 e3IoTrace)(const
702c0 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c char*,...);.#el
702d0 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 se.# define IOTR
702e0 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20 ACE(A).# define
702f0 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 sqlite3VdbeIOTra
70300 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a ceSql(X).#endif.
70310 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
70320 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
70330 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
70340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70360 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
70370 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
70380 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a ile global.c ***
70390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
703a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
703b0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
703c0 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a 008 June 13.**.*
703d0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
703e0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
703f0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
70400 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
70410 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
70420 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
70430 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
70440 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
70450 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
70460 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
70470 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
70480 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
70490 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
704a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
704b0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
704c0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
704d0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
704e0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
704f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
70520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
70530 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
70540 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 ins definitions
70550 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 of global variab
70560 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73 les and contants
70570 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67 6c 6f ..**.** $Id: glo
70580 62 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 bal.c,v 1.12 200
70590 39 2f 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 9/02/05 16:31:46
705a0 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
705b0 2f 2a 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d /* An array to m
705c0 61 70 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 ap all upper-cas
705d0 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 e characters int
705e0 6f 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f o their correspo
705f0 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 nding.** lower-c
70600 61 73 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a ase character. .
70610 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c **.** SQLite onl
70620 79 20 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 y considers US-A
70630 53 43 49 49 20 28 6f 72 20 45 42 43 44 49 43 29 SCII (or EBCDIC)
70640 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 57 65 characters. We
70650 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c do not.** handl
70660 65 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f e case conversio
70670 6e 73 20 66 6f 72 20 74 68 65 20 55 54 46 20 63 ns for the UTF c
70680 68 61 72 61 63 74 65 72 20 73 65 74 20 73 69 6e haracter set sin
70690 63 65 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a ce the tables.**
706a0 20 69 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 involved are ne
706b0 61 72 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 arly as big or b
706c0 69 67 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 igger than SQLit
706d0 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c e itself..*/.SQL
706e0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
706f0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
70700 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
70710 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 wer[] = {.#ifdef
70720 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 SQLITE_ASCII.
70730 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 0, 1, 2,
70740 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 3, 4, 5, 6,
70750 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 7, 8, 9, 10, 1
70760 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 1, 12, 13, 14, 1
70770 35 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 5, 16, 17,.
70780 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 18, 19, 20, 21,
70790 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 22, 23, 24, 25,
707a0 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 26, 27, 28, 29,
707b0 33 30 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 30, 31, 32, 33,
707c0 33 34 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 34, 35,. 36,
707d0 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 37, 38, 39, 40,
707e0 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 41, 42, 43, 44,
707f0 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 45, 46, 47, 48,
70800 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 49, 50, 51, 52,
70810 20 35 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 53,. 54, 55
70820 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 , 56, 57, 58, 59
70830 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 , 60, 61, 62, 63
70840 2c 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 , 64, 97, 98, 99
70850 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 ,100,101,102,103
70860 2c 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 ,. 104,105,10
70870 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 6,107,108,109,11
70880 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 0,111,112,113,11
70890 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 4,115,116,117,11
708a0 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 8,119,120,121,.
708b0 20 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 122, 91, 92,
708c0 39 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 93, 94, 95, 96,
708d0 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 97, 98, 99,100,1
708e0 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 01,102,103,104,1
708f0 30 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 05,106,107,.
70900 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 108,109,110,111,
70910 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 112,113,114,115,
70920 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 116,117,118,119,
70930 31 32 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 120,121,122,123,
70940 31 32 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 124,125,. 126
70950 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 ,127,128,129,130
70960 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 ,131,132,133,134
70970 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 ,135,136,137,138
70980 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 ,139,140,141,142
70990 2c 31 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 ,143,. 144,14
709a0 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 5,146,147,148,14
709b0 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 9,150,151,152,15
709c0 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 3,154,155,156,15
709d0 37 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 7,158,159,160,16
709e0 31 2c 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 1,. 162,163,1
709f0 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 64,165,166,167,1
70a00 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 68,169,170,171,1
70a10 37 32 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 72,173,174,175,1
70a20 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 76,177,178,179,.
70a30 20 20 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 180,181,182,
70a40 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 183,184,185,186,
70a50 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 187,188,189,190,
70a60 31 39 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 191,192,193,194,
70a70 31 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 195,196,197,.
70a80 20 31 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 198,199,200,201
70a90 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 ,202,203,204,205
70aa0 2c 32 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 ,206,207,208,209
70ab0 2c 32 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 ,210,211,212,213
70ac0 2c 32 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 ,214,215,. 21
70ad0 36 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 6,217,218,219,22
70ae0 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 0,221,222,223,22
70af0 34 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 4,225,226,227,22
70b00 38 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 8,229,230,231,23
70b10 32 2c 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 2,233,. 234,2
70b20 33 35 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 35,236,237,238,2
70b30 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 39,240,241,242,2
70b40 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 43,244,245,246,2
70b50 34 37 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 47,248,249,250,2
70b60 35 31 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 51,. 252,253,
70b70 32 35 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 254,255.#endif.#
70b80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 ifdef SQLITE_EBC
70b90 44 49 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c DIC. 0, 1,
70ba0 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 2, 3, 4, 5,
70bb0 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 6, 7, 8, 9,
70bc0 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 10, 11, 12, 13,
70bd0 20 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 14, 15, /* 0x *
70be0 2f 0a 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 /. 16, 17, 1
70bf0 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 8, 19, 20, 21, 2
70c00 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 2, 23, 24, 25, 2
70c10 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 6, 27, 28, 29, 3
70c20 30 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 0, 31, /* 1x */.
70c30 20 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 32, 33, 34,
70c40 20 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 35, 36, 37, 38,
70c50 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 39, 40, 41, 42,
70c60 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 43, 44, 45, 46,
70c70 20 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 47, /* 2x */.
70c80 20 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 48, 49, 50, 5
70c90 31 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 1, 52, 53, 54, 5
70ca0 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 5, 56, 57, 58, 5
70cb0 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 9, 60, 61, 62, 6
70cc0 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 3, /* 3x */.
70cd0 20 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 64, 65, 66, 67,
70ce0 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 68, 69, 70, 71,
70cf0 20 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 72, 73, 74, 75,
70d00 20 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 76, 77, 78, 79,
70d10 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 /* 4x */. 8
70d20 30 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 0, 81, 82, 83, 8
70d30 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 4, 85, 86, 87, 8
70d40 38 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 8, 89, 90, 91, 9
70d50 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2, 93, 94, 95, /
70d60 2a 20 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c * 5x */. 96,
70d70 20 39 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 97, 66, 67, 68,
70d80 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 69, 70, 71, 72,
70d90 20 37 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 73,106,107,108,
70da0 31 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 109,110,111, /*
70db0 36 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 6x */. 112, 8
70dc0 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 1, 82, 83, 84, 8
70dd0 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 5, 86, 87, 88, 8
70de0 39 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 9,122,123,124,12
70df0 35 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 5,126,127, /* 7x
70e00 20 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c */. 128,129,
70e10 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 130,131,132,133,
70e20 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 134,135,136,137,
70e30 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 138,139,140,141,
70e40 31 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 142,143, /* 8x *
70e50 2f 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 /. 144,145,14
70e60 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 6,147,148,149,15
70e70 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 0,151,152,153,15
70e80 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 4,155,156,157,15
70e90 36 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 6,159, /* 9x */.
70ea0 20 20 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 160,161,162,
70eb0 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 163,164,165,166,
70ec0 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 167,168,169,170,
70ed0 31 37 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 171,140,141,142,
70ee0 31 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 175, /* Ax */.
70ef0 20 20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 176,177,178,17
70f00 39 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 9,180,181,182,18
70f10 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 3,184,185,186,18
70f20 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 7,188,189,190,19
70f30 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 1, /* Bx */.
70f40 31 39 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 192,129,130,131,
70f50 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 132,133,134,135,
70f60 31 33 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 136,137,202,203,
70f70 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 204,205,206,207,
70f80 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 /* Cx */. 20
70f90 38 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 8,145,146,147,14
70fa0 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 8,149,150,151,15
70fb0 32 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 2,153,218,219,22
70fc0 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 0,221,222,223, /
70fd0 2a 20 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c * Dx */. 224,
70fe0 32 32 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 225,162,163,164,
70ff0 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 165,166,167,168,
71000 31 36 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 169,232,203,204,
71010 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 205,206,207, /*
71020 45 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 Ex */. 239,24
71030 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 0,241,242,243,24
71040 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 4,245,246,247,24
71050 38 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 8,249,219,220,22
71060 31 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 1,222,255, /* Fx
71070 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f */.#endif.};../
71080 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
71090 6e 67 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b ng 256 byte look
710a0 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 up table is used
710b0 20 74 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69 to support SQLi
710c0 74 65 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 tes built-in.**
710d0 65 71 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 equivalents to t
710e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 he following sta
710f0 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 66 75 ndard library fu
71100 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 nctions:.**.**
71110 20 69 73 73 70 61 63 65 28 29 20 20 20 20 20 20 isspace()
71120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71130 20 20 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 0x01.** isal
71140 70 68 61 28 29 20 20 20 20 20 20 20 20 20 20 20 pha()
71150 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
71160 32 0a 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 2.** isdigit()
71170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71180 20 20 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 0x04.**
71190 20 20 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 isalnum()
711a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
711b0 20 20 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 0x06.** isx
711c0 64 69 67 69 74 28 29 20 20 20 20 20 20 20 20 20 digit()
711d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 0x
711e0 30 38 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 08.** toupper(
711f0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
71200 20 20 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0x20.**
71210 0a 2a 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 .** Bit 0x20 is
71220 73 65 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 set if the mappe
71230 64 20 63 68 61 72 61 63 74 65 72 20 72 65 71 75 d character requ
71240 69 72 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e ires translation
71250 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 to upper.** cas
71260 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63 e. i.e. if the c
71270 68 61 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f haracter is a lo
71280 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 wer-case ASCII c
71290 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 haracter..** If
712a0 78 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 x is a lower-cas
712b0 65 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65 e ASCII characte
712c0 72 2c 20 74 68 65 6e 20 69 74 73 20 75 70 70 65 r, then its uppe
712d0 72 2d 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e r-case equivalen
712e0 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32 t.** is (x - 0x2
712f0 30 29 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f 0). Therefore to
71300 75 70 70 65 72 28 29 20 63 61 6e 20 62 65 20 69 upper() can be i
71310 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a mplemented as:.*
71320 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 *.** (x & ~(ma
71330 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a p[x]&0x20)).**.*
71340 2a 20 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74 * Standard funct
71350 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 ion tolower() is
71360 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 implemented usi
71370 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 55 70 ng the sqlite3Up
71380 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 perToLower[].**
71390 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 array. tolower()
713a0 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 is used more of
713b0 74 65 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72 ten than toupper
713c0 28 29 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a () by SQLite..**
713d0 0a 2a 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72 .** SQLite's ver
713e0 73 69 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 sions are identi
713f0 63 61 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64 cal to the stand
71400 61 72 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73 ard versions ass
71410 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c uming a.** local
71420 65 20 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61 e of "C". They a
71430 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 re implemented a
71440 73 20 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 s macros in sqli
71450 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 teInt.h..*/.#ifd
71460 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a ef SQLITE_ASCII.
71470 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
71480 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
71490 61 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d ar sqlite3CtypeM
714a0 61 70 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 ap[256] = {. 0x
714b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
714c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
714d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
714e0 2a 20 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e * 00..07 ....
714f0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
71500 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 0x01, 0x01, 0x01
71510 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 , 0x01, 0x01, 0x
71520 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 00, 0x00, /* 08
71530 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..0f ........
71540 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
71550 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71560 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71570 30 78 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 0x00, /* 10..17
71580 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
71590 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
715a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
715b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
715c0 2c 20 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 , /* 18..1f
715d0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
715e0 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 01, 0x00, 0x00,
715f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71600 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
71610 2a 20 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23 * 20..27 !"#
71620 24 25 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 $%&' */. 0x00,
71630 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71640 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71650 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 00, 0x00, /* 28
71660 2e 2e 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f ..2f ()*+,-./
71670 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 */. 0x0c, 0x0c
71680 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 , 0x0c, 0x0c, 0x
71690 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 0c, 0x0c, 0x0c,
716a0 30 78 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 0x0c, /* 30..37
716b0 20 20 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 01234567 */.
716c0 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 0x0c, 0x0c, 0x
716d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
716e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
716f0 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 , /* 38..3f
71700 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 89:;<=>? */.. 0
71710 78 30 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c x00, 0x0a, 0x0a,
71720 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 0x0a, 0x0a, 0x0
71730 61 2c 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20 a, 0x0a, 0x02,
71740 2f 2a 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42 /* 40..47 @AB
71750 43 44 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c CDEFG */. 0x02,
71760 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
71770 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
71780 78 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 x02, 0x02, /* 4
71790 38 2e 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 8..4f HIJKLMN
717a0 4f 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 O */. 0x02, 0x0
717b0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
717c0 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c x02, 0x02, 0x02,
717d0 20 30 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 0x02, /* 50..5
717e0 37 20 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f 7 PQRSTUVW */
717f0 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 . 0x02, 0x02, 0
71800 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x02, 0x00, 0x00,
71810 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71820 30 2c 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 0, /* 58..5f
71830 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 XYZ[\]^_ */. 0
71840 78 30 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c x00, 0x2a, 0x2a,
71850 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 0x2a, 0x2a, 0x2
71860 61 2c 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20 a, 0x2a, 0x22,
71870 2f 2a 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62 /* 60..67 `ab
71880 63 64 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c cdefg */. 0x22,
71890 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
718a0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 2, 0x22, 0x22, 0
718b0 78 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 x22, 0x22, /* 6
718c0 38 2e 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 8..6f hijklmn
718d0 6f 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 o */. 0x22, 0x2
718e0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 2, 0x22, 0x22, 0
718f0 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c x22, 0x22, 0x22,
71900 20 30 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 0x22, /* 70..7
71910 37 20 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f 7 pqrstuvw */
71920 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 . 0x22, 0x22, 0
71930 78 32 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x22, 0x00, 0x00,
71940 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71950 30 2c 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 0, /* 78..7f
71960 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 xyz{|}~. */..
71970 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71980 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71990 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
719a0 20 2f 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e /* 80..87 ..
719b0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
719c0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
719d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
719e0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
719f0 38 38 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 88..8f ......
71a00 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
71a10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71a20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71a30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e , 0x00, /* 90..
71a40 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 97 ........ *
71a50 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
71a60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71a70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71a80 30 30 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 00, /* 98..9f
71a90 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
71aa0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71ab0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71ac0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71ad0 20 2f 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e /* a0..a7 ..
71ae0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
71af0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71b00 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71b10 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
71b20 61 38 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e a8..af ......
71b30 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
71b40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
71b50 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71b60 2c 20 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e , 0x00, /* b0..
71b70 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a b7 ........ *
71b80 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
71b90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
71ba0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
71bb0 30 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 00, /* b8..bf
71bc0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 ........ */..
71bd0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71be0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71bf0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71c00 20 20 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e /* c0..c7 .
71c10 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
71c20 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71c30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71c40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
71c50 20 63 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e c8..cf .....
71c60 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
71c70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71c80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71c90 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 0, 0x00, /* d0.
71ca0 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .d7 ........
71cb0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
71cc0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71cd0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71ce0 78 30 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 x00, /* d8..df
71cf0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
71d00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71d10 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71d20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71d30 20 20 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e /* e0..e7 .
71d40 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
71d50 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71d60 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71d70 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
71d80 20 65 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e e8..ef .....
71d90 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
71da0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
71db0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71dc0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 0, 0x00, /* f0.
71dd0 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .f7 ........
71de0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
71df0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
71e00 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
71e10 78 30 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 x00 /* f8..ff
71e20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d ........ */.}
71e30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a ;.#endif..../*.*
71e40 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
71e50 73 69 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 singleton contai
71e60 6e 73 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f ns the global co
71e70 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a nfiguration for.
71e80 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 ** the SQLite li
71e90 62 72 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 brary..*/.SQLITE
71ea0 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f _PRIVATE SQLITE_
71eb0 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 WSD struct Sqlit
71ec0 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 e3Config sqlite3
71ed0 43 6f 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 Config = {. SQ
71ee0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d LITE_DEFAULT_MEM
71ef0 53 54 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d STATUS, /* bMem
71f00 73 74 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 stat */. 1,
71f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71f20 20 20 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 /* bCoreMu
71f30 74 65 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 tex */. SQLITE
71f40 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 _THREADSAFE==1,
71f50 20 20 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 /* bFullMut
71f60 65 78 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 ex */. 0x7ffff
71f70 66 66 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ffe,
71f80 20 20 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 /* mxStrlen
71f90 2a 2f 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20 */. 100,
71fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71fb0 20 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 /* szLookaside
71fc0 2a 2f 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20 */. 500,
71fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71fe0 20 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a /* nLookaside *
71ff0 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c /. {0,0,0,0,0,
72000 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 0,0,0},
72010 2f 2a 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c /* m */. {0,0,
72020 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 0,0,0,0,0,0,0},
72030 20 20 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a /* mutex *
72040 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c /. {0,0,0,0,0,
72050 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 0,0,0,0,0,0},
72060 2f 2a 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 /* pcache */.
72070 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 (void*)0,
72080 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 /* pH
72090 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 eap */. 0,
720a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
720b0 20 20 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f /* nHeap */
720c0 0a 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 . 0, 0,
720d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
720e0 2a 20 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 * mnHeap, mxHeap
720f0 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c */. (void*)0,
72100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72110 20 20 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f /* pScratch */
72120 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
72130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
72140 2a 20 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 * szScratch */.
72150 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
72160 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
72170 6e 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 nScratch */. (
72180 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 void*)0,
72190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 /* pPa
721a0 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ge */. 0,
721b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
721c0 20 20 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f /* szPage */
721d0 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
721e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
721f0 2a 20 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c * nPage */. 0,
72200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72210 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 /* mxPa
72220 72 73 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 rserStack */.
72230 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
72240 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 /* sh
72250 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 aredCacheEnabled
72260 20 2a 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 */. /* All th
72270 65 20 72 65 73 74 20 6e 65 65 64 20 74 6f 20 61 e rest need to a
72280 6c 77 61 79 73 20 62 65 20 7a 65 72 6f 20 2a 2f lways be zero */
72290 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
722a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
722b0 2a 20 69 73 49 6e 69 74 20 2a 2f 0a 20 20 20 30 * isInit */. 0
722c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
722d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 50 /* inP
722e0 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 20 30 2c rogress */. 0,
722f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72300 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 /* isMa
72310 6c 6c 6f 63 49 6e 69 74 20 2a 2f 0a 20 20 20 30 llocInit */. 0
72320 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
72330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 49 6e /* pIn
72340 69 74 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c itMutex */. 0,
72350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72360 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 52 65 66 /* nRef
72370 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a InitMutex */.};.
72380 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62 ../*.** Hash tab
72390 6c 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75 le for global fu
723a0 6e 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69 nctions - functi
723b0 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c ons common to al
723c0 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f l.** database co
723d0 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66 74 65 nnections. Afte
723e0 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e r initialization
723f0 2c 20 74 68 69 73 20 74 61 62 6c 65 20 69 73 0a , this table is.
72400 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f ** read-only..*/
72410 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72420 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 SQLITE_WSD FuncD
72430 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c efHash sqlite3Gl
72440 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a obalFunctions;..
72450 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 /*.** The value
72460 6f 66 20 74 68 65 20 22 70 65 6e 64 69 6e 67 22 of the "pending"
72470 20 62 79 74 65 20 6d 75 73 74 20 62 65 20 30 78 byte must be 0x
72480 34 30 30 30 30 30 30 30 20 28 31 20 62 79 74 65 40000000 (1 byte
72490 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67 past the.** 1-g
724a0 69 62 61 62 79 74 65 20 62 6f 75 6e 64 61 72 79 ibabyte boundary
724b0 29 20 69 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c ) in a compatibl
724c0 65 20 64 61 74 61 62 61 73 65 2e 20 20 53 51 4c e database. SQL
724d0 69 74 65 20 6e 65 76 65 72 20 75 73 65 73 0a 2a ite never uses.*
724e0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 * the database p
724f0 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e age that contain
72500 73 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 s the pending by
72510 74 65 2e 20 20 49 74 20 6e 65 76 65 72 20 61 74 te. It never at
72520 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61 tempts.** to rea
72530 64 20 6f 72 20 77 72 69 74 65 20 74 68 61 74 20 d or write that
72540 70 61 67 65 2e 20 20 54 68 65 20 70 65 6e 64 69 page. The pendi
72550 6e 67 20 62 79 74 65 20 70 61 67 65 20 69 73 20 ng byte page is
72560 73 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f set assign.** fo
72570 72 20 75 73 65 20 62 79 20 74 68 65 20 56 46 53 r use by the VFS
72580 20 6c 61 79 65 72 73 20 61 73 20 73 70 61 63 65 layers as space
72590 20 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20 66 69 for managing fi
725a0 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 le locks..**.**
725b0 44 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 During testing,
725c0 69 74 20 69 73 20 6f 66 74 65 6e 20 64 65 73 69 it is often desi
725d0 72 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68 rable to move th
725e0 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 e pending byte t
725f0 6f 0a 2a 2a 20 61 20 64 69 66 66 65 72 65 6e 74 o.** a different
72600 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 position in the
72610 20 66 69 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c file. This all
72620 6f 77 73 20 63 6f 64 65 20 74 68 61 74 20 68 61 ows code that ha
72630 73 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74 s to.** deal wit
72640 68 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 h the pending by
72650 74 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c te to run on fil
72660 65 73 20 74 68 61 74 20 61 72 65 20 6d 75 63 68 es that are much
72670 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e smaller.** than
72680 20 31 20 47 69 42 2e 20 20 54 68 65 20 73 71 6c 1 GiB. The sql
72690 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f ite3_test_contro
726a0 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 l() interface ca
726b0 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 n be used to.**
726c0 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e 67 move the pending
726d0 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 byte..**.** IMP
726e0 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e ORTANT: Changin
726f0 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 g the pending by
72700 74 65 20 74 6f 20 61 6e 79 20 76 61 6c 75 65 20 te to any value
72710 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78 other than.** 0x
72720 34 30 30 30 30 30 30 30 20 72 65 73 75 6c 74 73 40000000 results
72730 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 in an incompati
72740 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c ble database fil
72750 65 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61 e format!.** Cha
72760 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e nging the pendin
72770 67 20 62 79 74 65 20 64 75 72 69 6e 67 20 6f 70 g byte during op
72780 65 72 61 74 69 6e 67 20 72 65 73 75 6c 74 73 20 erating results
72790 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 in undefined.**
727a0 61 6e 64 20 64 69 6c 65 74 65 72 69 6f 75 73 20 and dileterious
727b0 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c behavior..*/.SQL
727c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
727d0 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 sqlite3PendingBy
727e0 74 65 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b te = 0x40000000;
727f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
72800 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e * End of global.
72810 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
72820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72840 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
72850 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74 61 * Begin file sta
72860 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tus.c **********
72870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72890 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e /./*.** 2008 Jun
728a0 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 e 18.**.** The a
728b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
728c0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
728d0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
728e0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
728f0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
72900 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
72910 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
72920 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
72930 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
72940 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
72950 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
72960 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
72970 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
72980 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
72990 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
729a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
729b0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
729c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
729f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
72a00 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d ***.**.** This m
72a10 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 odule implements
72a20 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 the sqlite3_sta
72a30 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 65 20 tus() interface
72a40 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 and related.** f
72a50 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a unctionality..**
72a60 0a 2a 2a 20 24 49 64 3a 20 73 74 61 74 75 73 2e .** $Id: status.
72a70 63 2c 76 20 31 2e 39 20 32 30 30 38 2f 30 39 2f c,v 1.9 2008/09/
72a80 30 32 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 02 00:52:52 drh
72a90 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
72aa0 56 61 72 69 61 62 6c 65 73 20 69 6e 20 77 68 69 Variables in whi
72ab0 63 68 20 74 6f 20 72 65 63 6f 72 64 20 73 74 61 ch to record sta
72ac0 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e tus information.
72ad0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
72ae0 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 ct sqlite3StatTy
72af0 70 65 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 pe sqlite3StatTy
72b00 70 65 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 pe;.static SQLIT
72b10 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c E_WSD struct sql
72b20 69 74 65 33 53 74 61 74 54 79 70 65 20 7b 0a 20 ite3StatType {.
72b30 20 69 6e 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d int nowValue[9]
72b40 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 ; /* Cur
72b50 72 65 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 rent value */.
72b60 69 6e 74 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 int mxValue[9];
72b70 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
72b80 6d 75 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 mum value */.} s
72b90 71 6c 69 74 65 33 53 74 61 74 20 3d 20 7b 20 7b qlite3Stat = { {
72ba0 30 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 0,}, {0,} };.../
72bb0 2a 20 54 68 65 20 22 77 73 64 53 74 61 74 22 20 * The "wsdStat"
72bc0 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c macro will resol
72bd0 76 65 20 74 6f 20 74 68 65 20 73 74 61 74 75 73 ve to the status
72be0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
72bf0 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 state vector. I
72c00 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 f writable stati
72c10 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 c data is unsupp
72c20 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 orted on the tar
72c30 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 get,.** we have
72c40 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 to locate the st
72c50 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 ate vector at ru
72c60 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 n-time. In the
72c70 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 more common.** c
72c80 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 ase where writab
72c90 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 le static data i
72ca0 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 s supported, wsd
72cb0 53 74 61 74 20 63 61 6e 20 72 65 66 65 72 20 64 Stat can refer d
72cc0 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 irectly.** to th
72cd0 65 20 22 73 71 6c 69 74 65 33 53 74 61 74 22 20 e "sqlite3Stat"
72ce0 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 state vector dec
72cf0 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a lared above..*/.
72d00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
72d10 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 IT_WSD.# define
72d20 77 73 64 53 74 61 74 49 6e 69 74 20 20 73 71 6c wsdStatInit sql
72d30 69 74 65 33 53 74 61 74 54 79 70 65 20 2a 78 20 ite3StatType *x
72d40 3d 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 = &GLOBAL(sqlite
72d50 33 53 74 61 74 54 79 70 65 2c 73 71 6c 69 74 65 3StatType,sqlite
72d60 33 53 74 61 74 29 0a 23 20 64 65 66 69 6e 65 20 3Stat).# define
72d70 77 73 64 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c wsdStat x[0].#el
72d80 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 se.# define wsdS
72d90 74 61 74 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 tatInit.# define
72da0 20 77 73 64 53 74 61 74 20 73 71 6c 69 74 65 33 wsdStat sqlite3
72db0 53 74 61 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a Stat.#endif../*.
72dc0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 ** Return the cu
72dd0 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 rrent value of a
72de0 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 status paramete
72df0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
72e00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72e10 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 20 StatusValue(int
72e20 6f 70 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e op){. wsdStatIn
72e30 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 it;. assert( op
72e40 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 >=0 && op<ArrayS
72e50 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 ize(wsdStat.nowV
72e60 61 6c 75 65 29 20 29 3b 0a 20 20 72 65 74 75 72 alue) );. retur
72e70 6e 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c n wsdStat.nowVal
72e80 75 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ue[op];.}../*.**
72e90 20 41 64 64 20 4e 20 74 6f 20 74 68 65 20 76 61 Add N to the va
72ea0 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 lue of a status
72eb0 72 65 63 6f 72 64 2e 20 20 49 74 20 69 73 20 61 record. It is a
72ec0 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a ssumed that the.
72ed0 2a 2a 20 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 ** caller holds
72ee0 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b appropriate lock
72ef0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
72f00 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
72f10 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 20 6f 3StatusAdd(int o
72f20 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 p, int N){. wsd
72f30 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 StatInit;. asse
72f40 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c rt( op>=0 && op<
72f50 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 ArraySize(wsdSta
72f60 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 t.nowValue) );.
72f70 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
72f80 65 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 e[op] += N;. if
72f90 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c ( wsdStat.nowVal
72fa0 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d ue[op]>wsdStat.m
72fb0 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 xValue[op] ){.
72fc0 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 wsdStat.mxValu
72fd0 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e e[op] = wsdStat.
72fe0 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 nowValue[op];.
72ff0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 }.}../*.** Set t
73000 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 he value of a st
73010 61 74 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 atus to X..*/.SQ
73020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73030 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 d sqlite3StatusS
73040 65 74 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 et(int op, int X
73050 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 ){. wsdStatInit
73060 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d ;. assert( op>=
73070 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 0 && op<ArraySiz
73080 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c e(wsdStat.nowVal
73090 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 ue) );. wsdStat
730a0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 .nowValue[op] =
730b0 58 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 74 X;. if( wsdStat
730c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 .nowValue[op]>ws
730d0 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 dStat.mxValue[op
730e0 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 ] ){. wsdStat
730f0 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 .mxValue[op] = w
73100 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
73110 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a op];. }.}../*.*
73120 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 * Query status i
73130 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a nformation..**.*
73140 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 * This implement
73150 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ation assumes th
73160 61 74 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 at reading or wr
73170 69 74 69 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 iting an aligned
73180 0a 2a 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67 .** 32-bit integ
73190 65 72 20 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 er is an atomic
731a0 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 operation. If t
731b0 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 hat assumption i
731c0 73 20 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 s not true,.** t
731d0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
731e0 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 is not threadsa
731f0 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 fe..*/.SQLITE_AP
73200 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 I int sqlite3_st
73210 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 atus(int op, int
73220 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 *pCurrent, int
73230 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 *pHighwater, int
73240 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 resetFlag){. w
73250 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 sdStatInit;. if
73260 28 20 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 ( op<0 || op>=Ar
73270 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e raySize(wsdStat.
73280 6e 6f 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 nowValue) ){.
73290 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d return SQLITE_M
732a0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 ISUSE;. }. *pC
732b0 75 72 72 65 6e 74 20 3d 20 77 73 64 53 74 61 74 urrent = wsdStat
732c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 .nowValue[op];.
732d0 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 77 *pHighwater = w
732e0 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f sdStat.mxValue[o
732f0 70 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 p];. if( resetF
73300 6c 61 67 20 29 7b 0a 20 20 20 20 77 73 64 53 74 lag ){. wsdSt
73310 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d at.mxValue[op] =
73320 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
73330 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 e[op];. }. ret
73340 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
73350 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 ../*.** Query st
73360 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e atus information
73370 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 for a single da
73380 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
73390 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 n.*/.SQLITE_API
733a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 int sqlite3_db_s
733b0 74 61 74 75 73 28 0a 20 20 73 71 6c 69 74 65 33 tatus(. sqlite3
733c0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f *db, /
733d0 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
733e0 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 onnection whose
733f0 73 74 61 74 75 73 20 69 73 20 64 65 73 69 72 65 status is desire
73400 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 d */. int op,
73410 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
73420 53 74 61 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 Status verb */.
73430 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 int *pCurrent,
73440 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
73450 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 68 65 current value he
73460 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 re */. int *pHi
73470 67 68 77 61 74 65 72 2c 20 20 20 20 20 20 2f 2a ghwater, /*
73480 20 57 72 69 74 65 20 68 69 67 68 2d 77 61 74 65 Write high-wate
73490 72 20 6d 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 r mark here */.
734a0 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67 20 20 int resetFlag
734b0 20 20 20 20 20 20 20 2f 2a 20 52 65 73 65 74 20 /* Reset
734c0 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 high-water mark
734d0 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 if true */.){.
734e0 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 switch( op ){.
734f0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 case SQLITE_DB
73500 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 STATUS_LOOKASIDE
73510 5f 55 53 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a _USED: {. *
73520 70 43 75 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c pCurrent = db->l
73530 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 ookaside.nOut;.
73540 20 20 20 20 20 2a 70 48 69 67 68 77 61 74 65 72 *pHighwater
73550 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 = db->lookaside
73560 2e 6d 78 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 .mxOut;. if
73570 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 ( resetFlag ){.
73580 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 db->looka
73590 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d side.mxOut = db-
735a0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b >lookaside.nOut;
735b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
735c0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
735d0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
735e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
735f0 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a RROR;. }. }.
73600 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
73610 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a OK;.}../********
73620 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 ****** End of st
73630 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a atus.c *********
73640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73660 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
73670 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
73680 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a e date.c *******
73690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
736a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
736b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
736c0 33 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 3 October 31.**.
736d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
736e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
736f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
73700 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
73710 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
73720 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
73730 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
73740 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
73750 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
73760 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
73770 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
73780 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
73790 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
737a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
737b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
737c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
737d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
737e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
737f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
73820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
73830 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
73840 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e s the C function
73850 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
73860 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a date and time.*
73870 2a 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 * functions for
73880 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 SQLite. .**.**
73890 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e There is only on
738a0 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f e exported symbo
738b0 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d l in this file -
738c0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a the function.**
738d0 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
738e0 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e DateTimeFunction
738f0 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65 s() found at the
73900 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 bottom of the f
73910 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 ile..** All othe
73920 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20 r code has file
73930 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 scope..**.** $Id
73940 3a 20 64 61 74 65 2e 63 2c 76 20 31 2e 31 30 37 : date.c,v 1.107
73950 20 32 30 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 2009/05/03 20:2
73960 33 3a 35 33 20 64 72 68 20 45 78 70 20 24 0a 2a 3:53 drh Exp $.*
73970 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 *.** SQLite proc
73980 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 esses all times
73990 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c and dates as Jul
739a0 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e ian Day numbers.
739b0 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 The.** dates a
739c0 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f nd times are sto
739d0 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65 red as the numbe
739e0 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 r of days since
739f0 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e noon.** in Green
73a00 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 wich on November
73a10 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 24, 4714 B.C. a
73a20 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
73a30 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c Gregorian.** cal
73a40 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a endar system. .*
73a50 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 *.** 1970-01-01
73a60 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 00:00:00 is JD 2
73a70 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 440587.5.** 2000
73a80 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 -01-01 00:00:00
73a90 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a is JD 2451544.5.
73aa0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 **.** This imple
73ab0 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73 mention requires
73ac0 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 78 70 years to be exp
73ad0 72 65 73 73 65 64 20 61 73 20 61 20 34 2d 64 69 ressed as a 4-di
73ae0 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 git number.** wh
73af0 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f ich means that o
73b00 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 65 65 nly dates betwee
73b10 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 n 0000-01-01 and
73b20 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 9999-12-31 can.
73b30 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 ** be represente
73b40 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a d, even though j
73b50 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 ulian day number
73b60 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 s allow a much w
73b70 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 ider.** range of
73b80 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 dates..**.** Th
73b90 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 e Gregorian cale
73ba0 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 20 75 ndar system is u
73bb0 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 sed for all date
73bc0 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 s and times,.**
73bd0 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20 even those that
73be0 70 72 65 64 61 74 65 20 74 68 65 20 47 72 65 67 predate the Greg
73bf0 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 orian calendar.
73c00 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61 Historians usua
73c10 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a lly.** use the J
73c20 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 ulian calendar f
73c30 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 20 74 or dates prior t
73c40 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 o 1582-10-15 and
73c50 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 for some.** dat
73c60 65 73 20 61 66 74 65 72 77 61 72 64 73 2c 20 64 es afterwards, d
73c70 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 epending on loca
73c80 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74 le. Beware of t
73c90 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a his difference..
73ca0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 **.** The conver
73cb0 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 sion algorithms
73cc0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 are implemented
73cd0 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70 based on descrip
73ce0 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 tions.** in the
73cf0 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a following text:.
73d00 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 **.** Jean
73d10 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 Meeus.** As
73d20 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 tronomical Algor
73d30 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 ithms, 2nd Editi
73d40 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 on, 1998.**
73d50 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 ISBM 0-943396-6
73d60 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 1-1.** Will
73d70 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a mann-Bell, Inc.*
73d80 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c * Richmond,
73d90 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a Virginia (USA).
73da0 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d */.#include <tim
73db0 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 e.h>..#ifndef SQ
73dc0 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 LITE_OMIT_DATETI
73dd0 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 ME_FUNCS../*.**
73de0 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f 77 On recent Window
73df0 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65 s platforms, the
73e00 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20 66 localtime_s() f
73e10 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69 6c unction is avail
73e20 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 able.** as part
73e30 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20 43 of the "Secure C
73e40 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65 6e RT". It is essen
73e50 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e tially equivalen
73e60 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 t to .** localti
73e70 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c 65 me_r() available
73e80 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53 49 under most POSI
73e90 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63 X platforms, exc
73ea0 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a 2a ept that the .**
73eb0 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70 61 order of the pa
73ec0 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 rameters is reve
73ed0 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 rsed..**.** See
73ee0 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 http://msdn.micr
73ef0 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f osoft.com/en-us/
73f00 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79 65 library/a442x3ye
73f10 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a (VS.80).aspx..**
73f20 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72 20 .** If the user
73f30 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74 65 has not indicate
73f40 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69 d to use localti
73f50 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74 me_r() or localt
73f60 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61 ime_s().** alrea
73f70 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e dy, check for an
73f80 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76 69 MSVC build envi
73f90 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72 6f ronment that pro
73fa0 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 vides .** localt
73fb0 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 ime_s()..*/.#if
73fc0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f !defined(HAVE_LO
73fd0 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21 64 CALTIME_R) && !d
73fe0 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 efined(HAVE_LOCA
73ff0 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20 LTIME_S) && \.
74000 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f defined(_MSC_
74010 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64 28 VER) && defined(
74020 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44 45 _CRT_INSECURE_DE
74030 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e 65 PRECATE).#define
74040 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f HAVE_LOCALTIME_
74050 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a S 1.#endif../*.*
74060 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66 6f * A structure fo
74070 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 r holding a sing
74080 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 le date and time
74090 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
740a0 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61 74 uct DateTime Dat
740b0 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 eTime;.struct Da
740c0 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 teTime {. sqlit
740d0 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a e3_int64 iJD; /*
740e0 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 The julian day
740f0 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36 34 number times 864
74100 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 00000 */. int Y
74110 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a , M, D; /*
74120 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e Year, month, an
74130 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 d day */. int h
74140 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a , m; /*
74150 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 Hour and minute
74160 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 s */. int tz;
74170 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d /* Tim
74180 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 ezone offset in
74190 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 minutes */. dou
741a0 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20 20 ble s;
741b0 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 /* Seconds */.
741c0 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 char validYMD;
741d0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 /* True (1) i
741e0 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 f Y,M,D are vali
741f0 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 d */. char vali
74200 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 dHMS; /* Tru
74210 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 e (1) if h,m,s a
74220 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 re valid */. ch
74230 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 ar validJD;
74240 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 /* True (1) if
74250 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a iJD is valid */.
74260 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 char validTZ;
74270 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 /* True (1)
74280 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20 if tz is valid
74290 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f */.};.../*.** Co
742a0 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74 6f nvert zDate into
742b0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 one or more int
742c0 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f 6e egers. Addition
742d0 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 al arguments.**
742e0 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 6f come in groups o
742f0 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a f 5 as follows:.
74300 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 **.** N
74310 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 number of di
74320 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74 65 gits in the inte
74330 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e ger.** min
74340 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c minimum all
74350 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 owed value of th
74360 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 e integer.**
74370 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69 6d max maxim
74380 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 um allowed value
74390 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a of the integer.
743a0 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20 20 ** nextC
743b0 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72 first character
743c0 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65 67 after the integ
743d0 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 6c er.** pVal
743e0 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72 69 where to wri
743f0 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 te the integers
74400 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e value..**.** Con
74410 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 versions continu
74420 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 e until one with
74430 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 nextC==0 is enc
74440 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 ountered..** The
74450 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
74460 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
74470 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 successful conve
74480 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 rsions..*/.stati
74490 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73 28 c int getDigits(
744a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 const char *zDat
744b0 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 e, ...){. va_li
744c0 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c st ap;. int val
744d0 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 ;. int N;. int
744e0 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b min;. int max;
744f0 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 . int nextC;.
74500 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 int *pVal;. int
74510 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 cnt = 0;. va_s
74520 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29 3b tart(ap, zDate);
74530 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 . do{. N = v
74540 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a a_arg(ap, int);.
74550 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 min = va_arg
74560 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d (ap, int);. m
74570 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 ax = va_arg(ap,
74580 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 int);. nextC
74590 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 = va_arg(ap, int
745a0 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61 );. pVal = va
745b0 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a _arg(ap, int*);.
745c0 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 val = 0;.
745d0 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 while( N-- ){.
745e0 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 if( !sqlite
745f0 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 3Isdigit(*zDate)
74600 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
74610 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a end_getDigits;.
74620 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61 }. va
74630 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 l = val*10 + *zD
74640 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 ate - '0';.
74650 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a zDate++;. }.
74660 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 if( val<min
74670 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e || val>max || (n
74680 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43 extC!=0 && nextC
74690 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 !=*zDate) ){.
746a0 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 goto end_getD
746b0 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 igits;. }.
746c0 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 *pVal = val;.
746d0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 zDate++;. c
746e0 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 nt++;. }while(
746f0 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 nextC );.end_get
74700 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 Digits:. va_end
74710 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 (ap);. return c
74720 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 nt;.}../*.** Rea
74730 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 d text from z[]
74740 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f and convert into
74750 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e a floating poin
74760 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 t number. Retur
74770 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 n.** the number
74780 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65 72 of digits conver
74790 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ted..*/.#define
747a0 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65 33 getValue sqlite3
747b0 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 AtoF../*.** Pars
747c0 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 e a timezone ext
747d0 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e ension on the en
747e0 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65 d of a date-time
747f0 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69 ..** The extensi
74800 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 on is of the for
74810 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 m:.**.**
74820 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a (+/-)HH:MM.**.**
74830 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e Or the "zulu" n
74840 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 otation:.**.**
74850 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 Z.**.** If
74860 20 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75 the parse is su
74870 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20 ccessful, write
74880 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
74890 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e nutes.** of chan
748a0 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 ge in p->tz and
748b0 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 return 0. If a
748c0 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 parser error occ
748d0 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e urs,.** return n
748e0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 on-zero..**.** A
748f0 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69 missing specifi
74900 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 er is not consid
74910 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a ered an error..*
74920 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 /.static int par
74930 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 seTimezone(const
74940 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 char *zDate, Da
74950 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e teTime *p){. in
74960 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 t sgn = 0;. int
74970 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 nHr, nMn;. int
74980 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c c;. while( sql
74990 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 ite3Isspace(*zDa
749a0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 te) ){ zDate++;
749b0 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 }. p->tz = 0;.
749c0 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 c = *zDate;. i
749d0 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 f( c=='-' ){.
749e0 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c sgn = -1;. }el
749f0 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b se if( c=='+' ){
74a00 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 . sgn = +1;.
74a10 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a }else if( c=='Z
74a20 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 ' || c=='z' ){.
74a30 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
74a40 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a goto zulu_time;.
74a50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
74a60 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 urn c!=0;. }.
74a70 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 zDate++;. if( g
74a80 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 etDigits(zDate,
74a90 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 2, 0, 14, ':', &
74aa0 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 nHr, 2, 0, 59, 0
74ab0 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 , &nMn)!=2 ){.
74ac0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
74ad0 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 zDate += 5;.
74ae0 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e p->tz = sgn*(nMn
74af0 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 + nHr*60);.zulu
74b00 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 _time:. while(
74b10 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a sqlite3Isspace(*
74b20 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b zDate) ){ zDate+
74b30 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a +; }. return *z
74b40 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Date!=0;.}../*.*
74b50 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f 66 * Parse times of
74b60 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 the form HH:MM
74b70 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 or HH:MM:SS or H
74b80 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a H:MM:SS.FFFF..**
74b90 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 The HH, MM, and
74ba0 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 65 SS must each be
74bb0 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 74 exactly 2 digit
74bc0 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 s. The.** fract
74bd0 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 ional seconds FF
74be0 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 FF can be one or
74bf0 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a more digits..**
74c00 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 .** Return 1 if
74c10 74 68 65 72 65 20 69 73 20 61 20 70 61 72 73 69 there is a parsi
74c20 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 6f ng error and 0 o
74c30 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 n success..*/.st
74c40 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 68 atic int parseHh
74c50 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 20 MmSs(const char
74c60 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 *zDate, DateTime
74c70 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d *p){. int h, m
74c80 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 , s;. double ms
74c90 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 = 0.0;. if( ge
74ca0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 tDigits(zDate, 2
74cb0 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 , 0, 24, ':', &h
74cc0 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 , 2, 0, 59, 0, &
74cd0 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 m)!=2 ){. ret
74ce0 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 urn 1;. }. zDa
74cf0 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a te += 5;. if( *
74d00 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 zDate==':' ){.
74d10 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 zDate++;. i
74d20 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 f( getDigits(zDa
74d30 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c te, 2, 0, 59, 0,
74d40 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 &s)!=1 ){.
74d50 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
74d60 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b . zDate += 2;
74d70 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 3d . if( *zDate=
74d80 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33 49 ='.' && sqlite3I
74d90 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 sdigit(zDate[1])
74da0 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 ){. double
74db0 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 rScale = 1.0;.
74dc0 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 zDate++;.
74dd0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
74de0 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 e3Isdigit(*zDate
74df0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 ) ){. ms
74e00 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 = ms*10.0 + *zDa
74e10 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 te - '0';.
74e20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 rScale *= 10.0
74e30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b ;. zDate+
74e40 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 +;. }.
74e50 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 ms /= rScale;.
74e60 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
74e70 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 s = 0;. }. p
74e80 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 ->validJD = 0;.
74e90 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 p->validHMS = 1
74ea0 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 ;. p->h = h;.
74eb0 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 p->m = m;. p->s
74ec0 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 = s + ms;. if(
74ed0 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a parseTimezone(z
74ee0 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72 Date, p) ) retur
74ef0 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 n 1;. p->validT
74f00 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 Z = (p->tz!=0)?1
74f10 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a :0;. return 0;.
74f20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
74f30 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 from YYYY-MM-DD
74f40 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c HH:MM:SS to jul
74f50 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77 ian day. We alw
74f60 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 ays assume.** th
74f70 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 at the YYYY-MM-D
74f80 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74 D is according t
74f90 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 o the Gregorian
74fa0 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 calendar..**.**
74fb0 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 Reference: Meeu
74fc0 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 s page 61.*/.sta
74fd0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 tic void compute
74fe0 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b JD(DateTime *p){
74ff0 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 . int Y, M, D,
75000 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 A, B, X1, X2;..
75010 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 if( p->validJD
75020 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
75030 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 p->validYMD ){.
75040 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 Y = p->Y;.
75050 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 M = p->M;. D
75060 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 = p->D;. }else
75070 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20 {. Y = 2000;
75080 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 /* If no YMD sp
75090 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20 ecified, assume
750a0 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 2000-Jan-01 */.
750b0 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 M = 1;. D
750c0 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d = 1;. }. if( M
750d0 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a <=2 ){. Y--;.
750e0 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d M += 12;. }
750f0 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 . A = Y/100;.
75100 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 B = 2 - A + (A/4
75110 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 2a );. X1 = 36525*
75120 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 (Y+4716)/100;.
75130 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 X2 = 306001*(M+1
75140 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a )/10000;. p->iJ
75150 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 D = (sqlite3_int
75160 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 44 64)((X1 + X2 + D
75170 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 20 + B - 1524.5 )
75180 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70 * 86400000);. p
75190 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 ->validJD = 1;.
751a0 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 if( p->validHMS
751b0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b ){. p->iJD +
751c0 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b = p->h*3600000 +
751d0 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 p->m*60000 + (s
751e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 2d qlite3_int64)(p-
751f0 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 66 >s*1000);. if
75200 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a ( p->validTZ ){.
75210 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 p->iJD -=
75220 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 p->tz*60000;.
75230 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d p->validYMD =
75240 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 0;. p->val
75250 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 idHMS = 0;.
75260 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b p->validTZ = 0;
75270 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
75280 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 20 .** Parse dates
75290 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a of the form.**.*
752a0 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 * YYYY-MM-DD
752b0 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a HH:MM:SS.FFF.**
752c0 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 YYYY-MM-DD
752d0 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 HH:MM:SS.**
752e0 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d YYYY-MM-DD HH:MM
752f0 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d .** YYYY-MM-
75300 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 DD.**.** Write t
75310 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 he result into t
75320 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 75 he DateTime stru
75330 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e cture and return
75340 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 0.** on success
75350 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 6e and 1 if the in
75360 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e 6f put string is no
75370 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a t a well-formed.
75380 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 ** date..*/.stat
75390 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 79 ic int parseYyyy
753a0 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 20 MmDd(const char
753b0 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 *zDate, DateTime
753c0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d *p){. int Y, M
753d0 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 , D, neg;.. if(
753e0 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 zDate[0]=='-' )
753f0 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 {. zDate++;.
75400 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 neg = 1;. }e
75410 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 lse{. neg = 0
75420 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 44 ;. }. if( getD
75430 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c igits(zDate,4,0,
75440 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 9999,'-',&Y,2,1,
75450 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 12,'-',&M,2,1,31
75460 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 ,0,&D)!=3 ){.
75470 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
75480 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 zDate += 10;.
75490 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
754a0 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c space(*zDate) ||
754b0 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 'T'==*(u8*)zDat
754c0 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a e ){ zDate++; }.
754d0 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 if( parseHhMmS
754e0 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 s(zDate, p)==0 )
754f0 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 {. /* We got
75500 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 the time */. }e
75510 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d lse if( *zDate==
75520 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 0 ){. p->vali
75530 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 dHMS = 0;. }els
75540 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b e{. return 1;
75550 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a . }. p->validJ
75560 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 D = 0;. p->vali
75570 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 dYMD = 1;. p->Y
75580 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b = neg ? -Y : Y;
75590 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 . p->M = M;. p
755a0 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 ->D = D;. if( p
755b0 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 ->validTZ ){.
755c0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
755d0 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
755e0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
755f0 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72 time to the curr
75600 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65 ent time reporte
75610 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a d by the VFS.*/.
75620 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 44 static void setD
75630 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 ateTimeToCurrent
75640 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
75650 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 *context, DateT
75660 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c ime *p){. doubl
75670 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a e r;. sqlite3 *
75680 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
75690 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
756a0 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 ontext);. sqlit
756b0 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 e3OsCurrentTime(
756c0 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 db->pVfs, &r);.
756d0 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 p->iJD = (sqlit
756e0 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 e3_int64)(r*8640
756f0 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 0000.0 + 0.5);.
75700 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b p->validJD = 1;
75710 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 .}../*.** Attemp
75720 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 67 t to parse the g
75730 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f iven string into
75740 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 a Julian Day Nu
75750 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a mber. Return.**
75760 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
75770 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rrors..**.** The
75780 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 following are a
75790 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 cceptable forms
757a0 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74 for the input st
757b0 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ring:.**.**
757c0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
757d0 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a M:SS.FFF +/-HH:
757e0 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 2e MM.** DDDD.
757f0 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a DD .** now.
75800 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 **.** In the fir
75810 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d st form, the +/-
75820 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 HH:MM is always
75830 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 optional. The f
75840 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 ractional.** sec
75850 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 onds extension (
75860 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 6f the ".FFF") is o
75870 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 ptional. The se
75880 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a conds portion.**
75890 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 20 (":SS.FFF") is
758a0 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 option. The yea
758b0 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 62 r and date can b
758c0 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e e omitted as lon
758d0 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73 g.** as there is
758e0 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 a time string.
758f0 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e 67 The time string
75900 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 can be omitted
75910 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 as long.** as th
75920 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61 6e ere is a year an
75930 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 d date..*/.stati
75940 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 4f c int parseDateO
75950 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 rTime(. sqlite3
75960 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
75970 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 t, . const char
75980 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 65 *zDate, . Date
75990 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 Time *p.){. int
759a0 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f isRealNum; /
759b0 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 * Return from sq
759c0 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 29 2e lite3IsNumber().
759d0 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 Not used */.
759e0 69 66 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44 if( parseYyyyMmD
759f0 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b d(zDate,p)==0 ){
75a00 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
75a10 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72 73 65 }else if( parse
75a20 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 HhMmSs(zDate, p)
75a30 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
75a40 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 n 0;. }else if(
75a50 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
75a60 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 zDate,"now")==0)
75a70 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69 6d {. setDateTim
75a80 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 eToCurrent(conte
75a90 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75 xt, p);. retu
75aa0 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 rn 0;. }else if
75ab0 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 ( sqlite3IsNumbe
75ac0 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65 61 6c r(zDate, &isReal
75ad0 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Num, SQLITE_UTF8
75ae0 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 ) ){. double
75af0 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 65 28 r;. getValue(
75b00 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 zDate, &r);.
75b10 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 p->iJD = (sqlite
75b20 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 3_int64)(r*86400
75b30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 000.0 + 0.5);.
75b40 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 p->validJD = 1
75b50 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
75b60 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
75b70 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 }../*.** Compute
75b80 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 the Year, Month
75b90 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 , and Day from t
75ba0 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 he julian day nu
75bb0 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mber..*/.static
75bc0 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 void computeYMD(
75bd0 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
75be0 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 int Z, A, B, C,
75bf0 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 D, E, X1;. if(
75c00 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 72 65 p->validYMD ) re
75c10 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e turn;. if( !p->
75c20 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 validJD ){. p
75c30 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 ->Y = 2000;.
75c40 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d p->M = 1;. p-
75c50 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b >D = 1;. }else{
75c60 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29 28 28 . Z = (int)((
75c70 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 p->iJD + 4320000
75c80 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 0)/86400000);.
75c90 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d A = (int)((Z -
75ca0 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33 36 35 1867216.25)/365
75cb0 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20 3d 20 24.25);. A =
75cc0 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 2f 34 Z + 1 + A - (A/4
75cd0 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b 20 31 );. B = A + 1
75ce0 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e 524;. C = (in
75cf0 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29 2f 33 t)((B - 122.1)/3
75d00 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20 3d 20 65.25);. D =
75d10 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20 (36525*C)/100;.
75d20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28 42 2d E = (int)((B-
75d30 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20 20 20 D)/30.6001);.
75d40 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30 2e 36 X1 = (int)(30.6
75d50 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44 001*E);. p->D
75d60 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 = B - D - X1;.
75d70 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f p->M = E<14 ?
75d80 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 E-1 : E-13;.
75d90 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f p->Y = p->M>2 ?
75da0 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 C - 4716 : C -
75db0 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 4715;. }. p->v
75dc0 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a alidYMD = 1;.}..
75dd0 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
75de0 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 e Hour, Minute,
75df0 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d and Seconds from
75e00 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 the julian day
75e10 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
75e20 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d c void computeHM
75e30 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a S(DateTime *p){.
75e40 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 int s;. if( p
75e50 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72 65 74 ->validHMS ) ret
75e60 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 urn;. computeJD
75e70 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 (p);. s = (int)
75e80 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 ((p->iJD + 43200
75e90 30 30 30 29 20 25 20 38 36 34 30 30 30 30 30 29 000) % 86400000)
75ea0 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30 ;. p->s = s/100
75eb0 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 0.0;. s = (int)
75ec0 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 p->s;. p->s -=
75ed0 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36 s;. p->h = s/36
75ee0 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 00;. s -= p->h*
75ef0 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 3600;. p->m = s
75f00 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 /60;. p->s += s
75f10 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d - p->m*60;. p-
75f20 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d >validHMS = 1;.}
75f30 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 ../*.** Compute
75f40 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 4d 53 both YMD and HMS
75f50 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
75f60 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 44 computeYMD_HMS(D
75f70 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63 ateTime *p){. c
75f80 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 omputeYMD(p);.
75f90 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d computeHMS(p);.}
75fa0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
75fb0 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e e YMD and HMS an
75fc0 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 d the TZ.*/.stat
75fd0 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 ic void clearYMD
75fe0 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 _HMS_TZ(DateTime
75ff0 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 *p){. p->valid
76000 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 YMD = 0;. p->va
76010 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d lidHMS = 0;. p-
76020 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a >validTZ = 0;.}.
76030 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
76040 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f OMIT_LOCALTIME./
76050 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 *.** Compute the
76060 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e 20 difference (in
76070 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a milliseconds).**
76080 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69 between localti
76090 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e me and UTC (a.k.
760a0 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 a. GMT).** for t
760b0 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 70 20 he time value p
760c0 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 55 54 where p is in UT
760d0 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c C..*/.static sql
760e0 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c ite3_int64 local
760f0 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 65 54 timeOffset(DateT
76100 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 65 54 ime *p){. DateT
76110 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65 ime x, y;. time
76120 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a _t t;. x = *p;.
76130 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 computeYMD_HMS
76140 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c (&x);. if( x.Y<
76150 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 1971 || x.Y>=203
76160 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32 8 ){. x.Y = 2
76170 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31 000;. x.M = 1
76180 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20 ;. x.D = 1;.
76190 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20 x.h = 0;.
761a0 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73 x.m = 0;. x.s
761b0 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 = 0.0;. } else
761c0 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d 20 28 {. int s = (
761d0 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b int)(x.s + 0.5);
761e0 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20 . x.s = s;.
761f0 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 }. x.tz = 0;.
76200 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 x.validJD = 0;.
76210 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a computeJD(&x);.
76220 20 20 74 20 3d 20 78 2e 69 4a 44 2f 31 30 30 30 t = x.iJD/1000
76230 20 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 - 21086676*(i64
76240 29 31 30 30 30 30 3b 0a 23 69 66 64 65 66 20 48 )10000;.#ifdef H
76250 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a AVE_LOCALTIME_R.
76260 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
76270 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f m sLocal;. lo
76280 63 61 6c 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 caltime_r(&t, &s
76290 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 79 2e 59 20 Local);. y.Y
762a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 = sLocal.tm_year
762b0 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d + 1900;. y.M
762c0 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e = sLocal.tm_mon
762d0 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 + 1;. y.D =
762e0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a sLocal.tm_mday;.
762f0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c y.h = sLocal
76300 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e .tm_hour;. y.
76310 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 m = sLocal.tm_mi
76320 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f n;. y.s = sLo
76330 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a cal.tm_sec;. }.
76340 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 #elif defined(HA
76350 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 0a VE_LOCALTIME_S).
76360 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
76370 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f m sLocal;. lo
76380 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61 caltime_s(&sLoca
76390 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 l, &t);. y.Y
763a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 = sLocal.tm_year
763b0 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d + 1900;. y.M
763c0 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e = sLocal.tm_mon
763d0 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 + 1;. y.D =
763e0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a sLocal.tm_mday;.
763f0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c y.h = sLocal
76400 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e .tm_hour;. y.
76410 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 m = sLocal.tm_mi
76420 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f n;. y.s = sLo
76430 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a cal.tm_sec;. }.
76440 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 #else. {. st
76450 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 ruct tm *pTm;.
76460 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
76470 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 enter(sqlite3Mut
76480 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
76490 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
764a0 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 ER));. pTm =
764b0 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 localtime(&t);.
764c0 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d y.Y = pTm->tm
764d0 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 _year + 1900;.
764e0 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f y.M = pTm->tm_
764f0 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 mon + 1;. y.D
76500 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b = pTm->tm_mday;
76510 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e . y.h = pTm->
76520 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d tm_hour;. y.m
76530 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a = pTm->tm_min;.
76540 20 20 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 y.s = pTm->t
76550 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 m_sec;. sqlit
76560 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
76570 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
76580 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
76590 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 ATIC_MASTER));.
765a0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61 }.#endif. y.va
765b0 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e lidYMD = 1;. y.
765c0 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 validHMS = 1;.
765d0 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 y.validJD = 0;.
765e0 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a y.validTZ = 0;.
765f0 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b computeJD(&y);
76600 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20 . return y.iJD
76610 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 - x.iJD;.}.#endi
76620 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
76630 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f _LOCALTIME */../
76640 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d *.** Process a m
76650 6f 64 69 66 69 65 72 20 74 6f 20 61 20 64 61 74 odifier to a dat
76660 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 e-time stamp. T
76670 68 65 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65 he modifiers are
76680 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a .** as follows:.
76690 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 **.** NNN da
766a0 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f ys.** NNN ho
766b0 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d urs.** NNN m
766c0 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e inutes.** NN
766d0 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a N.NNNN seconds.*
766e0 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 * NNN months
766f0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 .** NNN year
76700 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f s.** start o
76710 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 f month.** s
76720 74 61 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 tart of year.**
76730 20 20 20 20 73 74 61 72 74 20 6f 66 20 77 65 65 start of wee
76740 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f k.** start o
76750 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 f day.** wee
76760 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e kday N.** un
76770 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c ixepoch.** l
76780 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 ocaltime.**
76790 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e utc.**.** Return
767a0 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 0 on success an
767b0 64 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 d 1 if there is
767c0 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f any kind of erro
767d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
767e0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63 parseModifier(c
767f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c onst char *zMod,
76800 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 DateTime *p){.
76810 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 int rc = 1;. i
76820 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 nt n;. double r
76830 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 ;. char *z, zBu
76840 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 f[30];. z = zBu
76850 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c f;. for(n=0; n<
76860 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d ArraySize(zBuf)-
76870 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 1 && zMod[n]; n+
76880 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28 +){. z[n] = (
76890 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70 65 char)sqlite3Uppe
768a0 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f rToLower[(u8)zMo
768b0 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e d[n]];. }. z[n
768c0 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 ] = 0;. switch(
768d0 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 z[0] ){.#ifndef
768e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 SQLITE_OMIT_LOC
768f0 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20 ALTIME. case
76900 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 'l': {. /*
76910 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20 localtime.
76920 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 **. ** A
76930 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72 ssuming the curr
76940 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69 ent time value i
76950 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d s UTC (a.k.a. GM
76960 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a T), shift it to.
76970 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f ** show lo
76980 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 cal time..
76990 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 */. if( str
769a0 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d cmp(z, "localtim
769b0 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 e")==0 ){.
769c0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
769d0 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b p->iJD +
769e0 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 = localtimeOffse
769f0 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c t(p);. cl
76a00 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
76a10 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
76a20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
76a30 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
76a40 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27 dif. case 'u'
76a50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 : {. /*.
76a60 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f ** unixepo
76a70 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 ch. **.
76a80 20 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 ** Treat the c
76a90 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
76aa0 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75 p->iJD as the nu
76ab0 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a mber of. **
76ac0 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 seconds since 1
76ad0 39 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 970. Convert to
76ae0 20 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 a real julian d
76af0 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 ay number..
76b00 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 */. if( st
76b10 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f rcmp(z, "unixepo
76b20 63 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 ch")==0 && p->va
76b30 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 lidJD ){.
76b40 20 70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a p->iJD = (p->iJ
76b50 44 20 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30 D + 43200)/86400
76b60 20 2b 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 + 21086676*(i64
76b70 29 31 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20 )10000000;.
76b80 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f clearYMD_HMS_
76b90 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 TZ(p);. r
76ba0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 c = 0;. }.#
76bb0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
76bc0 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 IT_LOCALTIME.
76bd0 20 20 20 65 6c 73 65 20 69 66 28 20 73 74 72 63 else if( strc
76be0 6d 70 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20 mp(z, "utc")==0
76bf0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
76c00 65 33 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20 e3_int64 c1;.
76c10 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 computeJD(p
76c20 29 3b 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20 );. c1 =
76c30 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 localtimeOffset(
76c40 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 p);. p->i
76c50 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20 JD -= c1;.
76c60 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 clearYMD_HMS_T
76c70 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d Z(p);. p-
76c80 3e 69 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 >iJD += c1 - loc
76c90 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b altimeOffset(p);
76ca0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
76cb0 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
76cc0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
76cd0 20 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a }. case 'w':
76ce0 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 {. /*.
76cf0 20 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20 ** weekday
76d00 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 N. **.
76d10 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 ** Move the dat
76d20 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 e to the same ti
76d30 6d 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f me on the next o
76d40 63 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20 ccurrence of.
76d50 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20 ** weekday N
76d60 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c where 0==Sunday,
76d70 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 1==Monday, and
76d80 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 so forth. If th
76d90 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20 e. ** date
76da0 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68 is already on th
76db0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65 e appropriate we
76dc0 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61 ekday, this is a
76dd0 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f no-op.. */
76de0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 . if( strnc
76df0 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22 mp(z, "weekday "
76e00 2c 20 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61 , 8)==0 && getVa
76e10 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a lue(&z[8],&r)>0.
76e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76e30 20 26 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d && (n=(int)r)==
76e40 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 r && n>=0 && r<7
76e50 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
76e60 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20 te3_int64 Z;.
76e70 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f computeYMD_
76e80 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 HMS(p);.
76e90 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
76ea0 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 p->valid
76eb0 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 JD = 0;.
76ec0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
76ed0 20 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 Z = ((p->i
76ee0 4a 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f JD + 129600000)/
76ef0 38 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20 86400000) % 7;.
76f00 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29 if( Z>n )
76f10 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20 Z -= 7;.
76f20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 p->iJD += (n -
76f30 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 Z)*86400000;.
76f40 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d clearYMD_HM
76f50 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 S_TZ(p);.
76f60 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d rc = 0;. }
76f70 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
76f80 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27 }. case 's'
76f90 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 : {. /*.
76fa0 20 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f ** start o
76fb0 66 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a f TTTTT. **
76fc0 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 . ** Move t
76fd0 68 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64 he date backward
76fe0 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 s to the beginni
76ff0 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ng of the curren
77000 74 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 t day,. **
77010 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72 or month or year
77020 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
77030 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 if( strncmp(z,
77040 22 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 "start of ", 9)!
77050 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
77060 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20 z += 9;.
77070 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 computeYMD(p);.
77080 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 p->validHMS
77090 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68 = 1;. p->h
770a0 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 = p->m = 0;.
770b0 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 p->s = 0.0;.
770c0 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 p->validTZ
770d0 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 = 0;. p->va
770e0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 lidJD = 0;.
770f0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d if( strcmp(z,"m
77100 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 onth")==0 ){.
77110 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 p->D = 1;.
77120 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
77130 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 }else if( s
77140 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d trcmp(z,"year")=
77150 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f =0 ){. co
77160 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 mputeYMD(p);.
77170 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 p->M = 1;.
77180 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b p->D = 1;
77190 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
771a0 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
771b0 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 strcmp(z,"day")
771c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
771d0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 0;. }.
771e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
771f0 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a }. case '+':.
77200 20 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20 case '-':.
77210 20 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20 case '0':.
77220 63 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61 case '1':. ca
77230 73 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65 se '2':. case
77240 20 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '3':. case '
77250 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27 4':. case '5'
77260 3a 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a :. case '6':.
77270 20 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20 case '7':.
77280 20 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20 case '8':.
77290 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 case '9': {.
772a0 20 20 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65 double rRounde
772b0 72 3b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 r;. n = get
772c0 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 Value(z, &r);.
772d0 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 assert( n>=1
772e0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b );. if( z[
772f0 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 n]==':' ){.
77300 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 /* A modifier
77310 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c of the form (+|
77320 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 -)HH:MM:SS.FFF a
77330 64 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 dds (or subtract
77340 73 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a s) the. *
77350 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 * specified numb
77360 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e er of hours, min
77370 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 utes, seconds, a
77380 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 nd fractional se
77390 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a conds. **
773a0 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 to the time. T
773b0 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 he ".FFF" may be
773c0 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 omitted. The "
773d0 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a :SS.FFF" may be.
773e0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 ** omitt
773f0 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ed.. */.
77400 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 const cha
77410 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 r *z2 = z;.
77420 20 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a DateTime tx;.
77430 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
77440 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20 int64 day;.
77450 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 if( !sqlite3I
77460 73 64 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32 sdigit(*z2) ) z2
77470 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 ++;. mems
77480 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f et(&tx, 0, sizeo
77490 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20 f(tx));.
774a0 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 if( parseHhMmSs(
774b0 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b z2, &tx) ) break
774c0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
774d0 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20 eJD(&tx);.
774e0 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 tx.iJD -= 4320
774f0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61 0000;. da
77500 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 y = tx.iJD/86400
77510 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 000;. tx.
77520 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 iJD -= day*86400
77530 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 000;. if(
77540 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e z[0]=='-' ) tx.
77550 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 iJD = -tx.iJD;.
77560 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
77570 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 (p);. cle
77580 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
77590 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
775a0 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 += tx.iJD;.
775b0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
775c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
775d0 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a }. z += n;.
775e0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c while( sql
775f0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 ite3Isspace(*z)
77600 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d ) z++;. n =
77610 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
77620 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e (z);. if( n
77630 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65 >10 || n<3 ) bre
77640 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ak;. if( z[
77650 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e n-1]=='s' ){ z[n
77660 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a -1] = 0; n--; }.
77670 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
77680 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 p);. rc = 0
77690 3b 0a 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72 ;. rRounder
776a0 20 3d 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 = r<0 ? -0.5 :
776b0 2b 30 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20 +0.5;. if(
776c0 6e 3d 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a n==3 && strcmp(z
776d0 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 ,"day")==0 ){.
776e0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
776f0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
77700 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 r*86400000.0 + r
77710 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 Rounder);.
77720 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 }else if( n==4 &
77730 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72 & strcmp(z,"hour
77740 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
77750 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
77760 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 te3_int64)(r*(86
77770 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b 400000.0/24.0) +
77780 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 rRounder);.
77790 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 }else if( n==6
777a0 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69 && strcmp(z,"mi
777b0 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 nute")==0 ){.
777c0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
777d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
777e0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 *(86400000.0/(24
777f0 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 .0*60.0)) + rRou
77800 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
77810 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 se if( n==6 && s
77820 74 72 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22 trcmp(z,"second"
77830 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
77840 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
77850 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 e3_int64)(r*(864
77860 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 00000.0/(24.0*60
77870 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 .0*60.0)) + rRou
77880 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
77890 73 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73 se if( n==5 && s
778a0 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 trcmp(z,"month")
778b0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ==0 ){. i
778c0 6e 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20 nt x, y;.
778d0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
778e0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d p);. p->M
778f0 20 2b 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 += (int)r;.
77900 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f x = p->M>0 ?
77910 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 (p->M-1)/12 : (
77920 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20 p->M-12)/12;.
77930 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a p->Y += x;.
77940 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 p->M -=
77950 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d x*12;. p-
77960 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 >validJD = 0;.
77970 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
77980 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20 p);. y =
77990 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 (int)r;.
779a0 69 66 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 if( y!=r ){.
779b0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
779c0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
779d0 28 72 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34 (r - y)*30.0*864
779e0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 00000.0 + rRound
779f0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 er);. }.
77a00 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
77a10 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c ==4 && strcmp(z,
77a20 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 "year")==0 ){.
77a30 20 20 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69 int y = (i
77a40 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f nt)r;. co
77a50 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b mputeYMD_HMS(p);
77a60 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d . p->Y +=
77a70 20 79 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 y;. p->v
77a80 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 alidJD = 0;.
77a90 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
77aa0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 21 ;. if( y!
77ab0 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =r ){.
77ac0 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
77ad0 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79 e3_int64)((r - y
77ae0 29 2a 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30 )*365.0*86400000
77af0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a .0 + rRounder);.
77b00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
77b10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
77b20 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 1;. }.
77b30 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d clearYMD_HM
77b40 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 S_TZ(p);. b
77b50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
77b60 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
77b70 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
77b80 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
77b90 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 ../*.** Process
77ba0 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 time function ar
77bb0 67 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 guments. argv[0
77bc0 5d 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 ] is a date-time
77bd0 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b stamp..** argv[
77be0 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 1] and following
77bf0 20 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 are modifiers.
77c00 20 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 Parse them all
77c10 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 and write.** the
77c20 20 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 resulting time
77c30 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d into the DateTim
77c40 65 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 e structure p.
77c50 52 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 Return 0.** on s
77c60 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 uccess and 1 if
77c70 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 there are any er
77c80 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rors..**.** If t
77c90 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 here are zero pa
77ca0 72 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 rameters (if eve
77cb0 6e 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 n argv[0] is und
77cc0 65 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20 efined).** then
77cd0 61 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 assume a default
77ce0 20 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 value of "now"
77cf0 66 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a for argv[0]..*/.
77d00 73 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74 static int isDat
77d10 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e e(. sqlite3_con
77d20 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
77d30 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 int argc, . s
77d40 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
77d50 72 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 rgv, . DateTime
77d60 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a *p.){. int i;.
77d70 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
77d80 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 char *z;. int
77d90 65 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28 eType;. memset(
77da0 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 p, 0, sizeof(*p)
77db0 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 );. if( argc==0
77dc0 20 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 ){. setDateT
77dd0 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e imeToCurrent(con
77de0 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 text, p);. }els
77df0 65 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73 e if( (eType = s
77e00 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
77e10 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c e(argv[0]))==SQL
77e20 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20 ITE_FLOAT.
77e30 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
77e40 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e eType==SQLITE_IN
77e50 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e TEGER ){. p->
77e60 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
77e70 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61 nt64)(sqlite3_va
77e80 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b lue_double(argv[
77e90 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 0])*86400000.0 +
77ea0 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61 0.5);. p->va
77eb0 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c lidJD = 1;. }el
77ec0 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 se{. z = sqli
77ed0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
77ee0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 rgv[0]);. if(
77ef0 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65 !z || parseDate
77f00 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20 OrTime(context,
77f10 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a (char*)z, p) ){.
77f20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
77f30 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 }. }. for(
77f40 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b i=1; i<argc; i++
77f50 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20 ){. if( (z =
77f60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
77f70 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20 xt(argv[i]))==0
77f80 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 || parseModifier
77f90 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b ((char*)z, p) ){
77fa0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
77fb0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
77fc0 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn 0;.}.../*.**
77fd0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
77fe0 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
77ff0 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64 61 t the various da
78000 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63 te and time func
78010 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 tions.** of SQLi
78020 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 te..*/../*.**
78030 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45 julianday( TIME
78040 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
78050 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
78060 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 urn the julian d
78070 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ay number of the
78080 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64 20 date specified
78090 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 in the arguments
780a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
780b0 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20 juliandayFunc(.
780c0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
780d0 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
780e0 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
780f0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
78100 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 . DateTime x;.
78110 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 if( isDate(cont
78120 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c ext, argc, argv,
78130 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 &x)==0 ){. c
78140 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 omputeJD(&x);.
78150 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
78160 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c _double(context,
78170 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e x.iJD/86400000.
78180 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0);. }.}../*.**
78190 20 20 20 20 64 61 74 65 74 69 6d 65 28 20 54 49 datetime( TI
781a0 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
781b0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
781c0 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 eturn YYYY-MM-DD
781d0 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 HH:MM:SS.*/.sta
781e0 74 69 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d tic void datetim
781f0 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
78200 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
78210 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
78220 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
78230 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 *argv.){. DateT
78240 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 ime x;. if( isD
78250 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 ate(context, arg
78260 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 c, argv, &x)==0
78270 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 ){. char zBuf
78280 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 [100];. compu
78290 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 teYMD_HMS(&x);.
782a0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
782b0 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 ntf(sizeof(zBuf)
782c0 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 , zBuf, "%04d-%0
782d0 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 2d-%02d %02d:%02
782e0 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 d:%02d",.
782f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 78 2e x.
78300 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 Y, x.M, x.D, x.h
78310 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 , x.m, (int)(x.s
78320 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ));. sqlite3_
78330 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
78340 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 ext, zBuf, -1, S
78350 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
78360 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 ;. }.}../*.**
78370 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 time( TIMESTRI
78380 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e NG, MOD, MOD, ..
78390 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 .).**.** Return
783a0 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 HH:MM:SS.*/.stat
783b0 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 ic void timeFunc
783c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
783d0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
783e0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
783f0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
78400 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
78410 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 ;. if( isDate(c
78420 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 ontext, argc, ar
78430 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 gv, &x)==0 ){.
78440 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
78450 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53 ;. computeHMS
78460 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (&x);. sqlite
78470 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
78480 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 f(zBuf), zBuf, "
78490 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c %02d:%02d:%02d",
784a0 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 x.h, x.m, (int)
784b0 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 x.s);. sqlite
784c0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
784d0 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c ntext, zBuf, -1,
784e0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
784f0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a T);. }.}../*.**
78500 20 20 20 20 64 61 74 65 28 20 54 49 4d 45 53 54 date( TIMEST
78510 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
78520 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
78530 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a n YYYY-MM-DD.*/.
78540 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 static void date
78550 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
78560 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
78570 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
78580 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
78590 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 argv.){. DateTi
785a0 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 me x;. if( isDa
785b0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 te(context, argc
785c0 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 , argv, &x)==0 )
785d0 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b {. char zBuf[
785e0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 100];. comput
785f0 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71 eYMD(&x);. sq
78600 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
78610 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
78620 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 f, "%04d-%02d-%0
78630 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2d", x.Y, x.M, x
78640 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 .D);. sqlite3
78650 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
78660 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 text, zBuf, -1,
78670 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
78680 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
78690 20 20 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52 strftime( FOR
786a0 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c MAT, TIMESTRING,
786b0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a MOD, MOD, ...).
786c0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 **.** Return a s
786d0 74 72 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 tring described
786e0 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 by FORMAT. Conv
786f0 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f ersions as follo
78700 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 ws:.**.** %d
78710 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 day of month.**
78720 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f %f ** fractio
78730 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e nal seconds SS.
78740 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75 SSS.** %H hou
78750 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 r 00-24.** %j
78760 20 64 61 79 20 6f 66 20 79 65 61 72 20 30 30 30 day of year 000
78770 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a -366.** %J **
78780 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 Julian day numb
78790 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 er.** %m mont
787a0 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 h 01-12.** %M
787b0 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a minute 00-59.**
787c0 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73 %s seconds s
787d0 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a ince 1970-01-01.
787e0 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73 ** %S seconds
787f0 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20 00-59.** %w
78800 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20 day of week 0-6
78810 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 sunday==0.**
78820 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72 %W week of year
78830 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20 00-53.** %Y
78840 79 65 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a year 0000-9999.*
78850 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61 * %% %.*/.sta
78860 74 69 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d tic void strftim
78870 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
78880 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
78890 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
788a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
788b0 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 *argv.){. DateT
788c0 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a ime x;. u64 n;.
788d0 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20 size_t i,j;.
788e0 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 char *z;. sqlit
788f0 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 e3 *db;. const
78900 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f char *zFmt = (co
78910 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 nst char*)sqlite
78920 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
78930 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42 v[0]);. char zB
78940 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a uf[100];. if( z
78950 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65 Fmt==0 || isDate
78960 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31 (context, argc-1
78970 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20 , argv+1, &x) )
78980 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73 return;. db = s
78990 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
789a0 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
789b0 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d );. for(i=0, n=
789c0 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 1; zFmt[i]; i++,
789d0 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a n++){. if( z
789e0 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 Fmt[i]=='%' ){.
789f0 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d switch( zFm
78a00 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 t[i+1] ){.
78a10 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20 case 'd':.
78a20 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20 case 'H':.
78a30 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a case 'm':.
78a40 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 case 'M'
78a50 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
78a60 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 S':. case
78a70 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'W':.
78a80 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f n++;. /
78a90 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 * fall thru */.
78aa0 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a case 'w':
78ab0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 25 . case '%
78ac0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 ':. bre
78ad0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
78ae0 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'f':.
78af0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 n += 8;.
78b00 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
78b10 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20 case 'j':.
78b20 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 n += 3;.
78b30 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
78b40 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a case 'Y':.
78b50 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38 n += 8
78b60 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
78b70 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
78b80 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 's':. cas
78b90 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20 e 'J':.
78ba0 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20 n += 50;.
78bb0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
78bc0 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 default:.
78bd0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f return; /
78be0 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e * ERROR. return
78bf0 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 a NULL */.
78c00 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 }. i++;.
78c10 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61 }. }. testca
78c20 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 se( n==sizeof(zB
78c30 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63 uf)-1 );. testc
78c40 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a ase( n==sizeof(z
78c50 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61 Buf) );. testca
78c60 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e se( n==(u64)db->
78c70 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
78c80 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b MIT_LENGTH]+1 );
78c90 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d . testcase( n==
78ca0 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b (u64)db->aLimit[
78cb0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
78cc0 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c GTH] );. if( n<
78cd0 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a sizeof(zBuf) ){.
78ce0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 z = zBuf;.
78cf0 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34 }else if( n>(u64
78d00 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 )db->aLimit[SQLI
78d10 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
78d20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
78d30 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
78d40 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 big(context);.
78d50 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 return;. }els
78d60 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 e{. z = sqlit
78d70 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
78d80 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69 , (int)n);. i
78d90 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 f( z==0 ){.
78da0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
78db0 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 error_nomem(cont
78dc0 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ext);. retu
78dd0 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
78de0 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 computeJD(&x);.
78df0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
78e00 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 &x);. for(i=j=0
78e10 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b ; zFmt[i]; i++){
78e20 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d . if( zFmt[i]
78e30 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a !='%' ){. z
78e40 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b [j++] = zFmt[i];
78e50 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
78e60 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69 i++;. swi
78e70 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a tch( zFmt[i] ){.
78e80 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27 case 'd'
78e90 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
78ea0 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 ntf(3, &z[j],"%0
78eb0 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 2d",x.D); j+=2;
78ec0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
78ed0 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20 ase 'f': {.
78ee0 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20 double s =
78ef0 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 x.s;. i
78f00 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20 f( s>59.999 ) s
78f10 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20 = 59.999;.
78f20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
78f30 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(7, &z[j],"%
78f40 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20 06.3f", s);.
78f50 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 j += sqlit
78f60 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d e3Strlen30(&z[j]
78f70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
78f80 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
78f90 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 case 'H':
78fa0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
78fb0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 f(3, &z[j],"%02d
78fc0 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 ",x.h); j+=2; br
78fd0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
78fe0 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 e 'W': /* Fall t
78ff0 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 hru */. c
79000 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 ase 'j': {.
79010 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 int nDay;
79020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
79030 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e mber of days sin
79040 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 ce 1st day of ye
79050 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ar */.
79060 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a DateTime y = x;.
79070 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 y.vali
79080 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dJD = 0;.
79090 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 y.M = 1;.
790a0 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 y.D = 1;.
790b0 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 compute
790c0 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 JD(&y);.
790d0 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28 nDay = (int)((
790e0 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30 x.iJD-y.iJD+4320
790f0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0000)/86400000);
79100 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a . if( z
79110 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 Fmt[i]=='W' ){.
79120 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 77 int w
79130 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 d; /* 0=Monday
79140 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e , 1=Tuesday, ...
79150 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20 6=Sunday */.
79160 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 28 69 wd = (i
79170 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30 nt)(((x.iJD+4320
79180 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 25 0000)/86400000)%
79190 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7);.
791a0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
791b0 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 (3, &z[j],"%02d"
791c0 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b ,(nDay+7-wd)/7);
791d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b . j +
791e0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d = 2;. }
791f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
79200 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
79210 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33 tf(4, &z[j],"%03
79220 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20 d",nDay+1);.
79230 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a j += 3;.
79240 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
79250 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
79260 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 }. c
79270 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20 ase 'J': {.
79280 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
79290 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c rintf(20, &z[j],
792a0 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36 "%.16g",x.iJD/86
792b0 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20 400000.0);.
792c0 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53 j+=sqlite3S
792d0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
792e0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
792f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
79300 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71 case 'm': sq
79310 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
79320 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 , &z[j],"%02d",x
79330 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b .M); j+=2; break
79340 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
79350 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 M': sqlite3_snp
79360 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
79370 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 %02d",x.m); j+=2
79380 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
79390 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 case 's': {.
793a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
793b0 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d nprintf(30,&z[j]
793c0 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20 ,"%lld",.
793d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
793e0 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f (i64)(x.iJD/
793f0 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 1000 - 21086676*
79400 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20 (i64)10000));.
79410 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c j += sql
79420 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b ite3Strlen30(&z[
79430 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 j]);. b
79440 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
79450 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27 case 'S'
79460 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
79470 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 ntf(3,&z[j],"%02
79480 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b d",(int)x.s); j+
79490 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
794a0 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 case 'w': {.
794b0 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 z[j++]
794c0 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44 = (char)(((x.iJD
794d0 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 +129600000)/8640
794e0 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30 27 0000) % 7) + '0'
794f0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
79500 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
79510 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b case 'Y': {
79520 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
79530 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a e3_snprintf(5,&z
79540 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b [j],"%04d",x.Y);
79550 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 j+=sqlite3Strle
79560 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 n30(&z[j]);.
79570 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
79580 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 }. d
79590 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d efault: z[j++]
795a0 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 = '%'; break;.
795b0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
795c0 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 . z[j] = 0;. s
795d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
795e0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d xt(context, z, -
795f0 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
79600 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 z==zBuf
79610 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 ? SQLITE_TRANSI
79620 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e ENT : SQLITE_DYN
79630 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 AMIC);.}../*.**
79640 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a current_time().*
79650 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
79660 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 on returns the s
79670 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d ame value as tim
79680 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 e('now')..*/.sta
79690 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 tic void ctimeFu
796a0 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
796b0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
796c0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
796d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
796e0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 *NotUsed2.){. U
796f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
79700 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
79710 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28 d2);. timeFunc(
79720 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a context, 0, 0);.
79730 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 }../*.** current
79740 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 _date().**.** Th
79750 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
79760 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c rns the same val
79770 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 ue as date('now'
79780 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
79790 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73 d cdateFunc(. s
797a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
797b0 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e context,. int N
797c0 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 otUsed,. sqlite
797d0 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 3_value **NotUse
797e0 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 d2.){. UNUSED_P
797f0 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
79800 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
79810 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 dateFunc(context
79820 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , 0, 0);.}../*.*
79830 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 * current_timest
79840 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 amp().**.** This
79850 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
79860 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 s the same value
79870 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f as datetime('no
79880 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 w')..*/.static v
79890 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 oid ctimestampFu
798a0 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
798b0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
798c0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
798d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
798e0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 *NotUsed2.){. U
798f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
79900 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
79910 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46 d2);. datetimeF
79920 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
79930 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 0);.}.#endif /*
79940 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
79950 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 OMIT_DATETIME_FU
79960 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20 NCS) */..#ifdef
79970 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 SQLITE_OMIT_DATE
79980 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a TIME_FUNCS./*.**
79990 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 If the library
799a0 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f is compiled to o
799b0 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61 mit the full-sca
799c0 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 le date and time
799d0 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f .** handling (to
799e0 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62 get a smaller b
799f0 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c inary), the foll
79a00 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65 owing minimal ve
79a10 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 rsion.** of the
79a20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e functions curren
79a30 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e t_time(), curren
79a40 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75 72 t_date() and cur
79a50 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 rent_timestamp()
79a60 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 .** are included
79a70 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69 instead. This i
79a80 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c s to support col
79a90 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 umn declarations
79aa0 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 that.** include
79ab0 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e "DEFAULT CURREN
79ac0 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a T_TIME" etc..**.
79ad0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
79ae0 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72 uses the C-libr
79af0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69 ary functions ti
79b00 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a me(), gmtime().*
79b10 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29 * and strftime()
79b20 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72 . The format str
79b30 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73 ing to pass to s
79b40 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75 70 trftime() is sup
79b50 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 plied.** as the
79b60 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68 user-data for th
79b70 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 e function..*/.s
79b80 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72 65 tatic void curre
79b90 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 ntTimeFunc(. sq
79ba0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
79bb0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
79bc0 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
79bd0 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
79be0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72 time_t t;. char
79bf0 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61 *zFormat = (cha
79c00 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 r *)sqlite3_user
79c10 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a _data(context);.
79c20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
79c30 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68 double rT;. ch
79c40 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 ar zBuf[20];..
79c50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
79c60 28 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 (argc);. UNUSED
79c70 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29 _PARAMETER(argv)
79c80 3b 0a 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 ;.. db = sqlite
79c90 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
79ca0 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
79cb0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
79cc0 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 Time(db->pVfs, &
79cd0 72 54 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c rT);.#ifndef SQL
79ce0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
79cf0 47 5f 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36 G_POINT. t = 86
79d00 34 30 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30 400.0*(rT - 2440
79d10 35 38 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65 587.5) + 0.5;.#e
79d20 6c 73 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74 lse. /* without
79d30 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
79d40 73 75 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c support, rT will
79d50 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 have. ** alrea
79d60 64 79 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e dy lost fraction
79d70 61 6c 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e al day precision
79d80 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34 .. */. t = 864
79d90 30 30 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35 00 * (rT - 24405
79da0 38 37 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e 87) - 43200;.#en
79db0 64 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f dif.#ifdef HAVE_
79dc0 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 GMTIME_R. {.
79dd0 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b struct tm sNow;
79de0 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74 . gmtime_r(&t
79df0 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74 , &sNow);. st
79e00 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c rftime(zBuf, 20,
79e10 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29 zFormat, &sNow)
79e20 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a ;. }.#else. {.
79e30 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 struct tm *p
79e40 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f Tm;. sqlite3_
79e50 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 mutex_enter(sqli
79e60 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
79e70 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
79e80 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 C_MASTER));.
79e90 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29 pTm = gmtime(&t)
79ea0 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a ;. strftime(z
79eb0 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 Buf, 20, zFormat
79ec0 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69 , pTm);. sqli
79ed0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
79ee0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
79ef0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
79f00 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
79f10 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 }.#endif.. sq
79f20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
79f30 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
79f40 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
79f50 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 SIENT);.}.#endif
79f60 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
79f70 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
79f80 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 all of the abov
79f90 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 e C functions as
79fa0 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e SQL.** function
79fb0 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 s. This should
79fc0 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 be the only rout
79fd0 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ine in this file
79fe0 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 with.** externa
79ff0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 l linkage..*/.SQ
7a000 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
7a010 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 d sqlite3Registe
7a020 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f rDateTimeFunctio
7a030 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ns(void){. stat
7a040 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 ic SQLITE_WSD Fu
7a050 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 ncDef aDateTimeF
7a060 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 uncs[] = {.#ifnd
7a070 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
7a080 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 ATETIME_FUNCS.
7a090 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 FUNCTION(julia
7a0a0 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c nday, -1,
7a0b0 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 0, 0, julianday
7a0c0 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 Func ),. FUNC
7a0d0 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 TION(date,
7a0e0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c -1, 0, 0,
7a0f0 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 dateFunc )
7a100 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 ,. FUNCTION(t
7a110 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ime,
7a120 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 -1, 0, 0, timeF
7a130 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 unc ),.
7a140 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d FUNCTION(datetim
7a150 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 e, -1, 0
7a160 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e , 0, datetimeFun
7a170 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 c ),. FUNCTI
7a180 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 ON(strftime,
7a190 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 -1, 0, 0, s
7a1a0 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a trftimeFunc ),.
7a1b0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 FUNCTION(cur
7a1c0 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 rent_time,
7a1d0 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 0, 0, 0, ctimeFu
7a1e0 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 nc ),. FU
7a1f0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 NCTION(current_t
7a200 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 imestamp, 0, 0,
7a210 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 0, ctimestampFun
7a220 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e c),. FUNCTION
7a230 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 (current_date,
7a240 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 0, 0, 0, cda
7a250 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 teFunc ),.#e
7a260 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 lse. STR_FUNC
7a270 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d TION(current_tim
7a280 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 e, 0, "%H:%
7a290 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 M:%S",
7a2a0 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 0, currentTimeFu
7a2b0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e nc),. STR_FUN
7a2c0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 CTION(current_ti
7a2d0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d mestamp, 0, "%Y-
7a2e0 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 %m-%d",
7a2f0 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 0, currentTimeF
7a300 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 unc),. STR_FU
7a310 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 NCTION(current_d
7a320 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 ate, 0, "%Y
7a330 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 -%m-%d %H:%M:%S"
7a340 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 , 0, currentTime
7a350 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 Func),.#endif.
7a360 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 };. int i;. Fu
7a370 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 ncDefHash *pHash
7a380 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 = &GLOBAL(FuncD
7a390 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 efHash, sqlite3G
7a3a0 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b lobalFunctions);
7a3b0 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e . FuncDef *aFun
7a3c0 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 c = (FuncDef*)&G
7a3d0 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 LOBAL(FuncDef, a
7a3e0 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a DateTimeFuncs);.
7a3f0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 . for(i=0; i<Ar
7a400 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d raySize(aDateTim
7a410 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 eFuncs); i++){.
7a420 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 sqlite3FuncDe
7a430 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 fInsert(pHash, &
7a440 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d aFunc[i]);. }.}
7a450 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
7a460 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 * End of date.c
7a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a4a0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
7a4b0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e * Begin file os.
7a4c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
7a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a4f0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 /./*.** 2005 Nov
7a500 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 ember 29.**.** T
7a510 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
7a520 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
7a530 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
7a540 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
7a550 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
7a560 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
7a570 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
7a580 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
7a590 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
7a5a0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
7a5b0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
7a5c0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
7a5d0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
7a5e0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
7a5f0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
7a600 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
7a610 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
7a620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7a660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
7a670 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
7a680 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 tains OS interfa
7a690 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ce code that is
7a6a0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a common to all.**
7a6b0 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a architectures..
7a6c0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 63 2c **.** $Id: os.c,
7a6d0 76 20 31 2e 31 32 36 20 32 30 30 39 2f 30 33 2f v 1.126 2009/03/
7a6e0 32 35 20 31 34 3a 32 34 3a 34 32 20 64 72 68 20 25 14:24:42 drh
7a6f0 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66 69 6e 65 Exp $.*/.#define
7a700 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 _SQLITE_OS_C_ 1
7a710 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f .#undef _SQLITE_
7a720 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OS_C_../*.** The
7a730 20 64 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20 default SQLite
7a740 73 71 6c 69 74 65 33 5f 76 66 73 20 69 6d 70 6c sqlite3_vfs impl
7a750 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20 6e ementations do n
7a760 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d ot allocate.** m
7a770 65 6d 6f 72 79 20 28 61 63 74 75 61 6c 6c 79 2c emory (actually,
7a780 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c 6c 6f 63 os_unix.c alloc
7a790 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 61 6d 6f ates a small amo
7a7a0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a unt of memory.**
7a7b0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 4f 73 4f from within OsO
7a7c0 70 65 6e 28 29 29 2c 20 62 75 74 20 73 6f 6d 65 pen()), but some
7a7d0 20 74 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 third-party imp
7a7e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 lementations may
7a7f0 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65 73 74 20 ..** So we test
7a800 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 61 the effects of a
7a810 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 69 6e malloc() failin
7a820 67 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 g and the sqlite
7a830 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66 75 6e 63 3OsXXX().** func
7a840 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e 67 20 53 tion returning S
7a850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
7a860 4d 20 75 73 69 6e 67 20 74 68 65 20 44 4f 5f 4f M using the DO_O
7a870 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 6d 61 S_MALLOC_TEST ma
7a880 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 cro..**.** The f
7a890 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f ollowing functio
7a8a0 6e 73 20 61 72 65 20 69 6e 73 74 72 75 6d 65 6e ns are instrumen
7a8b0 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 28 29 ted for malloc()
7a8c0 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 74 65 73 failure .** tes
7a8d0 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ting:.**.**
7a8e0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 0a sqlite3OsOpen().
7a8f0 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
7a900 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 20 73 71 Read().** sq
7a910 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 0a 2a lite3OsWrite().*
7a920 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 * sqlite3OsS
7a930 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c ync().** sql
7a940 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a ite3OsLock().**.
7a950 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
7a960 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 28 QLITE_TEST) && (
7a970 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30 SQLITE_OS_WIN==0
7a980 29 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f ). #define DO_O
7a990 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 69 66 S_MALLOC_TEST if
7a9a0 20 28 31 29 20 7b 20 20 20 20 20 20 20 20 20 20 (1) {
7a9b0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54 \. void *pT
7a9c0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 stAlloc = sqlite
7a9d0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20 3Malloc(10);
7a9e0 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 54 \. if (!pT
7a9f0 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e 20 stAlloc) return
7aa00 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d SQLITE_IOERR_NOM
7aa10 45 4d 3b 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 EM; \. sqlit
7aa20 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f e3_free(pTstAllo
7aa30 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 c);
7aa40 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c \. }.#el
7aa50 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f se. #define DO_
7aa60 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 0a 23 OS_MALLOC_TEST.#
7aa70 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
7aa80 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
7aa90 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 nes are convenie
7aaa0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f nce wrappers aro
7aab0 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f und methods.** o
7aac0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 f the sqlite3_fi
7aad0 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 le object. This
7aae0 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 is mostly just
7aaf0 73 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e syntactic sugar.
7ab00 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 All.** of this
7ab10 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 would be complet
7ab20 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 ely automatic if
7ab30 20 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 SQLite were cod
7ab40 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 ed using.** C++
7ab50 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e instead of plain
7ab60 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 old C..*/.SQLIT
7ab70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7ab80 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c lite3OsClose(sql
7ab90 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b ite3_file *pId){
7aba0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
7abb0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 TE_OK;. if( pId
7abc0 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 ->pMethods ){.
7abd0 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 rc = pId->pMet
7abe0 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 hods->xClose(pId
7abf0 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 );. pId->pMet
7ac00 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 hods = 0;. }.
7ac10 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.SQL
7ac20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7ac30 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 sqlite3OsRead(sq
7ac40 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
7ac50 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 void *pBuf, int
7ac60 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 amt, i64 offset)
7ac70 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 {. DO_OS_MALLOC
7ac80 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 _TEST;. return
7ac90 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52 id->pMethods->xR
7aca0 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d ead(id, pBuf, am
7acb0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 t, offset);.}.SQ
7acc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
7acd0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
7ace0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
7acf0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 , const void *pB
7ad00 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 uf, int amt, i64
7ad10 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f offset){. DO_O
7ad20 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 S_MALLOC_TEST;.
7ad30 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
7ad40 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 2c hods->xWrite(id,
7ad50 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 pBuf, amt, offs
7ad60 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 et);.}.SQLITE_PR
7ad70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7ad80 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 3OsTruncate(sqli
7ad90 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 te3_file *id, i6
7ada0 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72 4 size){. retur
7adb0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
7adc0 78 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 69 xTruncate(id, si
7add0 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ze);.}.SQLITE_PR
7ade0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7adf0 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 3OsSync(sqlite3_
7ae00 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c file *id, int fl
7ae10 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 ags){. DO_OS_MA
7ae20 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 LLOC_TEST;. ret
7ae30 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
7ae40 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 ->xSync(id, flag
7ae50 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 s);.}.SQLITE_PRI
7ae60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7ae70 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 OsFileSize(sqlit
7ae80 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 e3_file *id, i64
7ae90 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f *pSize){. DO_O
7aea0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 S_MALLOC_TEST;.
7aeb0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
7aec0 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 hods->xFileSize(
7aed0 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 id, pSize);.}.SQ
7aee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
7aef0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 sqlite3OsLock(s
7af00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
7af10 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a int lockType){.
7af20 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
7af30 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 EST;. return id
7af40 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c 6f 63 ->pMethods->xLoc
7af50 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b k(id, lockType);
7af60 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
7af70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 E int sqlite3OsU
7af80 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
7af90 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
7afa0 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 Type){. return
7afb0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 55 id->pMethods->xU
7afc0 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 nlock(id, lockTy
7afd0 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 pe);.}.SQLITE_PR
7afe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7aff0 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 3OsCheckReserved
7b000 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
7b010 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
7b020 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 Out){. DO_OS_MA
7b030 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 LLOC_TEST;. ret
7b040 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
7b050 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 ->xCheckReserved
7b060 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 Lock(id, pResOut
7b070 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
7b080 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
7b090 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c sFileControl(sql
7b0a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
7b0b0 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 nt op, void *pAr
7b0c0 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d g){. return id-
7b0d0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 >pMethods->xFile
7b0e0 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 Control(id, op,
7b0f0 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pArg);.}.SQLITE_
7b100 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7b110 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 te3OsSectorSize(
7b120 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
7b130 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 ){. int (*xSect
7b140 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f orSize)(sqlite3_
7b150 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 file*) = id->pMe
7b160 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 thods->xSectorSi
7b170 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78 53 ze;. return (xS
7b180 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 ectorSize ? xSec
7b190 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51 torSize(id) : SQ
7b1a0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
7b1b0 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c TOR_SIZE);.}.SQL
7b1c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7b1d0 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 sqlite3OsDeviceC
7b1e0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 haracteristics(s
7b1f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
7b200 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 {. return id->p
7b210 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 Methods->xDevice
7b220 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
7b230 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 id);.}../*.** Th
7b240 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 e next group of
7b250 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e routines are con
7b260 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 venience wrapper
7b270 73 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 s around the.**
7b280 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a VFS methods..*/.
7b290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
7b2a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e nt sqlite3OsOpen
7b2b0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
7b2c0 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 *pVfs, . const
7b2d0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 char *zPath, .
7b2e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
7b2f0 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 ile, . int flag
7b300 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 s, . int *pFlag
7b310 73 4f 75 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 sOut.){. int rc
7b320 3b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 ;. DO_OS_MALLOC
7b330 5f 54 45 53 54 3b 0a 20 20 72 63 20 3d 20 70 56 _TEST;. rc = pV
7b340 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20 fs->xOpen(pVfs,
7b350 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c zPath, pFile, fl
7b360 61 67 73 2c 20 70 46 6c 61 67 73 4f 75 74 29 3b ags, pFlagsOut);
7b370 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
7b380 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c QLITE_OK || pFil
7b390 65 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 29 e->pMethods==0 )
7b3a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
7b3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b3c0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c int sqlite3OsDel
7b3d0 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ete(sqlite3_vfs
7b3e0 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 *pVfs, const cha
7b3f0 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 r *zPath, int di
7b400 72 53 79 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e rSync){. return
7b410 20 70 56 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 pVfs->xDelete(p
7b420 56 66 73 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 Vfs, zPath, dirS
7b430 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ync);.}.SQLITE_P
7b440 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
7b450 65 33 4f 73 41 63 63 65 73 73 28 0a 20 20 73 71 e3OsAccess(. sq
7b460 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
7b470 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a . const char *
7b480 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c zPath, . int fl
7b490 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 ags, . int *pRe
7b4a0 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f sOut.){. DO_OS_
7b4b0 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 MALLOC_TEST;. r
7b4c0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 eturn pVfs->xAcc
7b4d0 65 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 2c ess(pVfs, zPath,
7b4e0 20 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 29 flags, pResOut)
7b4f0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
7b500 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
7b510 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 FullPathname(.
7b520 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
7b530 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 s, . const char
7b540 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 *zPath, . int
7b550 6e 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 nPathOut, . cha
7b560 72 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 r *zPathOut.){.
7b570 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 return pVfs->xF
7b580 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 ullPathname(pVfs
7b590 2c 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 , zPath, nPathOu
7b5a0 74 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a t, zPathOut);.}.
7b5b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
7b5c0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
7b5d0 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ON.SQLITE_PRIVAT
7b5e0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f E void *sqlite3O
7b5f0 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f sDlOpen(sqlite3_
7b600 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
7b610 20 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 char *zPath){.
7b620 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 return pVfs->xD
7b630 6c 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 lOpen(pVfs, zPat
7b640 68 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 h);.}.SQLITE_PRI
7b650 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
7b660 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 3OsDlError(sqlit
7b670 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
7b680 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a t nByte, char *z
7b690 42 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d BufOut){. pVfs-
7b6a0 3e 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 >xDlError(pVfs,
7b6b0 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b nByte, zBufOut);
7b6c0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
7b6d0 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 E void (*sqlite3
7b6e0 4f 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f OsDlSym(sqlite3_
7b6f0 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 vfs *pVfs, void
7b700 2a 70 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 *pHdle, const ch
7b710 61 72 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 ar *zSym))(void)
7b720 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
7b730 3e 78 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 >xDlSym(pVfs, pH
7b740 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 dle, zSym);.}.SQ
7b750 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
7b760 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f d sqlite3OsDlClo
7b770 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a se(sqlite3_vfs *
7b780 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e pVfs, void *pHan
7b790 64 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 dle){. pVfs->xD
7b7a0 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 lClose(pVfs, pHa
7b7b0 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 ndle);.}.#endif
7b7c0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c /* SQLITE_OMIT_L
7b7d0 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f OAD_EXTENSION */
7b7e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b7f0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e int sqlite3OsRan
7b800 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f domness(sqlite3_
7b810 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
7b820 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 Byte, char *zBuf
7b830 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 Out){. return p
7b840 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 Vfs->xRandomness
7b850 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 (pVfs, nByte, zB
7b860 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 ufOut);.}.SQLITE
7b870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
7b880 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 ite3OsSleep(sqli
7b890 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
7b8a0 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 nt nMicro){. re
7b8b0 74 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 turn pVfs->xSlee
7b8c0 70 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b p(pVfs, nMicro);
7b8d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
7b8e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
7b8f0 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
7b900 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f e3_vfs *pVfs, do
7b910 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b uble *pTimeOut){
7b920 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e . return pVfs->
7b930 78 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 xCurrentTime(pVf
7b940 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a s, pTimeOut);.}.
7b950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b960 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 int sqlite3OsOpe
7b970 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 nMalloc(. sqlit
7b980 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 e3_vfs *pVfs, .
7b990 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
7b9a0 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 le, . sqlite3_f
7b9b0 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 ile **ppFile, .
7b9c0 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e int flags,. in
7b9d0 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a t *pOutFlags.){.
7b9e0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
7b9f0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 E_NOMEM;. sqlit
7ba00 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a e3_file *pFile;.
7ba10 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 pFile = (sqlit
7ba20 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 e3_file *)sqlite
7ba30 33 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 3Malloc(pVfs->sz
7ba40 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 OsFile);. if( p
7ba50 46 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d File ){. rc =
7ba60 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
7ba70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c Vfs, zFile, pFil
7ba80 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c e, flags, pOutFl
7ba90 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ags);. if( rc
7baa0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
7bab0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
7bac0 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 e(pFile);. }e
7bad0 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 lse{. *ppFi
7bae0 6c 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 le = pFile;.
7baf0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
7bb00 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 c;.}.SQLITE_PRIV
7bb10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
7bb20 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 sCloseFree(sqlit
7bb30 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b e3_file *pFile){
7bb40 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
7bb50 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 TE_OK;. assert(
7bb60 20 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d pFile );. rc =
7bb70 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
7bb80 70 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 pFile);. sqlite
7bb90 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 3_free(pFile);.
7bba0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
7bbb0 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 *.** The list of
7bbc0 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20 all registered
7bbd0 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 VFS implementati
7bbe0 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ons..*/.static s
7bbf0 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c qlite3_vfs * SQL
7bc00 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20 ITE_WSD vfsList
7bc10 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73 = 0;.#define vfs
7bc20 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69 List GLOBAL(sqli
7bc30 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69 te3_vfs *, vfsLi
7bc40 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 st)../*.** Locat
7bc50 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e e a VFS by name.
7bc60 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 If no name is
7bc70 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 given, simply re
7bc80 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 turn the.** firs
7bc90 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 t VFS on the lis
7bca0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
7bcb0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 sqlite3_vfs *sq
7bcc0 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 lite3_vfs_find(c
7bcd0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 onst char *zVfs)
7bce0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
7bcf0 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53 *pVfs = 0;.#if S
7bd00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
7bd10 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
7bd20 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a *mutex;.#endif.
7bd30 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
7bd40 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 MIT_AUTOINIT. i
7bd50 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f nt rc = sqlite3_
7bd60 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 initialize();.
7bd70 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
7bd80 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 0;.#endif.#if SQ
7bd90 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
7bda0 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 mutex = sqlite
7bdb0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
7bdc0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
7bdd0 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a MASTER);.#endif.
7bde0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
7bdf0 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 enter(mutex);.
7be00 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69 for(pVfs = vfsLi
7be10 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70 st; pVfs; pVfs=p
7be20 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Vfs->pNext){.
7be30 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62 if( zVfs==0 ) b
7be40 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74 reak;. if( st
7be50 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d rcmp(zVfs, pVfs-
7be60 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 >zName)==0 ) bre
7be70 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ak;. }. sqlite
7be80 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
7be90 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 tex);. return p
7bea0 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e Vfs;.}../*.** Un
7beb0 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20 link a VFS from
7bec0 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a the linked list.
7bed0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 */.static void v
7bee0 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33 fsUnlink(sqlite3
7bef0 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61 _vfs *pVfs){. a
7bf00 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
7bf10 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 utex_held(sqlite
7bf20 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
7bf30 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
7bf40 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66 MASTER)) );. if
7bf50 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20 ( pVfs==0 ){.
7bf60 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d /* No-op */. }
7bf70 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 else if( vfsList
7bf80 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66 ==pVfs ){. vf
7bf90 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e sList = pVfs->pN
7bfa0 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ext;. }else if(
7bfb0 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20 vfsList ){.
7bfc0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d sqlite3_vfs *p =
7bfd0 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68 vfsList;. wh
7bfe0 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 ile( p->pNext &&
7bff0 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 p->pNext!=pVfs
7c000 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e ){. p = p->
7c010 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 pNext;. }.
7c020 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 if( p->pNext==p
7c030 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e Vfs ){. p->
7c040 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e pNext = pVfs->pN
7c050 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ext;. }. }.}
7c060 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
7c070 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 20 a VFS with the
7c080 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68 system. It is h
7c090 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73 armless to regis
7c0a0 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 ter the same.**
7c0b0 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d VFS multiple tim
7c0c0 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53 es. The new VFS
7c0d0 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 becomes the def
7c0e0 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74 ault if makeDflt
7c0f0 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a is.** true..*/.
7c100 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
7c110 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
7c120 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ter(sqlite3_vfs
7c130 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 *pVfs, int makeD
7c140 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f flt){. sqlite3_
7c150 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30 mutex *mutex = 0
7c160 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
7c170 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 _OMIT_AUTOINIT.
7c180 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
7c190 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 3_initialize();.
7c1a0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
7c1b0 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d n rc;.#endif. m
7c1c0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 utex = sqlite3Mu
7c1d0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
7c1e0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
7c1f0 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f TER);. sqlite3_
7c200 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
7c210 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 x);. vfsUnlink(
7c220 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b pVfs);. if( mak
7c230 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 eDflt || vfsList
7c240 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d ==0 ){. pVfs-
7c250 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 >pNext = vfsList
7c260 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 ;. vfsList =
7c270 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 pVfs;. }else{.
7c280 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d pVfs->pNext =
7c290 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b vfsList->pNext;
7c2a0 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e . vfsList->pN
7c2b0 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a ext = pVfs;. }.
7c2c0 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 assert(vfsList
7c2d0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
7c2e0 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b ex_leave(mutex);
7c2f0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
7c300 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e _OK;.}../*.** Un
7c310 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 73 register a VFS s
7c320 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 o that it is no
7c330 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c longer accessibl
7c340 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
7c350 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 int sqlite3_vfs
7c360 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 _unregister(sqli
7c370 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a te3_vfs *pVfs){.
7c380 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
7c390 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f DSAFE. sqlite3_
7c3a0 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 mutex *mutex = s
7c3b0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
7c3c0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
7c3d0 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 ATIC_MASTER);.#e
7c3e0 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d ndif. sqlite3_m
7c3f0 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
7c400 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 );. vfsUnlink(p
7c410 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f Vfs);. sqlite3_
7c420 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
7c430 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c x);. return SQL
7c440 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a ITE_OK;.}../****
7c450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
7c460 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a f os.c *********
7c470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
7c4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
7c4b0 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a file fault.c **
7c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
7c4f0 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2008 Jan 22.**.
7c500 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
7c510 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
7c520 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
7c530 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
7c540 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
7c550 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
7c560 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
7c570 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
7c580 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
7c590 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7c5a0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
7c5b0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
7c5c0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
7c5d0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7c5e0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
7c5f0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
7c600 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
7c610 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
7c620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
7c660 2a 20 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c 76 * $Id: fault.c,v
7c670 20 31 2e 31 31 20 32 30 30 38 2f 30 39 2f 30 32 1.11 2008/09/02
7c680 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 00:52:52 drh Ex
7c690 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 p $.*/../*.** Th
7c6a0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
7c6b0 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f 72 74 code to support
7c6c0 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 the concept of
7c6d0 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c "benign" .** mal
7c6e0 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28 77 68 loc failures (wh
7c6f0 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 en the xMalloc()
7c700 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 20 6d or xRealloc() m
7c710 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 ethod of the.**
7c720 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
7c730 6f 64 73 20 73 74 72 75 63 74 75 72 65 20 66 61 ods structure fa
7c740 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ils to allocate
7c750 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 a block of memor
7c760 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 73 y.** and returns
7c770 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 0). .**.** Most
7c780 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
7c790 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e are non-benign.
7c7a0 20 41 66 74 65 72 20 74 68 65 79 20 6f 63 63 75 After they occu
7c7b0 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62 61 r, SQLite.** aba
7c7c0 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72 65 6e ndons the curren
7c7d0 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 t operation and
7c7e0 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 returns an error
7c7f0 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a 2a code (usually.*
7c800 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 * SQLITE_NOMEM)
7c810 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 6f 77 to the user. How
7c820 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 20 ever, sometimes
7c830 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 6e a fault is not n
7c840 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66 61 ecessarily.** fa
7c850 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 tal. For example
7c860 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 , if a malloc fa
7c870 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69 ils while resizi
7c880 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 2c ng a hash table,
7c890 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f 6d this .** is com
7c8a0 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65 72 61 pletely recovera
7c8b0 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20 6e 6f ble simply by no
7c8c0 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74 20 74 t carrying out t
7c8d0 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65 20 0a he resize. The .
7c8e0 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 ** hash table wi
7c8f0 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 66 ll continue to f
7c900 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 unction normally
7c910 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 . So a malloc f
7c920 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69 6e ailure .** durin
7c930 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 72 g a hash table r
7c940 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e 69 67 esize is a benig
7c950 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 n fault..*/...#i
7c960 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
7c970 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 0a T_BUILTIN_TEST..
7c980 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 72 /*.** Global var
7c990 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 iables..*/.typed
7c9a0 65 66 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e ef struct Benign
7c9b0 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 MallocHooks Beni
7c9c0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 gnMallocHooks;.s
7c9d0 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
7c9e0 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 struct BenignMa
7c9f0 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f llocHooks {. vo
7ca00 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 id (*xBenignBegi
7ca10 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 n)(void);. void
7ca20 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 (*xBenignEnd)(v
7ca30 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33 48 oid);.} sqlite3H
7ca40 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d 3b ooks = { 0, 0 };
7ca50 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48 6f 6f ../* The "wsdHoo
7ca60 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 ks" macro will r
7ca70 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 70 esolve to the ap
7ca80 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69 67 6e propriate Benign
7ca90 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 MallocHooks.** s
7caa0 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 77 72 tructure. If wr
7cab0 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 itable static da
7cac0 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 ta is unsupporte
7cad0 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c d on the target,
7cae0 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c .** we have to l
7caf0 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 ocate the state
7cb00 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 vector at run-ti
7cb10 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 me. In the more
7cb20 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 common.** case
7cb30 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 where writable s
7cb40 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75 tatic data is su
7cb50 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f 6b pported, wsdHook
7cb60 73 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 s can refer dire
7cb70 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 ctly.** to the "
7cb80 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73 74 sqlite3Hooks" st
7cb90 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 ate vector decla
7cba0 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 red above..*/.#i
7cbb0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
7cbc0 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 _WSD.# define ws
7cbd0 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20 42 dHooksInit \. B
7cbe0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
7cbf0 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42 65 *x = &GLOBAL(Be
7cc00 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c nignMallocHooks,
7cc10 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23 20 sqlite3Hooks).#
7cc20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 define wsdHooks
7cc30 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 x[0].#else.# def
7cc40 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 ine wsdHooksInit
7cc50 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f .# define wsdHoo
7cc60 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 0a ks sqlite3Hooks.
7cc70 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 #endif.../*.** R
7cc80 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f egister hooks to
7cc90 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69 74 call when sqlit
7cca0 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
7ccb0 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c loc() and.** sql
7ccc0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
7ccd0 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c 65 64 loc() are called
7cce0 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a , respectively..
7ccf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
7cd00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 E void sqlite3Be
7cd10 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 nignMallocHooks(
7cd20 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 . void (*xBenig
7cd30 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a 20 nBegin)(void),.
7cd40 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 void (*xBenignE
7cd50 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20 77 nd)(void).){. w
7cd60 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 sdHooksInit;. w
7cd70 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 sdHooks.xBenignB
7cd80 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42 65 egin = xBenignBe
7cd90 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e gin;. wsdHooks.
7cda0 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42 65 xBenignEnd = xBe
7cdb0 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a nignEnd;.}../*.*
7cdc0 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 33 45 * This (sqlite3E
7cdd0 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
7cde0 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53 ) is called by S
7cdf0 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 69 6e QLite code to in
7ce00 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 73 dicate that.** s
7ce10 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 ubsequent malloc
7ce20 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 62 65 failures are be
7ce30 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f 20 nign. A call to
7ce40 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
7ce50 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 Malloc().** indi
7ce60 63 61 74 65 73 20 74 68 61 74 20 73 75 62 73 65 cates that subse
7ce70 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 quent malloc fai
7ce80 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 lures are non-be
7ce90 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nign..*/.SQLITE_
7cea0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
7ceb0 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
7cec0 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 alloc(void){. w
7ced0 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 sdHooksInit;. i
7cee0 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e f( wsdHooks.xBen
7cef0 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 ignBegin ){.
7cf00 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e wsdHooks.xBenign
7cf10 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 Begin();. }.}.S
7cf20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7cf30 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e id sqlite3EndBen
7cf40 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b ignMalloc(void){
7cf50 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b . wsdHooksInit;
7cf60 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e . if( wsdHooks.
7cf70 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20 20 xBenignEnd ){.
7cf80 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 wsdHooks.xBeni
7cf90 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a gnEnd();. }.}..
7cfa0 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 6e #endif /* #ifn
7cfb0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7cfc0 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a BUILTIN_TEST */.
7cfd0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7cfe0 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63 20 End of fault.c
7cff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7d020 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7d030 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 30 Begin file mem0
7d040 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
7d050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7d070 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f ./*.** 2008 Octo
7d080 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 28.**.** The
7d090 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
7d0a0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
7d0b0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
7d0c0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
7d0d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
7d0e0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
7d0f0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
7d100 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
7d110 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
7d120 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
7d130 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
7d140 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
7d150 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
7d160 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
7d170 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
7d180 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
7d190 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
7d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1e0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
7d1f0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 file contains a
7d200 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61 6c no-op memory al
7d210 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 location drivers
7d220 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a 2a for use when.**
7d230 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c SQLITE_ZERO_MAL
7d240 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 2e 20 LOC is defined.
7d250 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 The allocation
7d260 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d 65 6e drivers implemen
7d270 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77 61 ted.** here alwa
7d280 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69 74 65 ys fail. SQLite
7d290 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61 74 will not operat
7d2a0 65 20 77 69 74 68 20 74 68 65 73 65 20 64 72 69 e with these dri
7d2b0 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a 20 vers. These.**
7d2c0 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61 63 65 are merely place
7d2d0 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c 20 64 holders. Real d
7d2e0 72 69 76 65 72 73 20 6d 75 73 74 20 62 65 20 73 rivers must be s
7d2f0 75 62 73 74 69 74 75 74 65 64 20 75 73 69 6e 67 ubstituted using
7d300 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 .** sqlite3_conf
7d310 69 67 28 29 20 62 65 66 6f 72 65 20 53 51 4c 69 ig() before SQLi
7d320 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74 65 2e te will operate.
7d330 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 .**.** $Id: mem0
7d340 2e 63 2c 76 20 31 2e 31 20 32 30 30 38 2f 31 30 .c,v 1.1 2008/10
7d350 2f 32 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68 /28 18:58:20 drh
7d360 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
7d370 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
7d380 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
7d390 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 cator is the def
7d3a0 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 ault. It is.**
7d3b0 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 used when no oth
7d3c0 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 er memory alloca
7d3d0 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64 tor is specified
7d3e0 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 using compile-t
7d3f0 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a ime.** macros..*
7d400 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
7d410 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a ZERO_MALLOC../*.
7d420 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e ** No-op version
7d430 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 s of all memory
7d440 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 allocation routi
7d450 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f nes.*/.static vo
7d460 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 id *sqlite3MemMa
7d470 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b lloc(int nByte){
7d480 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 return 0; }.sta
7d490 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
7d4a0 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 MemFree(void *pP
7d4b0 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d rior){ return; }
7d4c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 .static void *sq
7d4d0 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 lite3MemRealloc(
7d4e0 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e void *pPrior, in
7d4f0 74 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e t nByte){ return
7d500 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 0; }.static int
7d510 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 sqlite3MemSize(
7d520 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 void *pPrior){ r
7d530 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 eturn 0; }.stati
7d540 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
7d550 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 Roundup(int n){
7d560 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 return n; }.stat
7d570 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
7d580 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 mInit(void *NotU
7d590 73 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c sed){ return SQL
7d5a0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
7d5b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
7d5c0 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e Shutdown(void *N
7d5d0 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b otUsed){ return;
7d5e0 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 }../*.** This r
7d5f0 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e outine is the on
7d600 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 ly routine in th
7d610 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 is file with ext
7d620 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a ernal linkage..*
7d630 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 *.** Populate th
7d640 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f e low-level memo
7d650 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 ry allocation fu
7d660 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 nction pointers
7d670 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f in.** sqlite3Glo
7d680 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 balConfig.m with
7d690 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
7d6a0 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 routines in thi
7d6b0 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 s file..*/.SQLIT
7d6c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
7d6d0 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 qlite3MemSetDefa
7d6e0 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 ult(void){. sta
7d6f0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
7d700 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 3_mem_methods de
7d710 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b faultMethods = {
7d720 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
7d730 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c Malloc,. sql
7d740 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 ite3MemFree,.
7d750 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c sqlite3MemReal
7d760 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 loc,. sqlite
7d770 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 3MemSize,. s
7d780 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 qlite3MemRoundup
7d790 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
7d7a0 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 mInit,. sqli
7d7b0 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a te3MemShutdown,.
7d7c0 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 0. };. sq
7d7d0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c lite3_config(SQL
7d7e0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f ITE_CONFIG_MALLO
7d7f0 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f C, &defaultMetho
7d800 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f ds);.}..#endif /
7d810 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 * SQLITE_ZERO_MA
7d820 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a LLOC */../******
7d830 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
7d840 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem0.c *********
7d850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d870 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
7d880 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
7d890 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a ile mem1.c *****
7d8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d8c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
7d8d0 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 007 August 14.**
7d8e0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
7d8f0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
7d900 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
7d910 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
7d920 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
7d930 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
7d940 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
7d950 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
7d960 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
7d970 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
7d980 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
7d990 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
7d9a0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
7d9b0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
7d9c0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
7d9d0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
7d9e0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
7d9f0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
7da00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7da30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
7da40 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
7da50 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 tains low-level
7da60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
7da70 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 n drivers for wh
7da80 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c en.** SQLite wil
7da90 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 l use the standa
7daa0 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c rd C-library mal
7dab0 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 loc/realloc/free
7dac0 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f interface.** to
7dad0 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f obtain the memo
7dae0 72 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a ry it needs..**.
7daf0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
7db00 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 tains implementa
7db10 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 tions of the low
7db20 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
7db30 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 location.** rout
7db40 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69 ines specified i
7db50 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 n the sqlite3_me
7db60 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 m_methods object
7db70 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d ..**.** $Id: mem
7db80 31 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f 1.c,v 1.30 2009/
7db90 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64 03/23 04:33:33 d
7dba0 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
7dbb0 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .*/../*.** This
7dbc0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
7dbd0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
7dbe0 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 is the default.
7dbf0 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 It is.** used w
7dc00 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d hen no other mem
7dc10 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 ory allocator is
7dc20 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 specified using
7dc30 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a compile-time.**
7dc40 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 macros..*/.#ifd
7dc50 65 66 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d ef SQLITE_SYSTEM
7dc60 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c _MALLOC../*.** L
7dc70 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 ike malloc(), bu
7dc80 74 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73 t remember the s
7dc90 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
7dca0 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 ation.** so that
7dcb0 20 77 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20 we can find it
7dcc0 6c 61 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69 later using sqli
7dcd0 74 65 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a te3MemSize()..**
7dce0 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 .** For this low
7dcf0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 -level routine,
7dd00 77 65 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 we are guarantee
7dd10 64 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 d that nByte>0 b
7dd20 65 63 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20 ecause.** cases
7dd30 6f 66 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c of nByte<=0 will
7dd40 20 62 65 20 69 6e 74 65 72 63 65 70 74 65 64 20 be intercepted
7dd50 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 20 62 and dealt with b
7dd60 79 20 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a y higher level.*
7dd70 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 * routines..*/.s
7dd80 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 tatic void *sqli
7dd90 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 te3MemMalloc(int
7dda0 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 nByte){. sqlit
7ddb0 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 e3_int64 *p;. a
7ddc0 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 ssert( nByte>0 )
7ddd0 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e ;. nByte = ROUN
7dde0 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d D8(nByte);. p =
7ddf0 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38 malloc( nByte+8
7de00 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 );. if( p ){.
7de10 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b p[0] = nByte;
7de20 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 . p++;. }.
7de30 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70 return (void *)p
7de40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 ;.}../*.** Like
7de50 66 72 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73 free() but works
7de60 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 for allocations
7de70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
7de80 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 qlite3MemMalloc(
7de90 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d ).** or sqlite3M
7dea0 65 6d 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a emRealloc()..**.
7deb0 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d ** For this low-
7dec0 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 level routine, w
7ded0 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 e already know t
7dee0 68 61 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69 hat pPrior!=0 si
7def0 6e 63 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 nce.** cases whe
7df00 72 65 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c re pPrior==0 wil
7df10 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 l have been inte
7df20 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 cepted and dealt
7df30 20 77 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68 with.** by high
7df40 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 er-level routine
7df50 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
7df60 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 d sqlite3MemFree
7df70 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a (void *pPrior){.
7df80 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
7df90 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e *p = (sqlite3_in
7dfa0 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 t64*)pPrior;. a
7dfb0 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 ssert( pPrior!=0
7dfc0 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65 );. p--;. fre
7dfd0 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c e(p);.}../*.** L
7dfe0 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20 ike realloc().
7dff0 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 Resize an alloca
7e000 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 tion previously
7e010 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
7e020 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f sqlite3MemMallo
7e030 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 c()..**.** For t
7e040 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e his low-level in
7e050 74 65 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77 terface, we know
7e060 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e that pPrior!=0.
7e070 20 20 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a Cases where.**
7e080 20 70 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65 pPrior==0 while
7e090 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 have been inter
7e0a0 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72 cepted by higher
7e0b0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61 -level routine a
7e0c0 6e 64 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64 nd.** redirected
7e0d0 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 to xMalloc. Si
7e0e0 6d 69 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 milarly, we know
7e0f0 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65 that nByte>0 be
7e100 63 61 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20 causes.** cases
7e110 77 68 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77 where nByte<=0 w
7e120 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e ill have been in
7e130 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 67 tercepted by hig
7e140 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 her-level.** rou
7e150 74 69 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65 tines and redire
7e160 63 74 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a cted to xFree..*
7e170 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 /.static void *s
7e180 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
7e190 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 (void *pPrior, i
7e1a0 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c nt nByte){. sql
7e1b0 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 ite3_int64 *p =
7e1c0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
7e1d0 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 pPrior;. assert
7e1e0 28 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e ( pPrior!=0 && n
7e1f0 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 Byte>0 );. nByt
7e200 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 e = ROUND8(nByte
7e210 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 );. p = (sqlite
7e220 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
7e230 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65 . p--;. p = re
7e240 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38 alloc(p, nByte+8
7e250 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 );. if( p ){.
7e260 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b p[0] = nByte;
7e270 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 . p++;. }.
7e280 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
7e290 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 .}../*.** Report
7e2a0 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 the allocated s
7e2b0 69 7a 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72 ize of a prior r
7e2c0 65 74 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c eturn from xMall
7e2d0 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c oc().** or xReal
7e2e0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
7e2f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 int sqlite3MemS
7e300 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ize(void *pPrior
7e310 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
7e320 36 34 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 64 *p;. if( pPr
7e330 69 6f 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ior==0 ) return
7e340 30 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 0;. p = (sqlite
7e350 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
7e360 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e . p--;. return
7e370 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f (int)p[0];.}../
7e380 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 *.** Round up a
7e390 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 request size to
7e3a0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 the next valid a
7e3b0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a llocation size..
7e3c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
7e3d0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 lite3MemRoundup(
7e3e0 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e int n){. return
7e3f0 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f ROUND8(n);.}../
7e400 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
7e410 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a this module..*/.
7e420 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
7e430 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a e3MemInit(void *
7e440 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 NotUsed){. UNUS
7e450 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
7e460 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 Used);. return
7e470 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
7e480 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
7e490 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
7e4a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
7e4b0 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 ite3MemShutdown(
7e4c0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
7e4d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
7e4e0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
7e4f0 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn;.}../*.**
7e500 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
7e510 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
7e520 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
7e530 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b th external link
7e540 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c age..**.** Popul
7e550 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 ate the low-leve
7e560 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
7e570 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 ion function poi
7e580 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 nters in.** sqli
7e590 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
7e5a0 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 m with pointers
7e5b0 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 to the routines
7e5c0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f in this file..*/
7e5d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7e5e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 void sqlite3MemS
7e5f0 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b etDefault(void){
7e600 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
7e610 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
7e620 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f ods defaultMetho
7e630 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 ds = {. sqli
7e640 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 te3MemMalloc,.
7e650 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 sqlite3MemFre
7e660 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d e,. sqlite3M
7e670 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 emRealloc,.
7e680 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a sqlite3MemSize,.
7e690 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 sqlite3MemR
7e6a0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c oundup,. sql
7e6b0 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 ite3MemInit,.
7e6c0 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 sqlite3MemShut
7e6d0 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d down,. 0. }
7e6e0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ;. sqlite3_conf
7e6f0 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
7e700 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c _MALLOC, &defaul
7e710 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 tMethods);.}..#e
7e720 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 ndif /* SQLITE_S
7e730 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a YSTEM_MALLOC */.
7e740 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7e750 20 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a End of mem1.c *
7e760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7e790 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7e7a0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32 Begin file mem2
7e7b0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
7e7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7e7e0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
7e7f0 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 15.**.** The
7e800 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
7e810 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
7e820 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
7e830 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
7e840 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
7e850 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
7e860 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
7e870 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
7e880 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
7e890 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
7e8a0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
7e8b0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
7e8c0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
7e8d0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
7e8e0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
7e8f0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
7e900 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
7e910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e950 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
7e960 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f file contains lo
7e970 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
7e980 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 llocation driver
7e990 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 s for when.** SQ
7e9a0 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 Lite will use th
7e9b0 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 e standard C-lib
7e9c0 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c rary malloc/real
7e9d0 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 loc/free interfa
7e9e0 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 ce.** to obtain
7e9f0 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 the memory it ne
7ea00 65 64 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67 eds while adding
7ea10 20 6c 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f lots of additio
7ea20 6e 61 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a nal debugging.**
7ea30 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 information to
7ea40 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 each allocation
7ea50 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70 in order to help
7ea60 20 64 65 74 65 63 74 20 61 6e 64 20 66 69 78 20 detect and fix
7ea70 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 memory.** leaks
7ea80 61 6e 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 and memory usage
7ea90 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 errors..**.** T
7eaa0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
7eab0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
7eac0 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 s of the low-lev
7ead0 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
7eae0 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
7eaf0 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
7eb00 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 e sqlite3_mem_me
7eb10 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
7eb20 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 32 2e 63 2c .** $Id: mem2.c,
7eb30 76 20 31 2e 34 35 20 32 30 30 39 2f 30 33 2f 32 v 1.45 2009/03/2
7eb40 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e 69 65 3 04:33:33 danie
7eb50 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
7eb60 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 ./*.** This vers
7eb70 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
7eb80 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 y allocator is u
7eb90 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a sed only if the.
7eba0 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 ** SQLITE_MEMDEB
7ebb0 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 UG macro is defi
7ebc0 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ned.*/.#ifdef SQ
7ebd0 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f LITE_MEMDEBUG../
7ebe0 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 *.** The backtra
7ebf0 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 ce functionality
7ec00 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
7ec10 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f le with GLIBC.*/
7ec20 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f .#ifdef __GLIBC_
7ec30 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 _. extern int b
7ec40 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c acktrace(void**,
7ec50 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 int);. extern v
7ec60 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79 oid backtrace_sy
7ec70 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f mbols_fd(void*co
7ec80 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 nst*,int,int);.#
7ec90 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61 else.# define ba
7eca0 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23 cktrace(A,B) 1.#
7ecb0 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 define backtrac
7ecc0 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 e_symbols_fd(A,B
7ecd0 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ,C).#endif../*.*
7ece0 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c * Each memory al
7ecf0 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c location looks l
7ed00 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
7ed10 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
7ed20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ed30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ed40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ed50 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 ---------.** |
7ed60 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 Title | backtra
7ed70 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d ce pointers | M
7ed80 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c emBlockHdr | al
7ed90 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 location | EndG
7eda0 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d uard |.** -----
7edb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7edc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7edd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7ede0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
7edf0 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 ---.**.** The ap
7ee00 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 plication code s
7ee10 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 ees only a point
7ee20 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 er to the alloca
7ee30 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a tion. We have.*
7ee40 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f * to back up fro
7ee50 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e m the allocation
7ee60 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 pointer to find
7ee70 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 the MemBlockHdr
7ee80 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f . The.** MemBlo
7ee90 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74 ckHdr tells us t
7eea0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 he size of the a
7eeb0 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 llocation and th
7eec0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 e number of.** b
7eed0 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 acktrace pointer
7eee0 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 s. There is als
7eef0 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 61 o a guard word a
7ef00 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
7ef10 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e .** MemBlockHdr.
7ef20 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c .*/.struct MemBl
7ef30 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69 ockHdr {. i64 i
7ef40 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
7ef50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
7ef60 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61 * Size of this a
7ef70 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 llocation */. s
7ef80 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 truct MemBlockHd
7ef90 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 r *pNext, *pPrev
7efa0 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 ; /* Linked lis
7efb0 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 t of all unfreed
7efc0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 memory */. cha
7efd0 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20 r nBacktrace;
7efe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7eff0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 /* Number of ba
7f000 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73 cktraces on this
7f010 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 alloc */. char
7f020 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 nBacktraceSlots
7f030 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7f040 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63 /* Available bac
7f050 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a ktrace slots */.
7f060 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 short nTitle;
7f070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f080 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
7f090 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 f title; include
7f0a0 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 s '\0' */. int
7f0b0 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20 iForeGuard;
7f0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f0d0 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f /* Guard word fo
7f0e0 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a r sanity */.};..
7f0f0 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 /*.** Guard word
7f100 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 s.*/.#define FOR
7f110 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31 35 EGUARD 0x80F5E15
7f120 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 3.#define REARGU
7f130 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a ARD 0xE4676B53..
7f140 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /*.** Number of
7f150 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 malloc size incr
7f160 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e ements to track.
7f170 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 .*/.#define NCSI
7f180 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 ZE 1000../*.**
7f190 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 All of the stati
7f1a0 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 c variables used
7f1b0 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 by this module
7f1c0 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a are collected.**
7f1d0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 into a single s
7f1e0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 tructure named "
7f1f0 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 mem". This is t
7f200 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
7f210 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
7f220 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
7f230 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
7f240 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
7f250 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
7f260 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
7f270 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
7f280 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
7f290 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 atic struct {.
7f2a0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 . /*. ** Mutex
7f2b0 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 to control acce
7f2c0 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 ss to the memory
7f2d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
7f2e0 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 ystem.. */. sq
7f2f0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
7f300 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 ex;.. /*. ** H
7f310 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 ead and tail of
7f320 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 a linked list of
7f330 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 all outstanding
7f340 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a allocations. *
7f350 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c /. struct MemBl
7f360 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a ockHdr *pFirst;.
7f370 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
7f380 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a kHdr *pLast;. .
7f390 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 /*. ** The nu
7f3a0 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f mber of levels o
7f3b0 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73 f backtrace to s
7f3c0 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 ave in new alloc
7f3d0 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 ations.. */. i
7f3e0 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 nt nBacktrace;.
7f3f0 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 void (*xBacktra
7f400 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f ce)(int, int, vo
7f410 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 id **);.. /*.
7f420 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f ** Title text to
7f430 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 insert in front
7f440 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 of each block.
7f450 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 */. int nTitle
7f460 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 ; /* Byte
7f470 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 s of zTitle to s
7f480 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 ave. Includes '
7f490 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20 \0' and padding
7f4a0 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65 */. char zTitle
7f4b0 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 [100]; /* The t
7f4c0 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 itle text */..
7f4d0 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 /* . ** sqlite3
7f4e0 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 MallocDisallow()
7f4f0 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 increments the
7f500 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 following counte
7f510 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d r.. ** sqlite3M
7f520 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 allocAllow() dec
7f530 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f rements it.. */
7f540 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b . int disallow;
7f550 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 /* Do not allow
7f560 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
7f570 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a on */.. /*. **
7f580 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74 69 Gather statisti
7f590 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 cs on the sizes
7f5a0 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 of memory alloca
7f5b0 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c tions.. ** nAll
7f5c0 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d oc[i] is the num
7f5d0 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f ber of allocatio
7f5e0 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a n attempts of i*
7f5f0 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 8. ** bytes. i
7f600 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20 ==NCSIZE is the
7f610 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
7f620 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f tion attempts fo
7f630 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 r. ** sizes mor
7f640 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 e than NCSIZE*8
7f650 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e bytes.. */. in
7f660 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d t nAlloc[NCSIZE]
7f670 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 ; /* Total
7f680 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
7f690 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e tions */. int n
7f6a0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b Current[NCSIZE];
7f6b0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e /* Current n
7f6c0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
7f6d0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 ions */. int mx
7f6e0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b Current[NCSIZE];
7f6f0 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 /* Highwater
7f700 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e mark for nCurren
7f710 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f t */..} mem;.../
7f720 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f *.** Adjust memo
7f730 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73 74 ry usage statist
7f740 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ics.*/.static vo
7f750 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 69 id adjustStats(i
7f760 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e nt iSize, int in
7f770 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 crement){. int
7f780 69 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a 65 i = ROUND8(iSize
7f790 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 )/8;. if( i>NCS
7f7a0 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d IZE-1 ){. i =
7f7b0 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d NCSIZE - 1;. }
7f7c0 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 . if( increment
7f7d0 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 >0 ){. mem.nA
7f7e0 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d lloc[i]++;. m
7f7f0 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b em.nCurrent[i]++
7f800 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 ;. if( mem.nC
7f810 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 urrent[i]>mem.mx
7f820 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 Current[i] ){.
7f830 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e mem.mxCurren
7f840 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 t[i] = mem.nCurr
7f850 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 ent[i];. }.
7f860 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e }else{. mem.n
7f870 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 Current[i]--;.
7f880 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 assert( mem.nC
7f890 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a urrent[i]>=0 );.
7f8a0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 }.}../*.** Giv
7f8b0 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e en an allocation
7f8c0 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c , find the MemBl
7f8d0 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 ockHdr for that
7f8e0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a allocation..**.*
7f8f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
7f900 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 hecks the guards
7f910 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f at either end o
7f920 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
7f930 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 and.** if they
7f940 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 are incorrect it
7f950 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 asserts..*/.sta
7f960 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c tic struct MemBl
7f970 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d ockHdr *sqlite3M
7f980 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76 emsysGetHeader(v
7f990 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e oid *pAllocation
7f9a0 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 ){. struct MemB
7f9b0 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e lockHdr *p;. in
7f9c0 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 t *pInt;. u8 *p
7f9d0 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 U8;. int nReser
7f9e0 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 ve;.. p = (stru
7f9f0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 ct MemBlockHdr*)
7fa00 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 pAllocation;. p
7fa10 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d --;. assert( p-
7fa20 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e >iForeGuard==(in
7fa30 74 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 t)FOREGUARD );.
7fa40 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e nReserve = ROUN
7fa50 44 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 D8(p->iSize);.
7fa60 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c pInt = (int*)pAl
7fa70 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20 location;. pU8
7fa80 3d 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69 = (u8*)pAllocati
7fa90 6f 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 on;. assert( pI
7faa0 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 nt[nReserve/size
7fab0 6f 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 of(int)]==(int)R
7fac0 45 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f 2a EARGUARD );. /*
7fad0 20 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e 79 This checks any
7fae0 20 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 20 of the "extra"
7faf0 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 bytes allocated
7fb00 64 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 6e due. ** to roun
7fb10 64 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 20 ding up to an 8
7fb20 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 6f byte boundary to
7fb30 20 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74 68 ensure . ** th
7fb40 65 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e 20 ey haven't been
7fb50 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a overwritten.. *
7fb60 2f 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73 65 /. while( nRese
7fb70 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a 65 rve-- > p->iSize
7fb80 20 29 20 61 73 73 65 72 74 28 20 70 55 38 5b 6e ) assert( pU8[n
7fb90 52 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20 29 Reserve]==0x65 )
7fba0 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
7fbb0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
7fbc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
7fbd0 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f s currently allo
7fbe0 63 61 74 65 64 20 61 74 20 61 64 64 72 65 73 73 cated at address
7fbf0 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e p..*/.static in
7fc00 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 t sqlite3MemSize
7fc10 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72 (void *p){. str
7fc20 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
7fc30 2a 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20 *pHdr;. if( !p
7fc40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
7fc50 0a 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71 . }. pHdr = sq
7fc60 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 lite3MemsysGetHe
7fc70 61 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72 ader(p);. retur
7fc80 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d n pHdr->iSize;.}
7fc90 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
7fca0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
7fcb0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
7fcc0 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e em..*/.static in
7fcd0 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 t sqlite3MemInit
7fce0 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
7fcf0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
7fd00 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
7fd10 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28 assert( (sizeof(
7fd20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
7fd30 64 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a 20 dr)&7) == 0 );.
7fd40 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f if( !sqlite3Glo
7fd50 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
7fd60 61 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 at ){. /* If
7fd70 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69 73 memory status is
7fd80 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 enabled, then t
7fd90 68 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70 he malloc.c wrap
7fda0 70 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 per will already
7fdb0 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65 . ** hold the
7fdc0 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 STATIC_MEM mute
7fdd0 78 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74 69 x when the routi
7fde0 6e 65 73 20 68 65 72 65 20 61 72 65 20 69 6e 76 nes here are inv
7fdf0 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d oked. */. mem
7fe00 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 .mutex = sqlite3
7fe10 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
7fe20 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
7fe30 45 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 EM);. }. retur
7fe40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
7fe50 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 /*.** Deinitiali
7fe60 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
7fe70 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
7fe80 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f em..*/.static vo
7fe90 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 id sqlite3MemShu
7fea0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
7feb0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
7fec0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
7fed0 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d );. mem.mutex =
7fee0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 0;.}../*.** Rou
7fef0 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 nd up a request
7ff00 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 size to the next
7ff10 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f valid allocatio
7ff20 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 n size..*/.stati
7ff30 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
7ff40 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a Roundup(int n){.
7ff50 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 return ROUND8(
7ff60 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c n);.}../*.** All
7ff70 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 ocate nByte byte
7ff80 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a s of memory..*/.
7ff90 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c static void *sql
7ffa0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e ite3MemMalloc(in
7ffb0 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 t nByte){. stru
7ffc0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
7ffd0 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
7ffe0 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 Bt;. char *z;.
7fff0 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f int *pInt;. vo
80000 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 id *p = 0;. int
80010 20 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e totalSize;. in
80020 74 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 t nReserve;. sq
80030 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
80040 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 r(mem.mutex);.
80050 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 assert( mem.disa
80060 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 llow==0 );. nRe
80070 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 6e serve = ROUND8(n
80080 42 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53 69 Byte);. totalSi
80090 7a 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 ze = nReserve +
800a0 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 sizeof(*pHdr) +
800b0 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 sizeof(int) +.
800c0 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d mem
800d0 2e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 .nBacktrace*size
800e0 6f 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e of(void*) + mem.
800f0 6e 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 nTitle;. p = ma
80100 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b lloc(totalSize);
80110 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
80120 7a 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20 3d z = p;. pBt =
80130 20 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e (void**)&z[mem.
80140 6e 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 nTitle];. pHd
80150 72 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 r = (struct MemB
80160 6c 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 lockHdr*)&pBt[me
80170 6d 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 m.nBacktrace];.
80180 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d pHdr->pNext =
80190 20 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 0;. pHdr->pP
801a0 72 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b rev = mem.pLast;
801b0 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 . if( mem.pLa
801c0 73 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e st ){. mem.
801d0 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 pLast->pNext = p
801e0 48 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a Hdr;. }else{.
801f0 20 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 mem.pFirst
80200 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 = pHdr;. }.
80210 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 mem.pLast = p
80220 48 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 Hdr;. pHdr->i
80230 46 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52 45 ForeGuard = FORE
80240 47 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72 2d GUARD;. pHdr-
80250 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
80260 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 = mem.nBacktrac
80270 65 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 e;. pHdr->nTi
80280 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 tle = mem.nTitle
80290 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 ;. if( mem.nB
802a0 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 acktrace ){.
802b0 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 void *aAddr[40
802c0 5d 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e ];. pHdr->n
802d0 42 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63 6b Backtrace = back
802e0 74 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65 6d trace(aAddr, mem
802f0 2e 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 .nBacktrace+1)-1
80300 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 ;. memcpy(p
80310 42 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 Bt, &aAddr[1], p
80320 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a Hdr->nBacktrace*
80330 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a sizeof(void*));.
80340 20 20 20 20 20 20 61 73 73 65 72 74 28 70 42 74 assert(pBt
80350 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 [0]);. if(
80360 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 29 mem.xBacktrace )
80370 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 42 {. mem.xB
80380 61 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c 20 acktrace(nByte,
80390 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
803a0 2d 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b 0a -1, &aAddr[1]);.
803b0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
803c0 65 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e e{. pHdr->n
803d0 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a 20 Backtrace = 0;.
803e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d }. if( mem
803f0 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 20 .nTitle ){.
80400 20 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a memcpy(z, mem.z
80410 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c Title, mem.nTitl
80420 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 48 e);. }. pH
80430 64 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79 74 dr->iSize = nByt
80440 65 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74 61 e;. adjustSta
80450 74 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a 20 ts(nByte, +1);.
80460 20 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 pInt = (int*)
80470 26 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70 49 &pHdr[1];. pI
80480 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 nt[nReserve/size
80490 6f 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52 47 of(int)] = REARG
804a0 55 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65 74 UARD;. memset
804b0 28 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 65 (pInt, 0x65, nRe
804c0 73 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d 20 serve);. p =
804d0 28 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d (void*)pInt;. }
804e0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
804f0 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 _leave(mem.mutex
80500 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a );. return p; .
80510 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 }../*.** Free me
80520 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mory..*/.static
80530 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 void sqlite3MemF
80540 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
80550 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 ){. struct MemB
80560 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 lockHdr *pHdr;.
80570 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 void **pBt;. c
80580 68 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 har *z;. assert
80590 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
805a0 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c onfig.bMemstat |
805b0 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29 | mem.mutex!=0 )
805c0 3b 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 ;. pHdr = sqlit
805d0 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 e3MemsysGetHeade
805e0 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42 74 r(pPrior);. pBt
805f0 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b = (void**)pHdr;
80600 0a 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e . pBt -= pHdr->
80610 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b nBacktraceSlots;
80620 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
80630 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 _enter(mem.mutex
80640 29 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 );. if( pHdr->p
80650 50 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 65 Prev ){. asse
80660 72 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 2d rt( pHdr->pPrev-
80670 3e 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b 0a >pNext==pHdr );.
80680 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 2d pHdr->pPrev-
80690 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e 70 >pNext = pHdr->p
806a0 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Next;. }else{.
806b0 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 assert( mem.p
806c0 46 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 First==pHdr );.
806d0 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 mem.pFirst =
806e0 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d pHdr->pNext;. }
806f0 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e 65 . if( pHdr->pNe
80700 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 xt ){. assert
80710 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 ( pHdr->pNext->p
80720 50 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 20 Prev==pHdr );.
80730 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 pHdr->pNext->p
80740 50 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72 Prev = pHdr->pPr
80750 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ev;. }else{.
80760 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c 61 assert( mem.pLa
80770 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 st==pHdr );.
80780 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 mem.pLast = pHdr
80790 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 7a ->pPrev;. }. z
807a0 20 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a 20 = (char*)pBt;.
807b0 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 74 z -= pHdr->nTit
807c0 6c 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61 74 le;. adjustStat
807d0 73 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 2d s(pHdr->iSize, -
807e0 31 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c 20 1);. memset(z,
807f0 30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 69 0x2b, sizeof(voi
80800 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b 74 d*)*pHdr->nBackt
80810 72 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a 65 raceSlots + size
80820 6f 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 20 of(*pHdr) +.
80830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 48 pH
80840 64 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a 65 dr->iSize + size
80850 6f 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d 3e of(int) + pHdr->
80860 6e 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 28 nTitle);. free(
80870 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 z);. sqlite3_mu
80880 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 tex_leave(mem.mu
80890 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a tex); .}../*.**
808a0 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 Change the size
808b0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 of an existing
808c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
808d0 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 n..**.** For thi
808e0 73 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 6c s debugging impl
808f0 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 2a ementation, we *
80900 61 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 63 always* make a c
80910 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c opy of the.** al
80920 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20 location into a
80930 6e 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d new place in mem
80940 6f 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 61 ory. In this wa
80950 79 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 69 y, if the .** hi
80960 67 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20 gher level code
80970 69 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72 is using pointer
80980 20 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f to the old allo
80990 63 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a 2a cation, it is .*
809a0 2a 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 * much more like
809b0 6c 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20 ly to break and
809c0 77 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65 we are much more
809d0 20 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0a liking to find.
809e0 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f ** the error..*/
809f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 .static void *sq
80a00 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 lite3MemRealloc(
80a10 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e void *pPrior, in
80a20 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 t nByte){. stru
80a30 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
80a40 70 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64 20 pOldHdr;. void
80a50 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 28 *pNew;. assert(
80a60 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 mem.disallow==0
80a70 20 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20 );. pOldHdr =
80a80 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 sqlite3MemsysGet
80a90 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a Header(pPrior);.
80aa0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
80ab0 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 MemMalloc(nByte)
80ac0 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a ;. if( pNew ){.
80ad0 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c memcpy(pNew,
80ae0 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70 pPrior, nByte<p
80af0 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20 OldHdr->iSize ?
80b00 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d nByte : pOldHdr-
80b10 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 >iSize);. if(
80b20 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e nByte>pOldHdr->
80b30 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6d iSize ){. m
80b40 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 emset(&((char*)p
80b50 4e 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 53 New)[pOldHdr->iS
80b60 69 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79 74 ize], 0x2b, nByt
80b70 65 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 e - pOldHdr->iSi
80b80 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ze);. }. s
80b90 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50 qlite3MemFree(pP
80ba0 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 rior);. }. ret
80bb0 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
80bc0 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 ** Populate the
80bd0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
80be0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
80bf0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e tion pointers in
80c00 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 .** sqlite3Globa
80c10 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 lConfig.m with p
80c20 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 ointers to the r
80c30 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 outines in this
80c40 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f file..*/.SQLITE_
80c50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
80c60 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c ite3MemSetDefaul
80c70 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 t(void){. stati
80c80 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
80c90 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 mem_methods defa
80ca0 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 ultMethods = {.
80cb0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 sqlite3MemMa
80cc0 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 lloc,. sqlit
80cd0 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 e3MemFree,.
80ce0 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f sqlite3MemReallo
80cf0 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d c,. sqlite3M
80d00 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c emSize,. sql
80d10 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a ite3MemRoundup,.
80d20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 sqlite3MemI
80d30 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 nit,. sqlite
80d40 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 3MemShutdown,.
80d50 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 0. };. sqli
80d60 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
80d70 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c E_CONFIG_MALLOC,
80d80 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 &defaultMethods
80d90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 );.}../*.** Set
80da0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 61 the number of ba
80db0 63 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20 6b cktrace levels k
80dc0 65 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c 6c ept for each all
80dd0 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61 ocation..** A va
80de0 6c 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72 6e lue of zero turn
80df0 73 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69 6e s off backtracin
80e00 67 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 69 g. The number i
80e10 73 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65 64 s always rounded
80e20 0a 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c 74 .** up to a mult
80e30 69 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51 iple of 2..*/.SQ
80e40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
80e50 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 d sqlite3Memdebu
80e60 67 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 64 gBacktrace(int d
80e70 65 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65 70 epth){. if( dep
80e80 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d 20 th<0 ){ depth =
80e90 30 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74 68 0; }. if( depth
80ea0 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20 32 >20 ){ depth = 2
80eb0 30 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 28 0; }. depth = (
80ec0 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a 20 depth+1)&0xfe;.
80ed0 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 mem.nBacktrace
80ee0 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49 = depth;.}..SQLI
80ef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
80f00 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42 sqlite3MemdebugB
80f10 61 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63 6b acktraceCallback
80f20 28 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 (void (*xBacktra
80f30 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f ce)(int, int, vo
80f40 69 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78 id **)){. mem.x
80f50 42 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61 63 Backtrace = xBac
80f60 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ktrace;.}../*.**
80f70 20 53 65 74 20 74 68 65 20 74 69 74 6c 65 20 73 Set the title s
80f80 74 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65 71 tring for subseq
80f90 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 uent allocations
80fa0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
80fb0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
80fc0 4d 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c 65 MemdebugSettitle
80fd0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 69 (const char *zTi
80fe0 74 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 tle){. unsigned
80ff0 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 int n = sqlite3
81000 53 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 29 Strlen30(zTitle)
81010 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 5f + 1;. sqlite3_
81020 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e mutex_enter(mem.
81030 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 3e mutex);. if( n>
81040 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 =sizeof(mem.zTit
81050 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f 66 le) ) n = sizeof
81060 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a (mem.zTitle)-1;.
81070 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54 69 memcpy(mem.zTi
81080 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29 3b tle, zTitle, n);
81090 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d . mem.zTitle[n]
810a0 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74 = 0;. mem.nTit
810b0 6c 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b 0a le = ROUND8(n);.
810c0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
810d0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 leave(mem.mutex)
810e0 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
810f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
81100 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 0a MemdebugSync(){.
81110 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
81120 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 6f kHdr *pHdr;. fo
81130 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 r(pHdr=mem.pFirs
81140 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 t; pHdr; pHdr=pH
81150 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 dr->pNext){.
81160 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 6f void **pBt = (vo
81170 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 70 id**)pHdr;. p
81180 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 Bt -= pHdr->nBac
81190 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20 ktraceSlots;.
811a0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 mem.xBacktrace(
811b0 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 64 pHdr->iSize, pHd
811c0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c r->nBacktrace-1,
811d0 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d &pBt[1]);. }.}
811e0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 ../*.** Open the
811f0 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 file indicated
81200 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 and write a log
81210 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d of all unfreed m
81220 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 emory .** alloca
81230 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 tions into that
81240 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 log..*/.SQLITE_P
81250 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
81260 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 28 te3MemdebugDump(
81270 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
81280 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a ename){. FILE *
81290 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65 out;. struct Me
812a0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b mBlockHdr *pHdr;
812b0 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 . void **pBt;.
812c0 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d 20 int i;. out =
812d0 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c fopen(zFilename,
812e0 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 74 "w");. if( out
812f0 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e ==0 ){. fprin
81300 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 tf(stderr, "** U
81310 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 nable to output
81320 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 memory debug out
81330 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e put log: %s **\n
81340 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
81350 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 zFilename
81360 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
81370 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 }. for(pHdr=me
81380 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 m.pFirst; pHdr;
81390 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 pHdr=pHdr->pNext
813a0 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d ){. char *z =
813b0 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 20 (char*)pHdr;.
813c0 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 z -= pHdr->nBa
813d0 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 7a cktraceSlots*siz
813e0 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 64 eof(void*) + pHd
813f0 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 66 r->nTitle;. f
81400 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a 2a printf(out, "***
81410 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 20 * %lld bytes at
81420 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c %p from %s ****\
81430 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 n", .
81440 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 70 pHdr->iSize, &p
81450 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 54 Hdr[1], pHdr->nT
81460 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22 itle ? z : "???"
81470 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 2d );. if( pHdr-
81480 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 >nBacktrace ){.
81490 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 29 fflush(out)
814a0 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 76 ;. pBt = (v
814b0 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 oid**)pHdr;.
814c0 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e pBt -= pHdr->n
814d0 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a BacktraceSlots;.
814e0 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 5f backtrace_
814f0 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c 20 symbols_fd(pBt,
81500 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
81510 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a , fileno(out));.
81520 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
81530 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a t, "\n");. }.
81540 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 }. fprintf(ou
81550 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b t, "COUNTS:\n");
81560 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 . for(i=0; i<NC
81570 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 SIZE-1; i++){.
81580 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 if( mem.nAlloc
81590 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 72 [i] ){. fpr
815a0 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 intf(out, " %5
815b0 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 d: %10d %10d %10
815c0 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 d\n", .
815d0 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c i*8, mem.nAll
815e0 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72 oc[i], mem.nCurr
815f0 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75 ent[i], mem.mxCu
81600 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d rrent[i]);. }
81610 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e 6e . }. if( mem.n
81620 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 20 Alloc[NCSIZE-1]
81630 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f ){. fprintf(o
81640 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30 ut, " %5d: %10
81650 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0a d %10d %10d\n",.
81660 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 53 NCS
81670 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c IZE*8-8, mem.nAl
81680 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20 loc[NCSIZE-1],.
81690 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e mem.
816a0 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d nCurrent[NCSIZE-
816b0 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 1], mem.mxCurren
816c0 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 20 t[NCSIZE-1]);.
816d0 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b }. fclose(out);
816e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
816f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
81700 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d 4d imes sqlite3MemM
81710 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 6e alloc() has been
81720 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 called..*/.SQLI
81730 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
81740 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d 61 qlite3MemdebugMa
81750 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 69 llocCount(){. i
81760 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f 74 nt i;. int nTot
81770 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d al = 0;. for(i=
81780 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b 0; i<NCSIZE; i++
81790 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d ){. nTotal +=
817a0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a mem.nAlloc[i];.
817b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 6f }. return nTo
817c0 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 tal;.}...#endif
817d0 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 /* SQLITE_MEMDEB
817e0 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a UG */../********
817f0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
81800 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m2.c ***********
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81830 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
81840 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
81850 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a e mem3.c *******
81860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81880 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
81890 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 7 October 14.**.
818a0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
818b0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
818c0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
818d0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
818e0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
818f0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
81900 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
81910 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
81920 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
81930 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
81940 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
81950 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
81960 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
81970 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
81980 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
81990 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
819a0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
819b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
819c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
81a00 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
81a10 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e s the C function
81a20 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
81a30 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c a memory.** all
81a40 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
81a50 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c m for use by SQL
81a60 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ite. .**.** This
81a70 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
81a80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
81a90 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 n subsystem omit
81aa0 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 s all.** use of
81ab0 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 51 malloc(). The SQ
81ac0 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c 69 Lite user suppli
81ad0 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 es a block of me
81ae0 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 mory.** before c
81af0 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 alling sqlite3_i
81b00 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d nitialize() from
81b10 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f which allocatio
81b20 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 ns.** are made a
81b30 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 nd returned by t
81b40 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 he xMalloc() and
81b50 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 xRealloc() .**
81b60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e implementations.
81b70 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e Once sqlite3_in
81b80 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 itialize() has b
81b90 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 een called,.** t
81ba0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d he amount of mem
81bb0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f ory available to
81bc0 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 SQLite is fixed
81bd0 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 and cannot.** b
81be0 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a e changed..**.**
81bf0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
81c00 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
81c10 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
81c20 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 is included.**
81c30 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c in the build onl
81c40 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 y if SQLITE_ENAB
81c50 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65 LE_MEMSYS3 is de
81c60 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 fined..**.** $Id
81c70 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 2e 32 35 20 : mem3.c,v 1.25
81c80 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 2008/11/19 16:52
81c90 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :44 danielk1977
81ca0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
81cb0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
81cc0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
81cd0 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75 69 ator is only bui
81ce0 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72 lt into the libr
81cf0 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e ary.** SQLITE_EN
81d00 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 ABLE_MEMSYS3 is
81d10 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69 6e defined. Definin
81d20 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64 6f g this symbol do
81d30 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74 es not.** mean t
81d40 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 hat the library
81d50 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f 72 will use a memor
81d60 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75 6c y-pool by defaul
81d70 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a 20 t, just that.**
81d80 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e it is available.
81d90 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c The mempool all
81da0 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76 61 ocator is activa
81db0 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a ted by calling.*
81dc0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 * sqlite3_config
81dd0 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ()..*/.#ifdef SQ
81de0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
81df0 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d YS3../*.** Maxim
81e00 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 um size (in Mem3
81e10 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 6d Blocks) of a "sm
81e20 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 all" chunk..*/.#
81e30 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20 define MX_SMALL
81e40 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 10.../*.** Numbe
81e50 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 61 r of freelist ha
81e60 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66 sh slots.*/.#def
81e70 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a 0a ine N_HASH 61..
81e80 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61 /*.** A memory a
81e90 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f 20 llocation (also
81ea0 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b 22 called a "chunk"
81eb0 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77 ) consists of tw
81ec0 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c o or .** more bl
81ed0 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 20 ocks where each
81ee0 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 73 block is 8 bytes
81ef0 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20 62 . The first 8 b
81f00 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 68 ytes are .** a h
81f10 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f eader that is no
81f20 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 t returned to th
81f30 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 e user..**.** A
81f40 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20 chunk is two or
81f50 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74 more blocks that
81f60 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b is either check
81f70 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 65 ed out or.** fre
81f80 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 6c e. The first bl
81f90 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 75 ock has format u
81fa0 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 7a .hdr. u.hdr.siz
81fb0 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 74 e4x is 4 times t
81fc0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 he.** size of th
81fd0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 e allocation in
81fe0 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 6c blocks if the al
81ff0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
82000 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e 73 ..** The u.hdr.s
82010 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 74 ize4x&1 bit is t
82020 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b rue if the chunk
82030 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20 is checked out
82040 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 and.** false if
82050 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 the chunk is on
82060 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 54 the freelist. T
82070 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 he u.hdr.size4x&
82080 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 2 bit.** is true
82090 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 if the previous
820a0 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
820b0 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 20 d out and false
820c0 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f if the.** previo
820d0 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 65 us chunk is free
820e0 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 65 . The u.hdr.pre
820f0 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 74 vSize field is t
82100 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 he size of.** th
82110 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b e previous chunk
82120 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 in blocks if th
82130 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b e previous chunk
82140 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 72 is on the.** fr
82150 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 70 eelist. If the p
82160 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 revious chunk is
82170 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 checked out, th
82180 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 76 en.** u.hdr.prev
82190 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 74 Size can be part
821a0 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 of the data for
821b0 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 20 that chunk and
821c0 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65 should.** not be
821d0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
821e0 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 6e ..**.** We often
821f0 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 6e identify a chun
82200 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 69 k by its index i
82210 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20 n mem3.aPool[].
82220 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 73 When.** this is
82230 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b done, the chunk
82240 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f index refers to
82250 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 the second bloc
82260 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 6e k of.** the chun
82270 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c k. In this way,
82280 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b the first chunk
82290 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
822a0 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 1..** A chunk i
822b0 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20 ndex of 0 means
822c0 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20 "no such chunk"
822d0 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 76 and is the equiv
822e0 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e 55 alent.** of a NU
822f0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
82300 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f * The second blo
82310 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b ck of free chunk
82320 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d s is of the form
82330 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a 2a u.list. The.**
82340 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 6d two fields form
82350 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64 a double-linked
82360 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 20 list of chunks
82370 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65 73 of related sizes
82380 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f ..** Pointers to
82390 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 the head of the
823a0 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65 64 list are stored
823b0 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c in mem3.aiSmall
823c0 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c [] .** for small
823d0 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d 65 er chunks and me
823e0 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72 20 m3.aiHash[] for
823f0 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a 2a larger chunks..*
82400 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 *.** The second
82410 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e 6b block of a chunk
82420 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69 66 is user data if
82430 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68 the chunk is ch
82440 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20 20 ecked .** out.
82450 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63 68 If a chunk is ch
82460 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20 75 ecked out, the u
82470 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78 74 ser data may ext
82480 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 end into.** the
82490 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 76 u.hdr.prevSize v
824a0 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c alue of the foll
824b0 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a owing chunk..*/.
824c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d typedef struct M
824d0 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f em3Block Mem3Blo
824e0 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33 42 ck;.struct Mem3B
824f0 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b lock {. union {
82500 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 . struct {.
82510 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a 65 u32 prevSize
82520 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 ; /* Size of p
82530 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e revious chunk in
82540 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 Mem3Block eleme
82550 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 nts */. u32
82560 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 20 size4x; /*
82570 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 63 4x the size of c
82580 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e 20 urrent chunk in
82590 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e Mem3Block elemen
825a0 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72 3b ts */. } hdr;
825b0 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 . struct {.
825c0 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20 20 u32 next;
825d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
825e0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 mem3.aPool[] of
825f0 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20 next free chunk
82600 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72 65 */. u32 pre
82610 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 v; /* Inde
82620 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x in mem3.aPool[
82630 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72 ] of previous fr
82640 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 ee chunk */.
82650 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d } list;. } u;.}
82660 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 ;../*.** All of
82670 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 the static varia
82680 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69 bles used by thi
82690 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c s module are col
826a0 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 lected.** into a
826b0 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 single structur
826c0 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e 20 e named "mem3".
826d0 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 This is to keep
826e0 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 the.** static v
826f0 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a ariables organiz
82700 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 ed and to reduce
82710 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 namespace pollu
82720 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 tion.** when thi
82730 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 s module is comb
82740 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 ined with other
82750 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 in the amalgamat
82760 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 ion..*/.static S
82770 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
82780 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20 20 Mem3Global {.
82790 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 /*. ** Memory a
827a0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c vailable for all
827b0 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69 ocation. nPool i
827c0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 s the size of th
827d0 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69 6e e array. ** (in
827e0 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f 69 Mem3Blocks) poi
827f0 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f 6c nted to by aPool
82800 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20 20 less 2.. */.
82810 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d u32 nPool;. Mem
82820 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a 3Block *aPool;..
82830 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20 69 /*. ** True i
82840 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74 f we are evaluat
82850 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 ing an out-of-me
82860 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 mory callback..
82870 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d 42 */. int alarmB
82880 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a usy;. . /*. *
82890 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 * Mutex to contr
828a0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
828b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
828c0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 on subsystem..
828d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
828e0 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20 20 ex *mutex;. .
828f0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e 69 /*. ** The mini
82900 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 mum amount of fr
82910 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77 65 ee space that we
82920 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a 2f have seen.. */
82930 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72 3b . u32 mnMaster;
82940 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73 .. /*. ** iMas
82950 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65 78 ter is the index
82960 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 of the master c
82970 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77 20 hunk. Most new
82980 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2a allocations. **
82990 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74 68 occur off of th
829a0 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61 73 is chunk. szMas
829b0 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65 20 ter is the size
829c0 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a (in Mem3Blocks).
829d0 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72 ** of the curr
829e0 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d 61 ent master. iMa
829f0 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68 65 ster is 0 if the
82a00 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65 72 re is not master
82a10 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 chunk.. ** The
82a20 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69 73 master chunk is
82a30 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20 74 not in either t
82a40 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20 61 he aiHash[] or a
82a50 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20 iSmall[].. */.
82a60 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20 20 u32 iMaster;.
82a70 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a 20 u32 szMaster;..
82a80 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20 6f /*. ** Array o
82a90 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 20 f lists of free
82aa0 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e 67 blocks according
82ab0 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 to the block si
82ac0 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d 61 ze . ** for sma
82ad0 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72 20 ller chunks, or
82ae0 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62 6c a hash on the bl
82af0 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61 72 ock size for lar
82b00 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e ger. ** chunks.
82b10 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53 6d . */. u32 aiSm
82b20 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b all[MX_SMALL-1];
82b30 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 /* For sizes
82b40 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d 41 2 through MX_SMA
82b50 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a 2f LL, inclusive */
82b60 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e 5f . u32 aiHash[N_
82b70 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a HASH]; /*
82b80 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53 4d For sizes MX_SM
82b90 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65 72 ALL+1 and larger
82ba0 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20 39 */.} mem3 = { 9
82bb0 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65 66 7535575 };..#def
82bc0 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c 28 ine mem3 GLOBAL(
82bd0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 struct Mem3Globa
82be0 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20 l, mem3)../*.**
82bf0 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b Unlink the chunk
82c00 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 at mem3.aPool[i
82c10 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 ] from list it i
82c20 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f s currently.** o
82c30 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 n. *pRoot is th
82c40 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69 73 e list that i is
82c50 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f a member of..*/
82c60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
82c70 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 sys3UnlinkFromLi
82c80 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 st(u32 i, u32 *p
82c90 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65 78 Root){. u32 nex
82ca0 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t = mem3.aPool[i
82cb0 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 ].u.list.next;.
82cc0 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d 33 u32 prev = mem3
82cd0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
82ce0 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28 .prev;. assert(
82cf0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
82d00 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
82d10 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d 30 );. if( prev==0
82d20 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20 3d ){. *pRoot =
82d30 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a next;. }else{.
82d40 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 mem3.aPool[p
82d50 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 rev].u.list.next
82d60 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 = next;. }. i
82d70 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20 6d f( next ){. m
82d80 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e em3.aPool[next].
82d90 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 72 u.list.prev = pr
82da0 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e 61 ev;. }. mem3.a
82db0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
82dc0 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e ext = 0;. mem3.
82dd0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
82de0 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a prev = 0;.}../*.
82df0 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 ** Unlink the ch
82e00 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 66 unk at index i f
82e10 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65 72 rom .** whatever
82e20 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 list is current
82e30 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a ly a member of..
82e40 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
82e50 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33 32 emsys3Unlink(u32
82e60 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c i){. u32 size,
82e70 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28 hash;. assert(
82e80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
82e90 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
82ea0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
82eb0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
82ec0 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d hdr.size4x & 1)=
82ed0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
82ee0 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d i>=1 );. size =
82ef0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
82f00 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b .u.hdr.size4x/4;
82f10 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d . assert( size=
82f20 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 =mem3.aPool[i+si
82f30 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 ze-1].u.hdr.prev
82f40 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Size );. assert
82f50 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 ( size>=2 );. i
82f60 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d f( size <= MX_SM
82f70 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 ALL ){. memsy
82f80 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 s3UnlinkFromList
82f90 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c (i, &mem3.aiSmal
82fa0 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65 l[size-2]);. }e
82fb0 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 lse{. hash =
82fc0 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20 size % N_HASH;.
82fd0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
82fe0 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d FromList(i, &mem
82ff0 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 3.aiHash[hash]);
83000 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 . }.}../*.** Li
83010 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 nk the chunk at
83020 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f mem3.aPool[i] so
83030 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 that is on the
83040 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 61 list rooted.** a
83050 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61 t *pRoot..*/.sta
83060 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
83070 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 32 LinkIntoList(u32
83080 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b i, u32 *pRoot){
83090 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
830a0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
830b0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6d m3.mutex) );. m
830c0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c em3.aPool[i].u.l
830d0 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f 6f ist.next = *pRoo
830e0 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b t;. mem3.aPool[
830f0 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d i].u.list.prev =
83100 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f 74 0;. if( *pRoot
83110 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ){. mem3.aPo
83120 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73 ol[*pRoot].u.lis
83130 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a t.prev = i;. }.
83140 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a *pRoot = i;.}.
83150 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 ./*.** Link the
83160 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 chunk at index i
83170 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68 65 into either the
83180 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 appropriate.**
83190 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73 74 small chunk list
831a0 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c 61 , or into the la
831b0 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74 rge chunk hash t
831c0 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 able..*/.static
831d0 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b void memsys3Link
831e0 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73 (u32 i){. u32 s
831f0 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 ize, hash;. ass
83200 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
83210 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
83220 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
83230 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 i>=1 );. asser
83240 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
83250 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
83260 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73 69 & 1)==0 );. si
83270 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
83280 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
83290 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 x/4;. assert( s
832a0 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ize==mem3.aPool[
832b0 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e i+size-1].u.hdr.
832c0 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 prevSize );. as
832d0 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b sert( size>=2 );
832e0 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d . if( size <= M
832f0 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d X_SMALL ){. m
83300 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 emsys3LinkIntoLi
83310 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d st(i, &mem3.aiSm
83320 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 all[size-2]);.
83330 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 }else{. hash
83340 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b = size % N_HASH;
83350 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b . memsys3Link
83360 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d IntoList(i, &mem
83370 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 3.aiHash[hash]);
83380 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
83390 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 the STATIC_MEM
833a0 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 mutex is not alr
833b0 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 eady held, obtai
833c0 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 n it now. The mu
833d0 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 tex.** will alre
833e0 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 ady be held (obt
833f0 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e ained by code in
83400 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a malloc.c) if.**
83410 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
83420 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 nfig.bMemStat is
83430 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 true..*/.static
83440 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e 74 void memsys3Ent
83450 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 er(void){. if(
83460 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
83470 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 fig.bMemstat==0
83480 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d 30 && mem3.mutex==0
83490 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75 74 ){. mem3.mut
834a0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
834b0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
834c0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b TEX_STATIC_MEM);
834d0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
834e0 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e utex_enter(mem3.
834f0 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 mutex);.}.static
83500 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 61 void memsys3Lea
83510 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 ve(void){. sqli
83520 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
83530 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a mem3.mutex);.}..
83540 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68 65 /*.** Called whe
83550 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 n we are unable
83560 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61 6c to satisfy an al
83570 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79 74 location of nByt
83580 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es..*/.static vo
83590 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d id memsys3OutOfM
835a0 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65 29 emory(int nByte)
835b0 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61 6c {. if( !mem3.al
835c0 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20 6d armBusy ){. m
835d0 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 em3.alarmBusy =
835e0 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 1;. assert( s
835f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
83600 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
83610 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
83620 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 ex_leave(mem3.mu
83630 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tex);. sqlite
83640 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
83650 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c (nByte);. sql
83660 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
83670 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem3.mutex);.
83680 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 mem3.alarmBusy
83690 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a = 0;. }.}.../*
836a0 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20 61 .** Chunk i is a
836b0 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61 74 free chunk that
836c0 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b has been unlink
836d0 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73 20 ed. Adjust its
836e0 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65 74 .** size paramet
836f0 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f 75 ers for check-ou
83700 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 t and return a p
83710 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a 2a ointer to the .*
83720 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20 6f * user portion o
83730 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a f the chunk..*/.
83740 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d static void *mem
83750 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33 32 sys3Checkout(u32
83760 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29 7b i, u32 nBlock){
83770 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73 65 . u32 x;. asse
83780 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
83790 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
837a0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
837b0 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 i>=1 );. assert
837c0 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 ( mem3.aPool[i-1
837d0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 ].u.hdr.size4x/4
837e0 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 ==nBlock );. as
837f0 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c sert( mem3.aPool
83800 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 [i+nBlock-1].u.h
83810 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42 6c dr.prevSize==nBl
83820 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65 6d ock );. x = mem
83830 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
83840 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65 6d dr.size4x;. mem
83850 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
83860 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f dr.size4x = nBlo
83870 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 29 ck*4 | 1 | (x&2)
83880 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
83890 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 +nBlock-1].u.hdr
838a0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f .prevSize = nBlo
838b0 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ck;. mem3.aPool
838c0 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 [i+nBlock-1].u.h
838d0 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a dr.size4x |= 2;.
838e0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e 61 return &mem3.a
838f0 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a Pool[i];.}../*.*
83900 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65 20 * Carve a piece
83910 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20 6f off of the end o
83920 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73 74 f the mem3.iMast
83930 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 2a er free chunk..*
83940 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
83950 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 6c er to the new al
83960 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20 69 location. Or, i
83970 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 f the master chu
83980 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 nk.** is not lar
83990 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75 72 ge enough, retur
839a0 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 n 0..*/.static v
839b0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f 6d oid *memsys3From
839c0 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f 63 Master(u32 nBloc
839d0 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 k){. assert( sq
839e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
839f0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
83a00 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 73 assert( mem3.s
83a10 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 zMaster>=nBlock
83a20 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 3e );. if( nBlock>
83a30 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 =mem3.szMaster-1
83a40 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 ){. /* Use t
83a50 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 he entire master
83a60 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 20 */. void *p
83a70 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 = memsys3Checkou
83a80 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20 t(mem3.iMaster,
83a90 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a mem3.szMaster);.
83aa0 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 mem3.iMaster
83ab0 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 = 0;. mem3.s
83ac0 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 zMaster = 0;.
83ad0 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d mem3.mnMaster =
83ae0 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 0;. return p
83af0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
83b00 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73 74 * Split the mast
83b10 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75 72 er block. Retur
83b20 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a 20 n the tail. */.
83b30 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b 0a u32 newi, x;.
83b40 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33 2e newi = mem3.
83b50 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e 73 iMaster + mem3.s
83b60 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63 6b zMaster - nBlock
83b70 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 65 ;. assert( ne
83b80 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 wi > mem3.iMaste
83b90 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33 2e r+1 );. mem3.
83ba0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
83bb0 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
83bc0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 -1].u.hdr.prevSi
83bd0 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 ze = nBlock;.
83be0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
83bf0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
83c00 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
83c10 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 20 size4x |= 2;.
83c20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 mem3.aPool[newi
83c30 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
83c40 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b = nBlock*4 + 1;
83c50 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 . mem3.szMast
83c60 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 er -= nBlock;.
83c70 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 mem3.aPool[new
83c80 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 i-1].u.hdr.prevS
83c90 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 ize = mem3.szMas
83ca0 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65 6d ter;. x = mem
83cb0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
83cc0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ster-1].u.hdr.si
83cd0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d 65 ze4x & 2;. me
83ce0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
83cf0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
83d00 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d ize4x = mem3.szM
83d10 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 aster*4 | x;.
83d20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 if( mem3.szMast
83d30 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 er < mem3.mnMast
83d40 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 er ){. mem3
83d50 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 .mnMaster = mem3
83d60 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d .szMaster;. }
83d70 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 . return (voi
83d80 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e d*)&mem3.aPool[n
83d90 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ewi];. }.}../*.
83da0 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 65 ** *pRoot is the
83db0 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74 20 head of a list
83dc0 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 6f of free chunks o
83dd0 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 0a f the same size.
83de0 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65 20 ** or same size
83df0 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72 20 hash. In other
83e00 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69 73 words, *pRoot is
83e10 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69 74 an entry in eit
83e20 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d her.** mem3.aiSm
83e30 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61 69 all[] or mem3.ai
83e40 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 Hash[]. .**.**
83e50 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 61 This routine exa
83e60 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69 65 mines all entrie
83e70 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 6c s on the given l
83e80 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a ist and tries.**
83e90 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61 63 to coalesce eac
83ea0 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 61 h entries with a
83eb0 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68 75 djacent free chu
83ec0 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 nks. .**.** If
83ed0 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b 20 it sees a chunk
83ee0 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 that is larger t
83ef0 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 han mem3.iMaster
83f00 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a 2a , it replaces .*
83f10 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 * the current me
83f20 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20 m3.iMaster with
83f30 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63 the new larger c
83f40 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20 hunk. In order
83f50 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 33 for.** this mem3
83f60 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65 .iMaster replace
83f70 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68 ment to work, th
83f80 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d e master chunk m
83f90 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64 ust be.** linked
83fa0 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 into the hash t
83fb0 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73 20 ables. That is
83fc0 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73 not the normal s
83fd0 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 69 tate of.** affai
83fe0 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20 rs, of course.
83ff0 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 The calling rout
84000 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 68 ine must link th
84010 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 6e e master.** chun
84020 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 6e k before invokin
84030 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 g this routine,
84040 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e 6b then must unlink
84050 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a 2a the (possibly.*
84060 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74 65 * changed) maste
84070 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 69 r chunk once thi
84080 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69 s routine has fi
84090 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 nished..*/.stati
840a0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d 65 c void memsys3Me
840b0 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 7b rge(u32 *pRoot){
840c0 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70 72 . u32 iNext, pr
840d0 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b 0a ev, size, i, x;.
840e0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
840f0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
84100 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 m3.mutex) );. f
84110 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30 or(i=*pRoot; i>0
84120 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20 ; i=iNext){.
84130 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f iNext = mem3.aPo
84140 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 ol[i].u.list.nex
84150 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 t;. size = me
84160 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
84170 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20 hdr.size4x;.
84180 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31 29 assert( (size&1)
84190 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 ==0 );. if( (
841a0 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20 20 size&2)==0 ){.
841b0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
841c0 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 6f kFromList(i, pRo
841d0 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ot);. asser
841e0 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f t( i > mem3.aPoo
841f0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 l[i-1].u.hdr.pre
84200 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 70 vSize );. p
84210 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e 61 rev = i - mem3.a
84220 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
84230 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 20 prevSize;.
84240 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74 20 if( prev==iNext
84250 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 ){. iNext
84260 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 = mem3.aPool[pr
84270 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b ev].u.list.next;
84280 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
84290 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72 65 emsys3Unlink(pre
842a0 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d v);. size =
842b0 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70 72 i + size/4 - pr
842c0 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 ev;. x = me
842d0 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d m3.aPool[prev-1]
842e0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 .u.hdr.size4x &
842f0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 2;. mem3.aP
84300 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64 ool[prev-1].u.hd
84310 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65 2a r.size4x = size*
84320 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 4 | x;. mem
84330 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a 3.aPool[prev+siz
84340 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 e-1].u.hdr.prevS
84350 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 ize = size;.
84360 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70 72 memsys3Link(pr
84370 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 70 ev);. i = p
84380 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a rev;. }else{.
84390 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34 3b size /= 4;
843a0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
843b0 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ize>mem3.szMaste
843c0 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e r ){. mem3.
843d0 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20 20 iMaster = i;.
843e0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
843f0 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 = size;. }.
84400 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
84410 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 rn a block of me
84420 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74 mory of at least
84430 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e nBytes in size.
84440 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 .** Return NULL
84450 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a if unable..**.**
84460 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
84470 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
84480 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65 necessary mutexe
84490 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a s, if any, are.*
844a0 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62 * already held b
844b0 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65 y the caller. He
844c0 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f nce "Unsafe"..*/
844d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
844e0 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys3MallocUnsaf
844f0 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 e(int nByte){.
84500 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42 6c u32 i;. u32 nBl
84510 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72 65 ock;. u32 toFre
84520 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 e;.. assert( sq
84530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
84540 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
84550 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
84560 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 (Mem3Block)==8 )
84570 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 31 ;. if( nByte<=1
84580 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 2 ){. nBlock
84590 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 2;. }else{.
845a0 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 nBlock = (nByt
845b0 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a 20 e + 11)/8;. }.
845c0 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b 3e assert( nBlock>
845d0 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45 50 =2 );.. /* STEP
845e0 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f 1:. ** Look fo
845f0 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 r an entry of th
84600 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 69 e correct size i
84610 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d 61 n either the sma
84620 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74 61 ll. ** chunk ta
84630 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c 61 ble or in the la
84640 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74 rge chunk hash t
84650 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20 able. This is.
84660 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6d ** successful m
84670 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65 20 ost of the time
84680 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20 6f (about 9 times o
84690 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f 0a ut of 10).. */.
846a0 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20 if( nBlock <=
846b0 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 MX_SMALL ){.
846c0 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c i = mem3.aiSmall
846d0 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20 [nBlock-2];.
846e0 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 if( i>0 ){.
846f0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
84700 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e omList(i, &mem3.
84710 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 aiSmall[nBlock-2
84720 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ]);. return
84730 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 memsys3Checkout
84740 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 (i, nBlock);.
84750 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
84760 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f 63 int hash = nBloc
84770 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20 k % N_HASH;.
84780 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61 73 for(i=mem3.aiHas
84790 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69 3d h[hash]; i>0; i=
847a0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
847b0 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 list.next){.
847c0 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c if( mem3.aPool
847d0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
847e0 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 4x/4==nBlock ){.
847f0 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 memsys3U
84800 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c nlinkFromList(i,
84810 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 &mem3.aiHash[ha
84820 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 65 sh]);. re
84830 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 turn memsys3Chec
84840 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b kout(i, nBlock);
84850 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
84860 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32 3a }.. /* STEP 2:
84870 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61 74 . ** Try to sat
84880 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 isfy the allocat
84890 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 ion by carving a
848a0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 piece off of th
848b0 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 e end. ** of th
848c0 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 e master chunk.
848d0 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61 6c This step usual
848e0 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65 70 ly works if step
848f0 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 1 fails.. */.
84900 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 if( mem3.szMast
84910 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 er>=nBlock ){.
84920 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 return memsys3
84930 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63 FromMaster(nBloc
84940 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53 k);. }... /* S
84950 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f TEP 3: . ** Lo
84960 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 op through the e
84970 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f 6f ntire memory poo
84980 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64 6a l. Coalesce adj
84990 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a 20 acent free. **
849a0 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70 75 chunks. Recompu
849b0 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 te the master ch
849c0 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67 65 unk as the large
849d0 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 20 st free chunk..
849e0 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67 61 ** Then try aga
849f0 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 in to satisfy th
84a00 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 e allocation by
84a10 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 carving a piece
84a20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 off. ** of the
84a30 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 end of the maste
84a40 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 r chunk. This s
84a50 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72 79 tep happens very
84a60 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 65 . ** rarely (we
84a70 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20 66 hope!). */. f
84a80 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b or(toFree=nBlock
84a90 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 6d *16; toFree<(mem
84aa0 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46 3.nPool*16); toF
84ab0 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20 6d ree *= 2){. m
84ac0 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 emsys3OutOfMemor
84ad0 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20 69 y(toFree);. i
84ae0 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 f( mem3.iMaster
84af0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 ){. memsys3
84b00 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 Link(mem3.iMaste
84b10 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 r);. mem3.i
84b20 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
84b30 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 mem3.szMaster
84b40 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 = 0;. }. f
84b50 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 or(i=0; i<N_HASH
84b60 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 ; i++){. me
84b70 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 msys3Merge(&mem3
84b80 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20 20 .aiHash[i]);.
84b90 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 }. for(i=0;
84ba0 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b i<MX_SMALL-1; i+
84bb0 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 +){. memsys
84bc0 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 53 3Merge(&mem3.aiS
84bd0 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a mall[i]);. }.
84be0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d if( mem3.szM
84bf0 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
84c00 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d emsys3Unlink(mem
84c10 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 3.iMaster);.
84c20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 if( mem3.szMas
84c30 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 ter>=nBlock ){.
84c40 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 return me
84c50 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 msys3FromMaster(
84c60 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d nBlock);. }
84c70 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
84c80 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 If none of the
84c90 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 68 above worked, th
84ca0 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a 20 en we fail. */.
84cb0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
84cc0 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73 .** Free an outs
84cd0 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 tanding memory a
84ce0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
84cf0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
84d00 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
84d10 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65 necessary mutexe
84d20 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a s, if any, are.*
84d30 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62 * already held b
84d40 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65 y the caller. He
84d50 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f nce "Unsafe"..*/
84d60 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65 .void memsys3Fre
84d70 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f eUnsafe(void *pO
84d80 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b ld){. Mem3Block
84d90 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b *p = (Mem3Block
84da0 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b *)pOld;. int i;
84db0 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a . u32 size, x;.
84dc0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
84dd0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
84de0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
84df0 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f sert( p>mem3.aPo
84e00 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50 ol && p<&mem3.aP
84e10 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 ool[mem3.nPool]
84e20 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d );. i = p - mem
84e30 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 3.aPool;. asser
84e40 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
84e50 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
84e60 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65 &1)==1 );. size
84e70 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
84e80 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
84e90 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73 4;. assert( i+s
84ea0 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b ize<=mem3.nPool+
84eb0 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 1 );. mem3.aPoo
84ec0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
84ed0 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d e4x &= ~1;. mem
84ee0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 3.aPool[i+size-1
84ef0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
84f00 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e = size;. mem3.
84f10 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e aPool[i+size-1].
84f20 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 u.hdr.size4x &=
84f30 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e ~2;. memsys3Lin
84f40 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 k(i);.. /* Try
84f50 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d 61 to expand the ma
84f60 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20 6e ster using the n
84f70 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e 6b ewly freed chunk
84f80 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69 */. if( mem3.i
84f90 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 68 Master ){. wh
84fa0 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c ile( (mem3.aPool
84fb0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d [mem3.iMaster-1]
84fc0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 .u.hdr.size4x&2)
84fd0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a ==0 ){. siz
84fe0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d e = mem3.aPool[m
84ff0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
85000 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 .hdr.prevSize;.
85010 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 mem3.iMaste
85020 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 r -= size;.
85030 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b mem3.szMaster +
85040 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 = size;. me
85050 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 msys3Unlink(mem3
85060 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 .iMaster);.
85070 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
85080 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
85090 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 u.hdr.size4x & 2
850a0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f ;. mem3.aPo
850b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
850c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
850d0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a = mem3.szMaster*
850e0 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 4 | x;. mem
850f0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
85100 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
85110 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 er-1].u.hdr.prev
85120 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 Size = mem3.szMa
85130 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ster;. }.
85140 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d x = mem3.aPool[m
85150 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
85160 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b .hdr.size4x & 2;
85170 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d . while( (mem
85180 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
85190 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
851a0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
851b0 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 4x&1)==0 ){.
851c0 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 memsys3Unlink(
851d0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
851e0 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 3.szMaster);.
851f0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
85200 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d += mem3.aPool[m
85210 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
85220 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
85230 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20 dr.size4x/4;.
85240 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
85250 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
85260 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d hdr.size4x = mem
85270 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3.szMaster*4 | x
85280 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f ;. mem3.aPo
85290 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b ol[mem3.iMaster+
852a0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d mem3.szMaster-1]
852b0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
852c0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
852d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
852e0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
852f0 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 ize of an outsta
85300 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e nding allocation
85310 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 , in bytes. The
85320 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 .** size returne
85330 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 d omits the 8-by
85340 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 te header overhe
85350 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a ad. This only.*
85360 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e * works for chun
85370 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 ks that are curr
85380 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 ently checked ou
85390 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
853a0 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f 69 memsys3Size(voi
853b0 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f d *p){. Mem3Blo
853c0 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66 ck *pBlock;. if
853d0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
853e0 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d 0;. pBlock = (M
853f0 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61 em3Block*)p;. a
85400 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d ssert( (pBlock[-
85410 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 1].u.hdr.size4x&
85420 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 1)!=0 );. retur
85430 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e n (pBlock[-1].u.
85440 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32 hdr.size4x&~3)*2
85450 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 - 4;.}../*.** R
85460 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 ound up a reques
85470 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 t size to the ne
85480 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 xt valid allocat
85490 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 ion size..*/.sta
854a0 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 52 tic int memsys3R
854b0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 oundup(int n){.
854c0 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20 if( n<=12 ){.
854d0 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d return 12;. }
854e0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
854f0 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34 ((n+11)&~7) - 4
85500 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
85510 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f llocate nBytes o
85520 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 f memory..*/.sta
85530 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 tic void *memsys
85540 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 3Malloc(int nByt
85550 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 es){. sqlite3_i
85560 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 nt64 *p;. asser
85570 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20 20 t( nBytes>0 );
85580 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f /* mallo
85590 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74 20 c.c filters out
855a0 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73 20 0 byte requests
855b0 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 */. memsys3Ente
855c0 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 r();. p = memsy
855d0 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e s3MallocUnsafe(n
855e0 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73 Bytes);. memsys
855f0 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 3Leave();. retu
85600 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a rn (void*)p; .}.
85610 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f ./*.** Free memo
85620 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 ry..*/.void mems
85630 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70 50 ys3Free(void *pP
85640 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28 rior){. assert(
85650 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65 6d pPrior );. mem
85660 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6d sys3Enter();. m
85670 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 emsys3FreeUnsafe
85680 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 (pPrior);. mems
85690 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f ys3Leave();.}../
856a0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
856b0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 size of an exist
856c0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
856d0 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d ation.*/.void *m
856e0 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76 6f emsys3Realloc(vo
856f0 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 id *pPrior, int
85700 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e nBytes){. int n
85710 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a Old;. void *p;.
85720 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 if( pPrior==0
85730 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 ){. return sq
85740 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 lite3_malloc(nBy
85750 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 tes);. }. if(
85760 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 nBytes<=0 ){.
85770 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 sqlite3_free(pP
85780 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72 rior);. retur
85790 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 n 0;. }. nOld
857a0 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70 50 = memsys3Size(pP
857b0 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 rior);. if( nBy
857c0 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42 79 tes<=nOld && nBy
857d0 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b tes>=nOld-128 ){
857e0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69 . return pPri
857f0 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 or;. }. memsys
85800 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 3Enter();. p =
85810 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 memsys3MallocUns
85820 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 afe(nBytes);. i
85830 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 f( p ){. if(
85840 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a 20 nOld<nBytes ){.
85850 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 memcpy(p, p
85860 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
85870 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d }else{. m
85880 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c emcpy(p, pPrior,
85890 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d 0a nBytes);. }.
858a0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 memsys3FreeU
858b0 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 nsafe(pPrior);.
858c0 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 }. memsys3Leav
858d0 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b e();. return p;
858e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
858f0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
85900 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
85910 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69 64 memsys3Init(void
85920 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
85930 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
85940 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 21 otUsed);. if( !
85950 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
85960 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20 20 fig.pHeap ){.
85970 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
85980 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 RROR;. }.. /*
85990 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 Store a pointer
859a0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 6c to the memory bl
859b0 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 74 ock in global st
859c0 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a 2f ructure mem3. */
859d0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f . assert( sizeo
859e0 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 f(Mem3Block)==8
859f0 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20 );. mem3.aPool
85a00 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 73 = (Mem3Block *)s
85a10 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
85a20 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d 33 ig.pHeap;. mem3
85a30 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74 65 .nPool = (sqlite
85a40 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 3GlobalConfig.nH
85a50 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65 6d eap / sizeof(Mem
85a60 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a 20 3Block)) - 2;..
85a70 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
85a80 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e he master block.
85a90 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 */. mem3.szMas
85aa0 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c ter = mem3.nPool
85ab0 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 ;. mem3.mnMaste
85ac0 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 r = mem3.szMaste
85ad0 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 r;. mem3.iMaste
85ae0 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 r = 1;. mem3.aP
85af0 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 7a ool[0].u.hdr.siz
85b00 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61 e4x = (mem3.szMa
85b10 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20 20 ster<<2) + 2;.
85b20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
85b30 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72 65 nPool].u.hdr.pre
85b40 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f vSize = mem3.nPo
85b50 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ol;. mem3.aPool
85b60 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 [mem3.nPool].u.h
85b70 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a 0a dr.size4x = 1;..
85b80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
85b90 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
85ba0 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d nitialize this m
85bb0 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 odule..*/.static
85bc0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68 75 void memsys3Shu
85bd0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
85be0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
85bf0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
85c00 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a );. return;.}..
85c10 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 ../*.** Open the
85c20 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 file indicated
85c30 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 and write a log
85c40 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d of all unfreed m
85c50 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 emory .** alloca
85c60 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 tions into that
85c70 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 log..*/.SQLITE_P
85c80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
85c90 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 63 te3Memsys3Dump(c
85ca0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
85cb0 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 name){.#ifdef SQ
85cc0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c LITE_DEBUG. FIL
85cd0 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c E *out;. u32 i,
85ce0 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a j;. u32 size;.
85cf0 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d if( zFilename=
85d00 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b =0 || zFilename[
85d10 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 0]==0 ){. out
85d20 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c = stdout;. }el
85d30 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f se{. out = fo
85d40 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 pen(zFilename, "
85d50 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 w");. if( out
85d60 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72 ==0 ){. fpr
85d70 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a intf(stderr, "**
85d80 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 Unable to outpu
85d90 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f t memory debug o
85da0 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a utput log: %s **
85db0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
85dc0 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 zFile
85dd0 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 name);. ret
85de0 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 urn;. }. }.
85df0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
85e00 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
85e10 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20 "CHUNKS:\n");.
85e20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33 for(i=1; i<=mem3
85e30 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f .nPool; i+=size/
85e40 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 4){. size = m
85e50 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
85e60 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 .hdr.size4x;.
85e70 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29 if( size/4<=1 )
85e80 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
85e90 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20 65 72 out, "%p size er
85ea0 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 ror\n", &mem3.aP
85eb0 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 ool[i]);. a
85ec0 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 ssert( 0 );.
85ed0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
85ee0 20 20 20 69 66 28 20 28 73 69 7a 65 26 31 29 3d if( (size&1)=
85ef0 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c =0 && mem3.aPool
85f00 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 [i+size/4-1].u.h
85f10 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73 69 7a dr.prevSize!=siz
85f20 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66 70 72 e/4 ){. fpr
85f30 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 intf(out, "%p ta
85f40 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74 il size does not
85f50 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33 match\n", &mem3
85f60 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 .aPool[i]);.
85f70 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 assert( 0 );.
85f80 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
85f90 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 6d 33 }. if( ((mem3
85fa0 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d .aPool[i+size/4-
85fb0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 1].u.hdr.size4x&
85fc0 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 29 2)>>1)!=(size&1)
85fd0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ){. fprint
85fe0 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20 f(out, "%p tail
85ff0 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 73 20 checkout bit is
86000 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d incorrect\n", &m
86010 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 em3.aPool[i]);.
86020 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 assert( 0 )
86030 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
86040 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a }. if( siz
86050 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 e&1 ){. fpr
86060 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 intf(out, "%p %6
86070 64 20 62 79 74 65 73 20 63 68 65 63 6b 65 64 20 d bytes checked
86080 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 out\n", &mem3.aP
86090 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 ool[i], (size/4)
860a0 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 *8-8);. }else
860b0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
860c0 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 out, "%p %6d byt
860d0 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d es free%s\n", &m
860e0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 em3.aPool[i], (s
860f0 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20 ize/4)*8-8,.
86100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 3d i=
86110 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20 =mem3.iMaster ?
86120 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20 " **master**" :
86130 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 "");. }. }.
86140 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 for(i=0; i<MX_S
86150 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 MALL-1; i++){.
86160 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61 if( mem3.aiSma
86170 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 ll[i]==0 ) conti
86180 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 nue;. fprintf
86190 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64 (out, "small(%2d
861a0 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 ):", i);. for
861b0 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c (j = mem3.aiSmal
861c0 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d l[i]; j>0; j=mem
861d0 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 3.aPool[j].u.lis
861e0 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 t.next){. f
861f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 printf(out, " %p
86200 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f (%d)", &mem3.aPo
86210 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 ol[j],.
86220 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c (mem3.aPool
86230 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [j-1].u.hdr.size
86240 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 4x/4)*8-8);.
86250 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 }. fprintf(ou
86260 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 t, "\n"); . }.
86270 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 for(i=0; i<N_HA
86280 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 SH; i++){. if
86290 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d ( mem3.aiHash[i]
862a0 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a ==0 ) continue;.
862b0 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
862c0 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c 20 69 "hash(%2d):", i
862d0 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d );. for(j = m
862e0 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a em3.aiHash[i]; j
862f0 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; j=mem3.aPool
86300 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [j].u.list.next)
86310 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
86320 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 out, " %p(%d)",
86330 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a &mem3.aPool[j],.
86340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d (m
86350 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 em3.aPool[j-1].u
86360 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 .hdr.size4x/4)*8
86370 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 -8);. }. f
86380 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 printf(out, "\n"
86390 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 ); . }. fprint
863a0 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d 25 f(out, "master=%
863b0 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74 d\n", mem3.iMast
863c0 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f er);. fprintf(o
863d0 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c ut, "nowUsed=%d\
863e0 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 n", mem3.nPool*8
863f0 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 - mem3.szMaster
86400 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f *8);. fprintf(o
86410 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e ut, "mxUsed=%d\n
86420 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 ", mem3.nPool*8
86430 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a - mem3.mnMaster*
86440 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 8);. sqlite3_mu
86450 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d tex_leave(mem3.m
86460 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 75 74 utex);. if( out
86470 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 ==stdout ){.
86480 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a fflush(stdout);.
86490 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c }else{. fcl
864a0 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 ose(out);. }.#e
864b0 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 lse. UNUSED_PAR
864c0 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 AMETER(zFilename
864d0 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a );.#endif.}../*.
864e0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
864f0 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 is the only rout
86500 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ine in this file
86510 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a with external .
86520 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a ** linkage..**.*
86530 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c * Populate the l
86540 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
86550 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
86560 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a ion pointers in.
86570 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
86580 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f Config.m with po
86590 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f inters to the ro
865a0 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 utines in this f
865b0 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 ile. The.** argu
865c0 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20 74 68 ments specify th
865d0 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 e block of memor
865e0 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a y to manage..**.
865f0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
86600 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 is only called b
86610 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 y sqlite3_config
86620 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72 (), and therefor
86630 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 e.** is not requ
86640 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 ired to be threa
86650 64 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74 dsafe (it is not
86660 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
86670 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 VATE const sqlit
86680 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a e3_mem_methods *
86690 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d sqlite3MemGetMem
866a0 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20 73 74 sys3(void){. st
866b0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
866c0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d e3_mem_methods m
866d0 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20 empoolMethods =
866e0 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 61 {. memsys3Ma
866f0 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 lloc,. memsy
86700 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d s3Free,. mem
86710 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 sys3Realloc,.
86720 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20 memsys3Size,.
86730 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 memsys3Round
86740 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 up,. memsys3
86750 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 Init,. memsy
86760 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 s3Shutdown,.
86770 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 0. };. return
86780 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 &mempoolMethods
86790 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
867a0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
867b0 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a SYS3 */../******
867c0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
867d0 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem3.c *********
867e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
867f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86800 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
86810 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
86820 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a ile mem5.c *****
86830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86850 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
86860 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 007 October 14.*
86870 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
86880 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
86890 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
868a0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
868b0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
868c0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
868d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
868e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
868f0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
86900 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
86910 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
86920 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
86930 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
86940 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
86950 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
86960 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
86970 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
86980 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
86990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
869a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
869b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
869c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
869d0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
869e0 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
869f0 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
86a00 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 nt a memory.** a
86a10 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
86a20 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 tem for use by S
86a30 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 QLite. .**.** Th
86a40 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
86a50 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
86a60 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d ion subsystem om
86a70 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f its all.** use o
86a80 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 f malloc(). The
86a90 53 51 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 SQLite user supp
86aa0 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 lies a block of
86ab0 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 memory.** before
86ac0 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 calling sqlite3
86ad0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 _initialize() fr
86ae0 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 om which allocat
86af0 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 ions.** are made
86b00 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 and returned by
86b10 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 the xMalloc() a
86b20 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a nd xRealloc() .*
86b30 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
86b40 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f s. Once sqlite3_
86b50 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 initialize() has
86b60 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a been called,.**
86b70 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d the amount of m
86b80 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 emory available
86b90 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 to SQLite is fix
86ba0 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a ed and cannot.**
86bb0 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a be changed..**.
86bc0 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
86bd0 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
86be0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
86bf0 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a em is included.*
86c00 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f * in the build o
86c10 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e nly if SQLITE_EN
86c20 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 ABLE_MEMSYS5 is
86c30 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 defined..**.** $
86c40 49 64 3a 20 6d 65 6d 35 2e 63 2c 76 20 31 2e 31 Id: mem5.c,v 1.1
86c50 39 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 9 2008/11/19 16:
86c60 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 52:44 danielk197
86c70 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 7 Exp $.*/../*.*
86c80 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
86c90 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
86ca0 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20 6f ocator is used o
86cb0 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c nly when .** SQL
86cc0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
86cd0 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a S5 is defined..*
86ce0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
86cf0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a ENABLE_MEMSYS5..
86d00 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 /*.** A minimum
86d10 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e allocation is an
86d20 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
86d30 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
86d40 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 ture..** Larger
86d50 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 allocations are
86d60 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68 65 73 an array of thes
86d70 65 20 73 74 72 75 63 74 75 72 65 73 20 77 68 65 e structures whe
86d80 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f re the.** size o
86d90 66 20 74 68 65 20 61 72 72 61 79 20 69 73 20 61 f the array is a
86da0 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2f 0a power of 2..*/.
86db0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d typedef struct M
86dc0 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b em5Link Mem5Link
86dd0 3b 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e ;.struct Mem5Lin
86de0 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 k {. int next;
86df0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
86e00 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e f next free chun
86e10 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b k */. int prev;
86e20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
86e30 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 of previous free
86e40 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a chunk */.};../*
86e50 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 .** Maximum size
86e60 20 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 of any allocati
86e70 6f 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 on is ((1<<LOGMA
86e80 58 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 2e 20 X)*mem5.nAtom).
86e90 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 6e 41 Since.** mem5.nA
86ea0 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 74 tom is always at
86eb0 20 6c 65 61 73 74 20 38 2c 20 74 68 69 73 20 69 least 8, this i
86ec0 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 61 20 70 s not really a p
86ed0 72 61 63 74 69 63 61 6c 0a 2a 2a 20 6c 69 6d 69 ractical.** limi
86ee0 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 tation..*/.#defi
86ef0 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a ne LOGMAX 30../*
86f00 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20 66 .** Masks used f
86f10 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 or mem5.aCtrl[]
86f20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 elements..*/.#de
86f30 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a fine CTRL_LOGSIZ
86f40 45 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f E 0x1f /* Lo
86f50 67 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 g2 Size of this
86f60 62 6c 6f 63 6b 20 72 65 6c 61 74 69 76 65 20 74 block relative t
86f70 6f 20 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 23 64 o POW2_MIN */.#d
86f80 65 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 efine CTRL_FREE
86f90 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 0x20 /* T
86fa0 72 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b rue if not check
86fb0 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a ed out */../*.**
86fc0 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 All of the stat
86fd0 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 ic variables use
86fe0 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 d by this module
86ff0 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a are collected.*
87000 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 * into a single
87010 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 structure named
87020 22 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 "mem5". This is
87030 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 to keep the.**
87040 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 static variables
87050 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 organized and t
87060 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 o reduce namespa
87070 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 ce pollution.**
87080 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 when this module
87090 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 is combined wit
870a0 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 h other in the a
870b0 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a malgamation..*/.
870c0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
870d0 44 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f D struct Mem5Glo
870e0 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 bal {. /*. **
870f0 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 Memory available
87100 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a for allocation.
87110 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 74 6f 6d */. int nAtom
87120 3b 20 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c ; /* Small
87130 65 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c est possible all
87140 6f 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 ocation in bytes
87150 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b */. int nBlock
87160 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ; /* Number
87170 20 6f 66 20 6e 41 74 6f 6d 20 73 69 7a 65 64 20 of nAtom sized
87180 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 blocks in zPool
87190 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 0a */. u8 *zPool;.
871a0 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 . /*. ** Mut
871b0 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 ex to control ac
871c0 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f cess to the memo
871d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
871e0 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 bsystem.. */.
871f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
87200 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a utex;.. /*. **
87210 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 Performance sta
87220 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75 tistics. */. u
87230 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 64 nAlloc;
87240 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 /* Total numb
87250 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d er of calls to m
87260 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74 alloc */. u64 t
87270 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f otalAlloc; /
87280 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d * Total of all m
87290 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e alloc calls - in
872a0 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61 6c 20 cludes internal
872b0 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f frag */. u64 to
872c0 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20 2f 2a talExcess; /*
872d0 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20 Total internal
872e0 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f fragmentation */
872f0 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 4f 75 . u32 currentOu
87300 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e t; /* Curren
87310 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c t checkout, incl
87320 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66 uding internal f
87330 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a ragmentation */.
87340 20 20 75 33 32 20 63 75 72 72 65 6e 74 43 6f 75 u32 currentCou
87350 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 nt; /* Current
87360 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 number of disti
87370 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f nct checkouts */
87380 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b 20 20 . u32 maxOut;
87390 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
873a0 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 m instantaneous
873b0 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 currentOut */.
873c0 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20 u32 maxCount;
873d0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 /* Maximum i
873e0 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 nstantaneous cur
873f0 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 rentCount */. u
87400 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b 20 20 32 maxRequest;
87410 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c /* Largest al
87420 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c 75 73 location (exclus
87430 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20 ive of internal
87440 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a frag) */. . /*
87450 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 . ** Lists of f
87460 72 65 65 20 62 6c 6f 63 6b 73 20 6f 66 20 76 61 ree blocks of va
87470 72 69 6f 75 73 20 73 69 7a 65 73 2e 0a 20 20 2a rious sizes.. *
87480 2f 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 /. int aiFreeli
87490 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 st[LOGMAX+1];..
874a0 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 /*. ** Space f
874b0 6f 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 or tracking whic
874c0 68 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 h blocks are che
874d0 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 cked out and the
874e0 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 size. ** of ea
874f0 63 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 ch block. One b
87500 79 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 yte per block..
87510 20 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b */. u8 *aCtrl;
87520 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 31 39 38 ..} mem5 = { 198
87530 30 34 31 36 37 20 7d 3b 0a 0a 23 64 65 66 69 6e 04167 };..#defin
87540 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 e mem5 GLOBAL(st
87550 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c ruct Mem5Global,
87560 20 6d 65 6d 35 29 0a 0a 23 64 65 66 69 6e 65 20 mem5)..#define
87570 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 29 20 28 28 MEM5LINK(idx) ((
87580 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d Mem5Link *)(&mem
87590 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 5.zPool[(idx)*me
875a0 6d 35 2e 6e 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a m5.nAtom]))../*.
875b0 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 ** Unlink the ch
875c0 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f unk at mem5.aPoo
875d0 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 l[i] from list i
875e0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a t is currently.*
875f0 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 * on. It should
87600 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d be found on mem
87610 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
87620 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 gsize]..*/.stati
87630 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e c void memsys5Un
87640 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 link(int i, int
87650 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 iLogsize){. int
87660 20 6e 65 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 next, prev;. a
87670 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
87680 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a <mem5.nBlock );.
87690 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 assert( iLogsi
876a0 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a ze>=0 && iLogsiz
876b0 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 e<=LOGMAX );. a
876c0 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 ssert( (mem5.aCt
876d0 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 rl[i] & CTRL_LOG
876e0 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 SIZE)==iLogsize
876f0 29 3b 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d );.. next = MEM
87700 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 5LINK(i)->next;.
87710 20 20 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e prev = MEM5LIN
87720 4b 28 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 K(i)->prev;. if
87730 28 20 70 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 ( prev<0 ){.
87740 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
87750 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 iLogsize] = next
87760 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d ;. }else{. M
87770 45 4d 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e EM5LINK(prev)->n
87780 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a ext = next;. }.
87790 20 20 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b if( next>=0 ){
877a0 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 . MEM5LINK(ne
877b0 78 74 29 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 xt)->prev = prev
877c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c ;. }.}../*.** L
877d0 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 ink the chunk at
877e0 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 mem5.aPool[i] s
877f0 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 o that is on the
87800 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 iLogsize.** fre
87810 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 e list..*/.stati
87820 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 c void memsys5Li
87830 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c nk(int i, int iL
87840 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 ogsize){. int x
87850 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
87860 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
87870 65 6d 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em5.mutex) );.
87880 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
87890 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b i<mem5.nBlock );
878a0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 . assert( iLogs
878b0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 ize>=0 && iLogsi
878c0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 ze<=LOGMAX );.
878d0 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 assert( (mem5.aC
878e0 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f trl[i] & CTRL_LO
878f0 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 GSIZE)==iLogsize
87900 20 29 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c );.. x = MEM5L
87910 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d INK(i)->next = m
87920 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 em5.aiFreelist[i
87930 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 Logsize];. MEM5
87940 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 LINK(i)->prev =
87950 2d 31 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 -1;. if( x>=0 )
87960 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 78 3c {. assert( x<
87970 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 mem5.nBlock );.
87980 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e MEM5LINK(x)->
87990 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 prev = i;. }.
879a0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
879b0 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d iLogsize] = i;.}
879c0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 ../*.** If the S
879d0 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 TATIC_MEM mutex
879e0 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 is not already h
879f0 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e eld, obtain it n
87a00 6f 77 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a ow. The mutex.**
87a10 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 will already be
87a20 20 68 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 held (obtained
87a30 62 79 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f by code in mallo
87a40 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 c.c) if.** sqlit
87a50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
87a60 4d 65 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e MemStat is true.
87a70 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
87a80 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 76 6f 69 memsys5Enter(voi
87a90 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 d){. if( sqlite
87aa0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 3GlobalConfig.bM
87ab0 65 6d 73 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d emstat==0 && mem
87ac0 35 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 5.mutex==0 ){.
87ad0 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 mem5.mutex = s
87ae0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
87af0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
87b00 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 ATIC_MEM);. }.
87b10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
87b20 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29 nter(mem5.mutex)
87b30 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
87b40 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76 6f 69 memsys5Leave(voi
87b50 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 d){. sqlite3_mu
87b60 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d tex_leave(mem5.m
87b70 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 utex);.}../*.**
87b80 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
87b90 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e of an outstandin
87ba0 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e g allocation, in
87bb0 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 bytes. The.**
87bc0 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d size returned om
87bd0 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68 its the 8-byte h
87be0 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20 eader overhead.
87bf0 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f This only.** wo
87c00 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 rks for chunks t
87c10 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c hat are currentl
87c20 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a y checked out..*
87c30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d /.static int mem
87c40 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 sys5Size(void *p
87c50 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d ){. int iSize =
87c60 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 0;. if( p ){.
87c70 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75 38 20 int i = ((u8
87c80 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f *)p-mem5.zPool)/
87c90 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 20 20 mem5.nAtom;.
87ca0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
87cb0 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b i<mem5.nBlock );
87cc0 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d . iSize = mem
87cd0 35 2e 6e 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 5.nAtom * (1 <<
87ce0 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43 (mem5.aCtrl[i]&C
87cf0 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 TRL_LOGSIZE));.
87d00 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69 7a }. return iSiz
87d10 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 e;.}../*.** Find
87d20 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
87d30 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
87d40 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69 iLogsize. Unli
87d50 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79 nk that.** entry
87d60 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73 20 and return its
87d70 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 index. .*/.stati
87d80 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c c int memsys5Unl
87d90 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c 6f inkFirst(int iLo
87da0 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b gsize){. int i;
87db0 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a . int iFirst;..
87dc0 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 assert( iLogsi
87dd0 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a ze>=0 && iLogsiz
87de0 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69 e<=LOGMAX );. i
87df0 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d 35 = iFirst = mem5
87e00 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 .aiFreelist[iLog
87e10 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 size];. assert(
87e20 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20 iFirst>=0 );.
87e30 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 while( i>0 ){.
87e40 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20 29 if( i<iFirst )
87e50 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 20 iFirst = i;.
87e60 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 i = MEM5LINK(i)
87e70 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 ->next;. }. me
87e80 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72 msys5Unlink(iFir
87e90 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 st, iLogsize);.
87ea0 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b 0a return iFirst;.
87eb0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
87ec0 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 a block of memor
87ed0 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 y of at least nB
87ee0 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a ytes in size..**
87ef0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
87f00 75 6e 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 unable..*/.stati
87f10 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d c void *memsys5M
87f20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 allocUnsafe(int
87f30 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 69 3b nByte){. int i;
87f40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
87f50 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e 61 50 dex of a mem5.aP
87f60 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20 20 ool[] slot */.
87f70 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 20 20 int iBin;
87f80 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 6d /* Index into m
87f90 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 5d em5.aiFreelist[]
87fa0 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 */. int iFullS
87fb0 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f z; /* Size o
87fc0 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 f allocation rou
87fd0 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 65 72 nded up to power
87fe0 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74 20 69 of 2 */. int i
87ff0 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a 20 4c Logsize; /* L
88000 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a 2f 50 og2 of iFullSz/P
88010 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f 2a OW2_MIN */.. /*
88020 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 74 Keep track of t
88030 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 he maximum alloc
88040 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 ation request.
88050 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 Even unfulfilled
88060 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20 61 . ** requests a
88070 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20 re counted */.
88080 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e 6d if( (u32)nByte>m
88090 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 29 em5.maxRequest )
880a0 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 65 {. mem5.maxRe
880b0 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a 20 quest = nByte;.
880c0 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20 6e }.. /* Round n
880d0 42 79 74 65 20 75 70 20 74 6f 20 74 68 65 20 6e Byte up to the n
880e0 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65 72 20 ext valid power
880f0 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72 28 of two */. for(
88100 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41 74 iFullSz=mem5.nAt
88110 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20 om, iLogsize=0;
88120 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69 iFullSz<nByte; i
88130 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f FullSz *= 2, iLo
88140 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a gsize++){}.. /*
88150 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e Make sure mem5.
88160 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 aiFreelist[iLogs
88170 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74 ize] contains at
88180 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 0a least one free.
88190 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 ** block. If
881a0 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74 20 not, then split
881b0 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e a block of the n
881c0 65 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65 72 ext larger power
881d0 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20 of. ** two in
881e0 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 order to create
881f0 61 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b a new free block
88200 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a of size iLogsiz
88210 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42 e.. */. for(iB
88220 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d in=iLogsize; mem
88230 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42 69 5.aiFreelist[iBi
88240 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f n]<0 && iBin<=LO
88250 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a GMAX; iBin++){}.
88260 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41 if( iBin>LOGMA
88270 58 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 X ) return 0;.
88280 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e i = memsys5Unlin
88290 6b 46 69 72 73 74 28 69 42 69 6e 29 3b 0a 20 20 kFirst(iBin);.
882a0 77 68 69 6c 65 28 20 69 42 69 6e 3e 69 4c 6f 67 while( iBin>iLog
882b0 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 size ){. int
882c0 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20 20 69 42 newSize;.. iB
882d0 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 53 69 7a in--;. newSiz
882e0 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e 3b 0a 20 e = 1 << iBin;.
882f0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 2b mem5.aCtrl[i+
88300 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54 52 4c 5f newSize] = CTRL_
88310 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a 20 20 20 FREE | iBin;.
88320 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 2b 6e memsys5Link(i+n
88330 65 77 53 69 7a 65 2c 20 69 42 69 6e 29 3b 0a 20 ewSize, iBin);.
88340 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b }. mem5.aCtrl[
88350 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a 0a i] = iLogsize;..
88360 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6c 6c 6f /* Update allo
88370 63 61 74 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 cator performanc
88380 65 20 73 74 61 74 69 73 74 69 63 73 2e 20 2a 2f e statistics. */
88390 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b . mem5.nAlloc++
883a0 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c ;. mem5.totalAl
883b0 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a loc += iFullSz;.
883c0 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 mem5.totalExce
883d0 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d 20 ss += iFullSz -
883e0 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 63 75 nByte;. mem5.cu
883f0 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20 20 rrentCount++;.
88400 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 mem5.currentOut
88410 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66 += iFullSz;. if
88420 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 3c ( mem5.maxCount<
88430 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e mem5.currentCoun
88440 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e t ) mem5.maxCoun
88450 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 t = mem5.current
88460 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20 6d 65 6d Count;. if( mem
88470 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 2e 63 75 5.maxOut<mem5.cu
88480 72 72 65 6e 74 4f 75 74 20 29 20 6d 65 6d 35 2e rrentOut ) mem5.
88490 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35 2e 63 75 maxOut = mem5.cu
884a0 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a 20 rrentOut;.. /*
884b0 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
884c0 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 to the allocate
884d0 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 72 d memory. */. r
884e0 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 eturn (void*)&me
884f0 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e m5.zPool[i*mem5.
88500 6e 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a nAtom];.}../*.**
88510 20 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e Free an outstan
88520 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ding memory allo
88530 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 cation..*/.stati
88540 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 c void memsys5Fr
88550 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 eeUnsafe(void *p
88560 4f 6c 64 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 Old){. u32 size
88570 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69 6e , iLogsize;. in
88580 74 20 69 42 6c 6f 63 6b 3b 20 20 20 20 20 20 20 t iBlock;
88590 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20 53 65 74 .. /* Set
885a0 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 iBlock to the i
885b0 6e 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f 63 ndex of the bloc
885c0 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 k pointed to by
885d0 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68 pOld in . ** th
885e0 65 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e e array of mem5.
885f0 6e 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b nAtom byte block
88600 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 s pointed to by
88610 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f mem5.zPool.. */
88620 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38 . iBlock = ((u8
88630 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f *)pOld-mem5.zPo
88640 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a ol)/mem5.nAtom;.
88650 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 . /* Check that
88660 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f 6c the pointer pOl
88670 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 d points to a va
88680 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 6c lid, non-free bl
88690 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ock. */. assert
886a0 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 69 ( iBlock>=0 && i
886b0 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 Block<mem5.nBloc
886c0 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 k );. assert( (
886d0 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e (u8 *)pOld-mem5.
886e0 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 6e 41 74 6f zPool)%mem5.nAto
886f0 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 m==0 );. assert
88700 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 ( (mem5.aCtrl[iB
88710 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 52 45 lock] & CTRL_FRE
88720 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c 6f 67 E)==0 );.. iLog
88730 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 74 72 size = mem5.aCtr
88740 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c l[iBlock] & CTRL
88750 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 7a 65 _LOGSIZE;. size
88760 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 3b 0a = 1<<iLogsize;.
88770 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b assert( iBlock
88780 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d 65 6d +size-1<(u32)mem
88790 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 6d 5.nBlock );.. m
887a0 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b em5.aCtrl[iBlock
887b0 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a ] |= CTRL_FREE;.
887c0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c mem5.aCtrl[iBl
887d0 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d 20 43 ock+size-1] |= C
887e0 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 73 65 TRL_FREE;. asse
887f0 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 rt( mem5.current
88800 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 73 73 Count>0 );. ass
88810 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e ert( mem5.curren
88820 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 6d 35 tOut>=(size*mem5
88830 2e 6e 41 74 6f 6d 29 20 29 3b 0a 20 20 6d 65 6d .nAtom) );. mem
88840 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2d 2d 5.currentCount--
88850 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 ;. mem5.current
88860 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d 65 6d 35 Out -= size*mem5
88870 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73 73 65 72 74 .nAtom;. assert
88880 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 ( mem5.currentOu
88890 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 t>0 || mem5.curr
888a0 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 entCount==0 );.
888b0 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 assert( mem5.cu
888c0 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 rrentCount>0 ||
888d0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3d mem5.currentOut=
888e0 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 =0 );.. mem5.aC
888f0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 trl[iBlock] = CT
88900 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 RL_FREE | iLogsi
88910 7a 65 3b 0a 20 20 77 68 69 6c 65 28 20 69 4c 6f ze;. while( iLo
88920 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 20 29 7b 0a gsize<LOGMAX ){.
88930 20 20 20 20 69 6e 74 20 69 42 75 64 64 79 3b 0a int iBuddy;.
88940 20 20 20 20 69 66 28 20 28 69 42 6c 6f 63 6b 3e if( (iBlock>
88950 3e 69 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 29 >iLogsize) & 1 )
88960 7b 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d {. iBuddy =
88970 20 69 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a iBlock - size;.
88980 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
88990 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b iBuddy = iBlock
889a0 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 + size;. }.
889b0 20 20 20 61 73 73 65 72 74 28 20 69 42 75 64 64 assert( iBudd
889c0 79 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 y>=0 );. if(
889d0 28 69 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 (iBuddy+(1<<iLog
889e0 73 69 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f size))>mem5.nBlo
889f0 63 6b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 ck ) break;.
88a00 69 66 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 if( mem5.aCtrl[i
88a10 42 75 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46 52 Buddy]!=(CTRL_FR
88a20 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20 29 EE | iLogsize) )
88a30 20 62 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 break;. mems
88a40 79 73 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64 79 ys5Unlink(iBuddy
88a50 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 20 , iLogsize);.
88a60 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 iLogsize++;.
88a70 20 69 66 28 20 69 42 75 64 64 79 3c 69 42 6c 6f if( iBuddy<iBlo
88a80 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 ck ){. mem5
88a90 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d .aCtrl[iBuddy] =
88aa0 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f CTRL_FREE | iLo
88ab0 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d gsize;. mem
88ac0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
88ad0 3d 20 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f 63 = 0;. iBloc
88ae0 6b 20 3d 20 69 42 75 64 64 79 3b 0a 20 20 20 20 k = iBuddy;.
88af0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
88b00 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
88b10 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c = CTRL_FREE | iL
88b20 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 ogsize;. me
88b30 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d m5.aCtrl[iBuddy]
88b40 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
88b50 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 size *= 2;. }.
88b60 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42 6c memsys5Link(iBl
88b70 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a ock, iLogsize);.
88b80 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
88b90 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f e nBytes of memo
88ba0 72 79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ry.*/.static voi
88bb0 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 d *memsys5Malloc
88bc0 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 (int nBytes){.
88bd0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 sqlite3_int64 *p
88be0 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 74 = 0;. if( nByt
88bf0 65 73 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 es>0 ){. mems
88c00 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 20 20 ys5Enter();.
88c10 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f p = memsys5Mallo
88c20 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b cUnsafe(nBytes);
88c30 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 . memsys5Leav
88c40 65 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 e();. }. retur
88c50 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a n (void*)p; .}..
88c60 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 /*.** Free memor
88c70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 y..*/.static voi
88c80 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f d memsys5Free(vo
88c90 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 69 id *pPrior){. i
88ca0 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a f( pPrior==0 ){.
88cb0 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 72 assert(0);. r
88cc0 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6d 65 6d eturn;. }. mem
88cd0 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d sys5Enter();. m
88ce0 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 emsys5FreeUnsafe
88cf0 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 (pPrior);. mems
88d00 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a ys5Leave(); .}.
88d10 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
88d20 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
88d30 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
88d40 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 ocation.*/.stati
88d50 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 c void *memsys5R
88d60 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
88d70 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 ior, int nBytes)
88d80 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 {. int nOld;.
88d90 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 void *p;. if( p
88da0 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 Prior==0 ){.
88db0 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 35 4d 61 return memsys5Ma
88dc0 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 lloc(nBytes);.
88dd0 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d }. if( nBytes<=
88de0 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 0 ){. memsys5
88df0 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Free(pPrior);.
88e00 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
88e10 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 35 nOld = memsys5
88e20 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Size(pPrior);.
88e30 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 if( nBytes<=nOld
88e40 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
88e50 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d Prior;. }. mem
88e60 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 70 sys5Enter();. p
88e70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 = memsys5Malloc
88e80 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a Unsafe(nBytes);.
88e90 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d if( p ){. m
88ea0 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c emcpy(p, pPrior,
88eb0 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d 73 nOld);. mems
88ec0 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 70 50 ys5FreeUnsafe(pP
88ed0 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d rior);. }. mem
88ee0 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 72 sys5Leave();. r
88ef0 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
88f00 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 * Round up a req
88f10 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 uest size to the
88f20 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f next valid allo
88f30 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a cation size..*/.
88f40 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
88f50 73 35 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 s5Roundup(int n)
88f60 7b 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b {. int iFullSz;
88f70 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d . for(iFullSz=m
88f80 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69 46 75 6c 6c em5.nAtom; iFull
88f90 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d Sz<n; iFullSz *=
88fa0 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 2);. return iF
88fb0 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74 61 74 69 63 ullSz;.}..static
88fc0 20 69 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67 28 int memsys5Log(
88fd0 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 69 int iValue){. i
88fe0 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28 69 nt iLog;. for(i
88ff0 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67 29 Log=0; (1<<iLog)
89000 3c 69 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29 <iValue; iLog++)
89010 3b 0a 20 20 72 65 74 75 72 6e 20 69 4c 6f 67 3b ;. return iLog;
89020 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
89030 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
89040 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
89050 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f 69 64 memsys5Init(void
89060 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69 6e *NotUsed){. in
89070 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 t ii;. int nByt
89080 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 e = sqlite3Globa
89090 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a 20 lConfig.nHeap;.
890a0 20 75 38 20 2a 7a 42 79 74 65 20 3d 20 28 75 38 u8 *zByte = (u8
890b0 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c *)sqlite3Global
890c0 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 Config.pHeap;.
890d0 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20 int nMinLog;
890e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
890f0 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 61 Log of minimum a
89100 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 llocation size i
89110 6e 20 62 79 74 65 73 2a 2f 0a 20 20 69 6e 74 20 n bytes*/. int
89120 69 4f 66 66 73 65 74 3b 0a 0a 20 20 55 4e 55 53 iOffset;.. UNUS
89130 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
89140 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 21 7a Used);.. if( !z
89150 42 79 74 65 20 29 7b 0a 20 20 20 20 72 65 74 75 Byte ){. retu
89160 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
89170 0a 20 20 7d 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20 . }.. nMinLog
89180 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c = memsys5Log(sql
89190 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
891a0 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e .mnReq);. mem5.
891b0 6e 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e nAtom = (1<<nMin
891c0 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 Log);. while( (
891d0 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 4c int)sizeof(Mem5L
891e0 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41 74 6f 6d 20 ink)>mem5.nAtom
891f0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6e 41 74 6f ){. mem5.nAto
89200 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c m = mem5.nAtom <
89210 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 35 < 1;. }.. mem5
89220 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65 .nBlock = (nByte
89230 20 2f 20 28 6d 65 6d 35 2e 6e 41 74 6f 6d 2b 73 / (mem5.nAtom+s
89240 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 6d izeof(u8)));. m
89250 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74 em5.zPool = zByt
89260 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 20 e;. mem5.aCtrl
89270 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a 50 = (u8 *)&mem5.zP
89280 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a ool[mem5.nBlock*
89290 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 0a 20 20 mem5.nAtom];..
892a0 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c 4f for(ii=0; ii<=LO
892b0 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 GMAX; ii++){.
892c0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
892d0 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a [ii] = -1;. }..
892e0 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 iOffset = 0;.
892f0 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b 20 for(ii=LOGMAX;
89300 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 20 ii>=0; ii--){.
89310 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 28 int nAlloc = (
89320 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28 20 1<<ii);. if(
89330 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 (iOffset+nAlloc)
89340 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 7b <=mem5.nBlock ){
89350 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 . mem5.aCtr
89360 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 20 l[iOffset] = ii
89370 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 20 | CTRL_FREE;.
89380 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 memsys5Link(i
89390 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20 20 Offset, ii);.
893a0 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e 41 iOffset += nA
893b0 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 lloc;. }.
893c0 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74 2b assert((iOffset+
893d0 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 6c nAlloc)>mem5.nBl
893e0 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 ock);. }.. ret
893f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
89400 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 ../*.** Deinitia
89410 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
89420 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
89430 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e memsys5Shutdown
89440 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
89450 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
89460 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
89470 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a return;.}../*.**
89480 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 Open the file i
89490 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 ndicated and wri
894a0 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 te a log of all
894b0 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a unfreed memory .
894c0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 ** allocations i
894d0 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f nto that log..*/
894e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
894f0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 void sqlite3Mems
89500 79 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 ys5Dump(const ch
89510 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
89520 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
89530 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b BUG. FILE *out;
89540 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a . int i, j, n;.
89550 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a int nMinLog;..
89560 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d if( zFilename=
89570 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b =0 || zFilename[
89580 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 0]==0 ){. out
89590 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c = stdout;. }el
895a0 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f se{. out = fo
895b0 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 pen(zFilename, "
895c0 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 w");. if( out
895d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72 ==0 ){. fpr
895e0 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a intf(stderr, "**
895f0 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 Unable to outpu
89600 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f t memory debug o
89610 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a utput log: %s **
89620 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
89630 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 zFile
89640 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 name);. ret
89650 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 urn;. }. }.
89660 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b memsys5Enter();
89670 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d . nMinLog = mem
89680 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e 6e 41 74 sys5Log(mem5.nAt
89690 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 om);. for(i=0;
896a0 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b 6e i<=LOGMAX && i+n
896b0 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b MinLog<32; i++){
896c0 0a 20 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d . for(n=0, j=
896d0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
896e0 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45 i]; j>=0; j = ME
896f0 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c M5LINK(j)->next,
89700 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72 69 n++){}. fpri
89710 6e 74 66 28 6f 75 74 2c 20 22 66 72 65 65 6c 69 ntf(out, "freeli
89720 73 74 20 69 74 65 6d 73 20 6f 66 20 73 69 7a 65 st items of size
89730 20 25 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d 35 %d: %d\n", mem5
89740 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 3b .nAtom << i, n);
89750 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f . }. fprintf(o
89760 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 ut, "mem5.nAlloc
89770 20 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 = %llu\n"
89780 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b 0a , mem5.nAlloc);.
89790 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
897a0 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 mem5.totalAlloc
897b0 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d = %llu\n", mem
897c0 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a 20 5.totalAlloc);.
897d0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
897e0 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 em5.totalExcess
897f0 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 = %llu\n", mem5
89800 2e 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a 20 .totalExcess);.
89810 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
89820 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 20 em5.currentOut
89830 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 = %u\n", mem5.c
89840 75 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66 70 urrentOut);. fp
89850 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 rintf(out, "mem5
89860 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d 20 .currentCount =
89870 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 %u\n", mem5.curr
89880 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 entCount);. fpr
89890 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e intf(out, "mem5.
898a0 6d 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20 25 maxOut = %
898b0 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f 75 u\n", mem5.maxOu
898c0 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 t);. fprintf(ou
898d0 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e t, "mem5.maxCoun
898e0 74 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d t = %u\n", m
898f0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a 20 em5.maxCount);.
89900 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
89910 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 20 em5.maxRequest
89920 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d = %u\n", mem5.m
89930 61 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d 65 axRequest);. me
89940 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 msys5Leave();.
89950 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 if( out==stdout
89960 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74 ){. fflush(st
89970 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a dout);. }else{.
89980 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b fclose(out);
89990 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 . }.#else. UNU
899a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 SED_PARAMETER(zF
899b0 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 ilename);.#endif
899c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
899d0 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e outine is the on
899e0 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 ly routine in th
899f0 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 is file with ext
89a00 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 ernal .** linkag
89a10 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 e. It returns a
89a20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
89a30 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f tic sqlite3_mem_
89a40 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63 methods.** struc
89a50 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 t populated with
89a60 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74 the memsys5 met
89a70 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f hods..*/.SQLITE_
89a80 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 PRIVATE const sq
89a90 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
89aa0 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 s *sqlite3MemGet
89ab0 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20 Memsys5(void){.
89ac0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
89ad0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
89ae0 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 s memsys5Methods
89af0 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 = {. memsys
89b00 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 5Malloc,. me
89b10 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20 msys5Free,.
89b20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a memsys5Realloc,.
89b30 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65 memsys5Size
89b40 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f ,. memsys5Ro
89b50 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 undup,. mems
89b60 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 ys5Init,. me
89b70 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20 msys5Shutdown,.
89b80 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 0. };. ret
89b90 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68 urn &memsys5Meth
89ba0 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f ods;.}..#endif /
89bb0 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
89bc0 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a MEMSYS5 */../***
89bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
89be0 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a of mem5.c ******
89bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
89c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
89c30 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a n file mutex.c *
89c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
89c70 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 * 2007 August 14
89c80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
89c90 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
89ca0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
89cb0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
89cc0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
89cd0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
89ce0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
89cf0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
89d00 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
89d10 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
89d20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
89d30 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
89d40 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
89d50 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
89d60 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
89d70 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
89d80 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
89d90 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
89da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
89dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
89de0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
89df0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
89e00 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
89e10 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a ment mutexes..**
89e20 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
89e30 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 ntains code that
89e40 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73 is common acros
89e50 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c s all mutex impl
89e60 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a 2a ementations...**
89e70 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 63 .** $Id: mutex.c
89e80 2c 76 20 31 2e 33 30 20 32 30 30 39 2f 30 32 2f ,v 1.30 2009/02/
89e90 31 37 20 31 36 3a 32 39 3a 31 31 20 64 61 6e 69 17 16:29:11 dani
89ea0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
89eb0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
89ec0 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a _MUTEX_OMIT./*.*
89ed0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
89ee0 20 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 0a 2a mutex system..*
89ef0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
89f00 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 int sqlite3Mute
89f10 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 xInit(void){ .
89f20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
89f30 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 OK;. if( sqlite
89f40 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
89f50 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
89f60 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 if( !sqlite3Glob
89f70 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
89f80 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 MutexAlloc ){.
89f90 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 78 4d /* If the xM
89fa0 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 68 6f 64 utexAlloc method
89fb0 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 has not been se
89fc0 74 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 t, then the user
89fd0 20 64 69 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a did not. *
89fe0 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d 75 74 65 * install a mute
89ff0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
8a000 20 76 69 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e via sqlite3_con
8a010 66 69 67 28 29 20 70 72 69 6f 72 20 74 6f 20 0a fig() prior to .
8a020 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 ** sqlite3
8a030 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 62 65 _initialize() be
8a040 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54 68 69 73 ing called. This
8a050 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 70 6f block copies po
8a060 69 6e 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20 inters to.
8a070 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 69 ** the default i
8a080 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e mplementation in
8a090 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c to the sqlite3Gl
8a0a0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 obalConfig struc
8a0b0 74 75 72 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 ture.. **.
8a0c0 20 20 20 20 20 2a 2a 20 54 68 65 20 64 61 6e 67 ** The dang
8a0d0 65 72 20 69 73 20 74 68 61 74 20 61 6c 74 68 6f er is that altho
8a0e0 75 67 68 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ugh sqlite3_conf
8a0f0 69 67 28 29 20 69 73 20 6e 6f 74 20 61 20 74 68 ig() is not a th
8a100 72 65 61 64 73 61 66 65 0a 20 20 20 20 20 20 2a readsafe. *
8a110 2a 20 41 50 49 2c 20 73 71 6c 69 74 65 33 5f 69 * API, sqlite3_i
8a120 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 2c 20 nitialize() is,
8a130 61 6e 64 20 73 6f 20 6d 75 6c 74 69 70 6c 65 20 and so multiple
8a140 74 68 72 65 61 64 73 20 6d 61 79 20 62 65 0a 20 threads may be.
8a150 20 20 20 20 20 2a 2a 20 61 74 74 65 6d 70 74 69 ** attempti
8a160 6e 67 20 74 6f 20 72 75 6e 20 74 68 69 73 20 66 ng to run this f
8a170 75 6e 63 74 69 6f 6e 20 73 69 6d 75 6c 74 61 6e unction simultan
8a180 65 6f 75 73 6c 79 2e 20 54 6f 20 67 75 61 72 64 eously. To guard
8a190 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 write. **
8a1a0 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 access to the sq
8a1b0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
8a1c0 67 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 g structure, the
8a1d0 20 27 4d 41 53 54 45 52 27 20 73 74 61 74 69 63 'MASTER' static
8a1e0 20 6d 75 74 65 78 0a 20 20 20 20 20 20 2a 2a 20 mutex. **
8a1f0 69 73 20 6f 62 74 61 69 6e 65 64 20 62 65 66 6f is obtained befo
8a200 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 69 74 2e re modifying it.
8a210 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
8a220 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
8a230 74 68 6f 64 73 20 2a 70 20 3d 20 73 71 6c 69 74 thods *p = sqlit
8a240 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 29 e3DefaultMutex()
8a250 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
8a260 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 20 3d mutex *pMaster =
8a270 20 30 3b 0a 20 20 0a 20 20 20 20 20 20 72 63 20 0;. . rc
8a280 3d 20 70 2d 3e 78 4d 75 74 65 78 49 6e 69 74 28 = p->xMutexInit(
8a290 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
8a2a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
8a2b0 20 20 20 20 20 20 70 4d 61 73 74 65 72 20 3d 20 pMaster =
8a2c0 70 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 53 p->xMutexAlloc(S
8a2d0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
8a2e0 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 IC_MASTER);.
8a2f0 20 20 20 20 61 73 73 65 72 74 28 70 4d 61 73 74 assert(pMast
8a300 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e er);. p->
8a310 78 4d 75 74 65 78 45 6e 74 65 72 28 70 4d 61 73 xMutexEnter(pMas
8a320 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 61 73 ter);. as
8a330 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f sert( sqlite3Glo
8a340 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
8a350 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d 30 20 0a xMutexAlloc==0 .
8a360 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
8a370 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8a380 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
8a390 41 6c 6c 6f 63 3d 3d 70 2d 3e 78 4d 75 74 65 78 Alloc==p->xMutex
8a3a0 41 6c 6c 6f 63 0a 20 20 20 20 20 20 20 20 29 3b Alloc. );
8a3b0 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71 . if( !sq
8a3c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
8a3d0 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c g.mutex.xMutexAl
8a3e0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 loc ){.
8a3f0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
8a400 6e 66 69 67 2e 6d 75 74 65 78 20 3d 20 2a 70 3b nfig.mutex = *p;
8a410 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
8a420 20 20 20 70 2d 3e 78 4d 75 74 65 78 4c 65 61 76 p->xMutexLeav
8a430 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 e(pMaster);.
8a440 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
8a450 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
8a460 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
8a470 74 65 78 2e 78 4d 75 74 65 78 49 6e 69 74 28 29 tex.xMutexInit()
8a480 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 ;. }. }.. r
8a490 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
8a4a0 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 ** Shutdown the
8a4b0 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68 mutex system. Th
8a4c0 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65 is call frees re
8a4d0 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 sources allocate
8a4e0 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d d by.** sqlite3M
8a4f0 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 utexInit()..*/.S
8a500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
8a510 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e t sqlite3MutexEn
8a520 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 d(void){. int r
8a530 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
8a540 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
8a550 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
8a560 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20 20 20 MutexEnd ){.
8a570 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 rc = sqlite3Glob
8a580 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
8a590 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a MutexEnd();. }.
8a5a0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
8a5b0 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 /*.** Retrieve a
8a5c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 pointer to a st
8a5d0 61 74 69 63 20 6d 75 74 65 78 20 6f 72 20 61 6c atic mutex or al
8a5e0 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 64 79 6e locate a new dyn
8a5f0 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c amic one..*/.SQL
8a600 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
8a610 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d mutex *sqlite3_m
8a620 75 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 utex_alloc(int i
8a630 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 d){.#ifndef SQLI
8a640 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
8a650 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
8a660 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
8a670 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
8a680 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 return sqlite3G
8a690 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
8a6a0 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 x.xMutexAlloc(id
8a6b0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 );.}..SQLITE_PRI
8a6c0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
8a6d0 65 78 20 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 ex *sqlite3Mutex
8a6e0 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 Alloc(int id){.
8a6f0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f if( !sqlite3Glo
8a700 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d balConfig.bCoreM
8a710 75 74 65 78 20 29 7b 0a 20 20 20 20 72 65 74 75 utex ){. retu
8a720 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rn 0;. }. retu
8a730 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
8a740 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
8a750 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a texAlloc(id);.}.
8a760 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 ./*.** Free a dy
8a770 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a namic mutex..*/.
8a780 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
8a790 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 sqlite3_mutex_fr
8a7a0 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ee(sqlite3_mutex
8a7b0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
8a7c0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
8a7d0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
8a7e0 4d 75 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 MutexFree(p);.
8a7f0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 }.}../*.** Obtai
8a800 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 n the mutex p. I
8a810 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 f some other thr
8a820 65 61 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 ead already has
8a830 74 68 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b the mutex, block
8a840 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e .** until it can
8a850 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f be obtained..*/
8a860 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
8a870 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
8a880 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 nter(sqlite3_mut
8a890 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 ex *p){. if( p
8a8a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
8a8b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
8a8c0 2e 78 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b .xMutexEnter(p);
8a8d0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 . }.}../*.** Ob
8a8e0 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 tain the mutex p
8a8f0 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c . If successful,
8a900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
8a910 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 K. Otherwise, if
8a920 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 another.** thre
8a930 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 ad holds the mut
8a940 65 78 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 ex and it cannot
8a950 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 be obtained, re
8a960 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
8a970 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
8a980 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 int sqlite3_mute
8a990 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 x_try(sqlite3_mu
8a9a0 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 tex *p){. int r
8a9b0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
8a9c0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 if( p ){. re
8a9d0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
8a9e0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
8a9f0 4d 75 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d MutexTry(p);. }
8aa00 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
8aa10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
8aa20 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
8aa30 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
8aa40 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 20 mutex that was
8aa50 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e previously.** en
8aa60 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
8aa70 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
8aa80 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
8aa90 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
8aaa0 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 x .** is not cur
8aab0 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 rently entered.
8aac0 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 If a NULL pointe
8aad0 72 20 69 73 20 70 61 73 73 65 64 20 61 73 20 61 r is passed as a
8aae0 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 n argument.** th
8aaf0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
8ab00 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 no-op..*/.SQLIT
8ab10 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
8ab20 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
8ab30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
8ab40 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 {. if( p ){.
8ab50 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
8ab60 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
8ab70 78 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d xLeave(p);. }.}
8ab80 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
8ab90 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
8aba0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
8abb0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
8abc0 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 x_notheld() rout
8abd0 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e ine are.** inten
8abe0 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 ded for use insi
8abf0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
8ac00 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ements..*/.SQLIT
8ac10 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
8ac20 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 3_mutex_held(sql
8ac30 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
8ac40 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c return p==0 ||
8ac50 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
8ac60 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
8ac70 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 xHeld(p);.}.SQLI
8ac80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
8ac90 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
8aca0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
8acb0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d p){. return p==
8acc0 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 0 || sqlite3Glob
8acd0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
8ace0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b MutexNotheld(p);
8acf0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 .}.#endif..#endi
8ad00 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
8ad10 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a _MUTEX */../****
8ad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
8ad30 66 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a f mutex.c ******
8ad40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ad50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
8ad70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
8ad80 20 66 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 file mutex_noop
8ad90 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
8ada0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8adb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
8adc0 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 2008 October 07
8add0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
8ade0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
8adf0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
8ae00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
8ae10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
8ae20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
8ae30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
8ae40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
8ae50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
8ae60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
8ae70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
8ae80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
8ae90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
8aea0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
8aeb0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
8aec0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
8aed0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
8aee0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
8aef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8af00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8af10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8af20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
8af30 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
8af40 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
8af50 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
8af60 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a ment mutexes..**
8af70 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 .** This impleme
8af80 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 ntation in this
8af90 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 file does not pr
8afa0 6f 76 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c ovide any mutual
8afb0 0a 2a 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e .** exclusion an
8afc0 64 20 69 73 20 74 68 75 73 20 73 75 69 74 61 62 d is thus suitab
8afd0 6c 65 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 le for use only
8afe0 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a in applications.
8aff0 2a 2a 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 ** that use SQLi
8b000 74 65 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 te in a single t
8b010 68 72 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 hread. The rout
8b020 69 6e 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 ines defined.**
8b030 68 65 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 here are place-h
8b040 6f 6c 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 olders. Applica
8b050 74 69 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 tions can substi
8b060 74 75 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 tute working.**
8b070 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 mutex routines a
8b080 74 20 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 t start-time usi
8b090 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 ng the.**.**
8b0a0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
8b0b0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
8b0c0 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 TEX,...).**.** i
8b0d0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
8b0e0 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 If compiled with
8b0f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 SQLITE_DEBUG, t
8b100 68 65 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c hen additional l
8b110 6f 67 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 ogic is inserted
8b120 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 .** that does er
8b130 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 ror checking on
8b140 6d 75 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 mutexes to make
8b150 73 75 72 65 20 74 68 65 79 20 61 72 65 20 62 65 sure they are be
8b160 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f ing.** called co
8b170 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 rrectly..**.** $
8b180 49 64 3a 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 Id: mutex_noop.c
8b190 2c 76 20 31 2e 33 20 32 30 30 38 2f 31 32 2f 30 ,v 1.3 2008/12/0
8b1a0 35 20 31 37 3a 31 37 3a 30 38 20 64 72 68 20 45 5 17:17:08 drh E
8b1b0 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 64 65 xp $.*/...#if de
8b1c0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 fined(SQLITE_MUT
8b1d0 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 EX_NOOP) && !def
8b1e0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
8b1f0 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20 72 6f G)./*.** Stub ro
8b200 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6d utines for all m
8b210 75 74 65 78 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a utex methods..**
8b220 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
8b230 73 20 70 72 6f 76 69 64 65 20 6e 6f 20 6d 75 74 s provide no mut
8b240 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 6f 72 ual exclusion or
8b250 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e error checking.
8b260 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e .*/.static int n
8b270 6f 6f 70 4d 75 74 65 78 48 65 6c 64 28 73 71 6c oopMutexHeld(sql
8b280 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 ite3_mutex *p){
8b290 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 return 1; }.stat
8b2a0 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 ic int noopMutex
8b2b0 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f Notheld(sqlite3_
8b2c0 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 mutex *p){ retur
8b2d0 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e n 1; }.static in
8b2e0 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 28 t noopMutexInit(
8b2f0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
8b300 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 LITE_OK; }.stati
8b310 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 45 c int noopMutexE
8b320 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
8b330 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
8b340 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 atic sqlite3_mut
8b350 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c ex *noopMutexAll
8b360 6f 63 28 69 6e 74 20 69 64 29 7b 20 72 65 74 75 oc(int id){ retu
8b370 72 6e 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 rn (sqlite3_mute
8b380 78 2a 29 38 3b 20 7d 0a 73 74 61 74 69 63 20 76 x*)8; }.static v
8b390 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 oid noopMutexFre
8b3a0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
8b3b0 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 *p){ return; }.s
8b3c0 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d tatic void noopM
8b3d0 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 utexEnter(sqlite
8b3e0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 3_mutex *p){ ret
8b3f0 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e urn; }.static in
8b400 74 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 28 73 t noopMutexTry(s
8b410 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
8b420 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
8b430 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 OK; }.static voi
8b440 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 d noopMutexLeave
8b450 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
8b460 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 53 p){ return; }..S
8b470 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
8b480 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
8b490 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 ods *sqlite3Defa
8b4a0 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a ultMutex(void){.
8b4b0 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
8b4c0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
8b4d0 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f Mutex = {. no
8b4e0 6f 70 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 opMutexInit,.
8b4f0 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20 noopMutexEnd,.
8b500 20 20 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f noopMutexAllo
8b510 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 c,. noopMutex
8b520 46 72 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 Free,. noopMu
8b530 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f texEnter,. no
8b540 6f 70 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 opMutexTry,.
8b550 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a noopMutexLeave,.
8b560 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 48 65 . noopMutexHe
8b570 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 ld,. noopMute
8b580 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 xNotheld. };..
8b590 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b return &sMutex;
8b5a0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 .}.#endif /* def
8b5b0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 ined(SQLITE_MUTE
8b5c0 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 X_NOOP) && !defi
8b5d0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
8b5e0 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 ) */..#if define
8b5f0 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e d(SQLITE_MUTEX_N
8b600 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 OOP) && defined(
8b610 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a SQLITE_DEBUG)./*
8b620 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c .** In this impl
8b630 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 72 72 6f ementation, erro
8b640 72 20 63 68 65 63 6b 69 6e 67 20 69 73 20 70 72 r checking is pr
8b650 6f 76 69 64 65 64 20 66 6f 72 20 74 65 73 74 69 ovided for testi
8b660 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 ng.** and debugg
8b670 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 20 20 54 ing purposes. T
8b680 68 65 20 6d 75 74 65 78 65 73 20 73 74 69 6c 6c he mutexes still
8b690 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 do not provide
8b6a0 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 6c 20 65 78 any.** mutual ex
8b6b0 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a clusion..*/../*.
8b6c0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a ** The mutex obj
8b6d0 65 63 74 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 ect.*/.struct sq
8b6e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 lite3_mutex {.
8b6f0 69 6e 74 20 69 64 3b 20 20 20 20 20 2f 2a 20 54 int id; /* T
8b700 68 65 20 6d 75 74 65 78 20 74 79 70 65 20 2a 2f he mutex type */
8b710 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 2f . int cnt; /
8b720 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
8b730 69 65 73 20 77 69 74 68 6f 75 74 20 61 20 6d 61 ies without a ma
8b740 74 63 68 69 6e 67 20 6c 65 61 76 65 20 2a 2f 0a tching leave */.
8b750 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 };../*.** The sq
8b760 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
8b770 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
8b780 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 utex_notheld() r
8b790 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e outine are.** in
8b7a0 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 tended for use i
8b7b0 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 nside assert() s
8b7c0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 tatements..*/.st
8b7d0 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 atic int debugMu
8b7e0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f texHeld(sqlite3_
8b7f0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 mutex *p){. ret
8b800 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 urn p==0 || p->c
8b810 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 nt>0;.}.static i
8b820 6e 74 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 nt debugMutexNot
8b830 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
8b840 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ex *p){. return
8b850 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d p==0 || p->cnt=
8b860 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 =0;.}../*.** Ini
8b870 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
8b880 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 itialize the mut
8b890 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f ex subsystem..*/
8b8a0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 .static int debu
8b8b0 67 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 gMutexInit(void)
8b8c0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
8b8d0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 OK; }.static int
8b8e0 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64 28 76 debugMutexEnd(v
8b8f0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c oid){ return SQL
8b900 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK; }../*.**
8b910 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
8b920 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 ex_alloc() routi
8b930 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e ne allocates a n
8b940 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 ew.** mutex and
8b950 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
8b960 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 r to it. If it
8b970 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 returns NULL.**
8b980 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
8b990 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f a mutex could no
8b9a0 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 t be allocated.
8b9b0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
8b9c0 65 33 5f 6d 75 74 65 78 20 2a 64 65 62 75 67 4d e3_mutex *debugM
8b9d0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 utexAlloc(int id
8b9e0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
8b9f0 74 65 33 5f 6d 75 74 65 78 20 61 53 74 61 74 69 te3_mutex aStati
8ba00 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f c[6];. sqlite3_
8ba10 6d 75 74 65 78 20 2a 70 4e 65 77 20 3d 20 30 3b mutex *pNew = 0;
8ba20 0a 20 20 73 77 69 74 63 68 28 20 69 64 20 29 7b . switch( id ){
8ba30 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
8ba40 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 _MUTEX_FAST:.
8ba50 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 case SQLITE_MUT
8ba60 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a EX_RECURSIVE: {.
8ba70 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c pNew = sql
8ba80 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f ite3Malloc(sizeo
8ba90 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 20 f(*pNew));.
8baa0 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 if( pNew ){.
8bab0 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 pNew->id =
8bac0 69 64 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 id;. pNew
8bad0 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 ->cnt = 0;.
8bae0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
8baf0 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
8bb00 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 t: {. asser
8bb10 74 28 20 69 64 2d 32 20 3e 3d 20 30 20 29 3b 0a t( id-2 >= 0 );.
8bb20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64 assert( id
8bb30 2d 32 20 3c 20 28 69 6e 74 29 28 73 69 7a 65 6f -2 < (int)(sizeo
8bb40 66 28 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f f(aStatic)/sizeo
8bb50 66 28 61 53 74 61 74 69 63 5b 30 5d 29 29 20 29 f(aStatic[0])) )
8bb60 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 ;. pNew = &
8bb70 61 53 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 aStatic[id-2];.
8bb80 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 pNew->id =
8bb90 69 64 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b id;. break;
8bba0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
8bbb0 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
8bbc0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
8bbd0 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 deallocates a pr
8bbe0 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 eviously allocat
8bbf0 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 ed mutex..*/.sta
8bc00 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 tic void debugMu
8bc10 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
8bc20 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
8bc30 65 72 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 ert( p->cnt==0 )
8bc40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
8bc50 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
8bc60 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 FAST || p->id==S
8bc70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
8bc80 52 53 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 RSIVE );. sqlit
8bc90 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f e3_free(p);.}../
8bca0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
8bcb0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 _mutex_enter() a
8bcc0 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nd sqlite3_mutex
8bcd0 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 _try() routines
8bce0 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e attempt.** to en
8bcf0 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 ter a mutex. If
8bd00 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 another thread
8bd10 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 is already withi
8bd20 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 n the mutex,.**
8bd30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
8bd40 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b ter() will block
8bd50 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
8bd60 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 ex_try() will re
8bd70 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 turn.** SQLITE_B
8bd80 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 USY. The sqlite
8bd90 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 3_mutex_try() in
8bda0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
8bdb0 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f SQLITE_OK.** upo
8bdc0 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 n successful ent
8bdd0 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 ry. Mutexes cre
8bde0 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 ated using SQLIT
8bdf0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
8be00 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 E can.** be ente
8be10 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d red multiple tim
8be20 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 es by the same t
8be30 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 hread. In such
8be40 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 cases the,.** mu
8be50 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 tex must be exit
8be60 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 ed an equal numb
8be70 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f er of times befo
8be80 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 re another threa
8be90 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 d.** can enter.
8bea0 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 If the same thr
8beb0 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 ead tries to ent
8bec0 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e er any other kin
8bed0 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f d of mutex.** mo
8bee0 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 re than once, th
8bef0 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e e behavior is un
8bf00 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 defined..*/.stat
8bf10 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 ic void debugMut
8bf20 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f exEnter(sqlite3_
8bf30 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
8bf40 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
8bf50 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
8bf60 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 VE || debugMutex
8bf70 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
8bf80 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 p->cnt++;.}.stat
8bf90 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
8bfa0 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 xTry(sqlite3_mut
8bfb0 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
8bfc0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f ( p->id==SQLITE_
8bfd0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
8bfe0 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 || debugMutexNot
8bff0 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e held(p) );. p->
8c000 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 cnt++;. return
8c010 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
8c020 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
8c030 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f mutex_leave() ro
8c040 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 utine exits a mu
8c050 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 tex that was.**
8c060 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 previously enter
8c070 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 ed by the same t
8c080 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 hread. The beha
8c090 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 vior.** is undef
8c0a0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
8c0b0 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 x is not current
8c0c0 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a ly entered or.**
8c0d0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
8c0e0 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 y allocated. SQ
8c0f0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 Lite will never
8c100 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 do either..*/.st
8c110 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d atic void debugM
8c120 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 utexLeave(sqlite
8c130 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
8c140 73 73 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 ssert( debugMute
8c150 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d xHeld(p) );. p-
8c160 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 >cnt--;. assert
8c170 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f ( p->id==SQLITE_
8c180 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
8c190 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 || debugMutexNot
8c1a0 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 held(p) );.}..SQ
8c1b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
8c1c0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
8c1d0 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 ds *sqlite3Defau
8c1e0 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 ltMutex(void){.
8c1f0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
8c200 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d mutex_methods sM
8c210 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 utex = {. deb
8c220 75 67 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 ugMutexInit,.
8c230 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a debugMutexEnd,.
8c240 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c debugMutexAl
8c250 6c 6f 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 loc,. debugMu
8c260 74 65 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62 texFree,. deb
8c270 75 67 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 ugMutexEnter,.
8c280 20 20 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c debugMutexTry,
8c290 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c . debugMutexL
8c2a0 65 61 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 eave,.. debug
8c2b0 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 MutexHeld,. d
8c2c0 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 ebugMutexNotheld
8c2d0 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 . };.. return
8c2e0 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 &sMutex;.}.#endi
8c2f0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c f /* defined(SQL
8c300 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 ITE_MUTEX_NOOP)
8c310 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
8c320 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a E_DEBUG) */../**
8c330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
8c340 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 of mutex_noop.c
8c350 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
8c360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
8c380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
8c390 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 in file mutex_os
8c3a0 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2.c ************
8c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
8c3d0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 ** 2007 August 2
8c3e0 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 8.**.** The auth
8c3f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
8c400 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
8c410 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
8c420 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
8c430 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
8c440 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
8c450 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
8c460 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
8c470 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
8c480 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
8c490 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
8c4a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
8c4b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
8c4c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
8c4d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
8c4e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
8c4f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
8c500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c540 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
8c550 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
8c560 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
8c570 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f ement mutexes fo
8c580 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 r OS/2.**.** $Id
8c590 3a 20 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 : mutex_os2.c,v
8c5a0 31 2e 31 31 20 32 30 30 38 2f 31 31 2f 32 32 20 1.11 2008/11/22
8c5b0 31 39 3a 35 30 3a 35 34 20 70 77 65 69 6c 62 61 19:50:54 pweilba
8c5c0 63 68 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f cher Exp $.*/../
8c5d0 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e *.** The code in
8c5e0 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e this file is on
8c5f0 6c 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 ly used if SQLIT
8c600 45 5f 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 E_MUTEX_OS2 is d
8c610 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 efined..** See t
8c620 68 65 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 he mutex.h file
8c630 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a for details..*/.
8c640 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
8c650 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a TEX_OS2../******
8c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c670 20 4f 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c OS/2 Mutex Impl
8c680 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a ementation *****
8c690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c6a0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 *.**.** This imp
8c6b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d lementation of m
8c6c0 75 74 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 utexes is built
8c6d0 75 73 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 using the OS/2 A
8c6e0 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 PI..*/../*.** Th
8c6f0 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a e mutex object.*
8c700 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 * Each recursive
8c710 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 mutex is an ins
8c720 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
8c730 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
8c740 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
8c750 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d te3_mutex {. HM
8c760 54 58 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 TX mutex;
8c770 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c /* Mutex control
8c780 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f ling the lock */
8c790 0a 20 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 . int id;
8c7a0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 /* Mutex ty
8c7b0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 pe */. int nRe
8c7c0 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d f; /* Num
8c7d0 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
8c7e0 73 20 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 s */. TID owne
8c7f0 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 r; /* Thre
8c800 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 ad holding this
8c810 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 mutex */.};..#de
8c820 66 69 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 fine OS2_MUTEX_I
8c830 4e 49 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 NITIALIZER 0,0
8c840 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 ,0,0../*.** Init
8c850 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
8c860 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 tialize the mute
8c870 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a x subsystem..*/.
8c880 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 static int os2Mu
8c890 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 texInit(void){ r
8c8a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
8c8b0 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 }.static int os
8c8c0 32 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 2MutexEnd(void){
8c8d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
8c8e0 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K; }../*.** The
8c8f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
8c900 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c loc() routine al
8c910 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a locates a new.**
8c920 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 mutex and retur
8c930 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
8c940 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 it. If it retur
8c950 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
8c960 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
8c970 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
8c980 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 allocated. .** S
8c990 51 4c 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e QLite will unwin
8c9a0 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 d its stack and
8c9b0 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e return an error.
8c9c0 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a The argument.*
8c9d0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
8c9e0 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
8c9f0 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
8ca00 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
8ca10 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
8ca20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
8ca30 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 AST
8ca40 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 0.** <li> SQL
8ca50 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
8ca60 49 56 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a IVE 1.*
8ca70 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
8ca80 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
8ca90 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 ER 2.** <li
8caa0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
8cab0 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 STATIC_MEM
8cac0 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 3.** <li> SQ
8cad0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
8cae0 43 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a C_PRNG 4.
8caf0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
8cb00 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e he first two con
8cb10 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c stants cause sql
8cb20 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
8cb30 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 () to create.**
8cb40 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 a new mutex. Th
8cb50 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 e new mutex is r
8cb60 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 ecursive when SQ
8cb70 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
8cb80 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 SIVE.** is used
8cb90 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 but not necessar
8cba0 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 ily so when SQLI
8cbb0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 TE_MUTEX_FAST is
8cbc0 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 used..** The mu
8cbd0 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
8cbe0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 on does not need
8cbf0 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 to make a disti
8cc00 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 nction.** betwee
8cc10 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
8cc20 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c ECURSIVE and SQL
8cc30 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 ITE_MUTEX_FAST i
8cc40 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 f it does.** not
8cc50 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 want to. But S
8cc60 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 QLite will only
8cc70 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 request a recurs
8cc80 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 ive mutex in.**
8cc90 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 cases where it r
8cca0 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e eally needs one.
8ccb0 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f If a faster no
8ccc0 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n-recursive mute
8ccd0 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 x.** implementat
8cce0 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ion is available
8ccf0 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 on the host pla
8cd00 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 tform, the mutex
8cd10 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 subsystem.** mi
8cd20 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 ght return such
8cd30 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f a mutex in respo
8cd40 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 nse to SQLITE_MU
8cd50 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 TEX_FAST..**.**
8cd60 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 The other allowe
8cd70 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 d parameters to
8cd80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
8cd90 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 loc() each retur
8cda0 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 n.** a pointer t
8cdb0 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 o a static preex
8cdc0 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 isting mutex. T
8cdd0 68 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 hree static mute
8cde0 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 xes are.** used
8cdf0 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 by the current v
8ce00 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 ersion of SQLite
8ce10 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f . Future versio
8ce20 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 ns of SQLite.**
8ce30 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e may add addition
8ce40 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 al static mutexe
8ce50 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 s. Static mutex
8ce60 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 es are for inter
8ce70 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 nal.** use by SQ
8ce80 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c Lite only. Appl
8ce90 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 ications that us
8cea0 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 e SQLite mutexes
8ceb0 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f should.** use o
8cec0 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 nly the dynamic
8ced0 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 mutexes returned
8cee0 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 by SQLITE_MUTEX
8cef0 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 _FAST or.** SQLI
8cf00 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
8cf10 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 VE..**.** Note t
8cf20 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 hat if one of th
8cf30 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 e dynamic mutex
8cf40 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 parameters (SQLI
8cf50 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a TE_MUTEX_FAST.**
8cf60 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 or SQLITE_MUTEX
8cf70 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 _RECURSIVE) is u
8cf80 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 sed then sqlite3
8cf90 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a _mutex_alloc().*
8cfa0 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 * returns a diff
8cfb0 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 erent mutex on e
8cfc0 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 very call. But
8cfd0 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a for the static.*
8cfe0 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 * mutex types, t
8cff0 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 he same mutex is
8d000 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 returned on eve
8d010 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 ry call that has
8d020 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 .** the same typ
8d030 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
8d040 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tic sqlite3_mute
8d050 78 20 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 x *os2MutexAlloc
8d060 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 (int iType){. s
8d070 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 qlite3_mutex *p
8d080 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 = NULL;. switch
8d090 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 ( iType ){. c
8d0a0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
8d0b0 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 _FAST:. case
8d0c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
8d0d0 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 URSIVE: {.
8d0e0 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
8d0f0 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 cZero( sizeof(*p
8d100 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ) );. if( p
8d110 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
8d120 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 d = iType;.
8d130 20 20 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 if( DosCreate
8d140 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d MutexSem( 0, &p-
8d150 3e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 >mutex, 0, FALSE
8d160 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 ) != NO_ERROR )
8d170 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
8d180 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 te3_free( p );.
8d190 20 20 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c p = NUL
8d1a0 4c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 L;. }.
8d1b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
8d1c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
8d1d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 ult: {. sta
8d1e0 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 tic volatile int
8d1f0 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 isInit = 0;.
8d200 20 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 static sqlite
8d210 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 3_mutex staticMu
8d220 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 texes[] = {.
8d230 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f { OS2_MUTEX_
8d240 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a INITIALIZER, },.
8d250 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
8d260 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
8d270 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
8d280 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
8d290 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 ZER, },.
8d2a0 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 { OS2_MUTEX_INIT
8d2b0 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 IALIZER, },.
8d2c0 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f { OS2_MUTEX_
8d2d0 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a INITIALIZER, },.
8d2e0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
8d2f0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
8d300 20 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 },. };.
8d310 20 20 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 if ( !isInit
8d320 29 7b 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 ){. APIRE
8d330 54 20 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 T rc;. PT
8d340 49 42 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 IB ptib;.
8d350 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 PPIB ppib;.
8d360 20 20 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a HMTX mutex;.
8d370 20 20 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d char nam
8d380 65 5b 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 e[32];. D
8d390 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 osGetInfoBlocks(
8d3a0 20 26 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b &ptib, &ppib );
8d3b0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
8d3c0 5f 73 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f _snprintf( sizeo
8d3d0 66 28 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 f(name), name, "
8d3e0 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 \\SEM32\\SQLITE%
8d3f0 30 34 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 04x",.
8d400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8d410 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 ppib->pib_ulpid
8d420 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 );. while
8d430 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 ( !isInit ){.
8d440 20 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 mutex = 0
8d450 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
8d460 20 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 DosCreateMutexS
8d470 65 6d 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 em( name, &mutex
8d480 2c 20 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 , 0, FALSE);.
8d490 20 20 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d if( rc ==
8d4a0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
8d4b0 20 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 unsigne
8d4c0 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 d int i;.
8d4d0 20 20 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 if( !isInit
8d4e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
8d4f0 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 for( i = 0; i
8d500 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d < sizeof(staticM
8d510 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 utexes)/sizeof(s
8d520 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 taticMutexes[0])
8d530 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 ; i++ ){.
8d540 20 20 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 DosCrea
8d550 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 teMutexSem( 0, &
8d560 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d staticMutexes[i]
8d570 2e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 .mutex, 0, FALSE
8d580 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
8d590 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
8d5a0 20 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 isInit = 1;.
8d5b0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
8d5c0 20 20 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 DosClose
8d5d0 4d 75 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 MutexSem( mutex
8d5e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
8d5f0 73 65 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 se if( rc == ERR
8d600 4f 52 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d OR_DUPLICATE_NAM
8d610 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 E ){.
8d620 20 44 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a DosSleep( 1 );.
8d630 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
8d640 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 . ret
8d650 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 urn p;.
8d660 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
8d670 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 }. asser
8d680 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 t( iType-2 >= 0
8d690 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
8d6a0 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f iType-2 < sizeo
8d6b0 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 f(staticMutexes)
8d6c0 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 /sizeof(staticMu
8d6d0 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 texes[0]) );.
8d6e0 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 p = &staticMu
8d6f0 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a texes[iType-2];.
8d700 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
8d710 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ype;. break
8d720 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
8d730 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn p;.}.../*.*
8d740 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
8d750 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 eallocates a pre
8d760 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 viously allocate
8d770 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 d mutex..** SQLi
8d780 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f te is careful to
8d790 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 deallocate ever
8d7a0 79 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 y mutex that it
8d7b0 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 allocates..*/.st
8d7c0 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 atic void os2Mut
8d7d0 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d exFree(sqlite3_m
8d7e0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 utex *p){. if(
8d7f0 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
8d800 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
8d810 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
8d820 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
8d830 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e UTEX_FAST || p->
8d840 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
8d850 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
8d860 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d DosCloseMutexSem
8d870 28 20 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 ( p->mutex );.
8d880 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 sqlite3_free( p
8d890 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
8d8a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
8d8b0 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ter() and sqlite
8d8c0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
8d8d0 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a utines attempt.*
8d8e0 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 * to enter a mut
8d8f0 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 ex. If another
8d900 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 thread is alread
8d910 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 y within the mut
8d920 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ex,.** sqlite3_m
8d930 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c utex_enter() wil
8d940 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 l block and sqli
8d950 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
8d960 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 will return.** S
8d970 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 QLITE_BUSY. The
8d980 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
8d990 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ry() interface r
8d9a0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
8d9b0 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 .** upon success
8d9c0 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 ful entry. Mute
8d9d0 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e xes created usin
8d9e0 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 g SQLITE_MUTEX_R
8d9f0 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 ECURSIVE can.**
8da00 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 be entered multi
8da10 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 ple times by the
8da20 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 same thread. I
8da30 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 n such cases the
8da40 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 ,.** mutex must
8da50 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 be exited an equ
8da60 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d al number of tim
8da70 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 es before anothe
8da80 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 r thread.** can
8da90 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 enter. If the s
8daa0 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 ame thread tries
8dab0 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 to enter any ot
8dac0 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 her kind of mute
8dad0 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f x.** more than o
8dae0 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f nce, the behavio
8daf0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a r is undefined..
8db00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f */.static void o
8db10 73 32 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c s2MutexEnter(sql
8db20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
8db30 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 TID tid;. PID
8db40 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e holder1;. ULON
8db50 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 G holder2;. if(
8db60 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a p==0 ) return;.
8db70 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
8db80 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
8db90 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 CURSIVE || os2Mu
8dba0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
8dbb0 0a 20 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 . DosRequestMut
8dbc0 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 exSem(p->mutex,
8dbd0 53 45 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57 SEM_INDEFINITE_W
8dbe0 41 49 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 AIT);. DosQuery
8dbf0 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
8dc00 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 x, &holder1, &ti
8dc10 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 d, &holder2);.
8dc20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a p->owner = tid;.
8dc30 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 p->nRef++;.}.s
8dc40 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 tatic int os2Mut
8dc50 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 exTry(sqlite3_mu
8dc60 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 tex *p){. int r
8dc70 63 3b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 c;. TID tid;.
8dc80 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 PID holder1;. U
8dc90 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 LONG holder2;.
8dca0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
8dcb0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 n SQLITE_OK;. a
8dcc0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
8dcd0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
8dce0 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 SIVE || os2Mutex
8dcf0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
8dd00 69 66 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75 if( DosRequestMu
8dd10 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c texSem(p->mutex,
8dd20 20 53 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52 SEM_IMMEDIATE_R
8dd30 45 54 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52 ETURN) == NO_ERR
8dd40 4f 52 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 OR) {. DosQue
8dd50 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 ryMutexSem(p->mu
8dd60 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 tex, &holder1, &
8dd70 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a tid, &holder2);.
8dd80 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 p->owner = t
8dd90 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b id;. p->nRef+
8dda0 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 +;. rc = SQLI
8ddb0 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20 TE_OK;. } else
8ddc0 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
8ddd0 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72 E_BUSY;. }.. r
8dde0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
8ddf0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
8de00 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 utex_leave() rou
8de10 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 tine exits a mut
8de20 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 ex that was.** p
8de30 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 reviously entere
8de40 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 d by the same th
8de50 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 read. The behav
8de60 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 ior.** is undefi
8de70 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 ned if the mutex
8de80 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
8de90 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 y entered or.**
8dea0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
8deb0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c allocated. SQL
8dec0 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 ite will never d
8ded0 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 o either..*/.sta
8dee0 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 tic void os2Mute
8def0 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d xLeave(sqlite3_m
8df00 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 utex *p){. TID
8df10 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 tid;. PID holde
8df20 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 r1;. ULONG hold
8df30 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 er2;. if( p==0
8df40 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
8df50 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
8df60 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 . DosQueryMutex
8df70 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 Sem(p->mutex, &h
8df80 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 older1, &tid, &h
8df90 6f 6c 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72 older2);. asser
8dfa0 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 t( p->owner==tid
8dfb0 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b );. p->nRef--;
8dfc0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
8dfd0 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d ef==0 || p->id==
8dfe0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
8dff0 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52 URSIVE );. DosR
8e000 65 6c 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70 eleaseMutexSem(p
8e010 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 ->mutex);.}..#if
8e020 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
8e030 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
8e040 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
8e050 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
8e060 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 x_notheld() rout
8e070 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e ine are.** inten
8e080 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 ded for use insi
8e090 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
8e0a0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 ements..*/.stati
8e0b0 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 c int os2MutexHe
8e0c0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
8e0d0 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b *p){. TID tid;
8e0e0 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c . PID pid;. UL
8e0f0 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 ONG ulCount;. P
8e100 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 TIB ptib;. if(
8e110 70 21 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 p!=0 ) {. Dos
8e120 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d QueryMutexSem(p-
8e130 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 >mutex, &pid, &t
8e140 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 id, &ulCount);.
8e150 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f } else {. Do
8e160 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 sGetInfoBlocks(&
8e170 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 ptib, NULL);.
8e180 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 tid = ptib->tib
8e190 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 _ptib2->tib2_ult
8e1a0 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e id;. }. return
8e1b0 20 70 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 p==0 || (p->nRe
8e1c0 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 f!=0 && p->owner
8e1d0 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 ==tid);.}.static
8e1e0 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 int os2MutexNot
8e1f0 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
8e200 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 ex *p){. TID ti
8e210 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 d;. PID pid;.
8e220 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 ULONG ulCount;.
8e230 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 PTIB ptib;. if
8e240 28 20 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 ( p!= 0 ) {.
8e250 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d DosQueryMutexSem
8e260 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c (p->mutex, &pid,
8e270 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 &tid, &ulCount)
8e280 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
8e290 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b DosGetInfoBlock
8e2a0 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a s(&ptib, NULL);.
8e2b0 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e tid = ptib->
8e2c0 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f tib_ptib2->tib2_
8e2d0 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 ultid;. }. ret
8e2e0 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e urn p==0 || p->n
8e2f0 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e Ref==0 || p->own
8e300 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 er!=tid;.}.#endi
8e310 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
8e320 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f E sqlite3_mutex_
8e330 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
8e340 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
8e350 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c d){. static sql
8e360 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
8e370 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 ds sMutex = {.
8e380 20 20 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a os2MutexInit,.
8e390 20 20 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c os2MutexEnd,
8e3a0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c . os2MutexAll
8e3b0 6f 63 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 oc,. os2Mutex
8e3c0 46 72 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 Free,. os2Mut
8e3d0 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 exEnter,. os2
8e3e0 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 MutexTry,. os
8e3f0 32 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 2MutexLeave,.#if
8e400 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
8e410 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c . os2MutexHel
8e420 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e d,. os2MutexN
8e430 6f 74 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 otheld.#endif.
8e440 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d };.. return &sM
8e450 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f utex;.}.#endif /
8e460 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f * SQLITE_MUTEX_O
8e470 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a S2 */../********
8e480 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 ****** End of mu
8e490 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a tex_os2.c ******
8e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e4c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
8e4d0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
8e4e0 65 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a e mutex_unix.c *
8e4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e510 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
8e520 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 7 August 28.**.*
8e530 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
8e540 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
8e550 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
8e560 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
8e570 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
8e580 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
8e590 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
8e5a0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
8e5b0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
8e5c0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
8e5d0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
8e5e0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
8e5f0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
8e600 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
8e610 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
8e620 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
8e630 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
8e640 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
8e650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8e680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
8e690 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
8e6a0 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
8e6b0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
8e6c0 6d 75 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 mutexes for pthr
8e6d0 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 eads.**.** $Id:
8e6e0 6d 75 74 65 78 5f 75 6e 69 78 2e 63 2c 76 20 31 mutex_unix.c,v 1
8e6f0 2e 31 36 20 32 30 30 38 2f 31 32 2f 30 38 20 31 .16 2008/12/08 1
8e700 38 3a 31 39 3a 31 38 20 64 72 68 20 45 78 70 20 8:19:18 drh Exp
8e710 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
8e720 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c code in this fil
8e730 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 e is only used i
8e740 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 f we are compili
8e750 6e 67 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a ng threadsafe.**
8e760 20 75 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68 under unix with
8e770 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a pthreads..**.**
8e780 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 Note that this
8e790 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 implementation r
8e7a0 65 71 75 69 72 65 73 20 61 20 76 65 72 73 69 6f equires a versio
8e7b0 6e 20 6f 66 20 70 74 68 72 65 61 64 73 20 74 68 n of pthreads th
8e7c0 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 at.** supports r
8e7d0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
8e7e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
8e7f0 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 TE_MUTEX_PTHREAD
8e800 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 S..#include <pth
8e810 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 read.h>.../*.**
8e820 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d Each recursive m
8e830 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 utex is an insta
8e840 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
8e850 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
8e860 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
8e870 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72 3_mutex {. pthr
8e880 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 ead_mutex_t mute
8e890 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 x; /* Mutex
8e8a0 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 controlling the
8e8b0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 lock */. int id
8e8c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
8e8d0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 /* Mutex ty
8e8e0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 pe */. int nRef
8e8f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
8e900 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
8e910 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70 entrances */. p
8e920 74 68 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20 thread_t owner;
8e930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 /* Thr
8e940 65 61 64 20 74 68 61 74 20 69 73 20 77 69 74 68 ead that is with
8e950 69 6e 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f in this mutex */
8e960 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
8e970 45 42 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65 EBUG. int trace
8e980 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
8e990 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 /* True to tra
8e9a0 63 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 ce changes */.#e
8e9b0 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53 ndif.};.#ifdef S
8e9c0 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 QLITE_DEBUG.#def
8e9d0 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 ine SQLITE3_MUTE
8e9e0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 X_INITIALIZER {
8e9f0 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e PTHREAD_MUTEX_IN
8ea00 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c ITIALIZER, 0, 0,
8ea10 20 28 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30 (pthread_t)0, 0
8ea20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 }.#else.#define
8ea30 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 SQLITE3_MUTEX_I
8ea40 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 NITIALIZER { PTH
8ea50 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 READ_MUTEX_INITI
8ea60 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 ALIZER, 0, 0, (p
8ea70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e thread_t)0 }.#en
8ea80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 dif../*.** The s
8ea90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8eaa0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
8eab0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
8eac0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
8ead0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
8eae0 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 only inside asse
8eaf0 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
8eb00 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f On some platfo
8eb10 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 rms,.** there mi
8eb20 67 68 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64 ght be race cond
8eb30 69 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 itions that can
8eb40 63 61 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 cause these rout
8eb50 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 ines to.** deliv
8eb60 65 72 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 er incorrect res
8eb70 75 6c 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63 ults. In partic
8eb80 75 6c 61 72 2c 20 69 66 20 70 74 68 72 65 61 64 ular, if pthread
8eb90 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e _equal() is.** n
8eba0 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 ot an atomic ope
8ebb0 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 ration, then the
8ebc0 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 se routines migh
8ebd0 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e t delivery.** in
8ebe0 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e correct results.
8ebf0 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f On most platfo
8ec00 72 6d 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75 rms, pthread_equ
8ec10 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f al() is a .** co
8ec20 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 mparison of two
8ec30 69 6e 74 65 67 65 72 73 20 61 6e 64 20 69 73 20 integers and is
8ec40 74 68 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 therefore atomic
8ec50 2e 20 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a . But we are.**
8ec60 20 74 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20 told that HPUX
8ec70 69 73 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c is not such a pl
8ec80 61 74 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 atform. If so,
8ec90 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 then these routi
8eca0 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 nes.** will not
8ecb0 61 6c 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 always work corr
8ecc0 65 63 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a ectly on HPUX..*
8ecd0 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c *.** On those pl
8ece0 61 74 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74 atforms where pt
8ecf0 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 hread_equal() is
8ed00 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c not atomic, SQL
8ed10 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 ite.** should be
8ed20 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 compiled withou
8ed30 74 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 t -DSQLITE_DEBUG
8ed40 20 61 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42 and with -DNDEB
8ed50 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 UG to.** make su
8ed60 72 65 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73 re no assert() s
8ed70 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 tatements are ev
8ed80 61 6c 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63 aluated and henc
8ed90 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 e these.** routi
8eda0 6e 65 73 20 61 72 65 20 6e 65 76 65 72 20 63 61 nes are never ca
8edb0 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 lled..*/.#if !de
8edc0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c fined(NDEBUG) ||
8edd0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
8ede0 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e DEBUG).static in
8edf0 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 t pthreadMutexHe
8ee00 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
8ee10 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 *p){. return (
8ee20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74 p->nRef!=0 && pt
8ee30 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f hread_equal(p->o
8ee40 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 wner, pthread_se
8ee50 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 lf()));.}.static
8ee60 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 int pthreadMute
8ee70 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
8ee80 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
8ee90 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 turn p->nRef==0
8eea0 7c 7c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c || pthread_equal
8eeb0 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 (p->owner, pthre
8eec0 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d ad_self())==0;.}
8eed0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
8eee0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 nitialize and de
8eef0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d initialize the m
8ef00 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a utex subsystem..
8ef10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 */.static int pt
8ef20 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76 hreadMutexInit(v
8ef30 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c oid){ return SQL
8ef40 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
8ef50 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 int pthreadMute
8ef60 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 xEnd(void){ retu
8ef70 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
8ef80 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
8ef90 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
8efa0 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 routine allocat
8efb0 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 es a new.** mute
8efc0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 x and returns a
8efd0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
8efe0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 If it returns NU
8eff0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 LL.** that means
8f000 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f that a mutex co
8f010 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 uld not be alloc
8f020 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a ated. SQLite.**
8f030 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 will unwind its
8f040 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 stack and retur
8f050 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 n an error. The
8f060 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
8f070 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
8f080 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 loc() is one of
8f090 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f these integer co
8f0a0 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c nstants:.**.** <
8f0b0 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c ul>.** <li> SQL
8f0c0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a ITE_MUTEX_FAST.*
8f0d0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
8f0e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a UTEX_RECURSIVE.*
8f0f0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
8f100 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
8f110 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 ER.** <li> SQLI
8f120 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
8f130 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c MEM.** <li> SQL
8f140 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
8f150 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _MEM2.** <li> S
8f160 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
8f170 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 IC_PRNG.** <li>
8f180 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
8f190 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 2f 75 6c ATIC_LRU.** </ul
8f1a0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 >.**.** The firs
8f1b0 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
8f1c0 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
8f1d0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
8f1e0 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
8f1f0 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
8f200 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
8f210 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
8f220 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
8f230 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
8f240 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
8f250 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
8f260 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a X_FAST is used..
8f270 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 ** The mutex imp
8f280 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
8f290 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
8f2a0 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a e a distinction.
8f2b0 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 ** between SQLIT
8f2c0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
8f2d0 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 E and SQLITE_MUT
8f2e0 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f EX_FAST if it do
8f2f0 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 es.** not want t
8f300 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 o. But SQLite w
8f310 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 ill only request
8f320 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
8f330 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 ex in.** cases w
8f340 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e here it really n
8f350 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 eeds one. If a
8f360 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 faster non-recur
8f370 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d sive mutex.** im
8f380 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
8f390 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 available on the
8f3a0 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 host platform,
8f3b0 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
8f3c0 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 tem.** might ret
8f3d0 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 urn such a mutex
8f3e0 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
8f3f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
8f400 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 T..**.** The oth
8f410 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d er allowed param
8f420 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 eters to sqlite3
8f430 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 _mutex_alloc() e
8f440 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 ach return.** a
8f450 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
8f460 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 tic preexisting
8f470 6d 75 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 mutex. Three st
8f480 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 atic mutexes are
8f490 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 .** used by the
8f4a0 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 current version
8f4b0 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 of SQLite. Futu
8f4c0 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 re versions of S
8f4d0 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 QLite.** may add
8f4e0 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 additional stat
8f4f0 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 ic mutexes. Sta
8f500 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 tic mutexes are
8f510 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 for internal.**
8f520 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e use by SQLite on
8f530 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e ly. Application
8f540 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 s that use SQLit
8f550 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 e mutexes should
8f560 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 .** use only the
8f570 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 dynamic mutexes
8f580 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c returned by SQL
8f590 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f ITE_MUTEX_FAST o
8f5a0 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 r.** SQLITE_MUTE
8f5b0 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a X_RECURSIVE..**.
8f5c0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 ** Note that if
8f5d0 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d one of the dynam
8f5e0 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 ic mutex paramet
8f5f0 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 ers (SQLITE_MUTE
8f600 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c X_FAST.** or SQL
8f610 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
8f620 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 IVE) is used the
8f630 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f n sqlite3_mutex_
8f640 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 alloc().** retur
8f650 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d ns a different m
8f660 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 utex on every ca
8f670 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 ll. But for the
8f680 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 static .** mute
8f690 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d x types, the sam
8f6a0 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 e mutex is retur
8f6b0 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c ned on every cal
8f6c0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 l that has.** th
8f6d0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 e same type numb
8f6e0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 er..*/.static sq
8f6f0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 lite3_mutex *pth
8f700 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 readMutexAlloc(i
8f710 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61 nt iType){. sta
8f720 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tic sqlite3_mute
8f730 78 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b x staticMutexes[
8f740 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 ] = {. SQLITE
8f750 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
8f760 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 ZER,. SQLITE3
8f770 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
8f780 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f ER,. SQLITE3_
8f790 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
8f7a0 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d R,. SQLITE3_M
8f7b0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
8f7c0 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 ,. SQLITE3_MU
8f7d0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
8f7e0 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 . SQLITE3_MUT
8f7f0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 EX_INITIALIZER.
8f800 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 };. sqlite3_mu
8f810 74 65 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 tex *p;. switch
8f820 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 ( iType ){. c
8f830 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
8f840 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
8f850 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
8f860 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
8f870 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
8f880 66 28 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 f( p ){.#ifdef S
8f890 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f QLITE_HOMEGROWN_
8f8a0 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a RECURSIVE_MUTEX.
8f8b0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 /* If re
8f8c0 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 cursive mutexes
8f8d0 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c are not availabl
8f8e0 65 2c 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20 e, we will have
8f8f0 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 to. ** bu
8f900 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 ild our own. Se
8f910 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 e below. */.
8f920 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
8f930 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 x_init(&p->mutex
8f940 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 , 0);.#else.
8f950 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63 /* Use a rec
8f960 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 66 20 ursive mutex if
8f970 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 it is available
8f980 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 */. pthre
8f990 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72 ad_mutexattr_t r
8f9a0 65 63 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20 ecursiveAttr;.
8f9b0 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 pthread_mu
8f9c0 74 65 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65 texattr_init(&re
8f9d0 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 cursiveAttr);.
8f9e0 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 pthread_mu
8f9f0 74 65 78 61 74 74 72 5f 73 65 74 74 79 70 65 28 texattr_settype(
8fa00 26 72 65 63 75 72 73 69 76 65 41 74 74 72 2c 20 &recursiveAttr,
8fa10 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 PTHREAD_MUTEX_RE
8fa20 43 55 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 CURSIVE);.
8fa30 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
8fa40 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 init(&p->mutex,
8fa50 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b &recursiveAttr);
8fa60 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
8fa70 5f 6d 75 74 65 78 61 74 74 72 5f 64 65 73 74 72 _mutexattr_destr
8fa80 6f 79 28 26 72 65 63 75 72 73 69 76 65 41 74 74 oy(&recursiveAtt
8fa90 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 r);.#endif.
8faa0 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 p->id = iType
8fab0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
8fac0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
8fad0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 case SQLITE_MUT
8fae0 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 EX_FAST: {.
8faf0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
8fb00 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a ocZero( sizeof(*
8fb10 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 p) );. if(
8fb20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e p ){. p->
8fb30 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 id = iType;.
8fb40 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
8fb50 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 x_init(&p->mutex
8fb60 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
8fb70 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
8fb80 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
8fb90 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 assert( iT
8fba0 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 ype-2 >= 0 );.
8fbb0 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 assert( iTyp
8fbc0 65 2d 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28 e-2 < ArraySize(
8fbd0 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 staticMutexes) )
8fbe0 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 ;. p = &sta
8fbf0 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 ticMutexes[iType
8fc00 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 -2];. p->id
8fc10 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
8fc20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
8fc30 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
8fc40 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
8fc50 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
8fc60 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 a previously.**
8fc70 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e allocated mutex.
8fc80 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 SQLite is care
8fc90 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 ful to deallocat
8fca0 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 e every.** mutex
8fcb0 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 that it allocat
8fcc0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es..*/.static vo
8fcd0 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 id pthreadMutexF
8fce0 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ree(sqlite3_mute
8fcf0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 x *p){. assert(
8fd00 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 p->nRef==0 );.
8fd10 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d assert( p->id==
8fd20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
8fd30 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 T || p->id==SQLI
8fd40 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
8fd50 56 45 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f VE );. pthread_
8fd60 6d 75 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70 mutex_destroy(&p
8fd70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 ->mutex);. sqli
8fd80 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a te3_free(p);.}..
8fd90 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
8fda0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 3_mutex_enter()
8fdb0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
8fdc0 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 x_try() routines
8fdd0 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 attempt.** to e
8fde0 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 nter a mutex. I
8fdf0 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 f another thread
8fe00 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 is already with
8fe10 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a in the mutex,.**
8fe20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
8fe30 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 nter() will bloc
8fe40 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 k and sqlite3_mu
8fe50 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 tex_try() will r
8fe60 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
8fe70 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 BUSY. The sqlit
8fe80 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 e3_mutex_try() i
8fe90 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
8fea0 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 SQLITE_OK.** up
8feb0 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e on successful en
8fec0 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 try. Mutexes cr
8fed0 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 eated using SQLI
8fee0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
8fef0 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 VE can.** be ent
8ff00 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 ered multiple ti
8ff10 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 mes by the same
8ff20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 thread. In such
8ff30 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d cases the,.** m
8ff40 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 utex must be exi
8ff50 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d ted an equal num
8ff60 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 ber of times bef
8ff70 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 ore another thre
8ff80 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e ad.** can enter.
8ff90 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 If the same th
8ffa0 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e read tries to en
8ffb0 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 ter any other ki
8ffc0 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d nd of mutex.** m
8ffd0 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 ore than once, t
8ffe0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
8fff0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 ndefined..*/.sta
90000 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 tic void pthread
90010 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 MutexEnter(sqlit
90020 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
90030 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 assert( p->id==S
90040 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
90050 52 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 RSIVE || pthread
90060 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
90070 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
90080 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 E_HOMEGROWN_RECU
90090 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a RSIVE_MUTEX. /*
900a0 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 If recursive mu
900b0 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 texes are not av
900c0 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 ailable, then we
900d0 20 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 have to grow.
900e0 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 ** our own. Thi
900f0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
90100 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 assumes that pt
90110 68 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 hread_equal().
90120 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 ** is atomic - t
90130 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 hat it cannot be
90140 20 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 deceived into t
90150 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a hinking self. *
90160 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 * and p->owner a
90170 72 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f re equal if p->o
90180 77 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 wner changes bet
90190 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a ween two values.
901a0 20 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f ** that are no
901b0 74 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 t equal to self
901c0 77 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 while the compar
901d0 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 ison is taking p
901e0 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 lace.. ** This
901f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 implementation a
90200 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f lso assumes a co
90210 68 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 herent cache - t
90220 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 hat . ** separa
90230 74 65 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e te processes can
90240 6e 6f 74 20 72 65 61 64 20 64 69 66 66 65 72 65 not read differe
90250 6e 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 nt values from t
90260 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 he same. ** add
90270 72 65 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 ress at the same
90280 20 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 time. If eithe
90290 72 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 r of these two c
902a0 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 onditions. ** a
902b0 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e re not met, then
902c0 20 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c the mutexes wil
902d0 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c l fail and probl
902e0 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e ems will result.
902f0 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 . */. {. pt
90300 68 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 hread_t self = p
90310 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 thread_self();.
90320 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 if( p->nRef>0
90330 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 && pthread_equa
90340 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 l(p->owner, self
90350 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 ) ){. p->nR
90360 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ef++;. }else{
90370 0a 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d . pthread_m
90380 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 utex_lock(&p->mu
90390 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 tex);. asse
903a0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 rt( p->nRef==0 )
903b0 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 ;. p->owner
903c0 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 = self;. p
903d0 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 ->nRef = 1;.
903e0 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a }. }.#else. /*
903f0 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 Use the built-i
90400 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n recursive mute
90410 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20 xes if they are
90420 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a available.. */.
90430 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
90440 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b lock(&p->mutex);
90450 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 . p->owner = pt
90460 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 hread_self();.
90470 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 p->nRef++;.#endi
90480 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
90490 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e _DEBUG. if( p->
904a0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 trace ){. pri
904b0 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 ntf("enter mutex
904c0 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 %p (%d) with nR
904d0 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e ef=%d\n", p, p->
904e0 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b trace, p->nRef);
904f0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 . }.#endif.}.st
90500 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 atic int pthread
90510 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 MutexTry(sqlite3
90520 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e _mutex *p){. in
90530 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
90540 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
90550 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
90560 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 pthreadMutexNot
90570 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 held(p) );..#ifd
90580 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 ef SQLITE_HOMEGR
90590 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 OWN_RECURSIVE_MU
905a0 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 TEX. /* If recu
905b0 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 rsive mutexes ar
905c0 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c e not available,
905d0 20 74 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f then we have to
905e0 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f grow. ** our o
905f0 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d wn. This implem
90600 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 entation assumes
90610 20 74 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 that pthread_eq
90620 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 ual(). ** is at
90630 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 omic - that it c
90640 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 annot be deceive
90650 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 d into thinking
90660 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d self. ** and p-
90670 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c >owner are equal
90680 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 if p->owner cha
90690 6e 67 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f nges between two
906a0 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 values. ** tha
906b0 74 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 t are not equal
906c0 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 to self while th
906d0 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 e comparison is
906e0 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 taking place..
906f0 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e ** This implemen
90700 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 tation also assu
90710 6d 65 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 mes a coherent c
90720 61 63 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a ache - that . *
90730 2a 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 * separate proce
90740 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 sses cannot read
90750 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 different value
90760 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a s from the same.
90770 20 20 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 ** address at
90780 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 the same time.
90790 49 66 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 If either of the
907a0 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e se two condition
907b0 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d s. ** are not m
907c0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 et, then the mut
907d0 65 78 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 exes will fail a
907e0 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c nd problems will
907f0 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 result.. */.
90800 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 {. pthread_t
90810 73 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 self = pthread_s
90820 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 elf();. if( p
90830 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 ->nRef>0 && pthr
90840 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e ead_equal(p->own
90850 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 er, self) ){.
90860 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 p->nRef++;.
90870 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
90880 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 OK;. }else if
90890 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ( pthread_mutex_
908a0 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 trylock(&p->mute
908b0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 x)==0 ){. a
908c0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
908d0 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 0 );. p->ow
908e0 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 ner = self;.
908f0 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 p->nRef = 1;.
90900 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
90910 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a _OK;. }else{.
90920 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
90930 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 E_BUSY;. }.
90940 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 }.#else. /* Use
90950 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 the built-in re
90960 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 cursive mutexes
90970 69 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69 if they are avai
90980 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 lable.. */. if
90990 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ( pthread_mutex_
909a0 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 trylock(&p->mute
909b0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e x)==0 ){. p->
909c0 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f owner = pthread_
909d0 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e self();. p->n
909e0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
909f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
90a00 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c se{. rc = SQL
90a10 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 ITE_BUSY;. }.#e
90a20 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
90a30 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 ITE_DEBUG. if(
90a40 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
90a50 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 p->trace ){.
90a60 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d printf("enter m
90a70 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 utex %p (%d) wit
90a80 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c h nRef=%d\n", p,
90a90 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 p->trace, p->nR
90aa0 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ef);. }.#endif.
90ab0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
90ac0 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
90ad0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 3_mutex_leave()
90ae0 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 routine exits a
90af0 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a mutex that was.*
90b00 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 * previously ent
90b10 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 ered by the same
90b20 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 thread. The be
90b30 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 havior.** is und
90b40 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 efined if the mu
90b50 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 tex is not curre
90b60 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a ntly entered or.
90b70 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e ** is not curren
90b80 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 tly allocated.
90b90 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 SQLite will neve
90ba0 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a r do either..*/.
90bb0 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 static void pthr
90bc0 65 61 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71 eadMutexLeave(sq
90bd0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
90be0 0a 20 20 61 73 73 65 72 74 28 20 70 74 68 72 65 . assert( pthre
90bf0 61 64 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 adMutexHeld(p) )
90c00 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 ;. p->nRef--;.
90c10 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
90c20 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 ==0 || p->id==SQ
90c30 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
90c40 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20 SIVE );..#ifdef
90c50 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e SQLITE_HOMEGROWN
90c60 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 _RECURSIVE_MUTEX
90c70 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d . if( p->nRef==
90c80 30 20 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 0 ){. pthread
90c90 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 _mutex_unlock(&p
90ca0 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65 ->mutex);. }.#e
90cb0 6c 73 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 lse. pthread_mu
90cc0 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d tex_unlock(&p->m
90cd0 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 utex);.#endif..#
90ce0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
90cf0 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 UG. if( p->trac
90d00 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 e ){. printf(
90d10 22 6c 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 "leave mutex %p
90d20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 (%d) with nRef=%
90d30 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 d\n", p, p->trac
90d40 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d e, p->nRef);. }
90d50 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54 .#endif.}..SQLIT
90d60 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
90d70 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
90d80 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d *sqlite3DefaultM
90d90 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 utex(void){. st
90da0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 atic sqlite3_mut
90db0 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 ex_methods sMute
90dc0 78 20 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 x = {. pthrea
90dd0 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 dMutexInit,.
90de0 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c pthreadMutexEnd,
90df0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
90e00 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 xAlloc,. pthr
90e10 65 61 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 eadMutexFree,.
90e20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e pthreadMutexEn
90e30 74 65 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 ter,. pthread
90e40 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 MutexTry,. pt
90e50 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c hreadMutexLeave,
90e60 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
90e70 45 42 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 EBUG. pthread
90e80 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 MutexHeld,. p
90e90 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 threadMutexNothe
90ea0 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a ld.#else. 0,.
90eb0 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 0.#endif. }
90ec0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
90ed0 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f tex;.}..#endif /
90ee0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 * SQLITE_MUTEX_P
90ef0 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a THREAD */../****
90f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
90f10 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a f mutex_unix.c *
90f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
90f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
90f60 20 66 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e file mutex_w32.
90f70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
90f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
90fa0 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2007 August 14.
90fb0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
90fc0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
90fd0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
90fe0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
90ff0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
91000 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
91010 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
91020 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
91030 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
91040 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
91050 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
91060 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
91070 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
91080 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
91090 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
910a0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
910b0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
910c0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
910d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
910e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
910f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
91110 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
91120 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
91130 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
91140 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 ent mutexes for
91150 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a win32.**.** $Id:
91160 20 6d 75 74 65 78 5f 77 33 32 2e 63 2c 76 20 31 mutex_w32.c,v 1
91170 2e 31 35 20 32 30 30 39 2f 30 31 2f 33 30 20 31 .15 2009/01/30 1
91180 36 3a 30 39 3a 32 33 20 73 68 61 6e 65 20 45 78 6:09:23 shane Ex
91190 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 p $.*/../*.** Th
911a0 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 e code in this f
911b0 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ile is only used
911c0 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 if we are compi
911d0 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 ling multithread
911e0 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 ed.** on a win32
911f0 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 system..*/.#ifd
91200 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ef SQLITE_MUTEX_
91210 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 W32../*.** Each
91220 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 recursive mutex
91230 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
91240 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
91250 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 structure..*/.st
91260 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 ruct sqlite3_mut
91270 65 78 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f ex {. CRITICAL_
91280 53 45 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 SECTION mutex;
91290 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 /* Mutex contr
912a0 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 olling the lock
912b0 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 */. int id;
912c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
912d0 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f /* Mutex type */
912e0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
912f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
91300 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 Number of enter
91310 61 6e 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 ances */. DWORD
91320 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 owner;
91330 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 /* Thread
91340 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 holding this mut
91350 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ex */.};../*.**
91360 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e Return true (non
91370 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 -zero) if we are
91380 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 running under W
91390 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e inNT, Win2K, Win
913a0 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e XP,.** or WinCE.
913b0 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 Return false (
913c0 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c zero) for Win95,
913d0 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 Win98, or WinME
913e0 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 ..**.** Here is
913f0 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f an interesting o
91400 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e bservation: Win
91410 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 95, Win98, and W
91420 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 inME lack.** the
91430 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 LockFileEx() AP
91440 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 I. But we can s
91450 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 till statically
91460 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 link against tha
91470 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 t.** API as long
91480 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c as we don't cal
91490 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 l it win running
914a0 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 Win95/98/ME. A
914b0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 call to.** this
914c0 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
914d0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 to determine if
914e0 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e the host is Win
914f0 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 95/98/ME or.** W
91500 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 inNT/2K/XP so th
91510 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 at we will know
91520 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 whether or not w
91530 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c e can safely cal
91540 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c l.** the LockFil
91550 65 45 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a eEx() API..**.**
91560 20 6d 75 74 65 78 49 73 4e 54 28 29 20 69 73 20 mutexIsNT() is
91570 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68 only used for th
91580 65 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 e TryEnterCritic
91590 61 6c 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20 alSection() API
915a0 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69 call,.** which i
915b0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
915c0 20 69 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61 if your applica
915d0 74 69 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65 tion was compile
915e0 64 20 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33 d with .** _WIN3
915f0 32 5f 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20 2_WINNT defined
91600 74 6f 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78 to a value >= 0x
91610 30 34 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79 0400. Currently
91620 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61 , the only.** ca
91630 6c 6c 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72 ll to TryEnterCr
91640 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 iticalSection()
91650 69 73 20 23 69 66 64 65 66 27 65 64 20 6f 75 74 is #ifdef'ed out
91660 2c 20 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20 , so #ifdef .**
91670 74 68 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c this out as well
91680 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53 ..*/.#if 0.#if S
91690 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 QLITE_OS_WINCE.#
916a0 20 64 65 66 69 6e 65 20 6d 75 74 65 78 49 73 4e define mutexIsN
916b0 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 T() (1).#else.
916c0 20 73 74 61 74 69 63 20 69 6e 74 20 6d 75 74 65 static int mute
916d0 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 xIsNT(void){.
916e0 20 73 74 61 74 69 63 20 69 6e 74 20 6f 73 54 79 static int osTy
916f0 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 pe = 0;. if(
91700 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 osType==0 ){.
91710 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f OSVERSIONINFO
91720 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 sInfo;. sI
91730 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 nfo.dwOSVersionI
91740 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 nfoSize = sizeof
91750 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 (sInfo);. G
91760 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e etVersionEx(&sIn
91770 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 70 fo);. osTyp
91780 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 e = sInfo.dwPlat
91790 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 formId==VER_PLAT
917a0 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 FORM_WIN32_NT ?
917b0 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 2 : 1;. }.
917c0 20 72 65 74 75 72 6e 20 6f 73 54 79 70 65 3d 3d return osType==
917d0 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 2;. }.#endif /*
917e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
917f0 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 */.#endif..#ifd
91800 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
91810 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
91820 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 3_mutex_held() a
91830 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nd sqlite3_mutex
91840 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 _notheld() routi
91850 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 ne are.** intend
91860 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 ed for use only
91870 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
91880 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 statements..*/.s
91890 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 tatic int winMut
918a0 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d exHeld(sqlite3_m
918b0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
918c0 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 rn p->nRef!=0 &&
918d0 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 p->owner==GetCu
918e0 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b rrentThreadId();
918f0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 .}.static int wi
91900 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 nMutexNotheld(sq
91910 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
91920 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 . return p->nRe
91930 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 f==0 || p->owner
91940 21 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 !=GetCurrentThre
91950 61 64 49 64 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 adId();.}.#endif
91960 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c .../*.** Initial
91970 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 ize and deinitia
91980 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 lize the mutex s
91990 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 ubsystem..*/.sta
919a0 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 tic int winMutex
919b0 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 Init(void){ retu
919c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
919d0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 static int winMu
919e0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
919f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
91a00 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
91a10 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
91a20 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 () routine alloc
91a30 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 ates a new.** mu
91a40 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 tex and returns
91a50 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
91a60 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 If it returns
91a70 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 NULL.** that mea
91a80 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 ns that a mutex
91a90 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c could not be all
91aa0 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a ocated. SQLite.
91ab0 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 ** will unwind i
91ac0 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 ts stack and ret
91ad0 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 urn an error. T
91ae0 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 he argument.** t
91af0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f o sqlite3_mutex_
91b00 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f alloc() is one o
91b10 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 f these integer
91b20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a constants:.**.**
91b30 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 <ul>.** <li> S
91b40 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
91b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
91b60 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
91b70 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
91b80 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 3c 1.** <
91b90 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
91ba0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 X_STATIC_MASTER
91bb0 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 20 2.** <li>
91bc0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
91bd0 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 20 TIC_MEM
91be0 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 3.** <li> SQLIT
91bf0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 E_MUTEX_STATIC_P
91c00 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a 20 RNG 4.**
91c10 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 </ul>.**.** The
91c20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 first two consta
91c30 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 nts cause sqlite
91c40 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
91c50 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e to create.** a n
91c60 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e ew mutex. The n
91c70 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 ew mutex is recu
91c80 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 rsive when SQLIT
91c90 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
91ca0 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 E.** is used but
91cb0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 not necessarily
91cc0 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f so when SQLITE_
91cd0 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 MUTEX_FAST is us
91ce0 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ed..** The mutex
91cf0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
91d00 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
91d10 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 make a distinct
91d20 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 ion.** between S
91d30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
91d40 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 RSIVE and SQLITE
91d50 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 _MUTEX_FAST if i
91d60 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 t does.** not wa
91d70 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 nt to. But SQLi
91d80 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 te will only req
91d90 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65 uest a recursive
91da0 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 mutex in.** cas
91db0 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c es where it real
91dc0 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 ly needs one. I
91dd0 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 f a faster non-r
91de0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a ecursive mutex.*
91df0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
91e00 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e is available on
91e10 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f the host platfo
91e20 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 rm, the mutex su
91e30 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 bsystem.** might
91e40 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d return such a m
91e50 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 utex in response
91e60 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 to SQLITE_MUTEX
91e70 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 _FAST..**.** The
91e80 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 other allowed p
91e90 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c arameters to sql
91ea0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
91eb0 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a () each return.*
91ec0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * a pointer to a
91ed0 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 static preexist
91ee0 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65 ing mutex. Thre
91ef0 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 e static mutexes
91f00 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 are.** used by
91f10 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 the current vers
91f20 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ion of SQLite.
91f30 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
91f40 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 of SQLite.** may
91f50 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 add additional
91f60 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 static mutexes.
91f70 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 Static mutexes
91f80 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c are for internal
91f90 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 .** use by SQLit
91fa0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 e only. Applica
91fb0 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 tions that use S
91fc0 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 QLite mutexes sh
91fd0 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 ould.** use only
91fe0 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
91ff0 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 exes returned by
92000 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
92010 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f ST or.** SQLITE_
92020 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e MUTEX_RECURSIVE.
92030 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
92040 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 if one of the d
92050 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 ynamic mutex par
92060 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f ameters (SQLITE_
92070 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 MUTEX_FAST.** or
92080 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
92090 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 CURSIVE) is used
920a0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 then sqlite3_mu
920b0 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 tex_alloc().** r
920c0 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 eturns a differe
920d0 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 nt mutex on ever
920e0 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 y call. But for
920f0 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 the static .**
92100 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
92110 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
92120 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
92130 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
92140 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
92150 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
92160 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
92170 2a 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 28 69 *winMutexAlloc(i
92180 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c nt iType){. sql
92190 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a ite3_mutex *p;..
921a0 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65 20 switch( iType
921b0 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
921c0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 TE_MUTEX_FAST:.
921d0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
921e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 UTEX_RECURSIVE:
921f0 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 {. p = sqli
92200 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
92210 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 izeof(*p) );.
92220 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 if( p ){.
92230 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 p->id = iTyp
92240 65 3b 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 e;. Initi
92250 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 alizeCriticalSec
92260 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b tion(&p->mutex);
92270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
92280 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
92290 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
922a0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
922b0 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 mutex staticMute
922c0 78 65 73 5b 36 5d 3b 0a 20 20 20 20 20 20 73 74 xes[6];. st
922d0 61 74 69 63 20 69 6e 74 20 69 73 49 6e 69 74 20 atic int isInit
922e0 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 = 0;. while
922f0 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 ( !isInit ){.
92300 20 20 20 20 20 73 74 61 74 69 63 20 6c 6f 6e 67 static long
92310 20 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 lock = 0;.
92320 20 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b if( Interlock
92330 65 64 49 6e 63 72 65 6d 65 6e 74 28 26 6c 6f 63 edIncrement(&loc
92340 6b 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 k)==1 ){.
92350 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
92360 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 for(i=0; i<s
92370 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
92380 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 xes)/sizeof(stat
92390 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 69 icMutexes[0]); i
923a0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ++){.
923b0 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 InitializeCriti
923c0 63 61 6c 53 65 63 74 69 6f 6e 28 26 73 74 61 74 calSection(&stat
923d0 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 icMutexes[i].mut
923e0 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ex);. }
923f0 0a 20 20 20 20 20 20 20 20 20 20 69 73 49 6e 69 . isIni
92400 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d t = 1;. }
92410 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
92420 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 20 20 Sleep(1);.
92430 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
92440 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d assert( iType-
92450 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 2 >= 0 );.
92460 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 assert( iType-2
92470 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d < sizeof(staticM
92480 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 utexes)/sizeof(s
92490 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 taticMutexes[0])
924a0 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 );. p = &s
924b0 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 taticMutexes[iTy
924c0 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e pe-2];. p->
924d0 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 id = iType;.
924e0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
924f0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
92500 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f .../*.** This ro
92510 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 utine deallocate
92520 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a s a previously.*
92530 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 * allocated mute
92540 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 x. SQLite is ca
92550 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 reful to dealloc
92560 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 ate every.** mut
92570 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 ex that it alloc
92580 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ates..*/.static
92590 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 46 72 65 void winMutexFre
925a0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
925b0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
925c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
925d0 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 >nRef==0 );. as
925e0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
925f0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c ITE_MUTEX_FAST |
92600 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f | p->id==SQLITE_
92610 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
92620 29 3b 0a 20 20 44 65 6c 65 74 65 43 72 69 74 69 );. DeleteCriti
92630 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d calSection(&p->m
92640 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 utex);. sqlite3
92650 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a _free(p);.}../*.
92660 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
92670 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 utex_enter() and
92680 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
92690 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 ry() routines at
926a0 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 tempt.** to ente
926b0 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 r a mutex. If a
926c0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 nother thread is
926d0 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 already within
926e0 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 the mutex,.** sq
926f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
92700 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 r() will block a
92710 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nd sqlite3_mutex
92720 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 _try() will retu
92730 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 rn.** SQLITE_BUS
92740 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f Y. The sqlite3_
92750 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 mutex_try() inte
92760 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 rface returns SQ
92770 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 LITE_OK.** upon
92780 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 successful entry
92790 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 . Mutexes creat
927a0 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f ed using SQLITE_
927b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
927c0 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 can.** be entere
927d0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 d multiple times
927e0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 by the same thr
927f0 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 ead. In such ca
92800 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 ses the,.** mute
92810 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 x must be exited
92820 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 an equal number
92830 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 of times before
92840 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a another thread.
92850 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 ** can enter. I
92860 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 f the same threa
92870 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 d tries to enter
92880 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 any other kind
92890 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 of mutex.** more
928a0 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 than once, the
928b0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
928c0 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 fined..*/.static
928d0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 45 6e void winMutexEn
928e0 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ter(sqlite3_mute
928f0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 x *p){. assert(
92900 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
92910 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c UTEX_RECURSIVE |
92920 7c 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c | winMutexNothel
92930 64 28 70 29 20 29 3b 0a 20 20 45 6e 74 65 72 43 d(p) );. EnterC
92940 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 riticalSection(&
92950 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e p->mutex);. p->
92960 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 owner = GetCurre
92970 6e 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a 20 ntThreadId(); .
92980 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 p->nRef++;.}.st
92990 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 atic int winMute
929a0 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 xTry(sqlite3_mut
929b0 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 ex *p){. int rc
929c0 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
929d0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
929e0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
929f0 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 CURSIVE || winMu
92a00 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
92a10 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 73 . /*. ** The s
92a20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
92a30 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 76 65 () routine is ve
92a40 72 79 20 72 61 72 65 6c 79 20 75 73 65 64 2c 20 ry rarely used,
92a50 61 6e 64 20 77 68 65 6e 20 69 74 0a 20 20 2a 2a and when it. **
92a60 20 69 73 20 75 73 65 64 20 69 74 20 69 73 20 6d is used it is m
92a70 65 72 65 6c 79 20 61 6e 20 6f 70 74 69 6d 69 7a erely an optimiz
92a80 61 74 69 6f 6e 2e 20 20 53 6f 20 69 74 20 69 73 ation. So it is
92a90 20 4f 4b 20 66 6f 72 20 69 74 20 74 6f 20 61 6c OK for it to al
92aa0 77 61 79 73 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 ways. ** fail.
92ab0 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 . **. ** The
92ac0 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c TryEnterCritical
92ad0 53 65 63 74 69 6f 6e 28 29 20 69 6e 74 65 72 66 Section() interf
92ae0 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 ace is only avai
92af0 6c 61 62 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a lable on WinNT..
92b00 20 20 2a 2a 20 41 6e 64 20 73 6f 6d 65 20 77 69 ** And some wi
92b10 6e 64 6f 77 73 20 63 6f 6d 70 69 6c 65 72 73 20 ndows compilers
92b20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 79 6f 75 20 complain if you
92b30 74 72 79 20 74 6f 20 75 73 65 20 69 74 20 77 69 try to use it wi
92b40 74 68 6f 75 74 0a 20 20 2a 2a 20 66 69 72 73 74 thout. ** first
92b50 20 64 6f 69 6e 67 20 73 6f 6d 65 20 23 64 65 66 doing some #def
92b60 69 6e 65 73 20 74 68 61 74 20 70 72 65 76 65 6e ines that preven
92b70 74 20 53 51 4c 69 74 65 20 66 72 6f 6d 20 62 75 t SQLite from bu
92b80 69 6c 64 69 6e 67 20 6f 6e 20 57 69 6e 39 38 2e ilding on Win98.
92b90 0a 20 20 2a 2a 20 46 6f 72 20 74 68 61 74 20 72 . ** For that r
92ba0 65 61 73 6f 6e 2c 20 77 65 20 77 69 6c 6c 20 6f eason, we will o
92bb0 6d 69 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a mit this optimiz
92bc0 61 74 69 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20 20 ation for now.
92bd0 53 65 65 0a 20 20 2a 2a 20 74 69 63 6b 65 74 20 See. ** ticket
92be0 23 32 36 38 35 2e 0a 20 20 2a 2f 0a 23 69 66 20 #2685.. */.#if
92bf0 30 0a 20 20 69 66 28 20 6d 75 74 65 78 49 73 4e 0. if( mutexIsN
92c00 54 28 29 20 26 26 20 54 72 79 45 6e 74 65 72 43 T() && TryEnterC
92c10 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 riticalSection(&
92c20 70 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 p->mutex) ){.
92c30 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 p->owner = GetC
92c40 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 urrentThreadId()
92c50 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b ;. p->nRef++;
92c60 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
92c70 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 _OK;. }.#else.
92c80 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
92c90 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 R(p);.#endif. r
92ca0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
92cb0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
92cc0 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 utex_leave() rou
92cd0 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 tine exits a mut
92ce0 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 ex that was.** p
92cf0 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 reviously entere
92d00 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 d by the same th
92d10 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 read. The behav
92d20 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 ior.** is undefi
92d30 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 ned if the mutex
92d40 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
92d50 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 y entered or.**
92d60 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
92d70 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c allocated. SQL
92d80 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 ite will never d
92d90 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 o either..*/.sta
92da0 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 tic void winMute
92db0 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d xLeave(sqlite3_m
92dc0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 utex *p){. asse
92dd0 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
92de0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 . assert( p->ow
92df0 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 ner==GetCurrentT
92e00 68 72 65 61 64 49 64 28 29 20 29 3b 0a 20 20 70 hreadId() );. p
92e10 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 ->nRef--;. asse
92e20 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c rt( p->nRef==0 |
92e30 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f | p->id==SQLITE_
92e40 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
92e50 29 3b 0a 20 20 4c 65 61 76 65 43 72 69 74 69 63 );. LeaveCritic
92e60 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
92e70 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f tex);.}..SQLITE_
92e80 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
92e90 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 mutex_methods *s
92ea0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
92eb0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ex(void){. stat
92ec0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
92ed0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
92ee0 3d 20 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 = {. winMutex
92ef0 49 6e 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 Init,. winMut
92f00 65 78 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 exEnd,. winMu
92f10 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 texAlloc,. wi
92f20 6e 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 nMutexFree,.
92f30 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 winMutexEnter,.
92f40 20 20 20 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a winMutexTry,.
92f50 20 20 20 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 winMutexLeav
92f60 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 e,.#ifdef SQLITE
92f70 5f 44 45 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 _DEBUG. winMu
92f80 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e texHeld,. win
92f90 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c MutexNotheld.#el
92fa0 73 65 0a 20 20 20 20 30 2c 0a 20 20 20 20 30 0a se. 0,. 0.
92fb0 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 #endif. };.. r
92fc0 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d eturn &sMutex;.}
92fd0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
92fe0 45 5f 4d 55 54 45 58 5f 57 33 32 20 2a 2f 0a 0a E_MUTEX_W32 */..
92ff0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
93000 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 77 33 32 End of mutex_w32
93010 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
93020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
93040 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
93050 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f Begin file mallo
93060 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c.c ************
93070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
93090 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
930a0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
930b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
930c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
930d0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
930e0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
930f0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
93100 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
93110 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
93120 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
93130 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
93140 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
93150 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
93160 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
93170 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
93180 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
93190 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
931a0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
931b0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
931c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93200 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d ******.**.** Mem
93210 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
93220 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 unctions used th
93230 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e roughout sqlite.
93240 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c .**.** $Id: mall
93250 6f 63 2e 63 2c 76 20 31 2e 36 32 20 32 30 30 39 oc.c,v 1.62 2009
93260 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 34 20 /05/03 20:23:54
93270 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
93280 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
93290 20 72 75 6e 73 20 77 68 65 6e 20 74 68 65 20 6d runs when the m
932a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
932b0 73 65 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a sees that the.**
932c0 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c total memory al
932d0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 location is abou
932e0 74 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 t to exceed the
932f0 73 6f 66 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d soft heap.** lim
93300 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f it..*/.static vo
93310 69 64 20 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 id softHeapLimit
93320 45 6e 66 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 Enforcer(. void
93330 20 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 *NotUsed, . sq
93340 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55 lite3_int64 NotU
93350 73 65 64 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f sed2,. int allo
93360 63 53 69 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45 cSize.){. UNUSE
93370 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 D_PARAMETER2(Not
93380 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b Used, NotUsed2);
93390 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 . sqlite3_relea
933a0 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 se_memory(allocS
933b0 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ize);.}../*.** S
933c0 65 74 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 et the soft heap
933d0 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 -size limit for
933e0 74 68 65 20 6c 69 62 72 61 72 79 2e 20 50 61 73 the library. Pas
933f0 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f 72 20 0a sing a zero or .
93400 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 ** negative valu
93410 65 20 69 6e 64 69 63 61 74 65 73 20 6e 6f 20 6c e indicates no l
93420 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f imit..*/.SQLITE_
93430 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
93440 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 _soft_heap_limit
93450 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 (int n){. sqlit
93460 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 6d 69 74 e3_uint64 iLimit
93470 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 67 65 3b ;. int overage;
93480 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 . if( n<0 ){.
93490 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 iLimit = 0;.
934a0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 }else{. iLimi
934b0 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c t = n;. }. sql
934c0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
934d0 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e );. if( iLimit>
934e0 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
934f0 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74 MemoryAlarm(soft
93500 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 HeapLimitEnforce
93510 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 r, 0, iLimit);.
93520 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
93530 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 te3MemoryAlarm(0
93540 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f , 0, 0);. }. o
93550 76 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73 verage = (int)(s
93560 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 qlite3_memory_us
93570 65 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a ed() - (i64)n);.
93580 20 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20 if( overage>0
93590 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 ){. sqlite3_r
935a0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 elease_memory(ov
935b0 65 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f erage);. }.}../
935c0 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
935d0 72 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20 release up to n
935e0 62 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 bytes of non-ess
935f0 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75 ential memory cu
93600 72 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 rrently.** held
93610 62 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78 by SQLite. An ex
93620 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 ample of non-ess
93630 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73 ential memory is
93640 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a memory used to.
93650 2a 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 73 ** cache databas
93660 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65 e pages that are
93670 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69 not currently i
93680 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 n use..*/.SQLITE
93690 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
936a0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
936b0 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53 int n){.#ifdef S
936c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
936d0 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 ORY_MANAGEMENT.
936e0 20 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 23 int nRet = 0;.#
936f0 69 66 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20 73 if 0. nRet += s
93700 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 qlite3VdbeReleas
93710 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e 64 eMemory(n);.#end
93720 69 66 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c if. nRet += sql
93730 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
93740 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29 3b eMemory(n-nRet);
93750 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 3b 0a . return nRet;.
93760 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 #else. UNUSED_P
93770 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 72 ARAMETER(n);. r
93780 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
93790 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
937a0 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 State informati
937b0 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20 on local to the
937c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
937d0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a n subsystem..*/.
937e0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
937f0 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f D struct Mem0Glo
93800 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65 bal {. /* Numbe
93810 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 r of free pages
93820 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64 20 for scratch and
93830 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72 page-cache memor
93840 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61 y */. u32 nScra
93850 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e tchFree;. u32 n
93860 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c PageFree;.. sql
93870 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
93880 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 x; /* Mu
93890 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65 tex to serialize
938a0 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a access */.. /*
938b0 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20 . ** The alarm
938c0 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73 callback and its
938d0 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 arguments. The
938e0 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b mem0.mutex lock
938f0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 will. ** be he
93900 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c ld while the cal
93910 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 lback is running
93920 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c . Recursive cal
93930 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 ls into. ** the
93940 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 memory subsyste
93950 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 m are allowed, b
93960 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 ut no new callba
93970 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a cks will be. **
93980 20 69 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c issued. The al
93990 61 72 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65 armBusy variable
939a0 20 69 73 20 73 65 74 20 74 6f 20 70 72 65 76 65 is set to preve
939b0 6e 74 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a nt recursive. *
939c0 2a 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a * callbacks.. *
939d0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 /. sqlite3_int6
939e0 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 4 alarmThreshold
939f0 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d ;. void (*alarm
93a00 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c Callback)(void*,
93a10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 sqlite3_int64,i
93a20 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 nt);. void *ala
93a30 72 6d 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61 rmArg;. int ala
93a40 72 6d 42 75 73 79 3b 0a 0a 20 20 2f 2a 0a 20 20 rmBusy;.. /*.
93a50 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 ** Pointers to t
93a60 68 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 he end of sqlite
93a70 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 3GlobalConfig.pS
93a80 63 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 cratch and. **
93a90 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
93aa0 66 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62 fig.pPage to a b
93ab0 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 lock of memory t
93ac0 68 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a hat records. **
93ad0 20 77 68 69 63 68 20 70 61 67 65 73 20 61 72 65 which pages are
93ae0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f available.. */
93af0 0a 20 20 75 33 32 20 2a 61 53 63 72 61 74 63 68 . u32 *aScratch
93b00 46 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 Free;. u32 *aPa
93b10 67 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d geFree;.} mem0 =
93b20 20 7b 20 36 32 35 36 30 39 35 35 2c 20 30 2c 20 { 62560955, 0,
93b30 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
93b40 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 , 0 };..#define
93b50 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73 74 72 75 mem0 GLOBAL(stru
93b60 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c 20 6d ct Mem0Global, m
93b70 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 em0)../*.** Init
93b80 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
93b90 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
93ba0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 system..*/.SQLIT
93bb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
93bc0 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 lite3MallocInit(
93bd0 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c void){. if( sql
93be0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
93bf0 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b .m.xMalloc==0 ){
93c00 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 . sqlite3MemS
93c10 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d etDefault();. }
93c20 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c . memset(&mem0,
93c30 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 0, sizeof(mem0)
93c40 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
93c50 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f GlobalConfig.bCo
93c60 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 6d reMutex ){. m
93c70 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 em0.mutex = sqli
93c80 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
93c90 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
93ca0 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 C_MEM);. }. if
93cb0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
93cc0 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 26 onfig.pScratch &
93cd0 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 & sqlite3GlobalC
93ce0 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3e onfig.szScratch>
93cf0 3d 31 30 30 0a 20 20 20 20 20 20 26 26 20 73 71 =100. && sq
93d00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
93d10 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30 20 29 7b g.nScratch>=0 ){
93d20 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
93d30 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
93d40 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 fig.szScratch =
93d50 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74 ROUNDDOWN8(sqlit
93d60 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
93d70 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a 20 20 20 zScratch-4);.
93d80 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 mem0.aScratchFr
93d90 65 65 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 ee = (u32*)&((ch
93da0 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 ar*)sqlite3Globa
93db0 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 lConfig.pScratch
93dc0 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
93dd0 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62 [sqlite3Glob
93de0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
93df0 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ch*sqlite3Global
93e00 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 5d Config.nScratch]
93e10 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
93e20 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f <sqlite3GlobalCo
93e30 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 20 69 nfig.nScratch; i
93e40 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61 74 ++){ mem0.aScrat
93e50 63 68 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d chFree[i] = i; }
93e60 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 74 . mem0.nScrat
93e70 63 68 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 chFree = sqlite3
93e80 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 GlobalConfig.nSc
93e90 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a ratch;. }else{.
93ea0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
93eb0 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 lConfig.pScratch
93ec0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 0;. sqlite
93ed0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
93ee0 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 7d Scratch = 0;. }
93ef0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
93f00 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 obalConfig.pPage
93f10 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 && sqlite3Globa
93f20 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e 3d lConfig.szPage>=
93f30 35 31 32 0a 20 20 20 20 20 20 26 26 20 73 71 6c 512. && sql
93f40 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
93f50 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20 20 .nPage>=1 ){.
93f60 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 int i;. int
93f70 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 69 6e overhead;. in
93f80 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e t sz = ROUNDDOWN
93f90 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 8(sqlite3GlobalC
93fa0 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29 3b 0a 20 onfig.szPage);.
93fb0 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 int n = sqlit
93fc0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
93fd0 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72 68 65 Page;. overhe
93fe0 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a 20 2d ad = (4*n + sz -
93ff0 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71 6c 69 1)/sz;. sqli
94000 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
94010 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68 65 61 nPage -= overhea
94020 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50 61 67 d;. mem0.aPag
94030 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28 eFree = (u32*)&(
94040 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c (char*)sqlite3Gl
94050 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 obalConfig.pPage
94060 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
94070 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62 [sqlite3Glob
94080 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2a alConfig.szPage*
94090 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
940a0 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20 20 20 fig.nPage];.
940b0 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69 74 for(i=0; i<sqlit
940c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
940d0 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 Page; i++){ mem0
940e0 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20 3d 20 .aPageFree[i] =
940f0 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 50 i; }. mem0.nP
94100 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69 74 65 ageFree = sqlite
94110 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 3GlobalConfig.nP
94120 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 age;. }else{.
94130 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
94140 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 30 3b onfig.pPage = 0;
94150 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
94160 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 20 alConfig.szPage
94170 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
94180 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 n sqlite3GlobalC
94190 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28 73 71 onfig.m.xInit(sq
941a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
941b0 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a 7d g.m.pAppData);.}
941c0 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 ../*.** Deinitia
941d0 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 lize the memory
941e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
941f0 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f stem..*/.SQLITE_
94200 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
94210 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f ite3MallocEnd(vo
94220 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 id){. if( sqlit
94230 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
94240 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 .xShutdown ){.
94250 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
94260 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 onfig.m.xShutdow
94270 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 n(sqlite3GlobalC
94280 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 onfig.m.pAppData
94290 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 );. }. memset(
942a0 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 &mem0, 0, sizeof
942b0 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a (mem0));.}../*.*
942c0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f * Return the amo
942d0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75 unt of memory cu
942e0 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 rrently checked
942f0 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 out..*/.SQLITE_A
94300 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 PI sqlite3_int64
94310 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f sqlite3_memory_
94320 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e used(void){. in
94330 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 t n, mx;. sqlit
94340 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 e3_int64 res;.
94350 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53 sqlite3_status(S
94360 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
94370 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d ORY_USED, &n, &m
94380 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d 20 28 x, 0);. res = (
94390 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6e 3b sqlite3_int64)n;
943a0 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64 /* Work around
943b0 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20 bug in Borland
943c0 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20 C. Ticket #3216
943d0 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b */. return res;
943e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
943f0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f the maximum amo
94400 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 unt of memory th
94410 61 74 20 68 61 73 20 65 76 65 72 20 62 65 65 6e at has ever been
94420 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 20 .** checked out
94430 73 69 6e 63 65 20 65 69 74 68 65 72 20 74 68 65 since either the
94440 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
94450 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f 72 is process.** or
94460 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 since the most
94470 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a 2f recent reset..*/
94480 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
94490 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
944a0 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 3_memory_highwat
944b0 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 er(int resetFlag
944c0 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a ){. int n, mx;.
944d0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
944e0 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 res;. sqlite3_s
944f0 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 tatus(SQLITE_STA
94500 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c TUS_MEMORY_USED,
94510 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65 74 46 &n, &mx, resetF
94520 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d 20 28 73 lag);. res = (s
94530 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d 78 3b qlite3_int64)mx;
94540 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64 /* Work around
94550 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20 bug in Borland
94560 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20 C. Ticket #3216
94570 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b */. return res;
94580 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
94590 20 74 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 the alarm callb
945a0 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ack.*/.SQLITE_PR
945b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
945c0 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 0a 20 20 3MemoryAlarm(.
945d0 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 void(*xCallback)
945e0 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c (void *pArg, sql
945f0 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c ite3_int64 used,
94600 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a int N),. void *
94610 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f pArg,. sqlite3_
94620 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 int64 iThreshold
94630 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 .){. sqlite3_mu
94640 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
94650 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c utex);. mem0.al
94660 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 armCallback = xC
94670 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e allback;. mem0.
94680 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67 3b alarmArg = pArg;
94690 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 . mem0.alarmThr
946a0 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65 73 68 eshold = iThresh
946b0 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d old;. sqlite3_m
946c0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
946d0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
946e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
946f0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
94700 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a IT_DEPRECATED./*
94710 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20 65 .** Deprecated e
94720 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 xternal interfac
94730 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 e. Internal/cor
94740 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a 2a e SQLite code.**
94750 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c should call sql
94760 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 2e ite3MemoryAlarm.
94770 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
94780 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 nt sqlite3_memor
94790 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 y_alarm(. void(
947a0 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 *xCallback)(void
947b0 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f *pArg, sqlite3_
947c0 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20 4e int64 used,int N
947d0 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c ),. void *pArg,
947e0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
947f0 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 iThreshold.){.
94800 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d return sqlite3M
94810 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 61 6c 6c emoryAlarm(xCall
94820 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 54 68 72 back, pArg, iThr
94830 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 6e 64 69 eshold);.}.#endi
94840 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72 f../*.** Trigger
94850 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73 the alarm .*/.s
94860 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
94870 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 69 6e e3MallocAlarm(in
94880 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64 t nByte){. void
94890 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f (*xCallback)(vo
948a0 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 id*,sqlite3_int6
948b0 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 4,int);. sqlite
948c0 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b 3_int64 nowUsed;
948d0 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 . void *pArg;.
948e0 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 if( mem0.alarmC
948f0 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c 7c 20 6d 65 allback==0 || me
94900 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 20 29 20 m0.alarmBusy )
94910 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 30 2e 61 return;. mem0.a
94920 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b 0a 20 20 larmBusy = 1;.
94930 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 30 xCallback = mem0
94940 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a .alarmCallback;.
94950 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 71 6c 69 nowUsed = sqli
94960 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 te3StatusValue(S
94970 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
94980 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 70 41 72 ORY_USED);. pAr
94990 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 g = mem0.alarmAr
949a0 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 g;. sqlite3_mut
949b0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
949c0 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c 62 61 63 tex);. xCallbac
949d0 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 65 64 2c k(pArg, nowUsed,
949e0 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 6c 69 74 nByte);. sqlit
949f0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
94a00 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 em0.mutex);. me
94a10 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 30 m0.alarmBusy = 0
94a20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 ;.}../*.** Do a
94a30 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
94a40 6e 20 77 69 74 68 20 73 74 61 74 69 73 74 69 63 n with statistic
94a50 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20 41 s and alarms. A
94a60 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 ssume the.** loc
94a70 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c k is already hel
94a80 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
94a90 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d mallocWithAlarm
94aa0 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a 70 (int n, void **p
94ab0 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 3b p){. int nFull;
94ac0 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 . void *p;. as
94ad0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
94ae0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d 75 tex_held(mem0.mu
94af0 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c 20 tex) );. nFull
94b00 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
94b10 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 onfig.m.xRoundup
94b20 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 (n);. sqlite3St
94b30 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
94b40 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a TATUS_MALLOC_SIZ
94b50 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65 6d E, n);. if( mem
94b60 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 21 0.alarmCallback!
94b70 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 55 =0 ){. int nU
94b80 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 sed = sqlite3Sta
94b90 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f tusValue(SQLITE_
94ba0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
94bb0 45 44 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 73 ED);. if( nUs
94bc0 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30 ed+nFull >= mem0
94bd0 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 .alarmThreshold
94be0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
94bf0 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c MallocAlarm(nFul
94c00 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 l);. }. }.
94c10 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 p = sqlite3Globa
94c20 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f lConfig.m.xMallo
94c30 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28 20 c(nFull);. if(
94c40 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 p==0 && mem0.ala
94c50 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 rmCallback ){.
94c60 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 sqlite3MallocA
94c70 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 larm(nFull);.
94c80 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 p = sqlite3Glob
94c90 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c alConfig.m.xMall
94ca0 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 oc(nFull);. }.
94cb0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 46 if( p ){. nF
94cc0 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ull = sqlite3Mal
94cd0 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 locSize(p);.
94ce0 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
94cf0 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
94d00 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75 6c EMORY_USED, nFul
94d10 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 l);. }. *pp =
94d20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 6c p;. return nFul
94d30 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f l;.}../*.** Allo
94d40 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54 68 cate memory. Th
94d50 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6c 69 is routine is li
94d60 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ke sqlite3_mallo
94d70 63 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 c() except that
94d80 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 it.** assumes th
94d90 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 e memory subsyst
94da0 65 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 em has already b
94db0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e een initialized.
94dc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
94dd0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
94de0 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 Malloc(int n){.
94df0 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 void *p;. if(
94e00 6e 3c 3d 30 20 7c 7c 20 4e 45 56 45 52 28 6e 3e n<=0 || NEVER(n>
94e10 3d 30 78 37 66 66 66 66 66 30 30 29 20 29 7b 0a =0x7fffff00) ){.
94e20 20 20 20 20 2f 2a 20 54 68 65 20 4e 45 56 45 52 /* The NEVER
94e30 28 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 29 20 (n>=0x7fffff00)
94e40 74 65 72 6d 20 69 73 20 61 64 64 65 64 20 6f 75 term is added ou
94e50 74 20 6f 66 20 70 61 72 61 6e 6f 69 61 2e 20 20 t of paranoia.
94e60 57 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 0a We want to make.
94e70 20 20 20 20 2a 2a 20 61 62 73 6f 6c 75 74 65 6c ** absolutel
94e80 79 20 73 75 72 65 20 74 68 61 74 20 74 68 65 72 y sure that ther
94e90 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 77 69 74 e is nothing wit
94ea0 68 69 6e 20 53 51 4c 69 74 65 20 74 68 61 74 20 hin SQLite that
94eb0 63 61 6e 20 63 61 75 73 65 20 61 0a 20 20 20 20 can cause a.
94ec0 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** memory alloca
94ed0 74 69 6f 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72 tion of a number
94ee0 20 6f 66 20 62 79 74 65 73 20 77 68 69 63 68 20 of bytes which
94ef0 69 73 20 6e 65 61 72 20 74 68 65 20 6d 61 78 69 is near the maxi
94f00 6d 75 6d 20 73 69 67 6e 65 64 0a 20 20 20 20 2a mum signed. *
94f10 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 * integer value
94f20 61 6e 64 20 74 68 75 73 20 63 61 75 73 65 20 61 and thus cause a
94f30 6e 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c n integer overfl
94f40 6f 77 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 ow inside of the
94f50 20 78 4d 61 6c 6c 6f 63 28 29 0a 20 20 20 20 2a xMalloc(). *
94f60 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
94f70 2e 20 20 54 68 65 20 6e 3e 3d 30 78 37 66 66 66 . The n>=0x7fff
94f80 66 66 30 30 20 67 69 76 65 73 20 75 73 20 32 35 ff00 gives us 25
94f90 35 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 72 5 bytes of headr
94fa0 6f 6f 6d 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a oom. The. **
94fb0 20 74 65 73 74 20 73 68 6f 75 6c 64 20 6e 65 76 test should nev
94fc0 65 72 20 62 65 20 74 72 75 65 20 62 65 63 61 75 er be true becau
94fd0 73 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 se SQLITE_MAX_LE
94fe0 4e 47 54 48 20 73 68 6f 75 6c 64 20 62 65 20 6d NGTH should be m
94ff0 75 63 68 0a 20 20 20 20 2a 2a 20 6c 65 73 73 20 uch. ** less
95000 74 68 61 6e 20 30 78 37 66 66 66 66 66 30 30 20 than 0x7fffff00
95010 61 6e 64 20 69 74 20 73 68 6f 75 6c 64 20 63 61 and it should ca
95020 74 63 68 20 6c 61 72 67 65 20 6d 65 6d 6f 72 79 tch large memory
95030 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 20 allocations.
95040 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 79 20 ** before they
95050 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 reach this point
95060 2e 20 2a 2f 0a 20 20 20 20 70 20 3d 20 30 3b 0a . */. p = 0;.
95070 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 }else if( sqli
95080 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
95090 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 bMemstat ){.
950a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
950b0 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ter(mem0.mutex);
950c0 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 69 74 68 41 . mallocWithA
950d0 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 larm(n, &p);.
950e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
950f0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 eave(mem0.mutex)
95100 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
95110 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
95120 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 Config.m.xMalloc
95130 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 (n);. }. retur
95140 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 n p;.}../*.** Th
95150 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
95160 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
95170 69 6f 6e 20 69 73 20 66 6f 72 20 75 73 65 20 62 ion is for use b
95180 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f y the applicatio
95190 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d 61 6b 65 n..** First make
951a0 20 73 75 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 sure the memory
951b0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e subsystem is in
951c0 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20 itialized, then
951d0 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 do the.** alloca
951e0 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
951f0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 API void *sqlite
95200 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 3_malloc(int n){
95210 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
95220 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
95230 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 if( sqlite3_init
95240 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 ialize() ) retur
95250 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 n 0;.#endif. re
95260 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c turn sqlite3Mall
95270 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 oc(n);.}../*.**
95280 45 61 63 68 20 74 68 72 65 61 64 20 6d 61 79 20 Each thread may
95290 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 only have a sing
952a0 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 le outstanding a
952b0 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a llocation from.*
952c0 2a 20 78 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 * xScratchMalloc
952d0 28 29 2e 20 20 57 65 20 76 65 72 69 66 79 20 74 (). We verify t
952e0 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 his constraint i
952f0 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 n the single-thr
95300 65 61 64 65 64 0a 2a 2a 20 63 61 73 65 20 62 79 eaded.** case by
95310 20 73 65 74 74 69 6e 67 20 73 63 72 61 74 63 68 setting scratch
95320 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31 20 77 68 AllocOut to 1 wh
95330 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e en an allocation
95340 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69 .** is outstandi
95350 6e 67 20 63 6c 65 61 72 69 6e 67 20 69 74 20 77 ng clearing it w
95360 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 hen the allocati
95370 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a 2f 0a on is freed..*/.
95380 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
95390 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 DSAFE==0 && !def
953a0 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 73 74 61 ined(NDEBUG).sta
953b0 74 69 63 20 69 6e 74 20 73 63 72 61 74 63 68 41 tic int scratchA
953c0 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 65 6e llocOut = 0;.#en
953d0 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f dif.../*.** Allo
953e0 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 cate memory that
953f0 20 69 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 is to be used a
95400 6e 64 20 72 65 6c 65 61 73 65 64 20 72 69 67 68 nd released righ
95410 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68 69 73 20 t away..** This
95420 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c routine is simil
95430 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28 29 20 69 ar to alloca() i
95440 6e 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 n that it is not
95450 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20 66 6f 72 intended.** for
95460 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72 situations wher
95470 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 69 67 e the memory mig
95480 68 74 20 62 65 20 68 65 6c 64 20 6c 6f 6e 67 2d ht be held long-
95490 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a 2a 20 72 term. This.** r
954a0 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 outine is intend
954b0 65 64 20 74 6f 20 67 65 74 20 6d 65 6d 6f 72 79 ed to get memory
954c0 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65 20 74 72 to old large tr
954d0 61 6e 73 69 65 6e 74 20 64 61 74 61 0a 2a 2a 20 ansient data.**
954e0 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 structures that
954f0 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72 6d 61 6c would not normal
95500 6c 79 20 66 69 74 20 6f 6e 20 74 68 65 20 73 74 ly fit on the st
95510 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20 65 6d 62 ack of an.** emb
95520 65 64 64 65 64 20 70 72 6f 63 65 73 73 6f 72 2e edded processor.
95530 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
95540 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
95550 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e ScratchMalloc(in
95560 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b t n){. void *p;
95570 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 . assert( n>0 )
95580 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 ;..#if SQLITE_TH
95590 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 READSAFE==0 && !
955a0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a defined(NDEBUG).
955b0 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 /* Verify that
955c0 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e no more than on
955d0 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 e scratch alloca
955e0 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61 64 0a tion per thread.
955f0 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 ** is outstand
95600 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e ing at one time.
95610 20 20 28 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 (This is only
95620 63 68 65 63 6b 65 64 20 69 6e 20 74 68 65 0a 20 checked in the.
95630 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 ** single-threa
95640 64 65 64 20 63 61 73 65 20 73 69 6e 63 65 20 63 ded case since c
95650 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d hecking in the m
95660 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 63 61 ulti-threaded ca
95670 73 65 0a 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 se. ** would be
95680 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c much more compl
95690 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 61 73 icated.) */. as
956a0 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c sert( scratchAll
956b0 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a 23 65 6e 64 ocOut==0 );.#end
956c0 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 if.. if( sqlite
956d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
956e0 53 63 72 61 74 63 68 3c 6e 20 29 7b 0a 20 20 20 Scratch<n ){.
956f0 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f 76 goto scratch_ov
95700 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b erflow;. }else{
95710 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
95720 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e utex_enter(mem0.
95730 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 mutex);. if(
95740 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 mem0.nScratchFre
95750 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 e==0 ){. sq
95760 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
95770 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
95780 20 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63 goto scratc
95790 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 h_overflow;.
957a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 }else{. int
957b0 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 6d 65 i;. i = me
957c0 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b m0.aScratchFree[
957d0 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 --mem0.nScratchF
957e0 72 65 65 5d 3b 0a 20 20 20 20 20 20 69 20 2a 3d ree];. i *=
957f0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
95800 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3b 0a nfig.szScratch;.
95810 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 sqlite3Sta
95820 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
95830 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 ATUS_SCRATCH_USE
95840 44 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c D, 1);. sql
95850 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 ite3StatusSet(SQ
95860 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
95870 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 TCH_SIZE, n);.
95880 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
95890 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
958a0 65 78 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 ex);. p = (
958b0 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 73 void*)&((char*)s
958c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
958d0 69 67 2e 70 53 63 72 61 74 63 68 29 5b 69 5d 3b ig.pScratch)[i];
958e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 20 . assert(
958f0 28 28 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 (((u8*)p - (u8*)
95900 30 29 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 0) & 7)==0 );.
95910 20 20 7d 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 }. }.#if SQLI
95920 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 TE_THREADSAFE==0
95930 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 && !defined(NDE
95940 42 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c BUG). scratchAl
95950 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 locOut = p!=0;.#
95960 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 endif.. return
95970 70 3b 0a 0a 73 63 72 61 74 63 68 5f 6f 76 65 72 p;..scratch_over
95980 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73 71 6c 69 flow:. if( sqli
95990 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
959a0 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 bMemstat ){.
959b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
959c0 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ter(mem0.mutex);
959d0 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 . sqlite3Stat
959e0 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 usSet(SQLITE_STA
959f0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 TUS_SCRATCH_SIZE
95a00 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d 20 6d 61 , n);. n = ma
95a10 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c llocWithAlarm(n,
95a20 20 26 70 29 3b 0a 20 20 20 20 69 66 28 20 70 20 &p);. if( p
95a30 29 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 ) sqlite3StatusA
95a40 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
95a50 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f _SCRATCH_OVERFLO
95a60 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 W, n);. sqlit
95a70 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
95a80 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 em0.mutex);. }e
95a90 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c lse{. p = sql
95aa0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
95ab0 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 .m.xMalloc(n);.
95ac0 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 }.#if SQLITE_TH
95ad0 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 READSAFE==0 && !
95ae0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a defined(NDEBUG).
95af0 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 scratchAllocOu
95b00 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 t = p!=0;.#endif
95b10 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 20 20 20 . return p;
95b20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
95b30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 E void sqlite3Sc
95b40 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 20 2a ratchFree(void *
95b50 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 0a p){. if( p ){..
95b60 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
95b70 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 DSAFE==0 && !def
95b80 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 20 ined(NDEBUG).
95b90 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 /* Verify that
95ba0 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 no more than one
95bb0 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 scratch allocat
95bc0 69 6f 6e 20 70 65 72 20 74 68 72 65 61 64 0a 20 ion per thread.
95bd0 20 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e ** is outstan
95be0 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 ding at one time
95bf0 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e 6c 79 . (This is only
95c00 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68 65 0a checked in the.
95c10 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 ** single-th
95c20 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e 63 readed case sinc
95c30 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 e checking in th
95c40 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 e multi-threaded
95c50 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 77 6f 75 case. ** wou
95c60 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 ld be much more
95c70 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f complicated.) */
95c80 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 63 72 . assert( scr
95c90 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 31 20 atchAllocOut==1
95ca0 29 3b 0a 20 20 20 20 73 63 72 61 74 63 68 41 6c );. scratchAl
95cb0 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64 locOut = 0;.#end
95cc0 69 66 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 if.. if( sqli
95cd0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
95ce0 70 53 63 72 61 74 63 68 3d 3d 30 0a 20 20 20 20 pScratch==0.
95cf0 20 20 20 20 20 20 20 7c 7c 20 70 3c 73 71 6c 69 || p<sqli
95d00 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
95d10 70 53 63 72 61 74 63 68 0a 20 20 20 20 20 20 20 pScratch.
95d20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a || p>=(void*
95d30 29 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 )mem0.aScratchFr
95d40 65 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ee ){. if(
95d50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
95d60 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a fig.bMemstat ){.
95d70 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 69 7a int iSiz
95d80 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f e = sqlite3Mallo
95d90 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 cSize(p);.
95da0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
95db0 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 enter(mem0.mutex
95dc0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
95dd0 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 e3StatusAdd(SQLI
95de0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
95df0 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 H_OVERFLOW, -iSi
95e00 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ze);. sql
95e10 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
95e20 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
95e30 52 59 5f 55 53 45 44 2c 20 2d 69 53 69 7a 65 29 RY_USED, -iSize)
95e40 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
95e50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
95e60 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 xFree(p);.
95e70 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
95e80 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
95e90 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
95ea0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 sqlite3G
95eb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 lobalConfig.m.xF
95ec0 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a ree(p);. }.
95ed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
95ee0 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 int i;. i
95ef0 3d 20 28 69 6e 74 29 28 28 75 38 2a 29 70 20 2d = (int)((u8*)p -
95f00 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f (u8*)sqlite3Glo
95f10 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 balConfig.pScrat
95f20 63 68 29 3b 0a 20 20 20 20 20 20 69 20 2f 3d 20 ch);. i /=
95f30 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
95f40 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20 fig.szScratch;.
95f50 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d assert( i>=
95f60 30 20 26 26 20 69 3c 73 71 6c 69 74 65 33 47 6c 0 && i<sqlite3Gl
95f70 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
95f80 74 63 68 20 29 3b 0a 20 20 20 20 20 20 73 71 6c tch );. sql
95f90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
95fa0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
95fb0 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 30 assert( mem0
95fc0 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3c 28 75 .nScratchFree<(u
95fd0 33 32 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 32)sqlite3Global
95fe0 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 Config.nScratch
95ff0 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 61 53 );. mem0.aS
96000 63 72 61 74 63 68 46 72 65 65 5b 6d 65 6d 30 2e cratchFree[mem0.
96010 6e 53 63 72 61 74 63 68 46 72 65 65 2b 2b 5d 20 nScratchFree++]
96020 3d 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = i;. sqlit
96030 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 e3StatusAdd(SQLI
96040 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
96050 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 H_USED, -1);.
96060 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
96070 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
96080 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a x);. }. }.}.
96090 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69 66 20 70 ./*.** TRUE if p
960a0 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 is a lookaside
960b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
960c0 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66 n from db.*/.#if
960d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
960e0 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74 61 74 69 _LOOKASIDE.stati
960f0 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61 73 69 64 c int isLookasid
96100 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 e(sqlite3 *db, v
96110 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 oid *p){. retur
96120 6e 20 64 62 20 26 26 20 70 20 26 26 20 70 3e 3d n db && p && p>=
96130 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 db->lookaside.pS
96140 74 61 72 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f tart && p<db->lo
96150 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a okaside.pEnd;.}.
96160 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 69 73 #else.#define is
96170 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42 29 20 30 Lookaside(A,B) 0
96180 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
96190 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f eturn the size o
961a0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
961b0 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 ation previously
961c0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
961d0 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 * sqlite3Malloc(
961e0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c ) or sqlite3_mal
961f0 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 loc()..*/.SQLITE
96200 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
96210 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 ite3MallocSize(v
96220 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 oid *p){. retur
96230 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 n sqlite3GlobalC
96240 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 onfig.m.xSize(p)
96250 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
96260 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 62 TE int sqlite3Db
96270 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 MallocSize(sqlit
96280 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 e3 *db, void *p)
96290 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d {. assert( db==
962a0 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
962b0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
962c0 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 x) );. if( p==0
962d0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
962e0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 ;. }else if( is
962f0 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 Lookaside(db, p)
96300 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 ){. return d
96310 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 3b b->lookaside.sz;
96320 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
96330 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
96340 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 alConfig.m.xSize
96350 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
96360 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70 72 * Free memory pr
96370 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 eviously obtaine
96380 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 d from sqlite3Ma
96390 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 lloc()..*/.SQLIT
963a0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
963b0 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 29 e3_free(void *p)
963c0 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
963d0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c eturn;. if( sql
963e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
963f0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
96400 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
96410 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
96420 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
96430 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
96440 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
96450 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 , -sqlite3Malloc
96460 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73 71 Size(p));. sq
96470 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
96480 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 g.m.xFree(p);.
96490 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
964a0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
964b0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
964c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
964d0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
964e0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 }.}../*.** Fre
964f0 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 69 e memory that mi
96500 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 74 65 ght be associate
96510 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 d with a particu
96520 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 lar database.**
96530 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 connection..*/.S
96540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
96550 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 id sqlite3DbFree
96560 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f (sqlite3 *db, vo
96570 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 id *p){. assert
96580 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ( db==0 || sqlit
96590 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
965a0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
965b0 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 ( isLookaside(db
965c0 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b , p) ){. Look
965d0 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 20 asideSlot *pBuf
965e0 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 = (LookasideSlot
965f0 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e 70 *)p;. pBuf->p
96600 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 Next = db->looka
96610 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20 side.pFree;.
96620 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 db->lookaside.pF
96630 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 ree = pBuf;.
96640 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f db->lookaside.nO
96650 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ut--;. }else{.
96660 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
96670 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
96680 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 Change the size
96690 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 of an existing
966a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
966b0 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 n.*/.SQLITE_PRIV
966c0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
966d0 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 3Realloc(void *p
966e0 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 Old, int nBytes)
966f0 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e 4e {. int nOld, nN
96700 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 ew;. void *pNew
96710 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 20 ;. if( pOld==0
96720 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 ){. return sq
96730 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 lite3Malloc(nByt
96740 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e es);. }. if( n
96750 42 79 74 65 73 3c 3d 30 20 7c 7c 20 4e 45 56 45 Bytes<=0 || NEVE
96760 52 28 6e 42 79 74 65 73 3e 3d 30 78 37 66 66 66 R(nBytes>=0x7fff
96770 66 66 30 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 ff00) ){. /*
96780 54 68 65 20 4e 45 56 45 52 28 2e 2e 2e 29 20 74 The NEVER(...) t
96790 65 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 erm is explained
967a0 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 in comments on
967b0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 sqlite3Malloc()
967c0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 */. sqlite3_f
967d0 72 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72 ree(pOld);. r
967e0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
967f0 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Old = sqlite3Mal
96800 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 locSize(pOld);.
96810 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
96820 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 alConfig.bMemsta
96830 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
96840 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
96850 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 0.mutex);. sq
96860 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 lite3StatusSet(S
96870 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c QLITE_STATUS_MAL
96880 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 79 74 65 73 LOC_SIZE, nBytes
96890 29 3b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 );. nNew = sq
968a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
968b0 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42 79 g.m.xRoundup(nBy
968c0 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 6e 4f tes);. if( nO
968d0 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 ld==nNew ){.
968e0 20 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 pNew = pOld;.
968f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
96900 69 66 28 20 73 71 6c 69 74 65 33 53 74 61 74 75 if( sqlite3Statu
96910 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 sValue(SQLITE_ST
96920 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
96930 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a )+nNew-nOld >= .
96940 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 30 mem0
96950 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 .alarmThreshold
96960 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
96970 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e e3MallocAlarm(nN
96980 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 20 ew-nOld);.
96990 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 }. pNew = s
969a0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
969b0 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f ig.m.xRealloc(pO
969c0 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 ld, nNew);.
969d0 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20 if( pNew==0 &&
969e0 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
969f0 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ck ){. sq
96a00 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d lite3MallocAlarm
96a10 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 (nBytes);.
96a20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
96a30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
96a40 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e Realloc(pOld, nN
96a50 65 77 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ew);. }.
96a60 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 if( pNew ){.
96a70 20 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 nNew = sq
96a80 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
96a90 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 73 pNew);. s
96aa0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
96ab0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
96ac0 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d MORY_USED, nNew-
96ad0 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 nOld);. }.
96ae0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
96af0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
96b00 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 0.mutex);. }els
96b10 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 e{. pNew = sq
96b20 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
96b30 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c g.m.xRealloc(pOl
96b40 64 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a d, nBytes);. }.
96b50 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
96b60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c ../*.** The publ
96b70 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 ic interface to
96b80 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20 sqlite3Realloc.
96b90 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 Make sure that
96ba0 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75 the memory.** su
96bb0 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 bsystem is initi
96bc0 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 alized prior to
96bd0 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52 invoking sqliteR
96be0 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54 ealloc..*/.SQLIT
96bf0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
96c00 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 te3_realloc(void
96c10 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a *pOld, int n){.
96c20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
96c30 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 MIT_AUTOINIT. i
96c40 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 f( sqlite3_initi
96c50 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e alize() ) return
96c60 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 0;.#endif. ret
96c70 75 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c 6c urn sqlite3Reall
96c80 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a oc(pOld, n);.}..
96c90 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
96ca0 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e and zero memory.
96cb0 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/ .SQLITE_PRIV
96cc0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
96cd0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20 3MallocZero(int
96ce0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 n){. void *p =
96cf0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 sqlite3Malloc(n)
96d00 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
96d10 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 memset(p, 0, n)
96d20 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
96d30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
96d40 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d ate and zero mem
96d50 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c ory. If the all
96d60 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d ocation fails, m
96d70 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f ake.** the mallo
96d80 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 cFailed flag in
96d90 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 the connection p
96da0 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 ointer..*/.SQLIT
96db0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
96dc0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
96dd0 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ero(sqlite3 *db,
96de0 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 int n){. void
96df0 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 *p = sqlite3DbMa
96e00 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a llocRaw(db, n);.
96e10 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d if( p ){. m
96e20 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a emset(p, 0, n);.
96e30 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
96e40 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
96e50 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 e and zero memor
96e60 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 y. If the alloc
96e70 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b ation fails, mak
96e80 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 e.** the mallocF
96e90 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 ailed flag in th
96ea0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 e connection poi
96eb0 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 nter..**.** If d
96ec0 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c b!=0 and db->mal
96ed0 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 75 locFailed is tru
96ee0 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 20 e (indicating a
96ef0 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 prior malloc.**
96f00 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20 73 failure on the s
96f10 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
96f20 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c nection) then al
96f30 77 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a ways return 0..*
96f40 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70 61 * Hence for a pa
96f50 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 rticular databas
96f60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e e connection, on
96f70 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73 ce malloc starts
96f80 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74 20 .** failing, it
96f90 66 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74 fails consistent
96fa0 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46 ly until mallocF
96fb0 61 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e 0a ailed is reset..
96fc0 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6d ** This is an im
96fd0 70 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74 69 portant assumpti
96fe0 6f 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d on. There are m
96ff0 61 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74 68 any places in th
97000 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 64 e.** code that d
97010 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 68 o things like th
97020 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 is:.**.**
97030 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a int *a = (int*
97040 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 )sqlite3DbMalloc
97050 52 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a Raw(db, 100);.**
97060 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62 20 int *b
97070 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 = (int*)sqlite3D
97080 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 bMallocRaw(db, 2
97090 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 00);.**
970a0 69 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d 20 if( b ) a[10] =
970b0 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 9;.**.** In othe
970c0 72 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73 75 r words, if a su
970d0 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 bsequent malloc
970e0 28 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65 64 (ex: "b") worked
970f0 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 0a , it is assumed.
97100 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69 6f ** that all prio
97110 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22 r mallocs (ex: "
97120 61 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a a") worked too..
97130 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
97140 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 E void *sqlite3D
97150 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 bMallocRaw(sqlit
97160 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a e3 *db, int n){.
97170 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 void *p;. ass
97180 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 ert( db==0 || sq
97190 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
971a0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 (db->mutex) );.#
971b0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
971c0 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69 IT_LOOKASIDE. i
971d0 66 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f f( db ){. Loo
971e0 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 kasideSlot *pBuf
971f0 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 ;. if( db->ma
97200 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
97210 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
97220 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e }. if( db->
97230 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c lookaside.bEnabl
97240 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f ed && n<=db->loo
97250 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20 20 kaside.sz.
97260 20 20 20 26 26 20 28 70 42 75 66 20 3d 20 64 62 && (pBuf = db
97270 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 ->lookaside.pFre
97280 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 e)!=0 ){. d
97290 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 b->lookaside.pFr
972a0 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74 ee = pBuf->pNext
972b0 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b ;. db->look
972c0 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 aside.nOut++;.
972d0 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b if( db->look
972e0 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c aside.nOut>db->l
972f0 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 29 ookaside.mxOut )
97300 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f {. db->lo
97310 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 okaside.mxOut =
97320 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f db->lookaside.nO
97330 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ut;. }.
97340 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
97350 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a pBuf;. }. }.
97360 23 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20 26 #else. if( db &
97370 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c & db->mallocFail
97380 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ed ){. return
97390 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 0;. }.#endif.
973a0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
973b0 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70 20 oc(n);. if( !p
973c0 26 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62 2d && db ){. db-
973d0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
973e0 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
973f0 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 p;.}../*.** Resi
97400 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 ze the block of
97410 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 memory pointed t
97420 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65 o by p to n byte
97430 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 s. If the.** res
97440 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74 ize fails, set t
97450 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
97460 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e flag in the conn
97470 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a ection object..*
97480 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
97490 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
974a0 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 Realloc(sqlite3
974b0 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e *db, void *p, in
974c0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e t n){. void *pN
974d0 65 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ew = 0;. assert
974e0 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 ( db!=0 );. ass
974f0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
97500 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
97510 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e x) );. if( db->
97520 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 mallocFailed==0
97530 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 ){. if( p==0
97540 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
97550 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
97560 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d aw(db, n);. }
97570 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 . if( isLooka
97580 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 side(db, p) ){.
97590 20 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e if( n<=db->
975a0 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a lookaside.sz ){.
975b0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 return p
975c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
975d0 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
975e0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 MallocRaw(db, n)
975f0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 ;. if( pNew
97600 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 ){. memc
97610 70 79 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e py(pNew, p, db->
97620 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 lookaside.sz);.
97630 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
97640 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 Free(db, p);.
97650 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
97660 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c pNew = sql
97670 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 ite3_realloc(p,
97680 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 n);. if( !p
97690 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 64 New ){. d
976a0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
976b0 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 1;. }.
976c0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
976d0 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 pNew;.}../*.** A
976e0 74 74 65 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f ttempt to reallo
976f0 63 61 74 65 20 70 2e 20 20 49 66 20 74 68 65 20 cate p. If the
97700 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 reallocation fai
97710 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 20 70 0a ls, then free p.
97720 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 65 20 6d ** and set the m
97730 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
97740 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
97750 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a connection..*/.
97760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
97770 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 oid *sqlite3DbRe
97780 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 allocOrFree(sqli
97790 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 te3 *db, void *p
977a0 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 , int n){. void
977b0 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d *pNew;. pNew =
977c0 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
977d0 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 c(db, p, n);. i
977e0 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 f( !pNew ){.
977f0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
97800 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , p);. }. retu
97810 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
97820 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 * Make a copy of
97830 20 61 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d a string in mem
97840 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
97850 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 m sqliteMalloc()
97860 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 . These .** func
97870 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 tions call sqlit
97880 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 e3MallocRaw() di
97890 72 65 63 74 6c 79 20 69 6e 73 74 65 61 64 20 6f rectly instead o
978a0 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 f sqliteMalloc()
978b0 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 . This.** is bec
978c0 61 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 ause when memory
978d0 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 75 debugging is tu
978e0 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 rned on, these t
978f0 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 wo functions are
97900 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 .** called via
97910 6d 61 63 72 6f 73 20 74 68 61 74 20 72 65 63 6f macros that reco
97920 72 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 rd the current f
97930 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d ile and line num
97940 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 ber in the.** Th
97950 72 65 61 64 44 61 74 61 20 73 74 72 75 63 74 75 readData structu
97960 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 re..*/.SQLITE_PR
97970 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
97980 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 te3DbStrDup(sqli
97990 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
979a0 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 har *z){. char
979b0 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20 *zNew;. size_t
979c0 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b n;. if( z==0 ){
979d0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
979e0 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 }. n = sqlite3
979f0 53 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31 3b Strlen30(z) + 1;
97a00 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 . assert( (n&0x
97a10 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 7fffffff)==n );.
97a20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 zNew = sqlite3
97a30 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
97a40 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 20 7a (int)n);. if( z
97a50 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 New ){. memcp
97a60 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 y(zNew, z, n);.
97a70 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 }. return zNew
97a80 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
97a90 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
97aa0 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 DbStrNDup(sqlite
97ab0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
97ac0 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 r *z, int n){.
97ad0 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 66 char *zNew;. if
97ae0 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ( z==0 ){. re
97af0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 turn 0;. }. as
97b00 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 sert( (n&0x7ffff
97b10 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 fff)==n );. zNe
97b20 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
97b30 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 29 3b locRaw(db, n+1);
97b40 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 . if( zNew ){.
97b50 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 memcpy(zNew,
97b60 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b z, n);. zNew[
97b70 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 n] = 0;. }. re
97b80 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn zNew;.}../*
97b90 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74 72 .** Create a str
97ba0 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a 46 72 ing from the zFr
97bb0 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 61 6e omat argument an
97bc0 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20 74 68 d the va_list th
97bd0 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20 53 at follows..** S
97be0 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67 20 tore the string
97bf0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e in memory obtain
97c00 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 ed from sqliteMa
97c10 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 lloc() and make
97c20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 *pz.** point to
97c30 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a that string..*/.
97c40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
97c50 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74 oid sqlite3SetSt
97c60 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a 2c 20 ring(char **pz,
97c70 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
97c80 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
97c90 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
97ca0 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b t ap;. char *z;
97cb0 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c .. va_start(ap,
97cc0 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d zFormat);. z =
97cd0 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 sqlite3VMPrintf
97ce0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 (db, zFormat, ap
97cf0 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b );. va_end(ap);
97d00 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
97d10 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70 7a (db, *pz);. *pz
97d20 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 = z;.}.../*.**
97d30 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 This function mu
97d40 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 st be called bef
97d50 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e 79 20 ore exiting any
97d60 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69 2e API function (i.
97d70 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 e. .** returning
97d80 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 control to the
97d90 75 73 65 72 29 20 74 68 61 74 20 68 61 73 20 63 user) that has c
97da0 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f 6d 61 alled sqlite3_ma
97db0 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 lloc or.** sqlit
97dc0 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a e3_realloc..**.*
97dd0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 * The returned v
97de0 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 alue is normally
97df0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 a copy of the s
97e00 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
97e10 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 o this.** functi
97e20 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 on. However, if
97e30 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 a malloc() failu
97e40 72 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 re has occurred
97e50 73 69 6e 63 65 20 74 68 65 20 70 72 65 76 69 6f since the previo
97e60 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e us.** invocation
97e70 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 SQLITE_NOMEM is
97e80 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61 returned instea
97e90 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 d. .**.** If the
97ea0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c first argument,
97eb0 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c db, is not NULL
97ec0 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29 20 and a malloc()
97ed0 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 error has occurr
97ee0 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ed,.** then the
97ef0 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72 connection error
97f00 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75 65 -code (the value
97f10 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
97f20 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29 0a ite3_errcode()).
97f30 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c ** is set to SQL
97f40 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 ITE_NOMEM..*/.SQ
97f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
97f60 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 sqlite3ApiExit(
97f70 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e 74 sqlite3* db, int
97f80 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 rc){. /* If th
97f90 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20 6e e db handle is n
97fa0 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65 ot NULL, then we
97fb0 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63 must hold the c
97fc0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 onnection handle
97fd0 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 65 . ** mutex here
97fe0 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 . Otherwise the
97ff0 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 62 read (and possib
98000 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 2d le write) of db-
98010 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20 >mallocFailed .
98020 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 61 ** is unsafe, a
98030 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 6f s is the call to
98040 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e sqlite3Error().
98050 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
98060 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d !db || sqlite3_m
98070 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
98080 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 tex) );. if( db
98090 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 && (db->mallocF
980a0 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c ailed || rc==SQL
980b0 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29 ITE_IOERR_NOMEM)
980c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
980d0 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f rror(db, SQLITE_
980e0 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64 NOMEM, 0);. d
980f0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
98100 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 = 0;. rc = SQ
98110 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
98120 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 64 return rc & (d
98130 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 b ? db->errMask
98140 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a : 0xff);.}../***
98150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
98160 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a of malloc.c ****
98170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
981a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
981b0 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 n file printf.c
981c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
981d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
981e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
981f0 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 63 * The "printf" c
98200 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 ode that follows
98210 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20 dates from the
98220 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 69 1980's. It is i
98230 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20 n.** the public
98240 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 domain. The ori
98250 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 ginal comments a
98260 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 re included here
98270 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 for.** complete
98280 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20 ness. They are
98290 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 very out-of-date
982a0 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73 but might be us
982b0 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 eful as.** an hi
982c0 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 6e storical referen
982d0 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 ce. Most of the
982e0 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 "enhancements"
982f0 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 64 have been backed
98300 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 20 .** out so that
98310 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 the functionalit
98320 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 6d y is now the sam
98330 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 72 e as standard pr
98340 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 24 49 intf()..**.** $I
98350 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 31 2e d: printf.c,v 1.
98360 31 30 33 20 32 30 30 39 2f 30 35 2f 30 34 20 32 103 2009/05/04 2
98370 30 3a 32 30 3a 31 36 20 64 72 68 20 45 78 70 20 0:20:16 drh Exp
98380 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a $.**.***********
98390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
983a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
983b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
983c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
983d0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
983e0 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 61 ing modules is a
983f0 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c 61 n enhanced repla
98400 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 22 cement for the "
98410 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 69 printf" subrouti
98420 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 nes.** found in
98430 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c the standard C l
98440 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f 6c ibrary. The fol
98450 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d 65 lowing enhanceme
98460 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 6f nts are.** suppo
98470 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 rted:.**.**
98480 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 + Additional f
98490 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 73 unctions. The s
984a0 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 22 tandard set of "
984b0 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f 6e printf" function
984c0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 63 s.** inc
984d0 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 70 ludes printf, fp
984e0 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c 20 rintf, sprintf,
984f0 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e 74 vprintf, vfprint
98500 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 f, and.**
98510 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 69 vsprintf. Thi
98520 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 68 s module adds th
98530 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
98540 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
98550 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 6b snprintf -- Work
98560 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c 20 s like sprintf,
98570 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 but has an extra
98580 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 argument.**
98590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
985a0 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 74 which is t
985b0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 he size of the b
985c0 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 6f uffer written to
985d0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..**.**
985e0 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d 20 * mprintf --
985f0 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 69 Similar to spri
98600 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 74 ntf. Writes out
98610 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a put to memory.**
98620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98630 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e obtain
98640 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a ed from malloc..
98650 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
98660 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 43 * xprintf -- C
98670 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 alls a function
98680 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f 75 to dispose of ou
98690 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 tput..**.**
986a0 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 66 * nprintf
986b0 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c 20 -- No output,
986c0 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 but returns the
986d0 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
986e0 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ters.**
986f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98700 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 that would have
98710 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 20 been output by
98720 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 20 printf..**.**
98730 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d 20 * A v-
98740 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 6e version (ex: vsn
98750 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 79 printf) of every
98760 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 function is als
98770 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 o.**
98780 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a 2a supplied..**.*
98790 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 20 * + A few
987a0 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 68 extensions to th
987b0 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f 74 e formatting not
987c0 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f 72 ation are suppor
987d0 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ted:.**.**
987e0 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 20 * The "="
987f0 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 6f flag (similar to
98800 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 65 "-") causes the
98810 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a 2a output to be.**
98820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
98830 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 65 centered in the
98840 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 73 appropriately s
98850 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a ized field..**.*
98860 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 * * T
98870 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 70 he %b field outp
98880 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 uts an integer i
98890 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 6f n binary notatio
988a0 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 n..**.**
988b0 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 65 * The %c fie
988c0 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 61 ld now accepts a
988d0 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65 precision. The
988e0 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 75 character outpu
988f0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
98900 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62 79 is repeated by
98910 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
98920 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 69 imes the precisi
98930 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a 2a on specifies..**
98940 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 .** *
98950 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 6f The %' field wo
98960 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 74 rks like %c, but
98970 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 68 takes as its ch
98980 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 20 aracter the.**
98990 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 next
989a0 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 character of th
989b0 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c e format string,
989c0 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 instead of the
989d0 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 next.**
989e0 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 20 argument.
989f0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 72 For example, pr
98a00 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 20 intf("%.78'-")
98a10 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 0a prints 78 minus.
98a20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
98a30 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 20 signs, the same
98a40 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 as printf("%.78
98a50 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 20 c",'-')..**.**
98a60 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d 70 + When comp
98a70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 6f iled using GCC o
98a80 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 20 n a SPARC, this
98a90 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 version of print
98aa0 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 f is.**
98ab0 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 20 faster than the
98ac0 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 66 library printf f
98ad0 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a 2a or SUN OS 4.1..*
98ae0 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c 6c *.** + All
98af0 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 66 functions are f
98b00 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e 0a ully reentrant..
98b10 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e **.*/../*.** Con
98b20 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 61 version types fa
98b30 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 20 ll into various
98b40 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 65 categories as de
98b50 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 fined by the.**
98b60 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 72 following enumer
98b70 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e ation..*/.#defin
98b80 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20 20 e etRADIX
98b90 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 70 1 /* Integer typ
98ba0 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f 2c es. %d, %x, %o,
98bb0 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a 2f and so forth */
98bc0 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 54 .#define etFLOAT
98bd0 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f 61 2 /* Floa
98be0 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 20 ting point. %f
98bf0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 50 */.#define etEXP
98c00 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 78 3 /* Ex
98c10 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61 ponentional nota
98c20 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 20 tion. %e and %E
98c30 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 4e */.#define etGEN
98c40 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 6c ERIC 4 /* Fl
98c50 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e 65 oating or expone
98c60 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e 67 ntial, depending
98c70 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 67 on exponent. %g
98c80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 49 */.#define etSI
98c90 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 52 ZE 5 /* R
98ca0 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 eturn number of
98cb0 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 65 characters proce
98cc0 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e 20 ssed so far. %n
98cd0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 52 */.#define etSTR
98ce0 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 74 ING 6 /* St
98cf0 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 65 rings. %s */.#de
98d00 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e 47 fine etDYNSTRING
98d10 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 61 7 /* Dynamica
98d20 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 lly allocated st
98d30 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 65 rings. %z */.#de
98d40 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 20 fine etPERCENT
98d50 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 20 8 /* Percent
98d60 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64 symbol. %% */.#d
98d70 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 20 efine etCHARX
98d80 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 74 9 /* Charact
98d90 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 68 ers. %c */./* Th
98da0 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65 6e e rest are exten
98db0 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 sions, not norma
98dc0 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 69 lly found in pri
98dd0 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 ntf() */.#define
98de0 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 30 etSQLESCAPE 10
98df0 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 /* Strings with
98e00 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20 '\'' doubled.
98e10 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %q */.#define et
98e20 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f 2a SQLESCAPE2 11 /*
98e30 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c Strings with '\
98e40 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 65 '' doubled and e
98e50 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a 20 nclosed in '',.
98e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98e70 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 6f NULL po
98e80 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 20 inters replaced
98e90 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 51 by SQL NULL. %Q
98ea0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 4f */.#define etTO
98eb0 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 61 KEN 12 /* a
98ec0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 6f pointer to a To
98ed0 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a 2f ken structure */
98ee0 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c 49 .#define etSRCLI
98ef0 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f ST 13 /* a po
98f00 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c 69 inter to a SrcLi
98f10 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 st */.#define et
98f20 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f 2a POINTER 14 /*
98f30 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 69 The %p conversi
98f40 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 on */.#define et
98f50 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f 2a SQLESCAPE3 15 /*
98f60 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 77 %w -> Strings w
98f70 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 64 ith '\"' doubled
98f80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f 52 */.#define etOR
98f90 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 25 DINAL 16 /* %
98fa0 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 33 r -> 1st, 2nd, 3
98fb0 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 45 rd, 4th, etc. E
98fc0 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a nglish only */..
98fd0 23 64 65 66 69 6e 65 20 65 74 49 4e 56 41 4c 49 #define etINVALI
98fe0 44 20 20 20 20 20 30 20 2f 2a 20 41 6e 79 20 75 D 0 /* Any u
98ff0 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63 6f 6e 76 nrecognized conv
99000 65 72 73 69 6f 6e 20 74 79 70 65 20 2a 2f 0a 0a ersion type */..
99010 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74 ./*.** An "etByt
99020 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 75 e" is an 8-bit u
99030 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a 2a nsigned value..*
99040 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e /.typedef unsign
99050 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0a ed char etByte;.
99060 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 6c ./*.** Each buil
99070 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 tin conversion c
99080 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 68 haracter (ex: th
99090 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 69 e 'd' in "%d") i
990a0 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 s described.** b
990b0 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 y an instance of
990c0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
990d0 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 tructure.*/.type
990e0 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 6e def struct et_in
990f0 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d fo { /* Inform
99100 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 ation about each
99110 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a 2f format field */
99120 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 3b . char fmttype;
99130 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
99140 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 he format field
99150 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a 20 code letter */.
99160 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 20 etByte base;
99170 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
99180 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 20 base for radix
99190 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 conversion */.
991a0 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 20 etByte flags;
991b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
991c0 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 5f or more of FLAG_
991d0 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 constants below
991e0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 70 */. etByte typ
991f0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
99200 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72 * Conversion par
99210 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 74 adigm */. etByt
99220 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 20 e charset;
99230 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
99240 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 20 to aDigits[] of
99250 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69 6e the digits strin
99260 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 72 g */. etByte pr
99270 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 efix;
99280 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 /* Offset into a
99290 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 20 Prefix[] of the
992a0 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a 2f prefix string */
992b0 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a .} et_info;../*.
992c0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
992d0 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c s for et_info.fl
992e0 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 ags.*/.#define F
992f0 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 20 LAG_SIGNED 1
99300 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
99310 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 72 value to conver
99320 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a 23 t is signed */.#
99330 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 45 define FLAG_INTE
99340 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 75 RN 2 /* Tru
99350 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e 61 e if for interna
99360 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 l use only */.#d
99370 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 4e efine FLAG_STRIN
99380 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c 6f G 4 /* Allo
99390 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 69 w infinity preci
993a0 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 sion */.../*.**
993b0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 The following ta
993c0 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 20 ble is searched
993d0 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 20 linearly, so it
993e0 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 74 is good to put t
993f0 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 75 he.** most frequ
99400 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 65 ently used conve
99410 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 73 rsion types firs
99420 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e t..*/.static con
99430 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73 5b st char aDigits[
99440 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 41 ] = "0123456789A
99450 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39 61 BCDEF0123456789a
99460 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 63 bcdef";.static c
99470 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 69 onst char aPrefi
99480 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 30 x[] = "-x0\000X0
99490 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 ";.static const
994a0 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b et_info fmtinfo[
994b0 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c 20 ] = {. { 'd',
994c0 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 10, 1, etRADIX,
994d0 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 0, 0 },.
994e0 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65 { 's', 0, 4, e
994f0 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20 tSTRING, 0,
99500 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 20 0 },. { 'g',
99510 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 0, 1, etGENERIC
99520 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 , 30, 0 },.
99530 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65 { 'z', 0, 4, e
99540 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20 tDYNSTRING, 0,
99550 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 20 0 },. { 'q',
99560 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 0, 4, etSQLESCA
99570 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 PE, 0, 0 },.
99580 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 65 { 'Q', 0, 4, e
99590 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c 20 tSQLESCAPE2, 0,
995a0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 20 0 },. { 'w',
995b0 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 0, 4, etSQLESCA
995c0 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 PE3, 0, 0 },.
995d0 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 65 { 'c', 0, 0, e
995e0 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c 20 tCHARX, 0,
995f0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 20 0 },. { 'o',
99600 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 8, 0, etRADIX,
99610 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 20 0, 2 },.
99620 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 65 { 'u', 10, 0, e
99630 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 tRADIX, 0,
99640 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 20 0 },. { 'x',
99650 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 16, 0, etRADIX,
99660 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 20 16, 1 },.
99670 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 65 { 'X', 16, 0, e
99680 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 tRADIX, 0,
99690 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51 4 },.#ifndef SQ
996a0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
996b0 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 66 NG_POINT. { 'f
996c0 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f 41 ', 0, 1, etFLOA
996d0 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c T, 0, 0 },
996e0 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 31 . { 'e', 0, 1
996f0 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20 , etEXP,
99700 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 45 30, 0 },. { 'E
99710 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c ', 0, 1, etEXP,
99720 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d 2c 14, 0 },
99730 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 31 . { 'G', 0, 1
99740 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 , etGENERIC,
99750 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 14, 0 },.#endif.
99760 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c { 'i', 10, 1,
99770 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 etRADIX, 0
99780 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 27 , 0 },. { 'n'
99790 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c , 0, 0, etSIZE,
997a0 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 0, 0 },.
997b0 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 2c { '%', 0, 0,
997c0 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 30 etPERCENT, 0
997d0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 27 , 0 },. { 'p'
997e0 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e 54 , 16, 0, etPOINT
997f0 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c 0a ER, 0, 1 },.
99800 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 74 ./* All the rest
99810 20 68 61 76 65 20 74 68 65 20 46 4c 41 47 5f 49 have the FLAG_I
99820 4e 54 45 52 4e 20 62 69 74 20 73 65 74 20 61 6e NTERN bit set an
99830 64 20 61 72 65 20 74 68 75 73 20 66 6f 72 20 69 d are thus for i
99840 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 6f nternal.** use o
99850 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 54 27 2c nly */. { 'T',
99860 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 0, 2, etTOKEN,
99870 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
99880 20 7b 20 20 27 53 27 2c 20 20 30 2c 20 32 2c 20 { 'S', 0, 2,
99890 65 74 53 52 43 4c 49 53 54 2c 20 20 20 20 30 2c etSRCLIST, 0,
998a0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 72 27 2c 0 },. { 'r',
998b0 20 31 30 2c 20 33 2c 20 65 74 4f 52 44 49 4e 41 10, 3, etORDINA
998c0 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 7d L, 0, 0 },.}
998d0 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 ;../*.** If SQLI
998e0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
998f0 5f 50 4f 49 4e 54 20 69 73 20 64 65 66 69 6e 65 _POINT is define
99900 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 d, then none of
99910 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 the floating poi
99920 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e nt.** conversion
99930 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a s will work..*/.
99940 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
99950 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
99960 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 NT./*.** "*val"
99970 69 73 20 61 20 64 6f 75 62 6c 65 20 73 75 63 68 is a double such
99980 20 74 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61 that 0.1 <= *va
99990 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75 l < 10.0.** Retu
999a0 72 6e 20 74 68 65 20 61 73 63 69 69 20 63 6f 64 rn the ascii cod
999b0 65 20 66 6f 72 20 74 68 65 20 6c 65 61 64 69 6e e for the leadin
999c0 67 20 64 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c g digit of *val,
999d0 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c then.** multipl
999e0 79 20 22 2a 76 61 6c 22 20 62 79 20 31 30 2e 30 y "*val" by 10.0
999f0 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e to renormalize.
99a00 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a .**.** Example:.
99a10 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 ** input:
99a20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35 39 *val = 3.14159
99a30 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 .** output:
99a40 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39 *val = 1.4159
99a50 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 function ret
99a60 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 urn = '3'.**.**
99a70 54 68 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74 The counter *cnt
99a80 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
99a90 65 61 63 68 20 74 69 6d 65 2e 20 20 41 66 74 65 each time. Afte
99aa0 72 20 63 6f 75 6e 74 65 72 20 65 78 63 65 65 64 r counter exceed
99ab0 73 0a 2a 2a 20 31 36 20 28 74 68 65 20 6e 75 6d s.** 16 (the num
99ac0 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 ber of significa
99ad0 6e 74 20 64 69 67 69 74 73 20 69 6e 20 61 20 36 nt digits in a 6
99ae0 34 2d 62 69 74 20 66 6c 6f 61 74 29 20 27 30 27 4-bit float) '0'
99af0 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 is.** always re
99b00 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
99b10 63 20 63 68 61 72 20 65 74 5f 67 65 74 64 69 67 c char et_getdig
99b20 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 it(LONGDOUBLE_TY
99b30 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 6e PE *val, int *cn
99b40 74 29 7b 0a 20 20 69 6e 74 20 64 69 67 69 74 3b t){. int digit;
99b50 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 . LONGDOUBLE_TY
99b60 50 45 20 64 3b 0a 20 20 69 66 28 20 28 2a 63 6e PE d;. if( (*cn
99b70 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20 72 65 74 t)++ >= 16 ) ret
99b80 75 72 6e 20 27 30 27 3b 0a 20 20 64 69 67 69 74 urn '0';. digit
99b90 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b 0a 20 20 = (int)*val;.
99ba0 64 20 3d 20 64 69 67 69 74 3b 0a 20 20 64 69 67 d = digit;. dig
99bb0 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20 2a 76 61 it += '0';. *va
99bc0 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64 29 2a 31 l = (*val - d)*1
99bd0 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 63 0.0;. return (c
99be0 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a 23 65 6e har)digit;.}.#en
99bf0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
99c00 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
99c10 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 T */../*.** Appe
99c20 6e 64 20 4e 20 73 70 61 63 65 20 63 68 61 72 61 nd N space chara
99c30 63 74 65 72 73 20 74 6f 20 74 68 65 20 67 69 76 cters to the giv
99c40 65 6e 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 en string buffer
99c50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
99c60 20 61 70 70 65 6e 64 53 70 61 63 65 28 53 74 72 appendSpace(Str
99c70 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 69 Accum *pAccum, i
99c80 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 69 63 20 nt N){. static
99c90 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 70 61 63 const char zSpac
99ca0 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 20 20 20 es[] = "
99cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
99cc0 20 20 20 20 20 22 3b 0a 20 20 77 68 69 6c 65 28 ";. while(
99cd0 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 N>=(int)sizeof(
99ce0 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a 20 20 zSpaces)-1 ){.
99cf0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
99d00 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 mAppend(pAccum,
99d10 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f 66 28 zSpaces, sizeof(
99d20 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 20 20 zSpaces)-1);.
99d30 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53 70 N -= sizeof(zSp
99d40 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 20 69 aces)-1;. }. i
99d50 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73 71 f( N>0 ){. sq
99d60 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
99d70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 end(pAccum, zSpa
99d80 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d 0a 0a ces, N);. }.}..
99d90 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 6e 65 /*.** On machine
99da0 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c 20 73 s with a small s
99db0 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 20 63 tack size, you c
99dc0 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68 65 0a an redefine the.
99dd0 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f ** SQLITE_PRINT_
99de0 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 20 6c BUF_SIZE to be l
99df0 65 73 73 20 74 68 61 6e 20 33 35 30 2e 20 20 42 ess than 350. B
99e00 75 74 20 62 65 77 61 72 65 20 2d 20 66 6f 72 0a ut beware - for.
99e10 2a 2a 20 73 6d 61 6c 6c 65 72 20 76 61 6c 75 65 ** smaller value
99e20 73 20 73 6f 6d 65 20 25 66 20 63 6f 6e 76 65 72 s some %f conver
99e30 73 69 6f 6e 73 20 6d 61 79 20 67 6f 20 69 6e 74 sions may go int
99e40 6f 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6c 6f o an infinite lo
99e50 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 op..*/.#ifndef S
99e60 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f QLITE_PRINT_BUF_
99e70 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
99e80 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 LITE_PRINT_BUF_S
99e90 49 5a 45 20 33 35 30 0a 23 65 6e 64 69 66 0a 23 IZE 350.#endif.#
99ea0 64 65 66 69 6e 65 20 65 74 42 55 46 53 49 5a 45 define etBUFSIZE
99eb0 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 SQLITE_PRINT_BU
99ec0 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a 65 20 F_SIZE /* Size
99ed0 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 of the output bu
99ee0 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 ffer */../*.** T
99ef0 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 61 6d 2e he root program.
99f00 20 20 41 6c 6c 20 76 61 72 69 61 74 69 6f 6e 73 All variations
99f10 20 63 61 6c 6c 20 74 68 69 73 20 63 6f 72 65 2e call this core.
99f20 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a 0a 2a .**.** INPUTS:.*
99f30 2a 20 20 20 66 75 6e 63 20 20 20 54 68 69 73 20 * func This
99f40 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
99f50 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e a function takin
99f60 67 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 g three argument
99f70 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 s.**
99f80 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 1. A pointer to
99f90 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d 65 20 anything. Same
99fa0 61 73 20 74 68 65 20 22 61 72 67 22 20 70 61 72 as the "arg" par
99fb0 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20 20 20 ameter..**
99fc0 20 20 20 20 20 20 32 2e 20 41 20 70 6f 69 6e 74 2. A point
99fd0 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f er to the list o
99fe0 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 f characters to
99ff0 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 be output.**
9a000 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 (Note
9a010 2c 20 74 68 69 73 20 6c 69 73 74 20 69 73 20 4e , this list is N
9a020 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 OT null terminat
9a030 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 ed.).**
9a040 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 67 65 72 3. An integer
9a050 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
9a060 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70 cters to be outp
9a070 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ut..**
9a080 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 68 69 73 (Note: This
9a090 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 20 62 65 number might be
9a0a0 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 zero.).**.**
9a0b0 61 72 67 20 20 20 20 54 68 69 73 20 69 73 20 74 arg This is t
9a0c0 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e he pointer to an
9a0d0 79 74 68 69 6e 67 20 77 68 69 63 68 20 77 69 6c ything which wil
9a0e0 6c 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 l be passed as t
9a0f0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 he.** f
9a100 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
9a110 20 22 66 75 6e 63 22 2e 20 20 55 73 65 20 69 74 "func". Use it
9a120 20 66 6f 72 20 77 68 61 74 65 76 65 72 20 79 6f for whatever yo
9a130 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 u like..**.**
9a140 66 6d 74 20 20 20 20 54 68 69 73 20 69 73 20 74 fmt This is t
9a150 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 he format string
9a160 2c 20 61 73 20 69 6e 20 74 68 65 20 75 73 75 61 , as in the usua
9a170 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 l print..**.**
9a180 20 61 70 20 20 20 20 20 54 68 69 73 20 69 73 20 ap This is
9a190 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6c a pointer to a l
9a1a0 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ist of arguments
9a1b0 2e 20 20 53 61 6d 65 20 61 73 20 69 6e 0a 2a 2a . Same as in.**
9a1c0 20 20 20 20 20 20 20 20 20 20 76 66 70 72 69 6e vfprin
9a1d0 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 54 53 t..**.** OUTPUTS
9a1e0 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 54 68 :.** Th
9a1f0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
9a200 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 s the total numb
9a210 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 er of characters
9a220 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 sent to.**
9a230 20 20 20 20 20 74 68 65 20 66 75 6e 63 74 69 6f the functio
9a240 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 74 75 72 n "func". Retur
9a250 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 72 6f 72 ns -1 on a error
9a260 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
9a270 74 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77 t the order in w
9a280 68 69 63 68 20 61 75 74 6f 6d 61 74 69 63 20 76 hich automatic v
9a290 61 72 69 61 62 6c 65 73 20 61 72 65 20 64 65 63 ariables are dec
9a2a0 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a 20 73 lared below.** s
9a2b0 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61 20 62 eems to make a b
9a2c0 69 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e ig difference in
9a2d0 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 68 6f 77 determining how
9a2e0 20 66 61 73 74 20 74 68 69 73 20 62 65 61 73 74 fast this beast
9a2f0 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a 2a 2f .** will run..*/
9a300 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9a310 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 void sqlite3VXPr
9a320 69 6e 74 66 28 0a 20 20 53 74 72 41 63 63 75 6d intf(. StrAccum
9a330 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 20 20 *pAccum,
9a340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 /* Ac
9a350 63 75 6d 75 6c 61 74 65 20 72 65 73 75 6c 74 73 cumulate results
9a360 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 75 here */. int u
9a370 73 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20 20 seExtended,
9a380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
9a390 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64 20 Allow extended
9a3a0 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f %-conversions */
9a3b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 . const char *f
9a3c0 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
9a3d0 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74 20 /* Format
9a3e0 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f 6c string */. va_l
9a3f0 69 73 74 20 61 70 20 20 20 20 20 20 20 20 20 20 ist ap
9a400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
9a410 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 * arguments */.)
9a420 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20 {. int c;
9a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
9a440 2a 20 4e 65 78 74 20 63 68 61 72 61 63 74 65 72 * Next character
9a450 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20 73 in the format s
9a460 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 tring */. char
9a470 2a 62 75 66 70 74 3b 20 20 20 20 20 20 20 20 20 *bufpt;
9a480 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
9a490 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 to the conversi
9a4a0 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 on buffer */. i
9a4b0 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20 20 nt precision;
9a4c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
9a4d0 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 cision of the cu
9a4e0 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 rrent field */.
9a4f0 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 20 int length;
9a500 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
9a510 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69 65 ength of the fie
9a520 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b ld */. int idx;
9a530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9a540 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c 20 /* A general
9a550 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f 75 purpose loop cou
9a560 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77 69 nter */. int wi
9a570 64 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 dth;
9a580 20 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f 66 /* Width of
9a590 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 the current fie
9a5a0 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 ld */. etByte f
9a5b0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 3b lag_leftjustify;
9a5c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2d /* True if "-
9a5d0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
9a5e0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
9a5f0 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 20 20 ag_plussign;
9a600 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2b 22 /* True if "+"
9a610 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
9a620 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
9a630 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 20 20 g_blanksign;
9a640 20 2f 2a 20 54 72 75 65 20 69 66 20 22 20 22 20 /* True if " "
9a650 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 flag is present
9a660 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
9a670 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 20 _alternateform;
9a680 2f 2a 20 54 72 75 65 20 69 66 20 22 23 22 20 66 /* True if "#" f
9a690 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a lag is present *
9a6a0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
9a6b0 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 20 2f altform2; /
9a6c0 2a 20 54 72 75 65 20 69 66 20 22 21 22 20 66 6c * True if "!" fl
9a6d0 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f ag is present */
9a6e0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 7a . etByte flag_z
9a6f0 65 72 6f 70 61 64 3b 20 20 20 20 20 20 20 2f 2a eropad; /*
9a700 20 54 72 75 65 20 69 66 20 66 69 65 6c 64 20 77 True if field w
9a710 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 73 74 idth constant st
9a720 61 72 74 73 20 77 69 74 68 20 7a 65 72 6f 20 2a arts with zero *
9a730 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
9a740 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 20 2f long; /
9a750 2a 20 54 72 75 65 20 69 66 20 22 6c 22 20 66 6c * True if "l" fl
9a760 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f ag is present */
9a770 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c . etByte flag_l
9a780 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 2f 2a onglong; /*
9a790 20 54 72 75 65 20 69 66 20 74 68 65 20 22 6c 6c True if the "ll
9a7a0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
9a7b0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 64 6f t */. etByte do
9a7c0 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ne;
9a7d0 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d 69 6e /* Loop termin
9a7e0 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a 20 20 ation flag */.
9a7f0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 6c 6f sqlite_uint64 lo
9a800 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 56 61 ngvalue; /* Va
9a810 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 65 72 20 lue for integer
9a820 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 44 types */. LONGD
9a830 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 61 6c 76 OUBLE_TYPE realv
9a840 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 20 66 alue; /* Value f
9a850 6f 72 20 72 65 61 6c 20 74 79 70 65 73 20 2a 2f or real types */
9a860 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f . const et_info
9a870 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 2f 2a *infop; /*
9a880 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
9a890 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f appropriate info
9a8a0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
9a8b0 63 68 61 72 20 62 75 66 5b 65 74 42 55 46 53 49 char buf[etBUFSI
9a8c0 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f ZE]; /* Co
9a8d0 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 nversion buffer
9a8e0 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 66 69 78 */. char prefix
9a8f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
9a900 2f 2a 20 50 72 65 66 69 78 20 63 68 61 72 61 63 /* Prefix charac
9a910 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 2d 22 ter. "+" or "-"
9a920 20 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 27 2e or " " or '\0'.
9a930 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74 79 */. etByte xty
9a940 70 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 pe = 0;
9a950 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 /* Conversion p
9a960 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68 61 aradigm */. cha
9a970 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 20 20 r *zExtra;
9a980 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 /* Extra
9a990 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 memory used for
9a9a0 20 65 74 54 43 4c 45 53 43 41 50 45 20 63 6f 6e etTCLESCAPE con
9a9b0 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69 66 6e versions */.#ifn
9a9c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
9a9d0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 FLOATING_POINT.
9a9e0 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b 20 20 int exp, e2;
9a9f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65 /* e
9aa00 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c 20 xponent of real
9aa10 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64 6f 75 numbers */. dou
9aa20 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20 20 20 ble rounder;
9aa30 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
9aa40 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66 6c 6f for rounding flo
9aa50 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
9aa60 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 es */. etByte f
9aa70 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20 20 20 lag_dp;
9aa80 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 65 /* True if de
9aa90 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68 6f 75 cimal point shou
9aaa0 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f 0a 20 ld be shown */.
9aab0 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72 74 7a etByte flag_rtz
9aac0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
9aad0 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e 67 20 rue if trailing
9aae0 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62 65 20 zeros should be
9aaf0 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65 74 42 removed */. etB
9ab00 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20 20 20 yte flag_exp;
9ab10 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
9ab20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c 61 79 to force display
9ab30 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65 6e 74 of the exponent
9ab40 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b 20 20 */. int nsd;
9ab50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ab60 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 69 /* Number of si
9ab70 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 gnificant digits
9ab80 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23 65 6e returned */.#en
9ab90 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20 3d 20 dif.. length =
9aba0 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 30 3b 0a 0;. bufpt = 0;.
9abb0 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66 6d 74 for(; (c=(*fmt
9abc0 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b 0a 20 ))!=0; ++fmt){.
9abd0 20 20 20 69 66 28 20 63 21 3d 27 25 27 20 29 7b if( c!='%' ){
9abe0 0a 20 20 20 20 20 20 69 6e 74 20 61 6d 74 3b 0a . int amt;.
9abf0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 28 63 bufpt = (c
9ac00 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20 20 20 har *)fmt;.
9ac10 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 amt = 1;.
9ac20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b 66 6d while( (c=(*++fm
9ac30 74 29 29 21 3d 27 25 27 20 26 26 20 63 21 3d 30 t))!='%' && c!=0
9ac40 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20 20 20 ) amt++;.
9ac50 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
9ac60 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 ppend(pAccum, bu
9ac70 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20 20 20 fpt, amt);.
9ac80 20 69 66 28 20 63 3d 3d 30 20 29 20 62 72 65 61 if( c==0 ) brea
9ac90 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 k;. }. if(
9aca0 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30 (c=(*++fmt))==0
9acb0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
9acc0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
9acd0 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 31 29 3b pAccum, "%", 1);
9ace0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
9acf0 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 }. /* Find
9ad00 6f 75 74 20 77 68 61 74 20 66 6c 61 67 73 20 61 out what flags a
9ad10 72 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 re present */.
9ad20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 flag_leftjusti
9ad30 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73 69 fy = flag_plussi
9ad40 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 gn = flag_blanks
9ad50 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c 61 67 ign = . flag
9ad60 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d _alternateform =
9ad70 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d flag_altform2 =
9ad80 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 flag_zeropad =
9ad90 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20 30 3b 0;. done = 0;
9ada0 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 73 . do{. s
9adb0 77 69 74 63 68 28 20 63 20 29 7b 0a 20 20 20 20 witch( c ){.
9adc0 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 20 20 case '-':
9add0 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
9ade0 20 3d 20 31 3b 20 20 20 20 20 62 72 65 61 6b 3b = 1; break;
9adf0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2b . case '+
9ae00 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 ': flag_plussi
9ae10 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 gn = 1; b
9ae20 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
9ae30 73 65 20 27 20 27 3a 20 20 20 66 6c 61 67 5f 62 se ' ': flag_b
9ae40 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 20 20 lanksign = 1;
9ae50 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
9ae60 20 20 20 63 61 73 65 20 27 23 27 3a 20 20 20 66 case '#': f
9ae70 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 lag_alternatefor
9ae80 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 6b 3b 0a m = 1; break;.
9ae90 20 20 20 20 20 20 20 20 63 61 73 65 20 27 21 27 case '!'
9aea0 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d : flag_altform
9aeb0 32 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72 2 = 1; br
9aec0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
9aed0 65 20 27 30 27 3a 20 20 20 66 6c 61 67 5f 7a 65 e '0': flag_ze
9aee0 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 20 20 20 ropad = 1;
9aef0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
9af00 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 64 6f default: do
9af10 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 ne = 1;
9af20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9af30 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69 6c }. }whil
9af40 65 28 20 21 64 6f 6e 65 20 26 26 20 28 63 3d 28 e( !done && (c=(
9af50 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0a 20 *++fmt))!=0 );.
9af60 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 66 69 /* Get the fi
9af70 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20 20 20 eld width */.
9af80 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 width = 0;.
9af90 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 if( c=='*' ){.
9afa0 20 20 20 20 77 69 64 74 68 20 3d 20 76 61 5f 61 width = va_a
9afb0 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
9afc0 20 20 69 66 28 20 77 69 64 74 68 3c 30 20 29 7b if( width<0 ){
9afd0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 65 . flag_le
9afe0 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 0a 20 ftjustify = 1;.
9aff0 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 2d width = -
9b000 77 69 64 74 68 3b 0a 20 20 20 20 20 20 7d 0a 20 width;. }.
9b010 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b c = *++fmt;
9b020 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
9b030 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 while( c>='0'
9b040 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 && c<='9' ){.
9b050 20 20 20 20 20 77 69 64 74 68 20 3d 20 77 69 64 width = wid
9b060 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b th*10 + c - '0';
9b070 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b . c = *++
9b080 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 fmt;. }.
9b090 20 7d 0a 20 20 20 20 69 66 28 20 77 69 64 74 68 }. if( width
9b0a0 20 3e 20 65 74 42 55 46 53 49 5a 45 2d 31 30 20 > etBUFSIZE-10
9b0b0 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d ){. width =
9b0c0 20 65 74 42 55 46 53 49 5a 45 2d 31 30 3b 0a 20 etBUFSIZE-10;.
9b0d0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 }. /* Get
9b0e0 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f the precision */
9b0f0 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2e 27 20 . if( c=='.'
9b100 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 ){. precisi
9b110 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 on = 0;. c
9b120 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 = *++fmt;.
9b130 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 if( c=='*' ){.
9b140 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 precision
9b150 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 = va_arg(ap,int)
9b160 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 ;. if( pr
9b170 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 ecision<0 ) prec
9b180 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69 73 69 ision = -precisi
9b190 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 on;. c =
9b1a0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 *++fmt;. }e
9b1b0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 68 69 lse{. whi
9b1c0 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c le( c>='0' && c<
9b1d0 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='9' ){.
9b1e0 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 precision = pr
9b1f0 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63 20 2d ecision*10 + c -
9b200 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '0';.
9b210 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
9b220 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
9b230 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
9b240 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b 0a 20 recision = -1;.
9b250 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 }. /* Get
9b260 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 the conversion t
9b270 79 70 65 20 6d 6f 64 69 66 69 65 72 20 2a 2f 0a ype modifier */.
9b280 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 if( c=='l' )
9b290 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e {. flag_lon
9b2a0 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 20 3d g = 1;. c =
9b2b0 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69 *++fmt;. i
9b2c0 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 f( c=='l' ){.
9b2d0 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f flag_longlo
9b2e0 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ng = 1;.
9b2f0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
9b300 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
9b310 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d flag_longlong =
9b320 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
9b330 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61 }else{. fla
9b340 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f 6c 6f g_long = flag_lo
9b350 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 nglong = 0;.
9b360 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 68 20 74 }. /* Fetch t
9b370 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 20 66 6f he info entry fo
9b380 72 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 r the field */.
9b390 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 infop = &fmti
9b3a0 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 74 79 70 nfo[0];. xtyp
9b3b0 65 20 3d 20 65 74 49 4e 56 41 4c 49 44 3b 0a 20 e = etINVALID;.
9b3c0 20 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 64 for(idx=0; id
9b3d0 78 3c 41 72 72 61 79 53 69 7a 65 28 66 6d 74 69 x<ArraySize(fmti
9b3e0 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b 0a 20 20 nfo); idx++){.
9b3f0 20 20 20 20 69 66 28 20 63 3d 3d 66 6d 74 69 6e if( c==fmtin
9b400 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 70 65 20 fo[idx].fmttype
9b410 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 66 6f 70 ){. infop
9b420 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64 78 5d = &fmtinfo[idx]
9b430 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 73 ;. if( us
9b440 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 28 69 6e eExtended || (in
9b450 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 fop->flags & FLA
9b460 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29 7b 0a G_INTERN)==0 ){.
9b470 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 xtype
9b480 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b 0a 20 = infop->type;.
9b490 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
9b4a0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
9b4b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9b4c0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
9b4d0 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 78 74 72 . }. zExtr
9b4e0 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 2f 2a 20 a = 0;... /*
9b4f0 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 73 Limit the precis
9b500 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 6f ion to prevent o
9b510 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d verflowing buf[]
9b520 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 during conversi
9b530 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 72 on */. if( pr
9b540 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a ecision>etBUFSIZ
9b550 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e E-40 && (infop->
9b560 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 52 flags & FLAG_STR
9b570 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ING)==0 ){.
9b580 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 precision = etB
9b590 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d UFSIZE-40;. }
9b5a0 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 .. /*. **
9b5b0 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 At this point, v
9b5c0 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69 ariables are ini
9b5d0 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c tialized as foll
9b5e0 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ows:. **.
9b5f0 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e ** flag_altern
9b600 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20 ateform
9b610 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 69 TRUE if a '#' i
9b620 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a s present.. *
9b630 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d * flag_altform
9b640 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2
9b650 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 73 TRUE if a '!' is
9b660 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a present.. **
9b670 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e flag_plussign
9b680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
9b690 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 20 RUE if a '+' is
9b6a0 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 present.. **
9b6b0 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 flag_leftjusti
9b6c0 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 52 fy TR
9b6d0 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 70 UE if a '-' is p
9b6e0 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65 resent or if the
9b6f0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
9b700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b710 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 74 field widt
9b720 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a h was negative..
9b730 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 ** flag_ze
9b740 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 20 ropad
9b750 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 TRUE if the
9b760 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 74 width began wit
9b770 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c h 0.. ** fl
9b780 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 ag_long
9b790 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 TRUE i
9b7a0 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27 f the letter 'l'
9b7b0 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a (ell) prefixed.
9b7c0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
9b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b7e0 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 the convers
9b7f0 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 ion character..
9b800 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e ** flag_lon
9b810 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 glong
9b820 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 TRUE if the
9b830 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c letter 'll' (ell
9b840 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 ell) prefixed.
9b850 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
9b860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b870 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 the conversi
9b880 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 on character..
9b890 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e ** flag_blan
9b8a0 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 ksign
9b8b0 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 27 TRUE if a ' '
9b8c0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 is present..
9b8d0 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 20 ** width
9b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b8f0 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 The specified
9b900 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68 field width. Th
9b910 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 is is. **
9b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b930 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79 alway
9b940 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 s non-negative.
9b950 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66 Zero is the def
9b960 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 ault.. ** p
9b970 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20 recision
9b980 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73 The s
9b990 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69 pecified precisi
9b9a0 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 on. The default
9b9b0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
9b9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9b9d0 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 is -1..
9b9e0 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 20 ** xtype
9b9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ba00 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74 The class of t
9ba10 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 he conversion..
9ba20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 ** infop
9ba30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ba40 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 Pointer to t
9ba50 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 he appropriate i
9ba60 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20 nfo struct..
9ba70 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 78 */. switch( x
9ba80 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 type ){. ca
9ba90 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 se etPOINTER:.
9baa0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c flag_longl
9bab0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 ong = sizeof(cha
9bac0 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 r*)==sizeof(i64)
9bad0 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c ;. flag_l
9bae0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 ong = sizeof(cha
9baf0 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 r*)==sizeof(long
9bb00 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f int);. /
9bb10 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 * Fall through i
9bb20 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 nto the next cas
9bb30 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 e */. case
9bb40 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 etORDINAL:.
9bb50 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20 case etRADIX:.
9bb60 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70 if( infop
9bb70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 ->flags & FLAG_S
9bb80 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20 IGNED ){.
9bb90 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 i64 v;.
9bba0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e if( flag_lon
9bbb0 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 glong ){.
9bbc0 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 v = va_arg(
9bbd0 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 20 ap,i64);.
9bbe0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 }else if( fla
9bbf0 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 g_long ){.
9bc00 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 v = va_arg
9bc10 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 (ap,long int);.
9bc20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
9bc30 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 v =
9bc40 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a va_arg(ap,int);.
9bc50 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
9bc60 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b if( v<0 ){
9bc70 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e . lon
9bc80 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 gvalue = -v;.
9bc90 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
9bca0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 = '-';.
9bcb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
9bcc0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 longvalue =
9bcd0 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 v;. i
9bce0 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e f( flag_plussign
9bcf0 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78 ) prefix
9bd00 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 = '+';.
9bd10 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 else if( fla
9bd20 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 g_blanksign ) p
9bd30 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 refix = ' ';.
9bd40 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 else
9bd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9bd60 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a prefix = 0;.
9bd70 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
9bd80 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9bd90 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f if( flag_lo
9bda0 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 nglong ){.
9bdb0 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 longvalue
9bdc0 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 29 = va_arg(ap,u64)
9bdd0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
9bde0 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 e if( flag_long
9bdf0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c ){. l
9be00 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 ongvalue = va_ar
9be10 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c 6f g(ap,unsigned lo
9be20 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 ng int);.
9be30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
9be40 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 longvalue
9be50 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 = va_arg(ap,unsi
9be60 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 gned int);.
9be70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
9be80 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 prefix = 0;.
9be90 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
9bea0 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 f( longvalue==0
9beb0 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 ) flag_alternate
9bec0 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 form = 0;.
9bed0 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 if( flag_zerop
9bee0 61 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c ad && precision<
9bef0 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 width-(prefix!=0
9bf00 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
9bf10 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 recision = width
9bf20 2d 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 -(prefix!=0);.
9bf30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9bf40 62 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42 bufpt = &buf[etB
9bf50 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 UFSIZE-1];.
9bf60 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 if( xtype==et
9bf70 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 ORDINAL ){.
9bf80 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 static cons
9bf90 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 t char zOrd[] =
9bfa0 22 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 "thstndrd";.
9bfb0 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 int x = (i
9bfc0 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 nt)(longvalue %
9bfd0 31 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 10);. i
9bfe0 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 f( x>=4 || (long
9bff0 76 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 value/10)%10==1
9c000 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 ){. x
9c010 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
9c020 7d 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b }. buf[
9c030 65 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a etBUFSIZE-3] = z
9c040 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20 Ord[x*2];.
9c050 20 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a buf[etBUFSIZ
9c060 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b E-2] = zOrd[x*2+
9c070 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 1];. bu
9c080 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 fpt -= 2;.
9c090 20 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 }. {.
9c0a0 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 register
9c0b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 const char *cse
9c0c0 74 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 t; /* Use r
9c0d0 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 egisters for spe
9c0e0 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ed */.
9c0f0 72 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 register int bas
9c100 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 e;. cse
9c110 74 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 t = &aDigits[inf
9c120 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 op->charset];.
9c130 20 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 base = i
9c140 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 nfop->base;.
9c150 20 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 do{
9c160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c180 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 /* Convert t
9c190 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 o ascii */.
9c1a0 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 *(--bufpt
9c1b0 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c ) = cset[longval
9c1c0 75 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 ue%base];.
9c1d0 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 longvalue
9c1e0 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 = longvalue/base
9c1f0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 ;. }whi
9c200 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 le( longvalue>0
9c210 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
9c220 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 length = (i
9c230 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 nt)(&buf[etBUFSI
9c240 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 ZE-1]-bufpt);.
9c250 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 for(idx=pr
9c260 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 ecision-length;
9c270 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 idx>0; idx--){.
9c280 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 *(--buf
9c290 70 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 pt) = '0';
9c2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c2b0 20 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 /* Zero p
9c2c0 61 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a ad */. }.
9c2d0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 if( pref
9c2e0 69 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 ix ) *(--bufpt)
9c2f0 3d 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 = prefix;
9c300 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 /* Add s
9c310 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ign */. i
9c320 66 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 f( flag_alternat
9c330 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e eform && infop->
9c340 70 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f prefix ){ /
9c350 2a 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 * Add "0" or "0x
9c360 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 " */. c
9c370 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a onst char *pre;.
9c380 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78 char x
9c390 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 ;. pre
9c3a0 3d 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 = &aPrefix[infop
9c3b0 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 ->prefix];.
9c3c0 20 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a for(; (x=(*
9c3d0 70 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 pre))!=0; pre++)
9c3e0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b *(--bufpt) = x;
9c3f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
9c400 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 length = (int
9c410 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 )(&buf[etBUFSIZE
9c420 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20 -1]-bufpt);.
9c430 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
9c440 20 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 case etFLOAT:.
9c450 20 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a case etEXP:
9c460 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 . case etGE
9c470 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 NERIC:. r
9c480 65 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 ealvalue = va_ar
9c490 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 g(ap,double);.#i
9c4a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
9c4b0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
9c4c0 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 . if( pre
9c4d0 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 cision<0 ) preci
9c4e0 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 sion = 6;
9c4f0 20 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 /* Set default
9c500 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 precision */.
9c510 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 if( precis
9c520 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d ion>etBUFSIZE/2-
9c530 31 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 10 ) precision =
9c540 20 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b etBUFSIZE/2-10;
9c550 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 . if( rea
9c560 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 lvalue<0.0 ){.
9c570 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 realvalu
9c580 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a e = -realvalue;.
9c590 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 prefix
9c5a0 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 = '-';.
9c5b0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
9c5c0 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 if( flag_plussi
9c5d0 67 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 gn ) pr
9c5e0 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 efix = '+';.
9c5f0 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 else if( f
9c600 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 lag_blanksign )
9c610 20 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b prefix = ' ';
9c620 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 . else
9c630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c640 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d prefix =
9c650 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0;. }.
9c660 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d if( xtype=
9c670 3d 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 =etGENERIC && pr
9c680 65 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 ecision>0 ) prec
9c690 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 ision--;.#if 0.
9c6a0 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 /* Roundi
9c6b0 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 ng works like BS
9c6c0 44 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 D when the const
9c6d0 61 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 ant 0.4999 is us
9c6e0 65 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 ed. Wierd! */.
9c6f0 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 for(idx=p
9c700 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 recision, rounde
9c710 72 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b r=0.4999; idx>0;
9c720 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a idx--, rounder*
9c730 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 =0.1);.#else.
9c740 20 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 /* It makes
9c750 20 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 more sense to u
9c760 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 se 0.5 */.
9c770 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 for(idx=precis
9c780 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 ion, rounder=0.5
9c790 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 ; idx>0; idx--,
9c7a0 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a rounder*=0.1){}.
9c7b0 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 #endif. i
9c7c0 66 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 f( xtype==etFLOA
9c7d0 54 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d T ) realvalue +=
9c7e0 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 rounder;.
9c7f0 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 /* Normalize r
9c800 65 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 ealvalue to with
9c810 69 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 in 10.0 > realva
9c820 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 lue >= 1.0 */.
9c830 20 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 exp = 0;.
9c840 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
9c850 65 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29 e3IsNaN((double)
9c860 72 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 realvalue) ){.
9c870 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
9c880 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 "NaN";.
9c890 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 length = 3;.
9c8a0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
9c8b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9c8c0 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e if( realvalue>0.
9c8d0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 0 ){. w
9c8e0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e hile( realvalue>
9c8f0 3d 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 =1e32 && exp<=35
9c900 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 0 ){ realvalue *
9c910 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 = 1e-32; exp+=32
9c920 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 ; }. wh
9c930 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
9c940 31 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 1e8 && exp<=350
9c950 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
9c960 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 1e-8; exp+=8; }.
9c970 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 while(
9c980 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 realvalue>=10.0
9c990 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 && exp<=350 ){
9c9a0 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 realvalue *= 0.1
9c9b0 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 ; exp++; }.
9c9c0 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c while( real
9c9d0 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 value<1e-8 ){ re
9c9e0 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 alvalue *= 1e8;
9c9f0 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 exp-=8; }.
9ca00 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 while( realv
9ca10 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c alue<1.0 ){ real
9ca20 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 value *= 10.0; e
9ca30 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 xp--; }.
9ca40 20 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b if( exp>350 ){
9ca50 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
9ca60 20 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a prefix=='-' ){.
9ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 bu
9ca80 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 fpt = "-Inf";.
9ca90 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 }else
9caa0 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 if( prefix=='+'
9cab0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
9cac0 20 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b bufpt = "+Inf";
9cad0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
9cae0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
9caf0 20 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b bufpt = "Inf";
9cb00 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
9cb10 20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 lengt
9cb20 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 h = sqlite3Strle
9cb30 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 n30(bufpt);.
9cb40 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9cb50 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
9cb60 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 }. buf
9cb70 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 pt = buf;.
9cb80 20 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 /*. **
9cb90 49 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 If the field typ
9cba0 65 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 e is etGENERIC,
9cbb0 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 then convert to
9cbc0 65 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 either etEXP.
9cbd0 20 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f ** or etFLO
9cbe0 41 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 AT, as appropria
9cbf0 74 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 te.. */.
9cc00 20 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 flag_exp
9cc10 3d 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a = xtype==etEXP;.
9cc20 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 if( xtyp
9cc30 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 e!=etFLOAT ){.
9cc40 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 realvalu
9cc50 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 e += rounder;.
9cc60 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c if( real
9cc70 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 value>=10.0 ){ r
9cc80 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b ealvalue *= 0.1;
9cc90 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 exp++; }.
9cca0 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
9ccb0 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 xtype==etGENERIC
9ccc0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c ){. fl
9ccd0 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 ag_rtz = !flag_a
9cce0 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 lternateform;.
9ccf0 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c if( exp<
9cd00 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 -4 || exp>precis
9cd10 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ion ){.
9cd20 20 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 xtype = etEXP
9cd30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
9cd40 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 e{. p
9cd50 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 recision = preci
9cd60 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 sion - exp;.
9cd70 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 xtype =
9cd80 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 etFLOAT;.
9cd90 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c }. }el
9cda0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c se{. fl
9cdb0 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 ag_rtz = 0;.
9cdc0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
9cdd0 28 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 ( xtype==etEXP )
9cde0 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d {. e2 =
9cdf0 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 0;. }els
9ce00 65 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 e{. e2
9ce10 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d = exp;. }
9ce20 0a 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 . nsd = 0
9ce30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 ;. flag_d
9ce40 70 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 p = (precision>0
9ce50 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c ?1:0) | flag_al
9ce60 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c ternateform | fl
9ce70 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 ag_altform2;.
9ce80 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e /* The sign
9ce90 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 in front of the
9cea0 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 number */.
9ceb0 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b if( prefix ){
9cec0 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
9ced0 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a pt++) = prefix;.
9cee0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9cef0 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f /* Digits prio
9cf00 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c r to the decimal
9cf10 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 point */.
9cf20 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 if( e2<0 ){.
9cf30 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
9cf40 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 +) = '0';.
9cf50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
9cf60 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 for(; e2>=0;
9cf70 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 e2--){.
9cf80 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
9cf90 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 et_getdigit(&rea
9cfa0 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 lvalue,&nsd);.
9cfb0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9cfc0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 }. /* T
9cfd0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 he decimal point
9cfe0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
9cff0 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 flag_dp ){.
9d000 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
9d010 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d = '.';. }
9d020 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 . /* "0"
9d030 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 digits after the
9d040 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 decimal point b
9d050 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 ut before the fi
9d060 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 rst. ** s
9d070 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 ignificant digit
9d080 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a of the number *
9d090 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 /. for(e2
9d0a0 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 ++; e2<0; precis
9d0b0 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 ion--, e2++){.
9d0c0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
9d0d0 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 precision>0 );.
9d0e0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
9d0f0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 ++) = '0';.
9d100 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
9d110 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 Significant digi
9d120 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 ts after the dec
9d130 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 imal point */.
9d140 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 while( (pr
9d150 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a ecision--)>0 ){.
9d160 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
9d170 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 t++) = et_getdig
9d180 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e it(&realvalue,&n
9d190 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 sd);. }.
9d1a0 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 /* Remove
9d1b0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 trailing zeros
9d1c0 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e and the "." if n
9d1d0 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 o digits follow
9d1e0 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 the "." */.
9d1f0 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 if( flag_rtz
9d200 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 && flag_dp ){.
9d210 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 while( b
9d220 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 ufpt[-1]=='0' )
9d230 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a *(--bufpt) = 0;.
9d240 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
9d250 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 ( bufpt>buf );.
9d260 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 if( buf
9d270 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 pt[-1]=='.' ){.
9d280 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 if( f
9d290 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a lag_altform2 ){.
9d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 *(
9d2b0 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a bufpt++) = '0';.
9d2c0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
9d2d0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e{.
9d2e0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b *(--bufpt) = 0;
9d2f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
9d300 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
9d310 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
9d320 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 Add the "eNNN" s
9d330 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 uffix */.
9d340 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c if( flag_exp ||
9d350 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b xtype==etEXP ){
9d360 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
9d370 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b pt++) = aDigits[
9d380 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b infop->charset];
9d390 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 . if( e
9d3a0 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 xp<0 ){.
9d3b0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
9d3c0 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 '-'; exp = -exp
9d3d0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
9d3e0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a e{. *
9d3f0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b (bufpt++) = '+';
9d400 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
9d410 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d if( exp>=
9d420 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 100 ){.
9d430 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
9d440 28 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 (char)((exp/100)
9d450 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a +'0'); /*
9d460 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 100's digit */.
9d470 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 exp
9d480 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 %= 100;.
9d490 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 }. *(
9d4a0 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 bufpt++) = (char
9d4b0 29 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 )(exp/10+'0');
9d4c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 /* 10
9d4d0 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 's digit */.
9d4e0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
9d4f0 20 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 = (char)(exp%10
9d500 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 +'0');
9d510 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 /* 1's digit
9d520 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 */. }.
9d530 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b *bufpt = 0;
9d540 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 .. /* The
9d550 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 converted numbe
9d560 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e r is in buf[] an
9d570 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 d zero terminate
9d580 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 d. Output it..
9d590 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 ** Note th
9d5a0 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 at the number is
9d5b0 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 in the usual or
9d5c0 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 der, not reverse
9d5d0 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 d as with.
9d5e0 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e ** integer con
9d5f0 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 versions. */.
9d600 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 length = (i
9d610 6e 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a nt)(bufpt-buf);.
9d620 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
9d630 62 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a buf;.. /*
9d640 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 Special case:
9d650 41 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f Add leading zero
9d660 73 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65 s if the flag_ze
9d670 72 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20 ropad flag is.
9d680 20 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64 ** set and
9d690 20 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74 we are not left
9d6a0 20 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20 justified */.
9d6b0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a if( flag_z
9d6c0 65 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f eropad && !flag_
9d6d0 6c 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c leftjustify && l
9d6e0 65 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a ength < width){.
9d6f0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b int i;
9d700 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e . int n
9d710 50 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65 Pad = width - le
9d720 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20 ngth;.
9d730 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d for(i=width; i>=
9d740 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 nPad; i--){.
9d750 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d bufpt[i]
9d760 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d = bufpt[i-nPad]
9d770 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
9d780 20 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66 i = pref
9d790 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 ix!=0;.
9d7a0 20 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 while( nPad-- )
9d7b0 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 bufpt[i++] = '0
9d7c0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e ';. len
9d7d0 67 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20 gth = width;.
9d7e0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
9d7f0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
9d800 20 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a case etSIZE:.
9d810 20 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 *(va_arg
9d820 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 (ap,int*)) = pAc
9d830 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 cum->nChar;.
9d840 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 length = wid
9d850 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 th = 0;.
9d860 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 break;. cas
9d870 65 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20 e etPERCENT:.
9d880 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25 buf[0] = '%
9d890 27 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 ';. bufpt
9d8a0 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 = buf;.
9d8b0 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20 length = 1;.
9d8c0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
9d8d0 20 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 case etCHARX:.
9d8e0 20 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72 c = va_ar
9d8f0 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 g(ap,int);.
9d900 20 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61 buf[0] = (cha
9d910 72 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 r)c;. if(
9d920 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b precision>=0 ){
9d930 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 . for(i
9d940 64 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73 dx=1; idx<precis
9d950 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b ion; idx++) buf[
9d960 69 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a idx] = (char)c;.
9d970 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
9d980 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 = precision;.
9d990 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
9d9a0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 length =1
9d9b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
9d9c0 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b bufpt = buf;
9d9d0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
9d9e0 20 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52 case etSTR
9d9f0 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 ING:. case
9da00 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 etDYNSTRING:.
9da10 20 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f bufpt = va_
9da20 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 arg(ap,char*);.
9da30 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 if( bufpt
9da40 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
9da50 20 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 bufpt = "";.
9da60 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 }else if( x
9da70 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e type==etDYNSTRIN
9da80 47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a G ){. z
9da90 45 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 Extra = bufpt;.
9daa0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9dab0 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d if( precision>=
9dac0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 0 ){. f
9dad0 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e or(length=0; len
9dae0 67 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26 gth<precision &&
9daf0 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 bufpt[length];
9db00 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 length++){}.
9db10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9db20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 length = sq
9db30 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 lite3Strlen30(bu
9db40 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a fpt);. }.
9db50 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9db60 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 case etSQLE
9db70 53 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 SCAPE:. cas
9db80 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a e etSQLESCAPE2:.
9db90 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c case etSQL
9dba0 45 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 ESCAPE3: {.
9dbb0 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 int i, j, n,
9dbc0 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 isnull;.
9dbd0 69 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 int needQuote;.
9dbe0 20 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a char ch;.
9dbf0 20 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d char q =
9dc00 20 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 ((xtype==etSQLE
9dc10 53 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 SCAPE3)?'"':'\''
9dc20 29 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 ); /* Quote ch
9dc30 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 aracter */.
9dc40 20 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 char *escarg
9dc50 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 = va_arg(ap,char
9dc60 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 *);. isnu
9dc70 6c 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a ll = escarg==0;.
9dc80 20 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 if( isnu
9dc90 6c 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 ll ) escarg = (x
9dca0 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 type==etSQLESCAP
9dcb0 45 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 E2 ? "NULL" : "(
9dcc0 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 NULL)");.
9dcd0 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d for(i=n=0; (ch=
9dce0 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 escarg[i])!=0; i
9dcf0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
9dd00 66 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b f( ch==q ) n++;
9dd10 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
9dd20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 needQuote = !
9dd30 69 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d isnull && xtype=
9dd40 3d 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 =etSQLESCAPE2;.
9dd50 20 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 n += i +
9dd60 31 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 1 + needQuote*2;
9dd70 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 . if( n>e
9dd80 74 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 tBUFSIZE ){.
9dd90 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 bufpt = zE
9dda0 78 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 xtra = sqlite3Ma
9ddb0 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 lloc( n );.
9ddc0 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d if( bufpt==
9ddd0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
9dde0 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 pAccum->mallocF
9ddf0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 ailed = 1;.
9de00 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
9de10 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
9de20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
9de30 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b bufpt = buf;
9de40 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
9de50 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 j = 0;.
9de60 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 if( needQuote
9de70 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 ) bufpt[j++] = q
9de80 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d ;. for(i=
9de90 30 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 0; (ch=escarg[i]
9dea0 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 )!=0; i++){.
9deb0 20 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d bufpt[j++]
9dec0 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 = ch;.
9ded0 20 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 if( ch==q ) buf
9dee0 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 pt[j++] = ch;.
9def0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9df00 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 if( needQuote )
9df10 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a bufpt[j++] = q;.
9df20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d bufpt[j]
9df30 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 = 0;. le
9df40 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 ngth = j;.
9df50 20 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 /* The precisi
9df60 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e on is ignored on
9df70 20 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 %q and %Q */.
9df80 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 /* if( pre
9df90 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 cision>=0 && pre
9dfa0 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 cision<length )
9dfb0 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 length = precisi
9dfc0 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 on; */. b
9dfd0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
9dfe0 20 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e case etTOKEN
9dff0 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 : {. Toke
9e000 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 n *pToken = va_a
9e010 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a rg(ap, Token*);.
9e020 20 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b if( pTok
9e030 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 en ){.
9e040 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
9e050 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 ppend(pAccum, (c
9e060 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 onst char*)pToke
9e070 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 n->z, pToken->n)
9e080 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
9e090 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 length = wid
9e0a0 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 th = 0;.
9e0b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
9e0c0 20 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c case etSRCL
9e0d0 49 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 IST: {. S
9e0e0 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 rcList *pSrc = v
9e0f0 61 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 a_arg(ap, SrcLis
9e100 74 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 t*);. int
9e110 20 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 k = va_arg(ap,
9e120 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 int);. st
9e130 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
9e140 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 m *pItem = &pSrc
9e150 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 ->a[k];.
9e160 61 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 assert( k>=0 &&
9e170 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a k<pSrc->nSrc );.
9e180 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 if( pIte
9e190 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a m->zDatabase ){.
9e1a0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
9e1b0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
9e1c0 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a pAccum, pItem->z
9e1d0 44 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 Database, -1);.
9e1e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
9e1f0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
9e200 41 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a Accum, ".", 1);.
9e210 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
9e220 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
9e230 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 mAppend(pAccum,
9e240 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 pItem->zName, -1
9e250 29 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 );. lengt
9e260 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 h = width = 0;.
9e270 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
9e280 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 }. defa
9e290 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 ult: {. a
9e2a0 73 73 65 72 74 28 20 78 74 79 70 65 3d 3d 65 74 ssert( xtype==et
9e2b0 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 INVALID );.
9e2c0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
9e2d0 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 }. }/* End s
9e2e0 77 69 74 63 68 20 6f 76 65 72 20 74 68 65 20 66 witch over the f
9e2f0 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20 ormat type */.
9e300 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 /*. ** The
9e310 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 text of the conv
9e320 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 ersion is pointe
9e330 64 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 d to by "bufpt"
9e340 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c and is. ** "l
9e350 65 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 72 ength" character
9e360 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65 s long. The fie
9e370 6c 64 20 77 69 64 74 68 20 69 73 20 22 77 69 64 ld width is "wid
9e380 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20 th". Do. **
9e390 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 the output..
9e3a0 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67 */. if( !flag
9e3b0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a _leftjustify ){.
9e3c0 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 register i
9e3d0 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 nt nspace;.
9e3e0 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d nspace = width-
9e3f0 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 length;. if
9e400 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 ( nspace>0 ){.
9e410 20 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 appendSpac
9e420 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 e(pAccum, nspace
9e430 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
9e440 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e . if( length>
9e450 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
9e460 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
9e470 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 (pAccum, bufpt,
9e480 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 length);. }.
9e490 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 if( flag_left
9e4a0 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 justify ){.
9e4b0 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 register int ns
9e4c0 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 pace;. nspa
9e4d0 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 ce = width-lengt
9e4e0 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 h;. if( nsp
9e4f0 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ace>0 ){.
9e500 20 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 appendSpace(pAc
9e510 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 cum, nspace);.
9e520 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
9e530 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20 if( zExtra ){.
9e540 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
9e550 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a (zExtra);. }.
9e560 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f }/* End for lo
9e570 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d op over the form
9e580 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f at string */.} /
9e590 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f * End of functio
9e5a0 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 n */../*.** Appe
9e5b0 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65 nd N bytes of te
9e5c0 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 xt from z to the
9e5d0 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 StrAccum object
9e5e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9e5f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
9e600 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 StrAccumAppend(S
9e610 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 trAccum *p, cons
9e620 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e t char *z, int N
9e630 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d ){. assert( z!=
9e640 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69 0 || N==0 );. i
9e650 66 28 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 f( p->tooBig | p
9e660 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
9e670 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 70 {. testcase(p
9e680 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74 ->tooBig);. t
9e690 65 73 74 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f estcase(p->mallo
9e6a0 63 46 61 69 6c 65 64 29 3b 0a 20 20 20 20 72 65 cFailed);. re
9e6b0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 turn;. }. if(
9e6c0 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 N<0 ){. N = s
9e6d0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
9e6e0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d );. }. if( N==
9e6f0 30 20 7c 7c 20 4e 45 56 45 52 28 7a 3d 3d 30 29 0 || NEVER(z==0)
9e700 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
9e710 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 68 }. if( p->nCh
9e720 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f ar+N >= p->nAllo
9e730 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a c ){. char *z
9e740 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 21 70 2d New;. if( !p-
9e750 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 >useMalloc ){.
9e760 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 p->tooBig =
9e770 31 3b 0a 20 20 20 20 20 20 4e 20 3d 20 70 2d 3e 1;. N = p->
9e780 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e 43 68 61 nAlloc - p->nCha
9e790 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 r - 1;. if(
9e7a0 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 N<=0 ){.
9e7b0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
9e7c0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
9e7d0 20 20 69 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d i64 szNew = p-
9e7e0 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 20 73 7a >nChar;. sz
9e7f0 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 New += N + 1;.
9e800 20 20 20 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 if( szNew >
9e810 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 p->mxAlloc ){.
9e820 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
9e830 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 AccumReset(p);.
9e840 20 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 p->tooBig
9e850 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 = 1;. re
9e860 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 turn;. }els
9e870 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 e{. p->nA
9e880 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 lloc = (int)szNe
9e890 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 w;. }.
9e8a0 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 zNew = sqlite3D
9e8b0 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 bMallocRaw(p->db
9e8c0 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 , p->nAlloc );.
9e8d0 20 20 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b if( zNew ){
9e8e0 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
9e8f0 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20 zNew, p->zText,
9e900 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20 p->nChar);.
9e910 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
9e920 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 umReset(p);.
9e930 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a p->zText = z
9e940 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 New;. }else
9e950 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c {. p->mal
9e960 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
9e970 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
9e980 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a rAccumReset(p);.
9e990 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
9e9a0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
9e9b0 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a }. memcpy(&p->z
9e9c0 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20 Text[p->nChar],
9e9d0 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 z, N);. p->nCha
9e9e0 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a r += N;.}../*.**
9e9f0 20 46 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74 Finish off a st
9ea00 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73 ring by making s
9ea10 75 72 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74 ure it is zero-t
9ea20 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65 erminated..** Re
9ea30 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
9ea40 6f 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 o the resulting
9ea50 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 string. Return
9ea60 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 a NULL.** pointe
9ea70 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 r if any kind of
9ea80 20 65 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75 error was encou
9ea90 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 ntered..*/.SQLIT
9eaa0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
9eab0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
9eac0 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a inish(StrAccum *
9ead0 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 p){. if( p->zTe
9eae0 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65 xt ){. p->zTe
9eaf0 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 xt[p->nChar] = 0
9eb00 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65 ;. if( p->use
9eb10 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65 Malloc && p->zTe
9eb20 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a xt==p->zBase ){.
9eb30 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d p->zText =
9eb40 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
9eb50 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43 Raw(p->db, p->nC
9eb60 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69 har+1 );. i
9eb70 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 f( p->zText ){.
9eb80 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d memcpy(p-
9eb90 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65 >zText, p->zBase
9eba0 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20 , p->nChar+1);.
9ebb0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
9ebc0 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->mallocFai
9ebd0 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d led = 1;. }
9ebe0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
9ebf0 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a urn p->zText;.}.
9ec00 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20 ./*.** Reset an
9ec10 53 74 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e StrAccum string.
9ec20 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61 Reclaim all ma
9ec30 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a lloced memory..*
9ec40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9ec50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
9ec60 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 AccumReset(StrAc
9ec70 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 cum *p){. if( p
9ec80 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73 ->zText!=p->zBas
9ec90 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
9eca0 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d DbFree(p->db, p-
9ecb0 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70 >zText);. }. p
9ecc0 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a ->zText = 0;.}..
9ecd0 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
9ece0 20 61 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75 a string accumu
9ecf0 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f lator.*/.SQLITE_
9ed00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
9ed10 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
9ed20 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68 (StrAccum *p, ch
9ed30 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e ar *zBase, int n
9ed40 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e , int mx){. p->
9ed50 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65 zText = p->zBase
9ed60 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64 = zBase;. p->d
9ed70 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61 b = 0;. p->nCha
9ed80 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c r = 0;. p->nAll
9ed90 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41 oc = n;. p->mxA
9eda0 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e lloc = mx;. p->
9edb0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 useMalloc = 1;.
9edc0 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a p->tooBig = 0;.
9edd0 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 p->mallocFaile
9ede0 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 d = 0;.}../*.**
9edf0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 Print into memor
9ee00 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
9ee10 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 sqliteMalloc().
9ee20 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 Use the interna
9ee30 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f l.** %-conversio
9ee40 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f n extensions..*/
9ee50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9ee60 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 char *sqlite3VMP
9ee70 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 rintf(sqlite3 *d
9ee80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
9ee90 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 Format, va_list
9eea0 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap){. char *z;.
9eeb0 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c char zBase[SQL
9eec0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 ITE_PRINT_BUF_SI
9eed0 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 ZE];. StrAccum
9eee0 61 63 63 3b 0a 20 20 61 73 73 65 72 74 28 20 64 acc;. assert( d
9eef0 62 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 b!=0 );. sqlite
9ef00 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 3StrAccumInit(&a
9ef10 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f cc, zBase, sizeo
9ef20 66 28 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20 f(zBase),.
9ef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ef40 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
9ef50 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 29 E_LIMIT_LENGTH])
9ef60 3b 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b ;. acc.db = db;
9ef70 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e . sqlite3VXPrin
9ef80 74 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 tf(&acc, 1, zFor
9ef90 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 mat, ap);. z =
9efa0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
9efb0 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 inish(&acc);. i
9efc0 66 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 f( acc.mallocFai
9efd0 6c 65 64 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d led ){. db->m
9efe0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
9eff0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b . }. return z;
9f000 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 .}../*.** Print
9f010 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
9f020 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
9f030 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 Malloc(). Use t
9f040 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 he internal.** %
9f050 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 -conversion exte
9f060 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 nsions..*/.SQLIT
9f070 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
9f080 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 sqlite3MPrintf(s
9f090 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
9f0a0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
9f0b0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ...){. va_list
9f0c0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap;. char *z;.
9f0d0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
9f0e0 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 Format);. z = s
9f0f0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 qlite3VMPrintf(d
9f100 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
9f110 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
9f120 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a return z;.}../*
9f130 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 .** Like sqlite3
9f140 4d 50 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63 MPrintf(), but c
9f150 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 all sqlite3DbFre
9f160 65 28 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65 e() on zStr afte
9f170 72 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 r formatting.**
9f180 74 68 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62 the string and b
9f190 65 66 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67 efore returnning
9f1a0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
9f1b0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 is intended to b
9f1c0 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 e used.** to mod
9f1d0 69 66 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 ify an existing
9f1e0 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61 string. For exa
9f1f0 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 mple:.**.**
9f200 20 20 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 x = sqlite3MPr
9f210 69 6e 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65 intf(db, x, "pre
9f220 66 69 78 20 25 73 20 73 75 66 66 69 78 22 2c 20 fix %s suffix",
9f230 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 x);.**.*/.SQLITE
9f240 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
9f250 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 qlite3MAppendf(s
9f260 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 qlite3 *db, char
9f270 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68 *zStr, const ch
9f280 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
9f290 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
9f2a0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 . char *z;. va
9f2b0 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
9f2c0 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 at);. z = sqlit
9f2d0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a e3VMPrintf(db, z
9f2e0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
9f2f0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c a_end(ap);. sql
9f300 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
9f310 53 74 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a Str);. return z
9f320 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 ;.}../*.** Print
9f330 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 into memory obt
9f340 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
9f350 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d e3_malloc(). Om
9f360 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a it the internal.
9f370 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
9f380 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
9f390 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
9f3a0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 sqlite3_vmprintf
9f3b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f (const char *zFo
9f3c0 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 rmat, va_list ap
9f3d0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 ){. char *z;.
9f3e0 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 char zBase[SQLIT
9f3f0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 E_PRINT_BUF_SIZE
9f400 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 ];. StrAccum ac
9f410 63 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 c;.#ifndef SQLIT
9f420 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
9f430 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
9f440 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 itialize() ) ret
9f450 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 urn 0;.#endif.
9f460 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 sqlite3StrAccumI
9f470 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c nit(&acc, zBase,
9f480 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 sizeof(zBase),
9f490 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 SQLITE_MAX_LENGT
9f4a0 48 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 H);. sqlite3VXP
9f4b0 72 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a rintf(&acc, 0, z
9f4c0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a Format, ap);. z
9f4d0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 = sqlite3StrAcc
9f4e0 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a umFinish(&acc);.
9f4f0 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f return z;.}../
9f500 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 *.** Print into
9f510 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
9f520 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
9f530 6c 6f 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 loc()(). Omit t
9f540 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 he internal.** %
9f550 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 -conversion exte
9f560 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 nsions..*/.SQLIT
9f570 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 E_API char *sqli
9f580 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 te3_mprintf(cons
9f590 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
9f5a0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ...){. va_list
9f5b0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap;. char *z;.
9f5c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
9f5d0 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 MIT_AUTOINIT. i
9f5e0 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 f( sqlite3_initi
9f5f0 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e alize() ) return
9f600 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f 0;.#endif. va_
9f610 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 start(ap, zForma
9f620 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 t);. z = sqlite
9f630 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d 3_vmprintf(zForm
9f640 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e at, ap);. va_en
9f650 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 d(ap);. return
9f660 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 z;.}../*.** sqli
9f670 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77 te3_snprintf() w
9f680 6f 72 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e orks like snprin
9f690 74 66 28 29 20 65 78 63 65 70 74 20 74 68 61 74 tf() except that
9f6a0 20 69 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a it ignores the.
9f6b0 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c ** current local
9f6c0 65 20 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69 e settings. Thi
9f6d0 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 s is important f
9f6e0 6f 72 20 53 51 4c 69 74 65 20 62 65 63 61 75 73 or SQLite becaus
9f6f0 65 20 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 e we.** are not
9f700 61 62 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c able to use a ",
9f710 22 20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c " as the decimal
9f720 20 70 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 point in place
9f730 6f 66 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65 of "." as.** spe
9f740 63 69 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c cified by some l
9f750 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ocales..*/.SQLIT
9f760 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 E_API char *sqli
9f770 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 te3_snprintf(int
9f780 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 n, char *zBuf,
9f790 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
9f7a0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 mat, ...){. cha
9f7b0 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20 r *z;. va_list
9f7c0 61 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 ap;. StrAccum a
9f7d0 63 63 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20 cc;.. if( n<=0
9f7e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42 ){. return zB
9f7f0 75 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 uf;. }. sqlite
9f800 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 3StrAccumInit(&a
9f810 63 63 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b cc, zBuf, n, 0);
9f820 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 . acc.useMalloc
9f830 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 = 0;. va_start
9f840 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 (ap,zFormat);.
9f850 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 sqlite3VXPrintf(
9f860 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 &acc, 0, zFormat
9f870 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
9f880 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 ap);. z = sqlit
9f890 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
9f8a0 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e (&acc);. return
9f8b0 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e z;.}..#if defin
9f8c0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
9f8d0 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e ./*.** A version
9f8e0 20 6f 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 of printf() tha
9f8f0 74 20 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c t understands %l
9f900 6c 64 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 ld. Used for de
9f910 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 bugging..** The
9f920 70 72 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 printf() built i
9f930 6e 74 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e nto some version
9f940 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 s of windows doe
9f950 73 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 s not understand
9f960 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 %lld.** and seg
9f970 66 61 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 faults if you gi
9f980 76 65 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e ve it a long lon
9f990 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 g int..*/.SQLITE
9f9a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
9f9b0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
9f9c0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f (const char *zFo
9f9d0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
9f9e0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 _list ap;. StrA
9f9f0 63 63 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 ccum acc;. char
9fa00 20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 zBuf[500];. sq
9fa10 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
9fa20 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 t(&acc, zBuf, si
9fa30 7a 65 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a zeof(zBuf), 0);.
9fa40 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 acc.useMalloc
9fa50 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 = 0;. va_start(
9fa60 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 ap,zFormat);. s
9fa70 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 qlite3VXPrintf(&
9fa80 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c acc, 0, zFormat,
9fa90 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
9faa0 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 p);. sqlite3Str
9fab0 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 AccumFinish(&acc
9fac0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64 );. fprintf(std
9fad0 6f 75 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b out,"%s", zBuf);
9fae0 0a 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 . fflush(stdout
9faf0 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a );.}.#endif../**
9fb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
9fb10 20 6f 66 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a of printf.c ***
9fb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
9fb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
9fb60 69 6e 20 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 in file random.c
9fb70 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
9fb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
9fba0 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
9fbb0 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
9fbc0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
9fbd0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
9fbe0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
9fbf0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
9fc00 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
9fc10 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
9fc20 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
9fc30 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
9fc40 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
9fc50 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
9fc60 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
9fc70 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
9fc80 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
9fc90 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
9fca0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
9fcb0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
9fcc0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
9fcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fd10 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
9fd20 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 contains code t
9fd30 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73 o implement a ps
9fd40 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 eudo-random numb
9fd50 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20 er.** generator
9fd60 28 50 52 4e 47 29 20 66 6f 72 20 53 51 4c 69 74 (PRNG) for SQLit
9fd70 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 e..**.** Random
9fd80 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 numbers are used
9fd90 20 62 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 by some of the
9fda0 64 61 74 61 62 61 73 65 20 62 61 63 6b 65 6e 64 database backend
9fdb0 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f s in order.** to
9fdc0 20 67 65 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d generate random
9fdd0 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 66 6f integer keys fo
9fde0 72 20 74 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 r tables or rand
9fdf0 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a om filenames..**
9fe00 0a 2a 2a 20 24 49 64 3a 20 72 61 6e 64 6f 6d 2e .** $Id: random.
9fe10 63 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 32 c,v 1.29 2008/12
9fe20 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 64 72 68 /10 19:26:24 drh
9fe30 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 Exp $.*/.../* A
9fe40 6c 6c 20 74 68 72 65 61 64 73 20 73 68 61 72 65 ll threads share
9fe50 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d a single random
9fe60 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f number generato
9fe70 72 2e 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 r..** This struc
9fe80 74 75 72 65 20 69 73 20 74 68 65 20 63 75 72 72 ture is the curr
9fe90 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 ent state of the
9fea0 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 generator..*/.s
9feb0 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
9fec0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 struct sqlite3P
9fed0 72 6e 67 54 79 70 65 20 7b 0a 20 20 75 6e 73 69 rngType {. unsi
9fee0 67 6e 65 64 20 63 68 61 72 20 69 73 49 6e 69 74 gned char isInit
9fef0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ; /* Tr
9ff00 75 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 ue if initialize
9ff10 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 d */. unsigned
9ff20 63 68 61 72 20 69 2c 20 6a 3b 20 20 20 20 20 20 char i, j;
9ff30 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76 /* State v
9ff40 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 6e ariables */. un
9ff50 73 69 67 6e 65 64 20 63 68 61 72 20 73 5b 32 35 signed char s[25
9ff60 36 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6]; /*
9ff70 53 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 State variables
9ff80 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e 67 */.} sqlite3Prng
9ff90 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 73 ;../*.** Get a s
9ffa0 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 61 6e 64 ingle 8-bit rand
9ffb0 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 om value from th
9ffc0 65 20 52 43 34 20 50 52 4e 47 2e 20 20 54 68 65 e RC4 PRNG. The
9ffd0 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20 62 Mutex.** must b
9ffe0 65 20 68 65 6c 64 20 77 68 69 6c 65 20 65 78 65 e held while exe
9fff0 63 75 74 69 6e 67 20 74 68 69 73 20 72 6f 75 74 cuting this rout
a0000 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 6e ine..**.** Why n
a0010 6f 74 20 6a 75 73 74 20 75 73 65 20 61 20 6c 69 ot just use a li
a0020 62 72 61 72 79 20 72 61 6e 64 6f 6d 20 67 65 6e brary random gen
a0030 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61 6e erator like lran
a0040 64 34 38 28 29 20 66 6f 72 20 74 68 69 73 3f 0a d48() for this?.
a0050 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 4f ** Because the O
a0060 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f 64 P_NewRowid opcod
a0070 65 20 69 6e 20 74 68 65 20 56 44 42 45 20 64 65 e in the VDBE de
a0080 70 65 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67 20 pends on having
a0090 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 73 a very.** good s
a00a0 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 ource of random
a00b0 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 20 6c 72 numbers. The lr
a00c0 61 6e 64 34 38 28 29 20 6c 69 62 72 61 72 79 20 and48() library
a00d0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 function may.**
a00e0 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e 6f well be good eno
a00f0 75 67 68 2e 20 20 42 75 74 20 6d 61 79 62 65 20 ugh. But maybe
a0100 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62 65 20 6c not. Or maybe l
a0110 72 61 6e 64 34 38 28 29 20 68 61 73 20 73 6f 6d rand48() has som
a0120 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f 62 e.** subtle prob
a0130 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79 73 lems on some sys
a0140 74 65 6d 73 20 74 68 61 74 20 63 6f 75 6c 64 20 tems that could
a0150 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e 20 cause problems.
a0160 20 49 74 20 69 73 20 68 61 72 64 0a 2a 2a 20 74 It is hard.** t
a0170 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e 69 o know. To mini
a0180 6d 69 7a 65 20 74 68 65 20 72 69 73 6b 20 6f 66 mize the risk of
a0190 20 70 72 6f 62 6c 65 6d 73 20 64 75 65 20 74 6f problems due to
a01a0 20 62 61 64 20 6c 72 61 6e 64 34 38 28 29 0a 2a bad lrand48().*
a01b0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
a01c0 73 2c 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 s, SQLite uses t
a01d0 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 his random numbe
a01e0 72 20 67 65 6e 65 72 61 74 6f 72 20 62 61 73 65 r generator base
a01f0 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68 69 d.** on RC4, whi
a0200 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b 73 ch we know works
a0210 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a very well..**.*
a0220 2a 20 28 4c 61 74 65 72 29 3a 20 20 41 63 74 75 * (Later): Actu
a0230 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 ally, OP_NewRowi
a0240 64 20 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e d does not depen
a0250 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72 d on a good sour
a0260 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d 6e ce of.** randomn
a0270 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42 ess any more. B
a0280 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 ut we will leave
a0290 20 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61 6c this code in al
a02a0 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a 73 l the same..*/.s
a02b0 74 61 74 69 63 20 75 38 20 72 61 6e 64 6f 6d 42 tatic u8 randomB
a02c0 79 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 yte(void){. uns
a02d0 69 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a 0a igned char t;...
a02e0 20 20 2f 2a 20 54 68 65 20 22 77 73 64 50 72 6e /* The "wsdPrn
a02f0 67 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 g" macro will re
a0300 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 70 73 65 solve to the pse
a0310 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 udo-random numbe
a0320 72 20 67 65 6e 65 72 61 74 6f 72 0a 20 20 2a 2a r generator. **
a0330 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 state vector.
a0340 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 If writable stat
a0350 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 ic data is unsup
a0360 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 ported on the ta
a0370 72 67 65 74 2c 0a 20 20 2a 2a 20 77 65 20 68 61 rget,. ** we ha
a0380 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 ve to locate the
a0390 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 state vector at
a03a0 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 run-time. In t
a03b0 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 20 he more common.
a03c0 20 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 ** case where w
a03d0 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 ritable static d
a03e0 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 ata is supported
a03f0 2c 20 77 73 64 50 72 6e 67 20 63 61 6e 20 72 65 , wsdPrng can re
a0400 66 65 72 20 64 69 72 65 63 74 6c 79 0a 20 20 2a fer directly. *
a0410 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 * to the "sqlite
a0420 33 50 72 6e 67 22 20 73 74 61 74 65 20 76 65 63 3Prng" state vec
a0430 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f tor declared abo
a0440 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 ve.. */.#ifdef
a0450 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a SQLITE_OMIT_WSD.
a0460 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 struct sqlite3
a0470 50 72 6e 67 54 79 70 65 20 2a 70 20 3d 20 26 47 PrngType *p = &G
a0480 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c LOBAL(struct sql
a0490 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 ite3PrngType, sq
a04a0 6c 69 74 65 33 50 72 6e 67 29 3b 0a 23 20 64 65 lite3Prng);.# de
a04b0 66 69 6e 65 20 77 73 64 50 72 6e 67 20 70 5b 30 fine wsdPrng p[0
a04c0 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ].#else.# define
a04d0 20 77 73 64 50 72 6e 67 20 73 71 6c 69 74 65 33 wsdPrng sqlite3
a04e0 50 72 6e 67 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 Prng.#endif...
a04f0 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 /* Initialize th
a0500 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 72 e state of the r
a0510 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
a0520 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0a 20 20 2a erator once,. *
a0530 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 * the first time
a0540 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
a0550 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 73 65 called. The se
a0560 65 64 20 76 61 6c 75 65 20 64 6f 65 73 0a 20 20 ed value does.
a0570 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 63 ** not need to c
a0580 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 20 6f 66 20 ontain a lot of
a0590 72 61 6e 64 6f 6d 6e 65 73 73 20 73 69 6e 63 65 randomness since
a05a0 20 77 65 20 61 72 65 20 6e 6f 74 0a 20 20 2a 2a we are not. **
a05b0 20 74 72 79 69 6e 67 20 74 6f 20 64 6f 20 73 65 trying to do se
a05c0 63 75 72 65 20 65 6e 63 72 79 70 74 69 6f 6e 20 cure encryption
a05d0 6f 72 20 61 6e 79 74 68 69 6e 67 20 6c 69 6b 65 or anything like
a05e0 20 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 that.... **.
a05f0 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 6e 20 74 68 ** Nothing in th
a0600 69 73 20 66 69 6c 65 20 6f 72 20 61 6e 79 77 68 is file or anywh
a0610 65 72 65 20 65 6c 73 65 20 69 6e 20 53 51 4c 69 ere else in SQLi
a0620 74 65 20 64 6f 65 73 20 61 6e 79 20 6b 69 6e 64 te does any kind
a0630 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 79 70 74 of. ** encrypt
a0640 69 6f 6e 2e 20 20 54 68 65 20 52 43 34 20 61 6c ion. The RC4 al
a0650 67 6f 72 69 74 68 6d 20 69 73 20 62 65 69 6e 67 gorithm is being
a0660 20 75 73 65 64 20 61 73 20 61 20 50 52 4e 47 20 used as a PRNG
a0670 28 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 0a 20 (pseudo-random.
a0680 20 2a 2a 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 ** number gener
a0690 61 74 6f 72 29 20 6e 6f 74 20 61 73 20 61 6e 20 ator) not as an
a06a0 65 6e 63 72 79 70 74 69 6f 6e 20 64 65 76 69 63 encryption devic
a06b0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 77 e.. */. if( !w
a06c0 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 29 7b sdPrng.isInit ){
a06d0 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
a06e0 63 68 61 72 20 6b 5b 32 35 36 5d 3b 0a 20 20 20 char k[256];.
a06f0 20 77 73 64 50 72 6e 67 2e 6a 20 3d 20 30 3b 0a wsdPrng.j = 0;.
a0700 20 20 20 20 77 73 64 50 72 6e 67 2e 69 20 3d 20 wsdPrng.i =
a0710 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 0;. sqlite3Os
a0720 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
a0730 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c 20 e3_vfs_find(0),
a0740 32 35 36 2c 20 6b 29 3b 0a 20 20 20 20 66 6f 72 256, k);. for
a0750 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b (i=0; i<256; i++
a0760 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 ){. wsdPrng
a0770 2e 73 5b 69 5d 20 3d 20 28 75 38 29 69 3b 0a 20 .s[i] = (u8)i;.
a0780 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 }. for(i=0
a0790 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 ; i<256; i++){.
a07a0 20 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b wsdPrng.j +
a07b0 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 2b = wsdPrng.s[i] +
a07c0 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 20 3d k[i];. t =
a07d0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
a07e0 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77 73 64 ng.j];. wsd
a07f0 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a Prng.s[wsdPrng.j
a0800 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d ] = wsdPrng.s[i]
a0810 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e ;. wsdPrng.
a0820 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 7d 0a s[i] = t;. }.
a0830 20 20 20 20 77 73 64 50 72 6e 67 2e 69 73 49 6e wsdPrng.isIn
a0840 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f it = 1;. }.. /
a0850 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20 72 * Generate and r
a0860 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 6e eturn single ran
a0870 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20 20 dom byte. */.
a0880 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20 20 74 wsdPrng.i++;. t
a0890 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 = wsdPrng.s[wsd
a08a0 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64 50 72 Prng.i];. wsdPr
a08b0 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77 73 64 ng.j += t;. wsd
a08c0 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 Prng.s[wsdPrng.i
a08d0 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 ] = wsdPrng.s[ws
a08e0 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73 64 50 dPrng.j];. wsdP
a08f0 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d rng.s[wsdPrng.j]
a0900 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77 73 64 = t;. t += wsd
a0910 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 Prng.s[wsdPrng.i
a0920 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 50 ];. return wsdP
a0930 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f 2a 0a rng.s[t];.}../*.
a0940 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72 61 6e 64 ** Return N rand
a0950 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53 51 4c om bytes..*/.SQL
a0960 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
a0970 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
a0980 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70 42 75 int N, void *pBu
a0990 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 f){. unsigned c
a09a0 68 61 72 20 2a 7a 42 75 66 20 3d 20 70 42 75 66 har *zBuf = pBuf
a09b0 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ;.#if SQLITE_THR
a09c0 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 EADSAFE. sqlite
a09d0 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 3_mutex *mutex =
a09e0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
a09f0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
a0a00 53 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a 23 65 STATIC_PRNG);.#e
a0a10 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d ndif. sqlite3_m
a0a20 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
a0a30 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 );. while( N--
a0a40 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b 2b 29 ){. *(zBuf++)
a0a50 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28 29 3b = randomByte();
a0a60 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
a0a70 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
a0a80 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 );.}..#ifndef SQ
a0a90 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 LITE_OMIT_BUILTI
a0aa0 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46 6f 72 N_TEST./*.** For
a0ab0 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 testing purpose
a0ac0 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65 73 20 s, we sometimes
a0ad0 77 61 6e 74 20 74 6f 20 70 72 65 73 65 72 76 65 want to preserve
a0ae0 20 74 68 65 20 73 74 61 74 65 20 6f 66 0a 2a 2a the state of.**
a0af0 20 50 52 4e 47 20 61 6e 64 20 72 65 73 74 6f 72 PRNG and restor
a0b00 65 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 e the PRNG to it
a0b10 73 20 73 61 76 65 64 20 73 74 61 74 65 20 61 74 s saved state at
a0b20 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2c 20 6f a later time, o
a0b30 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20 74 68 r.** to reset th
a0b40 65 20 50 52 4e 47 20 74 6f 20 69 74 73 20 69 6e e PRNG to its in
a0b50 69 74 69 61 6c 20 73 74 61 74 65 2e 20 20 54 68 itial state. Th
a0b60 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 63 63 ese routines acc
a0b70 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f 73 65 omplish.** those
a0b80 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 tasks..**.** Th
a0b90 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 e sqlite3_test_c
a0ba0 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 ontrol() interfa
a0bb0 63 65 20 63 61 6c 6c 73 20 74 68 65 73 65 20 72 ce calls these r
a0bc0 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 63 6f outines to.** co
a0bd0 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 2e 0a ntrol the PRNG..
a0be0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 */.static SQLITE
a0bf0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 _WSD struct sqli
a0c00 74 65 33 50 72 6e 67 54 79 70 65 20 73 71 6c 69 te3PrngType sqli
a0c10 74 65 33 53 61 76 65 64 50 72 6e 67 3b 0a 53 51 te3SavedPrng;.SQ
a0c20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a0c30 64 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 d sqlite3PrngSav
a0c40 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 eState(void){.
a0c50 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f memcpy(. &GLO
a0c60 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 BAL(struct sqlit
a0c70 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 e3PrngType, sqli
a0c80 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a 20 te3SavedPrng),.
a0c90 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 &GLOBAL(struc
a0ca0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 t sqlite3PrngTyp
a0cb0 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c e, sqlite3Prng),
a0cc0 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 . sizeof(sqli
a0cd0 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a te3Prng). );.}.
a0ce0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
a0cf0 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 oid sqlite3PrngR
a0d00 65 73 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64 estoreState(void
a0d10 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 ){. memcpy(.
a0d20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 &GLOBAL(struct
a0d30 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c sqlite3PrngType,
a0d40 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 sqlite3Prng),.
a0d50 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 &GLOBAL(struc
a0d60 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 t sqlite3PrngTyp
a0d70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 e, sqlite3SavedP
a0d80 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 rng),. sizeof
a0d90 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 (sqlite3Prng).
a0da0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
a0db0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
a0dc0 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 76 PrngResetState(v
a0dd0 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 41 4c 28 73 oid){. GLOBAL(s
a0de0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a0df0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 gType, sqlite3Pr
a0e00 6e 67 29 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a ng).isInit = 0;.
a0e10 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
a0e20 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
a0e30 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a TEST */../******
a0e40 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
a0e50 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a random.c *******
a0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0e80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
a0e90 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
a0ea0 69 6c 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a ile utf.c ******
a0eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a0ed0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
a0ee0 30 30 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a 004 April 13.**.
a0ef0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
a0f00 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
a0f10 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
a0f20 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
a0f30 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
a0f40 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
a0f50 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
a0f60 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
a0f70 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
a0f80 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
a0f90 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
a0fa0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
a0fb0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
a0fc0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
a0fd0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
a0fe0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
a0ff0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
a1000 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
a1010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
a1050 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
a1060 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 s routines used
a1070 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 62 65 74 to translate bet
a1080 77 65 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20 ween UTF-8, .**
a1090 55 54 46 2d 31 36 2c 20 55 54 46 2d 31 36 42 45 UTF-16, UTF-16BE
a10a0 2c 20 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a , and UTF-16LE..
a10b0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63 **.** $Id: utf.c
a10c0 2c 76 20 31 2e 37 33 20 32 30 30 39 2f 30 34 2f ,v 1.73 2009/04/
a10d0 30 31 20 31 38 3a 34 30 3a 33 32 20 64 72 68 20 01 18:40:32 drh
a10e0 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 Exp $.**.** Note
a10f0 73 20 6f 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a s on UTF-8:.**.*
a1100 2a 20 20 20 42 79 74 65 2d 30 20 20 20 20 42 79 * Byte-0 By
a1110 74 65 2d 31 20 20 20 20 42 79 74 65 2d 32 20 20 te-1 Byte-2
a1120 20 20 42 79 74 65 2d 33 20 20 20 20 56 61 6c 75 Byte-3 Valu
a1130 65 0a 2a 2a 20 20 30 78 78 78 78 78 78 78 20 20 e.** 0xxxxxxx
a1140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
a1160 30 30 30 30 30 30 30 20 30 30 30 30 30 30 30 30 0000000 00000000
a1170 20 30 78 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 0xxxxxxx.** 11
a1180 30 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 0yyyyy 10xxxxxx
a1190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a11a0 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 00000000
a11b0 30 30 30 30 30 79 79 79 20 79 79 78 78 78 78 78 00000yyy yyxxxxx
a11c0 78 0a 2a 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 x.** 1110zzzz
a11d0 31 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78 10yyyyyy 10xxxx
a11e0 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 30 xx 0
a11f0 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 0000000 zzzzyyyy
a1200 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 yyxxxxxx.** 11
a1210 31 31 30 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 110uuu 10uuzzzz
a1220 20 20 31 30 79 79 79 79 79 79 20 20 31 30 78 78 10yyyyyy 10xx
a1230 78 78 78 78 20 20 20 30 30 30 75 75 75 75 75 20 xxxx 000uuuuu
a1240 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 zzzzyyyy yyxxxxx
a1250 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 x.**.**.** Notes
a1260 20 6f 6e 20 55 54 46 2d 31 36 3a 20 20 28 77 69 on UTF-16: (wi
a1270 74 68 20 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 th wwww+1==uuuuu
a1280 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 ).**.** Wor
a1290 64 2d 30 20 20 20 20 20 20 20 20 20 20 20 20 20 d-0
a12a0 20 20 57 6f 72 64 2d 31 20 20 20 20 20 20 20 20 Word-1
a12b0 20 20 56 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 Value.** 1101
a12c0 31 30 77 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 10ww wwzzzzyy
a12d0 31 31 30 31 31 31 79 79 20 79 79 78 78 78 78 78 110111yy yyxxxxx
a12e0 78 20 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a x 000uuuuu zz
a12f0 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a zzyyyy yyxxxxxx.
a1300 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 ** zzzzyyyy yyx
a1310 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 xxxxx
a1320 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 000
a1330 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 00000 zzzzyyyy y
a1340 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a yxxxxxx.**.**.**
a1350 20 42 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 BOM or Byte Ord
a1360 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 er Mark:.**
a1370 30 78 66 66 20 30 78 66 65 20 20 20 6c 69 74 74 0xff 0xfe litt
a1380 6c 65 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 le-endian utf-16
a1390 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 follows.**
a13a0 30 78 66 65 20 30 78 66 66 20 20 20 62 69 67 2d 0xfe 0xff big-
a13b0 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f endian utf-16 fo
a13c0 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a llows.**.*/./***
a13d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
a13e0 75 64 65 20 76 64 62 65 49 6e 74 2e 68 20 69 6e ude vdbeInt.h in
a13f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 75 the middle of u
a1400 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tf.c ***********
a1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
a1420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
a1430 6e 20 66 69 6c 65 20 76 64 62 65 49 6e 74 2e 68 n file vdbeInt.h
a1440 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
a1450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
a1470 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 * 2003 September
a1480 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6.**.** The aut
a1490 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
a14a0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
a14b0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
a14c0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
a14d0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
a14e0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
a14f0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
a1500 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
a1510 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
a1520 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
a1530 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
a1540 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
a1550 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
a1560 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
a1570 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
a1580 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
a1590 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
a15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a15e0 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 *.** This is the
a15f0 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 header file for
a1600 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 information tha
a1610 74 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 t is private to
a1620 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68 the.** VDBE. Th
a1630 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 is information u
a1640 73 65 64 20 74 6f 20 61 6c 6c 20 62 65 20 61 74 sed to all be at
a1650 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 the top of the
a1660 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f 75 72 63 65 single.** source
a1670 20 63 6f 64 65 20 66 69 6c 65 20 22 76 64 62 65 code file "vdbe
a1680 2e 63 22 2e 20 20 57 68 65 6e 20 74 68 61 74 20 .c". When that
a1690 66 69 6c 65 20 62 65 63 61 6d 65 20 74 6f 6f 20 file became too
a16a0 62 69 67 20 28 6f 76 65 72 0a 2a 2a 20 36 30 30 big (over.** 600
a16b0 30 20 6c 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74 0 lines long) it
a16c0 20 77 61 73 20 73 70 6c 69 74 20 75 70 20 69 6e was split up in
a16d0 74 6f 20 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c to several small
a16e0 65 72 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 er files and.**
a16f0 74 68 69 73 20 68 65 61 64 65 72 20 69 6e 66 6f this header info
a1700 72 6d 61 74 69 6f 6e 20 77 61 73 20 66 61 63 74 rmation was fact
a1710 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 ored out..**.**
a1720 24 49 64 3a 20 76 64 62 65 49 6e 74 2e 68 2c 76 $Id: vdbeInt.h,v
a1730 20 31 2e 31 37 30 20 32 30 30 39 2f 30 35 2f 30 1.170 2009/05/0
a1740 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 4 11:42:30 danie
a1750 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
a1760 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e 54 #ifndef _VDBEINT
a1770 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44 42 _H_.#define _VDB
a1780 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 69 EINT_H_../*.** i
a1790 6e 74 54 6f 4b 65 79 28 29 20 61 6e 64 20 6b 65 ntToKey() and ke
a17a0 79 54 6f 49 6e 74 28 29 20 75 73 65 64 20 74 6f yToInt() used to
a17b0 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 72 transform the r
a17c0 6f 77 69 64 2e 20 20 42 75 74 20 77 69 74 68 0a owid. But with.
a17d0 2a 2a 20 74 68 65 20 6c 61 74 65 73 74 20 76 65 ** the latest ve
a17e0 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 65 rsions of the de
a17f0 73 69 67 6e 20 74 68 65 79 20 61 72 65 20 6e 6f sign they are no
a1800 2d 6f 70 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 -ops..*/.#define
a1810 20 6b 65 79 54 6f 49 6e 74 28 58 29 20 20 20 28 keyToInt(X) (
a1820 58 29 0a 23 64 65 66 69 6e 65 20 69 6e 74 54 6f X).#define intTo
a1830 4b 65 79 28 58 29 20 20 20 28 58 29 0a 0a 0a 2f Key(X) (X).../
a1840 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72 61 6e *.** SQL is tran
a1850 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 73 65 slated into a se
a1860 71 75 65 6e 63 65 20 6f 66 20 69 6e 73 74 72 75 quence of instru
a1870 63 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 ctions to be.**
a1880 65 78 65 63 75 74 65 64 20 62 79 20 61 20 76 69 executed by a vi
a1890 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 rtual machine.
a18a0 45 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e Each instruction
a18b0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a is an instance.
a18c0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
a18d0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
a18e0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
a18f0 20 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a VdbeOp Op;../*.
a1900 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 ** Boolean value
a1910 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 s.*/.typedef uns
a1920 69 67 6e 65 64 20 63 68 61 72 20 42 6f 6f 6c 3b igned char Bool;
a1930 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 ../*.** A cursor
a1940 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 69 6e is a pointer in
a1950 74 6f 20 61 20 73 69 6e 67 6c 65 20 42 54 72 65 to a single BTre
a1960 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 e within a datab
a1970 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 ase file..** The
a1980 20 63 75 72 73 6f 72 20 63 61 6e 20 73 65 65 6b cursor can seek
a1990 20 74 6f 20 61 20 42 54 72 65 65 20 65 6e 74 72 to a BTree entr
a19a0 79 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 y with a particu
a19b0 6c 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c lar key, or.** l
a19c0 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65 6e 74 oop over all ent
a19d0 72 69 65 73 20 6f 66 20 74 68 65 20 42 74 72 65 ries of the Btre
a19e0 65 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c 73 6f e. You can also
a19f0 20 69 6e 73 65 72 74 20 6e 65 77 20 42 54 72 65 insert new BTre
a1a00 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f 72 20 e.** entries or
a1a10 72 65 74 72 69 65 76 65 20 74 68 65 20 6b 65 79 retrieve the key
a1a20 20 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 74 68 or data from th
a1a30 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 e entry that the
a1a40 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63 75 cursor.** is cu
a1a50 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
a1a60 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72 to..** .** Ever
a1a70 79 20 63 75 72 73 6f 72 20 74 68 61 74 20 74 68 y cursor that th
a1a80 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
a1a90 65 20 68 61 73 20 6f 70 65 6e 20 69 73 20 72 65 e has open is re
a1aa0 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 0a presented by an.
a1ab0 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
a1ac0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
a1ad0 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 ucture..**.** If
a1ae0 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e the VdbeCursor.
a1af0 69 73 54 72 69 67 67 65 72 52 6f 77 20 66 6c 61 isTriggerRow fla
a1b00 67 20 69 73 20 73 65 74 20 69 74 20 6d 65 61 6e g is set it mean
a1b10 73 20 74 68 61 74 20 74 68 69 73 20 63 75 72 73 s that this curs
a1b20 6f 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 or is.** really
a1b30 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 68 61 a single row tha
a1b40 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 t represents the
a1b50 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 NEW or OLD pseu
a1b60 64 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 do-table of.** a
a1b70 20 72 6f 77 20 74 72 69 67 67 65 72 2e 20 20 54 row trigger. T
a1b80 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 he data for the
a1b90 72 6f 77 20 69 73 20 73 74 6f 72 65 64 20 69 6e row is stored in
a1ba0 20 56 64 62 65 43 75 72 73 6f 72 2e 70 44 61 74 VdbeCursor.pDat
a1bb0 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 77 a and.** the row
a1bc0 69 64 20 69 73 20 69 6e 20 56 64 62 65 43 75 72 id is in VdbeCur
a1bd0 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 sor.iKey..*/.str
a1be0 75 63 74 20 56 64 62 65 43 75 72 73 6f 72 20 7b uct VdbeCursor {
a1bf0 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
a1c00 72 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68 65 20 rsor; /* The
a1c10 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 cursor structure
a1c20 20 6f 66 20 74 68 65 20 62 61 63 6b 65 6e 64 20 of the backend
a1c30 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 */. int iDb;
a1c40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
a1c50 64 65 78 20 6f 66 20 63 75 72 73 6f 72 20 64 61 dex of cursor da
a1c60 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 tabase in db->aD
a1c70 62 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f 0a 20 b[] (or -1) */.
a1c80 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 i64 lastRowid;
a1c90 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 /* Last r
a1ca0 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 78 74 owid from a Next
a1cb0 20 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 65 72 or NextIdx oper
a1cc0 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 6f 6f 6c 20 ation */. Bool
a1cd0 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20 20 zeroed;
a1ce0 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72 6f /* True if zero
a1cf0 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64 79 ed out and ready
a1d00 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20 20 for reuse */.
a1d10 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c 69 Bool rowidIsVali
a1d20 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 d; /* True if
a1d30 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76 61 lastRowid is va
a1d40 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 lid */. Bool at
a1d50 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 2f First; /
a1d60 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 69 * True if pointi
a1d70 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 ng to first entr
a1d80 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65 52 y */. Bool useR
a1d90 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 andomRowid; /*
a1da0 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65 63 Generate new rec
a1db0 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d 69 ord numbers semi
a1dc0 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 -randomly */. B
a1dd0 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 ool nullRow;
a1de0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a1df0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f pointing to a ro
a1e00 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20 2a w with no data *
a1e10 2f 0a 20 20 42 6f 6f 6c 20 70 73 65 75 64 6f 54 /. Bool pseudoT
a1e20 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 68 69 able; /* Thi
a1e30 73 20 69 73 20 61 20 4e 45 57 20 6f 72 20 4f 4c s is a NEW or OL
a1e40 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 D pseudo-tables
a1e50 6f 66 20 61 20 74 72 69 67 67 65 72 20 2a 2f 0a of a trigger */.
a1e60 20 20 42 6f 6f 6c 20 65 70 68 65 6d 50 73 65 75 Bool ephemPseu
a1e70 64 6f 54 61 62 6c 65 3b 0a 20 20 42 6f 6f 6c 20 doTable;. Bool
a1e80 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 deferredMoveto;
a1e90 20 2f 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 /* A call to sq
a1ea0 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f lite3BtreeMoveto
a1eb0 28 29 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a () is needed */.
a1ec0 20 20 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20 Bool isTable;
a1ed0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
a1ee0 69 66 20 61 20 74 61 62 6c 65 20 72 65 71 75 69 if a table requi
a1ef0 72 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 ring integer key
a1f00 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e s */. Bool isIn
a1f10 64 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 dex; /*
a1f20 54 72 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78 True if an index
a1f30 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73 containing keys
a1f40 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 only - no data
a1f50 2a 2f 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54 */. i64 movetoT
a1f60 61 72 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72 arget; /* Ar
a1f70 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65 gument to the de
a1f80 66 65 72 72 65 64 20 73 71 6c 69 74 65 33 42 74 ferred sqlite3Bt
a1f90 72 65 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 reeMoveto() */.
a1fa0 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 Btree *pBt;
a1fb0 20 20 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61 /* Separa
a1fc0 74 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 te file holding
a1fd0 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 temporary table
a1fe0 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 */. int nData;
a1ff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
a2000 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
a2010 20 70 44 61 74 61 20 2a 2f 0a 20 20 63 68 61 72 pData */. char
a2020 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 *pData;
a2030 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 61 20 /* Data for a
a2040 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 NEW or OLD pseud
a2050 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 69 36 34 o-table */. i64
a2060 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 iKey;
a2070 20 20 20 2f 2a 20 4b 65 79 20 66 6f 72 20 74 68 /* Key for th
a2080 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 e NEW or OLD pse
a2090 75 64 6f 2d 74 61 62 6c 65 20 72 6f 77 20 2a 2f udo-table row */
a20a0 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 . KeyInfo *pKey
a20b0 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f Info; /* Info
a20c0 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79 about index key
a20d0 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65 s needed by inde
a20e0 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 x cursors */. i
a20f0 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
a2100 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
a2110 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 f fields in the
a2120 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 header */. i64
a2130 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 seqCount;
a2140 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f /* Sequence co
a2150 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 unter */. sqlit
a2160 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a e3_vtab_cursor *
a2170 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a pVtabCursor; /*
a2180 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20 The cursor for
a2190 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
a21a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 */. const sqlit
a21b0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
a21c0 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c le; /* Modul
a21d0 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74 e for cursor pVt
a21e0 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f abCursor */.. /
a21f0 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 * Result of last
a2200 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
a2210 65 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61 6e eto() done by an
a2220 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 OP_NotExists or
a2230 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69 71 . ** OP_IsUniq
a2240 75 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68 69 ue opcode on thi
a2250 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 69 s cursor. */. i
a2260 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a 0a nt seekResult;..
a2270 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f /* Cached info
a2280 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 rmation about th
a2290 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65 e header for the
a22a0 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61 data record tha
a22b0 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f t the. ** curso
a22c0 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 r is currently p
a22d0 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c ointing to. Onl
a22e0 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65 y valid if cache
a22f0 56 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 20 Valid is true..
a2300 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 ** aRow might p
a2310 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 oint to (ephemer
a2320 61 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 al) data for the
a2330 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 current row, or
a2340 20 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 it might. ** b
a2350 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 e NULL.. */. i
a2360 6e 74 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 nt cacheStatus;
a2370 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 /* Cache is
a2380 20 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d valid if this m
a2390 61 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 atches Vdbe.cach
a23a0 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 eCtr */. int pa
a23b0 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 yloadSize;
a23c0 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
a23d0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 of bytes in the
a23e0 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 record */. u32
a23f0 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 *aType;
a2400 20 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 /* Type values
a2410 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 for all entries
a2420 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
a2430 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 /. u32 *aOffset
a2440 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 ; /* Cac
a2450 68 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 hed offsets to t
a2460 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 he start of each
a2470 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f columns data */
a2480 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 . u8 *aRow;
a2490 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
a24a0 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
a24b0 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 row, if all on
a24c0 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 one page */.};.t
a24d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 ypedef struct Vd
a24e0 62 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72 beCursor VdbeCur
a24f0 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 sor;../*.** A va
a2500 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72 73 lue for VdbeCurs
a2510 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 68 or.cacheValid th
a2520 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 63 at means the cac
a2530 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 76 he is always inv
a2540 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 alid..*/.#define
a2550 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a 0a CACHE_STALE 0..
a2560 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 /*.** Internally
a2570 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70 , the vdbe manip
a2580 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c ulates nearly al
a2590 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20 l SQL values as
a25a0 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 Mem.** structure
a25b0 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75 s. Each Mem stru
a25c0 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c ct may cache mul
a25d0 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61 tiple representa
a25e0 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a 2a tions (string,.*
a25f0 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20 * integer etc.)
a2600 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 of the same valu
a2610 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e 64 e. A value (and
a2620 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20 73 therefore Mem s
a2630 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61 73 tructure).** has
a2640 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 the following p
a2650 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a roperties:.**.**
a2660 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73 20 Each value has
a2670 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 2e a manifest type.
a2680 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74 79 The manifest ty
a2690 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 pe of the value
a26a0 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20 4d stored.** in a M
a26b0 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65 74 em struct is ret
a26c0 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65 6d urned by the Mem
a26d0 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72 6f Type(Mem*) macro
a26e0 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a 2a . The type is.**
a26f0 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e one of SQLITE_N
a2700 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 ULL, SQLITE_INTE
a2710 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41 4c GER, SQLITE_REAL
a2720 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f 72 , SQLITE_TEXT or
a2730 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42 2e .** SQLITE_BLOB.
a2740 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20 7b .*/.struct Mem {
a2750 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 . union {. i
a2760 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 64 i;
a2770 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 /* Integer va
a2780 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 lue. */. int
a2790 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 nZero;
a27a0 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62 69 74 /* Used when bit
a27b0 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73 65 74 MEM_Zero is set
a27c0 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 in flags */.
a27d0 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 FuncDef *pDef;
a27e0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c /* Used onl
a27f0 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 y when flags==ME
a2800 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52 6f 77 M_Agg */. Row
a2810 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20 20 20 Set *pRowSet;
a2820 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 /* Used only wh
a2830 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f en flags==MEM_Ro
a2840 77 53 65 74 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 wSet */. } u;.
a2850 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 double r;
a2860 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
a2870 75 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 ue */. sqlite3
a2880 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 *db; /* T
a2890 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 he associated da
a28a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
a28b0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 n */. char *z;
a28c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
a28d0 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c ring or BLOB val
a28e0 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 ue */. int n;
a28f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
a2900 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
a2910 65 72 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 ers in string va
a2920 6c 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27 lue, excluding '
a2930 5c 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 \0' */. u16 fla
a2940 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 gs; /*
a2950 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e Some combination
a2960 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 of MEM_Null, ME
a2970 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 M_Str, MEM_Dyn,
a2980 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 etc. */. u8 ty
a2990 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a pe; /*
a29a0 20 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e One of SQLITE_N
a29b0 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 ULL, SQLITE_TEXT
a29c0 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 , SQLITE_INTEGER
a29d0 2c 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 , etc */. u8 e
a29e0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f nc; /
a29f0 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 * SQLITE_UTF8, S
a2a00 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 QLITE_UTF16BE, S
a2a10 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f QLITE_UTF16LE */
a2a20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
a2a30 76 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 void *); /* If
a2a40 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 not null, call t
a2a50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 his function to
a2a60 64 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a delete Mem.z */.
a2a70 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b char *zMalloc;
a2a80 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 /* Dynamic
a2a90 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 buffer allocate
a2aa0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c d by sqlite3_mal
a2ab0 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 loc() */.};../*
a2ac0 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 One or more of t
a2ad0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 he following fla
a2ae0 67 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e gs are set to in
a2af0 64 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 dicate the valid
a2b00 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 OK.** representa
a2b10 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c tions of the val
a2b20 75 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 ue stored in the
a2b30 20 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a Mem struct..**.
a2b40 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 ** If the MEM_Nu
a2b50 6c 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 ll flag is set,
a2b60 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 then the value i
a2b70 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 s an SQL NULL va
a2b80 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 lue..** No other
a2b90 20 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 flags may be se
a2ba0 74 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a t in this case..
a2bb0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d **.** If the MEM
a2bc0 5f 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 _Str flag is set
a2bd0 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e then Mem.z poin
a2be0 74 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 ts at a string r
a2bf0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a epresentation..*
a2c00 2a 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 * Usually this i
a2c10 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 s encoded in the
a2c20 20 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e same unicode en
a2c30 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 coding as the ma
a2c40 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 in.** database (
a2c50 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 see below for ex
a2c60 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 ceptions). If th
a2c70 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 e MEM_Term flag
a2c80 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 is also.** set,
a2c90 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 then the string
a2ca0 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 is nul terminate
a2cb0 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 d. The MEM_Int a
a2cc0 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 nd MEM_Real .**
a2cd0 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 flags may coexis
a2ce0 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 t with the MEM_S
a2cf0 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d tr flag..**.** M
a2d00 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 ultiple of these
a2d10 20 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 values can appe
a2d20 61 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e ar in Mem.flags.
a2d30 20 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a But only one.*
a2d40 2a 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 * at a time can
a2d50 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 appear in Mem.ty
a2d60 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d pe..*/.#define M
a2d70 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 EM_Null 0x0
a2d80 30 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 001 /* Value i
a2d90 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e s NULL */.#defin
a2da0 65 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 e MEM_Str
a2db0 30 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 0x0002 /* Valu
a2dc0 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f e is a string */
a2dd0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 .#define MEM_Int
a2de0 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20 0x0004
a2df0 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 /* Value is an i
a2e00 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e nteger */.#defin
a2e10 65 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 e MEM_Real
a2e20 30 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 0x0008 /* Valu
a2e30 65 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 e is a real numb
a2e40 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 er */.#define ME
a2e50 4d 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 M_Blob 0x00
a2e60 31 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 10 /* Value is
a2e70 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69 a BLOB */.#defi
a2e80 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20 ne MEM_RowSet
a2e90 20 30 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c 0x0020 /* Val
a2ea0 75 65 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f ue is a RowSet o
a2eb0 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 bject */.#define
a2ec0 20 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30 MEM_TypeMask 0
a2ed0 78 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20 x00ff /* Mask
a2ee0 6f 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a of type bits */.
a2ef0 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d ./* Whenever Mem
a2f00 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69 contains a vali
a2f10 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 d string or blob
a2f20 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c representation,
a2f30 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 one of.** the f
a2f40 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d ollowing flags m
a2f50 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65 ust be set to de
a2f60 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f termine the memo
a2f70 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a ry management.**
a2f80 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e policy for Mem.
a2f90 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d z. The MEM_Term
a2fa0 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77 flag tells us w
a2fb0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
a2fc0 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c e.** string is \
a2fd0 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65 000 or \u0000 te
a2fe0 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66 rminated.*/.#def
a2ff0 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20 ine MEM_Term
a3000 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74 0x0200 /* St
a3010 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20 ring rep is nul
a3020 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64 terminated */.#d
a3030 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20 efine MEM_Dyn
a3040 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20 0x0400 /*
a3050 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c Need to call sql
a3060 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d iteFree() on Mem
a3070 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 .z */.#define ME
a3080 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38 M_Static 0x08
a3090 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 00 /* Mem.z po
a30a0 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63 ints to a static
a30b0 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
a30c0 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20 ne MEM_Ephem
a30d0 20 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d 0x1000 /* Mem
a30e0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 .z points to an
a30f0 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 ephemeral string
a3100 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
a3110 41 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30 Agg 0x2000
a3120 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e /* Mem.z poin
a3130 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e ts to an agg fun
a3140 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f ction context */
a3150 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 .#define MEM_Zer
a3160 6f 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20 o 0x4000
a3170 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e /* Mem.i contain
a3180 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70 s count of 0s ap
a3190 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a pended to blob *
a31a0 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
a31b0 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
a31c0 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f #undef MEM_Zero
a31d0 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a . #define MEM_Z
a31e0 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69 ero 0x0000.#endi
a31f0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 f.../*.** Clear
a3200 61 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70 any existing typ
a3210 65 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d e flags from a M
a3220 65 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74 em and replace t
a3230 68 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64 hem with f.*/.#d
a3240 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 efine MemSetType
a3250 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20 Flag(p, f) \.
a3260 28 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 ((p)->flags = ((
a3270 70 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f p)->flags&~(MEM_
a3280 54 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 TypeMask|MEM_Zer
a3290 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64 o))|f).../* A Vd
a32a0 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61 beFunc is just a
a32b0 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65 FuncDef (define
a32c0 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 d in sqliteInt.h
a32d0 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a ) that contains.
a32e0 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e ** additional in
a32f0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
a3300 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d auxiliary inform
a3310 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61 ation bound to a
a3320 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 rguments.** of t
a3330 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 he function. Th
a3340 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d is is used to im
a3350 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 plement the sqli
a3360 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
a3370 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 ).** and sqlite3
a3380 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 _set_auxdata() A
a3390 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61 PIs. The "auxda
a33a0 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69 ta" is some auxi
a33b0 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68 liary data.** th
a33c0 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69 at can be associ
a33d0 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73 ated with a cons
a33e0 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f tant argument to
a33f0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 a function. Th
a3400 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e is.** allows fun
a3410 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22 ctions such as "
a3420 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69 regexp" to compi
a3430 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e le their constan
a3440 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70 t regular.** exp
a3450 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74 ression argument
a3460 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64 once and reused
a3470 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f the compiled co
a3480 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a de for multiple.
a3490 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a ** invocations..
a34a0 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75 */.struct VdbeFu
a34b0 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a nc {. FuncDef *
a34c0 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 pFunc;
a34d0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69 /* The defi
a34e0 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 nition of the fu
a34f0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nction */. int
a3500 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 nAux;
a3510 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
a3520 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 ber of entries a
a3530 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41 llocated for apA
a3540 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 ux[] */. struct
a3550 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76 AuxData {. v
a3560 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 oid *pAux;
a3570 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a3580 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65 Aux data for the
a3590 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a i-th argument *
a35a0 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65 /. void (*xDe
a35b0 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20 lete)(void *);
a35c0 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f /* Destructo
a35d0 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61 r for the aux da
a35e0 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b ta */. } apAux[
a35f0 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1];
a3600 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f /* One slo
a3610 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74 t for each funct
a3620 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a ion argument */.
a3630 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 };../*.** The "c
a3640 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74 ontext" argument
a3650 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62 for a installab
a3660 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 le function. A
a3670 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a pointer to an.**
a3680 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
a3690 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74 s structure is t
a36a0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
a36b0 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 t to the routine
a36c0 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d s used.** implem
a36d0 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 ent the SQL func
a36e0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
a36f0 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20 re is a typedef
a3700 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75 for this structu
a3710 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20 re in sqlite.h.
a3720 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73 So all routines
a3730 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75 ,.** even the pu
a3740 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 blic interface t
a3750 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73 o SQLite, can us
a3760 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 e a pointer to t
a3770 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
a3780 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20 * But this file
a3790 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63 is the only plac
a37a0 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 e where the inte
a37b0 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20 rnal details of
a37c0 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 this.** structur
a37d0 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a e are known..**.
a37e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
a37f0 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 e is defined ins
a3800 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 ide of vdbeInt.h
a3810 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73 because it uses
a3820 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a substructures.*
a3830 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72 * (Mem) which ar
a3840 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74 e only defined t
a3850 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 here..*/.struct
a3860 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
a3870 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 {. FuncDef *pFu
a3880 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 nc; /* Poi
a3890 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e nter to function
a38a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d information. M
a38b0 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a UST BE FIRST */.
a38c0 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
a38d0 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c eFunc; /* Auxil
a38e0 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65 ary data, if cre
a38f0 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 ated. */. Mem s
a3900 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a3910 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 /* The return v
a3920 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68 alue is stored h
a3930 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d ere */. Mem *pM
a3940 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
a3950 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 * Memory cell us
a3960 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72 ed to store aggr
a3970 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f egate context */
a3980 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20 . int isError;
a3990 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
a39a0 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 r code returned
a39b0 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e by the function.
a39c0 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 */. CollSeq *p
a39d0 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 Coll; /* C
a39e0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
a39f0 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 e */.};../*.** A
a3a00 20 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69 Set structure i
a3a10 73 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b s used for quick
a3a20 20 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20 testing to see
a3a30 69 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73 if a value.** is
a3a40 20 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c part of a small
a3a50 20 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20 set. Sets are
a3a60 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
a3a70 74 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 t code like.** t
a3a80 68 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 his:.**
a3a90 20 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c x.y IN ('hi',
a3aa0 27 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 'hoo','hum').*/.
a3ab0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
a3ac0 65 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53 et Set;.struct S
a3ad0 65 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68 et {. Hash hash
a3ae0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a3af0 20 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61 A set is just a
a3b00 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 hash table */.
a3b10 20 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b HashElem *prev;
a3b20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 /* Previ
a3b30 6f 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68 ously accessed h
a3b40 61 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b ash elemen */.};
a3b50 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f 6e 74 65 78 ../*.** A Contex
a3b60 74 20 73 74 6f 72 65 73 20 74 68 65 20 6c 61 73 t stores the las
a3b70 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 2c 20 t insert rowid,
a3b80 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 the last stateme
a3b90 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c nt change count,
a3ba0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 75 72 72 .** and the curr
a3bb0 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 ent statement ch
a3bc0 61 6e 67 65 20 63 6f 75 6e 74 20 28 69 2e 65 2e ange count (i.e.
a3bd0 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 6c changes since l
a3be0 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 2e 0a ast statement)..
a3bf0 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 6b ** The current k
a3c00 65 79 6c 69 73 74 20 69 73 20 61 6c 73 6f 20 73 eylist is also s
a3c10 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e tored in the con
a3c20 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 6d 65 6e 74 text..** Element
a3c30 73 20 6f 66 20 43 6f 6e 74 65 78 74 20 73 74 72 s of Context str
a3c40 75 63 74 75 72 65 20 74 79 70 65 20 6d 61 6b 65 ucture type make
a3c50 20 75 70 20 74 68 65 20 43 6f 6e 74 65 78 74 53 up the ContextS
a3c60 74 61 63 6b 2c 20 77 68 69 63 68 20 69 73 0a 2a tack, which is.*
a3c70 2a 20 75 70 64 61 74 65 64 20 62 79 20 74 68 65 * updated by the
a3c80 20 43 6f 6e 74 65 78 74 50 75 73 68 20 61 6e 64 ContextPush and
a3c90 20 43 6f 6e 74 65 78 74 50 6f 70 20 6f 70 63 6f ContextPop opco
a3ca0 64 65 73 20 28 75 73 65 64 20 62 79 20 74 72 69 des (used by tri
a3cb0 67 67 65 72 73 29 2e 0a 2a 2a 20 54 68 65 20 63 ggers)..** The c
a3cc0 6f 6e 74 65 78 74 20 69 73 20 70 75 73 68 65 64 ontext is pushed
a3cd0 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 69 6e before executin
a3ce0 67 20 61 20 74 72 69 67 67 65 72 20 61 20 70 6f g a trigger a po
a3cf0 70 70 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a pped when the.**
a3d00 20 74 72 69 67 67 65 72 20 66 69 6e 69 73 68 65 trigger finishe
a3d10 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
a3d20 72 75 63 74 20 43 6f 6e 74 65 78 74 20 43 6f 6e ruct Context Con
a3d30 74 65 78 74 3b 0a 73 74 72 75 63 74 20 43 6f 6e text;.struct Con
a3d40 74 65 78 74 20 7b 0a 20 20 69 36 34 20 6c 61 73 text {. i64 las
a3d50 74 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 4c 61 tRowid; /* La
a3d60 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 st insert rowid
a3d70 28 73 71 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 (sqlite3.lastRow
a3d80 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 id) */. int nCh
a3d90 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 74 ange; /* St
a3da0 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 atement changes
a3db0 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20 (Vdbe.nChanges)
a3dc0 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a */.};../*.**
a3dd0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
a3de0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
a3df0 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 ine. This struc
a3e00 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 ture contains th
a3e10 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 e complete.** st
a3e20 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 ate of the virtu
a3e30 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a al machine..**.*
a3e40 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f 73 * The "sqlite3_s
a3e50 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 20 70 tmt" structure p
a3e60 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 72 ointer that is r
a3e70 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
a3e80 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 e3_compile().**
a3e90 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e is really a poin
a3ea0 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e ter to an instan
a3eb0 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ce of this struc
a3ec0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ture..**.** The
a3ed0 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f Vdbe.inVtabMetho
a3ee0 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 d variable is se
a3ef0 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f t to non-zero fo
a3f00 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f r the duration o
a3f10 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61 6c f.** any virtual
a3f20 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e table method in
a3f30 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 vocations made b
a3f40 79 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 y the vdbe progr
a3f50 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 am. It is.** set
a3f60 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74 72 to 2 for xDestr
a3f70 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 oy method calls
a3f80 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 and 1 for all ot
a3f90 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 her methods. Thi
a3fa0 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 s.** variable is
a3fb0 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70 75 used for two pu
a3fc0 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 rposes: to allow
a3fd0 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 xDestroy method
a3fe0 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 s to execute.**
a3ff0 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74 61 "DROP TABLE" sta
a4000 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 tements and to p
a4010 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 revent some nast
a4020 79 20 73 69 64 65 20 65 66 66 65 63 74 73 20 6f y side effects o
a4030 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c f.** malloc fail
a4040 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65 20 ure when SQLite
a4050 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 is invoked recur
a4060 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 72 74 sively by a virt
a4070 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 ual table .** me
a4080 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a thod function..*
a4090 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b 0a /.struct Vdbe {.
a40a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
a40b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f /* The who
a40c0 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 le database */.
a40d0 20 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e Vdbe *pPrev,*pN
a40e0 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c ext; /* Linked l
a40f0 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 69 74 ist of VDBEs wit
a4100 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 2e h the same Vdbe.
a4110 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b db */. int nOp;
a4120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
a4130 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 umber of instruc
a4140 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 6f tions in the pro
a4150 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f gram */. int nO
a4160 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a pAlloc; /*
a4170 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 Number of slots
a4180 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 allocated for a
a4190 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f Op[] */. Op *aO
a41a0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a p; /*
a41b0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
a41c0 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
a41d0 6e 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a ne's program */.
a41e0 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 int nLabel;
a41f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
a4200 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 20 2a of labels used *
a4210 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 41 6c /. int nLabelAl
a4220 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 loc; /* Numbe
a4230 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
a4240 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d ated in aLabel[]
a4250 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 */. int *aLabe
a4260 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 l; /* Spa
a4270 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6c ce to hold the l
a4280 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a abels */. Mem *
a4290 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 2f *apArg; /
a42a0 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 * Arguments to c
a42b0 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 urrently executi
a42c0 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e ng user function
a42d0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e */. Mem *aColN
a42e0 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6c ame; /* Col
a42f0 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74 umn names to ret
a4300 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 urn */. int nCu
a4310 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 rsor; /*
a4320 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 Number of slots
a4330 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20 in apCsr[] */.
a4340 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 VdbeCursor **apC
a4350 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65 sr; /* One eleme
a4360 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 nt of this array
a4370 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63 for each open c
a4380 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e ursor */. int n
a4390 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f Var; /
a43a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
a43b0 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a 2f ies in aVar[] */
a43c0 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20 . Mem *aVar;
a43d0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 /* Values
a43e0 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 for the OP_Vari
a43f0 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a able opcode. */.
a4400 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 char **azVar;
a4410 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
a4420 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 variables */.
a4430 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20 20 20 int okVar;
a4440 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
a4450 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e 20 zVar[] has been
a4460 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
a4470 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 u32 magic;
a4480 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 /* Magi
a4490 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 61 6e c number for san
a44a0 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
a44b0 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 int nMem;
a44c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
a44d0 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f ber of memory lo
a44e0 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c cations currentl
a44f0 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 y allocated */.
a4500 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 Mem *aMem;
a4510 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
a4520 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 memory locations
a4530 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 43 */. int cacheC
a4540 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a tr; /*
a4550 20 56 64 62 65 43 75 72 73 6f 72 20 72 6f 77 20 VdbeCursor row
a4560 63 61 63 68 65 20 67 65 6e 65 72 61 74 69 6f 6e cache generation
a4570 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
a4580 74 20 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f t contextStackTo
a4590 70 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f p; /* Index o
a45a0 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e f top element in
a45b0 20 74 68 65 20 63 6f 6e 74 65 78 74 20 73 74 61 the context sta
a45c0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 74 ck */. int cont
a45d0 65 78 74 53 74 61 63 6b 44 65 70 74 68 3b 20 20 extStackDepth;
a45e0 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 /* The size of t
a45f0 68 65 20 22 63 6f 6e 74 65 78 74 22 20 73 74 61 he "context" sta
a4600 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65 78 74 20 ck */. Context
a4610 2a 63 6f 6e 74 65 78 74 53 74 61 63 6b 3b 20 20 *contextStack;
a4620 2f 2a 20 53 74 61 63 6b 20 75 73 65 64 20 62 79 /* Stack used by
a4630 20 6f 70 63 6f 64 65 73 20 43 6f 6e 74 65 78 74 opcodes Context
a4640 50 75 73 68 20 26 20 43 6f 6e 74 65 78 74 50 6f Push & ContextPo
a4650 70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 p*/. int pc;
a4660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a4670 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 The program cou
a4680 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 nter */. int rc
a4690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a46a0 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
a46b0 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 65 72 turn */. int er
a46c0 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 rorAction;
a46d0 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63 /* Recovery ac
a46e0 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61 tion to do in ca
a46f0 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a se of an error *
a4700 2f 0a 20 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 /. int nResColu
a4710 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e mn; /* N
a4720 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
a4730 20 69 6e 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 in one row of t
a4740 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f he result set */
a4750 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73 43 . char **azResC
a4760 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 56 61 olumn; /* Va
a4770 6c 75 65 73 20 66 6f 72 20 6f 6e 65 20 72 6f 77 lues for one row
a4780 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 20 0a 20 of result */ .
a4790 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 char *zErrMsg;
a47a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
a47b0 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 r message writte
a47c0 6e 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 n here */. Mem
a47d0 2a 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20 *pResultSet;
a47e0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
a47f0 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 o an array of re
a4800 73 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 65 78 sults */. u8 ex
a4810 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 plain;
a4820 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 45 58 /* True if EX
a4830 50 4c 41 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e PLAIN present on
a4840 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a SQL command */.
a4850 20 20 75 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e u8 changeCntOn
a4860 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
a4870 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 e to update the
a4880 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a change-counter *
a4890 2f 0a 20 20 75 38 20 65 78 70 69 72 65 64 3b 20 /. u8 expired;
a48a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
a48b0 72 75 65 20 69 66 20 74 68 65 20 56 4d 20 6e 65 rue if the VM ne
a48c0 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 eds to be recomp
a48d0 69 6c 65 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e iled */. u8 min
a48e0 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b WriteFileFormat;
a48f0 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c /* Minimum fil
a4900 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 77 72 69 e format for wri
a4910 74 61 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 table database f
a4920 69 6c 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56 iles */. u8 inV
a4930 74 61 62 4d 65 74 68 6f 64 3b 20 20 20 20 20 20 tabMethod;
a4940 20 20 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 /* See comment
a4950 73 20 61 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20 s above */. u8
a4960 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b usesStmtJournal;
a4970 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a4980 75 73 65 73 20 61 20 73 74 61 74 65 6d 65 6e 74 uses a statement
a4990 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 journal */. u8
a49a0 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 readOnly;
a49b0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
a49c0 72 20 72 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74 r read-only stat
a49d0 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 75 38 20 69 ements */. u8 i
a49e0 73 50 72 65 70 61 72 65 56 32 3b 20 20 20 20 20 sPrepareV2;
a49f0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
a4a00 72 65 70 61 72 65 64 20 77 69 74 68 20 70 72 65 repared with pre
a4a10 70 61 72 65 5f 76 32 28 29 20 2a 2f 0a 20 20 69 pare_v2() */. i
a4a20 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 nt nChange;
a4a30 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
a4a40 20 6f 66 20 64 62 20 63 68 61 6e 67 65 73 20 6d of db changes m
a4a50 61 64 65 20 73 69 6e 63 65 20 6c 61 73 74 20 72 ade since last r
a4a60 65 73 65 74 20 2a 2f 0a 20 20 69 36 34 20 73 74 eset */. i64 st
a4a70 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20 20 20 artTime;
a4a80 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e 20 71 /* Time when q
a4a90 75 65 72 79 20 73 74 61 72 74 65 64 20 2d 20 75 uery started - u
a4aa0 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c 69 6e sed for profilin
a4ab0 67 20 2a 2f 0a 20 20 69 6e 74 20 62 74 72 65 65 g */. int btree
a4ac0 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f Mask; /
a4ad0 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 64 62 2d * Bitmask of db-
a4ae0 3e 61 44 62 5b 5d 20 65 6e 74 72 69 65 73 20 72 >aDb[] entries r
a4af0 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 42 eferenced */. B
a4b00 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 61 treeMutexArray a
a4b10 4d 75 74 65 78 3b 20 2f 2a 20 41 6e 20 61 72 72 Mutex; /* An arr
a4b20 61 79 20 6f 66 20 42 74 72 65 65 20 75 73 65 64 ay of Btree used
a4b30 20 68 65 72 65 20 61 6e 64 20 6e 65 65 64 69 6e here and needin
a4b40 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e 74 g locks */. int
a4b50 20 61 43 6f 75 6e 74 65 72 5b 32 5d 3b 20 20 20 aCounter[2];
a4b60 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73 /* Counters
a4b70 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 used by sqlite3
a4b80 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a _stmt_status() *
a4b90 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 /. char *zSql;
a4ba0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
a4bb0 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 t of the SQL sta
a4bc0 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65 tement that gene
a4bd0 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20 20 rated this */.
a4be0 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20 20 void *pFree;
a4bf0 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
a4c00 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74 69 this when deleti
a4c10 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a 23 ng the vdbe */.#
a4c20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
a4c30 55 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 UG. FILE *trace
a4c40 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 ; /* Wr
a4c50 69 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f 6e ite an execution
a4c60 20 74 72 61 63 65 20 68 65 72 65 2c 20 69 66 20 trace here, if
a4c70 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 not NULL */.#end
a4c80 69 66 0a 20 20 69 6e 74 20 69 53 74 61 74 65 6d if. int iStatem
a4c90 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ent; /*
a4ca0 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72 Statement number
a4cb0 20 28 6f 72 20 30 20 69 66 20 68 61 73 20 6e 6f (or 0 if has no
a4cc0 74 20 6f 70 65 6e 65 64 20 73 74 6d 74 29 20 2a t opened stmt) *
a4cd0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
a4ce0 53 53 45 0a 20 20 69 6e 74 20 66 65 74 63 68 49 SSE. int fetchI
a4cf0 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 d; /* S
a4d00 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20 tatement number
a4d10 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f used by sqlite3_
a4d20 66 65 74 63 68 5f 73 74 61 74 65 6d 65 6e 74 20 fetch_statement
a4d30 2a 2f 0a 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 */. int lru;
a4d40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
a4d50 75 6e 74 65 72 20 75 73 65 64 20 66 6f 72 20 4c unter used for L
a4d60 52 55 20 63 61 63 68 65 20 72 65 70 6c 61 63 65 RU cache replace
a4d70 6d 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 ment */.#endif.#
a4d80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
a4d90 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
a4da0 45 4d 45 4e 54 0a 20 20 56 64 62 65 20 2a 70 4c EMENT. Vdbe *pL
a4db0 72 75 50 72 65 76 3b 0a 20 20 56 64 62 65 20 2a ruPrev;. Vdbe *
a4dc0 70 4c 72 75 4e 65 78 74 3b 0a 23 65 6e 64 69 66 pLruNext;.#endif
a4dd0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .};../*.** The f
a4de0 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c ollowing are all
a4df0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 owed values for
a4e00 56 64 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 Vdbe.magic.*/.#d
a4e10 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 efine VDBE_MAGIC
a4e20 5f 49 4e 49 54 20 20 20 20 20 30 78 32 36 62 63 _INIT 0x26bc
a4e30 65 61 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 eaa5 /* Build
a4e40 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 ing a VDBE progr
a4e50 61 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 am */.#define VD
a4e60 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 BE_MAGIC_RUN
a4e70 20 20 30 78 62 64 66 32 30 64 61 33 20 20 20 20 0xbdf20da3
a4e80 2f 2a 20 56 44 42 45 20 69 73 20 72 65 61 64 79 /* VDBE is ready
a4e90 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 to execute */.#
a4ea0 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 define VDBE_MAGI
a4eb0 43 5f 48 41 4c 54 20 20 20 20 20 30 78 35 31 39 C_HALT 0x519
a4ec0 63 32 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45 c2973 /* VDBE
a4ed0 20 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 has completed e
a4ee0 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 xecution */.#def
a4ef0 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 ine VDBE_MAGIC_D
a4f00 45 41 44 20 20 20 20 20 30 78 62 36 30 36 63 33 EAD 0xb606c3
a4f10 63 38 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 c8 /* The VDB
a4f20 45 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c E has been deall
a4f30 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a ocated */../*.**
a4f40 20 46 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 Function protot
a4f50 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ypes.*/.SQLITE_P
a4f60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a4f70 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f te3VdbeFreeCurso
a4f80 72 28 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 r(Vdbe *, VdbeCu
a4f90 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c rsor*);.void sql
a4fa0 69 74 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28 iteVdbePopStack(
a4fb0 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 Vdbe*,int);.SQLI
a4fc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a4fd0 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
a4fe0 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f Moveto(VdbeCurso
a4ff0 72 2a 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 r*);.#if defined
a5000 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c (SQLITE_DEBUG) |
a5010 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 | defined(VDBE_P
a5020 52 4f 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 ROFILE).SQLITE_P
a5030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a5040 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 te3VdbePrintOp(F
a5050 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b ILE*, int, Op*);
a5060 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 .#endif.SQLITE_P
a5070 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a5080 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
a5090 4c 65 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 Len(u32);.SQLITE
a50a0 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c _PRIVATE u32 sql
a50b0 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
a50c0 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 pe(Mem*, int);.S
a50d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
a50e0 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 t sqlite3VdbeSer
a50f0 69 61 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20 ialPut(unsigned
a5100 63 68 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a char*, int, Mem*
a5110 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
a5120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a5130 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 e3VdbeSerialGet(
a5140 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
a5150 68 61 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 har*, u32, Mem*)
a5160 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a5170 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
a5180 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 eDeleteAuxData(V
a5190 64 62 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a dbeFunc*, int);.
a51a0 0a 69 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65 .int sqlite2Btre
a51b0 65 4b 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75 eKeyCompare(BtCu
a51c0 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f rsor *, const vo
a51d0 69 64 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 id *, int, int,
a51e0 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int *);.SQLITE_P
a51f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a5200 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 e3VdbeIdxKeyComp
a5210 61 72 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c are(VdbeCursor*,
a5220 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c UnpackedRecord*,
a5230 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
a5240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a5250 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 42 74 3VdbeIdxRowid(Bt
a5260 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 Cursor *, i64 *)
a5270 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a5280 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 int sqlite3MemC
a5290 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d ompare(const Mem
a52a0 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 *, const Mem*, c
a52b0 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a onst CollSeq*);.
a52c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a52d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 nt sqlite3VdbeEx
a52e0 65 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 ec(Vdbe*);.SQLIT
a52f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
a5300 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 56 64 lite3VdbeList(Vd
a5310 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
a5320 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
a5330 56 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b VdbeHalt(Vdbe*);
a5340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a5350 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
a5360 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 hangeEncoding(Me
a5370 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 m *, int);.SQLIT
a5380 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
a5390 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 lite3VdbeMemTooB
a53a0 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ig(Mem*);.SQLITE
a53b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a53c0 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 ite3VdbeMemCopy(
a53d0 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a Mem*, const Mem*
a53e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a53f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
a5400 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
a5410 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d (Mem*, const Mem
a5420 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
a5430 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a5440 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
a5450 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c Mem*, Mem*);.SQL
a5460 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
a5470 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
a5480 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 lTerminate(Mem*)
a5490 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
a54a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
a54b0 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 MemSetStr(Mem*,
a54c0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
a54d0 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f , u8, void(*)(vo
a54e0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 id*));.SQLITE_PR
a54f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a5500 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
a5510 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4(Mem*, i64);.SQ
a5520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a5530 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
a5540 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 SetDouble(Mem*,
a5550 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f double);.SQLITE_
a5560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a5570 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
a5580 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ll(Mem*);.SQLITE
a5590 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a55a0 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a lite3VdbeMemSetZ
a55b0 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 eroBlob(Mem*,int
a55c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a55d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
a55e0 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d beMemSetRowSet(M
a55f0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
a5600 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
a5610 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
a5620 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 able(Mem*);.SQLI
a5630 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a5640 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
a5650 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 ingify(Mem*, int
a5660 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a5670 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62 E i64 sqlite3Vdb
a5680 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b eIntValue(Mem*);
a5690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a56a0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
a56b0 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d emIntegerify(Mem
a56c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
a56d0 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 TE double sqlite
a56e0 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 3VdbeRealValue(M
a56f0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
a5700 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a5710 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 3VdbeIntegerAffi
a5720 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 nity(Mem*);.SQLI
a5730 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a5740 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 qlite3VdbeMemRea
a5750 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 lify(Mem*);.SQLI
a5760 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a5770 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d qlite3VdbeMemNum
a5780 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c erify(Mem*);.SQL
a5790 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
a57a0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
a57b0 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f 72 omBtree(BtCursor
a57c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 *,int,int,int,Me
a57d0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
a57e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
a57f0 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d VdbeMemRelease(M
a5800 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 em *p);.SQLITE_P
a5810 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a5820 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
a5830 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 eExternal(Mem *p
a5840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a5850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
a5860 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d eMemFinalize(Mem
a5870 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 *, FuncDef*);.SQ
a5880 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
a5890 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
a58a0 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b OpcodeName(int);
a58b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a58c0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f int sqlite3VdbeO
a58d0 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 pcodeHasProperty
a58e0 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 (int, int);.SQLI
a58f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a5900 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
a5910 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 w(Mem *pMem, int
a5920 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 n, int preserve
a5930 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
a5940 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
a5950 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 eCloseStatement(
a5960 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 Vdbe *, int);.#i
a5970 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
a5980 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 LE_MEMORY_MANAGE
a5990 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 MENT.SQLITE_PRIV
a59a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
a59b0 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 dbeReleaseBuffer
a59c0 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64 s(Vdbe *p);.#end
a59d0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
a59e0 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
a59f0 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 ACHE.SQLITE_PRIV
a5a00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
a5a10 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e VdbeMutexArrayEn
a5a20 74 65 72 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 ter(Vdbe *p);.#e
a5a30 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c lse.# define sql
a5a40 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 ite3VdbeMutexArr
a5a50 61 79 45 6e 74 65 72 28 70 29 0a 23 65 6e 64 69 ayEnter(p).#endi
a5a60 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
a5a70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
a5a80 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 eMemTranslate(Me
a5a90 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65 66 20 m*, u8);.#ifdef
a5aa0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
a5ab0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
a5ac0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 id sqlite3VdbePr
a5ad0 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 intSql(Vdbe*);.S
a5ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
a5af0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
a5b00 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d MemPrettyPrint(M
a5b10 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a em *pMem, char *
a5b20 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a 53 51 zBuf);.#endif.SQ
a5b30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
a5b40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 sqlite3VdbeMemH
a5b50 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d andleBom(Mem *pM
a5b60 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 em);..#ifndef SQ
a5b70 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
a5b80 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 OB.SQLITE_PRIVAT
a5b90 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 E int sqlite3V
a5ba0 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
a5bb0 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 (Mem *);.#else.
a5bc0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
a5bd0 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
a5be0 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 b(x) SQLITE_OK.#
a5bf0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
a5c00 20 21 64 65 66 69 6e 65 64 28 5f 56 44 42 45 49 !defined(_VDBEI
a5c10 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a NT_H_) */../****
a5c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
a5c30 66 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a f vdbeInt.h ****
a5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
a5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
a5c80 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
a5c90 65 66 74 20 6f 66 66 20 69 6e 20 75 74 66 2e 63 eft off in utf.c
a5ca0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
a5cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e *********/..#ifn
a5cc0 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 def SQLITE_AMALG
a5cd0 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54 68 AMATION./*.** Th
a5ce0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 e following cons
a5cf0 74 61 6e 74 20 76 61 6c 75 65 20 69 73 20 75 73 tant value is us
a5d00 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 ed by the SQLITE
a5d10 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 0a 2a _BIGENDIAN and.*
a5d20 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 * SQLITE_LITTLEE
a5d30 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f NDIAN macros..*/
a5d40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a5d50 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 const int sqlite
a5d60 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 3one = 1;.#endif
a5d70 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 /* SQLITE_AMALG
a5d80 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a AMATION */../*.*
a5d90 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 20 74 61 * This lookup ta
a5da0 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 ble is used to h
a5db0 65 6c 70 20 64 65 63 6f 64 65 20 74 68 65 20 66 elp decode the f
a5dc0 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 irst byte of.**
a5dd0 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 55 54 46 a multi-byte UTF
a5de0 38 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 8 character..*/.
a5df0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
a5e00 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 igned char sqlit
a5e10 65 33 55 74 66 38 54 72 61 6e 73 31 5b 5d 20 3d e3Utf8Trans1[] =
a5e20 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c {. 0x00, 0x01,
a5e30 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 0x02, 0x03, 0x0
a5e40 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 4, 0x05, 0x06, 0
a5e50 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 x07,. 0x08, 0x0
a5e60 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 9, 0x0a, 0x0b, 0
a5e70 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c x0c, 0x0d, 0x0e,
a5e80 20 30 78 30 66 2c 0a 20 20 30 78 31 30 2c 20 30 0x0f,. 0x10, 0
a5e90 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31 33 2c x11, 0x12, 0x13,
a5ea0 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30 78 31 0x14, 0x15, 0x1
a5eb0 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 31 38 2c 6, 0x17,. 0x18,
a5ec0 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30 78 31 0x19, 0x1a, 0x1
a5ed0 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c 20 30 b, 0x1c, 0x1d, 0
a5ee0 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 30 78 30 x1e, 0x1f,. 0x0
a5ef0 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 0, 0x01, 0x02, 0
a5f00 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c x03, 0x04, 0x05,
a5f10 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 0x06, 0x07,. 0
a5f20 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c x08, 0x09, 0x0a,
a5f30 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 0x0b, 0x0c, 0x0
a5f40 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 d, 0x0e, 0x0f,.
a5f50 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
a5f60 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 2, 0x03, 0x04, 0
a5f70 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c x05, 0x06, 0x07,
a5f80 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 . 0x00, 0x01, 0
a5f90 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 30 2c x02, 0x03, 0x00,
a5fa0 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 0x01, 0x00, 0x0
a5fb0 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 0,.};...#define
a5fc0 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c WRITE_UTF8(zOut,
a5fd0 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 c) {
a5fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a5ff0 0a 20 20 69 66 28 20 63 3c 30 78 30 30 30 38 30 . if( c<0x00080
a6000 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
a6010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6020 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
a6030 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 Out++ = (u8)(c&0
a6040 78 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 xFF);
a6050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6060 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
a6070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6090 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 6c \. el
a60a0 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 30 30 se if( c<0x00800
a60b0 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
a60c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a60d0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
a60e0 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29 28 28 = 0xC0 + (u8)((
a60f0 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 20 20 20 c>>6)&0x1F);
a6100 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a6110 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 *zOut++ = 0x80
a6120 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46 + (u8)(c & 0x3F
a6130 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
a6140 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 \. }
a6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a6180 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 31 else if( c<0x1
a6190 30 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 0000 ){
a61a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a61b0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
a61c0 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b 20 28 75 ut++ = 0xE0 + (u
a61d0 38 29 28 28 63 3e 3e 31 32 29 26 30 78 30 46 29 8)((c>>12)&0x0F)
a61e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a61f0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
a6200 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0x80 + (u8)((c>>
a6210 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 6) & 0x3F);
a6220 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
a6230 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
a6240 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 (u8)(c & 0x3F);
a6250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6260 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 \. }else{
a6270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6290 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a62a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 *zOut++ = 0xF0
a62b0 2b 20 28 75 38 29 28 28 63 3e 3e 31 38 29 20 26 + (u8)((c>>18) &
a62c0 20 30 78 30 37 29 3b 20 20 20 20 20 20 20 20 20 0x07);
a62d0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
a62e0 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 + = 0x80 + (u8)(
a62f0 28 63 3e 3e 31 32 29 20 26 20 30 78 33 46 29 3b (c>>12) & 0x3F);
a6300 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a6310 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
a6320 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20 0 + (u8)((c>>6)
a6330 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 & 0x3F);
a6340 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
a6350 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
a6360 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 )(c & 0x3F);
a6370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
a6380 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 . }
a6390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a63a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a63b0 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 \.}..#de
a63c0 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36 fine WRITE_UTF16
a63d0 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 LE(zOut, c) {
a63e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a63f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6400 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46 \. if( c<=0xFF
a6410 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 FF ){
a6420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6440 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
a6450 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 ut++ = (u8)(c&0x
a6460 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 00FF);
a6470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6480 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a6490 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
a64a0 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29 )((c>>8)&0x00FF)
a64b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a64c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a64d0 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 \. }else{
a64e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a64f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6510 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
a6520 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 28 zOut++ = (u8)(((
a6530 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b c>>10)&0x003F) +
a6540 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e (((c-0x10000)>>
a6550 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 10)&0x00C0)); \
a6560 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
a6570 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28 u8)(0x00D8 + (((
a6580 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 c-0x10000)>>18)&
a6590 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 0x03));
a65a0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
a65b0 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 ++ = (u8)(c&0x00
a65c0 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 FF);
a65d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a65e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a65f0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
a6600 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 0x00DC + ((c>>8)
a6610 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 &0x03));
a6620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6630 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
a6640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6670 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 \.}..#def
a6680 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36 42 ine WRITE_UTF16B
a6690 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 E(zOut, c) {
a66a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a66b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a66c0 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46 46 \. if( c<=0xFFF
a66d0 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 F ){
a66e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a66f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6700 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
a6710 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 t++ = (u8)((c>>8
a6720 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 )&0x00FF);
a6730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6740 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a6750 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
a6760 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 (c&0x00FF);
a6770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6790 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 \. }else{
a67a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a67b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a67c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a67d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
a67e0 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 Out++ = (u8)(0x0
a67f0 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 0D8 + (((c-0x100
a6800 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 00)>>18)&0x03));
a6810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a6820 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
a6830 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 8)(((c>>10)&0x00
a6840 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 3F) + (((c-0x100
a6850 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 00)>>10)&0x00C0)
a6860 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b ); \. *zOut+
a6870 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43 20 + = (u8)(0x00DC
a6880 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 + ((c>>8)&0x03))
a6890 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a68a0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
a68b0 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 *zOut++ = (u8)(c
a68c0 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 &0x00FF);
a68d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a68f0 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 \. }
a6900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6930 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 \.}..#defi
a6940 6e 65 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 ne READ_UTF16LE(
a6950 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20 20 zIn, c){
a6960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6980 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b \. c = (*zIn++
a6990 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
a69a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a69b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a69c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b \. c +
a69d0 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b = ((*zIn++)<<8);
a69e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a69f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a10 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38 \. if( c>=0xD8
a6a20 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 29 00 && c<0xE000 )
a6a30 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
a6a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 \. i
a6a60 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 nt c2 = (*zIn++)
a6a70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a6a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6aa0 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 28 2a \. c2 += ((*
a6ab0 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 zIn++)<<8);
a6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ae0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 \. c
a6af0 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b = (c2&0x03FF) +
a6b00 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30 ((c&0x003F)<<10
a6b10 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29 ) + (((c&0x03C0)
a6b20 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 +0x0040)<<10);
a6b30 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
a6b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6b70 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 \.}..#d
a6b80 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 efine READ_UTF16
a6b90 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 BE(zIn, c){
a6ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6bc0 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a \. c = ((*z
a6bd0 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 In++)<<8);
a6be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c00 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a6c10 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 c += (*zIn++);
a6c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c50 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 \. if( c>=0
a6c60 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 xD800 && c<0xE00
a6c70 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
a6c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6c90 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a6ca0 20 20 69 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49 int c2 = ((*zI
a6cb0 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 n++)<<8);
a6cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6ce0 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 \. c2 +=
a6cf0 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 (*zIn++);
a6d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
a6d30 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 c = (c2&0x03FF
a6d40 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c ) + ((c&0x003F)<
a6d50 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 <10) + (((c&0x03
a6d60 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 C0)+0x0040)<<10)
a6d70 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 ; \. }
a6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6db0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
a6dc0 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 ./*.** Translate
a6dd0 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 a single UTF-8
a6de0 63 68 61 72 61 63 74 65 72 2e 20 20 52 65 74 75 character. Retu
a6df0 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 20 76 rn the unicode v
a6e00 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 alue..**.** Duri
a6e10 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2c 20 ng translation,
a6e20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 assume that the
a6e30 62 79 74 65 20 74 68 61 74 20 7a 54 65 72 6d 20 byte that zTerm
a6e40 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61 20 30 points.** is a 0
a6e50 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 x00..**.** Write
a6e60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
a6e70 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20 62 79 e next unread by
a6e80 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a 70 7a te back into *pz
a6e90 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 Next..**.** Note
a6ea0 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55 54 46 s On Invalid UTF
a6eb0 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 -8:.**.** * Th
a6ec0 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 is routine never
a6ed0 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69 74 20 allows a 7-bit
a6ee0 63 68 61 72 61 63 74 65 72 20 28 30 78 30 30 20 character (0x00
a6ef0 74 68 72 6f 75 67 68 20 30 78 37 66 29 20 74 6f through 0x7f) to
a6f00 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e 63 6f 64 .** be encod
a6f10 65 64 20 61 73 20 61 20 6d 75 6c 74 69 2d 62 79 ed as a multi-by
a6f20 74 65 20 63 68 61 72 61 63 74 65 72 2e 20 20 41 te character. A
a6f30 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 ny multi-byte ch
a6f40 61 72 61 63 74 65 72 20 74 68 61 74 0a 2a 2a 20 aracter that.**
a6f50 20 20 20 20 61 74 74 65 6d 70 74 73 20 74 6f 20 attempts to
a6f60 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62 encode a value b
a6f70 65 74 77 65 65 6e 20 30 78 30 30 20 61 6e 64 20 etween 0x00 and
a6f80 30 78 37 66 20 69 73 20 72 65 6e 64 65 72 65 64 0x7f is rendered
a6f90 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a as 0xfffd..**.*
a6fa0 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 * * This routi
a6fb0 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 ne never allows
a6fc0 61 20 55 54 46 31 36 20 73 75 72 72 6f 67 61 74 a UTF16 surrogat
a6fd0 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 65 6e e value to be en
a6fe0 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20 49 66 coded..** If
a6ff0 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 a multi-byte ch
a7000 61 72 61 63 74 65 72 20 61 74 74 65 6d 70 74 73 aracter attempts
a7010 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c to encode a val
a7020 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20 20 ue between.**
a7030 20 20 30 78 64 38 30 30 20 61 6e 64 20 30 78 65 0xd800 and 0xe
a7040 30 30 30 20 74 68 65 6e 20 69 74 20 69 73 20 72 000 then it is r
a7050 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 endered as 0xfff
a7060 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79 74 d..**.** * Byt
a7070 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20 es in the range
a7080 6f 66 20 30 78 38 30 20 74 68 72 6f 75 67 68 20 of 0x80 through
a7090 30 78 62 66 20 77 68 69 63 68 20 6f 63 63 75 72 0xbf which occur
a70a0 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a as the first.**
a70b0 20 20 20 20 20 62 79 74 65 20 6f 66 20 61 20 63 byte of a c
a70c0 68 61 72 61 63 74 65 72 20 61 72 65 20 69 6e 74 haracter are int
a70d0 65 72 70 72 65 74 65 64 20 61 73 20 73 69 6e 67 erpreted as sing
a70e0 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 le-byte characte
a70f0 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20 72 65 rs.** and re
a7100 6e 64 65 72 65 64 20 61 73 20 74 68 65 6d 73 65 ndered as themse
a7110 6c 76 65 73 20 65 76 65 6e 20 74 68 6f 75 67 68 lves even though
a7120 20 74 68 65 79 20 61 72 65 20 74 65 63 68 6e 69 they are techni
a7130 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69 6e 76 cally.** inv
a7140 61 6c 69 64 20 63 68 61 72 61 63 74 65 72 73 2e alid characters.
a7150 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 .**.** * This
a7160 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74 73 20 routine accepts
a7170 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 6d 62 an infinite numb
a7180 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 er of different
a7190 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 0a 2a UTF8 encodings.*
a71a0 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63 6f 64 * for unicod
a71b0 65 20 76 61 6c 75 65 73 20 30 78 38 30 20 61 6e e values 0x80 an
a71c0 64 20 67 72 65 61 74 65 72 2e 20 20 49 74 20 64 d greater. It d
a71d0 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 o not change ove
a71e0 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20 20 20 r-length.**
a71f0 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30 78 66 encodings to 0xf
a7200 66 66 64 20 61 73 20 73 6f 6d 65 20 73 79 73 74 ffd as some syst
a7210 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a 2a ems recommend..*
a7220 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 /.#define READ_U
a7230 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 TF8(zIn, zTerm,
a7240 63 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c)
a7250 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
a7260 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 20 20 c = *(zIn++);
a7270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7290 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
a72a0 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 ( c>=0xc0 ){
a72b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a72c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a72d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d \. c =
a72e0 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e sqlite3Utf8Tran
a72f0 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 20 20 20 s1[c-0xc0];
a7300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7310 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65 28 \. while(
a7320 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 26 20 28 zIn!=zTerm && (
a7330 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 *zIn & 0xc0)==0x
a7340 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 80 ){
a7350 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 28 63 3c \. c = (c<
a7360 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a 28 <6) + (0x3f & *(
a7370 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 20 20 20 zIn++));
a7380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a7390 20 20 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 }
a73a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a73b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a73c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
a73d0 20 69 66 28 20 63 3c 30 78 38 30 20 20 20 20 20 if( c<0x80
a73e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a73f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7400 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 \.
a7410 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 38 || (c&0xFFFFF8
a7420 30 30 29 3d 3d 30 78 44 38 30 30 20 20 20 20 20 00)==0xD800
a7430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7440 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c \. |
a7450 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45 29 | (c&0xFFFFFFFE)
a7460 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d ==0xFFFE ){ c =
a7470 20 30 78 46 46 46 44 3b 20 7d 20 20 20 20 20 20 0xFFFD; }
a7480 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 45 5f 50 \. }.SQLITE_P
a7490 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a74a0 65 33 55 74 66 38 52 65 61 64 28 0a 20 20 63 6f e3Utf8Read(. co
a74b0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
a74c0 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20 2f 2a r *zIn, /*
a74d0 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 55 First byte of U
a74e0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 20 2a TF-8 character *
a74f0 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e /. const unsign
a7500 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e 65 78 74 ed char **pzNext
a7510 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66 69 72 /* Write fir
a7520 73 74 20 62 79 74 65 20 70 61 73 74 20 55 54 46 st byte past UTF
a7530 2d 38 20 63 68 61 72 20 68 65 72 65 20 2a 2f 0a -8 char here */.
a7540 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a 20 20 2f ){. int c;.. /
a7550 2a 20 53 61 6d 65 20 61 73 20 52 45 41 44 5f 55 * Same as READ_U
a7560 54 46 38 28 29 20 61 62 6f 76 65 20 62 75 74 20 TF8() above but
a7570 77 69 74 68 6f 75 74 20 74 68 65 20 7a 54 65 72 without the zTer
a7580 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a m parameter.. *
a7590 2a 20 46 6f 72 20 74 68 69 73 20 72 6f 75 74 69 * For this routi
a75a0 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 20 74 68 ne, we assume th
a75b0 65 20 55 54 46 38 20 73 74 72 69 6e 67 20 69 73 e UTF8 string is
a75c0 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 always zero-ter
a75d0 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 minated.. */.
a75e0 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 0a 20 20 c = *(zIn++);.
a75f0 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 0a 20 if( c>=0xc0 ){.
a7600 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 c = sqlite3Ut
a7610 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d f8Trans1[c-0xc0]
a7620 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 7a ;. while( (*z
a7630 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30 In & 0xc0)==0x80
a7640 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 28 63 ){. c = (c
a7650 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a <<6) + (0x3f & *
a7660 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20 20 7d 0a (zIn++));. }.
a7670 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 0a 20 if( c<0x80.
a7680 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 || (c&0xF
a7690 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 FFFF800)==0xD800
a76a0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 . || (c&0
a76b0 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 xFFFFFFFE)==0xFF
a76c0 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46 46 FE ){ c = 0xFFF
a76d0 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a 4e 65 D; }. }. *pzNe
a76e0 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72 65 74 75 xt = zIn;. retu
a76f0 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a rn c;.}...../*.*
a7700 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c 41 * If the TRANSLA
a7710 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20 69 TE_TRACE macro i
a7720 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20 76 s defined, the v
a7730 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65 6d alue of each Mem
a7740 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20 6f is.** printed o
a7750 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65 20 n stderr on the
a7760 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75 74 way into and out
a7770 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d of sqlite3VdbeM
a7780 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a 2a emTranslate()..*
a7790 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54 52 / ./* #define TR
a77a0 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31 20 ANSLATE_TRACE 1
a77b0 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
a77c0 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a TE_OMIT_UTF16./*
a77d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
a77e0 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65 20 transforms the
a77f0 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65 6e internal text en
a7800 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 70 coding used by p
a7810 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72 65 Mem to.** desire
a7820 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20 65 dEnc. It is an e
a7830 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72 69 rror if the stri
a7840 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f 66 ng is already of
a7850 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 the desired.**
a7860 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66 20 encoding, or if
a7870 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20 63 *pMem does not c
a7880 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67 20 ontain a string
a7890 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 value..*/.SQLITE
a78a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a78b0 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 ite3VdbeMemTrans
a78c0 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 late(Mem *pMem,
a78d0 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a u8 desiredEnc){.
a78e0 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 int len;
a78f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a7900 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 Maximum length
a7910 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67 of output string
a7920 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 in bytes */. u
a7930 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f nsigned char *zO
a7940 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
a7950 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
a7960 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 uffer */. unsig
a7970 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 ned char *zIn;
a7980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7990 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61 74 /* Input iterat
a79a0 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 or */. unsigned
a79b0 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 char *zTerm;
a79c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a79d0 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f End of input */
a79e0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
a79f0 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 *z;
a7a00 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
a7a10 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 ut iterator */.
a7a20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 3b unsigned int c;
a7a30 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d .. assert( pMem
a7a40 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
a7a50 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
a7a60 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
a7a70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d ;. assert( pMem
a7a80 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 ->flags&MEM_Str
a7a90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
a7aa0 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 m->enc!=desiredE
a7ab0 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nc );. assert(
a7ac0 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a pMem->enc!=0 );.
a7ad0 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
a7ae0 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65 66 n>=0 );..#if def
a7af0 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 ined(TRANSLATE_T
a7b00 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64 RACE) && defined
a7b10 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 20 (SQLITE_DEBUG).
a7b20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 {. char zBuf
a7b30 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 [100];. sqlit
a7b40 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 e3VdbeMemPrettyP
a7b50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 rint(pMem, zBuf)
a7b60 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 ;. fprintf(st
a7b70 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20 25 derr, "INPUT: %
a7b80 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d s\n", zBuf);. }
a7b90 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 .#endif.. /* If
a7ba0 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e the translation
a7bb0 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d is between UTF-
a7bc0 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62 69 16 little and bi
a7bd0 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20 0a g endian, then .
a7be0 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73 ** all that is
a7bf0 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f 20 required is to
a7c00 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f 72 swap the byte or
a7c10 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20 69 der. This case i
a7c20 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 64 s handled. ** d
a7c30 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 ifferently from
a7c40 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a 2f the others.. */
a7c50 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 . if( pMem->enc
a7c60 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 !=SQLITE_UTF8 &&
a7c70 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51 4c desiredEnc!=SQL
a7c80 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 ITE_UTF8 ){.
a7c90 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e 74 u8 temp;. int
a7ca0 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 rc;. rc = sq
a7cb0 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 lite3VdbeMemMake
a7cc0 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b Writeable(pMem);
a7cd0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
a7ce0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
a7cf0 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
a7d00 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
a7d10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
a7d20 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 NOMEM;. }.
a7d30 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d zIn = (u8*)pMem
a7d40 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20 3d ->z;. zTerm =
a7d50 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31 &zIn[pMem->n&~1
a7d60 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 ];. while( zI
a7d70 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 n<zTerm ){.
a7d80 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20 temp = *zIn;.
a7d90 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e *zIn = *(zIn
a7da0 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b 2b +1);. zIn++
a7db0 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d ;. *zIn++ =
a7dc0 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 temp;. }.
a7dd0 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 pMem->enc = des
a7de0 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f 74 iredEnc;. got
a7df0 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3b o translate_out;
a7e00 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 6c . }.. /* Set l
a7e10 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 en to the maximu
a7e20 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 m number of byte
a7e30 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 s required in th
a7e40 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e e output buffer.
a7e50 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72 65 */. if( desire
a7e60 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
a7e70 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 8 ){. /* When
a7e80 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d converting from
a7e90 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61 78 UTF-16, the max
a7ea0 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73 75 imum growth resu
a7eb0 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 lts from. **
a7ec0 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32 2d translating a 2-
a7ed0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74 byte character t
a7ee0 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d 38 o a 4-byte UTF-8
a7ef0 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 character..
a7f00 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74 65 ** A single byte
a7f10 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 is required for
a7f20 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72 69 the output stri
a7f30 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 ng. ** nul-te
a7f40 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f rminator.. */
a7f50 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20 . pMem->n &=
a7f60 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d ~1;. len = pM
a7f70 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20 em->n * 2 + 1;.
a7f80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 }else{. /* W
a7f90 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 hen converting f
a7fa0 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 rom UTF-8 to UTF
a7fb0 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 -16 the maximum
a7fc0 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65 64 growth is caused
a7fd0 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 31 . ** when a 1
a7fe0 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 72 -byte UTF-8 char
a7ff0 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c 61 acter is transla
a8000 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 74 ted into a 2-byt
a8010 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20 e UTF-16. **
a8020 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20 62 character. Two b
a8030 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 65 ytes are require
a8040 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 d in the output
a8050 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a 20 buffer for the.
a8060 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e ** nul-termin
a8070 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ator.. */.
a8080 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a len = pMem->n *
a8090 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2 + 2;. }.. /
a80a0 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69 * Set zIn to poi
a80b0 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 nt at the start
a80c0 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 of the input buf
a80d0 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f fer and zTerm to
a80e0 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79 point 1. ** by
a80f0 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 2e te past the end.
a8100 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61 . **. ** Varia
a8110 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74 20 ble zOut is set
a8120 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 to point at the
a8130 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 73 output buffer, s
a8140 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20 20 pace obtained.
a8150 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ** from sqlite3_
a8160 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20 malloc().. */.
a8170 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d zIn = (u8*)pMem
a8180 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26 ->z;. zTerm = &
a8190 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 zIn[pMem->n];.
a81a0 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62 zOut = sqlite3Db
a81b0 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e MallocRaw(pMem->
a81c0 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20 db, len);. if(
a81d0 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 74 !zOut ){. ret
a81e0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
a81f0 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74 ;. }. z = zOut
a8200 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 ;.. if( pMem->e
a8210 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc==SQLITE_UTF8
a8220 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69 72 ){. if( desir
a8230 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 edEnc==SQLITE_UT
a8240 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f F16LE ){. /
a8250 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 * UTF-8 -> UTF-1
a8260 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6 Little-endian
a8270 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
a8280 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
a8290 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 /* c = sqli
a82a0 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c te3Utf8Read(zIn,
a82b0 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 zTerm, (const u
a82c0 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 8**)&zIn); */.
a82d0 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28 READ_UTF8(
a82e0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a zIn, zTerm, c);.
a82f0 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
a8300 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 F16LE(z, c);.
a8310 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
a8320 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65 assert( de
a8330 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
a8340 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20 _UTF16BE );.
a8350 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 /* UTF-8 -> UT
a8360 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 F-16 Big-endian
a8370 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
a8380 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
a8390 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 /* c = sqli
a83a0 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c te3Utf8Read(zIn,
a83b0 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 zTerm, (const u
a83c0 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 8**)&zIn); */.
a83d0 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28 READ_UTF8(
a83e0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a zIn, zTerm, c);.
a83f0 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
a8400 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 F16BE(z, c);.
a8410 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 }. }. p
a8420 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a Mem->n = (int)(z
a8430 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a - zOut);. *z
a8440 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b ++ = 0;. }else{
a8450 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73 . assert( des
a8460 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
a8470 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 20 UTF8 );. if(
a8480 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 pMem->enc==SQLIT
a8490 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 E_UTF16LE ){.
a84a0 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74 /* UTF-16 Lit
a84b0 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 tle-endian -> UT
a84c0 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 F-8 */. whi
a84d0 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
a84e0 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 . READ_UT
a84f0 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20 0a F16LE(zIn, c); .
a8500 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
a8510 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 F8(z, c);.
a8520 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
a8530 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69 67 /* UTF-16 Big
a8540 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38 -endian -> UTF-8
a8550 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
a8560 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 zIn<zTerm ){.
a8570 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 READ_UTF16
a8580 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 20 BE(zIn, c); .
a8590 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 WRITE_UTF8(
a85a0 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 z, c);. }.
a85b0 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e }. pMem->n
a85c0 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 = (int)(z - zOu
a85d0 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 t);. }. *z = 0
a85e0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 ;. assert( (pMe
a85f0 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 6e 63 m->n+(desiredEnc
a8600 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a ==SQLITE_UTF8?1:
a8610 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 2))<=len );.. s
a8620 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
a8630 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d ease(pMem);. pM
a8640 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d em->flags &= ~(M
a8650 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 EM_Static|MEM_Dy
a8660 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 n|MEM_Ephem);.
a8670 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 pMem->enc = desi
a8680 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e redEnc;. pMem->
a8690 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 flags |= (MEM_Te
a86a0 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 rm|MEM_Dyn);. p
a86b0 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 Mem->z = (char*)
a86c0 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d zOut;. pMem->zM
a86d0 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b alloc = pMem->z;
a86e0 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a ..translate_out:
a86f0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 .#if defined(TRA
a8700 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 NSLATE_TRACE) &&
a8710 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
a8720 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 DEBUG). {. c
a8730 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 har zBuf[100];.
a8740 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
a8750 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 mPrettyPrint(pMe
a8760 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 m, zBuf);. fp
a8770 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 4f rintf(stderr, "O
a8780 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 UTPUT: %s\n", zB
a8790 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a uf);. }.#endif.
a87a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
a87b0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 OK;.}../*.** Thi
a87c0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
a87d0 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 64 65 for a byte-orde
a87e0 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 62 65 r mark at the be
a87f0 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a ginning of the .
a8800 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 ** UTF-16 string
a8810 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d stored in *pMem
a8820 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 65 73 . If one is pres
a8830 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 ent, it is remov
a8840 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e ed and.** the en
a8850 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 coding of the Me
a8860 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 69 73 m adjusted. This
a8870 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
a8880 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65 t do any.** byte
a8890 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 6a 75 -swapping, it ju
a88a0 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20 st sets Mem.enc
a88b0 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a appropriately..*
a88c0 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 *.** The allocat
a88d0 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 79 6e ion (static, dyn
a88e0 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 20 65 amic etc.) and e
a88f0 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d ncoding of the M
a8900 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61 em may be.** cha
a8910 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 75 6e nged by this fun
a8920 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
a8930 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a8940 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c ite3VdbeMemHandl
a8950 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b eBom(Mem *pMem){
a8960 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
a8970 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 TE_OK;. u8 bom
a8980 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
a8990 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20 pMem->n>=0 );.
a89a0 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b if( pMem->n>1 ){
a89b0 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a 28 75 . u8 b1 = *(u
a89c0 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 8 *)pMem->z;.
a89d0 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75 38 20 u8 b2 = *(((u8
a89e0 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b *)pMem->z) + 1);
a89f0 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 . if( b1==0xF
a8a00 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20 29 7b E && b2==0xFF ){
a8a10 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c . bom = SQL
a8a20 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 ITE_UTF16BE;.
a8a30 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 }. if( b1==0
a8a40 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46 45 20 xFF && b2==0xFE
a8a50 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 ){. bom = S
a8a60 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 QLITE_UTF16LE;.
a8a70 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 }. }. . if
a8a80 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20 ( bom ){. rc
a8a90 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
a8aa0 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d MakeWriteable(pM
a8ab0 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d em);. if( rc=
a8ac0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
a8ad0 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 pMem->n -= 2
a8ae0 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 ;. memmove(
a8af0 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e pMem->z, &pMem->
a8b00 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a z[2], pMem->n);.
a8b10 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d pMem->z[pM
a8b20 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 em->n] = '\0';.
a8b30 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 pMem->z[pMe
a8b40 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a m->n+1] = '\0';.
a8b50 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
a8b60 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 s |= MEM_Term;.
a8b70 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d pMem->enc =
a8b80 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a bom;. }. }.
a8b90 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 return rc;.}.#
a8ba0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
a8bb0 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f OMIT_UTF16 */../
a8bc0 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46 *.** pZ is a UTF
a8bd0 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f -8 encoded unico
a8be0 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 de string. If nB
a8bf0 79 74 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e yte is less than
a8c00 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e zero,.** return
a8c10 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 the number of u
a8c20 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 nicode character
a8c30 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 s in pZ up to (b
a8c40 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 ut not including
a8c50 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30 ).** the first 0
a8c60 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e 42 79 x00 byte. If nBy
a8c70 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 te is not less t
a8c80 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e han zero, return
a8c90 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
a8ca0 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 f unicode charac
a8cb0 74 65 72 73 20 69 6e 20 74 68 65 20 66 69 72 73 ters in the firs
a8cc0 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 28 6f t nByte of pZ (o
a8cd0 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 r up to .** the
a8ce0 66 69 72 73 74 20 30 78 30 30 2c 20 77 68 69 63 first 0x00, whic
a8cf0 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 hever comes firs
a8d00 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 t)..*/.SQLITE_PR
a8d10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a8d20 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 3Utf8CharLen(con
a8d30 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e st char *zIn, in
a8d40 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 t nByte){. int
a8d50 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 r = 0;. const u
a8d60 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 8 *z = (const u8
a8d70 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 *)zIn;. const u
a8d80 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 8 *zTerm;. if(
a8d90 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 nByte>=0 ){.
a8da0 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 zTerm = &z[nByte
a8db0 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
a8dc0 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 zTerm = (const u
a8dd0 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 8*)(-1);. }. a
a8de0 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 ssert( z<=zTerm
a8df0 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d );. while( *z!=
a8e00 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 0 && z<zTerm ){.
a8e10 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f SQLITE_SKIP_
a8e20 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b UTF8(z);. r++
a8e30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
a8e40 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73 ;.}../* This tes
a8e50 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f t function is no
a8e60 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 t currently used
a8e70 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61 74 65 by the automate
a8e80 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a d test-suite. .*
a8e90 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20 6f 6e * Hence it is on
a8ea0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 ly available in
a8eb0 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f debug builds..*/
a8ec0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
a8ed0 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 ITE_TEST) && def
a8ee0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
a8ef0 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 G)./*.** Transla
a8f00 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d te UTF-8 to UTF-
a8f10 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61 8..**.** This ha
a8f20 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 s the effect of
a8f30 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 making sure that
a8f40 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 77 the string is w
a8f50 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 ell-formed.** UT
a8f60 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 20 63 F-8. Miscoded c
a8f70 68 61 72 61 63 74 65 72 73 20 61 72 65 20 72 65 haracters are re
a8f80 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 moved..**.** The
a8f90 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 translation is
a8fa0 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 28 73 done in-place (s
a8fb0 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70 6f 73 ince it is impos
a8fc0 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a sible for the.**
a8fd0 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38 20 65 correct UTF-8 e
a8fe0 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f ncoding to be lo
a8ff0 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61 6c 66 nger than a malf
a9000 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e ormed encoding).
a9010 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a9020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 TE int sqlite3Ut
a9030 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 f8To8(unsigned c
a9040 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 har *zIn){. uns
a9050 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 igned char *zOut
a9060 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e = zIn;. unsign
a9070 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20 ed char *zStart
a9080 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63 3b 0a = zIn;. u32 c;.
a9090 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d . while( zIn[0]
a90a0 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 ){. c = sqli
a90b0 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c te3Utf8Read(zIn,
a90c0 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 (const u8**)&zI
a90d0 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21 3d 30 n);. if( c!=0
a90e0 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20 20 57 xfffd ){. W
a90f0 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 RITE_UTF8(zOut,
a9100 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 c);. }. }.
a9110 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 *zOut = 0;. ret
a9120 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74 20 2d urn (int)(zOut -
a9130 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65 6e 64 zStart);.}.#end
a9140 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
a9150 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a TE_OMIT_UTF16./*
a9160 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 .** Convert a UT
a9170 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 74 F-16 string in t
a9180 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 he native encodi
a9190 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 ng into a UTF-8
a91a0 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 string..** Memor
a91b0 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 y to hold the UT
a91c0 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20 6f 62 F-8 string is ob
a91d0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
a91e0 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d te3_malloc and m
a91f0 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65 64 20 ust.** be freed
a9200 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 by the calling f
a9210 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e unction..**.** N
a9220 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 ULL is returned
a9230 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 if there is an a
a9240 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e llocation error.
a9250 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a9260 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
a9270 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33 Utf16to8(sqlite3
a9280 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 *db, const void
a9290 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b *z, int nByte){
a92a0 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 . Mem m;. mems
a92b0 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 et(&m, 0, sizeof
a92c0 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 (m));. m.db = d
a92d0 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 b;. sqlite3Vdbe
a92e0 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c MemSetStr(&m, z,
a92f0 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45 5f 55 nByte, SQLITE_U
a9300 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 TF16NATIVE, SQLI
a9310 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71 TE_STATIC);. sq
a9320 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
a9330 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49 ncoding(&m, SQLI
a9340 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28 20 TE_UTF8);. if(
a9350 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
a9360 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
a9370 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d dbeMemRelease(&m
a9380 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a );. m.z = 0;.
a9390 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 6d }. assert( (m
a93a0 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 .flags & MEM_Ter
a93b0 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c m)!=0 || db->mal
a93c0 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 locFailed );. a
a93d0 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 ssert( (m.flags
a93e0 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c & MEM_Str)!=0 ||
a93f0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
a9400 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d d );. return (m
a9410 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e .flags & MEM_Dyn
a9420 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c )!=0 ? m.z : sql
a9430 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
a9440 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 m.z);.}../*.**
a9450 70 5a 20 69 73 20 61 20 55 54 46 2d 31 36 20 65 pZ is a UTF-16 e
a9460 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20 73 ncoded unicode s
a9470 74 72 69 6e 67 20 61 74 20 6c 65 61 73 74 20 6e tring at least n
a9480 43 68 61 72 20 63 68 61 72 61 63 74 65 72 73 20 Char characters
a9490 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 long..** Return
a94a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
a94b0 74 65 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 tes in the first
a94c0 20 6e 43 68 61 72 20 75 6e 69 63 6f 64 65 20 63 nChar unicode c
a94d0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 69 6e 20 haracters.** in
a94e0 70 5a 2e 20 20 6e 43 68 61 72 20 6d 75 73 74 20 pZ. nChar must
a94f0 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e be non-negative.
a9500 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a9510 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 TE int sqlite3Ut
a9520 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 f16ByteLen(const
a9530 20 76 6f 69 64 20 2a 7a 49 6e 2c 20 69 6e 74 20 void *zIn, int
a9540 6e 43 68 61 72 29 7b 0a 20 20 69 6e 74 20 63 3b nChar){. int c;
a9550 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
a9560 20 63 6f 6e 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b const *z = zIn;
a9570 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 . int n = 0;.
a9580 69 66 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36 if( SQLITE_UTF16
a9590 4e 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 NATIVE==SQLITE_U
a95a0 54 46 31 36 42 45 20 29 7b 0a 20 20 20 20 2f 2a TF16BE ){. /*
a95b0 20 55 73 69 6e 67 20 61 6e 20 22 69 66 20 28 53 Using an "if (S
a95c0 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 QLITE_UTF16NATIV
a95d0 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 E==SQLITE_UTF16B
a95e0 45 29 22 20 63 6f 6e 73 74 72 75 63 74 20 68 65 E)" construct he
a95f0 72 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 6e re. ** and in
a9600 20 6f 74 68 65 72 20 70 61 72 74 73 20 6f 66 20 other parts of
a9610 74 68 69 73 20 66 69 6c 65 20 6d 65 61 6e 73 20 this file means
a9620 74 68 61 74 20 61 74 20 6f 6e 65 20 62 72 61 6e that at one bran
a9630 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e ch will. ** n
a9640 6f 74 20 62 65 20 63 6f 76 65 72 65 64 20 62 79 ot be covered by
a9650 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e coverage testin
a9660 67 20 6f 6e 20 61 6e 79 20 73 69 6e 67 6c 65 20 g on any single
a9670 68 6f 73 74 2e 20 42 75 74 20 63 6f 76 65 72 61 host. But covera
a9680 67 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 ge. ** will b
a9690 65 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 74 68 e complete if th
a96a0 65 20 74 65 73 74 73 20 61 72 65 20 72 75 6e 20 e tests are run
a96b0 6f 6e 20 62 6f 74 68 20 61 20 6c 69 74 74 6c 65 on both a little
a96c0 2d 65 6e 64 69 61 6e 20 61 6e 64 20 0a 20 20 20 -endian and .
a96d0 20 2a 2a 20 62 69 67 2d 65 6e 64 69 61 6e 20 68 ** big-endian h
a96e0 6f 73 74 2e 20 42 65 63 61 75 73 65 20 62 6f 74 ost. Because bot
a96f0 68 20 74 68 65 20 55 54 46 31 36 4e 41 54 49 56 h the UTF16NATIV
a9700 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 55 54 46 E and SQLITE_UTF
a9710 31 36 42 45 0a 20 20 20 20 2a 2a 20 6d 61 63 72 16BE. ** macr
a9720 6f 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 os are constant
a9730 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 at compile time
a9740 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e the compiler can
a9750 20 64 65 74 65 72 6d 69 6e 65 0a 20 20 20 20 2a determine. *
a9760 2a 20 77 68 69 63 68 20 62 72 61 6e 63 68 20 77 * which branch w
a9770 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f 77 65 64 2e ill be followed.
a9780 20 49 74 20 69 73 20 74 68 65 72 65 66 6f 72 65 It is therefore
a9790 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 6e 6f assumed that no
a97a0 20 72 75 6e 74 69 6d 65 0a 20 20 20 20 2a 2a 20 runtime. **
a97b0 70 65 6e 61 6c 74 79 20 69 73 20 70 61 69 64 20 penalty is paid
a97c0 66 6f 72 20 74 68 69 73 20 22 69 66 22 20 73 74 for this "if" st
a97d0 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a atement.. */.
a97e0 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 while( n<nCh
a97f0 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 ar ){. READ
a9800 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a _UTF16BE(z, c);.
a9810 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d n++;. }
a9820 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 . }else{. wh
a9830 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a ile( n<nChar ){.
a9840 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 READ_UTF16
a9850 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 LE(z, c);.
a9860 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n++;. }. }.
a9870 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 2d return (int)(z-
a9880 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 (unsigned char c
a9890 6f 6e 73 74 20 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a onst *)zIn);.}..
a98a0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
a98b0 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 TE_TEST)./*.** T
a98c0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
a98d0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54 alled from the T
a98e0 43 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e CL test function
a98f0 20 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66 "translate_self
a9900 74 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65 test"..** It che
a9910 63 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 69 cks that the pri
a9920 6d 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 69 mitives for seri
a9930 61 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65 alizing and dese
a9940 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61 rializing.** cha
a9950 72 61 63 74 65 72 73 20 69 6e 20 65 61 63 68 20 racters in each
a9960 65 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76 encoding are inv
a9970 65 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74 erses of each ot
a9980 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 her..*/.SQLITE_P
a9990 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a99a0 74 65 33 55 74 66 53 65 6c 66 54 65 73 74 28 76 te3UtfSelfTest(v
a99b0 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 oid){. unsigned
a99c0 20 69 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e 73 int i, t;. uns
a99d0 69 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66 5b igned char zBuf[
a99e0 32 30 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 20];. unsigned
a99f0 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 6e char *z;. int n
a9a00 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 ;. unsigned int
a9a10 20 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 c;.. for(i=0;
a9a20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b i<0x00110000; i+
a9a30 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 +){. z = zBuf
a9a40 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 ;. WRITE_UTF8
a9a50 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 (z, i);. n =
a9a60 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 (int)(z-zBuf);.
a9a70 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 assert( n>0 &
a9a80 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b & n<=4 );. z[
a9a90 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 0] = 0;. z =
a9aa0 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71 zBuf;. c = sq
a9ab0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c lite3Utf8Read(z,
a9ac0 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 (const u8**)&z)
a9ad0 3b 0a 20 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 ;. t = i;.
a9ae0 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 if( i>=0xD800 &
a9af0 26 20 69 3c 3d 30 78 44 46 46 46 20 29 20 74 20 & i<=0xDFFF ) t
a9b00 3d 20 30 78 46 46 46 44 3b 0a 20 20 20 20 69 66 = 0xFFFD;. if
a9b10 28 20 28 69 26 30 78 46 46 46 46 46 46 46 45 29 ( (i&0xFFFFFFFE)
a9b20 3d 3d 30 78 46 46 46 45 20 29 20 74 20 3d 20 30 ==0xFFFE ) t = 0
a9b30 78 46 46 46 44 3b 0a 20 20 20 20 61 73 73 65 72 xFFFD;. asser
a9b40 74 28 20 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 t( c==t );. a
a9b50 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d ssert( (z-zBuf)=
a9b60 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 =n );. }. for(
a9b70 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 i=0; i<0x0011000
a9b80 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 0; i++){. if(
a9b90 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c i>=0xD800 && i<
a9ba0 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 0xE000 ) continu
a9bb0 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b e;. z = zBuf;
a9bc0 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 . WRITE_UTF16
a9bd0 4c 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 LE(z, i);. n
a9be0 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b = (int)(z-zBuf);
a9bf0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 . assert( n>0
a9c00 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 && n<=4 );.
a9c10 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 z[0] = 0;. z
a9c20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44 = zBuf;. READ
a9c30 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a _UTF16LE(z, c);.
a9c40 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 69 assert( c==i
a9c50 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
a9c60 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 (z-zBuf)==n );.
a9c70 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
a9c80 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 0x00110000; i++)
a9c90 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 {. if( i>=0xD
a9ca0 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 800 && i<0xE000
a9cb0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
a9cc0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 z = zBuf;. WR
a9cd0 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 69 ITE_UTF16BE(z, i
a9ce0 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 );. n = (int)
a9cf0 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 (z-zBuf);. as
a9d00 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d sert( n>0 && n<=
a9d10 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 4 );. z[0] =
a9d20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0;. z = zBuf;
a9d30 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 . READ_UTF16B
a9d40 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 E(z, c);. ass
a9d50 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 ert( c==i );.
a9d60 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 assert( (z-zBuf
a9d70 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 )==n );. }.}.#e
a9d80 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 ndif /* SQLITE_T
a9d90 45 53 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a EST */.#endif /*
a9da0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
a9db0 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 16 */../********
a9dc0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 ****** End of ut
a9dd0 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.c ************
a9de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9e00 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
a9e10 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
a9e20 65 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a e util.c *******
a9e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9e50 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
a9e60 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
a9e70 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
a9e80 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
a9e90 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
a9ea0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
a9eb0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
a9ec0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
a9ed0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
a9ee0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
a9ef0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
a9f00 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
a9f10 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
a9f20 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
a9f30 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
a9f40 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
a9f50 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
a9f60 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
a9f70 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
a9f80 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
a9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
a9fd0 20 55 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f Utility functio
a9fe0 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f ns used througho
a9ff0 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a ut sqlite..**.**
aa000 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
aa010 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f ins functions fo
aa020 72 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d r allocating mem
aa030 6f 72 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a ory, comparing.*
aa040 2a 20 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73 * strings, and s
aa050 74 75 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a tuff like that..
aa060 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 69 6c 2e **.** $Id: util.
aa070 63 2c 76 20 31 2e 32 35 34 20 32 30 30 39 2f 30 c,v 1.254 2009/0
aa080 35 2f 30 36 20 31 39 3a 30 33 3a 31 34 20 64 72 5/06 19:03:14 dr
aa090 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 64 65 h Exp $.*/.#ifde
aa0a0 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 f SQLITE_HAVE_IS
aa0b0 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6d NAN.# include <m
aa0c0 61 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f ath.h>.#endif../
aa0d0 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 65 *.** Routine nee
aa0e0 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ded to support t
aa0f0 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 he testcase() ma
aa100 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 cro..*/.#ifdef S
aa110 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 QLITE_COVERAGE_T
aa120 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 EST.SQLITE_PRIVA
aa130 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
aa140 6f 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b 0a overage(int x){.
aa150 20 20 73 74 61 74 69 63 20 69 6e 74 20 64 75 6d static int dum
aa160 6d 79 20 3d 20 30 3b 0a 20 20 64 75 6d 6d 79 20 my = 0;. dummy
aa170 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a += x;.}.#endif..
aa180 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 /*.** Routine ne
aa190 65 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 eded to support
aa1a0 74 68 65 20 41 4c 57 41 59 53 28 29 20 61 6e 64 the ALWAYS() and
aa1b0 20 4e 45 56 45 52 28 29 20 6d 61 63 72 6f 73 2e NEVER() macros.
aa1c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d .**.** The argum
aa1d0 65 6e 74 20 74 6f 20 41 4c 57 41 59 53 28 29 20 ent to ALWAYS()
aa1e0 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 should always be
aa1f0 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 61 72 true and the ar
aa200 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 4e 45 56 gument.** to NEV
aa210 45 52 28 29 20 73 68 6f 75 6c 64 20 61 6c 77 61 ER() should alwa
aa220 79 73 20 62 65 20 66 61 6c 73 65 2e 20 20 49 66 ys be false. If
aa230 20 65 69 74 68 65 72 20 69 73 20 6e 6f 74 20 74 either is not t
aa240 68 65 20 63 61 73 65 0a 2a 2a 20 74 68 65 6e 20 he case.** then
aa250 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
aa260 63 61 6c 6c 65 64 20 69 6e 20 6f 72 64 65 72 20 called in order
aa270 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f to throw an erro
aa280 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f r..**.** This ro
aa290 75 74 69 6e 65 20 6f 6e 6c 79 20 65 78 69 73 74 utine only exist
aa2a0 73 20 69 66 20 61 73 73 65 72 74 28 29 20 69 73 s if assert() is
aa2b0 20 6f 70 65 72 61 74 69 6f 6e 61 6c 2e 20 20 49 operational. I
aa2c0 74 20 61 6c 77 61 79 73 0a 2a 2a 20 74 68 72 6f t always.** thro
aa2d0 77 73 20 61 6e 20 61 73 73 65 72 74 20 6f 6e 20 ws an assert on
aa2e0 69 74 73 20 66 69 72 73 74 20 69 6e 76 6f 63 61 its first invoca
aa2f0 74 69 6f 6e 2e 20 20 54 68 65 20 76 61 72 69 61 tion. The varia
aa300 62 6c 65 20 68 61 73 20 61 20 6c 6f 6e 67 0a 2a ble has a long.*
aa310 2a 20 6e 61 6d 65 20 74 6f 20 68 65 6c 70 20 74 * name to help t
aa320 68 65 20 61 73 73 65 72 74 28 29 20 6d 65 73 73 he assert() mess
aa330 61 67 65 20 62 65 20 6d 6f 72 65 20 72 65 61 64 age be more read
aa340 61 62 6c 65 2e 20 20 54 68 65 20 76 61 72 69 61 able. The varia
aa350 62 6c 65 0a 2a 2a 20 69 73 20 75 73 65 64 20 74 ble.** is used t
aa360 6f 20 70 72 65 76 65 6e 74 20 61 20 74 6f 6f 2d o prevent a too-
aa370 63 6c 65 76 65 72 20 6f 70 74 69 6d 69 7a 65 72 clever optimizer
aa380 20 66 72 6f 6d 20 6f 70 74 69 6d 69 7a 69 6e 67 from optimizing
aa390 20 6f 75 74 20 74 68 65 0a 2a 2a 20 65 6e 74 69 out the.** enti
aa3a0 72 65 20 63 61 6c 6c 2e 0a 2a 2f 0a 23 69 66 6e re call..*/.#ifn
aa3b0 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 def NDEBUG.SQLIT
aa3c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
aa3d0 6c 69 74 65 33 41 73 73 65 72 74 28 76 6f 69 64 lite3Assert(void
aa3e0 29 7b 0a 20 20 73 74 61 74 69 63 20 76 6f 6c 61 ){. static vola
aa3f0 74 69 6c 65 20 69 6e 74 20 41 4c 57 41 59 53 5f tile int ALWAYS_
aa400 77 61 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56 was_false_or_NEV
aa410 45 52 5f 77 61 73 5f 74 72 75 65 20 3d 20 30 3b ER_was_true = 0;
aa420 0a 20 20 61 73 73 65 72 74 28 20 41 4c 57 41 59 . assert( ALWAY
aa430 53 5f 77 61 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e S_was_false_or_N
aa440 45 56 45 52 5f 77 61 73 5f 74 72 75 65 20 29 3b EVER_was_true );
aa450 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 /* Always
aa460 66 61 69 6c 73 20 2a 2f 0a 20 20 72 65 74 75 72 fails */. retur
aa470 6e 20 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c n ALWAYS_was_fal
aa480 73 65 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f se_or_NEVER_was_
aa490 74 72 75 65 2b 2b 3b 20 20 20 20 20 20 20 2f 2a true++; /*
aa4a0 20 4e 6f 74 20 52 65 61 63 68 65 64 20 2a 2f 0a Not Reached */.
aa4b0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
aa4c0 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
aa4d0 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e he floating poin
aa4e0 74 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61 t value is Not a
aa4f0 20 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a Number (NaN)..*
aa500 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74 *.** Use the mat
aa510 68 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28 h library isnan(
aa520 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f ) function if co
aa530 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 mpiled with SQLI
aa540 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a TE_HAVE_ISNAN..*
aa550 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20 * Otherwise, we
aa560 68 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70 have our own imp
aa570 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 lementation that
aa580 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 works on most s
aa590 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ystems..*/.SQLIT
aa5a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
aa5b0 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c lite3IsNaN(doubl
aa5c0 65 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 e x){. int rc;
aa5d0 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72 /* The value r
aa5e0 65 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65 eturn */.#if !de
aa5f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 fined(SQLITE_HAV
aa600 45 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20 E_ISNAN). /*.
aa610 2a 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20 ** Systems that
aa620 73 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61 support the isna
aa630 6e 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 n() library func
aa640 74 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62 tion should prob
aa650 61 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75 ably. ** make u
aa660 73 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70 se of it by comp
aa670 69 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c iling with -DSQL
aa680 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20 ITE_HAVE_ISNAN.
aa690 20 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a But we have. *
aa6a0 2a 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e * found that man
aa6b0 79 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74 y systems do not
aa6c0 20 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20 have a working
aa6d0 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e isnan() function
aa6e0 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d so. ** this im
aa6f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
aa700 70 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61 provided as an a
aa710 6c 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a lternative.. **
aa720 0a 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74 . ** This NaN t
aa730 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61 est sometimes fa
aa740 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ils if compiled
aa750 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61 on GCC with -ffa
aa760 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e st-math.. ** On
aa770 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
aa780 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61 the use of -ffa
aa790 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69 st-math comes wi
aa7a0 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 th the following
aa7b0 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 . ** warning:.
aa7c0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 **. ** Th
aa7d0 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 is option [-ffas
aa7e0 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e t-math] should n
aa7f0 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f ever be turned o
aa800 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20 n by any. **
aa810 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e -O option sin
aa820 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74 ce it can result
aa830 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 in incorrect ou
aa840 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d tput for program
aa850 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63 s. ** whic
aa860 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 h depend on an e
aa870 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 xact implementat
aa880 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49 ion of IEEE or I
aa890 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75 SO . ** ru
aa8a0 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f les/specificatio
aa8b0 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63 ns for math func
aa8c0 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
aa8d0 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69 Under MSVC, thi
aa8e0 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66 s NaN test may f
aa8f0 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ail if compiled
aa900 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d with a floating-
aa910 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63 . ** point prec
aa920 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72 ision mode other
aa930 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73 than /fp:precis
aa940 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44 e. From the MSD
aa950 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 N . ** document
aa960 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a ation:. **. **
aa970 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c The compil
aa980 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65 er [with /fp:pre
aa990 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65 cise] will prope
aa9a0 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61 rly handle compa
aa9b0 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20 risons . **
aa9c0 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e involving NaN.
aa9d0 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20 For example, x
aa9e0 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74 != x evaluates t
aa9f0 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e o true if x is N
aaa00 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e aN . ** ..
aaa10 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f .. */.#ifdef __
aaa20 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 FAST_MATH__.# er
aaa30 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ror SQLite will
aaa40 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 not work correct
aaa50 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61 ly with the -ffa
aaa60 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f st-math option o
aaa70 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20 f GCC..#endif.
aaa80 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 volatile double
aaa90 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c y = x;. volatil
aaaa0 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a e double z = y;.
aaab0 20 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23 rc = (y!=z);.#
aaac0 65 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69 else /* if defi
aaad0 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f ned(SQLITE_HAVE_
aaae0 49 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d ISNAN) */. rc =
aaaf0 20 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69 isnan(x);.#endi
aab00 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45 f /* SQLITE_HAVE
aab10 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74 _ISNAN */. test
aab20 63 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65 case( rc );. re
aab30 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
aab40 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 * Compute a stri
aab50 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69 ng length that i
aab60 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61 s limited to wha
aab70 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 t can be stored
aab80 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62 in.** lower 30 b
aab90 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20 its of a 32-bit
aaba0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a signed integer..
aabb0 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
aabc0 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65 returned will ne
aabd0 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e ver be negative.
aabe0 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76 Nor will it ev
aabf0 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a er be greater.**
aac00 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c than the actual
aac10 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 length of the s
aac20 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79 tring. For very
aac30 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67 long strings (g
aac40 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31 reater.** than 1
aac50 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72 GiB) the value r
aac60 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65 eturned might be
aac70 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74 less than the t
aac80 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 rue string lengt
aac90 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 h..*/.SQLITE_PRI
aaca0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
aacb0 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63 Strlen30(const c
aacc0 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74 har *z){. const
aacd0 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 char *z2 = z;.
aace0 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 7b 20 7a while( *z2 ){ z
aacf0 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2++; }. return
aad00 30 78 33 66 66 66 66 66 66 66 20 26 20 28 69 6e 0x3fffffff & (in
aad10 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d 0a 0a 2f t)(z2 - z);.}../
aad20 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 6f 73 *.** Set the mos
aad30 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 t recent error c
aad40 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 73 74 ode and error st
aad50 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73 71 6c ring for the sql
aad60 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 22 64 ite.** handle "d
aad70 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20 63 6f b". The error co
aad80 64 65 20 69 73 20 73 65 74 20 74 6f 20 22 65 72 de is set to "er
aad90 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49 r_code"..**.** I
aada0 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c f it is not NULL
aadb0 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d 61 74 , string zFormat
aadc0 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 66 specifies the f
aadd0 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a 2a 20 ormat of the.**
aade0 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69 6e 20 error string in
aadf0 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74 68 65 the style of the
aae00 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69 6f 6e printf function
aae10 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 s: The following
aae20 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 72 61 .** format chara
aae30 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 cters are allowe
aae40 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 d:.**.** %s
aae50 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 Insert a s
aae60 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a tring.** %z
aae70 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 A string t
aae80 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 hat should be fr
aae90 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a eed after use.**
aaea0 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e %d In
aaeb0 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a sert an integer.
aaec0 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20 ** %T
aaed0 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a Insert a token.*
aaee0 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49 * %S I
aaef0 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20 nsert the first
aaf00 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 element of a Src
aaf10 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d List.**.** zForm
aaf20 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72 69 6e at and any strin
aaf30 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 66 6f g tokens that fo
aaf40 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73 73 75 llow it are assu
aaf50 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 6e 63 med to be.** enc
aaf60 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a oded in UTF-8..*
aaf70 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 74 68 *.** To clear th
aaf80 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 e most recent er
aaf90 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 20 68 ror for sqlite h
aafa0 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 6c 69 andle "db", sqli
aafb0 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 6f 75 te3Error.** shou
aafc0 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 ld be called wit
aafd0 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74 20 74 h err_code set t
aafe0 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 o SQLITE_OK and
aaff0 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a 20 74 zFormat set.** t
ab000 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 o NULL..*/.SQLIT
ab010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ab020 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71 6c 69 qlite3Error(sqli
ab030 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 72 72 te3 *db, int err
ab040 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68 61 _code, const cha
ab050 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 r *zFormat, ...)
ab060 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64 {. if( db && (d
ab070 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 2d 3e b->pErr || (db->
ab080 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33 56 61 pErr = sqlite3Va
ab090 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30 29 20 lueNew(db))!=0)
ab0a0 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 6f ){. db->errCo
ab0b0 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b 0a 20 de = err_code;.
ab0c0 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 20 29 if( zFormat )
ab0d0 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b {. char *z;
ab0e0 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74 20 61 . va_list a
ab0f0 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 61 72 p;. va_star
ab100 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
ab110 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 z = sqlite
ab120 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 3VMPrintf(db, zF
ab130 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 ormat, ap);.
ab140 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
ab150 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
ab160 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c SetStr(db->pErr,
ab170 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 5f 55 -1, z, SQLITE_U
ab180 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 TF8, SQLITE_DYNA
ab190 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b MIC);. }else{
ab1a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
ab1b0 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 lueSetStr(db->pE
ab1c0 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 rr, 0, 0, SQLITE
ab1d0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 _UTF8, SQLITE_ST
ab1e0 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ATIC);. }. }
ab1f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e .}../*.** Add an
ab200 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
ab210 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 o pParse->zErrMs
ab220 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 g and increment
ab230 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a pParse->nErr..**
ab240 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 The following f
ab250 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72 61 63 ormatting charac
ab260 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 ters are allowed
ab270 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 :.**.** %s
ab280 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 Insert a st
ab290 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 ring.** %z
ab2a0 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68 A string th
ab2b0 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 at should be fre
ab2c0 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 ed after use.**
ab2d0 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e 73 %d Ins
ab2e0 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a ert an integer.*
ab2f0 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20 49 * %T I
ab300 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a nsert a token.**
ab310 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49 6e %S In
ab320 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20 65 sert the first e
ab330 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c lement of a SrcL
ab340 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ist.**.** This f
ab350 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 unction should b
ab360 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f 72 74 e used to report
ab370 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61 74 20 any error that
ab380 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a 2a 2a occurs whilst.**
ab390 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 53 51 compiling an SQ
ab3a0 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69 2e 65 L statement (i.e
ab3b0 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 . within sqlite3
ab3c0 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54 68 65 _prepare()). The
ab3d0 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 20 74 .** last thing t
ab3e0 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 he sqlite3_prepa
ab3f0 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 64 6f re() function do
ab400 65 73 20 69 73 20 63 6f 70 79 20 74 68 65 20 65 es is copy the e
ab410 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 20 62 rror.** stored b
ab420 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 y this function
ab430 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
ab440 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 20 73 e handle using s
ab450 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a 2a qlite3Error()..*
ab460 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 * Function sqlit
ab470 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75 6c 64 e3Error() should
ab480 20 62 65 20 75 73 65 64 20 64 75 72 69 6e 67 20 be used during
ab490 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63 75 74 statement execut
ab4a0 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 5f ion.** (sqlite3_
ab4b0 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 2a 2f step() etc.)..*/
ab4c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ab4d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f void sqlite3Erro
ab4e0 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50 61 72 rMsg(Parse *pPar
ab4f0 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a se, const char *
ab500 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
ab510 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73 va_list ap;. s
ab520 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
ab530 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 72 73 rse->db;. pPars
ab540 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 74 65 73 e->nErr++;. tes
ab550 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e 7a tcase( pParse->z
ab560 45 72 72 4d 73 67 21 3d 30 20 29 3b 0a 20 20 73 ErrMsg!=0 );. s
ab570 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
ab580 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
ab590 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 );. va_start(ap
ab5a0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50 , zFormat);. pP
ab5b0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
ab5c0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
ab5d0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
ab5e0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
ab5f0 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 if( pParse->rc
ab600 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
ab610 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 pParse->rc =
ab620 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
ab630 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 }.}../*.** Clear
ab640 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
ab650 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66 ge in pParse, if
ab660 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 any.*/.SQLITE_P
ab670 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
ab680 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 te3ErrorClear(Pa
ab690 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 rse *pParse){.
ab6a0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 sqlite3DbFree(pP
ab6b0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 arse->db, pParse
ab6c0 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 ->zErrMsg);. pP
ab6d0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
ab6e0 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 0;. pParse->nEr
ab6f0 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r = 0;.}../*.**
ab700 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 Convert an SQL-s
ab710 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 tyle quoted stri
ab720 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c ng into a normal
ab730 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 string by remov
ab740 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 ing.** the quote
ab750 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 characters. Th
ab760 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 e conversion is
ab770 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 done in-place.
ab780 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 If the.** input
ab790 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 does not begin w
ab7a0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 ith a quote char
ab7b0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 acter, then this
ab7c0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 routine.** is a
ab7d0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 no-op..**.** Th
ab7e0 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d e input string m
ab7f0 75 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d ust be zero-term
ab800 69 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a inated. A new z
ab810 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a ero-terminator.*
ab820 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 * is added to th
ab830 65 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e e dequoted strin
ab840 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 g..**.** The ret
ab850 75 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20 urn value is -1
ab860 69 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20 if no dequoting
ab870 6f 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65 occurs or the le
ab880 6e 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 ngth of the.** d
ab890 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20 equoted string,
ab8a0 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 exclusive of the
ab8b0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
ab8c0 2c 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64 , if dequoting d
ab8d0 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a oes.** occur..**
ab8e0 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a .** 2002-Feb-14:
ab8f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
ab900 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d extended to rem
ab910 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74 ove MS-Access st
ab920 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20 yle.** brackets
ab930 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e from around iden
ab940 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61 tifers. For exa
ab950 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 mple: "[a-b-c]"
ab960 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 becomes.** "a-b
ab970 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 -c"..*/.SQLITE_P
ab980 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ab990 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a e3Dequote(char *
ab9a0 7a 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65 z){. char quote
ab9b0 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 ;. int i, j;.
ab9c0 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 if( z==0 ) retur
ab9d0 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20 n -1;. quote =
ab9e0 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 z[0];. switch(
ab9f0 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 quote ){. cas
aba00 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a e '\'': break;.
aba10 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 case '"':
aba20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
aba30 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 '`': break;
aba40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aba50 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 For MySQL compat
aba60 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 ibility */. c
aba70 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 ase '[': quote
aba80 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 = ']'; break;
aba90 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 /* For MS SqlSe
abaa0 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 rver compatibili
abab0 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c ty */. defaul
abac0 74 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b t: return -1;
abad0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 . }. for(i=1,
abae0 6a 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d j=0; ALWAYS(z[i]
abaf0 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 ); i++){. if(
abb00 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a z[i]==quote ){.
abb10 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d if( z[i+1]
abb20 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 ==quote ){.
abb30 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 z[j++] = quot
abb40 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a e;. i++;.
abb50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
abb60 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
abb70 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
abb80 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b z[j++] = z[
abb90 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 i];. }. }.
abba0 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 z[j] = 0;. retu
abbb0 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 rn j;.}../* Conv
abbc0 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e enient short-han
abbd0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70 d */.#define Upp
abbe0 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65 erToLower sqlite
abbf0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 3UpperToLower../
abc00 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d *.** Some system
abc10 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29 s have stricmp()
abc20 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73 . Others have s
abc30 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65 trcasecmp(). Be
abc40 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69 cause.** there i
abc50 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79 s no consistency
abc60 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65 , we will define
abc70 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c our own..*/.SQL
abc80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
abc90 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 sqlite3StrICmp(c
abca0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 onst char *zLeft
abcb0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 , const char *zR
abcc0 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65 ight){. registe
abcd0 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
abce0 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
abcf0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
abd00 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
abd10 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
abd20 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61 ght;. while( *a
abd30 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f !=0 && UpperToLo
abd40 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f wer[*a]==UpperTo
abd50 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b Lower[*b]){ a++;
abd60 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e b++; }. return
abd70 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
abd80 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 ] - UpperToLower
abd90 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 [*b];.}.SQLITE_P
abda0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
abdb0 65 33 53 74 72 4e 49 43 6d 70 28 63 6f 6e 73 74 e3StrNICmp(const
abdc0 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f char *zLeft, co
abdd0 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 nst char *zRight
abde0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 , int N){. regi
abdf0 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 63 68 ster unsigned ch
abe00 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d ar *a, *b;. a =
abe10 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
abe20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 *)zLeft;. b = (
abe30 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
abe40 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 zRight;. while(
abe50 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d N-- > 0 && *a!=
abe60 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 0 && UpperToLowe
abe70 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f r[*a]==UpperToLo
abe80 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 wer[*b]){ a++; b
abe90 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e ++; }. return N
abea0 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 72 54 6f <0 ? 0 : UpperTo
abeb0 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 Lower[*a] - Uppe
abec0 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a rToLower[*b];.}.
abed0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
abee0 55 45 20 69 66 20 7a 20 69 73 20 61 20 70 75 72 UE if z is a pur
abef0 65 20 6e 75 6d 65 72 69 63 20 73 74 72 69 6e 67 e numeric string
abf00 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20 . Return FALSE
abf10 61 6e 64 20 6c 65 61 76 65 0a 2a 2a 20 2a 72 65 and leave.** *re
abf20 61 6c 6e 75 6d 20 75 6e 63 68 61 6e 67 65 64 20 alnum unchanged
abf30 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 63 6f if the string co
abf40 6e 74 61 69 6e 73 20 61 6e 79 20 63 68 61 72 61 ntains any chara
abf50 63 74 65 72 20 77 68 69 63 68 20 69 73 20 6e 6f cter which is no
abf60 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 6e t.** part of a n
abf70 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 umber..**.** If
abf80 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 70 75 the string is pu
abf90 72 65 20 6e 75 6d 65 72 69 63 2c 20 73 65 74 20 re numeric, set
abfa0 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 54 52 55 45 *realnum to TRUE
abfb0 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a if the string.*
abfc0 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 * contains the '
abfd0 2e 27 20 63 68 61 72 61 63 74 65 72 20 6f 72 20 .' character or
abfe0 61 6e 20 22 45 2b 30 30 30 22 20 73 74 79 6c 65 an "E+000" style
abff0 20 65 78 70 6f 6e 65 6e 74 69 61 74 69 6f 6e 20 exponentiation
ac000 73 75 66 66 69 78 2e 0a 2a 2a 20 4f 74 68 65 72 suffix..** Other
ac010 77 69 73 65 20 73 65 74 20 2a 72 65 61 6c 6e 75 wise set *realnu
ac020 6d 20 74 6f 20 46 41 4c 53 45 2e 20 20 4e 6f 74 m to FALSE. Not
ac030 65 20 74 68 61 74 20 6a 75 73 74 20 62 65 63 61 e that just beca
ac040 75 65 20 2a 72 65 61 6c 6e 75 6d 20 69 73 0a 2a ue *realnum is.*
ac050 2a 20 66 61 6c 73 65 20 64 6f 65 73 20 6e 6f 74 * false does not
ac060 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6e mean that the n
ac070 75 6d 62 65 72 20 63 61 6e 20 62 65 20 73 75 63 umber can be suc
ac080 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 76 65 72 cessfully conver
ac090 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69 ted into.** an i
ac0a0 6e 74 65 67 65 72 20 2d 20 69 74 20 6d 69 67 68 nteger - it migh
ac0b0 74 20 62 65 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a t be too big..**
ac0c0 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 73 74 72 .** An empty str
ac0d0 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 65 72 65 ing is considere
ac0e0 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a d non-numeric..*
ac0f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
ac100 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 int sqlite3IsNu
ac110 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 mber(const char
ac120 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d *z, int *realnum
ac130 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 , u8 enc){. int
ac140 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 incr = (enc==SQ
ac150 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a LITE_UTF8?1:2);.
ac160 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 if( enc==SQLIT
ac170 45 5f 55 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b E_UTF16BE ) z++;
ac180 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c . if( *z=='-' |
ac190 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d | *z=='+' ) z +=
ac1a0 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 21 73 71 incr;. if( !sq
ac1b0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
ac1c0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
ac1d0 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 ;. }. z += inc
ac1e0 72 3b 0a 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 r;. *realnum =
ac1f0 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 0;. while( sqli
ac200 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 te3Isdigit(*z) )
ac210 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 { z += incr; }.
ac220 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a if( *z=='.' ){.
ac230 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 z += incr;.
ac240 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 if( !sqlite3I
ac250 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72 65 74 sdigit(*z) ) ret
ac260 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 urn 0;. while
ac270 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 ( sqlite3Isdigit
ac280 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 (*z) ){ z += inc
ac290 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75 r; }. *realnu
ac2a0 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 m = 1;. }. if(
ac2b0 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d *z=='e' || *z==
ac2c0 27 45 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 'E' ){. z +=
ac2d0 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a incr;. if( *z
ac2e0 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 =='+' || *z=='-'
ac2f0 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 ) z += incr;.
ac300 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 if( !sqlite3Is
ac310 64 69 67 69 74 28 2a 7a 29 20 29 20 72 65 74 75 digit(*z) ) retu
ac320 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 rn 0;. while(
ac330 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
ac340 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 *z) ){ z += incr
ac350 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d ; }. *realnum
ac360 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
ac370 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a rn *z==0;.}../*.
ac380 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 5b ** The string z[
ac390 5d 20 69 73 20 61 6e 20 61 73 63 69 69 20 72 65 ] is an ascii re
ac3a0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
ac3b0 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a a real number..*
ac3c0 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 73 * Convert this s
ac3d0 74 72 69 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c tring to a doubl
ac3e0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
ac3f0 75 74 69 6e 65 20 61 73 73 75 6d 65 73 20 74 68 utine assumes th
ac400 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 at z[] really is
ac410 20 61 20 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e a valid number.
ac420 20 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f If it.** is no
ac430 74 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 t, the result is
ac440 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
ac450 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
ac460 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f s used instead o
ac470 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 f the library at
ac480 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 of() function be
ac490 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 cause.** the lib
ac4a0 72 61 72 79 20 61 74 6f 66 28 29 20 6d 69 67 68 rary atof() migh
ac4b0 74 20 77 61 6e 74 20 74 6f 20 75 73 65 20 22 2c t want to use ",
ac4c0 22 20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c " as the decimal
ac4d0 20 70 6f 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a point instead.*
ac4e0 2a 20 6f 66 20 22 2e 22 20 64 65 70 65 6e 64 69 * of "." dependi
ac4f0 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 ng on how locale
ac500 20 69 73 20 73 65 74 2e 20 20 42 75 74 20 74 68 is set. But th
ac510 61 74 20 77 6f 75 6c 64 20 63 61 75 73 65 20 70 at would cause p
ac520 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 roblems.** for S
ac530 51 4c 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 QL. So this rou
ac540 74 69 6e 65 20 61 6c 77 61 79 73 20 75 73 65 73 tine always uses
ac550 20 22 2e 22 20 72 65 67 61 72 64 6c 65 73 73 20 "." regardless
ac560 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 of locale..*/.SQ
ac570 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac580 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e sqlite3AtoF(con
ac590 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 st char *z, doub
ac5a0 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 le *pResult){.#i
ac5b0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
ac5c0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
ac5d0 0a 20 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b . int sign = 1;
ac5e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
ac5f0 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20 4c 4f 4e Begin = z;. LON
ac600 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 76 31 20 GDOUBLE_TYPE v1
ac610 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 6e 53 69 = 0.0;. int nSi
ac620 67 6e 69 66 69 63 61 6e 74 20 3d 20 30 3b 0a 20 gnificant = 0;.
ac630 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
ac640 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b sspace(*z) ) z++
ac650 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 ;. if( *z=='-'
ac660 29 7b 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d 31 ){. sign = -1
ac670 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c ;. z++;. }el
ac680 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 se if( *z=='+' )
ac690 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 {. z++;. }.
ac6a0 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 while( z[0]=='0
ac6b0 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 ' ){. z++;.
ac6c0 7d 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 }. while( sqlit
ac6d0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b e3Isdigit(*z) ){
ac6e0 0a 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30 2e . v1 = v1*10.
ac6f0 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 0 + (*z - '0');.
ac700 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 6e 53 69 z++;. nSi
ac710 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 7d gnificant++;. }
ac720 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 . if( *z=='.' )
ac730 7b 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 {. LONGDOUBLE
ac740 5f 54 59 50 45 20 64 69 76 69 73 6f 72 20 3d 20 _TYPE divisor =
ac750 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 1.0;. z++;.
ac760 20 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 61 if( nSignifica
ac770 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 nt==0 ){. w
ac780 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 hile( z[0]=='0'
ac790 29 7b 0a 20 20 20 20 20 20 20 20 64 69 76 69 73 ){. divis
ac7a0 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20 or *= 10.0;.
ac7b0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 7d z++;. }
ac7c0 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 . }. while
ac7d0 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 ( sqlite3Isdigit
ac7e0 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 69 66 (*z) ){. if
ac7f0 28 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 3c 31 ( nSignificant<1
ac800 38 20 29 7b 0a 20 20 20 20 20 20 20 20 76 31 20 8 ){. v1
ac810 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 = v1*10.0 + (*z
ac820 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20 20 - '0');.
ac830 64 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b divisor *= 10.0;
ac840 0a 20 20 20 20 20 20 20 20 6e 53 69 67 6e 69 66 . nSignif
ac850 69 63 61 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d icant++;. }
ac860 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 . z++;.
ac870 7d 0a 20 20 20 20 76 31 20 2f 3d 20 64 69 76 69 }. v1 /= divi
ac880 73 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a sor;. }. if( *
ac890 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 z=='e' || *z=='E
ac8a0 27 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 73 69 ' ){. int esi
ac8b0 67 6e 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20 gn = 1;. int
ac8c0 65 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 4c 4f eval = 0;. LO
ac8d0 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 63 NGDOUBLE_TYPE sc
ac8e0 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a ale = 1.0;. z
ac8f0 2b 2b 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d ++;. if( *z==
ac900 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 69 '-' ){. esi
ac910 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a gn = -1;. z
ac920 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ++;. }else if
ac930 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 ( *z=='+' ){.
ac940 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 z++;. }.
ac950 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
ac960 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 Isdigit(*z) ){.
ac970 20 20 20 20 20 65 76 61 6c 20 3d 20 65 76 61 6c eval = eval
ac980 2a 31 30 20 2b 20 2a 7a 20 2d 20 27 30 27 3b 0a *10 + *z - '0';.
ac990 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d z++;. }
ac9a0 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c . while( eval
ac9b0 3e 3d 36 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d >=64 ){ scale *=
ac9c0 20 31 2e 30 65 2b 36 34 3b 20 65 76 61 6c 20 2d 1.0e+64; eval -
ac9d0 3d 20 36 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c = 64; }. whil
ac9e0 65 28 20 65 76 61 6c 3e 3d 31 36 20 29 7b 20 73 e( eval>=16 ){ s
ac9f0 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 36 3b cale *= 1.0e+16;
aca00 20 65 76 61 6c 20 2d 3d 20 31 36 3b 20 7d 0a 20 eval -= 16; }.
aca10 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d while( eval>=
aca20 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 4 ){ scale *= 1.
aca30 30 65 2b 34 3b 20 65 76 61 6c 20 2d 3d 20 34 3b 0e+4; eval -= 4;
aca40 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 }. while( ev
aca50 61 6c 3e 3d 31 20 29 7b 20 73 63 61 6c 65 20 2a al>=1 ){ scale *
aca60 3d 20 31 2e 30 65 2b 31 3b 20 65 76 61 6c 20 2d = 1.0e+1; eval -
aca70 3d 20 31 3b 20 7d 0a 20 20 20 20 69 66 28 20 65 = 1; }. if( e
aca80 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 sign<0 ){.
aca90 76 31 20 2f 3d 20 73 63 61 6c 65 3b 0a 20 20 20 v1 /= scale;.
acaa0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 31 }else{. v1
acab0 20 2a 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d *= scale;. }
acac0 0a 20 20 7d 0a 20 20 2a 70 52 65 73 75 6c 74 20 . }. *pResult
acad0 3d 20 28 64 6f 75 62 6c 65 29 28 73 69 67 6e 3c = (double)(sign<
acae0 30 20 3f 20 2d 76 31 20 3a 20 76 31 29 3b 0a 20 0 ? -v1 : v1);.
acaf0 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 return (int)(z
acb00 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 - zBegin);.#else
acb10 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
acb20 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 3Atoi64(z, pResu
acb30 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 lt);.#endif /* S
acb40 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
acb50 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a ING_POINT */.}..
acb60 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 /*.** Compare th
acb70 65 20 31 39 2d 63 68 61 72 61 63 74 65 72 20 73 e 19-character s
acb80 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e tring zNum again
acb90 73 74 20 74 68 65 20 74 65 78 74 20 72 65 70 72 st the text repr
acba0 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 esentation.** va
acbb0 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33 lue 2^63: 92233
acbc0 37 32 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 72036854775808.
acbd0 20 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 Return negative
acbe0 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 , zero, or posit
acbf0 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 ive.** if zNum i
acc00 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 s less than, equ
acc10 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 al to, or greate
acc20 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e r than the strin
acc30 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 g..**.** Unlike
acc40 6d 65 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f memcmp() this ro
acc50 75 74 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 utine is guarant
acc60 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 eed to return th
acc70 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 e difference.**
acc80 69 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 in the values of
acc90 20 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 the last digit
acca0 69 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 if the only diff
accb0 65 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 erence is in the
accc0 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20 .** last digit.
accd0 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 So, for example
acce0 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d ,.**.** com
accf0 70 61 72 65 32 70 6f 77 36 33 28 22 39 32 32 33 pare2pow63("9223
acd00 33 37 32 30 33 36 38 35 34 37 37 35 38 30 30 22 372036854775800"
acd10 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 ).**.** will ret
acd20 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 urn -8..*/.stati
acd30 63 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f c int compare2po
acd40 77 36 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a w63(const char *
acd50 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a zNum){. int c;.
acd60 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 c = memcmp(zNu
acd70 6d 2c 22 39 32 32 33 33 37 32 30 33 36 38 35 34 m,"9223372036854
acd80 37 37 35 38 30 22 2c 31 38 29 3b 0a 20 20 69 66 77580",18);. if
acd90 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20 ( c==0 ){. c
acda0 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 27 = zNum[18] - '8'
acdb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 ;. }. return c
acdc0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ;.}.../*.** Retu
acdd0 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d 20 rn TRUE if zNum
acde0 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e is a 64-bit sign
acdf0 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 77 ed integer and w
ace00 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 rite.** the valu
ace10 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 e of the integer
ace20 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 66 into *pNum. If
ace30 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e 20 zNum is not an
ace40 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 73 integer.** or is
ace50 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 an integer that
ace60 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f is too large to
ace70 20 62 65 20 65 78 70 72 65 73 73 65 64 20 77 69 be expressed wi
ace80 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 74 th 64 bits,.** t
ace90 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 hen return false
acea0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 ..**.** When thi
aceb0 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f 72 s routine was or
acec0 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e iginally written
aced0 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 6f it dealt with o
acee0 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e 75 nly.** 32-bit nu
acef0 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 20 mbers. At that
acf00 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 63 time, it was muc
acf10 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 h faster than th
acf20 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 72 e.** atoi() libr
acf30 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 52 ary routine in R
acf40 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51 edHat 7.2..*/.SQ
acf50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
acf60 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 sqlite3Atoi64(c
acf70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c onst char *zNum,
acf80 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 69 i64 *pNum){. i
acf90 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 64 v = 0;. int
acfa0 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b neg;. int i, c;
acfb0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
acfc0 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28 20 Start;. while(
acfd0 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a sqlite3Isspace(*
acfe0 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a zNum) ) zNum++;.
acff0 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 if( *zNum=='-'
ad000 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b ){. neg = 1;
ad010 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
ad020 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d else if( *zNum==
ad030 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d '+' ){. neg =
ad040 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 0;. zNum++;.
ad050 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 }else{. neg
ad060 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61 = 0;. }. zSta
ad070 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68 69 rt = zNum;. whi
ad080 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 le( zNum[0]=='0'
ad090 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a ){ zNum++; } /*
ad0a0 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61 64 69 Skip over leadi
ad0b0 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b 65 74 ng zeros. Ticket
ad0c0 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f 72 28 #2454 */. for(
ad0d0 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 i=0; (c=zNum[i])
ad0e0 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b >='0' && c<='9';
ad0f0 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 i++){. v = v
ad100 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 *10 + c - '0';.
ad110 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e 65 67 }. *pNum = neg
ad120 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69 66 28 ? -v : v;. if(
ad130 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30 20 26 c!=0 || (i==0 &
ad140 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d 29 20 & zStart==zNum)
ad150 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f || i>19 ){. /
ad160 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 79 20 * zNum is empty
ad170 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d or contains non-
ad180 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f 72 20 numeric text or
ad190 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a is longer. **
ad1a0 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 than 19 digits
ad1b0 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 6e 67 (thus guaranting
ad1c0 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f 20 that it is too
ad1d0 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 72 65 large) */. re
ad1e0 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 turn 0;. }else
ad1f0 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 if( i<19 ){.
ad200 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39 20 /* Less than 19
ad210 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b 6e digits, so we kn
ad220 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73 20 ow that it fits
ad230 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20 20 in 64 bits */.
ad240 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 return 1;. }e
ad250 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 2d 64 lse{. /* 19-d
ad260 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d 75 73 igit numbers mus
ad270 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 20 74 t be no larger t
ad280 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 38 35 han 922337203685
ad290 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 69 74 4775807 if posit
ad2a0 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 39 32 ive. ** or 92
ad2b0 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 2337203685477580
ad2c0 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20 20 8 if negative.
ad2d0 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 33 37 Note that 922337
ad2e0 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 20 20 2036854665808.
ad2f0 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 2a 2f ** is 2^63. */
ad300 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 . return comp
ad310 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c are2pow63(zNum)<
ad320 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a neg;. }.}../*.*
ad330 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 4e 75 * The string zNu
ad340 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 m represents an
ad350 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
ad360 2e 20 20 54 68 65 72 65 20 6d 69 67 68 74 20 62 . There might b
ad370 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 e some other.**
ad380 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c information foll
ad390 6f 77 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65 owing the intege
ad3a0 72 20 74 6f 6f 2c 20 62 75 74 20 74 68 61 74 20 r too, but that
ad3b0 70 61 72 74 20 69 73 20 69 67 6e 6f 72 65 64 2e part is ignored.
ad3c0 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 67 .** If the integ
ad3d0 65 72 20 74 68 61 74 20 74 68 65 20 70 72 65 66 er that the pref
ad3e0 69 78 20 6f 66 20 7a 4e 75 6d 20 72 65 70 72 65 ix of zNum repre
ad3f0 73 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 69 sents will fit i
ad400 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 69 n a.** 64-bit si
ad410 67 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 65 gned integer, re
ad420 74 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 65 turn TRUE. Othe
ad430 72 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 4c rwise return FAL
ad440 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 SE..**.** If the
ad450 20 6e 65 67 46 6c 61 67 20 70 61 72 61 6d 65 74 negFlag paramet
ad460 65 72 20 69 73 20 74 72 75 65 2c 20 74 68 61 74 er is true, that
ad470 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 4e 75 6d means that zNum
ad480 20 72 65 61 6c 6c 79 20 72 65 70 72 65 73 65 6e really represen
ad490 74 73 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 ts.** a negative
ad4a0 20 6e 75 6d 62 65 72 2e 20 20 28 54 68 65 20 6c number. (The l
ad4b0 65 61 64 69 6e 67 20 22 2d 22 20 69 73 20 6f 6d eading "-" is om
ad4c0 69 74 74 65 64 20 66 72 6f 6d 20 7a 4e 75 6d 2e itted from zNum.
ad4d0 29 20 20 54 68 69 73 0a 2a 2a 20 70 61 72 61 6d ) This.** param
ad4e0 65 74 65 72 20 69 73 20 6e 65 65 64 65 64 20 74 eter is needed t
ad4f0 6f 20 64 65 74 65 72 6d 69 6e 65 20 61 20 62 6f o determine a bo
ad500 75 6e 64 61 72 79 20 63 61 73 65 2e 20 20 41 20 undary case. A
ad510 73 74 72 69 6e 67 0a 2a 2a 20 6f 66 20 22 39 32 string.** of "92
ad520 32 33 33 37 33 30 33 36 38 35 34 37 37 35 38 30 2337303685477580
ad530 38 22 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 8" returns false
ad540 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 66 if negFlag is f
ad550 61 6c 73 65 20 6f 72 20 74 72 75 65 0a 2a 2a 20 alse or true.**
ad560 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 74 72 if negFlag is tr
ad570 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 64 69 6e ue..**.** Leadin
ad580 67 20 7a 65 72 6f 73 20 61 72 65 20 69 67 6e 6f g zeros are igno
ad590 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 red..*/.SQLITE_P
ad5a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ad5b0 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63 e3FitsIn64Bits(c
ad5c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c onst char *zNum,
ad5d0 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a 20 int negFlag){.
ad5e0 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74 int i, c;. int
ad5f0 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 20 61 73 73 neg = 0;.. ass
ad600 65 72 74 28 20 7a 4e 75 6d 5b 30 5d 3e 3d 27 30 ert( zNum[0]>='0
ad610 27 20 26 26 20 7a 4e 75 6d 5b 30 5d 3c 3d 27 39 ' && zNum[0]<='9
ad620 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 ' ); /* zNum is
ad630 61 6e 20 75 6e 73 69 67 6e 65 64 20 6e 75 6d 62 an unsigned numb
ad640 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 65 67 er */.. if( neg
ad650 46 6c 61 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e Flag ) neg = 1-n
ad660 65 67 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e eg;. while( *zN
ad670 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a um=='0' ){. z
ad680 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 Num++; /* Skip
ad690 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 leading zeros.
ad6a0 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f Ticket #2454 */
ad6b0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
ad6c0 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 (c=zNum[i])>='0'
ad6d0 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 && c<='9'; i++)
ad6e0 7b 7d 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b {}. if( i<19 ){
ad6f0 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 . /* Guarante
ad700 65 64 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73 ed to fit if les
ad710 73 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 s than 19 digits
ad720 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 */. return 1
ad730 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e ;. }else if( i>
ad740 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 19 ){. /* Gua
ad750 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f ranteed to be to
ad760 6f 20 62 69 67 20 69 66 20 67 72 65 61 74 65 72 o big if greater
ad770 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 than 19 digits
ad780 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b */. return 0;
ad790 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
ad7a0 20 43 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 Compare against
ad7b0 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 2^63. */. re
ad7c0 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 turn compare2pow
ad7d0 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 63(zNum)<neg;.
ad7e0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e }.}../*.** If zN
ad7f0 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e um represents an
ad800 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 integer that wi
ad810 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 ll fit in 32-bit
ad820 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a s, then set.** *
ad830 70 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 pValue to that i
ad840 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 nteger and retur
ad850 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 n true. Otherwi
ad860 73 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e se return false.
ad870 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e .**.** Any non-n
ad880 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 umeric character
ad890 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 s that following
ad8a0 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 zNum are ignore
ad8b0 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 d..** This is di
ad8c0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c fferent from sql
ad8d0 69 74 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 ite3Atoi64() whi
ad8e0 63 68 20 72 65 71 75 69 72 65 73 20 74 68 65 0a ch requires the.
ad8f0 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 ** input number
ad900 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 to be zero-termi
ad910 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nated..*/.SQLITE
ad920 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ad930 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e ite3GetInt32(con
ad940 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 st char *zNum, i
ad950 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 nt *pValue){. s
ad960 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 qlite_int64 v =
ad970 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 0;. int i, c;.
ad980 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 int neg = 0;.
ad990 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 if( zNum[0]=='-'
ad9a0 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b ){. neg = 1;
ad9b0 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
ad9c0 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d else if( zNum[0]
ad9d0 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 =='+' ){. zNu
ad9e0 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 m++;. }. while
ad9f0 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 ( zNum[0]=='0' )
ada00 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 zNum++;. for(i
ada10 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d =0; i<11 && (c =
ada20 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e zNum[i] - '0')>
ada30 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 =0 && c<=9; i++)
ada40 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b {. v = v*10 +
ada50 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 c;. }.. /* Th
ada60 65 20 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 e longest decima
ada70 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e l representation
ada80 20 6f 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 of a 32 bit int
ada90 65 67 65 72 20 69 73 20 31 30 20 64 69 67 69 74 eger is 10 digit
adaa0 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 s:. **. **
adab0 20 20 20 20 20 20 20 20 20 31 32 33 34 35 36 37 1234567
adac0 38 39 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 890. ** 2^3
adad0 31 20 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 1 -> 2147483648.
adae0 20 20 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 */. if( i>10
adaf0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
adb00 0a 20 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 . }. if( v-neg
adb10 3e 32 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20 >2147483647 ){.
adb20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
adb30 0a 20 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 . if( neg ){.
adb40 20 20 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 v = -v;. }.
adb50 2a 70 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 *pValue = (int)v
adb60 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a ;. return 1;.}.
adb70 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 ./*.** The varia
adb80 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
adb90 65 72 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 er encoding is a
adba0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
adbb0 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 KEY:.**
adbc0 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 A = 0xxxxxxx
adbd0 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 7 bits of data
adbe0 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 and one flag bit
adbf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 .** B =
adc00 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69 1xxxxxxx 7 bi
adc10 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f ts of data and o
adc20 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 ne flag bit.**
adc30 20 20 20 20 20 20 20 43 20 3d 20 78 78 78 78 78 C = xxxxx
adc40 78 78 78 20 20 20 20 38 20 62 69 74 73 20 6f 66 xxx 8 bits of
adc50 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 data.**.** 7 b
adc60 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 its - A.** 14 bi
adc70 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 ts - BA.** 21 bi
adc80 74 73 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 ts - BBA.** 28 b
adc90 69 74 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 its - BBBA.** 35
adca0 20 62 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a bits - BBBBA.**
adcb0 20 34 32 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 bits - BBBBB
adcc0 41 0a 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42 A.** 49 bits - B
adcd0 42 42 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 BBBBBA.** 56 bit
adce0 73 20 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 s - BBBBBBBA.**
adcf0 36 34 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 64 bits - BBBBBB
add00 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 BBC.*/../*.** Wr
add10 69 74 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 ite a 64-bit var
add20 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 iable-length int
add30 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 eger to memory s
add40 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e tarting at p[0].
add50 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f .** The length o
add60 66 20 64 61 74 61 20 77 72 69 74 65 20 77 69 6c f data write wil
add70 6c 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 l be between 1 a
add80 6e 64 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 nd 9 bytes. The
add90 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 number.** of by
adda0 74 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 tes written is r
addb0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 eturned..**.** A
addc0 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 variable-length
addd0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 integer consist
adde0 73 20 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37 s of the lower 7
addf0 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 62 79 bits of each by
ade00 74 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 te.** for all by
ade10 74 65 73 20 74 68 61 74 20 68 61 76 65 20 74 68 tes that have th
ade20 65 20 38 74 68 20 62 69 74 20 73 65 74 20 61 6e e 8th bit set an
ade30 64 20 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20 d one byte with
ade40 74 68 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 the 8th.** bit c
ade50 6c 65 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69 lear. Except, i
ade60 66 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 f we get to the
ade70 39 74 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f 9th byte, it sto
ade80 72 65 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 res the full.**
ade90 38 20 62 69 74 73 20 61 6e 64 20 69 73 20 74 68 8 bits and is th
adea0 65 20 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a e last byte..*/.
adeb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
adec0 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 nt sqlite3PutVar
aded0 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 int(unsigned cha
adee0 72 20 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 r *p, u64 v){.
adef0 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 int i, j, n;. u
adf00 38 20 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 8 buf[10];. if(
adf10 20 76 20 26 20 28 28 28 75 36 34 29 30 78 66 66 v & (((u64)0xff
adf20 30 30 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 000000)<<32) ){.
adf30 20 20 20 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 p[8] = (u8)v
adf40 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 ;. v >>= 8;.
adf50 20 20 20 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 for(i=7; i>=0
adf60 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b ; i--){. p[
adf70 69 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 i] = (u8)((v & 0
adf80 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 x7f) | 0x80);.
adf90 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 v >>= 7;.
adfa0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 39 3b }. return 9;
adfb0 0a 20 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 . } . n = 0
adfc0 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b ;. do{. buf[
adfd0 6e 2b 2b 5d 20 3d 20 28 75 38 29 28 28 76 20 26 n++] = (u8)((v &
adfe0 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 0x7f) | 0x80);.
adff0 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d v >>= 7;. }
ae000 77 68 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 while( v!=0 );.
ae010 20 62 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b buf[0] &= 0x7f;
ae020 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 . assert( n<=9
ae030 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d );. for(i=0, j=
ae040 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 n-1; j>=0; j--,
ae050 69 2b 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d i++){. p[i] =
ae060 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 buf[j];. }. r
ae070 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn n;.}../*.*
ae080 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
ae090 73 20 61 20 66 61 73 74 65 72 20 76 65 72 73 69 s a faster versi
ae0a0 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74 on of sqlite3Put
ae0b0 56 61 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e Varint() that on
ae0c0 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 ly.** works for
ae0d0 33 32 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20 32-bit positive
ae0e0 69 6e 74 65 67 65 72 73 20 61 6e 64 20 77 68 69 integers and whi
ae0f0 63 68 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 ch is optimized
ae100 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f for.** the commo
ae110 6e 20 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 n case of small
ae120 69 6e 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43 integers. A MAC
ae130 52 4f 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 RO version, putV
ae140 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 arint32,.** is p
ae150 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e rovided which in
ae160 6c 69 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65 lines the single
ae170 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c -byte case. All
ae180 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 code should use
ae190 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65 .** the MACRO ve
ae1a0 72 73 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75 rsion as this fu
ae1b0 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 nction assumes t
ae1c0 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 he single-byte c
ae1d0 61 73 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 ase has.** alrea
ae1e0 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e dy been handled.
ae1f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
ae200 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 TE int sqlite3Pu
ae210 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e tVarint32(unsign
ae220 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 ed char *p, u32
ae230 76 29 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56 v){.#ifndef putV
ae240 61 72 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76 arint32. if( (v
ae250 20 26 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a & ~0x7f)==0 ){.
ae260 20 20 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 p[0] = v;.
ae270 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
ae280 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20 #endif. if( (v
ae290 26 20 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b & ~0x3fff)==0 ){
ae2a0 0a 20 20 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 . p[0] = (u8)
ae2b0 28 28 76 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b ((v>>7) | 0x80);
ae2c0 0a 20 20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 . p[1] = (u8)
ae2d0 28 76 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 (v & 0x7f);.
ae2e0 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 return 2;. }.
ae2f0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75 return sqlite3Pu
ae300 74 56 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d tVarint(p, v);.}
ae310 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 ../*.** Read a 6
ae320 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 4-bit variable-l
ae330 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 ength integer fr
ae340 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 om memory starti
ae350 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 ng at p[0]..** R
ae360 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
ae370 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 of bytes read.
ae380 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 The value is st
ae390 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 ored in *v..*/.S
ae3a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 QLITE_PRIVATE u8
ae3b0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
ae3c0 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 t(const unsigned
ae3d0 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 char *p, u64 *v
ae3e0 29 7b 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a ){. u32 a,b,s;.
ae3f0 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 . a = *p;. /*
ae400 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 a: p0 (unmasked)
ae410 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
ae420 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 80)). {. *v
ae430 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = a;. return
ae440 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 1;. }.. p++;.
ae450 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a b = *p;. /* b:
ae460 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p1 (unmasked) *
ae470 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 /. if (!(b&0x80
ae480 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 )). {. a &=
ae490 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 0x7f;. a = a<
ae4a0 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a <7;. a |= b;.
ae4b0 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 *v = a;.
ae4c0 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 return 2;. }..
ae4d0 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 p++;. a = a<<1
ae4e0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 4;. a |= *p;.
ae4f0 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 /* a: p0<<14 | p
ae500 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 2 (unmasked) */.
ae510 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 if (!(a&0x80))
ae520 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 . {. a &= (0
ae530 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
ae540 0a 20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a . b &= 0x7f;.
ae550 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 b = b<<7;.
ae560 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 a |= b;. *v
ae570 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e = a;. return
ae580 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 3;. }.. /* CS
ae590 45 31 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f E1 from below */
ae5a0 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 . a &= (0x7f<<1
ae5b0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 4)|(0x7f);. p++
ae5c0 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
ae5d0 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
ae5e0 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 : p1<<14 | p3 (u
ae5f0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
ae600 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b (!(b&0x80)). {
ae610 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c . b &= (0x7f<
ae620 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
ae630 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 /* moved CSE1 u
ae640 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d p */. /* a &=
ae650 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
ae660 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 f); */. a = a
ae670 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b <<7;. a |= b;
ae680 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 . *v = a;.
ae690 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a return 4;. }..
ae6a0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c /* a: p0<<14 |
ae6b0 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a p2 (masked) */.
ae6c0 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c /* b: p1<<14 |
ae6d0 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p3 (unmasked) *
ae6e0 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 /. /* 1:save of
ae6f0 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 f p0<<21 | p1<<1
ae700 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 4 | p2<<7 | p3 (
ae710 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 masked) */. /*
ae720 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f moved CSE1 up */
ae730 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 . /* a &= (0x7f
ae740 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f <<14)|(0x7f); */
ae750 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 . b &= (0x7f<<1
ae760 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 4)|(0x7f);. s =
ae770 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c a;. /* s: p0<<
ae780 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 14 | p2 (masked)
ae790 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 */.. p++;. a
ae7a0 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 = a<<14;. a |=
ae7b0 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c *p;. /* a: p0<<
ae7c0 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 28 | p2<<14 | p4
ae7d0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
ae7e0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a if (!(a&0x80)).
ae7f0 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 {. /* we ca
ae800 6e 20 73 6b 69 70 20 74 68 65 73 65 20 63 61 75 n skip these cau
ae810 73 65 20 74 68 65 79 20 77 65 72 65 20 28 65 66 se they were (ef
ae820 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 fectively) done
ae830 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e above in calc'in
ae840 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 g s */. /* a
ae850 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 &= (0x7f<<28)|(0
ae860 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
ae870 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 */. /* b &=
ae880 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
ae890 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c ); */. b = b<
ae8a0 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a <7;. a |= b;.
ae8b0 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 s = s>>18;.
ae8c0 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 *v = ((u64)s)
ae8d0 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 <<32 | a;. re
ae8e0 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f turn 5;. }.. /
ae8f0 2a 20 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c * 2:save off p0<
ae900 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 <21 | p1<<14 | p
ae910 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 2<<7 | p3 (maske
ae920 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 d) */. s = s<<7
ae930 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a ;. s |= b;. /*
ae940 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c s: p0<<21 | p1<
ae950 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 <14 | p2<<7 | p3
ae960 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 (masked) */..
ae970 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 p++;. b = b<<14
ae980 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. b |= *p;. /
ae990 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 * b: p1<<28 | p3
ae9a0 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 <<14 | p5 (unmas
ae9b0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
ae9c0 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 b&0x80)). {.
ae9d0 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 /* we can skip
ae9e0 74 68 69 73 20 63 61 75 73 65 20 69 74 20 77 61 this cause it wa
ae9f0 73 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 s (effectively)
aea00 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 done above in ca
aea10 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 lc'ing s */.
aea20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 /* b &= (0x7f<<2
aea30 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
aea40 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 x7f); */. a &
aea50 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
aea60 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 7f);. a = a<<
aea70 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
aea80 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 s = s>>18;.
aea90 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c *v = ((u64)s)<
aeaa0 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 <32 | a;. ret
aeab0 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b urn 6;. }.. p+
aeac0 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a +;. a = a<<14;.
aead0 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 a |= *p;. /*
aeae0 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c a: p2<<28 | p4<<
aeaf0 31 34 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 14 | p6 (unmaske
aeb00 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 d) */. if (!(a&
aeb10 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 0x80)). {. a
aeb20 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 &= (0x1f<<28)|(
aeb30 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
aeb40 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 ;. b &= (0x7f
aeb50 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
aeb60 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
aeb70 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 a |= b;. s =
aeb80 73 3e 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 s>>11;. *v =
aeb90 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 ((u64)s)<<32 | a
aeba0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a ;. return 7;.
aebb0 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 }.. /* CSE2 f
aebc0 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 rom below */. a
aebd0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
aebe0 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 0x7f);. p++;.
aebf0 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c b = b<<14;. b |
aec00 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 = *p;. /* b: p3
aec10 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 <<28 | p5<<14 |
aec20 70 37 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p7 (unmasked) */
aec30 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 . if (!(b&0x80)
aec40 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 ). {. b &= (
aec50 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 0x1f<<28)|(0x7f<
aec60 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
aec70 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 /* moved CSE2 u
aec80 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d p */. /* a &=
aec90 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
aeca0 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 f); */. a = a
aecb0 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b <<7;. a |= b;
aecc0 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 . s = s>>4;.
aecd0 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 *v = ((u64)s)
aece0 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 <<32 | a;. re
aecf0 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 turn 8;. }.. p
aed00 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b ++;. a = a<<15;
aed10 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
aed20 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c a: p4<<29 | p6<
aed30 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b <15 | p8 (unmask
aed40 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 ed) */.. /* mov
aed50 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 ed CSE2 up */.
aed60 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 /* a &= (0x7f<<2
aed70 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 9)|(0x7f<<15)|(0
aed80 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 xff); */. b &=
aed90 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
aeda0 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 );. b = b<<8;.
aedb0 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 a |= b;.. s =
aedc0 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 s<<4;. b = p[-4
aedd0 5d 3b 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a ];. b &= 0x7f;.
aede0 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 b = b>>3;. s
aedf0 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 |= b;.. *v = ((
aee00 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a u64)s)<<32 | a;.
aee10 0a 20 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a . return 9;.}..
aee20 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d /*.** Read a 32-
aee30 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e bit variable-len
aee40 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d gth integer from
aee50 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 memory starting
aee60 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 at p[0]..** Ret
aee70 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
aee80 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 f bytes read. T
aee90 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 he value is stor
aeea0 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a 20 41 20 4d ed in *v..** A M
aeeb0 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65 ACRO version, ge
aeec0 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70 72 tVarint32, is pr
aeed0 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c ovided which inl
aeee0 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e ines the .** sin
aeef0 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 20 gle-byte case.
aef00 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 All code should
aef10 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76 65 use the MACRO ve
aef20 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69 rsion as .** thi
aef30 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d s function assum
aef40 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 es the single-by
aef50 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72 65 te case has alre
aef60 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 ady been handled
aef70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
aef80 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 ATE u8 sqlite3Ge
aef90 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 tVarint32(const
aefa0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
aefb0 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33 32 , u32 *v){. u32
aefc0 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 a,b;.. /* The
aefd0 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20 4f 76 1-byte case. Ov
aefe0 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 68 65 erwhelmingly the
aeff0 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20 48 most common. H
af000 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a 20 20 andled inline.
af010 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 61 72 ** by the getVar
af020 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a 2f 0a in32() macro */.
af030 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 a = *p;. /* a
af040 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 : p0 (unmasked)
af050 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74 56 61 */.#ifndef getVa
af060 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21 28 61 rint32. if (!(a
af070 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
af080 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 65 /* Values betwee
af090 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f 0a 20 n 0 and 127 */.
af0a0 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 *v = a;. r
af0b0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e eturn 1;. }.#en
af0c0 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20 32 2d dif.. /* The 2-
af0d0 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 byte case */. p
af0e0 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 ++;. b = *p;.
af0f0 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b /* b: p1 (unmask
af100 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 ed) */. if (!(b
af110 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
af120 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 65 /* Values betwee
af130 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38 33 20 n 128 and 16383
af140 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78 37 66 */. a &= 0x7f
af150 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a ;. a = a<<7;.
af160 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a *v = a | b;.
af170 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 return 2;.
af180 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d 62 79 }.. /* The 3-by
af190 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b te case */. p++
af1a0 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 ;. a = a<<14;.
af1b0 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 a |= *p;. /* a
af1c0 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 : p0<<14 | p2 (u
af1d0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
af1e0 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b (!(a&0x80)). {
af1f0 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 . /* Values b
af200 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 6e 64 etween 16384 and
af210 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20 20 20 2097151 */.
af220 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
af230 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d (0x7f);. b &=
af240 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 0x7f;. b = b
af250 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 <<7;. *v = a
af260 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 | b;. return
af270 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 33 3;. }.. /* A 3
af280 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69 73 20 2-bit varint is
af290 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 73 69 used to store si
af2a0 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ze information i
af2b0 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a 20 4f n btrees.. ** O
af2c0 62 6a 65 63 74 73 20 61 72 65 20 72 61 72 65 6c bjects are rarel
af2d0 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 32 4d y larger than 2M
af2e0 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20 33 2d iB limit of a 3-
af2f0 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20 20 2a byte varint.. *
af300 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72 69 6e * A 3-byte varin
af310 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 2c t is sufficient,
af320 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f for example, to
af330 20 72 65 63 6f 72 64 20 74 68 65 20 73 69 7a 65 record the size
af340 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34 38 35 . ** of a 10485
af350 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f 72 20 69-byte BLOB or
af360 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a string.. **. *
af370 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c 6c * We only unroll
af380 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c 20 32 the first 1-, 2
af390 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65 20 63 -, and 3- byte c
af3a0 61 73 65 73 2e 20 20 54 68 65 20 76 65 72 79 0a ases. The very.
af3b0 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67 65 72 ** rare larger
af3c0 20 63 61 73 65 73 20 63 61 6e 20 62 65 20 68 61 cases can be ha
af3d0 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73 6c 6f ndled by the slo
af3e0 77 65 72 20 36 34 2d 62 69 74 20 76 61 72 69 6e wer 64-bit varin
af3f0 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a t. ** routine..
af400 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b 0a 20 */.#if 1. {.
af410 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 u64 v64;.
af420 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 u8 n;.. p -=
af430 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 2;. n = sqlit
af440 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 e3GetVarint(p, &
af450 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 v64);. assert
af460 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20 29 3b ( n>3 && n<=9 );
af470 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 . *v = (u32)v
af480 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 64;. return n
af490 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f ;. }..#else. /
af4a0 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 * For following
af4b0 63 6f 64 65 20 28 6b 65 70 74 20 66 6f 72 20 68 code (kept for h
af4c0 69 73 74 6f 72 69 63 61 6c 20 72 65 63 6f 72 64 istorical record
af4d0 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20 61 6e 0a only) shows an.
af4e0 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67 20 66 ** unrolling f
af4f0 6f 72 20 74 68 65 20 33 2d 20 61 6e 64 20 34 2d or the 3- and 4-
af500 62 79 74 65 20 76 61 72 69 6e 74 20 63 61 73 65 byte varint case
af510 73 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 s. This code is
af520 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66 . ** slightly f
af530 61 73 74 65 72 2c 20 62 75 74 20 69 74 20 69 73 aster, but it is
af540 20 61 6c 73 6f 20 6c 61 72 67 65 72 20 61 6e 64 also larger and
af550 20 6d 75 63 68 20 68 61 72 64 65 72 20 74 6f 20 much harder to
af560 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 2b 2b test.. */. p++
af570 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
af580 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
af590 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 : p1<<14 | p3 (u
af5a0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
af5b0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b (!(b&0x80)). {
af5c0 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 . /* Values b
af5d0 65 74 77 65 65 6e 20 32 30 39 37 31 35 32 20 61 etween 2097152 a
af5e0 6e 64 20 32 36 38 34 33 35 34 35 35 20 2a 2f 0a nd 268435455 */.
af5f0 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c b &= (0x7f<<
af600 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
af610 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
af620 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 (0x7f);. a =
af630 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 a<<7;. *v = a
af640 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e | b;. return
af650 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 4;. }.. p++;.
af660 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 a = a<<14;. a
af670 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 |= *p;. /* a:
af680 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 p0<<28 | p2<<14
af690 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p4 (unmasked)
af6a0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
af6b0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 57 0)). {. /* W
af6c0 61 6c 75 65 73 20 20 62 65 74 77 65 65 6e 20 32 alues between 2
af6d0 36 38 34 33 35 34 35 36 20 61 6e 64 20 33 34 33 68435456 and 343
af6e0 35 39 37 33 38 33 36 37 20 2a 2f 0a 20 20 20 20 59738367 */.
af6f0 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c a &= (0x1f<<28)|
af700 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
af710 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31 );. b &= (0x1
af720 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
af730 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 )|(0x7f);. b
af740 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d = b<<7;. *v =
af750 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 a | b;. retu
af760 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 5;. }.. /*
af770 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 We can only reac
af780 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77 68 65 h this point whe
af790 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f 72 72 n reading a corr
af7a0 75 70 74 20 64 61 74 61 62 61 73 65 0a 20 20 2a upt database. *
af7b0 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 * file. In that
af7c0 20 63 61 73 65 20 77 65 20 61 72 65 20 6e 6f 74 case we are not
af7d0 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 20 in any hurry.
af7e0 55 73 65 20 74 68 65 20 28 72 65 6c 61 74 69 76 Use the (relativ
af7f0 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 ely. ** slow) g
af800 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 eneral-purpose s
af810 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
af820 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 ) routine to ext
af830 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 ract the. ** va
af840 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 lue. */. {.
af850 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 u64 v64;. u8
af860 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a n;.. p -= 4;.
af870 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 n = sqlite3G
af880 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 etVarint(p, &v64
af890 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e );. assert( n
af8a0 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 >5 && n<=9 );.
af8b0 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b *v = (u32)v64;
af8c0 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 . return n;.
af8d0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a }.#endif.}../*.
af8e0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
af8f0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 mber of bytes th
af900 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 65 at will be neede
af910 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67 d to store the g
af920 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69 iven.** 64-bit i
af930 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 nteger..*/.SQLIT
af940 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
af950 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 lite3VarintLen(u
af960 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d 64 v){. int i =
af970 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b 0;. do{. i+
af980 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a +;. v >>= 7;.
af990 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 26 }while( v!=0 &
af9a0 26 20 41 4c 57 41 59 53 28 69 3c 39 29 20 29 3b & ALWAYS(i<9) );
af9b0 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a . return i;.}..
af9c0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 ./*.** Read or w
af9d0 72 69 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 rite a four-byte
af9e0 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 big-endian inte
af9f0 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 ger value..*/.SQ
afa00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
afa10 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 sqlite3Get4byte
afa20 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 (const u8 *p){.
afa30 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 return (p[0]<<2
afa40 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 4) | (p[1]<<16)
afa50 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b | (p[2]<<8) | p[
afa60 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 3];.}.SQLITE_PRI
afa70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
afa80 33 50 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e 3Put4byte(unsign
afa90 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 ed char *p, u32
afaa0 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38 v){. p[0] = (u8
afab0 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d )(v>>24);. p[1]
afac0 20 3d 20 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a = (u8)(v>>16);.
afad0 20 20 70 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e p[2] = (u8)(v>
afae0 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75 >8);. p[3] = (u
afaf0 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64 8)v;.}....#if !d
afb00 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
afb10 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 IT_BLOB_LITERAL)
afb20 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
afb30 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a TE_HAS_CODEC)./*
afb40 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 .** Translate a
afb50 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 48 single byte of H
afb60 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 ex into an integ
afb70 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 er..** This rout
afb80 69 6e 65 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 inen only works
afb90 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73 20 61 if h really is a
afba0 20 76 61 6c 69 64 20 68 65 78 61 64 65 63 69 6d valid hexadecim
afbb0 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 3a al.** character:
afbc0 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a 0..9a..fA..F.*
afbd0 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65 78 54 /.static u8 hexT
afbe0 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 20 61 oInt(int h){. a
afbf0 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27 20 26 ssert( (h>='0' &
afc00 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 28 68 & h<='9') || (h
afc10 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 27 29 >='a' && h<='f')
afc20 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 26 20 || (h>='A' &&
afc30 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 64 65 h<='F') );.#ifde
afc40 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 f SQLITE_ASCII.
afc50 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e 3e 36 h += 9*(1&(h>>6
afc60 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 ));.#endif.#ifde
afc70 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a f SQLITE_EBCDIC.
afc80 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 68 3e h += 9*(1&~(h>
afc90 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 >4));.#endif. r
afca0 65 74 75 72 6e 20 28 75 38 29 28 68 20 26 20 30 eturn (u8)(h & 0
afcb0 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a xf);.}.#endif /*
afcc0 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c !SQLITE_OMIT_BL
afcd0 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 OB_LITERAL || SQ
afce0 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a LITE_HAS_CODEC *
afcf0 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 /..#if !defined(
afd00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
afd10 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 _LITERAL) || def
afd20 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f ined(SQLITE_HAS_
afd30 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e CODEC)./*.** Con
afd40 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69 74 65 vert a BLOB lite
afd50 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ral of the form
afd60 22 78 27 68 68 68 68 68 68 27 22 20 69 6e 74 6f "x'hhhhhh'" into
afd70 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a 20 76 its binary.** v
afd80 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20 61 20 alue. Return a
afd90 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62 pointer to its b
afda0 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20 53 70 inary value. Sp
afdb0 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a ace to hold the.
afdc0 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75 65 20 ** binary value
afdd0 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 has been obtaine
afde0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e d from malloc an
afdf0 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 d must be freed
afe00 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e by.** the callin
afe10 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 g routine..*/.SQ
afe20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
afe30 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 6f 42 d *sqlite3HexToB
afe40 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c lob(sqlite3 *db,
afe50 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
afe60 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a int n){. char *
afe70 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a zBlob;. int i;.
afe80 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68 61 72 . zBlob = (char
afe90 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c *)sqlite3DbMall
afea0 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20 2b 20 ocRaw(db, n/2 +
afeb0 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 1);. n--;. if(
afec0 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f zBlob ){. fo
afed0 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 r(i=0; i<n; i+=2
afee0 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69 ){. zBlob[i
afef0 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e 74 28 /2] = (hexToInt(
aff00 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65 78 54 z[i])<<4) | hexT
aff10 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20 oInt(z[i+1]);.
aff20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b 69 2f }. zBlob[i/
aff30 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 2] = 0;. }. re
aff40 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 turn zBlob;.}.#e
aff50 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
aff60 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
aff70 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f L || SQLITE_HAS_
aff80 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a CODEC */.../*.**
aff90 20 43 68 61 6e 67 65 20 74 68 65 20 73 71 6c 69 Change the sqli
affa0 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 te.magic from SQ
affb0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 LITE_MAGIC_OPEN
affc0 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f to SQLITE_MAGIC_
affd0 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 BUSY..** Return
affe0 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 an error (non-ze
afff0 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 69 63 ro) if the magic
b0000 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f was not SQLITE_
b0010 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 MAGIC_OPEN.** wh
b0020 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
b0030 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a is called..**.**
b0040 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
b0050 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65 6e 74 called when ent
b0060 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74 65 20 ering an SQLite
b0070 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49 54 45 API. The SQLITE
b0080 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 76 _MAGIC_OPEN.** v
b0090 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 74 alue indicates t
b00a0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
b00b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73 connection pass
b00c0 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50 49 20 ed into the API
b00d0 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64 20 69 is.** open and i
b00e0 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73 65 64 s not being used
b00f0 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68 72 65 by another thre
b0100 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69 6e 67 ad. By changing
b0110 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 74 6f the value.** to
b0120 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 SQLITE_MAGIC_BU
b0130 53 59 20 77 65 20 69 6e 64 69 63 61 74 65 20 74 SY we indicate t
b0140 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 hat the connecti
b0150 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a 2a 2a on is in use..**
b0160 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
b0170 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 63 f() below will c
b0180 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
b0190 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4d back to SQLITE_M
b01a0 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 AGIC_OPEN.** whe
b01b0 6e 20 74 68 65 20 41 50 49 20 65 78 69 74 73 2e n the API exits.
b01c0 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 .**.** This rou
b01d0 74 69 6e 65 20 69 73 20 61 20 61 74 74 65 6d 70 tine is a attemp
b01e0 74 20 74 6f 20 64 65 74 65 63 74 20 69 66 20 74 t to detect if t
b01f0 77 6f 20 74 68 72 65 61 64 73 20 75 73 65 20 74 wo threads use t
b0200 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c 69 74 he.** same sqlit
b0210 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20 74 68 e* pointer at th
b0220 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 54 68 e same time. Th
b0230 65 72 65 20 69 73 20 61 20 72 61 63 65 20 0a 2a ere is a race .*
b0240 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f 20 69 * condition so i
b0250 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 t is possible th
b0260 61 74 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 at the error is
b0270 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2a not detected..**
b0280 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74 68 65 But usually the
b0290 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20 62 65 problem will be
b02a0 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65 73 75 seen. The resu
b02b0 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a 2a 2a lt will be an.**
b02c0 20 65 72 72 6f 72 20 77 68 69 63 68 20 63 61 6e error which can
b02d0 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 62 75 be used to debu
b02e0 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f g the applicatio
b02f0 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75 73 69 n that is.** usi
b0300 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f 72 72 ng SQLite incorr
b0310 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 ectly..**.** Tic
b0320 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20 64 62 ket #202: If db
b0330 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74 20 61 ->magic is not a
b0340 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61 6c 75 valid open valu
b0350 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e 6f 74 e, take care not
b0360 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 .** to modify th
b0370 65 20 64 62 20 73 74 72 75 63 74 75 72 65 20 61 e db structure a
b0380 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75 6c 64 t all. It could
b0390 20 62 65 20 74 68 61 74 20 64 62 20 69 73 20 61 be that db is a
b03a0 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e 74 65 stale.** pointe
b03b0 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 r. In other wor
b03c0 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 ds, it could be
b03d0 74 68 61 74 20 74 68 65 72 65 20 68 61 73 20 62 that there has b
b03e0 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a 20 63 een a prior.** c
b03f0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 all to sqlite3_c
b0400 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64 62 20 lose(db) and db
b0410 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 has been dealloc
b0420 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20 64 6f ated. And we do
b0430 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 .** not want to
b0440 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61 6c 6c write into deall
b0450 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a ocated memory..*
b0460 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
b0470 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
b0480 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b0490 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 SafetyOn(sqlite3
b04a0 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d *db){. if( db-
b04b0 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d >magic==SQLITE_M
b04c0 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 AGIC_OPEN ){.
b04d0 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c db->magic = SQL
b04e0 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a ITE_MAGIC_BUSY;.
b04f0 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
b0500 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
b0510 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 b->mutex) );.
b0520 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
b0530 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 se if( db->magic
b0540 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 ==SQLITE_MAGIC_B
b0550 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d USY ){. db->m
b0560 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 agic = SQLITE_MA
b0570 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 GIC_ERROR;. d
b0580 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 b->u1.isInterrup
b0590 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 ted = 1;. }. r
b05a0 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 eturn 1;.}.#endi
b05b0 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 f../*.** Change
b05c0 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d 20 53 the magic from S
b05d0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
b05e0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 to SQLITE_MAGIC
b05f0 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e _OPEN..** Return
b0600 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a an error (non-z
b0610 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 69 ero) if the magi
b0620 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 c was not SQLITE
b0630 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a 20 77 _MAGIC_BUSY.** w
b0640 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
b0650 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 is called..*/.#
b0660 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
b0670 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
b0680 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
b0690 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 20 2a etyOff(sqlite3 *
b06a0 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d db){. if( db->m
b06b0 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 agic==SQLITE_MAG
b06c0 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 IC_BUSY ){. d
b06d0 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 b->magic = SQLIT
b06e0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 E_MAGIC_OPEN;.
b06f0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
b0700 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3_mutex_held(db-
b0710 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 72 >mutex) );. r
b0720 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 eturn 0;. }else
b0730 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 {. db->magic
b0740 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 = SQLITE_MAGIC_E
b0750 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 RROR;. db->u1
b0760 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d .isInterrupted =
b0770 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 1;. return 1
b0780 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a ;. }.}.#endif..
b0790 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d /*.** Check to m
b07a0 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65 ake sure we have
b07b0 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f 69 6e a valid db poin
b07c0 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 20 ter. This test
b07d0 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72 is not.** foolpr
b07e0 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65 73 20 oof but it does
b07f0 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d 65 61 provide some mea
b0800 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63 74 69 sure of protecti
b0810 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 6d 69 on against.** mi
b0820 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e 74 65 suse of the inte
b0830 72 66 61 63 65 20 73 75 63 68 20 61 73 20 70 61 rface such as pa
b0840 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f 69 6e ssing in db poin
b0850 74 65 72 73 20 74 68 61 74 20 61 72 65 0a 2a 2a ters that are.**
b0860 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 20 68 NULL or which h
b0870 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 6f 75 ave been previou
b0880 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 sly closed. If
b0890 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
b08a0 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d 65 61 urns.** 1 it mea
b08b0 6e 73 20 74 68 61 74 20 74 68 65 20 64 62 20 70 ns that the db p
b08c0 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 20 ointer is valid
b08d0 61 6e 64 20 30 20 69 66 20 69 74 20 73 68 6f 75 and 0 if it shou
b08e0 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64 65 72 ld not be.** der
b08f0 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20 61 6e eferenced for an
b0900 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65 20 63 y reason. The c
b0910 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
b0920 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a should invoke.**
b0930 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 SQLITE_MISUSE i
b0940 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a mmediately..**.*
b0950 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 * sqlite3SafetyC
b0960 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69 72 65 heckOk() require
b0970 73 20 74 68 61 74 20 74 68 65 20 64 62 20 70 6f s that the db po
b0980 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64 20 66 inter be valid f
b0990 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71 6c 69 or.** use. sqli
b09a0 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 te3SafetyCheckSi
b09b0 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77 73 20 ckOrOk() allows
b09c0 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74 68 61 a db pointer tha
b09d0 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a 20 6f t failed to.** o
b09e0 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 pen properly and
b09f0 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f 72 20 is not fit for
b0a00 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75 74 20 general use but
b0a10 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a 2a 20 which can be.**
b0a20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d used as an argum
b0a30 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 ent to sqlite3_e
b0a40 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c 69 74 rrmsg() or sqlit
b0a50 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 53 e3_close()..*/.S
b0a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b0a70 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 t sqlite3SafetyC
b0a80 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 20 2a heckOk(sqlite3 *
b0a90 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 69 63 db){. u32 magic
b0aa0 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20 ;. if( db==0 )
b0ab0 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61 67 69 return 0;. magi
b0ac0 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 c = db->magic;.
b0ad0 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 if( magic!=SQLI
b0ae0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 0a 23 TE_MAGIC_OPEN .#
b0af0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
b0b00 55 47 0a 20 20 20 20 20 26 26 20 6d 61 67 69 63 UG. && magic
b0b10 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 !=SQLITE_MAGIC_B
b0b20 55 53 59 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a USY.#endif. ){.
b0b30 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b0b40 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
b0b50 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 n 1;. }.}.SQLIT
b0b60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b0b70 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b lite3SafetyCheck
b0b80 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65 33 SickOrOk(sqlite3
b0b90 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 *db){. u32 mag
b0ba0 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 ic;. magic = db
b0bb0 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d ->magic;. if( m
b0bc0 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 agic!=SQLITE_MAG
b0bd0 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20 20 IC_SICK &&.
b0be0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d magic!=SQLITE_M
b0bf0 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 AGIC_OPEN &&.
b0c00 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 magic!=SQLITE
b0c10 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72 65 _MAGIC_BUSY ) re
b0c20 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e turn 0;. return
b0c30 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 1;.}../********
b0c40 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 ****** End of ut
b0c50 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a il.c ***********
b0c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0c80 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
b0c90 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
b0ca0 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a e hash.c *******
b0cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0cd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
b0ce0 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 1 September 22.*
b0cf0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
b0d00 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
b0d10 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
b0d20 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
b0d30 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
b0d40 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
b0d50 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
b0d60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
b0d70 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
b0d80 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
b0d90 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
b0da0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
b0db0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
b0dc0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
b0dd0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
b0de0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
b0df0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
b0e00 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
b0e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
b0e50 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 This is the imp
b0e60 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67 lementation of g
b0e70 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c eneric hash-tabl
b0e80 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 es.** used in SQ
b0e90 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a Lite..**.** $Id:
b0ea0 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 37 20 32 hash.c,v 1.37 2
b0eb0 30 30 39 2f 30 35 2f 30 32 20 31 33 3a 32 39 3a 009/05/02 13:29:
b0ec0 33 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 38 drh Exp $.*/.
b0ed0 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 ./* Turn bulk me
b0ee0 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68 mory into a hash
b0ef0 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 table object by
b0f00 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
b0f10 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 e.** fields of t
b0f20 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72 he Hash structur
b0f30 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 e..**.** "pNew"
b0f40 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
b0f50 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 the hash table t
b0f60 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69 hat is to be ini
b0f70 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c tialized..*/.SQL
b0f80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b0f90 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 sqlite3HashInit
b0fa0 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20 (Hash *pNew){.
b0fb0 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 assert( pNew!=0
b0fc0 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 );. pNew->first
b0fd0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f = 0;. pNew->co
b0fe0 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d unt = 0;. pNew-
b0ff0 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 >htsize = 0;. p
b1000 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a New->ht = 0;.}..
b1010 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e /* Remove all en
b1020 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 tries from a has
b1030 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 h table. Reclai
b1040 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a m all memory..**
b1050 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 Call this routi
b1060 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 ne to delete a h
b1070 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 ash table or to
b1080 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 reset a hash tab
b1090 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 le.** to the emp
b10a0 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c ty state..*/.SQL
b10b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b10c0 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 sqlite3HashClea
b10d0 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 r(Hash *pH){. H
b10e0 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 ashElem *elem;
b10f0 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f /* For lo
b1100 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 oping over all e
b1110 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74 lements of the t
b1120 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 able */.. asser
b1130 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c t( pH!=0 );. el
b1140 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a em = pH->first;.
b1150 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b pH->first = 0;
b1160 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
b1170 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 pH->ht);. pH->h
b1180 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 t = 0;. pH->hts
b1190 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 ize = 0;. while
b11a0 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 ( elem ){. Ha
b11b0 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 shElem *next_ele
b11c0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a m = elem->next;.
b11d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
b11e0 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d (elem);. elem
b11f0 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 = next_elem;.
b1200 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 }. pH->count =
b1210 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 0;.}../*.** The
b1220 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e hashing function
b1230 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 ..*/.static unsi
b1240 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73 68 gned int strHash
b1250 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 (const char *z,
b1260 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 int nKey){. int
b1270 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 h = 0;. assert
b1280 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77 ( nKey>=0 );. w
b1290 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20 hile( nKey > 0
b12a0 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 ){. h = (h<<3
b12b0 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 55 ) ^ h ^ sqlite3U
b12c0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 pperToLower[(uns
b12d0 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d igned char)*z++]
b12e0 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 ;. nKey--;.
b12f0 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a }. return h;.}.
b1300 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65 ../* Link pNew e
b1310 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 lement into the
b1320 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 hash table pH.
b1330 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68 65 If pEntry!=0 the
b1340 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 n also.** insert
b1350 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20 70 pNew into the p
b1360 45 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b 65 Entry hash bucke
b1370 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
b1380 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 d insertElement(
b1390 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 . Hash *pH,
b13a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
b13b0 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 complete hash t
b13c0 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 able */. struct
b13d0 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 20 _ht *pEntry,
b13e0 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 6e /* The entry in
b13f0 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 73 to which pNew is
b1400 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 48 inserted */. H
b1410 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 ashElem *pNew
b1420 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 /* The ele
b1430 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 ment to be inser
b1440 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 ted */.){. Hash
b1450 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 Elem *pHead;
b1460 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d /* First elem
b1470 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70 ent already in p
b1480 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20 70 Entry */. if( p
b1490 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48 65 Entry ){. pHe
b14a0 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 ad = pEntry->cou
b14b0 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61 nt ? pEntry->cha
b14c0 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74 in : 0;. pEnt
b14d0 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 ry->count++;.
b14e0 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d pEntry->chain =
b14f0 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a pNew;. }else{.
b1500 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a 20 pHead = 0;.
b1510 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20 29 }. if( pHead )
b1520 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 {. pNew->next
b1530 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e = pHead;. pN
b1540 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64 ew->prev = pHead
b1550 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20 ->prev;. if(
b1560 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 pHead->prev ){ p
b1570 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 Head->prev->next
b1580 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 = pNew; }. e
b1590 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 lse
b15a0 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e { pH->first = pN
b15b0 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d ew; }. pHead-
b15c0 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 >prev = pNew;.
b15d0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d }else{. pNew-
b15e0 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 >next = pH->firs
b15f0 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66 t;. if( pH->f
b1600 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 irst ){ pH->firs
b1610 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 t->prev = pNew;
b1620 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 }. pNew->prev
b1630 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 = 0;. pH->fi
b1640 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a rst = pNew;. }.
b1650 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 }.../* Resize th
b1660 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 e hash table so
b1670 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 that it cantains
b1680 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b "new_size" buck
b1690 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 ets..**.** The h
b16a0 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 ash table might
b16b0 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20 69 fail to resize i
b16c0 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 f sqlite3_malloc
b16d0 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 () fails or.** i
b16e0 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 69 f the new size i
b16f0 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 s the same as th
b1700 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a e prior size..**
b1710 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
b1720 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75 72 the resize occur
b1730 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e s and false if n
b1740 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ot..*/.static in
b1750 74 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70 t rehash(Hash *p
b1760 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 H, unsigned int
b1770 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 new_size){. str
b1780 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b uct _ht *new_ht;
b1790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
b17a0 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c he new hash tabl
b17b0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
b17c0 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 *elem, *next_ele
b17d0 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f m; /* For loo
b17e0 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 ping over existi
b17f0 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a ng elements */..
b1800 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f #if SQLITE_MALLO
b1810 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 C_SOFT_LIMIT>0.
b1820 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 if( new_size*si
b1830 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 zeof(struct _ht)
b1840 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 >SQLITE_MALLOC_S
b1850 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 OFT_LIMIT ){.
b1860 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 new_size = SQLI
b1870 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c TE_MALLOC_SOFT_L
b1880 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 IMIT/sizeof(stru
b1890 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 ct _ht);. }. i
b18a0 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d f( new_size==pH-
b18b0 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e >htsize ) return
b18c0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0;.#endif.. /*
b18d0 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 74 The inability t
b18e0 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 o allocates spac
b18f0 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 68 e for a larger h
b1900 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20 2a ash table is. *
b1910 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 * a performance
b1920 68 69 74 20 62 75 74 20 69 74 20 69 73 20 6e 6f hit but it is no
b1930 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e t a fatal error.
b1940 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20 So mark the.
b1950 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 ** allocation as
b1960 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a a benign.. */.
b1970 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
b1980 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
b1990 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74 new_ht = (struct
b19a0 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 _ht *)sqlite3Ma
b19b0 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73 lloc( new_size*s
b19c0 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 izeof(struct _ht
b19d0 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e ) );. sqlite3En
b19e0 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
b19f0 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d .. if( new_ht==
b1a00 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
b1a10 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d sqlite3_free(pH-
b1a20 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d >ht);. pH->ht =
b1a30 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 new_ht;. pH->h
b1a40 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 tsize = new_size
b1a50 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
b1a60 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a Size(new_ht)/siz
b1a70 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b eof(struct _ht);
b1a80 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74 . memset(new_ht
b1a90 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69 , 0, new_size*si
b1aa0 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 zeof(struct _ht)
b1ab0 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 );. for(elem=pH
b1ac0 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 ->first, pH->fir
b1ad0 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d st=0; elem; elem
b1ae0 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 = next_elem){.
b1af0 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
b1b00 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d h = strHash(elem
b1b10 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b ->pKey, elem->nK
b1b20 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a ey) % new_size;.
b1b30 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 next_elem =
b1b40 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 elem->next;.
b1b50 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 insertElement(pH
b1b60 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c , &new_ht[h], el
b1b70 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 em);. }. retur
b1b80 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 n 1;.}../* This
b1b90 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e function (for in
b1ba0 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 ternal use only)
b1bb0 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d locates an elem
b1bc0 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 ent in an.** has
b1bd0 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 h table that mat
b1be0 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b ches the given k
b1bf0 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f ey. The hash fo
b1c00 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a r this key has.*
b1c10 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 * already been c
b1c20 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 omputed and is p
b1c30 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68 assed as the 4th
b1c40 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 parameter..*/.s
b1c50 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a tatic HashElem *
b1c60 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e findElementGiven
b1c70 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61 Hash(. const Ha
b1c80 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 sh *pH, /* T
b1c90 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 he pH to be sear
b1ca0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ched */. const
b1cb0 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a char *pKey, /*
b1cc0 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 The key we are
b1cd0 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f searching for */
b1ce0 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 . int nKey,
b1cf0 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
b1d00 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e in key (not coun
b1d10 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e ting zero termin
b1d20 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67 ator) */. unsig
b1d30 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f ned int h /
b1d40 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 * The hash for t
b1d50 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 his key. */.){.
b1d60 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b HashElem *elem;
b1d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1d80 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 /* Used to loop
b1d90 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 thru the element
b1da0 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 list */. int c
b1db0 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 ount;
b1dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
b1dd0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
b1de0 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a left to test */.
b1df0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b . if( pH->ht ){
b1e00 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 . struct _ht
b1e10 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 *pEntry = &pH->h
b1e20 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d t[h];. elem =
b1e30 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a pEntry->chain;.
b1e40 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 count = pEnt
b1e50 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c ry->count;. }el
b1e60 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 se{. elem = p
b1e70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f H->first;. co
b1e80 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b unt = pH->count;
b1e90 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f . }. while( co
b1ea0 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28 unt-- && ALWAYS(
b1eb0 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28 elem) ){. if(
b1ec0 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 elem->nKey==nKe
b1ed0 79 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e y && sqlite3StrN
b1ee0 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c ICmp(elem->pKey,
b1ef0 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b pKey,nKey)==0 ){
b1f00 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 . return e
b1f10 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 lem;. }. e
b1f20 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 lem = elem->next
b1f30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
b1f40 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 ;.}../* Remove a
b1f50 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 single entry fr
b1f60 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c om the hash tabl
b1f70 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 e given a pointe
b1f80 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 r to that.** ele
b1f90 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 ment and a hash
b1fa0 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 on the element's
b1fb0 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 key..*/.static
b1fc0 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 void removeEleme
b1fd0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 ntGivenHash(. H
b1fe0 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 ash *pH,
b1ff0 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 /* The pH conta
b2000 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a ining "elem" */.
b2010 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d HashElem* elem
b2020 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 , /* The eleme
b2030 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 nt to be removed
b2040 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a from the pH */.
b2050 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
b2060 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 /* Hash valu
b2070 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e e for the elemen
b2080 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 t */.){. struct
b2090 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 _ht *pEntry;.
b20a0 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 if( elem->prev )
b20b0 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 {. elem->prev
b20c0 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e ->next = elem->n
b20d0 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 ext; . }else{.
b20e0 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 pH->first = e
b20f0 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 lem->next;. }.
b2100 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 if( elem->next
b2110 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 ){. elem->nex
b2120 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e t->prev = elem->
b2130 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 prev;. }. if(
b2140 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45 pH->ht ){. pE
b2150 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 ntry = &pH->ht[h
b2160 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72 ];. if( pEntr
b2170 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 y->chain==elem )
b2180 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e {. pEntry->
b2190 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 chain = elem->ne
b21a0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 xt;. }. pE
b21b0 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 ntry->count--;.
b21c0 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 74 72 assert( pEntr
b21d0 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 y->count>=0 );.
b21e0 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
b21f0 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d e( elem );. pH-
b2200 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 >count--;. if(
b2210 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a pH->count<=0 ){.
b2220 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e assert( pH->
b2230 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 first==0 );.
b2240 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e assert( pH->coun
b2250 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 t==0 );. sqli
b2260 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 te3HashClear(pH)
b2270 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 ;. }.}../* Atte
b2280 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e mpt to locate an
b2290 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
b22a0 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 hash table pH wi
b22b0 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 th a key.** that
b22c0 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b matches pKey,nK
b22d0 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 ey. Return the
b22e0 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c data for this el
b22f0 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a ement if it is.*
b2300 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c * found, or NULL
b2310 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
b2320 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 match..*/.SQLITE
b2330 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
b2340 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 qlite3HashFind(c
b2350 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 onst Hash *pH, c
b2360 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c onst char *pKey,
b2370 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 int nKey){. Ha
b2380 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 shElem *elem;
b2390 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 /* The element
b23a0 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 that matches key
b23b0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
b23c0 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61 nt h; /* A ha
b23d0 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 sh on key */..
b23e0 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b assert( pH!=0 );
b23f0 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 . assert( pKey!
b2400 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
b2410 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 nKey>=0 );. if(
b2420 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68 pH->ht ){. h
b2430 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c = strHash(pKey,
b2440 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 nKey) % pH->hts
b2450 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ize;. }else{.
b2460 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 h = 0;. }. e
b2470 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e lem = findElemen
b2480 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70 tGivenHash(pH, p
b2490 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 Key, nKey, h);.
b24a0 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 return elem ? e
b24b0 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d lem->data : 0;.}
b24c0 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 ../* Insert an e
b24d0 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 lement into the
b24e0 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 hash table pH.
b24f0 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c The key is pKey,
b2500 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 nKey.** and the
b2510 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a data is "data"..
b2520 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d **.** If no elem
b2530 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20 ent exists with
b2540 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 a matching key,
b2550 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c then a new.** el
b2560 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 ement is created
b2570 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 and NULL is ret
b2580 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
b2590 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 another element
b25a0 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77 already exists w
b25b0 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 ith the same key
b25c0 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 , then the.** ne
b25d0 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 w data replaces
b25e0 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 the old data and
b25f0 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 the old data is
b2600 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 returned..** Th
b2610 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 e key is not cop
b2620 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 ied in this inst
b2630 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c ance. If a mall
b2640 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a oc fails, then.*
b2650 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 * the new data i
b2660 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
b2670 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 he hash table is
b2680 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a unchanged..**.*
b2690 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20 * If the "data"
b26a0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 parameter to thi
b26b0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 s function is NU
b26c0 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 LL, then the.**
b26d0 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f element correspo
b26e0 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 nding to "key" i
b26f0 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 s removed from t
b2700 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a he hash table..*
b2710 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b2720 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 void *sqlite3Ha
b2730 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70 shInsert(Hash *p
b2740 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 H, const char *p
b2750 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 Key, int nKey, v
b2760 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e oid *data){. un
b2770 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 signed int h;
b2780 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 /* the hash
b2790 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c of the key modul
b27a0 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a o hash table siz
b27b0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
b27c0 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 *elem; /*
b27d0 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 Used to loop thr
b27e0 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 u the element li
b27f0 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d st */. HashElem
b2800 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a *new_elem; /*
b2810 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 New element add
b2820 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a ed to the pH */.
b2830 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 . assert( pH!=0
b2840 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b );. assert( pK
b2850 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ey!=0 );. asser
b2860 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 t( nKey>=0 );.
b2870 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 if( pH->htsize )
b2880 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 {. h = strHas
b2890 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 h(pKey, nKey) %
b28a0 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 pH->htsize;. }e
b28b0 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a lse{. h = 0;.
b28c0 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e }. elem = fin
b28d0 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 dElementGivenHas
b28e0 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 h(pH,pKey,nKey,h
b28f0 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b );. if( elem ){
b2900 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 . void *old_d
b2910 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 ata = elem->data
b2920 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d ;. if( data==
b2930 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76 0 ){. remov
b2940 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 eElementGivenHas
b2950 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 h(pH,elem,h);.
b2960 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 }else{. e
b2970 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 lem->data = data
b2980 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b ;. elem->pK
b2990 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 ey = pKey;.
b29a0 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c assert(nKey==el
b29b0 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d em->nKey);. }
b29c0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f . return old_
b29d0 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 data;. }. if(
b29e0 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e data==0 ) return
b29f0 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 0;. new_elem =
b2a00 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69 (HashElem*)sqli
b2a10 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f te3Malloc( sizeo
b2a20 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 f(HashElem) );.
b2a30 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 if( new_elem==0
b2a40 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a ) return data;.
b2a50 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 new_elem->pKey
b2a60 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 = pKey;. new_e
b2a70 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 lem->nKey = nKey
b2a80 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 ;. new_elem->da
b2a90 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d ta = data;. pH-
b2aa0 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 >count++;. if(
b2ab0 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26 pH->count>=10 &&
b2ac0 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 pH->count > 2*p
b2ad0 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 H->htsize ){.
b2ae0 20 69 66 28 20 72 65 68 61 73 68 28 70 48 2c 20 if( rehash(pH,
b2af0 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 26 26 20 pH->count*2) &&
b2b00 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 pH->htsize ){.
b2b10 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 h = strHash(
b2b20 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 pKey, nKey) % pH
b2b30 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a ->htsize;. }.
b2b40 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 }. if( pH->ht
b2b50 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c ){. insertEl
b2b60 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 ement(pH, &pH->h
b2b70 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b t[h], new_elem);
b2b80 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
b2b90 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 sertElement(pH,
b2ba0 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 0, new_elem);.
b2bb0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
b2bc0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b2bd0 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a End of hash.c *
b2be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b2c10 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b2c20 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f Begin file opco
b2c30 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.c **********
b2c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b2c60 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c ./* Automaticall
b2c70 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f y generated. Do
b2c80 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 not edit */./*
b2c90 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 See the mkopcode
b2ca0 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 c.awk script for
b2cb0 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 details. */.#if
b2cc0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
b2cd0 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c _OMIT_EXPLAIN) |
b2ce0 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 | !defined(NDEBU
b2cf0 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 G) || defined(VD
b2d00 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 BE_PROFILE) || d
b2d10 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
b2d20 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 BUG).SQLITE_PRIV
b2d30 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
b2d40 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d sqlite3OpcodeNam
b2d50 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 e(int i){. stati
b2d60 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f c const char *co
b2d70 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b nst azName[] = {
b2d80 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 "?",. /*
b2d90 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20 20 1 */ "VNext",.
b2da0 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 41 66 /* 2 */ "Af
b2db0 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a finity",. /*
b2dc0 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 3 */ "Column"
b2dd0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f ,. /* 4 */
b2de0 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 "SetCookie",.
b2df0 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 53 65 /* 5 */ "Se
b2e00 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 ek",. /* 6
b2e10 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 0a */ "Sequence",.
b2e20 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22 /* 7 */ "
b2e30 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20 20 20 Savepoint",.
b2e40 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f 77 4b /* 8 */ "RowK
b2e50 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 ey",. /* 9
b2e60 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20 20 20 */ "SCopy",.
b2e70 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 4f 70 65 /* 10 */ "Ope
b2e80 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a nWrite",. /*
b2e90 20 20 31 31 20 2a 2f 20 22 49 66 22 2c 0a 20 20 11 */ "If",.
b2ea0 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 43 6f /* 12 */ "Co
b2eb0 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 llSeq",. /*
b2ec0 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 13 */ "OpenRead
b2ed0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a ",. /* 14 *
b2ee0 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20 / "Expire",.
b2ef0 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75 74 6f /* 15 */ "Auto
b2f00 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a Commit",. /*
b2f10 20 20 31 36 20 2a 2f 20 22 50 61 67 65 63 6f 75 16 */ "Pagecou
b2f20 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 37 nt",. /* 17
b2f30 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 6b */ "IntegrityCk
b2f40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20 2a ",. /* 18 *
b2f50 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f / "Sort",. /
b2f60 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c 0a * 19 */ "Not",.
b2f70 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 22 /* 20 */ "
b2f80 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Copy",. /*
b2f90 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20 21 */ "Trace",.
b2fa0 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 46 /* 22 */ "F
b2fb0 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f unction",. /
b2fc0 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65 67 22 * 23 */ "IfNeg"
b2fd0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a 2f ,. /* 24 */
b2fe0 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a "Noop",. /*
b2ff0 20 20 32 35 20 2a 2f 20 22 52 65 74 75 72 6e 22 25 */ "Return"
b3000 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f ,. /* 26 */
b3010 20 22 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 "NewRowid",.
b3020 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 56 61 72 /* 27 */ "Var
b3030 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 iable",. /*
b3040 20 32 38 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c 28 */ "String",
b3050 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 . /* 29 */
b3060 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a "RealAffinity",.
b3070 20 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 /* 30 */ "
b3080 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f VRename",. /
b3090 2a 20 20 33 31 20 2a 2f 20 22 50 61 72 73 65 53 * 31 */ "ParseS
b30a0 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20 chema",. /*
b30b0 20 33 32 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a 32 */ "VOpen",.
b30c0 20 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22 /* 33 */ "
b30d0 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20 Close",. /*
b30e0 20 33 34 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 34 */ "CreateIn
b30f0 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 dex",. /* 3
b3100 35 20 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c 5 */ "IsUnique",
b3110 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 . /* 36 */
b3120 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20 "NotFound",.
b3130 20 2f 2a 20 20 33 37 20 2a 2f 20 22 49 6e 74 36 /* 37 */ "Int6
b3140 34 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 4",. /* 38
b3150 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a */ "MustBeInt",.
b3160 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 /* 39 */ "
b3170 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Halt",. /*
b3180 34 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 40 */ "Rowid",.
b3190 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 49 /* 41 */ "I
b31a0 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 dxLT",. /*
b31b0 34 32 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 42 */ "AddImm",.
b31c0 20 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 /* 43 */ "
b31d0 53 74 61 74 65 6d 65 6e 74 22 2c 0a 20 20 20 20 Statement",.
b31e0 20 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44 /* 44 */ "RowD
b31f0 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 ata",. /* 4
b3200 35 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 5 */ "MemMax",.
b3210 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e /* 46 */ "N
b3220 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 otExists",.
b3230 2f 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62 /* 47 */ "Gosub
b3240 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a ",. /* 48 *
b3250 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 / "Integer",.
b3260 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65 /* 49 */ "Pre
b3270 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 v",. /* 50
b3280 2a 2f 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c */ "RowSetRead",
b3290 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 . /* 51 */
b32a0 22 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 "RowSetAdd",.
b32b0 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 56 43 6f /* 52 */ "VCo
b32c0 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lumn",. /*
b32d0 35 33 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 53 */ "CreateTab
b32e0 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34 le",. /* 54
b32f0 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 */ "Last",.
b3300 20 2f 2a 20 20 35 35 20 2a 2f 20 22 53 65 65 6b /* 55 */ "Seek
b3310 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 Le",. /* 56
b3320 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 */ "IncrVacuum"
b3330 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f ,. /* 57 */
b3340 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 "IdxRowid",.
b3350 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 52 65 73 /* 58 */ "Res
b3360 65 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f etCount",. /
b3370 2a 20 20 35 39 20 2a 2f 20 22 43 6f 6e 74 65 78 * 59 */ "Contex
b3380 74 50 75 73 68 22 2c 0a 20 20 20 20 20 2f 2a 20 tPush",. /*
b3390 20 36 30 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 60 */ "Yield",.
b33a0 20 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 /* 61 */ "
b33b0 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 DropTrigger",.
b33c0 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72 /* 62 */ "Dr
b33d0 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f opIndex",. /
b33e0 2a 20 20 36 33 20 2a 2f 20 22 49 64 78 47 45 22 * 63 */ "IdxGE"
b33f0 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f ,. /* 64 */
b3400 20 22 49 64 78 44 65 6c 65 74 65 22 2c 0a 20 20 "IdxDelete",.
b3410 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 56 61 /* 65 */ "Va
b3420 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 cuum",. /*
b3430 36 36 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 66 */ "Or",.
b3440 20 2f 2a 20 20 36 37 20 2a 2f 20 22 41 6e 64 22 /* 67 */ "And"
b3450 2c 0a 20 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f ,. /* 68 */
b3460 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f "IfNot",. /
b3470 2a 20 20 36 39 20 2a 2f 20 22 44 72 6f 70 54 61 * 69 */ "DropTa
b3480 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 ble",. /* 7
b3490 30 20 2a 2f 20 22 53 65 65 6b 4c 74 22 2c 0a 20 0 */ "SeekLt",.
b34a0 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 49 /* 71 */ "I
b34b0 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 sNull",. /*
b34c0 20 37 32 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22 72 */ "NotNull"
b34d0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f ,. /* 73 */
b34e0 20 22 4e 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "Ne",. /*
b34f0 37 34 20 2a 2f 20 22 45 71 22 2c 0a 20 20 20 20 74 */ "Eq",.
b3500 20 2f 2a 20 20 37 35 20 2a 2f 20 22 47 74 22 2c /* 75 */ "Gt",
b3510 0a 20 20 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 . /* 76 */
b3520 22 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 "Le",. /* 7
b3530 37 20 2a 2f 20 22 4c 74 22 2c 0a 20 20 20 20 20 7 */ "Lt",.
b3540 2f 2a 20 20 37 38 20 2a 2f 20 22 47 65 22 2c 0a /* 78 */ "Ge",.
b3550 20 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 /* 79 */ "
b3560 4d 61 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20 20 MakeRecord",.
b3570 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 42 69 74 /* 80 */ "Bit
b3580 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 And",. /* 8
b3590 31 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20 1 */ "BitOr",.
b35a0 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22 53 68 /* 82 */ "Sh
b35b0 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f iftLeft",. /
b35c0 2a 20 20 38 33 20 2a 2f 20 22 53 68 69 66 74 52 * 83 */ "ShiftR
b35d0 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ight",. /*
b35e0 38 34 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20 84 */ "Add",.
b35f0 20 20 2f 2a 20 20 38 35 20 2a 2f 20 22 53 75 62 /* 85 */ "Sub
b3600 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20 tract",. /*
b3610 20 38 36 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79 86 */ "Multiply
b3620 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37 20 2a ",. /* 87 *
b3630 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20 20 / "Divide",.
b3640 20 2f 2a 20 20 38 38 20 2a 2f 20 22 52 65 6d 61 /* 88 */ "Rema
b3650 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 inder",. /*
b3660 20 38 39 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c 89 */ "Concat",
b3670 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f 20 . /* 90 */
b3680 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20 20 "ResultRow",.
b3690 20 20 2f 2a 20 20 39 31 20 2a 2f 20 22 44 65 6c /* 91 */ "Del
b36a0 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 ete",. /* 9
b36b0 32 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c 2 */ "AggFinal",
b36c0 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20 . /* 93 */
b36d0 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f "BitNot",. /
b36e0 2a 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e 67 * 94 */ "String
b36f0 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35 20 8",. /* 95
b3700 2a 2f 20 22 43 6f 6d 70 61 72 65 22 2c 0a 20 20 */ "Compare",.
b3710 20 20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 47 6f /* 96 */ "Go
b3720 74 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 to",. /* 97
b3730 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c */ "TableLock",
b3740 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20 . /* 98 */
b3750 22 43 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a "Clear",. /*
b3760 20 20 39 39 20 2a 2f 20 22 56 65 72 69 66 79 43 99 */ "VerifyC
b3770 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 ookie",. /*
b3780 31 30 30 20 2a 2f 20 22 41 67 67 53 74 65 70 22 100 */ "AggStep"
b3790 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f ,. /* 101 */
b37a0 20 22 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22 "SetNumColumns"
b37b0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 32 20 2a 2f ,. /* 102 */
b37c0 20 22 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a "Transaction",.
b37d0 20 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22 /* 103 */ "
b37e0 56 46 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f VFilter",. /
b37f0 2a 20 31 30 34 20 2a 2f 20 22 56 44 65 73 74 72 * 104 */ "VDestr
b3800 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35 oy",. /* 105
b3810 20 2a 2f 20 22 43 6f 6e 74 65 78 74 50 6f 70 22 */ "ContextPop"
b3820 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a 2f ,. /* 106 */
b3830 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a "Next",. /*
b3840 20 31 30 37 20 2a 2f 20 22 43 6f 75 6e 74 22 2c 107 */ "Count",
b3850 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 . /* 108 */
b3860 22 49 64 78 49 6e 73 65 72 74 22 2c 0a 20 20 20 "IdxInsert",.
b3870 20 20 2f 2a 20 31 30 39 20 2a 2f 20 22 53 65 65 /* 109 */ "See
b3880 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 kGe",. /* 11
b3890 30 20 2a 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 0 */ "Insert",.
b38a0 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 44 /* 111 */ "D
b38b0 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a estroy",. /*
b38c0 20 31 31 32 20 2a 2f 20 22 52 65 61 64 43 6f 6f 112 */ "ReadCoo
b38d0 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 kie",. /* 11
b38e0 33 20 2a 2f 20 22 52 6f 77 53 65 74 54 65 73 74 3 */ "RowSetTest
b38f0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a ",. /* 114 *
b3900 2f 20 22 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22 / "LoadAnalysis"
b3910 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f ,. /* 115 */
b3920 20 22 45 78 70 6c 61 69 6e 22 2c 0a 20 20 20 20 "Explain",.
b3930 20 2f 2a 20 31 31 36 20 2a 2f 20 22 48 61 6c 74 /* 116 */ "Halt
b3940 49 66 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a IfNull",. /*
b3950 20 31 31 37 20 2a 2f 20 22 4f 70 65 6e 50 73 65 117 */ "OpenPse
b3960 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 udo",. /* 11
b3970 38 20 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 8 */ "OpenEpheme
b3980 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 ral",. /* 11
b3990 39 20 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 9 */ "Null",.
b39a0 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 4d 6f 76 /* 120 */ "Mov
b39b0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 31 20 e",. /* 121
b39c0 2a 2f 20 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 */ "Blob",.
b39d0 2f 2a 20 31 32 32 20 2a 2f 20 22 52 65 77 69 6e /* 122 */ "Rewin
b39e0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 d",. /* 123
b39f0 2a 2f 20 22 53 65 65 6b 47 74 22 2c 0a 20 20 20 */ "SeekGt",.
b3a00 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22 56 42 65 /* 124 */ "VBe
b3a10 67 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 gin",. /* 12
b3a20 35 20 2a 2f 20 22 56 55 70 64 61 74 65 22 2c 0a 5 */ "VUpdate",.
b3a30 20 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 /* 126 */ "
b3a40 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a IfZero",. /*
b3a50 20 31 32 37 20 2a 2f 20 22 56 43 72 65 61 74 65 127 */ "VCreate
b3a60 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20 2a ",. /* 128 *
b3a70 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 / "Found",.
b3a80 2f 2a 20 31 32 39 20 2a 2f 20 22 49 66 50 6f 73 /* 129 */ "IfPos
b3a90 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 30 20 2a ",. /* 130 *
b3aa0 2f 20 22 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f / "Real",. /
b3ab0 2a 20 31 33 31 20 2a 2f 20 22 4e 75 6c 6c 52 6f * 131 */ "NullRo
b3ac0 77 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32 20 w",. /* 132
b3ad0 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20 20 */ "Jump",.
b3ae0 2f 2a 20 31 33 33 20 2a 2f 20 22 50 65 72 6d 75 /* 133 */ "Permu
b3af0 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a tation",. /*
b3b00 20 31 33 34 20 2a 2f 20 22 4e 6f 74 55 73 65 64 134 */ "NotUsed
b3b10 5f 31 33 34 22 2c 0a 20 20 20 20 20 2f 2a 20 31 _134",. /* 1
b3b20 33 35 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 35 */ "NotUsed_1
b3b30 33 35 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36 35",. /* 136
b3b40 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 36 */ "NotUsed_136
b3b50 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 37 20 2a ",. /* 137 *
b3b60 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 37 22 2c / "NotUsed_137",
b3b70 0a 20 20 20 20 20 2f 2a 20 31 33 38 20 2a 2f 20 . /* 138 */
b3b80 22 4e 6f 74 55 73 65 64 5f 31 33 38 22 2c 0a 20 "NotUsed_138",.
b3b90 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22 4e /* 139 */ "N
b3ba0 6f 74 55 73 65 64 5f 31 33 39 22 2c 0a 20 20 20 otUsed_139",.
b3bb0 20 20 2f 2a 20 31 34 30 20 2a 2f 20 22 4e 6f 74 /* 140 */ "Not
b3bc0 55 73 65 64 5f 31 34 30 22 2c 0a 20 20 20 20 20 Used_140",.
b3bd0 2f 2a 20 31 34 31 20 2a 2f 20 22 54 6f 54 65 78 /* 141 */ "ToTex
b3be0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32 20 t",. /* 142
b3bf0 2a 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 */ "ToBlob",.
b3c00 20 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 4e /* 143 */ "ToN
b3c10 75 6d 65 72 69 63 22 2c 0a 20 20 20 20 20 2f 2a umeric",. /*
b3c20 20 31 34 34 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 144 */ "ToInt",
b3c30 0a 20 20 20 20 20 2f 2a 20 31 34 35 20 2a 2f 20 . /* 145 */
b3c40 22 54 6f 52 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 "ToReal",. };.
b3c50 20 72 65 74 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 return azName[i
b3c60 5d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a ];.}.#endif../**
b3c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
b3c80 20 6f 66 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a of opcodes.c **
b3c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
b3cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
b3cd0 69 6e 20 66 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 in file os_os2.c
b3ce0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
b3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
b3d10 2a 2a 20 32 30 30 36 20 46 65 62 20 31 34 0a 2a ** 2006 Feb 14.*
b3d20 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
b3d30 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
b3d40 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
b3d50 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
b3d60 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
b3d70 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
b3d80 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
b3d90 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
b3da0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
b3db0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
b3dc0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
b3dd0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
b3de0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
b3df0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
b3e00 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
b3e10 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
b3e20 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
b3e30 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
b3e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3e80 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
b3e90 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
b3ea0 20 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69 that is specifi
b3eb0 63 20 74 6f 20 4f 53 2f 32 2e 0a 2a 2a 0a 2a 2a c to OS/2..**.**
b3ec0 20 24 49 64 3a 20 6f 73 5f 6f 73 32 2e 63 2c 76 $Id: os_os2.c,v
b3ed0 20 31 2e 36 33 20 32 30 30 38 2f 31 32 2f 31 30 1.63 2008/12/10
b3ee0 20 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78 19:26:24 drh Ex
b3ef0 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c p $.*/...#if SQL
b3f00 49 54 45 5f 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a ITE_OS_OS2../*.*
b3f10 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d * A Note About M
b3f20 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e emory Allocation
b3f30 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 :.**.** This dri
b3f40 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 ver uses malloc(
b3f50 29 2f 66 72 65 65 28 29 20 64 69 72 65 63 74 6c )/free() directl
b3f60 79 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f y rather than go
b3f70 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 ing through.** t
b3f80 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65 he SQLite-wrappe
b3f90 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f rs sqlite3_mallo
b3fa0 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 c()/sqlite3_free
b3fb0 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70 (). Those wrapp
b3fc0 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 ers.** are desig
b3fd0 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 ned for use on e
b3fe0 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20 mbedded systems
b3ff0 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 where memory is
b4000 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 scarce and.** ma
b4010 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61 lloc failures ha
b4020 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e ppen frequently.
b4030 20 20 4f 53 2f 32 20 64 6f 65 73 20 6e 6f 74 20 OS/2 does not
b4040 74 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e typically run on
b4050 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73 .** embedded sys
b4060 74 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 tems, and when i
b4070 74 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c t does the devel
b4080 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 opers normally h
b4090 61 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 ave bigger.** pr
b40a0 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 oblems to worry
b40b0 61 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 about than runni
b40c0 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 ng out of memory
b40d0 2e 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e . So there is n
b40e0 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 ot.** a compelli
b40f0 6e 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 ng need to use t
b4100 68 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a he wrappers..**.
b4110 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 ** But there is
b4120 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f a good reason to
b4130 20 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61 not use the wra
b4140 70 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73 ppers. If we us
b4150 65 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 e the.** wrapper
b4160 73 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 s then we will g
b4170 65 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c et simulated mal
b4180 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77 loc() failures w
b4190 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 ithin this.** dr
b41a0 69 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 iver. And that
b41b0 63 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 causes all kinds
b41c0 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 of problems for
b41d0 20 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a our tests. We.
b41e0 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 ** could enhance
b41f0 20 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 SQLite to deal
b4200 77 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d with simulated m
b4210 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77 alloc failures w
b4220 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 ithin.** the OS
b4230 64 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20 driver, but the
b4240 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74 code to deal wit
b4250 68 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20 h those failure
b4260 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 would not.** be
b4270 65 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e exercised on Lin
b4280 75 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e ux (which does n
b4290 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f ot need to mallo
b42a0 63 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65 c() in the drive
b42b0 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 r).** and so we
b42c0 77 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69 would have diffi
b42d0 63 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f culty writing co
b42e0 76 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72 verage tests for
b42f0 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 that.** code.
b4300 42 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20 Better to leave
b4310 74 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 the code out, we
b4320 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 think..**.** Th
b4330 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 e point of this
b4340 64 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73 discussion is as
b4350 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 follows: When
b4360 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a creating a new.*
b4370 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61 * OS layer for a
b4380 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
b4390 6d 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68 m, if you use th
b43a0 69 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 is file as an ex
b43b0 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 ample,.** avoid
b43c0 74 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f the use of mallo
b43d0 63 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f c()/free(). Tho
b43e0 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b se routines work
b43f0 20 6f 6b 20 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 ok on OS/2.** d
b4400 65 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 esktops but not
b4410 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 so well in embed
b4420 64 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a ded systems..*/.
b4430 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 ./*.** Macros us
b4440 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
b4450 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
b4460 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a o use threads..*
b4470 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
b4480 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 LITE_THREADSAFE)
b4490 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 && SQLITE_THREA
b44a0 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 DSAFE.# define S
b44b0 51 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44 QLITE_OS2_THREAD
b44c0 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a S 1.#endif../*.*
b44d0 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 * Include code t
b44e0 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f hat is common to
b44f0 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 all os_*.c file
b4500 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.*/./**********
b4510 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f **** Include os_
b4520 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 common.h in the
b4530 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 middle of os_os2
b4540 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
b4550 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
b4560 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
b4570 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
b4580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b45a0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
b45b0 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 May 22.**.** The
b45c0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
b45d0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
b45e0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
b45f0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
b4600 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
b4610 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
b4620 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
b4630 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
b4640 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
b4650 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
b4660 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
b4670 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
b4680 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
b4690 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
b46a0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
b46b0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
b46c0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
b46d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b46e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b46f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
b4720 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
b4730 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 ins macros and a
b4740 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 little bit of c
b4750 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d ode that is comm
b4760 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 on to.** all of
b4770 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 the platform-spe
b4780 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f cific files (os_
b4790 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 *.c) and is #inc
b47a0 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 luded into those
b47b0 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a .** files..**.**
b47c0 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c This file shoul
b47d0 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 d be #included b
b47e0 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c y the os_*.c fil
b47f0 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 es only. It is
b4800 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c not a.** general
b4810 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 purpose header
b4820 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a file..**.** $Id:
b4830 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 os_common.h,v 1
b4840 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31 .38 2009/02/24 1
b4850 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 8:40:50 danielk1
b4860 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 977 Exp $.*/.#if
b4870 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f ndef _OS_COMMON_
b4880 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 H_.#define _OS_C
b4890 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 OMMON_H_../*.**
b48a0 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 At least two bug
b48b0 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 s have slipped i
b48c0 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 n because we cha
b48d0 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f nged the MEMORY_
b48e0 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 DEBUG.** macro t
b48f0 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 o SQLITE_DEBUG a
b4900 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 nd some older ma
b4910 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 kefiles have not
b4920 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a yet made the.**
b4930 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f switch. The fo
b4940 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f llowing code sho
b4950 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 uld catch this p
b4960 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c roblem at compil
b4970 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 e-time..*/.#ifde
b4980 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 f MEMORY_DEBUG.#
b4990 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f error "The MEMO
b49a0 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 RY_DEBUG macro i
b49b0 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 s obsolete. Use
b49c0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e SQLITE_DEBUG in
b49d0 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a stead.".#endif..
b49e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
b49f0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
b4a00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 TE int sqlite3OS
b4a10 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 Trace = 0;.#defi
b4a20 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 ne OSTRACE1(X)
b4a30 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
b4a40 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
b4a50 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
b4a60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
b4a70 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 E2(X,Y) if
b4a80 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
b4a90 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
b4aa0 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 rintf(X,Y).#defi
b4ab0 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
b4ac0 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 Z) if( sqlit
b4ad0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
b4ae0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
b4af0 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Y,Z).#define OS
b4b00 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 TRACE4(X,Y,Z,A)
b4b10 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
b4b20 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
b4b30 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
b4b40 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
b4b50 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 CE5(X,Y,Z,A,B) i
b4b60 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
b4b70 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
b4b80 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
b4b90 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
b4ba0 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 E6(X,Y,Z,A,B,C)
b4bb0 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
b4bc0 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
b4bd0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
b4be0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
b4bf0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
b4c00 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 A,B,C,D) \. i
b4c10 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
b4c20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
b4c30 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
b4c40 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e ,D).#else.#defin
b4c50 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 e OSTRACE1(X).#d
b4c60 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 efine OSTRACE2(X
b4c70 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
b4c80 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 ACE3(X,Y,Z).#def
b4c90 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 ine OSTRACE4(X,Y
b4ca0 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Z,A).#define OS
b4cb0 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE5(X,Y,Z,A,B
b4cc0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
b4cd0 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a E6(X,Y,Z,A,B,C).
b4ce0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
b4cf0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a (X,Y,Z,A,B,C,D).
b4d00 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
b4d10 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d cros for perform
b4d20 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e ance tracing. N
b4d30 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f ormally turned o
b4d40 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a ff. Only works.
b4d50 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 ** on i486 hardw
b4d60 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 are..*/.#ifdef S
b4d70 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 QLITE_PERFORMANC
b4d80 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 E_TRACE../* .**
b4d90 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e hwtime.h contain
b4da0 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c s inline assembl
b4db0 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c er code for impl
b4dc0 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 ementing .** hig
b4dd0 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 h-performance ti
b4de0 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a ming routines..*
b4df0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
b4e00 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 * Include hwtime
b4e10 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
b4e20 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 of os_common.h
b4e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4e40 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
b4e50 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 * Begin file hwt
b4e60 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
b4e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4e90 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 /./*.** 2008 May
b4ea0 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 27.**.** The au
b4eb0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
b4ec0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
b4ed0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
b4ee0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
b4ef0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
b4f00 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
b4f10 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
b4f20 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
b4f30 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
b4f40 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
b4f50 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
b4f60 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
b4f70 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
b4f80 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
b4f90 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
b4fa0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
b4fb0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
b4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b4ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5000 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
b5010 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
b5020 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 inline asm code
b5030 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 for retrieving
b5040 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 "high-performanc
b5050 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 e".** counters f
b5060 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 or x86 class CPU
b5070 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 s..**.** $Id: hw
b5080 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 time.h,v 1.3 200
b5090 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 8/08/01 14:33:15
b50a0 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a shane Exp $.*/.
b50b0 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f #ifndef _HWTIME_
b50c0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 H_.#define _HWTI
b50d0 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ME_H_../*.** The
b50e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
b50f0 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ne only works on
b5100 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 pentium-class (
b5110 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 or newer) proces
b5120 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 sors..** It uses
b5130 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 the RDTSC opcod
b5140 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 e to read the cy
b5150 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 cle count value
b5160 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 out of the.** pr
b5170 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 ocessor and retu
b5180 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 rns that value.
b5190 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
b51a0 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a d for high-res.*
b51b0 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a * profiling..*/.
b51c0 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 #if (defined(__G
b51d0 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 NUC__) || define
b51e0 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 d(_MSC_VER)) &&
b51f0 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 \. (defined
b5200 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
b5210 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
b5220 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 efined(_M_IX86))
b5230 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 .. #if defined(
b5240 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 __GNUC__).. __i
b5250 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
b5260 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
b5270 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
b5280 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c unsigned int lo,
b5290 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f hi;. __asm_
b52a0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
b52b0 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 "rdtsc" : "=a" (
b52c0 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b lo), "=d" (hi));
b52d0 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 . return (sq
b52e0 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c lite_uint64)hi <
b52f0 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a < 32 | lo;. }..
b5300 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 #elif defined(
b5310 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 _MSC_VER).. __d
b5320 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f eclspec(naked) _
b5330 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 _inline sqlite_u
b5340 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 int64 __cdecl sq
b5350 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
b5360 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a ){. __asm {.
b5370 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 rdtsc.
b5380 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 ret
b5390 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 ; return value a
b53a0 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d t EDX:EAX. }
b53b0 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a . }.. #endif..
b53c0 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f #elif (defined(_
b53d0 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 _GNUC__) && defi
b53e0 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 ned(__x86_64__))
b53f0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
b5400 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
b5410 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
b5420 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
b5430 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 long val;.
b5440 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
b5450 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
b5460 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 "=A" (val));.
b5470 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
b5480 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 }. .#elif (def
b5490 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
b54a0 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f & defined(__ppc_
b54b0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
b54c0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
b54d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
b54e0 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
b54f0 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
b5500 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 tval;. unsi
b5510 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a gned long junk;.
b5520 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
b5530 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c volatile__ ("\n\
b5540 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 . 1:
b5550 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c mftbu %1\n\
b5560 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
b5570 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e mftb %L0\n
b5580 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
b5590 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e mftbu %0\n
b55a0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
b55b0 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 cmpw %0,%
b55c0 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
b55d0 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 bne 1
b55e0 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b".
b55f0 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 : "=r" (ret
b5600 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b val), "=r" (junk
b5610 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ));. return
b5620 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 retval;. }..#e
b5630 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 lse.. #error Ne
b5640 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ed implementatio
b5650 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 n of sqlite3Hwti
b5660 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
b5670 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 atform... /*.
b5680 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 ** To compile wi
b5690 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 thout implementi
b56a0 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 ng sqlite3Hwtime
b56b0 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
b56c0 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 form,. ** you c
b56d0 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 an remove the ab
b56e0 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 ove #error and u
b56f0 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 se the following
b5700 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 . ** stub funct
b5710 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c ion. You will l
b5720 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f ose timing suppo
b5730 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a rt for many. **
b5740 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e of the debuggin
b5750 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 g and testing ut
b5760 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 ilities, but it
b5770 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c should at. ** l
b5780 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 east compile and
b5790 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 run.. */.SQLIT
b57a0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
b57b0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
b57c0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 3Hwtime(void){ r
b57d0 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 eturn ((sqlite_u
b57e0 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e int64)0); }..#en
b57f0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
b5800 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f defined(_HWTIME_
b5810 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
b5820 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
b5830 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
b5840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5860 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
b5870 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
b5880 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
b5890 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f off in os_commo
b58a0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
b58b0 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 ******/..static
b58c0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
b58d0 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 start;.static sq
b58e0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c lite_uint64 g_el
b58f0 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 apsed;.#define T
b5900 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 IMER_START
b5910 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 g_start=sqlite3
b5920 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 Hwtime().#define
b5930 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 TIMER_END
b5940 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c g_elapsed=sql
b5950 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 ite3Hwtime()-g_s
b5960 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d tart.#define TIM
b5970 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 ER_ELAPSED g
b5980 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 _elapsed.#else.#
b5990 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 define TIMER_STA
b59a0 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 RT.#define TIMER
b59b0 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d _END.#define TIM
b59c0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 ER_ELAPSED (
b59d0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
b59e0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
b59f0 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 If we compile wi
b5a00 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 th the SQLITE_TE
b5a10 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 ST macro set, th
b5a20 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 en the following
b5a30 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 block.** of cod
b5a40 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 e will give us t
b5a50 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 he ability to si
b5a60 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f mulate a disk I/
b5a70 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a O error. This.*
b5a80 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 * is used for te
b5a90 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 sting the I/O re
b5aa0 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f covery logic..*/
b5ab0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
b5ac0 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
b5ad0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
b5ae0 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 ror_hit = 0;
b5af0 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
b5b00 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 number of I/O E
b5b10 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f rrors */.SQLITE_
b5b20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
b5b30 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
b5b40 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
b5b50 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 Number of non-be
b5b60 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 nign errors */.S
b5b70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
b5b80 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
b5b90 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 ending = 0;
b5ba0 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e /* Count down
b5bb0 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 to first I/O er
b5bc0 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ror */.SQLITE_AP
b5bd0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
b5be0 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d _error_persist =
b5bf0 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
b5c00 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 ue if I/O errors
b5c10 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 persist */.SQLI
b5c20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
b5c30 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
b5c40 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 gn = 0;
b5c50 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 /* True if error
b5c60 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a s are benign */.
b5c70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
b5c80 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
b5c90 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c pending = 0;.SQL
b5ca0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
b5cb0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 te3_diskfull = 0
b5cc0 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ;.#define Simula
b5cd0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
b5ce0 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 X) sqlite3_io_er
b5cf0 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 ror_benign=(X).#
b5d00 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
b5d10 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a OError(CODE) \.
b5d20 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 if( (sqlite3_i
b5d30 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 o_error_persist
b5d40 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 && sqlite3_io_er
b5d50 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 ror_hit) \.
b5d60 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f || sqlite3_io_
b5d70 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 error_pending--
b5d80 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 == 1 ) \.
b5d90 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f { local_
b5da0 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d ioerr(); CODE; }
b5db0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 .static void loc
b5dc0 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f al_ioerr(){. IO
b5dd0 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 TRACE(("IOERR\n"
b5de0 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f ));. sqlite3_io
b5df0 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 _error_hit++;.
b5e00 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f if( !sqlite3_io_
b5e10 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 error_benign ) s
b5e20 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
b5e30 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 hardhit++;.}.#de
b5e40 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
b5e50 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 kfullError(CODE)
b5e60 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 \. if( sqlite
b5e70 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
b5e80 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 ng ){ \. if(
b5e90 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
b5ea0 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 l_pending == 1 )
b5eb0 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c { \. local
b5ec0 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 _ioerr(); \.
b5ed0 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 sqlite3_diskf
b5ee0 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 ull = 1; \.
b5ef0 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
b5f00 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 or_hit = 1; \.
b5f10 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 CODE; \.
b5f20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 }else{ \.
b5f30 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 sqlite3_diskfu
b5f40 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a ll_pending--; \.
b5f50 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 } \. }.#e
b5f60 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 lse.#define Simu
b5f70 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
b5f80 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d n(X).#define Sim
b5f90 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a ulateIOError(A).
b5fa0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
b5fb0 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 DiskfullError(A)
b5fc0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
b5fd0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 hen testing, kee
b5fe0 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 p a count of the
b5ff0 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 number of open
b6000 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 files..*/.#ifdef
b6010 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
b6020 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
b6030 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f te3_open_file_co
b6040 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 unt = 0;.#define
b6050 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 OpenCounter(X)
b6060 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
b6070 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 le_count+=(X).#e
b6080 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e lse.#define Open
b6090 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 Counter(X).#endi
b60a0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
b60b0 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e fined(_OS_COMMON
b60c0 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _H_) */../******
b60d0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
b60e0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
b60f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6110 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
b6120 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
b6130 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
b6140 74 20 6f 66 66 20 69 6e 20 6f 73 5f 6f 73 32 2e t off in os_os2.
b6150 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
b6160 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
b6170 54 68 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75 The os2File stru
b6180 63 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 cture is subclas
b6190 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c s of sqlite3_fil
b61a0 65 20 73 70 65 63 69 66 69 63 20 66 6f 72 20 74 e specific for t
b61b0 68 65 20 4f 53 2f 32 0a 2a 2a 20 70 72 6f 74 61 he OS/2.** prota
b61c0 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f bility layer..*/
b61d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
b61e0 6f 73 32 46 69 6c 65 20 6f 73 32 46 69 6c 65 3b os2File os2File;
b61f0 0a 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 .struct os2File
b6200 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 {. const sqlite
b6210 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
b6220 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 ethod; /* Alway
b6230 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 s the first entr
b6240 79 20 2a 2f 0a 20 20 48 46 49 4c 45 20 68 3b 20 y */. HFILE h;
b6250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b6260 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 /* Handle for a
b6270 63 63 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c ccessing the fil
b6280 65 20 2a 2f 0a 20 20 63 68 61 72 2a 20 70 61 74 e */. char* pat
b6290 68 54 6f 44 65 6c 3b 20 20 20 20 20 20 20 20 20 hToDel;
b62a0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 /* Name of file
b62b0 20 74 6f 20 64 65 6c 65 74 65 20 6f 6e 20 63 6c to delete on cl
b62c0 6f 73 65 2c 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 ose, NULL if not
b62d0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
b62e0 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 har locktype;
b62f0 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20 /* Type of lock
b6300 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f currently held o
b6310 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 7d n this file */.}
b6320 3b 0a 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 5f ;..#define LOCK_
b6330 54 49 4d 45 4f 55 54 20 31 30 4c 20 2f 2a 20 74 TIMEOUT 10L /* t
b6340 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 he default locki
b6350 6e 67 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f ng timeout */../
b6360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b63a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
b63b0 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 The next group
b63c0 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c of routines impl
b63d0 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 ement the I/O me
b63e0 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a thods specified.
b63f0 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 ** by the sqlite
b6400 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
b6410 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ect..***********
b6420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b6460 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 ***/../*.** Clos
b6470 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 e a file..*/.sta
b6480 74 69 63 20 69 6e 74 20 6f 73 32 43 6c 6f 73 65 tic int os2Close
b6490 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ( sqlite3_file *
b64a0 69 64 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 id ){. APIRET r
b64b0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
b64c0 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 3b 0a os2File *pFile;.
b64d0 20 20 69 66 28 20 69 64 20 26 26 20 28 70 46 69 if( id && (pFi
b64e0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
b64f0 64 29 20 21 3d 20 30 20 29 7b 0a 20 20 20 20 4f d) != 0 ){. O
b6500 53 54 52 41 43 45 32 28 20 22 43 4c 4f 53 45 20 STRACE2( "CLOSE
b6510 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 %d\n", pFile->h
b6520 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 43 );. rc = DosC
b6530 6c 6f 73 65 28 20 70 46 69 6c 65 2d 3e 68 20 29 lose( pFile->h )
b6540 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 ;. pFile->loc
b6550 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b ktype = NO_LOCK;
b6560 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
b6570 70 61 74 68 54 6f 44 65 6c 20 21 3d 20 4e 55 4c pathToDel != NUL
b6580 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 L ){. rc =
b6590 44 6f 73 46 6f 72 63 65 44 65 6c 65 74 65 28 20 DosForceDelete(
b65a0 28 50 53 5a 29 70 46 69 6c 65 2d 3e 70 61 74 68 (PSZ)pFile->path
b65b0 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 66 ToDel );. f
b65c0 72 65 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 ree( pFile->path
b65d0 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 ToDel );. p
b65e0 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 File->pathToDel
b65f0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 = NULL;. }.
b6600 20 20 69 64 20 3d 20 30 3b 0a 20 20 20 20 4f 70 id = 0;. Op
b6610 65 6e 43 6f 75 6e 74 65 72 28 20 2d 31 20 29 3b enCounter( -1 );
b6620 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
b6630 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 c == NO_ERROR ?
b6640 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
b6650 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a TE_IOERR;.}../*.
b6660 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f ** Read data fro
b6670 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 m a file into a
b6680 62 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 buffer. Return
b6690 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c SQLITE_OK if all
b66a0 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 .** bytes were r
b66b0 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ead successfully
b66c0 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 and SQLITE_IOER
b66d0 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f R if anything go
b66e0 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a es.** wrong..*/.
b66f0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 65 static int os2Re
b6700 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 ad(. sqlite3_fi
b6710 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 le *id,
b6720 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f /* File to
b6730 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 read from */.
b6740 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 void *pBuf,
b6750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b6760 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 /* Write content
b6770 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 into this buffe
b6780 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 r */. int amt,
b6790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b67a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
b67b0 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
b67c0 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 d */. sqlite3_i
b67d0 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 nt64 offset
b67e0 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 /* Begin
b67f0 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 reading at this
b6800 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 55 offset */.){. U
b6810 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f LONG fileLocatio
b6820 6e 20 3d 20 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20 n = 0L;. ULONG
b6830 67 6f 74 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a got;. os2File *
b6840 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
b6850 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 *)id;. assert(
b6860 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c id!=0 );. Simul
b6870 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
b6880 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
b6890 52 45 41 44 20 29 3b 0a 20 20 4f 53 54 52 41 43 READ );. OSTRAC
b68a0 45 33 28 20 22 52 45 41 44 20 25 64 20 6c 6f 63 E3( "READ %d loc
b68b0 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e k=%d\n", pFile->
b68c0 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
b68d0 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 pe );. if( DosS
b68e0 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d etFilePtr(pFile-
b68f0 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 >h, offset, FILE
b6900 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 _BEGIN, &fileLoc
b6910 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 ation) != NO_ERR
b6920 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OR ){. return
b6930 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
b6940 20 7d 0a 20 20 69 66 28 20 44 6f 73 52 65 61 64 }. if( DosRead
b6950 28 20 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 ( pFile->h, pBuf
b6960 2c 20 61 6d 74 2c 20 26 67 6f 74 20 29 20 21 3d , amt, &got ) !=
b6970 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
b6980 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
b6990 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 OERR_READ;. }.
b69a0 20 69 66 28 20 67 6f 74 20 3d 3d 20 28 55 4c 4f if( got == (ULO
b69b0 4e 47 29 61 6d 74 20 29 0a 20 20 20 20 72 65 74 NG)amt ). ret
b69c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
b69d0 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 55 else {. /* U
b69e0 6e 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f nread portions o
b69f0 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 f the input buff
b6a00 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d er must be zero-
b6a10 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 filled */. me
b6a20 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 mset(&((char*)pB
b6a30 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 uf)[got], 0, amt
b6a40 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 -got);. retur
b6a50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 n SQLITE_IOERR_S
b6a60 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d HORT_READ;. }.}
b6a70 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
b6a80 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 ta from a buffer
b6a90 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 into a file. R
b6aa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
b6ab0 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 on success.** or
b6ac0 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f some other erro
b6ad0 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 r code on failur
b6ae0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
b6af0 20 6f 73 32 57 72 69 74 65 28 0a 20 20 73 71 6c os2Write(. sql
b6b00 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 ite3_file *id,
b6b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b6b20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e File to write in
b6b30 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f to */. const vo
b6b40 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 id *pBuf,
b6b50 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 /* The b
b6b60 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 ytes to be writt
b6b70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c en */. int amt,
b6b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b6b90 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
b6ba0 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 r of bytes to wr
b6bb0 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ite */. sqlite3
b6bc0 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 _int64 offset
b6bd0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
b6be0 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 et into the file
b6bf0 20 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e to begin writin
b6c00 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f g at */.){. ULO
b6c10 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 NG fileLocation
b6c20 3d 20 30 4c 3b 0a 20 20 41 50 49 52 45 54 20 72 = 0L;. APIRET r
b6c30 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
b6c40 55 4c 4f 4e 47 20 77 72 6f 74 65 3b 0a 20 20 6f ULONG wrote;. o
b6c50 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
b6c60 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
b6c70 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
b6c80 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
b6c90 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
b6ca0 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 29 3b E_IOERR_WRITE );
b6cb0 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 . SimulateDiskf
b6cc0 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e ullError( return
b6cd0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a SQLITE_FULL );.
b6ce0 20 20 4f 53 54 52 41 43 45 33 28 20 22 57 52 49 OSTRACE3( "WRI
b6cf0 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 TE %d lock=%d\n"
b6d00 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
b6d10 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
b6d20 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 if( DosSetFileP
b6d30 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 tr(pFile->h, off
b6d40 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c set, FILE_BEGIN,
b6d50 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 &fileLocation)
b6d60 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
b6d70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
b6d80 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 61 73 _IOERR;. }. as
b6d90 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20 sert( amt>0 );.
b6da0 20 77 68 69 6c 65 28 20 61 6d 74 20 3e 20 30 20 while( amt > 0
b6db0 26 26 0a 20 20 20 20 20 20 20 20 20 28 20 72 63 &&. ( rc
b6dc0 20 3d 20 44 6f 73 57 72 69 74 65 28 20 70 46 69 = DosWrite( pFi
b6dd0 6c 65 2d 3e 68 2c 20 28 50 56 4f 49 44 29 70 42 le->h, (PVOID)pB
b6de0 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 20 uf, amt, &wrote
b6df0 29 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 ) ) == NO_ERROR
b6e00 26 26 0a 20 20 20 20 20 20 20 20 20 77 72 6f 74 &&. wrot
b6e10 65 20 3e 20 30 0a 20 20 29 7b 0a 20 20 20 20 61 e > 0. ){. a
b6e20 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 mt -= wrote;.
b6e30 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a pBuf = &((char*
b6e40 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 )pBuf)[wrote];.
b6e50 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 20 72 }.. return ( r
b6e60 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c c != NO_ERROR ||
b6e70 20 61 6d 74 20 3e 20 28 69 6e 74 29 77 72 6f 74 amt > (int)wrot
b6e80 65 20 29 20 3f 20 53 51 4c 49 54 45 5f 46 55 4c e ) ? SQLITE_FUL
b6e90 4c 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d L : SQLITE_OK;.}
b6ea0 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 ../*.** Truncate
b6eb0 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f an open file to
b6ec0 20 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a a specified siz
b6ed0 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 e.*/.static int
b6ee0 6f 73 32 54 72 75 6e 63 61 74 65 28 20 73 71 6c os2Truncate( sql
b6ef0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
b6f00 36 34 20 6e 42 79 74 65 20 29 7b 0a 20 20 41 50 64 nByte ){. AP
b6f10 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 IRET rc = NO_ERR
b6f20 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 OR;. os2File *p
b6f30 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
b6f40 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 )id;. OSTRACE3(
b6f50 20 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c "TRUNCATE %d %l
b6f60 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c ld\n", pFile->h,
b6f70 20 6e 42 79 74 65 20 29 3b 0a 20 20 53 69 6d 75 nByte );. Simu
b6f80 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
b6f90 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
b6fa0 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 _TRUNCATE );. r
b6fb0 63 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 53 69 c = DosSetFileSi
b6fc0 7a 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 ze( pFile->h, nB
b6fd0 79 74 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 yte );. return
b6fe0 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f rc == NO_ERROR ?
b6ff0 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
b7000 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
b7010 54 45 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 TE;.}..#ifdef SQ
b7020 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
b7030 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 Count the number
b7040 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e of fullsyncs an
b7050 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 d normal syncs.
b7060 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f This is used to
b7070 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 test.** that sy
b7080 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 ncs and fullsync
b7090 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 s are occuring a
b70a0 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 t the right time
b70b0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
b70c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e int sqlite3_syn
b70d0 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c c_count = 0;.SQL
b70e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
b70f0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
b7100 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
b7110 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
b7120 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 all writes to a
b7130 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 particular file
b7140 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f are committed to
b7150 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 disk..*/.static
b7160 20 69 6e 74 20 6f 73 32 53 79 6e 63 28 20 73 71 int os2Sync( sq
b7170 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
b7180 69 6e 74 20 66 6c 61 67 73 20 29 7b 0a 20 20 6f int flags ){. o
b7190 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
b71a0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
b71b0 4f 53 54 52 41 43 45 33 28 20 22 53 59 4e 43 20 OSTRACE3( "SYNC
b71c0 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 %d lock=%d\n", p
b71d0 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e File->h, pFile->
b71e0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 23 69 66 64 locktype );.#ifd
b71f0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
b7200 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
b7210 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a ITE_SYNC_FULL){.
b7220 20 20 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c sqlite3_full
b7230 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 sync_count++;.
b7240 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 }. sqlite3_sync
b7250 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
b7260 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 . /* If we comp
b7270 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 iled with the SQ
b7280 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 LITE_NO_SYNC fla
b7290 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 g, then syncing
b72a0 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a is a. ** no-op.
b72b0 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 */.#ifdef SQLI
b72c0 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 TE_NO_SYNC. UNU
b72d0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 46 SED_PARAMETER(pF
b72e0 69 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ile);. return S
b72f0 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a QLITE_OK;.#else.
b7300 20 20 72 65 74 75 72 6e 20 44 6f 73 52 65 73 65 return DosRese
b7310 74 42 75 66 66 65 72 28 20 70 46 69 6c 65 2d 3e tBuffer( pFile->
b7320 68 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 h ) == NO_ERROR
b7330 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 ? SQLITE_OK : SQ
b7340 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 23 65 6e 64 LITE_IOERR;.#end
b7350 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 if.}../*.** Dete
b7360 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e rmine the curren
b7370 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 t size of a file
b7380 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 in bytes.*/.sta
b7390 74 69 63 20 69 6e 74 20 6f 73 32 46 69 6c 65 53 tic int os2FileS
b73a0 69 7a 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c ize( sqlite3_fil
b73b0 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 e *id, sqlite3_i
b73c0 6e 74 36 34 20 2a 70 53 69 7a 65 20 29 7b 0a 20 nt64 *pSize ){.
b73d0 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
b73e0 45 52 52 4f 52 3b 0a 20 20 46 49 4c 45 53 54 41 ERROR;. FILESTA
b73f0 54 55 53 33 20 66 73 74 73 33 46 69 6c 65 49 6e TUS3 fsts3FileIn
b7400 66 6f 3b 0a 20 20 6d 65 6d 73 65 74 28 26 66 73 fo;. memset(&fs
b7410 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 30 2c 20 ts3FileInfo, 0,
b7420 73 69 7a 65 6f 66 28 66 73 74 73 33 46 69 6c 65 sizeof(fsts3File
b7430 49 6e 66 6f 29 29 3b 0a 20 20 61 73 73 65 72 74 Info));. assert
b7440 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d ( id!=0 );. Sim
b7450 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 ulateIOError( re
b7460 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
b7470 52 5f 46 53 54 41 54 20 29 3b 0a 20 20 72 63 20 R_FSTAT );. rc
b7480 3d 20 44 6f 73 51 75 65 72 79 46 69 6c 65 49 6e = DosQueryFileIn
b7490 66 6f 28 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 fo( ((os2File*)i
b74a0 64 29 2d 3e 68 2c 20 46 49 4c 5f 53 54 41 4e 44 d)->h, FIL_STAND
b74b0 41 52 44 2c 20 26 66 73 74 73 33 46 69 6c 65 49 ARD, &fsts3FileI
b74c0 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 nfo, sizeof(FILE
b74d0 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 69 66 STATUS3) );. if
b74e0 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 ( rc == NO_ERROR
b74f0 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d ){. *pSize =
b7500 20 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2e 63 fsts3FileInfo.c
b7510 62 46 69 6c 65 3b 0a 20 20 20 20 72 65 74 75 72 bFile;. retur
b7520 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
b7530 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
b7540 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 SQLITE_IOERR_FS
b7550 54 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a TAT;. }.}../*.*
b7560 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 * Acquire a read
b7570 65 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 er lock..*/.stat
b7580 69 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c 6f ic int getReadLo
b7590 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ck( os2File *pFi
b75a0 6c 65 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b le ){. FILELOCK
b75b0 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 LockArea,.
b75c0 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 UnlockAr
b75d0 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 ea;. APIRET res
b75e0 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b ;. memset(&Lock
b75f0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
b7600 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 LockArea));. me
b7610 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 mset(&UnlockArea
b7620 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f , 0, sizeof(Unlo
b7630 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b ckArea));. Lock
b7640 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 Area.lOffset = S
b7650 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 4c HARED_FIRST;. L
b7660 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
b7670 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 SHARED_SIZE;.
b7680 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
b7690 65 74 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 et = 0L;. Unloc
b76a0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
b76b0 4c 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 L;. res = DosSe
b76c0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c tFileLocks( pFil
b76d0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 e->h, &UnlockAre
b76e0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f a, &LockArea, LO
b76f0 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 CK_TIMEOUT, 1L )
b7700 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 47 ;. OSTRACE3( "G
b7710 45 54 52 45 41 44 4c 4f 43 4b 20 25 64 20 72 65 ETREADLOCK %d re
b7720 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
b7730 68 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 74 75 h, res );. retu
b7740 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn res;.}../*.**
b7750 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b Undo a readlock
b7760 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
b7770 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 20 6f nlockReadLock( o
b7780 73 32 46 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 s2File *id ){.
b7790 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 FILELOCK LockAr
b77a0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ea,.
b77b0 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 UnlockArea;. AP
b77c0 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 IRET res;. mems
b77d0 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c et(&LockArea, 0,
b77e0 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 sizeof(LockArea
b77f0 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e ));. memset(&Un
b7800 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a lockArea, 0, siz
b7810 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 eof(UnlockArea))
b7820 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 ;. LockArea.lOf
b7830 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63 fset = 0L;. Loc
b7840 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
b7850 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e L;. UnlockArea.
b7860 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 lOffset = SHARED
b7870 5f 46 49 52 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b _FIRST;. Unlock
b7880 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 Area.lRange = SH
b7890 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 72 65 73 ARED_SIZE;. res
b78a0 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 = DosSetFileLoc
b78b0 6b 73 28 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f ks( id->h, &Unlo
b78c0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
b78d0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
b78e0 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 1L );. OSTRACE
b78f0 33 28 20 22 55 4e 4c 4f 43 4b 2d 52 45 41 44 4c 3( "UNLOCK-READL
b7900 4f 43 4b 20 66 69 6c 65 20 68 61 6e 64 6c 65 3d OCK file handle=
b7910 25 64 20 72 65 73 3d 25 64 3f 5c 6e 22 2c 20 69 %d res=%d?\n", i
b7920 64 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 72 d->h, res );. r
b7930 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a eturn res;.}../*
b7940 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c .** Lock the fil
b7950 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 e with the lock
b7960 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 specified by par
b7970 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 ameter locktype
b7980 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 - one.** of the
b7990 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
b79a0 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f (1) SHARED_
b79b0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 LOCK.** (2)
b79c0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a RESERVED_LOCK.**
b79d0 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 (3) PENDING
b79e0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 _LOCK.** (4)
b79f0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a EXCLUSIVE_LOCK.
b7a00 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 **.** Sometimes
b7a10 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 when requesting
b7a20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 one lock state,
b7a30 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 additional lock
b7a40 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e states.** are in
b7a50 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 serted in betwee
b7a60 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 n. The locking
b7a70 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e might fail on on
b7a80 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a e of the later.*
b7a90 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 * transitions le
b7aa0 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 aving the lock s
b7ab0 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 tate different f
b7ac0 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 rom what it star
b7ad0 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c ted but.** still
b7ae0 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f short of its go
b7af0 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 al. The followi
b7b00 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 ng chart shows t
b7b10 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 he allowed.** tr
b7b20 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 ansitions and th
b7b30 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 e inserted inter
b7b40 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a mediate states:.
b7b50 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 **.** UNLOCKE
b7b60 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 D -> SHARED.**
b7b70 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 SHARED -> RESE
b7b80 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 RVED.** SHARE
b7b90 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
b7ba0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
b7bb0 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 RESERVED -> (P
b7bc0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
b7bd0 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 SIVE.** PENDI
b7be0 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a NG -> EXCLUSIVE.
b7bf0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
b7c00 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 ne will only inc
b7c10 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 rease a lock. T
b7c20 68 65 20 6f 73 32 55 6e 6c 6f 63 6b 28 29 20 72 he os2Unlock() r
b7c30 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 outine.** erases
b7c40 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e all locks at on
b7c50 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 ce and returns u
b7c60 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f s immediately to
b7c70 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 locking level 0
b7c80 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 ..** It is not p
b7c90 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 ossible to lower
b7ca0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
b7cb0 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 el one step at a
b7cc0 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d time. You.** m
b7cd0 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 ust go straight
b7ce0 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c to locking level
b7cf0 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 0..*/.static in
b7d00 74 20 6f 73 32 4c 6f 63 6b 28 20 73 71 6c 69 74 t os2Lock( sqlit
b7d10 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
b7d20 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69 locktype ){. i
b7d30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
b7d40 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 K; /* Retu
b7d50 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 rn code from sub
b7d60 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 41 50 routines */. AP
b7d70 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 IRET res = NO_ER
b7d80 52 4f 52 3b 20 20 20 20 2f 2a 20 52 65 73 75 6c ROR; /* Resul
b7d90 74 20 6f 66 20 61 6e 20 4f 53 2f 32 20 6c 6f 63 t of an OS/2 loc
b7da0 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 k call */. int
b7db0 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 newLocktype;
b7dc0 20 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d /* Set pFile-
b7dd0 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 >locktype to thi
b7de0 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 s value before e
b7df0 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 xiting */. int
b7e00 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d gotPendingLock =
b7e10 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 0;/* True if we
b7e20 20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 acquired a PEND
b7e30 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 ING lock this ti
b7e40 6d 65 20 2a 2f 0a 20 20 46 49 4c 45 4c 4f 43 4b me */. FILELOCK
b7e50 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 LockArea,.
b7e60 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 UnlockAr
b7e70 65 61 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 ea;. os2File *p
b7e80 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
b7e90 29 69 64 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c )id;. memset(&L
b7ea0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
b7eb0 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 of(LockArea));.
b7ec0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 memset(&UnlockA
b7ed0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 rea, 0, sizeof(U
b7ee0 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 nlockArea));. a
b7ef0 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 ssert( pFile!=0
b7f00 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 );. OSTRACE4( "
b7f10 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25 LOCK %d %d was %
b7f20 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
b7f30 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d locktype, pFile-
b7f40 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 0a 20 20 >locktype );..
b7f50 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 /* If there is a
b7f60 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 lready a lock of
b7f70 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f this type or mo
b7f80 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f re restrictive o
b7f90 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 73 32 46 69 n the. ** os2Fi
b7fa0 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 le, do nothing.
b7fb0 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e Don't use the en
b7fc0 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 d_lock: exit pat
b7fd0 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 h, as. ** sqlit
b7fe0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
b7ff0 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c hasn't been cal
b8000 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 led yet.. */.
b8010 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
b8020 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype>=locktype ){
b8030 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 . OSTRACE3( "
b8040 4c 4f 43 4b 20 25 64 20 25 64 20 6f 6b 20 28 61 LOCK %d %d ok (a
b8050 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c lready held)\n",
b8060 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
b8070 79 70 65 20 29 3b 0a 20 20 20 20 72 65 74 75 72 ype );. retur
b8080 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
b8090 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
b80a0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 the locking seq
b80b0 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 uence is correct
b80c0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
b80d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 pFile->locktype!
b80e0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b =NO_LOCK || lock
b80f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
b8100 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c K );. assert( l
b8110 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 ocktype!=PENDING
b8120 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
b8130 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 t( locktype!=RES
b8140 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 ERVED_LOCK || pF
b8150 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
b8160 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 HARED_LOCK );..
b8170 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e /* Lock the PEN
b8180 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 DING_LOCK byte i
b8190 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 f we need to acq
b81a0 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c uire a PENDING l
b81b0 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 ock or. ** a SH
b81c0 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 ARED lock. If w
b81d0 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 e are acquiring
b81e0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 a SHARED lock, t
b81f0 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f he acquisition o
b8200 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 f. ** the PENDI
b8210 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 NG_LOCK byte is
b8220 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a temporary.. */.
b8230 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 newLocktype =
b8240 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b pFile->locktype;
b8250 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
b8260 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a cktype==NO_LOCK.
b8270 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 || (lockty
b8280 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
b8290 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK && pFile->loc
b82a0 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f ktype==RESERVED_
b82b0 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 4c LOCK). ){. L
b82c0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
b82d0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
b82e0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
b82f0 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e nge = 1L;. Un
b8300 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
b8310 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
b8320 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
b8330 4c 3b 0a 0a 20 20 20 20 2f 2a 20 77 61 69 74 20 L;.. /* wait
b8340 6c 6f 6e 67 65 72 20 74 68 61 6e 20 4c 4f 43 4b longer than LOCK
b8350 5f 54 49 4d 45 4f 55 54 20 68 65 72 65 20 6e 6f _TIMEOUT here no
b8360 74 20 74 6f 20 68 61 76 65 20 74 6f 20 74 72 79 t to have to try
b8370 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 multiple times
b8380 2a 2f 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 */. res = Dos
b8390 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
b83a0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
b83b0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
b83c0 31 30 30 4c 2c 20 30 4c 20 29 3b 0a 20 20 20 20 100L, 0L );.
b83d0 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 if( res == NO_ER
b83e0 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ROR ){. got
b83f0 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 31 3b PendingLock = 1;
b8400 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 . OSTRACE3(
b8410 20 22 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e "LOCK %d pendin
b8420 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e 20 73 g lock boolean s
b8430 65 74 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 et. res=%d\n",
b8440 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
b8450 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
b8460 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65 Acquire a share
b8470 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 d lock. */. if
b8480 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
b8490 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d ED_LOCK && res =
b84a0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
b84b0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
b84c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f >locktype==NO_LO
b84d0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 CK );. res =
b84e0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
b84f0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 e);. if( res
b8500 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
b8510 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 newLocktype
b8520 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a = SHARED_LOCK;.
b8530 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 }. OSTRAC
b8540 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 E3( "LOCK %d acq
b8550 75 69 72 65 20 73 68 61 72 65 64 20 6c 6f 63 6b uire shared lock
b8560 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 . res=%d\n", pFi
b8570 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 le->h, res );.
b8580 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
b8590 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a a RESERVED lock.
b85a0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
b85b0 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype==RESERVED_LO
b85c0 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f CK && res == NO_
b85d0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 ERROR ){. ass
b85e0 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ert( pFile->lock
b85f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
b8600 4b 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 K );. LockAre
b8610 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 a.lOffset = RESE
b8620 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c RVED_BYTE;. L
b8630 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
b8640 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 1L;. UnlockA
b8650 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
b8660 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
b8670 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
b8680 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 res = DosSetFi
b8690 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e leLocks( pFile->
b86a0 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 h, &UnlockArea,
b86b0 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f &LockArea, LOCK_
b86c0 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 TIMEOUT, 0L );.
b86d0 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f if( res == NO
b86e0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 _ERROR ){.
b86f0 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 newLocktype = RE
b8700 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 SERVED_LOCK;.
b8710 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 }. OSTRACE3(
b8720 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 "LOCK %d acquir
b8730 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e e reserved lock.
b8740 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
b8750 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d e->h, res );. }
b8760 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 .. /* Acquire a
b8770 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 PENDING lock.
b8780 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
b8790 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
b87a0 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 K && res == NO_E
b87b0 52 52 4f 52 20 29 7b 0a 20 20 20 20 6e 65 77 4c RROR ){. newL
b87c0 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e ocktype = PENDIN
b87d0 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 G_LOCK;. gotP
b87e0 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a endingLock = 0;.
b87f0 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4c OSTRACE2( "L
b8800 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 70 OCK %d acquire p
b8810 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 70 65 6e ending lock. pen
b8820 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 ding lock boolea
b8830 6e 20 75 6e 73 65 74 2e 5c 6e 22 2c 20 70 46 69 n unset.\n", pFi
b8840 6c 65 2d 3e 68 20 29 3b 0a 20 20 7d 0a 0a 20 20 le->h );. }..
b8850 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 /* Acquire an EX
b8860 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a CLUSIVE lock. *
b8870 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 /. if( locktype
b8880 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
b8890 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 && res == NO_ER
b88a0 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ROR ){. asser
b88b0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
b88c0 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe>=SHARED_LOCK
b88d0 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c );. res = unl
b88e0 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c ockReadLock(pFil
b88f0 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 e);. OSTRACE2
b8900 28 20 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 ( "unreadlock =
b8910 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20 %d\n", res );.
b8920 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
b8930 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 et = SHARED_FIRS
b8940 54 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e T;. LockArea.
b8950 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f lRange = SHARED_
b8960 53 49 5a 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b SIZE;. Unlock
b8970 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 Area.lOffset = 0
b8980 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
b8990 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
b89a0 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 res = DosSetF
b89b0 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d ileLocks( pFile-
b89c0 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c >h, &UnlockArea,
b89d0 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b &LockArea, LOCK
b89e0 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a _TIMEOUT, 0L );.
b89f0 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e if( res == N
b8a00 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
b8a10 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45 newLocktype = E
b8a20 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 XCLUSIVE_LOCK;.
b8a30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
b8a40 4f 53 54 52 41 43 45 32 28 20 22 4f 53 2f 32 20 OSTRACE2( "OS/2
b8a50 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c error-code = %d\
b8a60 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 20 n", res );.
b8a70 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 getReadLock(pFi
b8a80 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f le);. }. O
b8a90 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
b8aa0 64 20 61 63 71 75 69 72 65 20 65 78 63 6c 75 73 d acquire exclus
b8ab0 69 76 65 20 6c 6f 63 6b 2e 20 20 72 65 73 3d 25 ive lock. res=%
b8ac0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
b8ad0 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a res );. }.. /*
b8ae0 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69 If we are holdi
b8af0 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ng a PENDING loc
b8b00 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20 k that ought to
b8b10 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 be released, the
b8b20 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 n. ** release i
b8b30 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 t now.. */. if
b8b40 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b ( gotPendingLock
b8b50 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 && locktype==SH
b8b60 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
b8b70 20 69 6e 74 20 72 3b 0a 20 20 20 20 4c 6f 63 6b int r;. Lock
b8b80 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 Area.lOffset = 0
b8b90 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e L;. LockArea.
b8ba0 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 lRange = 0L;.
b8bb0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
b8bc0 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 set = PENDING_BY
b8bd0 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 TE;. UnlockAr
b8be0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a ea.lRange = 1L;.
b8bf0 20 20 20 20 72 20 3d 20 44 6f 73 53 65 74 46 69 r = DosSetFi
b8c00 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e leLocks( pFile->
b8c10 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 h, &UnlockArea,
b8c20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f &LockArea, LOCK_
b8c30 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 TIMEOUT, 0L );.
b8c40 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f OSTRACE3( "LO
b8c50 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 69 6e 67 20 CK %d unlocking
b8c60 70 65 6e 64 69 6e 67 2f 69 73 20 73 68 61 72 65 pending/is share
b8c70 64 2e 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c d. r=%d\n", pFil
b8c80 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 0a e->h, r );. }..
b8c90 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
b8ca0 73 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 state of the loc
b8cb0 6b 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68 k has held in th
b8cc0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
b8cd0 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 r then. ** retu
b8ce0 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 rn the appropria
b8cf0 74 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a te result code..
b8d00 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 3d */. if( res =
b8d10 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
b8d20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
b8d30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f ;. }else{. O
b8d40 53 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 46 STRACE4( "LOCK F
b8d50 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 AILED %d trying
b8d60 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 for %d but got %
b8d70 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
b8d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f lo
b8d90 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 cktype, newLockt
b8da0 79 70 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ype );. rc =
b8db0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
b8dc0 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 . pFile->lockty
b8dd0 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 70 65 pe = newLocktype
b8de0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c ;. OSTRACE3( "L
b8df0 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22 OCK %d now %d\n"
b8e00 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
b8e10 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
b8e20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
b8e30 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b8e40 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
b8e50 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
b8e60 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
b8e70 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
b8e80 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
b8e90 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
b8ea0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
b8eb0 69 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a is held, return.
b8ec0 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 ** non-zero, oth
b8ed0 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a erwise zero..*/.
b8ee0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 43 68 static int os2Ch
b8ef0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
b8f00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
b8f10 64 2c 20 69 6e 74 20 2a 70 4f 75 74 20 29 7b 0a d, int *pOut ){.
b8f20 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 6f int r = 0;. o
b8f30 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
b8f40 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
b8f50 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 assert( pFile!=0
b8f60 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d );. if( pFile-
b8f70 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 >locktype>=RESER
b8f80 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 VED_LOCK ){.
b8f90 72 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 r = 1;. OSTRA
b8fa0 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f CE3( "TEST WR-LO
b8fb0 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 CK %d %d (local)
b8fc0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
b8fd0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
b8fe0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
b8ff0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
b9000 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 UnlockArea;.
b9010 20 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e APIRET rc = N
b9020 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 20 6d 65 6d O_ERROR;. mem
b9030 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 set(&LockArea, 0
b9040 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 , sizeof(LockAre
b9050 61 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 a));. memset(
b9060 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 &UnlockArea, 0,
b9070 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 sizeof(UnlockAre
b9080 61 29 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 a));. LockAre
b9090 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 a.lOffset = RESE
b90a0 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c RVED_BYTE;. L
b90b0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
b90c0 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 1L;. UnlockA
b90d0 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
b90e0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
b90f0 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
b9100 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69 6c rc = DosSetFil
b9110 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
b9120 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
b9130 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
b9140 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 IMEOUT, 0L );.
b9150 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 OSTRACE3( "TES
b9160 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 6c 6f 63 T WR-LOCK %d loc
b9170 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 k reserved byte
b9180 72 63 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d rc=%d\n", pFile-
b9190 3e 68 2c 20 72 63 20 29 3b 0a 20 20 20 20 69 66 >h, rc );. if
b91a0 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 ( rc == NO_ERROR
b91b0 20 29 7b 0a 20 20 20 20 20 20 41 50 49 52 45 54 ){. APIRET
b91c0 20 72 63 75 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b rcu = NO_ERROR;
b91d0 20 2f 2a 20 72 65 74 75 72 6e 20 63 6f 64 65 20 /* return code
b91e0 66 6f 72 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f for unlocking */
b91f0 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e . LockArea.
b9200 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
b9210 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
b9220 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 nge = 0L;.
b9230 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
b9240 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 et = RESERVED_BY
b9250 54 45 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b TE;. Unlock
b9260 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c Area.lRange = 1L
b9270 3b 0a 20 20 20 20 20 20 72 63 75 20 3d 20 44 6f ;. rcu = Do
b9280 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
b9290 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
b92a0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
b92b0 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 LOCK_TIMEOUT, 0
b92c0 4c 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 L );. OSTRA
b92d0 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f CE3( "TEST WR-LO
b92e0 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 20 72 65 73 CK %d unlock res
b92f0 65 72 76 65 64 20 62 79 74 65 20 72 3d 25 64 5c erved byte r=%d\
b9300 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 n", pFile->h, rc
b9310 75 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 u );. }. r
b9320 20 3d 20 21 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 = !(rc == NO_ER
b9330 52 4f 52 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 ROR);. OSTRAC
b9340 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 E3( "TEST WR-LOC
b9350 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 K %d %d (remote)
b9360 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
b9370 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 );. }. *pOut
b9380 3d 20 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 = r;. return SQ
b9390 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
b93a0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b * Lower the lock
b93b0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c ing level on fil
b93c0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20 e descriptor id
b93d0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
b93e0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
b93f0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
b9400 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
b9410 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
b9420 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
b9430 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
b9440 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
b9450 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
b9460 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
b9470 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
b9480 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
b9490 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e p..**.** It is n
b94a0 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 ot possible for
b94b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 this routine to
b94c0 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f fail if the seco
b94d0 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 nd argument.** i
b94e0 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 s NO_LOCK. If t
b94f0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
b9500 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 nt is SHARED_LOC
b9510 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 K then this rout
b9520 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 ine.** might ret
b9530 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
b9540 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ;.*/.static int
b9550 6f 73 32 55 6e 6c 6f 63 6b 28 20 73 71 6c 69 74 os2Unlock( sqlit
b9560 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
b9570 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69 locktype ){. i
b9580 6e 74 20 74 79 70 65 3b 0a 20 20 6f 73 32 46 69 nt type;. os2Fi
b9590 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
b95a0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50 49 52 File*)id;. APIR
b95b0 45 54 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f ET rc = SQLITE_O
b95c0 4b 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 K;. APIRET res
b95d0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 = NO_ERROR;. FI
b95e0 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 LELOCK LockArea
b95f0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e ,. Un
b9600 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6d 65 6d 73 lockArea;. mems
b9610 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c et(&LockArea, 0,
b9620 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 sizeof(LockArea
b9630 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e ));. memset(&Un
b9640 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a lockArea, 0, siz
b9650 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 eof(UnlockArea))
b9660 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c ;. assert( pFil
b9670 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 e!=0 );. assert
b9680 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
b9690 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 ED_LOCK );. OST
b96a0 52 41 43 45 34 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE4( "UNLOCK %
b96b0 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 5c 6e d to %d was %d\n
b96c0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
b96d0 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f ktype, pFile->lo
b96e0 63 6b 74 79 70 65 20 29 3b 0a 20 20 74 79 70 65 cktype );. type
b96f0 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 = pFile->lockty
b9700 70 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d pe;. if( type>=
b9710 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
b9720 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c {. LockArea.l
b9730 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 Offset = 0L;.
b9740 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 LockArea.lRange
b9750 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
b9760 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
b9770 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 SHARED_FIRST;.
b9780 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
b9790 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a nge = SHARED_SIZ
b97a0 45 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 E;. res = Dos
b97b0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
b97c0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
b97d0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
b97e0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
b97f0 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 );. OSTRACE3
b9800 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 65 78 63 ( "UNLOCK %d exc
b9810 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 65 73 3d lusive lock res=
b9820 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
b9830 20 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 res );. if(
b9840 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
b9850 5f 4c 4f 43 4b 20 26 26 20 67 65 74 52 65 61 64 _LOCK && getRead
b9860 4c 6f 63 6b 28 70 46 69 6c 65 29 20 21 3d 20 4e Lock(pFile) != N
b9870 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
b9880 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 /* This should
b9890 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 never happen. W
b98a0 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 e should always
b98b0 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 be able to.
b98c0 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 ** reacquire th
b98d0 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 e read lock */.
b98e0 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 OSTRACE3( "
b98f0 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 UNLOCK %d to %d
b9900 67 65 74 52 65 61 64 4c 6f 63 6b 28 29 20 66 61 getReadLock() fa
b9910 69 6c 65 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e iled\n", pFile->
b9920 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 h, locktype );.
b9930 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
b9940 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 _IOERR_UNLOCK;.
b9950 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 }. }. if( t
b9960 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype>=RESERVED_LO
b9970 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 CK ){. LockAr
b9980 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
b9990 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 . LockArea.lR
b99a0 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 ange = 0L;. U
b99b0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 nlockArea.lOffse
b99c0 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
b99d0 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 E;. UnlockAre
b99e0 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 a.lRange = 1L;.
b99f0 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 res = DosSetF
b9a00 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d ileLocks( pFile-
b9a10 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c >h, &UnlockArea,
b9a20 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b &LockArea, LOCK
b9a30 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a _TIMEOUT, 0L );.
b9a40 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 OSTRACE3( "U
b9a50 4e 4c 4f 43 4b 20 25 64 20 72 65 73 65 72 76 65 NLOCK %d reserve
b9a60 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 d res=%d\n", pFi
b9a70 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 le->h, res );.
b9a80 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 }. if( locktype
b9a90 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 ==NO_LOCK && typ
b9aa0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e>=SHARED_LOCK )
b9ab0 7b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f {. res = unlo
b9ac0 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 ckReadLock(pFile
b9ad0 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 35 28 );. OSTRACE5(
b9ae0 20 22 55 4e 4c 4f 43 4b 20 25 64 20 69 73 20 25 "UNLOCK %d is %
b9af0 64 20 77 61 6e 74 20 25 64 20 72 65 73 3d 25 64 d want %d res=%d
b9b00 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 74 \n", pFile->h, t
b9b10 79 70 65 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 72 ype, locktype, r
b9b20 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 es );. }. if(
b9b30 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f type>=PENDING_LO
b9b40 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 CK ){. LockAr
b9b50 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
b9b60 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 . LockArea.lR
b9b70 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 ange = 0L;. U
b9b80 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 nlockArea.lOffse
b9b90 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 t = PENDING_BYTE
b9ba0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
b9bb0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
b9bc0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 res = DosSetFi
b9bd0 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e leLocks( pFile->
b9be0 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 h, &UnlockArea,
b9bf0 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f &LockArea, LOCK_
b9c00 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 TIMEOUT, 0L );.
b9c10 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e OSTRACE3( "UN
b9c20 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e 67 20 LOCK %d pending
b9c30 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 res=%d\n", pFile
b9c40 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a ->h, res );. }.
b9c50 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
b9c60 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
b9c70 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 OSTRACE3( "UNLOC
b9c80 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 K %d now %d\n",
b9c90 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d pFile->h, pFile-
b9ca0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 72 >locktype );. r
b9cb0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
b9cc0 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 ** Control and q
b9cd0 75 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e uery of the open
b9ce0 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f file handle..*/
b9cf0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 .static int os2F
b9d00 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 ileControl(sqlit
b9d10 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
b9d20 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 op, void *pArg)
b9d30 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 {. switch( op )
b9d40 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
b9d50 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
b9d60 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 E: {. *(int
b9d70 2a 29 70 41 72 67 20 3d 20 28 28 6f 73 32 46 69 *)pArg = ((os2Fi
b9d80 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 le*)id)->locktyp
b9d90 65 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 e;. OSTRACE
b9da0 33 28 20 22 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 3( "FCNTL_LOCKST
b9db0 41 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e ATE %d lock=%d\n
b9dc0 22 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 ", ((os2File*)id
b9dd0 29 2d 3e 68 2c 20 28 28 6f 73 32 46 69 6c 65 2a )->h, ((os2File*
b9de0 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 )id)->locktype )
b9df0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
b9e00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
b9e10 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
b9e20 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a ITE_ERROR;.}../*
b9e30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
b9e40 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 ector size in by
b9e50 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 tes of the under
b9e60 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 lying block devi
b9e70 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 ce for.** the sp
b9e80 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 ecified file. Th
b9e90 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 is is almost alw
b9ea0 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 ays 512 bytes, b
b9eb0 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 ut may be.** lar
b9ec0 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 ger for some dev
b9ed0 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 ices..**.** SQLi
b9ee0 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 te code assumes
b9ef0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 this function ca
b9f00 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c nnot fail. It al
b9f10 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a so assumes that.
b9f20 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 ** if two files
b9f30 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 are created in t
b9f40 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 he same file-sys
b9f50 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 tem directory (i
b9f60 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 .e..** a databas
b9f70 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 e and its journa
b9f80 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 l file) that the
b9f90 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c sector size wil
b9fa0 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 l be the.** same
b9fb0 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 for both..*/.st
b9fc0 61 74 69 63 20 69 6e 74 20 6f 73 32 53 65 63 74 atic int os2Sect
b9fd0 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 orSize(sqlite3_f
b9fe0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 ile *id){. retu
b9ff0 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c rn SQLITE_DEFAUL
ba000 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d T_SECTOR_SIZE;.}
ba010 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
ba020 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 vector of devic
ba030 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
ba040 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
ba050 20 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63 os2DeviceCharac
ba060 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
ba070 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 3_file *id){. r
ba080 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn 0;.}.../*.
ba090 2a 2a 20 43 68 61 72 61 63 74 65 72 20 73 65 74 ** Character set
ba0a0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 conversion obje
ba0b0 63 74 73 20 75 73 65 64 20 62 79 20 63 6f 6e 76 cts used by conv
ba0c0 65 72 73 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e ersion routines.
ba0d0 0a 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 .*/.static Uconv
ba0e0 4f 62 6a 65 63 74 20 75 63 55 74 66 38 20 3d 20 Object ucUtf8 =
ba0f0 4e 55 4c 4c 3b 20 2f 2a 20 63 6f 6e 76 65 72 74 NULL; /* convert
ba100 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 20 61 between UTF-8 a
ba110 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 73 74 61 74 nd UCS-2 */.stat
ba120 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 ic UconvObject u
ba130 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a clCp = NULL; /*
ba140 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e convert between
ba150 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20 local codepage
ba160 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 0a 2f 2a and UCS-2 */../*
ba170 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 .** Helper funct
ba180 69 6f 6e 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a ion to initializ
ba190 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e e the conversion
ba1a0 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e objects from an
ba1b0 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 d to UTF-8..*/.s
ba1c0 74 61 74 69 63 20 76 6f 69 64 20 69 6e 69 74 55 tatic void initU
ba1d0 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 convObjects( voi
ba1e0 64 20 29 7b 0a 20 20 69 66 28 20 55 6e 69 43 72 d ){. if( UniCr
ba1f0 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 eateUconvObject(
ba200 20 55 54 46 5f 38 2c 20 26 75 63 55 74 66 38 20 UTF_8, &ucUtf8
ba210 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 ) != ULS_SUCCESS
ba220 20 29 0a 20 20 20 20 75 63 55 74 66 38 20 3d 20 ). ucUtf8 =
ba230 4e 55 4c 4c 3b 0a 20 20 69 66 20 28 20 55 6e 69 NULL;. if ( Uni
ba240 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63 CreateUconvObjec
ba250 74 28 20 28 55 6e 69 43 68 61 72 20 2a 29 4c 22 t( (UniChar *)L"
ba260 40 70 61 74 68 3d 79 65 73 22 2c 20 26 75 63 6c @path=yes", &ucl
ba270 43 70 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 Cp ) != ULS_SUCC
ba280 45 53 53 20 29 0a 20 20 20 20 75 63 6c 43 70 20 ESS ). uclCp
ba290 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a = NULL;.}../*.**
ba2a0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e Helper function
ba2b0 20 74 6f 20 66 72 65 65 20 74 68 65 20 63 6f 6e to free the con
ba2c0 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 version objects
ba2d0 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d from and to UTF-
ba2e0 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 8..*/.static voi
ba2f0 64 20 66 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 d freeUconvObjec
ba300 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 ts( void ){. if
ba310 20 28 20 75 63 55 74 66 38 20 29 0a 20 20 20 20 ( ucUtf8 ).
ba320 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 UniFreeUconvObje
ba330 63 74 28 20 75 63 55 74 66 38 20 29 3b 0a 20 20 ct( ucUtf8 );.
ba340 69 66 20 28 20 75 63 6c 43 70 20 29 0a 20 20 20 if ( uclCp ).
ba350 20 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a UniFreeUconvObj
ba360 65 63 74 28 20 75 63 6c 43 70 20 29 3b 0a 20 20 ect( uclCp );.
ba370 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 ucUtf8 = NULL;.
ba380 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d uclCp = NULL;.}
ba390 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 ../*.** Helper f
ba3a0 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 unction to conve
ba3b0 72 74 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d rt UTF-8 filenam
ba3c0 65 73 20 74 6f 20 6c 6f 63 61 6c 20 4f 53 2f 32 es to local OS/2
ba3d0 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68 codepage..** Th
ba3e0 65 20 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65 e two-step proce
ba3f0 73 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72 ss: first conver
ba400 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 55 t the incoming U
ba410 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 69 TF-8 string.** i
ba420 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 nto UCS-2 and th
ba430 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f en from UCS-2 to
ba440 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 64 the current cod
ba450 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 72 65 epage..** The re
ba460 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e turned char poin
ba470 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72 ter has to be fr
ba480 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 eed..*/.static c
ba490 68 61 72 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 har *convertUtf8
ba4a0 50 61 74 68 54 6f 43 70 28 20 63 6f 6e 73 74 20 PathToCp( const
ba4b0 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e char *in ){. Un
ba4c0 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 iChar tempPath[C
ba4d0 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 CHMAXPATH];. ch
ba4e0 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 ar *out = (char
ba4f0 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 *)calloc( CCHMAX
ba500 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 PATH, 1 );.. if
ba510 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 ( !out ). ret
ba520 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 urn NULL;.. if(
ba530 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c !ucUtf8 || !ucl
ba540 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f Cp ). initUco
ba550 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 nvObjects();..
ba560 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74 72 /* determine str
ba570 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 ing for the conv
ba580 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20 ersion of UTF-8
ba590 77 68 69 63 68 20 69 73 20 43 50 31 32 30 38 20 which is CP1208
ba5a0 2a 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 */. if( UniStrT
ba5b0 6f 55 63 73 28 20 75 63 55 74 66 38 2c 20 74 65 oUcs( ucUtf8, te
ba5c0 6d 70 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 mpPath, (char *)
ba5d0 69 6e 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 in, CCHMAXPATH )
ba5e0 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 != ULS_SUCCESS
ba5f0 29 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 ). return out
ba600 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 ; /* if conversi
ba610 6f 6e 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e on fails, return
ba620 20 74 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e the empty strin
ba630 67 20 2a 2f 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 g */.. /* conve
ba640 72 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e rsion for curren
ba650 74 20 63 6f 64 65 70 61 67 65 20 77 68 69 63 68 t codepage which
ba660 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 can be used for
ba670 20 70 61 74 68 73 20 2a 2f 0a 20 20 55 6e 69 53 paths */. UniS
ba680 74 72 46 72 6f 6d 55 63 73 28 20 75 63 6c 43 70 trFromUcs( uclCp
ba690 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c , out, tempPath,
ba6a0 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a CCHMAXPATH );..
ba6b0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a return out;.}.
ba6c0 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 ./*.** Helper fu
ba6d0 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 nction to conver
ba6e0 74 20 66 69 6c 65 6e 61 6d 65 73 20 66 72 6f 6d t filenames from
ba6f0 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20 local codepage
ba700 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 to UTF-8..** The
ba710 20 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65 73 two-step proces
ba720 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72 74 s: first convert
ba730 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f the incoming co
ba740 64 65 70 61 67 65 2d 73 70 65 63 69 66 69 63 0a depage-specific.
ba750 2a 2a 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 55 ** string into U
ba760 43 53 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 CS-2 and then fr
ba770 6f 6d 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 om UCS-2 to the
ba780 63 6f 64 65 70 61 67 65 20 6f 66 20 55 54 46 2d codepage of UTF-
ba790 38 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 8..** The return
ba7a0 65 64 20 63 68 61 72 20 70 6f 69 6e 74 65 72 20 ed char pointer
ba7b0 68 61 73 20 74 6f 20 62 65 20 66 72 65 65 64 2e has to be freed.
ba7c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
ba7d0 74 69 6f 6e 20 69 73 20 6e 6f 6e 2d 73 74 61 74 tion is non-stat
ba7e0 69 63 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f ic to be able to
ba7f0 20 75 73 65 20 74 68 69 73 20 69 6e 20 73 68 65 use this in she
ba800 6c 6c 2e 63 20 61 6e 64 0a 2a 2a 20 73 69 6d 69 ll.c and.** simi
ba810 6c 61 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 lar applications
ba820 20 74 68 61 74 20 74 61 6b 65 20 63 6f 6d 6d 61 that take comma
ba830 6e 64 20 6c 69 6e 65 20 61 72 67 75 6d 65 6e 74 nd line argument
ba840 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a 63 6f 6e 76 s..*/.char *conv
ba850 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 ertCpPathToUtf8(
ba860 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 const char *in
ba870 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d ){. UniChar tem
ba880 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 pPath[CCHMAXPATH
ba890 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d ];. char *out =
ba8a0 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 (char *)calloc(
ba8b0 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 CCHMAXPATH, 1 )
ba8c0 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a ;.. if( !out ).
ba8d0 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b return NULL;
ba8e0 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20 .. if( !ucUtf8
ba8f0 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 || !uclCp ).
ba900 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 initUconvObjects
ba910 28 29 3b 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 ();.. /* conver
ba920 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e 74 sion for current
ba930 20 63 6f 64 65 70 61 67 65 20 77 68 69 63 68 20 codepage which
ba940 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 can be used for
ba950 70 61 74 68 73 20 2a 2f 0a 20 20 69 66 28 20 55 paths */. if( U
ba960 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 6c 43 niStrToUcs( uclC
ba970 70 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 p, tempPath, (ch
ba980 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 ar *)in, CCHMAXP
ba990 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 ATH ) != ULS_SUC
ba9a0 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 CESS ). retur
ba9b0 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e n out; /* if con
ba9c0 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 version fails, r
ba9d0 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 eturn the empty
ba9e0 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 string */.. /*
ba9f0 64 65 74 65 72 6d 69 6e 65 20 73 74 72 69 6e 67 determine string
baa00 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 72 73 for the convers
baa10 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 68 69 ion of UTF-8 whi
baa20 63 68 20 69 73 20 43 50 31 32 30 38 20 2a 2f 0a ch is CP1208 */.
baa30 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 UniStrFromUcs(
baa40 20 75 63 55 74 66 38 2c 20 6f 75 74 2c 20 74 65 ucUtf8, out, te
baa50 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 mpPath, CCHMAXPA
baa60 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 TH );.. return
baa70 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 out;.}../*.** Th
baa80 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 is vector define
baa90 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 s all the method
baaa0 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 s that can opera
baab0 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 te on an.** sqli
baac0 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 6f 73 32 te3_file for os2
baad0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
baae0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
baaf0 68 6f 64 73 20 6f 73 32 49 6f 4d 65 74 68 6f 64 hods os2IoMethod
bab00 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 = {. 1,
bab10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bab20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
bab30 20 20 6f 73 32 43 6c 6f 73 65 2c 0a 20 20 6f 73 os2Close,. os
bab40 32 52 65 61 64 2c 0a 20 20 6f 73 32 57 72 69 74 2Read,. os2Writ
bab50 65 2c 0a 20 20 6f 73 32 54 72 75 6e 63 61 74 65 e,. os2Truncate
bab60 2c 0a 20 20 6f 73 32 53 79 6e 63 2c 0a 20 20 6f ,. os2Sync,. o
bab70 73 32 46 69 6c 65 53 69 7a 65 2c 0a 20 20 6f 73 s2FileSize,. os
bab80 32 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55 6e 6c 6f 2Lock,. os2Unlo
bab90 63 6b 2c 0a 20 20 6f 73 32 43 68 65 63 6b 52 65 ck,. os2CheckRe
baba0 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 6f 73 servedLock,. os
babb0 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 2FileControl,.
babc0 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 os2SectorSize,.
babd0 20 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63 os2DeviceCharac
babe0 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a teristics.};../*
babf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 **********.** He
bac40 72 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 re ends the I/O
bac50 6d 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 methods that for
bac60 6d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f m the sqlite3_io
bac70 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
bac80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 .**.** The next
bac90 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d block of code im
baca0 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 plements the VFS
bacb0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a methods..******
bacc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bace0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bacf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bad00 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 ******/../*.** C
bad10 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 reate a temporar
bad20 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a y file name in z
bad30 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 Buf. zBuf must
bad40 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f be big enough to
bad50 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73 .** hold at pVfs
bad60 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61 ->mxPathname cha
bad70 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 racters..*/.stat
bad80 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 ic int getTempna
bad90 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 me(int nBuf, cha
bada0 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20 73 74 61 r *zBuf ){. sta
badb0 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
badc0 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d ed char zChars[]
badd0 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 =. "abcdefgh
bade0 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 ijklmnopqrstuvwx
badf0 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 yz". "ABCDEFG
bae00 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 HIJKLMNOPQRSTUVW
bae10 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 XYZ". "012345
bae20 36 37 38 39 22 3b 0a 20 20 69 6e 74 20 69 2c 20 6789";. int i,
bae30 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 j;. char zTempP
bae40 61 74 68 42 75 66 5b 33 5d 3b 0a 20 20 50 53 5a athBuf[3];. PSZ
bae50 20 7a 54 65 6d 70 50 61 74 68 20 3d 20 28 50 53 zTempPath = (PS
bae60 5a 29 26 7a 54 65 6d 70 50 61 74 68 42 75 66 3b Z)&zTempPathBuf;
bae70 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 . if( sqlite3_t
bae80 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b emp_directory ){
bae90 0a 20 20 20 20 7a 54 65 6d 70 50 61 74 68 20 3d . zTempPath =
baea0 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 sqlite3_temp_di
baeb0 72 65 63 74 6f 72 79 3b 0a 20 20 7d 65 6c 73 65 rectory;. }else
baec0 7b 0a 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 {. if( DosSca
baed0 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 45 4d 50 nEnv( (PSZ)"TEMP
baee0 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 ", &zTempPath )
baef0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 44 6f 73 ){. if( Dos
baf00 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 ScanEnv( (PSZ)"T
baf10 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 MP", &zTempPath
baf20 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ) ){. if(
baf30 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 DosScanEnv( (PS
baf40 5a 29 22 54 4d 50 44 49 52 22 2c 20 26 7a 54 65 Z)"TMPDIR", &zTe
baf50 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 mpPath ) ){.
baf60 20 20 20 20 20 20 20 55 4c 4f 4e 47 20 75 6c 44 ULONG ulD
baf70 72 69 76 65 4e 75 6d 20 3d 20 30 2c 20 75 6c 44 riveNum = 0, ulD
baf80 72 69 76 65 4d 61 70 20 3d 20 30 3b 0a 20 20 20 riveMap = 0;.
baf90 20 20 20 20 20 20 20 20 44 6f 73 51 75 65 72 79 DosQuery
bafa0 43 75 72 72 65 6e 74 44 69 73 6b 28 20 26 75 6c CurrentDisk( &ul
bafb0 44 72 69 76 65 4e 75 6d 2c 20 26 75 6c 44 72 69 DriveNum, &ulDri
bafc0 76 65 4d 61 70 20 29 3b 0a 20 20 20 20 20 20 20 veMap );.
bafd0 20 20 20 20 73 70 72 69 6e 74 66 28 20 28 63 68 sprintf( (ch
bafe0 61 72 2a 29 7a 54 65 6d 70 50 61 74 68 2c 20 22 ar*)zTempPath, "
baff0 25 63 3a 22 2c 20 28 63 68 61 72 29 28 20 27 41 %c:", (char)( 'A
bb000 27 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d 20 2d ' + ulDriveNum -
bb010 20 31 20 29 20 29 3b 0a 20 20 20 20 20 20 20 20 1 ) );.
bb020 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
bb030 20 20 7d 0a 20 20 2f 2a 20 53 74 72 69 70 20 6f }. /* Strip o
bb040 66 66 20 61 20 74 72 61 69 6c 69 6e 67 20 73 6c ff a trailing sl
bb050 61 73 68 65 73 20 6f 72 20 62 61 63 6b 73 6c 61 ashes or backsla
bb060 73 68 65 73 2c 20 6f 74 68 65 72 77 69 73 65 20 shes, otherwise
bb070 77 65 20 77 6f 75 6c 64 20 67 65 74 20 2a 0a 20 we would get *.
bb080 20 20 2a 20 6d 75 6c 74 69 70 6c 65 20 28 62 61 * multiple (ba
bb090 63 6b 29 73 6c 61 73 68 65 73 20 77 68 69 63 68 ck)slashes which
bb0a0 20 63 61 75 73 65 73 20 44 6f 73 4f 70 65 6e 28 causes DosOpen(
bb0b0 29 20 74 6f 20 66 61 69 6c 2e 20 20 20 20 20 20 ) to fail.
bb0c0 20 20 20 20 20 20 20 20 2a 0a 20 20 20 2a 20 54 *. * T
bb0d0 72 61 69 6c 69 6e 67 20 73 70 61 63 65 73 20 61 railing spaces a
bb0e0 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 2c 20 re not allowed,
bb0f0 65 69 74 68 65 72 2e 20 20 20 20 20 20 20 20 20 either.
bb100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb110 20 20 20 2a 2f 0a 20 20 6a 20 3d 20 73 71 6c 69 */. j = sqli
bb120 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d te3Strlen30(zTem
bb130 70 50 61 74 68 29 3b 0a 20 20 77 68 69 6c 65 28 pPath);. while(
bb140 20 6a 20 3e 20 30 20 26 26 20 28 20 7a 54 65 6d j > 0 && ( zTem
bb150 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c pPath[j-1] == '\
bb160 5c 27 20 7c 7c 20 7a 54 65 6d 70 50 61 74 68 5b \' || zTempPath[
bb170 6a 2d 31 5d 20 3d 3d 20 27 2f 27 0a 20 20 20 20 j-1] == '/'.
bb180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb190 7c 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 || zTempPath[j-1
bb1a0 5d 20 3d 3d 20 27 20 27 20 29 20 29 7b 0a 20 20 ] == ' ' ) ){.
bb1b0 20 20 6a 2d 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65 j--;. }. zTe
bb1c0 6d 70 50 61 74 68 5b 6a 5d 20 3d 20 27 5c 30 27 mpPath[j] = '\0'
bb1d0 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 ;. if( !sqlite3
bb1e0 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 _temp_directory
bb1f0 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 ){. char *zTe
bb200 6d 70 50 61 74 68 55 54 46 20 3d 20 63 6f 6e 76 mpPathUTF = conv
bb210 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 ertCpPathToUtf8(
bb220 20 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 zTempPath );.
bb230 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
bb240 74 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a 42 75 tf( nBuf-30, zBu
bb250 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 f,.
bb260 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53 "%s\\"S
bb270 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f QLITE_TEMP_FILE_
bb280 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 PREFIX, zTempPat
bb290 68 55 54 46 20 29 3b 0a 20 20 20 20 66 72 65 65 hUTF );. free
bb2a0 28 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 ( zTempPathUTF )
bb2b0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
bb2c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
bb2d0 20 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a nBuf-30, zBuf,.
bb2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb2f0 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 "%s\\"SQLI
bb300 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
bb310 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 20 29 FIX, zTempPath )
bb320 3b 0a 20 20 7d 0a 20 20 6a 20 3d 20 73 71 6c 69 ;. }. j = sqli
bb330 74 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 42 75 te3Strlen30( zBu
bb340 66 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 f );. sqlite3_r
bb350 61 6e 64 6f 6d 6e 65 73 73 28 20 32 30 2c 20 26 andomness( 20, &
bb360 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 66 6f 72 zBuf[j] );. for
bb370 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 32 30 3b ( i = 0; i < 20;
bb380 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a 20 20 20 i++, j++ ){.
bb390 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 zBuf[j] = (char
bb3a0 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 )zChars[ ((unsig
bb3b0 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d ned char)zBuf[j]
bb3c0 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 )%(sizeof(zChars
bb3d0 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 )-1) ];. }. zB
bb3e0 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 uf[j] = 0;. OST
bb3f0 52 41 43 45 32 28 20 22 54 45 4d 50 20 46 49 4c RACE2( "TEMP FIL
bb400 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 ENAME: %s\n", zB
bb410 75 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 uf );. return S
bb420 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a QLITE_OK;.}.../*
bb430 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 .** Turn a relat
bb440 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 ive pathname int
bb450 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d o a full pathnam
bb460 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75 e. Write the fu
bb470 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69 ll.** pathname i
bb480 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20 20 7a 46 nto zFull[]. zF
bb490 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 ull[] will be at
bb4a0 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 least pVfs->mxP
bb4b0 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 athname.** bytes
bb4c0 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 in size..*/.sta
bb4d0 74 69 63 20 69 6e 74 20 6f 73 32 46 75 6c 6c 50 tic int os2FullP
bb4e0 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 athname(. sqlit
bb4f0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
bb500 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
bb510 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 r to vfs object
bb520 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
bb530 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20 *zRelative,
bb540 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c /* Possibly rel
bb550 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 ative input path
bb560 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c */. int nFull,
bb570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb580 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 /* Size of out
bb590 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 put buffer in by
bb5a0 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a tes */. char *z
bb5b0 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 Full
bb5c0 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
bb5d0 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 63 68 uffer */.){. ch
bb5e0 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 43 70 20 ar *zRelativeCp
bb5f0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 = convertUtf8Pat
bb600 68 54 6f 43 70 28 20 7a 52 65 6c 61 74 69 76 65 hToCp( zRelative
bb610 20 29 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c 6c );. char zFull
bb620 43 70 5b 43 43 48 4d 41 58 50 41 54 48 5d 20 3d Cp[CCHMAXPATH] =
bb630 20 22 5c 30 22 3b 0a 20 20 63 68 61 72 20 2a 7a "\0";. char *z
bb640 46 75 6c 6c 55 54 46 3b 0a 20 20 41 50 49 52 45 FullUTF;. APIRE
bb650 54 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 T rc = DosQueryP
bb660 61 74 68 49 6e 66 6f 28 20 7a 52 65 6c 61 74 69 athInfo( zRelati
bb670 76 65 43 70 2c 20 46 49 4c 5f 51 55 45 52 59 46 veCp, FIL_QUERYF
bb680 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c 6c 43 70 ULLNAME, zFullCp
bb690 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
bb6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bb6b0 20 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a CCHMAXPATH );.
bb6c0 20 20 66 72 65 65 28 20 7a 52 65 6c 61 74 69 76 free( zRelativ
bb6d0 65 43 70 20 29 3b 0a 20 20 7a 46 75 6c 6c 55 54 eCp );. zFullUT
bb6e0 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 F = convertCpPat
bb6f0 68 54 6f 55 74 66 38 28 20 7a 46 75 6c 6c 43 70 hToUtf8( zFullCp
bb700 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e );. sqlite3_sn
bb710 70 72 69 6e 74 66 28 20 6e 46 75 6c 6c 2c 20 7a printf( nFull, z
bb720 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 46 20 29 Full, zFullUTF )
bb730 3b 0a 20 20 66 72 65 65 28 20 7a 46 75 6c 6c 55 ;. free( zFullU
bb740 54 46 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 TF );. return r
bb750 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 c == NO_ERROR ?
bb760 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
bb770 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f 2a TE_IOERR;.}.../*
bb780 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e .** Open a file.
bb790 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
bb7a0 73 32 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 s2Open(. sqlite
bb7b0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
bb7c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 /* Not u
bb7d0 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 sed */. const c
bb7e0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 har *zName,
bb7f0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
bb800 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 f the file */.
bb810 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
bb820 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
bb830 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 74 Write the SQLit
bb840 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 e file handle he
bb850 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 re */. int flag
bb860 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s,
bb870 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f /* Open mo
bb880 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e de flags */. in
bb890 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 t *pOutFlags
bb8a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
bb8b0 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 tatus return fla
bb8c0 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 46 49 4c 45 gs */.){. HFILE
bb8d0 20 68 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 46 69 h;. ULONG ulFi
bb8e0 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 49 leAttribute = FI
bb8f0 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 55 4c 4f LE_NORMAL;. ULO
bb900 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 3d NG ulOpenFlags =
bb910 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 0;. ULONG ulOp
bb920 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 6f 73 enMode = 0;. os
bb930 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
bb940 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41 os2File*)id;. A
bb950 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 PIRET rc = NO_ER
bb960 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 41 ROR;. ULONG ulA
bb970 63 74 69 6f 6e 3b 0a 20 20 63 68 61 72 20 2a 7a ction;. char *z
bb980 4e 61 6d 65 43 70 3b 0a 20 20 63 68 61 72 20 7a NameCp;. char z
bb990 54 6d 70 6e 61 6d 65 5b 43 43 48 4d 41 58 50 41 Tmpname[CCHMAXPA
bb9a0 54 48 2b 31 5d 3b 20 20 20 20 2f 2a 20 42 75 66 TH+1]; /* Buf
bb9b0 66 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 fer to hold name
bb9c0 20 6f 66 20 74 65 6d 70 20 66 69 6c 65 20 2a 2f of temp file */
bb9d0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 .. /* If the se
bb9e0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
bb9f0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
bba00 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 s NULL, generate
bba10 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 a . ** tempora
bba20 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 ry file name to
bba30 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 use . */. if(
bba40 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e !zName ){. in
bba50 74 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 t rc = getTempna
bba60 6d 65 28 43 43 48 4d 41 58 50 41 54 48 2b 31 2c me(CCHMAXPATH+1,
bba70 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 zTmpname);.
bba80 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
bba90 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
bbaa0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
bbab0 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 zName = zTmpname
bbac0 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d 73 65 74 ;. }... memset
bbad0 28 20 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 ( pFile, 0, size
bbae0 6f 66 28 2a 70 46 69 6c 65 29 20 29 3b 0a 0a 20 of(*pFile) );..
bbaf0 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e OSTRACE2( "OPEN
bbb00 20 77 61 6e 74 20 25 64 5c 6e 22 2c 20 66 6c 61 want %d\n", fla
bbb10 67 73 20 29 3b 0a 0a 20 20 69 66 28 20 66 6c 61 gs );.. if( fla
bbb20 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
bbb30 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 _READWRITE ){.
bbb40 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 ulOpenMode |=
bbb50 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 OPEN_ACCESS_READ
bbb60 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 WRITE;. OSTRA
bbb70 43 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 2f CE1( "OPEN read/
bbb80 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 write\n" );. }e
bbb90 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d lse{. ulOpenM
bbba0 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 ode |= OPEN_ACCE
bbbb0 53 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 SS_READONLY;.
bbbc0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
bbbd0 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b read only\n" );
bbbe0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 . }.. if( flag
bbbf0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
bbc00 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 75 6c CREATE ){. ul
bbc10 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 OpenFlags |= OPE
bbc20 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 N_ACTION_OPEN_IF
bbc30 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 _EXISTS | OPEN_A
bbc40 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46 5f CTION_CREATE_IF_
bbc50 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 NEW;. OSTRACE
bbc60 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 6e 65 1( "OPEN open ne
bbc70 77 2f 63 72 65 61 74 65 5c 6e 22 20 29 3b 0a 20 w/create\n" );.
bbc80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 }else{. ulOp
bbc90 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e 5f enFlags |= OPEN_
bbca0 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 ACTION_OPEN_IF_E
bbcb0 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 54 XISTS | OPEN_ACT
bbcc0 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e 45 57 3b ION_FAIL_IF_NEW;
bbcd0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
bbce0 4f 50 45 4e 20 6f 70 65 6e 20 65 78 69 73 74 69 OPEN open existi
bbcf0 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 ng\n" );. }..
bbd00 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
bbd10 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 TE_OPEN_MAIN_DB
bbd20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 ){. ulOpenMod
bbd30 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f e |= OPEN_SHARE_
bbd40 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 20 4f 53 DENYNONE;. OS
bbd50 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68 TRACE1( "OPEN sh
bbd60 61 72 65 20 72 65 61 64 2f 77 72 69 74 65 5c 6e are read/write\n
bbd70 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 " );. }else{.
bbd80 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 ulOpenMode |=
bbd90 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 57 OPEN_SHARE_DENYW
bbda0 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 RITE;. OSTRAC
bbdb0 45 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65 20 E1( "OPEN share
bbdc0 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a read only\n" );.
bbdd0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 }.. if( flags
bbde0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 & SQLITE_OPEN_D
bbdf0 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a ELETEONCLOSE ){.
bbe00 20 20 20 20 63 68 61 72 20 70 61 74 68 55 74 66 char pathUtf
bbe10 38 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 23 8[CCHMAXPATH];.#
bbe20 69 66 64 65 66 20 4e 44 45 42 55 47 20 2f 2a 20 ifdef NDEBUG /*
bbe30 77 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 77 when debugging w
bbe40 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 73 e want to make s
bbe50 75 72 65 20 69 74 20 69 73 20 64 65 6c 65 74 65 ure it is delete
bbe60 64 20 2a 2f 0a 20 20 20 20 75 6c 46 69 6c 65 41 d */. ulFileA
bbe70 74 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45 5f ttribute = FILE_
bbe80 48 49 44 44 45 4e 3b 0a 23 65 6e 64 69 66 0a 20 HIDDEN;.#endif.
bbe90 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 os2FullPathna
bbea0 6d 65 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c me( pVfs, zName,
bbeb0 20 43 43 48 4d 41 58 50 41 54 48 2c 20 70 61 74 CCHMAXPATH, pat
bbec0 68 55 74 66 38 20 29 3b 0a 20 20 20 20 70 46 69 hUtf8 );. pFi
bbed0 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 le->pathToDel =
bbee0 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 convertUtf8PathT
bbef0 6f 43 70 28 20 70 61 74 68 55 74 66 38 20 29 3b oCp( pathUtf8 );
bbf00 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
bbf10 4f 50 45 4e 20 68 69 64 64 65 6e 2f 64 65 6c 65 OPEN hidden/dele
bbf20 74 65 20 6f 6e 20 63 6c 6f 73 65 20 66 69 6c 65 te on close file
bbf30 20 61 74 74 72 69 62 75 74 65 73 5c 6e 22 20 29 attributes\n" )
bbf40 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
bbf50 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 File->pathToDel
bbf60 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f 53 54 52 = NULL;. OSTR
bbf70 41 43 45 31 28 20 22 4f 50 45 4e 20 6e 6f 72 6d ACE1( "OPEN norm
bbf80 61 6c 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 al file attribut
bbf90 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f e\n" );. }.. /
bbfa0 2a 20 61 6c 77 61 79 73 20 6f 70 65 6e 20 69 6e * always open in
bbfb0 20 72 61 6e 64 6f 6d 20 61 63 63 65 73 73 20 6d random access m
bbfc0 6f 64 65 20 66 6f 72 20 70 6f 73 73 69 62 6c 79 ode for possibly
bbfd0 20 62 65 74 74 65 72 20 73 70 65 65 64 20 2a 2f better speed */
bbfe0 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d . ulOpenMode |=
bbff0 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e 44 OPEN_FLAGS_RAND
bc000 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 OM;. ulOpenMode
bc010 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 46 |= OPEN_FLAGS_F
bc020 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b 0a 20 20 AIL_ON_ERROR;.
bc030 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 ulOpenMode |= OP
bc040 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e 48 45 52 EN_FLAGS_NOINHER
bc050 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43 70 20 3d IT;.. zNameCp =
bc060 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 convertUtf8Path
bc070 54 6f 43 70 28 20 7a 4e 61 6d 65 20 29 3b 0a 20 ToCp( zName );.
bc080 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20 28 rc = DosOpen( (
bc090 50 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a 20 20 20 PSZ)zNameCp,.
bc0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 26 68 2c &h,
bc0b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
bc0c0 20 26 75 6c 41 63 74 69 6f 6e 2c 0a 20 20 20 20 &ulAction,.
bc0d0 20 20 20 20 20 20 20 20 20 20 20 20 30 4c 2c 0a 0L,.
bc0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc0f0 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c ulFileAttribute,
bc100 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
bc110 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 ulOpenFlags,.
bc120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c ul
bc130 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20 20 20 20 20 OpenMode,.
bc140 20 20 20 20 20 20 20 20 20 20 28 50 45 41 4f 50 (PEAOP
bc150 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 66 72 65 65 2)NULL );. free
bc160 28 20 7a 4e 61 6d 65 43 70 20 29 3b 0a 20 20 69 ( zNameCp );. i
bc170 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f f( rc != NO_ERRO
bc180 52 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 R ){. OSTRACE
bc190 37 28 20 22 4f 50 45 4e 20 49 6e 76 61 6c 69 64 7( "OPEN Invalid
bc1a0 20 68 61 6e 64 6c 65 20 72 63 3d 25 64 3a 20 7a handle rc=%d: z
bc1b0 4e 61 6d 65 3d 25 73 2c 20 75 6c 41 63 74 69 6f Name=%s, ulActio
bc1c0 6e 3d 25 23 6c 78 2c 20 75 6c 41 74 74 72 3d 25 n=%#lx, ulAttr=%
bc1d0 23 6c 78 2c 20 75 6c 46 6c 61 67 73 3d 25 23 6c #lx, ulFlags=%#l
bc1e0 78 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c 78 5c 6e x, ulMode=%#lx\n
bc1f0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
bc200 20 72 63 2c 20 7a 4e 61 6d 65 2c 20 75 6c 41 63 rc, zName, ulAc
bc210 74 69 6f 6e 2c 20 75 6c 46 69 6c 65 41 74 74 72 tion, ulFileAttr
bc220 69 62 75 74 65 2c 20 75 6c 4f 70 65 6e 46 6c 61 ibute, ulOpenFla
bc230 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 29 gs, ulOpenMode )
bc240 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d ;. if( pFile-
bc250 3e 70 61 74 68 54 6f 44 65 6c 20 29 0a 20 20 20 >pathToDel ).
bc260 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d 3e free( pFile->
bc270 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20 pathToDel );.
bc280 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
bc290 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 l = NULL;. if
bc2a0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
bc2b0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
bc2c0 29 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 ){. OSTRACE
bc2d0 32 28 20 22 4f 50 45 4e 20 25 64 20 49 6e 76 61 2( "OPEN %d Inva
bc2e0 6c 69 64 20 68 61 6e 64 6c 65 5c 6e 22 2c 20 28 lid handle\n", (
bc2f0 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f (flags | SQLITE_
bc300 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 OPEN_READONLY) &
bc310 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 ~SQLITE_OPEN_RE
bc320 41 44 57 52 49 54 45 29 20 29 3b 0a 20 20 20 20 ADWRITE) );.
bc330 20 20 72 65 74 75 72 6e 20 6f 73 32 4f 70 65 6e return os2Open
bc340 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 ( pVfs, zName, i
bc350 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 d,.
bc360 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 ((flags
bc370 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 | SQLITE_OPEN_R
bc380 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 EADONLY) & ~SQLI
bc390 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
bc3a0 45 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 E),.
bc3b0 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 46 6c pOutFl
bc3c0 61 67 73 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ags );. }else
bc3d0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
bc3e0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
bc3f0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
bc400 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 pOutFlags ){.
bc410 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 *pOutFlags = f
bc420 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
bc430 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3f 20 53 EN_READWRITE ? S
bc440 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
bc450 52 49 54 45 20 3a 20 53 51 4c 49 54 45 5f 4f 50 RITE : SQLITE_OP
bc460 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d EN_READONLY;. }
bc470 0a 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 .. pFile->pMeth
bc480 6f 64 20 3d 20 26 6f 73 32 49 6f 4d 65 74 68 6f od = &os2IoMetho
bc490 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 d;. pFile->h =
bc4a0 68 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 h;. OpenCounter
bc4b0 28 2b 31 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 (+1);. OSTRACE3
bc4c0 28 20 22 4f 50 45 4e 20 25 64 20 70 4f 75 74 46 ( "OPEN %d pOutF
bc4d0 6c 61 67 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c lags=%d\n", pFil
bc4e0 65 2d 3e 68 2c 20 70 4f 75 74 46 6c 61 67 73 20 e->h, pOutFlags
bc4f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
bc500 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
bc510 44 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 64 Delete the named
bc520 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
bc530 20 69 6e 74 20 6f 73 32 44 65 6c 65 74 65 28 0a int os2Delete(.
bc540 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
bc550 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 Vfs,
bc560 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
bc570 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 used on os2 */.
bc580 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
bc590 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 lename,
bc5a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
bc5b0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 of file to delet
bc5c0 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 e */. int syncD
bc5d0 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ir
bc5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bc5f0 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 Not used on os2
bc600 20 2a 2f 0a 29 7b 0a 20 20 41 50 49 52 45 54 20 */.){. APIRET
bc610 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 rc = NO_ERROR;.
bc620 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
bc630 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 Cp = convertUtf8
bc640 50 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e PathToCp( zFilen
bc650 61 6d 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 ame );. Simulat
bc660 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
bc670 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 SQLITE_IOERR_DE
bc680 4c 45 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 LETE );. rc = D
bc690 6f 73 44 65 6c 65 74 65 28 20 28 50 53 5a 29 7a osDelete( (PSZ)z
bc6a0 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 FilenameCp );.
bc6b0 66 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 free( zFilenameC
bc6c0 70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 p );. OSTRACE2(
bc6d0 20 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c "DELETE \"%s\"\
bc6e0 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b n", zFilename );
bc6f0 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 . return rc ==
bc700 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 NO_ERROR ? SQLIT
bc710 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f E_OK : SQLITE_IO
bc720 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f ERR_DELETE;.}../
bc730 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 *.** Check the e
bc740 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 xistance and sta
bc750 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a tus of a file..*
bc760 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
bc770 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 Access(. sqlite
bc780 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
bc790 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 /* Not used
bc7a0 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 on os2 */. cons
bc7b0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
bc7c0 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 e, /* Name of
bc7d0 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a file to check *
bc7e0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
bc7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bc800 20 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 6f Type of test to
bc810 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 make on this fi
bc820 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 le */. int *pOu
bc830 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
bc840 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c /* Write resul
bc850 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ts here */.){.
bc860 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 FILESTATUS3 fsts
bc870 33 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a 20 20 41 3ConfigInfo;. A
bc880 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 PIRET rc = NO_ER
bc890 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 ROR;. char *zFi
bc8a0 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 lenameCp = conve
bc8b0 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 rtUtf8PathToCp(
bc8c0 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 0a 20 20 zFilename );..
bc8d0 6d 65 6d 73 65 74 28 20 26 66 73 74 73 33 43 6f memset( &fsts3Co
bc8e0 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a nfigInfo, 0, siz
bc8f0 65 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67 49 eof(fsts3ConfigI
bc900 6e 66 6f 29 20 29 3b 0a 20 20 72 63 20 3d 20 44 nfo) );. rc = D
bc910 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 osQueryPathInfo(
bc920 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 (PSZ)zFilenameC
bc930 70 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c p, FIL_STANDARD,
bc940 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
bc950 20 20 20 20 20 20 20 20 20 20 26 66 73 74 73 33 &fsts3
bc960 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 65 ConfigInfo, size
bc970 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 of(FILESTATUS3)
bc980 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 );. free( zFile
bc990 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 nameCp );. OSTR
bc9a0 41 43 45 34 28 20 22 41 43 43 45 53 53 20 66 73 ACE4( "ACCESS fs
bc9b0 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 ts3ConfigInfo.at
bc9c0 74 72 46 69 6c 65 3d 25 64 20 66 6c 61 67 73 3d trFile=%d flags=
bc9d0 25 64 20 72 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 %d rc=%d\n",.
bc9e0 20 20 20 20 20 20 20 20 20 66 73 74 73 33 43 6f fsts3Co
bc9f0 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c nfigInfo.attrFil
bca00 65 2c 20 66 6c 61 67 73 2c 20 72 63 20 29 3b 0a e, flags, rc );.
bca10 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 switch( flags
bca20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
bca30 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a TE_ACCESS_READ:.
bca40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
bca50 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 ACCESS_EXISTS:.
bca60 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d rc = (rc ==
bca70 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 NO_ERROR);.
bca80 20 20 4f 53 54 52 41 43 45 33 28 20 22 41 43 43 OSTRACE3( "ACC
bca90 45 53 53 20 25 73 20 61 63 63 65 73 73 20 6f 66 ESS %s access of
bcaa0 20 72 65 61 64 20 61 6e 64 20 65 78 69 73 74 73 read and exists
bcab0 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c rc=%d\n", zFil
bcac0 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 ename, rc );.
bcad0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
bcae0 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
bcaf0 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 _READWRITE:.
bcb00 20 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e 4f rc = (rc == NO
bcb10 5f 45 52 52 4f 52 29 20 26 26 20 28 20 28 66 73 _ERROR) && ( (fs
bcb20 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 ts3ConfigInfo.at
bcb30 74 72 46 69 6c 65 20 26 20 46 49 4c 45 5f 52 45 trFile & FILE_RE
bcb40 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 20 29 3b 0a ADONLY) == 0 );.
bcb50 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 OSTRACE3(
bcb60 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73 "ACCESS %s acces
bcb70 73 20 6f 66 20 72 65 61 64 2f 77 72 69 74 65 20 s of read/write
bcb80 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 rc=%d\n", zFile
bcb90 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 20 name, rc );.
bcba0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 break;. def
bcbb0 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 ault:. asse
bcbc0 72 74 28 20 21 22 49 6e 76 61 6c 69 64 20 66 6c rt( !"Invalid fl
bcbd0 61 67 73 20 61 72 67 75 6d 65 6e 74 22 20 29 3b ags argument" );
bcbe0 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20 72 . }. *pOut = r
bcbf0 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 c;. return SQLI
bcc00 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 TE_OK;.}...#ifnd
bcc10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
bcc20 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a OAD_EXTENSION./*
bcc30 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 .** Interfaces f
bcc40 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 or opening a sha
bcc50 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e red library, fin
bcc60 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 ding entry point
bcc70 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 s.** within the
bcc80 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
bcc90 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 and closing the
bcca0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a shared library..
bccb0 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 */./*.** Interfa
bccc0 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 ces for opening
bccd0 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 a shared library
bcce0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 , finding entry
bccf0 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e points.** within
bcd00 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
bcd10 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ary, and closing
bcd20 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
bcd30 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ary..*/.static v
bcd40 6f 69 64 20 2a 6f 73 32 44 6c 4f 70 65 6e 28 73 oid *os2DlOpen(s
bcd50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
bcd60 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
bcd70 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 43 48 41 ilename){. UCHA
bcd80 52 20 6c 6f 61 64 45 72 72 5b 32 35 36 5d 3b 0a R loadErr[256];.
bcd90 20 20 48 4d 4f 44 55 4c 45 20 68 6d 6f 64 3b 0a HMODULE hmod;.
bcda0 20 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 63 APIRET rc;. c
bcdb0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 har *zFilenameCp
bcdc0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 = convertUtf8Pa
bcdd0 74 68 54 6f 43 70 28 7a 46 69 6c 65 6e 61 6d 65 thToCp(zFilename
bcde0 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4c 6f 61 );. rc = DosLoa
bcdf0 64 4d 6f 64 75 6c 65 28 28 50 53 5a 29 6c 6f 61 dModule((PSZ)loa
bce00 64 45 72 72 2c 20 73 69 7a 65 6f 66 28 6c 6f 61 dErr, sizeof(loa
bce10 64 45 72 72 29 2c 20 7a 46 69 6c 65 6e 61 6d 65 dErr), zFilename
bce20 43 70 2c 20 26 68 6d 6f 64 29 3b 0a 20 20 66 72 Cp, &hmod);. fr
bce30 65 65 28 7a 46 69 6c 65 6e 61 6d 65 43 70 29 3b ee(zFilenameCp);
bce40 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20 . return rc !=
bce50 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 NO_ERROR ? 0 : (
bce60 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f 2a void*)hmod;.}./*
bce70 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73 69 6e 63 .** A no-op sinc
bce80 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 e the error code
bce90 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
bcea0 74 68 65 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c the DosLoadModul
bceb0 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73 32 44 6c e call..** os2Dl
bcec0 6f 70 65 6e 20 72 65 74 75 72 6e 73 20 7a 65 72 open returns zer
bced0 6f 20 69 66 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 o if DosLoadModu
bcee0 6c 65 20 69 73 20 6e 6f 74 20 73 75 63 63 65 73 le is not succes
bcef0 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 sful..*/.static
bcf00 76 6f 69 64 20 6f 73 32 44 6c 45 72 72 6f 72 28 void os2DlError(
bcf10 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
bcf20 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 s, int nBuf, cha
bcf30 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 2f 2a 20 r *zBufOut){./*
bcf40 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74 61 74 69 no-op */.}.stati
bcf50 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 53 79 6d c void *os2DlSym
bcf60 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
bcf70 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c fs, void *pHandl
bcf80 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
bcf90 53 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 4e 20 70 Symbol){. PFN p
bcfa0 66 6e 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b fn;. APIRET rc;
bcfb0 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 . rc = DosQuery
bcfc0 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c ProcAddr((HMODUL
bcfd0 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 7a E)pHandle, 0L, z
bcfe0 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 Symbol, &pfn);.
bcff0 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 if( rc != NO_ER
bd000 52 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 69 66 ROR ){. /* if
bd010 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 74 73 65 the symbol itse
bd020 6c 66 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e 64 lf was not found
bd030 2c 20 73 65 61 72 63 68 20 61 67 61 69 6e 20 66 , search again f
bd040 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 or the same.
bd050 20 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20 61 * symbol with a
bd060 6e 20 65 78 74 72 61 20 75 6e 64 65 72 73 63 6f n extra undersco
bd070 72 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 re, that might b
bd080 65 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64 69 e needed dependi
bd090 6e 67 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68 65 ng. * on the
bd0a0 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 calling convent
bd0b0 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 ion */. char
bd0c0 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d 20 _zSymbol[256] =
bd0d0 22 5f 22 3b 0a 20 20 20 20 73 74 72 6e 63 61 74 "_";. strncat
bd0e0 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79 6d 62 (_zSymbol, zSymb
bd0f0 6f 6c 2c 20 32 35 35 29 3b 0a 20 20 20 20 72 63 ol, 255);. rc
bd100 20 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 = DosQueryProcA
bd110 64 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 ddr((HMODULE)pHa
bd120 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 79 6d 62 ndle, 0L, _zSymb
bd130 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 7d 0a 20 ol, &pfn);. }.
bd140 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e 4f return rc != NO
bd150 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 6f _ERROR ? 0 : (vo
bd160 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74 61 74 69 id*)pfn;.}.stati
bd170 63 20 76 6f 69 64 20 6f 73 32 44 6c 43 6c 6f 73 c void os2DlClos
bd180 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 e(sqlite3_vfs *p
bd190 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 Vfs, void *pHand
bd1a0 6c 65 29 7b 0a 20 20 44 6f 73 46 72 65 65 4d 6f le){. DosFreeMo
bd1b0 64 75 6c 65 28 28 48 4d 4f 44 55 4c 45 29 70 48 dule((HMODULE)pH
bd1c0 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 andle);.}.#else
bd1d0 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 /* if SQLITE_OMI
bd1e0 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
bd1f0 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a is defined: */.
bd200 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 4f #define os2DlO
bd210 70 65 6e 20 30 0a 20 20 23 64 65 66 69 6e 65 20 pen 0. #define
bd220 6f 73 32 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 os2DlError 0. #
bd230 64 65 66 69 6e 65 20 6f 73 32 44 6c 53 79 6d 20 define os2DlSym
bd240 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 0. #define os2D
bd250 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a lClose 0.#endif.
bd260 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 ../*.** Write up
bd270 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f to nBuf bytes o
bd280 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 f randomness int
bd290 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 o zBuf..*/.stati
bd2a0 63 20 69 6e 74 20 6f 73 32 52 61 6e 64 6f 6d 6e c int os2Randomn
bd2b0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ess(sqlite3_vfs
bd2c0 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c *pVfs, int nBuf,
bd2d0 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 char *zBuf ){.
bd2e0 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 23 69 66 20 int n = 0;.#if
bd2f0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
bd300 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66 3b EST). n = nBuf;
bd310 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 . memset(zBuf,
bd320 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0a 0, nBuf);.#else.
bd330 20 20 69 6e 74 20 73 69 7a 65 6f 66 55 4c 6f 6e int sizeofULon
bd340 67 20 3d 20 73 69 7a 65 6f 66 28 55 4c 4f 4e 47 g = sizeof(ULONG
bd350 29 3b 0a 20 20 69 66 28 20 28 69 6e 74 29 73 69 );. if( (int)si
bd360 7a 65 6f 66 28 44 41 54 45 54 49 4d 45 29 20 3c zeof(DATETIME) <
bd370 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 = nBuf - n ){.
bd380 20 20 44 41 54 45 54 49 4d 45 20 78 3b 0a 20 20 DATETIME x;.
bd390 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65 DosGetDateTime
bd3a0 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 (&x);. memcpy
bd3b0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 (&zBuf[n], &x, s
bd3c0 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e izeof(x));. n
bd3d0 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 += sizeof(x);.
bd3e0 20 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 }.. if( sizeof
bd3f0 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 ULong <= nBuf -
bd400 6e 20 29 7b 0a 20 20 20 20 50 50 49 42 20 70 70 n ){. PPIB pp
bd410 69 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e ib;. DosGetIn
bd420 66 6f 42 6c 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 foBlocks(NULL, &
bd430 70 70 69 62 29 3b 0a 20 20 20 20 6d 65 6d 63 70 ppib);. memcp
bd440 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 70 69 y(&zBuf[n], &ppi
bd450 62 2d 3e 70 69 62 5f 75 6c 70 69 64 2c 20 73 69 b->pib_ulpid, si
bd460 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 zeofULong);.
bd470 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 n += sizeofULong
bd480 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 69 7a ;. }.. if( siz
bd490 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 eofULong <= nBuf
bd4a0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 50 54 49 42 - n ){. PTIB
bd4b0 20 70 74 69 62 3b 0a 20 20 20 20 44 6f 73 47 65 ptib;. DosGe
bd4c0 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 tInfoBlocks(&pti
bd4d0 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 6d 65 b, NULL);. me
bd4e0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
bd4f0 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d ptib->tib_ptib2-
bd500 3e 74 69 62 32 5f 75 6c 74 69 64 2c 20 73 69 7a >tib2_ultid, siz
bd510 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e eofULong);. n
bd520 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b += sizeofULong;
bd530 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65 . }.. /* if we
bd540 20 73 74 69 6c 6c 20 68 61 76 65 6e 27 74 20 66 still haven't f
bd550 69 6c 6c 65 64 20 74 68 65 20 62 75 66 66 65 72 illed the buffer
bd560 20 79 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 yet the followi
bd570 6e 67 20 77 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20 ng will */. /*
bd580 67 72 61 62 20 65 76 65 72 79 74 68 69 6e 67 20 grab everything
bd590 6f 6e 63 65 20 69 6e 73 74 65 61 64 20 6f 66 20 once instead of
bd5a0 6d 61 6b 69 6e 67 20 73 65 76 65 72 61 6c 20 63 making several c
bd5b0 61 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c alls for a singl
bd5c0 65 20 69 74 65 6d 20 2a 2f 0a 20 20 69 66 28 20 e item */. if(
bd5d0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e sizeofULong <= n
bd5e0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 55 Buf - n ){. U
bd5f0 4c 4f 4e 47 20 75 6c 53 79 73 49 6e 66 6f 5b 51 LONG ulSysInfo[Q
bd600 53 56 5f 4d 41 58 5d 3b 0a 20 20 20 20 44 6f 73 SV_MAX];. Dos
bd610 51 75 65 72 79 53 79 73 49 6e 66 6f 28 31 4c 2c QuerySysInfo(1L,
bd620 20 51 53 56 5f 4d 41 58 2c 20 75 6c 53 79 73 49 QSV_MAX, ulSysI
bd630 6e 66 6f 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 nfo, sizeofULong
bd640 20 2a 20 51 53 56 5f 4d 41 58 29 3b 0a 0a 20 20 * QSV_MAX);..
bd650 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
bd660 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 ], &ulSysInfo[QS
bd670 56 5f 4d 53 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c V_MS_COUNT - 1],
bd680 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 sizeofULong);.
bd690 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c n += sizeofUL
bd6a0 6f 6e 67 3b 0a 0a 20 20 20 20 69 66 28 20 73 69 ong;.. if( si
bd6b0 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 zeofULong <= nBu
bd6c0 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d f - n ){. m
bd6d0 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 emcpy(&zBuf[n],
bd6e0 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 &ulSysInfo[QSV_T
bd6f0 49 4d 45 52 5f 49 4e 54 45 52 56 41 4c 20 2d 20 IMER_INTERVAL -
bd700 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 1], sizeofULong)
bd710 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a ;. n += siz
bd720 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a eofULong;. }.
bd730 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c if( sizeofUL
bd740 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
bd750 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
bd760 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
bd770 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f Info[QSV_TIME_LO
bd780 57 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c W - 1], sizeofUL
bd790 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d ong);. n +=
bd7a0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 sizeofULong;.
bd7b0 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 }. if( size
bd7c0 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 ofULong <= nBuf
bd7d0 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d - n ){. mem
bd7e0 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 cpy(&zBuf[n], &u
bd7f0 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d lSysInfo[QSV_TIM
bd800 45 5f 48 49 47 48 20 2d 20 31 5d 2c 20 73 69 7a E_HIGH - 1], siz
bd810 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
bd820 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
bd830 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 g;. }. if(
bd840 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
bd850 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
bd860 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
bd870 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 ], &ulSysInfo[QS
bd880 56 5f 54 4f 54 41 56 41 49 4c 4d 45 4d 20 2d 20 V_TOTAVAILMEM -
bd890 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 1], sizeofULong)
bd8a0 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a ;. n += siz
bd8b0 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a eofULong;. }.
bd8c0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 }.#endif.. re
bd8d0 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn n;.}../*.**
bd8e0 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 Sleep for a lit
bd8f0 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 tle while. Retu
bd900 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 rn the amount of
bd910 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 time slept..**
bd920 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 The argument is
bd930 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
bd940 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 croseconds we wa
bd950 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 nt to sleep..**
bd960 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
bd970 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
bd980 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f f microseconds o
bd990 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 f sleep actually
bd9a0 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 .** requested fr
bd9b0 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e om the underlyin
bd9c0 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 g operating syst
bd9d0 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 em, a number whi
bd9e0 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 ch.** might be g
bd9f0 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
bda00 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 qual to the argu
bda10 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 ment, but not le
bda20 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 ss.** than the a
bda30 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 rgument..*/.stat
bda40 69 63 20 69 6e 74 20 6f 73 32 53 6c 65 65 70 28 ic int os2Sleep(
bda50 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
bda60 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 fs, int microsec
bda70 20 29 7b 0a 20 20 44 6f 73 53 6c 65 65 70 28 20 ){. DosSleep(
bda80 28 6d 69 63 72 6f 73 65 63 2f 31 30 30 30 29 20 (microsec/1000)
bda90 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 );. return micr
bdaa0 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 osec;.}../*.** T
bdab0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 he following var
bdac0 69 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f iable, if set to
bdad0 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 a non-zero valu
bdae0 65 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 e, becomes the r
bdaf0 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 esult.** returne
bdb00 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 d from sqlite3Os
bdb10 43 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 CurrentTime().
bdb20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 This is used for
bdb30 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 testing..*/.#if
bdb40 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
bdb50 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
bdb60 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
bdb70 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a ime = 0;.#endif.
bdb80 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 ./*.** Find the
bdb90 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e current time (in
bdba0 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 Universal Coord
bdbb0 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 inated Time). W
bdbc0 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 rite the.** curr
bdbd0 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 ent time and dat
bdbe0 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 e as a Julian Da
bdbf0 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 y number into *p
bdc00 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 rNow and.** retu
bdc10 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 rn 0. Return 1
bdc20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 if the time and
bdc30 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 date cannot be f
bdc40 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 ound..*/.int os2
bdc50 43 75 72 72 65 6e 74 54 69 6d 65 28 20 73 71 6c CurrentTime( sql
bdc60 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
bdc70 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 20 29 7b double *prNow ){
bdc80 0a 20 20 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a 20 . double now;.
bdc90 20 53 48 4f 52 54 20 6d 69 6e 75 74 65 3b 20 2f SHORT minute; /
bdca0 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 62 * needs to be ab
bdcb0 6c 65 20 74 6f 20 63 6f 70 65 20 77 69 74 68 20 le to cope with
bdcc0 6e 65 67 61 74 69 76 65 20 74 69 6d 65 7a 6f 6e negative timezon
bdcd0 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 55 53 e offset */. US
bdce0 48 4f 52 54 20 73 65 63 6f 6e 64 2c 20 68 6f 75 HORT second, hou
bdcf0 72 2c 0a 20 20 20 20 20 20 20 20 20 64 61 79 2c r,. day,
bdd00 20 6d 6f 6e 74 68 2c 20 79 65 61 72 3b 0a 20 20 month, year;.
bdd10 44 41 54 45 54 49 4d 45 20 64 74 3b 0a 20 20 44 DATETIME dt;. D
bdd20 6f 73 47 65 74 44 61 74 65 54 69 6d 65 28 20 26 osGetDateTime( &
bdd30 64 74 20 29 3b 0a 20 20 73 65 63 6f 6e 64 20 3d dt );. second =
bdd40 20 28 55 53 48 4f 52 54 29 64 74 2e 73 65 63 6f (USHORT)dt.seco
bdd50 6e 64 73 3b 0a 20 20 6d 69 6e 75 74 65 20 3d 20 nds;. minute =
bdd60 28 53 48 4f 52 54 29 64 74 2e 6d 69 6e 75 74 65 (SHORT)dt.minute
bdd70 73 20 2b 20 64 74 2e 74 69 6d 65 7a 6f 6e 65 3b s + dt.timezone;
bdd80 0a 20 20 68 6f 75 72 20 3d 20 28 55 53 48 4f 52 . hour = (USHOR
bdd90 54 29 64 74 2e 68 6f 75 72 73 3b 0a 20 20 64 61 T)dt.hours;. da
bdda0 79 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 64 y = (USHORT)dt.d
bddb0 61 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d 20 28 55 ay;. month = (U
bddc0 53 48 4f 52 54 29 64 74 2e 6d 6f 6e 74 68 3b 0a SHORT)dt.month;.
bddd0 20 20 79 65 61 72 20 3d 20 28 55 53 48 4f 52 54 year = (USHORT
bdde0 29 64 74 2e 79 65 61 72 3b 0a 0a 20 20 2f 2a 20 )dt.year;.. /*
bddf0 43 61 6c 63 75 6c 61 74 69 6f 6e 73 20 66 72 6f Calculations fro
bde00 6d 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 m http://www.ast
bde10 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e ro.keele.ac.uk/~
bde20 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a rno/Astronomy/hj
bde30 64 2e 68 74 6d 6c 0a 20 20 20 20 20 68 74 74 70 d.html. http
bde40 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 ://www.astro.kee
bde50 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 le.ac.uk/~rno/As
bde60 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30 2e 31 2e tronomy/hjd-0.1.
bde70 63 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c 63 75 6c c */. /* Calcul
bde80 61 74 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 64 ate the Julian d
bde90 61 79 73 20 2a 2f 0a 20 20 6e 6f 77 20 3d 20 64 ays */. now = d
bdea0 61 79 20 2d 20 33 32 30 37 36 20 2b 0a 20 20 20 ay - 32076 +.
bdeb0 20 31 34 36 31 2a 28 79 65 61 72 20 2b 20 34 38 1461*(year + 48
bdec0 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 00 + (month - 14
bded0 29 2f 31 32 29 2f 34 20 2b 0a 20 20 20 20 33 36 )/12)/4 +. 36
bdee0 37 2a 28 6d 6f 6e 74 68 20 2d 20 32 20 2d 20 28 7*(month - 2 - (
bdef0 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 2a 31 month - 14)/12*1
bdf00 32 29 2f 31 32 20 2d 0a 20 20 20 20 33 2a 28 28 2)/12 -. 3*((
bdf10 79 65 61 72 20 2b 20 34 39 30 30 20 2b 20 28 6d year + 4900 + (m
bdf20 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 31 onth - 14)/12)/1
bdf30 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20 41 64 64 00)/4;.. /* Add
bdf40 20 74 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 20 the fractional
bdf50 68 6f 75 72 73 2c 20 6d 69 6e 73 20 61 6e 64 20 hours, mins and
bdf60 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 6e 6f 77 seconds */. now
bdf70 20 2b 3d 20 28 68 6f 75 72 20 2b 20 31 32 2e 30 += (hour + 12.0
bdf80 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d )/24.0;. now +=
bdf90 20 6d 69 6e 75 74 65 2f 31 34 34 30 2e 30 3b 0a minute/1440.0;.
bdfa0 20 20 6e 6f 77 20 2b 3d 20 73 65 63 6f 6e 64 2f now += second/
bdfb0 38 36 34 30 30 2e 30 3b 0a 20 20 2a 70 72 4e 6f 86400.0;. *prNo
bdfc0 77 20 3d 20 6e 6f 77 3b 0a 23 69 66 64 65 66 20 w = now;.#ifdef
bdfd0 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 SQLITE_TEST. if
bdfe0 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e ( sqlite3_curren
bdff0 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 t_time ){. *p
be000 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 rNow = sqlite3_c
be010 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 urrent_time/8640
be020 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0.0 + 2440587.5;
be030 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 . }.#endif. re
be040 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61 74 69 turn 0;.}..stati
be050 63 20 69 6e 74 20 6f 73 32 47 65 74 4c 61 73 74 c int os2GetLast
be060 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
be070 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 s *pVfs, int nBu
be080 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
be090 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
be0a0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
be0b0 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 and deinitialize
be0c0 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
be0d0 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e ystem interface.
be0e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
be0f0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e nt sqlite3_os_in
be100 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 it(void){. stat
be110 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f ic sqlite3_vfs o
be120 73 32 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c s2Vfs = {. 1,
be130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be140 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
be150 20 20 20 20 73 69 7a 65 6f 66 28 6f 73 32 46 69 sizeof(os2Fi
be160 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 le), /* szOsFi
be170 6c 65 20 2a 2f 0a 20 20 20 20 43 43 48 4d 41 58 le */. CCHMAX
be180 50 41 54 48 2c 20 20 20 20 20 20 20 20 2f 2a 20 PATH, /*
be190 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 mxPathname */.
be1a0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
be1b0 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f /* pNext */
be1c0 0a 20 20 20 20 22 6f 73 32 22 2c 20 20 20 20 20 . "os2",
be1d0 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 /* zName
be1e0 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 */. 0,
be1f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 /* pA
be200 70 70 44 61 74 61 20 2a 2f 0a 0a 20 20 20 20 6f ppData */.. o
be210 73 32 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 s2Open,
be220 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 /* xOpen */.
be230 20 20 6f 73 32 44 65 6c 65 74 65 2c 20 20 20 20 os2Delete,
be240 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 /* xDelete
be250 2a 2f 0a 20 20 20 20 6f 73 32 41 63 63 65 73 73 */. os2Access
be260 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 , /* xAc
be270 63 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 46 cess */. os2F
be280 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f ullPathname, /
be290 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 * xFullPathname
be2a0 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 4f 70 65 6e */. os2DlOpen
be2b0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c , /* xDl
be2c0 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 Open */. os2D
be2d0 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f lError, /
be2e0 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 * xDlError */.
be2f0 20 20 6f 73 32 44 6c 53 79 6d 2c 20 20 20 20 20 os2DlSym,
be300 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a /* xDlSym *
be310 2f 0a 20 20 20 20 6f 73 32 44 6c 43 6c 6f 73 65 /. os2DlClose
be320 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 , /* xDlC
be330 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 6f 73 32 52 lose */. os2R
be340 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f andomness, /
be350 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f * xRandomness */
be360 0a 20 20 20 20 6f 73 32 53 6c 65 65 70 2c 20 20 . os2Sleep,
be370 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 /* xSlee
be380 70 20 2a 2f 0a 20 20 20 20 6f 73 32 43 75 72 72 p */. os2Curr
be390 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78 entTime, /* x
be3a0 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 CurrentTime */.
be3b0 20 20 20 6f 73 32 47 65 74 4c 61 73 74 45 72 72 os2GetLastErr
be3c0 6f 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 or /* xGetLas
be3d0 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20 tError */. };.
be3e0 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 sqlite3_vfs_reg
be3f0 69 73 74 65 72 28 26 6f 73 32 56 66 73 2c 20 31 ister(&os2Vfs, 1
be400 29 3b 0a 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 );. initUconvOb
be410 6a 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75 72 jects();. retur
be420 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 n SQLITE_OK;.}.S
be430 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
be440 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 lite3_os_end(voi
be450 64 29 7b 0a 20 20 66 72 65 65 55 63 6f 6e 76 4f d){. freeUconvO
be460 62 6a 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75 bjects();. retu
be470 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
be480 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
be490 45 5f 4f 53 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a E_OS_OS2 */../**
be4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
be4b0 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a of os_os2.c ***
be4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
be4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
be500 69 6e 20 66 69 6c 65 20 6f 73 5f 75 6e 69 78 2e in file os_unix.
be510 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
be520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
be540 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a ** 2004 May 22.*
be550 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
be560 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
be570 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
be580 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
be590 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
be5a0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
be5b0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
be5c0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
be5d0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
be5e0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
be5f0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
be600 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
be610 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
be620 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
be630 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
be640 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
be650 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
be660 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
be670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be6b0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
be6c0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
be6d0 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 VFS implementati
be6e0 6f 6e 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 on for unix-like
be6f0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
be700 6d 73 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 4c 69 ms.** include Li
be710 6e 75 78 2c 20 4d 61 63 4f 53 58 2c 20 2a 42 53 nux, MacOSX, *BS
be720 44 2c 20 51 4e 58 2c 20 56 78 57 6f 72 6b 73 2c D, QNX, VxWorks,
be730 20 41 49 58 2c 20 48 50 55 58 2c 20 61 6e 64 20 AIX, HPUX, and
be740 6f 74 68 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 others..**.** Th
be750 65 72 65 20 61 72 65 20 61 63 74 75 61 6c 6c 79 ere are actually
be760 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 several differe
be770 6e 74 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 nt VFS implement
be780 61 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20 66 ations in this f
be790 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 64 69 66 66 ile..** The diff
be7a0 65 72 65 6e 63 65 73 20 61 72 65 20 69 6e 20 74 erences are in t
be7b0 68 65 20 77 61 79 20 74 68 61 74 20 66 69 6c 65 he way that file
be7c0 20 6c 6f 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 locking is done
be7d0 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a . The default.*
be7e0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
be7f0 20 75 73 65 73 20 50 6f 73 69 78 20 41 64 76 69 uses Posix Advi
be800 73 6f 72 79 20 4c 6f 63 6b 73 2e 20 20 41 6c 74 sory Locks. Alt
be810 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 ernative impleme
be820 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 75 73 65 20 ntations.** use
be830 66 6c 6f 63 6b 28 29 2c 20 64 6f 74 2d 66 69 6c flock(), dot-fil
be840 65 73 2c 20 76 61 72 69 6f 75 73 20 70 72 6f 70 es, various prop
be850 72 69 65 74 61 72 79 20 6c 6f 63 6b 69 6e 67 20 rietary locking
be860 73 63 68 65 6d 61 73 2c 20 6f 72 20 73 69 6d 70 schemas, or simp
be870 6c 79 0a 2a 2a 20 73 6b 69 70 20 6c 6f 63 6b 69 ly.** skip locki
be880 6e 67 20 61 6c 6c 20 74 6f 67 65 74 68 65 72 2e ng all together.
be890 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 6f 75 72 .**.** This sour
be8a0 63 65 20 66 69 6c 65 20 69 73 20 6f 72 67 61 6e ce file is organ
be8b0 69 7a 65 64 20 69 6e 74 6f 20 64 69 76 69 73 69 ized into divisi
be8c0 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f ons where the lo
be8d0 67 69 63 20 66 6f 72 20 76 61 72 69 6f 75 73 0a gic for various.
be8e0 2a 2a 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 ** subfunctions
be8f0 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 is contained wit
be900 68 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 hin the appropri
be910 61 74 65 20 64 69 76 69 73 69 6f 6e 2e 20 20 50 ate division. P
be920 4c 45 41 53 45 0a 2a 2a 20 4b 45 45 50 20 54 48 LEASE.** KEEP TH
be930 45 20 53 54 52 55 43 54 55 52 45 20 4f 46 20 54 E STRUCTURE OF T
be940 48 49 53 20 46 49 4c 45 20 49 4e 54 41 43 54 2e HIS FILE INTACT.
be950 20 20 4e 65 77 20 63 6f 64 65 20 73 68 6f 75 6c New code shoul
be960 64 20 62 65 20 70 6c 61 63 65 64 0a 2a 2a 20 69 d be placed.** i
be970 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 64 69 n the correct di
be980 76 69 73 69 6f 6e 20 61 6e 64 20 73 68 6f 75 6c vision and shoul
be990 64 20 62 65 20 63 6c 65 61 72 6c 79 20 6c 61 62 d be clearly lab
be9a0 65 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eled..**.** The
be9b0 6c 61 79 6f 75 74 20 6f 66 20 64 69 76 69 73 69 layout of divisi
be9c0 6f 6e 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 ons is as follow
be9d0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 47 65 s:.**.** * Ge
be9e0 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 64 65 neral-purpose de
be9f0 63 6c 61 72 61 74 69 6f 6e 73 20 61 6e 64 20 75 clarations and u
bea00 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 tility functions
bea10 2e 0a 2a 2a 20 20 20 2a 20 20 55 6e 69 71 75 65 ..** * Unique
bea20 20 66 69 6c 65 20 49 44 20 6c 6f 67 69 63 20 75 file ID logic u
bea30 73 65 64 20 62 79 20 56 78 57 6f 72 6b 73 2e 0a sed by VxWorks..
bea40 2a 2a 20 20 20 2a 20 20 56 61 72 69 6f 75 73 20 ** * Various
bea50 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 locking primitiv
bea60 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
bea70 73 20 28 61 6c 6c 20 65 78 63 65 70 74 20 70 72 s (all except pr
bea80 6f 78 79 20 6c 6f 63 6b 69 6e 67 29 3a 0a 2a 2a oxy locking):.**
bea90 20 20 20 20 20 20 2b 20 66 6f 72 20 50 6f 73 69 + for Posi
beaa0 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 x Advisory Locks
beab0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e .** + for n
beac0 6f 2d 6f 70 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 o-op locks.**
bead0 20 20 20 2b 20 66 6f 72 20 64 6f 74 2d 66 69 6c + for dot-fil
beae0 65 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 e locks.**
beaf0 2b 20 66 6f 72 20 66 6c 6f 63 6b 28 29 20 6c 6f + for flock() lo
beb00 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 2b 20 cking.** +
beb10 66 6f 72 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 for named semaph
beb20 6f 72 65 20 6c 6f 63 6b 73 20 28 56 78 57 6f 72 ore locks (VxWor
beb30 6b 73 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 20 ks only).**
beb40 20 2b 20 66 6f 72 20 41 46 50 20 66 69 6c 65 73 + for AFP files
beb50 79 73 74 65 6d 20 6c 6f 63 6b 73 20 28 4d 61 63 ystem locks (Mac
beb60 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a OSX only).** *
beb70 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d sqlite3_file m
beb80 65 74 68 6f 64 73 20 6e 6f 74 20 61 73 73 6f 63 ethods not assoc
beb90 69 61 74 65 64 20 77 69 74 68 20 6c 6f 63 6b 69 iated with locki
beba0 6e 67 2e 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69 ng..** * Defi
bebb0 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 nitions of sqlit
bebc0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
bebd0 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f jects for all lo
bebe0 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 cking.** me
bebf0 74 68 6f 64 73 20 70 6c 75 73 20 22 66 69 6e 64 thods plus "find
bec00 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f er" functions fo
bec10 72 20 65 61 63 68 20 6c 6f 63 6b 69 6e 67 20 6d r each locking m
bec20 65 74 68 6f 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 ethod..** * s
bec30 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f qlite3_vfs metho
bec40 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e d implementation
bec50 73 2e 0a 2a 2a 20 20 20 2a 20 20 4c 6f 63 6b 69 s..** * Locki
bec60 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f ng primitives fo
bec70 72 20 74 68 65 20 70 72 6f 78 79 20 75 62 65 72 r the proxy uber
bec80 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 2e -locking-method.
bec90 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a (MacOSX only).*
beca0 2a 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f * * Definitio
becb0 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 ns of sqlite3_vf
becc0 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c s objects for al
becd0 6c 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 l locking method
bece0 73 0a 2a 2a 20 20 20 20 20 20 70 6c 75 73 20 69 s.** plus i
becf0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
bed00 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 f sqlite3_os_ini
bed10 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f t() and sqlite3_
bed20 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 os_end()..**.**
bed30 24 49 64 3a 20 6f 73 5f 75 6e 69 78 2e 63 2c 76 $Id: os_unix.c,v
bed40 20 31 2e 32 35 30 20 32 30 30 39 2f 30 34 2f 30 1.250 2009/04/0
bed50 37 20 30 35 3a 33 35 3a 30 34 20 63 68 77 20 45 7 05:35:04 chw E
bed60 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 xp $.*/.#if SQLI
bed70 54 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 TE_OS_UNIX
bed80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
bed90 66 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 file is used on
beda0 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a unix only */../*
bedb0 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 76 61 .** There are va
bedc0 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 6f rious methods fo
bedd0 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 r file locking u
bede0 73 65 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 sed for concurre
bedf0 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a ncy.** control:.
bee00 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 **.** 1. POSIX
bee10 20 6c 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 locking (the de
bee20 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 fault),.** 2.
bee30 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 No locking,.**
bee40 20 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 3. Dot-file loc
bee50 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c king,.** 4. fl
bee60 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a ock() locking,.*
bee70 2a 20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 * 5. AFP locki
bee80 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a ng (OSX only),.*
bee90 2a 20 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 * 6. Named POS
beea0 49 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 IX semaphores (V
beeb0 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a XWorks only),.**
beec0 20 20 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 7. proxy lock
beed0 69 6e 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a ing. (OSX only).
beee0 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 **.** Styles 4,
beef0 35 2c 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 5, and 7 are onl
bef00 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 y available of S
bef10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
bef20 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 KING_STYLE.** is
bef30 20 64 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 defined to 1.
bef40 54 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c The SQLITE_ENABL
bef50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
bef60 61 6c 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 also enables aut
bef70 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 omatic.** select
bef80 69 6f 6e 20 6f 66 20 74 68 65 20 61 70 70 72 6f ion of the appro
bef90 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 priate locking s
befa0 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 68 tyle based on th
befb0 65 20 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 e filesystem.**
befc0 77 68 65 72 65 20 74 68 65 20 64 61 74 61 62 61 where the databa
befd0 73 65 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 se is located.
befe0 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
beff0 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c (SQLITE_ENABLE_L
bf000 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 OCKING_STYLE).#
bf010 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 if defined(__AP
bf020 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 PLE__).# defi
bf030 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ne SQLITE_ENABLE
bf040 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 _LOCKING_STYLE 1
bf050 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 .# else.# de
bf060 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 fine SQLITE_ENAB
bf070 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
bf080 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 0.# endif.#end
bf090 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 if../*.** Define
bf0a0 20 74 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 the OS_VXWORKS
bf0b0 70 72 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 pre-processor ma
bf0c0 63 72 6f 20 74 6f 20 31 20 69 66 20 62 75 69 6c cro to 1 if buil
bf0d0 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f ding on .** vxwo
bf0e0 72 6b 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 rks, or 0 otherw
bf0f0 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ise..*/.#ifndef
bf100 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 OS_VXWORKS.# if
bf110 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f defined(__RTP__
bf120 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 ) || defined(_WR
bf130 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 S_KERNEL).# d
bf140 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 efine OS_VXWORKS
bf150 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 1.# else.#
bf160 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b define OS_VXWORK
bf170 53 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e S 0.# endif.#en
bf180 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 dif../*.** These
bf190 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 #defines should
bf1a0 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 6c enable >2GB fil
bf1b0 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 e support on Pos
bf1c0 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 ix if the.** und
bf1d0 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e erlying operatin
bf1e0 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 g system support
bf1f0 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f 53 s it. If the OS
bf200 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 lacks.** large
bf210 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 68 file support, th
bf220 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f ese should be no
bf230 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 -ops..**.** Larg
bf240 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 e file support c
bf250 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 an be disabled u
bf260 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 sing the -DSQLIT
bf270 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 E_DISABLE_LFS sw
bf280 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 itch.** on the c
bf290 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 ompiler command
bf2a0 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e line. This is n
bf2b0 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 ecessary if you
bf2c0 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a are compiling.**
bf2d0 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 on a recent mac
bf2e0 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 hine (ex: RedHat
bf2f0 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61 7.2) but you wa
bf300 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 nt your code to
bf310 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c work.** on an ol
bf320 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a der machine (ex:
bf330 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49 RedHat 6.0). I
bf340 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e f you compile on
bf350 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 RedHat 7.2.** w
bf360 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69 ithout this opti
bf370 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c on, LFS is enabl
bf380 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73 e. But LFS does
bf390 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68 not exist in th
bf3a0 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 e kernel.** in R
bf3b0 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 edHat 6.0, so th
bf3c0 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 e code won't wor
bf3d0 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d k. Hence, for m
bf3e0 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a aximum binary.**
bf3f0 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75 portability you
bf400 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 should omit LFS
bf410 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 ..**.** The prev
bf420 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 20 77 ious paragraph w
bf430 61 73 20 77 72 69 74 74 65 6e 20 69 6e 20 32 30 as written in 20
bf440 30 35 2e 20 20 28 54 68 69 73 20 70 61 72 61 67 05. (This parag
bf450 72 61 70 68 20 69 73 20 77 72 69 74 74 65 6e 0a raph is written.
bf460 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 ** on 2008-11-28
bf470 2e 29 20 54 68 65 73 65 20 64 61 79 73 2c 20 61 .) These days, a
bf480 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 ll Linux kernels
bf490 20 73 75 70 70 6f 72 74 20 6c 61 72 67 65 20 66 support large f
bf4a0 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 iles, so.** you
bf4b0 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 should probably
bf4c0 6c 65 61 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 leave LFS enable
bf4d0 64 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 d. But some emb
bf4e0 65 64 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 edded platforms
bf4f0 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 might.** lack LF
bf500 53 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 S in which case
bf510 74 68 65 20 53 51 4c 49 54 45 5f 44 49 53 41 42 the SQLITE_DISAB
bf520 4c 45 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 LE_LFS macro mig
bf530 68 74 20 73 74 69 6c 6c 20 62 65 20 75 73 65 66 ht still be usef
bf540 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ul..*/.#ifndef S
bf550 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 QLITE_DISABLE_LF
bf560 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 S.# define _LARG
bf570 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 E_FILE 1.#
bf580 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 ifndef _FILE_OF
bf590 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 FSET_BITS.# de
bf5a0 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 fine _FILE_OFFSE
bf5b0 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 T_BITS 64.# endi
bf5c0 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 f.# define _LARG
bf5d0 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 EFILE_SOURCE 1.#
bf5e0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 endif../*.** sta
bf5f0 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 ndard include fi
bf600 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 les..*/.#include
bf610 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 <sys/types.h>.#
bf620 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 include <sys/sta
bf630 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 t.h>.#include <f
bf640 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 cntl.h>.#include
bf650 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 <unistd.h>.#inc
bf660 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 lude <sys/time.h
bf670 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e >.#include <errn
bf680 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 o.h>..#if SQLITE
bf690 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
bf6a0 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 STYLE.# include
bf6b0 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 <sys/ioctl.h>.#
bf6c0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 if OS_VXWORKS.#
bf6d0 20 69 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 include <semaph
bf6e0 6f 72 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 ore.h>.# includ
bf6f0 65 20 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 e <limits.h>.# e
bf700 6c 73 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c lse.# include <
bf710 73 79 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 sys/file.h>.# i
bf720 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 nclude <sys/para
bf730 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 m.h>.# include
bf740 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 <sys/mount.h>.#
bf750 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 endif.#endif /*
bf760 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
bf770 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a CKING_STYLE */..
bf780 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 /*.** If we are
bf790 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73 61 66 to be thread-saf
bf7a0 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 e, include the p
bf7b0 74 68 72 65 61 64 73 20 68 65 61 64 65 72 20 61 threads header a
bf7c0 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 nd define.** the
bf7d0 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 SQLITE_UNIX_THR
bf7e0 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 EADS macro..*/.#
bf7f0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
bf800 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SAFE.# define SQ
bf810 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 LITE_UNIX_THREAD
bf820 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a S 1.#endif../*.*
bf830 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73 * Default permis
bf840 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74 sions when creat
bf850 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a ing a new file.*
bf860 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
bf870 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 _DEFAULT_FILE_PE
bf880 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 RMISSIONS.# defi
bf890 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
bf8a0 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f T_FILE_PERMISSIO
bf8b0 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a NS 0644.#endif..
bf8c0 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 /*. ** Default p
bf8d0 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 ermissions when
bf8e0 63 72 65 61 74 69 6e 67 20 61 75 74 6f 20 70 72 creating auto pr
bf8f0 6f 78 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e oxy dir. */.#ifn
bf900 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
bf910 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d LT_PROXYDIR_PERM
bf920 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 ISSIONS.# define
bf930 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
bf940 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
bf950 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 66 IONS 0755.#endif
bf960 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 ../*.** Maximum
bf970 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c supported path-l
bf980 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e ength..*/.#defin
bf990 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 e MAX_PATHNAME 5
bf9a0 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 12../*.** Only s
bf9b0 65 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f et the lastErrno
bf9c0 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f if the error co
bf9d0 64 65 20 69 73 20 61 20 72 65 61 6c 20 65 72 72 de is a real err
bf9e0 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 or and not .** a
bf9f0 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64 normal expected
bfa00 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 return code of
bfa10 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53 SQLITE_BUSY or S
bfa20 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 QLITE_OK.*/.#def
bfa30 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f ine IS_LOCK_ERRO
bfa40 52 28 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c R(x) ((x != SQL
bfa50 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d ITE_OK) && (x !=
bfa60 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a SQLITE_BUSY))..
bfa70 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 78 46 ./*.** The unixF
bfa80 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 ile structure is
bfa90 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c subclass of sql
bfaa0 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 ite3_file specif
bfab0 69 63 20 74 6f 20 74 68 65 20 75 6e 69 78 0a 2a ic to the unix.*
bfac0 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 * VFS implementa
bfad0 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 tions..*/.typede
bfae0 66 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c f struct unixFil
bfaf0 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74 72 75 e unixFile;.stru
bfb00 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a 20 20 ct unixFile {.
bfb10 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
bfb20 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f ds const *pMetho
bfb30 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 d; /* Always th
bfb40 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f e first entry */
bfb50 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
bfb60 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 enCnt *pOpen;
bfb70 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 /* Info abou
bfb80 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 73 20 t all open fd's
bfb90 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f on this inode */
bfba0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f . struct unixLo
bfbb0 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 ckInfo *pLock;
bfbc0 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 /* Info abou
bfbd0 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 20 t locks on this
bfbe0 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 inode */. int h
bfbf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
bfc00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
bfc10 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
bfc20 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 or */. int dirf
bfc30 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
bfc40 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
bfc50 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 descriptor for
bfc60 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 2a 2f the directory */
bfc70 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
bfc80 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 locktype;
bfc90 20 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65 20 /* The type
bfca0 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 of lock held on
bfcb0 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e 74 this fd */. int
bfcc0 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20 20 lastErrno;
bfcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bfce0 20 54 68 65 20 75 6e 69 78 20 65 72 72 6e 6f 20 The unix errno
bfcf0 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f from the last I/
bfd00 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 76 6f 69 O error */. voi
bfd10 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 d *lockingContex
bfd20 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
bfd30 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 73 Locking style s
bfd40 70 65 63 69 66 69 63 20 73 74 61 74 65 20 2a 2f pecific state */
bfd50 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
bfd60 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
bfd70 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 . int openFlags
bfd80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
bfd90 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 67 73 /* The flags
bfda0 20 73 70 65 63 69 66 69 65 64 20 61 74 20 6f 70 specified at op
bfdb0 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 en() */.#endif.#
bfdc0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
bfdd0 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 SAFE && defined(
bfde0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 __linux__). pth
bfdf0 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20 20 20 read_t tid;
bfe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bfe10 20 54 68 65 20 74 68 72 65 61 64 20 74 68 61 74 The thread that
bfe20 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75 6e 69 "owns" this uni
bfe30 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a xFile */.#endif.
bfe40 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
bfe50 20 69 6e 74 20 69 73 44 65 6c 65 74 65 3b 20 20 int isDelete;
bfe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bfe70 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 /* Delete on c
bfe80 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a lose if true */.
bfe90 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
bfea0 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 20 20 FileId *pId;
bfeb0 20 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 69 6c /* Unique fil
bfec0 65 20 49 44 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 e ID */.#endif.#
bfed0 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
bfee0 2f 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 /* The next grou
bfef0 70 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61 p of variables a
bff00 72 65 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b re used to track
bff10 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
bff20 74 68 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 the. ** transac
bff30 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e 20 tion counter in
bff40 62 79 74 65 73 20 32 34 2d 32 37 20 6f 66 20 64 bytes 24-27 of d
bff50 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 61 72 atabase files ar
bff60 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 77 e updated. ** w
bff70 68 65 6e 65 76 65 72 20 61 6e 79 20 70 61 72 74 henever any part
bff80 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
bff90 20 63 68 61 6e 67 65 73 2e 20 20 41 6e 20 61 73 changes. An as
bffa0 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 sertion fault wi
bffb0 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 66 ll. ** occur if
bffc0 20 61 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 a file is updat
bffd0 65 64 20 77 69 74 68 6f 75 74 20 61 6c 73 6f 20 ed without also
bffe0 75 70 64 61 74 69 6e 67 20 74 68 65 20 74 72 61 updating the tra
bfff0 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f nsaction. ** co
c0000 75 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 unter. This tes
c0010 74 20 69 73 20 6d 61 64 65 20 74 6f 20 61 76 6f t is made to avo
c0020 69 64 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 20 id new problems
c0030 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a 20 similar to the.
c0040 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 65 ** one describe
c0050 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 35 38 d by ticket #358
c0060 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 4. . */. unsig
c0070 6e 65 64 20 63 68 61 72 20 74 72 61 6e 73 43 6e ned char transCn
c0080 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72 75 trChng; /* Tru
c0090 65 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 e if the transac
c00a0 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68 61 tion counter cha
c00b0 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e nged */. unsign
c00c0 65 64 20 63 68 61 72 20 64 62 55 70 64 61 74 65 ed char dbUpdate
c00d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
c00e0 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 if any part of
c00f0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 database file ch
c0100 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 anged */. unsig
c0110 6e 65 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d 61 ned char inNorma
c0120 6c 57 72 69 74 65 3b 20 20 20 2f 2a 20 54 72 75 lWrite; /* Tru
c0130 65 20 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61 6c e if in a normal
c0140 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e write operation
c0150 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 72 75 */.. /* If tru
c0160 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 e, that means we
c0170 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
c0180 68 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c h a database fil
c0190 65 20 74 68 61 74 20 68 61 73 0a 20 20 2a 2a 20 e that has. **
c01a0 61 20 72 61 6e 67 65 20 6f 66 20 6c 6f 63 6b 69 a range of locki
c01b0 6e 67 20 62 79 74 65 73 20 66 72 6f 6d 20 50 45 ng bytes from PE
c01c0 4e 44 49 4e 47 5f 42 59 54 45 20 74 68 72 6f 75 NDING_BYTE throu
c01d0 67 68 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b gh PENDING_BYTE+
c01e0 35 31 31 0a 20 20 2a 2a 20 77 68 69 63 68 20 73 511. ** which s
c01f0 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 hould never be r
c0200 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 ead or written.
c0210 20 41 73 73 65 72 74 73 28 29 20 77 69 6c 6c 20 Asserts() will
c0220 76 65 72 69 66 79 20 74 68 69 73 20 2a 2f 0a 20 verify this */.
c0230 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 unsigned char i
c0240 73 4c 6f 63 6b 61 62 6c 65 3b 20 20 20 20 20 20 sLockable;
c0250 2f 2a 20 54 72 75 65 20 69 66 20 66 69 6c 65 20 /* True if file
c0260 6d 69 67 68 74 20 62 65 20 6c 6f 63 6b 65 64 20 might be locked
c0270 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 */.#endif.#ifdef
c0280 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f SQLITE_TEST. /
c0290 2a 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20 * In test mode,
c02a0 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a increase the siz
c02b0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 e of this struct
c02c0 75 72 65 20 61 20 62 69 74 20 73 6f 20 74 68 61 ure a bit so tha
c02d0 74 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c 61 t . ** it is la
c02e0 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 rger than the st
c02f0 72 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 64 ruct CrashFile d
c0300 65 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36 2e efined in test6.
c0310 63 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 61 c.. */. char a
c0320 50 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e Padding[32];.#en
c0330 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e dif.};../*.** In
c0340 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 clude code that
c0350 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c is common to all
c0360 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f os_*.c files.*/
c0370 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c0380 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d Include os_comm
c0390 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 on.h in the midd
c03a0 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 le of os_unix.c
c03b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c03c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c03d0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 Begin file os_c
c03e0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
c03f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c0410 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
c0420 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
c0430 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
c0440 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
c0450 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
c0460 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
c0470 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
c0480 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
c0490 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
c04a0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
c04b0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
c04c0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
c04d0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
c04e0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
c04f0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
c0500 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
c0510 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
c0520 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
c0530 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
c0540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0580 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
c0590 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
c05a0 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 macros and a lit
c05b0 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 tle bit of code
c05c0 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
c05d0 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 o.** all of the
c05e0 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 platform-specifi
c05f0 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 c files (os_*.c)
c0600 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 and is #include
c0610 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 d into those.**
c0620 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 files..**.** Thi
c0630 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 s file should be
c0640 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 #included by th
c0650 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f e os_*.c files o
c0660 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 nly. It is not
c0670 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 a.** general pur
c0680 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 pose header file
c0690 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f ..**.** $Id: os_
c06a0 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 common.h,v 1.38
c06b0 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 2009/02/24 18:40
c06c0 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :50 danielk1977
c06d0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
c06e0 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 _OS_COMMON_H_.#
c06f0 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f define _OS_COMMO
c0700 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c N_H_../*.** At l
c0710 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 east two bugs ha
c0720 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 ve slipped in be
c0730 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 cause we changed
c0740 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 the MEMORY_DEBU
c0750 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 G.** macro to SQ
c0760 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 LITE_DEBUG and s
c0770 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 ome older makefi
c0780 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 les have not yet
c0790 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 made the.** swi
c07a0 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 tch. The follow
c07b0 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 ing code should
c07c0 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c catch this probl
c07d0 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 em at compile-ti
c07e0 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 me..*/.#ifdef ME
c07f0 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 MORY_DEBUG.# err
c0800 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 or "The MEMORY_D
c0810 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 EBUG macro is ob
c0820 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c solete. Use SQL
c0830 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 ITE_DEBUG instea
c0840 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 d.".#endif..#ifd
c0850 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
c0860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
c0870 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 nt sqlite3OSTrac
c0880 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f e = 0;.#define O
c0890 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 STRACE1(X)
c08a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
c08b0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
c08c0 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 ebugPrintf(X).#d
c08d0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 efine OSTRACE2(X
c08e0 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71 ,Y) if( sq
c08f0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
c0900 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c0910 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f f(X,Y).#define O
c0920 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 STRACE3(X,Y,Z)
c0930 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
c0940 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
c0950 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
c0960 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c0970 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 E4(X,Y,Z,A) if
c0980 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
c0990 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
c09a0 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 rintf(X,Y,Z,A).#
c09b0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
c09c0 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 X,Y,Z,A,B) if( s
c09d0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
c09e0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
c09f0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 tf(X,Y,Z,A,B).#d
c0a00 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 efine OSTRACE6(X
c0a10 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 ,Y,Z,A,B,C) \.
c0a20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 if(sqlite3OSTr
c0a30 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 ace) sqlite3Debu
c0a40 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c gPrintf(X,Y,Z,A,
c0a50 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 B,C).#define OST
c0a60 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c RACE7(X,Y,Z,A,B,
c0a70 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 C,D) \. if(sq
c0a80 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 lite3OSTrace) sq
c0a90 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
c0aa0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a (X,Y,Z,A,B,C,D).
c0ab0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 #else.#define OS
c0ac0 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e TRACE1(X).#defin
c0ad0 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a e OSTRACE2(X,Y).
c0ae0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 #define OSTRACE3
c0af0 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
c0b00 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
c0b10 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c0b20 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 E5(X,Y,Z,A,B).#d
c0b30 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 efine OSTRACE6(X
c0b40 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 ,Y,Z,A,B,C).#def
c0b50 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 ine OSTRACE7(X,Y
c0b60 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 ,Z,A,B,C,D).#end
c0b70 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 if../*.** Macros
c0b80 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 for performance
c0b90 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 tracing. Norma
c0ba0 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 lly turned off.
c0bb0 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f Only works.** o
c0bc0 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e n i486 hardware.
c0bd0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
c0be0 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 E_PERFORMANCE_TR
c0bf0 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 ACE../* .** hwti
c0c00 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e me.h contains in
c0c10 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 line assembler c
c0c20 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e ode for implemen
c0c30 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 ting .** high-pe
c0c40 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 rformance timing
c0c50 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a routines..*/./*
c0c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
c0c70 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 clude hwtime.h i
c0c80 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
c0c90 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
c0ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
c0cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
c0cc0 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e gin file hwtime.
c0cd0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
c0ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
c0d00 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a .** 2008 May 27.
c0d10 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
c0d20 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
c0d30 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
c0d40 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
c0d50 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
c0d60 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
c0d70 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
c0d80 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
c0d90 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
c0da0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
c0db0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
c0dc0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
c0dd0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
c0de0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
c0df0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
c0e00 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
c0e10 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
c0e20 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
c0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0e70 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
c0e80 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c ile contains inl
c0e90 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 ine asm code for
c0ea0 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 retrieving "hig
c0eb0 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a h-performance".*
c0ec0 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 * counters for x
c0ed0 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 86 class CPUs..*
c0ee0 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 *.** $Id: hwtime
c0ef0 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 .h,v 1.3 2008/08
c0f00 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 /01 14:33:15 sha
c0f10 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e ne Exp $.*/.#ifn
c0f20 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 def _HWTIME_H_.#
c0f30 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 define _HWTIME_H
c0f40 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c _../*.** The fol
c0f50 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f lowing routine o
c0f60 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e nly works on pen
c0f70 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e tium-class (or n
c0f80 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 ewer) processors
c0f90 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 ..** It uses the
c0fa0 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f RDTSC opcode to
c0fb0 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 read the cycle
c0fc0 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 count value out
c0fd0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 of the.** proces
c0fe0 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 sor and returns
c0ff0 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 that value. Thi
c1000 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f s can be used fo
c1010 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 r high-res.** pr
c1020 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 ofiling..*/.#if
c1030 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
c1040 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d _) || defined(_M
c1050 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 SC_VER)) && \.
c1060 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 (defined(i38
c1070 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 6) || defined(__
c1080 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e i386__) || defin
c1090 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 ed(_M_IX86))..
c10a0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e #if defined(__GN
c10b0 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e UC__).. __inlin
c10c0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
c10d0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
c10e0 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 void){. unsi
c10f0 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b gned int lo, hi;
c1100 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f . __asm__ __
c1110 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 volatile__ ("rdt
c1120 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c sc" : "=a" (lo),
c1130 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 "=d" (hi));.
c1140 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 return (sqlite
c1150 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 _uint64)hi << 32
c1160 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 | lo;. }.. #e
c1170 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 lif defined(_MSC
c1180 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 _VER).. __decls
c1190 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c pec(naked) __inl
c11a0 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 ine sqlite_uint6
c11b0 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 4 __cdecl sqlite
c11c0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
c11d0 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 __asm {.
c11e0 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 rdtsc.
c11f0 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 ret ; re
c1200 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 turn value at ED
c1210 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d X:EAX. }. }
c1220 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 .. #endif..#eli
c1230 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
c1240 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 C__) && defined(
c1250 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 __x86_64__))..
c1260 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
c1270 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
c1280 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
c1290 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
c12a0 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 g val;. __a
c12b0 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
c12c0 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 _ ("rdtsc" : "=A
c12d0 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 " (val));.
c12e0 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a return val;. }.
c12f0 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 .#elif (defined
c1300 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
c1310 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a fined(__ppc__)).
c1320 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 . __inline__ sq
c1330 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
c1340 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
c1350 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
c1360 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c long long retval
c1370 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 ;. unsigned
c1380 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 long junk;.
c1390 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
c13a0 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 tile__ ("\n\.
c13b0 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 1: m
c13c0 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 ftbu %1\n\.
c13d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
c13e0 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 ftb %L0\n\.
c13f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1400 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 mftbu %0\n\.
c1410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1420 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c cmpw %0,%1\n\
c1430 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c1440 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 bne 1b".
c1450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1460 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 : "=r" (retval)
c1470 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a , "=r" (junk));.
c1480 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 return ret
c1490 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a val;. }..#else.
c14a0 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 . #error Need i
c14b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
c14c0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
c14d0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f for your platfo
c14e0 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 rm... /*. ** T
c14f0 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 o compile withou
c1500 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 t implementing s
c1510 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 qlite3Hwtime() f
c1520 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d or your platform
c1530 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 ,. ** you can r
c1540 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 emove the above
c1550 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 #error and use t
c1560 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a he following. *
c1570 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e * stub function.
c1580 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 You will lose
c1590 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 timing support f
c15a0 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 or many. ** of
c15b0 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e the debugging an
c15c0 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 d testing utilit
c15d0 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 ies, but it shou
c15e0 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 ld at. ** least
c15f0 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e compile and run
c1600 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 .. */.SQLITE_PR
c1610 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 IVATE sqlite_u
c1620 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
c1630 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 ime(void){ retur
c1640 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 n ((sqlite_uint6
c1650 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 4)0); }..#endif.
c1660 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
c1670 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 ned(_HWTIME_H_)
c1680 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
c1690 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d *** End of hwtim
c16a0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
c16b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c16c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c16d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
c16e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
c16f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
c1700 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 in os_common.h
c1710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1720 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 **/..static sqli
c1730 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 te_uint64 g_star
c1740 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 t;.static sqlite
c1750 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 _uint64 g_elapse
c1760 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 d;.#define TIMER
c1770 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 _START g_s
c1780 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 tart=sqlite3Hwti
c1790 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d me().#define TIM
c17a0 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 ER_END g
c17b0 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 _elapsed=sqlite3
c17c0 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 Hwtime()-g_start
c17d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
c17e0 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 LAPSED g_ela
c17f0 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 psed.#else.#defi
c1800 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 ne TIMER_START.#
c1810 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 define TIMER_END
c1820 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
c1830 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c LAPSED ((sql
c1840 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 ite_uint64)0).#e
c1850 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 ndif../*.** If w
c1860 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 e compile with t
c1870 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d he SQLITE_TEST m
c1880 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 acro set, then t
c1890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
c18a0 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 ck.** of code wi
c18b0 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61 ll give us the a
c18c0 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 bility to simula
c18d0 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 te a disk I/O er
c18e0 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 ror. This.** is
c18f0 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
c1900 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 g the I/O recove
c1910 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 ry logic..*/.#if
c1920 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
c1930 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c1940 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c1950 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 hit = 0;
c1960 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
c1970 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 ber of I/O Error
c1980 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 s */.SQLITE_API
c1990 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
c19a0 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 rror_hardhit = 0
c19b0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
c19c0 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e er of non-benign
c19d0 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 errors */.SQLIT
c19e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c19f0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
c1a00 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f ng = 0; /
c1a10 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 * Count down to
c1a20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 first I/O error
c1a30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
c1a40 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
c1a50 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 or_persist = 0;
c1a60 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
c1a70 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 f I/O errors per
c1a80 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 sist */.SQLITE_A
c1a90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
c1aa0 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d o_error_benign =
c1ab0 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 0; /* T
c1ac0 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 rue if errors ar
c1ad0 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 e benign */.SQLI
c1ae0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c1af0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
c1b00 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f ing = 0;.SQLITE_
c1b10 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c1b20 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 diskfull = 0;.#d
c1b30 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
c1b40 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 ErrorBenign(X) s
c1b50 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c1b60 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 benign=(X).#defi
c1b70 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
c1b80 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 or(CODE) \. if
c1b90 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ( (sqlite3_io_er
c1ba0 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 ror_persist && s
c1bb0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c1bc0 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c hit) \. ||
c1bd0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c1be0 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 r_pending-- == 1
c1bf0 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 ) \.
c1c00 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 { local_ioer
c1c10 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 r(); CODE; }.sta
c1c20 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 tic void local_i
c1c30 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 oerr(){. IOTRAC
c1c40 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a E(("IOERR\n"));.
c1c50 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
c1c60 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 or_hit++;. if(
c1c70 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f !sqlite3_io_erro
c1c80 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 r_benign ) sqlit
c1c90 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 e3_io_error_hard
c1ca0 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 hit++;.}.#define
c1cb0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
c1cc0 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 lError(CODE) \.
c1cd0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 if( sqlite3_di
c1ce0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 skfull_pending )
c1cf0 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c { \. if( sql
c1d00 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
c1d10 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a nding == 1 ){ \.
c1d20 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 local_ioe
c1d30 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 rr(); \. s
c1d40 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 qlite3_diskfull
c1d50 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 = 1; \. sq
c1d60 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
c1d70 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 it = 1; \.
c1d80 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 CODE; \. }e
c1d90 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 lse{ \. sq
c1da0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
c1db0 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 ending--; \.
c1dc0 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a } \. }.#else.
c1dd0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
c1de0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 IOErrorBenign(X)
c1df0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
c1e00 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 eIOError(A).#def
c1e10 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ine SimulateDisk
c1e20 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e fullError(A).#en
c1e30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 dif../*.** When
c1e40 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 testing, keep a
c1e50 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d count of the num
c1e60 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 ber of open file
c1e70 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c s..*/.#ifdef SQL
c1e80 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
c1e90 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c1ea0 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 open_file_count
c1eb0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 = 0;.#define Ope
c1ec0 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c nCounter(X) sql
c1ed0 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 ite3_open_file_c
c1ee0 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a ount+=(X).#else.
c1ef0 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e #define OpenCoun
c1f00 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 ter(X).#endif..#
c1f10 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
c1f20 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 d(_OS_COMMON_H_)
c1f30 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
c1f40 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 **** End of os_c
c1f50 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
c1f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1f80 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c1f90 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
c1fa0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
c1fb0 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a f in os_unix.c *
c1fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1fd0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 ***/../*.** Defi
c1fe0 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f ne various macro
c1ff0 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 69 s that are missi
c2000 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 ng from some sys
c2010 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tems..*/.#ifndef
c2020 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 O_LARGEFILE.# d
c2030 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c efine O_LARGEFIL
c2040 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 E 0.#endif.#ifde
c2050 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 f SQLITE_DISABLE
c2060 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c _LFS.# undef O_L
c2070 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e ARGEFILE.# defin
c2080 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a e O_LARGEFILE 0.
c2090 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f #endif.#ifndef O
c20a0 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 _NOFOLLOW.# defi
c20b0 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a ne O_NOFOLLOW 0.
c20c0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f #endif.#ifndef O
c20d0 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 _BINARY.# define
c20e0 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 O_BINARY 0.#end
c20f0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a if../*.** The DJ
c2100 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 GPP compiler env
c2110 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d ironment looks m
c2120 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c ostly like Unix,
c2130 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 but it.** lacks
c2140 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 the fcntl() sys
c2150 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 tem call. So re
c2160 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 define fcntl() t
c2170 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a o be something.*
c2180 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 75 * that always su
c2190 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65 cceeds. This me
c21a0 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 ans that locking
c21b0 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 does not occur
c21c0 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 under.** DJGPP.
c21d0 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d But it is DOS -
c21e0 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 78 what did you ex
c21f0 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 pect?.*/.#ifdef
c2200 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 __DJGPP__.# defi
c2210 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 ne fcntl(A,B,C)
c2220 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
c2230 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 The threadid mac
c2240 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 ro resolves to t
c2250 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 he thread-id or
c2260 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a to 0. Used for.
c2270 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 ** testing and d
c2280 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a ebugging only..*
c2290 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 /.#if SQLITE_THR
c22a0 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 EADSAFE.#define
c22b0 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61 64 threadid pthread
c22c0 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 _self().#else.#d
c22d0 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 efine threadid 0
c22e0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
c22f0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 Helper functions
c2300 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 to obtain and r
c2310 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c elinquish the gl
c2320 6f 62 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 obal mutex..*/.s
c2330 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 45 tatic void unixE
c2340 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29 7b nterMutex(void){
c2350 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
c2360 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 _enter(sqlite3Mu
c2370 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
c2380 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
c2390 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 TER));.}.static
c23a0 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d 75 void unixLeaveMu
c23b0 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c tex(void){. sql
c23c0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
c23d0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
c23e0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
c23f0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b STATIC_MASTER));
c2400 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 .}...#ifdef SQLI
c2410 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 TE_DEBUG./*.** H
c2420 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 elper function f
c2430 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 or printing out
c2440 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f trace informatio
c2450 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 n from debugging
c2460 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 .** binaries. Th
c2470 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 is returns the s
c2480 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 tring represetat
c2490 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c ion of the suppl
c24a0 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c ied.** integer l
c24b0 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 ock-type..*/.sta
c24c0 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
c24d0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 locktypeName(int
c24e0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 locktype){. sw
c24f0 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 itch( locktype )
c2500 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b {. case NO_LOCK
c2510 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b : return "NONE";
c2520 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c . case SHARED_L
c2530 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 OCK: return "SHA
c2540 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53 RED";. case RES
c2550 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 ERVED_LOCK: retu
c2560 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 rn "RESERVED";.
c2570 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f case PENDING_LO
c2580 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44 CK: return "PEND
c2590 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43 ING";. case EXC
c25a0 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74 LUSIVE_LOCK: ret
c25b0 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b urn "EXCLUSIVE";
c25c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45 . }. return "E
c25d0 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a RROR";.}.#endif.
c25e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c .#ifdef SQLITE_L
c25f0 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 OCK_TRACE./*.**
c2600 50 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d Print out inform
c2610 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 ation about all
c2620 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f locking operatio
c2630 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ns..**.** This r
c2640 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 outine is used f
c2650 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 or troubleshooti
c2660 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 ng locks on mult
c2670 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 ithreaded.** pla
c2680 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 tforms. Enable
c2690 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 by compiling wit
c26a0 68 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c h the -DSQLITE_L
c26b0 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d OCK_TRACE.** com
c26c0 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e mand-line option
c26d0 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 on the compiler
c26e0 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 . This code is
c26f0 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e normally.** turn
c2700 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 ed off..*/.stati
c2710 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 c int lockTrace(
c2720 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 int fd, int op,
c2730 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 struct flock *p)
c2740 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d {. char *zOpNam
c2750 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 e, *zType;. int
c2760 20 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 s;. int savedE
c2770 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d rrno;. if( op==
c2780 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a F_GETLK ){. z
c2790 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 OpName = "GETLK"
c27a0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 ;. }else if( op
c27b0 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 ==F_SETLK ){.
c27c0 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c zOpName = "SETL
c27d0 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 K";. }else{.
c27e0 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f s = fcntl(fd, o
c27f0 70 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 p, p);. sqlit
c2800 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 e3DebugPrintf("f
c2810 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 cntl unknown %d
c2820 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 %d %d\n", fd, op
c2830 2c 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e , s);. return
c2840 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d s;. }. if( p-
c2850 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b >l_type==F_RDLCK
c2860 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 ){. zType =
c2870 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 "RDLCK";. }else
c2880 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d if( p->l_type==
c2890 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a F_WRLCK ){. z
c28a0 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a Type = "WRLCK";.
c28b0 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c }else if( p->l
c28c0 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 _type==F_UNLCK )
c28d0 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 {. zType = "U
c28e0 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a NLCK";. }else{.
c28f0 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b assert( 0 );
c2900 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
c2910 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b ->l_whence==SEEK
c2920 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 _SET );. s = fc
c2930 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a ntl(fd, op, p);.
c2940 20 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 savedErrno = e
c2950 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 rrno;. sqlite3D
c2960 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 ebugPrintf("fcnt
c2970 6c 20 25 64 20 25 64 20 25 73 20 25 73 20 25 64 l %d %d %s %s %d
c2980 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 %d %d %d\n",.
c2990 20 20 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c threadid, fd,
c29a0 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c zOpName, zType,
c29b0 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 (int)p->l_start
c29c0 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c , (int)p->l_len,
c29d0 0a 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f . (int)p->l_
c29e0 70 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 pid, s);. if( s
c29f0 3d 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f ==(-1) && op==F_
c2a00 53 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 SETLK && (p->l_t
c2a10 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 ype==F_RDLCK ||
c2a20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c p->l_type==F_WRL
c2a30 43 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 CK) ){. struc
c2a40 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 t flock l2;.
c2a50 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e l2 = *p;. fcn
c2a60 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 tl(fd, F_GETLK,
c2a70 26 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 &l2);. if( l2
c2a80 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b .l_type==F_RDLCK
c2a90 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 ){. zType
c2aa0 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d = "RDLCK";. }
c2ab0 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 else if( l2.l_ty
c2ac0 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 pe==F_WRLCK ){.
c2ad0 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 zType = "WR
c2ae0 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 LCK";. }else
c2af0 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 if( l2.l_type==F
c2b00 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 _UNLCK ){.
c2b10 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b zType = "UNLCK";
c2b20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
c2b30 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 assert( 0 );.
c2b40 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
c2b50 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e DebugPrintf("fcn
c2b60 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f tl-failure-reaso
c2b70 6e 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e n: %s %d %d %d\n
c2b80 22 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c ",. zType,
c2b90 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 (int)l2.l_start
c2ba0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c , (int)l2.l_len,
c2bb0 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b (int)l2.l_pid);
c2bc0 0a 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 . }. errno = s
c2bd0 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 avedErrno;. ret
c2be0 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 urn s;.}.#define
c2bf0 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 fcntl lockTrace
c2c00 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
c2c10 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a E_LOCK_TRACE */.
c2c20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f .../*.** This ro
c2c30 75 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73 utine translates
c2c40 20 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49 a standard POSI
c2c50 58 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74 X errno code int
c2c60 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75 o something.** u
c2c70 73 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 seful to the cli
c2c80 65 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 ents of the sqli
c2c90 74 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 te3 functions.
c2ca0 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 Specifically, it
c2cb0 20 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 is.** intended
c2cc0 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 to translate a v
c2cd0 61 72 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 ariety of "try a
c2ce0 67 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 gain" errors int
c2cf0 6f 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a o SQLITE_BUSY.**
c2d00 20 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f and a variety o
c2d10 66 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 f "please close
c2d20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
c2d30 74 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 tor NOW" errors
c2d40 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f into .** SQLITE_
c2d50 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72 IOERR.** .** Err
c2d60 6f 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69 ors during initi
c2d70 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63 alization of loc
c2d80 6b 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74 ks, or file syst
c2d90 65 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c em support for l
c2da0 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 ocks,.** should
c2db0 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45 handle ENOLCK, E
c2dc0 4e 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55 NOTSUP, EOPNOTSU
c2dd0 50 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a PP separately..*
c2de0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
c2df0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
c2e00 78 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78 xError(int posix
c2e10 45 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74 Error, int sqlit
c2e20 65 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74 eIOErr) {. swit
c2e30 63 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20 ch (posixError)
c2e40 7b 0a 20 20 63 61 73 65 20 30 3a 20 0a 20 20 20 {. case 0: .
c2e50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c2e60 4b 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 K;. . case E
c2e70 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54 AGAIN:. case ET
c2e80 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20 IMEDOUT:. case
c2e90 45 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49 EBUSY:. case EI
c2ea0 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c NTR:. case ENOL
c2eb0 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e CK: . /* ran
c2ec0 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72 dom NFS retry er
c2ed0 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 ror, unless duri
c2ee0 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 ng file system s
c2ef0 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 upport . * i
c2f00 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e ntrospection, in
c2f10 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c which it actual
c2f20 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 ly means what it
c2f30 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74 says */. ret
c2f40 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
c2f50 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 43 . . case EAC
c2f60 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43 CES: . /* EAC
c2f70 43 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41 CES is like EAGA
c2f80 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e IN during lockin
c2f90 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75 g operations, bu
c2fa0 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20 t not any other
c2fb0 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28 time*/. if( (
c2fc0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 sqliteIOErr == S
c2fd0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
c2fe0 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f ) || ..(sqliteIO
c2ff0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f Err == SQLITE_IO
c3000 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20 0a ERR_UNLOCK) || .
c3010 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d .(sqliteIOErr ==
c3020 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 SQLITE_IOERR_RD
c3030 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69 74 LOCK) ||..(sqlit
c3040 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 eIOErr == SQLITE
c3050 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
c3060 52 56 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 RVEDLOCK) ){.
c3070 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
c3080 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 _BUSY;. }.
c3090 20 2f 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74 68 /* else fall th
c30a0 72 6f 75 67 68 20 2a 2f 0a 20 20 63 61 73 65 20 rough */. case
c30b0 45 50 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 75 EPERM: . retu
c30c0 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a rn SQLITE_PERM;.
c30d0 20 20 20 20 0a 20 20 63 61 73 65 20 45 44 45 41 . case EDEA
c30e0 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e 20 DLK:. return
c30f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f SQLITE_IOERR_BLO
c3100 43 4b 45 44 3b 0a 20 20 20 20 0a 23 69 66 20 45 CKED;. .#if E
c3110 4f 50 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54 53 OPNOTSUPP!=ENOTS
c3120 55 50 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f 54 UP. case EOPNOT
c3130 53 55 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73 6f SUPP: . /* so
c3140 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 74 65 72 mething went ter
c3150 72 69 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c 65 ribly awry, unle
c3160 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 ss during file s
c3170 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 ystem support .
c3180 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 * introspect
c3190 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 ion, in which it
c31a0 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 actually means
c31b0 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a what it says */.
c31c0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 45 4e #endif.#ifdef EN
c31d0 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4e 4f OTSUP. case ENO
c31e0 54 53 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69 6e TSUP: . /* in
c31f0 76 61 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73 73 valid fd, unless
c3200 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 during file sys
c3210 74 65 6d 20 73 75 70 70 6f 72 74 20 69 6e 74 72 tem support intr
c3220 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 ospection, in wh
c3230 69 63 68 20 0a 20 20 20 20 20 2a 20 69 74 20 61 ich . * it a
c3240 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 ctually means wh
c3250 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65 at it says */.#e
c3260 6e 64 69 66 0a 20 20 63 61 73 65 20 45 49 4f 3a ndif. case EIO:
c3270 0a 20 20 63 61 73 65 20 45 42 41 44 46 3a 0a 20 . case EBADF:.
c3280 20 63 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20 20 case EINVAL:.
c3290 63 61 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a 20 case ENOTCONN:.
c32a0 20 63 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20 20 case ENODEV:.
c32b0 63 61 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63 61 case ENXIO:. ca
c32c0 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61 73 se ENOENT:. cas
c32d0 65 20 45 53 54 41 4c 45 3a 0a 20 20 63 61 73 65 e ESTALE:. case
c32e0 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a 20 ENOSYS:. /*
c32f0 74 68 65 73 65 20 73 68 6f 75 6c 64 20 66 6f 72 these should for
c3300 63 65 20 74 68 65 20 63 6c 69 65 6e 74 20 74 6f ce the client to
c3310 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 close the file
c3320 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a 2f and reconnect */
c3330 0a 20 20 20 20 0a 20 20 64 65 66 61 75 6c 74 3a . . default:
c3340 20 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c . return sql
c3350 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d 0a iteIOErr;. }.}.
c3360 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .../************
c3370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c33a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c33b0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
c33c0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 71 ***** Begin Uniq
c33d0 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 ue File ID Utili
c33e0 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 ty Used By VxWor
c33f0 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ks *************
c3400 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 74 **.**.** On most
c3410 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 versions of uni
c3420 78 2c 20 77 65 20 63 61 6e 20 67 65 74 20 61 20 x, we can get a
c3430 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20 61 20 unique ID for a
c3440 66 69 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 6e file by concaten
c3450 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 76 ating.** the dev
c3460 69 63 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 ice number and t
c3470 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e he inode number.
c3480 20 20 42 75 74 20 74 68 69 73 20 64 6f 65 73 20 But this does
c3490 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f not work on VxWo
c34a0 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f 72 rks..** On VxWor
c34b0 6b 73 2c 20 61 20 75 6e 69 71 75 65 20 66 69 6c ks, a unique fil
c34c0 65 20 69 64 20 6d 75 73 74 20 62 65 20 62 61 73 e id must be bas
c34d0 65 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e 69 ed on the canoni
c34e0 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a 2a cal filename..**
c34f0 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f .** A pointer to
c3500 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
c3510 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
c3520 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 ructure can be u
c3530 73 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 71 sed as a.** uniq
c3540 75 65 20 66 69 6c 65 20 49 44 20 69 6e 20 56 78 ue file ID in Vx
c3550 57 6f 72 6b 73 2e 20 20 45 61 63 68 20 69 6e 73 Works. Each ins
c3560 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
c3570 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
c3580 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 .** a copy of th
c3590 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 e canonical file
c35a0 6e 61 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 name. There is
c35b0 61 6c 73 6f 20 61 20 72 65 66 65 72 65 6e 63 65 also a reference
c35c0 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 65 count. .** The
c35d0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 structure is re
c35e0 63 6c 61 69 6d 65 64 20 77 68 65 6e 20 74 68 65 claimed when the
c35f0 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 number of point
c3600 65 72 73 20 74 6f 20 69 74 20 64 72 6f 70 73 20 ers to it drops
c3610 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a to.** zero..**.*
c3620 2a 20 54 68 65 72 65 20 61 72 65 20 6e 65 76 65 * There are neve
c3630 72 20 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65 r very many file
c3640 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69 s open at one ti
c3650 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61 me and lookups a
c3660 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 66 re not.** a perf
c3670 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c ormance-critical
c3680 20 70 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20 path, so it is
c3690 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75 sufficient to pu
c36a0 74 20 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 63 t these.** struc
c36b0 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b 65 tures on a linke
c36c0 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 d list..*/.struc
c36d0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
c36e0 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 {. struct vxwor
c36f0 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 3b ksFileId *pNext;
c3700 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 6c /* Next in a l
c3710 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 ist of them all
c3720 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
c3730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3740 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
c3750 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
c3760 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 is one */. int
c3770 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 nName;
c3780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e /* Len
c3790 67 74 68 20 6f 66 20 74 68 65 20 7a 43 61 6e 6f gth of the zCano
c37a0 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 69 nicalName[] stri
c37b0 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 ng */. char *zC
c37c0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 20 anonicalName;
c37d0 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 63 /* Canonic
c37e0 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 7d al filename */.}
c37f0 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b ;..#if OS_VXWORK
c3800 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e 69 S./* .** All uni
c3810 71 75 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72 que filenames ar
c3820 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b e held on a link
c3830 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 ed list headed b
c3840 79 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 y this.** variab
c3850 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 le:.*/.static st
c3860 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
c3870 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c Id *vxworksFileL
c3880 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 ist = 0;../*.**
c3890 53 69 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 6e Simplify a filen
c38a0 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 63 61 6e ame into its can
c38b0 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 62 onical form.** b
c38c0 79 20 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f 6c y making the fol
c38d0 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 3a 0a lowing changes:.
c38e0 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 6e **.** * removin
c38f0 67 20 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 61 g any trailing a
c3900 6e 64 20 64 75 70 6c 69 63 61 74 65 20 2f 0a 2a nd duplicate /.*
c3910 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e 2f * * convert /./
c3920 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 20 into just /.**
c3930 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e 2e * convert /A/..
c3940 2f 20 77 68 65 72 65 20 41 20 69 73 20 61 6e 79 / where A is any
c3950 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e 74 simple name int
c3960 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 43 o just /.**.** C
c3970 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 hanges are made
c3980 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 72 in-place. Retur
c3990 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 6c n the new name l
c39a0 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ength..**.** The
c39b0 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e 61 original filena
c39c0 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e 2d me is in z[0..n-
c39d0 31 5d 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 1]. Return the
c39e0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 number of.** cha
c39f0 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 73 racters in the s
c3a00 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e 0a implified name..
c3a10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78 */.static int vx
c3a20 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d worksSimplifyNam
c3a30 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e e(char *z, int n
c3a40 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 ){. int i, j;.
c3a50 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a while( n>1 && z
c3a60 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d [n-1]=='/' ){ n-
c3a70 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 -; }. for(i=j=0
c3a80 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 ; i<n; i++){.
c3a90 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 29 if( z[i]=='/' )
c3aa0 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b {. if( z[i+
c3ab0 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 1]=='/' ) contin
c3ac0 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ue;. if( z[
c3ad0 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 32 i+1]=='.' && i+2
c3ae0 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2f <n && z[i+2]=='/
c3af0 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b ' ){. i +
c3b00 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e = 1;. con
c3b10 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 tinue;. }.
c3b20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d if( z[i+1]=
c3b30 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 26 ='.' && i+3<n &&
c3b40 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 20 z[i+2]=='.' &&
c3b50 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 z[i+3]=='/' ){.
c3b60 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e while( j>
c3b70 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f 27 0 && z[j-1]!='/'
c3b80 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 ){ j--; }.
c3b90 20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a 2d if( j>0 ){ j-
c3ba0 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 20 2b -; }. i +
c3bb0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e = 2;. con
c3bc0 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 tinue;. }.
c3bd0 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20 }. z[j++]
c3be0 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a 5b = z[i];. }. z[
c3bf0 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e j] = 0;. return
c3c00 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e j;.}../*.** Fin
c3c10 64 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 d a unique file
c3c20 49 44 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e ID for the given
c3c30 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 6e 61 absolute pathna
c3c40 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 me. Return.** a
c3c50 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
c3c60 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 vxworksFileId ob
c3c70 6a 65 63 74 2e 20 20 54 68 69 73 20 70 6f 69 6e ject. This poin
c3c80 74 65 72 20 69 73 20 74 68 65 20 75 6e 69 71 75 ter is the uniqu
c3c90 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a 2a e.** file ID..**
c3ca0 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20 66 69 65 .** The nRef fie
c3cb0 6c 64 20 6f 66 20 74 68 65 20 76 78 77 6f 72 6b ld of the vxwork
c3cc0 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 sFileId object i
c3cd0 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 65 s incremented be
c3ce0 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 fore.** the obje
c3cf0 63 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 ct is returned.
c3d00 20 41 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 69 A new vxworksFi
c3d10 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20 63 leId object is c
c3d20 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 64 reated.** and ad
c3d30 64 65 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 ded to the globa
c3d40 6c 20 6c 69 73 74 20 69 66 20 6e 65 63 65 73 73 l list if necess
c3d50 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ary..**.** If a
c3d60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
c3d70 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
c3d80 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a return NULL..*/.
c3d90 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78 static struct vx
c3da0 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 worksFileId *vxw
c3db0 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63 orksFindFileId(c
c3dc0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f onst char *zAbso
c3dd0 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 72 luteName){. str
c3de0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
c3df0 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 d *pNew;
c3e00 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 20 61 /* search key a
c3e10 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a nd new file ID *
c3e20 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 /. struct vxwor
c3e30 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 69 ksFileId *pCandi
c3e40 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 6c date; /* For l
c3e50 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 ooping over exis
c3e60 74 69 6e 67 20 66 69 6c 65 20 49 44 73 20 2a 2f ting file IDs */
c3e70 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 . int n;
c3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3e90 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
c3ea0 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d of zAbsoluteNam
c3eb0 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 61 e string */.. a
c3ec0 73 73 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 65 ssert( zAbsolute
c3ed0 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a Name[0]=='/' );.
c3ee0 20 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 n = (int)strle
c3ef0 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 n(zAbsoluteName)
c3f00 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 ;. pNew = sqlit
c3f10 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f e3_malloc( sizeo
c3f20 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 29 f(*pNew) + (n+1)
c3f30 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d );. if( pNew==
c3f40 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
c3f50 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c pNew->zCanonical
c3f60 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 Name = (char*)&p
c3f70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 New[1];. memcpy
c3f80 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 (pNew->zCanonica
c3f90 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 65 lName, zAbsolute
c3fa0 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e 20 Name, n+1);. n
c3fb0 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 = vxworksSimplif
c3fc0 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 6e yName(pNew->zCan
c3fd0 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b 0a onicalName, n);.
c3fe0 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 . /* Search for
c3ff0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74 an existing ent
c4000 72 79 20 74 68 61 74 20 6d 61 74 63 68 69 6e 67 ry that matching
c4010 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e the canonical n
c4020 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f 75 ame.. ** If fou
c4030 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 nd, increment th
c4040 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
c4050 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 t and return a p
c4060 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 74 ointer to. ** t
c4070 68 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 he existing file
c4080 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 ID.. */. unix
c4090 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
c40a0 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65 3d 76 for(pCandidate=v
c40b0 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 xworksFileList;
c40c0 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43 61 6e pCandidate; pCan
c40d0 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64 61 74 didate=pCandidat
c40e0 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 e->pNext){. i
c40f0 66 28 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e f( pCandidate->n
c4100 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 26 Name==n . &&
c4110 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 61 memcmp(pCandida
c4120 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 te->zCanonicalNa
c4130 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e me, pNew->zCanon
c4140 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 0a icalName, n)==0.
c4150 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 73 71 ){. sq
c4160 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 lite3_free(pNew)
c4170 3b 0a 20 20 20 20 20 20 20 70 43 61 6e 64 69 64 ;. pCandid
c4180 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 ate->nRef++;.
c4190 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
c41a0 65 78 28 29 3b 0a 20 20 20 20 20 20 20 72 65 74 ex();. ret
c41b0 75 72 6e 20 70 43 61 6e 64 69 64 61 74 65 3b 0a urn pCandidate;.
c41c0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
c41d0 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 66 6f 75 No match was fou
c41e0 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b nd. We will mak
c41f0 65 20 61 20 6e 65 77 20 66 69 6c 65 20 49 44 20 e a new file ID
c4200 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 */. pNew->nRef
c4210 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61 = 1;. pNew->nNa
c4220 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d 3e me = n;. pNew->
c4230 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46 pNext = vxworksF
c4240 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f 72 ileList;. vxwor
c4250 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e 65 ksFileList = pNe
c4260 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 w;. unixLeaveMu
c4270 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 tex();. return
c4280 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 pNew;.}../*.** D
c4290 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 ecrement the ref
c42a0 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 erence count on
c42b0 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 a vxworksFileId
c42c0 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a 2a object. Free.**
c42d0 20 74 68 65 20 6f 62 6a 65 63 74 20 77 68 65 6e the object when
c42e0 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
c42f0 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 ount reaches zer
c4300 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 o..*/.static voi
c4310 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 d vxworksRelease
c4320 46 69 6c 65 49 64 28 73 74 72 75 63 74 20 76 78 FileId(struct vx
c4330 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 worksFileId *pId
c4340 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 ){. unixEnterMu
c4350 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 tex();. assert(
c4360 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a pId->nRef>0 );.
c4370 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 pId->nRef--;.
c4380 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d if( pId->nRef==
c4390 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 0 ){. struct
c43a0 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a vxworksFileId **
c43b0 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d 26 pp;. for(pp=&
c43c0 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b vxworksFileList;
c43d0 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64 *pp && *pp!=pId
c43e0 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e ; pp = &((*pp)->
c43f0 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 73 pNext)){}. as
c4400 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 29 sert( *pp==pId )
c4410 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 64 2d ;. *pp = pId-
c4420 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 >pNext;. sqli
c4430 74 65 33 5f 66 72 65 65 28 70 49 64 29 3b 0a 20 te3_free(pId);.
c4440 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 }. unixLeaveMu
c4450 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 tex();.}.#endif
c4460 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f /* OS_VXWORKS */
c4470 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c4480 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 20 * End of Unique
c4490 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 20 File ID Utility
c44a0 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 20 Used By VxWorks
c44b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c44c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
c44d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c44e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c44f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c4510 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .../************
c4520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c4560 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
c4570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50 ************** P
c4580 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f osix Advisory Lo
c4590 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a cking **********
c45a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c45b0 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 61 **.**.** POSIX a
c45c0 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72 dvisory locks ar
c45d0 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69 e broken by desi
c45e0 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30 gn. ANSI STD 10
c45f0 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 73 03.1 (1996).** s
c4600 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c ection 6.5.2.2 l
c4610 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67 68 ines 483 through
c4620 20 34 39 30 20 73 70 65 63 69 66 79 20 74 68 61 490 specify tha
c4630 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73 t when a process
c4640 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 61 .** sets or clea
c4650 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 20 rs a lock, that
c4660 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 69 operation overri
c4670 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c 6f des any prior lo
c4680 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 68 cks set.** by th
c4690 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 e same process.
c46a0 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 It does not exp
c46b0 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f 2c 20 licitly say so,
c46c0 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69 65 73 but this implies
c46d0 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 65 72 .** that it over
c46e0 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 20 rides locks set
c46f0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 by the same proc
c4700 65 73 73 20 75 73 69 6e 67 20 61 20 64 69 66 66 ess using a diff
c4710 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 erent.** file de
c4720 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 69 scriptor. Consi
c4730 64 65 72 20 74 68 69 73 20 74 65 73 74 20 63 61 der this test ca
c4740 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 se:.**.**
c4750 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 22 int fd1 = open("
c4760 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 52 ./file1", O_RDWR
c4770 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b |O_CREAT, 0644);
c4780 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 .** int fd
c4790 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 2 = open("./file
c47a0 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 2", O_RDWR|O_CRE
c47b0 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a AT, 0644);.**.**
c47c0 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 Suppose ./file1
c47d0 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 and ./file2 are
c47e0 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 really the same
c47f0 20 66 69 6c 65 20 28 62 65 63 61 75 73 65 0a 2a file (because.*
c4800 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72 64 20 * one is a hard
c4810 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b or symbolic link
c4820 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29 20 74 to the other) t
c4830 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74 0a 2a hen if you set.*
c4840 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c * an exclusive l
c4850 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e ock on fd1, then
c4860 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e 20 65 try to get an e
c4870 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a xclusive lock.**
c4880 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 6b on fd2, it work
c4890 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 76 65 s. I would have
c48a0 20 65 78 70 65 63 74 65 64 20 74 68 65 20 73 65 expected the se
c48b0 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 cond lock to.**
c48c0 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65 72 65 fail since there
c48d0 20 77 61 73 20 61 6c 72 65 61 64 79 20 61 20 6c was already a l
c48e0 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 ock on the file
c48f0 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 due to fd1..** B
c4900 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 ut not so. Sinc
c4910 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d e both locks cam
c4920 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 e from the same
c4930 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 process, the.**
c4940 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 second overrides
c4950 20 74 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e the first, even
c4960 20 74 68 6f 75 67 68 20 74 68 65 79 20 77 65 72 though they wer
c4970 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a e on different.*
c4980 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f * file descripto
c4990 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 rs opened on dif
c49a0 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 ferent file name
c49b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 s..**.** This me
c49c0 61 6e 73 20 74 68 61 74 20 77 65 20 63 61 6e 6e ans that we cann
c49d0 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c 6f 63 ot use POSIX loc
c49e0 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a ks to synchroniz
c49f0 65 20 66 69 6c 65 20 61 63 63 65 73 73 0a 2a 2a e file access.**
c4a00 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e 67 among competing
c4a10 20 74 68 72 65 61 64 73 20 6f 66 20 74 68 65 20 threads of the
c4a20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 50 same process. P
c4a30 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 OSIX locks will
c4a40 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20 work fine.** to
c4a50 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 65 synchronize acce
c4a60 73 73 20 66 6f 72 20 74 68 72 65 61 64 73 20 69 ss for threads i
c4a70 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 n separate proce
c4a80 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a sses, but not.**
c4a90 20 74 68 72 65 61 64 73 20 77 69 74 68 69 6e 20 threads within
c4aa0 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 the same process
c4ab0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 ..**.** To work
c4ac0 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 6c around the probl
c4ad0 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 20 74 em, SQLite has t
c4ae0 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f o manage file lo
c4af0 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a cks internally.*
c4b00 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57 * on its own. W
c4b10 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 64 61 henever a new da
c4b20 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
c4b30 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 69 6e , we have to fin
c4b40 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 d the.** specifi
c4b50 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 64 c inode of the d
c4b60 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 74 68 atabase file (th
c4b70 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74 65 72 e inode is deter
c4b80 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 mined by the.**
c4b90 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 6e st_dev and st_in
c4ba0 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 o fields of the
c4bb0 73 74 61 74 20 73 74 72 75 63 74 75 72 65 20 74 stat structure t
c4bc0 68 61 74 20 66 73 74 61 74 28 29 20 66 69 6c 6c hat fstat() fill
c4bd0 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65 s in).** and che
c4be0 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72 ck for locks alr
c4bf0 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 6f 6e eady existing on
c4c00 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 68 that inode. Wh
c4c10 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 en locks are.**
c4c20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d 6f 76 created or remov
c4c30 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6c ed, we have to l
c4c40 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 69 ook at our own i
c4c50 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f nternal record o
c4c60 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74 f the.** locks t
c4c70 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72 o see if another
c4c80 20 74 68 72 65 61 64 20 68 61 73 20 70 72 65 76 thread has prev
c4c90 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 iously set a loc
c4ca0 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a k on that same.*
c4cb0 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 28 * inode..**.** (
c4cc0 41 73 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f Aside: The use o
c4cd0 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 f inode numbers
c4ce0 61 73 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f as unique IDs do
c4cf0 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 es not work on V
c4d00 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 56 xWorks..** For V
c4d10 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 20 xWorks, we have
c4d20 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65 72 to use the alter
c4d30 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20 49 44 native unique ID
c4d40 20 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f 6e system based on
c4d50 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 .** canonical fi
c4d60 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c 65 lename and imple
c4d70 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 70 72 mented in the pr
c4d80 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f 6e 2e evious division.
c4d90 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ).**.** The sqli
c4da0 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 te3_file structu
c4db0 72 65 20 66 6f 72 20 50 4f 53 49 58 20 69 73 20 re for POSIX is
c4dc0 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 no longer just a
c4dd0 6e 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a n integer file.*
c4de0 2a 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 49 * descriptor. I
c4df0 74 20 69 73 20 6e 6f 77 20 61 20 73 74 72 75 63 t is now a struc
c4e00 74 75 72 65 20 74 68 61 74 20 68 6f 6c 64 73 20 ture that holds
c4e10 74 68 65 20 69 6e 74 65 67 65 72 20 66 69 6c 65 the integer file
c4e20 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 61 .** descriptor a
c4e30 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 nd a pointer to
c4e40 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 a structure that
c4e50 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69 describes the i
c4e60 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 nternal.** locks
c4e70 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f on the correspo
c4e80 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 nding inode. Th
c4e90 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 ere is one locki
c4ea0 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 ng structure.**
c4eb0 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 per inode, so if
c4ec0 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 the same inode
c4ed0 69 73 20 6f 70 65 6e 65 64 20 74 77 69 63 65 2c is opened twice,
c4ee0 20 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 73 both unixFile s
c4ef0 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 tructures.** poi
c4f00 6e 74 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6c nt to the same l
c4f10 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 ocking structure
c4f20 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 . The locking s
c4f30 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 0a 2a tructure keeps.*
c4f40 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f * a reference co
c4f50 75 6e 74 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 unt (so we will
c4f60 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 6c know when to del
c4f70 65 74 65 20 69 74 29 20 61 6e 64 20 61 20 22 63 ete it) and a "c
c4f80 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 nt".** field tha
c4f90 74 20 74 65 6c 6c 73 20 75 73 20 69 74 73 20 69 t tells us its i
c4fa0 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 nternal lock sta
c4fb0 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 tus. cnt==0 mea
c4fc0 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 ns the.** file i
c4fd0 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 s unlocked. cnt
c4fe0 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 66 ==-1 means the f
c4ff0 69 6c 65 20 68 61 73 20 61 6e 20 65 78 63 6c 75 ile has an exclu
c5000 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e sive lock..** cn
c5010 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 t>0 means there
c5020 61 72 65 20 63 6e 74 20 73 68 61 72 65 64 20 6c are cnt shared l
c5030 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 ocks on the file
c5040 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 ..**.** Any atte
c5050 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 mpt to lock or u
c5060 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72 nlock a file fir
c5070 73 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f st checks the lo
c5080 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 cking.** structu
c5090 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29 re. The fcntl()
c50a0 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 system call is
c50b0 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 only invoked to
c50c0 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 set a .** POSIX
c50d0 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65 lock if the inte
c50e0 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 rnal lock struct
c50f0 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 ure transitions
c5100 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 between.** a loc
c5110 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 ked and an unloc
c5120 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ked state..**.**
c5130 20 42 75 74 20 77 61 69 74 3a 20 20 74 68 65 72 But wait: ther
c5140 65 20 61 72 65 20 79 65 74 20 6d 6f 72 65 20 70 e are yet more p
c5150 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 4f 53 roblems with POS
c5160 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b IX advisory lock
c5170 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 s..**.** If you
c5180 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 close a file des
c5190 63 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 criptor that poi
c51a0 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 nts to a file th
c51b0 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a at has locks,.**
c51c0 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 all locks on th
c51d0 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 at file that are
c51e0 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 owned by the cu
c51f0 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 rrent process ar
c5200 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 e.** released.
c5210 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 To work around t
c5220 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 his problem, eac
c5230 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 h unixFile struc
c5240 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a ture contains.**
c5250 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
c5260 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 unixOpenCnt str
c5270 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 69 ucture. There i
c5280 73 20 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43 6e s one unixOpenCn
c5290 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 t structure.** p
c52a0 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 77 er open inode, w
c52b0 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 hich means that
c52c0 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 6c multiple unixFil
c52d0 65 20 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 61 e can point to a
c52e0 20 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78 4f single.** unixO
c52f0 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e penCnt. When an
c5300 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
c5310 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 to close an uni
c5320 78 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20 xFile, if there
c5330 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 are.** other uni
c5340 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 xFile open on th
c5350 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 e same inode tha
c5360 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f t are holding lo
c5370 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a cks, the call.**
c5380 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 to close() the
c5390 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
c53a0 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 is deferred unti
c53b0 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 l all of the loc
c53c0 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65 ks clear..** The
c53d0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 unixOpenCnt str
c53e0 75 63 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c ucture keeps a l
c53f0 69 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63 ist of file desc
c5400 72 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65 riptors that nee
c5410 64 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 d to.** be close
c5420 64 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20 d and that list
c5430 69 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63 is walked (and c
c5440 6c 65 61 72 65 64 29 20 77 68 65 6e 20 74 68 65 leared) when the
c5450 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c last lock.** cl
c5460 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74 20 ears..**.** Yet
c5470 61 6e 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d 3a another problem:
c5480 20 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 64 LinuxThreads d
c5490 6f 20 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c 20 o not play well
c54a0 77 69 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b 73 with posix locks
c54b0 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c 64 ..**.** Many old
c54c0 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c er versions of l
c54d0 69 6e 75 78 20 75 73 65 20 74 68 65 20 4c 69 6e inux use the Lin
c54e0 75 78 54 68 72 65 61 64 73 20 6c 69 62 72 61 72 uxThreads librar
c54f0 79 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e 6f y which is.** no
c5500 74 20 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61 6e t posix complian
c5510 74 2e 20 20 55 6e 64 65 72 20 4c 69 6e 75 78 54 t. Under LinuxT
c5520 68 72 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20 63 hreads, a lock c
c5530 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64 reated by thread
c5540 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65 20 .** A cannot be
c5550 6d 6f 64 69 66 69 65 64 20 6f 72 20 6f 76 65 72 modified or over
c5560 72 69 64 64 65 6e 20 62 79 20 61 20 64 69 66 66 ridden by a diff
c5570 65 72 65 6e 74 20 74 68 72 65 61 64 20 42 2e 0a erent thread B..
c5580 2a 2a 20 4f 6e 6c 79 20 74 68 72 65 61 64 20 41 ** Only thread A
c5590 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 can modify the
c55a0 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20 62 lock. Locking b
c55b0 65 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72 65 ehavior is corre
c55c0 63 74 0a 2a 2a 20 69 66 20 74 68 65 20 61 70 70 ct.** if the app
c55d0 6c 69 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 liation uses the
c55e0 20 6e 65 77 65 72 20 4e 61 74 69 76 65 20 50 6f newer Native Po
c55f0 73 69 78 20 54 68 72 65 61 64 20 4c 69 62 72 61 six Thread Libra
c5600 72 79 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e 20 ry (NPTL).** on
c5610 6c 69 6e 75 78 20 2d 20 77 69 74 68 20 4e 50 54 linux - with NPT
c5620 4c 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 L a lock created
c5630 20 62 79 20 74 68 72 65 61 64 20 41 20 63 61 6e by thread A can
c5640 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a override locks.
c5650 2a 2a 20 69 6e 20 74 68 72 65 61 64 20 42 2e 20 ** in thread B.
c5660 20 42 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f But there is no
c5670 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 20 way to know at
c5680 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 69 compile-time whi
c5690 63 68 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 20 ch.** threading
c56a0 6c 69 62 72 61 72 79 20 69 73 20 62 65 69 6e 67 library is being
c56b0 20 75 73 65 64 2e 20 20 53 6f 20 74 68 65 72 65 used. So there
c56c0 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e is no way to kn
c56d0 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 ow at.** compile
c56e0 2d 74 69 6d 65 20 77 68 65 74 68 65 72 20 6f 72 -time whether or
c56f0 20 6e 6f 74 20 74 68 72 65 61 64 20 41 20 63 61 not thread A ca
c5700 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 n override locks
c5710 20 6f 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a on thread B..**
c5720 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 61 We have to do a
c5730 20 72 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b 20 run-time check
c5740 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 to discover the
c5750 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 0a behavior of the.
c5760 2a 2a 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 ** current proce
c5770 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79 73 ss..**.** On sys
c5780 74 65 6d 73 20 77 68 65 72 65 20 74 68 72 65 61 tems where threa
c5790 64 20 41 20 69 73 20 75 6e 61 62 6c 65 20 74 6f d A is unable to
c57a0 20 6d 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63 72 modify locks cr
c57b0 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 72 65 eated by.** thre
c57c0 61 64 20 42 2c 20 77 65 20 68 61 76 65 20 74 6f ad B, we have to
c57d0 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 77 keep track of w
c57e0 68 69 63 68 20 74 68 72 65 61 64 20 63 72 65 61 hich thread crea
c57f0 74 65 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63 6b ted each.** lock
c5800 2e 20 20 48 65 6e 63 65 20 74 68 65 72 65 20 69 . Hence there i
c5810 73 20 61 6e 20 65 78 74 72 61 20 66 69 65 6c 64 s an extra field
c5820 20 69 6e 20 74 68 65 20 6b 65 79 20 74 6f 20 74 in the key to t
c5830 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 0a he unixLockInfo.
c5840 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 ** structure to
c5850 72 65 63 6f 72 64 20 74 68 69 73 20 69 6e 66 6f record this info
c5860 72 6d 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f 6e rmation. And on
c5870 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73 20 69 those systems i
c5880 74 0a 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c 20 t.** is illegal
c5890 74 6f 20 62 65 67 69 6e 20 61 20 74 72 61 6e 73 to begin a trans
c58a0 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 action in one th
c58b0 72 65 61 64 20 61 6e 64 20 66 69 6e 69 73 68 20 read and finish
c58c0 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65 72 it.** in another
c58d0 2e 20 20 46 6f 72 20 74 68 69 73 20 6c 61 74 74 . For this latt
c58e0 65 72 20 72 65 73 74 72 69 63 74 69 6f 6e 2c 20 er restriction,
c58f0 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b there is no work
c5900 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20 69 -around..** It i
c5910 73 20 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20 6f s a limitation o
c5920 66 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a f LinuxThreads..
c5930 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 */../*.** Set or
c5940 20 63 68 65 63 6b 20 74 68 65 20 75 6e 69 78 46 check the unixF
c5950 69 6c 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 20 ile.tid field.
c5960 54 68 69 73 20 66 69 65 6c 64 20 69 73 20 73 65 This field is se
c5970 74 20 77 68 65 6e 20 61 6e 20 75 6e 69 78 46 69 t when an unixFi
c5980 6c 65 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 6f le.** is first o
c5990 70 65 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 73 pened. All subs
c59a0 65 71 75 65 6e 74 20 75 73 65 73 20 6f 66 20 74 equent uses of t
c59b0 68 65 20 75 6e 69 78 46 69 6c 65 20 76 65 72 69 he unixFile veri
c59c0 66 79 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 fy that the.** s
c59d0 61 6d 65 20 74 68 72 65 61 64 20 69 73 20 6f 70 ame thread is op
c59e0 65 72 61 74 69 6e 67 20 6f 6e 20 74 68 65 20 75 erating on the u
c59f0 6e 69 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20 6f nixFile. Some o
c5a00 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 perating systems
c5a10 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f 77 do.** not allow
c5a20 20 6c 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76 65 locks to be ove
c5a30 72 72 69 64 64 65 6e 20 62 79 20 6f 74 68 65 72 rridden by other
c5a40 20 74 68 72 65 61 64 73 20 61 6e 64 20 74 68 61 threads and tha
c5a50 74 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a t restriction.**
c5a60 20 6d 65 61 6e 73 20 74 68 61 74 20 73 71 6c 69 means that sqli
c5a70 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 te3* database ha
c5a80 6e 64 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 ndles cannot be
c5a90 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 moved from one t
c5aa0 68 72 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f 74 hread.** to anot
c5ab0 68 65 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73 20 her while locks
c5ac0 61 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 are held..**.**
c5ad0 56 65 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28 32 Version 3.3.1 (2
c5ae0 30 30 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e 69 006-01-15): uni
c5af0 78 46 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f 76 xFile can be mov
c5b00 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 ed from one thre
c5b10 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 ad to.** another
c5b20 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 61 as long as we a
c5b30 72 65 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61 20 re running on a
c5b40 73 79 73 74 65 6d 20 74 68 61 74 20 73 75 70 70 system that supp
c5b50 6f 72 74 73 20 74 68 72 65 61 64 73 0a 2a 2a 20 orts threads.**
c5b60 6f 76 65 72 72 69 64 69 6e 67 20 65 61 63 68 20 overriding each
c5b70 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 77 68 others locks (wh
c5b80 69 63 68 20 69 73 20 6e 6f 77 20 74 68 65 20 6d ich is now the m
c5b90 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61 76 ost common behav
c5ba0 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e 6f ior).** or if no
c5bb0 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e locks are held.
c5bc0 20 20 42 75 74 20 74 68 65 20 75 6e 69 78 46 69 But the unixFi
c5bd0 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20 6e le.pLock field n
c5be0 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72 65 eeds to be.** re
c5bf0 63 6f 6d 70 75 74 65 64 20 62 65 63 61 75 73 65 computed because
c5c00 20 69 74 73 20 6b 65 79 20 69 6e 63 6c 75 64 65 its key include
c5c10 73 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 2e s the thread-id.
c5c20 20 20 53 65 65 20 74 68 65 20 0a 2a 2a 20 74 72 See the .** tr
c5c30 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 ansferOwnership(
c5c40 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f 77 ) function below
c5c50 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
c5c60 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a 23 information.*/.#
c5c70 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
c5c80 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 SAFE && defined(
c5c90 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 __linux__).# def
c5ca0 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 ine SET_THREADID
c5cb0 28 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20 3d (X) (X)->tid =
c5cc0 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 0a pthread_self().
c5cd0 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 # define CHECK_T
c5ce0 48 52 45 41 44 49 44 28 58 29 20 28 74 68 72 65 HREADID(X) (thre
c5cf0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
c5d00 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26 thersLocks==0 &&
c5d10 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 !
c5d30 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28 58 pthread_equal((X
c5d40 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64 5f )->tid, pthread_
c5d50 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a 23 self())).#else.#
c5d60 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 define SET_THRE
c5d70 41 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e 65 ADID(X).# define
c5d80 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 CHECK_THREADID(
c5d90 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a X) 0.#endif../*.
c5da0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
c5db0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
c5dc0 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73 structure serves
c5dd0 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 as the key used
c5de0 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 .** to locate a
c5df0 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4f particular unixO
c5e00 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 penCnt structure
c5e10 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 given its inode
c5e20 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 . This.** is th
c5e30 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 75 6e e same as the un
c5e40 69 78 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70 74 ixLockKey except
c5e50 20 74 68 61 74 20 74 68 65 20 74 68 72 65 61 64 that the thread
c5e60 20 49 44 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a ID is omitted..
c5e70 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 */.struct unixFi
c5e80 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 64 leId {. dev_t d
c5e90 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ev;
c5ea0 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e /* Device n
c5eb0 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 5f umber */.#if OS_
c5ec0 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 VXWORKS. struct
c5ed0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a vxworksFileId *
c5ee0 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 pId; /* Unique
c5ef0 66 69 6c 65 20 49 44 20 66 6f 72 20 76 78 77 6f file ID for vxwo
c5f00 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 rks. */.#else.
c5f10 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20 ino_t ino;
c5f20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
c5f30 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 node number */.#
c5f40 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
c5f50 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
c5f60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
c5f70 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61 73 ucture serves as
c5f80 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a the key used.**
c5f90 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 72 to locate a par
c5fa0 74 69 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63 6b ticular unixLock
c5fb0 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 67 Info structure g
c5fc0 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a iven its inode..
c5fd0 2a 2a 0a 2a 2a 20 49 66 20 74 68 72 65 61 64 73 **.** If threads
c5fe0 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 cannot override
c5ff0 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
c6000 6b 73 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73 ks (LinuxThreads
c6010 29 2c 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73 65 ), then we.** se
c6020 74 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 t the unixLockKe
c6030 79 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20 74 y.tid field to t
c6040 68 65 20 74 68 72 65 61 64 20 49 44 2e 20 20 49 he thread ID. I
c6050 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 f threads can ov
c6060 65 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20 6f erride.** each o
c6070 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f 73 thers locks (Pos
c6080 69 78 20 61 6e 64 20 4e 50 54 4c 29 20 74 68 65 ix and NPTL) the
c6090 6e 20 74 69 64 20 69 73 20 61 6c 77 61 79 73 20 n tid is always
c60a0 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 set to zero..**
c60b0 74 69 64 20 69 73 20 6f 6d 69 74 74 65 64 20 69 tid is omitted i
c60c0 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 f we compile wit
c60d0 68 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20 73 hout threading s
c60e0 75 70 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e 20 upport or on an
c60f0 4f 53 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e OS.** other than
c6100 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75 63 linux..*/.struc
c6110 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b 0a t unixLockKey {.
c6120 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c struct unixFil
c6130 65 49 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e 69 eId fid; /* Uni
c6140 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 66 que identifier f
c6150 6f 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 23 or the file */.#
c6160 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
c6170 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 SAFE && defined(
c6180 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 __linux__). pth
c6190 72 65 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a 20 read_t tid; /*
c61a0 54 68 72 65 61 64 20 49 44 20 6f 66 20 6c 6f 63 Thread ID of loc
c61b0 6b 20 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69 66 k owner. Zero if
c61c0 20 6e 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75 78 not using Linux
c61d0 54 68 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64 69 Threads */.#endi
c61e0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 f.};../*.** An i
c61f0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
c6200 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
c6210 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 re is allocated
c6220 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a for each open.**
c6230 20 69 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e 20 inode. Or, on
c6240 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 74 68 LinuxThreads, th
c6250 65 72 65 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 ere is one of th
c6260 65 73 65 20 73 74 72 75 63 74 75 72 65 73 20 66 ese structures f
c6270 6f 72 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64 65 or.** each inode
c6280 20 6f 70 65 6e 65 64 20 62 79 20 65 61 63 68 20 opened by each
c6290 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 thread..**.** A
c62a0 73 69 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e single inode can
c62b0 20 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 66 have multiple f
c62c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c ile descriptors,
c62d0 20 73 6f 20 65 61 63 68 20 75 6e 69 78 46 69 6c so each unixFil
c62e0 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 e.** structure c
c62f0 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
c6300 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
c6310 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
c6320 61 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 and this.** obje
c6330 63 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74 ct keeps a count
c6340 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f of the number o
c6350 66 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74 f unixFile point
c6360 69 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 74 ing to it..*/.st
c6370 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 ruct unixLockInf
c6380 6f 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 o {. struct uni
c6390 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79 xLockKey lockKey
c63a0 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f ; /* The loo
c63b0 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 kup key */. int
c63c0 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 cnt;
c63d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c63e0 4e 75 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44 Number of SHARED
c63f0 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 locks held */.
c6400 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 int locktype;
c6410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6420 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 45 /* One of SHARE
c6430 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 44 D_LOCK, RESERVED
c6440 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 20 _LOCK etc. */.
c6450 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
c6460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6470 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 /* Number of poi
c6480 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 nters to this st
c6490 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 ructure */. str
c64a0 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f uct unixLockInfo
c64b0 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20 *pNext; /*
c64c0 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 List of all unix
c64d0 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 73 LockInfo objects
c64e0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
c64f0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65 76 xLockInfo *pPrev
c6500 3b 20 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e 2e ; /* ....
c6510 20 64 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20 2a doubly linked *
c6520 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 /.};../*.** An i
c6530 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
c6540 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
c6550 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 re is allocated
c6560 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a for each open.**
c6570 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 inode. This st
c6580 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 ructure keeps tr
c6590 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ack of the numbe
c65a0 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 r of locks on th
c65b0 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 at.** inode. If
c65c0 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 a close is atte
c65d0 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e mpted against an
c65e0 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 inode that is h
c65f0 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c olding.** locks,
c6600 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 the close is de
c6610 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c ferred until all
c6620 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 locks clear by
c6630 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 adding the.** fi
c6640 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
c6650 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 be closed to th
c6660 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a e pending list..
c6670 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f 6e **.** TODO: Con
c6680 73 69 64 65 72 20 63 68 61 6e 67 69 6e 67 20 74 sider changing t
c6690 68 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 72 his so that ther
c66a0 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 e is only a sing
c66b0 6c 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 le file.** descr
c66c0 69 70 74 6f 72 20 66 6f 72 20 65 61 63 68 20 6f iptor for each o
c66d0 70 65 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20 77 pen file, even w
c66e0 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 hen it is opened
c66f0 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e multiple times.
c6700 0a 2a 2a 20 54 68 65 20 63 6c 6f 73 65 28 29 20 .** The close()
c6710 73 79 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75 6c system call woul
c6720 64 20 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 65 d only occur whe
c6730 6e 20 74 68 65 20 6c 61 73 74 20 64 61 74 61 62 n the last datab
c6740 61 73 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 ase.** using the
c6750 20 66 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a 2f file closes..*/
c6760 0a 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e .struct unixOpen
c6770 43 6e 74 20 7b 0a 20 20 73 74 72 75 63 74 20 75 Cnt {. struct u
c6780 6e 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 nixFileId fileId
c6790 3b 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 ; /* The looku
c67a0 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e p key */. int n
c67b0 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
c67c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
c67d0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
c67e0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
c67f0 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 /. int nLock;
c6800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6810 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 /* Number of out
c6820 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 2a standing locks *
c6830 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67 /. int nPending
c6840 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c6850 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 65 6e /* Number of pen
c6860 64 69 6e 67 20 63 6c 6f 73 65 28 29 20 6f 70 65 ding close() ope
c6870 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 rations */. int
c6880 20 2a 61 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 *aPending;
c6890 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 /* Malloc
c68a0 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69 6e 67 ed space holding
c68b0 20 66 64 27 73 20 61 77 61 69 74 69 6e 67 20 61 fd's awaiting a
c68c0 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66 20 close() */.#if
c68d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d OS_VXWORKS. sem
c68e0 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20 _t *pSem;
c68f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c6900 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d Named POSIX sem
c6910 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72 aphore */. char
c6920 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41 aSemName[MAX_PA
c6930 54 48 4e 41 4d 45 2b 31 5d 3b 20 20 20 2f 2a 20 THNAME+1]; /*
c6940 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d Name of that sem
c6950 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66 aphore */.#endif
c6960 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
c6970 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70 enCnt *pNext, *p
c6980 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 Prev; /* List
c6990 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43 of all unixOpenC
c69a0 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b nt objects */.};
c69b0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66 ../*.** Lists of
c69c0 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 all unixLockInf
c69d0 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e o and unixOpenCn
c69e0 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73 t objects. Thes
c69f0 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61 73 e used to be has
c6a00 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75 h.** tables. Bu
c6a10 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
c6a20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c objects is rarel
c6a30 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f y more than a do
c6a40 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 zen and.** never
c6a50 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20 74 exceeds a few t
c6a60 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f housand. And lo
c6a70 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61 okup is not on a
c6a80 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74 critical.** pat
c6a90 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69 h so a simple li
c6aa0 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73 nked list will s
c6ab0 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 uffice..*/.stati
c6ac0 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 c struct unixLoc
c6ad0 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20 kInfo *lockList
c6ae0 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75 = 0;.static stru
c6af0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
c6b00 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f openList = 0;../
c6b10 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 *.** This variab
c6b20 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65 le remembers whe
c6b30 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 ther or not thre
c6b40 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 ads can override
c6b50 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 each others.**
c6b60 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 locks..**.**
c6b70 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 0: No. Threads
c6b80 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 cannot override
c6b90 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
c6ba0 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61 ks. (LinuxThrea
c6bb0 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 ds).** 1: Ye
c6bc0 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e 20 s. Threads can
c6bd0 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 override each ot
c6be0 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f hers locks. (Po
c6bf0 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20 six & NLPT).**
c6c00 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b -1: We don't k
c6c10 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f now yet..**.** O
c6c20 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 n some systems,
c6c30 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 we know at compi
c6c40 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61 le-time if threa
c6c50 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 ds can override
c6c60 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c each.** others l
c6c70 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 ocks. On those
c6c80 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c systems, the SQL
c6c90 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 ITE_THREAD_OVERR
c6ca0 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a IDE_LOCK macro.*
c6cb0 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70 * will be set ap
c6cc0 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e propriately. On
c6cd0 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20 other systems,
c6ce0 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b we have to check
c6cf0 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 at.** runtime.
c6d00 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72 On these latter
c6d10 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 systems, SQLTIE
c6d20 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
c6d30 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 _LOCK is.** unde
c6d40 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 fined..**.** Thi
c6d50 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 s variable norma
c6d60 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f lly has file sco
c6d70 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 pe only. But du
c6d80 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65 ring testing, we
c6d90 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c make.** it a gl
c6da0 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65 obal so that the
c6db0 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63 test code can c
c6dc0 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20 hange its value
c6dd0 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 in order to veri
c6de0 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 fy.** that the r
c6df0 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70 65 ight stuff happe
c6e00 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 ns in either cas
c6e10 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 e..*/.#if SQLITE
c6e20 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
c6e30 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
c6e40 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49 ).# ifndef SQLI
c6e50 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 TE_THREAD_OVERRI
c6e60 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66 DE_LOCK.# def
c6e70 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 ine SQLITE_THREA
c6e80 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 D_OVERRIDE_LOCK
c6e90 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69 -1.# endif.# i
c6ea0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c6eb0 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 .int threadsOver
c6ec0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
c6ed0 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 cks = SQLITE_THR
c6ee0 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 EAD_OVERRIDE_LOC
c6ef0 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69 K;.# else.stati
c6f00 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 c int threadsOve
c6f10 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
c6f20 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 ocks = SQLITE_TH
c6f30 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f READ_OVERRIDE_LO
c6f40 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e CK;.# endif.#en
c6f50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
c6f60 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 structure holds
c6f70 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 information pass
c6f80 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 ed into individu
c6f90 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 al test.** threa
c6fa0 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54 68 ds by the testTh
c6fb0 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 readLockingBehav
c6fc0 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a ior() routine..*
c6fd0 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64 54 /.struct threadT
c6fe0 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 estData {. int
c6ff0 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 fd;
c7000 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 /* File to be
c7010 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 locked */. str
c7020 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 uct flock lock;
c7030 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 /* The locki
c7040 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a ng operation */.
c7050 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 int result;
c7060 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
c7070 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e lt of the lockin
c7080 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d g operation */.}
c7090 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 ;..#if SQLITE_TH
c70a0 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
c70b0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f ned(__linux__)./
c70c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
c70d0 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74 68 on is used as th
c70e0 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66 e main routine f
c70f0 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e or a thread laun
c7100 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54 ched by.** testT
c7110 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 hreadLockingBeha
c7120 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73 vior(). It tests
c7130 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68 61 whether the sha
c7140 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 red-lock obtaine
c7150 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e d.** by the main
c7160 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74 54 thread in testT
c7170 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 hreadLockingBeha
c7180 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73 vior() conflicts
c7190 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74 with a.** hypot
c71a0 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f hetical write-lo
c71b0 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 ck obtained by t
c71c0 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68 his thread on th
c71d0 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a e same file..**.
c71e0 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63 ** The write-loc
c71f0 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c k is not actuall
c7200 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20 74 y acquired, as t
c7210 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 his is not possi
c7220 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66 ble if .** the f
c7230 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72 ile is open in r
c7240 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73 ead-only mode (s
c7250 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32 29 ee ticket #3472)
c7260 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 ..*/ .static voi
c7270 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 d *threadLocking
c7280 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29 Test(void *pArg)
c7290 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61 {. struct threa
c72a0 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74 61 dTestData *pData
c72b0 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65 61 = (struct threa
c72c0 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b dTestData*)pArg;
c72d0 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74 . pData->result
c72e0 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e = fcntl(pData->
c72f0 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44 fd, F_GETLK, &pD
c7300 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65 ata->lock);. re
c7310 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e turn pArg;.}.#en
c7320 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 dif /* SQLITE_TH
c7330 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 READSAFE && defi
c7340 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a ned(__linux__) *
c7350 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 /...#if SQLITE_T
c7360 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
c7370 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
c7380 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 /*.** This proce
c7390 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f dure attempts to
c73a0 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 determine wheth
c73b0 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 er or not thread
c73c0 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 s.** can overrid
c73d0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f e each others lo
c73e0 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 cks then sets th
c73f0 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 e .** threadsOve
c7400 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
c7410 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 ocks variable ap
c7420 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a propriately..*/.
c7430 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 static void test
c7440 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 ThreadLockingBeh
c7450 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 avior(int fd_ori
c7460 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 g){. int fd;.
c7470 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 int rc;. struct
c7480 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61 20 threadTestData
c7490 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 d;. struct floc
c74a0 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 k l;. pthread_t
c74b0 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28 t;.. fd = dup(
c74c0 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20 fd_orig);. if(
c74d0 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 fd<0 ) return;.
c74e0 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73 memset(&l, 0, s
c74f0 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c izeof(l));. l.l
c7500 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b _type = F_RDLCK;
c7510 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a . l.l_len = 1;.
c7520 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b l.l_start = 0;
c7530 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 . l.l_whence =
c7540 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d SEEK_SET;. rc =
c7550 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20 fcntl(fd_orig,
c7560 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20 F_SETLK, &l);.
c7570 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74 75 if( rc!=0 ) retu
c7580 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c rn;. memset(&d,
c7590 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a 0, sizeof(d));.
c75a0 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64 d.fd = fd;. d
c75b0 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c .lock = l;. d.l
c75c0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 ock.l_type = F_W
c75d0 52 4c 43 4b 3b 0a 20 20 70 74 68 72 65 61 64 5f RLCK;. pthread_
c75e0 63 72 65 61 74 65 28 26 74 2c 20 30 2c 20 74 68 create(&t, 0, th
c75f0 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c readLockingTest,
c7600 20 26 64 29 3b 0a 20 20 70 74 68 72 65 61 64 5f &d);. pthread_
c7610 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 63 6c join(t, 0);. cl
c7620 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64 ose(fd);. if( d
c7630 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74 .result!=0 ) ret
c7640 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 urn;. threadsOv
c7650 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
c7660 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e Locks = (d.lock.
c7670 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 l_type==F_UNLCK)
c7680 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
c7690 4c 49 54 45 5f 54 48 45 52 41 44 53 41 46 45 20 LITE_THERADSAFE
c76a0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e && defined(__lin
c76b0 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ux__) */../*.**
c76c0 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f Release a unixLo
c76d0 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 ckInfo structure
c76e0 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
c76f0 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 cated by findLoc
c7700 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 74 61 74 kInfo()..*/.stat
c7710 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4c ic void releaseL
c7720 6f 63 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 75 ockInfo(struct u
c7730 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f nixLockInfo *pLo
c7740 63 6b 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63 6b ck){. if( pLock
c7750 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e ){. pLock->n
c7760 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 Ref--;. if( p
c7770 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b Lock->nRef==0 ){
c7780 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b . if( pLock
c7790 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 ->pPrev ){.
c77a0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
c77b0 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d ->pPrev->pNext==
c77c0 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 pLock );.
c77d0 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 pLock->pPrev->p
c77e0 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e Next = pLock->pN
c77f0 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ext;. }else
c7800 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
c7810 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63 ( lockList==pLoc
c7820 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 k );. loc
c7830 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 kList = pLock->p
c7840 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
c7850 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 if( pLock->p
c7860 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Next ){.
c7870 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 assert( pLock->p
c7880 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f Next->pPrev==pLo
c7890 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c ck );. pL
c78a0 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 ock->pNext->pPre
c78b0 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 v = pLock->pPrev
c78c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
c78d0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f sqlite3_free(pLo
c78e0 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ck);. }. }.}
c78f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
c7900 61 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 a unixOpenCnt st
c7910 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 ructure previous
c7920 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 ly allocated by
c7930 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a findLockInfo()..
c7940 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
c7950 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74 eleaseOpenCnt(st
c7960 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ruct unixOpenCnt
c7970 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28 20 *pOpen){. if(
c7980 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 pOpen ){. pOp
c7990 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 en->nRef--;.
c79a0 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d if( pOpen->nRef=
c79b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 =0 ){. if(
c79c0 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a pOpen->pPrev ){.
c79d0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
c79e0 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e pOpen->pPrev->pN
c79f0 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 ext==pOpen );.
c7a00 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 pOpen->pPr
c7a10 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 ev->pNext = pOpe
c7a20 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 n->pNext;.
c7a30 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 }else{. a
c7a40 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d ssert( openList=
c7a50 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 =pOpen );.
c7a60 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 openList = pOp
c7a70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 en->pNext;.
c7a80 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 }. if( pOp
c7a90 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 en->pNext ){.
c7aa0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
c7ab0 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 en->pNext->pPrev
c7ac0 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 ==pOpen );.
c7ad0 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d pOpen->pNext-
c7ae0 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e >pPrev = pOpen->
c7af0 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 pPrev;. }.
c7b00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
c7b10 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e e(pOpen->aPendin
c7b20 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 g);. sqlite
c7b30 33 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 3_free(pOpen);.
c7b40 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
c7b50 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 20 64 * Given a file d
c7b60 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 escriptor, locat
c7b70 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 e unixLockInfo a
c7b80 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 nd unixOpenCnt s
c7b90 74 72 75 63 74 75 72 65 73 20 74 68 61 74 0a 2a tructures that.*
c7ba0 2a 20 64 65 73 63 72 69 62 65 73 20 74 68 61 74 * describes that
c7bb0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
c7bc0 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20 6f 6e . Create new on
c7bd0 65 73 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e es if necessary.
c7be0 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 The.** return
c7bf0 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62 65 20 values might be
c7c00 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 uninitialized if
c7c10 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
c7c20 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ..**.** Return a
c7c30 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 n appropriate er
c7c40 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 ror code..*/.sta
c7c50 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b tic int findLock
c7c60 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c 65 Info(. unixFile
c7c70 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 *pFile,
c7c80 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78 20 66 /* Unix f
c7c90 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20 64 65 ile with file de
c7ca0 73 63 20 75 73 65 64 20 69 6e 20 74 68 65 20 6b sc used in the k
c7cb0 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 ey */. struct u
c7cc0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 nixLockInfo **pp
c7cd0 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72 6e Lock, /* Return
c7ce0 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 the unixLockInf
c7cf0 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 o structure here
c7d00 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
c7d10 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 xOpenCnt **ppOpe
c7d20 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 n /* Return t
c7d30 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 he unixOpenCnt s
c7d40 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f tructure here */
c7d50 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 .){. int rc;
c7d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7d70 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d 20 63 /* System c
c7d80 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 all return code
c7d90 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 */. int fd;
c7da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7db0 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 /* The file
c7dc0 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 70 descriptor for p
c7dd0 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 File */. struct
c7de0 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 unixLockKey loc
c7df0 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b kKey; /* Look
c7e00 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75 up key for the u
c7e10 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 nixLockInfo stru
c7e20 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 cture */. struc
c7e30 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c t unixFileId fil
c7e40 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 6f eId; /* Loo
c7e50 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 kup key for the
c7e60 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
c7e70 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 ct */. struct s
c7e80 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 20 20 tat statbuf;
c7e90 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 /* Low-le
c7ea0 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 vel file informa
c7eb0 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 tion */. struct
c7ec0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 unixLockInfo *p
c7ed0 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 43 61 6e 64 Lock; /* Cand
c7ee0 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e idate unixLockIn
c7ef0 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 fo object */. s
c7f00 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
c7f10 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f 2a t *pOpen; /*
c7f20 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4f Candidate unixO
c7f30 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a 2f penCnt object */
c7f40 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c .. /* Get low-l
c7f50 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e evel information
c7f60 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 about the file
c7f70 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 that we can used
c7f80 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 to. ** create
c7f90 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f a unique name fo
c7fa0 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f r the file.. */
c7fb0 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 . fd = pFile->h
c7fc0 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 ;. rc = fstat(f
c7fd0 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 d, &statbuf);.
c7fe0 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 if( rc!=0 ){.
c7ff0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
c8000 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65 o = errno;.#ifde
c8010 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 f EOVERFLOW.
c8020 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 if( pFile->lastE
c8030 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20 rrno==EOVERFLOW
c8040 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
c8050 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20 NOLFS;.#endif.
c8060 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
c8070 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 64 IOERR;. }..#ifd
c8080 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 2f ef __APPLE__. /
c8090 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20 * On OS X on an
c80a0 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d msdos filesystem
c80b0 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 , the inode numb
c80c0 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a 20 er is reported.
c80d0 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 ** incorrectly
c80e0 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69 for zero-size fi
c80f0 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74 les. See ticket
c8100 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b #3260. To work
c8110 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69 . ** around thi
c8120 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f s problem (we co
c8130 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67 20 nsider it a bug
c8140 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c in OS X, not SQL
c8150 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77 ite). ** we alw
c8160 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68 65 ays increase the
c8170 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 20 file size to 1
c8180 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69 6e by writing a sin
c8190 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 72 gle byte. ** pr
c81a0 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e 67 ior to accessing
c81b0 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 the inode numbe
c81c0 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74 65 r. The one byte
c81d0 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a written is. **
c81e0 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63 68 an ASCII 'S' ch
c81f0 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61 6c aracter which al
c8200 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 so happens to be
c8210 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a the first byte.
c8220 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 64 ** in the head
c8230 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c 69 er of every SQLi
c8240 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e te database. In
c8250 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 this way, if th
c8260 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 61 ere. ** is a ra
c8270 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63 ce condition suc
c8280 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 h that another t
c8290 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61 64 hread has alread
c82a0 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a y populated. **
c82b0 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
c82c0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2c of the database,
c82d0 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64 6f no damage is do
c82e0 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 ne.. */. if( s
c82f0 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d tatbuf.st_size==
c8300 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 72 0 ){. rc = wr
c8310 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b ite(fd, "S", 1);
c8320 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 20 29 . if( rc!=1 )
c8330 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
c8340 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 QLITE_IOERR;.
c8350 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 74 61 }. rc = fsta
c8360 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b t(fd, &statbuf);
c8370 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 . if( rc!=0 )
c8380 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
c8390 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
c83a0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
c83b0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 QLITE_IOERR;.
c83c0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
c83d0 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79 memset(&lockKey
c83e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b , 0, sizeof(lock
c83f0 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79 Key));. lockKey
c8400 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62 .fid.dev = statb
c8410 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f uf.st_dev;.#if O
c8420 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b S_VXWORKS. lock
c8430 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46 Key.fid.pId = pF
c8440 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a ile->pId;.#else.
c8450 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e lockKey.fid.in
c8460 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69 o = statbuf.st_i
c8470 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 no;.#endif.#if S
c8480 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
c8490 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
c84a0 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72 nux__). if( thr
c84b0 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
c84c0 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b OthersLocks<0 ){
c84d0 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c . testThreadL
c84e0 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 ockingBehavior(f
c84f0 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65 d);. }. lockKe
c8500 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f y.tid = threadsO
c8510 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 verrideEachOther
c8520 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 sLocks ? 0 : pth
c8530 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e read_self();.#en
c8540 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c dif. fileId = l
c8550 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66 ockKey.fid;. if
c8560 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20 ( ppLock!=0 ){.
c8570 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c pLock = lockL
c8580 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ist;. while(
c8590 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 pLock && memcmp(
c85a0 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b &lockKey, &pLock
c85b0 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f ->lockKey, sizeo
c85c0 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20 f(lockKey)) ){.
c85d0 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f pLock = pLo
c85e0 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d ck->pNext;. }
c85f0 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d . if( pLock==
c8600 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 0 ){. pLock
c8610 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
c8620 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b c( sizeof(*pLock
c8630 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ) );. if( p
c8640 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Lock==0 ){.
c8650 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
c8660 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f OMEM;. go
c8670 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b to exit_findlock
c8680 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 info;. }.
c8690 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b pLock->lockK
c86a0 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a 20 20 ey = lockKey;.
c86b0 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 pLock->nRef
c86c0 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b = 1;. pLock
c86d0 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 ->cnt = 0;.
c86e0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
c86f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 = 0;. pLoc
c8700 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c k->pNext = lockL
c8710 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b ist;. pLock
c8720 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 ->pPrev = 0;.
c8730 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 74 20 if( lockList
c8740 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 72 65 ) lockList->pPre
c8750 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 v = pLock;.
c8760 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 lockList = pLoc
c8770 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 k;. }else{.
c8780 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b pLock->nRef+
c8790 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 +;. }. *pp
c87a0 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 Lock = pLock;.
c87b0 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e 21 3d }. if( ppOpen!=
c87c0 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d 0 ){. pOpen =
c87d0 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77 openList;. w
c87e0 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 20 6d hile( pOpen && m
c87f0 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 emcmp(&fileId, &
c8800 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c 20 73 pOpen->fileId, s
c8810 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 29 izeof(fileId)) )
c8820 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 {. pOpen =
c8830 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 pOpen->pNext;.
c8840 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 65 }. if( pOpe
c8850 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4f n==0 ){. pO
c8860 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 pen = sqlite3_ma
c8870 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4f lloc( sizeof(*pO
c8880 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 69 66 pen) );. if
c8890 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 ( pOpen==0 ){.
c88a0 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 releaseLoc
c88b0 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 kInfo(pLock);.
c88c0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
c88d0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 E_NOMEM;.
c88e0 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c goto exit_findl
c88f0 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d ockinfo;. }
c8900 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69 . pOpen->fi
c8910 6c 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20 leId = fileId;.
c8920 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 pOpen->nRef
c8930 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65 = 1;. pOpe
c8940 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 n->nLock = 0;.
c8950 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 pOpen->nPend
c8960 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 ing = 0;. p
c8970 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d Open->aPending =
c8980 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 0;. pOpen-
c8990 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73 >pNext = openLis
c89a0 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e t;. pOpen->
c89b0 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 pPrev = 0;.
c89c0 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 if( openList )
c89d0 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 openList->pPrev
c89e0 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f = pOpen;. o
c89f0 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b penList = pOpen;
c8a00 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
c8a10 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53 65 pOpen->pSe
c8a20 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 m = NULL;.
c8a30 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b pOpen->aSemName[
c8a40 30 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64 69 0] = '\0';.#endi
c8a50 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 f. }else{.
c8a60 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b pOpen->nRef++
c8a70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f ;. }. *ppO
c8a80 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d pen = pOpen;. }
c8a90 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 ..exit_findlocki
c8aa0 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 nfo:. return rc
c8ab0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 ;.}../*.** If we
c8ac0 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 are currently i
c8ad0 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 n a different th
c8ae0 72 65 61 64 20 74 68 61 6e 20 74 68 65 20 74 68 read than the th
c8af0 72 65 61 64 20 74 68 61 74 20 74 68 65 0a 2a 2a read that the.**
c8b00 20 75 6e 69 78 46 69 6c 65 20 61 72 67 75 6d 65 unixFile argume
c8b10 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 nt belongs to, t
c8b20 68 65 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 6e hen transfer own
c8b30 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 75 6e ership of the un
c8b40 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 ixFile.** over t
c8b50 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 o the current th
c8b60 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e read..**.** A un
c8b70 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f ixFile is only o
c8b80 77 6e 65 64 20 62 79 20 61 20 74 68 72 65 61 64 wned by a thread
c8b90 20 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 on systems that
c8ba0 20 75 73 65 20 4c 69 6e 75 78 54 68 72 65 61 64 use LinuxThread
c8bb0 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 s..**.** Ownersh
c8bc0 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f ip transfer is o
c8bd0 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 nly allowed if t
c8be0 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63 he unixFile is c
c8bf0 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 urrently unlocke
c8c00 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 d..** If the uni
c8c10 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 xFile is locked
c8c20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 and an ownership
c8c30 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 is wrong, then
c8c40 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
c8c50 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45 _MISUSE. SQLITE
c8c60 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
c8c70 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f if everything wo
c8c80 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 rks..*/.#if SQLI
c8c90 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
c8ca0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
c8cb0 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20 74 __).static int t
c8cc0 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 ransferOwnership
c8cd0 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
c8ce0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 ){. int rc;. p
c8cf0 74 68 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a thread_t hSelf;.
c8d00 20 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 65 if( threadsOve
c8d10 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
c8d20 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f ocks ){. /* O
c8d30 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 wnership transfe
c8d40 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e rs not needed on
c8d50 20 74 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a this system */.
c8d60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
c8d70 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c E_OK;. }. hSel
c8d80 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 f = pthread_self
c8d90 28 29 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61 ();. if( pthrea
c8da0 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 d_equal(pFile->t
c8db0 69 64 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 id, hSelf) ){.
c8dc0 20 20 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c /* We are stil
c8dd0 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68 l in the same th
c8de0 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 read */. OSTR
c8df0 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 ACE1("No-transfe
c8e00 72 2c 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e r, same thread\n
c8e10 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ");. return S
c8e20 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
c8e30 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
c8e40 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a ype!=NO_LOCK ){.
c8e50 20 20 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 /* We cannot
c8e60 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 change ownershi
c8e70 70 20 77 68 69 6c 65 20 77 65 20 61 72 65 20 68 p while we are h
c8e80 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a olding a lock! *
c8e90 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c /. return SQL
c8ea0 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a ITE_MISUSE;. }.
c8eb0 20 20 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e OSTRACE4("Tran
c8ec0 73 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f sfer ownership o
c8ed0 66 20 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 f %d from %d to
c8ee0 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %d\n",.
c8ef0 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 pFile->h, pFi
c8f00 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b le->tid, hSelf);
c8f10 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 . pFile->tid =
c8f20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 hSelf;. if (pFi
c8f30 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c le->pLock != NUL
c8f40 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 L) {. release
c8f50 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e LockInfo(pFile->
c8f60 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d pLock);. rc =
c8f70 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 findLockInfo(pF
c8f80 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f ile, &pFile->pLo
c8f90 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 ck, 0);. OSTR
c8fa0 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE5("LOCK %d
c8fb0 20 69 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 is now %s(%s,%d
c8fc0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a )\n", pFile->h,.
c8fd0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 lockt
c8fe0 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c ypeName(pFile->l
c8ff0 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 ocktype),.
c9000 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d locktypeNam
c9010 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e e(pFile->pLock->
c9020 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 locktype), pFile
c9030 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 ->pLock->cnt);.
c9040 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
c9050 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 } else {. ret
c9060 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
c9070 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69 }.}.#else /* i
c9080 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48 52 f not SQLITE_THR
c9090 45 41 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 EADSAFE */. /*
c90a0 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 On single-thread
c90b0 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 ed builds, owner
c90c0 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 ship transfer is
c90d0 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 a no-op */.# de
c90e0 66 69 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e fine transferOwn
c90f0 65 72 73 68 69 70 28 58 29 20 53 51 4c 49 54 45 ership(X) SQLITE
c9100 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 _OK.#endif /* SQ
c9110 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
c9120 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 */.../*.** This
c9130 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
c9140 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
c9150 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
c9160 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
c9170 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
c9180 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
c9190 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
c91a0 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
c91b0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
c91c0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
c91d0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
c91e0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
c91f0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
c9200 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
c9210 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
c9220 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
c9230 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
c9240 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
c9250 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
c9260 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ixCheckReservedL
c9270 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
c9280 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
c9290 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ut){. int rc =
c92a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
c92b0 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
c92c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
c92d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
c92e0 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 ;.. SimulateIOE
c92f0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
c9300 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
c9310 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a ESERVEDLOCK; );.
c9320 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
c9330 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d );. unixEnterM
c9340 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 utex(); /* Becau
c9350 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 se pFile->pLock
c9360 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 is shared across
c9370 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f threads */.. /
c9380 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 * Check if a thr
c9390 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 ead in this proc
c93a0 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 ess holds such a
c93b0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 lock */. if( p
c93c0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 File->pLock->loc
c93d0 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 ktype>SHARED_LOC
c93e0 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 K ){. reserve
c93f0 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a d = 1;. }.. /*
c9400 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 Otherwise see i
c9410 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f f some other pro
c9420 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 cess holds it..
c9430 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 5f 44 4a */.#ifndef __DJ
c9440 47 50 50 5f 5f 0a 20 20 69 66 28 20 21 72 65 73 GPP__. if( !res
c9450 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 74 72 erved ){. str
c9460 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a uct flock lock;.
c9470 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 lock.l_whenc
c9480 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
c9490 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d lock.l_start =
c94a0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
c94b0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
c94c0 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 1;. lock.l_t
c94d0 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 ype = F_WRLCK;.
c94e0 20 20 20 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e if (-1 == fcn
c94f0 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 tl(pFile->h, F_G
c9500 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a ETLK, &lock)) {.
c9510 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f int tErrno
c9520 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
c9530 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
c9540 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
c9550 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
c9560 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 ERR_CHECKRESERVE
c9570 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 70 46 DLOCK);. pF
c9580 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
c9590 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 tErrno;. } e
c95a0 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 lse if( lock.l_t
c95b0 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a ype!=F_UNLCK ){.
c95c0 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d reserved =
c95d0 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 1;. }. }.#e
c95e0 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 78 4c 65 ndif. . unixLe
c95f0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 aveMutex();. OS
c9600 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d TRACE4("TEST WR-
c9610 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 LOCK %d %d %d\n"
c9620 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
c9630 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 reserved);.. *p
c9640 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 ResOut = reserve
c9650 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a d;. return rc;.
c9660 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
c9670 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
c9680 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
c9690 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
c96a0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
c96b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
c96c0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
c96d0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
c96e0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
c96f0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
c9700 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
c9710 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
c9720 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
c9730 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
c9740 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
c9750 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
c9760 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
c9770 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
c9780 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
c9790 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
c97a0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
c97b0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
c97c0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
c97d0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
c97e0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
c97f0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
c9800 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
c9810 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
c9820 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
c9830 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
c9840 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
c9850 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
c9860 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
c9870 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
c9880 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
c9890 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
c98a0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
c98b0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
c98c0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
c98d0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
c98e0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
c98f0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
c9900 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
c9910 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
c9920 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
c9930 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
c9940 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
c9950 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
c9960 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
c9970 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
c9980 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
c9990 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 65 unixLock(sqlite
c99a0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
c99b0 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a 20 locktype){. /*
c99c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 The following de
c99d0 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 6c scribes the impl
c99e0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
c99f0 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 e various locks
c9a00 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 and. ** lock tr
c9a10 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 72 ansitions in ter
c9a20 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 20 ms of the POSIX
c9a30 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 20 advisory shared
c9a40 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 20 and exclusive.
c9a50 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 76 ** lock primitiv
c9a60 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 2d es (called read-
c9a70 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 2d locks and write-
c9a80 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f 20 locks below, to
c9a90 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 avoid. ** confu
c9aa0 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 65 sion with SQLite
c9ab0 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 68 lock names). Th
c9ac0 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 e algorithms are
c9ad0 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 2a complicated. *
c9ae0 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f 72 * slightly in or
c9af0 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 74 der to be compat
c9b00 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f 77 ible with window
c9b10 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c 74 s systems simult
c9b20 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 63 aneously. ** ac
c9b30 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 cessing the same
c9b40 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
c9b50 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 20 in case that is
c9b60 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a 20 ever required..
c9b70 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 **. ** Symbols
c9b80 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e 68 defined in os.h
c9b90 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 27 indentify the '
c9ba0 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 6e pending byte' an
c9bb0 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64 0a d the 'reserved.
c9bc0 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 68 ** byte', each
c9bd0 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 74 single bytes at
c9be0 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 well known offs
c9bf0 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 68 ets, and the 'sh
c9c00 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 ared byte. ** r
c9c10 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 6f ange', a range o
c9c20 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20 61 f 510 bytes at a
c9c30 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 well known offs
c9c40 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f et.. **. ** To
c9c50 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 44 obtain a SHARED
c9c60 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c 6f lock, a read-lo
c9c70 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f ck is obtained o
c9c80 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a 20 n the 'pending.
c9c90 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 74 ** byte'. If t
c9ca0 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66 75 his is successfu
c9cb0 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 l, a random byte
c9cc0 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 65 from the 'share
c9cd0 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 d byte. ** rang
c9ce0 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b 65 e' is read-locke
c9cf0 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f d and the lock o
c9d00 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 n the 'pending b
c9d10 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a 20 yte' released..
c9d20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 **. ** A proce
c9d30 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 ss may only obta
c9d40 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f in a RESERVED lo
c9d50 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20 ck after it has
c9d60 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 a SHARED lock..
c9d70 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 6c ** A RESERVED l
c9d80 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 ock is implement
c9d90 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 61 ed by grabbing a
c9da0 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 write-lock on t
c9db0 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 65 he. ** 'reserve
c9dc0 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 d byte'. . **.
c9dd0 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d 61 ** A process ma
c9de0 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 y only obtain a
c9df0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 74 PENDING lock aft
c9e00 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 6e er it has obtain
c9e10 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 44 ed a. ** SHARED
c9e20 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e 47 lock. A PENDING
c9e30 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 lock is impleme
c9e40 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e nted by obtainin
c9e50 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 g a write-lock.
c9e60 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 ** on the 'pend
c9e70 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 20 ing byte'. This
c9e80 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20 ensures that no
c9e90 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 new SHARED locks
c9ea0 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 74 can be. ** obt
c9eb0 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 74 ained, but exist
c9ec0 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 ing SHARED locks
c9ed0 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 are allowed to
c9ee0 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 65 persist. A proce
c9ef0 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 ss. ** does not
c9f00 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e 20 have to obtain
c9f10 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
c9f20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 20 on the way to a
c9f30 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 PENDING lock..
c9f40 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 79 ** This property
c9f50 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
c9f60 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 6f algorithm for ro
c9f70 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 lling back a jou
c9f80 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 61 rnal file. ** a
c9f90 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 20 fter a crash..
c9fa0 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c 55 **. ** An EXCLU
c9fb0 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 SIVE lock, obtai
c9fc0 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e 44 ned after a PEND
c9fd0 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 ING lock is held
c9fe0 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d , is. ** implem
c9ff0 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 ented by obtaini
ca000 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 ng a write-lock
ca010 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 73 on the entire 's
ca020 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 hared byte. **
ca030 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 6c range'. Since al
ca040 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 65 l other locks re
ca050 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f 63 quire a read-loc
ca060 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 k on one of the
ca070 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 69 bytes. ** withi
ca080 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 68 n this range, th
ca090 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 is ensures that
ca0a0 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 61 no other locks a
ca0b0 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a 20 re held on the.
ca0c0 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20 ** database. .
ca0d0 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 61 **. ** The rea
ca0e0 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 74 son a single byt
ca0f0 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 e cannot be used
ca100 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 instead of the
ca110 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 'shared byte. *
ca120 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 74 * range' is that
ca130 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f some versions o
ca140 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 f windows do not
ca150 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c 6f support read-lo
ca160 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 cks. By. ** loc
ca170 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 79 king a random by
ca180 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 2c te from a range,
ca190 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 52 concurrent SHAR
ca1a0 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 69 ED locks may exi
ca1b0 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 20 st. ** even if
ca1c0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d the locking prim
ca1d0 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61 6c itive used is al
ca1e0 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 ways a write-loc
ca1f0 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 63 k.. */. int rc
ca200 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
ca210 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
ca220 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
ca230 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f . struct unixLo
ca240 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 ckInfo *pLock =
ca250 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 pFile->pLock;.
ca260 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
ca270 6b 3b 0a 20 20 69 6e 74 20 73 3b 0a 0a 20 20 61 k;. int s;.. a
ca280 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
ca290 20 20 4f 53 54 52 41 43 45 37 28 22 4c 4f 43 4b OSTRACE7("LOCK
ca2a0 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25 73 %d %s was %s
ca2b0 28 25 73 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e (%s,%d) pid=%d\n
ca2c0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 ", pFile->h,.
ca2d0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 locktypeName(
ca2e0 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 locktype), lockt
ca2f0 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c ypeName(pFile->l
ca300 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 ocktype),.
ca310 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f locktypeName(pLo
ca320 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 ck->locktype), p
ca330 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70 Lock->cnt , getp
ca340 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 id());.. /* If
ca350 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 there is already
ca360 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 a lock of this
ca370 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 type or more res
ca380 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a trictive on the.
ca390 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 ** unixFile, d
ca3a0 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 o nothing. Don't
ca3b0 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 use the end_loc
ca3c0 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 k: exit path, as
ca3d0 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d . ** unixEnterM
ca3e0 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 utex() hasn't be
ca3f0 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 en called yet..
ca400 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
ca410 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 >locktype>=lockt
ca420 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 ype ){. OSTRA
ca430 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 CE3("LOCK %d
ca440 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 %s ok (already h
ca450 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e eld)\n", pFile->
ca460 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c h,. l
ca470 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b ocktypeName(lock
ca480 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 type));. retu
ca490 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
ca4a0 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 }.. /* Make sur
ca4b0 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 e the locking se
ca4c0 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 quence is correc
ca4d0 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 t. */. assert(
ca4e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
ca4f0 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 !=NO_LOCK || loc
ca500 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
ca510 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
ca520 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e locktype!=PENDIN
ca530 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 G_LOCK );. asse
ca540 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 rt( locktype!=RE
ca550 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 SERVED_LOCK || p
ca560 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
ca570 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a SHARED_LOCK );..
ca580 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20 /* This mutex
ca590 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73 is needed becaus
ca5a0 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 e pFile->pLock i
ca5b0 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 s shared across
ca5c0 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75 threads. */. u
ca5d0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
ca5e0 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
ca5f0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 the current thr
ca600 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 69 ead owns the pFi
ca610 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 le.. */. rc =
ca620 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 transferOwnershi
ca630 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 p(pFile);. if(
ca640 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
ca650 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
ca660 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 tex();. retur
ca670 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f 63 n rc;. }. pLoc
ca680 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b k = pFile->pLock
ca690 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 ;.. /* If some
ca6a0 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 69 thread using thi
ca6b0 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 6b s PID has a lock
ca6c0 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e 74 via a different
ca6d0 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a 20 unixFile*. **
ca6e0 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 63 handle that prec
ca6f0 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65 73 ludes the reques
ca700 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 6e ted lock, return
ca710 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 BUSY.. */. if
ca720 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 ( (pFile->lockty
ca730 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 pe!=pLock->lockt
ca740 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20 20 ype && .
ca750 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 (pLock->lockty
ca760 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b pe>=PENDING_LOCK
ca770 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48 41 || locktype>SHA
ca780 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a RED_LOCK)). ){.
ca790 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
ca7a0 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 65 BUSY;. goto e
ca7b0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 nd_lock;. }..
ca7c0 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 6c /* If a SHARED l
ca7d0 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 ock is requested
ca7e0 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 , and some threa
ca7f0 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 d using this PID
ca800 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 already. ** ha
ca810 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 45 s a SHARED or RE
ca820 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 SERVED lock, the
ca830 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 n increment refe
ca840 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 rence counts and
ca850 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c . ** return SQL
ca860 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 ITE_OK.. */. i
ca870 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
ca880 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 RED_LOCK && .
ca890 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 (pLock->lockt
ca8a0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
ca8b0 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 || pLock->lockt
ca8c0 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype==RESERVED_LO
ca8d0 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 CK) ){. asser
ca8e0 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 t( locktype==SHA
ca8f0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
ca900 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
ca910 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20 20 ocktype==0 );.
ca920 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d assert( pLock-
ca930 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70 46 >cnt>0 );. pF
ca940 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
ca950 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 SHARED_LOCK;.
ca960 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 pLock->cnt++;.
ca970 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d pFile->pOpen-
ca980 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f >nLock++;. go
ca990 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d to end_lock;. }
ca9a0 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d .. lock.l_len =
ca9b0 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 1L;.. lock.l_w
ca9c0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
ca9d0 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e ;.. /* A PENDIN
ca9e0 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 G lock is needed
ca9f0 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e before acquirin
caa00 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 g a SHARED lock
caa10 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 and before. **
caa20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 acquiring an EXC
caa30 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f LUSIVE lock. Fo
caa40 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 r the SHARED loc
caa50 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 k, the PENDING w
caa60 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 ill. ** be rele
caa70 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ased.. */. if(
caa80 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
caa90 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c D_LOCK . ||
caaa0 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c (locktype==EXCL
caab0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 USIVE_LOCK && pF
caac0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 ile->locktype<PE
caad0 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b NDING_LOCK). ){
caae0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
caaf0 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 = (locktype==SH
cab00 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 ARED_LOCK?F_RDLC
cab10 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 20 K:F_WRLCK);.
cab20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 lock.l_start = P
cab30 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
cab40 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 s = fcntl(pFile
cab50 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
cab60 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d ock);. if( s=
cab70 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69 =(-1) ){. i
cab80 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
cab90 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 o;. rc = sq
caba0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
cabb0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
cabc0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
cabd0 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 K);. if( IS
cabe0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
cabf0 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
cac00 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
cac10 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rrno;. }.
cac20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 goto end_loc
cac30 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 k;. }. }...
cac40 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 /* If control g
cac50 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e ets to this poin
cac60 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 t, then actually
cac70 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 go ahead and ma
cac80 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e ke. ** operatin
cac90 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 g system calls f
caca0 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 or the specified
cacb0 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 lock.. */. if
cacc0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
cacd0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 ED_LOCK ){. i
cace0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 nt tErrno = 0;.
cacf0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
cad00 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 ->cnt==0 );.
cad10 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c assert( pLock->l
cad20 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 ocktype==0 );..
cad30 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 /* Now get th
cad40 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 e read-lock */.
cad50 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
cad60 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a = SHARED_FIRST;.
cad70 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
cad80 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 SHARED_SIZE;.
cad90 20 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74 6c if( (s = fcntl
cada0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 (pFile->h, F_SET
cadb0 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d 31 LK, &lock))==(-1
cadc0 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e ) ){. tErrn
cadd0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d o = errno;. }
cade0 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 . /* Drop the
cadf0 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 temporary PENDI
cae00 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c NG lock */. l
cae10 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 ock.l_start = PE
cae20 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 NDING_BYTE;.
cae30 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b lock.l_len = 1L;
cae40 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
cae50 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 = F_UNLCK;.
cae60 69 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d if( fcntl(pFile-
cae70 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f >h, F_SETLK, &lo
cae80 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ck)!=0 ){.
cae90 69 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20 if( s != -1 ){.
caea0 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 /* This c
caeb0 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68 ould happen with
caec0 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 a network mount
caed0 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 72 */. tErr
caee0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20 no = errno; .
caef0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
caf00 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
caf10 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
caf20 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
caf30 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 ; . if( I
caf40 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
caf50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 ){. pF
caf60 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
caf70 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 tErrno;.
caf80 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
caf90 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 end_lock;.
cafa0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
cafb0 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 s==(-1) ){.
cafc0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
cafd0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
cafe0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
caff0 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 OERR_LOCK);.
cb000 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
cb010 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
cb020 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
cb030 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
cb040 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
cb050 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
cb060 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f cktype = SHARED_
cb070 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c LOCK;. pFil
cb080 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b e->pOpen->nLock+
cb090 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e +;. pLock->
cb0a0 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 cnt = 1;. }.
cb0b0 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 }else if( lockt
cb0c0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
cb0d0 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e OCK && pLock->cn
cb0e0 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 t>1 ){. /* We
cb0f0 20 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 are trying for
cb100 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
cb110 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 k but another th
cb120 72 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 read in this.
cb130 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73 ** same process
cb140 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e is still holdin
cb150 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e g a shared lock.
cb160 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c */. rc = SQL
cb170 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 ITE_BUSY;. }els
cb180 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 e{. /* The re
cb190 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 quest was for a
cb1a0 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c RESERVED or EXCL
cb1b0 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 USIVE lock. It
cb1c0 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 is. ** assume
cb1d0 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 d that there is
cb1e0 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 a SHARED or grea
cb1f0 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ter lock on the
cb200 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 file. ** alre
cb210 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ady.. */.
cb220 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 assert( 0!=pFile
cb230 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
cb240 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
cb250 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 F_WRLCK;. swi
cb260 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b tch( locktype ){
cb270 0a 20 20 20 20 20 20 63 61 73 65 20 52 45 53 45 . case RESE
cb280 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 RVED_LOCK:.
cb290 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
cb2a0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b = RESERVED_BYTE;
cb2b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
cb2c0 20 20 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 case EXCLU
cb2d0 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 SIVE_LOCK:.
cb2e0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
cb2f0 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a = SHARED_FIRST;.
cb300 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c lock.l_l
cb310 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 en = SHARED_SIZE
cb320 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
cb330 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a . default:.
cb340 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 30 assert(0
cb350 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20 3d );. }. s =
cb360 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
cb370 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
cb380 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 ;. if( s==(-1
cb390 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 ) ){. int t
cb3a0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
cb3b0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
cb3c0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
cb3d0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
cb3e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a TE_IOERR_LOCK);.
cb3f0 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
cb400 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
cb410 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
cb420 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
cb430 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
cb440 20 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20 }. ..#ifndef
cb450 4e 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 NDEBUG. /* Set
cb460 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 up the transacti
cb470 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 on-counter chang
cb480 65 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 e checking flags
cb490 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 when. ** trans
cb4a0 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 itioning from a
cb4b0 53 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 SHARED to a RESE
cb4c0 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 RVED lock. The
cb4d0 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d change. ** from
cb4e0 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 SHARED to RESER
cb4f0 56 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 VED marks the be
cb500 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 ginning of a nor
cb510 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f mal. ** write o
cb520 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 peration (not a
cb530 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot journal roll
cb540 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 back).. */. if
cb550 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a ( rc==SQLITE_OK.
cb560 20 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 && pFile->loc
cb570 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f ktype<=SHARED_LO
cb580 43 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70 CK. && locktyp
cb590 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e==RESERVED_LOCK
cb5a0 0a 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d . ){. pFile-
cb5b0 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d >transCntrChng =
cb5c0 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 0;. pFile->d
cb5d0 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 bUpdate = 0;.
cb5e0 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c pFile->inNormal
cb5f0 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 Write = 1;. }.#
cb600 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63 endif... if( rc
cb610 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
cb620 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
cb630 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
cb640 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
cb650 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
cb660 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 }else if( lockt
cb670 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
cb680 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 OCK ){. pFile
cb690 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e ->locktype = PEN
cb6a0 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 DING_LOCK;. p
cb6b0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
cb6c0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 PENDING_LOCK;.
cb6d0 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 }..end_lock:.
cb6e0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
cb6f0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f ;. OSTRACE4("LO
cb700 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e CK %d %s %s\n
cb710 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
cb720 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 ktypeName(lockty
cb730 70 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d 3d pe), . rc==
cb740 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 SQLITE_OK ? "ok"
cb750 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 : "failed");.
cb760 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
cb770 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
cb780 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
cb790 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
cb7a0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
cb7b0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
cb7c0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
cb7d0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
cb7e0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
cb7f0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
cb800 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
cb810 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
cb820 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
cb830 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
cb840 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
cb850 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
cb860 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
cb870 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b c int unixUnlock
cb880 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
cb890 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
cb8a0 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c {. struct unixL
cb8b0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 0a ockInfo *pLock;.
cb8c0 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
cb8d0 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ock;. int rc =
cb8e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 SQLITE_OK;. uni
cb8f0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
cb900 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
cb910 69 6e 74 20 68 3b 0a 0a 20 20 61 73 73 65 72 74 int h;.. assert
cb920 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 ( pFile );. OST
cb930 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 25 RACE7("UNLOCK %
cb940 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c 25 d %d was %d(%d,%
cb950 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 d) pid=%d\n", pF
cb960 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
cb970 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ,. pFile->l
cb980 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
cb990 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c pLock->locktype,
cb9a0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 pFile->pLock->c
cb9b0 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a nt, getpid());..
cb9c0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
cb9d0 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe<=SHARED_LOCK
cb9e0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e );. if( pFile->
cb9f0 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 locktype<=lockty
cba00 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e pe ){. return
cba10 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
cba20 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 if( CHECK_THRE
cba30 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 ADID(pFile) ){.
cba40 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
cba50 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 _MISUSE;. }. u
cba60 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
cba70 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 3b . h = pFile->h;
cba80 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 . pLock = pFile
cba90 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 ->pLock;. asser
cbaa0 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30 t( pLock->cnt!=0
cbab0 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d );. if( pFile-
cbac0 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 >locktype>SHARED
cbad0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 _LOCK ){. ass
cbae0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ert( pLock->lock
cbaf0 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63 type==pFile->loc
cbb00 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69 6d ktype );. Sim
cbb10 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
cbb20 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 6c gn(1);. Simul
cbb30 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d ateIOError( h=(-
cbb40 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 74 1) ). Simulat
cbb50 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 eIOErrorBenign(0
cbb60 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 );..#ifndef NDEB
cbb70 55 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 UG. /* When r
cbb80 65 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 educing a lock s
cbb90 75 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 uch that other p
cbba0 72 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 rocesses can sta
cbbb0 72 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e rt. ** readin
cbbc0 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 g the database f
cbbd0 69 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 ile again, make
cbbe0 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 sure that the.
cbbf0 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
cbc00 20 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 counter was upd
cbc10 61 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 ated if any part
cbc20 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
cbc30 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 . ** file cha
cbc40 6e 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 nged. If the tr
cbc50 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
cbc60 72 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 r is not updated
cbc70 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 ,. ** other c
cbc80 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 onnections to th
cbc90 65 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 e same file migh
cbca0 74 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 t not realize th
cbcb0 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 at. ** the fi
cbcc0 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 le has changed a
cbcd0 6e 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e nd hence might n
cbce0 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 ot know to flush
cbcf0 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 their. ** ca
cbd00 63 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 che. The use of
cbd10 20 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 a stale cache c
cbd20 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 an lead to datab
cbd30 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a ase corruption..
cbd40 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
cbd50 74 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d t( pFile->inNorm
cbd60 61 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20 alWrite==0.
cbd70 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62 || pFile->db
cbd80 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20 Update==0.
cbd90 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 61 || pFile->tra
cbda0 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 3b nsCntrChng==1 );
cbdb0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f . pFile->inNo
cbdc0 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a 23 rmalWrite = 0;.#
cbdd0 65 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 20 endif... if(
cbde0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
cbdf0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 6c _LOCK ){. l
cbe00 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 ock.l_type = F_R
cbe10 44 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b DLCK;. lock
cbe20 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b .l_whence = SEEK
cbe30 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b _SET;. lock
cbe40 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 .l_start = SHARE
cbe50 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c D_FIRST;. l
cbe60 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 ock.l_len = SHAR
cbe70 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 69 ED_SIZE;. i
cbe80 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 f( fcntl(h, F_SE
cbe90 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 TLK, &lock)==(-1
cbea0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ) ){. int
cbeb0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
cbec0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
cbed0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
cbee0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
cbef0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c SQLITE_IOERR_RDL
cbf00 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 OCK);. if
cbf10 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
cbf20 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc) ){.
cbf30 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
cbf40 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
cbf50 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f }. go
cbf60 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 to end_unlock;.
cbf70 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
cbf80 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 lock.l_type = F
cbf90 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b _UNLCK;. lock
cbfa0 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b .l_whence = SEEK
cbfb0 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c _SET;. lock.l
cbfc0 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 _start = PENDING
cbfd0 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e _BYTE;. lock.
cbfe0 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 73 73 l_len = 2L; ass
cbff0 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 59 54 ert( PENDING_BYT
cc000 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f 42 59 E+1==RESERVED_BY
cc010 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20 66 63 TE );. if( fc
cc020 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 ntl(h, F_SETLK,
cc030 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a &lock)!=(-1) ){.
cc040 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 pLock->loc
cc050 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
cc060 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a OCK;. }else{.
cc070 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f int tErrno
cc080 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
cc090 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
cc0a0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
cc0b0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
cc0c0 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 ERR_UNLOCK);.
cc0d0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
cc0e0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
cc0f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
cc100 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
cc110 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
cc120 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 o end_unlock;.
cc130 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f }. }. if( lo
cc140 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 cktype==NO_LOCK
cc150 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e ){. struct un
cc160 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e ixOpenCnt *pOpen
cc170 3b 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 ;. int rc2 =
cc180 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 20 20 SQLITE_OK;..
cc190 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 /* Decrement the
cc1a0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 shared lock cou
cc1b0 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 nter. Release t
cc1c0 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e he lock using an
cc1d0 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 . ** OS call
cc1e0 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 only when all th
cc1f0 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 reads in this sa
cc200 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 me process have
cc210 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 released. **
cc220 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f the lock.. */
cc230 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d . pLock->cnt-
cc240 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b -;. if( pLock
cc250 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ->cnt==0 ){.
cc260 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
cc270 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 6c F_UNLCK;. l
cc280 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 ock.l_whence = S
cc290 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c EEK_SET;. l
cc2a0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c 6f ock.l_start = lo
cc2b0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 ck.l_len = 0L;.
cc2c0 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
cc2d0 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 rrorBenign(1);.
cc2e0 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
cc2f0 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 rror( h=(-1) ).
cc300 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
cc310 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 rrorBenign(0);.
cc320 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 if( fcntl(h
cc330 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
cc340 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 )!=(-1) ){.
cc350 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
cc360 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 pe = NO_LOCK;.
cc370 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
cc380 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
cc390 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 errno;. r
cc3a0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
cc3b0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
cc3c0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
cc3d0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 RR_UNLOCK);.
cc3e0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
cc3f0 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 ERROR(rc) ){.
cc400 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
cc410 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
cc420 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
cc430 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 pLock->lockt
cc440 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 ype = NO_LOCK;.
cc450 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f pFile->lo
cc460 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b cktype = NO_LOCK
cc470 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
cc480 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e . /* Decremen
cc490 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c t the count of l
cc4a0 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68 69 ocks against thi
cc4b0 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 68 s same file. Wh
cc4c0 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f en the. ** co
cc4d0 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f unt reaches zero
cc4e0 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65 , close any othe
cc4f0 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f r file descripto
cc500 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20 rs whose close.
cc510 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72 72 ** was deferr
cc520 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f 75 ed because of ou
cc530 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e tstanding locks.
cc540 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70 65 . */. pOpe
cc550 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e n = pFile->pOpen
cc560 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f ;. pOpen->nLo
cc570 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 ck--;. assert
cc580 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d ( pOpen->nLock>=
cc590 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 0 );. if( pOp
cc5a0 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 20 en->nLock==0 &&
cc5b0 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3e pOpen->nPending>
cc5c0 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 0 ){. int i
cc5d0 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
cc5e0 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 i<pOpen->nPendi
cc5f0 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ng; i++){.
cc600 20 20 2f 2a 20 63 6c 6f 73 65 20 70 65 6e 64 69 /* close pendi
cc610 6e 67 20 66 64 73 2c 20 62 75 74 20 69 66 20 63 ng fds, but if c
cc620 6c 6f 73 69 6e 67 20 66 61 69 6c 73 20 64 6f 6e losing fails don
cc630 27 74 20 66 72 65 65 20 74 68 65 20 61 72 72 61 't free the arra
cc640 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 73 y. ** ass
cc650 69 67 6e 20 2d 31 20 74 6f 20 74 68 65 20 73 75 ign -1 to the su
cc660 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73 65 ccessfully close
cc670 64 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 6e d descriptors an
cc680 64 20 72 65 63 6f 72 64 20 74 68 65 0a 20 20 20 d record the.
cc690 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 2e 20 20 ** error.
cc6a0 54 68 65 20 6e 65 78 74 20 61 74 74 65 6d 70 74 The next attempt
cc6b0 20 74 6f 20 75 6e 6c 6f 63 6b 20 77 69 6c 6c 20 to unlock will
cc6c0 74 72 79 20 61 67 61 69 6e 2e 20 2a 2f 0a 20 20 try again. */.
cc6d0 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d if( pOpen-
cc6e0 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3c 20 30 >aPending[i] < 0
cc6f0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
cc700 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 70 if( close(p
cc710 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 Open->aPending[i
cc720 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ]) ){.
cc730 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
cc740 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
cc750 20 20 20 20 72 63 32 20 3d 20 53 51 4c 49 54 45 rc2 = SQLITE
cc760 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
cc770 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
cc780 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 pOpen->aP
cc790 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b 0a ending[i] = -1;.
cc7a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
cc7b0 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 32 3d }. if( rc2=
cc7c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
cc7d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
cc7e0 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 ee(pOpen->aPendi
cc7f0 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 ng);. pOp
cc800 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 30 en->nPending = 0
cc810 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d ;. pOpen-
cc820 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 >aPending = 0;.
cc830 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
cc840 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
cc850 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
cc860 20 72 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rc2;. }. }.
cc870 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20 ..end_unlock:.
cc880 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
cc890 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
cc8a0 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c TE_OK ) pFile->l
cc8b0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
cc8c0 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b pe;. return rc;
cc8d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
cc8e0 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 unction performs
cc8f0 20 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 the parts of th
cc900 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f e "close file" o
cc910 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d peration .** com
cc920 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 mon to all locki
cc930 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 ng schemes. It c
cc940 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 loses the direct
cc950 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 ory and file.**
cc960 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 handles, if they
cc970 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 are valid, and
cc980 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 sets all fields
cc990 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a of the unixFile.
cc9a0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 ** structure to
cc9b0 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 0..**.** It is *
cc9c0 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 74 not* necessary t
cc9d0 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 o hold the mutex
cc9e0 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
cc9f0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a ne is called,.**
cca00 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 even on VxWorks
cca10 2e 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c 20 . A mutex will
cca20 62 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 56 be acquired on V
cca30 78 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a 2a xWorks by the.**
cca40 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 vxworksReleaseF
cca50 69 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 2e ileId() routine.
cca60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
cca70 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c loseUnixFile(sql
cca80 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
cca90 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
ccaa0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
ccab0 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 d;. if( pFile )
ccac0 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d {. if( pFile-
ccad0 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 >dirfd>=0 ){.
ccae0 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f int err = clo
ccaf0 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 se(pFile->dirfd)
ccb00 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 ;. if( err
ccb10 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
ccb20 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
ccb30 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 rno;. ret
ccb40 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
ccb50 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 _DIR_CLOSE;.
ccb60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
ccb70 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d 31 pFile->dirfd=-1
ccb80 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
ccb90 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 if( pFile->h
ccba0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 >=0 ){. int
ccbb0 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 err = close(pFi
ccbc0 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69 66 le->h);. if
ccbd0 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 ( err ){.
ccbe0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
ccbf0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
ccc00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ccc10 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
ccc20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 }. }.#if
ccc30 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 69 OS_VXWORKS. i
ccc40 66 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 7b f( pFile->pId ){
ccc50 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 . if( pFile
ccc60 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20 20 ->isDelete ){.
ccc70 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 69 unlink(pFi
ccc80 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 le->pId->zCanoni
ccc90 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 calName);.
ccca0 7d 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73 52 }. vxworksR
cccb0 65 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 69 eleaseFileId(pFi
cccc0 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20 20 le->pId);.
cccd0 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b 0a pFile->pId = 0;.
ccce0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
cccf0 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 OSTRACE2("CLOSE
ccd00 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c %-3d\n", pFil
ccd10 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e 43 e->h);. OpenC
ccd20 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 20 ounter(-1);.
ccd30 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c memset(pFile, 0,
ccd40 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 sizeof(unixFile
ccd50 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ));. }. return
ccd60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
ccd70 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
ccd80 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
ccd90 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74 unixClose(sqlit
ccda0 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 e3_file *id){.
ccdb0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
ccdc0 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a OK;. if( id ){.
ccdd0 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 unixFile *pF
ccde0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 ile = (unixFile
ccdf0 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 6e *)id;. unixUn
cce00 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b lock(id, NO_LOCK
cce10 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 );. unixEnter
cce20 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 Mutex();. if(
cce30 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 pFile->pOpen &&
cce40 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e pFile->pOpen->n
cce50 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a Lock ){. /*
cce60 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 If there are ou
cce70 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c tstanding locks,
cce80 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 do not actually
cce90 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 close the file
ccea0 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 just. ** ye
cceb0 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 77 t because that w
ccec0 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65 ould clear those
cced0 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 locks. Instead
ccee0 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20 , add the file.
ccef0 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74 ** descript
ccf00 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 or to pOpen->aPe
ccf10 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20 nding. It will
ccf20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 be automatically
ccf30 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20 20 closed when.
ccf40 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c ** the last l
ccf50 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a ock is cleared..
ccf60 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
ccf70 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20 20 nt *aNew;.
ccf80 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
ccf90 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c nt *pOpen = pFil
ccfa0 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 e->pOpen;.
ccfb0 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 aNew = sqlite3_r
ccfc0 65 61 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 ealloc(pOpen->aP
ccfd0 65 6e 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e ending, (pOpen->
ccfe0 6e 50 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 nPending+1)*size
ccff0 6f 66 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 20 of(int) );.
cd000 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a if( aNew==0 ){.
cd010 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 /* If a
cd020 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75 malloc fails, ju
cd030 73 74 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 st leak the file
cd040 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
cd050 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
cd060 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 pOpen->aPend
cd070 69 6e 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 ing = aNew;.
cd080 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 pOpen->aPend
cd090 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 ing[pOpen->nPend
cd0a0 69 6e 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b ing] = pFile->h;
cd0b0 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e . pOpen->
cd0c0 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 nPending++;.
cd0d0 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d pFile->h = -
cd0e0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
cd0f0 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b . releaseLock
cd100 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 Info(pFile->pLoc
cd110 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f k);. releaseO
cd120 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f penCnt(pFile->pO
cd130 70 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 pen);. rc = c
cd140 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
cd150 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d ;. unixLeaveM
cd160 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 utex();. }. re
cd170 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a turn rc;.}../***
cd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
cd190 6f 66 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 of the posix adv
cd1a0 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 isory lock imple
cd1b0 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a mentation ******
cd1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
cd1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
cd220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
cd270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f *********** No-o
cd290 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a p Locking ******
cd2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
cd2c0 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75 ** Of the variou
cd2d0 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d s locking implem
cd2e0 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61 entations availa
cd2f0 62 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79 20 ble, this is by
cd300 66 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c far the.** simpl
cd310 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73 est: locking is
cd320 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74 ignored. No at
cd330 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f tempt is made to
cd340 20 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 lock the databa
cd350 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72 se.** file for r
cd360 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e eading or writin
cd370 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f g..**.** This lo
cd380 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70 cking mode is ap
cd390 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 propriate for us
cd3a0 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64 e on read-only d
cd3b0 61 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a atabases.** (ex:
cd3c0 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 databases that
cd3d0 61 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20 are burned into
cd3e0 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d CD-ROM, for exam
cd3f0 70 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a ple.) It can.**
cd400 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66 also be used if
cd410 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
cd420 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78 employs some ex
cd430 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d ternal mechanism
cd440 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73 to.** prevent s
cd450 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65 imultaneous acce
cd460 73 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64 ss of the same d
cd470 61 74 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f atabase by two o
cd480 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61 r more.** databa
cd490 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 se connections.
cd4a0 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 But there is a
cd4b0 73 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20 serious risk of
cd4c0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 database.** corr
cd4d0 75 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c uption if this l
cd4e0 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75 ocking mode is u
cd4f0 73 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e sed in situation
cd500 73 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 s where multiple
cd510 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
cd520 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63 nections are acc
cd530 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 essing the same
cd540 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 database file at
cd550 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d the same.** tim
cd560 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 e and one or mor
cd570 65 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 e of those conne
cd580 63 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69 ctions are writi
cd590 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69 ng..*/..static i
cd5a0 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 nt nolockCheckRe
cd5b0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
cd5c0 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 e3_file *NotUsed
cd5d0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b , int *pResOut){
cd5e0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
cd5f0 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
cd600 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 *pResOut = 0;.
cd610 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
cd620 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e ;.}.static int n
cd630 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 olockLock(sqlite
cd640 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 3_file *NotUsed,
cd650 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a int NotUsed2){.
cd660 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
cd670 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 ER2(NotUsed, Not
cd680 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e Used2);. return
cd690 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 SQLITE_OK;.}.st
cd6a0 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 atic int nolockU
cd6b0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
cd6c0 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 le *NotUsed, int
cd6d0 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e NotUsed2){. UN
cd6e0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 USED_PARAMETER2(
cd6f0 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 NotUsed, NotUsed
cd700 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 2);. return SQL
cd710 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
cd720 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e Close the file.
cd730 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e .*/.static int n
cd740 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 olockClose(sqlit
cd750 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 e3_file *id) {.
cd760 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 return closeUni
cd770 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a xFile(id);.}../*
cd780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd790 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f ** End of the no
cd7a0 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 -op lock impleme
cd7b0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a ntation ********
cd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
cd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
cd820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
cd870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd880 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 ******** Begin d
cd890 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 ot-file Locking
cd8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cd8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
cd8c0 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65 *.** The dotfile
cd8d0 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 locking impleme
cd8e0 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 ntation uses the
cd8f0 20 65 78 69 73 74 69 6e 67 20 6f 66 20 73 65 70 existing of sep
cd900 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69 arate lock.** fi
cd910 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 les in order to
cd920 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 control access t
cd930 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 o the database.
cd940 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a This works on j
cd950 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65 ust.** about eve
cd960 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69 6d ry filesystem im
cd970 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20 74 aginable. But t
cd980 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75 73 here are serious
cd990 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a downsides:.**.*
cd9a0 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65 20 * (1) There
cd9b0 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65 is zero concurre
cd9c0 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 72 ncy. A single r
cd9d0 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c eader blocks all
cd9e0 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 other.**
cd9f0 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 connections fr
cda00 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 om reading or wr
cda10 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 iting the databa
cda20 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 se..**.** (2)
cda30 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e An application
cda40 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20 crash or power
cda50 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 73 loss can leave s
cda60 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a tale lock files.
cda70 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 69 ** sitti
cda80 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 6e ng around that n
cda90 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 eed to be cleare
cdaa0 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a d manually..**.*
cdab0 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 * Nevertheless,
cdac0 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20 a dotlock is an
cdad0 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b appropriate lock
cdae0 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73 65 ing mode for use
cdaf0 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20 if no.** other
cdb00 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 locking strategy
cdb10 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a is available..*
cdb20 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 *.** Dotfile loc
cdb30 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63 72 king works by cr
cdb40 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69 6e eating a file in
cdb50 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74 the same direct
cdb60 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64 61 ory as the.** da
cdb70 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68 20 tabase and with
cdb80 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 the same name bu
cdb90 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22 t with a ".lock"
cdba0 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 64 extension added
cdbb0 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 6e ..** The existan
cdbc0 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c ce of a lock fil
cdbd0 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 43 e implies an EXC
cdbe0 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c LUSIVE lock. Al
cdbf0 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20 l other lock.**
cdc00 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 52 types (SHARED, R
cdc10 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e 47 ESERVED, PENDING
cdc20 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e 74 ) are mapped int
cdc30 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a o EXCLUSIVE..*/.
cdc40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 ./*.** The file
cdc50 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f 20 suffix added to
cdc60 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 69 the data base fi
cdc70 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 20 lename in order
cdc80 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a 2a to create the.**
cdc90 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23 lock file..*/.#
cdca0 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53 define DOTLOCK_S
cdcb0 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f UFFIX ".lock"../
cdcc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
cdcd0 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
cdce0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
cdcf0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
cdd00 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
cdd10 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
cdd20 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
cdd30 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
cdd40 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
cdd50 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
cdd60 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
cdd70 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
cdd80 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
cdd90 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
cdda0 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
cddb0 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
cddc0 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
cddd0 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
cdde0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 checking..**.**
cddf0 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 In dotfile locki
cde00 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f 63 ng, either a loc
cde10 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20 64 k exists or it d
cde20 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20 oes not. So in
cde30 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f this.** variatio
cde40 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72 76 n of CheckReserv
cde50 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f edLock(), *pResO
cde60 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 ut is set to tru
cde70 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a e if any lock.**
cde80 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 is held on the
cde90 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20 69 file and false i
cdea0 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e f the file is un
cdeb0 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 locked..*/.stati
cdec0 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 c int dotlockChe
cded0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
cdee0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
cdef0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b int *pResOut) {
cdf00 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
cdf10 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 TE_OK;. int res
cdf20 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 erved = 0;. uni
cdf30 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
cdf40 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 unixFile*)id;..
cdf50 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
cdf60 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
cdf70 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
cdf80 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
cdf90 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
cdfa0 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 ;.. /* Check if
cdfb0 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 a thread in thi
cdfc0 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 s process holds
cdfd0 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 such a lock */.
cdfe0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
cdff0 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
ce000 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 ){. /* Eithe
ce010 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f r this connectio
ce020 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 n or some other
ce030 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68 connection in th
ce040 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20 e same process.
ce050 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f ** holds a lo
ce060 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 ck on the file.
ce070 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 No need to chec
ce080 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 k further. */.
ce090 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
ce0a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
ce0b0 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 The lock is held
ce0c0 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 if and only if
ce0d0 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 the lockfile exi
ce0e0 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 sts */. const
ce0f0 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 char *zLockFile
ce100 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 = (const char*)
ce110 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
ce120 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72 ntext;. reser
ce130 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f ved = access(zLo
ce140 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20 ckFile, 0)==0;.
ce150 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 }. OSTRACE4("T
ce160 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
ce170 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d %d\n", pFile->
ce180 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 h, rc, reserved)
ce190 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 ;. *pResOut = r
ce1a0 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 eserved;. retur
ce1b0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
ce1c0 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 ock the file wit
ce1d0 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 h the lock speci
ce1e0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
ce1f0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 r locktype - one
ce200 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
ce210 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
ce220 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a (1) SHARED_LOCK.
ce230 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 ** (2) RESER
ce240 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 VED_LOCK.**
ce250 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b (3) PENDING_LOCK
ce260 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c .** (4) EXCL
ce270 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a USIVE_LOCK.**.**
ce280 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 Sometimes when
ce290 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c requesting one l
ce2a0 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 ock state, addit
ce2b0 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 ional lock state
ce2c0 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 s.** are inserte
ce2d0 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 d in between. T
ce2e0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 he locking might
ce2f0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 fail on one of
ce300 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 the later.** tra
ce310 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 nsitions leaving
ce320 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 the lock state
ce330 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 different from w
ce340 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 hat it started b
ce350 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 ut.** still shor
ce360 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 t of its goal.
ce370 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 The following ch
ce380 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c art shows the al
ce390 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 lowed.** transit
ce3a0 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 ions and the ins
ce3b0 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 erted intermedia
ce3c0 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a te states:.**.**
ce3d0 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 UNLOCKED ->
ce3e0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 SHARED.** SHA
ce3f0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a RED -> RESERVED.
ce400 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
ce410 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
ce420 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 LUSIVE.** RES
ce430 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e ERVED -> (PENDIN
ce440 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
ce450 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e ** PENDING ->
ce460 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a EXCLUSIVE.**.**
ce470 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
ce480 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 ll only increase
ce490 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 a lock. Use th
ce4a0 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 e sqlite3OsUnloc
ce4b0 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 k().** routine t
ce4c0 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e o lower a lockin
ce4d0 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 g level..**.** W
ce4e0 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b ith dotfile lock
ce4f0 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f ing, we really o
ce500 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61 74 nly support stat
ce510 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56 45 e (4): EXCLUSIVE
ce520 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61 63 ..** But we trac
ce530 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b k the other lock
ce540 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65 72 ing levels inter
ce550 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 nally..*/.static
ce560 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b int dotlockLock
ce570 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
ce580 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
ce590 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 {. unixFile *p
ce5a0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
ce5b0 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a *)id;. int fd;.
ce5c0 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c char *zLockFil
ce5d0 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c e = (char *)pFil
ce5e0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
ce5f0 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
ce600 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 LITE_OK;... /*
ce610 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 6c If we have any l
ce620 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f ock, then the lo
ce630 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79 20 ck file already
ce640 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65 20 exists. All we
ce650 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 have. ** to do
ce660 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69 6e is adjust our in
ce670 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 ternal record of
ce680 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e the lock level.
ce690 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
ce6a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f e->locktype > NO
ce6b0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 _LOCK ){. pFi
ce6c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
ce6d0 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f 53 ocktype;.#if !OS
ce6e0 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20 _VXWORKS. /*
ce6f0 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74 68 Always update th
ce700 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74 e timestamp on t
ce710 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20 he old file */.
ce720 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46 utimes(zLockF
ce730 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64 ile, NULL);.#end
ce740 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 if. return SQ
ce750 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
ce760 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 /* grab an exc
ce770 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 lusive lock */.
ce780 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b fd = open(zLock
ce790 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f File,O_RDONLY|O_
ce7a0 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30 CREAT|O_EXCL,060
ce7b0 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 0);. if( fd<0 )
ce7c0 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 {. /* failed
ce7d0 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74 to open/create t
ce7e0 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 he file, someone
ce7f0 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73 else may have s
ce800 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a tolen the lock *
ce810 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f /. int tErrno
ce820 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 = errno;. if
ce830 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72 ( EEXIST == tErr
ce840 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d no ){. rc =
ce850 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
ce860 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
ce870 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
ce880 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
ce890 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
ce8a0 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 OERR_LOCK);.
ce8b0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
ce8c0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
ce8d0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
ce8e0 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
ce8f0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
ce900 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a return rc;. } .
ce910 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 20 if( close(fd)
ce920 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 ){. pFile->la
ce930 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
ce940 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
ce950 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
ce960 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 }. . /* got it
ce970 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 , set the type a
ce980 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a nd return ok */.
ce990 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
ce9a0 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
ce9b0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
ce9c0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
ce9d0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
ce9e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
ce9f0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
cea00 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
cea10 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
cea20 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
cea30 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
cea40 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
cea50 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
cea60 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
cea70 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
cea80 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
cea90 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
ceaa0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
ceab0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 no-op..**.** Wh
ceac0 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c en the locking l
cead0 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f evel reaches NO_
ceae0 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 65 LOCK, delete the
ceaf0 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 lock file..*/.s
ceb00 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 tatic int dotloc
ceb10 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f kUnlock(sqlite3_
ceb20 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
ceb30 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 cktype) {. unix
ceb40 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
ceb50 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63 nixFile*)id;. c
ceb60 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d har *zLockFile =
ceb70 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e (char *)pFile->
ceb80 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
ceb90 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
ceba0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 );. OSTRACE5("
cebb0 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 UNLOCK %d %d wa
cebc0 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 s %d pid=%d\n",
cebd0 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
cebe0 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c pe,.. pFile->l
cebf0 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 ocktype, getpid(
cec00 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f ));. assert( lo
cec10 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
cec20 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e OCK );. . /* n
cec30 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 o-op if possible
cec40 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
cec50 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 >locktype==lockt
cec60 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
cec70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
cec80 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72 .. /* To downgr
cec90 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 73 ade to shared, s
ceca0 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 72 imply update our
cecb0 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e internal notion
cecc0 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 of the. ** loc
cecd0 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65 k state. No nee
cece0 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 74 d to mess with t
cecf0 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e he file on disk.
ced00 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
ced10 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
ced20 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
ced30 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 locktype = SHARE
ced40 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 D_LOCK;. retu
ced50 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
ced60 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c }. . /* To ful
ced70 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 ly unlock the da
ced80 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74 tabase, delete t
ced90 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a he lock file */.
ceda0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
cedb0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
cedc0 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f 63 if( unlink(zLoc
cedd0 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 6e kFile) ){. in
cede0 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 t rc, tErrno = e
cedf0 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 4e rrno;. if( EN
cee00 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29 OENT != tErrno )
cee10 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
cee20 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
cee30 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
cee40 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
cee50 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 CK);. }. i
cee60 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
cee70 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 (rc) ){. pF
cee80 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
cee90 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 tErrno;. }.
ceea0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 return rc; .
ceeb0 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b }. pFile->lock
ceec0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a type = NO_LOCK;.
ceed0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ceee0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f OK;.}../*.** Clo
ceef0 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65 se a file. Make
cef00 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 20 68 sure the lock h
cef10 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 as been released
cef20 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e before closing.
cef30 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
cef40 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 otlockClose(sqli
cef50 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a te3_file *id) {.
cef60 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 int rc;. if(
cef70 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 id ){. unixFi
cef80 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
cef90 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 xFile*)id;. d
cefa0 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c otlockUnlock(id,
cefb0 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 NO_LOCK);. s
cefc0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c qlite3_free(pFil
cefd0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
cefe0 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 t);. }. rc = c
ceff0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
cf000 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
cf010 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
cf020 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 **** End of the
cf030 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20 69 6d dot-file lock im
cf040 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a plementation ***
cf050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf060 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
cf070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
cf0b0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
cf0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf100 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
cf110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
cf120 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 in flock Locking
cf130 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
cf140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cf150 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 *.**.** Use the
cf160 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d 20 63 flock() system c
cf170 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65 20 6c all to do file l
cf180 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c ocking..**.** fl
cf190 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 69 73 ock() locking is
cf1a0 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65 20 6c like dot-file l
cf1b0 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74 20 74 ocking in that t
cf1c0 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66 69 he various.** fi
cf1d0 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 ne-grain locking
cf1e0 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72 74 65 levels supporte
cf1f0 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 20 d by SQLite are
cf200 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f 0a 2a collapsed into.*
cf210 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63 6c 75 * a single exclu
cf220 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f sive lock. In o
cf230 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48 41 52 ther words, SHAR
cf240 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 61 6e ED, RESERVED, an
cf250 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c 6f 63 d.** PENDING loc
cf260 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 ks are the same
cf270 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58 43 4c thing as an EXCL
cf280 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53 51 4c USIVE lock. SQL
cf290 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 ite.** still wor
cf2a0 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 74 ks when you do t
cf2b0 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75 72 72 his, but concurr
cf2c0 65 6e 63 79 20 69 73 20 72 65 64 75 63 65 64 20 ency is reduced
cf2d0 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 since.** only a
cf2e0 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63 single process c
cf2f0 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 74 68 an be reading th
cf300 65 20 64 61 74 61 62 61 73 65 20 61 74 20 61 20 e database at a
cf310 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 time..**.** Omit
cf320 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 69 66 this section if
cf330 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
cf340 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20 OCKING_STYLE is
cf350 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 69 66 turned off or if
cf360 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f .** compiling fo
cf370 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 r VXWORKS..*/.#i
cf380 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
cf390 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 LOCKING_STYLE &&
cf3a0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a !OS_VXWORKS../*
cf3b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
cf3c0 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
cf3d0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
cf3e0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
cf3f0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
cf400 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
cf410 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
cf420 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
cf430 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
cf440 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
cf450 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
cf460 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
cf470 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
cf480 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
cf490 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
cf4a0 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
cf4b0 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
cf4c0 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
cf4d0 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
cf4e0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 ic int flockChec
cf4f0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
cf500 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
cf510 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 int *pResOut){.
cf520 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
cf530 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 _OK;. int reser
cf540 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 ved = 0;. unixF
cf550 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
cf560 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 ixFile*)id;. .
cf570 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
cf580 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
cf590 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
cf5a0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
cf5b0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
cf5c0 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 ;. . /* Check
cf5d0 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 if a thread in t
cf5e0 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 his process hold
cf5f0 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f s such a lock */
cf600 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
cf610 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f cktype>SHARED_LO
cf620 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 CK ){. reserv
cf630 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 ed = 1;. }. .
cf640 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 /* Otherwise se
cf650 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
cf660 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 process holds it
cf670 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 . */. if( !rese
cf680 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61 rved ){. /* a
cf690 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 68 ttempt to get th
cf6a0 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e e lock */. in
cf6b0 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 t lrc = flock(pF
cf6c0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 ile->h, LOCK_EX
cf6d0 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 | LOCK_NB);.
cf6e0 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20 20 20 if( !lrc ){.
cf6f0 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63 /* got the loc
cf700 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a k, unlock it */.
cf710 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63 lrc = floc
cf720 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b k(pFile->h, LOCK
cf730 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 28 _UN);. if (
cf740 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 20 20 lrc ) {.
cf750 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
cf760 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 rno;. /*
cf770 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 unlock failed wi
cf780 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 th an error */.
cf790 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c lrc = sql
cf7a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
cf7b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
cf7c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
cf7d0 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66 CK); . if
cf7e0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
cf7f0 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 lrc) ){.
cf800 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
cf810 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
cf820 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b rc = lrc;
cf830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
cf840 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a }. } else {.
cf850 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f int tErrno
cf860 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
cf870 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
cf880 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 /* someone e
cf890 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 20 69 lse might have i
cf8a0 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 t reserved */.
cf8b0 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 lrc = sqlite
cf8c0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
cf8d0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
cf8e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 TE_IOERR_LOCK);
cf8f0 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
cf900 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b CK_ERROR(lrc) ){
cf910 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
cf920 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
cf930 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d no;. rc =
cf940 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 lrc;. }.
cf950 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 }. }. OSTRAC
cf960 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E4("TEST WR-LOCK
cf970 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 %d %d %d\n", pF
cf980 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 ile->h, rc, rese
cf990 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66 20 53 rved);..#ifdef S
cf9a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
cf9b0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 CK_LOCK_ERRORS.
cf9c0 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 if( (rc & SQLIT
cf9d0 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 E_IOERR) == SQLI
cf9e0 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 TE_IOERR ){.
cf9f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
cfa00 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 3b 0a reserved=1;.
cfa10 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.#endif /* SQ
cfa20 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 LITE_IGNORE_FLOC
cfa30 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f K_LOCK_ERRORS */
cfa40 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 . *pResOut = re
cfa50 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e served;. return
cfa60 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
cfa70 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
cfa80 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
cfa90 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
cfaa0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
cfab0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
cfac0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
cfad0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
cfae0 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
cfaf0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
cfb00 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
cfb10 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
cfb20 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
cfb30 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
cfb40 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
cfb50 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
cfb60 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
cfb70 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
cfb80 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
cfb90 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
cfba0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
cfbb0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
cfbc0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
cfbd0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
cfbe0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
cfbf0 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
cfc00 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
cfc10 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
cfc20 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
cfc30 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
cfc40 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
cfc50 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
cfc60 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
cfc70 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
cfc80 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
cfc90 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
cfca0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
cfcb0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
cfcc0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
cfcd0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
cfce0 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
cfcf0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
cfd00 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
cfd10 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
cfd20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65 61 flock() only rea
cfd30 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c lly support EXCL
cfd40 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 USIVE locks. We
cfd50 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 track intermedi
cfd60 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 ate.** lock stat
cfd70 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 es in the sqlite
cfd80 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 3_file structure
cfd90 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 , but all locks
cfda0 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f SHARED or.** abo
cfdb0 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58 ve are really EX
cfdc0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e CLUSIVE locks an
cfdd0 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74 d exclude all ot
cfde0 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 72 her processes fr
cfdf0 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 om.** access the
cfe00 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 file..**.** Thi
cfe10 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
cfe20 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
cfe30 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 ock. Use the sq
cfe40 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a lite3OsUnlock().
cfe50 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f ** routine to lo
cfe60 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 wer a locking le
cfe70 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 vel..*/.static i
cfe80 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c nt flockLock(sql
cfe90 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
cfea0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
cfeb0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
cfec0 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
cfed0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
cfee0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 le*)id;.. asser
cfef0 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f t( pFile );.. /
cff00 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 * if we already
cff10 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 have a lock, it
cff20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a is exclusive. .
cff30 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 ** Just adjust
cff40 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 level and punt
cff50 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a on outta here. *
cff60 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c /. if (pFile->l
cff70 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 ocktype > NO_LOC
cff80 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K) {. pFile->
cff90 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
cffa0 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ype;. return
cffb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
cffc0 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 . /* grab an e
cffd0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f xclusive lock */
cffe0 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63 6b 28 . . if (flock(
cfff0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 pFile->h, LOCK_E
d0000 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a X | LOCK_NB)) {.
d0010 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
d0020 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64 errno;. /* d
d0030 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 20 idn't get, must
d0040 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 72 be busy */. r
d0050 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d0060 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d0070 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d0080 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 RR_LOCK);. if
d0090 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d00a0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 rc) ){. pFi
d00b0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d00c0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
d00d0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 } else {. /*
d00e0 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 got it, set the
d00f0 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 type and return
d0100 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d ok */. pFile-
d0110 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
d0120 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 type;. }. OSTR
d0130 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE4("LOCK %d
d0140 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 %s %s\n", pFile
d0150 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d ->h, locktypeNam
d0160 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 e(locktype), .
d0170 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c rc==SQL
d0180 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 ITE_OK ? "ok" :
d0190 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66 64 65 "failed");.#ifde
d01a0 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f f SQLITE_IGNORE_
d01b0 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 FLOCK_LOCK_ERROR
d01c0 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 51 S. if( (rc & SQ
d01d0 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 LITE_IOERR) == S
d01e0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 QLITE_IOERR ){.
d01f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
d0200 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 USY;. }.#endif
d0210 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 /* SQLITE_IGNORE
d0220 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d0230 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 RS */. return r
d0240 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 c;.}.../*.** Low
d0250 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
d0260 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
d0270 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f criptor pFile to
d0280 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b locktype. lock
d0290 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 type.** must be
d02a0 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f either NO_LOCK o
d02b0 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a r SHARED_LOCK..*
d02c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b *.** If the lock
d02d0 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 ing level of the
d02e0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d02f0 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f is already at o
d0300 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 r below.** the r
d0310 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 equested locking
d0320 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 level, this rou
d0330 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
d0340 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
d0350 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 lockUnlock(sqlit
d0360 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d0370 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
d0380 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d0390 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d03a0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
d03b0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 le );. OSTRACE5
d03c0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 ("UNLOCK %d %d
d03d0 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 was %d pid=%d\n"
d03e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
d03f0 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20 type,.
d0400 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d0410 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61 , getpid());. a
d0420 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
d0430 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
d0440 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 . /* no-op if
d0450 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 possible */. i
d0460 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d0470 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe==locktype ){.
d0480 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d0490 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
d04a0 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 73 * shared can jus
d04b0 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 65 t be set because
d04c0 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 20 we always have
d04d0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a an exclusive */.
d04e0 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d if (locktype==
d04f0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 SHARED_LOCK) {.
d0500 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d0510 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
d0520 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d0530 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a _OK;. }. . /*
d0540 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c no, really, unl
d0550 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ock. */. int rc
d0560 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e = flock(pFile->
d0570 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69 h, LOCK_UN);. i
d0580 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 6e 74 f (rc) {. int
d0590 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 r, tErrno = err
d05a0 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 6c 69 no;. r = sqli
d05b0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d05c0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d05d0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
d05e0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c K);. if( IS_L
d05f0 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 7b 0a OCK_ERROR(r) ){.
d0600 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d0610 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d0620 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 . }.#ifdef SQ
d0630 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 LITE_IGNORE_FLOC
d0640 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 K_LOCK_ERRORS.
d0650 20 20 69 66 28 20 28 72 20 26 20 53 51 4c 49 54 if( (r & SQLIT
d0660 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 E_IOERR) == SQLI
d0670 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 TE_IOERR ){.
d0680 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 r = SQLITE_BUS
d0690 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 Y;. }.#endif
d06a0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 /* SQLITE_IGNORE
d06b0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d06c0 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 72 RS */. . r
d06d0 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 eturn r;. } els
d06e0 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c e {. pFile->l
d06f0 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
d0700 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 K;. return SQ
d0710 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a LITE_OK;. }.}..
d0720 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
d0730 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
d0740 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c t flockClose(sql
d0750 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
d0760 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
d0770 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c flockUnlock(id,
d0780 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 NO_LOCK);. }.
d0790 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 return closeUni
d07a0 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 65 xFile(id);.}..#e
d07b0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
d07c0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
d07d0 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 YLE && !OS_VXWOR
d07e0 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a K */../*********
d07f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
d0800 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b f the flock lock
d0810 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d0820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0830 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
d0840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0880 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
d0890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d08d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
d08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
d08f0 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 6d 61 Begin Named Sema
d0900 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a phore Locking **
d0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d0920 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d ******.**.** Nam
d0930 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ed semaphore loc
d0940 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 75 70 king is only sup
d0950 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f 72 6b ported on VxWork
d0960 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f s..**.** Semapho
d0970 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 re locking is li
d0980 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 ke dot-lock and
d0990 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69 74 flock in that it
d09a0 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 really only.**
d09b0 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53 49 supports EXCLUSI
d09c0 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c VE locking. Onl
d09d0 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 y a single proce
d09e0 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20 77 ss can read or w
d09f0 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 74 61 rite.** the data
d0a00 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20 74 base file at a t
d0a10 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75 63 ime. This reduc
d0a20 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f 6e es potential con
d0a30 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a 2a 2a currency, but.**
d0a40 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 6b 20 makes the lock
d0a50 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d implementation m
d0a60 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f 0a 23 uch easier..*/.#
d0a70 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f if OS_VXWORKS../
d0a80 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
d0a90 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
d0aa0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
d0ab0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
d0ac0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
d0ad0 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
d0ae0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
d0af0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
d0b00 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
d0b10 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
d0b20 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
d0b30 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
d0b40 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
d0b50 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
d0b60 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
d0b70 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
d0b80 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
d0b90 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
d0ba0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 checking..*/.sta
d0bb0 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 63 6b tic int semCheck
d0bc0 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
d0bd0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d0be0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 nt *pResOut) {.
d0bf0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d0c00 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 _OK;. int reser
d0c10 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 ved = 0;. unixF
d0c20 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d0c30 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 ixFile*)id;.. S
d0c40 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
d0c50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
d0c60 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 ERR_CHECKRESERVE
d0c70 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 DLOCK; );. . a
d0c80 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
d0c90 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 . /* Check if a
d0ca0 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 thread in this
d0cb0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 process holds su
d0cc0 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ch a lock */. i
d0cd0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d0ce0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 pe>SHARED_LOCK )
d0cf0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d {. reserved =
d0d00 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 1;. }. . /*
d0d10 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 Otherwise see if
d0d20 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
d0d30 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f ess holds it. */
d0d40 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 . if( !reserved
d0d50 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 2a 70 ){. sem_t *p
d0d60 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 Sem = pFile->pOp
d0d70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20 73 74 en->pSem;. st
d0d80 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42 75 ruct stat statBu
d0d90 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65 6d 5f f;.. if( sem_
d0da0 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d trywait(pSem)==-
d0db0 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 1 ){. int t
d0dc0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d0dd0 20 20 20 20 20 69 66 28 20 45 41 47 41 49 4e 20 if( EAGAIN
d0de0 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 != tErrno ){.
d0df0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d0e00 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d0e10 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d0e20 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d0e30 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 SERVEDLOCK);.
d0e40 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d0e50 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d0e60 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 } else {.
d0e70 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e /* someon
d0e80 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 6c e else has the l
d0e90 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65 20 ock when we are
d0ea0 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 in NO_LOCK */.
d0eb0 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d reserved =
d0ec0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
d0ed0 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 e < SHARED_LOCK)
d0ee0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
d0ef0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 77 65 lse{. /* we
d0f00 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20 69 could have it i
d0f10 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f 0a f we want it */.
d0f20 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 28 70 sem_post(p
d0f30 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Sem);. }. }.
d0f40 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 OSTRACE4("TEST
d0f50 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 WR-LOCK %d %d %
d0f60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
d0f70 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a rc, reserved);..
d0f80 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 *pResOut = res
d0f90 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 erved;. return
d0fa0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 rc;.}../*.** Loc
d0fb0 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
d0fc0 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
d0fd0 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
d0fe0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
d0ff0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
d1000 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
d1010 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
d1020 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
d1030 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
d1040 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
d1050 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
d1060 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
d1070 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
d1080 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
d1090 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
d10a0 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
d10b0 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
d10c0 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
d10d0 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
d10e0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
d10f0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
d1100 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
d1110 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
d1120 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
d1130 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
d1140 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
d1150 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
d1160 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
d1170 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
d1180 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
d1190 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
d11a0 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
d11b0 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
d11c0 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
d11d0 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
d11e0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
d11f0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
d1200 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
d1210 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
d1220 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
d1230 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
d1240 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
d1250 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 XCLUSIVE.**.** S
d1260 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 6f emaphore locks o
d1270 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f nly really suppo
d1280 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 rt EXCLUSIVE loc
d1290 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e ks. We track in
d12a0 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f termediate.** lo
d12b0 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 ck states in the
d12c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 sqlite3_file st
d12d0 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c ructure, but all
d12e0 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 locks SHARED or
d12f0 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 .** above are re
d1300 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c ally EXCLUSIVE l
d1310 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 ocks and exclude
d1320 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 all other proce
d1330 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 sses from.** acc
d1340 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a ess the file..**
d1350 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
d1360 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 will only incre
d1370 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 ase a lock. Use
d1380 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e the sqlite3OsUn
d1390 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e lock().** routin
d13a0 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 e to lower a loc
d13b0 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 king level..*/.s
d13c0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f 63 tatic int semLoc
d13d0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d13e0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
d13f0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
d1400 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d1410 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b e*)id;. int fd;
d1420 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d . sem_t *pSem =
d1430 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 pFile->pOpen->p
d1440 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 Sem;. int rc =
d1450 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
d1460 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 if we already h
d1470 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 ave a lock, it i
d1480 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 s exclusive. .
d1490 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 ** Just adjust
d14a0 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f level and punt o
d14b0 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f n outta here. */
d14c0 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f . if (pFile->lo
d14d0 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b cktype > NO_LOCK
d14e0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ) {. pFile->l
d14f0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d1500 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c pe;. rc = SQL
d1510 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f ITE_OK;. goto
d1520 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 sem_end_lock;.
d1530 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20 }. . /* lock
d1540 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62 75 semaphore now bu
d1550 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e 20 t bail out when
d1560 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e 20 already locked.
d1570 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72 79 */. if( sem_try
d1580 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 wait(pSem)==-1 )
d1590 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
d15a0 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f E_BUSY;. goto
d15b0 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 sem_end_lock;.
d15c0 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c }.. /* got it,
d15d0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e set the type an
d15e0 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 d return ok */.
d15f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d1600 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 73 = locktype;.. s
d1610 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 em_end_lock:. r
d1620 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d1630 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 ** Lower the loc
d1640 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 king level on fi
d1650 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 le descriptor pF
d1660 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e ile to locktype.
d1670 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 locktype.** mu
d1680 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
d1690 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
d16a0 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
d16b0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
d16c0 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
d16d0 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
d16e0 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
d16f0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
d1700 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
d1710 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
d1720 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
d1730 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28 73 int semUnlock(s
d1740 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d1750 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
d1760 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d1770 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d1780 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 id;. sem_t *pSe
d1790 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e m = pFile->pOpen
d17a0 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65 72 ->pSem;.. asser
d17b0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 73 t( pFile );. as
d17c0 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20 20 sert( pSem );.
d17d0 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b OSTRACE5("UNLOCK
d17e0 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 %d %d was %d p
d17f0 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d id=%d\n", pFile-
d1800 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 >h, locktype,..
d1810 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d1820 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 e, getpid());.
d1830 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
d1840 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
d1850 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 . . /* no-op i
d1860 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 f possible */.
d1870 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d1880 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype==locktype ){
d1890 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d18a0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d18b0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 /* shared can ju
d18c0 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 st be set becaus
d18d0 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 e we always have
d18e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f an exclusive */
d18f0 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d . if (locktype=
d1900 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a =SHARED_LOCK) {.
d1910 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d1920 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d1930 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d1940 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
d1950 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e 6c * no, really unl
d1960 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 73 ock. */. if ( s
d1970 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d 2d em_post(pSem)==-
d1980 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 1 ) {. int rc
d1990 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f , tErrno = errno
d19a0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
d19b0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d19c0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d19d0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
d19e0 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f );. if( IS_LO
d19f0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
d1a00 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d1a10 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d1a20 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
d1a30 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 n rc; . }. pFi
d1a40 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e le->locktype = N
d1a50 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e O_LOCK;. return
d1a60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
d1a70 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 *. ** Close a fi
d1a80 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 le.. */.static i
d1a90 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c 69 nt semClose(sqli
d1aa0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a te3_file *id) {.
d1ab0 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 if( id ){.
d1ac0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d1ad0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d1ae0 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 . semUnlock(i
d1af0 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 d, NO_LOCK);.
d1b00 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d1b10 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d ;. unixEnterM
d1b20 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c 65 utex();. rele
d1b30 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c aseLockInfo(pFil
d1b40 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 e->pLock);. r
d1b50 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 eleaseOpenCnt(pF
d1b60 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 ile->pOpen);.
d1b70 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
d1b80 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 );. closeUnix
d1b90 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 File(id);. }.
d1ba0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d1bb0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f ;.}..#endif /* O
d1bc0 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a S_VXWORKS */./*.
d1bd0 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f ** Named semapho
d1be0 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e re locking is on
d1bf0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 ly available on
d1c00 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a VxWorks..**.****
d1c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
d1c20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d of the named sem
d1c30 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70 6c aphore lock impl
d1c40 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a ementation *****
d1c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
d1c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a **********/.../*
d1cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d1d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
d1d20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a AFP Locking ****
d1d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d1d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d1d50 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20 41 .** AFP is the A
d1d60 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f 74 pple Filing Prot
d1d70 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61 20 ocol. AFP is a
d1d80 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 74 network filesyst
d1d90 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41 em found.** on A
d1da0 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20 63 pple Macintosh c
d1db0 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68 20 omputers - both
d1dc0 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a OS9 and OSX..**.
d1dd0 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20 69 ** Third-party i
d1de0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
d1df0 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c 61 f AFP are availa
d1e00 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20 63 ble. But this c
d1e10 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79 ode here.** only
d1e20 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a works on OSX..*
d1e30 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f /..#if defined(_
d1e40 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
d1e50 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d1e60 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 NG_STYLE./*.** T
d1e70 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e he afpLockingCon
d1e80 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 63 text structure c
d1e90 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70 20 ontains all afp
d1ea0 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73 74 lock specific st
d1eb0 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ate.*/.typedef s
d1ec0 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 truct afpLocking
d1ed0 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b 69 Context afpLocki
d1ee0 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 ngContext;.struc
d1ef0 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 t afpLockingCont
d1f00 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 ext {. unsigned
d1f10 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72 65 long long share
d1f20 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 63 dByte;. const c
d1f30 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20 har *dbPath;
d1f40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
d1f50 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c of the open fil
d1f60 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 e */.};..struct
d1f70 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
d1f80 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f .{. unsigned lo
d1f90 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b 20 ng long offset;
d1fa0 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 /* offset
d1fb0 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 74 to first byte t
d1fc0 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 o lock */. unsi
d1fd0 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c gned long long l
d1fe0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f 2a ength; /*
d1ff0 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 6f nbr of bytes to
d2000 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 lock */. unsig
d2010 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
d2020 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a 20 tRangeStart; /*
d2030 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 20 nbr of 1st byte
d2040 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 73 locked if succes
d2050 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e sful */. unsign
d2060 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 6c ed char unLockFl
d2070 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 31 ag; /* 1
d2080 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c = unlock, 0 = l
d2090 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ock */. unsigne
d20a0 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 46 d char startEndF
d20b0 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 3d lag; /* 1=
d20c0 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 6f rel to end of fo
d20d0 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 61 rk, 0=rel to sta
d20e0 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 rt */. int fd;
d20f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d2100 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c /* fil
d2110 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 20 e desc to assoc
d2120 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 2a this lock with *
d2130 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 66 /.};..#define af
d2140 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b pfsByteRangeLock
d2150 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f 49 2FSCTL _I
d2160 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 OWR('z', 23, str
d2170 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 uct ByteRangeLoc
d2180 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 kPB2)../*.** Thi
d2190 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 66 s is a utility f
d21a0 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63 6c or setting or cl
d21b0 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61 6e earing a bit-ran
d21c0 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a ge lock on an.**
d21d0 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 2e AFP filesystem.
d21e0 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 53 .** .** Return S
d21f0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
d2200 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 ess, SQLITE_BUSY
d2210 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a on failure..*/.
d2220 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 65 static int afpSe
d2230 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20 63 tLock(. const c
d2240 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 20 har *path,
d2250 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
d2260 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 of the file to b
d2270 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c 6f e locked or unlo
d2280 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46 69 cked */. unixFi
d2290 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 le *pFile,
d22a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e /* Open
d22b0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d22c0 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75 6e on path */. un
d22d0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
d22e0 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a 20 offset, /*
d22f0 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62 65 First byte to be
d2300 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 73 locked */. uns
d2310 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
d2320 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20 4e length, /* N
d2330 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
d2340 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 o lock */. int
d2350 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20 20 setLockFlag
d2360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
d2370 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e 20 ue to set lock.
d2380 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72 20 False to clear
d2390 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 lock */.){. str
d23a0 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 uct ByteRangeLoc
d23b0 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20 65 kPB2 pb;. int e
d23c0 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c 6f rr;. . pb.unLo
d23d0 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63 6b ckFlag = setLock
d23e0 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20 20 Flag ? 0 : 1;.
d23f0 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67 20 pb.startEndFlag
d2400 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65 74 = 0;. pb.offset
d2410 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62 2e = offset;. pb.
d2420 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68 3b length = length;
d2430 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69 6c . pb.fd = pFil
d2440 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52 41 e->h;. . OSTRA
d2450 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 CE6("AFPSETLOCK
d2460 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 6e [%s] for %d%s in
d2470 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 range %llx:%llx
d2480 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c 6f \n", . (setLo
d2490 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 ckFlag?"ON":"OFF
d24a0 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 70 "), pFile->h, (p
d24b0 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 76 b.fd==-1?"[testv
d24c0 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20 20 al-1]":""),.
d24d0 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 3b offset, length);
d24e0 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28 70 . err = fsctl(p
d24f0 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52 61 ath, afpfsByteRa
d2500 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 26 ngeLock2FSCTL, &
d2510 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 65 pb, 0);. if ( e
d2520 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 rr==-1 ) {. i
d2530 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 74 nt rc;. int t
d2540 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d2550 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46 50 OSTRACE4("AFP
d2560 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20 74 SETLOCK failed t
d2570 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20 25 o fsctl() '%s' %
d2580 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 d %s\n",.
d2590 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72 72 path, tErr
d25a0 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45 72 no, strerror(tEr
d25b0 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53 51 rno));.#ifdef SQ
d25c0 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f LITE_IGNORE_AFP_
d25d0 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 LOCK_ERRORS.
d25e0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
d25f0 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d ;.#else. rc =
d2600 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d2610 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d2620 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 o,.
d2630 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46 6c setLockFl
d2640 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45 52 ag ? SQLITE_IOER
d2650 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45 5f R_LOCK : SQLITE_
d2660 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23 IOERR_UNLOCK);.#
d2670 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
d2680 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f IGNORE_AFP_LOCK_
d2690 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69 66 ERRORS */. if
d26a0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d26b0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 rc) ){. pFi
d26c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d26d0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
d26e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
d26f0 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 else {. retu
d2700 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d2710 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
d2720 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
d2730 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
d2740 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
d2750 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
d2760 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
d2770 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
d2780 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
d2790 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
d27a0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
d27b0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
d27c0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
d27d0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
d27e0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
d27f0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
d2800 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
d2810 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
d2820 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
d2830 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
d2840 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 */.static int af
d2850 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f pCheckReservedLo
d2860 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d2870 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
d2880 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
d2890 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
d28a0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
d28b0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d28c0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d28d0 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f . . SimulateIO
d28e0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
d28f0 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
d2900 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b RESERVEDLOCK; );
d2910 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 . . assert( pF
d2920 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63 6b ile );. afpLock
d2930 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 ingContext *cont
d2940 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e ext = (afpLockin
d2950 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c gContext *) pFil
d2960 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
d2970 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b t;. . /* Check
d2980 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
d2990 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
d29a0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
d29b0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d29c0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d29d0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 OCK ){. reser
d29e0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a ved = 1;. }. .
d29f0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
d2a00 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
d2a10 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
d2a20 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 21 t.. */. if( !
d2a30 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
d2a40 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53 45 /* lock the RESE
d2a50 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20 20 RVED byte */.
d2a60 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53 65 int lrc = afpSe
d2a70 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
d2a80 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 bPath, pFile, RE
d2a90 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 SERVED_BYTE, 1,1
d2aa0 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51 4c ); . if( SQL
d2ab0 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 ITE_OK==lrc ){.
d2ac0 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73 75 /* if we su
d2ad0 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69 6e cceeded in takin
d2ae0 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c g the reserved l
d2af0 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74 ock, unlock it t
d2b00 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20 20 o restore.
d2b10 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 ** the original
d2b20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20 6c state */. l
d2b30 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 rc = afpSetLock(
d2b40 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c context->dbPath,
d2b50 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 pFile, RESERVED
d2b60 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 _BYTE, 1, 0);.
d2b70 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
d2b80 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65 64 /* if we failed
d2b90 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b to get the lock
d2ba0 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c then someone el
d2bb0 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74 20 se must have it
d2bc0 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 */. reserve
d2bd0 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 d = 1;. }.
d2be0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d2bf0 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 OR(lrc) ){.
d2c00 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 rc=lrc;. }.
d2c10 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 34 }. . OSTRACE4
d2c20 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 ("TEST WR-LOCK %
d2c30 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c d %d %d\n", pFil
d2c40 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 e->h, rc, reserv
d2c50 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 73 4f ed);. . *pResO
d2c60 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 ut = reserved;.
d2c70 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
d2c80 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 *.** Lock the fi
d2c90 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b le with the lock
d2ca0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
d2cb0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 rameter locktype
d2cc0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
d2cd0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
d2ce0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
d2cf0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
d2d00 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
d2d10 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
d2d20 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
d2d30 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
d2d40 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
d2d50 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
d2d60 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
d2d70 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
d2d80 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
d2d90 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
d2da0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
d2db0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
d2dc0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
d2dd0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
d2de0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
d2df0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
d2e00 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
d2e10 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
d2e20 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
d2e30 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
d2e40 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
d2e50 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
d2e60 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
d2e70 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
d2e80 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
d2e90 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
d2ea0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
d2eb0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
d2ec0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
d2ed0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
d2ee0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
d2ef0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
d2f00 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
d2f10 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
d2f20 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
d2f30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
d2f40 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e ine will only in
d2f50 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 crease a lock.
d2f60 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f Use the sqlite3O
d2f70 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 sUnlock().** rou
d2f80 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 tine to lower a
d2f90 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a locking level..*
d2fa0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 /.static int afp
d2fb0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
d2fc0 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
d2fd0 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ype){. int rc =
d2fe0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e SQLITE_OK;. un
d2ff0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d3000 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
d3010 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 afpLockingConte
d3020 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 xt *context = (a
d3030 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
d3040 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 *) pFile->locki
d3050 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 ngContext;. .
d3060 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d3070 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 . OSTRACE5("LOC
d3080 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25 K %d %s was %
d3090 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 s pid=%d\n", pFi
d30a0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
d30b0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
d30c0 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 ktype), locktype
d30d0 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b Name(pFile->lock
d30e0 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 29 29 type), getpid())
d30f0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
d3100 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
d3110 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
d3120 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
d3130 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
d3140 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 unixFile, do not
d3150 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 hing. Don't use
d3160 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b the afp_end_lock
d3170 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a : exit path, as.
d3180 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 ** unixEnterMu
d3190 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 tex() hasn't bee
d31a0 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 n called yet..
d31b0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
d31c0 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 locktype>=lockty
d31d0 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 pe ){. OSTRAC
d31e0 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 E3("LOCK %d %
d31f0 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 s ok (already he
d3200 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 ld)\n", pFile->h
d3210 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 ,. loc
d3220 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 ktypeName(lockty
d3230 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e pe));. return
d3240 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
d3250 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 . /* Make sure
d3260 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 the locking sequ
d3270 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a ence is correct.
d3280 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
d3290 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d File->locktype!=
d32a0 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 NO_LOCK || lockt
d32b0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d32c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
d32d0 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f cktype!=PENDING_
d32e0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
d32f0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 ( locktype!=RESE
d3300 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 RVED_LOCK || pFi
d3310 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 le->locktype==SH
d3320 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a ARED_LOCK );. .
d3330 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20 /* This mutex
d3340 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73 is needed becaus
d3350 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 e pFile->pLock i
d3360 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 s shared across
d3370 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75 threads. */. u
d3380 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
d3390 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
d33a0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 the current thr
d33b0 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 69 ead owns the pFi
d33c0 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 le.. */. rc =
d33d0 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 transferOwnershi
d33e0 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 p(pFile);. if(
d33f0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
d3400 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
d3410 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 tex();. retur
d3420 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 n rc;. }. .
d3430 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f /* A PENDING lo
d3440 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 ck is needed bef
d3450 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 ore acquiring a
d3460 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 SHARED lock and
d3470 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75 before. ** acqu
d3480 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 iring an EXCLUSI
d3490 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 VE lock. For th
d34a0 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 e SHARED lock, t
d34b0 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a he PENDING will.
d34c0 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 ** be released
d34d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 .. */. if( loc
d34e0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d34f0 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f CK . || (lo
d3500 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
d3510 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d E_LOCK && pFile-
d3520 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e >locktype<PENDIN
d3530 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 G_LOCK). ){.
d3540 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 20 int failed;.
d3550 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 failed = afpSet
d3560 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
d3570 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e Path, pFile, PEN
d3580 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 31 29 DING_BYTE, 1, 1)
d3590 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65 64 ;. if (failed
d35a0 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 ) {. rc = f
d35b0 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67 6f 74 ailed;. got
d35c0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o afp_end_lock;.
d35d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f }. }. . /
d35e0 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 * If control get
d35f0 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c s to this point,
d3600 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 then actually g
d3610 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 o ahead and make
d3620 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 . ** operating
d3630 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 system calls for
d3640 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c the specified l
d3650 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ock.. */. if(
d3660 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
d3670 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 _LOCK ){. int
d3680 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63 32 2c lk, lrc1, lrc2,
d3690 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 lrc1Errno;.
d36a0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 . /* Now get
d36b0 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48 the read-lock SH
d36c0 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 ARED_LOCK */.
d36d0 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68 /* note that th
d36e0 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65 e quality of the
d36f0 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73 randomness does
d3700 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 20 n't matter that
d3710 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d much */. lk =
d3720 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20 random(); .
d3730 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 context->sharedB
d3740 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 yte = (lk & 0x7f
d3750 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f ffffff)%(SHARED_
d3760 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c SIZE - 1);. l
d3770 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b rc1 = afpSetLock
d3780 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
d3790 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 , pFile, .
d37a0 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 SHARED_FIRST
d37b0 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 +context->shared
d37c0 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 Byte, 1, 1);.
d37d0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d37e0 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20 OR(lrc1) ){.
d37f0 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46 lrc1Errno = pF
d3800 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a ile->lastErrno;.
d3810 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f }. /* Dro
d3820 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 p the temporary
d3830 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a PENDING lock */.
d3840 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65 lrc2 = afpSe
d3850 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
d3860 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 bPath, pFile, PE
d3870 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 NDING_BYTE, 1, 0
d3880 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 );. . if(
d3890 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 IS_LOCK_ERROR(lr
d38a0 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46 c1) ) {. pF
d38b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d38c0 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 lrc1Errno;.
d38d0 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 rc = lrc1;.
d38e0 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f goto afp_end_
d38f0 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 lock;. } else
d3900 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d3910 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 OR(lrc2) ){.
d3920 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 rc = lrc2;.
d3930 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f goto afp_end_
d3940 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 lock;. } else
d3950 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c if( lrc1 != SQL
d3960 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 ITE_OK ) {.
d3970 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 rc = lrc1;.
d3980 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 } else {. p
d3990 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d39a0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d39b0 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e pFile->pOpen
d39c0 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d ->nLock++;. }
d39d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
d39e0 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73 The request was
d39f0 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 for a RESERVED
d3a00 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 or EXCLUSIVE loc
d3a10 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a k. It is. **
d3a20 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
d3a30 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20 ere is a SHARED
d3a40 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 or greater lock
d3a50 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 on the file.
d3a60 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 ** already..
d3a70 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 */. int faile
d3a80 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 d = 0;. asser
d3a90 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 t( 0!=pFile->loc
d3aa0 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20 ktype );. if
d3ab0 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 (locktype >= RES
d3ac0 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 ERVED_LOCK && pF
d3ad0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 ile->locktype <
d3ae0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b RESERVED_LOCK) {
d3af0 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75 . /* Acqu
d3b00 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c ire a RESERVED l
d3b10 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 ock */. f
d3b20 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f ailed = afpSetLo
d3b30 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 ck(context->dbPa
d3b40 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 th, pFile, RESER
d3b50 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a VED_BYTE, 1,1);.
d3b60 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 }. if (!f
d3b70 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 ailed && locktyp
d3b80 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c e == EXCLUSIVE_L
d3b90 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 OCK) {. /*
d3ba0 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 Acquire an EXCLU
d3bb0 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 SIVE lock */.
d3bc0 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 . /* R
d3bd0 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 emove the shared
d3be0 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 lock before try
d3bf0 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 ing the range.
d3c00 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 we'll need to .
d3c10 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c ** reestabl
d3c20 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c ish the shared l
d3c30 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 ock if we can't
d3c40 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f get the afpUnlo
d3c50 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ck. */.
d3c60 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d if( !(failed =
d3c70 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
d3c80 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
d3c90 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 le, SHARED_FIRST
d3ca0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
d3cb0 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 cont
d3cc0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c ext->sharedByte,
d3cd0 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20 1, 0)) ){.
d3ce0 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d int failed2 =
d3cf0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
d3d00 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d /* now attem
d3d10 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 65 mpt to get the e
d3d20 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 xclusive lock ra
d3d30 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 nge */. f
d3d40 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f ailed = afpSetLo
d3d50 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 ck(context->dbPa
d3d60 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 th, pFile, SHARE
d3d70 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20 D_FIRST, .
d3d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d3d90 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f SHARED_
d3da0 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20 SIZE, 1);.
d3db0 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 20 if( failed &&
d3dc0 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65 (failed2 = afpSe
d3dd0 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
d3de0 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 bPath, pFile, .
d3df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d3e00 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 SHARED_FIR
d3e10 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 ST + context->sh
d3e20 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29 aredByte, 1, 1))
d3e30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
d3e40 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69 Can't reestabli
d3e50 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f sh the shared lo
d3e60 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27 ck. Sqlite can'
d3e70 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a t deal, this is.
d3e80 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 ** a c
d3e90 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f ritical I/O erro
d3ea0 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 r. */.
d3eb0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 28 rc = ((
d3ec0 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f failed & SQLITE_
d3ed0 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 IOERR) == SQLITE
d3ee0 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64 _IOERR) ? failed
d3ef0 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20 2 : .
d3f00 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 SQLITE_IOERR
d3f10 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20 _LOCK;.
d3f20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f goto afp_end_lo
d3f30 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 ck;. } .
d3f40 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
d3f50 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b rc = failed;
d3f60 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a . }. }.
d3f70 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 29 if( failed )
d3f80 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 {. rc = fai
d3f90 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 led;. }. }.
d3fa0 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 . if( rc==SQLI
d3fb0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 TE_OK ){. pFi
d3fc0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d3fd0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 ocktype;. }else
d3fe0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 if( locktype==E
d3ff0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
d4000 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d4010 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c type = PENDING_L
d4020 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f OCK;. }. .afp_
d4030 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 end_lock:. unix
d4040 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
d4050 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 OSTRACE4("LOCK
d4060 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 %d %s %s\n", p
d4070 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
d4080 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c eName(locktype),
d4090 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 . rc==S
d40a0 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 QLITE_OK ? "ok"
d40b0 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 : "failed");. r
d40c0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d40d0 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 ** Lower the loc
d40e0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 king level on fi
d40f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 le descriptor pF
d4100 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e ile to locktype.
d4110 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 locktype.** mu
d4120 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
d4130 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
d4140 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
d4150 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
d4160 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
d4170 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
d4180 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
d4190 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
d41a0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
d41b0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
d41c0 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
d41d0 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73 int afpUnlock(s
d41e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d41f0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
d4200 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d4210 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c TE_OK;. unixFil
d4220 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
d4230 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c File*)id;. afpL
d4240 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 ockingContext *p
d4250 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e Ctx = (afpLockin
d4260 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c gContext *) pFil
d4270 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
d4280 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 t;.. assert( pF
d4290 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 ile );. OSTRACE
d42a0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 5("UNLOCK %d %d
d42b0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e was %d pid=%d\n
d42c0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
d42d0 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 ktype,.
d42e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c pFile->locktype,
d42f0 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 getpid());.. a
d4300 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
d4310 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
d4320 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
d4330 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 ktype<=locktype
d4340 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
d4350 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 LITE_OK;. }. i
d4360 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 f( CHECK_THREADI
d4370 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 D(pFile) ){.
d4380 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
d4390 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 SUSE;. }. unix
d43a0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
d43b0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d43c0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
d43d0 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 ){. . if(
d43e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
d43f0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
d4400 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 ){. rc = af
d4410 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 pSetLock(pCtx->d
d4420 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 bPath, pFile, SH
d4430 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41 52 ARED_FIRST, SHAR
d4440 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 ED_SIZE, 0);.
d4450 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
d4460 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 E_OK && locktype
d4470 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
d4480 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 . /* only
d4490 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68 re-establish th
d44a0 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 e shared lock if
d44b0 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 necessary */.
d44c0 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 int shared
d44d0 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 LockByte = SHARE
d44e0 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 D_FIRST+pCtx->sh
d44f0 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20 20 aredByte;.
d4500 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 rc = afpSetLoc
d4510 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 k(pCtx->dbPath,
d4520 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 pFile, sharedLoc
d4530 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 kByte, 1, 1);.
d4540 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d4550 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
d4560 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K && pFile->lock
d4570 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f type>=PENDING_LO
d4580 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d CK ){. rc =
d4590 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 afpSetLock(pCtx
d45a0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
d45b0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 PENDING_BYTE, 1
d45c0 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 20 , 0);. } .
d45d0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
d45e0 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 OK && pFile->loc
d45f0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f ktype>=RESERVED_
d4600 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 LOCK ){. rc
d4610 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 = afpSetLock(pC
d4620 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c tx->dbPath, pFil
d4630 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 e, RESERVED_BYTE
d4640 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 , 1, 0);. }.
d4650 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 }else if( lockt
d4660 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a ype==NO_LOCK ){.
d4670 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 65 /* clear the
d4680 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a shared lock */.
d4690 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f int sharedLo
d46a0 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f ckByte = SHARED_
d46b0 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 FIRST+pCtx->shar
d46c0 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20 3d edByte;. rc =
d46d0 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 afpSetLock(pCtx
d46e0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
d46f0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c sharedLockByte,
d4700 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 1, 0);. }.. i
d4710 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
d4720 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b ){. if( lock
d4730 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type==NO_LOCK ){
d4740 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75 6e . struct un
d4750 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e ixOpenCnt *pOpen
d4760 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b = pFile->pOpen;
d4770 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c . pOpen->nL
d4780 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73 ock--;. ass
d4790 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 ert( pOpen->nLoc
d47a0 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 k>=0 );. if
d47b0 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d ( pOpen->nLock==
d47c0 30 20 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 0 && pOpen->nPen
d47d0 64 69 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20 20 ding>0 ){.
d47e0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 int i;.
d47f0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 65 for(i=0; i<pOpe
d4800 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b 20 69 2b 2b n->nPending; i++
d4810 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
d4820 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 pOpen->aPending
d4830 5b 69 5d 20 3c 20 30 20 29 20 63 6f 6e 74 69 6e [i] < 0 ) contin
d4840 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ue;. if
d4850 28 20 63 6c 6f 73 65 28 70 4f 70 65 6e 2d 3e 61 ( close(pOpen->a
d4860 50 65 6e 64 69 6e 67 5b 69 5d 29 20 29 7b 0a 20 Pending[i]) ){.
d4870 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 pFile
d4880 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
d4890 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 rno;.
d48a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
d48b0 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 RR_CLOSE;.
d48c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d48d0 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 pOpen->aP
d48e0 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b 0a ending[i] = -1;.
d48f0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
d4900 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
d4910 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
d4920 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
d4930 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 2d ite3_free(pOpen-
d4940 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20 20 20 20 >aPending);.
d4950 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 pOpen->nPe
d4960 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 nding = 0;.
d4970 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e pOpen->aPen
d4980 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 ding = 0;.
d4990 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
d49a0 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 }. }. unixLeav
d49b0 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 eMutex();. if(
d49c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc==SQLITE_OK )
d49d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d49e0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 = locktype;. re
d49f0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
d4a00 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 * Close a file &
d4a10 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 cleanup AFP spe
d4a20 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f cific locking co
d4a30 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 ntext .*/.static
d4a40 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71 int afpClose(sq
d4a50 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 lite3_file *id)
d4a60 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 {. if( id ){.
d4a70 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d4a80 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d4a90 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b d;. afpUnlock
d4aa0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
d4ab0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 unixEnterMute
d4ac0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 x();. if( pFi
d4ad0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 le->pOpen && pFi
d4ae0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b le->pOpen->nLock
d4af0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
d4b00 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 there are outsta
d4b10 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 nding locks, do
d4b20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f not actually clo
d4b30 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 se the file just
d4b40 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 . ** yet be
d4b50 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 cause that would
d4b60 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 clear those loc
d4b70 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 ks. Instead, ad
d4b80 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 d the file.
d4b90 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 ** descriptor t
d4ba0 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e o pOpen->aPendin
d4bb0 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 g. It will be a
d4bc0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f utomatically clo
d4bd0 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a sed when. *
d4be0 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 * the last lock
d4bf0 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 is cleared..
d4c00 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a */. int *
d4c10 61 4e 65 77 3b 0a 20 20 20 20 20 20 73 74 72 75 aNew;. stru
d4c20 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
d4c30 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 pOpen = pFile->p
d4c40 4f 70 65 6e 3b 0a 20 20 20 20 20 20 61 4e 65 77 Open;. aNew
d4c50 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c = sqlite3_reall
d4c60 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 oc(pOpen->aPendi
d4c70 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50 65 6e ng, (pOpen->nPen
d4c80 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66 28 69 ding+1)*sizeof(i
d4c90 6e 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 nt) );. if(
d4ca0 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 aNew==0 ){.
d4cb0 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c /* If a mall
d4cc0 6f 63 20 66 61 69 6c 73 2c 20 6a 75 73 74 20 6c oc fails, just l
d4cd0 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64 65 73 eak the file des
d4ce0 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 20 20 20 criptor */.
d4cf0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
d4d00 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 pOpen->aPending
d4d10 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 = aNew;.
d4d20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b pOpen->aPending[
d4d30 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 5d pOpen->nPending]
d4d40 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 20 = pFile->h;.
d4d50 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e pOpen->nPen
d4d60 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 ding++;.
d4d70 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 pFile->h = -1;.
d4d80 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
d4d90 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 releaseOpenCnt(
d4da0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 pFile->pOpen);.
d4db0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
d4dc0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
d4dd0 6e 74 65 78 74 29 3b 0a 20 20 20 20 63 6c 6f 73 ntext);. clos
d4de0 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 eUnixFile(id);.
d4df0 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
d4e00 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 x();. }. retur
d4e10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d4e20 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 #endif /* define
d4e30 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 d(__APPLE__) &&
d4e40 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
d4e50 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f CKING_STYLE */./
d4e60 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 61 62 *.** The code ab
d4e70 6f 76 65 20 69 73 20 74 68 65 20 41 46 50 20 6c ove is the AFP l
d4e80 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ock implementati
d4e90 6f 6e 2e 20 20 54 68 65 20 63 6f 64 65 20 69 73 on. The code is
d4ea0 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 specific.** to
d4eb0 4d 61 63 4f 53 58 20 61 6e 64 20 64 6f 65 73 20 MacOSX and does
d4ec0 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 6f 74 68 65 not work on othe
d4ed0 72 20 75 6e 69 78 20 70 6c 61 74 66 6f 72 6d 73 r unix platforms
d4ee0 2e 20 20 4e 6f 20 61 6c 74 65 72 6e 61 74 69 76 . No alternativ
d4ef0 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 62 6c e.** is availabl
d4f00 65 2e 20 20 49 66 20 79 6f 75 20 64 6f 6e 27 74 e. If you don't
d4f10 20 63 6f 6d 70 69 6c 65 20 66 6f 72 20 61 20 6d compile for a m
d4f20 61 63 2c 20 74 68 65 6e 20 74 68 65 20 22 75 6e ac, then the "un
d4f30 69 78 2d 61 66 70 22 0a 2a 2a 20 56 46 53 20 69 ix-afp".** VFS i
d4f40 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e s not available.
d4f50 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
d4f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
d4f70 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d the AFP lock im
d4f80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a plementation ***
d4f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4fa0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
d4fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d4ff0 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a **/.../*********
d5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5040 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
d5050 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69 ****** Non-locki
d5060 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ng sqlite3_file
d5070 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a methods ********
d5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5090 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 *****.**.** The
d50a0 6e 65 78 74 20 64 69 76 69 73 69 6f 6e 20 63 6f next division co
d50b0 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 ntains implement
d50c0 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20 6d ations for all m
d50d0 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 0a 2a ethods of the .*
d50e0 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f * sqlite3_file o
d50f0 62 6a 65 63 74 20 6f 74 68 65 72 20 74 68 61 6e bject other than
d5100 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 the locking met
d5110 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b 69 hods. The locki
d5120 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 77 65 ng.** methods we
d5130 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20 64 69 re defined in di
d5140 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 20 28 6f visions above (o
d5150 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f ne locking metho
d5160 64 20 70 65 72 0a 2a 2a 20 64 69 76 69 73 69 6f d per.** divisio
d5170 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65 74 68 6f n). Those metho
d5180 64 73 20 74 68 61 74 20 61 72 65 20 63 6f 6d 6d ds that are comm
d5190 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e on to all lockin
d51a0 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72 65 20 67 g modes.** are g
d51b0 61 74 68 65 72 20 74 6f 67 65 74 68 65 72 20 69 ather together i
d51c0 6e 74 6f 20 74 68 69 73 20 64 69 76 69 73 69 6f nto this divisio
d51d0 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 n..*/../*.** See
d51e0 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 k to the offset
d51f0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
d5200 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 cond argument, t
d5210 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a 2a 2a hen read cnt .**
d5220 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 bytes into pBuf
d5230 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d . Return the num
d5240 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
d5250 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a 0a 2a ually read..**.*
d5260 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20 64 65 * NB: If you de
d5270 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 20 6f fine USE_PREAD o
d5280 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c 20 74 r USE_PREAD64, t
d5290 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61 6c 73 hen it might als
d52a0 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 61 72 o.** be necessar
d52b0 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 y to define _XOP
d52c0 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62 65 20 EN_SOURCE to be
d52d0 35 30 30 2e 20 20 54 68 69 73 20 76 61 72 69 65 500. This varie
d52e0 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 73 79 s from.** one sy
d52f0 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65 72 2e stem to another.
d5300 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65 20 64 Since SQLite d
d5310 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 55 oes not define U
d5320 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e 79 20 SE_PREAD.** any
d5330 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65 66 61 any form by defa
d5340 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e 6f 74 ult, we will not
d5350 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 66 69 attempt to defi
d5360 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 ne _XOPEN_SOURCE
d5370 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 74 73 ..** See tickets
d5380 20 23 32 37 34 31 20 61 6e 64 20 23 32 36 38 31 #2741 and #2681
d5390 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 ..**.** To avoid
d53a0 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 stomping the er
d53b0 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 rno value on a f
d53c0 61 69 6c 65 64 20 72 65 61 64 20 74 68 65 20 6c ailed read the l
d53d0 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a astErrno value.*
d53e0 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 * is set before
d53f0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 returning..*/.st
d5400 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 atic int seekAnd
d5410 52 65 61 64 28 75 6e 69 78 46 69 6c 65 20 2a 69 Read(unixFile *i
d5420 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 d, sqlite3_int64
d5430 20 6f 66 66 73 65 74 2c 20 76 6f 69 64 20 2a 70 offset, void *p
d5440 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 Buf, int cnt){.
d5450 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 int got;. i64
d5460 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d newOffset;. TIM
d5470 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64 65 ER_START;.#if de
d5480 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 fined(USE_PREAD)
d5490 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 64 28 69 . got = pread(i
d54a0 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c d->h, pBuf, cnt,
d54b0 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 offset);. Simu
d54c0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 lateIOError( got
d54d0 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 69 66 20 64 = -1 );.#elif d
d54e0 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 efined(USE_PREAD
d54f0 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 64). got = prea
d5500 64 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c d64(id->h, pBuf,
d5510 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 cnt, offset);.
d5520 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d5530 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 ( got = -1 );.#e
d5540 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 lse. newOffset
d5550 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f = lseek(id->h, o
d5560 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 ffset, SEEK_SET)
d5570 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
d5580 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 74 2d 2d ror( newOffset--
d5590 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 );. if( newOff
d55a0 73 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 set!=offset ){.
d55b0 20 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 if( newOffset
d55c0 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 == -1 ){.
d55d0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
d55e0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
d55f0 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 no;. }else{.
d5600 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a ((unixFile*
d5610 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 )id)->lastErrno
d5620 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 = 0;.... }.
d5630 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d return -1;. }
d5640 0a 20 20 67 6f 74 20 3d 20 72 65 61 64 28 69 64 . got = read(id
d5650 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b ->h, pBuf, cnt);
d5660 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f .#endif. TIMER_
d5670 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 END;. if( got<0
d5680 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 ){. ((unixFi
d5690 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
d56a0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a no = errno;. }.
d56b0 20 20 4f 53 54 52 41 43 45 35 28 22 52 45 41 44 OSTRACE5("READ
d56c0 20 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c %-3d %5d %7l
d56d0 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e ld %llu\n", id->
d56e0 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 h, got, offset,
d56f0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a TIMER_ELAPSED);.
d5700 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a return got;.}.
d5710 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
d5720 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 from a file int
d5730 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 o a buffer. Ret
d5740 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
d5750 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 all.** bytes we
d5760 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66 re read successf
d5770 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f ully and SQLITE_
d5780 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e IOERR if anythin
d5790 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
d57a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
d57b0 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74 nixRead(. sqlit
d57c0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 e3_file *id, .
d57d0 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 void *pBuf, . i
d57e0 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 nt amt,. sqlite
d57f0 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29 3_int64 offset.)
d5800 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 {. int got;. a
d5810 73 73 65 72 74 28 20 69 64 20 29 3b 0a 0a 20 20 ssert( id );..
d5820 2f 2a 20 4e 65 76 65 72 20 72 65 61 64 20 6f 72 /* Never read or
d5830 20 77 72 69 74 65 20 61 6e 79 20 6f 66 20 74 68 write any of th
d5840 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c e bytes in the l
d5850 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 2a 2f 0a ocking range */.
d5860 20 20 61 73 73 65 72 74 28 20 28 28 75 6e 69 78 assert( ((unix
d5870 46 69 6c 65 2a 29 69 64 29 2d 3e 69 73 4c 6f 63 File*)id)->isLoc
d5880 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 20 20 20 20 kable==0.
d5890 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 45 || offset>=PE
d58a0 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a 20 NDING_BYTE+512.
d58b0 20 20 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 || offs
d58c0 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f et+amt<=PENDING_
d58d0 42 59 54 45 20 29 3b 0a 0a 20 20 67 6f 74 20 3d BYTE );.. got =
d58e0 20 73 65 65 6b 41 6e 64 52 65 61 64 28 28 75 6e seekAndRead((un
d58f0 69 78 46 69 6c 65 2a 29 69 64 2c 20 6f 66 66 73 ixFile*)id, offs
d5900 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 3b 0a et, pBuf, amt);.
d5910 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d 74 20 29 if( got==amt )
d5920 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
d5930 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 ITE_OK;. }else
d5940 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 if( got<0 ){.
d5950 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 /* lastErrno se
d5960 74 20 62 79 20 73 65 65 6b 41 6e 64 52 65 61 64 t by seekAndRead
d5970 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 */. return S
d5980 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
d5990 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 28 ;. }else{. (
d59a0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
d59b0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f lastErrno = 0; /
d59c0 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 * not a system e
d59d0 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 rror */. /* U
d59e0 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 nread parts of t
d59f0 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 he buffer must b
d5a00 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f e zero-filled */
d5a10 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 . memset(&((c
d5a20 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c har*)pBuf)[got],
d5a30 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 0, amt-got);.
d5a40 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d5a50 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
d5a60 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
d5a70 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 eek to the offse
d5a80 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 t in id->offset
d5a90 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 then read cnt by
d5aa0 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a tes into pBuf..*
d5ab0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
d5ac0 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
d5ad0 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 ually read. Upd
d5ae0 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a ate the offset..
d5af0 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 **.** To avoid s
d5b00 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e tomping the errn
d5b10 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 o value on a fai
d5b20 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 led write the la
d5b30 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a stErrno value.**
d5b40 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 is set before r
d5b50 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 eturning..*/.sta
d5b60 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 tic int seekAndW
d5b70 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 rite(unixFile *i
d5b80 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 d, i64 offset, c
d5b90 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c onst void *pBuf,
d5ba0 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 int cnt){. int
d5bb0 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f got;. i64 newO
d5bc0 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 ffset;. TIMER_S
d5bd0 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 TART;.#if define
d5be0 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 d(USE_PREAD). g
d5bf0 6f 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e ot = pwrite(id->
d5c00 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 h, pBuf, cnt, of
d5c10 66 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 fset);.#elif def
d5c20 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 ined(USE_PREAD64
d5c30 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 ). got = pwrite
d5c40 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 64(id->h, pBuf,
d5c50 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 cnt, offset);.#e
d5c60 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 lse. newOffset
d5c70 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f = lseek(id->h, o
d5c80 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 ffset, SEEK_SET)
d5c90 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 ;. if( newOffse
d5ca0 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 t!=offset ){.
d5cb0 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d if( newOffset =
d5cc0 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28 = -1 ){. ((
d5cd0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
d5ce0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
d5cf0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
d5d00 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
d5d10 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
d5d20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 0;.... }.
d5d30 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 return -1;. }.
d5d40 20 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d got = write(id-
d5d50 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a >h, pBuf, cnt);.
d5d60 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 #endif. TIMER_E
d5d70 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 ND;. if( got<0
d5d80 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c ){. ((unixFil
d5d90 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e e*)id)->lastErrn
d5da0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a o = errno;. }..
d5db0 20 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 OSTRACE5("WRIT
d5dc0 45 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c E %-3d %5d %7l
d5dd0 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e ld %llu\n", id->
d5de0 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 h, got, offset,
d5df0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a TIMER_ELAPSED);.
d5e00 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a return got;.}.
d5e10 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
d5e20 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 ta from a buffer
d5e30 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 into a file. R
d5e40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
d5e50 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 on success.** or
d5e60 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f some other erro
d5e70 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 r code on failur
d5e80 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
d5e90 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71 unixWrite(. sq
d5ea0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d5eb0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 . const void *p
d5ec0 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c Buf, . int amt,
d5ed0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
d5ee0 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 69 6e offset .){. in
d5ef0 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 t wrote = 0;. a
d5f00 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 61 ssert( id );. a
d5f10 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a ssert( amt>0 );.
d5f20 0a 20 20 2f 2a 20 4e 65 76 65 72 20 72 65 61 64 . /* Never read
d5f30 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 6f 66 or write any of
d5f40 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 the bytes in th
d5f50 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
d5f60 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 28 75 */. assert( ((u
d5f70 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 73 nixFile*)id)->is
d5f80 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 20 Lockable==0.
d5f90 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e || offset>
d5fa0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 =PENDING_BYTE+51
d5fb0 32 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6f 2. || o
d5fc0 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 ffset+amt<=PENDI
d5fd0 4e 47 5f 42 59 54 45 20 29 3b 0a 0a 23 69 66 6e NG_BYTE );..#ifn
d5fe0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 def NDEBUG. /*
d5ff0 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 If we are doing
d6000 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 74 a normal write t
d6010 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c o a database fil
d6020 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 6f e (as opposed to
d6030 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 6f . ** doing a ho
d6040 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
d6050 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 6f ck or a write to
d6060 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 72 some file other
d6070 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f 72 than a. ** nor
d6080 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c mal database fil
d6090 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20 74 e) then record t
d60a0 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
d60b0 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68 database. ** h
d60c0 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 as changed. If
d60d0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
d60e0 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 66 counter is modif
d60f0 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61 74 ied, record that
d6100 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e 0a . ** fact too..
d6110 20 20 2a 2f 0a 20 20 69 66 28 20 28 28 75 6e 69 */. if( ((uni
d6120 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 6e 4e 6f xFile*)id)->inNo
d6130 72 6d 61 6c 57 72 69 74 65 20 29 7b 0a 20 20 20 rmalWrite ){.
d6140 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d6150 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d6160 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 55 ;. pFile->dbU
d6170 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a 20 54 pdate = 1; /* T
d6180 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 he database has
d6190 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 2a 2f been modified */
d61a0 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 3c . if( offset<
d61b0 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b 61 6d =24 && offset+am
d61c0 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 20 69 t>=27 ){. i
d61d0 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 68 61 nt rc;. cha
d61e0 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a 20 20 r oldCntr[4];.
d61f0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
d6200 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 rorBenign(1);.
d6210 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 6e 64 rc = seekAnd
d6220 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c 20 Read(pFile, 24,
d6230 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20 20 oldCntr, 4);.
d6240 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 SimulateIOErr
d6250 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 orBenign(0);.
d6260 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c 7c 20 if( rc!=4 ||
d6270 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 2c 20 memcmp(oldCntr,
d6280 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 32 &((char*)pBuf)[2
d6290 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 3d 30 4-offset], 4)!=0
d62a0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d62b0 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 e->transCntrChng
d62c0 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 74 72 = 1; /* The tr
d62d0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
d62e0 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a 2f r has changed */
d62f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d6300 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 68 69 }.#endif.. whi
d6310 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 72 le( amt>0 && (wr
d6320 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 ote = seekAndWri
d6330 74 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 te((unixFile*)id
d6340 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 , offset, pBuf,
d6350 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 amt))>0 ){. a
d6360 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 mt -= wrote;.
d6370 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 offset += wrote
d6380 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 ;. pBuf = &((
d6390 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 char*)pBuf)[wrot
d63a0 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 e];. }. Simula
d63b0 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 teIOError(( wrot
d63c0 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 e=(-1), amt=1 ))
d63d0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ;. SimulateDisk
d63e0 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 fullError(( wrot
d63f0 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 e=0, amt=1 ));.
d6400 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 if( amt>0 ){.
d6410 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b if( wrote<0 ){
d6420 0a 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 . /* lastEr
d6430 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 rno set by seekA
d6440 6e 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 ndWrite */.
d6450 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
d6460 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 OERR_WRITE;.
d6470 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 }else{. ((u
d6480 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
d6490 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 stErrno = 0; /*
d64a0 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 not a system err
d64b0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 or */. retu
d64c0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a rn SQLITE_FULL;.
d64d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
d64e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
d64f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
d6500 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 EST./*.** Count
d6510 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 the number of fu
d6520 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d llsyncs and norm
d6530 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 al syncs. This
d6540 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a is used to test.
d6550 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e ** that syncs an
d6560 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 d fullsyncs are
d6570 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65 occurring at the
d6580 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f right times..*/
d6590 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
d65a0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 sqlite3_sync_cou
d65b0 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 nt = 0;.SQLITE_A
d65c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
d65d0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 ullsync_count =
d65e0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
d65f0 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74 We do not trust
d6600 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76 systems to prov
d6610 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64 ide a working fd
d6620 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65 atasync(). Some
d6630 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64 do..** Others d
d6640 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66 o no. To be saf
d6650 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b e, we will stick
d6660 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65 with the (slowe
d6670 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49 r) fsync()..** I
d6680 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20 f you know that
d6690 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73 your system does
d66a0 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79 support fdatasy
d66b0 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a nc() correctly,.
d66c0 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63 ** then simply c
d66d0 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64 ompile with -Dfd
d66e0 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e atasync=fdatasyn
d66f0 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 c.*/.#if !define
d6700 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20 d(fdatasync) &&
d6710 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 !defined(__linux
d6720 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61 __).# define fda
d6730 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e tasync fsync.#en
d6740 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e dif../*.** Defin
d6750 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 e HAVE_FULLFSYNC
d6760 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e to 0 or 1 depen
d6770 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 ding on whether
d6780 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f or not.** the F_
d6790 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20 FULLFSYNC macro
d67a0 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46 is defined. F_F
d67b0 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72 ULLFSYNC is curr
d67c0 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76 ently.** only av
d67d0 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f ailable on Mac O
d67e0 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63 S X. But that c
d67f0 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a ould change..*/.
d6800 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59 #ifdef F_FULLFSY
d6810 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 NC.# define HAVE
d6820 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c _FULLFSYNC 1.#el
d6830 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 se.# define HAVE
d6840 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e _FULLFSYNC 0.#en
d6850 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 dif.../*.** The
d6860 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63 fsync() system c
d6870 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 all does not wor
d6880 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20 k as advertised
d6890 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20 on many.** unix
d68a0 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f systems. The fo
d68b0 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72 llowing procedur
d68c0 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 e is an attempt
d68d0 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f to make.** it wo
d68e0 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a rk better..**.**
d68f0 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 The SQLITE_NO_S
d6900 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c YNC macro disabl
d6910 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e es all fsync()s.
d6920 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c This is useful
d6930 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20 .** for testing
d6940 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 when we want to
d6950 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 run through the
d6960 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b test suite quick
d6970 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73 ly..** You are s
d6980 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20 trongly advised
d6990 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20 *not* to deploy
d69a0 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 with SQLITE_NO_S
d69b0 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 YNC.** enabled,
d69c0 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77 however, since w
d69d0 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 ith SQLITE_NO_SY
d69e0 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f NC enabled, an O
d69f0 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f S crash.** or po
d6a00 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c wer failure will
d6a10 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20 likely corrupt
d6a20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
d6a30 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 e..**.** SQLite
d6a40 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c sets the dataOnl
d6a50 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69 y flag if the si
d6a60 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
d6a70 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 s unchanged..**
d6a80 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20 The idea behind
d6a90 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74 dataOnly is that
d6aa0 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 it should only
d6ab0 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63 write the file c
d6ac0 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73 ontent.** to dis
d6ad0 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65 k, not the inode
d6ae0 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64 . We only set d
d6af0 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66 ataOnly if the f
d6b00 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20 ile size is .**
d6b10 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 unchanged since
d6b20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 the file size is
d6b30 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f part of the ino
d6b40 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a de. However, .*
d6b50 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73 * Ted Ts'o tells
d6b60 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79 us that fdatasy
d6b70 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77 nc() will also w
d6b80 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69 rite the inode i
d6b90 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69 f the.** file si
d6ba0 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 ze has changed.
d6bb0 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64 The only real d
d6bc0 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
d6bd0 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a n fdatasync().**
d6be0 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65 and fsync(), Te
d6bf0 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74 d tells us, is t
d6c00 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20 hat fdatasync()
d6c10 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74 will not flush t
d6c20 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74 he.** inode if t
d6c30 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65 he mtime or owne
d6c40 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65 r or other inode
d6c50 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65 attributes have
d6c60 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20 changed..** We
d6c70 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20 only care about
d6c80 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e the file size, n
d6c90 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c ot the other fil
d6ca0 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f e attributes, so
d6cb0 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51 .** as far as SQ
d6cc0 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65 Lite is concerne
d6cd0 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28 d, an fdatasync(
d6ce0 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71 ) is always adeq
d6cf0 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20 uate..** So, we
d6d00 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61 always use fdata
d6d10 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20 sync() if it is
d6d20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72 available, regar
d6d30 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20 dless of.** the
d6d40 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 value of the dat
d6d50 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73 aOnly flag..*/.s
d6d60 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 tatic int full_f
d6d70 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 sync(int fd, int
d6d80 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 fullSync, int d
d6d90 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 ataOnly){. int
d6da0 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f rc;.. /* The fo
d6db0 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 llowing "ifdef/e
d6dc0 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b lif/else/" block
d6dd0 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 has the same st
d6de0 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20 ructure as. **
d6df0 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 the one below. I
d6e00 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 t is replicated
d6e10 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 here solely to a
d6e20 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 void cluttering
d6e30 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61 . ** up the rea
d6e40 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 l code with the
d6e50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
d6e60 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a () macros.. */.
d6e70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f #ifdef SQLITE_NO
d6e80 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 _SYNC. UNUSED_P
d6e90 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 ARAMETER(fd);.
d6ea0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
d6eb0 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e (fullSync);. UN
d6ec0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 USED_PARAMETER(d
d6ed0 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20 ataOnly);.#elif
d6ee0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 HAVE_FULLFSYNC.
d6ef0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d6f00 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c R(dataOnly);.#el
d6f10 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
d6f20 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b METER(fullSync);
d6f30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
d6f40 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
d6f50 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f endif.. /* Reco
d6f60 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rd the number of
d6f70 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64 times that we d
d6f80 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 o a normal fsync
d6f90 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c () and . ** FUL
d6fa0 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20 LSYNC. This is
d6fb0 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 used during test
d6fc0 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 ing to verify th
d6fd0 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 at this procedur
d6fe0 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c e. ** gets call
d6ff0 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 ed with the corr
d7000 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 ect arguments..
d7010 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
d7020 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c E_TEST. if( ful
d7030 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f lSync ) sqlite3_
d7040 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b fullsync_count++
d7050 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 ;. sqlite3_sync
d7060 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
d7070 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d .. /* If we com
d7080 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 piled with the S
d7090 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c QLITE_NO_SYNC fl
d70a0 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 ag, then syncing
d70b0 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 is a. ** no-op
d70c0 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
d70d0 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 ITE_NO_SYNC. rc
d70e0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 = SQLITE_OK;.#e
d70f0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 lif HAVE_FULLFSY
d7100 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e NC. if( fullSyn
d7110 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63 c ){. rc = fc
d7120 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53 ntl(fd, F_FULLFS
d7130 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 YNC, 0);. }else
d7140 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 {. rc = 1;.
d7150 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55 }. /* If the FU
d7160 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20 LLFSYNC failed,
d7170 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74 fall back to att
d7180 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63 empting an fsync
d7190 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75 ().. ** It shou
d71a0 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c ldn't be possibl
d71b0 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20 e for fullfsync
d71c0 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c to fail on the l
d71d0 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 ocal . ** file
d71e0 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c system (on OSX),
d71f0 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69 so failure indi
d7200 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46 cates that FULLF
d7210 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20 SYNC. ** isn't
d7220 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68 supported for th
d7230 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 is file system.
d7240 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66 So, attempt an f
d7250 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28 sync . ** and (
d7260 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20 for now) ignore
d7270 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20 the overhead of
d7280 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63 a superfluous fc
d7290 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a ntl call. . **
d72a0 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20 It'd be better
d72b0 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73 to detect fullfs
d72c0 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65 ync support once
d72d0 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a and avoid . **
d72e0 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20 the fcntl call
d72f0 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20 every time sync
d7300 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a is called.. */.
d7310 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20 if( rc ) rc =
d7320 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73 fsync(fd);..#els
d7330 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73 e . rc = fdatas
d7340 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f ync(fd);.#if OS_
d7350 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 VXWORKS. if( rc
d7360 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45 ==-1 && errno==E
d7370 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63 NOTSUP ){. rc
d7380 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 = fsync(fd);.
d7390 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 }.#endif /* OS_V
d73a0 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66 XWORKS */.#endif
d73b0 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45 /* ifdef SQLITE
d73c0 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41 _NO_SYNC elif HA
d73d0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a VE_FULLFSYNC */.
d73e0 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b . if( OS_VXWORK
d73f0 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a S && rc!= -1 ){.
d7400 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a rc = 0;. }.
d7410 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
d7420 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
d7430 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 all writes to a
d7440 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 particular file
d7450 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f are committed to
d7460 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 disk..**.** If
d7470 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e dataOnly==0 then
d7480 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69 both the file i
d7490 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65 tself and its me
d74a0 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20 tadata (file.**
d74b0 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d size, access tim
d74c0 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63 e, etc) are sync
d74d0 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79 ed. If dataOnly
d74e0 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 !=0 then only th
d74f0 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69 e.** file data i
d7500 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 s synced..**.**
d7510 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f Under Unix, also
d7520 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
d7530 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e the directory en
d7540 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 try for the file
d7550 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65 .** has been cre
d7560 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e ated by fsync-in
d7570 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 g the directory
d7580 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
d7590 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65 e file..** If we
d75a0 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 do not do this
d75b0 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 and we encounter
d75c0 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
d75d0 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a , the directory.
d75e0 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ** entry for the
d75f0 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e journal might n
d7600 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77 ot exist after w
d7610 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e e reboot. The n
d7620 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f ext.** SQLite to
d7630 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 access the file
d7640 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74 will not know t
d7650 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
d7660 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a exists (because.
d7670 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 ** the directory
d7680 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a entry for the j
d7690 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72 ournal was never
d76a0 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68 created) and th
d76b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a e transaction.**
d76c0 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62 will not roll b
d76d0 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c ack - possibly l
d76e0 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 eading to databa
d76f0 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a se corruption..*
d7700 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d7710 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 xSync(sqlite3_fi
d7720 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 le *id, int flag
d7730 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 s){. int rc;.
d7740 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d7750 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d7760 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e .. int isDataOn
d7770 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49 ly = (flags&SQLI
d7780 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
d7790 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73 );. int isFulls
d77a0 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30 ync = (flags&0x0
d77b0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f F)==SQLITE_SYNC_
d77c0 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 FULL;.. /* Chec
d77d0 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51 k that one of SQ
d77e0 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
d77f0 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73 or FULL was pas
d7800 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 sed */. assert(
d7810 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 (flags&0x0F)==SQ
d7820 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
d7830 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73 . || (flags
d7840 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 &0x0F)==SQLITE_S
d7850 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20 YNC_FULL. );..
d7860 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c /* Unix cannot,
d7870 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d but some system
d7880 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c s may return SQL
d7890 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65 ITE_FULL from he
d78a0 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69 re. This. ** li
d78b0 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 ne is to test th
d78c0 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73 at doing so does
d78d0 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70 not cause any p
d78e0 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20 roblems.. */.
d78f0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
d7900 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
d7910 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20 LITE_FULL );..
d7920 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d7930 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e . OSTRACE2("SYN
d7940 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 C %-3d\n", pF
d7950 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20 ile->h);. rc =
d7960 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 full_fsync(pFile
d7970 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c ->h, isFullsync,
d7980 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 isDataOnly);.
d7990 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
d79a0 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 rc=1 );. if( r
d79b0 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e c ){. pFile->
d79c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
d79d0 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 o;. return SQ
d79e0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 LITE_IOERR_FSYNC
d79f0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c ;. }. if( pFil
d7a00 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 e->dirfd>=0 ){.
d7a10 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20 int err;.
d7a20 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e OSTRACE4("DIRSYN
d7a30 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c C %-3d (have_ful
d7a40 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79 lfsync=%d fullsy
d7a50 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 nc=%d)\n", pFile
d7a60 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20 ->dirfd,.
d7a70 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53 HAVE_FULLFS
d7a80 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29 YNC, isFullsync)
d7a90 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
d7aa0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 _DISABLE_DIRSYNC
d7ab0 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 . /* The dire
d7ac0 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e ctory sync is on
d7ad0 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 ly attempted if
d7ae0 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20 full_fsync is.
d7af0 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20 ** turned off
d7b00 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20 or unavailable.
d7b10 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63 If a full_fsync
d7b20 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c occurred above,
d7b30 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 . ** then the
d7b40 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 directory sync
d7b50 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a is superfluous..
d7b60 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 */. if( (
d7b70 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 !HAVE_FULLFSYNC
d7b80 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20 || !isFullsync)
d7b90 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 && full_fsync(pF
d7ba0 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20 ile->dirfd,0,0)
d7bb0 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 ){. /*.
d7bc0 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72 ** We have r
d7bd0 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65 eceived multiple
d7be0 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e reports of fsyn
d7bf0 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20 c() returning.
d7c00 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77 ** errors w
d7c10 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64 hen applied to d
d7c20 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65 irectories on ce
d7c30 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65 rtain file syste
d7c40 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20 ms.. ** A
d7c50 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79 failed directory
d7c60 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62 sync is not a b
d7c70 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20 ig deal. So it
d7c80 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20 seems. **
d7c90 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65 better to ignore
d7ca0 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63 the error. Tic
d7cb0 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20 ket #1657.
d7cc0 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46 */. /* pF
d7cd0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d7ce0 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20 errno; */.
d7cf0 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 /* return SQLI
d7d00 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 TE_IOERR; */.
d7d10 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72 }.#endif. er
d7d20 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d r = close(pFile-
d7d30 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79 >dirfd); /* Only
d7d40 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e need to sync on
d7d50 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65 ce, so close the
d7d60 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d */. if( err=
d7d70 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 =0 ){
d7d80 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20 /* directory
d7d90 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65 when we are done
d7da0 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d */. pFile-
d7db0 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20 >dirfd = -1;.
d7dc0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 }else{. pF
d7dd0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d7de0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 errno;. rc
d7df0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
d7e00 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d DIR_CLOSE;. }
d7e10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
d7e20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 ;.}../*.** Trunc
d7e30 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 ate an open file
d7e40 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 to a specified
d7e50 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 size.*/.static i
d7e60 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28 nt unixTruncate(
d7e70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d7e80 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 , i64 nByte){.
d7e90 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
d7ea0 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61 ( id );. Simula
d7eb0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
d7ec0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
d7ed0 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 RUNCATE );. rc
d7ee0 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e = ftruncate(((un
d7ef0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 ixFile*)id)->h,
d7f00 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20 (off_t)nByte);.
d7f10 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28 if( rc ){. (
d7f20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
d7f30 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
d7f40 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 o;. return SQ
d7f50 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
d7f60 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ATE;. }else{.
d7f70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d7f80 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a OK;. }.}../*.**
d7f90 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 Determine the c
d7fa0 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 urrent size of a
d7fb0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a file in bytes.*
d7fc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d7fd0 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 xFileSize(sqlite
d7fe0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 3_file *id, i64
d7ff0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 *pSize){. int r
d8000 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 c;. struct stat
d8010 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 buf;. assert(
d8020 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74 id );. rc = fst
d8030 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 at(((unixFile*)i
d8040 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 d)->h, &buf);.
d8050 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
d8060 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 rc=1 );. if( r
d8070 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e c!=0 ){. ((un
d8080 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
d8090 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d80a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d80b0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 E_IOERR_FSTAT;.
d80c0 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75 }. *pSize = bu
d80d0 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a f.st_size;.. /*
d80e0 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 When opening a
d80f0 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61 zero-size databa
d8100 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b se, the findLock
d8110 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65 Info() procedure
d8120 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73 . ** writes a s
d8130 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20 ingle byte into
d8140 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64 that file in ord
d8150 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e er to work aroun
d8160 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20 d a bug. ** in
d8170 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 the OS-X msdos f
d8180 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f ilesystem. In o
d8190 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 rder to avoid pr
d81a0 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65 oblems with uppe
d81b0 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77 r. ** layers, w
d81c0 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74 e need to report
d81d0 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20 this file size
d81e0 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f as zero even tho
d81f0 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72 ugh it is. ** r
d8200 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65 eally 1. Ticke
d8210 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 t #3260.. */.
d8220 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 if( *pSize==1 )
d8230 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 *pSize = 0;...
d8240 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d8250 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f ;.}..#if SQLITE_
d8260 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
d8270 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
d8280 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a __APPLE__)./*.**
d8290 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f Handler for pro
d82a0 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d xy-locking file-
d82b0 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20 control verbs.
d82c0 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e Defined below in
d82d0 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 the.** proxying
d82e0 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f locking divisio
d82f0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 n..*/.static int
d8300 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f proxyFileContro
d8310 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c l(sqlite3_file*,
d8320 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 int,void*);.#end
d8330 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 if.../*.** Infor
d8340 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 mation and contr
d8350 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 ol of an open fi
d8360 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 le handle..*/.st
d8370 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c atic int unixFil
d8380 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
d8390 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
d83a0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
d83b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
d83c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
d83d0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a FCNTL_LOCKSTATE:
d83e0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 {. *(int*)
d83f0 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c pArg = ((unixFil
d8400 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 e*)id)->locktype
d8410 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
d8420 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
d8430 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
d8440 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 LAST_ERRNO: {.
d8450 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
d8460 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = ((unixFile*)id
d8470 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 )->lastErrno;.
d8480 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d8490 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e E_OK;. }.#ifn
d84a0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f def NDEBUG. /
d84b0 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c * The pager call
d84c0 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f s this method to
d84d0 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 signal that it
d84e0 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 has done. **
d84f0 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 a rollback and t
d8500 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
d8510 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e is therefore un
d8520 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 changed and.
d8530 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 ** it hence it i
d8540 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 s OK for the tra
d8550 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 nsaction change
d8560 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 counter to be.
d8570 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a ** unchanged..
d8580 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
d8590 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f SQLITE_FCNTL_DB_
d85a0 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 UNCHANGED: {.
d85b0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
d85c0 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 d)->dbUpdate = 0
d85d0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
d85e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
d85f0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
d8600 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
d8610 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
d8620 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 d(__APPLE__).
d8630 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 case SQLITE_SET
d8640 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a _LOCKPROXYFILE:.
d8650 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
d8660 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c GET_LOCKPROXYFIL
d8670 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 E: {. retur
d8680 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 n proxyFileContr
d8690 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a ol(id,op,pArg);.
d86a0 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.#endif /*
d86b0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
d86c0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 CKING_STYLE && d
d86d0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
d86e0 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 ) */. }. retur
d86f0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
d8700 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
d8710 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
d8720 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 in bytes of the
d8730 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b underlying block
d8740 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 device for.** t
d8750 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c he specified fil
d8760 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 e. This is almos
d8770 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 t always 512 byt
d8780 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a es, but may be.*
d8790 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d * larger for som
d87a0 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a e devices..**.**
d87b0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 SQLite code ass
d87c0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 umes this functi
d87d0 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 on cannot fail.
d87e0 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 It also assumes
d87f0 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 that.** if two f
d8800 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 iles are created
d8810 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c in the same fil
d8820 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f e-system directo
d8830 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 ry (i.e..** a da
d8840 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a tabase and its j
d8850 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 ournal file) tha
d8860 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a t the sector siz
d8870 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a e will be the.**
d8880 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a same for both..
d8890 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
d88a0 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c ixSectorSize(sql
d88b0 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
d88c0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
d88d0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
d88e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
d88f0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
d8900 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 _SIZE;.}../*.**
d8910 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 Return the devic
d8920 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
d8930 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 s for the file.
d8940 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 This is always 0
d8950 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 for unix..*/.st
d8960 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 atic int unixDev
d8970 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
d8980 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
d8990 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 *NotUsed){. UNU
d89a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
d89b0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
d89c0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 0;.}../*.** Her
d89d0 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65 e ends the imple
d89e0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c mentation of all
d89f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 sqlite3_file me
d8a00 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a thods..**.******
d8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a20 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c End sqlite3_fil
d8a30 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a e Methods ******
d8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
d8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8aa0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
d8ab0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 This division c
d8ac0 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 ontains definiti
d8ad0 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 ons of sqlite3_i
d8ae0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
d8af0 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d s that.** implem
d8b00 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65 ent various file
d8b10 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
d8b20 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f ies. It also co
d8b30 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f ntains definitio
d8b40 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72 ns.** of "finder
d8b50 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20 " functions. A
d8b60 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
d8b70 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 is used to locat
d8b80 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 e the appropriat
d8b90 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f e.** sqlite3_io_
d8ba0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66 methods object f
d8bb0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 or a particular
d8bc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
d8bd0 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20 The pAppData.**
d8be0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c field of the sql
d8bf0 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a ite3_vfs VFS obj
d8c00 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c ects are initial
d8c10 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74 ized to be point
d8c20 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f ers to.** the co
d8c30 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e rrect finder-fun
d8c40 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56 ction for that V
d8c50 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 FS..**.** Most f
d8c60 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 inder functions
d8c70 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
d8c80 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 to a fixed sqli
d8c90 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a te3_io_methods.*
d8ca0 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f * object. The o
d8cb0 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20 nly interesting
d8cc0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
d8cd0 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e is autolockIoFin
d8ce0 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f der, which.** lo
d8cf0 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 oks at the files
d8d00 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74 ystem type and t
d8d10 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68 ries to guess th
d8d20 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a e best locking.*
d8d30 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20 * strategy from
d8d40 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 that..**.** For
d8d50 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 finder-funtion F
d8d60 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 , two objects ar
d8d70 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a e created:.**.**
d8d80 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c (1) The real
d8d90 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
d8da0 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 named "FImpt()"
d8db0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41 ..**.** (2) A
d8dc0 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65 constant pointe
d8dd0 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 r to this functi
d8de0 6f 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 22 o named just "F"
d8df0 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 ..**.**.** A poi
d8e00 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 6f nter to the F po
d8e10 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
d8e20 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 61 the pAppData va
d8e30 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 6f lue for VFS.** o
d8e40 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 65 bjects. We have
d8e50 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 74 to do this inst
d8e60 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 70 ead of letting p
d8e70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a 2a AppData point.**
d8e80 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 65 directly at the
d8e90 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
d8ea0 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 73 since C90 rules
d8eb0 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 2a prevent a void*
d8ec0 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 74 .** from be cast
d8ed0 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e into a function
d8ee0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a pointer..**.**.
d8ef0 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 ** Each instance
d8f00 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 67 of this macro g
d8f10 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 6a enerates two obj
d8f20 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 ects:.**.** *
d8f30 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c 69 A constant sqli
d8f40 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
d8f50 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 4f bject call METHO
d8f60 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 69 D that has locki
d8f70 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f ng.** metho
d8f80 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 ds CLOSE, LOCK,
d8f90 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 UNLOCK, CKRESLOC
d8fa0 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e K..**.** * An
d8fb0 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64 I/O method find
d8fc0 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c er function call
d8fd0 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 72 ed FINDER that r
d8fe0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
d8ff0 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 .** to the
d9000 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 6e METHOD object in
d9010 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 75 the previous bu
d9020 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 llet..*/.#define
d9030 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 45 IOMETHODS(FINDE
d9040 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 45 R, METHOD, CLOSE
d9050 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 , LOCK, UNLOCK,
d9060 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 20 CKLOCK)
d9070 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 \.static c
d9080 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
d9090 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 3d methods METHOD =
d90a0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
d90b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d90c0 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 20 \. 1,
d90d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d90e0 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
d90f0 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 n */
d9100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9110 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 \. CLOSE,
d9120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9130 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a /* xClose *
d9140 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
d9150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9160 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 64 \. unixRead
d9170 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
d9180 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20 /* xRead */
d9190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d91a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d91b0 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 65 \. unixWrite
d91c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
d91d0 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 20 /* xWrite */
d91e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d91f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9200 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 61 \. unixTrunca
d9210 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
d9220 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a /* xTruncate *
d9230 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
d9240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9250 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20 \. unixSync,
d9260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9270 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20 /* xSync */
d9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
d92a0 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 . unixFileSize
d92b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
d92c0 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20 /* xFileSize */
d92d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d92e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
d92f0 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 LOCK,
d9300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d9310 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 * xLock */
d9320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9330 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
d9340 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 20 UNLOCK,
d9350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d9360 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 xUnlock */
d9370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9380 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
d9390 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 CKLOCK,
d93a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d93b0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
d93c0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 ck */
d93d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
d93e0 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c unixFileControl,
d93f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
d9400 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 FileControl */
d9410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9420 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 \. u
d9430 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 20 nixSectorSize,
d9440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 /* xS
d9450 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 20 ectorSize */
d9460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9470 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e \. un
d9480 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 ixDeviceCharacte
d9490 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 65 ristics /* xDe
d94a0 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 viceCapabilities
d94b0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
d94c0 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 20 \.};
d94d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d94e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d94f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9510 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 \.static
d9520 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
d9530 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 52 _methods *FINDER
d9540 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61 ##Impl(const cha
d9550 72 20 2a 7a 2c 20 69 6e 74 20 68 29 7b 20 20 20 r *z, int h){
d9560 20 20 20 20 20 20 5c 0a 20 20 55 4e 55 53 45 44 \. UNUSED
d9570 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 55 _PARAMETER(z); U
d9580 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
d9590 68 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 h);
d95a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d95b0 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e 20 \. return
d95c0 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 20 &METHOD;
d95d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d95e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d95f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9600 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 20 \.}
d9610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9650 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 \.static cons
d9660 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
d9670 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 49 hods *(*const FI
d9680 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 72 NDER)(const char
d9690 2a 2c 69 6e 74 29 20 20 20 20 20 20 20 20 20 20 *,int)
d96a0 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 52 \. = FINDER
d96b0 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48 ##Impl;../*.** H
d96c0 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 74 ere are all of t
d96d0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
d96e0 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f thods objects fo
d96f0 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a r each of the.**
d9700 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
d9710 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 20 ies. Functions
d9720 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 6e that return poin
d9730 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d 65 ters to these me
d9740 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c 73 thods.** are als
d9750 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f o created..*/.IO
d9760 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 78 METHODS(. posix
d9770 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
d9780 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
d9790 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
d97a0 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 posixIoMethods
d97b0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 , /* s
d97c0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
d97d0 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f s object name */
d97e0 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20 . unixClose,
d97f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d9800 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
d9810 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 . unixLock,
d9820 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d9830 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
d9840 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 20 unixUnlock,
d9850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
d9860 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
d9870 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 . unixCheckRese
d9880 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a 20 rvedLock /*
d9890 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
d98a0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 ck method */.).I
d98b0 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f OMETHODS(. nolo
d98c0 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 ckIoFinder,
d98d0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
d98e0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
d98f0 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f . nolockIoMetho
d9900 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ds, /*
d9910 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
d9920 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
d9930 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c /. nolockClose,
d9940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d9950 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
d9960 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20 /. nolockLock,
d9970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d9980 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
d9990 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c . nolockUnlock,
d99a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d99b0 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
d99c0 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 /. nolockCheckR
d99d0 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a eservedLock /*
d99e0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
d99f0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
d9a00 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74 IOMETHODS(. dot
d9a10 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 lockIoFinder,
d9a20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
d9a30 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
d9a40 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 /. dotlockIoMet
d9a50 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a hods, /*
d9a60 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
d9a70 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
d9a80 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 */. dotlockClos
d9a90 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e, /
d9aa0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 * xClose method
d9ab0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b */. dotlockLock
d9ac0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
d9ad0 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a * xLock method *
d9ae0 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 /. dotlockUnloc
d9af0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a k, /*
d9b00 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 xUnlock method
d9b10 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 63 */. dotlockChec
d9b20 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 2f kReservedLock /
d9b30 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
d9b40 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
d9b50 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
d9b60 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
d9b70 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 E && !OS_VXWORKS
d9b80 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c .IOMETHODS(. fl
d9b90 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 ockIoFinder,
d9ba0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
d9bb0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
d9bc0 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68 */. flockIoMeth
d9bd0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ods, /
d9be0 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
d9bf0 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
d9c00 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65 */. flockClose
d9c10 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
d9c20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
d9c30 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c */. flockLock,
d9c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9c50 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
d9c60 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b */. flockUnlock
d9c70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
d9c80 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
d9c90 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b */. flockCheck
d9ca0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 ReservedLock
d9cb0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
d9cc0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
d9cd0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53 ).#endif..#if OS
d9ce0 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f _VXWORKS.IOMETHO
d9cf0 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65 DS(. semIoFinde
d9d00 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
d9d10 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
d9d20 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d on name */. sem
d9d30 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 IoMethods,
d9d40 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
d9d50 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
d9d60 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 ect name */. se
d9d70 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 mClose,
d9d80 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
d9d90 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 e method */. se
d9da0 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 mLock,
d9db0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
d9dc0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d method */. sem
d9dd0 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
d9de0 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
d9df0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 k method */. se
d9e00 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f mCheckReservedLo
d9e10 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 ck /* xChec
d9e20 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
d9e30 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 thod */.).#endif
d9e40 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f ..#if defined(__
d9e50 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
d9e60 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
d9e70 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44 G_STYLE.IOMETHOD
d9e80 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 72 S(. afpIoFinder
d9e90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
d9ea0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
d9eb0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49 n name */. afpI
d9ec0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
d9ed0 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
d9ee0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
d9ef0 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 ct name */. afp
d9f00 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
d9f10 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
d9f20 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
d9f30 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 Lock,
d9f40 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
d9f50 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 method */. afpU
d9f60 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 nlock,
d9f70 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
d9f80 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
d9f90 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d9fa0 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b k /* xCheck
d9fb0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
d9fc0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a hod */.).#endif.
d9fd0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 ./*.** The proxy
d9fe0 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 locking method
d9ff0 69 73 20 61 20 22 73 75 70 65 72 2d 6d 65 74 68 is a "super-meth
da000 6f 64 22 20 69 6e 20 74 68 65 20 73 65 6e 73 65 od" in the sense
da010 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e that it.** open
da020 73 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 s secondary file
da030 20 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f 72 descriptors for
da040 20 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c the conch and l
da050 6f 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a ock files and.**
da060 20 69 74 20 75 73 65 73 20 70 72 6f 78 79 2c 20 it uses proxy,
da070 64 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 dot-file, AFP, a
da080 6e 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 nd flock() locki
da090 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 ng methods on th
da0a0 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 ose.** secondary
da0b0 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 files. For thi
da0c0 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 s reason, the di
da0d0 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c vision that impl
da0e0 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 ements.** proxy
da0f0 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 locking is locat
da100 65 64 20 6d 75 63 68 20 66 75 72 74 68 65 72 20 ed much further
da110 64 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 down in the file
da120 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64 0a 2a . But we need.*
da130 2a 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e * to go ahead an
da140 64 20 64 65 66 69 6e 65 20 74 68 65 20 73 71 6c d define the sql
da150 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
da160 61 6e 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 and finder funct
da170 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 ion.** for proxy
da180 20 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 locking here.
da190 53 6f 20 77 65 20 66 6f 72 77 61 72 64 20 64 65 So we forward de
da1a0 63 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 clare the I/O me
da1b0 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 thods..*/.#if de
da1c0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
da1d0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
da1e0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
da1f0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
da200 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 Close(sqlite3_fi
da210 6c 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 le*);.static int
da220 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 proxyLock(sqlit
da230 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
da240 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
da250 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
da260 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 ile*, int);.stat
da270 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 ic int proxyChec
da280 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
da290 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
da2a0 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 *);.IOMETHODS(.
da2b0 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 proxyIoFinder,
da2c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
da2d0 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 nder function na
da2e0 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d me */. proxyIoM
da2f0 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 ethods,
da300 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f /* sqlite3_io_
da310 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e methods object n
da320 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c ame */. proxyCl
da330 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ose,
da340 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 /* xClose met
da350 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f hod */. proxyLo
da360 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
da370 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 /* xLock meth
da380 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c od */. proxyUnl
da390 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
da3a0 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 /* xUnlock met
da3b0 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68 hod */. proxyCh
da3c0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
da3d0 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 /* xCheckRese
da3e0 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 rvedLock method
da3f0 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 */.).#endif...#i
da400 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
da410 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
da420 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
da430 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 YLE./* .** This
da440 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f "finder" functio
da450 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 n attempts to de
da460 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 termine the best
da470 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
da480 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 y .** for the da
da490 74 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c tabase file "fil
da4a0 65 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e ePath". It then
da4b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c returns the sql
da4c0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a ite3_io_methods.
da4d0 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 ** object that i
da4e0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 mplements that s
da4f0 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 trategy..**.** T
da500 68 69 73 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 his is for MacOS
da510 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 X only..*/.stati
da520 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
da530 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f io_methods *auto
da540 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c lockIoFinderImpl
da550 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a (. const char *
da560 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 filePath, /*
da570 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
da580 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 base file */. i
da590 6e 74 20 66 64 20 20 20 20 20 20 20 20 20 20 20 nt fd
da5a0 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 /* file
da5b0 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 descriptor open
da5c0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
da5d0 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 file */.){. sta
da5e0 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 tic const struct
da5f0 20 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20 20 63 Mapping {. c
da600 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
da610 73 79 73 74 65 6d 3b 20 20 20 20 20 20 20 20 20 system;
da620 20 20 20 20 20 2f 2a 20 46 69 6c 65 73 79 73 74 /* Filesyst
da630 65 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a 2f 0a em type name */.
da640 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 const sqlite
da650 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
da660 65 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41 70 70 ethods; /* App
da670 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 ropriate locking
da680 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d 20 61 method */. } a
da690 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 Map[] = {. {
da6a0 22 68 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78 "hfs", &posix
da6b0 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 IoMethods },.
da6c0 20 7b 20 22 75 66 73 22 2c 20 20 20 20 26 70 6f { "ufs", &po
da6d0 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a sixIoMethods },.
da6e0 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c 20 20 { "afpfs",
da6f0 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c &afpIoMethods },
da700 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
da710 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e NABLE_AFP_LOCKIN
da720 47 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73 6d 62 G_SMB. { "smb
da730 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 68 fs", &afpIoMeth
da740 6f 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 ods },.#else.
da750 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 66 6c { "smbfs", &fl
da760 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a ockIoMethods },.
da770 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 77 65 #endif. { "we
da780 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f bdav", &nolockIo
da790 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b Methods },. {
da7a0 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20 20 69 0, 0 }. };. i
da7b0 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73 nt i;. struct s
da7c0 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 tatfs fsInfo;.
da7d0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
da7e0 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 kInfo;.. if( !f
da7f0 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f ilePath ){. /
da800 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e * If filePath==N
da810 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 ULL that means w
da820 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 e are dealing wi
da830 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 th a transient f
da840 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 ile. ** that
da850 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
da860 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 be locked. */.
da870 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 return &noloc
da880 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a kIoMethods;. }.
da890 20 20 69 66 28 20 73 74 61 74 66 73 28 66 69 6c if( statfs(fil
da8a0 65 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 ePath, &fsInfo)
da8b0 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69 66 28 != -1 ){. if(
da8c0 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 fsInfo.f_flags
da8d0 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a & MNT_RDONLY ){.
da8e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f return &no
da8f0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 lockIoMethods;.
da900 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 }. for(i=0
da910 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 ; aMap[i].zFiles
da920 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 ystem; i++){.
da930 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 66 73 if( strcmp(fs
da940 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d Info.f_fstypenam
da950 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 e, aMap[i].zFile
da960 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 system)==0 ){.
da970 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 return aMa
da980 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a 20 p[i].pMethods;.
da990 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
da9a0 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 .. /* Default c
da9b0 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d ase. Handles, am
da9c0 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22 6e ongst others, "n
da9d0 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74 20 62 fs".. ** Test b
da9e0 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 75 yte-range lock u
da9f0 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 49 66 sing fcntl(). If
daa00 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63 65 65 the call succee
daa10 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 6d 65 ds, . ** assume
daa20 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73 that the file-s
daa30 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 50 ystem supports P
daa40 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73 OSIX style locks
daa50 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e . . */. lockIn
daa60 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 fo.l_len = 1;.
daa70 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 lockInfo.l_start
daa80 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f = 0;. lockInfo
daa90 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b .l_whence = SEEK
daaa0 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f _SET;. lockInfo
daab0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 .l_type = F_RDLC
daac0 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 66 K;. if( fcntl(f
daad0 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 d, F_GETLK, &loc
daae0 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 kInfo)!=-1 ) {.
daaf0 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 return &posix
dab00 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c IoMethods;. }el
dab10 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 se{. return &
dab20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 dotlockIoMethods
dab30 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 ;. }.}.static c
dab40 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
dab50 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 methods *(*const
dab60 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
dab70 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 r)(const char*,i
dab80 6e 74 29 0a 20 20 20 20 20 20 20 20 3d 20 61 75 nt). = au
dab90 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d tolockIoFinderIm
daba0 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 pl;..#endif /* d
dabb0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
dabc0 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 ) && SQLITE_ENAB
dabd0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
dabe0 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f */..#if OS_VXWO
dabf0 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e RKS && SQLITE_EN
dac00 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
dac10 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 LE./* .** This "
dac20 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e finder" function
dac30 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 attempts to det
dac40 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 ermine the best
dac50 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 locking strategy
dac60 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 .** for the dat
dac70 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 abase file "file
dac80 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 Path". It then
dac90 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 returns the sqli
daca0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a te3_io_methods.*
dacb0 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d * object that im
dacc0 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 plements that st
dacd0 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rategy..**.** Th
dace0 69 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b is is for VXWork
dacf0 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 s only..*/.stati
dad00 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
dad10 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f io_methods *auto
dad20 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c lockIoFinderImpl
dad30 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a (. const char *
dad40 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 filePath, /*
dad50 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
dad60 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 base file */. i
dad70 6e 74 20 66 64 20 20 20 20 20 20 20 20 20 20 20 nt fd
dad80 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 /* file
dad90 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 descriptor open
dada0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
dadb0 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 file */.){. str
dadc0 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e uct flock lockIn
dadd0 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 fo;.. if( !file
dade0 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 Path ){. /* I
dadf0 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c f filePath==NULL
dae00 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61 that means we a
dae10 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 re dealing with
dae20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 a transient file
dae30 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 . ** that doe
dae40 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 s not need to be
dae50 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 locked. */.
dae60 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f return &nolockIo
dae70 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 0a 20 20 Methods;. }..
dae80 2f 2a 20 54 65 73 74 20 69 66 20 66 63 6e 74 6c /* Test if fcntl
dae90 28 29 20 69 73 20 73 75 70 70 6f 72 74 65 64 20 () is supported
daea0 61 6e 64 20 75 73 65 20 50 4f 53 49 58 20 73 74 and use POSIX st
daeb0 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20 20 2a 2a 20 yle locks.. **
daec0 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 62 Otherwise fall b
daed0 61 63 6b 20 74 6f 20 74 68 65 20 6e 61 6d 65 64 ack to the named
daee0 20 73 65 6d 61 70 68 6f 72 65 20 6d 65 74 68 6f semaphore metho
daef0 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e d.. */. lockIn
daf00 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 fo.l_len = 1;.
daf10 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 lockInfo.l_start
daf20 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f = 0;. lockInfo
daf30 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b .l_whence = SEEK
daf40 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f _SET;. lockInfo
daf50 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 .l_type = F_RDLC
daf60 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 66 K;. if( fcntl(f
daf70 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 d, F_GETLK, &loc
daf80 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 kInfo)!=-1 ) {.
daf90 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 return &posix
dafa0 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c IoMethods;. }el
dafb0 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 se{. return &
dafc0 73 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 semIoMethods;.
dafd0 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 }.}.static const
dafe0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
daff0 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 61 75 74 ods *(*const aut
db000 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 63 olockIoFinder)(c
db010 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 0a onst char*,int).
db020 20 20 20 20 20 20 20 20 3d 20 61 75 74 6f 6c 6f = autolo
db030 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a ckIoFinderImpl;.
db040 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
db050 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f WORKS && SQLITE_
db060 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
db070 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 TYLE */../*.** A
db080 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 n abstract type
db090 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f for a pointer to
db0a0 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e a IO method fin
db0b0 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f der function:.*/
db0c0 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 .typedef const s
db0d0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
db0e0 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 s *(*finder_type
db0f0 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e )(const char*,in
db100 74 29 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a t);.../*********
db110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db150 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
db160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db170 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 sqlite3_vfs met
db180 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a hods ***********
db190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db1a0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 *.**.** This div
db1b0 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 ision contains t
db1c0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
db1d0 6e 20 6f 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20 n of methods on
db1e0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 the.** sqlite3_v
db1f0 66 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f fs object..*/../
db200 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
db210 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
db220 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 the unixFile str
db230 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 ucture pointed t
db240 6f 20 62 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61 o by pId..*/.sta
db250 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e tic int fillInUn
db260 69 78 46 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 ixFile(. sqlite
db270 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
db280 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
db290 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 vfs object */.
db2a0 69 6e 74 20 68 2c 20 20 20 20 20 20 20 20 20 20 int h,
db2b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 /* Open
db2c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
db2d0 6f 66 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 of file being op
db2e0 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 ened */. int di
db2f0 72 66 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 rfd,
db300 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 /* Directory f
db310 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
db320 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
db330 20 2a 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 *pId, /* W
db340 72 69 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 rite to the unix
db350 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 File structure h
db360 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ere */. const c
db370 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
db380 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
db390 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 file being opene
db3a0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 d */. int noLoc
db3b0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k, /
db3c0 2a 20 4f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 * Omit locking i
db3d0 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 f true */. int
db3e0 69 73 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 isDelete
db3f0 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e /* Delete on
db400 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a close if true *
db410 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c /.){. const sql
db420 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
db430 2a 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a *pLockingStyle;.
db440 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 unixFile *pNew
db450 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 = (unixFile *)p
db460 49 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 Id;. int rc = S
db470 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
db480 65 72 74 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b ert( pNew->pLock
db490 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 ==NULL );. asse
db4a0 72 74 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d rt( pNew->pOpen=
db4b0 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50 =NULL );.. /* P
db4c0 61 72 61 6d 65 74 65 72 20 69 73 44 65 6c 65 74 arameter isDelet
db4d0 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f e is only used o
db4e0 6e 20 76 78 77 6f 72 6b 73 2e 0a 20 20 2a 2a 20 n vxworks.. **
db4f0 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 70 Express this exp
db500 6c 69 63 69 74 6c 79 20 68 65 72 65 20 74 6f 20 licitly here to
db510 70 72 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72 prevent compiler
db520 20 77 61 72 6e 69 6e 67 73 0a 20 20 2a 2a 20 61 warnings. ** a
db530 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 72 61 bout unused para
db540 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 23 69 66 meters.. */.#if
db550 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 55 !OS_VXWORKS. U
db560 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
db570 69 73 44 65 6c 65 74 65 29 3b 0a 23 65 6e 64 69 isDelete);.#endi
db580 66 0a 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f f.. OSTRACE3("O
db590 50 45 4e 20 20 20 20 25 2d 33 64 20 25 73 5c 6e PEN %-3d %s\n
db5a0 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 ", h, zFilename)
db5b0 3b 20 20 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20 ; . pNew->h
db5c0 3d 20 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 = h;. pNew->dir
db5d0 66 64 20 3d 20 64 69 72 66 64 3b 0a 20 20 53 45 fd = dirfd;. SE
db5e0 54 5f 54 48 52 45 41 44 49 44 28 70 4e 65 77 29 T_THREADID(pNew)
db5f0 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b ;..#if OS_VXWORK
db600 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d 20 S. pNew->pId =
db610 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 vxworksFindFileI
db620 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 d(zFilename);.
db630 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d 30 if( pNew->pId==0
db640 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20 3d ){. noLock =
db650 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 1;. rc = SQL
db660 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 ITE_NOMEM;. }.#
db670 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f 4c endif.. if( noL
db680 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b ock ){. pLock
db690 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c 6f ingStyle = &nolo
db6a0 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d ckIoMethods;. }
db6b0 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 else{. pLocki
db6c0 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66 69 ngStyle = (**(fi
db6d0 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 73 2d nder_type*)pVfs-
db6e0 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 6c 65 >pAppData)(zFile
db6f0 6e 61 6d 65 2c 20 68 29 3b 0a 23 69 66 20 53 51 name, h);.#if SQ
db700 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
db710 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a ING_STYLE. /*
db720 20 43 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65 Cache zFilename
db730 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 in the locking
db740 63 6f 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64 context (AFP and
db750 20 64 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64 dotlock overrid
db760 65 29 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72 e) for. ** pr
db770 6f 78 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69 oxyLock activati
db780 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28 on is possible (
db790 72 65 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20 remote proxy is
db7a0 62 61 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65 based on db name
db7b0 29 0a 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 ). ** zFilena
db7c0 6d 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 me remains valid
db7d0 20 75 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63 until file is c
db7e0 6c 6f 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72 losed, to suppor
db7f0 74 20 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c t */. pNew->l
db800 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
db810 28 76 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 (void*)zFilename
db820 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 ;.#endif. }..
db830 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c if( pLockingStyl
db840 65 20 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74 e == &posixIoMet
db850 68 6f 64 73 20 29 7b 0a 20 20 20 20 75 6e 69 78 hods ){. unix
db860 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
db870 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 rc = findLockI
db880 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d nfo(pNew, &pNew-
db890 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 >pLock, &pNew->p
db8a0 4f 70 65 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c Open);. unixL
db8b0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
db8c0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
db8d0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
db8e0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 E && defined(__A
db8f0 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 PPLE__). else i
db900 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 f( pLockingStyle
db910 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 == &afpIoMethod
db920 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 s ){. /* AFP
db930 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 locking uses the
db940 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 file path so it
db950 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 needs to be inc
db960 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 luded in. **
db970 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f the afpLockingCo
db980 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 ntext.. */.
db990 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 afpLockingCont
db9a0 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 ext *pCtx;. p
db9b0 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 New->lockingCont
db9c0 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c ext = pCtx = sql
db9d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a ite3_malloc( siz
db9e0 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 eof(*pCtx) );.
db9f0 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b if( pCtx==0 ){
dba00 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
dba10 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 TE_NOMEM;. }e
dba20 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 lse{. /* NB
dba30 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 : zFilename exis
dba40 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 ts and remains v
dba50 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 alid until the f
dba60 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 ile is closed.
dba70 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 ** according
dba80 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 to requirement
dba90 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 F11141. So we d
dbaa0 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 o not need to ma
dbab0 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f ke a. ** co
dbac0 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 py of the filena
dbad0 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 me. */. pCt
dbae0 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c x->dbPath = zFil
dbaf0 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 ename;. sra
dbb00 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 ndomdev();.
dbb10 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
dbb20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 );. rc = fi
dbb30 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c ndLockInfo(pNew,
dbb40 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f NULL, &pNew->pO
dbb50 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 pen);. unix
dbb60 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 LeaveMutex();
dbb70 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a . }. }.
dbb80 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 #endif.. else i
dbb90 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 f( pLockingStyle
dbba0 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 == &dotlockIoMe
dbbb0 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
dbbc0 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 Dotfile locking
dbbd0 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 uses the file pa
dbbe0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 th so it needs t
dbbf0 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e o be included in
dbc00 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c . ** the dotl
dbc10 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 ockLockingContex
dbc20 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 t . */. ch
dbc30 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 ar *zLockFile;.
dbc40 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 int nFilename
dbc50 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 ;. nFilename
dbc60 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 = (int)strlen(zF
dbc70 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 ilename) + 6;.
dbc80 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 zLockFile = (c
dbc90 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 har *)sqlite3_ma
dbca0 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b lloc(nFilename);
dbcb0 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 . if( zLockFi
dbcc0 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 le==0 ){. r
dbcd0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
dbce0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
dbcf0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
dbd00 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a ntf(nFilename, z
dbd10 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 LockFile, "%s" D
dbd20 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a OTLOCK_SUFFIX, z
dbd30 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d Filename);. }
dbd40 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 . pNew->locki
dbd50 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 ngContext = zLoc
dbd60 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 kFile;. }..#if
dbd70 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 OS_VXWORKS. els
dbd80 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 e if( pLockingSt
dbd90 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 yle == &semIoMet
dbda0 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e hods ){. /* N
dbdb0 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
dbdc0 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 ocking uses the
dbdd0 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 file path so it
dbde0 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 needs to be.
dbdf0 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ** included in t
dbe00 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e he semLockingCon
dbe10 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 text. */.
dbe20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
dbe30 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c ;. rc = findL
dbe40 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 ockInfo(pNew, &p
dbe50 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 New->pLock, &pNe
dbe60 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 w->pOpen);. i
dbe70 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f f( (rc==SQLITE_O
dbe80 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 K) && (pNew->pOp
dbe90 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 en->pSem==NULL)
dbea0 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a ){. char *z
dbeb0 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e SemName = pNew->
dbec0 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b pOpen->aSemName;
dbed0 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 . int n;.
dbee0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
dbef0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d intf(MAX_PATHNAM
dbf00 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 25 73 E, zSemName, "%s
dbf10 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20 .sem",.
dbf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e pN
dbf30 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 ew->pId->zCanoni
dbf40 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 calName);.
dbf50 66 6f 72 28 20 6e 3d 30 3b 20 7a 53 65 6d 4e 61 for( n=0; zSemNa
dbf60 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20 me[n]; n++ ).
dbf70 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d if( zSemNam
dbf80 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d e[n]=='/' ) zSem
dbf90 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 Name[n] = '_';.
dbfa0 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e pNew->pOpen
dbfb0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 ->pSem = sem_ope
dbfc0 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 n(zSemName, O_CR
dbfd0 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 EAT, 0666, 1);.
dbfe0 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 if( pNew->p
dbff0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45 Open->pSem == SE
dc000 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20 M_FAILED ){.
dc010 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
dc020 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 NOMEM;. p
dc030 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d New->pOpen->aSem
dc040 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a Name[0] = '\0';.
dc050 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
dc060 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
dc070 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ();. }.#endif.
dc080 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72 . pNew->lastEr
dc090 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f rno = 0;.#if OS_
dc0a0 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 VXWORKS. if( rc
dc0b0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
dc0c0 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e unlink(zFilen
dc0d0 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65 ame);. isDele
dc0e0 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e te = 0;. }. pN
dc0f0 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69 ew->isDelete = i
dc100 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a sDelete;.#endif.
dc110 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
dc120 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 _OK ){. if( d
dc130 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 irfd>=0 ) close(
dc140 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e dirfd); /* silen
dc150 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 t leak if fail,
dc160 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 already in error
dc170 20 2a 2f 0a 20 20 20 20 63 6c 6f 73 65 28 68 29 */. close(h)
dc180 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
dc190 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 New->pMethod = p
dc1a0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 LockingStyle;.
dc1b0 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 OpenCounter(+1
dc1c0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
dc1d0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 rc;.}../*.** Ope
dc1e0 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 n a file descrip
dc1f0 74 6f 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 tor to the direc
dc200 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 tory containing
dc210 66 69 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a file zFilename..
dc220 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c ** If successful
dc230 2c 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f , *pFd is set to
dc240 20 74 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 the opened file
dc250 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a descriptor and.
dc260 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
dc270 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 returned. If an
dc280 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 error occurs, ei
dc290 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ther SQLITE_NOME
dc2a0 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 M.** or SQLITE_C
dc2b0 41 4e 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 ANTOPEN is retur
dc2c0 6e 65 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 ned and *pFd is
dc2d0 73 65 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 set to an undefi
dc2e0 6e 65 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a ned.** value..**
dc2f0 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b .** If SQLITE_OK
dc300 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
dc310 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 e caller is resp
dc320 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 onsible for clos
dc330 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 ing.** the file
dc340 64 65 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 descriptor *pFd
dc350 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a using close()..*
dc360 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 /.static int ope
dc370 6e 44 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 nDirectory(const
dc380 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
dc390 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 , int *pFd){. i
dc3a0 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 nt ii;. int fd
dc3b0 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 = -1;. char zDi
dc3c0 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 rname[MAX_PATHNA
dc3d0 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 ME+1];.. sqlite
dc3e0 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 3_snprintf(MAX_P
dc3f0 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d ATHNAME, zDirnam
dc400 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 e, "%s", zFilena
dc410 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69 me);. for(ii=(i
dc420 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 nt)strlen(zDirna
dc430 6d 65 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69 me); ii>1 && zDi
dc440 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 rname[ii]!='/';
dc450 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e ii--);. if( ii>
dc460 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 0 ){. zDirnam
dc470 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 e[ii] = '\0';.
dc480 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72 fd = open(zDir
dc490 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f name, O_RDONLY|O
dc4a0 5f 42 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20 _BINARY, 0);.
dc4b0 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69 if( fd>=0 ){.#i
dc4c0 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a fdef FD_CLOEXEC.
dc4d0 20 20 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 fcntl(fd,
dc4e0 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 F_SETFD, fcntl(f
dc4f0 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c d, F_GETFD, 0) |
dc500 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 FD_CLOEXEC);.#e
dc510 6e 64 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41 ndif. OSTRA
dc520 43 45 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 CE3("OPENDIR %-3
dc530 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 d %s\n", fd, zDi
dc540 72 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 rname);. }.
dc550 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20 }. *pFd = fd;.
dc560 20 72 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53 return (fd>=0?S
dc570 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f QLITE_OK:SQLITE_
dc580 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a CANTOPEN);.}../*
dc590 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d .** Create a tem
dc5a0 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
dc5b0 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 in zBuf. zBuf
dc5c0 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 must be allocate
dc5d0 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c d.** by the call
dc5e0 69 6e 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20 ing process and
dc5f0 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 must be big enou
dc600 67 68 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 gh to hold at le
dc610 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 ast.** pVfs->mxP
dc620 61 74 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a athname bytes..*
dc630 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
dc640 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 Tempname(int nBu
dc650 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
dc660 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 static const c
dc670 68 61 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 har *azDirs[] =
dc680 7b 0a 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30 {. 0,. 0
dc690 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 ,. "/var/tmp
dc6a0 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d ",. "/usr/tm
dc6b0 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c p",. "/tmp",
dc6c0 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a . ".",. };.
dc6d0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
dc6e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 nsigned char zCh
dc6f0 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 ars[] =. "abc
dc700 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 defghijklmnopqrs
dc710 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 tuvwxyz". "AB
dc720 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 CDEFGHIJKLMNOPQR
dc730 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 STUVWXYZ". "0
dc740 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e 123456789";. un
dc750 73 69 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b signed int i, j;
dc760 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 . struct stat b
dc770 75 66 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 uf;. const char
dc780 20 2a 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 *zDir = ".";..
dc790 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 /* It's odd to
dc7a0 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 simulate an io-e
dc7b0 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 rror here, but r
dc7c0 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 eally this is ju
dc7d0 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 st. ** using th
dc7e0 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 e io-error infra
dc7f0 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 structure to tes
dc800 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 t that SQLite ha
dc810 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 ndles this. **
dc820 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 function failing
dc830 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 . . */. Simula
dc840 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
dc850 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 n SQLITE_IOERR )
dc860 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d ;.. azDirs[0] =
dc870 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 sqlite3_temp_di
dc880 72 65 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e rectory;. if (N
dc890 55 4c 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d ULL == azDirs[1]
dc8a0 29 20 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31 ) {. azDirs[1
dc8b0 5d 20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44 ] = getenv("TMPD
dc8c0 49 52 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 IR");. }. . f
dc8d0 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 or(i=0; i<sizeof
dc8e0 28 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 (azDirs)/sizeof(
dc8f0 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 azDirs[0]); i++)
dc900 7b 0a 20 20 20 20 69 66 28 20 61 7a 44 69 72 73 {. if( azDirs
dc910 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 [i]==0 ) continu
dc920 65 3b 0a 20 20 20 20 69 66 28 20 73 74 61 74 28 e;. if( stat(
dc930 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29 azDirs[i], &buf)
dc940 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
dc950 20 69 66 28 20 21 53 5f 49 53 44 49 52 28 62 75 if( !S_ISDIR(bu
dc960 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e f.st_mode) ) con
dc970 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61 tinue;. if( a
dc980 63 63 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c ccess(azDirs[i],
dc990 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 07) ) continue;
dc9a0 0a 20 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69 . zDir = azDi
dc9b0 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b rs[i];. break
dc9c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
dc9d0 6b 20 74 68 61 74 20 74 68 65 20 6f 75 74 70 75 k that the outpu
dc9e0 74 20 62 75 66 66 65 72 20 69 73 20 6c 61 72 67 t buffer is larg
dc9f0 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65 e enough for the
dca00 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
dca10 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69 . ** name. If i
dca20 74 20 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e t is not, return
dca30 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20 SQLITE_ERROR..
dca40 20 2a 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65 */. if( (strle
dca50 6e 28 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e n(zDir) + strlen
dca60 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c (SQLITE_TEMP_FIL
dca70 45 5f 50 52 45 46 49 58 29 20 2b 20 31 37 29 20 E_PREFIX) + 17)
dca80 3e 3d 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 >= (size_t)nBuf
dca90 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
dcaa0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
dcab0 0a 20 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74 . do{. sqlit
dcac0 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
dcad0 2d 31 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22 -17, zBuf, "%s/"
dcae0 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 SQLITE_TEMP_FILE
dcaf0 5f 50 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a _PREFIX, zDir);.
dcb00 20 20 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72 j = (int)str
dcb10 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 len(zBuf);. s
dcb20 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
dcb30 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b s(15, &zBuf[j]);
dcb40 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
dcb50 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 15; i++, j++){.
dcb60 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 zBuf[j] = (
dcb70 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 char)zChars[ ((u
dcb80 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 nsigned char)zBu
dcb90 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 f[j])%(sizeof(zC
dcba0 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20 hars)-1) ];.
dcbb0 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 }. zBuf[j] =
dcbc0 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63 0;. }while( acc
dcbd0 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29 ess(zBuf,0)==0 )
dcbe0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
dcbf0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c E_OK;.}..#if SQL
dcc00 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
dcc10 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 NG_STYLE && defi
dcc20 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f ned(__APPLE__)./
dcc30 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20 *.** Routine to
dcc40 74 72 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78 transform a unix
dcc50 46 69 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78 File into a prox
dcc60 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69 y-locking unixFi
dcc70 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 le..** Implement
dcc80 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f ation in the pro
dcc90 78 79 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e xy-lock division
dcca0 2c 20 62 75 74 20 75 73 65 64 20 62 79 20 75 6e , but used by un
dccb0 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53 ixOpen().** if S
dccc0 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f QLITE_PREFER_PRO
dccd0 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65 XY_LOCKING is de
dcce0 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 fined..*/.static
dccf0 20 69 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 int proxyTransf
dcd00 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 ormUnixFile(unix
dcd10 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 File*, const cha
dcd20 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a r*);.#endif.../*
dcd30 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c .** Open the fil
dcd40 65 20 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 e zPath..** .**
dcd50 50 72 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 Previously, the
dcd60 53 51 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20 SQLite OS layer
dcd70 75 73 65 64 20 74 68 72 65 65 20 66 75 6e 63 74 used three funct
dcd80 69 6f 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 ions in place of
dcd90 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a this.** one:.**
dcda0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f .** sqlite3O
dcdb0 73 4f 70 65 6e 52 65 61 64 57 72 69 74 65 28 29 sOpenReadWrite()
dcdc0 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ;.** sqlite3
dcdd0 4f 73 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 OsOpenReadOnly()
dcde0 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ;.** sqlite3
dcdf0 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 OsOpenExclusive(
dce00 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 );.**.** These c
dce10 61 6c 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 alls correspond
dce20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
dce30 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 combinations of
dce40 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 flags:.**.**
dce50 20 20 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e ReadWrite() ->
dce60 20 20 20 20 20 28 52 45 41 44 57 52 49 54 45 20 (READWRITE
dce70 7c 20 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 | CREATE).**
dce80 20 52 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 ReadOnly() ->
dce90 20 20 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a (READONLY) .
dcea0 2a 2a 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 ** OpenExclu
dceb0 73 69 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57 sive() -> (READW
dcec0 52 49 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20 RITE | CREATE |
dced0 45 58 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a EXCLUSIVE).**.**
dcee0 20 54 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 The old OpenExc
dcef0 6c 75 73 69 76 65 28 29 20 61 63 63 65 70 74 65 lusive() accepte
dcf00 64 20 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 d a boolean argu
dcf10 6d 65 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 ment - "delFlag"
dcf20 2e 20 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 . If.** true, th
dcf30 65 20 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 e file was confi
dcf40 67 75 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f gured to be auto
dcf50 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 matically delete
dcf60 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 d when the.** fi
dcf70 6c 65 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 le handle closed
dcf80 2e 20 54 6f 20 61 63 68 69 65 76 65 20 74 68 65 . To achieve the
dcf90 20 73 61 6d 65 20 65 66 66 65 63 74 20 75 73 69 same effect usi
dcfa0 6e 67 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 ng this new .**
dcfb0 69 6e 74 65 72 66 61 63 65 2c 20 61 64 64 20 74 interface, add t
dcfc0 68 65 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 he DELETEONCLOSE
dcfd0 20 66 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73 flag to those s
dcfe0 70 65 63 69 66 69 65 64 20 61 62 6f 76 65 20 66 pecified above f
dcff0 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 or .** OpenExclu
dd000 73 69 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 sive()..*/.stati
dd010 63 20 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a c int unixOpen(.
dd020 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
dd030 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f Vfs, /
dd040 2a 20 54 68 65 20 56 46 53 20 66 6f 72 20 77 68 * The VFS for wh
dd050 69 63 68 20 74 68 69 73 20 69 73 20 74 68 65 20 ich this is the
dd060 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a xOpen method */.
dd070 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
dd080 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f ath, /
dd090 2a 20 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69 * Pathname of fi
dd0a0 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 le to be opened
dd0b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
dd0c0 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 e *pFile,
dd0d0 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
dd0e0 73 63 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66 scriptor to be f
dd0f0 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e illed in */. in
dd100 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
dd110 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
dd120 70 75 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e put flags to con
dd130 74 72 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67 trol the opening
dd140 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 */. int *pOutF
dd150 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 lags
dd160 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61 /* Output fla
dd170 67 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 gs returned to S
dd180 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b QLite core */.){
dd190 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b 20 . int fd = -1;
dd1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd1b0 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 /* File descr
dd1c0 69 70 74 6f 72 20 72 65 74 75 72 6e 65 64 20 62 iptor returned b
dd1d0 79 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e y open() */. in
dd1e0 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 t dirfd = -1;
dd1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
dd200 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 Directory file d
dd210 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 escriptor */. i
dd220 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30 nt openFlags = 0
dd230 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
dd240 20 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74 Flags to pass t
dd250 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e o open() */. in
dd260 74 20 65 54 79 70 65 20 3d 20 66 6c 61 67 73 26 t eType = flags&
dd270 30 78 46 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 0xFFFFFF00; /*
dd280 54 79 70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Type of file to
dd290 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f open */. int no
dd2a0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 Lock;
dd2b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
dd2c0 20 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 to omit locking
dd2d0 20 70 72 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20 primitives */.
dd2e0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
dd2f0 5f 4f 4b 3b 0a 0a 20 20 69 6e 74 20 69 73 45 78 _OK;.. int isEx
dd300 63 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 clusive = (flag
dd310 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
dd320 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e EXCLUSIVE);. in
dd330 74 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d t isDelete =
dd340 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
dd350 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
dd360 4f 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 OSE);. int isCr
dd370 65 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 eate = (flag
dd380 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
dd390 43 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 CREATE);. int i
dd3a0 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 sReadonly = (f
dd3b0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
dd3c0 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 EN_READONLY);.
dd3d0 69 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20 int isReadWrite
dd3e0 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 = (flags & SQLI
dd3f0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
dd400 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 E);.. /* If cre
dd410 61 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f ating a master o
dd420 72 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 r main-file jour
dd430 6e 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 nal, this functi
dd440 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a on will open. *
dd450 2a 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 * a file-descrip
dd460 74 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 tor on the direc
dd470 74 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 tory too. The fi
dd480 72 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e rst time unixSyn
dd490 63 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c c(). ** is call
dd4a0 65 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 ed the directory
dd4b0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
dd4c0 20 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 will be fsync()
dd4d0 65 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e ed and close()d.
dd4e0 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 . */. int isOp
dd4f0 65 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 enDirectory = (i
dd500 73 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 sCreate && .
dd510 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 (eType==SQLITE
dd520 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 _OPEN_MASTER_JOU
dd530 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 RNAL || eType==S
dd540 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
dd550 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 JOURNAL). );..
dd560 20 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 /* If argument
dd570 7a 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 zPath is a NULL
dd580 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 pointer, this fu
dd590 6e 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 nction is requir
dd5a0 65 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 ed to open. **
dd5b0 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
dd5c0 2e 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65 . Use this buffe
dd5d0 72 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 r to store the f
dd5e0 69 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a ile name in.. *
dd5f0 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d /. char zTmpnam
dd600 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 e[MAX_PATHNAME+1
dd610 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ];. const char
dd620 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a *zName = zPath;.
dd630 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 . /* Check the
dd640 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d following statem
dd650 65 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a ents are true: .
dd660 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 **. ** (a)
dd670 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 Exactly one of t
dd680 68 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 he READWRITE and
dd690 20 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 READONLY flags
dd6a0 6d 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 must be set, and
dd6b0 20 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 . ** (b) if
dd6c0 43 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 CREATE is set, t
dd6d0 68 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 hen READWRITE mu
dd6e0 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 st also be set,
dd6f0 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 and. ** (c) i
dd700 66 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 f EXCLUSIVE is s
dd710 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 et, then CREATE
dd720 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 must also be set
dd730 2e 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 .. ** (d) if
dd740 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 DELETEONCLOSE is
dd750 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 set, then CREAT
dd760 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 E must also be s
dd770 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 et.. */. asser
dd780 74 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 t((isReadonly==0
dd790 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d || isReadWrite=
dd7a0 3d 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 =0) && (isReadWr
dd7b0 69 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c ite || isReadonl
dd7c0 79 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 y));. assert(is
dd7d0 43 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 Create==0 || isR
dd7e0 65 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 eadWrite);. ass
dd7f0 65 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d ert(isExclusive=
dd800 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b =0 || isCreate);
dd810 0a 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 . assert(isDele
dd820 74 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 te==0 || isCreat
dd830 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 e);.. /* The ma
dd840 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 in DB, main jour
dd850 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 nal, and master
dd860 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 journal are neve
dd870 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a r automatically.
dd880 20 20 2a 2a 20 64 65 6c 65 74 65 64 0a 20 20 2a ** deleted. *
dd890 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 /. assert( eTyp
dd8a0 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e!=SQLITE_OPEN_M
dd8b0 41 49 4e 5f 44 42 20 7c 7c 20 21 69 73 44 65 6c AIN_DB || !isDel
dd8c0 65 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ete );. assert(
dd8d0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f eType!=SQLITE_O
dd8e0 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
dd8f0 20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b || !isDelete );
dd900 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 . assert( eType
dd910 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 !=SQLITE_OPEN_MA
dd920 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 STER_JOURNAL ||
dd930 21 69 73 44 65 6c 65 74 65 20 29 3b 0a 0a 20 20 !isDelete );..
dd940 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 /* Assert that t
dd950 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 68 he upper layer h
dd960 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 74 68 as set one of th
dd970 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 66 6c e "file-type" fl
dd980 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ags. */. assert
dd990 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f ( eType==SQLITE_
dd9a0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 OPEN_MAIN_DB
dd9b0 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 || eType==SQLI
dd9c0 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 TE_OPEN_TEMP_DB
dd9d0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
dd9e0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
dd9f0 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 IN_JOURNAL || eT
dda00 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
dda10 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 _TEMP_JOURNAL .
dda20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d || eType==
dda30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a SQLITE_OPEN_SUBJ
dda40 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70 OURNAL || eTyp
dda50 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e==SQLITE_OPEN_M
dda60 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 ASTER_JOURNAL .
dda70 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d || eType==
dda80 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e SQLITE_OPEN_TRAN
dda90 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a 0a 20 SIENT_DB. );..
ddaa0 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 memset(pFile, 0
ddab0 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c , sizeof(unixFil
ddac0 65 29 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4e 61 e));.. if( !zNa
ddad0 6d 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 me ){. assert
ddae0 28 69 73 44 65 6c 65 74 65 20 26 26 20 21 69 73 (isDelete && !is
ddaf0 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 29 3b 0a OpenDirectory);.
ddb00 20 20 20 20 72 63 20 3d 20 67 65 74 54 65 6d 70 rc = getTemp
ddb10 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 4e 41 4d name(MAX_PATHNAM
ddb20 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a E+1, zTmpname);.
ddb30 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
ddb40 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
ddb50 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
ddb60 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 zName = zTmp
ddb70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 name;. }.. if(
ddb80 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f isReadonly ) o
ddb90 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 penFlags |= O_RD
ddba0 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65 ONLY;. if( isRe
ddbb0 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c adWrite ) openFl
ddbc0 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 ags |= O_RDWR;.
ddbd0 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 if( isCreate )
ddbe0 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 openFlags |=
ddbf0 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69 O_CREAT;. if( i
ddc00 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 sExclusive ) ope
ddc10 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 nFlags |= (O_EXC
ddc20 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 L|O_NOFOLLOW);.
ddc30 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f openFlags |= (O
ddc40 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e _LARGEFILE|O_BIN
ddc50 41 52 59 29 3b 0a 0a 20 20 66 64 20 3d 20 6f 70 ARY);.. fd = op
ddc60 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c en(zName, openFl
ddc70 61 67 73 2c 20 69 73 44 65 6c 65 74 65 3f 30 36 ags, isDelete?06
ddc80 30 30 3a 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 00:SQLITE_DEFAUL
ddc90 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f T_FILE_PERMISSIO
ddca0 4e 53 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 NS);. OSTRACE4(
ddcb0 22 4f 50 45 4e 58 20 20 20 25 2d 33 64 20 25 73 "OPENX %-3d %s
ddcc0 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20 7a 4e 61 0%o\n", fd, zNa
ddcd0 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 29 3b 0a me, openFlags);.
ddce0 20 20 69 66 28 20 66 64 3c 30 20 26 26 20 65 72 if( fd<0 && er
ddcf0 72 6e 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69 rno!=EISDIR && i
ddd00 73 52 65 61 64 57 72 69 74 65 20 26 26 20 21 69 sReadWrite && !i
ddd10 73 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 sExclusive ){.
ddd20 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f /* Failed to o
ddd30 70 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 pen the file for
ddd40 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 read/write acce
ddd50 73 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c ss. Try read-onl
ddd60 79 2e 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 y. */. flags
ddd70 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e &= ~(SQLITE_OPEN
ddd80 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
ddd90 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a E_OPEN_CREATE);.
ddda0 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c flags |= SQL
dddb0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
dddc0 59 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e Y;. return un
dddd0 69 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 ixOpen(pVfs, zPa
ddde0 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 th, pFile, flags
dddf0 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 , pOutFlags);.
dde00 7d 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a }. if( fd<0 ){.
dde10 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
dde20 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a E_CANTOPEN;. }.
dde30 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 20 29 if( isDelete )
dde40 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 {.#if OS_VXWORKS
dde50 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 . zPath = zNa
dde60 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e me;.#else. un
dde70 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e link(zName);.#en
dde80 64 69 66 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 dif. }.#if SQLI
dde90 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
ddea0 47 5f 53 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a G_STYLE. else{.
ddeb0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
ddec0 70 46 69 6c 65 29 2d 3e 6f 70 65 6e 46 6c 61 67 pFile)->openFlag
dded0 73 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 s = openFlags;.
ddee0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 }.#endif. if(
ddef0 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 pOutFlags ){.
ddf00 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c *pOutFlags = fl
ddf10 61 67 73 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 ags;. }..#ifnde
ddf20 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 28 f NDEBUG. if( (
ddf30 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
ddf40 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 PEN_MAIN_DB)!=0
ddf50 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c ){. ((unixFil
ddf60 65 2a 29 70 46 69 6c 65 29 2d 3e 69 73 4c 6f 63 e*)pFile)->isLoc
ddf70 6b 61 62 6c 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 kable = 1;. }.#
ddf80 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 endif.. assert(
ddf90 20 66 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 fd>=0 );. if(
ddfa0 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 isOpenDirectory
ddfb0 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e ){. rc = open
ddfc0 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c Directory(zPath,
ddfd0 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20 69 66 &dirfd);. if
ddfe0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
ddff0 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66 ){. close(f
de000 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 d); /* silently
de010 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c leak if fail, al
de020 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a ready in error *
de030 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 /. return r
de040 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 c;. }. }..#i
de050 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a fdef FD_CLOEXEC.
de060 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 fcntl(fd, F_SE
de070 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 TFD, fcntl(fd, F
de080 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f _GETFD, 0) | FD_
de090 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 CLOEXEC);.#endif
de0a0 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79 .. noLock = eTy
de0b0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe!=SQLITE_OPEN_
de0c0 4d 41 49 4e 5f 44 42 3b 0a 0a 23 69 66 20 53 51 MAIN_DB;..#if SQ
de0d0 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 LITE_PREFER_PROX
de0e0 59 5f 4c 4f 43 4b 49 4e 47 0a 20 20 69 66 28 20 Y_LOCKING. if(
de0f0 7a 50 61 74 68 21 3d 4e 55 4c 4c 20 26 26 20 21 zPath!=NULL && !
de100 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 63 68 noLock ){. ch
de110 61 72 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20 67 ar *envforce = g
de120 65 74 65 6e 76 28 22 53 51 4c 49 54 45 5f 46 4f etenv("SQLITE_FO
de130 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e RCE_PROXY_LOCKIN
de140 47 22 29 3b 0a 20 20 20 20 69 6e 74 20 75 73 65 G");. int use
de150 50 72 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20 20 Proxy = 0;..
de160 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f /* SQLITE_FORCE_
de170 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 PROXY_LOCKING==1
de180 20 6d 65 61 6e 73 20 66 6f 72 63 65 20 61 6c 77 means force alw
de190 61 79 73 20 75 73 65 20 70 72 6f 78 79 2c 20 0a ays use proxy, .
de1a0 20 20 20 20 2a 2a 20 30 20 6d 65 61 6e 73 20 6e ** 0 means n
de1b0 65 76 65 72 20 75 73 65 20 70 72 6f 78 79 2c 20 ever use proxy,
de1c0 4e 55 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20 70 NULL means use p
de1d0 72 6f 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 roxy for non-loc
de1e0 61 6c 20 66 69 6c 65 73 20 6f 6e 6c 79 0a 20 20 al files only.
de1f0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 6e 76 */. if( env
de200 66 6f 72 63 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20 force!=NULL ){.
de210 20 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 useProxy =
de220 61 74 6f 69 28 65 6e 76 66 6f 72 63 65 29 3e 30 atoi(envforce)>0
de230 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
de240 20 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73 struct statfs
de250 20 66 73 49 6e 66 6f 3b 0a 0a 20 20 20 20 20 20 fsInfo;..
de260 69 66 28 20 73 74 61 74 66 73 28 7a 50 61 74 68 if( statfs(zPath
de270 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 , &fsInfo) == -1
de280 20 29 7b 0a 09 09 09 09 28 28 75 6e 69 78 46 69 ){.....((unixFi
de290 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 6c 61 73 74 le*)pFile)->last
de2a0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
de2b0 20 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64 if( dirfd
de2c0 3e 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66 >=0 ) close(dirf
de2d0 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 d); /* silently
de2e0 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e leak if fail, in
de2f0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 error */.
de300 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 close(fd); /*
de310 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 silently leak if
de320 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 20 fail, in error
de330 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
de340 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 n SQLITE_IOERR_A
de350 43 43 45 53 53 3b 0a 20 20 20 20 20 20 7d 0a 20 CCESS;. }.
de360 20 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 useProxy =
de370 21 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 !(fsInfo.f_flags
de380 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 &MNT_LOCAL);.
de390 20 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 }. if( usePr
de3a0 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 oxy ){. rc
de3b0 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 = fillInUnixFile
de3c0 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 (pVfs, fd, dirfd
de3d0 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 , pFile, zPath,
de3e0 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 noLock, isDelete
de3f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
de400 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
de410 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
de420 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c TransformUnixFil
de430 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 e((unixFile*)pFi
de440 6c 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 le, ":auto:");.
de450 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
de460 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
de470 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 72 65 }.#endif. . re
de480 74 75 72 6e 20 66 69 6c 6c 49 6e 55 6e 69 78 46 turn fillInUnixF
de490 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69 ile(pVfs, fd, di
de4a0 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 rfd, pFile, zPat
de4b0 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c h, noLock, isDel
de4c0 65 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ete);.}../*.** D
de4d0 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20 61 elete the file a
de4e0 74 20 7a 50 61 74 68 2e 20 49 66 20 74 68 65 20 t zPath. If the
de4f0 64 69 72 53 79 6e 63 20 61 72 67 75 6d 65 6e 74 dirSync argument
de500 20 69 73 20 74 72 75 65 2c 20 66 73 79 6e 63 28 is true, fsync(
de510 29 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f ).** the directo
de520 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e ry after deletin
de530 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 g the file..*/.s
de540 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 tatic int unixDe
de550 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f lete(. sqlite3_
de560 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 vfs *NotUsed,
de570 20 20 2f 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e /* VFS contain
de580 69 6e 67 20 74 68 69 73 20 61 73 20 74 68 65 20 ing this as the
de590 78 44 65 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a xDelete method *
de5a0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
de5b0 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a zPath, /*
de5c0 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f Name of file to
de5d0 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 be deleted */.
de5e0 20 69 6e 74 20 64 69 72 53 79 6e 63 20 20 20 20 int dirSync
de5f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
de600 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 20 64 true, fsync() d
de610 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 irectory after d
de620 65 6c 65 74 69 6e 67 20 66 69 6c 65 20 2a 2f 0a eleting file */.
de630 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
de640 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 LITE_OK;. UNUSE
de650 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
de660 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 sed);. Simulate
de670 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 IOError(return S
de680 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
de690 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 TE);. unlink(zP
de6a0 61 74 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ath);.#ifndef SQ
de6b0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 LITE_DISABLE_DIR
de6c0 53 59 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79 SYNC. if( dirSy
de6d0 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 nc ){. int fd
de6e0 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 ;. rc = openD
de6f0 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 irectory(zPath,
de700 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 &fd);. if( rc
de710 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 ==SQLITE_OK ){.#
de720 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
de730 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 if( fsync(fd
de740 29 3d 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20 )==-1 ).#else.
de750 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 if( fsync(fd
de760 29 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 ) ).#endif.
de770 20 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 {. rc =
de780 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 SQLITE_IOERR_DIR
de790 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20 20 7d 0a _FSYNC;. }.
de7a0 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 if( close(
de7b0 66 64 29 26 26 21 72 63 20 29 7b 0a 20 20 20 20 fd)&&!rc ){.
de7c0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
de7d0 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b IOERR_DIR_CLOSE;
de7e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
de7f0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
de800 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
de810 54 65 73 74 20 74 68 65 20 65 78 69 73 74 61 6e Test the existan
de820 63 65 20 6f 66 20 6f 72 20 61 63 63 65 73 73 20 ce of or access
de830 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 66 permissions of f
de840 69 6c 65 20 7a 50 61 74 68 2e 20 54 68 65 0a 2a ile zPath. The.*
de850 2a 20 74 65 73 74 20 70 65 72 66 6f 72 6d 65 64 * test performed
de860 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 depends on the
de870 76 61 6c 75 65 20 6f 66 20 66 6c 61 67 73 3a 0a value of flags:.
de880 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 **.** SQLITE
de890 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 20 _ACCESS_EXISTS:
de8a0 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
de8b0 66 69 6c 65 20 65 78 69 73 74 73 0a 2a 2a 20 20 file exists.**
de8c0 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 SQLITE_ACCESS
de8d0 5f 52 45 41 44 57 52 49 54 45 3a 20 52 65 74 75 _READWRITE: Retu
de8e0 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 rn 1 if the file
de8f0 20 69 73 20 72 65 61 64 20 61 6e 64 20 77 72 69 is read and wri
de900 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 53 51 table..** SQ
de910 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
de920 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20 31 20 69 ONLY: Return 1 i
de930 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 f the file is re
de940 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 adable..**.** Ot
de950 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 30 herwise return 0
de960 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
de970 75 6e 69 78 41 63 63 65 73 73 28 0a 20 20 73 71 unixAccess(. sq
de980 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 lite3_vfs *NotUs
de990 65 64 2c 20 20 20 2f 2a 20 54 68 65 20 56 46 53 ed, /* The VFS
de9a0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 containing this
de9b0 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 xAccess method
de9c0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
de9d0 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 2f 2a 20 *zPath, /*
de9e0 50 61 74 68 20 6f 66 20 74 68 65 20 66 69 6c 65 Path of the file
de9f0 20 74 6f 20 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 to examine */.
dea00 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 int flags,
dea10 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 /* What
dea20 20 64 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c do we want to l
dea30 65 61 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a earn about the z
dea40 50 61 74 68 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 Path file? */.
dea50 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 int *pResOut
dea60 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
dea70 20 72 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 result boolean
dea80 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
dea90 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20 20 55 4e amode = 0;. UN
deaa0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
deab0 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c otUsed);. Simul
deac0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
dead0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
deae0 41 43 43 45 53 53 3b 20 29 3b 0a 20 20 73 77 69 ACCESS; );. swi
deaf0 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 tch( flags ){.
deb00 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
deb10 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 CESS_EXISTS:.
deb20 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b amode = F_OK;
deb30 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
deb40 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
deb50 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a CESS_READWRITE:.
deb60 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 57 5f amode = W_
deb70 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 OK|R_OK;. b
deb80 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
deb90 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
deba0 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d D:. amode =
debb0 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 R_OK;. bre
debc0 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 ak;.. default
debd0 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 :. assert(!
debe0 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 "Invalid flags a
debf0 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 rgument");. }.
dec00 20 2a 70 52 65 73 4f 75 74 20 3d 20 28 61 63 63 *pResOut = (acc
dec10 65 73 73 28 7a 50 61 74 68 2c 20 61 6d 6f 64 65 ess(zPath, amode
dec20 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 )==0);. return
dec30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
dec40 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 *.** Turn a rela
dec50 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e tive pathname in
dec60 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 to a full pathna
dec70 6d 65 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65 me. The relative
dec80 20 70 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 path.** is stor
dec90 65 64 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d ed as a nul-term
deca0 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e inated string in
decb0 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e the buffer poin
decc0 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 ted to by.** zPa
decd0 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 th. .**.** zOut
dece0 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 points to a buff
decf0 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 er of at least s
ded00 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 qlite3_vfs.mxPat
ded10 68 6e 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 hname bytes .**
ded20 28 69 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d (in this case, M
ded30 41 58 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65 AX_PATHNAME byte
ded40 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 s). The full-pat
ded50 68 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a h is written to.
ded60 2a 2a 20 74 68 69 73 20 62 75 66 66 65 72 20 62 ** this buffer b
ded70 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
ded80 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
ded90 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 nixFullPathname(
deda0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
dedb0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
dedc0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
dedd0 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 fs object */. c
dede0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
dedf0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
dee00 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 Possibly relativ
dee10 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a e input path */.
dee20 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 int nOut,
dee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dee40 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 /* Size of outpu
dee50 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 t buffer in byte
dee60 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 s */. char *zOu
dee70 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
dee80 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
dee90 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f uffer */.){.. /
deea0 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 * It's odd to si
deeb0 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 mulate an io-err
deec0 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 or here, but rea
deed0 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 lly this is just
deee0 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 . ** using the
deef0 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 io-error infrast
def00 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 ructure to test
def10 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 that SQLite hand
def20 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 les this. ** fu
def30 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 nction failing.
def40 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f This function co
def50 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72 uld fail if, for
def60 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20 example, the.
def70 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69 ** current worki
def80 6e 67 20 64 69 72 65 63 74 6f 72 79 20 68 61 73 ng directory has
def90 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a been unlinked..
defa0 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 */. SimulateI
defb0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
defc0 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a QLITE_ERROR );..
defd0 20 20 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e assert( pVfs->
defe0 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f mxPathname==MAX_
deff0 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e PATHNAME );. UN
df000 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
df010 56 66 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f Vfs);.. zOut[nO
df020 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 ut-1] = '\0';.
df030 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f if( zPath[0]=='/
df040 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ' ){. sqlite3
df050 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 _snprintf(nOut,
df060 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 zOut, "%s", zPat
df070 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 h);. }else{.
df080 20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 int nCwd;. i
df090 66 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 f( getcwd(zOut,
df0a0 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 nOut-1)==0 ){.
df0b0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
df0c0 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 E_CANTOPEN;.
df0d0 7d 0a 20 20 20 20 6e 43 77 64 20 3d 20 28 69 6e }. nCwd = (in
df0e0 74 29 73 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a t)strlen(zOut);.
df0f0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
df100 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 intf(nOut-nCwd,
df110 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 &zOut[nCwd], "/%
df120 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a s", zPath);. }.
df130 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
df140 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 OK;.}...#ifndef
df150 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
df160 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a _EXTENSION./*.**
df170 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 Interfaces for
df180 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 opening a shared
df190 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e library, findin
df1a0 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a g entry points.*
df1b0 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 * within the sha
df1c0 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 red library, and
df1d0 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 closing the sha
df1e0 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a red library..*/.
df1f0 23 69 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e #include <dlfcn.
df200 68 3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a h>.static void *
df210 75 6e 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74 unixDlOpen(sqlit
df220 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
df230 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
df240 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 4e 55 53 45 lename){. UNUSE
df250 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
df260 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 sed);. return d
df270 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c lopen(zFilename,
df280 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 RTLD_NOW | RTLD
df290 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a _GLOBAL);.}../*.
df2a0 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 ** SQLite calls
df2b0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d this function im
df2c0 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 mediately after
df2d0 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c a call to unixDl
df2e0 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 Sym() or.** unix
df2f0 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28 DlOpen() fails (
df300 72 65 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70 returns a null p
df310 6f 69 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f ointer). If a mo
df320 72 65 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f re detailed erro
df330 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 r.** message is
df340 61 76 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73 available, it is
df350 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66 written to zBuf
df360 4f 75 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 Out. If no error
df370 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61 message.** is a
df380 76 61 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75 vailable, zBufOu
df390 74 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 t is left unmodi
df3a0 66 69 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 fied and SQLite
df3b0 75 73 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a uses a default.*
df3c0 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e * error message.
df3d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
df3e0 75 6e 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69 unixDlError(sqli
df3f0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
df400 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
df410 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63 68 *zBufOut){. ch
df420 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 ar *zErr;. UNUS
df430 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
df440 55 73 65 64 29 3b 0a 20 20 75 6e 69 78 45 6e 74 Used);. unixEnt
df450 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72 erMutex();. zEr
df460 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20 r = dlerror();.
df470 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 if( zErr ){.
df480 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
df490 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c f(nBuf, zBufOut,
df4a0 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 "%s", zErr);.
df4b0 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 }. unixLeaveMut
df4c0 65 78 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 ex();.}.static v
df4d0 6f 69 64 20 28 2a 75 6e 69 78 44 6c 53 79 6d 28 oid (*unixDlSym(
df4e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 sqlite3_vfs *Not
df4f0 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 2c 20 63 Used, void *p, c
df500 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79 6d 29 29 onst char*zSym))
df510 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 (void){. /* .
df520 2a 2a 20 47 43 43 20 77 69 74 68 20 2d 70 65 64 ** GCC with -ped
df530 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 73 61 79 antic-errors say
df540 73 20 74 68 61 74 20 43 39 30 20 64 6f 65 73 20 s that C90 does
df550 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 76 6f 69 64 not allow a void
df560 2a 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 61 73 * to be. ** cas
df570 74 20 69 6e 74 6f 20 61 20 70 6f 69 6e 74 65 72 t into a pointer
df580 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 to a function.
df590 20 41 6e 64 20 79 65 74 20 74 68 65 20 6c 69 62 And yet the lib
df5a0 72 61 72 79 20 64 6c 73 79 6d 28 29 20 72 6f 75 rary dlsym() rou
df5b0 74 69 6e 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e tine. ** return
df5c0 73 20 61 20 76 6f 69 64 2a 20 77 68 69 63 68 20 s a void* which
df5d0 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e is really a poin
df5e0 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ter to a functio
df5f0 6e 2e 20 20 53 6f 20 68 6f 77 20 64 6f 20 77 65 n. So how do we
df600 0a 20 20 2a 2a 20 75 73 65 20 64 6c 73 79 6d 28 . ** use dlsym(
df610 29 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 ) with -pedantic
df620 2d 65 72 72 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 -errors?. **.
df630 2a 2a 20 56 61 72 69 61 62 6c 65 20 78 20 62 65 ** Variable x be
df640 6c 6f 77 20 69 73 20 64 65 66 69 6e 65 64 20 74 low is defined t
df650 6f 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 o be a pointer t
df660 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b o a function tak
df670 69 6e 67 0a 20 20 2a 2a 20 70 61 72 61 6d 65 74 ing. ** paramet
df680 65 72 73 20 76 6f 69 64 2a 20 61 6e 64 20 63 6f ers void* and co
df690 6e 73 74 20 63 68 61 72 2a 20 61 6e 64 20 72 65 nst char* and re
df6a0 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 turning a pointe
df6b0 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e r to a function.
df6c0 0a 20 20 2a 2a 20 57 65 20 69 6e 69 74 69 61 6c . ** We initial
df6d0 69 7a 65 20 78 20 62 79 20 61 73 73 69 67 6e 69 ize x by assigni
df6e0 6e 67 20 69 74 20 61 20 70 6f 69 6e 74 65 72 20 ng it a pointer
df6f0 74 6f 20 74 68 65 20 64 6c 73 79 6d 28 29 20 66 to the dlsym() f
df700 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 unction.. ** (T
df710 68 61 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 72 hat assignment r
df720 65 71 75 69 72 65 73 20 61 20 63 61 73 74 2e 29 equires a cast.)
df730 20 20 54 68 65 6e 20 77 65 20 63 61 6c 6c 20 74 Then we call t
df740 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 he function that
df750 0a 20 20 2a 2a 20 78 20 70 6f 69 6e 74 73 20 74 . ** x points t
df760 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 o. . **. ** T
df770 68 69 73 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 his work-around
df780 69 73 20 75 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 is unlikely to w
df790 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e ork correctly on
df7a0 20 61 6e 79 20 73 79 73 74 65 6d 20 77 68 65 72 any system wher
df7b0 65 0a 20 20 2a 2a 20 79 6f 75 20 72 65 61 6c 6c e. ** you reall
df7c0 79 20 63 61 6e 6e 6f 74 20 63 61 73 74 20 61 20 y cannot cast a
df7d0 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
df7e0 20 69 6e 74 6f 20 76 6f 69 64 2a 2e 20 20 42 75 into void*. Bu
df7f0 74 20 74 68 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 t then, on the.
df800 20 2a 2a 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 ** other hand,
df810 64 6c 73 79 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 dlsym() will not
df820 20 77 6f 72 6b 20 6f 6e 20 73 75 63 68 20 61 20 work on such a
df830 73 79 73 74 65 6d 20 65 69 74 68 65 72 2c 20 73 system either, s
df840 6f 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 6e o we have. ** n
df850 6f 74 20 72 65 61 6c 6c 79 20 6c 6f 73 74 20 61 ot really lost a
df860 6e 79 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 nything.. */.
df870 76 6f 69 64 20 28 2a 28 2a 78 29 28 76 6f 69 64 void (*(*x)(void
df880 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 *,const char*))(
df890 76 6f 69 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f void);. UNUSED_
df8a0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
df8b0 64 29 3b 0a 20 20 78 20 3d 20 28 76 6f 69 64 28 d);. x = (void(
df8c0 2a 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 *(*)(void*,const
df8d0 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 29 64 char*))(void))d
df8e0 6c 73 79 6d 3b 0a 20 20 72 65 74 75 72 6e 20 28 lsym;. return (
df8f0 2a 78 29 28 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a *x)(p, zSym);.}.
df900 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 static void unix
df910 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f DlClose(sqlite3_
df920 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f vfs *NotUsed, vo
df930 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 id *pHandle){.
df940 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
df950 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 64 6c 63 (NotUsed);. dlc
df960 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b 0a 7d lose(pHandle);.}
df970 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c .#else /* if SQL
df980 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
df990 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e TENSION is defin
df9a0 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 ed: */. #define
df9b0 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20 30 0a 20 unixDlOpen 0.
df9c0 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 45 #define unixDlE
df9d0 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 rror 0. #define
df9e0 20 75 6e 69 78 44 6c 53 79 6d 20 20 20 30 0a 20 unixDlSym 0.
df9f0 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 43 #define unixDlC
dfa00 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 2f lose 0.#endif../
dfa10 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42 75 66 20 *.** Write nBuf
dfa20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 20 bytes of random
dfa30 64 61 74 61 20 74 6f 20 74 68 65 20 73 75 70 70 data to the supp
dfa40 6c 69 65 64 20 62 75 66 66 65 72 20 7a 42 75 66 lied buffer zBuf
dfa50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
dfa60 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 28 73 unixRandomness(s
dfa70 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
dfa80 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 sed, int nBuf, c
dfa90 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e har *zBuf){. UN
dfaa0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
dfab0 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 72 otUsed);. asser
dfac0 74 28 28 73 69 7a 65 5f 74 29 6e 42 75 66 3e 3d t((size_t)nBuf>=
dfad0 28 73 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b (sizeof(time_t)+
dfae0 73 69 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a sizeof(int)));..
dfaf0 20 20 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20 /* We have to
dfb00 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20 initialize zBuf
dfb10 74 6f 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72 to prevent valgr
dfb20 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69 ind from reporti
dfb30 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20 ng. ** errors.
dfb40 20 54 68 65 20 72 65 70 6f 72 74 73 20 69 73 73 The reports iss
dfb50 75 65 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20 ued by valgrind
dfb60 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20 are incorrect -
dfb70 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 we would. ** pr
dfb80 65 66 65 72 20 74 68 61 74 20 74 68 65 20 72 61 efer that the ra
dfb90 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72 ndomness be incr
dfba0 65 61 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20 eased by making
dfbb0 75 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 use of the. **
dfbc0 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 uninitialized sp
dfbd0 61 63 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75 ace in zBuf - bu
dfbe0 74 20 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72 t valgrind error
dfbf0 73 20 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a s tend to worry.
dfc00 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e ** some users.
dfc10 20 20 52 61 74 68 65 72 20 74 68 61 6e 20 61 72 Rather than ar
dfc20 67 75 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61 gue, it seems ea
dfc30 73 69 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69 sier just to ini
dfc40 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65 tialize. ** the
dfc50 20 77 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64 whole array and
dfc60 20 73 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e silence valgrin
dfc70 64 2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 d, even if that
dfc80 6d 65 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f means less rando
dfc90 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68 mness. ** in th
dfca0 65 20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 e random seed..
dfcb0 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65 **. ** When te
dfcc0 73 74 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a sting, initializ
dfcd0 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 ing zBuf[] to ze
dfce0 72 6f 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e ro is all we do.
dfcf0 20 20 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a That means. *
dfd00 2a 20 74 68 61 74 20 77 65 20 61 6c 77 61 79 73 * that we always
dfd10 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 61 use the same ra
dfd20 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75 ndom number sequ
dfd30 65 6e 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65 ence. This make
dfd40 73 20 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73 s the. ** tests
dfd50 20 72 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a repeatable.. *
dfd60 2f 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c /. memset(zBuf,
dfd70 20 30 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21 0, nBuf);.#if !
dfd80 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
dfd90 45 53 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74 EST). {. int
dfda0 20 70 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64 pid, fd;. fd
dfdb0 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 = open("/dev/ur
dfdc0 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 andom", O_RDONLY
dfdd0 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 );. if( fd<0
dfde0 29 7b 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20 ){. time_t
dfdf0 74 3b 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74 t;. time(&t
dfe00 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 );. memcpy(
dfe10 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66 zBuf, &t, sizeof
dfe20 28 74 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20 (t));. pid
dfe30 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20 = getpid();.
dfe40 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73 memcpy(&zBuf[s
dfe50 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c izeof(t)], &pid,
dfe60 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 sizeof(pid));.
dfe70 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a assert( siz
dfe80 65 6f 66 28 74 29 2b 73 69 7a 65 6f 66 28 70 69 eof(t)+sizeof(pi
dfe90 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e 42 75 66 d)<=(size_t)nBuf
dfea0 20 29 3b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d );. nBuf =
dfeb0 20 73 69 7a 65 6f 66 28 74 29 20 2b 20 73 69 7a sizeof(t) + siz
dfec0 65 6f 66 28 70 69 64 29 3b 0a 20 20 20 20 7d 65 eof(pid);. }e
dfed0 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42 75 66 20 lse{. nBuf
dfee0 3d 20 72 65 61 64 28 66 64 2c 20 7a 42 75 66 2c = read(fd, zBuf,
dfef0 20 6e 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6c nBuf);. cl
dff00 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20 ose(fd);. }.
dff10 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
dff20 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a rn nBuf;.}.../*.
dff30 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c ** Sleep for a l
dff40 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 ittle while. Re
dff50 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 turn the amount
dff60 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a of time slept..*
dff70 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 * The argument i
dff80 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
dff90 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 microseconds we
dffa0 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a want to sleep..*
dffb0 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c * The return val
dffc0 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ue is the number
dffd0 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 of microseconds
dffe0 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c of sleep actual
dfff0 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 ly.** requested
e0000 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 from the underly
e0010 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 ing operating sy
e0020 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 stem, a number w
e0030 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 hich.** might be
e0040 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
e0050 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72 equal to the ar
e0060 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 gument, but not
e0070 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 less.** than the
e0080 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 argument..*/.st
e0090 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 6c 65 atic int unixSle
e00a0 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ep(sqlite3_vfs *
e00b0 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6d 69 63 NotUsed, int mic
e00c0 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20 roseconds){.#if
e00d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 OS_VXWORKS. str
e00e0 75 63 74 20 74 69 6d 65 73 70 65 63 20 73 70 3b uct timespec sp;
e00f0 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63 20 3d 20 .. sp.tv_sec =
e0100 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 2f 20 31 microseconds / 1
e0110 30 30 30 30 30 30 3b 0a 20 20 73 70 2e 74 76 5f 000000;. sp.tv_
e0120 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f 73 65 63 nsec = (microsec
e0130 6f 6e 64 73 20 25 20 31 30 30 30 30 30 30 29 20 onds % 1000000)
e0140 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c * 1000;. nanosl
e0150 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a eep(&sp, NULL);.
e0160 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
e0170 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
e0180 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e eturn microsecon
e0190 64 73 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 ds;.#elif define
e01a0 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26 d(HAVE_USLEEP) &
e01b0 26 20 48 41 56 45 5f 55 53 4c 45 45 50 0a 20 20 & HAVE_USLEEP.
e01c0 75 73 6c 65 65 70 28 6d 69 63 72 6f 73 65 63 6f usleep(microseco
e01d0 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 nds);. UNUSED_P
e01e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
e01f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 );. return micr
e0200 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a oseconds;.#else.
e0210 20 20 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 int seconds =
e0220 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 (microseconds+99
e0230 39 39 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 9999)/1000000;.
e0240 20 73 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b sleep(seconds);
e0250 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e0260 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
e0270 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a 31 return seconds*1
e0280 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 000000;.#endif.}
e0290 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
e02a0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
e02b0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
e02c0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 zero value, is i
e02d0 6e 74 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a nterpreted as.**
e02e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 the number of s
e02f0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 econds since 197
e0300 30 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 0 and is used to
e0310 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 set the result
e0320 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 of.** sqlite3OsC
e0330 75 72 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 urrentTime() dur
e0340 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a ing testing..*/.
e0350 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
e0360 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
e0370 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e t sqlite3_curren
e0380 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 t_time = 0; /*
e0390 46 61 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 Fake system time
e03a0 20 69 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 in seconds sinc
e03b0 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 e 1970. */.#endi
e03c0 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 f../*.** Find th
e03d0 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 e current time (
e03e0 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f in Universal Coo
e03f0 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 rdinated Time).
e0400 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 Write the.** cu
e0410 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 rrent time and d
e0420 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 ate as a Julian
e0430 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 Day number into
e0440 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 *prNow and.** re
e0450 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 turn 0. Return
e0460 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 1 if the time an
e0470 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 d date cannot be
e0480 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 found..*/.stati
e0490 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e c int unixCurren
e04a0 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 tTime(sqlite3_vf
e04b0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62 s *NotUsed, doub
e04c0 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20 le *prNow){.#if
e04d0 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 4f defined(NO_GETTO
e04e0 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 D). time_t t;.
e04f0 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 time(&t);. *pr
e0500 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30 20 Now = t/86400.0
e0510 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65 6c + 2440587.5;.#el
e0520 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
e0530 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20 struct timespec
e0540 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67 65 sNow;. clock_ge
e0550 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41 4c ttime(CLOCK_REAL
e0560 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 TIME, &sNow);.
e0570 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 *prNow = 2440587
e0580 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 .5 + sNow.tv_sec
e0590 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e /86400.0 + sNow.
e05a0 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30 30 tv_nsec/86400000
e05b0 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65 0a 000000.0;.#else.
e05c0 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 6c struct timeval
e05d0 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65 sNow;. gettime
e05e0 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b ofday(&sNow, 0);
e05f0 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 . *prNow = 2440
e0600 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 587.5 + sNow.tv_
e0610 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e sec/86400.0 + sN
e0620 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30 30 ow.tv_usec/86400
e0630 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66 000000.0;.#endif
e0640 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
e0650 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 TEST. if( sqlit
e0660 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
e0670 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 ){. *prNow =
e0680 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
e0690 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 time/86400.0 + 2
e06a0 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 440587.5;. }.#e
e06b0 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 ndif. UNUSED_PA
e06c0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e06d0 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
e06e0 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64 20 ./*.** We added
e06f0 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 72 6f the xGetLastErro
e0700 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20 r() method with
e0710 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f 66 the intention of
e0720 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62 65 providing.** be
e0730 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20 65 tter low-level e
e0740 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 68 rror messages wh
e0750 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 en operating-sys
e0760 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f 6d tem problems com
e0770 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20 53 e up.** during S
e0780 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 2e QLite operation.
e0790 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e 6f But so far, no
e07a0 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 20 62 ne of that has b
e07b0 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a een implemented.
e07c0 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e 20 ** in the core.
e07d0 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 So this routine
e07e0 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 is never called
e07f0 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20 69 . For now, it i
e0800 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70 6c s merely.** a pl
e0810 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a 73 ace-holder..*/.s
e0820 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 65 tatic int unixGe
e0830 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 tLastError(sqlit
e0840 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e0850 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20 63 int NotUsed2, c
e0860 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b 0a har *NotUsed3){.
e0870 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
e0880 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 55 ER(NotUsed);. U
e0890 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e08a0 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e 55 NotUsed2);. UNU
e08b0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
e08c0 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75 72 tUsed3);. retur
e08d0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a n 0;.}../*.*****
e08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e08f0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 *** End of sqlit
e0900 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a e3_vfs methods *
e0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
e0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a *********/../***
e0980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
e09d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e09e0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72 6f ****** Begin Pro
e09f0 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a xy Locking *****
e0a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
e0a20 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 * Proxy locking
e0a30 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b 69 is a "uber-locki
e0a40 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74 68 ng-method" in th
e0a50 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75 73 is sense: It us
e0a60 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72 20 es the.** other
e0a70 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 locking methods
e0a80 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f 63 on secondary loc
e0a90 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79 20 k files. Proxy
e0aa0 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a 20 locking is a.**
e0ab0 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72 20 meta-layer over
e0ac0 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d 69 top of the primi
e0ad0 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 tive locking imp
e0ae0 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e 20 lemented above.
e0af0 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65 61 For.** this rea
e0b00 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f son, the divisio
e0b10 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 n that implement
e0b20 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 s of proxy locki
e0b30 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 0a 2a ng is deferred.*
e0b40 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e 20 * until late in
e0b50 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 29 20 the file (here)
e0b60 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 after all of the
e0b70 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68 6f other I/O metho
e0b80 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 ds have.** been
e0b90 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68 61 defined - so tha
e0ba0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 t the primitive
e0bb0 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 locking methods
e0bc0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a are available.**
e0bd0 20 61 73 20 73 65 72 76 69 63 65 73 20 74 6f 20 as services to
e0be0 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 69 6d help with the im
e0bf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
e0c00 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a proxy locking..*
e0c10 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 *.****.**.** The
e0c20 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 default locking
e0c30 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c 69 schemes in SQLi
e0c40 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e 67 te use byte-rang
e0c50 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a 2a e locks on the.*
e0c60 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
e0c70 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73 61 to coordinate sa
e0c80 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 61 fe, concurrent a
e0c90 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 6c ccess by multipl
e0ca0 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e 64 e readers.** and
e0cb0 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a 2f writers [http:/
e0cc0 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63 6b /sqlite.org/lock
e0cd0 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54 68 ingv3.html]. Th
e0ce0 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63 6b e five file lock
e0cf0 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28 55 ing.** states (U
e0d00 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e 47 NLOCKED, PENDING
e0d10 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 , SHARED, RESERV
e0d20 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20 61 ED, EXCLUSIVE) a
e0d30 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a re implemented.*
e0d40 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 64 20 * as POSIX read
e0d50 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f 76 & write locks ov
e0d60 65 72 20 66 69 78 65 64 20 73 65 74 20 6f 66 20 er fixed set of
e0d70 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20 66 locations (via f
e0d80 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46 50 sctl),.** on AFP
e0d90 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65 78 and SMB only ex
e0da0 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 6e clusive byte-ran
e0db0 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76 61 ge locks are ava
e0dc0 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74 6c ilable via fsctl
e0dd0 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28 27 .** with _IOWR('
e0de0 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42 z', 23, struct B
e0df0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29 yteRangeLockPB2)
e0e00 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73 61 to track the sa
e0e10 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a 20 me 5 states..**
e0e20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46 5f To simulate a F_
e0e30 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68 61 RDLCK on the sha
e0e40 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41 46 red range, on AF
e0e50 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65 6c P a randomly sel
e0e60 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73 73 ected.** address
e0e70 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 72 in the shared r
e0e80 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 6f ange is taken fo
e0e90 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c r a SHARED lock,
e0ea0 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 73 the entire.** s
e0eb0 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 74 hared range is t
e0ec0 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43 4c aken for an EXCL
e0ed0 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a USIVE lock):.**.
e0ee0 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f ** PENDING_
e0ef0 42 59 54 45 20 20 20 20 20 20 20 20 30 78 34 30 BYTE 0x40
e0f00 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a 20 000000.. ..**
e0f10 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42 59 RESERVED_BY
e0f20 54 45 20 20 20 20 20 20 20 30 78 34 30 30 30 30 TE 0x40000
e0f30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 001.** SHAR
e0f40 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20 20 ED_RANGE
e0f50 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30 78 0x40000002 -> 0x
e0f60 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 54 40000200.**.** T
e0f70 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20 6f his works well o
e0f80 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 n the local file
e0f90 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68 6f system, but sho
e0fa0 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30 78 ws a nearly 100x
e0fb0 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e 20 .** slowdown in
e0fc0 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63 65 read performance
e0fd0 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65 20 on AFP because
e0fe0 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20 64 the AFP client d
e0ff0 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20 72 isables.** the r
e1000 65 61 64 20 63 61 63 68 65 20 77 68 65 6e 20 62 ead cache when b
e1010 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 yte-range locks
e1020 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45 6e are present. En
e1030 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64 0a abling the read.
e1040 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65 73 ** cache exposes
e1050 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e a cache coheren
e1060 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74 20 cy problem that
e1070 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61 6c is present on al
e1080 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f 72 l OS X.** suppor
e1090 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 ted network file
e10a0 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20 61 systems. NFS a
e10b0 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73 65 nd AFP both obse
e10c0 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 65 rve the.** close
e10d0 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 69 -to-open semanti
e10e0 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 cs for ensuring
e10f0 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 0a cache coherency.
e1100 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e 73 ** [http://nfs.s
e1110 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f 23 ourceforge.net/#
e1120 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20 64 faq_a8], which d
e1130 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69 76 oes not effectiv
e1140 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 ely.** address t
e1150 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 20 he requirements
e1160 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20 64 for concurrent d
e1170 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 62 atabase access b
e1180 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 65 y multiple.** re
e1190 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 aders and writer
e11a0 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 77 s.** [http://www
e11b0 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c 69 .nabble.com/SQLi
e11c0 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 2d te-on-NFS-cache-
e11d0 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36 35 coherency-td1565
e11e0 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 5701.html]..**.*
e11f0 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 68 65 * To address the
e1200 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e 64 performance and
e1210 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 cache coherency
e1220 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20 66 issues, proxy f
e1230 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 ile locking.** c
e1240 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20 64 hanges the way d
e1250 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 69 atabase access i
e1260 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 s controlled by
e1270 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73 20 limiting access
e1280 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 68 to a.** single h
e1290 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 61 6e ost at a time an
e12a0 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c 6f d moving file lo
e12b0 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20 64 cks off of the d
e12c0 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 atabase file.**
e12d0 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78 79 and onto a proxy
e12e0 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 file on the loc
e12f0 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 al file system.
e1300 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e 67 .**.**.** Using
e1310 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a 20 proxy locks.**
e1320 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
e1330 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a 2a -.**.** C APIs.*
e1340 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 *.** sqlite3_fi
e1350 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 le_control(db, d
e1360 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53 45 bname, SQLITE_SE
e1370 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c T_LOCKPROXYFILE,
e1380 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
e1390 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78 79 <proxy
e13a0 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f 3a _path> | ":auto:
e13b0 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f ");.** sqlite3_
e13c0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c file_control(db,
e13d0 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f dbname, SQLITE_
e13e0 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c GET_LOCKPROXYFIL
e13f0 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68 3e E, &<proxy_path>
e1400 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 );.**.**.** SQL
e1410 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 50 pragmas.**.** P
e1420 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e RAGMA [database.
e1430 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 ]lock_proxy_file
e1440 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 =<proxy_path> |
e1450 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 4d :auto:.** PRAGM
e1460 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 A [database.]loc
e1470 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a 0a k_proxy_file.**.
e1480 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22 3a ** Specifying ":
e1490 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68 61 auto:" means tha
e14a0 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 t if there is a
e14b0 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68 20 conch file with
e14c0 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68 6f a matching.** ho
e14d0 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68 65 st ID in it, the
e14e0 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20 74 proxy path in t
e14f0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 he conch file wi
e1500 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68 65 ll be used, othe
e1510 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78 79 rwise.** a proxy
e1520 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20 74 path based on t
e1530 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 20 64 he user's temp d
e1540 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66 73 ir.** (via confs
e1550 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 tr(_CS_DARWIN_US
e1560 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e 29 ER_TEMP_DIR,...)
e1570 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 61 ) will be used a
e1580 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c nd the.** actual
e1590 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d 65 proxy file name
e15a0 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 72 is generated fr
e15b0 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 om the name and
e15c0 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 path of the.** d
e15d0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 46 atabase file. F
e15e0 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a or example:.**.*
e15f0 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 74 61 * For data
e1600 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 65 72 base path "/User
e1610 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a 2a s/me/foo.db" .**
e1620 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b 20 The lock
e1630 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c 74 path will be "<t
e1640 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c 6f mpdir>/sqliteplo
e1650 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 6f cks/_Users_me_fo
e1660 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a 0a o.db:auto:").**.
e1670 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 70 ** Once a lock p
e1680 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75 72 roxy is configur
e1690 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 ed for a databas
e16a0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 74 e connection, it
e16b0 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 72 can not.** be r
e16c0 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72 20 emoved, however
e16d0 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 63 68 it may be switch
e16e0 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e ed to a differen
e16f0 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 69 61 t proxy path via
e1700 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41 50 .** the above AP
e1710 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68 65 Is (assuming the
e1720 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20 6e conch file is n
e1730 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62 79 ot being held by
e1740 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 6e another.** conn
e1750 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65 73 ection or proces
e1760 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f s). .**.**.** Ho
e1770 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 w proxy locking
e1780 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d works.** -------
e1790 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
e17a0 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69 6c .**.** Proxy fil
e17b0 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65 73 e locking relies
e17c0 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74 77 primarily on tw
e17d0 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e 67 o new supporting
e17e0 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 files: .**.**
e17f0 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20 74 * conch file t
e1800 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20 74 o limit access t
e1810 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
e1820 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 ile to a single
e1830 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74 20 host.** at
e1840 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 2a a time.**.** *
e1850 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f 20 proxy file to
e1860 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 20 66 act as a proxy f
e1870 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 79 20 or the advisory
e1880 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a locks normally.*
e1890 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e 20 * taken on
e18a0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 0a the database.**.
e18b0 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c ** The conch fil
e18c0 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72 6f e - to use a pro
e18d0 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65 20 xy file, sqlite
e18e0 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c 64 must first "hold
e18f0 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 62 the conch".** b
e1900 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c 69 y taking an sqli
e1910 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64 20 te-style shared
e1920 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 lock on the conc
e1930 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67 20 h file, reading
e1940 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 the.** contents
e1950 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 and comparing th
e1960 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65 20 e host's unique
e1970 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 65 6c host ID (see bel
e1980 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 ow) and lock.**
e1990 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 69 6e proxy path again
e19a0 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 st the values st
e19b0 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e 63 ored in the conc
e19c0 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66 69 h. The conch fi
e19d0 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 le is.** stored
e19e0 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 in the same dire
e19f0 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 61 74 ctory as the dat
e1a00 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 abase file and t
e1a10 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 he file name.**
e1a20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 66 74 is patterned aft
e1a30 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 er the database
e1a40 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e 3c file name as ".<
e1a50 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63 6f databasename>-co
e1a60 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65 20 nch"..** If the
e1a70 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73 20 conch file does
e1a80 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69 74 not exist, or it
e1a90 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20 6e 's contents do n
e1aa0 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a 20 ot match the.**
e1ab0 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20 70 host ID and/or p
e1ac0 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e 20 roxy path, then
e1ad0 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63 61 the lock is esca
e1ae0 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63 6c lated to an excl
e1af0 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 6e usive.** lock an
e1b00 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 d the conch file
e1b10 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70 64 contents is upd
e1b20 61 74 65 64 20 77 69 74 68 20 74 68 65 20 68 6f ated with the ho
e1b30 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79 0a st ID and proxy.
e1b40 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65 20 ** path and the
e1b50 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61 64 lock is downgrad
e1b60 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 20 6c ed to a shared l
e1b70 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20 74 ock again. If t
e1b80 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 68 he conch.** is h
e1b90 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 70 eld by another p
e1ba0 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 20 73 rocess (with a s
e1bb0 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68 65 hared lock), the
e1bc0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a exclusive lock.
e1bd0 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 ** will fail and
e1be0 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
e1bf0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
e1c00 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 2d The proxy file -
e1c10 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 66 a single-byte f
e1c20 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c 6c ile used for all
e1c30 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20 6c advisory file l
e1c40 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 ocks.** normally
e1c50 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 taken on the da
e1c60 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20 54 tabase file. T
e1c70 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20 73 his allows for s
e1c80 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 6f afe sharing.** o
e1c90 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
e1ca0 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 ile for multiple
e1cb0 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 readers and wri
e1cc0 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 ters on the same
e1cd0 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63 6f .** host (the co
e1ce0 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 61 74 nch ensures that
e1cf0 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74 68 they all use th
e1d00 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f 63 e same local loc
e1d10 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 k file)..**.** T
e1d20 68 65 72 65 20 69 73 20 61 20 74 68 69 72 64 20 here is a third
e1d30 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74 20 file - the host
e1d40 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20 61 ID file - used a
e1d50 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 72 s a persistent r
e1d60 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75 6e ecord.** of a un
e1d70 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 ique identifier
e1d80 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61 20 for the host, a
e1d90 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65 20 128-byte unique
e1da0 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a 20 host id file.**
e1db0 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 66 69 in the path defi
e1dc0 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 54 49 ned by the HOSTI
e1dd0 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65 66 DPATH macro (def
e1de0 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a 2a ault value is.**
e1df0 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 73 /Library/Caches
e1e00 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 /.com.apple.sqli
e1e10 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e 0a teConchHostId)..
e1e20 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e 67 **.** Requesting
e1e30 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79 20 the lock proxy
e1e40 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69 61 does not immedia
e1e50 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63 6f tely take the co
e1e60 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f 6e nch, it is.** on
e1e70 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 68 ly taken when th
e1e80 65 20 66 69 72 73 74 20 72 65 71 75 65 73 74 20 e first request
e1e90 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73 65 to lock database
e1ea0 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20 20 file is made.
e1eb0 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65 73 .** This matches
e1ec0 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20 6f the semantics o
e1ed0 66 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e 61 f the traditiona
e1ee0 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 l locking behavi
e1ef0 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70 65 or, where.** ope
e1f00 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69 6f ning a connectio
e1f10 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 n to a database
e1f20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74 61 file does not ta
e1f30 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74 2e ke a lock on it.
e1f40 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 6c .** The shared l
e1f50 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e 20 ock and an open
e1f60 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e1f70 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20 75 are maintained u
e1f80 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f 6e ntil .** the con
e1f90 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 nection to the d
e1fa0 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 atabase is close
e1fb0 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 d. .**.** The pr
e1fc0 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68 65 oxy file and the
e1fd0 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20 6e lock file are n
e1fe0 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f 20 ever deleted so
e1ff0 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a 2a they only need.*
e2000 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 * to be created
e2010 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 the first time t
e2020 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a 2a hey are used..**
e2030 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f .** Configuratio
e2040 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d n options.** ---
e2050 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
e2060 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 --.**.** SQLITE
e2070 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f _PREFER_PROXY_LO
e2080 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20 CKING.**.**
e2090 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 73 Database files
e20a0 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f 6e accessed on non
e20b0 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 -local file syst
e20c0 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 ems are.**
e20d0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 automatically c
e20e0 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70 72 onfigured for pr
e20f0 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f 63 oxy locking, loc
e2100 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20 20 k files are.**
e2110 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f 6d named autom
e2120 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20 74 atically using t
e2130 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61 73 he same logic as
e2140 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d 41 .** PRAGMA
e2150 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 lock_proxy_file
e2160 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20 20 =":auto:".**
e2170 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f 58 .** SQLITE_PROX
e2180 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20 20 Y_DEBUG.**.**
e2190 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65 20 Enables the
e21a0 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f 72 logging of error
e21b0 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e 67 messages during
e21c0 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a host id file.**
e21d0 20 20 20 20 20 20 20 72 65 74 72 69 65 76 61 6c retrieval
e21e0 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a 2a and creation.**
e21f0 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48 0a .** HOSTIDPATH.
e2200 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 72 **.** Over
e2210 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 6c rides the defaul
e2220 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 70 t host ID file p
e2230 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 0a ath location.**.
e2240 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 ** LOCKPROXYDIR
e2250 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 .**.** Ove
e2260 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 rrides the defau
e2270 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 73 65 lt directory use
e2280 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78 79 d for lock proxy
e2290 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20 20 files that.**
e22a0 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20 61 are named a
e22b0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69 61 utomatically via
e22c0 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73 65 the ":auto:" se
e22d0 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51 4c tting.**.** SQL
e22e0 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 ITE_DEFAULT_PROX
e22f0 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 YDIR_PERMISSIONS
e2300 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65 72 .**.** Per
e2310 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65 20 missions to use
e2320 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 when creating a
e2330 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73 74 directory for st
e2340 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20 20 oring the.**
e2350 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 lock proxy fi
e2360 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20 77 les, only used w
e2370 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 hen LOCKPROXYDIR
e2380 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a 20 is not set..**
e2390 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41 73 .** .** As
e23a0 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76 65 mentioned above
e23b0 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 , when compiled
e23c0 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45 46 with SQLITE_PREF
e23d0 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 ER_PROXY_LOCKING
e23e0 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68 65 ,.** setting the
e23f0 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72 environment var
e2400 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f 52 iable SQLITE_FOR
e2410 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 CE_PROXY_LOCKING
e2420 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66 6f to 1 will.** fo
e2430 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e rce proxy lockin
e2440 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 g to be used for
e2450 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20 every database
e2460 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e 64 file opened, and
e2470 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63 65 0.** will force
e2480 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78 79 automatic proxy
e2490 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 64 locking to be d
e24a0 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c 20 isabled for all
e24b0 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 database.** file
e24c0 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 61 6c s (explicity cal
e24d0 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f ling the SQLITE_
e24e0 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c SET_LOCKPROXYFIL
e24f0 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 73 E pragma or.** s
e2500 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74 72 qlite_file_contr
e2510 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61 66 ol API is not af
e2520 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 45 fected by SQLITE
e2530 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 _FORCE_PROXY_LOC
e2540 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a KING)..*/../*.**
e2550 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 Proxy locking i
e2560 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
e2570 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a 23 on MacOSX .*/.#
e2580 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 if defined(__APP
e2590 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
e25a0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e25b0 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51 4c TYLE..#ifdef SQL
e25c0 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d 75 ITE_TEST./* simu
e25d0 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68 6f late multiple ho
e25e0 73 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 sts by creating
e25f0 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66 69 unique hostid fi
e2600 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c 49 le paths */.SQLI
e2610 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
e2620 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d 20 e3_hostid_num =
e2630 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
e2640 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69 6e The proxyLockin
e2650 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68 65 gContext has the
e2660 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20 73 path and file s
e2670 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 74 68 tructures for th
e2680 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e 64 e remote .** and
e2690 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69 6c local proxy fil
e26a0 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70 65 es in it.*/.type
e26b0 64 65 66 20 73 74 72 75 63 74 20 70 72 6f 78 79 def struct proxy
e26c0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 70 LockingContext p
e26d0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
e26e0 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78 79 xt;.struct proxy
e26f0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b LockingContext {
e2700 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e . unixFile *con
e2710 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 chFile;
e2720 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66 69 /* Open conch fi
e2730 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f le */. char *co
e2740 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20 20 nchFilePath;
e2750 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
e2760 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a the conch file *
e2770 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f /. unixFile *lo
e2780 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20 20 ckProxy;
e2790 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20 6c /* Open proxy l
e27a0 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 ock file */. ch
e27b0 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61 74 ar *lockProxyPat
e27c0 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 h; /* Na
e27d0 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20 me of the proxy
e27e0 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 lock file */. c
e27f0 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20 har *dbPath;
e2800 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
e2810 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 ame of the open
e2820 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f file */. int co
e2830 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20 20 nchHeld;
e2840 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
e2850 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 63 f the conch is c
e2860 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a 2f urrently held */
e2870 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63 6b . void *oldLock
e2880 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 ingContext;
e2890 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b /* Original lock
e28a0 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 ingcontext to re
e28b0 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20 2a store on close *
e28c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /. sqlite3_io_m
e28d0 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4f ethods const *pO
e28e0 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f 2a ldMethod; /*
e28f0 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d 65 Original I/O me
e2900 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65 20 thods for close
e2910 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49 44 */.};../* HOSTID
e2920 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45 4e LEN and CONCHLEN
e2930 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73 70 both include sp
e2940 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 72 69 ace for the stri
e2950 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 69 ng .** terminati
e2960 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66 69 ng nul .*/.#defi
e2970 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20 20 ne HOSTIDLEN
e2980 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 128.#define
e2990 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20 20 CONCHLEN
e29a0 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b 48 (MAXPATHLEN+H
e29b0 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66 6e OSTIDLEN+1).#ifn
e29c0 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a 23 def HOSTIDPATH.#
e29d0 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50 41 define HOSTIDPA
e29e0 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72 61 TH "/Libra
e29f0 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61 ry/Caches/.com.a
e2a00 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68 pple.sqliteConch
e2a10 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a 0a HostId".#endif..
e2a20 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20 63 /* basically a c
e2a30 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64 6f opy of unixRando
e2a40 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 66 65 mness with diffe
e2a50 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65 68 rent.** test beh
e2a60 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20 2a avior built in *
e2a70 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
e2a80 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44 xyGenerateHostID
e2a90 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29 7b (char *pHostID){
e2aa0 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c 20 . int pid, fd,
e2ab0 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 len;. unsigned
e2ac0 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e 73 char *key = (uns
e2ad0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48 6f igned char *)pHo
e2ae0 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73 65 stID;. . memse
e2af0 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49 44 t(key, 0, HOSTID
e2b00 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 3b LEN);. len = 0;
e2b10 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 . fd = open("/d
e2b20 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 ev/urandom", O_R
e2b30 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66 64 DONLY);. if( fd
e2b40 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d >=0 ){. len =
e2b50 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20 48 read(fd, key, H
e2b60 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 63 OSTIDLEN);. c
e2b70 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c lose(fd); /* sil
e2b80 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 ently leak the f
e2b90 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a 2f d if it fails */
e2ba0 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20 3c . }. if( len <
e2bb0 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 HOSTIDLEN ){.
e2bc0 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 time_t t;.
e2bd0 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d 65 time(&t);. me
e2be0 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73 69 mcpy(key, &t, si
e2bf0 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70 69 zeof(t));. pi
e2c00 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 d = getpid();.
e2c10 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73 69 memcpy(&key[si
e2c20 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 zeof(t)], &pid,
e2c30 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 sizeof(pid));.
e2c40 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b 45 }. .#ifdef MAKE
e2c50 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a 20 _PRETTY_HOSTID.
e2c60 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 {. int i;.
e2c70 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65 20 /* filter the
e2c80 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e 74 bytes into print
e2c90 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 72 61 able ascii chara
e2ca0 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74 65 cters and NUL te
e2cb0 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20 6b rminate */. k
e2cc0 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29 ey[(HOSTIDLEN-1)
e2cd0 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66 6f ] = 0x00;. fo
e2ce0 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54 49 r( i=0; i<(HOSTI
e2cf0 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b 0a DLEN-1); i++ ){.
e2d00 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 unsigned c
e2d10 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d 26 har pa = key[i]&
e2d20 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28 20 0x7F;. if(
e2d30 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20 20 pa<0x20 ){.
e2d40 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 79 key[i] = (key
e2d50 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 30 [i]&0x80 == 0x80
e2d60 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70 61 ) ? pa+0x40 : pa
e2d70 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65 6c +0x20;. }el
e2d80 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46 20 se if( pa==0x7F
e2d90 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b 69 ){. key[i
e2da0 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 30 ] = (key[i]&0x80
e2db0 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d 30 == 0x80) ? pa=0
e2dc0 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a 20 x20 : pa+0x7E;.
e2dd0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
e2de0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
e2df0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
e2e00 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 6f 73 * writes the hos
e2e10 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 61 74 t id path to pat
e2e20 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20 62 h, path should b
e2e30 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61 74 e an pre-allocat
e2e40 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69 74 ed buffer.** wit
e2e50 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 66 h enough space f
e2e60 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73 74 or a path .*/.st
e2e70 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79 47 atic void proxyG
e2e80 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 68 61 etHostIDPath(cha
e2e90 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74 20 r *path, size_t
e2ea0 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79 28 len){. strlcpy(
e2eb0 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54 48 path, HOSTIDPATH
e2ec0 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20 53 , len);.#ifdef S
e2ed0 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 QLITE_TEST. if(
e2ee0 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f sqlite3_hostid_
e2ef0 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68 61 num>0 ){. cha
e2f00 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22 31 r suffix[2] = "1
e2f10 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30 5d ";. suffix[0]
e2f20 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20 73 = suffix[0] + s
e2f30 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 qlite3_hostid_nu
e2f40 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28 70 m;. strlcat(p
e2f50 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65 6e ath, suffix, len
e2f60 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
e2f70 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 OSTRACE3("GETHOS
e2f80 54 49 44 50 41 54 48 20 20 25 73 20 70 69 64 3d TIDPATH %s pid=
e2f90 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65 74 %d\n", path, get
e2fa0 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67 65 pid());.}../* ge
e2fb0 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 72 t the host ID fr
e2fc0 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73 74 om a sqlite host
e2fd0 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20 69 id file stored i
e2fe0 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d 73 n the .** user-s
e2ff0 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 72 65 pecific tmp dire
e3000 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74 68 ctory, create th
e3010 65 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f 74 e ID if it's not
e3020 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 20 0a there already .
e3030 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
e3040 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 68 61 oxyGetHostID(cha
e3050 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74 20 r *pHostID, int
e3060 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74 20 *pError){. int
e3070 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68 5b fd;. char path[
e3080 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20 20 MAXPATHLEN]; .
e3090 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69 6e size_t len;. in
e30a0 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b 0a t rc=SQLITE_OK;.
e30b0 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 . proxyGetHostI
e30c0 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58 50 DPath(path, MAXP
e30d0 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74 72 ATHLEN);. /* tr
e30e0 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 y to create the
e30f0 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69 66 host ID file, if
e3100 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 it already exis
e3110 74 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 ts read the cont
e3120 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20 6f ents */. fd = o
e3130 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45 41 pen(path, O_CREA
e3140 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58 43 T|O_WRONLY|O_EXC
e3150 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28 20 L, 0644);. if(
e3160 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 fd<0 ){. int
e3170 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20 20 err=errno;....
e3180 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 53 if( err!=EEXIS
e3190 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 T ){.#ifdef SQLI
e31a0 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f TE_PROXY_DEBUG /
e31b0 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 * set the sqlite
e31c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
e31d0 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 nstead */.
e31e0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
e31f0 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63 72 "sqlite error cr
e3200 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20 66 eating host ID f
e3210 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20 ile %s: %s\n",.
e3220 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 pat
e3230 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 h, strerror(err)
e3240 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
e3250 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 return SQLITE_PE
e3260 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a RM;. }. /*
e3270 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 65 couldn't create
e3280 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64 20 the file, read
e3290 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 it instead */.
e32a0 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68 fd = open(path
e32b0 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58 43 , O_RDONLY|O_EXC
e32c0 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 L);. if( fd<0
e32d0 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ){.#ifdef SQLIT
e32e0 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f 2a E_PROXY_DEBUG /*
e32f0 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 20 set the sqlite
e3300 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
e3310 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 69 stead */. i
e3320 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a nt err = errno;.
e3330 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
e3340 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 derr, "sqlite er
e3350 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73 74 ror opening host
e3360 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c ID file %s: %s\
e3370 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
e3380 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72 path, strerror
e3390 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 (err));.#endif.
e33a0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
e33b0 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 TE_PERM;. }.
e33c0 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28 66 len = pread(f
e33d0 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 54 d, pHostID, HOST
e33e0 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69 IDLEN, 0);. i
e33f0 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 20 f( len<0 ){.
e3400 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 6e *pError = errn
e3410 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 o;. rc = SQ
e3420 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b LITE_IOERR_READ;
e3430 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c . }else if( l
e3440 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a en<HOSTIDLEN ){.
e3450 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 *pError =
e3460 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 0;. rc = SQ
e3470 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
e3480 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20 20 _READ;. }.
e3490 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 close(fd); /* s
e34a0 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 ilently leak the
e34b0 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 fd if it fails
e34c0 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 */. OSTRACE3(
e34d0 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 61 64 "GETHOSTID read
e34e0 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %s pid=%d\n", p
e34f0 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29 HostID, getpid()
e3500 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 );. return rc
e3510 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
e3520 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 6e 67 * we're creating
e3530 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c the host ID fil
e3540 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d 20 e (use a random
e3550 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73 29 string of bytes)
e3560 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65 6e */. proxyGen
e3570 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f 73 erateHostID(pHos
e3580 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 tID);. len =
e3590 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73 74 pwrite(fd, pHost
e35a0 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 30 ID, HOSTIDLEN, 0
e35b0 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 30 );. if( len<0
e35c0 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f ){. *pErro
e35d0 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 r = errno;.
e35e0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
e35f0 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 RR_WRITE;. }e
e3600 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54 lse if( len<HOST
e3610 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a IDLEN ){. *
e3620 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 pError = 0;.
e3630 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
e3640 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d ERR_WRITE;. }
e3650 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 . close(fd);
e3660 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
e3670 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61 the fd if it fa
e3680 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 ils */. OSTRA
e3690 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 CE3("GETHOSTID
e36a0 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64 5c wrote %s pid=%d\
e36b0 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74 n", pHostID, get
e36c0 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75 pid());. retu
e36d0 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 rn rc;. }.}..st
e36e0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 atic int proxyGe
e36f0 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74 20 tLockPath(const
e3700 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68 char *dbPath, ch
e3710 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65 5f ar *lPath, size_
e3720 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e 74 t maxLen){. int
e3730 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c 65 len;. int dbLe
e3740 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69 66 n;. int i;..#if
e3750 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 def LOCKPROXYDIR
e3760 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 . len = strlcpy
e3770 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f 58 (lPath, LOCKPROX
e3780 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 YDIR, maxLen);.#
e3790 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43 53 else.# ifdef _CS
e37a0 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d _DARWIN_USER_TEM
e37b0 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63 6f P_DIR. {. co
e37c0 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e nfstr(_CS_DARWIN
e37d0 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 20 _USER_TEMP_DIR,
e37e0 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b 0a lPath, maxLen);.
e37f0 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 len = strlca
e3800 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74 65 t(lPath, "sqlite
e3810 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e 29 plocks", maxLen)
e3820 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72 28 ;. if( mkdir(
e3830 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44 45 lPath, SQLITE_DE
e3840 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 FAULT_PROXYDIR_P
e3850 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a 20 ERMISSIONS) ){.
e3860 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69 72 /* if mkdir
e3870 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20 61 fails, handle a
e3880 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65 61 s lock file crea
e3890 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f 0a tion failure */.
e38a0 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 int err =
e38b0 65 72 72 6e 6f 3b 0a 23 20 20 69 66 64 65 66 20 errno;.# ifdef
e38c0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
e38d0 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 if( err!=EEXI
e38e0 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70 ST ){. fp
e38f0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 70 rintf(stderr, "p
e3900 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 3a roxyGetLockPath:
e3910 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20 65 mkdir(%s,0%o) e
e3920 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20 6c rror %d %s\n", l
e3930 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20 20 Path,.
e3940 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 SQLITE_DEF
e3950 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 AULT_PROXYDIR_PE
e3960 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c 20 RMISSIONS, err,
e3970 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a strerror(err));.
e3980 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69 66 }.# endif
e3990 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
e39a0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 4c OSTRACE3("GETL
e39b0 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20 25 OCKPATH mkdir %
e39c0 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 61 s pid=%d\n", lPa
e39d0 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 th, getpid());.
e39e0 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23 20 }. . }.#
e39f0 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 72 else. len = str
e3a00 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 6d lcpy(lPath, "/tm
e3a10 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 20 p/", maxLen);.#
e3a20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 endif.#endif..
e3a30 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 5d if( lPath[len-1]
e3a40 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 6e !='/' ){. len
e3a50 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 = strlcat(lPath
e3a60 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a , "/", maxLen);.
e3a70 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 6e }. . /* tran
e3a80 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 74 sform the db pat
e3a90 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 61 h to a unique ca
e3aa0 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 62 che name */. db
e3ab0 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 Len = (int)strle
e3ac0 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f 72 n(dbPath);. for
e3ad0 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 26 ( i=0; i<dbLen &
e3ae0 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78 4c & (i+len+7)<maxL
e3af0 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 en; i++){. ch
e3b00 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69 5d ar c = dbPath[i]
e3b10 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c 65 ;. lPath[i+le
e3b20 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27 5f n] = (c=='/')?'_
e3b30 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74 68 ':c;. }. lPath
e3b40 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20 20 [i+len]='\0';.
e3b50 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 strlcat(lPath, "
e3b60 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e 29 :auto:", maxLen)
e3b70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
e3b80 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
e3b90 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 53 20 reate a new VFS
e3ba0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e3bb0 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 (stored in memor
e3bc0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a y obtained from.
e3bd0 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
e3be0 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65 20 c) and open the
e3bf0 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74 68 file named "path
e3c00 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 " in the file de
e3c10 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 scriptor..**.**
e3c20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 The caller is re
e3c30 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f 6e sponsible not on
e3c40 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20 74 ly for closing t
e3c50 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
e3c60 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20 66 or.** but also f
e3c70 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d or freeing the m
e3c80 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 emory associated
e3c90 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 64 with the file d
e3ca0 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73 74 escriptor..*/.st
e3cb0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72 atic int proxyCr
e3cc0 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f 6e eateUnixFile(con
e3cd0 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 75 st char *path, u
e3ce0 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c 65 nixFile **ppFile
e3cf0 29 20 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 ) {. int fd;.
e3d00 69 6e 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 0a int dirfd = -1;.
e3d10 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 unixFile *pNew
e3d20 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
e3d30 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 ITE_OK;. sqlite
e3d40 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b 0a 3_vfs dummyVfs;.
e3d50 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 . fd = open(pat
e3d60 68 2c 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52 h, O_RDWR | O_CR
e3d70 45 41 54 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 EAT, SQLITE_DEFA
e3d80 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 ULT_FILE_PERMISS
e3d90 49 4f 4e 53 29 3b 0a 20 20 69 66 28 20 66 64 3c IONS);. if( fd<
e3da0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
e3db0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
e3dc0 0a 20 20 7d 0a 20 20 0a 20 20 70 4e 65 77 20 3d . }. . pNew =
e3dd0 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c (unixFile *)sql
e3de0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 ite3_malloc(size
e3df0 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 of(unixFile));.
e3e00 20 69 66 28 20 70 4e 65 77 3d 3d 4e 55 4c 4c 20 if( pNew==NULL
e3e10 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
e3e20 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f TE_NOMEM;. go
e3e30 74 6f 20 65 6e 64 5f 63 72 65 61 74 65 5f 70 72 to end_create_pr
e3e40 6f 78 79 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 oxy;. }. memse
e3e50 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f t(pNew, 0, sizeo
e3e60 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 f(unixFile));..
e3e70 20 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44 61 dummyVfs.pAppDa
e3e80 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75 74 ta = (void*)&aut
e3e90 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20 olockIoFinder;.
e3ea0 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 rc = fillInUnix
e3eb0 46 69 6c 65 28 26 64 75 6d 6d 79 56 66 73 2c 20 File(&dummyVfs,
e3ec0 66 64 2c 20 64 69 72 66 64 2c 20 28 73 71 6c 69 fd, dirfd, (sqli
e3ed0 74 65 33 5f 66 69 6c 65 2a 29 70 4e 65 77 2c 20 te3_file*)pNew,
e3ee0 70 61 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 69 path, 0, 0);. i
e3ef0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
e3f00 20 29 7b 0a 20 20 20 20 2a 70 70 46 69 6c 65 20 ){. *ppFile
e3f10 3d 20 70 4e 65 77 3b 0a 20 20 20 20 72 65 74 75 = pNew;. retu
e3f20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
e3f30 7d 0a 65 6e 64 5f 63 72 65 61 74 65 5f 70 72 6f }.end_create_pro
e3f40 78 79 3a 20 20 20 20 0a 20 20 63 6c 6f 73 65 28 xy: . close(
e3f50 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 fd); /* silently
e3f60 20 6c 65 61 6b 20 66 64 20 69 66 20 65 72 72 6f leak fd if erro
e3f70 72 2c 20 77 65 27 72 65 20 61 6c 72 65 61 64 79 r, we're already
e3f80 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 in error */. s
e3f90 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 qlite3_free(pNew
e3fa0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
e3fb0 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 }../* takes the
e3fc0 63 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 conch by taking
e3fd0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e a shared lock an
e3fe0 64 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 d read the conte
e3ff0 6e 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a nts conch, if .*
e4000 2a 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f * lockPath is no
e4010 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 n-NULL, the host
e4020 20 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c ID and lock fil
e4030 65 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 e path must matc
e4040 68 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c h. A NULL .** l
e4050 6f 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 ockPath means th
e4060 61 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 at the lockPath
e4070 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c in the conch fil
e4080 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 e will be used i
e4090 66 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 f the .** host I
e40a0 44 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e Ds match, or a n
e40b0 65 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c ew lock path wil
e40c0 6c 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 l be generated a
e40d0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a utomatically .**
e40e0 20 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 and written to
e40f0 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a the conch file..
e4100 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
e4110 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 oxyTakeConch(uni
e4120 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 xFile *pFile){.
e4130 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
e4140 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
e4150 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
e4160 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
e4170 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 ngContext; . .
e4180 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 if( pCtx->conch
e4190 48 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 Held>0 ){. re
e41a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e41b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 }else{. uni
e41c0 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 xFile *conchFile
e41d0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 = pCtx->conchFi
e41e0 6c 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 le;. char tes
e41f0 74 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d tValue[CONCHLEN]
e4200 3b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 ;. char conch
e4210 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b Value[CONCHLEN];
e4220 0a 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 . char lockPa
e4230 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a th[MAXPATHLEN];.
e4240 20 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 char *tLockP
e4250 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 ath = NULL;.
e4260 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
e4270 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 OK;. int read
e4280 52 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a Rc = SQLITE_OK;.
e4290 20 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d int syncPerm
e42a0 73 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 s = 0;.. OSTR
e42b0 41 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 ACE4("TAKECONCH
e42c0 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 %d for %s pid=%
e42d0 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d d\n", conchFile-
e42e0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 >h,.
e42f0 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 (pCtx->lockProx
e4300 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f yPath ? pCtx->lo
e4310 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a ckProxyPath : ":
e4320 61 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 auto:"), getpid(
e4330 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f ));.. rc = co
e4340 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 nchFile->pMethod
e4350 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 ->xLock((sqlite3
e4360 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 _file*)conchFile
e4370 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a , SHARED_LOCK);.
e4380 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
e4390 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 TE_OK ){. i
e43a0 6e 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 nt pError = 0;.
e43b0 20 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 memset(test
e43c0 56 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c Value, 0, CONCHL
e43d0 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 EN); /* conch is
e43e0 20 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 fixed size */.
e43f0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 rc = proxyG
e4400 65 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c etHostID(testVal
e4410 75 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 ue, &pError);.
e4420 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 if( (rc&0xff
e4430 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 )==SQLITE_IOERR
e4440 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
e4450 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 ->lastErrno = pE
e4460 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rror;. }.
e4470 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f if( pCtx->lo
e4480 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
e4490 20 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 strlcpy(&
e44a0 74 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 testValue[HOSTID
e44b0 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b LEN], pCtx->lock
e44c0 50 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 ProxyPath, MAXPA
e44d0 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a THLEN);. }.
e44e0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
e44f0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
e4500 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 goto end_ta
e4510 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 keconch;. }.
e4520 20 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d . readRc =
e4530 20 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 unixRead((sqlit
e4540 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 e3_file *)conchF
e4550 69 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c ile, conchValue,
e4560 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 CONCHLEN, 0);.
e4570 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 if( readRc!=S
e4580 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
e4590 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 T_READ ){.
e45a0 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 if( readRc!=SQLI
e45b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
e45c0 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d if( (rc&0xff)==
e45d0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a SQLITE_IOERR ){.
e45e0 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
e45f0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e >lastErrno = con
e4600 63 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e chFile->lastErrn
e4610 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 o;. }.
e4620 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 rc = readRc
e4630 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 ;. goto e
e4640 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 nd_takeconch;.
e4650 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 }. /* i
e4660 66 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 f the conch has
e4670 64 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 data compare the
e4680 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 contents */.
e4690 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f if( !pCtx->lo
e46a0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
e46b0 20 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 /* for au
e46c0 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c to-named local l
e46d0 6f 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 ock file, just c
e46e0 68 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 heck the host ID
e46f0 20 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 and we'll.
e4700 20 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c ** use the l
e4710 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 ocal lock file p
e4720 61 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 ath that's alrea
e4730 64 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 dy in there */.
e4740 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 if( !memc
e4750 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f mp(testValue, co
e4760 6e 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 nchValue, HOSTID
e4770 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 LEN) ){.
e4780 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 tLockPath = (c
e4790 68 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 har *)&conchValu
e47a0 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 e[HOSTIDLEN];.
e47b0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
e47c0 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 _takeconch;.
e47d0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
e47e0 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 e{. /* we
e47f0 27 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 've got the conc
e4800 68 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 h if conchValue
e4810 6d 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 matches our path
e4820 20 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a and host ID */.
e4830 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d if( !mem
e4840 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 cmp(testValue, c
e4850 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 onchValue, CONCH
e4860 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 LEN) ){.
e4870 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 goto end_takec
e4880 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a onch;. }.
e4890 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
e48a0 65 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 e{. /* a sh
e48b0 6f 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 ort read means w
e48c0 65 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 e're "creating"
e48d0 74 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 the conch (even
e48e0 74 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 though it could
e48f0 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 . ** have b
e4900 65 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 een user-interve
e4910 6e 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 ntion), if we ac
e4920 71 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 quire the exclus
e4930 69 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 ive lock,.
e4940 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 ** we'll try to
e4950 6d 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e match the curren
e4960 74 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 t on-disk permis
e4970 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 sions of the dat
e4980 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 abase. */.
e4990 20 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d syncPerms =
e49a0 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 1;. }. .
e49b0 20 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e /* either con
e49c0 63 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 ch was emtpy or
e49d0 64 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a didn't match */.
e49e0 20 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c if( !pCtx->l
e49f0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a ockProxyPath ){.
e4a00 20 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f proxyGetLo
e4a10 63 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 ckPath(pCtx->dbP
e4a20 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d ath, lockPath, M
e4a30 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 AXPATHLEN);.
e4a40 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f tLockPath = lo
e4a50 63 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 ckPath;. st
e4a60 72 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 rlcpy(&testValue
e4a70 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 [HOSTIDLEN], loc
e4a80 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 kPath, MAXPATHLE
e4a90 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 N);. }. .
e4aa0 20 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e /* update con
e4ab0 63 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 ch with host and
e4ac0 20 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c path (this will
e4ad0 20 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 fail if other p
e4ae0 72 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 rocess. ** h
e4af0 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b as a shared lock
e4b00 20 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 already) */.
e4b10 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d rc = conchFile-
e4b20 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 >pMethod->xLock(
e4b30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 (sqlite3_file*)c
e4b40 6f 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 onchFile, EXCLUS
e4b50 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 IVE_LOCK);. i
e4b60 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
e4b70 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 ){. rc = u
e4b80 6e 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 nixWrite((sqlite
e4b90 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 3_file *)conchFi
e4ba0 6c 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 le, testValue, C
e4bb0 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 ONCHLEN, 0);.
e4bc0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
e4bd0 45 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d E_OK && syncPerm
e4be0 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 s ){. str
e4bf0 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 uct stat buf;.
e4c00 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 int err =
e4c10 66 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 fstat(pFile->h,
e4c20 26 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 &buf);. i
e4c30 66 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 f( err==0 ){.
e4c40 20 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f /* try to
e4c50 20 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62 match the datab
e4c60 61 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 ase file permiss
e4c70 69 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 ions, ignore fai
e4c80 6c 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 lure */.#ifndef
e4c90 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 SQLITE_PROXY_DEB
e4ca0 55 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 UG. fch
e4cb0 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 mod(conchFile->h
e4cc0 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a , buf.st_mode);.
e4cd0 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 #else.
e4ce0 69 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 if( fchmod(conch
e4cf0 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f File->h, buf.st_
e4d00 6d 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 mode)!=0 ){.
e4d10 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 int code
e4d20 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
e4d30 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
e4d40 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f derr, "fchmod %o
e4d50 20 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 FAILED with %d
e4d60 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %s\n",.
e4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4d80 20 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c buf.st_mode,
e4d90 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 code, strerror(
e4da0 63 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 code));.
e4db0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
e4dc0 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 fprintf(s
e4dd0 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 tderr, "fchmod %
e4de0 6f 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 o SUCCEDED\n",bu
e4df0 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 f.st_mode);.
e4e00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
e4e10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
e4e20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e int code = errn
e4e30 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 o;. fpr
e4e40 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 intf(stderr, "ST
e4e50 41 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 AT FAILED[%d] wi
e4e60 74 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 th %d %s\n", .
e4e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4e80 20 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 err, cod
e4e90 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 e, strerror(code
e4ea0 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 ));.#endif.
e4eb0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
e4ec0 20 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 }. conchFile
e4ed0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f ->pMethod->xUnlo
e4ee0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
e4ef0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 *)conchFile, SHA
e4f00 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e RED_LOCK);. .en
e4f10 64 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 d_takeconch:.
e4f20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 OSTRACE2("TRANS
e4f30 50 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 PROXY: CLOSE %d
e4f40 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a \n", pFile->h);.
e4f50 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
e4f60 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e TE_OK && pFile->
e4f70 6f 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 openFlags ){.
e4f80 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e if( pFile->h>
e4f90 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 =0 ){.#ifdef STR
e4fa0 49 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a ICT_CLOSE_ERROR.
e4fb0 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 if( clos
e4fc0 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 e(pFile->h) ){.
e4fd0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
e4fe0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
e4ff0 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 o;. ret
e5000 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
e5010 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 _CLOSE;.
e5020 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 }.#else.
e5030 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b close(pFile->h);
e5040 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 /* silently lea
e5050 6b 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a k fd if fail */.
e5060 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 #endif. }.
e5070 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 pFile->h =
e5080 2d 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 -1;. int fd
e5090 20 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 = open(pCtx->db
e50a0 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 Path, pFile->ope
e50b0 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 nFlags,.
e50c0 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 SQLI
e50d0 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f TE_DEFAULT_FILE_
e50e0 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 PERMISSIONS);.
e50f0 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 OSTRACE2("TR
e5100 41 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 ANSPROXY: OPEN
e5110 25 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 %d\n", fd);.
e5120 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 if( fd>=0 ){.
e5130 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 pFile->h
e5140 3d 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 = fd;. }els
e5150 65 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 e{. rc=SQ
e5160 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f LITE_CANTOPEN; /
e5170 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 * SQLITE_BUSY? p
e5180 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 roxyTakeConch ca
e5190 6c 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 lled.
e51a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e51b0 20 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 during locki
e51c0 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 ng */. }.
e51d0 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
e51e0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 SQLITE_OK && !pC
e51f0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b tx->lockProxy ){
e5200 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 . char *pat
e5210 68 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 h = tLockPath ?
e5220 74 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 tLockPath : pCtx
e5230 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b ->lockProxyPath;
e5240 0a 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e . /* ACS: N
e5250 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f eed to make a co
e5260 70 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 py of path somet
e5270 69 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 imes */. rc
e5280 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e = proxyCreateUn
e5290 69 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 ixFile(path, &pC
e52a0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a tx->lockProxy);.
e52b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
e52c0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
e52d0 20 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 pCtx->conch
e52e0 48 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 Held = 1;..
e52f0 20 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 if( tLockPath )
e5300 7b 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e {. pCtx->
e5310 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 lockProxyPath =
e5320 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
e5330 30 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 0, tLockPath);.
e5340 20 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d if( pCtx-
e5350 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 >lockProxy->pMet
e5360 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 hod == &afpIoMet
e5370 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 hods ){.
e5380 20 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f ((afpLockingCo
e5390 6e 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f ntext *)pCtx->lo
e53a0 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 ckProxy->locking
e53b0 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 Context)->dbPath
e53c0 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 =.
e53d0 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f pCtx->lo
e53e0 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 ckProxyPath;.
e53f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
e5400 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
e5410 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 conchFile->pMe
e5420 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 thod->xUnlock((s
e5430 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
e5440 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 chFile, NO_LOCK)
e5450 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 ;. }. OSTR
e5460 41 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 ACE3("TAKECONCH
e5470 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 %d %s\n", conch
e5480 46 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c File->h, rc==SQL
e5490 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 ITE_OK?"ok":"fai
e54a0 6c 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 led");. retur
e54b0 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a n rc;. }.}../*.
e54c0 2a 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 ** If pFile hold
e54d0 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f s a lock on a co
e54e0 6e 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 nch file, then r
e54f0 65 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b elease that lock
e5500 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e5510 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 proxyReleaseConc
e5520 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c h(unixFile *pFil
e5530 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 e){. int rc;
e5540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e5550 20 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 /* Subroutine
e5560 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 return code */.
e5570 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
e5580 74 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 text *pCtx; /*
e5590 54 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 The locking cont
e55a0 65 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 ext for the prox
e55b0 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 y lock */. unix
e55c0 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b File *conchFile;
e55d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
e55e0 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c of the conch fil
e55f0 65 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 e */.. pCtx = (
e5600 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
e5610 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 ext *)pFile->loc
e5620 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 kingContext;. c
e5630 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d onchFile = pCtx-
e5640 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 >conchFile;. OS
e5650 54 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 TRACE4("RELEASEC
e5660 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 ONCH %d for %s
e5670 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 pid=%d\n", conch
e5680 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
e5690 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 (pCtx->lockP
e56a0 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d roxyPath ? pCtx-
e56b0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a >lockProxyPath :
e56c0 20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 ":auto:"), .
e56d0 20 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 getpid()
e56e0 29 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 );. pCtx->conch
e56f0 48 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d Held = 0;. rc =
e5700 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 conchFile->pMet
e5710 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 hod->xUnlock((sq
e5720 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 lite3_file*)conc
e5730 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b hFile, NO_LOCK);
e5740 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c . OSTRACE3("REL
e5750 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 EASECONCH %d %s
e5760 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e \n", conchFile->
e5770 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 h,. (r
e5780 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 c==SQLITE_OK ? "
e5790 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 ok" : "failed"))
e57a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
e57b0 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 ../*.** Given th
e57c0 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 e name of a data
e57d0 62 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 base file, compu
e57e0 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 te the name of i
e57f0 74 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a ts conch file..*
e5800 2a 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 * Store the conc
e5810 68 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 h filename in me
e5820 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
e5830 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
e5840 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 c()..** Make *pC
e5850 6f 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 onchPath point t
e5860 6f 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 o the new name.
e5870 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
e5880 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 K on success.**
e5890 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 or SQLITE_NOMEM
e58a0 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 if unable to obt
e58b0 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a ain memory..**.*
e58c0 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 * The caller is
e58d0 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
e58e0 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 ensuring that th
e58f0 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f e allocated memo
e5900 72 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 ry.** space is e
e5910 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e ventually freed.
e5920 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 .**.** *pConchPa
e5930 74 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c th is set to NUL
e5940 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c L if a memory al
e5950 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f location error o
e5960 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ccurs..*/.static
e5970 20 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 int proxyCreate
e5980 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 ConchPathname(ch
e5990 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 ar *dbPath, char
e59a0 20 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a **pConchPath){.
e59b0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
e59c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e59d0 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
e59e0 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 */. int len = (
e59f0 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 int)strlen(dbPat
e5a00 68 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 h); /* Length of
e5a10 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 database filena
e5a20 6d 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 me - dbPath */.
e5a30 20 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 char *conchPath
e5a40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
e5a50 2a 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 * buffer in whic
e5a60 68 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 h to construct c
e5a70 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 onch name */..
e5a80 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 /* Allocate spac
e5a90 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 e for the conch
e5aa0 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 filename and ini
e5ab0 74 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 tialize the name
e5ac0 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d to. ** the nam
e5ad0 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 e of the origina
e5ae0 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e l database file.
e5af0 20 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 */ . *pConchP
e5b00 61 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 ath = conchPath
e5b10 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
e5b20 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 3_malloc(len + 8
e5b30 29 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 );. if( conchPa
e5b40 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 th==0 ){. ret
e5b50 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
e5b60 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 ;. }. memcpy(c
e5b70 6f 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 onchPath, dbPath
e5b80 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f , len+1);. . /
e5b90 2a 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 * now insert a "
e5ba0 2e 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 ." before the la
e5bb0 73 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a st / character *
e5bc0 2f 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d /. for( i=(len-
e5bd0 31 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 1); i>=0; i-- ){
e5be0 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 . if( conchPa
e5bf0 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 th[i]=='/' ){.
e5c00 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 i++;. b
e5c10 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
e5c20 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 conchPath[i]='
e5c30 2e 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c .';. while ( i<
e5c40 6c 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 len ){. conch
e5c50 50 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 Path[i+1]=dbPath
e5c60 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 [i];. i++;.
e5c70 7d 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 }.. /* append t
e5c80 68 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 he "-conch" suff
e5c90 69 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a ix to the file *
e5ca0 2f 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 /. memcpy(&conc
e5cb0 68 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f hPath[i+1], "-co
e5cc0 6e 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 nch", 7);. asse
e5cd0 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 rt( (int)strlen(
e5ce0 63 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 conchPath) == le
e5cf0 6e 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e n+7 );.. return
e5d00 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
e5d10 2f 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 /* Takes a fully
e5d20 20 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 configured prox
e5d30 79 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 y locking-style
e5d40 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 unix file and sw
e5d50 69 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f itches.** the lo
e5d60 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 cal lock file pa
e5d70 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th .*/.static in
e5d80 74 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 t switchLockProx
e5d90 79 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a yPath(unixFile *
e5da0 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 pFile, const cha
e5db0 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f r *path) {. pro
e5dc0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
e5dd0 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c *pCtx = (proxyL
e5de0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 ockingContext*)p
e5df0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
e5e00 74 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c text;. char *ol
e5e10 64 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f dPath = pCtx->lo
e5e20 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 ckProxyPath;. i
e5e30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
e5e40 4b 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d K;.. if( pFile-
e5e50 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f >locktype!=NO_LO
e5e60 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e CK ){. return
e5e70 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
e5e80 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e } .. /* nothin
e5e90 67 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 g to do if the p
e5ea0 61 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 ath is NULL, :au
e5eb0 74 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 to: or matches t
e5ec0 68 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 he existing path
e5ed0 20 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 */. if( !path
e5ee0 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 || path[0]=='\0'
e5ef0 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 || !strcmp(path
e5f00 2c 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 , ":auto:") ||.
e5f10 20 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 (oldPath && !
e5f20 73 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c strncmp(oldPath,
e5f30 20 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 path, MAXPATHLE
e5f40 4e 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 N)) ){. retur
e5f50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
e5f60 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 else{. unixFi
e5f70 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 le *lockProxy =
e5f80 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
e5f90 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 . pCtx->lockP
e5fa0 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 roxy=NULL;. p
e5fb0 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d Ctx->conchHeld =
e5fc0 20 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 0;. if( lock
e5fd0 50 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 Proxy!=NULL ){.
e5fe0 20 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 rc=lockProx
e5ff0 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f y->pMethod->xClo
e6000 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 se((sqlite3_file
e6010 20 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 *)lockProxy);.
e6020 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
e6030 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 turn rc;. s
e6040 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b qlite3_free(lock
e6050 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 Proxy);. }.
e6060 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f sqlite3_free(o
e6070 6c 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 ldPath);. pCt
e6080 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
e6090 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
e60a0 75 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d up(0, path);. }
e60b0 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b . . return rc;
e60c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 .}../*.** pFile
e60d0 69 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 is a file that h
e60e0 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 as been opened b
e60f0 79 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 y a prior xOpen
e6100 63 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a call. dbPath.**
e6110 20 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 is a string buf
e6120 66 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 fer at least MAX
e6130 50 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 PATHLEN+1 charac
e6140 74 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a ters in size..**
e6150 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
e6160 20 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 find the filena
e6170 6d 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 me associated wi
e6180 74 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 th pFile and wri
e6190 74 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 tes it.** int db
e61a0 50 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Path..*/.static
e61b0 69 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 int proxyGetDbPa
e61c0 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e thForUnixFile(un
e61d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 ixFile *pFile, c
e61e0 68 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 har *dbPath){.#i
e61f0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
e6200 45 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 E__). if( pFile
e6210 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 ->pMethod == &af
e6220 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 pIoMethods ){.
e6230 20 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b /* afp style k
e6240 65 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 eeps a reference
e6250 20 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 to the db path
e6260 69 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 in the filePath
e6270 66 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 field . ** of
e6280 20 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 the struct */.
e6290 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 assert( (int)
e62a0 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 strlen((char*)pF
e62b0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
e62c0 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e ext)<=MAXPATHLEN
e62d0 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 );. strcpy(d
e62e0 62 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b bPath, ((afpLock
e62f0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 ingContext *)pFi
e6300 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
e6310 78 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 xt)->dbPath);.
e6320 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 }else.#endif. i
e6330 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f f( pFile->pMetho
e6340 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d d == &dotlockIoM
e6350 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a ethods ){. /*
e6360 20 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 dot lock style
e6370 75 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 uses the locking
e6380 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 context to stor
e6390 65 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 e the dot lock.
e63a0 20 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 ** file path
e63b0 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d */. int len =
e63c0 20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 strlen((char *)
e63d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
e63e0 6e 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 ntext) - strlen(
e63f0 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b DOTLOCK_SUFFIX);
e6400 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 . memcpy(dbPa
e6410 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c th, (char *)pFil
e6420 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
e6430 74 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d t, len + 1);. }
e6440 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c else{. /* all
e6450 20 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 other styles us
e6460 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f e the locking co
e6470 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 ntext to store t
e6480 68 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 he db file path
e6490 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 */. assert( s
e64a0 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 trlen((char*)pFi
e64b0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
e64c0 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 xt)<=MAXPATHLEN
e64d0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 );. strcpy(db
e64e0 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 Path, (char *)pF
e64f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
e6500 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ext);. }. retu
e6510 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
e6520 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 ./*.** Takes an
e6530 61 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 already filled i
e6540 6e 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 n unix file and
e6550 61 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c alters it so all
e6560 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a file locking .*
e6570 2a 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 * will be perfor
e6580 6d 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c med on the local
e6590 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 proxy lock file
e65a0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
e65b0 20 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 fields.** are p
e65c0 72 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 reserved in the
e65d0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 locking context
e65e0 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e so that they can
e65f0 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 be restored and
e6600 20 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 .** the unix st
e6610 72 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 ructure properly
e6620 20 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 cleaned up at c
e6630 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d lose time:.** -
e6640 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a >lockingContext.
e6650 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f ** ->pMethod.*/
e6660 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
e6670 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 yTransformUnixFi
e6680 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 le(unixFile *pFi
e6690 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a le, const char *
e66a0 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c path) {. proxyL
e66b0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 ockingContext *p
e66c0 43 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 Ctx;. char dbPa
e66d0 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d th[MAXPATHLEN+1]
e66e0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ; /* Name
e66f0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
e6700 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
e6710 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 lockPath=NULL;.
e6720 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
e6730 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 _OK;. . if( pF
e6740 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e ile->locktype!=N
e6750 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 O_LOCK ){. re
e6760 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
e6770 3b 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 ;. }. proxyGet
e6780 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c DbPathForUnixFil
e6790 65 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 e(pFile, dbPath)
e67a0 3b 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c ;. if( !path ||
e67b0 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c path[0]=='\0' |
e67c0 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 | !strcmp(path,
e67d0 22 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 ":auto:") ){.
e67e0 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a lockPath=NULL;.
e67f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 }else{. loc
e6800 6b 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 kPath=(char *)pa
e6810 74 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 th;. }. . OST
e6820 52 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 RACE4("TRANSPROX
e6830 59 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 Y %d for %s pid
e6840 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
e6850 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f ,. (lo
e6860 63 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 ckPath ? lockPat
e6870 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 h : ":auto:"), g
e6880 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 etpid());.. pCt
e6890 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c x = sqlite3_mall
e68a0 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 oc( sizeof(*pCtx
e68b0 29 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d ) );. if( pCtx=
e68c0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
e68d0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
e68e0 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 }. memset(pCtx
e68f0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 , 0, sizeof(*pCt
e6900 78 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f x));.. rc = pro
e6910 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 xyCreateConchPat
e6920 68 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 hname(dbPath, &p
e6930 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 Ctx->conchFilePa
e6940 74 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 th);. if( rc==S
e6950 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e6960 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 rc = proxyCreate
e6970 55 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 UnixFile(pCtx->c
e6980 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 onchFilePath, &p
e6990 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b Ctx->conchFile);
e69a0 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d . } . if( rc=
e69b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f =SQLITE_OK && lo
e69c0 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 ckPath ){. pC
e69d0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
e69e0 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 h = sqlite3DbStr
e69f0 44 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 Dup(0, lockPath)
e6a00 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d ;. }.. if( rc=
e6a10 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
e6a20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 /* all memory
e6a30 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 is allocated, pr
e6a40 6f 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64 oxys are created
e6a50 20 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a and assigned, .
e6a60 20 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 ** switch th
e6a70 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 e locking contex
e6a80 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 t and pMethod th
e6a90 65 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a en return.. *
e6aa0 2f 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 /. pCtx->dbPa
e6ab0 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 th = sqlite3DbSt
e6ac0 72 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b rDup(0, dbPath);
e6ad0 0a 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f . pCtx->oldLo
e6ae0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 ckingContext = p
e6af0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
e6b00 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d text;. pFile-
e6b10 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 >lockingContext
e6b20 3d 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 = pCtx;. pCtx
e6b30 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 ->pOldMethod = p
e6b40 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 File->pMethod;.
e6b50 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f pFile->pMetho
e6b60 64 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 d = &proxyIoMeth
e6b70 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ods;. }else{.
e6b80 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 if( pCtx->conc
e6b90 68 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 hFile ){ .
e6ba0 72 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 rc = pCtx->conch
e6bb0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 File->pMethod->x
e6bc0 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 Close((sqlite3_f
e6bd0 69 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 ile *)pCtx->conc
e6be0 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 hFile);. if
e6bf0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
e6c00 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
e6c10 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 free(pCtx->conch
e6c20 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 File);. }.
e6c30 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
e6c40 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 tx->conchFilePat
e6c50 68 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 h); . sqlite3
e6c60 5f 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d _free(pCtx);. }
e6c70 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 . OSTRACE3("TRA
e6c80 4e 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e NSPROXY %d %s\n
e6c90 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 ", pFile->h,.
e6ca0 20 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c (rc==SQL
e6cb0 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 ITE_OK ? "ok" :
e6cc0 22 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 "failed"));. re
e6cd0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
e6ce0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
e6cf0 68 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f handles sqlite3_
e6d00 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 file_control() c
e6d10 61 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 alls that are sp
e6d20 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f ecific.** to pro
e6d30 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 xy locking..*/.s
e6d40 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 tatic int proxyF
e6d50 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 ileControl(sqlit
e6d60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
e6d70 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 op, void *pArg)
e6d80 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 {. switch( op )
e6d90 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
e6da0 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_GET_LOCKPROXYF
e6db0 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 ILE: {. uni
e6dc0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
e6dd0 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
e6de0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 if( pFile->p
e6df0 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 Method == &proxy
e6e00 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
e6e10 20 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e proxyLockin
e6e20 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d gContext *pCtx =
e6e30 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f (proxyLockingCo
e6e40 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f ntext*)pFile->lo
e6e50 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
e6e60 20 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 proxyTakeC
e6e70 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 onch(pFile);.
e6e80 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c if( pCtx->l
e6e90 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a ockProxyPath ){.
e6ea0 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 *(cons
e6eb0 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d t char **)pArg =
e6ec0 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
e6ed0 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 Path;. }e
e6ee0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a lse{. *
e6ef0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 (const char **)p
e6f00 41 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e Arg = ":auto: (n
e6f10 6f 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 ot held)";.
e6f20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 }. } els
e6f30 65 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f e {. *(co
e6f40 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 nst char **)pArg
e6f50 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d = NULL;. }
e6f60 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e6f70 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
e6f80 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 case SQLITE_S
e6f90 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
e6fa0 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 : {. unixFi
e6fb0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
e6fc0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 xFile*)id;.
e6fd0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
e6fe0 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 _OK;. int i
e6ff0 73 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 sProxyStyle = (p
e7000 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d File->pMethod ==
e7010 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 &proxyIoMethods
e7020 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 );. if( pAr
e7030 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 g==NULL || (cons
e7040 74 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 t char *)pArg==0
e7050 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
e7060 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a isProxyStyle ){.
e7070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 /* tur
e7080 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b n off proxy lock
e7090 69 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 ing - not suppor
e70a0 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 ted */.
e70b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
e70c0 4f 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 OR /*SQLITE_PROT
e70d0 4f 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 OCOL? SQLITE_MIS
e70e0 55 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 USE?*/;.
e70f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
e7100 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f /* turn off pro
e7110 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 xy locking - alr
e7120 65 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 eady off - NOOP
e7130 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 */. rc
e7140 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
e7150 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
e7160 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 se{. cons
e7170 74 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 t char *proxyPat
e7180 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 h = (const char
e7190 2a 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 *)pArg;.
e71a0 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 if( isProxyStyle
e71b0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 ){. pr
e71c0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
e71d0 74 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 t *pCtx = .
e71e0 20 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 (proxyLoc
e71f0 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 kingContext*)pFi
e7200 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
e7210 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 xt;. if
e7220 28 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 ( !strcmp(pArg,
e7230 22 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 ":auto:") .
e7240 20 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e || (pCtx->
e7250 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 lockProxyPath &&
e7260 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
e7270 21 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c !strncmp(pCtx->l
e7280 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 ockProxyPath, pr
e7290 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 oxyPath, MAXPATH
e72a0 4c 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 LEN)).
e72b0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ){. r
e72c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
e72d0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
e72e0 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
e72f0 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 switchLockProxy
e7300 50 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 Path(pFile, prox
e7310 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 yPath);.
e7320 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
e7330 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
e7340 74 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 turn on proxy fi
e7350 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 le locking */.
e7360 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f rc = pro
e7370 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 xyTransformUnixF
e7380 69 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 ile(pFile, proxy
e7390 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d Path);. }
e73a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
e73b0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
e73c0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
e73d0 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 assert( 0 )
e73e0 3b 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 ; /* The call a
e73f0 73 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 ssures that only
e7400 20 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 valid opcodes a
e7410 72 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d re sent */. }
e7420 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 . }. /*NOTREAC
e7430 48 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 HED*/. return S
e7440 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a QLITE_ERROR;.}..
e7450 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 /*.** Within thi
e7460 73 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 s division (the
e7470 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 proxying locking
e7480 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 implementation)
e7490 20 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a the procedures.
e74a0 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f ** above this po
e74b0 69 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c int are all util
e74c0 69 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b ities. The lock
e74d0 2d 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 -related methods
e74e0 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 of the.** proxy
e74f0 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 -locking sqlite3
e7500 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 _io_method objec
e7510 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f t follow..*/.../
e7520 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
e7530 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
e7540 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
e7550 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
e7560 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
e7570 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
e7580 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
e7590 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
e75a0 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
e75b0 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
e75c0 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
e75d0 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
e75e0 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
e75f0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
e7600 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
e7610 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
e7620 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
e7630 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
e7640 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 checking..*/.sta
e7650 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 tic int proxyChe
e7660 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
e7670 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
e7680 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b int *pResOut) {
e7690 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
e76a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
e76b0 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 id;. int rc = p
e76c0 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 roxyTakeConch(pF
e76d0 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ile);. if( rc==
e76e0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
e76f0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
e7700 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
e7710 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
e7720 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
e7730 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 ngContext;. u
e7740 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d nixFile *proxy =
e7750 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
e7760 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f ;. return pro
e7770 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 xy->pMethod->xCh
e7780 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
e7790 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 (sqlite3_file*)p
e77a0 72 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a roxy, pResOut);.
e77b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
e77c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
e77d0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
e77e0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
e77f0 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
e7800 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
e7810 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
e7820 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
e7830 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
e7840 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
e7850 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
e7860 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
e7870 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
e7880 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
e7890 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
e78a0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
e78b0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
e78c0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
e78d0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
e78e0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
e78f0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
e7900 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
e7910 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
e7920 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
e7930 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
e7940 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
e7950 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
e7960 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
e7970 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
e7980 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
e7990 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
e79a0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
e79b0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
e79c0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
e79d0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
e79e0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
e79f0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
e7a00 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
e7a10 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
e7a20 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
e7a30 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
e7a40 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
e7a50 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
e7a60 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
e7a70 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
e7a80 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
e7a90 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
e7aa0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
e7ab0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
e7ac0 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
e7ad0 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
e7ae0 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
e7af0 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 t proxyLock(sqli
e7b00 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
e7b10 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 t locktype) {.
e7b20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
e7b30 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
e7b40 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 . int rc = prox
e7b50 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 yTakeConch(pFile
e7b60 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
e7b70 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 ITE_OK ){. pr
e7b80 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
e7b90 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 t *pCtx = (proxy
e7ba0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
e7bb0 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
e7bc0 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 ontext;. unix
e7bd0 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 File *proxy = pC
e7be0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 tx->lockProxy;.
e7bf0 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 rc = proxy->p
e7c00 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 Method->xLock((s
e7c10 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f qlite3_file*)pro
e7c20 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 xy, locktype);.
e7c30 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
e7c40 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b pe = proxy->lock
e7c50 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 type;. }. retu
e7c60 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
e7c70 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
e7c80 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
e7c90 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
e7ca0 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 e to locktype.
e7cb0 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 locktype.** must
e7cc0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
e7cd0 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
e7ce0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
e7cf0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
e7d00 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
e7d10 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
e7d20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
e7d30 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
e7d40 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
e7d50 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
e7d60 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -op..*/.static i
e7d70 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 nt proxyUnlock(s
e7d80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
e7d90 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
e7da0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
e7db0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
e7dc0 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 id;. int rc = p
e7dd0 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 roxyTakeConch(pF
e7de0 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ile);. if( rc==
e7df0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
e7e00 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
e7e10 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
e7e20 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
e7e30 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
e7e40 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 ngContext;. u
e7e50 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d nixFile *proxy =
e7e60 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
e7e70 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 ;. rc = proxy
e7e80 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f ->pMethod->xUnlo
e7e90 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
e7ea0 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 *)proxy, locktyp
e7eb0 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c e);. pFile->l
e7ec0 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d ocktype = proxy-
e7ed0 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 >locktype;. }.
e7ee0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
e7ef0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
e7f00 65 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 e that uses prox
e7f10 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 y locks..*/.stat
e7f20 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 ic int proxyClos
e7f30 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
e7f40 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 id) {. if( id )
e7f50 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a {. unixFile *
e7f60 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
e7f70 65 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 e*)id;. proxy
e7f80 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
e7f90 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 pCtx = (proxyLoc
e7fa0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 kingContext *)pF
e7fb0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
e7fc0 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c ext;. unixFil
e7fd0 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 e *lockProxy = p
e7fe0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
e7ff0 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f unixFile *co
e8000 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e nchFile = pCtx->
e8010 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 conchFile;. i
e8020 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
e8030 4b 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 K;. . if(
e8040 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 lockProxy ){.
e8050 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 rc = lockProx
e8060 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c y->pMethod->xUnl
e8070 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
e8080 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f e*)lockProxy, NO
e8090 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 _LOCK);. if
e80a0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
e80b0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 ;. rc = loc
e80c0 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d kProxy->pMethod-
e80d0 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 >xClose((sqlite3
e80e0 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 _file*)lockProxy
e80f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
e8100 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
e8110 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
e8120 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 lockProxy);.
e8130 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 pCtx->lockProx
e8140 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 y = 0;. }.
e8150 20 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 if( conchFile )
e8160 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 {. if( pCtx
e8170 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 ->conchHeld ){.
e8180 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 rc = prox
e8190 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 yReleaseConch(pF
e81a0 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ile);. if
e81b0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
e81c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
e81d0 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e rc = conchFile->
e81e0 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 pMethod->xClose(
e81f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 (sqlite3_file*)c
e8200 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 onchFile);.
e8210 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
e8220 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 rc;. sqlit
e8230 65 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c e3_free(conchFil
e8240 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 e);. }. sq
e8250 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d lite3_free(pCtx-
e8260 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b >lockProxyPath);
e8270 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
e8280 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c e(pCtx->conchFil
e8290 65 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 ePath);. sqli
e82a0 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 te3_free(pCtx->d
e82b0 62 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 bPath);. /* r
e82c0 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 estore the origi
e82d0 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 nal locking cont
e82e0 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 ext and pMethod
e82f0 74 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f then close it */
e8300 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
e8310 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 ingContext = pCt
e8320 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e x->oldLockingCon
e8330 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d text;. pFile-
e8340 3e 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d >pMethod = pCtx-
e8350 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 >pOldMethod;.
e8360 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
e8370 74 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 tx);. return
e8380 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e pFile->pMethod->
e8390 78 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a xClose(id);. }.
e83a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e83b0 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 OK;.}....#endif
e83c0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 /* defined(__APP
e83d0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
e83e0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e83f0 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 TYLE */./*.** Th
e8400 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 e proxy locking
e8410 73 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 style is intende
e8420 64 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 d for use with A
e8430 46 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a FP filesystems..
e8440 2a 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 ** And since AFP
e8450 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
e8460 65 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 ed on MacOSX, th
e8470 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 e proxy locking
e8480 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 is also.** restr
e8490 69 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e icted to MacOSX.
e84a0 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a .** .**.********
e84b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
e84c0 6f 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 of the proxy loc
e84d0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
e84e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
e84f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
e8500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8540 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 ******/../*.** I
e8550 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 nitialize the op
e8560 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
e8570 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
e8580 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 This routine reg
e8590 69 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 isters all VFS i
e85a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 mplementations f
e85b0 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 or unix-like ope
e85c0 72 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d rating.** system
e85d0 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 s. This routine
e85e0 2c 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 , and the sqlite
e85f0 33 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 3_os_end() routi
e8600 6e 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c ne that follows,
e8610 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 .** should be th
e8620 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 e only routines
e8630 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 in this file tha
e8640 74 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 t are visible fr
e8650 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 om other.** file
e8660 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
e8670 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
e8680 6f 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 once during SQLi
e8690 74 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f te initializatio
e86a0 6e 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 n and by a.** si
e86b0 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 ngle thread. Th
e86c0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
e86d0 69 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 ion and mutex su
e86e0 62 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f bsystems have no
e86f0 74 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 t.** necessarily
e8700 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 been initialize
e8710 64 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 d when this rout
e8720 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 ine is called, a
e8730 6e 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 nd so they.** sh
e8740 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 ould not be used
e8750 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
e8760 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 int sqlite3_os_i
e8770 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a nit(void){ . /*
e8780 20 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f . ** The follo
e8790 77 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e wing macro defin
e87a0 65 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 es an initialize
e87b0 72 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 r for an sqlite3
e87c0 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a _vfs object.. *
e87d0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
e87e0 65 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 e VFS is NAME.
e87f0 54 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20 The pAppData is
e8800 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 a pointer to a p
e8810 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 ointer. ** to t
e8820 68 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 he "finder" func
e8830 74 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 tion. (pAppData
e8840 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
e8850 20 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 a pointer becau
e8860 73 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 se. ** silly C9
e8870 30 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 0 rules prohibit
e8880 20 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 a void* from be
e8890 69 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 ing cast to a fu
e88a0 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 nction pointer.
e88b0 20 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 ** and so we ha
e88c0 76 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 ve to go through
e88d0 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 the intermediat
e88e0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f e pointer to avo
e88f0 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a id problems. **
e8900 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 when compiling
e8910 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 with -pedantic-e
e8920 72 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 rrors on GCC.).
e8930 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e **. ** The FIN
e8940 44 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f DER parameter to
e8950 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 this macro is t
e8960 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 he name of the p
e8970 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 ointer to the.
e8980 2a 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 ** finder-functi
e8990 6f 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d on. The finder-
e89a0 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
e89b0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
e89c0 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f e. ** sqlite_io
e89d0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 _methods object
e89e0 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 that implements
e89f0 74 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b the desired lock
e8a00 69 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f ing. ** behavio
e8a10 72 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 rs. See the div
e8a20 69 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 ision above that
e8a30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f contains the IO
e8a40 4d 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 METHODS. ** mac
e8a50 72 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 ro for addition
e8a60 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 information on f
e8a70 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e inder-functions.
e8a80 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 . **. ** Most
e8a90 66 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 finders simply r
e8aa0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
e8ab0 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 to a fixed sqlit
e8ac0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 e3_io_methods.
e8ad0 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 ** object. But
e8ae0 74 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 the "autolockIoF
e8af0 69 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 inder" available
e8b00 20 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 on MacOSX does
e8b10 61 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f a little. ** mo
e8b20 72 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 re than that; it
e8b30 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 looks at the fi
e8b40 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 lesystem type th
e8b50 61 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 at hosts the .
e8b60 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
e8b70 20 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 and tries to ch
e8b80 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 oose an locking
e8b90 6d 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 method appropria
e8ba0 74 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 te for. ** that
e8bb0 20 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 filesystem time
e8bc0 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 .. */. #define
e8bd0 20 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 UNIXVFS(VFSNAME
e8be0 2c 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 , FINDER) {
e8bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8c00 20 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 \. 1,
e8c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e8c20 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 * iVersion */
e8c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8c40 20 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e \. sizeof(un
e8c50 69 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 ixFile), /*
e8c60 73 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 szOsFile */
e8c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e8c80 0a 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d . MAX_PATHNAM
e8c90 45 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 E, /* mx
e8ca0 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 Pathname */
e8cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e8cc0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
e8cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 /* pNex
e8ce0 74 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 t */
e8cf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e8d00 20 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 VFSNAME,
e8d10 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 /* zName
e8d20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e8d30 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 \. (
e8d40 76 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 void*)&FINDER,
e8d50 20 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 /* pAppData
e8d60 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e8d70 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
e8d80 78 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 xOpen,
e8d90 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 /* xOpen */
e8da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8db0 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 \. unixD
e8dc0 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 elete,
e8dd0 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 /* xDelete */
e8de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8df0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 \. unixAcc
e8e00 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ess, /
e8e10 2a 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 * xAccess */
e8e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8e30 20 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 \. unixFullP
e8e40 61 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 athname, /*
e8e50 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f xFullPathname */
e8e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e8e70 0a 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c . unixDlOpen,
e8e80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 /* xD
e8e90 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 lOpen */
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e8eb0 20 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 unixDlError,
e8ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 /* xDlE
e8ed0 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 rror */
e8ee0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e8ef0 20 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 unixDlSym,
e8f00 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d /* xDlSym
e8f10 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e8f20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
e8f30 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 nixDlClose,
e8f40 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 /* xDlClose
e8f50 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e8f60 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
e8f70 78 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 xRandomness,
e8f80 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 /* xRandomnes
e8f90 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 s */
e8fa0 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 \. unixS
e8fb0 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 leep,
e8fc0 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 /* xSleep */
e8fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8fe0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 \. unixCur
e8ff0 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f rentTime, /
e9000 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a * xCurrentTime *
e9010 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e9020 20 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 \. unixGetLa
e9030 73 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 stError /*
e9040 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f xGetLastError */
e9050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e9060 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 . }.. /*. **
e9070 41 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 All default VFSe
e9080 73 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 s for unix are c
e9090 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 ontained in the
e90a0 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e following array.
e90b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 . **. ** Note
e90c0 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 that the sqlite3
e90d0 5f 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 _vfs.pNext field
e90e0 20 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 of the VFS obje
e90f0 63 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 ct is modified.
e9100 20 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 ** by the SQLit
e9110 65 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 e core when the
e9120 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 VFS is registere
e9130 64 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f d. So the follo
e9140 77 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 wing. ** array
e9150 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e cannot be const.
e9160 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 . */. static s
e9170 71 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b qlite3_vfs aVfs[
e9180 5d 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 ] = {.#if SQLITE
e9190 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e91a0 53 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 STYLE && (OS_VXW
e91b0 4f 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 ORKS || defined(
e91c0 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 __APPLE__)).
e91d0 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 UNIXVFS("unix",
e91e0 20 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 autoloc
e91f0 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c kIoFinder ),.#el
e9200 73 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 se. UNIXVFS("
e9210 75 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 unix",
e9220 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c posixIoFinder ),
e9230 0a 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 .#endif. UNIX
e9240 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c VFS("unix-none",
e9250 20 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e nolockIoFin
e9260 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 der ),. UNIXV
e9270 46 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 FS("unix-dotfile
e9280 22 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e ", dotlockIoFin
e9290 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 der ),.#if OS_VX
e92a0 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 WORKS. UNIXVF
e92b0 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d S("unix-namedsem
e92c0 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 ", semIoFinder )
e92d0 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c ,.#endif.#if SQL
e92e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e92f0 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 NG_STYLE. UNI
e9300 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 XVFS("unix-posix
e9310 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e ", posixIoFin
e9320 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56 der ),.#if !OS_V
e9330 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 XWORKS. UNIXV
e9340 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c FS("unix-flock",
e9350 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 flockIoFinde
e9360 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64 r ),.#endif.#end
e9370 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e if.#if SQLITE_EN
e9380 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e9390 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f LE && defined(__
e93a0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 APPLE__). UNI
e93b0 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c XVFS("unix-afp",
e93c0 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65 afpIoFinde
e93d0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
e93e0 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 ("unix-proxy",
e93f0 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 proxyIoFinder
e9400 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 ),.#endif. };.
e9410 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b unsigned int i;
e9420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
e9430 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 p counter */..
e9440 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 /* Register all
e9450 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e VFSes defined in
e9460 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61 the aVfs[] arra
e9470 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 y */. for(i=0;
e9480 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f i<(sizeof(aVfs)/
e9490 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 sizeof(sqlite3_v
e94a0 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 fs)); i++){.
e94b0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
e94c0 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69 ster(&aVfs[i], i
e94d0 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ==0);. }. retu
e94e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d rn SQLITE_OK; .}
e94f0 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e ../*.** Shutdown
e9500 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
e9510 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e ystem interface.
e9520 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72 .**.** Some oper
e9530 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69 ating systems mi
e9540 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73 ght need to do s
e9550 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74 ome cleanup in t
e9560 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 his routine,.**
e9570 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d to release dynam
e9580 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 ically allocated
e9590 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e objects. But n
e95a0 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54 ot on unix..** T
e95b0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
e95c0 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e no-op for unix.
e95d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
e95e0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nt sqlite3_os_en
e95f0 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 d(void){ . retu
e9600 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d rn SQLITE_OK; .}
e9610 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c . .#endif /* SQL
e9620 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a ITE_OS_UNIX */..
e9630 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
e9640 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 End of os_unix.c
e9650 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
e9660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
e9680 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
e9690 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69 Begin file os_wi
e96a0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
e96b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e96c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
e96d0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
e96e0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
e96f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
e9700 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
e9710 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
e9720 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
e9730 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
e9740 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
e9750 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
e9760 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
e9770 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
e9780 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
e9790 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
e97a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
e97b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
e97c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
e97d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
e97e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
e97f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
e9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9840 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
e9850 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
e9860 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 ode that is spec
e9870 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e ific to windows.
e9880 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 77 .**.** $Id: os_w
e9890 69 6e 2e 63 2c 76 20 31 2e 31 35 36 20 32 30 30 in.c,v 1.156 200
e98a0 39 2f 30 34 2f 32 33 20 31 39 3a 30 38 3a 33 33 9/04/23 19:08:33
e98b0 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a shane Exp $.*/.
e98c0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
e98d0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
e98e0 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 /* This file is
e98f0 75 73 65 64 20 66 6f 72 20 77 69 6e 64 6f 77 73 used for windows
e9900 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a only */.../*.**
e9910 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 A Note About Me
e9920 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a mory Allocation:
e9930 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 .**.** This driv
e9940 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 er uses malloc()
e9950 2f 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79 /free() directly
e9960 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69 rather than goi
e9970 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 ng through.** th
e9980 65 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72 e SQLite-wrapper
e9990 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 s sqlite3_malloc
e99a0 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 ()/sqlite3_free(
e99b0 29 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70 65 ). Those wrappe
e99c0 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e rs.** are design
e99d0 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d ed for use on em
e99e0 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20 77 bedded systems w
e99f0 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 here memory is s
e9a00 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c carce and.** mal
e9a10 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61 70 loc failures hap
e9a20 70 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 pen frequently.
e9a30 20 57 69 6e 33 32 20 64 6f 65 73 20 6e 6f 74 20 Win32 does not
e9a40 74 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e typically run on
e9a50 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73 .** embedded sys
e9a60 74 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 tems, and when i
e9a70 74 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c t does the devel
e9a80 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 opers normally h
e9a90 61 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 ave bigger.** pr
e9aa0 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 oblems to worry
e9ab0 61 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 about than runni
e9ac0 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 ng out of memory
e9ad0 2e 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e . So there is n
e9ae0 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 ot.** a compelli
e9af0 6e 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 ng need to use t
e9b00 68 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a he wrappers..**.
e9b10 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 ** But there is
e9b20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f a good reason to
e9b30 20 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61 not use the wra
e9b40 70 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73 ppers. If we us
e9b50 65 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 e the.** wrapper
e9b60 73 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 s then we will g
e9b70 65 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c et simulated mal
e9b80 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77 loc() failures w
e9b90 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 ithin this.** dr
e9ba0 69 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 iver. And that
e9bb0 63 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 causes all kinds
e9bc0 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 of problems for
e9bd0 20 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a our tests. We.
e9be0 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 ** could enhance
e9bf0 20 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 SQLite to deal
e9c00 77 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d with simulated m
e9c10 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77 alloc failures w
e9c20 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 ithin.** the OS
e9c30 64 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20 driver, but the
e9c40 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74 code to deal wit
e9c50 68 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20 h those failure
e9c60 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 would not.** be
e9c70 65 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e exercised on Lin
e9c80 75 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e ux (which does n
e9c90 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f ot need to mallo
e9ca0 63 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65 c() in the drive
e9cb0 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 r).** and so we
e9cc0 77 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69 would have diffi
e9cd0 63 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f culty writing co
e9ce0 76 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72 verage tests for
e9cf0 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 that.** code.
e9d00 42 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20 Better to leave
e9d10 74 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 the code out, we
e9d20 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 think..**.** Th
e9d30 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 e point of this
e9d40 64 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73 discussion is as
e9d50 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 follows: When
e9d60 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a creating a new.*
e9d70 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61 * OS layer for a
e9d80 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
e9d90 6d 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68 m, if you use th
e9da0 69 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 is file as an ex
e9db0 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 ample,.** avoid
e9dc0 74 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f the use of mallo
e9dd0 63 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f c()/free(). Tho
e9de0 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b se routines work
e9df0 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77 73 0a 2a ok on windows.*
e9e00 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 74 20 6e * desktops but n
e9e10 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d ot so well in em
e9e20 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2e 0a bedded systems..
e9e30 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 77 69 */..#include <wi
e9e40 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66 64 65 66 nbase.h>..#ifdef
e9e50 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23 20 69 6e __CYGWIN__.# in
e9e60 63 6c 75 64 65 20 3c 73 79 73 2f 63 79 67 77 69 clude <sys/cygwi
e9e70 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a n.h>.#endif../*.
e9e80 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 74 ** Macros used t
e9e90 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 o determine whet
e9ea0 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 her or not to us
e9eb0 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 e threads..*/.#i
e9ec0 66 20 64 65 66 69 6e 65 64 28 54 48 52 45 41 44 f defined(THREAD
e9ed0 53 41 46 45 29 20 26 26 20 54 48 52 45 41 44 53 SAFE) && THREADS
e9ee0 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c AFE.# define SQL
e9ef0 49 54 45 5f 57 33 32 5f 54 48 52 45 41 44 53 20 ITE_W32_THREADS
e9f00 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 1.#endif../*.**
e9f10 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 Include code tha
e9f20 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 t is common to a
e9f30 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a ll os_*.c files.
e9f40 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
e9f50 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f ** Include os_co
e9f60 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 mmon.h in the mi
e9f70 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 ddle of os_win.c
e9f80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
e9f90 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
e9fa0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 ** Begin file os
e9fb0 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
e9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9fe0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 */./*.** 2004 Ma
e9ff0 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 22.**.** The a
ea000 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
ea010 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
ea020 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
ea030 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
ea040 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
ea050 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
ea060 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
ea070 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
ea080 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
ea090 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
ea0a0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
ea0b0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
ea0c0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
ea0d0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
ea0e0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
ea0f0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
ea100 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
ea110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea150 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
ea160 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
ea170 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c s macros and a l
ea180 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 ittle bit of cod
ea190 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e e that is common
ea1a0 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 to.** all of th
ea1b0 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 e platform-speci
ea1c0 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e fic files (os_*.
ea1d0 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 c) and is #inclu
ea1e0 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a ded into those.*
ea1f0 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 * files..**.** T
ea200 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 his file should
ea210 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 be #included by
ea220 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 the os_*.c files
ea230 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f only. It is no
ea240 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 t a.** general p
ea250 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 urpose header fi
ea260 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f le..**.** $Id: o
ea270 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 s_common.h,v 1.3
ea280 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 8 2009/02/24 18:
ea290 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 40:50 danielk197
ea2a0 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 7 Exp $.*/.#ifnd
ea2b0 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ef _OS_COMMON_H_
ea2c0 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d .#define _OS_COM
ea2d0 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 MON_H_../*.** At
ea2e0 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 least two bugs
ea2f0 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 have slipped in
ea300 62 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 because we chang
ea310 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 ed the MEMORY_DE
ea320 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 BUG.** macro to
ea330 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 SQLITE_DEBUG and
ea340 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 some older make
ea350 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 files have not y
ea360 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 et made the.** s
ea370 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c witch. The foll
ea380 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c owing code shoul
ea390 64 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f d catch this pro
ea3a0 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d blem at compile-
ea3b0 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 time..*/.#ifdef
ea3c0 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 MEMORY_DEBUG.# e
ea3d0 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 rror "The MEMORY
ea3e0 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 _DEBUG macro is
ea3f0 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 obsolete. Use S
ea400 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 QLITE_DEBUG inst
ea410 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 ead.".#endif..#i
ea420 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
ea430 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
ea440 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 int sqlite3OSTr
ea450 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 ace = 0;.#define
ea460 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 OSTRACE1(X)
ea470 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
ea480 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
ea490 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 3DebugPrintf(X).
ea4a0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 #define OSTRACE2
ea4b0 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 (X,Y) if(
ea4c0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
ea4d0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
ea4e0 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 ntf(X,Y).#define
ea4f0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 OSTRACE3(X,Y,Z)
ea500 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
ea510 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
ea520 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
ea530 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Z).#define OSTR
ea540 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 ACE4(X,Y,Z,A)
ea550 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
ea560 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
ea570 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 gPrintf(X,Y,Z,A)
ea580 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
ea590 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 5(X,Y,Z,A,B) if(
ea5a0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
ea5b0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
ea5c0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a intf(X,Y,Z,A,B).
ea5d0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 #define OSTRACE6
ea5e0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a (X,Y,Z,A,B,C) \.
ea5f0 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 if(sqlite3OS
ea600 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 Trace) sqlite3De
ea610 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
ea620 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f A,B,C).#define O
ea630 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c STRACE7(X,Y,Z,A,
ea640 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 B,C,D) \. if(
ea650 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 sqlite3OSTrace)
ea660 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
ea670 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 tf(X,Y,Z,A,B,C,D
ea680 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ).#else.#define
ea690 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 OSTRACE1(X).#def
ea6a0 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 ine OSTRACE2(X,Y
ea6b0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
ea6c0 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e E3(X,Y,Z).#defin
ea6d0 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a e OSTRACE4(X,Y,Z
ea6e0 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,A).#define OSTR
ea6f0 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a ACE5(X,Y,Z,A,B).
ea700 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 #define OSTRACE6
ea710 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 (X,Y,Z,A,B,C).#d
ea720 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 efine OSTRACE7(X
ea730 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 ,Y,Z,A,B,C,D).#e
ea740 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 ndif../*.** Macr
ea750 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e os for performan
ea760 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 ce tracing. Nor
ea770 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 mally turned off
ea780 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a . Only works.**
ea790 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 on i486 hardwar
ea7a0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
ea7b0 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f ITE_PERFORMANCE_
ea7c0 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 TRACE../* .** hw
ea7d0 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 time.h contains
ea7e0 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 inline assembler
ea7f0 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d code for implem
ea800 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d enting .** high-
ea810 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 performance timi
ea820 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a ng routines..*/.
ea830 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
ea840 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 Include hwtime.h
ea850 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
ea860 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a f os_common.h **
ea870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
ea880 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
ea890 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d Begin file hwtim
ea8a0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
ea8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
ea8d0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 /*.** 2008 May 2
ea8e0 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
ea8f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
ea900 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
ea910 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
ea920 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
ea930 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
ea940 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
ea950 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
ea960 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
ea970 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
ea980 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
ea990 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
ea9a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
ea9b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
ea9c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
ea9d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
ea9e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
ea9f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa40 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
eaa50 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 file contains i
eaa60 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 nline asm code f
eaa70 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 or retrieving "h
eaa80 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 igh-performance"
eaa90 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 .** counters for
eaaa0 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e x86 class CPUs.
eaab0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 .**.** $Id: hwti
eaac0 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f me.h,v 1.3 2008/
eaad0 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 08/01 14:33:15 s
eaae0 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 hane Exp $.*/.#i
eaaf0 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f fndef _HWTIME_H_
eab00 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 .#define _HWTIME
eab10 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 _H_../*.** The f
eab20 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
eab30 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 only works on p
eab40 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 entium-class (or
eab50 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f newer) processo
eab60 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 rs..** It uses t
eab70 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 he RDTSC opcode
eab80 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c to read the cycl
eab90 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 e count value ou
eaba0 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 t of the.** proc
eabb0 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e essor and return
eabc0 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 s that value. T
eabd0 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 his can be used
eabe0 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 for high-res.**
eabf0 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 profiling..*/.#i
eac00 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
eac10 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 C__) || defined(
eac20 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a _MSC_VER)) && \.
eac30 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 (defined(i
eac40 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 386) || defined(
eac50 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 __i386__) || def
eac60 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a ined(_M_IX86))..
eac70 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f #if defined(__
eac80 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c GNUC__).. __inl
eac90 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
eaca0 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
eacb0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e e(void){. un
eacc0 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 signed int lo, h
eacd0 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 i;. __asm__
eace0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
eacf0 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f dtsc" : "=a" (lo
ead00 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 ), "=d" (hi));.
ead10 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 return (sqli
ead20 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 te_uint64)hi <<
ead30 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 32 | lo;. }..
ead40 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d #elif defined(_M
ead50 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 SC_VER).. __dec
ead60 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 lspec(naked) __i
ead70 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e nline sqlite_uin
ead80 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 t64 __cdecl sqli
ead90 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
eada0 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 . __asm {.
eadb0 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 rdtsc.
eadc0 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 ret ;
eadd0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 return value at
eade0 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 EDX:EAX. }.
eadf0 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 }.. #endif..#e
eae00 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 lif (defined(__G
eae10 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 NUC__) && define
eae20 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a d(__x86_64__))..
eae30 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
eae40 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
eae50 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
eae60 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
eae70 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f ong val;. _
eae80 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
eae90 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
eaea0 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 =A" (val));.
eaeb0 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 return val;.
eaec0 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e }. .#elif (defin
eaed0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
eaee0 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 defined(__ppc__)
eaef0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
eaf00 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
eaf10 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
eaf20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 ){. unsigne
eaf30 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 d long long retv
eaf40 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e al;. unsign
eaf50 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 ed long junk;.
eaf60 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
eaf70 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 latile__ ("\n\.
eaf80 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 1:
eaf90 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 mftbu %1\n\.
eafa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eafb0 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a mftb %L0\n\.
eafc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eafd0 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a mftbu %0\n\.
eafe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eaff0 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c cmpw %0,%1\
eb000 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
eb010 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 bne 1b"
eb020 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
eb030 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 : "=r" (retva
eb040 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 l), "=r" (junk))
eb050 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
eb060 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 etval;. }..#els
eb070 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 e.. #error Need
eb080 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
eb090 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 of sqlite3Hwtime
eb0a0 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
eb0b0 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a form... /*. **
eb0c0 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 To compile with
eb0d0 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 out implementing
eb0e0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
eb0f0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f for your platfo
eb100 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e rm,. ** you can
eb110 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 remove the abov
eb120 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 e #error and use
eb130 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
eb140 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f ** stub functio
eb150 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 n. You will los
eb160 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 e timing support
eb170 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f for many. ** o
eb180 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 f the debugging
eb190 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c and testing util
eb1a0 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 ities, but it sh
eb1b0 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 ould at. ** lea
eb1c0 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 st compile and r
eb1d0 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f un.. */.SQLITE_
eb1e0 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 PRIVATE sqlite
eb1f0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
eb200 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 wtime(void){ ret
eb210 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e urn ((sqlite_uin
eb220 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 t64)0); }..#endi
eb230 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
eb240 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f fined(_HWTIME_H_
eb250 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
eb260 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 ***** End of hwt
eb270 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
eb280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eb2a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
eb2b0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
eb2c0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
eb2d0 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e ff in os_common.
eb2e0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
eb2f0 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 ****/..static sq
eb300 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 lite_uint64 g_st
eb310 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 art;.static sqli
eb320 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 te_uint64 g_elap
eb330 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d sed;.#define TIM
eb340 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 ER_START g
eb350 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 _start=sqlite3Hw
eb360 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 time().#define T
eb370 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 IMER_END
eb380 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 g_elapsed=sqlit
eb390 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 e3Hwtime()-g_sta
eb3a0 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 rt.#define TIMER
eb3b0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 _ELAPSED g_e
eb3c0 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 lapsed.#else.#de
eb3d0 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 fine TIMER_START
eb3e0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
eb3f0 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 ND.#define TIMER
eb400 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 _ELAPSED ((s
eb410 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a qlite_uint64)0).
eb420 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 #endif../*.** If
eb430 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 we compile with
eb440 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 the SQLITE_TEST
eb450 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e macro set, then
eb460 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 the following b
eb470 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 lock.** of code
eb480 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 will give us the
eb490 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 ability to simu
eb4a0 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 late a disk I/O
eb4b0 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 error. This.**
eb4c0 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 is used for test
eb4d0 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f ing the I/O reco
eb4e0 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 very logic..*/.#
eb4f0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
eb500 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
eb510 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
eb520 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 r_hit = 0;
eb530 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
eb540 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 umber of I/O Err
eb550 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ors */.SQLITE_AP
eb560 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
eb570 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d _error_hardhit =
eb580 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 0; /* Nu
eb590 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 mber of non-beni
eb5a0 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c gn errors */.SQL
eb5b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
eb5c0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
eb5d0 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 ding = 0;
eb5e0 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 /* Count down t
eb5f0 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f o first I/O erro
eb600 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 r */.SQLITE_API
eb610 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
eb620 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 rror_persist = 0
eb630 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
eb640 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 if I/O errors p
eb650 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 ersist */.SQLITE
eb660 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
eb670 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e _io_error_benign
eb680 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
eb690 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 True if errors
eb6a0 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 are benign */.SQ
eb6b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
eb6c0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
eb6d0 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 nding = 0;.SQLIT
eb6e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
eb6f0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 3_diskfull = 0;.
eb700 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
eb710 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 IOErrorBenign(X)
eb720 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
eb730 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 r_benign=(X).#de
eb740 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
eb750 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 rror(CODE) \.
eb760 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f if( (sqlite3_io_
eb770 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 error_persist &&
eb780 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
eb790 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 r_hit) \.
eb7a0 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 || sqlite3_io_er
eb7b0 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d ror_pending-- ==
eb7c0 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 1 ) \.
eb7d0 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f { local_io
eb7e0 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 err(); CODE; }.s
eb7f0 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c tatic void local
eb800 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 _ioerr(){. IOTR
eb810 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 ACE(("IOERR\n"))
eb820 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 ;. sqlite3_io_e
eb830 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 rror_hit++;. if
eb840 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ( !sqlite3_io_er
eb850 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c ror_benign ) sql
eb860 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 ite3_io_error_ha
eb870 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 rdhit++;.}.#defi
eb880 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 ne SimulateDiskf
eb890 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c ullError(CODE) \
eb8a0 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f . if( sqlite3_
eb8b0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
eb8c0 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 ){ \. if( s
eb8d0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
eb8e0 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 pending == 1 ){
eb8f0 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 \. local_i
eb900 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 oerr(); \.
eb910 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
eb920 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 l = 1; \.
eb930 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
eb940 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 _hit = 1; \.
eb950 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 CODE; \.
eb960 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 }else{ \.
eb970 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
eb980 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 _pending--; \.
eb990 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 } \. }.#els
eb9a0 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 e.#define Simula
eb9b0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
eb9c0 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c X).#define Simul
eb9d0 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 ateIOError(A).#d
eb9e0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 efine SimulateDi
eb9f0 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 skfullError(A).#
eba00 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 endif../*.** Whe
eba10 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 n testing, keep
eba20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e a count of the n
eba30 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 umber of open fi
eba40 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 les..*/.#ifdef S
eba50 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
eba60 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
eba70 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
eba80 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f t = 0;.#define O
eba90 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 penCounter(X) s
ebaa0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 qlite3_open_file
ebab0 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 _count+=(X).#els
ebac0 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f e.#define OpenCo
ebad0 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a unter(X).#endif.
ebae0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
ebaf0 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ned(_OS_COMMON_H
ebb00 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _) */../********
ebb10 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
ebb20 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
ebb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebb50 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
ebb60 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
ebb70 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
ebb80 6f 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e 63 20 off in os_win.c
ebb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebba0 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f *****/../*.** So
ebbb0 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d me microsoft com
ebbc0 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 pilers lack this
ebbd0 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a definition..*/.
ebbe0 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f #ifndef INVALID_
ebbf0 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 0a FILE_ATTRIBUTES.
ebc00 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 # define INVALID
ebc10 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 _FILE_ATTRIBUTES
ebc20 20 28 28 44 57 4f 52 44 29 2d 31 29 20 0a 23 65 ((DWORD)-1) .#e
ebc30 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 ndif../*.** Dete
ebc40 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 rmine if we are
ebc50 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e dealing with Win
ebc60 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 dowsCE - which h
ebc70 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 as a much.** red
ebc80 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 uced API..*/.#if
ebc90 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ebca0 0a 23 20 64 65 66 69 6e 65 20 41 72 65 46 69 6c .# define AreFil
ebcb0 65 41 70 69 73 41 4e 53 49 28 29 20 31 0a 23 20 eApisANSI() 1.#
ebcc0 64 65 66 69 6e 65 20 47 65 74 44 69 73 6b 46 72 define GetDiskFr
ebcd0 65 65 53 70 61 63 65 57 28 29 20 30 0a 23 65 6e eeSpaceW() 0.#en
ebce0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 dif../*.** WinCE
ebcf0 20 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 lacks native su
ebd00 70 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c pport for file l
ebd10 6f 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 ocking so we hav
ebd20 65 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 e to fake it.**
ebd30 77 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f with some code o
ebd40 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 f our own..*/.#i
ebd50 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
ebd60 45 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 E.typedef struct
ebd70 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 winceLock {. i
ebd80 6e 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 nt nReaders;
ebd90 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ebda0 72 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 reader locks obt
ebdb0 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 ained */. BOOL
ebdc0 62 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f bPending; /
ebdd0 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 * Indicates a pe
ebde0 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 nding lock has b
ebdf0 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a een obtained */.
ebe00 20 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 BOOL bReserved
ebe10 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 ; /* Indicat
ebe20 65 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f es a reserved lo
ebe30 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 ck has been obta
ebe40 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 ined */. BOOL b
ebe50 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a Exclusive; /*
ebe60 20 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 Indicates an ex
ebe70 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 clusive lock has
ebe80 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a been obtained *
ebe90 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 /.} winceLock;.#
ebea0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
ebeb0 20 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 winFile structu
ebec0 72 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 re is a subclass
ebed0 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 of sqlite3_file
ebee0 2a 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 * specific to th
ebef0 65 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 e win32.** porta
ebf00 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f bility layer..*/
ebf10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
ebf20 77 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b winFile winFile;
ebf30 0a 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 .struct winFile
ebf40 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 {. const sqlite
ebf50 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
ebf60 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 ethod;/* Must be
ebf70 20 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 first */. HAND
ebf80 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 LE h;
ebf90 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f /* Handle fo
ebfa0 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 r accessing the
ebfb0 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e file */. unsign
ebfc0 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 ed char locktype
ebfd0 3b 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 ; /* Type of loc
ebfe0 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 k currently held
ebff0 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f on this file */
ec000 0a 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c . short sharedL
ec010 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 ockByte; /* Ra
ec020 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 ndomly chosen by
ec030 74 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61 te used as a sha
ec040 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57 red lock */. DW
ec050 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 ORD lastErrno;
ec060 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e /* The Win
ec070 64 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 dows errno from
ec080 74 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 the last I/O err
ec090 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65 or */. DWORD se
ec0a0 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 ctorSize;
ec0b0 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f /* Sector size o
ec0c0 66 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c f the device fil
ec0d0 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53 e is on */.#if S
ec0e0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
ec0f0 20 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f WCHAR *zDeleteO
ec100 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 nClose; /* Name
ec110 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 of file to dele
ec120 74 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 te when closing
ec130 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74 */. HANDLE hMut
ec140 65 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ex; /*
ec150 4d 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f Mutex used to co
ec160 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 ntrol access to
ec170 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20 shared lock */
ec180 0a 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65 . HANDLE hShare
ec190 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 d; /* Sh
ec1a0 61 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d ared memory segm
ec1b0 65 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 ent used for loc
ec1c0 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c king */. winceL
ec1d0 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20 ock local;
ec1e0 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69 /* Locks obtai
ec1f0 6e 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74 ned by this inst
ec200 61 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20 ance of winFile
ec210 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a */. winceLock *
ec220 73 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20 shared; /*
ec230 47 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f Global shared lo
ec240 63 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 ck memory for th
ec250 65 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69 e file */.#endi
ec260 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 f.};../*.** Forw
ec270 61 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a ard prototypes..
ec280 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
ec290 74 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 tSectorSize(.
ec2a0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
ec2b0 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 fs,. const ch
ec2c0 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 ar *zRelative
ec2d0 20 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 /* UTF-8 file
ec2e0 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a name */.);../*.*
ec2f0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
ec300 76 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 variable is (nor
ec310 6d 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 mally) set once
ec320 61 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 and never change
ec330 73 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e s.** thereafter.
ec340 20 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 It records whe
ec350 74 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 ther the operati
ec360 6e 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e ng system is Win
ec370 39 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 95.** or WinNT..
ec380 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 **.** 0: Opera
ec390 74 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e ting system unkn
ec3a0 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 own..** 1: Ope
ec3b0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 rating system is
ec3c0 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 Win95..** 2:
ec3d0 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d Operating system
ec3e0 20 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a is WinNT..**.**
ec3f0 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 In order to fac
ec400 69 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 ilitate testing
ec410 6f 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 on a WinNT syste
ec420 6d 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 m, the test fixt
ec430 75 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 ure.** can manua
ec440 6c 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c lly set this val
ec450 75 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 ue to 1 to emula
ec460 74 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f te Win98 behavio
ec470 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c r..*/.#ifdef SQL
ec480 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
ec490 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
ec4a0 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c os_type = 0;.#el
ec4b0 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 se.static int sq
ec4c0 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 lite3_os_type =
ec4d0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
ec4e0 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f Return true (no
ec4f0 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 n-zero) if we ar
ec500 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 e running under
ec510 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 WinNT, Win2K, Wi
ec520 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 nXP,.** or WinCE
ec530 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 . Return false
ec540 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 (zero) for Win95
ec550 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d , Win98, or WinM
ec560 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 E..**.** Here is
ec570 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 an interesting
ec580 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 observation: Wi
ec590 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 n95, Win98, and
ec5a0 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 WinME lack.** th
ec5b0 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 e LockFileEx() A
ec5c0 50 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 PI. But we can
ec5d0 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 still statically
ec5e0 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 link against th
ec5f0 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e at.** API as lon
ec600 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 g as we don't ca
ec610 6c 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 ll it when runni
ec620 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 ng Win95/98/ME.
ec630 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 A call to.** th
ec640 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
ec650 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
ec660 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 if the host is W
ec670 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a in95/98/ME or.**
ec680 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 WinNT/2K/XP so
ec690 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f that we will kno
ec6a0 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 w whether or not
ec6b0 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 we can safely c
ec6c0 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 all.** the LockF
ec6d0 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a ileEx() API..*/.
ec6e0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
ec6f0 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e NCE.# define isN
ec700 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 T() (1).#else.
ec710 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 static int isNT
ec720 28 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 (void){. if(
ec730 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d sqlite3_os_type=
ec740 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 =0 ){. OSVE
ec750 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b RSIONINFO sInfo;
ec760 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f . sInfo.dwO
ec770 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 SVersionInfoSize
ec780 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 = sizeof(sInfo)
ec790 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 ;. GetVersi
ec7a0 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 onEx(&sInfo);.
ec7b0 20 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 sqlite3_os_t
ec7c0 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c ype = sInfo.dwPl
ec7d0 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c atformId==VER_PL
ec7e0 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 ATFORM_WIN32_NT
ec7f0 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 ? 2 : 1;. }.
ec800 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
ec810 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 3_os_type==2;.
ec820 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
ec830 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a TE_OS_WINCE */..
ec840 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
ec850 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 UTF-8 string to
ec860 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 microsoft unicod
ec870 65 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a e (UTF-16?). .**
ec880 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c .** Space to hol
ec890 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 d the returned s
ec8a0 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
ec8b0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a d from malloc..*
ec8c0 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a /.static WCHAR *
ec8d0 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f utf8ToUnicode(co
ec8e0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
ec8f0 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 ame){. int nCha
ec900 72 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 r;. WCHAR *zWid
ec910 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 eFilename;.. nC
ec920 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 har = MultiByteT
ec930 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 oWideChar(CP_UTF
ec940 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 8, 0, zFilename,
ec950 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 -1, NULL, 0);.
ec960 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d zWideFilename =
ec970 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 malloc( nChar*s
ec980 69 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e izeof(zWideFilen
ec990 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 ame[0]) );. if(
ec9a0 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d zWideFilename==
ec9b0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
ec9c0 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 0;. }. nChar =
ec9d0 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 MultiByteToWide
ec9e0 43 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c Char(CP_UTF8, 0,
ec9f0 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 zFilename, -1,
eca00 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e zWideFilename, n
eca10 43 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 Char);. if( nCh
eca20 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 ar==0 ){. fre
eca30 65 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 e(zWideFilename)
eca40 3b 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e ;. zWideFilen
eca50 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ame = 0;. }. r
eca60 65 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e eturn zWideFilen
eca70 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f ame;.}../*.** Co
eca80 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 nvert microsoft
eca90 75 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 unicode to UTF-8
ecaa0 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 . Space to hold
ecab0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 the returned st
ecac0 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 ring is.** obtai
ecad0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 ned from malloc(
ecae0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 )..*/.static cha
ecaf0 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 r *unicodeToUtf8
ecb00 28 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 (const WCHAR *zW
ecb10 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 ideFilename){.
ecb20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 int nByte;. cha
ecb30 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 r *zFilename;..
ecb40 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
ecb50 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f rToMultiByte(CP_
ecb60 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 UTF8, 0, zWideFi
ecb70 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 lename, -1, 0, 0
ecb80 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 , 0, 0);. zFile
ecb90 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e name = malloc( n
ecba0 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 Byte );. if( zF
ecbb0 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 ilename==0 ){.
ecbc0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
ecbd0 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 nByte = WideCh
ecbe0 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 arToMultiByte(CP
ecbf0 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 _UTF8, 0, zWideF
ecc00 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 ilename, -1, zFi
ecc10 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 lename, nByte,.
ecc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ecc30 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 0,
ecc40 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 0);. if( nByte
ecc50 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 == 0 ){. free
ecc60 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 (zFilename);.
ecc70 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a zFilename = 0;.
ecc80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 }. return zFi
ecc90 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a lename;.}../*.**
ecca0 20 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 Convert an ansi
eccb0 20 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f string to micro
eccc0 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 soft unicode, ba
eccd0 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 sed on the.** cu
ecce0 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 rrent codepage s
eccf0 65 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 ettings for file
ecd00 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 apis..** .** Sp
ecd10 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ace to hold the
ecd20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
ecd30 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 is obtained.** f
ecd40 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 rom malloc..*/.s
ecd50 74 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 tatic WCHAR *mbc
ecd60 73 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 sToUnicode(const
ecd70 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
ecd80 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a ){. int nByte;.
ecd90 20 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 WCHAR *zMbcsFi
ecda0 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f lename;. int co
ecdb0 64 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 depage = AreFile
ecdc0 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f ApisANSI() ? CP_
ecdd0 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a ACP : CP_OEMCP;.
ecde0 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 . nByte = Multi
ecdf0 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 ByteToWideChar(c
ece00 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c odepage, 0, zFil
ece10 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c ename, -1, NULL,
ece20 30 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 0)*sizeof(WCHAR)
ece30 3b 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d ;. zMbcsFilenam
ece40 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 e = malloc( nByt
ece50 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 e*sizeof(zMbcsFi
ece60 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 lename[0]) );.
ece70 69 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d if( zMbcsFilenam
ece80 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 e==0 ){. retu
ece90 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 rn 0;. }. nByt
ecea0 65 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 e = MultiByteToW
eceb0 69 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 ideChar(codepage
ecec0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
eced0 2d 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d -1, zMbcsFilenam
ecee0 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 e, nByte);. if(
ecef0 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 nByte==0 ){.
ecf00 20 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e free(zMbcsFilen
ecf10 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 ame);. zMbcsF
ecf20 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
ecf30 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 . return zMbcsF
ecf40 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ilename;.}../*.*
ecf50 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 * Convert micros
ecf60 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d oft unicode to m
ecf70 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 ultibyte charact
ecf80 65 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 er string, based
ecf90 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 on the.** user'
ecfa0 73 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e s Ansi codepage.
ecfb0 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 .**.** Space to
ecfc0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
ecfd0 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 d string is obta
ecfe0 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c ined from.** mal
ecff0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
ed000 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f char *unicodeTo
ed010 4d 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 Mbcs(const WCHAR
ed020 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 *zWideFilename)
ed030 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
ed040 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
ed050 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 ;. int codepage
ed060 20 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e = AreFileApisAN
ed070 53 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 SI() ? CP_ACP :
ed080 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 CP_OEMCP;.. nBy
ed090 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d te = WideCharToM
ed0a0 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 ultiByte(codepag
ed0b0 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e e, 0, zWideFilen
ed0c0 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 ame, -1, 0, 0, 0
ed0d0 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d , 0);. zFilenam
ed0e0 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 e = malloc( nByt
ed0f0 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 e );. if( zFile
ed100 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 name==0 ){. r
ed110 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
ed120 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 Byte = WideCharT
ed130 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 oMultiByte(codep
ed140 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c age, 0, zWideFil
ed150 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 ename, -1, zFile
ed160 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 name, nByte,.
ed170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ed180 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 0, 0)
ed190 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d ;. if( nByte ==
ed1a0 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 0 ){. free(z
ed1b0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a Filename);. z
ed1c0 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 Filename = 0;.
ed1d0 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 }. return zFile
ed1e0 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 name;.}../*.** C
ed1f0 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 onvert multibyte
ed200 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e character strin
ed210 67 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 g to UTF-8. Spa
ed220 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a ce to hold the.*
ed230 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e * returned strin
ed240 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 g is obtained fr
ed250 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a om malloc()..*/.
ed260 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
ed270 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d *sqlite3_win32_m
ed280 62 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 bcs_to_utf8(cons
ed290 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
ed2a0 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c e){. char *zFil
ed2b0 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 enameUtf8;. WCH
ed2c0 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 AR *zTmpWide;..
ed2d0 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 zTmpWide = mbcs
ed2e0 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e ToUnicode(zFilen
ed2f0 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 ame);. if( zTmp
ed300 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 Wide==0 ){. r
ed310 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a eturn 0;. }. z
ed320 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 FilenameUtf8 = u
ed330 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d nicodeToUtf8(zTm
ed340 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a pWide);. free(z
ed350 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 TmpWide);. retu
ed360 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 rn zFilenameUtf8
ed370 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
ed380 72 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 rt UTF-8 to mult
ed390 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 ibyte character
ed3a0 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 string. Space t
ed3b0 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 o hold the .** r
ed3c0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
ed3d0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
ed3e0 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 malloc()..*/.sta
ed3f0 74 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f tic char *utf8To
ed400 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 Mbcs(const char
ed410 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 *zFilename){. c
ed420 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 har *zFilenameMb
ed430 63 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d cs;. WCHAR *zTm
ed440 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 pWide;.. zTmpWi
ed450 64 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f de = utf8ToUnico
ed460 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 de(zFilename);.
ed470 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 if( zTmpWide==0
ed480 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
ed490 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d ;. }. zFilenam
ed4a0 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 eMbcs = unicodeT
ed4b0 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b oMbcs(zTmpWide);
ed4c0 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 . free(zTmpWide
ed4d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c );. return zFil
ed4e0 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 enameMbcs;.}..#i
ed4f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
ed500 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a E./*************
ed510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
ed550 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e This section con
ed560 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 tains code for W
ed570 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a inCE only..*/./*
ed580 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f .** WindowsCE do
ed590 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f es not have a lo
ed5a0 63 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 caltime() functi
ed5b0 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 on. So create a
ed5c0 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a .** substitute..
ed5d0 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f */.struct tm *__
ed5e0 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 cdecl localtime(
ed5f0 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 const time_t *t)
ed600 0a 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 .{. static stru
ed610 63 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 ct tm y;. FILET
ed620 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 IME uTm, lTm;.
ed630 53 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a SYSTEMTIME pTm;.
ed640 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
ed650 74 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b t64;. t64 = *t;
ed660 0a 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 . t64 = (t64 +
ed670 31 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 11644473600)*100
ed680 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 00000;. uTm.dwL
ed690 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 74 36 34 owDateTime = t64
ed6a0 20 26 20 30 78 46 46 46 46 46 46 46 46 3b 0a 20 & 0xFFFFFFFF;.
ed6b0 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74 65 54 uTm.dwHighDateT
ed6c0 69 6d 65 3d 20 74 36 34 20 3e 3e 20 33 32 3b 0a ime= t64 >> 32;.
ed6d0 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 FileTimeToLoca
ed6e0 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c 26 lFileTime(&uTm,&
ed6f0 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65 lTm);. FileTime
ed700 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c 54 ToSystemTime(&lT
ed710 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d 5f m,&pTm);. y.tm_
ed720 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 72 year = pTm.wYear
ed730 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d 5f - 1900;. y.tm_
ed740 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 68 mon = pTm.wMonth
ed750 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 61 - 1;. y.tm_wda
ed760 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 65 y = pTm.wDayOfWe
ed770 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 20 ek;. y.tm_mday
ed780 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 2e = pTm.wDay;. y.
ed790 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 48 tm_hour = pTm.wH
ed7a0 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e 20 our;. y.tm_min
ed7b0 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a 20 = pTm.wMinute;.
ed7c0 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d 2e y.tm_sec = pTm.
ed7d0 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 72 wSecond;. retur
ed7e0 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 n &y;.}../* This
ed7f0 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 63 will never be c
ed800 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69 6e alled, but defin
ed810 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 ed to make the c
ed820 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a 23 ode compile */.#
ed830 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50 61 define GetTempPa
ed840 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 6e thA(a,b)..#defin
ed850 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 e LockFile(a,b,c
ed860 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e 63 ,d,e) winc
ed870 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c eLockFile(&a, b,
ed880 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e c, d, e).#defin
ed890 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c 62 e UnlockFile(a,b
ed8a0 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e 63 ,c,d,e) winc
ed8b0 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 eUnlockFile(&a,
ed8c0 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 b, c, d, e).#def
ed8d0 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 61 ine LockFileEx(a
ed8e0 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 69 ,b,c,d,e,f) wi
ed8f0 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 61 nceLockFileEx(&a
ed900 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 29 , b, c, d, e, f)
ed910 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c 45 ..#define HANDLE
ed920 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 28 _TO_WINFILE(a) (
ed930 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 72 winFile*)&((char
ed940 2a 29 61 29 5b 2d 6f 66 66 73 65 74 6f 66 28 77 *)a)[-offsetof(w
ed950 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a inFile,h)]../*.*
ed960 2a 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b * Acquire a lock
ed970 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68 on the handle h
ed980 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
ed990 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 winceMutexAcquir
ed9a0 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 e(HANDLE h){.
ed9b0 44 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20 DWORD dwErr;.
ed9c0 64 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20 do {. dwErr
ed9d0 3d 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f = WaitForSingleO
ed9e0 62 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 bject(h, INFINIT
ed9f0 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 E);. } while (
eda00 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 dwErr != WAIT_OB
eda10 4a 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 JECT_0 && dwErr
eda20 21 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 != WAIT_ABANDONE
eda30 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 D);.}./*.** Rele
eda40 61 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 ase a lock acqui
eda50 72 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 red by winceMute
eda60 78 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 xAcquire().*/.#d
eda70 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 efine winceMutex
eda80 52 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 Release(h) Relea
eda90 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a seMutex(h)../*.*
edaa0 2a 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 * Create the mut
edab0 65 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 ex and shared me
edac0 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f mory used for lo
edad0 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c cking in the fil
edae0 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 e.** descriptor
edaf0 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 pFile.*/.static
edb00 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 BOOL winceCreate
edb10 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 Lock(const char
edb20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 *zFilename, winF
edb30 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57 ile *pFile){. W
edb40 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 CHAR *zTok;. WC
edb50 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 HAR *zName = utf
edb60 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 8ToUnicode(zFile
edb70 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 name);. BOOL bI
edb80 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f nit = TRUE;.. /
edb90 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
edba0 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20 local lockdata
edbb0 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 */. ZeroMemory(
edbc0 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 &pFile->local, s
edbd0 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 izeof(pFile->loc
edbe0 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c al));.. /* Repl
edbf0 61 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73 ace the backslas
edc00 68 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c hes from the fil
edc10 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 ename and lowerc
edc20 61 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64 ase it. ** to d
edc30 65 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 erive a mutex na
edc40 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 me. */. zTok =
edc50 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 CharLowerW(zName
edc60 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b );. for (;*zTok
edc70 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 ;zTok++){. if
edc80 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 (*zTok == '\\')
edc90 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 *zTok = '_';.
edca0 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f }.. /* Create/o
edcb0 70 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75 pen the named mu
edcc0 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e tex */. pFile->
edcd0 68 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d hMutex = CreateM
edce0 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 utexW(NULL, FALS
edcf0 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20 E, zName);. if
edd00 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 (!pFile->hMutex)
edd10 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
edd20 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
edd30 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 66 72 65 Error();. fre
edd40 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 e(zName);. re
edd50 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a turn FALSE;. }.
edd60 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 . /* Acquire th
edd70 65 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 63 e mutex before c
edd80 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 ontinuing */. w
edd90 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 inceMutexAcquire
edda0 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
eddb0 0a 20 20 0a 20 20 2f 2a 20 53 69 6e 63 65 20 74 . . /* Since t
eddc0 68 65 20 6e 61 6d 65 73 20 6f 66 20 6e 61 6d 65 he names of name
eddd0 64 20 6d 75 74 65 78 65 73 2c 20 73 65 6d 61 70 d mutexes, semap
edde0 68 6f 72 65 73 2c 20 66 69 6c 65 20 6d 61 70 70 hores, file mapp
eddf0 69 6e 67 73 20 65 74 63 20 61 72 65 20 0a 20 20 ings etc are .
ede00 2a 2a 20 63 61 73 65 2d 73 65 6e 73 69 74 69 76 ** case-sensitiv
ede10 65 2c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 e, take advantag
ede20 65 20 6f 66 20 74 68 61 74 20 62 79 20 75 70 70 e of that by upp
ede30 65 72 63 61 73 69 6e 67 20 74 68 65 20 6d 75 74 ercasing the mut
ede40 65 78 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 ex name. ** and
ede50 20 75 73 69 6e 67 20 74 68 61 74 20 61 73 20 74 using that as t
ede60 68 65 20 73 68 61 72 65 64 20 66 69 6c 65 6d 61 he shared filema
ede70 70 70 69 6e 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f pping name.. */
ede80 0a 20 20 43 68 61 72 55 70 70 65 72 57 28 7a 4e . CharUpperW(zN
ede90 61 6d 65 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 ame);. pFile->h
edea0 53 68 61 72 65 64 20 3d 20 43 72 65 61 74 65 46 Shared = CreateF
edeb0 69 6c 65 4d 61 70 70 69 6e 67 57 28 49 4e 56 41 ileMappingW(INVA
edec0 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
eded0 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 , NULL,.
edee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
edf00 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30 AGE_READWRITE, 0
edf10 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f , sizeof(winceLo
edf20 63 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ck),.
edf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
edf40 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d zNam
edf50 65 29 3b 20 20 0a 0a 20 20 2f 2a 20 53 65 74 20 e); .. /* Set
edf60 61 20 66 6c 61 67 20 74 68 61 74 20 69 6e 64 69 a flag that indi
edf70 63 61 74 65 73 20 77 65 27 72 65 20 74 68 65 20 cates we're the
edf80 66 69 72 73 74 20 74 6f 20 63 72 65 61 74 65 20 first to create
edf90 74 68 65 20 6d 65 6d 6f 72 79 20 73 6f 20 69 74 the memory so it
edfa0 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 7a . ** must be z
edfb0 65 72 6f 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 ero-initialized
edfc0 2a 2f 0a 20 20 69 66 20 28 47 65 74 4c 61 73 74 */. if (GetLast
edfd0 45 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52 Error() == ERROR
edfe0 5f 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29 _ALREADY_EXISTS)
edff0 7b 0a 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41 {. bInit = FA
ee000 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 LSE;. }.. free
ee010 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 (zName);.. /* I
ee020 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 f we succeeded i
ee030 6e 20 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61 n making the sha
ee040 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c red memory handl
ee050 65 2c 20 6d 61 70 20 69 74 2e 20 2a 2f 0a 20 20 e, map it. */.
ee060 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 if (pFile->hShar
ee070 65 64 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ed){. pFile->
ee080 73 68 61 72 65 64 20 3d 20 28 77 69 6e 63 65 4c shared = (winceL
ee090 6f 63 6b 2a 29 4d 61 70 56 69 65 77 4f 66 46 69 ock*)MapViewOfFi
ee0a0 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 le(pFile->hShare
ee0b0 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 d, .
ee0c0 20 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 7c 46 FILE_MAP_READ|F
ee0d0 49 4c 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30 ILE_MAP_WRITE, 0
ee0e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 , 0, sizeof(winc
ee0f0 65 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 eLock));. /*
ee100 49 66 20 6d 61 70 70 69 6e 67 20 66 61 69 6c 65 If mapping faile
ee110 64 2c 20 63 6c 6f 73 65 20 74 68 65 20 73 68 61 d, close the sha
ee120 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c red memory handl
ee130 65 20 61 6e 64 20 65 72 61 73 65 20 69 74 20 2a e and erase it *
ee140 2f 0a 20 20 20 20 69 66 20 28 21 70 46 69 6c 65 /. if (!pFile
ee150 2d 3e 73 68 61 72 65 64 29 7b 0a 20 20 20 20 20 ->shared){.
ee160 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
ee170 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
ee180 28 29 3b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48 ();. CloseH
ee190 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 andle(pFile->hSh
ee1a0 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69 ared);. pFi
ee1b0 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 le->hShared = NU
ee1c0 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 LL;. }. }..
ee1d0 20 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65 /* If shared me
ee1e0 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 mory could not b
ee1f0 65 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 e created, then
ee200 63 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20 close the mutex
ee210 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 and fail */. if
ee220 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 (pFile->hShared
ee230 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 == NULL){. w
ee240 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 inceMutexRelease
ee250 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
ee260 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 . CloseHandle
ee270 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
ee280 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 . pFile->hMut
ee290 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 ex = NULL;. r
ee2a0 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d eturn FALSE;. }
ee2b0 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c . . /* Initial
ee2c0 69 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d ize the shared m
ee2d0 65 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 emory if we're s
ee2e0 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 upposed to */.
ee2f0 69 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 if (bInit) {.
ee300 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c ZeroMemory(pFil
ee310 65 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f e->shared, sizeo
ee320 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 f(winceLock));.
ee330 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 }.. winceMutex
ee340 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 Release(pFile->h
ee350 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e Mutex);. return
ee360 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TRUE;.}../*.**
ee370 44 65 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 Destroy the part
ee380 20 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 of winFile that
ee390 20 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 deals with winc
ee3a0 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 e locks.*/.stati
ee3b0 63 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 c void winceDest
ee3c0 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 royLock(winFile
ee3d0 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 *pFile){. if (p
ee3e0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 File->hMutex){.
ee3f0 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 /* Acquire th
ee400 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 e mutex */. w
ee410 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 inceMutexAcquire
ee420 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
ee430 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c .. /* The fol
ee440 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 lowing blocks sh
ee450 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 ould probably as
ee460 73 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f sert in debug mo
ee470 64 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 de, but they.
ee480 20 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e are to clean
ee490 75 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c up in case any l
ee4a0 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 ocks remained op
ee4b0 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 en */. if (pF
ee4c0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
ee4d0 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c ers){. pFil
ee4e0 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 e->shared->nRead
ee4f0 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 ers --;. }.
ee500 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
ee510 61 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 al.bReserved){.
ee520 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
ee530 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 ed->bReserved =
ee540 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 FALSE;. }.
ee550 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 if (pFile->loca
ee560 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 l.bPending){.
ee570 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
ee580 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c ->bPending = FAL
ee590 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 SE;. }. if
ee5a0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 (pFile->local.b
ee5b0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 Exclusive){.
ee5c0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
ee5d0 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 >bExclusive = FA
ee5e0 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 LSE;. }..
ee5f0 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 /* De-reference
ee600 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f and close our co
ee610 70 79 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 py of the shared
ee620 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a memory handle *
ee630 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f /. UnmapViewO
ee640 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 fFile(pFile->sha
ee650 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 red);. CloseH
ee660 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 andle(pFile->hSh
ee670 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 ared);.. /* D
ee680 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75 74 one with the mut
ee690 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d ex */. winceM
ee6a0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
ee6b0 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a e->hMutex); .
ee6c0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 CloseHandle(
ee6d0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
ee6e0 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 pFile->hMute
ee6f0 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a x = NULL;. }.}.
ee700 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 ./* .** An imple
ee710 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
ee720 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20 LockFile() API
ee730 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 of windows for w
ee740 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 ince.*/.static B
ee750 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c OOL winceLockFil
ee760 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 e(. HANDLE *phF
ee770 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 ile,. DWORD dwF
ee780 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 ileOffsetLow,.
ee790 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 DWORD dwFileOffs
ee7a0 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 etHigh,. DWORD
ee7b0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
ee7c0 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 LockLow,. DWORD
ee7d0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
ee7e0 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 oLockHigh.){. w
ee7f0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 inFile *pFile =
ee800 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c HANDLE_TO_WINFIL
ee810 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f E(phFile);. BOO
ee820 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 L bReturn = FALS
ee830 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 E;.. if (!pFile
ee840 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e ->hMutex) return
ee850 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 TRUE;. winceMu
ee860 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 texAcquire(pFile
ee870 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a ->hMutex);.. /*
ee880 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c Wanting an excl
ee890 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 usive lock? */.
ee8a0 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 if (dwFileOffse
ee8b0 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 46 tLow == SHARED_F
ee8c0 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e IRST. && n
ee8d0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
ee8e0 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 ockLow == SHARED
ee8f0 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 _SIZE){. if (
ee900 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e pFile->shared->n
ee910 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 Readers == 0 &&
ee920 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
ee930 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b Exclusive == 0){
ee940 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 . pFile->s
ee950 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 hared->bExclusiv
ee960 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 e = TRUE;.
ee970 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 pFile->local.bE
ee980 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b xclusive = TRUE;
ee990 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 . bReturn
ee9a0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
ee9b0 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 }.. /* Want a r
ee9c0 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a ead-only lock? *
ee9d0 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 28 64 77 /. else if ((dw
ee9e0 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d FileOffsetLow >=
ee9f0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 SHARED_FIRST &&
eea00 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 77 46 . dwF
eea10 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20 53 ileOffsetLow < S
eea20 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 53 48 HARED_FIRST + SH
eea30 41 52 45 44 5f 53 49 5a 45 29 20 26 26 0a 20 20 ARED_SIZE) &&.
eea40 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 65 nNumbe
eea50 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f rOfBytesToLockLo
eea60 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 w == 1){. if
eea70 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e (pFile->shared->
eea80 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 bExclusive == 0)
eea90 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
eeaa0 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b 2b ocal.nReaders ++
eeab0 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69 6c ;. if (pFil
eeac0 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 e->local.nReader
eead0 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 20 s == 1){.
eeae0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
eeaf0 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 nReaders ++;.
eeb00 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 }. bRetu
eeb10 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
eeb20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 . }.. /* Want
eeb30 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f 20 a pending lock?
eeb40 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 */. else if (dw
eeb50 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d FileOffsetLow ==
eeb60 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 PENDING_BYTE &&
eeb70 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
eeb80 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a oLockLow == 1){.
eeb90 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e /* If no pen
eeba0 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 ding lock has be
eebb0 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68 65 en acquired, the
eebc0 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f 0a n acquire it */.
eebd0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 if (pFile->s
eebe0 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 hared->bPending
eebf0 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 == 0) {. pF
eec00 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 ile->shared->bPe
eec10 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 nding = TRUE;.
eec20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c pFile->local
eec30 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 .bPending = TRUE
eec40 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 ;. bReturn
eec50 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
eec60 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 }. /* Want a re
eec70 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a served lock? */.
eec80 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c else if (dwFil
eec90 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 45 eOffsetLow == RE
eeca0 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e SERVED_BYTE && n
eecb0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
eecc0 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 ockLow == 1){.
eecd0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 if (pFile->sha
eece0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d red->bReserved =
eecf0 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 = 0) {. pFi
eed00 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 le->shared->bRes
eed10 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 erved = TRUE;.
eed20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c pFile->local
eed30 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 .bReserved = TRU
eed40 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
eed50 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
eed60 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 }.. winceMutex
eed70 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 Release(pFile->h
eed80 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e Mutex);. return
eed90 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a bReturn;.}../*.
eeda0 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ** An implementa
eedb0 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f tion of the Unlo
eedc0 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69 ckFile API of wi
eedd0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a ndows for wince.
eede0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 */.static BOOL w
eedf0 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a inceUnlockFile(.
eee00 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 HANDLE *phFile
eee10 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 ,. DWORD dwFile
eee20 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f OffsetLow,. DWO
eee30 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 RD dwFileOffsetH
eee40 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 igh,. DWORD nNu
eee50 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c mberOfBytesToUnl
eee60 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 ockLow,. DWORD
eee70 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
eee80 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 UnlockHigh.){.
eee90 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
eeea0 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 HANDLE_TO_WINFI
eeeb0 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f LE(phFile);. BO
eeec0 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c OL bReturn = FAL
eeed0 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c SE;.. if (!pFil
eeee0 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 e->hMutex) retur
eeef0 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d n TRUE;. winceM
eef00 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
eef10 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f e->hMutex);.. /
eef20 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65 * Releasing a re
eef30 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 ader lock or an
eef40 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a exclusive lock *
eef50 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 /. if (dwFileOf
eef60 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52 45 fsetLow >= SHARE
eef70 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 D_FIRST &&.
eef80 20 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f dwFileOffsetLo
eef90 77 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 w < SHARED_FIRST
eefa0 20 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 7b + SHARED_SIZE){
eefb0 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 68 . /* Did we h
eefc0 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ave an exclusive
eefd0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 lock? */. if
eefe0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 (pFile->local.b
eeff0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 Exclusive){.
ef000 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 pFile->local.b
ef010 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 Exclusive = FALS
ef020 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e E;. pFile->
ef030 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 shared->bExclusi
ef040 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ve = FALSE;.
ef050 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 bReturn = TRUE
ef060 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
ef070 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65 Did we just have
ef080 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20 a reader lock?
ef090 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 */. else if (
ef0a0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 pFile->local.nRe
ef0b0 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 aders){. pF
ef0c0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
ef0d0 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66 ers --;. if
ef0e0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e (pFile->local.n
ef0f0 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 20 Readers == 0).
ef100 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 46 {. pF
ef110 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 ile->shared->nRe
ef120 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 aders --;.
ef130 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 }. bReturn
ef140 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
ef150 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e }.. /* Releasin
ef160 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b g a pending lock
ef170 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
ef180 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
ef190 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 = PENDING_BYTE &
ef1a0 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 & nNumberOfBytes
ef1b0 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 ToUnlockLow == 1
ef1c0 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 ){. if (pFile
ef1d0 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 ->local.bPending
ef1e0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
ef1f0 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d local.bPending =
ef200 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 FALSE;. pF
ef210 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 ile->shared->bPe
ef220 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 nding = FALSE;.
ef230 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
ef240 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 RUE;. }. }.
ef250 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 /* Releasing a
ef260 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f reserved lock */
ef270 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 . else if (dwFi
ef280 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 leOffsetLow == R
ef290 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 ESERVED_BYTE &&
ef2a0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
ef2b0 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b UnlockLow == 1){
ef2c0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
ef2d0 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 local.bReserved)
ef2e0 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e {. pFile->
ef2f0 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 local.bReserved
ef300 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 = FALSE;. p
ef310 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 File->shared->bR
ef320 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b eserved = FALSE;
ef330 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d . bReturn =
ef340 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d TRUE;. }. }
ef350 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 .. winceMutexRe
ef360 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 lease(pFile->hMu
ef370 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 tex);. return b
ef380 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Return;.}../*.**
ef390 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 An implementati
ef3a0 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 on of the LockFi
ef3b0 6c 65 45 78 28 29 20 41 50 49 20 6f 66 20 77 69 leEx() API of wi
ef3c0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a ndows for wince.
ef3d0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 */.static BOOL w
ef3e0 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a inceLockFileEx(.
ef3f0 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 HANDLE *phFile
ef400 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 ,. DWORD dwFlag
ef410 73 2c 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73 s,. DWORD dwRes
ef420 65 72 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e erved,. DWORD n
ef430 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
ef440 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 ockLow,. DWORD
ef450 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
ef460 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56 LockHigh,. LPOV
ef470 45 52 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c ERLAPPED lpOverl
ef480 61 70 70 65 64 0a 29 7b 0a 20 20 2f 2a 20 49 66 apped.){. /* If
ef490 20 74 68 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 the caller want
ef4a0 73 20 61 20 73 68 61 72 65 64 20 72 65 61 64 20 s a shared read
ef4b0 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 lock, forward th
ef4c0 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 is call. ** to
ef4d0 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f winceLockFile */
ef4e0 0a 20 20 69 66 20 28 6c 70 4f 76 65 72 6c 61 70 . if (lpOverlap
ef4f0 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 53 ped->Offset == S
ef500 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 HARED_FIRST &&.
ef510 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d 20 dwFlags ==
ef520 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 62 1 &&. nNumb
ef530 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
ef540 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 49 5a ow == SHARED_SIZ
ef550 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 77 E){. return w
ef560 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70 68 46 inceLockFile(phF
ef570 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ile, SHARED_FIRS
ef580 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d T, 0, 1, 0);. }
ef590 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b . return FALSE;
ef5a0 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f 66 20 .}./*.** End of
ef5b0 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 64 65 the special code
ef5c0 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a 2a 2a for wince.*****
ef5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef610 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 ********/.#endif
ef620 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 /* SQLITE_OS_WI
ef630 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a NCE */../*******
ef640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef680 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ******.** The ne
ef690 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 xt group of rout
ef6a0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 ines implement t
ef6b0 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 he I/O methods s
ef6c0 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 pecified.** by t
ef6d0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
ef6e0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
ef6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ef730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
ef740 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
ef750 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 72 e..**.** It is r
ef760 65 70 6f 72 74 65 64 20 74 68 61 74 20 61 6e 20 eported that an
ef770 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f 73 65 attempt to close
ef780 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68 74 20 a handle might
ef790 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 61 69 sometimes.** fai
ef7a0 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20 76 65 l. This is a ve
ef7b0 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 ry unreasonable
ef7c0 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69 6e 64 result, but wind
ef7d0 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f 75 73 ows is notorious
ef7e0 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20 75 6e .** for being un
ef7f0 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20 49 20 reasonable so I
ef800 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74 68 61 do not doubt tha
ef810 74 20 69 74 20 6d 69 67 68 74 20 68 61 70 70 65 t it might happe
ef820 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 63 6c n. If.** the cl
ef830 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20 70 61 ose fails, we pa
ef840 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69 6c 6c use for 100 mill
ef850 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 iseconds and try
ef860 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a 20 6d again. As.** m
ef870 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53 45 5f any as MX_CLOSE_
ef880 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70 74 73 ATTEMPT attempts
ef890 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 68 61 to close the ha
ef8a0 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20 62 65 ndle are made be
ef8b0 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67 20 75 fore.** giving u
ef8c0 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 p and returning
ef8d0 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 an error..*/.#de
ef8e0 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 fine MX_CLOSE_AT
ef8f0 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63 20 69 TEMPT 3.static i
ef900 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71 6c 69 nt winClose(sqli
ef910 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
ef920 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d 20 30 int rc, cnt = 0
ef930 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 ;. winFile *pFi
ef940 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 le = (winFile*)i
ef950 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 d;.. assert( id
ef960 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 !=0 );. OSTRACE
ef970 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 2("CLOSE %d\n",
ef980 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b pFile->h);. do{
ef990 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 65 48 . rc = CloseH
ef9a0 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b andle(pFile->h);
ef9b0 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30 . }while( rc==0
ef9c0 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43 && ++cnt < MX_C
ef9d0 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26 26 20 LOSE_ATTEMPT &&
ef9e0 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 (Sleep(100), 1)
ef9f0 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 );.#if SQLITE_OS
efa00 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e 65 20 57 _WINCE.#define W
efa10 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 INCE_DELETION_AT
efa20 54 45 4d 50 54 53 20 33 0a 20 20 77 69 6e 63 65 TEMPTS 3. wince
efa30 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c DestroyLock(pFil
efa40 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d e);. if( pFile-
efa50 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 >zDeleteOnClose
efa60 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d ){. int cnt =
efa70 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 0a 20 0;. while(.
efa80 20 20 20 20 20 20 20 20 20 20 44 65 6c 65 74 65 Delete
efa90 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a 44 65 FileW(pFile->zDe
efaa0 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a leteOnClose)==0.
efab0 20 20 20 20 20 20 20 20 26 26 20 47 65 74 46 69 && GetFi
efac0 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 70 46 leAttributesW(pF
efad0 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c ile->zDeleteOnCl
efae0 6f 73 65 29 21 3d 30 78 66 66 66 66 66 66 66 66 ose)!=0xffffffff
efaf0 20 0a 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 . && cnt
efb00 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54 ++ < WINCE_DELET
efb10 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20 20 20 ION_ATTEMPTS.
efb20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c 65 65 70 ){. Sleep
efb30 28 31 30 30 29 3b 20 20 2f 2a 20 57 61 69 74 20 (100); /* Wait
efb40 61 20 6c 69 74 74 6c 65 20 62 65 66 6f 72 65 20 a little before
efb50 74 72 79 69 6e 67 20 61 67 61 69 6e 20 2a 2f 0a trying again */.
efb60 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 28 70 }. free(p
efb70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 File->zDeleteOnC
efb80 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 lose);. }.#endi
efb90 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 f. OpenCounter(
efba0 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 -1);. return rc
efbb0 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
efbc0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a QLITE_IOERR;.}..
efbd0 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f /*.** Some micro
efbe0 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c soft compilers l
efbf0 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 ack this definit
efc00 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ion..*/.#ifndef
efc10 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 INVALID_SET_FILE
efc20 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66 69 6e _POINTER.# defin
efc30 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 e INVALID_SET_FI
efc40 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44 57 4f LE_POINTER ((DWO
efc50 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a 0a 2f RD)-1).#endif../
efc60 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 *.** Read data f
efc70 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 rom a file into
efc80 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 72 a buffer. Retur
efc90 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 n SQLITE_OK if a
efca0 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65 ll.** bytes were
efcb0 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c read successful
efcc0 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f ly and SQLITE_IO
efcd0 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ERR if anything
efce0 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a goes.** wrong..*
efcf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
efd00 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f Read(. sqlite3_
efd10 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 file *id,
efd20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 /* File to re
efd30 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 ad from */. voi
efd40 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 d *pBuf,
efd50 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
efd60 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 content into th
efd70 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 is buffer */. i
efd80 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
efd90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
efda0 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
efdb0 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 read */. sqlite
efdc0 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 3_int64 offset
efdd0 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 /* Begin re
efde0 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 ading at this of
efdf0 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e fset */.){. LON
efe00 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c G upperBits = (L
efe10 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 ONG)((offset>>32
efe20 29 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b ) & 0x7fffffff);
efe30 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 . LONG lowerBit
efe40 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 s = (LONG)(offse
efe50 74 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b t & 0xffffffff);
efe60 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 . DWORD rc;. w
efe70 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 inFile *pFile =
efe80 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (winFile*)id;.
efe90 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44 DWORD error;. D
efea0 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20 61 73 73 WORD got;.. ass
efeb0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
efec0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
efed0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
efee0 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f 53 54 ERR_READ);. OST
efef0 52 41 43 45 33 28 22 52 45 41 44 20 25 64 20 6c RACE3("READ %d l
eff00 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
eff10 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
eff20 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 type);. rc = Se
eff30 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 tFilePointer(pFi
eff40 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 le->h, lowerBits
eff50 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 , &upperBits, FI
eff60 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 LE_BEGIN);. if(
eff70 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 rc==INVALID_SET
eff80 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 _FILE_POINTER &&
eff90 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 (error=GetLastE
effa0 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f rror())!=NO_ERRO
effb0 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e R ){. pFile->
effc0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f lastErrno = erro
effd0 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 r;. return SQ
effe0 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 LITE_FULL;. }.
efff0 20 69 66 28 20 21 52 65 61 64 46 69 6c 65 28 70 if( !ReadFile(p
f0000 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 File->h, pBuf, a
f0010 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20 29 7b 0a mt, &got, 0) ){.
f0020 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
f0030 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 rrno = GetLastEr
f0040 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 ror();. retur
f0050 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 n SQLITE_IOERR_R
f0060 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 EAD;. }. if( g
f0070 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74 20 29 ot==(DWORD)amt )
f0080 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
f0090 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
f00a0 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 . /* Unread p
f00b0 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 66 arts of the buff
f00c0 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d er must be zero-
f00d0 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 filled */. me
f00e0 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 mset(&((char*)pB
f00f0 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 uf)[got], 0, amt
f0100 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 -got);. retur
f0110 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 n SQLITE_IOERR_S
f0120 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d HORT_READ;. }.}
f0130 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
f0140 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 ta from a buffer
f0150 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 into a file. R
f0160 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
f0170 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 on success.** or
f0180 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f some other erro
f0190 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 r code on failur
f01a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
f01b0 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 71 6c winWrite(. sql
f01c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 ite3_file *id,
f01d0 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 /* File t
f01e0 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a o write into */.
f01f0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 const void *pB
f0200 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 uf, /* T
f0210 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20 77 he bytes to be w
f0220 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 ritten */. int
f0230 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 amt,
f0240 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
f0250 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 of bytes to writ
f0260 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 e */. sqlite3_i
f0270 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 nt64 offset
f0280 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
f0290 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 the file to begi
f02a0 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a n writing at */.
f02b0 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 ){. LONG upperB
f02c0 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 its = (LONG)((of
f02d0 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 fset>>32) & 0x7f
f02e0 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 ffffff);. LONG
f02f0 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e lowerBits = (LON
f0300 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 G)(offset & 0xff
f0310 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 ffffff);. DWORD
f0320 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a rc;. winFile *
f0330 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
f0340 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 *)id;. DWORD er
f0350 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 77 72 6f ror;. DWORD wro
f0360 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 te = 0;.. asser
f0370 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 t( id!=0 );. Si
f0380 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 mulateIOError(re
f0390 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
f03a0 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 6d 75 R_WRITE);. Simu
f03b0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
f03c0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
f03d0 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 43 45 FULL);. OSTRACE
f03e0 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3("WRITE %d lock
f03f0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
f0400 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
f0410 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 e);. rc = SetFi
f0420 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d lePointer(pFile-
f0430 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 >h, lowerBits, &
f0440 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f upperBits, FILE_
f0450 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 BEGIN);. if( rc
f0460 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 ==INVALID_SET_FI
f0470 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 LE_POINTER && (e
f0480 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f rror=GetLastErro
f0490 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 r())!=NO_ERROR )
f04a0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
f04b0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a tErrno = error;.
f04c0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
f04d0 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 61 73 E_FULL;. }. as
f04e0 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20 sert( amt>0 );.
f04f0 20 77 68 69 6c 65 28 0a 20 20 20 20 20 61 6d 74 while(. amt
f0500 3e 30 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d >0. && (rc =
f0510 20 57 72 69 74 65 46 69 6c 65 28 70 46 69 6c 65 WriteFile(pFile
f0520 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 ->h, pBuf, amt,
f0530 26 77 72 6f 74 65 2c 20 30 29 29 21 3d 30 0a 20 &wrote, 0))!=0.
f0540 20 20 20 20 26 26 20 77 72 6f 74 65 3e 30 0a 20 && wrote>0.
f0550 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 ){. amt -= w
f0560 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d rote;. pBuf =
f0570 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b &((char*)pBuf)[
f0580 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 69 66 wrote];. }. if
f0590 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e 28 69 6e ( !rc || amt>(in
f05a0 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 20 20 70 t)wrote ){. p
f05b0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
f05c0 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
f05d0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
f05e0 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 ITE_FULL;. }.
f05f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f0600 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 ;.}../*.** Trunc
f0610 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 ate an open file
f0620 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 to a specified
f0630 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 size.*/.static i
f0640 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65 28 73 nt winTruncate(s
f0650 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
f0660 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e sqlite3_int64 n
f0670 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 Byte){. LONG up
f0680 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 perBits = (LONG)
f0690 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 20 30 ((nByte>>32) & 0
f06a0 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f x7fffffff);. LO
f06b0 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 NG lowerBits = (
f06c0 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20 30 78 LONG)(nByte & 0x
f06d0 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f ffffffff);. DWO
f06e0 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 RD rc;. winFile
f06f0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
f0700 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 le*)id;. DWORD
f0710 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 error;.. assert
f0720 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 ( id!=0 );. OST
f0730 52 41 43 45 33 28 22 54 52 55 4e 43 41 54 45 20 RACE3("TRUNCATE
f0740 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c %d %lld\n", pFil
f0750 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a 20 20 e->h, nByte);.
f0760 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
f0770 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
f0780 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b 0a 20 ERR_TRUNCATE);.
f0790 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 rc = SetFilePoi
f07a0 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c nter(pFile->h, l
f07b0 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 owerBits, &upper
f07c0 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e Bits, FILE_BEGIN
f07d0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 );. if( rc==INV
f07e0 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f ALID_SET_FILE_PO
f07f0 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d INTER && (error=
f0800 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 GetLastError())!
f0810 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 =NO_ERROR ){.
f0820 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
f0830 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 o = error;. r
f0840 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
f0850 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d RR_TRUNCATE;. }
f0860 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f 66 46 69 . /* SetEndOfFi
f0870 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 le will fail if
f0880 6e 42 79 74 65 20 69 73 20 6e 65 67 61 74 69 76 nByte is negativ
f0890 65 20 2a 2f 0a 20 20 69 66 28 20 21 53 65 74 45 e */. if( !SetE
f08a0 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e ndOfFile(pFile->
f08b0 68 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d h) ){. pFile-
f08c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 >lastErrno = Get
f08d0 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
f08e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
f08f0 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 OERR_TRUNCATE;.
f0900 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
f0910 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 TE_OK;.}..#ifdef
f0920 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a SQLITE_TEST./*.
f0930 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d ** Count the num
f0940 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 ber of fullsyncs
f0950 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 and normal sync
f0960 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 s. This is used
f0970 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 to test.** that
f0980 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 syncs and fulls
f0990 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 69 6e yncs are occurin
f09a0 67 20 61 74 20 74 68 65 20 72 69 67 68 74 20 74 g at the right t
f09b0 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f imes..*/.SQLITE_
f09c0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f09d0 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a sync_count = 0;.
f09e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f09f0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f qlite3_fullsync_
f0a00 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 count = 0;.#endi
f0a10 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 f../*.** Make su
f0a20 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f re all writes to
f0a30 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 a particular fi
f0a40 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 le are committed
f0a50 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 to disk..*/.sta
f0a60 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e 63 28 tic int winSync(
f0a70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
f0a80 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 23 69 , int flags){.#i
f0a90 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f fndef SQLITE_NO_
f0aa0 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65 20 2a SYNC. winFile *
f0ab0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
f0ac0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 *)id;.. assert(
f0ad0 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 id!=0 );. OSTR
f0ae0 41 43 45 33 28 22 53 59 4e 43 20 25 64 20 6c 6f ACE3("SYNC %d lo
f0af0 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
f0b00 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
f0b10 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20 20 55 4e ype);.#else. UN
f0b20 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
f0b30 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 d);.#endif.#ifnd
f0b40 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
f0b50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f0b60 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c 73 65 0a R(flags);.#else.
f0b70 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 if( flags & SQ
f0b80 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 29 LITE_SYNC_FULL )
f0b90 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 75 {. sqlite3_fu
f0ba0 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a llsync_count++;.
f0bb0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 }. sqlite3_sy
f0bc0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 nc_count++;.#end
f0bd0 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f if. /* If we co
f0be0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 mpiled with the
f0bf0 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 SQLITE_NO_SYNC f
f0c00 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e lag, then syncin
f0c10 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f g is a. ** no-o
f0c20 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 p. */.#ifdef SQ
f0c30 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 20 LITE_NO_SYNC.
f0c40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f0c50 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 46 K;.#else. if( F
f0c60 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 73 28 lushFileBuffers(
f0c70 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 pFile->h) ){.
f0c80 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f0c90 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
f0ca0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
f0cb0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
f0cc0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
f0cd0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a LITE_IOERR;. }.
f0ce0 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
f0cf0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 Determine the cu
f0d00 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 rrent size of a
f0d10 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f file in bytes.*/
f0d20 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 .static int winF
f0d30 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f ileSize(sqlite3_
f0d40 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 file *id, sqlite
f0d50 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 3_int64 *pSize){
f0d60 0a 20 20 44 57 4f 52 44 20 75 70 70 65 72 42 69 . DWORD upperBi
f0d70 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c 6f 77 65 ts;. DWORD lowe
f0d80 72 42 69 74 73 3b 0a 20 20 77 69 6e 46 69 6c 65 rBits;. winFile
f0d90 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
f0da0 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 le*)id;. DWORD
f0db0 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 error;.. assert
f0dc0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d ( id!=0 );. Sim
f0dd0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 ulateIOError(ret
f0de0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
f0df0 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f 77 65 72 _FSTAT);. lower
f0e00 42 69 74 73 20 3d 20 47 65 74 46 69 6c 65 53 69 Bits = GetFileSi
f0e10 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 26 75 70 ze(pFile->h, &up
f0e20 70 65 72 42 69 74 73 29 3b 0a 20 20 69 66 28 20 perBits);. if(
f0e30 20 20 28 6c 6f 77 65 72 42 69 74 73 20 3d 3d 20 (lowerBits ==
f0e40 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 53 49 5a INVALID_FILE_SIZ
f0e50 45 29 0a 20 20 20 20 20 26 26 20 28 28 65 72 72 E). && ((err
f0e60 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f or = GetLastErro
f0e70 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 r()) != NO_ERROR
f0e80 29 20 29 0a 20 20 7b 0a 20 20 20 20 70 46 69 6c ) ). {. pFil
f0e90 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
f0ea0 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e rror;. return
f0eb0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 SQLITE_IOERR_FS
f0ec0 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a TAT;. }. *pSiz
f0ed0 65 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 e = (((sqlite3_i
f0ee0 6e 74 36 34 29 75 70 70 65 72 42 69 74 73 29 3c nt64)upperBits)<
f0ef0 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42 69 74 73 <32) + lowerBits
f0f00 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
f0f10 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c E_OK;.}../*.** L
f0f20 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d OCKFILE_FAIL_IMM
f0f30 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e 64 65 EDIATELY is unde
f0f40 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 57 69 fined on some Wi
f0f50 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a ndows systems..*
f0f60 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b 46 49 /.#ifndef LOCKFI
f0f70 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 LE_FAIL_IMMEDIAT
f0f80 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c 4f 43 ELY.# define LOC
f0f90 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 KFILE_FAIL_IMMED
f0fa0 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69 66 0a IATELY 1.#endif.
f0fb0 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 ./*.** Acquire a
f0fc0 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2a reader lock..**
f0fd0 20 44 69 66 66 65 72 65 6e 74 20 41 50 49 20 72 Different API r
f0fe0 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c outines are call
f0ff0 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 ed depending on
f1000 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
f1010 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e 39 35 20 his.** is Win95
f1020 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 or WinNT..*/.sta
f1030 74 69 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c tic int getReadL
f1040 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 ock(winFile *pFi
f1050 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a le){. int res;.
f1060 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
f1070 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44 20 6f OVERLAPPED o
f1080 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 vlp;. ovlp.Of
f1090 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 fset = SHARED_FI
f10a0 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 RST;. ovlp.Of
f10b0 66 73 65 74 48 69 67 68 20 3d 20 30 3b 0a 20 20 fsetHigh = 0;.
f10c0 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74 20 3d 20 ovlp.hEvent =
f10d0 30 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 0;. res = Loc
f10e0 6b 46 69 6c 65 45 78 28 70 46 69 6c 65 2d 3e 68 kFileEx(pFile->h
f10f0 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f , LOCKFILE_FAIL_
f1100 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a 20 20 20 IMMEDIATELY,.
f1110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1120 20 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 0, SHARED_SIZE
f1130 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a 2f 2a 20 , 0, &ovlp);./*
f1140 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
f1150 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
f1160 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
f1170 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
f1180 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 d. .*/.#if SQLIT
f1190 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
f11a0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6c }else{. int l
f11b0 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 k;. sqlite3_r
f11c0 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 andomness(sizeof
f11d0 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20 20 20 (lk), &lk);.
f11e0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 pFile->sharedLoc
f11f0 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 74 29 28 kByte = (short)(
f1200 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 (lk & 0x7fffffff
f1210 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d )%(SHARED_SIZE -
f1220 20 31 29 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 1));. res =
f1230 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e LockFile(pFile->
f1240 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b h, SHARED_FIRST+
f1250 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 pFile->sharedLoc
f1260 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b kByte, 0, 1, 0);
f1270 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 .#endif. }. if
f1280 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 ( res == 0 ){.
f1290 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
f12a0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f no = GetLastErro
f12b0 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 r();. }. retur
f12c0 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n res;.}../*.**
f12d0 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a Undo a readlock.
f12e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
f12f0 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e lockReadLock(win
f1300 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 File *pFile){.
f1310 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 int res;. if( i
f1320 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 sNT() ){. res
f1330 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 = UnlockFile(pF
f1340 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 ile->h, SHARED_F
f1350 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f IRST, 0, SHARED_
f1360 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e SIZE, 0);./* isN
f1370 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 T() is 1 if SQLI
f1380 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 TE_OS_WINCE==1,
f1390 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 so this else is
f13a0 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 never executed.
f13b0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
f13c0 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c S_WINCE==0. }el
f13d0 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e se{. res = Un
f13e0 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
f13f0 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 h, SHARED_FIRST
f1400 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c + pFile->sharedL
f1410 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 ockByte, 0, 1, 0
f1420 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
f1430 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a if( res == 0 ){.
f1440 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
f1450 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 rrno = GetLastEr
f1460 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ror();. }. ret
f1470 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a urn res;.}../*.*
f1480 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 * Lock the file
f1490 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 with the lock sp
f14a0 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ecified by param
f14b0 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 eter locktype -
f14c0 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f one.** of the fo
f14d0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
f14e0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f (1) SHARED_LO
f14f0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 CK.** (2) RE
f1500 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SERVED_LOCK.**
f1510 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c (3) PENDING_L
f1520 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 OCK.** (4) E
f1530 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a XCLUSIVE_LOCK.**
f1540 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 .** Sometimes wh
f1550 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e en requesting on
f1560 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 e lock state, ad
f1570 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 ditional lock st
f1580 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 ates.** are inse
f1590 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e rted in between.
f15a0 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 The locking mi
f15b0 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 ght fail on one
f15c0 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 of the later.**
f15d0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 transitions leav
f15e0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 ing the lock sta
f15f0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f te different fro
f1600 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 m what it starte
f1610 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 d but.** still s
f1620 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c hort of its goal
f1630 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
f1640 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 chart shows the
f1650 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e allowed.** tran
f1660 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 sitions and the
f1670 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 inserted interme
f1680 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a diate states:.**
f1690 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 .** UNLOCKED
f16a0 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 -> SHARED.**
f16b0 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 SHARED -> RESERV
f16c0 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
f16d0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
f16e0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
f16f0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e RESERVED -> (PEN
f1700 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
f1710 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 VE.** PENDING
f1720 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
f1730 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
f1740 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 will only incre
f1750 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 ase a lock. The
f1760 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 winUnlock() rou
f1770 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 61 tine.** erases a
f1780 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65 ll locks at once
f1790 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20 and returns us
f17a0 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c immediately to l
f17b0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a ocking level 0..
f17c0 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 ** It is not pos
f17d0 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74 sible to lower t
f17e0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
f17f0 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 74 one step at a t
f1800 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 ime. You.** mus
f1810 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f t go straight to
f1820 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 locking level 0
f1830 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f1840 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f winLock(sqlite3_
f1850 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
f1860 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 cktype){. int r
f1870 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
f1880 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
f1890 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 from subroutine
f18a0 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d s */. int res =
f18b0 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1; /*
f18c0 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 69 6e Result of a win
f18d0 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a dows lock call *
f18e0 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 /. int newLockt
f18f0 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 ype; /* Se
f1900 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 t pFile->locktyp
f1910 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 e to this value
f1920 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a before exiting *
f1930 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 /. int gotPendi
f1940 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 ngLock = 0;/* Tr
f1950 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65 ue if we acquire
f1960 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b d a PENDING lock
f1970 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 this time */.
f1980 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
f1990 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 (winFile*)id;.
f19a0 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d 20 4e DWORD error = N
f19b0 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 73 73 65 O_ERROR;.. asse
f19c0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f rt( id!=0 );. O
f19d0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 25 64 STRACE5("LOCK %d
f19e0 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e %d was %d(%d)\n
f19f0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ",. pFi
f1a00 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
f1a10 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
f1a20 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c , pFile->sharedL
f1a30 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 ockByte);.. /*
f1a40 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 If there is alre
f1a50 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 ady a lock of th
f1a60 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 is type or more
f1a70 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 restrictive on t
f1a80 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 he. ** OsFile,
f1a90 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 do nothing. Don'
f1aa0 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f t use the end_lo
f1ab0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 ck: exit path, a
f1ac0 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73 s. ** sqlite3Os
f1ad0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 EnterMutex() has
f1ae0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 n't been called
f1af0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 yet.. */. if(
f1b00 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
f1b10 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
f1b20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f1b30 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b K;. }.. /* Mak
f1b40 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 e sure the locki
f1b50 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 ng sequence is c
f1b60 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 orrect. */. as
f1b70 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
f1b80 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c ktype!=NO_LOCK |
f1b90 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 | locktype==SHAR
f1ba0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
f1bb0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 ert( locktype!=P
f1bc0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 ENDING_LOCK );.
f1bd0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
f1be0 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e!=RESERVED_LOCK
f1bf0 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 || pFile->lockt
f1c00 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
f1c10 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 );.. /* Lock t
f1c20 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 he PENDING_LOCK
f1c30 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 byte if we need
f1c40 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e to acquire a PEN
f1c50 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a DING lock or. *
f1c60 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e * a SHARED lock.
f1c70 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75 If we are acqu
f1c80 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c iring a SHARED l
f1c90 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 ock, the acquisi
f1ca0 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 tion of. ** the
f1cb0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 PENDING_LOCK by
f1cc0 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e te is temporary.
f1cd0 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 . */. newLockt
f1ce0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 ype = pFile->loc
f1cf0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 20 20 28 ktype;. if( (
f1d00 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
f1d10 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 7c =NO_LOCK). |
f1d20 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79 70 65 3d | ( (locktype=
f1d30 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 =EXCLUSIVE_LOCK)
f1d40 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70 46 . && (pF
f1d50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 ile->locktype==R
f1d60 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 29 0a 20 ESERVED_LOCK)).
f1d70 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 ){. int cnt
f1d80 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 = 3;. while(
f1d90 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65 73 20 cnt-->0 && (res
f1da0 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 = LockFile(pFile
f1db0 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 ->h, PENDING_BYT
f1dc0 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d 30 20 E, 0, 1, 0))==0
f1dd0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 79 20 ){. /* Try
f1de0 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74 20 74 3 times to get t
f1df0 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e he pending lock.
f1e00 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f The pending lo
f1e10 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 20 20 20 ck might be.
f1e20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 61 6e 6f ** held by ano
f1e30 74 68 65 72 20 72 65 61 64 65 72 20 70 72 6f 63 ther reader proc
f1e40 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 72 65 6c ess who will rel
f1e50 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e 74 61 72 ease it momentar
f1e60 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ily.. */.
f1e70 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 63 6f OSTRACE2("co
f1e80 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 50 45 uld not get a PE
f1e90 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d NDING lock. cnt=
f1ea0 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a 20 20 20 %d\n", cnt);.
f1eb0 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 Sleep(1);.
f1ec0 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e }. gotPendin
f1ed0 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20 20 20 gLock = res;.
f1ee0 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20 20 20 if( !res ){.
f1ef0 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 error = GetLa
f1f00 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d stError();. }
f1f10 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
f1f20 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b re a shared lock
f1f30 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
f1f40 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
f1f50 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 K && res ){.
f1f60 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
f1f70 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
f1f80 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 );. res = ge
f1f90 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
f1fa0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
f1fb0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
f1fc0 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b pe = SHARED_LOCK
f1fd0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
f1fe0 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 error = GetLa
f1ff0 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d stError();. }
f2000 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
f2010 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f re a RESERVED lo
f2020 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f ck. */. if( lo
f2030 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 cktype==RESERVED
f2040 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
f2050 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
f2060 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 e->locktype==SHA
f2070 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
f2080 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 res = LockFile(p
f2090 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 File->h, RESERVE
f20a0 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 D_BYTE, 0, 1, 0)
f20b0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
f20c0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
f20d0 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f pe = RESERVED_LO
f20e0 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 CK;. }else{.
f20f0 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 error = Get
f2100 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
f2110 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 }. }.. /* Acq
f2120 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c uire a PENDING l
f2130 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ock. */. if( l
f2140 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
f2150 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 VE_LOCK && res )
f2160 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 {. newLocktyp
f2170 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b e = PENDING_LOCK
f2180 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 ;. gotPending
f2190 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 Lock = 0;. }..
f21a0 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 /* Acquire an E
f21b0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 XCLUSIVE lock.
f21c0 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
f21d0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
f21e0 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 K && res ){.
f21f0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
f2200 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f ocktype>=SHARED_
f2210 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 LOCK );. res
f2220 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b = unlockReadLock
f2230 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 (pFile);. OST
f2240 52 41 43 45 32 28 22 75 6e 72 65 61 64 6c 6f 63 RACE2("unreadloc
f2250 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 29 3b k = %d\n", res);
f2260 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 . res = LockF
f2270 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 ile(pFile->h, SH
f2280 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 ARED_FIRST, 0, S
f2290 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a HARED_SIZE, 0);.
f22a0 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 if( res ){.
f22b0 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 newLocktype
f22c0 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 = EXCLUSIVE_LOC
f22d0 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 K;. }else{.
f22e0 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 4c error = GetL
f22f0 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 astError();.
f2300 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 72 6f OSTRACE2("erro
f2310 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 r-code = %d\n",
f2320 65 72 72 6f 72 29 3b 0a 20 20 20 20 20 20 67 65 error);. ge
f2330 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
f2340 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
f2350 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 * If we are hold
f2360 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f ing a PENDING lo
f2370 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f ck that ought to
f2380 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 be released, th
f2390 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 en. ** release
f23a0 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 it now.. */. i
f23b0 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 f( gotPendingLoc
f23c0 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 k && locktype==S
f23d0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
f23e0 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 UnlockFile(pFi
f23f0 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 le->h, PENDING_B
f2400 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 YTE, 0, 1, 0);.
f2410 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 }.. /* Update
f2420 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 the state of the
f2430 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69 lock has held i
f2440 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 n the file descr
f2450 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 iptor then. **
f2460 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f return the appro
f2470 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f priate result co
f2480 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 de.. */. if( r
f2490 65 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 es ){. rc = S
f24a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
f24b0 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 e{. OSTRACE4(
f24c0 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 "LOCK FAILED %d
f24d0 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 trying for %d bu
f24e0 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 t got %d\n", pFi
f24f0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
f2500 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c locktype, newL
f2510 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46 ocktype);. pF
f2520 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
f2530 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 63 20 3d error;. rc =
f2540 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
f2550 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 }. pFile->lockt
f2560 79 70 65 20 3d 20 28 75 38 29 6e 65 77 4c 6f 63 ype = (u8)newLoc
f2570 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 ktype;. return
f2580 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
f2590 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
f25a0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
f25b0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
f25c0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
f25d0 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
f25e0 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
f25f0 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
f2600 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
f2610 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a return.** non-z
f2620 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a ero, otherwise z
f2630 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ero..*/.static i
f2640 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73 65 72 nt winCheckReser
f2650 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
f2660 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
f2670 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 ResOut){. int r
f2680 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 c;. winFile *pF
f2690 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 ile = (winFile*)
f26a0 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 id;.. assert( i
f26b0 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 d!=0 );. if( pF
f26c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 ile->locktype>=R
f26d0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a ESERVED_LOCK ){.
f26e0 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 rc = 1;.
f26f0 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 OSTRACE3("TEST W
f2700 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f R-LOCK %d %d (lo
f2710 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e cal)\n", pFile->
f2720 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b h, rc);. }else{
f2730 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b 46 69 . rc = LockFi
f2740 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 le(pFile->h, RES
f2750 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 ERVED_BYTE, 0, 1
f2760 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
f2770 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b ){. Unlock
f2780 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 File(pFile->h, R
f2790 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c ESERVED_BYTE, 0,
f27a0 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 1, 0);. }.
f27b0 20 20 72 63 20 3d 20 21 72 63 3b 0a 20 20 20 20 rc = !rc;.
f27c0 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 OSTRACE3("TEST W
f27d0 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 R-LOCK %d %d (re
f27e0 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d mote)\n", pFile-
f27f0 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 2a >h, rc);. }. *
f2800 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 pResOut = rc;.
f2810 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f2820 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 ;.}../*.** Lower
f2830 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
f2840 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 el on file descr
f2850 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b iptor id to lock
f2860 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
f2870 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
f2880 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
f2890 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
f28a0 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
f28b0 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
f28c0 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
f28d0 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
f28e0 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
f28f0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
f2900 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
f2910 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
f2920 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 * It is not poss
f2930 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f ible for this ro
f2940 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 utine to fail if
f2950 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
f2960 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f ment.** is NO_LO
f2970 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f CK. If the seco
f2980 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 nd argument is S
f2990 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 HARED_LOCK then
f29a0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
f29b0 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c might return SQL
f29c0 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 ITE_IOERR;.*/.st
f29d0 61 74 69 63 20 69 6e 74 20 77 69 6e 55 6e 6c 6f atic int winUnlo
f29e0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
f29f0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
f2a00 65 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a e){. int type;.
f2a10 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
f2a20 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
f2a30 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
f2a40 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 TE_OK;. assert(
f2a50 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 pFile!=0 );. a
f2a60 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
f2a70 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
f2a80 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f OSTRACE5("UNLO
f2a90 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20 CK %d to %d was
f2aa0 25 64 28 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 %d(%d)\n", pFile
f2ab0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 ->h, locktype,.
f2ac0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
f2ad0 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d locktype, pFile-
f2ae0 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 >sharedLockByte)
f2af0 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 ;. type = pFile
f2b00 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 ->locktype;. if
f2b10 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 ( type>=EXCLUSIV
f2b20 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e E_LOCK ){. Un
f2b30 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
f2b40 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c h, SHARED_FIRST,
f2b50 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 0, SHARED_SIZE,
f2b60 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 0);. if( loc
f2b70 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
f2b80 43 4b 20 26 26 20 21 67 65 74 52 65 61 64 4c 6f CK && !getReadLo
f2b90 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 ck(pFile) ){.
f2ba0 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c /* This shoul
f2bb0 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20 d never happen.
f2bc0 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 We should alway
f2bd0 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 s be able to.
f2be0 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20 ** reacquire
f2bf0 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f the read lock */
f2c00 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
f2c10 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b TE_IOERR_UNLOCK;
f2c20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
f2c30 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f type>=RESERVED_
f2c40 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f LOCK ){. Unlo
f2c50 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
f2c60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
f2c70 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0, 1, 0);. }.
f2c80 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f if( locktype==NO
f2c90 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 _LOCK && type>=S
f2ca0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
f2cb0 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b unlockReadLock
f2cc0 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 (pFile);. }. i
f2cd0 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 f( type>=PENDING
f2ce0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c _LOCK ){. Unl
f2cf0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 ockFile(pFile->h
f2d00 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
f2d10 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0, 1, 0);. }.
f2d20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
f2d30 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a = (u8)locktype;.
f2d40 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
f2d50 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e /*.** Control an
f2d60 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 6f d query of the o
f2d70 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e pen file handle.
f2d80 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
f2d90 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 inFileControl(sq
f2da0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f2db0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 int op, void *pA
f2dc0 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f rg){. switch( o
f2dd0 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 p ){. case SQ
f2de0 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
f2df0 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 TATE: {. *(
f2e00 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 77 69 int*)pArg = ((wi
f2e10 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b nFile*)id)->lock
f2e20 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75 type;. retu
f2e30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
f2e40 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
f2e50 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 ITE_LAST_ERRNO:
f2e60 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 {. *(int*)p
f2e70 41 72 67 20 3d 20 28 69 6e 74 29 28 28 77 69 6e Arg = (int)((win
f2e80 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 File*)id)->lastE
f2e90 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 rrno;. retu
f2ea0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
f2eb0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
f2ec0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d SQLITE_ERROR;.}
f2ed0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
f2ee0 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 he sector size i
f2ef0 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 n bytes of the u
f2f00 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 nderlying block
f2f10 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 device for.** th
f2f20 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 e specified file
f2f30 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 . This is almost
f2f40 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
f2f50 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a s, but may be.**
f2f60 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 larger for some
f2f70 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 devices..**.**
f2f80 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 SQLite code assu
f2f90 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f mes this functio
f2fa0 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 n cannot fail. I
f2fb0 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 t also assumes t
f2fc0 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 hat.** if two fi
f2fd0 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 les are created
f2fe0 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 in the same file
f2ff0 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 -system director
f3000 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 y (i.e..** a dat
f3010 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f abase and its jo
f3020 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 urnal file) that
f3030 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
f3040 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 will be the.**
f3050 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a same for both..*
f3060 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
f3070 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 SectorSize(sqlit
f3080 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 e3_file *id){.
f3090 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
f30a0 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 . return (int)(
f30b0 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e ((winFile*)id)->
f30c0 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 7d 0a 0a sectorSize);.}..
f30d0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 76 /*.** Return a v
f30e0 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65 20 ector of device
f30f0 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 2e characteristics.
f3100 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
f3110 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 inDeviceCharacte
f3120 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f ristics(sqlite3_
f3130 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55 4e 55 file *id){. UNU
f3140 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 64 SED_PARAMETER(id
f3150 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
f3160 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 ../*.** This vec
f3170 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 tor defines all
f3180 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 the methods that
f3190 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 can operate on
f31a0 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 an.** sqlite3_fi
f31b0 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e 0a 2a 2f le for win32..*/
f31c0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 .static const sq
f31d0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
f31e0 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 3d 20 7b winIoMethod = {
f31f0 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 . 1,
f3200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f3210 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 77 69 iVersion */. wi
f3220 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e 52 65 61 nClose,. winRea
f3230 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 2c 0a 20 d,. winWrite,.
f3240 20 77 69 6e 54 72 75 6e 63 61 74 65 2c 0a 20 20 winTruncate,.
f3250 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 6e 46 69 winSync,. winFi
f3260 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e 4c 6f 63 leSize,. winLoc
f3270 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 6b 2c 0a k,. winUnlock,.
f3280 20 20 77 69 6e 43 68 65 63 6b 52 65 73 65 72 76 winCheckReserv
f3290 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e 46 69 6c edLock,. winFil
f32a0 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 69 6e 53 eControl,. winS
f32b0 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 77 69 6e ectorSize,. win
f32c0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
f32d0 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a stics.};../*****
f32e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f32f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3320 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 ******.** Here e
f3330 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 nds the I/O meth
f3340 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68 ods that form th
f3350 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 e sqlite3_io_met
f3360 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a hods object..**.
f3370 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 ** The next bloc
f3380 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d k of code implem
f3390 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d 65 74 ents the VFS met
f33a0 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a hods..**********
f33b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f33f0 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 **/../*.** Conve
f3400 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c 65 6e rt a UTF-8 filen
f3410 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65 76 65 ame into whateve
f3420 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64 65 72 r form the under
f3430 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 lying.** operati
f3440 6e 67 20 73 79 73 74 65 6d 20 77 61 6e 74 73 20 ng system wants
f3450 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e 20 20 53 filenames in. S
f3460 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 pace to hold the
f3470 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 6f 62 result.** is ob
f3480 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
f3490 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 oc and must be f
f34a0 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c reed by the call
f34b0 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e ing.** function.
f34c0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
f34d0 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 *convertUtf8File
f34e0 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 61 72 20 name(const char
f34f0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 76 *zFilename){. v
f3500 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 oid *zConverted
f3510 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 = 0;. if( isNT(
f3520 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 ) ){. zConver
f3530 74 65 64 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 ted = utf8ToUnic
f3540 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ode(zFilename);.
f3550 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
f3560 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f3570 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
f3580 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
f3590 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 uted. .*/.#if SQ
f35a0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
f35b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43 . }else{. zC
f35c0 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 onverted = utf8T
f35d0 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d 65 29 oMbcs(zFilename)
f35e0 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 2f ;.#endif. }. /
f35f0 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 68 61 * caller will ha
f3600 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65 6d 6f ndle out of memo
f3610 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 7a ry */. return z
f3620 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a 0a 2f 2a Converted;.}../*
f3630 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d .** Create a tem
f3640 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
f3650 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 in zBuf. zBuf
f3660 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 must be big enou
f3670 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 gh to.** hold at
f3680 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
f3690 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f e characters..*/
f36a0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 .static int getT
f36b0 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 empname(int nBuf
f36c0 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
f36d0 20 73 74 61 74 69 63 20 63 68 61 72 20 7a 43 68 static char zCh
f36e0 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 ars[] =. "abc
f36f0 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 defghijklmnopqrs
f3700 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 tuvwxyz". "AB
f3710 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 CDEFGHIJKLMNOPQR
f3720 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 STUVWXYZ". "0
f3730 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 73 69 123456789";. si
f3740 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 ze_t i, j;. cha
f3750 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d 41 58 5f r zTempPath[MAX_
f3760 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 28 20 73 PATH+1];. if( s
f3770 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
f3780 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 73 71 6c ctory ){. sql
f3790 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 ite3_snprintf(MA
f37a0 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 X_PATH-30, zTemp
f37b0 50 61 74 68 2c 20 22 25 73 22 2c 20 73 71 6c 69 Path, "%s", sqli
f37c0 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f te3_temp_directo
f37d0 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ry);. }else if(
f37e0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 63 isNT() ){. c
f37f0 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20 20 20 har *zMulti;.
f3800 20 57 43 48 41 52 20 7a 57 69 64 65 50 61 74 68 WCHAR zWidePath
f3810 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 [MAX_PATH];.
f3820 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d 41 58 GetTempPathW(MAX
f3830 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64 65 50 _PATH-30, zWideP
f3840 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c 74 69 ath);. zMulti
f3850 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
f3860 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20 (zWidePath);.
f3870 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b 0a 20 if( zMulti ){.
f3880 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
f3890 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 rintf(MAX_PATH-3
f38a0 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 0, zTempPath, "%
f38b0 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 s", zMulti);.
f38c0 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 69 29 3b free(zMulti);
f38d0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
f38e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f38f0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 2f 2a 20 NOMEM;. }./*
f3900 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
f3910 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
f3920 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
f3930 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
f3940 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
f3950 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
f3960 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
f3970 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
f3980 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
f3990 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
f39a0 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
f39b0 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
f39c0 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
f39d0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
f39e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 }else{. cha
f39f0 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20 63 68 r *zUtf8;. ch
f3a00 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d 41 58 ar zMbcsPath[MAX
f3a10 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 _PATH];. GetT
f3a20 65 6d 70 50 61 74 68 41 28 4d 41 58 5f 50 41 54 empPathA(MAX_PAT
f3a30 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74 68 29 H-30, zMbcsPath)
f3a40 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d 20 73 71 ;. zUtf8 = sq
f3a50 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 lite3_win32_mbcs
f3a60 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73 50 61 _to_utf8(zMbcsPa
f3a70 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 55 74 th);. if( zUt
f3a80 66 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 f8 ){. sqli
f3a90 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 te3_snprintf(MAX
f3aa0 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 _PATH-30, zTempP
f3ab0 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 74 66 38 ath, "%s", zUtf8
f3ac0 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 55 );. free(zU
f3ad0 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b tf8);. }else{
f3ae0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
f3af0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
f3b00 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 }.#endif. }. f
f3b10 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 74 72 6c or(i=sqlite3Strl
f3b20 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b en30(zTempPath);
f3b30 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50 61 74 i>0 && zTempPat
f3b40 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20 69 2d h[i-1]=='\\'; i-
f3b50 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 -){}. zTempPath
f3b60 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 [i] = 0;. sqlit
f3b70 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
f3b80 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 -30, zBuf,.
f3b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 "%
f3ba0 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f s\\"SQLITE_TEMP_
f3bb0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 FILE_PREFIX, zTe
f3bc0 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d 20 73 mpPath);. j = s
f3bd0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
f3be0 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f Buf);. sqlite3_
f3bf0 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c 20 26 randomness(20, &
f3c00 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f 72 28 zBuf[j]);. for(
f3c10 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b 2c 20 i=0; i<20; i++,
f3c20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a j++){. zBuf[j
f3c30 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 ] = (char)zChars
f3c40 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 [ ((unsigned cha
f3c50 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 r)zBuf[j])%(size
f3c60 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b of(zChars)-1) ];
f3c70 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d . }. zBuf[j] =
f3c80 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 0;. OSTRACE2("
f3c90 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 TEMP FILENAME: %
f3ca0 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 72 s\n", zBuf);. r
f3cb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
f3cc0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 .}../*.** The r
f3cd0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 67 eturn value of g
f3ce0 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 0a 2a etLastErrorMsg.*
f3cf0 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65 * is zero if the
f3d00 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 error message f
f3d10 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 its in the buffe
f3d20 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a r, or non-zero.*
f3d30 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 66 20 * otherwise (if
f3d40 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 73 20 the message was
f3d50 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f 0a 73 truncated)..*/.s
f3d60 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c 61 73 tatic int getLas
f3d70 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20 6e 42 tErrorMsg(int nB
f3d80 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b uf, char *zBuf){
f3d90 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d . DWORD error =
f3da0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b GetLastError();
f3db0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
f3dc0 57 49 4e 43 45 0a 20 20 73 71 6c 69 74 65 33 5f WINCE. sqlite3_
f3dd0 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a snprintf(nBuf, z
f3de0 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 Buf, "OsError 0x
f3df0 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72 2c %x (%u)", error,
f3e00 20 65 72 72 6f 72 29 3b 0a 23 65 6c 73 65 0a 20 error);.#else.
f3e10 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 /* FormatMessag
f3e20 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e 20 66 e returns 0 on f
f3e30 61 69 6c 75 72 65 2e 20 20 4f 74 68 65 72 77 69 ailure. Otherwi
f3e40 73 65 20 69 74 0a 20 20 2a 2a 20 72 65 74 75 72 se it. ** retur
f3e50 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
f3e60 20 54 43 48 41 52 73 20 77 72 69 74 74 65 6e 20 TCHARs written
f3e70 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 to the output.
f3e80 2a 2a 20 62 75 66 66 65 72 2c 20 65 78 63 6c 75 ** buffer, exclu
f3e90 64 69 6e 67 20 74 68 65 20 74 65 72 6d 69 6e 61 ding the termina
f3ea0 74 69 6e 67 20 6e 75 6c 6c 20 63 68 61 72 2e 0a ting null char..
f3eb0 20 20 2a 2f 0a 20 20 69 66 20 28 21 46 6f 72 6d */. if (!Form
f3ec0 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d 41 atMessageA(FORMA
f3ed0 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 T_MESSAGE_FROM_S
f3ee0 59 53 54 45 4d 2c 0a 20 20 20 20 20 20 20 20 20 YSTEM,.
f3ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c NUL
f3f00 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 L,.
f3f10 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c 0a error,.
f3f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3f30 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 0,.
f3f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
f3f50 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Buf,.
f3f60 20 20 20 20 20 20 20 20 20 20 20 6e 42 75 66 2d nBuf-
f3f70 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
f3f80 20 20 20 20 20 20 20 20 20 30 29 29 0a 20 20 7b 0)). {
f3f90 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
f3fa0 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 rintf(nBuf, zBuf
f3fb0 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20 , "OsError 0x%x
f3fc0 28 25 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72 (%u)", error, er
f3fd0 72 6f 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 ror);. }.#endif
f3fe0 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a .. return 0;.}.
f3ff0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 ./*.** Open a fi
f4000 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
f4010 74 20 77 69 6e 4f 70 65 6e 28 0a 20 20 73 71 6c t winOpen(. sql
f4020 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
f4030 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
f4040 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ed */. const ch
f4050 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 ar *zName,
f4060 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
f4070 20 66 69 6c 65 20 28 55 54 46 2d 38 29 20 2a 2f file (UTF-8) */
f4080 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
f4090 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 *id, /*
f40a0 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 74 65 Write the SQLite
f40b0 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 file handle her
f40c0 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
f40d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
f40e0 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c /* Open mode fl
f40f0 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f ags */. int *pO
f4100 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 utFlags
f4110 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74 /* Status ret
f4120 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a urn flags */.){.
f4130 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 44 57 HANDLE h;. DW
f4140 4f 52 44 20 64 77 44 65 73 69 72 65 64 41 63 63 ORD dwDesiredAcc
f4150 65 73 73 3b 0a 20 20 44 57 4f 52 44 20 64 77 53 ess;. DWORD dwS
f4160 68 61 72 65 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 hareMode;. DWOR
f4170 44 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 D dwCreationDisp
f4180 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 osition;. DWORD
f4190 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 dwFlagsAndAttri
f41a0 62 75 74 65 73 20 3d 20 30 3b 0a 23 69 66 20 53 butes = 0;.#if S
f41b0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
f41c0 20 69 6e 74 20 69 73 54 65 6d 70 20 3d 20 30 3b int isTemp = 0;
f41d0 0a 23 65 6e 64 69 66 0a 20 20 77 69 6e 46 69 6c .#endif. winFil
f41e0 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 e *pFile = (winF
f41f0 69 6c 65 2a 29 69 64 3b 0a 20 20 76 6f 69 64 20 ile*)id;. void
f4200 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 20 20 20 20 *zConverted;
f4210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f4220 46 69 6c 65 6e 61 6d 65 20 69 6e 20 4f 53 20 65 Filename in OS e
f4230 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e ncoding */. con
f4240 73 74 20 63 68 61 72 20 2a 7a 55 74 66 38 4e 61 st char *zUtf8Na
f4250 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 20 20 20 2f me = zName; /
f4260 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 55 54 * Filename in UT
f4270 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a F-8 encoding */.
f4280 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b char zTmpname[
f4290 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 20 20 20 20 MAX_PATH+1];
f42a0 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 75 73 /* Buffer us
f42b0 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 65 6d ed to create tem
f42c0 70 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20 p filename */..
f42d0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
f42e0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
f42f0 45 54 45 52 28 70 56 66 73 29 3b 0a 0a 20 20 2f ETER(pVfs);.. /
f4300 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 * If the second
f4310 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 argument to this
f4320 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c function is NUL
f4330 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20 L, generate a .
f4340 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 ** temporary fi
f4350 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a le name to use .
f4360 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 55 74 66 */. if( !zUtf
f4370 38 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74 8Name ){. int
f4380 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d rc = getTempnam
f4390 65 28 4d 41 58 5f 50 41 54 48 2b 31 2c 20 7a 54 e(MAX_PATH+1, zT
f43a0 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 mpname);. if(
f43b0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
f43c0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
f43d0 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 55 74 c;. }. zUt
f43e0 66 38 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d f8Name = zTmpnam
f43f0 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e e;. }.. /* Con
f4400 76 65 72 74 20 74 68 65 20 66 69 6c 65 6e 61 6d vert the filenam
f4410 65 20 74 6f 20 74 68 65 20 73 79 73 74 65 6d 20 e to the system
f4420 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 7a encoding. */. z
f4430 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 Converted = conv
f4440 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 ertUtf8Filename(
f4450 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 20 20 69 66 zUtf8Name);. if
f4460 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 ( zConverted==0
f4470 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
f4480 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
f4490 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 . if( flags & S
f44a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
f44b0 52 49 54 45 20 29 7b 0a 20 20 20 20 64 77 44 65 RITE ){. dwDe
f44c0 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47 45 siredAccess = GE
f44d0 4e 45 52 49 43 5f 52 45 41 44 20 7c 20 47 45 4e NERIC_READ | GEN
f44e0 45 52 49 43 5f 57 52 49 54 45 3b 0a 20 20 7d 65 ERIC_WRITE;. }e
f44f0 6c 73 65 7b 0a 20 20 20 20 64 77 44 65 73 69 72 lse{. dwDesir
f4500 65 64 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 edAccess = GENER
f4510 49 43 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 2f IC_READ;. }. /
f4520 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 * SQLITE_OPEN_EX
f4530 43 4c 55 53 49 56 45 20 69 73 20 75 73 65 64 20 CLUSIVE is used
f4540 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
f4550 74 20 61 20 6e 65 77 20 66 69 6c 65 20 69 73 20 t a new file is
f4560 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 2e 20 53 . ** created. S
f4570 51 4c 69 74 65 20 64 6f 65 73 6e 27 74 20 75 73 QLite doesn't us
f4580 65 20 69 74 20 74 6f 20 69 6e 64 69 63 61 74 65 e it to indicate
f4590 20 22 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 "exclusive acce
f45a0 73 73 22 20 0a 20 20 2a 2a 20 61 73 20 69 74 20 ss" . ** as it
f45b0 69 73 20 75 73 75 61 6c 6c 79 20 75 6e 64 65 72 is usually under
f45c0 73 74 6f 6f 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 stood.. */. as
f45d0 73 65 72 74 28 21 28 66 6c 61 67 73 20 26 20 53 sert(!(flags & S
f45e0 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
f45f0 53 49 56 45 29 20 7c 7c 20 28 66 6c 61 67 73 20 SIVE) || (flags
f4600 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 & SQLITE_OPEN_CR
f4610 45 41 54 45 29 29 3b 0a 20 20 69 66 28 20 66 6c EATE));. if( fl
f4620 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
f4630 4e 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 N_EXCLUSIVE ){.
f4640 20 20 20 2f 2a 20 43 72 65 61 74 65 73 20 61 20 /* Creates a
f4650 6e 65 77 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 new file, only i
f4660 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c f it does not al
f4670 72 65 61 64 79 20 65 78 69 73 74 2e 20 2a 2f 0a ready exist. */.
f4680 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 /* If the fi
f4690 6c 65 20 65 78 69 73 74 73 2c 20 69 74 20 66 61 le exists, it fa
f46a0 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 64 77 43 72 ils. */. dwCr
f46b0 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f eationDispositio
f46c0 6e 20 3d 20 43 52 45 41 54 45 5f 4e 45 57 3b 0a n = CREATE_NEW;.
f46d0 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 }else if( flag
f46e0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
f46f0 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 2f 2a CREATE ){. /*
f4700 20 4f 70 65 6e 20 65 78 69 73 74 69 6e 67 20 66 Open existing f
f4710 69 6c 65 2c 20 6f 72 20 63 72 65 61 74 65 20 69 ile, or create i
f4720 66 20 69 74 20 64 6f 65 73 6e 27 74 20 65 78 69 f it doesn't exi
f4730 73 74 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 st */. dwCrea
f4740 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 tionDisposition
f4750 3d 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 = OPEN_ALWAYS;.
f4760 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4f }else{. /* O
f4770 70 65 6e 73 20 61 20 66 69 6c 65 2c 20 6f 6e 6c pens a file, onl
f4780 79 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 y if it exists.
f4790 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f */. dwCreatio
f47a0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f nDisposition = O
f47b0 50 45 4e 5f 45 58 49 53 54 49 4e 47 3b 0a 20 20 PEN_EXISTING;.
f47c0 7d 0a 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20 }. dwShareMode
f47d0 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 45 41 = FILE_SHARE_REA
f47e0 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 5f 57 D | FILE_SHARE_W
f47f0 52 49 54 45 3b 0a 20 20 69 66 28 20 66 6c 61 67 RITE;. if( flag
f4800 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
f4810 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b DELETEONCLOSE ){
f4820 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
f4830 49 4e 43 45 0a 20 20 20 20 64 77 46 6c 61 67 73 INCE. dwFlags
f4840 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 AndAttributes =
f4850 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 FILE_ATTRIBUTE_H
f4860 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 54 65 6d IDDEN;. isTem
f4870 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 20 20 20 p = 1;.#else.
f4880 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 dwFlagsAndAttri
f4890 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 butes = FILE_ATT
f48a0 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41 52 59 RIBUTE_TEMPORARY
f48b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f48c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f48d0 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 | FILE_ATTRIBUTE
f48e0 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 20 20 20 _HIDDEN.
f48f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4900 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 46 4c | FILE_FL
f4910 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f AG_DELETE_ON_CLO
f4920 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c SE;.#endif. }el
f4930 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 67 73 41 se{. dwFlagsA
f4940 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 ndAttributes = F
f4950 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f ILE_ATTRIBUTE_NO
f4960 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 RMAL;. }. /* R
f4970 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 68 65 20 eports from the
f4980 69 6e 74 65 72 6e 65 74 20 61 72 65 20 74 68 61 internet are tha
f4990 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 t performance is
f49a0 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 62 65 74 always. ** bet
f49b0 74 65 72 20 69 66 20 46 49 4c 45 5f 46 4c 41 47 ter if FILE_FLAG
f49c0 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53 20 69 _RANDOM_ACCESS i
f49d0 73 20 75 73 65 64 2e 20 20 54 69 63 6b 65 74 20 s used. Ticket
f49e0 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 20 53 51 #2699. */.#if SQ
f49f0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
f4a00 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 dwFlagsAndAttrib
f4a10 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 utes |= FILE_FLA
f4a20 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53 3b G_RANDOM_ACCESS;
f4a30 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69 73 .#endif. if( is
f4a40 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 NT() ){. h =
f4a50 43 72 65 61 74 65 46 69 6c 65 57 28 28 57 43 48 CreateFileW((WCH
f4a60 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a AR*)zConverted,.
f4a70 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65 64 dwDesired
f4a80 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64 Access,. d
f4a90 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 wShareMode,.
f4aa0 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 NULL,.
f4ab0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 dwCreationDispos
f4ac0 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 ition,. dw
f4ad0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 FlagsAndAttribut
f4ae0 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a es,. NULL.
f4af0 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 );./* isNT()
f4b00 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
f4b10 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
f4b20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
f4b30 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a er executed. .**
f4b40 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 Since the ASCII
f4b50 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 version of thes
f4b60 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f e Windows API do
f4b70 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 not exist for W
f4b80 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d INCE,.** it's im
f4b90 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 portant to not r
f4ba0 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f eference them fo
f4bb0 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a r WINCE builds..
f4bc0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f4bd0 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
f4be0 65 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61 74 e{. h = Creat
f4bf0 65 46 69 6c 65 41 28 28 63 68 61 72 2a 29 7a 43 eFileA((char*)zC
f4c00 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 onverted,.
f4c10 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 dwDesiredAccess
f4c20 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 72 65 ,. dwShare
f4c30 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c Mode,. NUL
f4c40 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 65 61 L,. dwCrea
f4c50 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c tionDisposition,
f4c60 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 41 . dwFlagsA
f4c70 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20 20 ndAttributes,.
f4c80 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b NULL. );
f4c90 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 .#endif. }. if
f4ca0 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e ( h==INVALID_HAN
f4cb0 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20 DLE_VALUE ){.
f4cc0 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 free(zConverted
f4cd0 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 );. if( flags
f4ce0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
f4cf0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
f4d00 20 20 72 65 74 75 72 6e 20 77 69 6e 4f 70 65 6e return winOpen
f4d10 28 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 (pVfs, zName, id
f4d20 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
f4d30 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f ((flags|SQLITE_O
f4d40 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53 PEN_READONLY)&~S
f4d50 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
f4d60 52 49 54 45 29 2c 20 70 4f 75 74 46 6c 61 67 73 RITE), pOutFlags
f4d70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
f4d80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
f4d90 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 E_CANTOPEN;.
f4da0 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 }. }. if( pOut
f4db0 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 66 28 Flags ){. if(
f4dc0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
f4dd0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 OPEN_READWRITE )
f4de0 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 {. *pOutFla
f4df0 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs = SQLITE_OPEN
f4e00 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 _READWRITE;.
f4e10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f }else{. *pO
f4e20 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 utFlags = SQLITE
f4e30 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a _OPEN_READONLY;.
f4e40 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 }. }. mems
f4e50 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a et(pFile, 0, siz
f4e60 65 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20 eof(*pFile));.
f4e70 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d pFile->pMethod =
f4e80 20 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 &winIoMethod;.
f4e90 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 pFile->h = h;.
f4ea0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
f4eb0 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 o = NO_ERROR;.
f4ec0 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a pFile->sectorSiz
f4ed0 65 20 3d 20 67 65 74 53 65 63 74 6f 72 53 69 7a e = getSectorSiz
f4ee0 65 28 70 56 66 73 2c 20 7a 55 74 66 38 4e 61 6d e(pVfs, zUtf8Nam
f4ef0 65 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f e);.#if SQLITE_O
f4f00 53 5f 57 49 4e 43 45 0a 20 20 69 66 28 20 28 66 S_WINCE. if( (f
f4f10 6c 61 67 73 20 26 20 28 53 51 4c 49 54 45 5f 4f lags & (SQLITE_O
f4f20 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 PEN_READWRITE|SQ
f4f30 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
f4f40 42 29 29 20 3d 3d 0a 20 20 20 20 20 20 20 20 20 B)) ==.
f4f50 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 (SQLITE_OP
f4f60 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c EN_READWRITE|SQL
f4f70 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
f4f80 29 0a 20 20 20 20 20 20 20 26 26 20 21 77 69 6e ). && !win
f4f90 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 7a 4e 61 ceCreateLock(zNa
f4fa0 6d 65 2c 20 70 46 69 6c 65 29 0a 20 20 29 7b 0a me, pFile). ){.
f4fb0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 CloseHandle(
f4fc0 68 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f h);. free(zCo
f4fd0 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 72 65 nverted);. re
f4fe0 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 turn SQLITE_CANT
f4ff0 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 OPEN;. }. if(
f5000 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 70 46 isTemp ){. pF
f5010 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c ile->zDeleteOnCl
f5020 6f 73 65 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 ose = zConverted
f5030 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 ;. }else.#endif
f5040 0a 20 20 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 . {. free(zC
f5050 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 7d 0a 20 onverted);. }.
f5060 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 OpenCounter(+1)
f5070 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
f5080 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
f5090 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 64 20 elete the named
f50a0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 file..**.** Note
f50b0 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 64 6f that windows do
f50c0 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 66 es not allow a f
f50d0 69 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 ile to be delete
f50e0 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 0a d if some other.
f50f0 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 73 20 69 ** process has i
f5100 74 20 6f 70 65 6e 2e 20 20 53 6f 6d 65 74 69 6d t open. Sometim
f5110 65 73 20 61 20 76 69 72 75 73 20 73 63 61 6e 6e es a virus scann
f5120 65 72 20 6f 72 20 69 6e 64 65 78 69 6e 67 20 70 er or indexing p
f5130 72 6f 67 72 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f rogram.** will o
f5140 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 pen a journal fi
f5150 6c 65 20 73 68 6f 72 74 6c 79 20 61 66 74 65 72 le shortly after
f5160 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20 69 it is created i
f5170 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 0a 2a 2a n order to do.**
f5180 20 77 68 61 74 65 76 65 72 20 69 74 20 64 6f 65 whatever it doe
f5190 73 2e 20 20 57 68 69 6c 65 20 74 68 69 73 20 6f s. While this o
f51a0 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 ther process is
f51b0 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 holding the.** f
f51c0 69 6c 65 20 6f 70 65 6e 2c 20 77 65 20 77 69 6c ile open, we wil
f51d0 6c 20 62 65 20 75 6e 61 62 6c 65 20 74 6f 20 64 l be unable to d
f51e0 65 6c 65 74 65 20 69 74 2e 20 20 54 6f 20 77 6f elete it. To wo
f51f0 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 0a 2a rk around this.*
f5200 2a 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 64 65 * problem, we de
f5210 6c 61 79 20 31 30 30 20 6d 69 6c 6c 69 73 65 63 lay 100 millisec
f5220 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 74 6f 20 onds and try to
f5230 64 65 6c 65 74 65 20 61 67 61 69 6e 2e 20 20 55 delete again. U
f5240 70 0a 2a 2a 20 74 6f 20 4d 58 5f 44 45 4c 45 54 p.** to MX_DELET
f5250 49 4f 4e 5f 41 54 54 45 4d 50 54 73 20 64 65 6c ION_ATTEMPTs del
f5260 65 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 61 etion attempts a
f5270 72 65 20 72 75 6e 20 62 65 66 6f 72 65 20 67 69 re run before gi
f5280 76 69 6e 67 0a 2a 2a 20 75 70 20 61 6e 64 20 72 ving.** up and r
f5290 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f eturning an erro
f52a0 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 r..*/.#define MX
f52b0 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 _DELETION_ATTEMP
f52c0 54 53 20 35 0a 73 74 61 74 69 63 20 69 6e 74 20 TS 5.static int
f52d0 77 69 6e 44 65 6c 65 74 65 28 0a 20 20 73 71 6c winDelete(. sql
f52e0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
f52f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
f5300 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f used on win32 */
f5310 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
f5320 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20 2f Filename, /
f5330 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
f5340 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e o delete */. in
f5350 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 20 t syncDir
f5360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 /* Not
f5370 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a used on win32 *
f5380 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 6e 74 20 3d /.){. int cnt =
f5390 20 30 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 0;. DWORD rc;.
f53a0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d 20 DWORD error =
f53b0 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 0;. void *zConv
f53c0 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 erted = convertU
f53d0 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c tf8Filename(zFil
f53e0 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 ename);. UNUSED
f53f0 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
f5400 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
f5410 45 54 45 52 28 73 79 6e 63 44 69 72 29 3b 0a 20 ETER(syncDir);.
f5420 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d if( zConverted=
f5430 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
f5440 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
f5450 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 }. SimulateIOE
f5460 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 rror(return SQLI
f5470 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 TE_IOERR_DELETE)
f5480 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 ;. if( isNT() )
f5490 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 {. do{.
f54a0 44 65 6c 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e DeleteFileW(zCon
f54b0 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 verted);. }wh
f54c0 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 63 20 ile( ( ((rc
f54d0 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 = GetFileAttribu
f54e0 74 65 73 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 tesW(zConverted)
f54f0 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c ) != INVALID_FIL
f5500 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20 20 E_ATTRIBUTES).
f5510 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
f5520 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 ((error = GetLas
f5530 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 tError()) == ERR
f5540 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44 OR_ACCESS_DENIED
f5550 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 )). &&
f5560 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c (++cnt < MX_DEL
f5570 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a ETION_ATTEMPTS).
f5580 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 53 && (S
f5590 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b leep(100), 1) );
f55a0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
f55b0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f55c0 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
f55d0 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
f55e0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
f55f0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
f5600 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
f5610 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
f5620 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
f5630 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
f5640 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
f5650 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
f5660 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
f5670 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f5680 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
f5690 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 do{. Delet
f56a0 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65 eFileA(zConverte
f56b0 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 d);. }while(
f56c0 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 ( ((rc = Get
f56d0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 28 FileAttributesA(
f56e0 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 zConverted)) !=
f56f0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
f5700 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 RIBUTES).
f5710 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 || ((err
f5720 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f or = GetLastErro
f5730 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 r()) == ERROR_AC
f5740 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 CESS_DENIED)).
f5750 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 && (++c
f5760 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e nt < MX_DELETION
f5770 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 _ATTEMPTS).
f5780 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 && (Sleep(
f5790 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 65 6e 64 100), 1) );.#end
f57a0 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 if. }. free(zC
f57b0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f 53 54 onverted);. OST
f57c0 52 41 43 45 32 28 22 44 45 4c 45 54 45 20 5c 22 RACE2("DELETE \"
f57d0 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 %s\"\n", zFilena
f57e0 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 20 me);. return (
f57f0 20 20 28 72 63 20 3d 3d 20 49 4e 56 41 4c 49 44 (rc == INVALID
f5800 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 _FILE_ATTRIBUTES
f5810 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 ) . &&
f5820 28 65 72 72 6f 72 20 3d 3d 20 45 52 52 4f 52 5f (error == ERROR_
f5830 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 FILE_NOT_FOUND))
f5840 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
f5850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
f5860 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 TE;.}../*.** Che
f5870 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 ck the existance
f5880 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 and status of a
f5890 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
f58a0 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73 28 0a int winAccess(.
f58b0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
f58c0 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 Vfs, /*
f58d0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 Not used on win3
f58e0 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 2 */. const cha
f58f0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 r *zFilename,
f5900 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c /* Name of fil
f5910 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 e to check */.
f5920 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
f5930 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 /* Ty
f5940 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 pe of test to ma
f5950 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 ke on this file
f5960 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 */. int *pResOu
f5970 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
f5980 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a /* OUT: Result *
f5990 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 61 74 74 /.){. DWORD att
f59a0 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b r;. int rc = 0;
f59b0 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 . void *zConver
f59c0 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 ted = convertUtf
f59d0 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 8Filename(zFilen
f59e0 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ame);. UNUSED_P
f59f0 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
f5a00 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 if( zConverted
f5a10 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f5a20 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
f5a30 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 }. if( isNT()
f5a40 20 29 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 47 ){. attr = G
f5a50 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 etFileAttributes
f5a60 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 W((WCHAR*)zConve
f5a70 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 rted);./* isNT()
f5a80 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
f5a90 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
f5aa0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
f5ab0 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a er executed. .**
f5ac0 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 Since the ASCII
f5ad0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 version of thes
f5ae0 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f e Windows API do
f5af0 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 not exist for W
f5b00 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d INCE,.** it's im
f5b10 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 portant to not r
f5b20 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f eference them fo
f5b30 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a r WINCE builds..
f5b40 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f5b50 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
f5b60 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 47 65 e{. attr = Ge
f5b70 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 tFileAttributesA
f5b80 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 ((char*)zConvert
f5b90 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a ed);.#endif. }.
f5ba0 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
f5bb0 64 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c d);. switch( fl
f5bc0 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20 ags ){. case
f5bd0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 SQLITE_ACCESS_RE
f5be0 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c AD:. case SQL
f5bf0 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
f5c00 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 61 74 S:. rc = at
f5c10 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46 49 4c 45 tr!=INVALID_FILE
f5c20 5f 41 54 54 52 49 42 55 54 45 53 3b 0a 20 20 20 _ATTRIBUTES;.
f5c30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
f5c40 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
f5c50 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 _READWRITE:.
f5c60 20 20 72 63 20 3d 20 28 61 74 74 72 20 26 20 46 rc = (attr & F
f5c70 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 52 45 ILE_ATTRIBUTE_RE
f5c80 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 ADONLY)==0;.
f5c90 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 break;. def
f5ca0 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 ault:. asse
f5cb0 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 rt(!"Invalid fla
f5cc0 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 gs argument");.
f5cd0 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 }. *pResOut =
f5ce0 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c rc;. return SQL
f5cf0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ITE_OK;.}.../*.*
f5d00 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 * Turn a relativ
f5d10 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 e pathname into
f5d20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e a full pathname.
f5d30 20 20 57 72 69 74 65 20 74 68 65 20 66 75 6c 6c Write the full
f5d40 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 .** pathname int
f5d50 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b o zOut[]. zOut[
f5d60 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 ] will be at lea
f5d70 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e st pVfs->mxPathn
f5d80 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 ame.** bytes in
f5d90 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 size..*/.static
f5da0 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e int winFullPathn
f5db0 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ame(. sqlite3_v
f5dc0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 fs *pVfs,
f5dd0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
f5de0 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f to vfs object */
f5df0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
f5e00 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20 20 20 Relative,
f5e10 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c /* Possibly rel
f5e20 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 ative input path
f5e30 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c */. int nFull,
f5e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f5e50 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f /* Size of o
f5e60 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 utput buffer in
f5e70 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 bytes */. char
f5e80 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20 *zFull
f5e90 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
f5ea0 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a ut buffer */.){.
f5eb0 20 20 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f .#if defined(_
f5ec0 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 55 4e 55 _CYGWIN__). UNU
f5ed0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 SED_PARAMETER(nF
f5ee0 75 6c 6c 29 3b 0a 20 20 63 79 67 77 69 6e 5f 63 ull);. cygwin_c
f5ef0 6f 6e 76 5f 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 onv_to_full_win3
f5f00 32 5f 70 61 74 68 28 7a 52 65 6c 61 74 69 76 65 2_path(zRelative
f5f10 2c 20 7a 46 75 6c 6c 29 3b 0a 20 20 72 65 74 75 , zFull);. retu
f5f20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 rn SQLITE_OK;.#e
f5f30 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 ndif..#if SQLITE
f5f40 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 _OS_WINCE. UNUS
f5f50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 ED_PARAMETER(nFu
f5f60 6c 6c 29 3b 0a 20 20 2f 2a 20 57 69 6e 43 45 20 ll);. /* WinCE
f5f70 68 61 73 20 6e 6f 20 63 6f 6e 63 65 70 74 20 6f has no concept o
f5f80 66 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 f a relative pat
f5f90 68 6e 61 6d 65 2c 20 6f 72 20 73 6f 20 49 20 61 hname, or so I a
f5fa0 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c m told. */. sql
f5fb0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 56 ite3_snprintf(pV
f5fc0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 fs->mxPathname,
f5fd0 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 52 65 zFull, "%s", zRe
f5fe0 6c 61 74 69 76 65 29 3b 0a 20 20 72 65 74 75 72 lative);. retur
f5ff0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e n SQLITE_OK;.#en
f6000 64 69 66 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 dif..#if !SQLITE
f6010 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20 21 64 65 _OS_WINCE && !de
f6020 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f fined(__CYGWIN__
f6030 29 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 ). int nByte;.
f6040 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
f6050 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 3b d;. char *zOut;
f6060 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
f6070 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43 TER(nFull);. zC
f6080 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 onverted = conve
f6090 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a rtUtf8Filename(z
f60a0 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 66 28 Relative);. if(
f60b0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 isNT() ){. W
f60c0 43 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 CHAR *zTemp;.
f60d0 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c nByte = GetFull
f60e0 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 PathNameW((WCHAR
f60f0 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c *)zConverted, 0,
f6100 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 0, 0) + 3;.
f6110 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 zTemp = malloc(
f6120 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 nByte*sizeof(zTe
f6130 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 mp[0]) );. if
f6140 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 ( zTemp==0 ){.
f6150 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
f6160 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ted);. retu
f6170 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
f6180 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 . }. GetFu
f6190 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 llPathNameW((WCH
f61a0 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 AR*)zConverted,
f61b0 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 nByte, zTemp, 0)
f61c0 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 ;. free(zConv
f61d0 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 erted);. zOut
f61e0 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
f61f0 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 (zTemp);. fre
f6200 65 28 7a 54 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e e(zTemp);./* isN
f6210 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 T() is 1 if SQLI
f6220 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 TE_OS_WINCE==1,
f6230 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 so this else is
f6240 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 never executed.
f6250 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 .** Since the AS
f6260 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 CII version of t
f6270 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 hese Windows API
f6280 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f do not exist fo
f6290 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 r WINCE,.** it's
f62a0 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f important to no
f62b0 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d t reference them
f62c0 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 for WINCE build
f62d0 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
f62e0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
f62f0 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a else{. char *
f6300 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 zTemp;. nByte
f6310 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 = GetFullPathNa
f6320 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 meA((char*)zConv
f6330 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 erted, 0, 0, 0)
f6340 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d + 3;. zTemp =
f6350 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 malloc( nByte*s
f6360 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 izeof(zTemp[0])
f6370 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 );. if( zTemp
f6380 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 ==0 ){. fre
f6390 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
f63a0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
f63b0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
f63c0 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e GetFullPathN
f63d0 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e ameA((char*)zCon
f63e0 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a verted, nByte, z
f63f0 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 Temp, 0);. fr
f6400 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
f6410 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 zOut = sqlit
f6420 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f e3_win32_mbcs_to
f6430 5f 75 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 _utf8(zTemp);.
f6440 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 23 free(zTemp);.#
f6450 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 endif. }. if(
f6460 7a 4f 75 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 zOut ){. sqli
f6470 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 te3_snprintf(pVf
f6480 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a s->mxPathname, z
f6490 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 4f 75 74 Full, "%s", zOut
f64a0 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4f 75 74 );. free(zOut
f64b0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
f64c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
f64d0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
f64e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 ITE_NOMEM;. }.#
f64f0 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 endif.}../*.** G
f6500 65 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 et the sector si
f6510 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65 ze of the device
f6520 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a used to store.*
f6530 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 * file..*/.stati
f6540 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f 72 53 c int getSectorS
f6550 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74 65 33 ize(. sqlite3
f6560 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20 20 20 _vfs *pVfs,.
f6570 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c const char *zRel
f6580 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55 54 46 ative /* UTF
f6590 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a -8 file name */.
f65a0 29 7b 0a 20 20 44 57 4f 52 44 20 62 79 74 65 73 ){. DWORD bytes
f65b0 50 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 PerSector = SQLI
f65c0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
f65d0 52 5f 53 49 5a 45 3b 0a 20 20 63 68 61 72 20 7a R_SIZE;. char z
f65e0 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54 Fullpath[MAX_PAT
f65f0 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a H+1];. int rc;.
f6600 20 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20 DWORD dwRet =
f6610 30 2c 20 64 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f 0, dwDummy;.. /
f6620 2a 0a 20 20 2a 2a 20 57 65 20 6e 65 65 64 20 74 *. ** We need t
f6630 6f 20 67 65 74 20 74 68 65 20 66 75 6c 6c 20 70 o get the full p
f6640 61 74 68 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 ath name of the
f6650 66 69 6c 65 0a 20 20 2a 2a 20 74 6f 20 67 65 74 file. ** to get
f6660 20 74 68 65 20 64 72 69 76 65 20 6c 65 74 74 65 the drive lette
f6670 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 74 68 65 r to look up the
f6680 20 73 65 63 74 6f 72 0a 20 20 2a 2a 20 73 69 7a sector. ** siz
f6690 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77 e.. */. rc = w
f66a0 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 inFullPathname(p
f66b0 56 66 73 2c 20 7a 52 65 6c 61 74 69 76 65 2c 20 Vfs, zRelative,
f66c0 4d 41 58 5f 50 41 54 48 2c 20 7a 46 75 6c 6c 70 MAX_PATH, zFullp
f66d0 61 74 68 29 3b 0a 20 20 69 66 28 20 72 63 20 3d ath);. if( rc =
f66e0 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 0a 20 20 = SQLITE_OK ).
f66f0 7b 0a 20 20 20 20 76 6f 69 64 20 2a 7a 43 6f 6e {. void *zCon
f6700 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 verted = convert
f6710 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 75 Utf8Filename(zFu
f6720 6c 6c 70 61 74 68 29 3b 0a 20 20 20 20 69 66 28 llpath);. if(
f6730 20 7a 43 6f 6e 76 65 72 74 65 64 20 29 7b 0a 20 zConverted ){.
f6740 20 20 20 20 20 69 66 28 20 69 73 4e 54 28 29 20 if( isNT()
f6750 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 74 72 ){. /* tr
f6760 69 6d 20 70 61 74 68 20 74 6f 20 6a 75 73 74 20 im path to just
f6770 64 72 69 76 65 20 72 65 66 65 72 65 6e 63 65 20 drive reference
f6780 2a 2f 0a 20 20 20 20 20 20 20 20 57 43 48 41 52 */. WCHAR
f6790 20 2a 70 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 *p = zConverted
f67a0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a ;. for(;*
f67b0 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 p;p++){.
f67c0 20 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 if( *p == '\\'
f67d0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
f67e0 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 *p = '\0';.
f67f0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
f6800 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
f6810 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 }. dwRe
f6820 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 t = GetDiskFreeS
f6830 70 61 63 65 57 28 28 57 43 48 41 52 2a 29 7a 43 paceW((WCHAR*)zC
f6840 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 onverted,.
f6850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6860 20 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 &dwD
f6870 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 ummy,.
f6880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6890 20 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65 &bytesPe
f68a0 72 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 rSector,.
f68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f68c0 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 &dwDu
f68d0 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 mmy,.
f68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f68f0 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29 &dwDummy)
f6900 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ;.#if SQLITE_OS_
f6910 57 49 4e 43 45 3d 3d 30 0a 20 20 20 20 20 20 7d WINCE==0. }
f6920 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
f6930 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75 trim path to ju
f6940 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e st drive referen
f6950 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 48 ce */. CH
f6960 41 52 20 2a 70 20 3d 20 28 43 48 41 52 20 2a 29 AR *p = (CHAR *)
f6970 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 zConverted;.
f6980 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 for(;*p;p++)
f6990 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
f69a0 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 *p == '\\' ){.
f69b0 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27 *p = '
f69c0 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 \0';.
f69d0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
f69e0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
f69f0 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 65 dwRet = Ge
f6a00 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 28 tDiskFreeSpaceA(
f6a10 28 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 (CHAR*)zConverte
f6a20 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 d,.
f6a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6a40 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 &dwDummy,.
f6a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6a70 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 &bytesPerSector
f6a80 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
f6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6aa0 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 &dwDummy,.
f6ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6ad0 26 64 77 44 75 6d 6d 79 29 3b 0a 23 65 6e 64 69 &dwDummy);.#endi
f6ae0 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 f. }.
f6af0 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
f6b00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
f6b10 21 64 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20 !dwRet ){.
f6b20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d bytesPerSector =
f6b30 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
f6b40 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 SECTOR_SIZE;.
f6b50 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
f6b60 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53 65 (int) bytesPerSe
f6b70 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64 65 ctor; .}..#ifnde
f6b80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
f6b90 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a AD_EXTENSION./*.
f6ba0 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f ** Interfaces fo
f6bb0 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 r opening a shar
f6bc0 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 ed library, find
f6bd0 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 ing entry points
f6be0 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 .** within the s
f6bf0 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 hared library, a
f6c00 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 nd closing the s
f6c10 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a hared library..*
f6c20 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 /./*.** Interfac
f6c30 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 es for opening a
f6c40 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
f6c50 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 finding entry p
f6c60 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 oints.** within
f6c70 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
f6c80 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 ry, and closing
f6c90 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
f6ca0 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ry..*/.static vo
f6cb0 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 71 id *winDlOpen(sq
f6cc0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
f6cd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
f6ce0 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 4c lename){. HANDL
f6cf0 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f E h;. void *zCo
f6d00 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 nverted = conver
f6d10 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 tUtf8Filename(zF
f6d20 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 ilename);. UNUS
f6d30 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
f6d40 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 s);. if( zConve
f6d50 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 rted==0 ){. r
f6d60 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 eturn 0;. }. i
f6d70 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
f6d80 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 h = LoadLibrary
f6d90 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 W((WCHAR*)zConve
f6da0 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 rted);./* isNT()
f6db0 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
f6dc0 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
f6dd0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
f6de0 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a er executed. .**
f6df0 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 Since the ASCII
f6e00 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 version of thes
f6e10 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f e Windows API do
f6e20 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 not exist for W
f6e30 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d INCE,.** it's im
f6e40 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 portant to not r
f6e50 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f eference them fo
f6e60 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a r WINCE builds..
f6e70 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f6e80 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
f6e90 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c e{. h = LoadL
f6ea0 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29 7a ibraryA((char*)z
f6eb0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 Converted);.#end
f6ec0 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 if. }. free(zC
f6ed0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65 74 onverted);. ret
f6ee0 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a urn (void*)h;.}.
f6ef0 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44 static void winD
f6f00 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 lError(sqlite3_v
f6f10 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 fs *pVfs, int nB
f6f20 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 uf, char *zBufOu
f6f30 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 t){. UNUSED_PAR
f6f40 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
f6f50 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 getLastErrorMsg(
f6f60 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b 0a nBuf, zBufOut);.
f6f70 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 79 }.void (*winDlSy
f6f80 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 m(sqlite3_vfs *p
f6f90 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 Vfs, void *pHand
f6fa0 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a le, const char *
f6fb0 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 7b zSymbol))(void){
f6fc0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
f6fd0 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20 53 TER(pVfs);.#if S
f6fe0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
f6ff0 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63 41 /* The GetProcA
f7000 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69 6e ddressA() routin
f7010 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 e is only availa
f7020 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f ble on wince. */
f7030 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28 . return (void(
f7040 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63 *)(void))GetProc
f7050 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c 45 AddressA((HANDLE
f7060 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f )pHandle, zSymbo
f7070 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 41 l);.#else. /* A
f7080 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77 73 ll other windows
f7090 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65 63 platforms expec
f70a0 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 t GetProcAddress
f70b0 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a 20 () to take. **
f70c0 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20 72 an Ansi string r
f70d0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
f70e0 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69 6e _UNICODE settin
f70f0 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 g */. return (v
f7100 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74 oid(*)(void))Get
f7110 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41 4e ProcAddress((HAN
f7120 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 DLE)pHandle, zSy
f7130 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a mbol);.#endif.}.
f7140 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65 28 void winDlClose(
f7150 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
f7160 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 s, void *pHandle
f7170 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
f7180 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 46 METER(pVfs);. F
f7190 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e 44 reeLibrary((HAND
f71a0 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 LE)pHandle);.}.#
f71b0 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 else /* if SQLIT
f71c0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
f71d0 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 NSION is defined
f71e0 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 77 : */. #define w
f71f0 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 inDlOpen 0. #d
f7200 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f 72 efine winDlError
f7210 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 0. #define win
f7220 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 DlSym 0. #def
f7230 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 30 ine winDlClose 0
f7240 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
f7250 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 Write up to nBuf
f7260 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d bytes of random
f7270 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a ness into zBuf..
f7280 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
f7290 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 nRandomness(sqli
f72a0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
f72b0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
f72c0 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 Buf){. int n =
f72d0 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 0;. UNUSED_PARA
f72e0 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 METER(pVfs);.#if
f72f0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
f7300 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66 TEST). n = nBuf
f7310 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c ;. memset(zBuf,
f7320 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0, nBuf);.#else
f7330 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53 59 . if( sizeof(SY
f7340 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66 2d STEMTIME)<=nBuf-
f7350 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d 54 n ){. SYSTEMT
f7360 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53 79 IME x;. GetSy
f7370 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20 20 stemTime(&x);.
f7380 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
f7390 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 ], &x, sizeof(x)
f73a0 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
f73b0 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28 of(x);. }. if(
f73c0 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d sizeof(DWORD)<=
f73d0 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 nBuf-n ){. DW
f73e0 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75 72 ORD pid = GetCur
f73f0 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29 3b rentProcessId();
f7400 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 . memcpy(&zBu
f7410 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 f[n], &pid, size
f7420 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e 20 of(pid));. n
f7430 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a += sizeof(pid);.
f7440 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 }. if( sizeof
f7450 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 (DWORD)<=nBuf-n
f7460 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e 74 ){. DWORD cnt
f7470 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74 28 = GetTickCount(
f7480 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
f7490 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 Buf[n], &cnt, si
f74a0 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20 20 zeof(cnt));.
f74b0 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29 n += sizeof(cnt)
f74c0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 ;. }. if( size
f74d0 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 of(LARGE_INTEGER
f74e0 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 )<=nBuf-n ){.
f74f0 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20 69 LARGE_INTEGER i
f7500 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66 6f ;. QueryPerfo
f7510 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 69 rmanceCounter(&i
f7520 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
f7530 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a 65 Buf[n], &i, size
f7540 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b 3d of(i));. n +=
f7550 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d 0a sizeof(i);. }.
f7560 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
f7570 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 n;.}.../*.** Sle
f7580 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 ep for a little
f7590 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 while. Return t
f75a0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d he amount of tim
f75b0 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 74 e slept..*/.stat
f75c0 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 28 ic int winSleep(
f75d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
f75e0 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 29 s, int microsec)
f75f0 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72 6f {. Sleep((micro
f7600 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a sec+999)/1000);.
f7610 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f7620 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 ER(pVfs);. retu
f7630 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 rn ((microsec+99
f7640 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d 9)/1000)*1000;.}
f7650 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
f7660 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
f7670 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
f7680 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f zero value, beco
f7690 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a mes the result.*
f76a0 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 * returned from
f76b0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
f76c0 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 Time(). This is
f76d0 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
f76e0 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c g..*/.#ifdef SQL
f76f0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
f7700 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f7710 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 current_time = 0
f7720 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
f7730 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 Find the current
f7740 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 time (in Univer
f7750 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 sal Coordinated
f7760 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 Time). Write th
f7770 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d e.** current tim
f7780 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 e and date as a
f7790 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 Julian Day numbe
f77a0 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e r into *prNow an
f77b0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 d.** return 0.
f77c0 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
f77d0 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 time and date ca
f77e0 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a nnot be found..*
f77f0 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 /.int winCurrent
f7800 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 Time(sqlite3_vfs
f7810 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a *pVfs, double *
f7820 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 49 prNow){. FILETI
f7830 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c 45 ME ft;. /* FILE
f7840 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 69 TIME structure i
f7850 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 65 s a 64-bit value
f7860 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 representing th
f7870 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 20 e number of .
f7880 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 100-nanosecond
f7890 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 65 intervals since
f78a0 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 31 January 1, 1601
f78b0 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 35 (= JD 2305813.5
f78c0 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ). . */. sqlit
f78d0 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b 20 e3_int64 timeW;
f78e0 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73 20 /* Whole days
f78f0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 */. sqlite3_int
f7900 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20 46 64 timeF; /* F
f7910 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20 2a ractional Days *
f7920 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /.. /* Number o
f7930 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 f 100-nanosecond
f7940 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61 20 intervals in a
f7950 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20 20 single day */.
f7960 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
f7970 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 ite3_int64 ntuPe
f7980 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30 rDay = . 10
f7990 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 000000*(sqlite3_
f79a0 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20 20 int64)86400;..
f79b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 30 /* Number of 100
f79c0 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 -nanosecond inte
f79d0 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f 66 rvals in half of
f79e0 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74 a day */. stat
f79f0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
f7a00 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61 6c _int64 ntuPerHal
f7a10 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30 fDay = . 10
f7a20 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 000000*(sqlite3_
f7a30 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20 20 int64)43200;..
f7a40 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76 6f /* 2^32 - to avo
f7a50 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e 64 id use of LL and
f7a60 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63 63 warnings in gcc
f7a70 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e */. static con
f7a80 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 st sqlite3_int64
f7a90 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20 3d max32BitValue =
f7aa0 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 . (sqlite3
f7ab0 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30 _int64)200000000
f7ac0 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 0 + (sqlite3_int
f7ad0 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b 20 64)2000000000 +
f7ae0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 (sqlite3_int64)2
f7af0 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20 53 94967296;..#if S
f7b00 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
f7b10 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d 65 SYSTEMTIME time
f7b20 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d ;. GetSystemTim
f7b30 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 e(&time);. /* i
f7b40 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 f SystemTimeToFi
f7b50 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c 20 leTime() fails,
f7b60 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2e it returns zero.
f7b70 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74 65 */. if (!Syste
f7b80 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 mTimeToFileTime(
f7b90 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20 &time,&ft)){.
f7ba0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 return 1;. }.#
f7bb0 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65 6d else. GetSystem
f7bc0 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 20 TimeAsFileTime(
f7bd0 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 &ft );.#endif.
f7be0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f7bf0 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57 20 (pVfs);. timeW
f7c00 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 = (((sqlite3_int
f7c10 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74 65 64)ft.dwHighDate
f7c20 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56 61 Time)*max32BitVa
f7c30 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33 5f lue) + (sqlite3_
f7c40 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44 61 int64)ft.dwLowDa
f7c50 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46 20 teTime;. timeF
f7c60 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65 72 = timeW % ntuPer
f7c70 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Day; /*
f7c80 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 fractional days
f7c90 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 (100-nanosecond
f7ca0 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 s) */. timeW =
f7cb0 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44 61 timeW / ntuPerDa
f7cc0 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77 y; /* w
f7cd0 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 74 hole days */. t
f7ce0 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 32 imeW = timeW + 2
f7cf0 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20 20 305813;
f7d00 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 /* add whole
f7d10 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35 38 days (from 23058
f7d20 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 46 13.5) */. timeF
f7d30 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50 65 = timeF + ntuPe
f7d40 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20 2f rHalfDay; /
f7d50 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61 79 * add half a day
f7d60 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35 (from 2305813.5
f7d70 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 ) */. timeW = t
f7d80 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e 74 imeW + (timeF/nt
f7d90 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61 64 uPerDay); /* ad
f7da0 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20 68 d whole day if h
f7db0 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e 65 alf day made one
f7dc0 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 69 */. timeF = ti
f7dd0 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79 3b meF % ntuPerDay;
f7de0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6d /* com
f7df0 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69 6f pute new fractio
f7e00 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a 70 nal days */. *p
f7e10 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29 74 rNow = (double)t
f7e20 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65 29 imeW + ((double)
f7e30 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65 29 timeF / (double)
f7e40 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66 64 ntuPerDay);.#ifd
f7e50 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
f7e60 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 if( sqlite3_cur
f7e70 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 rent_time ){.
f7e80 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75 62 *prNow = ((doub
f7e90 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65 le)sqlite3_curre
f7ea0 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62 6c nt_time + (doubl
f7eb0 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75 62 e)43200) / (doub
f7ec0 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75 62 le)86400 + (doub
f7ed0 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d 0a le)2440587;. }.
f7ee0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
f7ef0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 0;.}../*.** The
f7f00 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68 69 idea is that thi
f7f10 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 s function works
f7f20 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74 like a combinat
f7f30 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 ion of.** GetLas
f7f40 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72 tError() and For
f7f50 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20 matMessage() on
f7f60 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e windows (or errn
f7f70 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f o and.** strerro
f7f80 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e 20 r_r() on unix).
f7f90 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69 After an error i
f7fa0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e s returned by an
f7fb0 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c OS.** function,
f7fc0 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 SQLite calls th
f7fd0 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 is function with
f7fe0 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74 zBuf pointing t
f7ff0 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66 o.** a buffer of
f8000 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 65 nBuf bytes. The
f8010 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64 OS layer should
f8020 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a populate the.**
f8030 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20 6e buffer with a n
f8040 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ul-terminated UT
f8050 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f F-8 encoded erro
f8060 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73 r message.** des
f8070 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74 cribing the last
f8080 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76 IO error to hav
f8090 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 69 e occurred withi
f80a0 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a n the calling.**
f80b0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 thread..**.** I
f80c0 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 f the error mess
f80d0 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 age is too large
f80e0 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 65 for the supplie
f80f0 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20 d buffer,.** it
f8100 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 should be trunca
f8110 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e 20 ted. The return
f8120 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 73 value of xGetLas
f8130 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 tError.** is zer
f8140 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d o if the error m
f8150 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 essage fits in t
f8160 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f he buffer, or no
f8170 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 n-zero.** otherw
f8180 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 ise (if the mess
f8190 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 age was truncate
f81a0 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 d). If non-zero
f81b0 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 is returned,.**
f81c0 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e then it is not n
f81d0 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 6c ecessary to incl
f81e0 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d ude the nul-term
f81f0 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 72 inator character
f8200 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75 .** in the outpu
f8210 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 t buffer..**.**
f8220 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 6e Not supplying an
f8230 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 error message w
f8240 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 65 ill have no adve
f8250 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e rse effect.** on
f8260 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 66 SQLite. It is f
f8270 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 69 ine to have an i
f8280 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 mplementation th
f8290 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75 at never.** retu
f82a0 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 rns an error mes
f82b0 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e sage:.**.** in
f82c0 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 t xGetLastError(
f82d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
f82e0 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 s, int nBuf, cha
f82f0 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20 r *zBuf){.**
f8300 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d assert(zBuf[0]=
f8310 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72 ='\0');.** r
f8320 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a eturn 0;.** }.
f8330 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 66 **.** However if
f8340 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
f8350 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 69 e is supplied, i
f8360 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70 t will be incorp
f8370 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c orated.** by sql
f8380 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 72 ite into the err
f8390 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 6c or message avail
f83a0 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72 able to the user
f83b0 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 using.** sqlite
f83c0 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 73 3_errmsg(), poss
f83d0 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65 ibly making IO e
f83e0 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f 20 rrors easier to
f83f0 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 debug..*/.static
f8400 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 45 int winGetLastE
f8410 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
f8420 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
f8430 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
f8440 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f8450 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72 R(pVfs);. retur
f8460 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 n getLastErrorMs
f8470 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d g(nBuf, zBuf);.}
f8480 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
f8490 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c ze and deinitial
f84a0 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e ize the operatin
f84b0 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 g system interfa
f84c0 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ce..*/.SQLITE_AP
f84d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 I int sqlite3_os
f84e0 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 _init(void){. s
f84f0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 tatic sqlite3_vf
f8500 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20 s winVfs = {.
f8510 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
f8520 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
f8530 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 69 */. sizeof(wi
f8540 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f nFile), /* szO
f8550 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58 sFile */. MAX
f8560 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 20 _PATH,
f8570 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f /* mxPathname */
f8580 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
f8590 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
f85a0 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 2c */. "win32",
f85b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e /* zN
f85c0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
f85d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f85e0 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20 pAppData */. .
f85f0 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20 winOpen,
f8600 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a /* xOpen *
f8610 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 2c /. winDelete,
f8620 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c /* xDel
f8630 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63 ete */. winAc
f8640 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a cess, /*
f8650 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 xAccess */.
f8660 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c winFullPathname,
f8670 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e /* xFullPathn
f8680 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c ame */. winDl
f8690 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a Open, /*
f86a0 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 xDlOpen */.
f86b0 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 winDlError,
f86c0 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a /* xDlError *
f86d0 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20 /. winDlSym,
f86e0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 /* xDlS
f86f0 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43 ym */. winDlC
f8700 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 lose, /*
f8710 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 xDlClose */.
f8720 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 winRandomness,
f8730 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 /* xRandomnes
f8740 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 s */. winSlee
f8750 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 p, /* x
f8760 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e Sleep */. win
f8770 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 CurrentTime,
f8780 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 /* xCurrentTime
f8790 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 73 */. winGetLas
f87a0 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 tError /* xGe
f87b0 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 tLastError */.
f87c0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 };. sqlite3_vfs
f87d0 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 66 _register(&winVf
f87e0 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 s, 1);. return
f87f0 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51 SQLITE_OK; .}.SQ
f8800 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
f8810 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 ite3_os_end(void
f8820 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ){ . return SQL
f8830 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 ITE_OK;.}..#endi
f8840 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 f /* SQLITE_OS_W
f8850 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a IN */../********
f8860 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
f8870 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _win.c *********
f8880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
f88b0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
f88c0 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a e bitvec.c *****
f88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f88f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
f8900 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a 2a 8 February 16.**
f8910 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
f8920 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
f8930 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
f8940 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
f8950 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
f8960 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
f8970 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
f8980 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
f8990 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
f89a0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
f89b0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
f89c0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
f89d0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
f89e0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
f89f0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
f8a00 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
f8a10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
f8a20 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
f8a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
f8a70 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d This file implem
f8a80 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 74 ents an object t
f8a90 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 hat represents a
f8aa0 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a fixed-length.**
f8ab0 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20 61 bitmap. Bits a
f8ac0 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 re numbered star
f8ad0 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a ting with 1..**.
f8ae0 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20 75 ** A bitmap is u
f8af0 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 sed to record wh
f8b00 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20 64 ich pages of a d
f8b10 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 76 atabase file hav
f8b20 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 e been.** journa
f8b30 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74 72 lled during a tr
f8b40 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68 ansaction, or wh
f8b50 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20 74 ich pages have t
f8b60 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a he "dont-write".
f8b70 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55 73 ** property. Us
f8b80 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77 ually only a few
f8b90 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74 20 pages are meet
f8ba0 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e either condition
f8bb0 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74 6d ..** So the bitm
f8bc0 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73 70 ap is usually sp
f8bd0 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f 77 arse and has low
f8be0 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a cardinality..**
f8bf0 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 28 But sometimes (
f8c00 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65 6e for example when
f8c10 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20 6f during a DROP o
f8c20 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65 29 f a large table)
f8c30 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20 most.** or all
f8c40 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e 20 of the pages in
f8c50 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 67 a database can g
f8c60 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20 et journalled.
f8c70 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 In those cases,
f8c80 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20 62 .** the bitmap b
f8c90 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69 74 ecomes dense wit
f8ca0 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c 69 h high cardinali
f8cb0 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69 74 ty. The algorit
f8cc0 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20 hm needs .** to
f8cd0 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73 65 handle both case
f8ce0 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 s well..**.** Th
f8cf0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 69 e size of the bi
f8d00 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77 68 tmap is fixed wh
f8d10 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 en the object is
f8d20 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 created..**.**
f8d30 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c 65 All bits are cle
f8d40 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74 6d ar when the bitm
f8d50 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 ap is created.
f8d60 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 0a Individual bits.
f8d70 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f 72 ** may be set or
f8d80 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74 20 cleared one at
f8d90 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65 a time..**.** Te
f8da0 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 st operations ar
f8db0 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d 65 e about 100 time
f8dc0 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 s more common th
f8dd0 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e at set operation
f8de0 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72 s..** Clear oper
f8df0 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65 65 ations are excee
f8e00 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 68 dingly rare. Th
f8e10 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79 20 ere are usually
f8e20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64 between.** 5 and
f8e30 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74 69 500 set operati
f8e40 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20 6f ons per Bitvec o
f8e50 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74 68 bject, though th
f8e60 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 e number of sets
f8e70 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 can.** sometime
f8e80 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73 s grow into tens
f8e90 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f 72 of thousands or
f8ea0 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73 69 larger. The si
f8eb0 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74 ze of the.** Bit
f8ec0 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74 68 vec object is th
f8ed0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
f8ee0 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
f8ef0 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a e file at the.**
f8f00 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61 6e start of a tran
f8f10 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20 saction, and is
f8f20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65 73 thus usually les
f8f30 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68 6f s than a few tho
f8f40 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61 usand,.** but ca
f8f50 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61 73 n be as large as
f8f60 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61 2 billion for a
f8f70 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74 61 really big data
f8f80 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 base..**.** @(#)
f8f90 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 2c 76 $Id: bitvec.c,v
f8fa0 20 31 2e 31 34 20 32 30 30 39 2f 30 34 2f 30 31 1.14 2009/04/01
f8fb0 20 32 33 3a 34 39 3a 30 34 20 64 72 68 20 45 78 23:49:04 drh Ex
f8fc0 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20 p $.*/../* Size
f8fd0 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 73 74 of the Bitvec st
f8fe0 72 75 63 74 75 72 65 20 69 6e 20 62 79 74 65 73 ructure in bytes
f8ff0 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 . */.#define BIT
f9000 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 35 31 VEC_SZ 51
f9010 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 2../* Round the
f9020 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 union size down
f9030 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 70 to the nearest p
f9040 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 79 2c ointer boundary,
f9050 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 68 6f since that's ho
f9060 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 w .** it will be
f9070 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 6e 20 aligned within
f9080 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63 the Bitvec struc
f9090 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 t. */.#define BI
f90a0 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20 28 TVEC_USIZE (
f90b0 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 ((BITVEC_SZ-(3*s
f90c0 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 69 7a izeof(u32)))/siz
f90d0 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a 73 69 eof(Bitvec*))*si
f90e0 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 0a 0a zeof(Bitvec*))..
f90f0 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 61 /* Type of the a
f9100 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 20 66 rray "element" f
f9110 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 72 65 or the bitmap re
f9120 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a presentation. .*
f9130 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20 70 6f * Should be a po
f9140 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20 69 64 wer of 2, and id
f9150 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69 eally, evenly di
f9160 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56 45 43 vide into BITVEC
f9170 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 _USIZE. .** Sett
f9180 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68 65 20 ing this to the
f9190 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22 20 73 "natural word" s
f91a0 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50 55 20 ize of your CPU
f91b0 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 may improve.** p
f91c0 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 erformance. */.#
f91d0 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 54 45 define BITVEC_TE
f91e0 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20 53 69 LEM u8./* Si
f91f0 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f 66 20 ze, in bits, of
f9200 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65 6d 65 the bitmap eleme
f9210 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 nt. */.#define B
f9220 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20 ITVEC_SZELEM
f9230 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 8./* Number of e
f9240 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62 69 74 lements in a bit
f9250 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 map array. */.#d
f9260 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c efine BITVEC_NEL
f9270 45 4d 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 EM (BITVEC_U
f9280 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49 54 56 SIZE/sizeof(BITV
f9290 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 EC_TELEM))./* Nu
f92a0 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 mber of bits in
f92b0 74 68 65 20 62 69 74 6d 61 70 20 61 72 72 61 79 the bitmap array
f92c0 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 . */.#define BIT
f92d0 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20 28 42 VEC_NBIT (B
f92e0 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 ITVEC_NELEM*BITV
f92f0 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e EC_SZELEM)../* N
f9300 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76 61 6c umber of u32 val
f9310 75 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c ues in hash tabl
f9320 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 e. */.#define BI
f9330 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 TVEC_NINT (
f9340 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a BITVEC_USIZE/siz
f9350 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78 eof(u32))./* Max
f9360 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 imum number of e
f9370 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68 20 74 ntries in hash t
f9380 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 able before .**
f9390 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 sub-dividing and
f93a0 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a re-hashing. */.
f93b0 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4d #define BITVEC_M
f93c0 58 48 41 53 48 20 20 20 20 28 42 49 54 56 45 43 XHASH (BITVEC
f93d0 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68 _NINT/2)./* Hash
f93e0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 ing function for
f93f0 20 74 68 65 20 61 48 61 73 68 20 72 65 70 72 65 the aHash repre
f9400 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d sentation..** Em
f9410 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e 67 20 pirical testing
f9420 73 68 6f 77 65 64 20 74 68 61 74 20 74 68 65 20 showed that the
f9430 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a *37 multiplier .
f9440 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61 72 79 ** (an arbitrary
f9450 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20 68 61 prime)in the ha
f9460 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 sh function prov
f9470 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 ided .** no fewe
f9480 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61 r collisions tha
f9490 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 n the no-op *1.
f94a0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
f94b0 43 5f 48 41 53 48 28 58 29 20 20 20 28 28 28 58 C_HASH(X) (((X
f94c0 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 )*1)%BITVEC_NINT
f94d0 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 )..#define BITVE
f94e0 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 49 54 C_NPTR (BIT
f94f0 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 VEC_USIZE/sizeof
f9500 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a (Bitvec *)).../*
f9510 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20 .** A bitmap is
f9520 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
f9530 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
f9540 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ucture..**.** Th
f9550 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f 72 64 is bitmap record
f9560 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 s the existance
f9570 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 of zero or more
f9580 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c bits.** with val
f9590 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e ues between 1 an
f95a0 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 d iSize, inclusi
f95b0 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 ve..**.** There
f95c0 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69 62 are three possib
f95d0 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f le representatio
f95e0 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 ns of the bitmap
f95f0 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42 ..** If iSize<=B
f9600 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e ITVEC_NBIT, then
f9610 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 6d 61 Bitvec.u.aBitma
f9620 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 67 68 p[] is a straigh
f9630 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 54 68 t.** bitmap. Th
f9640 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63 e least signific
f9650 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 20 31 ant bit is bit 1
f9660 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65 ..**.** If iSize
f9670 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 6e 64 >BITVEC_NBIT and
f9680 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 68 65 iDivisor==0 the
f9690 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 73 68 n Bitvec.u.aHash
f96a0 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 68 20 [] is.** a hash
f96b0 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c 6c 20 table that will
f96c0 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 56 45 hold up to BITVE
f96d0 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 6e 63 C_MXHASH distinc
f96e0 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 t values..**.**
f96f0 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 76 Otherwise, the v
f9700 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 72 65 alue i is redire
f9710 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 cted into one of
f9720 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 BITVEC_NPTR.**
f9730 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e sub-bitmaps poin
f9740 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 65 63 ted to by Bitvec
f9750 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 61 63 .u.apSub[]. Eac
f9760 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68 h subbitmap.** h
f9770 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 44 69 andles up to iDi
f9780 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 20 76 visor separate v
f9790 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 70 53 alues of i. apS
f97a0 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 ub[0] holds.** v
f97b0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 alues between 1
f97c0 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 20 61 and iDivisor. a
f97d0 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61 pSub[1] holds va
f97e0 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 lues between.**
f97f0 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 32 iDivisor+1 and 2
f9800 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75 *iDivisor. apSu
f9810 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 b[N] holds value
f9820 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 s between.** N*i
f9830 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e Divisor+1 and (N
f9840 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 +1)*iDivisor. E
f9850 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 69 73 ach subbitmap is
f9860 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 normalized.** t
f9870 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 o hold deal with
f9880 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 values between
f9890 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a 1 and iDivisor..
f98a0 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 65 63 */.struct Bitvec
f98b0 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 3b 20 {. u32 iSize;
f98c0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
f98d0 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 78 20 bit index. Max
f98e0 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 2c 39 iSize is 4,294,9
f98f0 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32 67,296. */. u32
f9900 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 nSet; /*
f9910 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 74 Number of bits t
f9920 68 61 74 20 61 72 65 20 73 65 74 20 2d 20 6f 6e hat are set - on
f9930 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 48 61 ly valid for aHa
f9940 73 68 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 sh.
f9950 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e ** element.
f9960 20 20 4d 61 78 20 69 73 20 42 49 54 56 45 43 5f Max is BITVEC_
f9970 4e 49 4e 54 2e 20 20 46 6f 72 20 42 49 54 56 45 NINT. For BITVE
f9980 43 5f 53 5a 20 6f 66 20 35 31 32 2c 0a 20 20 20 C_SZ of 512,.
f9990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
f99a0 2a 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 * this would be
f99b0 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44 125. */. u32 iD
f99c0 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d ivisor; /* Num
f99d0 62 65 72 20 6f 66 20 62 69 74 73 20 68 61 6e 64 ber of bits hand
f99e0 6c 65 64 20 62 79 20 65 61 63 68 20 61 70 53 75 led by each apSu
f99f0 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 b[] entry. */.
f9a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f9a10 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f /* Should >=0 fo
f9a20 72 20 61 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e r apSub element.
f9a30 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
f9a40 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 69 44 69 /* Max iDi
f9a50 76 69 73 6f 72 20 69 73 20 6d 61 78 28 75 33 32 visor is max(u32
f9a60 29 20 2f 20 42 49 54 56 45 43 5f 4e 50 54 52 20 ) / BITVEC_NPTR
f9a70 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 + 1. */.
f9a80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f /* Fo
f9a90 72 20 61 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 r a BITVEC_SZ of
f9aa0 20 35 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 512, this would
f9ab0 20 62 65 20 33 34 2c 33 35 39 2c 37 33 39 2e 20 be 34,359,739.
f9ac0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 */. union {.
f9ad0 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 61 42 BITVEC_TELEM aB
f9ae0 69 74 6d 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c itmap[BITVEC_NEL
f9af0 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 EM]; /* Bitma
f9b00 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e p representation
f9b10 20 2a 2f 0a 20 20 20 20 75 33 32 20 61 48 61 73 */. u32 aHas
f9b20 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 h[BITVEC_NINT];
f9b30 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 /* Hash tab
f9b40 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f le representatio
f9b50 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76 65 63 20 n */. Bitvec
f9b60 2a 61 70 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 *apSub[BITVEC_NP
f9b70 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 TR]; /* Recursi
f9b80 76 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ve representatio
f9b90 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a n */. } u;.};..
f9ba0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
f9bb0 65 77 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 ew bitmap object
f9bc0 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 able to handle
f9bd0 62 69 74 73 20 62 65 74 77 65 65 6e 20 30 20 61 bits between 0 a
f9be0 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 nd iSize,.** inc
f9bf0 6c 75 73 69 76 65 2e 20 20 52 65 74 75 72 6e 20 lusive. Return
f9c00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
f9c10 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 new object. Re
f9c20 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a turn NULL if .**
f9c30 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a malloc fails..*
f9c40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
f9c50 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 65 33 Bitvec *sqlite3
f9c60 42 69 74 76 65 63 43 72 65 61 74 65 28 75 33 32 BitvecCreate(u32
f9c70 20 69 53 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 iSize){. Bitve
f9c80 63 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 c *p;. assert(
f9c90 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 sizeof(*p)==BITV
f9ca0 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 EC_SZ );. p = s
f9cb0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
f9cc0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a ( sizeof(*p) );.
f9cd0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 if( p ){. p
f9ce0 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b ->iSize = iSize;
f9cf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
f9d00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
f9d10 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 69 2d to see if the i-
f9d20 74 68 20 62 69 74 20 69 73 20 73 65 74 2e 20 20 th bit is set.
f9d30 52 65 74 75 72 6e 20 74 72 75 65 20 6f 72 20 66 Return true or f
f9d40 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 alse..** If p is
f9d50 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 20 62 69 NULL (if the bi
f9d60 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 62 65 65 tmap has not bee
f9d70 6e 20 63 72 65 61 74 65 64 29 20 6f 72 20 69 66 n created) or if
f9d80 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 6f 66 20 .** i is out of
f9d90 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 65 74 75 range, then retu
f9da0 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c rn false..*/.SQL
f9db0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
f9dc0 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
f9dd0 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 t(Bitvec *p, u32
f9de0 20 69 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 i){. if( p==0
f9df0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 ) return 0;. if
f9e00 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 ( i>p->iSize ||
f9e10 69 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b i==0 ) return 0;
f9e20 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 . i--;. while(
f9e30 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a p->iDivisor ){.
f9e40 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f u32 bin = i/
f9e50 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 p->iDivisor;.
f9e60 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 i = i%p->iDivis
f9e70 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 or;. p = p->u
f9e80 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 .apSub[bin];.
f9e90 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20 if (!p) {.
f9ea0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
f9eb0 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 . }. if( p->iS
f9ec0 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 ize<=BITVEC_NBIT
f9ed0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 ){. return (
f9ee0 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 p->u.aBitmap[i/B
f9ef0 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 ITVEC_SZELEM] &
f9f00 28 31 3c 3c 28 69 26 28 42 49 54 56 45 43 5f 53 (1<<(i&(BITVEC_S
f9f10 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a ZELEM-1))))!=0;.
f9f20 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20 20 75 33 } else{. u3
f9f30 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 2 h = BITVEC_HAS
f9f40 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c H(i++);. whil
f9f50 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d e( p->u.aHash[h]
f9f60 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d ){. if( p-
f9f70 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 >u.aHash[h]==i )
f9f80 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
f9f90 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 h++;. if(
f9fa0 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 h>=BITVEC_NINT )
f9fb0 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 h = 0;. }.
f9fc0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
f9fd0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
f9fe0 20 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 i-th bit. Retu
f9ff0 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 rn 0 on success
fa000 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
fa010 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 e if.** anything
fa020 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a goes wrong..**.
fa030 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
fa040 6d 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d might cause sub-
fa050 62 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c bitmaps to be al
fa060 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e located. Failin
fa070 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 g.** to get the
fa080 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f memory needed to
fa090 20 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 hold the sub-bi
fa0a0 74 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 tmap is the only
fa0b0 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 .** that can go
fa0c0 77 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e wrong with an in
fa0d0 73 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 sert, assuming p
fa0e0 20 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 and i are valid
fa0f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
fa100 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 ing function mus
fa110 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 t ensure that p
fa120 69 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 is a valid Bitve
fa130 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 c object.** and
fa140 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 that the value f
fa150 6f 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e or "i" is within
fa160 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 range of the Bi
fa170 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 tvec object..**
fa180 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65 Otherwise the be
fa190 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
fa1a0 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
fa1b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
fa1c0 65 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 e3BitvecSet(Bitv
fa1d0 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 ec *p, u32 i){.
fa1e0 20 75 33 32 20 68 3b 0a 20 20 61 73 73 65 72 74 u32 h;. assert
fa1f0 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
fa200 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73 rt( i>0 );. ass
fa210 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 ert( i<=p->iSize
fa220 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 );. i--;. whi
fa230 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20 3e 20 42 le((p->iSize > B
fa240 49 54 56 45 43 5f 4e 42 49 54 29 20 26 26 20 70 ITVEC_NBIT) && p
fa250 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b 0a 20 20 ->iDivisor) {.
fa260 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d u32 bin = i/p-
fa270 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 >iDivisor;. i
fa280 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 = i%p->iDivisor
fa290 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 ;. if( p->u.a
fa2a0 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a pSub[bin]==0 ){.
fa2b0 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62 p->u.apSub
fa2c0 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42 [bin] = sqlite3B
fa2d0 69 74 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e itvecCreate( p->
fa2e0 69 44 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20 iDivisor );.
fa2f0 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 if( p->u.apSub
fa300 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 [bin]==0 ) retur
fa310 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
fa320 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
fa330 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 >u.apSub[bin];.
fa340 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a }. if( p->iSiz
fa350 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 e<=BITVEC_NBIT )
fa360 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d {. p->u.aBitm
fa370 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c ap[i/BITVEC_SZEL
fa380 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 28 EM] |= 1 << (i&(
fa390 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 BITVEC_SZELEM-1)
fa3a0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
fa3b0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 LITE_OK;. }. h
fa3c0 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 = BITVEC_HASH(i
fa3d0 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 20 74 68 65 ++);. /* if the
fa3e0 72 65 20 77 61 73 6e 27 74 20 61 20 68 61 73 68 re wasn't a hash
fa3f0 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 collision, and
fa400 74 68 69 73 20 64 6f 65 73 6e 27 74 20 2a 2f 0a this doesn't */.
fa410 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 /* completely
fa420 66 69 6c 6c 20 74 68 65 20 68 61 73 68 2c 20 74 fill the hash, t
fa430 68 65 6e 20 6a 75 73 74 20 61 64 64 20 69 74 20 hen just add it
fa440 77 69 74 68 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 without */. /*
fa450 77 6f 72 72 69 6e 67 20 61 62 6f 75 74 20 73 75 worring about su
fa460 62 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 b-dividing and r
fa470 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 e-hashing. */.
fa480 69 66 28 20 21 70 2d 3e 75 2e 61 48 61 73 68 5b if( !p->u.aHash[
fa490 68 5d 20 29 7b 0a 20 20 20 20 69 66 20 28 70 2d h] ){. if (p-
fa4a0 3e 6e 53 65 74 3c 28 42 49 54 56 45 43 5f 4e 49 >nSet<(BITVEC_NI
fa4b0 4e 54 2d 31 29 29 20 7b 0a 20 20 20 20 20 20 67 NT-1)) {. g
fa4c0 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 65 oto bitvec_set_e
fa4d0 6e 64 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b nd;. } else {
fa4e0 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 . goto bitv
fa4f0 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3b 0a 20 ec_set_rehash;.
fa500 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 }. }. /* th
fa510 65 72 65 20 77 61 73 20 61 20 63 6f 6c 6c 69 73 ere was a collis
fa520 69 6f 6e 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 ion, check to se
fa530 65 20 69 66 20 69 74 27 73 20 61 6c 72 65 61 64 e if it's alread
fa540 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e 20 68 61 73 y */. /* in has
fa550 68 2c 20 69 66 20 6e 6f 74 2c 20 74 72 79 20 74 h, if not, try t
fa560 6f 20 66 69 6e 64 20 61 20 73 70 6f 74 20 66 6f o find a spot fo
fa570 72 20 69 74 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 r it */. do {.
fa580 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 if( p->u.aHas
fa590 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e h[h]==i ) return
fa5a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
fa5b0 68 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3e 3d h++;. if( h>=
fa5c0 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 BITVEC_NINT ) h
fa5d0 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 = 0;. } while(
fa5e0 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 3b p->u.aHash[h] );
fa5f0 0a 20 20 2f 2a 20 77 65 20 64 69 64 6e 27 74 20 . /* we didn't
fa600 66 69 6e 64 20 69 74 20 69 6e 20 74 68 65 20 68 find it in the h
fa610 61 73 68 2e 20 20 68 20 70 6f 69 6e 74 73 20 74 ash. h points t
fa620 6f 20 74 68 65 20 66 69 72 73 74 20 2a 2f 0a 20 o the first */.
fa630 20 2f 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 72 /* available fr
fa640 65 65 20 73 70 6f 74 2e 20 63 68 65 63 6b 20 74 ee spot. check t
fa650 6f 20 73 65 65 20 69 66 20 74 68 69 73 20 69 73 o see if this is
fa660 20 67 6f 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f going to */. /
fa670 2a 20 6d 61 6b 65 20 6f 75 72 20 68 61 73 68 20 * make our hash
fa680 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a too "full". */.
fa690 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 bitvec_set_rehas
fa6a0 68 3a 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74 h:. if( p->nSet
fa6b0 3e 3d 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 >=BITVEC_MXHASH
fa6c0 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 ){. unsigned
fa6d0 69 6e 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 72 int j;. int r
fa6e0 63 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c c;. u32 aiVal
fa6f0 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d ues[BITVEC_NINT]
fa700 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 ;. memcpy(aiV
fa710 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 alues, p->u.aHas
fa720 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 h, sizeof(aiValu
fa730 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 es));. memset
fa740 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 (p->u.apSub, 0,
fa750 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 sizeof(aiValues)
fa760 29 3b 0a 20 20 20 20 70 2d 3e 69 44 69 76 69 73 );. p->iDivis
fa770 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b or = (p->iSize +
fa780 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 BITVEC_NPTR - 1
fa790 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 )/BITVEC_NPTR;.
fa7a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
fa7b0 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a itvecSet(p, i);.
fa7c0 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 for(j=0; j<B
fa7d0 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 ITVEC_NINT; j++)
fa7e0 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69 56 61 {. if( aiVa
fa7f0 6c 75 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20 lues[j] ) rc |=
fa800 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
fa810 28 70 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29 (p, aiValues[j])
fa820 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
fa830 72 6e 20 72 63 3b 0a 20 20 7d 0a 62 69 74 76 65 rn rc;. }.bitve
fa840 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20 70 2d 3e c_set_end:. p->
fa850 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e 75 2e 61 nSet++;. p->u.a
fa860 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a 20 20 72 Hash[h] = i;. r
fa870 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
fa880 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
fa890 74 68 65 20 69 2d 74 68 20 62 69 74 2e 0a 2a 2f the i-th bit..*/
fa8a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
fa8b0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 void sqlite3Bitv
fa8c0 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a ecClear(Bitvec *
fa8d0 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 61 73 73 p, u32 i){. ass
fa8e0 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 ert( p!=0 );. a
fa8f0 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 ssert( i>0 );.
fa900 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d i--;. while( p-
fa910 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 >iDivisor ){.
fa920 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e u32 bin = i/p->
fa930 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 iDivisor;. i
fa940 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b = i%p->iDivisor;
fa950 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 . p = p->u.ap
fa960 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 Sub[bin];. if
fa970 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 (!p) {. re
fa980 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a turn;. }. }.
fa990 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d if( p->iSize<=
fa9a0 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 BITVEC_NBIT ){.
fa9b0 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b p->u.aBitmap[
fa9c0 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d i/BITVEC_SZELEM]
fa9d0 20 26 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 &= ~(1 << (i&(B
fa9e0 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 ITVEC_SZELEM-1))
fa9f0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
faa00 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a unsigned int j;.
faa10 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65 73 u32 aiValues
faa20 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a 20 [BITVEC_NINT];.
faa30 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 memcpy(aiValu
faa40 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 es, p->u.aHash,
faa50 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29 sizeof(aiValues)
faa60 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d );. memset(p-
faa70 3e 75 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a >u.aHash, 0, siz
faa80 65 6f 66 28 61 69 56 61 6c 75 65 73 29 29 3b 0a eof(aiValues));.
faa90 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b p->nSet = 0;
faaa0 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c . for(j=0; j<
faab0 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b BITVEC_NINT; j++
faac0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69 56 ){. if( aiV
faad0 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61 69 56 61 alues[j] && aiVa
faae0 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 lues[j]!=(i+1) )
faaf0 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 68 20 {. u32 h
fab00 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 61 69 = BITVEC_HASH(ai
fab10 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 Values[j]-1);.
fab20 20 20 20 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b p->nSet++;
fab30 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
fab40 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b p->u.aHash[h] ){
fab50 0a 20 20 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a . h++;.
fab60 20 20 20 20 20 20 20 20 20 20 69 66 28 20 68 3e if( h>
fab70 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 =BITVEC_NINT ) h
fab80 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 0;. }.
fab90 20 20 20 20 20 20 20 20 70 2d 3e 75 2e 61 48 61 p->u.aHa
faba0 73 68 5b 68 5d 20 3d 20 61 69 56 61 6c 75 65 73 sh[h] = aiValues
fabb0 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 [j];. }.
fabc0 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
fabd0 44 65 73 74 72 6f 79 20 61 20 62 69 74 6d 61 70 Destroy a bitmap
fabe0 20 6f 62 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 object. Reclai
fabf0 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 m all memory use
fac00 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
fac10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
fac20 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 42 3BitvecDestroy(B
fac30 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 itvec *p){. if(
fac40 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a p==0 ) return;.
fac50 20 20 69 66 28 20 70 2d 3e 69 44 69 76 69 73 6f if( p->iDiviso
fac60 72 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 r ){. unsigne
fac70 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 d int i;. for
fac80 28 69 3d 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e (i=0; i<BITVEC_N
fac90 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 PTR; i++){.
faca0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
facb0 73 74 72 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62 stroy(p->u.apSub
facc0 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a [i]);. }. }.
facd0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
face0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
facf0 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 rn the value of
fad00 74 68 65 20 69 53 69 7a 65 20 70 61 72 61 6d 65 the iSize parame
fad10 74 65 72 20 73 70 65 63 69 66 69 65 64 20 77 68 ter specified wh
fad20 65 6e 20 42 69 74 76 65 63 20 2a 70 0a 2a 2a 20 en Bitvec *p.**
fad30 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a was created..*/.
fad40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
fad50 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 32 sqlite3Bitvec
fad60 53 69 7a 65 28 42 69 74 76 65 63 20 2a 70 29 7b Size(Bitvec *p){
fad70 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 53 69 . return p->iSi
fad80 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ze;.}..#ifndef S
fad90 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 QLITE_OMIT_BUILT
fada0 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 IN_TEST./*.** Le
fadb0 74 20 56 5b 5d 20 62 65 20 61 6e 20 61 72 72 61 t V[] be an arra
fadc0 79 20 6f 66 20 75 6e 73 69 67 6e 65 64 20 63 68 y of unsigned ch
fadd0 61 72 61 63 74 65 72 73 20 73 75 66 66 69 63 69 aracters suffici
fade0 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 ent to hold.** u
fadf0 70 20 74 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65 p to N bits. Le
fae00 74 20 49 20 62 65 20 61 6e 20 69 6e 74 65 67 65 t I be an intege
fae10 72 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 r between 0 and
fae20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 N. 0<=I<N..** T
fae30 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e hen the followin
fae40 67 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 g macros can be
fae50 75 73 65 64 20 74 6f 20 73 65 74 2c 20 63 6c 65 used to set, cle
fae60 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69 ar, or test.** i
fae70 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 20 77 ndividual bits w
fae80 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 ithin V..*/.#def
fae90 69 6e 65 20 53 45 54 42 49 54 28 56 2c 49 29 20 ine SETBIT(V,I)
faea0 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 V[I>>3] |=
faeb0 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66 69 (1<<(I&7)).#defi
faec0 6e 65 20 43 4c 45 41 52 42 49 54 28 56 2c 49 29 ne CLEARBIT(V,I)
faed0 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e V[I>>3] &= ~
faee0 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66 69 (1<<(I&7)).#defi
faef0 6e 65 20 54 45 53 54 42 49 54 28 56 2c 49 29 20 ne TESTBIT(V,I)
faf00 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c (V[I>>3]&(1<
faf10 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a <(I&7)))!=0../*.
faf20 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
faf30 72 75 6e 73 20 61 6e 20 65 78 74 65 6e 73 69 76 runs an extensiv
faf40 65 20 74 65 73 74 20 6f 66 20 74 68 65 20 42 69 e test of the Bi
faf50 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a tvec code..**.**
faf60 20 54 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e The input is an
faf70 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67 65 array of intege
faf80 72 73 20 74 68 61 74 20 61 63 74 73 20 61 73 20 rs that acts as
faf90 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 a program.** to
fafa0 74 65 73 74 20 74 68 65 20 42 69 74 76 65 63 2e test the Bitvec.
fafb0 20 20 54 68 65 20 69 6e 74 65 67 65 72 73 20 61 The integers a
fafc0 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f re opcodes follo
fafd0 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20 wed.** by 0, 1,
fafe0 6f 72 20 33 20 6f 70 65 72 61 6e 64 73 2c 20 64 or 3 operands, d
faff0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
fb000 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72 opcode. Another
fb010 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f .** opcode follo
fb020 77 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 ws immediately a
fb030 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 6f 70 fter the last op
fb040 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 erand..**.** The
fb050 72 65 20 61 72 65 20 36 20 6f 70 63 6f 64 65 73 re are 6 opcodes
fb060 20 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 numbered from 0
fb070 20 74 68 72 6f 75 67 68 20 35 2e 20 20 30 20 69 through 5. 0 i
fb080 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20 s the.** "halt"
fb090 6f 70 63 6f 64 65 20 61 6e 64 20 63 61 75 73 65 opcode and cause
fb0a0 73 20 74 68 65 20 74 65 73 74 20 74 6f 20 65 6e s the test to en
fb0b0 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 d..**.** 0
fb0c0 20 20 20 20 20 20 20 48 61 6c 74 20 61 6e 64 20 Halt and
fb0d0 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 return the numbe
fb0e0 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20 r of errors.**
fb0f0 20 20 31 20 4e 20 53 20 58 20 20 20 20 53 65 74 1 N S X Set
fb100 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 6e N bits beginnin
fb110 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e 63 g with S and inc
fb120 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a rementing by X.*
fb130 2a 20 20 20 20 32 20 4e 20 53 20 58 20 20 20 20 * 2 N S X
fb140 43 6c 65 61 72 20 4e 20 62 69 74 73 20 62 65 67 Clear N bits beg
fb150 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e inning with S an
fb160 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 d incrementing b
fb170 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20 y X.** 3 N
fb180 20 20 20 20 20 53 65 74 20 4e 20 72 61 6e 64 6f Set N rando
fb190 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0a mly chosen bits.
fb1a0 2a 2a 20 20 20 20 34 20 4e 20 20 20 20 20 20 20 ** 4 N
fb1b0 20 43 6c 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c Clear N randoml
fb1c0 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a y chosen bits.**
fb1d0 20 20 20 20 35 20 4e 20 53 20 58 20 20 20 20 53 5 N S X S
fb1e0 65 74 20 4e 20 62 69 74 73 20 66 72 6f 6d 20 53 et N bits from S
fb1f0 20 69 6e 63 72 65 6d 65 6e 74 20 58 20 69 6e 20 increment X in
fb200 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 array only, not
fb210 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 in bitvec.**.**
fb220 54 68 65 20 6f 70 63 6f 64 65 73 20 31 20 74 68 The opcodes 1 th
fb230 72 6f 75 67 68 20 34 20 70 65 72 66 6f 72 6d 20 rough 4 perform
fb240 73 65 74 20 61 6e 64 20 63 6c 65 61 72 20 6f 70 set and clear op
fb250 65 72 61 74 69 6f 6e 73 20 61 72 65 20 70 65 72 erations are per
fb260 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 formed.** on bot
fb270 68 20 61 20 42 69 74 76 65 63 20 6f 62 6a 65 63 h a Bitvec objec
fb280 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61 t and on a linea
fb290 72 20 61 72 72 61 79 20 6f 66 20 62 69 74 73 20 r array of bits
fb2a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
fb2b0 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 lloc..** Opcode
fb2c0 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c 5 works on the l
fb2d0 69 6e 65 61 72 20 61 72 72 61 79 20 6f 6e 6c 79 inear array only
fb2e0 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20 42 69 74 , not on the Bit
fb2f0 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 vec..** Opcode 5
fb300 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c 69 is used to deli
fb310 62 65 72 61 74 65 6c 79 20 69 6e 64 75 63 65 20 berately induce
fb320 61 20 66 61 75 6c 74 20 69 6e 20 6f 72 64 65 72 a fault in order
fb330 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 to.** confirm t
fb340 68 61 74 20 65 72 72 6f 72 20 64 65 74 65 63 74 hat error detect
fb350 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a ion works..**.**
fb360 20 41 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 At the conclusi
fb370 6f 6e 20 6f 66 20 74 68 65 20 74 65 73 74 20 74 on of the test t
fb380 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 he linear array
fb390 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 is compared.** a
fb3a0 67 61 69 6e 73 74 20 74 68 65 20 42 69 74 76 65 gainst the Bitve
fb3b0 63 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 74 68 c object. If th
fb3c0 65 72 65 20 61 72 65 20 61 6e 79 20 64 69 66 66 ere are any diff
fb3d0 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 erences,.** an e
fb3e0 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
fb3f0 2e 20 20 49 66 20 74 68 65 79 20 61 72 65 20 74 . If they are t
fb400 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73 he same, zero is
fb410 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
fb420 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c If a memory all
fb430 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 ocation error oc
fb440 63 75 72 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e curs, return -1.
fb450 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fb460 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 TE int sqlite3Bi
fb470 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 tvecBuiltinTest(
fb480 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 int sz, int *aOp
fb490 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 42 69 ){. Bitvec *pBi
fb4a0 74 76 65 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69 tvec = 0;. unsi
fb4b0 67 6e 65 64 20 63 68 61 72 20 2a 70 56 20 3d 20 gned char *pV =
fb4c0 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 0;. int rc = -1
fb4d0 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70 ;. int i, nx, p
fb4e0 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a 20 41 6c 6c c, op;.. /* All
fb4f0 6f 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63 ocate the Bitvec
fb500 20 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e to be tested an
fb510 64 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 d a linear array
fb520 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f of. ** bits to
fb530 20 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65 act as the refe
fb540 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 rence */. pBitv
fb550 65 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 ec = sqlite3Bitv
fb560 65 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a ecCreate( sz );.
fb570 20 20 70 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d pV = sqlite3_m
fb580 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 alloc( (sz+7)/8
fb590 2b 20 31 20 29 3b 0a 20 20 69 66 28 20 70 42 69 + 1 );. if( pBi
fb5a0 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 3d 3d 30 tvec==0 || pV==0
fb5b0 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 ) goto bitvec_e
fb5c0 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 56 2c nd;. memset(pV,
fb5d0 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 0, (sz+7)/8 + 1
fb5e0 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 );.. /* Run the
fb5f0 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63 program */. pc
fb600 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28 = 0;. while( (
fb610 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30 op = aOp[pc])!=0
fb620 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 ){. switch(
fb630 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 op ){. case
fb640 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 1:. case 2
fb650 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20 :. case 5:
fb660 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 34 {. nx = 4
fb670 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61 4f ;. i = aO
fb680 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20 p[pc+2] - 1;.
fb690 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b aOp[pc+2] +
fb6a0 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20 = aOp[pc+3];.
fb6b0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
fb6c0 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 33 }. case 3
fb6d0 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20 :. case 4:
fb6e0 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 . default:
fb6f0 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 32 {. nx = 2
fb700 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
fb710 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 3_randomness(siz
fb720 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20 20 eof(i), &i);.
fb730 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
fb740 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
fb750 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20 ( (--aOp[pc+1])
fb760 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20 > 0 ) nx = 0;.
fb770 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20 pc += nx;.
fb780 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66 66 i = (i & 0x7ffff
fb790 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66 28 fff)%sz;. if(
fb7a0 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b 0a (op & 1)!=0 ){.
fb7b0 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56 2c SETBIT(pV,
fb7c0 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 (i+1));. i
fb7d0 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20 f( op!=5 ){.
fb7e0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 if( sqlite3B
fb7f0 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65 63 itvecSet(pBitvec
fb800 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62 69 , i+1) ) goto bi
fb810 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20 tvec_end;.
fb820 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
fb830 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c 20 CLEARBIT(pV,
fb840 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73 71 (i+1));. sq
fb850 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
fb860 28 70 42 69 74 76 65 63 2c 20 69 2b 31 29 3b 0a (pBitvec, i+1);.
fb870 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
fb880 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 Test to make sur
fb890 65 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 e the linear arr
fb8a0 61 79 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 ay exactly match
fb8b0 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 69 74 76 es the. ** Bitv
fb8c0 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 74 61 72 ec object. Star
fb8d0 74 20 77 69 74 68 20 74 68 65 20 61 73 73 75 6d t with the assum
fb8e0 70 74 69 6f 6e 20 74 68 61 74 20 74 68 65 79 20 ption that they
fb8f0 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20 28 72 do. ** match (r
fb900 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65 20 72 c==0). Change r
fb910 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 c to non-zero if
fb920 20 61 20 64 69 73 63 72 65 70 61 6e 63 79 0a 20 a discrepancy.
fb930 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 ** is found..
fb940 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
fb950 33 42 69 74 76 65 63 54 65 73 74 28 30 2c 30 29 3BitvecTest(0,0)
fb960 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 + sqlite3Bitvec
fb970 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 73 7a Test(pBitvec, sz
fb980 2b 31 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 +1). +
fb990 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
fb9a0 74 28 70 42 69 74 76 65 63 2c 20 30 29 0a 20 20 t(pBitvec, 0).
fb9b0 20 20 20 20 20 20 20 20 2b 20 28 73 71 6c 69 74 + (sqlit
fb9c0 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 69 e3BitvecSize(pBi
fb9d0 74 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20 20 66 tvec) - sz);. f
fb9e0 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 or(i=1; i<=sz; i
fb9f0 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 28 54 ++){. if( (T
fba00 45 53 54 42 49 54 28 70 56 2c 69 29 29 21 3d 73 ESTBIT(pV,i))!=s
fba10 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
fba20 28 70 42 69 74 76 65 63 2c 69 29 20 29 7b 0a 20 (pBitvec,i) ){.
fba30 20 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 20 20 rc = i;.
fba40 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
fba50 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 }.. /* Free a
fba60 6c 6c 6f 63 61 74 65 64 20 73 74 72 75 63 74 75 llocated structu
fba70 72 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64 re */.bitvec_end
fba80 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 :. sqlite3_free
fba90 28 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 (pV);. sqlite3B
fbaa0 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 69 itvecDestroy(pBi
fbab0 74 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 tvec);. return
fbac0 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 rc;.}.#endif /*
fbad0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
fbae0 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a TIN_TEST */../**
fbaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
fbb00 20 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a of bitvec.c ***
fbb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
fbb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
fbb50 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 63 in file pcache.c
fbb60 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
fbb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
fbb90 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 ** 2008 August 0
fbba0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
fbbb0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
fbbc0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
fbbd0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
fbbe0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
fbbf0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
fbc00 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
fbc10 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
fbc20 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
fbc30 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
fbc40 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
fbc50 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
fbc60 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
fbc70 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
fbc80 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
fbc90 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
fbca0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
fbcb0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
fbcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fbd00 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
fbd10 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70 61 plements that pa
fbd20 67 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 ge cache..**.**
fbd30 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 @(#) $Id: pcache
fbd40 2e 63 2c 76 20 31 2e 34 34 20 32 30 30 39 2f 30 .c,v 1.44 2009/0
fbd50 33 2f 33 31 20 30 31 3a 33 32 3a 31 38 20 64 72 3/31 01:32:18 dr
fbd60 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a h Exp $.*/../*.*
fbd70 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 * A complete pag
fbd80 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e e cache is an in
fbd90 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
fbda0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 tructure..*/.str
fbdb0 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 uct PCache {. P
fbdc0 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 gHdr *pDirty, *p
fbdd0 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 DirtyTail;
fbde0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 /* List of di
fbdf0 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 rty pages in LRU
fbe00 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 order */. PgHd
fbe10 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 r *pSynced;
fbe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbe30 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 /* Last synced p
fbe40 61 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 age in dirty pag
fbe50 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e list */. int
fbe60 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
fbe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbe80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 /* Number of ref
fbe90 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f erenced pages */
fbea0 0a 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 . int nMax;
fbeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbec0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 /* Config
fbed0 75 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 ured cache size
fbee0 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b */. int szPage;
fbef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbf00 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
fbf10 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69 of every page i
fbf20 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a n this cache */.
fbf30 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 int szExtra;
fbf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbf50 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
fbf60 20 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 extra space for
fbf70 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 each page */.
fbf80 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 int bPurgeable;
fbf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fbfa0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
fbfb0 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b ages are on back
fbfc0 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 ing store */. i
fbfd0 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f nt (*xStress)(vo
fbfe0 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 id*,PgHdr*);
fbff0 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 /* Call to tr
fc000 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c y make a page cl
fc010 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ean */. void *p
fc020 53 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 Stress;
fc030 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fc040 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 Argument to xStr
fc050 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ess */. sqlite3
fc060 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b _pcache *pCache;
fc070 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fc080 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 Pluggable cache
fc090 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 module */. PgHd
fc0a0 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 r *pPage1;
fc0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fc0c0 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 /* Reference to
fc0d0 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a page 1 */.};../*
fc0e0 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 .** Some of the
fc0f0 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 assert() macros
fc100 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 in this code are
fc110 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 too expensive t
fc120 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 o run.** even du
fc130 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 ring normal debu
fc140 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d gging. Use them
fc150 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 only rarely on
fc160 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 long-running.**
fc170 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 tests. Enable t
fc180 68 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 he expensive ass
fc190 65 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a erts using the.*
fc1a0 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c * -DSQLITE_ENABL
fc1b0 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 E_EXPENSIVE_ASSE
fc1c0 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d RT=1 compile-tim
fc1d0 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 e option..*/.#if
fc1e0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
fc1f0 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 E_EXPENSIVE_ASSE
fc200 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 RT.# define expe
fc210 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 nsive_assert(X)
fc220 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 assert(X).#else
fc230 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 .# define expens
fc240 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 ive_assert(X).#e
fc250 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
fc260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fc270 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 ********* Linked
fc280 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 List Management
fc290 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
fc2a0 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 *****/..#if !def
fc2b0 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 ined(NDEBUG) &&
fc2c0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 defined(SQLITE_E
fc2d0 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f NABLE_EXPENSIVE_
fc2e0 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 ASSERT)./*.** Ch
fc2f0 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 eck that the pCa
fc300 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 che->pSynced var
fc310 69 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 iable is set cor
fc320 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a rectly. If it.**
fc330 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 is not, either
fc340 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f fail an assert o
fc350 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f r return zero. O
fc360 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e therwise, return
fc370 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 .** non-zero. Th
fc380 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 is is only used
fc390 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 in debugging bui
fc3a0 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a lds, as follows:
fc3b0 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 .**.** expensi
fc3c0 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 ve_assert( pcach
fc3d0 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 eCheckSynced(pCa
fc3e0 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 che) );.*/.stati
fc3f0 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 c int pcacheChec
fc400 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a kSynced(PCache *
fc410 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 pCache){. PgHdr
fc420 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 *p;. for(p=pCa
fc430 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b che->pDirtyTail;
fc440 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e p!=pCache->pSyn
fc450 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 ced; p=p->pDirty
fc460 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 Prev){. asser
fc470 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 t( p->nRef || (p
fc480 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
fc490 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a ED_SYNC) );. }.
fc4a0 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c return (p==0 |
fc4b0 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d | p->nRef || (p-
fc4c0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
fc4d0 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 D_SYNC)==0);.}.#
fc4e0 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 endif /* !NDEBUG
fc4f0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
fc500 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 E_EXPENSIVE_ASSE
fc510 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d RT */../*.** Rem
fc520 6f 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66 ove page pPage f
fc530 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 rom the list of
fc540 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a dirty pages..*/.
fc550 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
fc560 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
fc570 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 yList(PgHdr *pPa
fc580 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 ge){. PCache *p
fc590 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 = pPage->pCache
fc5a0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ;.. assert( pPa
fc5b0 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c ge->pDirtyNext |
fc5c0 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 | pPage==p->pDir
fc5d0 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 tyTail );. asse
fc5e0 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 rt( pPage->pDirt
fc5f0 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d yPrev || pPage==
fc600 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 p->pDirty );..
fc610 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43 /* Update the PC
fc620 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 ache1.pSynced va
fc630 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 riable if necess
fc640 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d ary. */. if( p-
fc650 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 >pSynced==pPage
fc660 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 ){. PgHdr *pS
fc670 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 ynced = pPage->p
fc680 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 DirtyPrev;. w
fc690 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 hile( pSynced &&
fc6a0 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 (pSynced->flags
fc6b0 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
fc6c0 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 ) ){. pSync
fc6d0 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 ed = pSynced->pD
fc6e0 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a irtyPrev;. }.
fc6f0 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d p->pSynced =
fc700 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 pSynced;. }..
fc710 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 if( pPage->pDir
fc720 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 tyNext ){. pP
fc730 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d age->pDirtyNext-
fc740 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 >pDirtyPrev = pP
fc750 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b age->pDirtyPrev;
fc760 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
fc770 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e sert( pPage==p->
fc780 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 pDirtyTail );.
fc790 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 p->pDirtyTail
fc7a0 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 = pPage->pDirtyP
fc7b0 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 rev;. }. if( p
fc7c0 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 Page->pDirtyPrev
fc7d0 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 ){. pPage->p
fc7e0 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 DirtyPrev->pDirt
fc7f0 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 yNext = pPage->p
fc800 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c DirtyNext;. }el
fc810 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
fc820 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 pPage==p->pDirty
fc830 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 );. p->pDirt
fc840 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 y = pPage->pDirt
fc850 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 yNext;. }. pPa
fc860 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d ge->pDirtyNext =
fc870 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 0;. pPage->pDi
fc880 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 rtyPrev = 0;..
fc890 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 expensive_assert
fc8a0 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e ( pcacheCheckSyn
fc8b0 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a ced(p) );.}../*.
fc8c0 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67 ** Add page pPag
fc8d0 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 e to the head of
fc8e0 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 the dirty list
fc8f0 28 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 (PCache1.pDirty
fc900 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 is set to.** pPa
fc910 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ge)..*/.static v
fc920 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 oid pcacheAddToD
fc930 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a irtyList(PgHdr *
fc940 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 pPage){. PCache
fc950 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 *p = pPage->pCa
fc960 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 che;.. assert(
fc970 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
fc980 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 t==0 && pPage->p
fc990 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 DirtyPrev==0 &&
fc9a0 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 p->pDirty!=pPage
fc9b0 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 );.. pPage->pD
fc9c0 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 irtyNext = p->pD
fc9d0 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 irty;. if( pPag
fc9e0 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b e->pDirtyNext ){
fc9f0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
fca00 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e ge->pDirtyNext->
fca10 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b pDirtyPrev==0 );
fca20 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 . pPage->pDir
fca30 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 tyNext->pDirtyPr
fca40 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a ev = pPage;. }.
fca50 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 p->pDirty = pP
fca60 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 age;. if( !p->p
fca70 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 DirtyTail ){.
fca80 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d p->pDirtyTail =
fca90 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 pPage;. }. if
fcaa0 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 ( !p->pSynced &&
fcab0 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 0==(pPage->flag
fcac0 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
fcad0 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 C) ){. p->pSy
fcae0 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 nced = pPage;.
fcaf0 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 }. expensive_as
fcb00 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 sert( pcacheChec
fcb10 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a kSynced(p) );.}.
fcb20 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 ./*.** Wrapper a
fcb30 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 round the plugga
fcb40 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 ble caches xUnpi
fcb50 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 n method. If the
fcb60 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 cache is.** bei
fcb70 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 ng used for an i
fcb80 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
fcb90 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e e, this function
fcba0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
fcbb0 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
fcbc0 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 heUnpin(PgHdr *p
fcbd0 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 ){. PCache *pCa
fcbe0 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
fcbf0 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 . if( pCache->b
fcc00 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 Purgeable ){.
fcc10 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 if( p->pgno==1
fcc20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d ){. pCache-
fcc30 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 >pPage1 = 0;.
fcc40 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c }. sqlite3Gl
fcc50 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
fcc60 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d e.xUnpin(pCache-
fcc70 3e 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a >pCache, p, 0);.
fcc80 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a }.}../********
fcc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fcca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fccb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 *********** Gene
fccc0 72 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a ral Interfaces *
fccd0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 *****.**.** Init
fcce0 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 ialize and shutd
fccf0 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 own the page cac
fcd00 68 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 he subsystem. Ne
fcd10 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a ither of these .
fcd20 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 ** functions are
fcd30 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a threadsafe..*/.
fcd40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
fcd50 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
fcd60 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 Initialize(void)
fcd70 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 {. if( sqlite3G
fcd80 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
fcd90 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 he.xInit==0 ){.
fcda0 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 sqlite3PCache
fcdb0 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 SetDefault();.
fcdc0 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
fcdd0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
fcde0 63 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 cache.xInit(sqli
fcdf0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
fce00 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a pcache.pArg);.}.
fce10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
fce20 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
fce30 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b eShutdown(void){
fce40 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
fce50 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
fce60 65 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 e.xShutdown ){.
fce70 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
fce80 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 Config.pcache.xS
fce90 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 hutdown(sqlite3G
fcea0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
fceb0 68 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a he.pArg);. }.}.
fcec0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
fced0 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
fcee0 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 of a PCache obje
fcef0 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ct..*/.SQLITE_PR
fcf00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
fcf10 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 3PcacheSize(void
fcf20 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 ){ return sizeof
fcf30 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a (PCache); }../*.
fcf40 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
fcf50 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 PCache object. S
fcf60 74 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 torage space to
fcf70 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a hold the object.
fcf80 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ** has already b
fcf90 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e een allocated an
fcfa0 64 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 d is passed in a
fcfb0 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e s the p pointer.
fcfc0 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 .** The caller
fcfd0 64 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 discovers how mu
fcfe0 63 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 ch space needs t
fcff0 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 o be allocated b
fd000 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 y .** calling sq
fd010 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 lite3PcacheSize(
fd020 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
fd030 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
fd040 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 3PcacheOpen(. i
fd050 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 nt szPage,
fd060 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
fd070 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 ize of every pag
fd080 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 e */. int szExt
fd090 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ra,
fd0a0 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 /* Extra spa
fd0b0 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ce associated wi
fd0c0 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a th each page */.
fd0d0 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 int bPurgeable
fd0e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
fd0f0 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 * True if pages
fd100 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 are on backing s
fd110 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a tore */. int (*
fd120 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 xStress)(void*,P
fd130 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 gHdr*),/* Call t
fd140 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 o try to make pa
fd150 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 ges clean */. v
fd160 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 oid *pStress,
fd170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
fd180 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 rgument to xStre
fd190 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a ss */. PCache *
fd1a0 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
fd1b0 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 /* Prealloc
fd1c0 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 ated space for t
fd1d0 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a he PCache */.){.
fd1e0 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 memset(p, 0, s
fd1f0 69 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a izeof(PCache));.
fd200 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a p->szPage = sz
fd210 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 Page;. p->szExt
fd220 72 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 ra = szExtra;.
fd230 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 p->bPurgeable =
fd240 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d bPurgeable;. p-
fd250 3e 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 >xStress = xStre
fd260 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 ss;. p->pStress
fd270 20 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d = pStress;. p-
fd280 3e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a >nMax = 100;.}..
fd290 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
fd2a0 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 page size for P
fd2b0 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 Cache object. Th
fd2c0 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e e caller must en
fd2d0 73 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a sure that there.
fd2e0 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 ** are no outsta
fd2f0 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 nding page refer
fd300 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 ences when this
fd310 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
fd320 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
fd330 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
fd340 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 e3PcacheSetPageS
fd350 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 ize(PCache *pCac
fd360 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b he, int szPage){
fd370 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 . assert( pCach
fd380 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 e->nRef==0 && pC
fd390 61 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 ache->pDirty==0
fd3a0 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d );. if( pCache-
fd3b0 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 >pCache ){. s
fd3c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
fd3d0 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 ig.pcache.xDestr
fd3e0 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 oy(pCache->pCach
fd3f0 65 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e e);. pCache->
fd400 70 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a pCache = 0;. }.
fd410 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 pCache->szPage
fd420 20 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a = szPage;.}../*
fd430 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 .** Try to obtai
fd440 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 n a page from th
fd450 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 e cache..*/.SQLI
fd460 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
fd470 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 qlite3PcacheFetc
fd480 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 h(. PCache *pCa
fd490 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 che, /* Ob
fd4a0 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 tain the page fr
fd4b0 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f om this cache */
fd4c0 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 . Pgno pgno,
fd4d0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
fd4e0 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 number to obtai
fd4f0 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 n */. int creat
fd500 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 eFlag, /*
fd510 49 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 If true, create
fd520 70 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 page if it does
fd530 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 not exist alread
fd540 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 y */. PgHdr **p
fd550 70 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 pPage /*
fd560 57 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68 Write the page h
fd570 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 ere */.){. PgHd
fd580 72 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 r *pPage = 0;.
fd590 69 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 int eCreate;..
fd5a0 61 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d assert( pCache!=
fd5b0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
fd5c0 67 6e 6f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 gno>0 );.. /* I
fd5d0 66 20 74 68 65 20 70 6c 75 67 67 61 62 6c 65 20 f the pluggable
fd5e0 63 61 63 68 65 20 28 73 71 6c 69 74 65 33 5f 70 cache (sqlite3_p
fd5f0 63 61 63 68 65 2a 29 20 68 61 73 20 6e 6f 74 20 cache*) has not
fd600 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 0a been allocated,.
fd610 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 69 74 ** allocate it
fd620 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 now.. */. if(
fd630 20 21 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 !pCache->pCache
fd640 20 26 26 20 63 72 65 61 74 65 46 6c 61 67 20 29 && createFlag )
fd650 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 70 63 {. sqlite3_pc
fd660 61 63 68 65 20 2a 70 3b 0a 20 20 20 20 69 6e 74 ache *p;. int
fd670 20 6e 42 79 74 65 3b 0a 20 20 20 20 6e 42 79 74 nByte;. nByt
fd680 65 20 3d 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 e = pCache->szPa
fd690 67 65 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 ge + pCache->szE
fd6a0 78 74 72 61 20 2b 20 73 69 7a 65 6f 66 28 50 67 xtra + sizeof(Pg
fd6b0 48 64 72 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 Hdr);. p = sq
fd6c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
fd6d0 67 2e 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 g.pcache.xCreate
fd6e0 28 6e 42 79 74 65 2c 20 70 43 61 63 68 65 2d 3e (nByte, pCache->
fd6f0 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 bPurgeable);.
fd700 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 if( !p ){.
fd710 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
fd720 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OMEM;. }.
fd730 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
fd740 66 69 67 2e 70 63 61 63 68 65 2e 78 43 61 63 68 fig.pcache.xCach
fd750 65 73 69 7a 65 28 70 2c 20 70 43 61 63 68 65 2d esize(p, pCache-
fd760 3e 6e 4d 61 78 29 3b 0a 20 20 20 20 70 43 61 63 >nMax);. pCac
fd770 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 3b 0a he->pCache = p;.
fd780 20 20 7d 0a 0a 20 20 65 43 72 65 61 74 65 20 3d }.. eCreate =
fd790 20 63 72 65 61 74 65 46 6c 61 67 20 3f 20 31 20 createFlag ? 1
fd7a0 3a 20 30 3b 0a 20 20 69 66 28 20 65 43 72 65 61 : 0;. if( eCrea
fd7b0 74 65 20 26 26 20 28 21 70 43 61 63 68 65 2d 3e te && (!pCache->
fd7c0 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 21 70 bPurgeable || !p
fd7d0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 20 29 Cache->pDirty) )
fd7e0 7b 0a 20 20 20 20 65 43 72 65 61 74 65 20 3d 20 {. eCreate =
fd7f0 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 61 2;. }. if( pCa
fd800 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 che->pCache ){.
fd810 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74 pPage = sqlit
fd820 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
fd830 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61 cache.xFetch(pCa
fd840 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e che->pCache, pgn
fd850 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 20 7d o, eCreate);. }
fd860 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 26 .. if( !pPage &
fd870 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 7b 0a & eCreate==1 ){.
fd880 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a PgHdr *pPg;.
fd890 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 20 64 . /* Find a d
fd8a0 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 72 69 irty page to wri
fd8b0 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 79 63 te-out and recyc
fd8c0 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 74 6f le. First try to
fd8d0 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a 2a 20 find a . **
fd8e0 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 6e page that does n
fd8f0 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 ot require a jou
fd900 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 20 77 rnal-sync (one w
fd910 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 ith PGHDR_NEED_S
fd920 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 61 72 YNC. ** clear
fd930 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 61 74 ed), but if that
fd940 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
fd950 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e 79 20 settle for any
fd960 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 75 6e other . ** un
fd970 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 74 79 referenced dirty
fd980 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
fd990 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
fd9a0 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
fd9b0 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b ynced(pCache) );
fd9c0 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 61 . for(pPg=pCa
fd9d0 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 0a 20 che->pSynced; .
fd9e0 20 20 20 20 20 20 20 70 50 67 20 26 26 20 28 70 pPg && (p
fd9f0 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 50 67 Pg->nRef || (pPg
fda00 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
fda10 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 20 20 ED_SYNC)); .
fda20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 pPg=pPg->pDi
fda30 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b 0a 20 rtyPrev. );.
fda40 20 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a 20 if( !pPg ){.
fda50 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 61 for(pPg=pCa
fda60 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b che->pDirtyTail;
fda70 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52 65 pPg && pPg->nRe
fda80 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 72 f; pPg=pPg->pDir
fda90 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a 20 tyPrev);. }.
fdaa0 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 if( pPg ){.
fdab0 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 int rc;.
fdac0 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e 78 rc = pCache->x
fdad0 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e 70 Stress(pCache->p
fdae0 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20 20 Stress, pPg);.
fdaf0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
fdb00 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c TE_OK && rc!=SQL
fdb10 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
fdb20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
fdb30 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
fdb40 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74 65 pPage = sqlite
fdb50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
fdb60 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61 63 ache.xFetch(pCac
fdb70 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f he->pCache, pgno
fdb80 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 , 2);. }.. if(
fdb90 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69 66 pPage ){. if
fdba0 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 61 20 ( !pPage->pData
fdbb0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 ){. memset(
fdbc0 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f 66 pPage, 0, sizeof
fdbd0 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 68 65 (PgHdr) + pCache
fdbe0 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 ->szExtra);.
fdbf0 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 61 20 pPage->pExtra
fdc00 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 65 5b = (void*)&pPage[
fdc10 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 1];. pPage-
fdc20 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 20 2a >pData = (void *
fdc30 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 67 65 )&((char *)pPage
fdc40 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 29 20 )[sizeof(PgHdr)
fdc50 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 + pCache->szExtr
fdc60 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d a];. pPage-
fdc70 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 >pCache = pCache
fdc80 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 ;. pPage->p
fdc90 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 gno = pgno;.
fdca0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 }. assert( pP
fdcb0 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 age->pCache==pCa
fdcc0 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 che );. asser
fdcd0 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d t( pPage->pgno==
fdce0 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 73 65 pgno );. asse
fdcf0 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 74 72 rt( pPage->pExtr
fdd00 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 61 67 a==(void *)&pPag
fdd10 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 66 28 e[1] );.. if(
fdd20 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 66 20 0==pPage->nRef
fdd30 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d ){. pCache-
fdd40 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 >nRef++;. }.
fdd50 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 2b 2b pPage->nRef++
fdd60 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d ;. if( pgno==
fdd70 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 1 ){. pCach
fdd80 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 67 e->pPage1 = pPag
fdd90 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a e;. }. }. *
fdda0 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a ppPage = pPage;.
fddb0 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 3d return (pPage=
fddc0 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 20 3f =0 && eCreate) ?
fddd0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20 SQLITE_NOMEM :
fdde0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
fddf0 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 .** Decrement th
fde00 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
fde10 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66 20 t on a page. If
fde20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65 61 the page is clea
fde30 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65 66 n and the.** ref
fde40 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 72 6f erence count dro
fde50 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 69 74 ps to 0, then it
fde60 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c 65 20 is made elible
fde70 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a 2a for recycling..*
fde80 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
fde90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
fdea0 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 72 cheRelease(PgHdr
fdeb0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
fdec0 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 70 p->nRef>0 );. p
fded0 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 ->nRef--;. if(
fdee0 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 p->nRef==0 ){.
fdef0 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 PCache *pCache
fdf00 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 = p->pCache;.
fdf10 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d 2d pCache->nRef--
fdf20 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e 66 6c ;. if( (p->fl
fdf30 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 ags&PGHDR_DIRTY)
fdf40 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 61 ==0 ){. pca
fdf50 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 20 20 cheUnpin(p);.
fdf60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a }else{. /*
fdf70 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 74 Move the page t
fdf80 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 o the head of th
fdf90 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 2a 2f e dirty list. */
fdfa0 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 65 6d . pcacheRem
fdfb0 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 oveFromDirtyList
fdfc0 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 (p);. pcach
fdfd0 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 eAddToDirtyList(
fdfe0 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a p);. }. }.}.
fdff0 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 65 20 ./*.** Increase
fe000 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
fe010 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c 69 65 unt of a supplie
fe020 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a 2f 0a d page by 1..*/.
fe030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
fe040 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
fe050 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 7b 0a eRef(PgHdr *p){.
fe060 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 65 66 assert(p->nRef
fe070 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b >0);. p->nRef++
fe080 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 ;.}../*.** Drop
fe090 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
fe0a0 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d 75 73 cache. There mus
fe0b0 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 t be exactly one
fe0c0 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 reference to th
fe0d0 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 73 20 e.** page. This
fe0e0 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 function deletes
fe0f0 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 2c that reference,
fe100 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 65 74 so after it ret
fe110 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 urns the.** page
fe120 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
fe130 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a is invalid..*/.
fe140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
fe150 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
fe160 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 29 7b eDrop(PgHdr *p){
fe170 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 . PCache *pCach
fe180 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e e;. assert( p->
fe190 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28 nRef==1 );. if(
fe1a0 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f p->flags&PGHDR_
fe1b0 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 63 61 DIRTY ){. pca
fe1c0 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 cheRemoveFromDir
fe1d0 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 20 tyList(p);. }.
fe1e0 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 pCache = p->pCa
fe1f0 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e 6e che;. pCache->n
fe200 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e Ref--;. if( p->
fe210 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 70 pgno==1 ){. p
fe220 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 Cache->pPage1 =
fe230 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 0;. }. sqlite3
fe240 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
fe250 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 che.xUnpin(pCach
fe260 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 31 29 e->pCache, p, 1)
fe270 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
fe280 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73 sure the page is
fe290 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 marked as dirty
fe2a0 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 64 69 . If it isn't di
fe2b0 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 rty already,.**
fe2c0 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 make it so..*/.S
fe2d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
fe2e0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
fe2f0 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 20 MakeDirty(PgHdr
fe300 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 *p){. p->flags
fe310 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 5f 57 &= ~PGHDR_DONT_W
fe320 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 28 20 RITE;. assert(
fe330 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 69 p->nRef>0 );. i
fe340 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 73 20 f( 0==(p->flags
fe350 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 29 & PGHDR_DIRTY) )
fe360 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c {. p->flags |
fe370 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 20 = PGHDR_DIRTY;.
fe380 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 pcacheAddToDi
fe390 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20 7d rtyList( p);. }
fe3a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 .}../*.** Make s
fe3b0 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73 20 ure the page is
fe3c0 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e 2e marked as clean.
fe3d0 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c 65 If it isn't cle
fe3e0 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d an already,.** m
fe3f0 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 ake it so..*/.SQ
fe400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
fe410 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d d sqlite3PcacheM
fe420 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a akeClean(PgHdr *
fe430 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c p){. if( (p->fl
fe440 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 ags & PGHDR_DIRT
fe450 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 Y) ){. pcache
fe460 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c RemoveFromDirtyL
fe470 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 ist(p);. p->f
fe480 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 5f lags &= ~(PGHDR_
fe490 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45 44 DIRTY|PGHDR_NEED
fe4a0 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 20 _SYNC);. if(
fe4b0 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 p->nRef==0 ){.
fe4c0 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 pcacheUnpin(
fe4d0 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a p);. }. }.}.
fe4e0 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65 72 ./*.** Make ever
fe4f0 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 y page in the ca
fe500 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 che clean..*/.SQ
fe510 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
fe520 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 d sqlite3PcacheC
fe530 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20 2a leanAll(PCache *
fe540 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 pCache){. PgHdr
fe550 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 *p;. while( (p
fe560 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 = pCache->pDirt
fe570 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c y)!=0 ){. sql
fe580 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c ite3PcacheMakeCl
fe590 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ean(p);. }.}../
fe5a0 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 50 *.** Clear the P
fe5b0 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
fe5c0 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 72 lag from all dir
fe5d0 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c ty pages..*/.SQL
fe5e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
fe5f0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c sqlite3PcacheCl
fe600 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 earSyncFlags(PCa
fe610 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
fe620 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 PgHdr *p;. for(
fe630 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 p=pCache->pDirty
fe640 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 ; p; p=p->pDirty
fe650 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 6c Next){. p->fl
fe660 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 ags &= ~PGHDR_NE
fe670 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 ED_SYNC;. }. p
fe680 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 3d Cache->pSynced =
fe690 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 pCache->pDirtyT
fe6a0 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ail;.}../*.** Ch
fe6b0 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e 75 ange the page nu
fe6c0 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20 74 mber of page p t
fe6d0 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53 o newPgno. .*/.S
fe6e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
fe6f0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
fe700 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 50 Move(PgHdr *p, P
fe710 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 gno newPgno){.
fe720 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d PCache *pCache =
fe730 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 73 p->pCache;. as
fe740 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 sert( p->nRef>0
fe750 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 77 );. assert( new
fe760 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c 69 Pgno>0 );. sqli
fe770 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
fe780 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70 43 pcache.xRekey(pC
fe790 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c ache->pCache, p,
fe7a0 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67 6e p->pgno, newPgn
fe7b0 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d 20 o);. p->pgno =
fe7c0 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20 28 newPgno;. if( (
fe7d0 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 p->flags&PGHDR_D
fe7e0 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c 61 IRTY) && (p->fla
fe7f0 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
fe800 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 NC) ){. pcach
fe810 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 eRemoveFromDirty
fe820 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63 61 List(p);. pca
fe830 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 cheAddToDirtyLis
fe840 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a t(p);. }.}../*.
fe850 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 61 ** Drop every ca
fe860 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 20 che entry whose
fe870 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 page number is g
fe880 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 6e reater than "pgn
fe890 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 65 o". The.** calle
fe8a0 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 r must ensure th
fe8b0 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 at there are no
fe8c0 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 outstanding refe
fe8d0 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 rences to any pa
fe8e0 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 ges.** other tha
fe8f0 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61 20 n page 1 with a
fe900 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 61 page number grea
fe910 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a ter than pgno..*
fe920 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 *.** If there is
fe930 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
fe940 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20 70 page 1 and the p
fe950 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70 61 gno parameter pa
fe960 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 ssed to this.**
fe970 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20 74 function is 0, t
fe980 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72 65 hen the data are
fe990 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
fe9a0 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72 6f h page 1 is zero
fe9b0 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 70 ed, but.** the p
fe9c0 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f age object is no
fe9d0 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 t dropped..*/.SQ
fe9e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
fe9f0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 d sqlite3PcacheT
fea00 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20 2a runcate(PCache *
fea10 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67 6e pCache, Pgno pgn
fea20 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 o){. if( pCache
fea30 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 ->pCache ){.
fea40 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 67 PgHdr *p;. Pg
fea50 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20 20 Hdr *pNext;.
fea60 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 for(p=pCache->pD
fea70 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78 74 irty; p; p=pNext
fea80 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d ){. pNext =
fea90 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a p->pDirtyNext;.
feaa0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e if( p->pgn
feab0 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 o>pgno ){.
feac0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 assert( p->fla
fead0 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 gs&PGHDR_DIRTY )
feae0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
feaf0 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
feb00 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 (p);. }.
feb10 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d }. if( pgno=
feb20 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 50 =0 && pCache->pP
feb30 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 age1 ){. me
feb40 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50 61 mset(pCache->pPa
feb50 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 ge1->pData, 0, p
feb60 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b 0a Cache->szPage);.
feb70 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b 0a pgno = 1;.
feb80 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
feb90 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
feba0 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28 70 ache.xTruncate(p
febb0 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
febc0 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f gno+1);. }.}../
febd0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61 63 *.** Close a cac
febe0 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 he..*/.SQLITE_PR
febf0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
fec00 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43 e3PcacheClose(PC
fec10 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
fec20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 if( pCache->pCa
fec30 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 che ){. sqlit
fec40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
fec50 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28 70 cache.xDestroy(p
fec60 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a Cache->pCache);.
fec70 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69 }.}../* .** Di
fec80 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e scard the conten
fec90 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 2e ts of the cache.
feca0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
fecb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
fecc0 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68 cacheClear(PCach
fecd0 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 71 e *pCache){. sq
fece0 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 lite3PcacheTrunc
fecf0 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a ate(pCache, 0);.
fed00 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 }../*.** Merge t
fed10 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 67 65 wo lists of page
fed20 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 s connected by p
fed30 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 67 6e Dirty and in pgn
fed40 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f 20 6e o order..** Do n
fed50 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 20 74 ot both fixing t
fed60 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70 6f he pDirtyPrev po
fed70 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 inters..*/.stati
fed80 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65 4d c PgHdr *pcacheM
fed90 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 50 67 ergeDirtyList(Pg
feda0 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20 2a Hdr *pA, PgHdr *
fedb0 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65 73 pB){. PgHdr res
fedc0 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 70 ult, *pTail;. p
fedd0 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b 0a Tail = &result;.
fede0 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70 while( pA && p
fedf0 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 2d B ){. if( pA-
fee00 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20 29 >pgno<pB->pgno )
fee10 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 {. pTail->p
fee20 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20 20 Dirty = pA;.
fee30 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20 20 pTail = pA;.
fee40 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44 69 pA = pA->pDi
fee50 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a rty;. }else{.
fee60 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 pTail->pDi
fee70 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 rty = pB;.
fee80 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20 20 pTail = pB;.
fee90 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72 74 pB = pB->pDirt
feea0 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 y;. }. }. i
feeb0 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54 61 f( pA ){. pTa
feec0 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b il->pDirty = pA;
feed0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 20 . }else if( pB
feee0 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 ){. pTail->pD
feef0 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65 6c irty = pB;. }el
fef00 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 se{. pTail->p
fef10 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 Dirty = 0;. }.
fef20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e 70 return result.p
fef30 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Dirty;.}../*.**
fef40 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f 66 Sort the list of
fef50 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 pages in accend
fef60 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67 6e ing order by pgn
fef70 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a 2a o. Pages are.**
fef80 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 44 connected by pD
fef90 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20 20 irty pointers.
fefa0 54 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70 The pDirtyPrev p
fefb0 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20 63 ointers are.** c
fefc0 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69 73 orrupted by this
fefd0 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e sort..*/.#defin
fefe0 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f e N_SORT_BUCKET_
feff0 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69 6e 65 ALLOC 25.#define
ff000 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20 20 N_SORT_BUCKET
ff010 20 20 20 20 20 32 35 0a 23 69 66 64 65 66 20 53 25.#ifdef S
ff020 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 QLITE_TEST. int
ff030 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e sqlite3_pager_n
ff040 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d 20 30 _sort_bucket = 0
ff050 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53 4f 52 ;. #undef N_SOR
ff060 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65 66 69 T_BUCKET. #defi
ff070 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 ne N_SORT_BUCKET
ff080 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33 5f 70 \. (sqlite3_p
ff090 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b ager_n_sort_buck
ff0a0 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67 65 72 et?sqlite3_pager
ff0b0 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 3a 4e _n_sort_bucket:N
ff0c0 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 4c 4c _SORT_BUCKET_ALL
ff0d0 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61 74 69 OC).#endif.stati
ff0e0 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65 53 c PgHdr *pcacheS
ff0f0 6f 72 74 44 69 72 74 79 4c 69 73 74 28 50 67 48 ortDirtyList(PgH
ff100 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 67 48 64 dr *pIn){. PgHd
ff110 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 43 4b r *a[N_SORT_BUCK
ff120 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a 70 3b 0a 20 ET_ALLOC], *p;.
ff130 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 int i;. memset
ff140 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 (a, 0, sizeof(a)
ff150 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 );. while( pIn
ff160 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a ){. p = pIn;.
ff170 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 pIn = p->pDi
ff180 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 rty;. p->pDir
ff190 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 ty = 0;. for(
ff1a0 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 i=0; i<N_SORT_BU
ff1b0 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 CKET-1; i++){.
ff1c0 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d 30 20 if( a[i]==0
ff1d0 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 ){. a[i]
ff1e0 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 72 65 = p;. bre
ff1f0 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ak;. }else{
ff200 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 63 61 . p = pca
ff210 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
ff220 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20 t(a[i], p);.
ff230 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a 20 20 a[i] = 0;.
ff240 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
ff250 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 if( i==N_SORT_BU
ff260 43 4b 45 54 2d 31 20 29 7b 0a 20 20 20 20 20 20 CKET-1 ){.
ff270 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20 54 6f 20 /* Coverage: To
ff280 67 65 74 20 68 65 72 65 2c 20 74 68 65 72 65 20 get here, there
ff290 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28 4e 5f need to be 2^(N_
ff2a0 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 0a 20 20 SORT_BUCKET) .
ff2b0 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 ** elements
ff2c0 69 6e 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 in the input lis
ff2d0 74 2e 20 54 68 69 73 20 69 73 20 70 6f 73 73 69 t. This is possi
ff2e0 62 6c 65 2c 20 62 75 74 20 69 6d 70 72 61 63 74 ble, but impract
ff2f0 69 63 61 6c 2e 0a 20 20 20 20 20 20 2a 2a 20 54 ical.. ** T
ff300 65 73 74 69 6e 67 20 74 68 69 73 20 6c 69 6e 65 esting this line
ff310 20 69 73 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 is the point of
ff320 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
ff330 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 . ** sqlite
ff340 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 3_pager_n_sort_b
ff350 75 63 6b 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a ucket.. */.
ff360 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 63 61 a[i] = pca
ff370 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
ff380 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20 t(a[i], p);.
ff390 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30 5d }. }. p = a[0]
ff3a0 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e ;. for(i=1; i<N
ff3b0 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69 2b _SORT_BUCKET; i+
ff3c0 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 61 63 +){. p = pcac
ff3d0 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 heMergeDirtyList
ff3e0 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d 0a 20 (p, a[i]);. }.
ff3f0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
ff400 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 73 .** Return a lis
ff410 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 t of all dirty p
ff420 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 ages in the cach
ff430 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 e, sorted by pag
ff440 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 51 4c e number..*/.SQL
ff450 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 ITE_PRIVATE PgHd
ff460 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 r *sqlite3Pcache
ff470 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 DirtyList(PCache
ff480 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
ff490 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 dr *p;. for(p=p
ff4a0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 Cache->pDirty; p
ff4b0 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78 ; p=p->pDirtyNex
ff4c0 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 t){. p->pDirt
ff4d0 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 y = p->pDirtyNex
ff4e0 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 t;. }. return
ff4f0 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c pcacheSortDirtyL
ff500 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69 72 ist(pCache->pDir
ff510 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 ty);.}../* .** R
ff520 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 eturn the total
ff530 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 number of refere
ff540 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c 64 20 nced pages held
ff550 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f by the cache..*/
ff560 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ff570 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
ff580 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 eRefCount(PCache
ff590 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 *pCache){. ret
ff5a0 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 urn pCache->nRef
ff5b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
ff5c0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
ff5d0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
ff5e0 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64 20 e page supplied
ff5f0 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a as an argument..
ff600 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
ff610 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
ff620 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 chePageRefcount(
ff630 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72 65 74 PgHdr *p){. ret
ff640 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a urn p->nRef;.}..
ff650 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 /* .** Return th
ff660 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
ff670 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 f pages in the c
ff680 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
ff690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ff6a0 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 te3PcachePagecou
ff6b0 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 nt(PCache *pCach
ff6c0 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 20 e){. int nPage
ff6d0 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61 63 68 = 0;. if( pCach
ff6e0 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 e->pCache ){.
ff6f0 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 nPage = sqlite3
ff700 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
ff710 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28 70 che.xPagecount(p
ff720 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a Cache->pCache);.
ff730 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50 61 }. return nPa
ff740 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 ge;.}..#ifdef SQ
ff750 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
ff760 47 65 74 20 74 68 65 20 73 75 67 67 65 73 74 65 Get the suggeste
ff770 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c d cache-size val
ff780 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ue..*/.SQLITE_PR
ff790 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ff7a0 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73 3PcacheGetCaches
ff7b0 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 ize(PCache *pCac
ff7c0 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 he){. return pC
ff7d0 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a 23 65 ache->nMax;.}.#e
ff7e0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 ndif../*.** Set
ff7f0 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 the suggested ca
ff800 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a che-size value..
ff810 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
ff820 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
ff830 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 acheSetCachesize
ff840 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c (PCache *pCache,
ff850 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 int mxPage){.
ff860 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 6d pCache->nMax = m
ff870 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70 43 61 xPage;. if( pCa
ff880 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 che->pCache ){.
ff890 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
ff8a0 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43 Config.pcache.xC
ff8b0 61 63 68 65 73 69 7a 65 28 70 43 61 63 68 65 2d achesize(pCache-
ff8c0 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29 >pCache, mxPage)
ff8d0 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 ;. }.}..#ifdef
ff8e0 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
ff8f0 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c ES./*.** For all
ff900 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 dirty pages cur
ff910 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61 rently in the ca
ff920 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 che, invoke the
ff930 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c specified.** cal
ff940 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f lback. This is o
ff950 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20 nly used if the
ff960 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
ff970 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64 ES macro is.** d
ff980 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
ff990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ff9a0 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 qlite3PcacheIter
ff9b0 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20 ateDirty(PCache
ff9c0 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a *pCache, void (*
ff9d0 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 xIter)(PgHdr *))
ff9e0 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 {. PgHdr *pDirt
ff9f0 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d y;. for(pDirty=
ffa00 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 pCache->pDirty;
ffa10 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70 pDirty; pDirty=p
ffa20 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78 Dirty->pDirtyNex
ffa30 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44 t){. xIter(pD
ffa40 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e irty);. }.}.#en
ffa50 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
ffa60 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 **** End of pcac
ffa70 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a he.c ***********
ffa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffaa0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
ffab0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
ffac0 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a pcache1.c ******
ffad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffaf0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
ffb00 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a November 05.**.*
ffb10 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
ffb20 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
ffb30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
ffb40 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
ffb50 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
ffb60 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
ffb70 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
ffb80 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
ffb90 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
ffba0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
ffbb0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
ffbc0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
ffbd0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
ffbe0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
ffbf0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
ffc00 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
ffc10 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
ffc20 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
ffc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ffc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
ffc70 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
ffc80 6d 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c ments the defaul
ffc90 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 t page cache imp
ffca0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65 lementation (the
ffcb0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 .** sqlite3_pcac
ffcc0 68 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49 he interface). I
ffcd0 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 t also contains
ffce0 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c part of the impl
ffcf0 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 ementation.** of
ffd00 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 the SQLITE_CONF
ffd10 49 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64 IG_PAGECACHE and
ffd20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
ffd30 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72 _memory() featur
ffd40 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 es..** If the de
ffd50 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 fault page cache
ffd60 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
ffd70 69 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68 is overriden, th
ffd80 65 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a en neither of.**
ffd90 20 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75 these two featu
ffda0 72 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c res are availabl
ffdb0 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 e..**.** @(#) $I
ffdc0 64 3a 20 70 63 61 63 68 65 31 2e 63 2c 76 20 31 d: pcache1.c,v 1
ffdd0 2e 31 31 20 32 30 30 39 2f 30 34 2f 31 34 20 31 .11 2009/04/14 1
ffde0 38 3a 34 34 3a 33 39 20 61 73 77 69 66 74 20 45 8:44:39 aswift E
ffdf0 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65 xp $.*/...typede
ffe00 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 31 f struct PCache1
ffe10 20 50 43 61 63 68 65 31 3b 0a 74 79 70 65 64 65 PCache1;.typede
ffe20 66 20 73 74 72 75 63 74 20 50 67 48 64 72 31 20 f struct PgHdr1
ffe30 50 67 48 64 72 31 3b 0a 74 79 70 65 64 65 66 20 PgHdr1;.typedef
ffe40 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f struct PgFreeslo
ffe50 74 20 50 67 46 72 65 65 73 6c 6f 74 3b 0a 0a 2f t PgFreeslot;../
ffe60 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 * Pointers to st
ffe70 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 69 73 ructures of this
ffe80 20 74 79 70 65 20 61 72 65 20 63 61 73 74 20 61 type are cast a
ffe90 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73 20 0a nd returned as .
ffea0 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69 74 65 ** opaque sqlite
ffeb0 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64 6c 65 3_pcache* handle
ffec0 73 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 61 63 s.*/.struct PCac
ffed0 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61 63 68 65 he1 {. /* Cache
ffee0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 configuration p
ffef0 61 72 61 6d 65 74 65 72 73 2e 20 50 61 67 65 20 arameters. Page
fff00 73 69 7a 65 20 28 73 7a 50 61 67 65 29 20 61 6e size (szPage) an
fff10 64 20 74 68 65 20 70 75 72 67 65 61 62 6c 65 0a d the purgeable.
fff20 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 75 72 67 ** flag (bPurg
fff30 65 61 62 6c 65 29 20 61 72 65 20 73 65 74 20 77 eable) are set w
fff40 68 65 6e 20 74 68 65 20 63 61 63 68 65 20 69 73 hen the cache is
fff50 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 78 20 6d created. nMax m
fff60 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d 6f 64 69 ay be . ** modi
fff70 66 69 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 fied at any time
fff80 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 by a call to th
fff90 65 20 70 63 61 63 68 65 31 43 61 63 68 65 53 69 e pcache1CacheSi
fffa0 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a 20 20 2a ze() method.. *
fffb0 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 * The global mut
fffc0 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 ex must be held
fffd0 77 68 65 6e 20 61 63 63 65 73 73 69 6e 67 20 6e when accessing n
fffe0 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 Max.. */. int
ffff0 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 szPage;
10000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10001 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 /* Size of alloc
10002 61 74 65 64 20 70 61 67 65 73 20 69 6e 20 62 79 ated pages in by
10003 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 tes */. int bPu
10004 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 rgeable;
10005 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10006 54 72 75 65 20 69 66 20 63 61 63 68 65 20 69 73 True if cache is
10007 20 70 75 72 67 65 61 62 6c 65 20 2a 2f 0a 20 20 purgeable */.
10008 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 unsigned int nMi
10009 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
1000a 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e /* Minimum n
1000b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 72 umber of pages r
1000c 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 75 6e 73 eserved */. uns
1000d 69 67 6e 65 64 20 69 6e 74 20 6e 4d 61 78 3b 20 igned int nMax;
1000e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1000f 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 22 /* Configured "
10010 63 61 63 68 65 5f 73 69 7a 65 22 20 76 61 6c 75 cache_size" valu
10011 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73 68 20 e */.. /* Hash
10012 74 61 62 6c 65 20 6f 66 20 61 6c 6c 20 70 61 67 table of all pag
10013 65 73 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e es. The followin
10014 67 20 76 61 72 69 61 62 6c 65 73 20 6d 61 79 20 g variables may
10015 6f 6e 6c 79 20 62 65 20 61 63 63 65 73 73 65 64 only be accessed
10016 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 61 . ** when the a
10017 63 63 65 73 73 6f 72 20 69 73 20 68 6f 6c 64 69 ccessor is holdi
10018 6e 67 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 ng the global mu
10019 74 65 78 20 28 73 65 65 20 70 63 61 63 68 65 31 tex (see pcache1
1001a 45 6e 74 65 72 4d 75 74 65 78 28 29 20 0a 20 20 EnterMutex() .
1001b 2a 2a 20 61 6e 64 20 70 63 61 63 68 65 31 4c 65 ** and pcache1Le
1001c 61 76 65 4d 75 74 65 78 28 29 29 2e 0a 20 20 2a aveMutex()).. *
1001d 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
1001e 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20 20 20 nRecyclable;
1001f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
10020 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
10021 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a 20 20 e LRU list */.
10022 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 61 unsigned int nPa
10023 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
10024 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
10025 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
10026 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e 73 69 apHash */. unsi
10027 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68 3b 20 gned int nHash;
10028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10029 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f /* Number of slo
1002a 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d 20 2a ts in apHash[] *
1002b 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 70 48 /. PgHdr1 **apH
1002c 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 ash;
1002d 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 /* Hash
1002e 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74 20 6c table for fast l
1002f 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a 2f 0a ookup by key */.
10030 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10031 69 4d 61 78 4b 65 79 3b 20 20 20 20 20 20 20 20 iMaxKey;
10032 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 /* Larges
10033 74 20 6b 65 79 20 73 65 65 6e 20 73 69 6e 63 65 t key seen since
10034 20 78 54 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a xTruncate() */.
10035 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 63 };../*.** Each c
10036 61 63 68 65 20 65 6e 74 72 79 20 69 73 20 72 65 ache entry is re
10037 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 20 presented by an
10038 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
10039 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 following .** st
1003a 72 75 63 74 75 72 65 2e 20 41 20 62 75 66 66 65 ructure. A buffe
1003b 72 20 6f 66 20 50 67 48 64 72 31 2e 70 43 61 63 r of PgHdr1.pCac
1003c 68 65 2d 3e 73 7a 50 61 67 65 20 62 79 74 65 73 he->szPage bytes
1003d 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a is allocated .*
1003e 2a 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 72 * directly after
1003f 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 69 the structure i
10040 6e 20 6d 65 6d 6f 72 79 20 28 73 65 65 20 74 68 n memory (see th
10041 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 e PGHDR1_TO_PAGE
10042 28 29 20 0a 2a 2a 20 6d 61 63 72 6f 20 62 65 6c () .** macro bel
10043 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 ow)..*/.struct P
10044 67 48 64 72 31 20 7b 0a 20 20 75 6e 73 69 67 6e gHdr1 {. unsign
10045 65 64 20 69 6e 74 20 69 4b 65 79 3b 20 20 20 20 ed int iKey;
10046 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 /* Key
10047 76 61 6c 75 65 20 28 70 61 67 65 20 6e 75 6d 62 value (page numb
10048 65 72 29 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 er) */. PgHdr1
10049 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
1004a 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
1004b 69 6e 20 68 61 73 68 20 74 61 62 6c 65 20 63 68 in hash table ch
1004c 61 69 6e 20 2a 2f 0a 20 20 50 43 61 63 68 65 31 ain */. PCache1
1004d 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 *pCache;
1004e 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 /* Cache
1004f 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 that currently
10050 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a owns this page *
10051 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 /. PgHdr1 *pLru
10052 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
10053 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 4c 52 /* Next in LR
10054 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e U list of unpinn
10055 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 ed pages */. Pg
10056 48 64 72 31 20 2a 70 4c 72 75 50 72 65 76 3b 20 Hdr1 *pLruPrev;
10057 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10058 50 72 65 76 69 6f 75 73 20 69 6e 20 4c 52 55 20 Previous in LRU
10059 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 list of unpinned
1005a 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a pages */.};../*
1005b 0a 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69 .** Free slots i
1005c 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 6f 72 20 n the allocator
1005d 75 73 65 64 20 74 6f 20 64 69 76 69 64 65 20 75 used to divide u
1005e 70 20 74 68 65 20 62 75 66 66 65 72 20 70 72 6f p the buffer pro
1005f 76 69 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 vided using.** t
10060 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 he SQLITE_CONFIG
10061 5f 50 41 47 45 43 41 43 48 45 20 6d 65 63 68 61 _PAGECACHE mecha
10062 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 nism..*/.struct
10063 50 67 46 72 65 65 73 6c 6f 74 20 7b 0a 20 20 50 PgFreeslot {. P
10064 67 46 72 65 65 73 6c 6f 74 20 2a 70 4e 65 78 74 gFreeslot *pNext
10065 3b 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 ; /* Next free
10066 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a slot */.};../*.*
10067 2a 20 47 6c 6f 62 61 6c 20 64 61 74 61 20 75 73 * Global data us
10068 65 64 20 62 79 20 74 68 69 73 20 63 61 63 68 65 ed by this cache
10069 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 ..*/.static SQLI
1006a 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 50 43 TE_WSD struct PC
1006b 61 63 68 65 47 6c 6f 62 61 6c 20 7b 0a 20 20 73 acheGlobal {. s
1006c 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
1006d 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 tex;
1006e 20 20 20 2f 2a 20 73 74 61 74 69 63 20 6d 75 74 /* static mut
1006f 65 78 20 4d 55 54 45 58 5f 53 54 41 54 49 43 5f ex MUTEX_STATIC_
10070 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d LRU */.. int nM
10071 61 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 axPage;
10072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10073 20 53 75 6d 20 6f 66 20 6e 4d 61 78 50 61 67 65 Sum of nMaxPage
10074 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 for purgeable c
10075 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e aches */. int n
10076 4d 69 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 MinPage;
10077 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10078 2a 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 50 61 67 * Sum of nMinPag
10079 65 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 e for purgeable
1007a 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 caches */. int
1007b 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 20 20 20 nCurrentPage;
1007c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1007d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 75 72 /* Number of pur
1007e 67 65 61 62 6c 65 20 70 61 67 65 73 20 61 6c 6c geable pages all
1007f 6f 63 61 74 65 64 20 2a 2f 0a 20 20 50 67 48 64 ocated */. PgHd
10080 72 31 20 2a 70 4c 72 75 48 65 61 64 2c 20 2a 70 r1 *pLruHead, *p
10081 4c 72 75 54 61 69 6c 3b 20 20 20 20 20 20 20 20 LruTail;
10082 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 /* LRU list of u
10083 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f npinned pages */
10084 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 .. /* Variables
10085 20 72 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49 related to SQLI
10086 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
10087 43 48 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f CHE settings. */
10088 0a 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20 . int szSlot;
10089 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1008a 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1008b 66 20 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74 f each free slot
1008c 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 */. void *pSta
1008d 72 74 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 rt, *pEnd;
1008e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 /* Bou
1008f 6e 64 73 20 6f 66 20 70 61 67 65 63 61 63 68 65 nds of pagecache
10090 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f malloc range */
10091 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 . PgFreeslot *p
10092 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 Free;
10093 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70 /* Free p
10094 61 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 7d 20 age blocks */.}
10095 70 63 61 63 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a pcache1_g;../*.*
10096 2a 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74 68 * All code in th
10097 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 is file should a
10098 63 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61 6c ccess the global
10099 20 73 74 72 75 63 74 75 72 65 20 61 62 6f 76 65 structure above
1009a 20 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c 69 61 via the.** alia
1009b 73 20 22 70 63 61 63 68 65 31 22 2e 20 54 68 69 s "pcache1". Thi
1009c 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 s ensures that t
1009d 68 65 20 57 53 44 20 65 6d 75 6c 61 74 69 6f 6e he WSD emulation
1009e 20 69 73 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a is used when.**
1009f 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 73 compiling for s
100a0 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e ystems that do n
100a1 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 6c 20 ot support real
100a2 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 WSD..*/.#define
100a3 70 63 61 63 68 65 31 20 28 47 4c 4f 42 41 4c 28 pcache1 (GLOBAL(
100a4 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f struct PCacheGlo
100a5 62 61 6c 2c 20 70 63 61 63 68 65 31 5f 67 29 29 bal, pcache1_g))
100a6 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 50 ../*.** When a P
100a7 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20 gHdr1 structure
100a8 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 is allocated, th
100a9 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 43 61 e associated PCa
100aa 63 68 65 31 2e 73 7a 50 61 67 65 0a 2a 2a 20 62 che1.szPage.** b
100ab 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 ytes of data are
100ac 20 6c 6f 63 61 74 65 64 20 64 69 72 65 63 74 6c located directl
100ad 79 20 61 66 74 65 72 20 69 74 20 69 6e 20 6d 65 y after it in me
100ae 6d 6f 72 79 20 28 69 2e 65 2e 20 74 68 65 20 74 mory (i.e. the t
100af 6f 74 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 otal.** size of
100b0 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
100b1 73 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 s sizeof(PgHdr1)
100b2 2b 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 20 +PCache1.szPage
100b3 62 79 74 65 29 2e 20 54 68 65 0a 2a 2a 20 50 47 byte). The.** PG
100b4 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 29 20 6d HDR1_TO_PAGE() m
100b5 61 63 72 6f 20 74 61 6b 65 73 20 61 20 70 6f 69 acro takes a poi
100b6 6e 74 65 72 20 74 6f 20 61 20 50 67 48 64 72 31 nter to a PgHdr1
100b7 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 2a 2a structure as.**
100b8 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 an argument and
100b9 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
100ba 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 er to the associ
100bb 61 74 65 64 20 62 6c 6f 63 6b 20 6f 66 20 73 7a ated block of sz
100bc 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 2e 20 54 Page.** bytes. T
100bd 68 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 he PAGE_TO_PGHDR
100be 31 28 29 20 6d 61 63 72 6f 20 64 6f 65 73 20 74 1() macro does t
100bf 68 65 20 6f 70 70 6f 73 69 74 65 3a 20 69 74 73 he opposite: its
100c0 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 argument is.**
100c1 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 a pointer to a b
100c2 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 20 62 lock of szPage b
100c3 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 6e 64 ytes of data and
100c4 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
100c5 65 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 e is.** a pointe
100c6 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61 r to the associa
100c7 74 65 64 20 50 67 48 64 72 31 20 73 74 72 75 63 ted PgHdr1 struc
100c8 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 73 ture..**.** as
100c9 73 65 72 74 28 20 50 47 48 44 52 31 5f 54 4f 5f sert( PGHDR1_TO_
100ca 50 41 47 45 28 50 41 47 45 5f 54 4f 5f 50 47 48 PAGE(PAGE_TO_PGH
100cb 44 52 31 28 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f DR1(X))==X );.*/
100cc 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f .#define PGHDR1_
100cd 54 4f 5f 50 41 47 45 28 70 29 20 28 76 6f 69 64 TO_PAGE(p) (void
100ce 20 2a 29 28 26 28 28 75 6e 73 69 67 6e 65 64 20 *)(&((unsigned
100cf 63 68 61 72 20 2a 29 70 29 5b 73 69 7a 65 6f 66 char *)p)[sizeof
100d0 28 50 67 48 64 72 31 29 5d 29 0a 23 64 65 66 69 (PgHdr1)]).#defi
100d1 6e 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 ne PAGE_TO_PGHDR
100d2 31 28 70 29 20 28 50 67 48 64 72 31 20 2a 29 28 1(p) (PgHdr1 *)(
100d3 26 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 &((unsigned char
100d4 20 2a 29 70 29 5b 2d 31 2a 28 69 6e 74 29 73 69 *)p)[-1*(int)si
100d5 7a 65 6f 66 28 50 67 48 64 72 31 29 5d 29 0a 0a zeof(PgHdr1)])..
100d6 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 /*.** Macros to
100d7 65 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 enter and leave
100d8 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6d the global LRU m
100d9 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 utex..*/.#define
100da 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
100db 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 ex() sqlite3_mut
100dc 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 ex_enter(pcache1
100dd 2e 6d 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20 .mutex).#define
100de 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 pcache1LeaveMute
100df 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 x() sqlite3_mute
100e0 78 5f 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e x_leave(pcache1.
100e1 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a mutex)../*******
100e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
100e6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
100e7 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63 61 74 69 ** Page Allocati
100e8 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 on/SQLITE_CONFIG
100e9 5f 50 43 41 43 48 45 20 52 65 6c 61 74 65 64 20 _PCACHE Related
100ea 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a Functions ******
100eb 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
100ec 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
100ed 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 s called during
100ee 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 initialization i
100ef 66 20 61 20 73 74 61 74 69 63 20 62 75 66 66 65 f a static buffe
100f0 72 20 69 73 20 0a 2a 2a 20 73 75 70 70 6c 69 65 r is .** supplie
100f1 64 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 d to use for the
100f2 20 70 61 67 65 2d 63 61 63 68 65 20 62 79 20 70 page-cache by p
100f3 61 73 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 assing the SQLIT
100f4 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
100f5 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f 20 73 71 HE.** verb to sq
100f6 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20 lite3_config().
100f7 50 61 72 61 6d 65 74 65 72 20 70 42 75 66 20 70 Parameter pBuf p
100f8 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f oints to an allo
100f9 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a 2a 2a 20 cation large.**
100fa 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e 74 61 69 enough to contai
100fb 6e 20 27 6e 27 20 62 75 66 66 65 72 73 20 6f 66 n 'n' buffers of
100fc 20 27 73 7a 27 20 62 79 74 65 73 20 65 61 63 68 'sz' bytes each
100fd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
100fe 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
100ff 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75 PCacheBufferSetu
10100 70 28 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e p(void *pBuf, in
10101 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 t sz, int n){.
10102 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 PgFreeslot *p;.
10103 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 sz = ROUNDDOWN8
10104 28 73 7a 29 3b 0a 20 20 70 63 61 63 68 65 31 2e (sz);. pcache1.
10105 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 70 szSlot = sz;. p
10106 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 3d 20 cache1.pStart =
10107 70 42 75 66 3b 0a 20 20 70 63 61 63 68 65 31 2e pBuf;. pcache1.
10108 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 77 68 69 pFree = 0;. whi
10109 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20 20 20 70 le( n-- ){. p
1010a 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 = (PgFreeslot*)
1010b 70 42 75 66 3b 0a 20 20 20 20 70 2d 3e 70 4e 65 pBuf;. p->pNe
1010c 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 xt = pcache1.pFr
1010d 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e ee;. pcache1.
1010e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 pFree = p;. p
1010f 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 Buf = (void*)&((
10110 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b char*)pBuf)[sz];
10111 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e 70 . }. pcache1.p
10112 45 6e 64 20 3d 20 70 42 75 66 3b 0a 7d 0a 0a 2f End = pBuf;.}../
10113 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 *.** Malloc func
10114 74 69 6f 6e 20 75 73 65 64 20 77 69 74 68 69 6e tion used within
10115 20 74 68 69 73 20 66 69 6c 65 20 74 6f 20 61 6c this file to al
10116 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 72 6f locate space fro
10117 6d 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 m the buffer.**
10118 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 configured using
10119 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
1011a 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
1011b 47 45 43 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e GECACHE) option.
1011c 20 49 66 20 6e 6f 20 0a 2a 2a 20 73 75 63 68 20 If no .** such
1011d 62 75 66 66 65 72 20 65 78 69 73 74 73 20 6f 72 buffer exists or
1011e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 70 61 there is no spa
1011f 63 65 20 6c 65 66 74 20 69 6e 20 69 74 2c 20 74 ce left in it, t
10120 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c his function fal
10121 6c 73 20 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 73 ls .** back to s
10122 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a qlite3Malloc()..
10123 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
10124 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 69 6e 74 pcache1Alloc(int
10125 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 nByte){. void
10126 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 *p;. assert( sq
10127 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
10128 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 (pcache1.mutex)
10129 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d );. if( nByte<=
1012a 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 26 pcache1.szSlot &
1012b 26 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 & pcache1.pFree
1012c 29 7b 0a 20 20 20 20 70 20 3d 20 28 50 67 48 64 ){. p = (PgHd
1012d 72 31 20 2a 29 70 63 61 63 68 65 31 2e 70 46 72 r1 *)pcache1.pFr
1012e 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e ee;. pcache1.
1012f 70 46 72 65 65 20 3d 20 70 63 61 63 68 65 31 2e pFree = pcache1.
10130 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 pFree->pNext;.
10131 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 sqlite3StatusS
10132 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 et(SQLITE_STATUS
10133 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 2c _PAGECACHE_SIZE,
10134 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c nByte);. sql
10135 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
10136 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
10137 43 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a CACHE_USED, 1);.
10138 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a }else{.. /*
10139 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
1013a 62 75 66 66 65 72 20 75 73 69 6e 67 20 73 71 6c buffer using sql
1013b 69 74 65 33 4d 61 6c 6c 6f 63 2e 20 42 65 66 6f ite3Malloc. Befo
1013c 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20 65 78 69 re doing so, exi
1013d 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 67 6c 6f t the. ** glo
1013e 62 61 6c 20 70 63 61 63 68 65 20 6d 75 74 65 78 bal pcache mutex
1013f 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 and unlock the
10140 70 61 67 65 72 2d 63 61 63 68 65 20 6f 62 6a 65 pager-cache obje
10141 63 74 20 70 43 61 63 68 65 2e 20 54 68 69 73 20 ct pCache. This
10142 69 73 20 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68 is . ** so th
10143 61 74 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 at if the attemp
10144 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 t to allocate a
10145 6e 65 77 20 62 75 66 66 65 72 20 63 61 75 73 65 new buffer cause
10146 73 20 74 68 65 20 74 68 65 20 0a 20 20 20 20 2a s the the . *
10147 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 73 6f 66 * configured sof
10148 74 2d 68 65 61 70 2d 6c 69 6d 69 74 20 74 6f 20 t-heap-limit to
10149 62 65 20 62 72 65 61 63 68 65 64 2c 20 69 74 20 be breached, it
1014a 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c 65 will be possible
1014b 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 63 6c 61 to. ** recla
1014c 69 6d 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 im memory from t
1014d 68 69 73 20 70 61 67 65 72 2d 63 61 63 68 65 2e his pager-cache.
1014e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 61 63 . */. pcac
1014f 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b he1LeaveMutex();
10150 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
10151 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 Malloc(nByte);.
10152 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d pcache1EnterM
10153 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 utex();. if(
10154 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 p ){. int s
10155 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f z = sqlite3Mallo
10156 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 cSize(p);.
10157 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
10158 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 (SQLITE_STATUS_P
10159 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f AGECACHE_OVERFLO
1015a 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 W, sz);. }.
1015b 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
1015c 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 ./*.** Free an a
1015d 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 llocated buffer
1015e 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 70 63 obtained from pc
1015f 61 63 68 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a 2f ache1Alloc()..*/
10160 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10161 63 68 65 31 46 72 65 65 28 76 6f 69 64 20 2a 70 che1Free(void *p
10162 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
10163 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10164 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
10165 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
10166 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d eturn;. if( p>=
10167 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 26 pcache1.pStart &
10168 26 20 70 3c 70 63 61 63 68 65 31 2e 70 45 6e 64 & p<pcache1.pEnd
10169 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c ){. PgFreesl
1016a 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73 ot *pSlot;. s
1016b 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
1016c 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
1016d 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 GECACHE_USED, -1
1016e 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 28 );. pSlot = (
1016f 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20 PgFreeslot*)p;.
10170 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 pSlot->pNext
10171 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b = pcache1.pFree;
10172 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72 . pcache1.pFr
10173 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d 65 ee = pSlot;. }e
10174 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53 69 lse{. int iSi
10175 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ze = sqlite3Mall
10176 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 ocSize(p);. s
10177 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
10178 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
10179 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 GECACHE_OVERFLOW
1017a 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 73 , -iSize);. s
1017b 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
1017c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c }.}../*.** All
1017d 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 ocate a new page
1017e 20 6f 62 6a 65 63 74 20 69 6e 69 74 69 61 6c 6c object initiall
1017f 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
10180 68 20 63 61 63 68 65 20 70 43 61 63 68 65 2e 0a h cache pCache..
10181 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 31 */.static PgHdr1
10182 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 *pcache1AllocPa
10183 67 65 28 50 43 61 63 68 65 31 20 2a 70 43 61 63 ge(PCache1 *pCac
10184 68 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 he){. int nByte
10185 20 3d 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 = sizeof(PgHdr1
10186 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 ) + pCache->szPa
10187 67 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 20 ge;. PgHdr1 *p
10188 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 = (PgHdr1 *)pcac
10189 68 65 31 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b he1Alloc(nByte);
1018a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
1018b 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 if( pCache->bPur
1018c 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 geable ){.
1018d 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
1018e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 Page++;. }.
1018f 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
10190 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 ./*.** Free a pa
10191 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61 ge object alloca
10192 74 65 64 20 62 79 20 70 63 61 63 68 65 31 41 6c ted by pcache1Al
10193 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2f 0a 73 74 locPage()..*/.st
10194 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10195 31 46 72 65 65 50 61 67 65 28 50 67 48 64 72 31 1FreePage(PgHdr1
10196 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
10197 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 . if( p->pCac
10198 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 he->bPurgeable )
10199 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
1019a 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a nCurrentPage--;.
1019b 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
1019c 31 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 1Free(p);. }.}.
1019d 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 ./*.** Malloc fu
1019e 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 nction used by S
1019f 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 QLite to obtain
101a0 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
101a1 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 uffer configured
101a2 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 .** using sqlite
101a3 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
101a4 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
101a5 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
101a6 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 such buffer.** e
101a7 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 xists, this func
101a8 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 tion falls back
101a9 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 to sqlite3Malloc
101aa 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
101ab 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
101ac 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e te3PageMalloc(in
101ad 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 t sz){. void *p
101ae 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
101af 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 Mutex();. p = p
101b0 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b cache1Alloc(sz);
101b1 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
101b2 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
101b3 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p;.}../*.** Fre
101b4 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 e an allocated b
101b5 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 uffer obtained f
101b6 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d rom sqlite3PageM
101b7 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
101b8 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
101b9 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
101ba 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 void *p){. pcac
101bb 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
101bc 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70 . pcache1Free(p
101bd 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 );. pcache1Leav
101be 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a eMutex();.}../**
101bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
101c4 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 ******* General
101c5 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 Implementation F
101c6 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a unctions *******
101c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
101c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
101c9 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
101ca 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 ion is used to r
101cb 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 esize the hash t
101cc 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 able used by the
101cd 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a cache passed.**
101ce 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
101cf 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
101d0 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d e global mutex m
101d1 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e ust be held when
101d2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
101d3 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
101d4 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 tic int pcache1R
101d5 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 esizeHash(PCache
101d6 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 1 *p){. PgHdr1
101d7 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 **apNew;. unsig
101d8 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 ned int nNew;.
101d9 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a unsigned int i;.
101da 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
101db 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
101dc 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
101dd 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 . nNew = p->nHa
101de 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 sh*2;. if( nNew
101df 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 <256 ){. nNew
101e0 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 = 256;. }.. p
101e1 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
101e2 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 ();. if( p->nHa
101e3 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 sh ){ sqlite3Beg
101e4 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
101e5 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 ; }. apNew = (P
101e6 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 gHdr1 **)sqlite3
101e7 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 _malloc(sizeof(P
101e8 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a gHdr1 *)*nNew);.
101e9 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 if( p->nHash )
101ea 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 { sqlite3EndBeni
101eb 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 gnMalloc(); }.
101ec 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
101ed 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 x();. if( apNew
101ee 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 ){. memset(a
101ef 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 pNew, 0, sizeof(
101f0 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b PgHdr1 *)*nNew);
101f1 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
101f2 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a p->nHash; i++){.
101f3 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 PgHdr1 *pP
101f4 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 age;. PgHdr
101f5 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 1 *pNext = p->ap
101f6 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 Hash[i];. w
101f7 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 hile( (pPage = p
101f8 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 Next)!=0 ){.
101f9 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
101fa 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 h = pPage->iKey
101fb 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 % nNew;.
101fc 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e pNext = pPage->
101fd 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 pNext;. p
101fe 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 Page->pNext = ap
101ff 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 New[h];.
10200 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 apNew[h] = pPage
10201 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
10202 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
10203 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 (p->apHash);.
10204 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e p->apHash = apN
10205 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 ew;. p->nHash
10206 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 = nNew;. }..
10207 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 return (p->apHas
10208 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 h ? SQLITE_OK :
10209 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d SQLITE_NOMEM);.}
1020a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1020b 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e ction is used in
1020c 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f ternally to remo
1020d 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67 ve the page pPag
1020e 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 e from the .** g
1020f 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 lobal LRU list,
10210 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 if is part of it
10211 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f . If pPage is no
10212 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c t part of the gl
10213 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 obal.** LRU list
10214 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 , then this func
10215 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
10216 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10217 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10218 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10219 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
1021a 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
1021b 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 d pcache1PinPage
1021c 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
1021d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1021e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
1021f 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10220 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28 if( pPage && (
10221 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10222 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 || pPage==pcache
10223 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 1.pLruTail) ){.
10224 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c if( pPage->pL
10225 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 ruPrev ){.
10226 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d pPage->pLruPrev-
10227 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 >pLruNext = pPag
10228 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 e->pLruNext;.
10229 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 }. if( pPage
1022a 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 ->pLruNext ){.
1022b 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e pPage->pLruN
1022c 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 ext->pLruPrev =
1022d 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b pPage->pLruPrev;
1022e 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1022f 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d cache1.pLruHead=
10230 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 =pPage ){.
10231 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10232 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 = pPage->pLruNe
10233 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 xt;. }. if
10234 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 ( pcache1.pLruTa
10235 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 il==pPage ){.
10236 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 pcache1.pLruT
10237 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 ail = pPage->pLr
10238 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 uPrev;. }.
10239 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
1023a 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d = 0;. pPage-
1023b 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 >pLruPrev = 0;.
1023c 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 pPage->pCache
1023d 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b ->nRecyclable--;
1023e 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 . }.}.../*.** R
1023f 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 emove the page s
10240 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
10241 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 gument from the
10242 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 hash table .** (
10243 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 PCache1.apHash s
10244 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 tructure) that i
10245 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 t is currently s
10246 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 tored in..**.**
10247 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10248 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10249 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
1024a 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
1024b 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
1024c 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 e1RemoveFromHash
1024d 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
1024e 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
1024f 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 h;. PCache1 *pC
10250 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 ache = pPage->pC
10251 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a ache;. PgHdr1 *
10252 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 *pp;.. h = pPag
10253 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 e->iKey % pCache
10254 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 ->nHash;. for(p
10255 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 p=&pCache->apHas
10256 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 h[h]; (*pp)!=pPa
10257 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 ge; pp=&(*pp)->p
10258 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 Next);. *pp = (
10259 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 *pp)->pNext;..
1025a 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b pCache->nPage--;
1025b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
1025c 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 re are currently
1025d 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 more than pcach
1025e 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 e.nMaxPage pages
1025f 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a allocated, try.
10260 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 ** to recycle pa
10261 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 ges to reduce th
10262 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 e number allocat
10263 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 ed to pcache.nMa
10264 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 xPage..*/.static
10265 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 void pcache1Enf
10266 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 orceMaxPage(void
10267 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
10268 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10269 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
1026a 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 ;. while( pcach
1026b 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e e1.nCurrentPage>
1026c 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 pcache1.nMaxPage
1026d 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 && pcache1.pLru
1026e 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 Tail ){. PgHd
1026f 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e r1 *p = pcache1.
10270 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 pLruTail;. pc
10271 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b ache1PinPage(p);
10272 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f . pcache1Remo
10273 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 veFromHash(p);.
10274 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 pcache1FreePa
10275 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ge(p);. }.}../*
10276 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 .** Discard all
10277 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 pages from cache
10278 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70 pCache with a p
10279 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 age number (key
1027a 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 value) .** great
1027b 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
1027c 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 to iLimit. Any
1027d 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 pinned pages tha
1027e 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 t meet this .**
1027f 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 criteria are unp
10280 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 inned before the
10281 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e y are discarded.
10282 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10283 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10284 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10285 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10286 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10287 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 d pcache1Truncat
10288 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 eUnsafe(. PCach
10289 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 e1 *pCache, . u
1028a 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d nsigned int iLim
1028b 69 74 20 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 it .){. unsigne
1028c 64 20 69 6e 74 20 68 3b 0a 20 20 61 73 73 65 72 d int h;. asser
1028d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1028e 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 _held(pcache1.mu
1028f 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 68 3d tex) );. for(h=
10290 30 3b 20 68 3c 70 43 61 63 68 65 2d 3e 6e 48 61 0; h<pCache->nHa
10291 73 68 3b 20 68 2b 2b 29 7b 0a 20 20 20 20 50 67 sh; h++){. Pg
10292 48 64 72 31 20 2a 2a 70 70 20 3d 20 26 70 43 61 Hdr1 **pp = &pCa
10293 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 che->apHash[h];
10294 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61 . PgHdr1 *pPa
10295 67 65 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 ge;. while( (
10296 70 50 61 67 65 20 3d 20 2a 70 70 29 21 3d 30 20 pPage = *pp)!=0
10297 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 ){. if( pPa
10298 67 65 2d 3e 69 4b 65 79 3e 3d 69 4c 69 6d 69 74 ge->iKey>=iLimit
10299 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 61 63 ){. pcac
1029a 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 he1PinPage(pPage
1029b 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d );. *pp =
1029c 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 pPage->pNext;.
1029d 20 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 pcache1Fr
1029e 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 eePage(pPage);.
1029f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
102a0 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d pp = &pPage-
102a1 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a >pNext;. }.
102a2 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a }. }.}../**
102a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
102a8 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f ******* sqlite3_
102a9 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 2a pcache Methods *
102aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
102ad 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
102ae 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
102af 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 74 te3_pcache.xInit
102b0 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 method..*/.stat
102b1 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 6e ic int pcache1In
102b2 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 it(void *NotUsed
102b3 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
102b4 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
102b5 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 memset(&pcache
102b6 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 1, 0, sizeof(pca
102b7 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 che1));. if( sq
102b8 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
102b9 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
102ba 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 pcache1.mute
102bb 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 x = sqlite3_mute
102bc 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d x_alloc(SQLITE_M
102bd 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 UTEX_STATIC_LRU)
102be 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
102bf 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
102c0 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
102c1 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
102c2 5f 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 _pcache.xShutdow
102c3 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 n method..*/.sta
102c4 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
102c5 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e Shutdown(void *N
102c6 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
102c7 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
102c8 73 65 64 29 3b 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 sed);. /* no-op
102c9 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 */.}../*.** Imp
102ca 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
102cb 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
102cc 65 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 e.xCreate method
102cd 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ..**.** Allocate
102ce 20 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f a new cache..*/
102cf 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
102d0 70 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43 pcache *pcache1C
102d1 72 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65 reate(int szPage
102d2 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 , int bPurgeable
102d3 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 ){. PCache1 *pC
102d4 61 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 ache;.. pCache
102d5 3d 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c = (PCache1 *)sql
102d6 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 ite3_malloc(size
102d7 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 of(PCache1));.
102d8 69 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20 if( pCache ){.
102d9 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c memset(pCache,
102da 20 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 0, sizeof(PCach
102db 65 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 e1));. pCache
102dc 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 ->szPage = szPag
102dd 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62 e;. pCache->b
102de 50 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75 Purgeable = (bPu
102df 72 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 rgeable ? 1 : 0)
102e0 3b 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65 ;. if( bPurge
102e1 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 able ){. pC
102e2 61 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b ache->nMin = 10;
102e3 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e . pcache1En
102e4 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
102e5 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 pcache1.nMinPa
102e6 67 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d ge += pCache->nM
102e7 69 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 in;. pcache
102e8 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
102e9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
102ea 6e 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 n (sqlite3_pcach
102eb 65 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f e *)pCache;.}../
102ec 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
102ed 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
102ee 65 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65 e3_pcache.xCache
102ef 73 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a size method. .**
102f0 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 .** Configure th
102f1 65 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d e cache_size lim
102f2 69 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a it for a cache..
102f3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
102f4 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28 cache1Cachesize(
102f5 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
102f6 70 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 p, int nMax){.
102f7 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 PCache1 *pCache
102f8 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a = (PCache1 *)p;.
102f9 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 if( pCache->bP
102fa 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 urgeable ){.
102fb 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
102fc 78 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 x();. pcache1
102fd 2e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d .nMaxPage += (nM
102fe 61 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 ax - pCache->nMa
102ff 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e x);. pCache->
10300 6e 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 nMax = nMax;.
10301 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d pcache1EnforceM
10302 61 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63 axPage();. pc
10303 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
10304 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
10305 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
10306 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
10307 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 ache.xPagecount
10308 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 method. .*/.stat
10309 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 ic int pcache1Pa
1030a 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f gecount(sqlite3_
1030b 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e pcache *p){. in
1030c 74 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e t n;. pcache1En
1030d 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 terMutex();. n
1030e 3d 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29 = ((PCache1 *)p)
1030f 2d 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68 ->nPage;. pcach
10310 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
10311 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f return n;.}../
10312 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10313 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10314 65 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68 e3_pcache.xFetch
10315 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 method. .**.**
10316 46 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20 Fetch a page by
10317 6b 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a key value..**.**
10318 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 Whether or not
10319 61 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62 a new page may b
1031a 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 e allocated by t
1031b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 his function dep
1031c 65 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 ends on.** the v
1031d 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 alue of the crea
1031e 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e teFlag argument.
1031f 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
10320 20 74 68 72 65 65 20 64 69 66 66 65 72 65 6e 74 three different
10321 20 61 70 70 72 6f 61 63 68 65 73 20 74 6f 20 6f approaches to o
10322 62 74 61 69 6e 69 6e 67 20 73 70 61 63 65 20 66 btaining space f
10323 6f 72 20 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65 or a page,.** de
10324 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 pending on the v
10325 61 6c 75 65 20 6f 66 20 70 61 72 61 6d 65 74 65 alue of paramete
10326 72 20 63 72 65 61 74 65 46 6c 61 67 20 28 77 68 r createFlag (wh
10327 69 63 68 20 6d 61 79 20 62 65 20 30 2c 20 31 20 ich may be 0, 1
10328 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 or 2)..**.** 1
10329 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 . Regardless of
1032a 74 68 65 20 76 61 6c 75 65 20 6f 66 20 63 72 65 the value of cre
1032b 61 74 65 46 6c 61 67 2c 20 74 68 65 20 63 61 63 ateFlag, the cac
1032c 68 65 20 69 73 20 73 65 61 72 63 68 65 64 20 66 he is searched f
1032d 6f 72 20 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f or a .** co
1032e 70 79 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 py of the reques
1032f 74 65 64 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 ted page. If one
10330 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 is found, it is
10331 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
10332 20 20 20 32 2e 20 49 66 20 63 72 65 61 74 65 46 2. If createF
10333 6c 61 67 3d 3d 30 20 61 6e 64 20 74 68 65 20 70 lag==0 and the p
10334 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
10335 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c dy in the cache,
10336 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20 NULL is.**
10337 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
10338 20 20 20 33 2e 20 49 66 20 63 72 65 61 74 65 46 3. If createF
10339 6c 61 67 20 69 73 20 31 2c 20 74 68 65 20 63 61 lag is 1, the ca
1033a 63 68 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 che is marked as
1033b 20 70 75 72 67 65 61 62 6c 65 20 61 6e 64 20 74 purgeable and t
1033c 68 65 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 20 he page is .**
1033d 20 20 20 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 not already
1033e 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 61 6e in the cache, an
1033f 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 20 74 d if either of t
10340 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
10341 20 74 72 75 65 2c 20 0a 2a 2a 20 20 20 20 20 20 true, .**
10342 72 65 74 75 72 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a return NULL:.**.
10343 2a 2a 20 20 20 20 20 20 20 28 61 29 20 74 68 65 ** (a) the
10344 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10345 20 70 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63 pinned by the c
10346 61 63 68 65 20 69 73 20 67 72 65 61 74 65 72 20 ache is greater
10347 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 than.**
10348 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 PCache1.nMax,
10349 6f 72 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 or.** (b)
1034a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
1034b 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68 ges pinned by th
1034c 65 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74 e cache is great
1034d 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 er than.**
1034e 20 20 20 20 20 74 68 65 20 73 75 6d 20 6f 66 20 the sum of
1034f 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 70 75 72 nMax for all pur
10350 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 20 6c geable caches, l
10351 65 73 73 20 74 68 65 20 73 75 6d 20 6f 66 20 0a ess the sum of .
10352 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e 4d 69 ** nMi
10353 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 n for all other
10354 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 purgeable caches
10355 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 34 2e 20 49 66 . .**.** 4. If
10356 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 72 none of the fir
10357 73 74 20 74 68 72 65 65 20 63 6f 6e 64 69 74 69 st three conditi
10358 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64 20 74 68 ons apply and th
10359 65 20 63 61 63 68 65 20 69 73 20 6d 61 72 6b 65 e cache is marke
1035a 64 0a 2a 2a 20 20 20 20 20 20 61 73 20 70 75 72 d.** as pur
1035b 67 65 61 62 6c 65 2c 20 61 6e 64 20 69 66 20 6f geable, and if o
1035c 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
1035d 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 2a 2a 0a ing is true:.**.
1035e 2a 2a 20 20 20 20 20 20 20 28 61 29 20 54 68 65 ** (a) The
1035f 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10360 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
10361 68 65 20 63 61 63 68 65 20 69 73 20 61 6c 72 65 he cache is alre
10362 61 64 79 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ady .**
10363 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 PCache1.nMax,
10364 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 or.**.** (
10365 62 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 b) The number of
10366 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 pages allocated
10367 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 for all purgeab
10368 6c 65 20 63 61 63 68 65 73 20 69 73 0a 2a 2a 20 le caches is.**
10369 20 20 20 20 20 20 20 20 20 20 61 6c 72 65 61 64 alread
1036a 79 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 y equal to or gr
1036b 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 eater than the s
1036c 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61 um of nMax for a
1036d 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ll.**
1036e 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 purgeable caches
1036f 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 74 68 65 ,.**.** the
10370 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 63 n attempt to rec
10371 79 63 6c 65 20 61 20 70 61 67 65 20 66 72 6f 6d ycle a page from
10372 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 49 the LRU list. I
10373 66 20 69 74 20 69 73 20 74 68 65 20 72 69 67 68 f it is the righ
10374 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a 65 2c 20 t.** size,
10375 72 65 74 75 72 6e 20 74 68 65 20 72 65 63 79 63 return the recyc
10376 6c 65 64 20 62 75 66 66 65 72 2e 20 4f 74 68 65 led buffer. Othe
10377 72 77 69 73 65 2c 20 66 72 65 65 20 74 68 65 20 rwise, free the
10378 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a 20 20 20 buffer and.**
10379 20 20 20 70 72 6f 63 65 65 64 20 74 6f 20 73 74 proceed to st
1037a 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35 ep 5. .**.** 5
1037b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6c 6c . Otherwise, all
1037c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e ocate and return
1037d 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 66 a new page buff
1037e 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
1037f 69 64 20 2a 70 63 61 63 68 65 31 46 65 74 63 68 id *pcache1Fetch
10380 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 (sqlite3_pcache
10381 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 *p, unsigned int
10382 20 69 4b 65 79 2c 20 69 6e 74 20 63 72 65 61 74 iKey, int creat
10383 65 46 6c 61 67 29 7b 0a 20 20 75 6e 73 69 67 6e eFlag){. unsign
10384 65 64 20 69 6e 74 20 6e 50 69 6e 6e 65 64 3b 0a ed int nPinned;.
10385 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
10386 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 e = (PCache1 *)p
10387 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 ;. PgHdr1 *pPag
10388 65 20 3d 20 30 3b 0a 0a 20 20 70 63 61 63 68 65 e = 0;.. pcache
10389 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
1038a 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d if( createFlag=
1038b 3d 31 20 29 20 73 71 6c 69 74 65 33 42 65 67 69 =1 ) sqlite3Begi
1038c 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
1038d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 .. /* Search th
1038e 65 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 e hash table for
1038f 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74 an existing ent
10390 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 ry. */. if( pCa
10391 63 68 65 2d 3e 6e 48 61 73 68 3e 30 20 29 7b 0a che->nHash>0 ){.
10392 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
10393 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43 61 63 h = iKey % pCac
10394 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 20 66 he->nHash;. f
10395 6f 72 28 70 50 61 67 65 3d 70 43 61 63 68 65 2d or(pPage=pCache-
10396 3e 61 70 48 61 73 68 5b 68 5d 3b 20 70 50 61 67 >apHash[h]; pPag
10397 65 26 26 70 50 61 67 65 2d 3e 69 4b 65 79 21 3d e&&pPage->iKey!=
10398 69 4b 65 79 3b 20 70 50 61 67 65 3d 70 50 61 67 iKey; pPage=pPag
10399 65 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a e->pNext);. }..
1039a 20 20 69 66 28 20 70 50 61 67 65 20 7c 7c 20 63 if( pPage || c
1039b 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 7b 0a reateFlag==0 ){.
1039c 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 pcache1PinPa
1039d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 67 ge(pPage);. g
1039e 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 oto fetch_out;.
1039f 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 }.. /* Step 3
103a0 6f 66 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e of header commen
103a1 74 2e 20 2a 2f 0a 20 20 6e 50 69 6e 6e 65 64 20 t. */. nPinned
103a2 3d 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 = pCache->nPage
103a3 2d 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 - pCache->nRecyc
103a4 6c 61 62 6c 65 3b 0a 20 20 69 66 28 20 63 72 65 lable;. if( cre
103a5 61 74 65 46 6c 61 67 3d 3d 31 20 26 26 20 70 43 ateFlag==1 && pC
103a6 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
103a7 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 6e 50 && (. nP
103a8 69 6e 6e 65 64 3e 3d 28 70 63 61 63 68 65 31 2e inned>=(pcache1.
103a9 6e 4d 61 78 50 61 67 65 2b 70 43 61 63 68 65 2d nMaxPage+pCache-
103aa 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d >nMin-pcache1.nM
103ab 69 6e 50 61 67 65 29 0a 20 20 20 20 20 7c 7c 20 inPage). ||
103ac 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65 nPinned>=(pCache
103ad 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f 20 31 30 29 ->nMax * 9 / 10)
103ae 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 . )){. goto
103af 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a fetch_out;. }..
103b0 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 if( pCache->nP
103b1 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 age>=pCache->nHa
103b2 73 68 20 26 26 20 70 63 61 63 68 65 31 52 65 73 sh && pcache1Res
103b3 69 7a 65 48 61 73 68 28 70 43 61 63 68 65 29 20 izeHash(pCache)
103b4 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 ){. goto fetc
103b5 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a h_out;. }.. /*
103b6 20 53 74 65 70 20 34 2e 20 54 72 79 20 74 6f 20 Step 4. Try to
103b7 72 65 63 79 63 6c 65 20 61 20 70 61 67 65 20 62 recycle a page b
103b8 75 66 66 65 72 20 69 66 20 61 70 70 72 6f 70 72 uffer if appropr
103b9 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 iate. */. if( p
103ba 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
103bb 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 e && pcache1.pLr
103bc 75 54 61 69 6c 20 26 26 20 28 0a 20 20 20 20 20 uTail && (.
103bd 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31 (pCache->nPage+1
103be 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20 >=pCache->nMax)
103bf 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 || pcache1.nCurr
103c0 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68 65 31 entPage>=pcache1
103c1 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29 7b 0a .nMaxPage. )){.
103c2 20 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 pPage = pcac
103c3 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 he1.pLruTail;.
103c4 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 pcache1RemoveF
103c5 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 3b 0a romHash(pPage);.
103c6 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 pcache1PinPa
103c7 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 ge(pPage);. i
103c8 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 f( pPage->pCache
103c9 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63 68 65 ->szPage!=pCache
103ca 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20 20 20 ->szPage ){.
103cb 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 pcache1FreePag
103cc 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
103cd 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d pPage = 0;. }
103ce 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63 else{. pcac
103cf 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 he1.nCurrentPage
103d0 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43 61 63 -= (pPage->pCac
103d1 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 2d he->bPurgeable -
103d2 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
103d3 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ble);. }. }.
103d4 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 . /* Step 5. If
103d5 20 61 20 75 73 61 62 6c 65 20 70 61 67 65 20 62 a usable page b
103d6 75 66 66 65 72 20 68 61 73 20 73 74 69 6c 6c 20 uffer has still
103d7 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 not been found,
103d8 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f . ** attempt to
103d9 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
103da 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 one. . */. if(
103db 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 !pPage ){. p
103dc 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 41 6c Page = pcache1Al
103dd 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65 29 3b locPage(pCache);
103de 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 . }.. if( pPag
103df 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 e ){. unsigne
103e0 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 d int h = iKey %
103e1 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a pCache->nHash;.
103e2 20 20 20 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50 *(void **)(P
103e3 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 GHDR1_TO_PAGE(pP
103e4 61 67 65 29 29 20 3d 20 30 3b 0a 20 20 20 20 70 age)) = 0;. p
103e5 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 2b 3b 0a Cache->nPage++;.
103e6 20 20 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 pPage->iKey
103e7 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70 50 61 67 = iKey;. pPag
103e8 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61 63 68 e->pNext = pCach
103e9 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 e->apHash[h];.
103ea 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 20 pPage->pCache
103eb 3d 20 70 43 61 63 68 65 3b 0a 20 20 20 20 70 50 = pCache;. pP
103ec 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 age->pLruPrev =
103ed 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 0;. pPage->pL
103ee 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 ruNext = 0;.
103ef 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 pCache->apHash[h
103f0 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a ] = pPage;. }..
103f1 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28 fetch_out:. if(
103f2 20 70 50 61 67 65 20 26 26 20 69 4b 65 79 3e 70 pPage && iKey>p
103f3 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 Cache->iMaxKey )
103f4 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d {. pCache->iM
103f5 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 axKey = iKey;.
103f6 7d 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c }. if( createFl
103f7 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 33 45 ag==1 ) sqlite3E
103f8 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
103f9 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 ;. pcache1Leave
103fa 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 Mutex();. retur
103fb 6e 20 28 70 50 61 67 65 20 3f 20 50 47 48 44 52 n (pPage ? PGHDR
103fc 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67 65 29 1_TO_PAGE(pPage)
103fd 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a : 0);.}.../*.**
103fe 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
103ff 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
10400 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 cache.xUnpin met
10401 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 hod..**.** Mark
10402 61 20 70 61 67 65 20 61 73 20 75 6e 70 69 6e 6e a page as unpinn
10403 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 ed (eligible for
10404 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 asynchronous re
10405 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 cycling)..*/.sta
10406 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
10407 55 6e 70 69 6e 28 73 71 6c 69 74 65 33 5f 70 63 Unpin(sqlite3_pc
10408 61 63 68 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70 ache *p, void *p
10409 50 67 2c 20 69 6e 74 20 72 65 75 73 65 55 6e 6c Pg, int reuseUnl
1040a 69 6b 65 6c 79 29 7b 0a 20 20 50 43 61 63 68 65 ikely){. PCache
1040b 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 1 *pCache = (PCa
1040c 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 che1 *)p;. PgHd
1040d 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 r1 *pPage = PAGE
1040e 5f 54 4f 5f 50 47 48 44 52 31 28 70 50 67 29 3b _TO_PGHDR1(pPg);
1040f 0a 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 .. pcache1Enter
10410 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 Mutex();.. /* I
10411 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f t is an error to
10412 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
10413 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 ion if the page
10414 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a is already . **
10415 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f part of the glo
10416 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 bal LRU list..
10417 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
10418 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 ge->pLruPrev==0
10419 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 && pPage->pLruNe
1041a 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 xt==0 );. asser
1041b 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 t( pcache1.pLruH
1041c 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 ead!=pPage && pc
1041d 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d ache1.pLruTail!=
1041e 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 pPage );.. if(
1041f 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c reuseUnlikely ||
10420 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10421 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d tPage>pcache1.nM
10422 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 axPage ){. pc
10423 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
10424 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ash(pPage);.
10425 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10426 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b pPage);. }else{
10427 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 . /* Add the
10428 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 page to the glob
10429 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 al LRU list. Nor
1042a 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 mally, the page
1042b 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 is added to.
1042c 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 ** the head of t
1042d 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 he list (last pa
1042e 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 ge to be recycle
1042f 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 d). However, if
10430 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 the . ** reus
10431 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 eUnlikely flag p
10432 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 assed to this fu
10433 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 nction is true,
10434 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65 the page is adde
10435 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 d. ** to the
10436 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 tail of the list
10437 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 (first page to
10438 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 be recycled)..
10439 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 */. if( pca
1043a 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b che1.pLruHead ){
1043b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 . pcache1.p
1043c 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 LruHead->pLruPre
1043d 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 v = pPage;.
1043e 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
1043f 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 = pcache1.pLruH
10440 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 ead;. pcach
10441 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 e1.pLruHead = pP
10442 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a age;. }else{.
10443 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10444 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a ruTail = pPage;.
10445 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10446 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a ruHead = pPage;.
10447 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 }. pCache
10448 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b ->nRecyclable++;
10449 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c . }.. pcache1L
1044a 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a eaveMutex();.}..
1044b 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
1044c 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
1044d 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 te3_pcache.xReke
1044e 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 y method. .*/.st
1044f 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10450 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 1Rekey(. sqlite
10451 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 3_pcache *p,. v
10452 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 oid *pPg,. unsi
10453 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 gned int iOld,.
10454 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e unsigned int iN
10455 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 ew.){. PCache1
10456 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
10457 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 e1 *)p;. PgHdr1
10458 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 *pPage = PAGE_T
10459 4f 5f 50 47 48 44 52 31 28 70 50 67 29 3b 0a 20 O_PGHDR1(pPg);.
1045a 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 20 20 PgHdr1 **pp;.
1045b 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 unsigned int h;
1045c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1045d 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29 3b 0a ->iKey==iOld );.
1045e 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d . pcache1EnterM
1045f 75 74 65 78 28 29 3b 0a 0a 20 20 68 20 3d 20 69 utex();.. h = i
10460 4f 6c 64 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 Old%pCache->nHas
10461 68 3b 0a 20 20 70 70 20 3d 20 26 70 43 61 63 68 h;. pp = &pCach
10462 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 e->apHash[h];.
10463 77 68 69 6c 65 28 20 28 2a 70 70 29 21 3d 70 50 while( (*pp)!=pP
10464 61 67 65 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 age ){. pp =
10465 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 &(*pp)->pNext;.
10466 20 7d 0a 20 20 2a 70 70 20 3d 20 70 50 61 67 65 }. *pp = pPage
10467 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 68 20 3d 20 ->pNext;.. h =
10468 69 4e 65 77 25 70 43 61 63 68 65 2d 3e 6e 48 61 iNew%pCache->nHa
10469 73 68 3b 0a 20 20 70 50 61 67 65 2d 3e 69 4b 65 sh;. pPage->iKe
1046a 79 20 3d 20 69 4e 65 77 3b 0a 20 20 70 50 61 67 y = iNew;. pPag
1046b 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61 63 68 e->pNext = pCach
1046c 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 e->apHash[h];.
1046d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 pCache->apHash[h
1046e 5d 20 3d 20 70 50 61 67 65 3b 0a 0a 20 20 69 66 ] = pPage;.. if
1046f 28 20 69 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69 ( iNew>pCache->i
10470 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 43 MaxKey ){. pC
10471 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 ache->iMaxKey =
10472 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 iNew;. }.. pca
10473 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10474 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
10475 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
10476 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e sqlite3_pcache.
10477 78 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 xTruncate method
10478 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 . .**.** Discard
10479 20 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 all unpinned pa
1047a 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 ges in the cache
1047b 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d with a page num
1047c 62 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 ber equal to.**
1047d 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
1047e 70 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 parameter iLimit
1047f 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 . Any pinned pag
10480 65 73 20 77 69 74 68 20 61 20 70 61 67 65 20 6e es with a page n
10481 75 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 umber.** equal t
10482 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 o or greater tha
10483 6e 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 n iLimit are imp
10484 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 licitly unpinned
10485 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
10486 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 pcache1Truncate
10487 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 (sqlite3_pcache
10488 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 *p, unsigned int
10489 20 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 iLimit){. PCac
1048a 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 he1 *pCache = (P
1048b 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 Cache1 *)p;. pc
1048c 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
1048d 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3c );. if( iLimit<
1048e 3d 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 =pCache->iMaxKey
1048f 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 54 ){. pcache1T
10490 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 runcateUnsafe(pC
10491 61 63 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 ache, iLimit);.
10492 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b pCache->iMaxK
10493 65 79 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 ey = iLimit-1;.
10494 20 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 }. pcache1Leav
10495 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a eMutex();.}../*.
10496 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
10497 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
10498 5f 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 _pcache.xDestroy
10499 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 method. .**.**
1049a 44 65 73 74 72 6f 79 20 61 20 63 61 63 68 65 20 Destroy a cache
1049b 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 allocated using
1049c 70 63 61 63 68 65 31 43 72 65 61 74 65 28 29 2e pcache1Create().
1049d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1049e 70 63 61 63 68 65 31 44 65 73 74 72 6f 79 28 73 pcache1Destroy(s
1049f 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 qlite3_pcache *p
104a0 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 ){. PCache1 *pC
104a1 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 ache = (PCache1
104a2 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e *)p;. pcache1En
104a3 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 terMutex();. pc
104a4 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 ache1TruncateUns
104a5 61 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a afe(pCache, 0);.
104a6 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 pcache1.nMaxPa
104a7 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d ge -= pCache->nM
104a8 61 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d ax;. pcache1.nM
104a9 69 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65 inPage -= pCache
104aa 2d 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 ->nMin;. pcache
104ab 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 1EnforceMaxPage(
104ac 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 );. pcache1Leav
104ad 65 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 eMutex();. sqli
104ae 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 2d te3_free(pCache-
104af 3e 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 >apHash);. sqli
104b0 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 29 te3_free(pCache)
104b1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
104b2 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
104b3 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 ed during initia
104b4 6c 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 lization (sqlite
104b5 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 3_initialize())
104b6 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 to.** install th
104b7 65 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67 61 e default plugga
104b8 62 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 ble cache module
104b9 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 75 , assuming the u
104ba 73 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 ser has not.** a
104bb 6c 72 65 61 64 79 20 70 72 6f 76 69 64 65 64 20 lready provided
104bc 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a an alternative..
104bd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
104be 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 E void sqlite3PC
104bf 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76 acheSetDefault(v
104c0 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 oid){. static s
104c1 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
104c2 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 thods defaultMet
104c3 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 hods = {. 0,
104c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
104c5 20 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f /* pArg */
104c6 0a 20 20 20 20 70 63 61 63 68 65 31 49 6e 69 74 . pcache1Init
104c7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
104c8 20 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 xInit */. pc
104c9 61 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 ache1Shutdown,
104ca 20 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74 64 /* xShutd
104cb 6f 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 own */. pcach
104cc 65 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 e1Create,
104cd 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2a /* xCreate *
104ce 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 63 /. pcache1Cac
104cf 68 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 2f hesize, /
104d0 2a 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a * xCachesize */.
104d1 20 20 20 20 70 63 61 63 68 65 31 50 61 67 65 63 pcache1Pagec
104d2 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 ount, /*
104d3 78 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 xPagecount */.
104d4 20 20 70 63 61 63 68 65 31 46 65 74 63 68 2c 20 pcache1Fetch,
104d5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 /* xF
104d6 65 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 etch */. pcac
104d7 68 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 he1Unpin,
104d8 20 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a /* xUnpin *
104d9 2f 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6b /. pcache1Rek
104da 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ey, /
104db 2a 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 * xRekey */.
104dc 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 2c pcache1Truncate,
104dd 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 /* xTru
104de 6e 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 ncate */. pca
104df 63 68 65 31 44 65 73 74 72 6f 79 20 20 20 20 20 che1Destroy
104e0 20 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f /* xDestro
104e1 79 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 y */. };. sqli
104e2 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
104e3 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c E_CONFIG_PCACHE,
104e4 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 &defaultMethods
104e5 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c );.}..#ifdef SQL
104e6 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ITE_ENABLE_MEMOR
104e7 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a Y_MANAGEMENT./*.
104e8 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
104e9 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 is called to fr
104ea 65 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 64 ee superfluous d
104eb 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
104ec 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 ated memory.** h
104ed 65 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 72 eld by the pager
104ee 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 system. Memory
104ef 69 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 51 in use by any SQ
104f0 4c 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 Lite pager alloc
104f1 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 ated.** by the c
104f2 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d 61 urrent thread ma
104f3 79 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 65 y be sqlite3_fre
104f4 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 e()ed..**.** nRe
104f5 71 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 q is the number
104f6 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f of bytes of memo
104f7 72 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 ry required. Onc
104f8 65 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 0a e this much has.
104f9 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 ** been released
104fa 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 , the function r
104fb 65 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75 eturns. The retu
104fc 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
104fd 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a total number .**
104fe 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d of bytes of mem
104ff 6f 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f ory released..*/
10500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10501 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
10502 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 eReleaseMemory(i
10503 6e 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 nt nReq){. int
10504 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 nFree = 0;. if(
10505 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 3d pcache1.pStart=
10506 3d 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31 =0 ){. PgHdr1
10507 20 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65 31 *p;. pcache1
10508 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
10509 20 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 while( (nReq<0
1050a 20 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 || nFree<nReq)
1050b 26 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70 4c && (p=pcache1.pL
1050c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 ruTail) ){.
1050d 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 nFree += sqlite
1050e 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 3MallocSize(p);.
1050f 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e pcache1Pin
10510 50 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20 70 Page(p);. p
10511 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d cache1RemoveFrom
10512 48 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 70 Hash(p);. p
10513 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 cache1FreePage(p
10514 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 );. }. pca
10515 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10516 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e ;. }. return n
10517 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f Free;.}.#endif /
10518 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
10519 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e MEMORY_MANAGEMEN
1051a 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c T */..#ifdef SQL
1051b 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 ITE_TEST./*.** T
1051c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1051d 75 73 65 64 20 62 79 20 74 65 73 74 20 70 72 6f used by test pro
1051e 63 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70 65 cedures to inspe
1051f 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ct the internal
10520 73 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 state.** of the
10521 67 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f global cache..*/
10522 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10523 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
10524 68 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20 2a heStats(. int *
10525 70 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 pnCurrent,
10526 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 /* OUT: Total nu
10527 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 mber of pages ca
10528 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ched */. int *p
10529 6e 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20 2f nMax, /
1052a 2a 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 * OUT: Global ma
1052b 78 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a 65 ximum cache size
1052c 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e */. int *pnMin
1052d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 , /* OU
1052e 54 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 65 T: Sum of PCache
1052f 31 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 1.nMin for purge
10530 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 able caches */.
10531 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 int *pnRecyclab
10532 6c 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f le /* OUT: To
10533 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
10534 67 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f ges available fo
10535 72 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 r recycling */.)
10536 7b 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 {. PgHdr1 *p;.
10537 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 int nRecyclable
10538 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 = 0;. for(p=pc
10539 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 ache1.pLruHead;
1053a 70 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 p; p=p->pLruNext
1053b 29 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61 62 ){. nRecyclab
1053c 6c 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 le++;. }. *pnC
1053d 75 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65 31 urrent = pcache1
1053e 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 .nCurrentPage;.
1053f 20 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 *pnMax = pcache
10540 31 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 1.nMaxPage;. *p
10541 6e 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e nMin = pcache1.n
10542 4d 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 MinPage;. *pnRe
10543 63 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 cyclable = nRecy
10544 63 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 clable;.}.#endif
10545 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
10546 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 31 * End of pcache1
10547 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
10548 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10549 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1054a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1054b 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 * Begin file row
1054c 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a set.c **********
1054d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1054e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1054f 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 /./*.** 2008 Dec
10550 65 6d 62 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 ember 3.**.** Th
10551 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
10552 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
10553 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
10554 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
10555 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
10556 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
10557 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
10558 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
10559 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1055a 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1055b 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1055c 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1055d 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1055e 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1055f 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
10560 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
10561 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
10562 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10563 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10564 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10565 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10566 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
10567 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 s module impleme
10568 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 nts an object we
10569 20 63 61 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 call a "RowSet"
1056a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 ..**.** The RowS
1056b 65 74 20 6f 62 6a 65 63 74 20 69 73 20 61 20 63 et object is a c
1056c 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 ollection of row
1056d 69 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 ids. Rowids.**
1056e 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 are inserted int
1056f 6f 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 20 o the RowSet in
10570 61 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72 64 an arbitrary ord
10571 65 72 2e 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 er. Inserts.**
10572 63 61 6e 20 62 65 20 69 6e 74 65 72 6d 69 78 65 can be intermixe
10573 64 20 77 69 74 68 20 74 65 73 74 73 20 74 6f 20 d with tests to
10574 73 65 65 20 69 66 20 61 20 67 69 76 65 6e 20 72 see if a given r
10575 6f 77 69 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a owid has been.**
10576 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 previously inse
10577 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52 6f rted into the Ro
10578 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 wSet..**.** Afte
10579 72 20 61 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 r all inserts ar
1057a 65 20 66 69 6e 69 73 68 65 64 2c 20 69 74 20 69 e finished, it i
1057b 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 s possible to ex
1057c 74 72 61 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 tract the.** ele
1057d 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f 77 ments of the Row
1057e 53 65 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 Set in sorted or
1057f 64 65 72 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 der. Once this
10580 65 78 74 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 extraction.** pr
10581 6f 63 65 73 73 20 68 61 73 20 73 74 61 72 74 65 ocess has starte
10582 64 2c 20 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e d, no new elemen
10583 74 73 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74 ts may be insert
10584 65 64 2e 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c ed..**.** Hence,
10585 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6f the primitive o
10586 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 perations for a
10587 52 6f 77 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a RowSet are:.**.*
10588 2a 20 20 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 * CREATE.**
10589 20 20 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 INSERT.** T
1058a 45 53 54 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 EST.** SMALLE
1058b 53 54 0a 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 ST.** DESTROY
1058c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 .**.** The CREAT
1058d 45 20 61 6e 64 20 44 45 53 54 52 4f 59 20 70 72 E and DESTROY pr
1058e 69 6d 69 74 69 76 65 73 20 61 72 65 20 74 68 65 imitives are the
1058f 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 constructor and
10590 20 64 65 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 destructor,.**
10591 6f 62 76 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 obviously. The
10592 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 INSERT primitive
10593 20 61 64 64 73 20 61 20 6e 65 77 20 65 6c 65 6d adds a new elem
10594 65 6e 74 20 74 6f 20 74 68 65 20 52 6f 77 53 65 ent to the RowSe
10595 74 2e 0a 2a 2a 20 54 45 53 54 20 63 68 65 63 6b t..** TEST check
10596 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 s to see if an e
10597 6c 65 6d 65 6e 74 20 69 73 20 61 6c 72 65 61 64 lement is alread
10598 79 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e y in the RowSet.
10599 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 SMALLEST.** ex
1059a 74 72 61 63 74 73 20 74 68 65 20 6c 65 61 73 74 tracts the least
1059b 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 value from the
1059c 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 RowSet..**.** Th
1059d 65 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 e INSERT primiti
1059e 76 65 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 ve might allocat
1059f 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d e additional mem
105a0 6f 72 79 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a ory. Memory is.
105a1 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 ** allocated in
105a2 63 68 75 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 chunks so most I
105a3 4e 53 45 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c NSERTs do no all
105a4 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 ocation. There
105a5 69 73 20 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 is an .** upper
105a6 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a bound on the siz
105a7 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d e of allocated m
105a8 65 6d 6f 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 emory. No memor
105a9 79 20 69 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e y is freed.** un
105aa 74 69 6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a til DESTROY..**.
105ab 2a 2a 20 54 68 65 20 54 45 53 54 20 70 72 69 6d ** The TEST prim
105ac 69 74 69 76 65 20 69 6e 63 6c 75 64 65 73 20 61 itive includes a
105ad 20 22 62 61 74 63 68 22 20 6e 75 6d 62 65 72 2e "batch" number.
105ae 20 20 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 The TEST primi
105af 74 69 76 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c tive.** will onl
105b0 79 20 73 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 y see elements t
105b1 68 61 74 20 77 65 72 65 20 69 6e 73 65 72 74 65 hat were inserte
105b2 64 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 d before the las
105b3 74 20 63 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 t change.** in t
105b4 68 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 2e he batch number.
105b5 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
105b6 2c 20 69 66 20 61 6e 20 49 4e 53 45 52 54 20 6f , if an INSERT o
105b7 63 63 75 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a ccurs between.**
105b8 20 74 77 6f 20 54 45 53 54 73 20 77 68 65 72 65 two TESTs where
105b9 20 74 68 65 20 54 45 53 54 73 20 68 61 76 65 20 the TESTs have
105ba 74 68 65 20 73 61 6d 65 20 62 61 74 63 68 20 6e the same batch n
105bb 75 62 6d 65 72 2c 20 74 68 65 6e 20 74 68 65 0a ubmer, then the.
105bc 2a 2a 20 76 61 6c 75 65 20 61 64 64 65 64 20 62 ** value added b
105bd 79 20 74 68 65 20 49 4e 53 45 52 54 20 77 69 6c y the INSERT wil
105be 6c 20 6e 6f 74 20 62 65 20 76 69 73 69 62 6c 65 l not be visible
105bf 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 54 to the second T
105c0 45 53 54 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 EST..** The init
105c1 69 61 6c 20 62 61 74 63 68 20 6e 75 6d 62 65 72 ial batch number
105c2 20 69 73 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 is zero, so if
105c3 74 68 65 20 76 65 72 79 20 66 69 72 73 74 20 54 the very first T
105c4 45 53 54 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 EST contains.**
105c5 61 20 6e 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 a non-zero batch
105c6 20 6e 75 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c number, it will
105c7 20 73 65 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 see all prior I
105c8 4e 53 45 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f NSERTs..**.** No
105c9 20 49 4e 53 45 52 54 73 20 6d 61 79 20 6f 63 63 INSERTs may occ
105ca 75 72 73 20 61 66 74 65 72 20 61 20 53 4d 41 4c urs after a SMAL
105cb 4c 45 53 54 2e 20 20 41 6e 20 61 73 73 65 72 74 LEST. An assert
105cc 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 ion will fail if
105cd 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 74 74 65 .** that is atte
105ce 6d 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 mpted..**.** The
105cf 20 63 6f 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 cost of an INSE
105d0 52 54 20 69 73 20 72 6f 75 67 68 6c 79 20 63 6f RT is roughly co
105d1 6e 73 74 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 nstant. (Someti
105d2 6d 65 20 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a me new memory.**
105d3 20 68 61 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 has to be alloc
105d4 61 74 65 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 ated on an INSER
105d5 54 2e 29 20 20 54 68 65 20 63 6f 73 74 20 6f 66 T.) The cost of
105d6 20 61 20 54 45 53 54 20 77 69 74 68 20 61 20 6e a TEST with a n
105d7 65 77 0a 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 ew.** batch numb
105d8 65 72 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 er is O(NlogN) w
105d9 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 here N is the nu
105da 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 mber of elements
105db 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 0a in the RowSet..
105dc 2a 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 ** The cost of a
105dd 20 54 45 53 54 20 75 73 69 6e 67 20 74 68 65 20 TEST using the
105de 73 61 6d 65 20 62 61 74 63 68 20 6e 75 6d 62 65 same batch numbe
105df 72 20 69 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 r is O(logN). T
105e0 68 65 20 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 he cost.** of th
105e1 65 20 66 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 e first SMALLEST
105e2 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 is O(NlogN). S
105e3 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 econd and subseq
105e4 75 65 6e 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a uent SMALLEST.**
105e5 20 70 72 69 6d 69 74 69 76 65 73 20 61 72 65 20 primitives are
105e6 63 6f 6e 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 constant time.
105e7 54 68 65 20 63 6f 73 74 20 6f 66 20 44 45 53 54 The cost of DEST
105e8 52 4f 59 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a ROY is O(N)..**.
105e9 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 61 ** There is an a
105ea 64 64 65 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e dded cost of O(N
105eb 29 20 77 68 65 6e 20 73 77 69 74 63 68 69 6e 67 ) when switching
105ec 20 62 65 74 77 65 65 6e 20 54 45 53 54 20 61 6e between TEST an
105ed 64 0a 2a 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 d.** SMALLEST pr
105ee 69 6d 69 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 imitives..**.**
105ef 24 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 $Id: rowset.c,v
105f0 31 2e 36 20 32 30 30 39 2f 30 34 2f 32 32 20 31 1.6 2009/04/22 1
105f1 35 3a 33 32 3a 35 39 20 64 72 68 20 45 78 70 20 5:32:59 drh Exp
105f2 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 $.*/.../*.** Tar
105f3 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c get size for all
105f4 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a ocation chunks..
105f5 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 */.#define ROWSE
105f6 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a T_ALLOCATION_SIZ
105f7 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 E 1024../*.** Th
105f8 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 e number of rows
105f9 65 74 20 65 6e 74 72 69 65 73 20 70 65 72 20 61 et entries per a
105fa 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e llocation chunk.
105fb 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 .*/.#define ROWS
105fc 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 ET_ENTRY_PER_CHU
105fd 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 NK \.
105fe 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 52 ((R
105ff 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e OWSET_ALLOCATION
10600 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 _SIZE-8)/sizeof(
10601 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
10602 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 ry))../*.** Each
10603 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 entry in a RowS
10604 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 et is an instanc
10605 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
10606 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 ng object..*/.st
10607 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10608 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 { .
10609 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20 i64 v;
1060a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1060b 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20 66 6f * ROWID value fo
1060c 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a r this entry */.
1060d 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
1060e 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20 20 20 ntry *pRight;
1060f 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72 65 65 /* Right subtree
10610 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69 65 73 (larger entries
10611 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 ) or list */. s
10612 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
10613 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 y *pLeft; /*
10614 4c 65 66 74 20 73 75 62 74 72 65 65 20 28 73 6d Left subtree (sm
10615 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 20 2a aller entries) *
10616 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 /.};../*.** RowS
10617 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 etEntry objects
10618 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e are allocated in
10619 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 28 69 large chunks (i
1061a 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 0a nstances of the.
1061b 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 ** following str
1061c 75 63 74 75 72 65 29 20 74 6f 20 72 65 64 75 63 ucture) to reduc
1061d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
1061e 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 20 54 ion overhead. T
1061f 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 he.** chunks are
10620 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 kept on a linke
10621 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74 20 74 d list so that t
10622 68 65 79 20 63 61 6e 20 62 65 20 64 65 61 6c 6c hey can be deall
10623 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 ocated.** when t
10624 68 65 20 52 6f 77 53 65 74 20 69 73 20 64 65 73 he RowSet is des
10625 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 troyed..*/.struc
10626 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a t RowSetChunk {.
10627 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 struct RowSetC
10628 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b hunk *pNextChunk
10629 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 ; /* Next
1062a 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f chunk on list o
1062b 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 f them all */.
1062c 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
1062d 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 ry aEntry[ROWSET
1062e 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b _ENTRY_PER_CHUNK
1062f 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 ]; /* Allocated
10630 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f entries */.};../
10631 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 69 6e *.** A RowSet in
10632 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
10633 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
10634 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 ructure..**.** A
10635 20 74 79 70 65 64 65 66 20 6f 66 20 74 68 69 73 typedef of this
10636 20 73 74 72 75 63 74 75 72 65 20 69 66 20 66 6f structure if fo
10637 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 und in sqliteInt
10638 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f .h..*/.struct Ro
10639 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63 74 20 wSet {. struct
1063a 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 RowSetChunk *pCh
1063b 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 unk; /* List
1063c 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c of all chunk all
1063d 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 ocations */. sq
1063e 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
1063f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10640 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
10641 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 nection */. str
10642 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
10643 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c *pEntry; /* L
10644 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73 20 75 ist of entries u
10645 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 sing pRight */.
10646 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
10647 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 try *pLast;
10648 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e /* Last entry on
10649 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74 the pEntry list
1064a 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 */. struct Row
1064b 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65 73 68 SetEntry *pFresh
1064c 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f ; /* Source o
1064d 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 f new entry obje
1064e 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 cts */. struct
1064f 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54 72 RowSetEntry *pTr
10650 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 ee; /* Binar
10651 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72 69 65 y tree of entrie
10652 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 s */. u16 nFres
10653 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
10654 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10655 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 of objects on pF
10656 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 resh */. u8 isS
10657 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 orted;
10658 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
10659 20 69 66 20 70 45 6e 74 72 79 20 69 73 20 73 6f if pEntry is so
1065a 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 rted */. u8 iBa
1065b 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 tch;
1065c 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 /* Curr
1065d 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74 63 68 ent insert batch
1065e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 */.};../*.** Tu
1065f 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 rn bulk memory i
10660 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62 6a nto a RowSet obj
10661 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 ect. N bytes of
10662 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 memory.** are a
10663 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70 61 vailable at pSpa
10664 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69 6e ce. The db poin
10665 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 61 ter is used as a
10666 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a memory context.
10667 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73 65 ** for any subse
10668 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e quent allocation
10669 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 6f s that need to o
1066a 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 ccur..** Return
1066b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1066c 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 new RowSet obje
1066d 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 ct..**.** It mus
1066e 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 t be the case th
1066f 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 69 65 at N is sufficie
10670 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 nt to make a Row
10671 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 set. If not.**
10672 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 an assertion fau
10673 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a lt occurs..** .*
10674 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65 72 * If N is larger
10675 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 than the minimu
10676 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 70 6c m, use the surpl
10677 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61 6c us as an initial
10678 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f .** allocation o
10679 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c 61 f entries availa
1067a 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 ble to be filled
1067b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1067c 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 ATE RowSet *sqli
1067d 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 te3RowSetInit(sq
1067e 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 lite3 *db, void
1067f 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 *pSpace, unsigne
10680 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 d int N){. RowS
10681 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 et *p;. assert(
10682 20 4e 20 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29 N >= sizeof(*p)
10683 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65 );. p = pSpace
10684 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 ;. p->pChunk =
10685 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0;. p->db = db;
10686 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 . p->pEntry = 0
10687 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 ;. p->pLast = 0
10688 3b 0a 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 ;. p->pTree = 0
10689 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 ;. p->pFresh =
1068a 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e (struct RowSetEn
1068b 74 72 79 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d try*)&p[1];. p-
1068c 3e 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 >nFresh = (u16)(
1068d 28 4e 20 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29 (N - sizeof(*p))
1068e 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 52 /sizeof(struct R
1068f 6f 77 53 65 74 45 6e 74 72 79 29 29 3b 0a 20 20 owSetEntry));.
10690 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b p->isSorted = 1;
10691 0a 20 20 70 2d 3e 69 42 61 74 63 68 20 3d 20 30 . p->iBatch = 0
10692 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
10693 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 ./*.** Deallocat
10694 65 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f e all chunks fro
10695 6d 20 61 20 52 6f 77 53 65 74 2e 20 20 54 68 69 m a RowSet. Thi
10696 73 20 66 72 65 65 73 20 61 6c 6c 20 6d 65 6d 6f s frees all memo
10697 72 79 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 52 ry that.** the R
10698 6f 77 53 65 74 20 68 61 73 20 61 6c 6c 6f 63 61 owSet has alloca
10699 74 65 64 20 6f 76 65 72 20 69 74 73 20 6c 69 66 ted over its lif
1069a 65 74 69 6d 65 2e 20 20 54 68 69 73 20 72 6f 75 etime. This rou
1069b 74 69 6e 65 20 69 73 0a 2a 2a 20 74 68 65 20 64 tine is.** the d
1069c 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 estructor for th
1069d 65 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c e RowSet..*/.SQL
1069e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1069f 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c sqlite3RowSetCl
106a0 65 61 72 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a ear(RowSet *p){.
106a1 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 struct RowSetC
106a2 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 hunk *pChunk, *p
106a3 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 NextChunk;. for
106a4 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e (pChunk=p->pChun
106a5 6b 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e k; pChunk; pChun
106a6 6b 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b k = pNextChunk){
106a7 0a 20 20 20 20 70 4e 65 78 74 43 68 75 6e 6b 20 . pNextChunk
106a8 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 = pChunk->pNextC
106a9 68 75 6e 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 hunk;. sqlite
106aa 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 3DbFree(p->db, p
106ab 43 68 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d Chunk);. }. p-
106ac 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 >pChunk = 0;. p
106ad 2d 3e 6e 46 72 65 73 68 20 3d 20 30 3b 0a 20 20 ->nFresh = 0;.
106ae 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 p->pEntry = 0;.
106af 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 p->pLast = 0;.
106b0 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 p->pTree = 0;.
106b1 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 p->isSorted = 1
106b2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 ;.}../*.** Inser
106b3 74 20 61 20 6e 65 77 20 76 61 6c 75 65 20 69 6e t a new value in
106b4 74 6f 20 61 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a to a RowSet..**.
106b5 2a 2a 20 54 68 65 20 6d 61 6c 6c 6f 63 46 61 69 ** The mallocFai
106b6 6c 65 64 20 66 6c 61 67 20 6f 66 20 74 68 65 20 led flag of the
106b7 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
106b8 69 6f 6e 20 69 73 20 73 65 74 20 69 66 20 61 0a ion is set if a.
106b9 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** memory alloca
106ba 74 69 6f 6e 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 tion fails..*/.S
106bb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
106bc 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 id sqlite3RowSet
106bd 49 6e 73 65 72 74 28 52 6f 77 53 65 74 20 2a 70 Insert(RowSet *p
106be 2c 20 69 36 34 20 72 6f 77 69 64 29 7b 0a 20 20 , i64 rowid){.
106bf 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
106c0 72 79 20 2a 70 45 6e 74 72 79 3b 20 20 2f 2a 20 ry *pEntry; /*
106c1 54 68 65 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f The new entry */
106c2 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
106c3 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 Entry *pLast;
106c4 2f 2a 20 54 68 65 20 6c 61 73 74 20 70 72 69 6f /* The last prio
106c5 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 61 73 73 r entry */. ass
106c6 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 ert( p!=0 );. i
106c7 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d 30 20 f( p->nFresh==0
106c8 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f ){. struct Ro
106c9 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 77 3b wSetChunk *pNew;
106ca 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 . pNew = sqli
106cb 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
106cc 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e ->db, sizeof(*pN
106cd 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4e ew));. if( pN
106ce 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 ew==0 ){. r
106cf 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 eturn;. }.
106d0 20 70 4e 65 77 2d 3e 70 4e 65 78 74 43 68 75 6e pNew->pNextChun
106d1 6b 20 3d 20 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 k = p->pChunk;.
106d2 20 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 p->pChunk = p
106d3 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 70 46 72 65 New;. p->pFre
106d4 73 68 20 3d 20 70 4e 65 77 2d 3e 61 45 6e 74 72 sh = pNew->aEntr
106d5 79 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 65 73 68 y;. p->nFresh
106d6 20 3d 20 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f = ROWSET_ENTRY_
106d7 50 45 52 5f 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 PER_CHUNK;. }.
106d8 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 46 72 pEntry = p->pFr
106d9 65 73 68 2b 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 esh++;. p->nFre
106da 73 68 2d 2d 3b 0a 20 20 70 45 6e 74 72 79 2d 3e sh--;. pEntry->
106db 76 20 3d 20 72 6f 77 69 64 3b 0a 20 20 70 45 6e v = rowid;. pEn
106dc 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b try->pRight = 0;
106dd 0a 20 20 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c . pLast = p->pL
106de 61 73 74 3b 0a 20 20 69 66 28 20 70 4c 61 73 74 ast;. if( pLast
106df 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 ){. if( p->i
106e0 73 53 6f 72 74 65 64 20 26 26 20 72 6f 77 69 64 sSorted && rowid
106e1 3c 3d 70 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20 <=pLast->v ){.
106e2 20 20 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 p->isSorted
106e3 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 = 0;. }. p
106e4 4c 61 73 74 2d 3e 70 52 69 67 68 74 20 3d 20 70 Last->pRight = p
106e5 45 6e 74 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a Entry;. }else{.
106e6 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 assert( p->p
106e7 45 6e 74 72 79 3d 3d 30 20 29 3b 20 2f 2a 20 46 Entry==0 ); /* F
106e8 69 72 65 73 20 69 66 20 49 4e 53 45 52 54 20 61 ires if INSERT a
106e9 66 74 65 72 20 53 4d 41 4c 4c 45 53 54 20 2a 2f fter SMALLEST */
106ea 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d . p->pEntry =
106eb 20 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 pEntry;. }. p
106ec 2d 3e 70 4c 61 73 74 20 3d 20 70 45 6e 74 72 79 ->pLast = pEntry
106ed 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 ;.}../*.** Merge
106ee 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 52 6f two lists of Ro
106ef 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 wSetEntry object
106f0 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 70 6c 69 s. Remove dupli
106f1 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cates..**.** The
106f2 20 69 6e 70 75 74 20 6c 69 73 74 73 20 61 72 65 input lists are
106f3 20 63 6f 6e 6e 65 63 74 65 64 20 76 69 61 20 70 connected via p
106f4 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 20 61 Right pointers a
106f5 6e 64 20 61 72 65 20 0a 2a 2a 20 61 73 73 75 6d nd are .** assum
106f6 65 64 20 74 6f 20 65 61 63 68 20 61 6c 72 65 61 ed to each alrea
106f7 64 79 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20 dy be in sorted
106f8 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 order..*/.static
106f9 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
106fa 74 72 79 20 2a 72 6f 77 53 65 74 4d 65 72 67 65 try *rowSetMerge
106fb 28 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 (. struct RowSe
106fc 74 45 6e 74 72 79 20 2a 70 41 2c 20 20 20 20 2f tEntry *pA, /
106fd 2a 20 46 69 72 73 74 20 73 6f 72 74 65 64 20 6c * First sorted l
106fe 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 ist to be merged
106ff 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 */. struct Row
10700 53 65 74 45 6e 74 72 79 20 2a 70 42 20 20 20 20 SetEntry *pB
10701 20 2f 2a 20 53 65 63 6f 6e 64 20 73 6f 72 74 65 /* Second sorte
10702 64 20 6c 69 73 74 20 74 6f 20 62 65 20 6d 65 72 d list to be mer
10703 67 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 ged */.){. stru
10704 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 68 ct RowSetEntry h
10705 65 61 64 3b 0a 20 20 73 74 72 75 63 74 20 52 6f ead;. struct Ro
10706 77 53 65 74 45 6e 74 72 79 20 2a 70 54 61 69 6c wSetEntry *pTail
10707 3b 0a 0a 20 20 70 54 61 69 6c 20 3d 20 26 68 65 ;.. pTail = &he
10708 61 64 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 ad;. while( pA
10709 26 26 20 70 42 20 29 7b 0a 20 20 20 20 61 73 73 && pB ){. ass
1070a 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d ert( pA->pRight=
1070b 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d =0 || pA->v<=pA-
1070c 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 >pRight->v );.
1070d 20 20 61 73 73 65 72 74 28 20 70 42 2d 3e 70 52 assert( pB->pR
1070e 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 ight==0 || pB->v
1070f 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 <=pB->pRight->v
10710 29 3b 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 76 );. if( pA->v
10711 3c 70 42 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 <pB->v ){.
10712 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 pTail->pRight =
10713 70 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 pA;. pA = p
10714 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 A->pRight;.
10715 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e pTail = pTail->
10716 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 pRight;. }els
10717 65 20 69 66 28 20 70 42 2d 3e 76 3c 70 41 2d 3e e if( pB->v<pA->
10718 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c v ){. pTail
10719 2d 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 ->pRight = pB;.
1071a 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 52 pB = pB->pR
1071b 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69 ight;. pTai
1071c 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 l = pTail->pRigh
1071d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
1071e 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 pA = pA->pRi
1071f 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ght;. }. }.
10720 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 61 if( pA ){. a
10721 73 73 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 ssert( pA->pRigh
10722 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 t==0 || pA->v<=p
10723 41 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a A->pRight->v );.
10724 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 pTail->pRigh
10725 74 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b t = pA;. }else{
10726 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 3d . assert( pB=
10727 3d 30 20 7c 7c 20 70 42 2d 3e 70 52 69 67 68 74 =0 || pB->pRight
10728 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 ==0 || pB->v<=pB
10729 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 ->pRight->v );.
1072a 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 pTail->pRight
1072b 20 3d 20 70 42 3b 0a 20 20 7d 0a 20 20 72 65 74 = pB;. }. ret
1072c 75 72 6e 20 68 65 61 64 2e 70 52 69 67 68 74 3b urn head.pRight;
1072d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 .}../*.** Sort a
1072e 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 ll elements on t
1072f 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 6f he pEntry list o
10730 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 74 f the RowSet int
10731 6f 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 o ascending orde
10732 72 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f r..*/ .static vo
10733 69 64 20 72 6f 77 53 65 74 53 6f 72 74 28 52 6f id rowSetSort(Ro
10734 77 53 65 74 20 2a 70 29 7b 0a 20 20 75 6e 73 69 wSet *p){. unsi
10735 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 73 74 gned int i;. st
10736 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10737 20 2a 70 45 6e 74 72 79 3b 0a 20 20 73 74 72 75 *pEntry;. stru
10738 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
10739 61 42 75 63 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 aBucket[40];..
1073a 61 73 73 65 72 74 28 20 70 2d 3e 69 73 53 6f 72 assert( p->isSor
1073b 74 65 64 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 ted==0 );. mems
1073c 65 74 28 61 42 75 63 6b 65 74 2c 20 30 2c 20 73 et(aBucket, 0, s
1073d 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 29 3b izeof(aBucket));
1073e 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 45 6e . while( p->pEn
1073f 74 72 79 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 try ){. pEntr
10740 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 y = p->pEntry;.
10741 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 p->pEntry = p
10742 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 Entry->pRight;.
10743 20 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 pEntry->pRigh
10744 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 t = 0;. for(i
10745 3d 30 3b 20 61 42 75 63 6b 65 74 5b 69 5d 3b 20 =0; aBucket[i];
10746 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 i++){. pEnt
10747 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72 67 65 ry = rowSetMerge
10748 28 61 42 75 63 6b 65 74 5b 69 5d 2c 20 70 45 6e (aBucket[i], pEn
10749 74 72 79 29 3b 0a 20 20 20 20 20 20 61 42 75 63 try);. aBuc
1074a 6b 65 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 ket[i] = 0;.
1074b 7d 0a 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d }. aBucket[i]
1074c 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 = pEntry;. }.
1074d 20 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 66 pEntry = 0;. f
1074e 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 or(i=0; i<sizeof
1074f 28 61 42 75 63 6b 65 74 29 2f 73 69 7a 65 6f 66 (aBucket)/sizeof
10750 28 61 42 75 63 6b 65 74 5b 30 5d 29 3b 20 69 2b (aBucket[0]); i+
10751 2b 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d +){. pEntry =
10752 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70 45 6e rowSetMerge(pEn
10753 74 72 79 2c 20 61 42 75 63 6b 65 74 5b 69 5d 29 try, aBucket[i])
10754 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 ;. }. p->pEntr
10755 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 70 2d y = pEntry;. p-
10756 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d >pLast = 0;. p-
10757 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d >isSorted = 1;.}
10758 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 .../*.** The inp
10759 75 74 2c 20 70 49 6e 2c 20 69 73 20 61 20 62 69 ut, pIn, is a bi
1075a 6e 61 72 79 20 74 72 65 65 20 28 6f 72 20 73 75 nary tree (or su
1075b 62 74 72 65 65 29 20 6f 66 20 52 6f 77 53 65 74 btree) of RowSet
1075c 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 0a 2a Entry objects..*
1075d 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 74 * Convert this t
1075e 72 65 65 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 ree into a linke
1075f 64 20 6c 69 73 74 20 63 6f 6e 6e 65 63 74 65 64 d list connected
10760 20 62 79 20 74 68 65 20 70 52 69 67 68 74 20 70 by the pRight p
10761 6f 69 6e 74 65 72 73 0a 2a 2a 20 61 6e 64 20 72 ointers.** and r
10762 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 eturn pointers t
10763 6f 20 74 68 65 20 66 69 72 73 74 20 61 6e 64 20 o the first and
10764 6c 61 73 74 20 65 6c 65 6d 65 6e 74 73 20 6f 66 last elements of
10765 20 74 68 65 20 6e 65 77 20 6c 69 73 74 2e 0a 2a the new list..*
10766 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f /.static void ro
10767 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 0a wSetTreeToList(.
10768 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
10769 6e 74 72 79 20 2a 70 49 6e 2c 20 20 20 20 20 20 ntry *pIn,
1076a 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 /* Root of th
1076b 65 20 69 6e 70 75 74 20 74 72 65 65 20 2a 2f 0a e input tree */.
1076c 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
1076d 6e 74 72 79 20 2a 2a 70 70 46 69 72 73 74 2c 20 ntry **ppFirst,
1076e 20 20 20 2f 2a 20 57 72 69 74 65 20 68 65 61 64 /* Write head
1076f 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 6c of the output l
10770 69 73 74 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 ist here */. st
10771 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
10772 20 2a 2a 70 70 4c 61 73 74 20 20 20 20 20 20 2f **ppLast /
10773 2a 20 57 72 69 74 65 20 74 61 69 6c 20 6f 66 20 * Write tail of
10774 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 the output list
10775 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 here */.){. ass
10776 65 72 74 28 20 70 49 6e 21 3d 30 20 29 3b 0a 20 ert( pIn!=0 );.
10777 20 69 66 28 20 70 49 6e 2d 3e 70 4c 65 66 74 20 if( pIn->pLeft
10778 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f ){. struct Ro
10779 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 wSetEntry *p;.
1077a 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 rowSetTreeToLi
1077b 73 74 28 70 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 st(pIn->pLeft, p
1077c 70 46 69 72 73 74 2c 20 26 70 29 3b 0a 20 20 20 pFirst, &p);.
1077d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 70 49 6e p->pRight = pIn
1077e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
1077f 70 70 46 69 72 73 74 20 3d 20 70 49 6e 3b 0a 20 ppFirst = pIn;.
10780 20 7d 0a 20 20 69 66 28 20 70 49 6e 2d 3e 70 52 }. if( pIn->pR
10781 69 67 68 74 20 29 7b 0a 20 20 20 20 72 6f 77 53 ight ){. rowS
10782 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49 6e etTreeToList(pIn
10783 2d 3e 70 52 69 67 68 74 2c 20 26 70 49 6e 2d 3e ->pRight, &pIn->
10784 70 52 69 67 68 74 2c 20 70 70 4c 61 73 74 29 3b pRight, ppLast);
10785 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 . }else{. *p
10786 70 4c 61 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d pLast = pIn;. }
10787 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 4c . assert( (*ppL
10788 61 73 74 29 2d 3e 70 52 69 67 68 74 3d 3d 30 20 ast)->pRight==0
10789 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e );.}.../*.** Con
1078a 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69 vert a sorted li
1078b 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 28 st of elements (
1078c 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 52 69 connected by pRi
1078d 67 68 74 29 20 69 6e 74 6f 20 61 20 62 69 6e 61 ght) into a bina
1078e 72 79 0a 2a 2a 20 74 72 65 65 20 77 69 74 68 20 ry.** tree with
1078f 64 65 70 74 68 20 6f 66 20 69 44 65 70 74 68 2e depth of iDepth.
10790 20 20 41 20 64 65 70 74 68 20 6f 66 20 31 20 6d A depth of 1 m
10791 65 61 6e 73 20 74 68 65 20 74 72 65 65 20 63 6f eans the tree co
10792 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a ntains a single.
10793 2a 2a 20 6e 6f 64 65 20 74 61 6b 65 6e 20 66 72 ** node taken fr
10794 6f 6d 20 74 68 65 20 68 65 61 64 20 6f 66 20 2a om the head of *
10795 70 70 4c 69 73 74 2e 20 20 41 20 64 65 70 74 68 ppList. A depth
10796 20 6f 66 20 32 20 6d 65 61 6e 73 20 61 20 74 72 of 2 means a tr
10797 65 65 20 77 69 74 68 0a 2a 2a 20 74 68 72 65 65 ee with.** three
10798 20 6e 6f 64 65 73 2e 20 20 41 6e 64 20 73 6f 20 nodes. And so
10799 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 forth..**.** Use
1079a 20 61 73 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 as many entries
1079b 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 20 from the input
1079c 6c 69 73 74 20 61 73 20 72 65 71 75 69 72 65 64 list as required
1079d 20 61 6e 64 20 75 70 64 61 74 65 20 74 68 65 0a and update the.
1079e 2a 2a 20 2a 70 70 4c 69 73 74 20 74 6f 20 70 6f ** *ppList to po
1079f 69 6e 74 20 74 6f 20 74 68 65 20 75 6e 75 73 65 int to the unuse
107a0 64 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 d elements of th
107a1 65 20 6c 69 73 74 2e 20 20 49 66 20 74 68 65 20 e list. If the
107a2 69 6e 70 75 74 0a 2a 2a 20 6c 69 73 74 20 63 6f input.** list co
107a3 6e 74 61 69 6e 73 20 74 6f 6f 20 66 65 77 20 65 ntains too few e
107a4 6c 65 6d 65 6e 74 73 2c 20 74 68 65 6e 20 63 6f lements, then co
107a5 6e 73 74 72 75 63 74 20 61 6e 20 69 6e 63 6f 6d nstruct an incom
107a6 70 6c 65 74 65 20 74 72 65 65 0a 2a 2a 20 61 6e plete tree.** an
107a7 64 20 6c 65 61 76 65 20 2a 70 70 4c 69 73 74 20 d leave *ppList
107a8 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a set to NULL..**.
107a9 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
107aa 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 ter to the root
107ab 6f 66 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 of the construct
107ac 65 64 20 62 69 6e 61 72 79 20 74 72 65 65 2e 0a ed binary tree..
107ad 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
107ae 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f RowSetEntry *ro
107af 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 0a 20 wSetNDeepTree(.
107b0 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
107b1 74 72 79 20 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 try **ppList,.
107b2 69 6e 74 20 69 44 65 70 74 68 0a 29 7b 0a 20 20 int iDepth.){.
107b3 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
107b4 72 79 20 2a 70 3b 20 20 20 20 20 20 20 20 20 2f ry *p; /
107b5 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 6e 65 * Root of the ne
107b6 77 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 w tree */. stru
107b7 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
107b8 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 pLeft; /* Le
107b9 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 20 20 ft subtree */.
107ba 69 66 28 20 2a 70 70 4c 69 73 74 3d 3d 30 20 29 if( *ppList==0 )
107bb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
107bc 20 20 7d 0a 20 20 69 66 28 20 69 44 65 70 74 68 }. if( iDepth
107bd 3d 3d 31 20 29 7b 0a 20 20 20 20 70 20 3d 20 2a ==1 ){. p = *
107be 70 70 4c 69 73 74 3b 0a 20 20 20 20 2a 70 70 4c ppList;. *ppL
107bf 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b ist = p->pRight;
107c0 0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 . p->pLeft =
107c1 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 p->pRight = 0;.
107c2 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d return p;. }
107c3 0a 20 20 70 4c 65 66 74 20 3d 20 72 6f 77 53 65 . pLeft = rowSe
107c4 74 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69 73 tNDeepTree(ppLis
107c5 74 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 t, iDepth-1);.
107c6 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 20 69 p = *ppList;. i
107c7 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 f( p==0 ){. r
107c8 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d eturn pLeft;. }
107c9 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c . p->pLeft = pL
107ca 65 66 74 3b 0a 20 20 2a 70 70 4c 69 73 74 20 3d eft;. *ppList =
107cb 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d p->pRight;. p-
107cc 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53 65 74 >pRight = rowSet
107cd 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69 73 74 NDeepTree(ppList
107ce 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 72 , iDepth-1);. r
107cf 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
107d0 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 * Convert a sort
107d1 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 ed list of eleme
107d2 6e 74 73 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 nts into a binar
107d3 79 20 74 72 65 65 2e 20 4d 61 6b 65 20 74 68 65 y tree. Make the
107d4 20 74 72 65 65 0a 2a 2a 20 61 73 20 64 65 65 70 tree.** as deep
107d5 20 61 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 as it needs to
107d6 62 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 be in order to c
107d7 6f 6e 74 61 69 6e 20 74 68 65 20 65 6e 74 69 72 ontain the entir
107d8 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 e list..*/.stati
107d9 63 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 c struct RowSetE
107da 6e 74 72 79 20 2a 72 6f 77 53 65 74 4c 69 73 74 ntry *rowSetList
107db 54 6f 54 72 65 65 28 73 74 72 75 63 74 20 52 6f ToTree(struct Ro
107dc 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 69 73 74 wSetEntry *pList
107dd 29 7b 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b ){. int iDepth;
107de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
107df 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 pth of the tree
107e0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 73 74 72 75 so far */. stru
107e1 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
107e2 70 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 p; /* Curr
107e3 65 6e 74 20 74 72 65 65 20 72 6f 6f 74 20 2a 2f ent tree root */
107e4 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
107e5 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 Entry *pLeft;
107e6 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 /* Left subtree
107e7 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4c */.. assert( pL
107e8 69 73 74 21 3d 30 20 29 3b 0a 20 20 70 20 3d 20 ist!=0 );. p =
107e9 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d pList;. pList =
107ea 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d p->pRight;. p-
107eb 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 >pLeft = p->pRig
107ec 68 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 44 ht = 0;. for(iD
107ed 65 70 74 68 3d 31 3b 20 70 4c 69 73 74 3b 20 69 epth=1; pList; i
107ee 44 65 70 74 68 2b 2b 29 7b 0a 20 20 20 20 70 4c Depth++){. pL
107ef 65 66 74 20 3d 20 70 3b 0a 20 20 20 20 70 20 3d eft = p;. p =
107f0 20 70 4c 69 73 74 3b 0a 20 20 20 20 70 4c 69 73 pList;. pLis
107f1 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 t = p->pRight;.
107f2 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c p->pLeft = pL
107f3 65 66 74 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 eft;. p->pRig
107f4 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 ht = rowSetNDeep
107f5 54 72 65 65 28 26 70 4c 69 73 74 2c 20 69 44 65 Tree(&pList, iDe
107f6 70 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 pth);. }. retu
107f7 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rn p;.}../*.** C
107f8 6f 6e 76 65 72 74 20 74 68 65 20 6c 69 73 74 20 onvert the list
107f9 69 6e 20 70 2d 3e 70 45 6e 74 72 79 20 69 6e 74 in p->pEntry int
107fa 6f 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 o a sorted list
107fb 69 66 20 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 if it is not.**
107fc 73 6f 72 74 65 64 20 61 6c 72 65 61 64 79 2e 20 sorted already.
107fd 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 62 If there is a b
107fe 69 6e 61 72 79 20 74 72 65 65 20 6f 6e 20 70 2d inary tree on p-
107ff 3e 70 54 72 65 65 2c 20 74 68 65 6e 0a 2a 2a 20 >pTree, then.**
10800 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 convert it into
10801 61 20 6c 69 73 74 20 74 6f 6f 20 61 6e 64 20 6d a list too and m
10802 65 72 67 65 20 69 74 20 69 6e 74 6f 20 74 68 65 erge it into the
10803 20 70 2d 3e 70 45 6e 74 72 79 20 6c 69 73 74 2e p->pEntry list.
10804 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10805 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 52 6f 77 rowSetToList(Row
10806 53 65 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 Set *p){. if( !
10807 70 2d 3e 69 73 53 6f 72 74 65 64 20 29 7b 0a 20 p->isSorted ){.
10808 20 20 20 72 6f 77 53 65 74 53 6f 72 74 28 70 29 rowSetSort(p)
10809 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 ;. }. if( p->p
1080a 54 72 65 65 20 29 7b 0a 20 20 20 20 73 74 72 75 Tree ){. stru
1080b 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
1080c 70 48 65 61 64 2c 20 2a 70 54 61 69 6c 3b 0a 20 pHead, *pTail;.
1080d 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c rowSetTreeToL
1080e 69 73 74 28 70 2d 3e 70 54 72 65 65 2c 20 26 70 ist(p->pTree, &p
1080f 48 65 61 64 2c 20 26 70 54 61 69 6c 29 3b 0a 20 Head, &pTail);.
10810 20 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b p->pTree = 0;
10811 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d . p->pEntry =
10812 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70 2d 3e rowSetMerge(p->
10813 70 45 6e 74 72 79 2c 20 70 48 65 61 64 29 3b 0a pEntry, pHead);.
10814 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 }.}../*.** Ext
10815 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65 73 ract the smalles
10816 74 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 74 t element from t
10817 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 he RowSet..** Wr
10818 69 74 65 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 ite the element
10819 69 6e 74 6f 20 2a 70 52 6f 77 69 64 2e 20 20 52 into *pRowid. R
1081a 65 74 75 72 6e 20 31 20 6f 6e 20 73 75 63 63 65 eturn 1 on succe
1081b 73 73 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 30 ss. Return.** 0
1081c 20 69 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 if the RowSet i
1081d 73 20 61 6c 72 65 61 64 79 20 65 6d 70 74 79 2e s already empty.
1081e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 .**.** After thi
1081f 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 s routine has be
10820 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 73 en called, the s
10821 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
10822 72 74 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 rt().** routine
10823 6d 61 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 may not be calle
10824 64 20 61 67 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51 d again. .*/.SQ
10825 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10826 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 sqlite3RowSetNe
10827 78 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 xt(RowSet *p, i6
10828 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 72 6f 4 *pRowid){. ro
10829 77 53 65 74 54 6f 4c 69 73 74 28 70 29 3b 0a 20 wSetToList(p);.
1082a 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 if( p->pEntry )
1082b 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64 20 3d 20 {. *pRowid =
1082c 70 2d 3e 70 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 p->pEntry->v;.
1082d 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 2d p->pEntry = p-
1082e 3e 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b >pEntry->pRight;
1082f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 . if( p->pEnt
10830 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 ry==0 ){. s
10831 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 qlite3RowSetClea
10832 72 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 r(p);. }.
10833 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 return 1;. }els
10834 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b e{. return 0;
10835 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 . }.}../*.** Ch
10836 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 65 6c eck to see if el
10837 65 6d 65 6e 74 20 69 52 6f 77 69 64 20 77 61 73 ement iRowid was
10838 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 inserted into t
10839 68 65 20 74 68 65 20 72 6f 77 73 65 74 20 61 73 he the rowset as
1083a 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e 79 20 .** part of any
1083b 69 6e 73 65 72 74 20 62 61 74 63 68 20 70 72 69 insert batch pri
1083c 6f 72 20 74 6f 20 69 42 61 74 63 68 2e 20 20 52 or to iBatch. R
1083d 65 74 75 72 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f eturn 1 or 0..*/
1083e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1083f 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 int sqlite3RowSe
10840 74 54 65 73 74 28 52 6f 77 53 65 74 20 2a 70 52 tTest(RowSet *pR
10841 6f 77 53 65 74 2c 20 75 38 20 69 42 61 74 63 68 owSet, u8 iBatch
10842 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
10843 69 52 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63 iRowid){. struc
10844 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
10845 3b 0a 20 20 69 66 28 20 69 42 61 74 63 68 21 3d ;. if( iBatch!=
10846 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20 pRowSet->iBatch
10847 29 7b 0a 20 20 20 20 69 66 28 20 70 52 6f 77 53 ){. if( pRowS
10848 65 74 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 et->pEntry ){.
10849 20 20 20 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 rowSetToList
1084a 28 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 20 (pRowSet);.
1084b 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 20 pRowSet->pTree
1084c 3d 20 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 = rowSetListToTr
1084d 65 65 28 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 ee(pRowSet->pEnt
1084e 72 79 29 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 ry);. pRowS
1084f 65 74 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a et->pEntry = 0;.
10850 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 pRowSet->p
10851 4c 61 73 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Last = 0;. }.
10852 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 69 42 61 pRowSet->iBa
10853 74 63 68 20 3d 20 69 42 61 74 63 68 3b 0a 20 20 tch = iBatch;.
10854 7d 0a 20 20 70 20 3d 20 70 52 6f 77 53 65 74 2d }. p = pRowSet-
10855 3e 70 54 72 65 65 3b 0a 20 20 77 68 69 6c 65 28 >pTree;. while(
10856 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p ){. if( p-
10857 3e 76 3c 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 >v<iRowid ){.
10858 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 p = p->pRight
10859 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1085a 70 2d 3e 76 3e 69 52 6f 77 69 64 20 29 7b 0a 20 p->v>iRowid ){.
1085b 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 p = p->pLef
1085c 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
1085d 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1085e 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1085f 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 0;.}../********
10860 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 6f ****** End of ro
10861 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a wset.c *********
10862 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10863 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10864 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
10865 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
10866 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a e pager.c ******
10867 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10868 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10869 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1086a 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
1086b 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
1086c 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
1086d 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
1086e 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
1086f 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
10870 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
10871 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
10872 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
10873 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
10874 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
10875 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
10876 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
10877 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
10878 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
10879 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
1087a 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
1087b 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
1087c 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1087d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1087e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1087f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
10881 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 This is the imp
10882 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10883 68 65 20 70 61 67 65 20 63 61 63 68 65 20 73 75 he page cache su
10884 62 73 79 73 74 65 6d 20 6f 72 20 22 70 61 67 65 bsystem or "page
10885 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 70 r"..** .** The p
10886 61 67 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 ager is used to
10887 61 63 63 65 73 73 20 61 20 64 61 74 61 62 61 73 access a databas
10888 65 20 64 69 73 6b 20 66 69 6c 65 2e 20 20 49 74 e disk file. It
10889 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 implements.** a
1088a 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 61 6e 64 tomic commit and
1088b 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72 6f 75 67 rollback throug
1088c 68 20 74 68 65 20 75 73 65 20 6f 66 20 61 20 6a h the use of a j
1088d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 ournal file that
1088e 0a 2a 2a 20 69 73 20 73 65 70 61 72 61 74 65 20 .** is separate
1088f 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
10890 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 67 e file. The pag
10891 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e er also implemen
10892 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 ts file.** locki
10893 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 20 74 77 ng to prevent tw
10894 6f 20 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d o processes from
10895 20 77 72 69 74 69 6e 67 20 74 68 65 20 73 61 6d writing the sam
10896 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 e database.** fi
10897 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c le simultaneousl
10898 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f 63 65 73 y, or one proces
10899 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 74 s from reading t
1089a 68 65 20 64 61 74 61 62 61 73 65 20 77 68 69 6c he database whil
1089b 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 69 73 20 e.** another is
1089c 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 40 writing..**.** @
1089d 28 23 29 20 24 49 64 3a 20 70 61 67 65 72 2e 63 (#) $Id: pager.c
1089e 2c 76 20 31 2e 35 38 36 20 32 30 30 39 2f 30 35 ,v 1.586 2009/05
1089f 2f 30 36 20 31 38 3a 35 37 3a 31 30 20 73 68 61 /06 18:57:10 sha
108a0 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e ne Exp $.*/.#ifn
108a1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
108a2 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 DISKIO../*.** Ma
108a3 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65 cros for trouble
108a4 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 shooting. Norma
108a5 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a lly turned off.*
108a6 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 /.#if 0.int sqli
108a7 74 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b te3PagerTrace=1;
108a8 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
108a9 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 ble tracing */.#
108aa 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 define sqlite3De
108ab 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66 bugPrintf printf
108ac 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 .#define PAGERTR
108ad 41 43 45 28 58 29 20 20 20 20 20 69 66 28 20 73 ACE(X) if( s
108ae 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65 qlite3PagerTrace
108af 20 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67 ){ sqlite3Debug
108b0 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73 Printf X; }.#els
108b1 65 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 e.#define PAGERT
108b2 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a RACE(X).#endif..
108b3 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
108b4 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 ing two macros a
108b5 72 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 re used within t
108b6 68 65 20 50 41 47 45 52 54 52 41 43 45 28 29 20 he PAGERTRACE()
108b7 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 macros above.**
108b8 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c to print out fil
108b9 65 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a e-descriptors. .
108ba 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 **.** PAGERID()
108bb 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 takes a pointer
108bc 74 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63 to a Pager struc
108bd 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e t as its argumen
108be 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 t. The.** associ
108bf 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69 ated file-descri
108c0 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 ptor is returned
108c1 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 . FILEHANDLEID()
108c2 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 takes an sqlite
108c3 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 3_file.** struct
108c4 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 as its argument
108c5 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 ..*/.#define PAG
108c6 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 70 ERID(p) ((int)(p
108c7 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46 ->fd)).#define F
108c8 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20 ILEHANDLEID(fd)
108c9 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a ((int)fd)../*.**
108ca 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
108cb 61 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c as a whole is al
108cc 77 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 ways in one of t
108cd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
108ce 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
108cf 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 PAGER_UNLOCK
108d0 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 The page cac
108d1 68 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e he is not curren
108d2 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a tly reading or .
108d3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
108d4 20 20 20 20 20 20 20 20 20 77 72 69 74 69 6e 67 writing
108d5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
108d6 6c 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f le. There is no
108d7 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
108d8 20 20 20 20 20 20 20 20 20 20 64 61 74 61 20 68 data h
108d9 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 eld in memory.
108da 54 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 74 This is the init
108db 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ial.**
108dc 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 sta
108dd 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 te..**.** PAGE
108de 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 20 R_SHARED
108df 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 The page cache i
108e0 73 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 s reading the da
108e1 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 tabase..**
108e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108e3 20 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 Writing is not
108e4 70 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72 permitted. Ther
108e5 65 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 e can be.**
108e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108e7 20 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 multiple reade
108e8 72 73 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 rs accessing the
108e9 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a same database.*
108ea 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
108eb 20 20 20 20 20 20 20 20 66 69 6c 65 20 61 74 20 file at
108ec 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a the same time..*
108ed 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53 *.** PAGER_RES
108ee 45 52 56 45 44 20 20 20 20 20 20 54 68 69 73 20 ERVED This
108ef 70 72 6f 63 65 73 73 20 68 61 73 20 72 65 73 65 process has rese
108f0 72 76 65 64 20 74 68 65 20 64 61 74 61 62 61 73 rved the databas
108f1 65 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a e for writing.**
108f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108f3 20 20 20 20 20 20 20 62 75 74 20 68 61 73 20 6e but has n
108f4 6f 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20 ot yet made any
108f5 63 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f changes. Only o
108f6 6e 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 ne process.**
108f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
108f8 20 20 20 20 61 74 20 61 20 74 69 6d 65 20 63 61 at a time ca
108f9 6e 20 72 65 73 65 72 76 65 20 74 68 65 20 64 61 n reserve the da
108fa 74 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69 tabase. The ori
108fb 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 ginal.**
108fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
108fd 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
108fe 20 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 not been modifi
108ff 65 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 ed so other.**
10900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10901 20 20 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d processes m
10902 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64 ay still be read
10903 69 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a ing the on-disk.
10904 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
10905 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
10906 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 e file..**.**
10907 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
10908 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63 The page cac
10909 68 65 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 he is writing th
1090a 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 e database..**
1090b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1090c 20 20 20 20 20 41 63 63 65 73 73 20 69 73 20 65 Access is e
1090d 78 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 xclusive. No ot
1090e 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72 her processes or
1090f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
10910 20 20 20 20 20 20 20 20 20 20 74 68 72 65 61 64 thread
10911 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 s can be reading
10912 20 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c or writing whil
10913 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 e one.**
10914 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
10915 72 6f 63 65 73 73 20 69 73 20 77 72 69 74 69 6e rocess is writin
10916 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 g..**.** PAGER
10917 5f 53 59 4e 43 45 44 20 20 20 20 20 20 20 20 54 _SYNCED T
10918 68 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 he pager moves t
10919 6f 20 74 68 69 73 20 73 74 61 74 65 20 66 72 6f o this state fro
1091a 6d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 m PAGER_EXCLUSIV
1091b 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 E.**
1091c 20 20 20 20 20 20 20 20 20 20 20 61 66 74 65 72 after
1091d 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
1091e 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 have been writt
1091f 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 en to the.**
10920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10921 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 database file
10922 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61 and the file ha
10923 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f s been synced to
10924 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
10925 20 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20 disk.
10926 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73 All that remains
10927 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d to do is to rem
10928 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ove or.**
10929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1092a 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 truncate the jou
1092b 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 rnal file and th
1092c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a e transaction .*
1092d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1092e 20 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20 will be
1092f 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a committed..**.**
10930 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
10931 63 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45 comes up in PAGE
10932 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 R_UNLOCK. The f
10933 69 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 irst time a.** s
10934 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
10935 20 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74 61 occurs, the sta
10936 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 te transitions t
10937 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a o PAGER_SHARED..
10938 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67 ** After all pag
10939 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c es have been rel
1093a 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69 eased using sqli
1093b 74 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c te_page_unref(),
1093c 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74 72 .** the state tr
1093d 61 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 ansitions back t
1093e 6f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 o PAGER_UNLOCK.
1093f 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a The first time.
10940 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 ** that sqlite3P
10941 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 agerWrite() is c
10942 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65 alled, the state
10943 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a transitions to.
10944 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 ** PAGER_RESERVE
10945 44 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73 D. (Note that s
10946 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
10947 28 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a () can only be.*
10948 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f * called on an o
10949 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 utstanding page
1094a 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 which means that
1094b 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a the pager must.
1094c 2a 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53 ** be in PAGER_S
1094d 48 41 52 45 44 20 62 65 66 6f 72 65 20 69 74 20 HARED before it
1094e 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 transitions to P
1094f 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a AGER_RESERVED.).
10950 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 ** PAGER_RESERVE
10951 44 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 D means that the
10952 72 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f re is an open ro
10953 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a llback journal..
10954 2a 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f ** The transitio
10955 6e 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 n to PAGER_EXCLU
10956 53 49 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f SIVE occurs befo
10957 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a re any changes.*
10958 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 * are made to th
10959 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1095a 20 74 68 6f 75 67 68 20 77 72 69 74 65 73 20 74 though writes t
1095b 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a o the rollback.*
1095c 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 * journal occurs
1095d 20 77 69 74 68 20 6a 75 73 74 20 50 41 47 45 52 with just PAGER
1095e 5f 52 45 53 45 52 56 45 44 2e 20 20 41 66 74 65 _RESERVED. Afte
1095f 72 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 r an sqlite3Page
10960 72 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f rRollback().** o
10961 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f r sqlite3PagerCo
10962 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20 mmitPhaseTwo(),
10963 74 68 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f the state can go
10964 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 back to PAGER_S
10965 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 HARED,.** or it
10966 63 61 6e 20 73 74 61 79 20 61 74 20 50 41 47 45 can stay at PAGE
10967 52 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20 77 R_EXCLUSIVE if w
10968 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 e are in exclusi
10969 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a ve access mode..
1096a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
1096b 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 _UNLOCK 0.#
1096c 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41 define PAGER_SHA
1096d 52 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20 RED 1 /*
1096e 73 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c same as SHARED_L
1096f 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 OCK */.#define P
10970 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 AGER_RESERVED
10971 20 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 2 /* same as
10972 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f RESERVED_LOCK */
10973 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45 .#define PAGER_E
10974 58 43 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f XCLUSIVE 4 /
10975 2a 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53 * same as EXCLUS
10976 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 IVE_LOCK */.#def
10977 69 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44 ine PAGER_SYNCED
10978 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 5../*.** A
10979 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20 macro used for
1097a 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64 invoking the cod
1097b 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ec if there is o
1097c 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ne.*/.#ifdef SQL
1097d 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20 ITE_HAS_CODEC.#
1097e 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c define CODEC1(P,
1097f 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78 43 D,N,X) if( P->xC
10980 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78 43 odec!=0 ){ P->xC
10981 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 odec(P->pCodecAr
10982 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64 65 g,D,N,X); }.# de
10983 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c fine CODEC2(P,D,
10984 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50 2d N,X) ((char*)(P-
10985 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78 43 >xCodec!=0?P->xC
10986 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72 odec(P->pCodecAr
10987 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65 6c g,D,N,X):D)).#el
10988 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 se.# define CODE
10989 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20 4e C1(P,D,N,X) /* N
1098a 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 O-OP */.# define
1098b 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 29 CODEC2(P,D,N,X)
1098c 20 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e 64 ((char*)D).#end
1098d 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
1098e 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 ximum allowed se
1098f 63 74 6f 72 20 73 69 7a 65 2e 20 31 36 4d 42 2e ctor size. 16MB.
10990 20 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 If the xSectors
10991 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a ize() method .**
10992 20 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 returns a value
10993 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 larger than thi
10994 73 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 s, then MAX_SECT
10995 4f 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 OR_SIZE is used
10996 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 instead..** This
10997 20 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 could conceivab
10998 6c 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 ly cause corrupt
10999 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 ion following a
1099a 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e power failure on
1099b 0a 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 .** such a syste
1099c 6d 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65 m. This is curre
1099d 6e 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 ntly an undocume
1099e 6e 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 nted limit..*/.#
1099f 64 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f define MAX_SECTO
109a0 52 5f 53 49 5a 45 20 30 78 30 31 30 30 30 30 30 R_SIZE 0x0100000
109a1 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
109a2 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
109a3 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 wing structure i
109a4 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
109a5 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 each active.** s
109a6 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 avepoint and sta
109a7 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
109a8 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d on in the system
109a9 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63 . All such struc
109aa 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f tures.** are sto
109ab 72 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72 red in the Pager
109ac 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 .aSavepoint[] ar
109ad 72 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c ray, which is al
109ae 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 located and.** r
109af 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c esized using sql
109b0 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a ite3Realloc()..*
109b1 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65 *.** When a save
109b2 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64 point is created
109b3 2c 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 , the PagerSavep
109b4 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 oint.iHdrOffset
109b5 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 field is.** set
109b6 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e to 0. If a journ
109b7 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 al-header is wri
109b8 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 tten into the ma
109b9 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 in journal while
109ba 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e .** the savepoin
109bb 74 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65 t is active, the
109bc 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 n iHdrOffset is
109bd 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 set to the byte
109be 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 offset .** immed
109bf 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 iately following
109c0 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 the last journa
109c1 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e l record written
109c2 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a into the main.*
109c3 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 * journal before
109c4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 the journal-hea
109c5 64 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71 der. This is req
109c6 75 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76 uired during sav
109c7 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 epoint.** rollba
109c8 63 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61 ck (see pagerPla
109c9 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 ybackSavepoint()
109ca 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 )..*/.typedef st
109cb 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f ruct PagerSavepo
109cc 69 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 int PagerSavepoi
109cd 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72 nt;.struct Pager
109ce 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 Savepoint {. i6
109cf 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 4 iOffset;
109d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
109d1 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e arting offset in
109d2 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f main journal */
109d3 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65 . i64 iHdrOffse
109d4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
109d5 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a /* See above */.
109d6 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76 Bitvec *pInSav
109d7 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f epoint; /
109d8 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69 * Set of pages i
109d9 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74 n this savepoint
109da 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 */. Pgno nOrig
109db 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
109dc 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e /* Original n
109dd 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
109de 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f n file */. Pgno
109df 20 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 iSubRec;
109e0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
109e1 78 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72 x of first recor
109e2 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c d in sub-journal
109e3 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
109e4 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20 open page cache
109e5 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
109e6 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
109e7 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
109e8 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 errCode.**.**
109e9 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d Pager.errCode m
109ea 61 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c ay be set to SQL
109eb 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 ITE_IOERR, SQLIT
109ec 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a E_CORRUPT, or.**
109ed 20 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c or SQLITE_FUL
109ee 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 L. Once one of t
109ef 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 65 he first three e
109f0 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74 rrors occurs, it
109f1 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 20 61 persists.** a
109f2 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 nd is returned a
109f3 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 s the result of
109f4 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 every major page
109f5 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 r API call. The
109f6 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c .** SQLITE_FUL
109f7 4c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 L return code is
109f8 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 slightly differ
109f9 65 6e 74 2e 20 49 74 20 70 65 72 73 69 73 74 73 ent. It persists
109fa 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a only until the.
109fb 2a 2a 20 20 20 6e 65 78 74 20 73 75 63 63 65 73 ** next succes
109fc 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 sful rollback is
109fd 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 performed on th
109fe 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 41 e pager cache. A
109ff 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45 lso,.** SQLITE
10a00 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 _FULL does not a
10a01 66 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 ffect the sqlite
10a02 33 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20 3PagerGet() and
10a03 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b sqlite3PagerLook
10a04 75 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20 up().** APIs,
10a05 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 they may still b
10a06 65 20 75 73 65 64 20 73 75 63 63 65 73 73 66 75 e used successfu
10a07 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a lly..**.** dbSiz
10a08 65 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20 eValid, dbSize,
10a09 64 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69 dbOrigSize, dbFi
10a0a 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d leSize.**.** M
10a0b 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65 anaging the size
10a0c 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
10a0d 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69 file in pages i
10a0e 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c s a little compl
10a0f 69 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65 icated..** The
10a10 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e variable Pager.
10a11 64 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 dbSize contains
10a12 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
10a13 67 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 ges that the dat
10a14 61 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65 abase.** image
10a15 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 currently conta
10a16 69 6e 73 2e 20 41 73 20 74 68 65 20 64 61 74 61 ins. As the data
10a17 62 61 73 65 20 69 6d 61 67 65 20 67 72 6f 77 73 base image grows
10a18 20 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 69 73 or shrinks this
10a19 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 69 .** variable i
10a1a 73 20 75 70 64 61 74 65 64 2e 20 54 68 65 20 76 s updated. The v
10a1b 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 ariable Pager.db
10a1c 46 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e FileSize contain
10a1d 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 s the number.**
10a1e 20 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 of pages in th
10a1f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
10a20 20 54 68 69 73 20 6d 61 79 20 62 65 20 64 69 66 This may be dif
10a21 66 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 67 65 ferent from Page
10a22 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66 r.dbSize.** if
10a23 20 73 6f 6d 65 20 70 61 67 65 73 20 68 61 76 65 some pages have
10a24 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64 20 74 been appended t
10a25 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 o the database i
10a26 6d 61 67 65 20 62 75 74 20 6e 6f 74 20 79 65 74 mage but not yet
10a27 20 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 written.** ou
10a28 74 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 t from the cache
10a29 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 to the actual f
10a2a 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 ile on disk. Or
10a2b 69 66 20 74 68 65 20 69 6d 61 67 65 20 68 61 73 if the image has
10a2c 20 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 been.** trunc
10a2d 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63 72 65 ated by an incre
10a2e 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70 mental-vacuum op
10a2f 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 61 67 eration. The Pag
10a30 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 76 61 er.dbOrigSize va
10a31 72 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 riable.** cont
10a32 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 ains the number
10a33 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
10a34 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 database image w
10a35 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a hen the current.
10a36 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
10a37 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 was opened. The
10a38 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c contents of all
10a39 20 74 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 three of these
10a3a 76 61 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 variables is.**
10a3b 20 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 only guarantee
10a3c 64 20 74 6f 20 62 65 20 63 6f 72 72 65 63 74 20 d to be correct
10a3d 69 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50 if the boolean P
10a3e 61 67 65 72 2e 64 62 53 69 7a 65 56 61 6c 69 64 ager.dbSizeValid
10a3f 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 is true..**.**
10a40 20 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68 TODO: Under wh
10a41 61 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 at conditions is
10a42 20 64 62 53 69 7a 65 56 61 6c 69 64 20 73 65 74 dbSizeValid set
10a43 3f 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a ? Cleared?.**.**
10a44 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
10a45 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f .**.** This bo
10a46 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 20 69 olean variable i
10a47 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 s used to make s
10a48 75 72 65 20 74 68 61 74 20 74 68 65 20 63 68 61 ure that the cha
10a49 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 nge-counter .**
10a4a 20 20 28 74 68 65 20 34 2d 62 79 74 65 20 68 65 (the 4-byte he
10a4b 61 64 65 72 20 66 69 65 6c 64 20 61 74 20 62 79 ader field at by
10a4c 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 20 te offset 24 of
10a4d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
10a4e 65 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 e) is .** not
10a4f 75 70 64 61 74 65 64 20 6d 6f 72 65 20 6f 66 74 updated more oft
10a50 65 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 en than necessar
10a51 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 y. .**.** It i
10a52 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 77 68 s set to true wh
10a53 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f en the change-co
10a54 75 6e 74 65 72 20 66 69 65 6c 64 20 69 73 20 75 unter field is u
10a55 70 64 61 74 65 64 2c 20 77 68 69 63 68 20 0a 2a pdated, which .*
10a56 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 * can only hap
10a57 70 65 6e 20 69 66 20 61 6e 20 65 78 63 6c 75 73 pen if an exclus
10a58 69 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 ive lock is held
10a59 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
10a5a 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69 file..** It i
10a5b 73 20 63 6c 65 61 72 65 64 20 28 73 65 74 20 74 s cleared (set t
10a5c 6f 20 66 61 6c 73 65 29 20 77 68 65 6e 65 76 65 o false) wheneve
10a5d 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c r an exclusive l
10a5e 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c ock is .** rel
10a5f 69 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 68 65 inquished on the
10a60 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
10a61 45 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e Each time a tran
10a62 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 saction is commi
10a63 74 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63 tted,.** The c
10a64 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 hangeCountDone f
10a65 6c 61 67 20 69 73 20 69 6e 73 70 65 63 74 65 64 lag is inspected
10a66 2e 20 49 66 20 69 74 20 69 73 20 74 72 75 65 2c . If it is true,
10a67 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 the work of.**
10a68 20 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 63 updating the c
10a69 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 hange-counter is
10a6a 20 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 68 65 omitted for the
10a6b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
10a6c 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 tion..**.** Th
10a6d 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61 is mechanism mea
10a6e 6e 73 20 74 68 61 74 20 77 68 65 6e 20 72 75 6e ns that when run
10a6f 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 ning in exclusiv
10a70 65 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63 e mode, a connec
10a71 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20 tion .** need
10a72 6f 6e 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 only update the
10a73 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f change-counter o
10a74 6e 63 65 2c 20 66 6f 72 20 74 68 65 20 66 69 72 nce, for the fir
10a75 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a st transaction.*
10a76 2a 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a * committed..*
10a77 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a *.** dbModified.
10a78 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f **.** The dbMo
10a79 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 73 dified flag is s
10a7a 65 74 20 77 68 65 6e 65 76 65 72 20 61 20 64 61 et whenever a da
10a7b 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 64 tabase page is d
10a7c 69 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20 irtied..** It
10a7d 69 73 20 63 6c 65 61 72 65 64 20 61 74 20 74 68 is cleared at th
10a7e 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74 72 e end of each tr
10a7f 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
10a80 20 20 20 49 74 20 69 73 20 75 73 65 64 20 77 68 It is used wh
10a81 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 en committing or
10a82 20 6f 74 68 65 72 77 69 73 65 20 65 6e 64 69 6e otherwise endin
10a83 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e g a transaction.
10a84 20 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d If.** the dbM
10a85 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 odified flag is
10a86 63 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 73 20 clear then less
10a87 77 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 20 64 work has to be d
10a88 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e one..**.** journ
10a89 61 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 alStarted.**.**
10a8a 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73 This flag is s
10a8b 65 74 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 et whenever the
10a8c 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
10a8d 20 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a is synced. .**.
10a8e 2a 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 20 6f ** The point o
10a8f 66 20 74 68 69 73 20 66 6c 61 67 20 69 73 20 74 f this flag is t
10a90 68 61 74 20 69 74 20 6d 75 73 74 20 62 65 20 73 hat it must be s
10a91 65 74 20 61 66 74 65 72 20 74 68 65 20 0a 2a 2a et after the .**
10a92 20 20 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c first journal
10a93 20 68 65 61 64 65 72 20 69 6e 20 61 20 6a 6f 75 header in a jou
10a94 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 62 65 rnal file has be
10a95 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 en synced to dis
10a96 6b 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 74 68 k..** After th
10a97 69 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c is has happened,
10a98 20 6e 65 77 20 70 61 67 65 73 20 61 70 70 65 6e new pages appen
10a99 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 ded to the datab
10a9a 61 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 ase .** do not
10a9b 20 6e 65 65 64 20 74 68 65 20 50 47 48 44 52 5f need the PGHDR_
10a9c 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 73 NEED_SYNC flag s
10a9d 65 74 2c 20 61 73 20 74 68 65 79 20 64 6f 20 6e et, as they do n
10a9e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20 ot need.** to
10a9f 77 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e wait for a journ
10aa0 61 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 20 74 al sync before t
10aa1 68 65 79 20 63 61 6e 20 62 65 20 77 72 69 74 74 hey can be writt
10aa2 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74 en out to.** t
10aa3 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
10aa4 20 28 73 65 65 20 66 75 6e 63 74 69 6f 6e 20 70 (see function p
10aa5 61 67 65 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a ager_write())..*
10aa6 2a 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65 * .** setMaste
10aa7 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76 r.**.** This v
10aa8 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 ariable is used
10aa9 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 to ensure that t
10aaa 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
10aab 6c 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 l file name.**
10aac 20 28 69 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c (if any) is onl
10aad 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 y written into t
10aae 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
10aaf 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 once..**.** Wh
10ab0 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 en committing a
10ab1 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
10ab2 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
10ab3 66 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 61 6e file name (if an
10ab4 79 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 20 77 y).** may be w
10ab5 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
10ab6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 69 journal file whi
10ab7 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 le the pager is
10ab8 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 still in.** PA
10ab9 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61 GER_RESERVED sta
10aba 74 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 50 68 te (see CommitPh
10abb 61 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 68 65 aseOne() for the
10abc 20 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 action). It.**
10abd 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 then attempts
10abe 74 6f 20 75 70 67 72 61 64 65 20 74 6f 20 61 6e to upgrade to an
10abf 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e exclusive lock.
10ac0 20 49 66 20 74 68 69 73 20 61 74 74 65 6d 70 74 If this attempt
10ac1 0a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 68 65 .** fails, the
10ac2 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 61 n SQLITE_BUSY ma
10ac3 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 74 6f y be returned to
10ac4 20 74 68 65 20 75 73 65 72 20 61 6e 64 20 74 68 the user and th
10ac5 65 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20 e user.** may
10ac6 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 attempt to commi
10ac7 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
10ac8 6e 20 61 67 61 69 6e 20 6c 61 74 65 72 20 28 63 n again later (c
10ac9 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d alling.** Comm
10aca 69 74 50 68 61 73 65 4f 6e 65 28 29 20 61 67 61 itPhaseOne() aga
10acb 69 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 20 69 in). This flag i
10acc 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 s used to ensure
10acd 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 20 20 that the .**
10ace 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
10acf 61 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 ame is only writ
10ad0 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
10ad1 61 6c 20 66 69 6c 65 20 74 68 65 20 66 69 72 73 al file the firs
10ad2 74 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d t.** time Comm
10ad3 69 74 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20 itPhaseOne() is
10ad4 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f called..**.** do
10ad5 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 NotSync.**.**
10ad6 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 This variable is
10ad7 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 65 64 set and cleared
10ad8 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 by sqlite3Pager
10ad9 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e Write()..**.** n
10ada 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 eedSync.**.**
10adb 54 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 20 62 TODO: It might b
10adc 65 20 65 61 73 69 65 72 20 74 6f 20 73 65 74 20 e easier to set
10add 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e this variable in
10ade 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 writeJournalHdr
10adf 28 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 69 74 ().** and writ
10ae0 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 eMasterJournal()
10ae1 20 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 69 74 only. Change it
10ae2 73 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e s meaning to "un
10ae3 73 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a 20 20 synced data.**
10ae4 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 has been writte
10ae5 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
10ae6 22 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d "..**.** subjInM
10ae7 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 emory.**.** Th
10ae8 69 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 is is a boolean
10ae9 76 61 72 69 61 62 6c 65 2e 20 49 66 20 74 72 75 variable. If tru
10aea 65 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 e, then any requ
10aeb 69 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c ired sub-journal
10aec 0a 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 64 20 .** is opened
10aed 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 as an in-memory
10aee 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 journal file. If
10aef 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d false, then in-
10af0 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d memory.** sub-
10af1 6a 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c journals are onl
10af2 79 20 75 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65 y used for in-me
10af3 6d 6f 72 79 20 70 61 67 65 72 20 66 69 6c 65 73 mory pager files
10af4 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65 ..*/.struct Page
10af5 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 r {. sqlite3_vf
10af6 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 s *pVfs;
10af7 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e /* OS function
10af8 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 s to use for IO
10af9 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 69 76 */. u8 exclusiv
10afa 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 eMode;
10afb 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 /* Boolean. Tru
10afc 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 e if locking_mod
10afd 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a e==EXCLUSIVE */.
10afe 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 u8 journalMode
10aff 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
10b00 20 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 45 52 On of the PAGER
10b01 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 _JOURNALMODE_* v
10b02 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 75 73 alues */. u8 us
10b03 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 eJournal;
10b04 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 /* Use a
10b05 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
10b06 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f on this file */
10b07 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b . u8 noReadlock
10b08 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
10b09 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 * Do not bother
10b0a 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f to obtain readlo
10b0b 63 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 cks */. u8 noSy
10b0c 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
10b0d 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 /* Do not s
10b0e 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ync the journal
10b0f 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 if true */. u8
10b10 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 fullSync;
10b11 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 /* Do e
10b12 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 xtra syncs of th
10b13 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f e journal for ro
10b14 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 bustness */. u8
10b15 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 sync_flags;
10b16 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
10b17 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 of SYNC_NORMAL
10b18 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a or SYNC_FULL */.
10b19 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 u8 tempFile;
10b1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b1b 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 zFilename is a
10b1c 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a temporary file *
10b1d 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b /. u8 readOnly;
10b1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b1f 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65 /* True for a re
10b20 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 ad-only database
10b21 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20 */. u8 memDb;
10b22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b23 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 68 /* True to inh
10b24 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f ibit all file I/
10b25 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 O */.. /* The f
10b26 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63 ollowing block c
10b27 6f 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 63 6c ontains those cl
10b28 61 73 73 20 6d 65 6d 62 65 72 73 20 74 68 61 74 ass members that
10b29 20 61 72 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 are dynamically
10b2a 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64 . ** modified d
10b2b 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 uring normal ope
10b2c 72 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68 rations. The oth
10b2d 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 er variables in
10b2e 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 20 this structure.
10b2f 20 2a 2a 20 61 72 65 20 65 69 74 68 65 72 20 63 ** are either c
10b30 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f onstant througho
10b31 75 74 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 ut the lifetime
10b32 6f 66 20 74 68 65 20 70 61 67 65 72 2c 20 6f 72 of the pager, or
10b33 20 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20 else. ** used
10b34 74 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 67 75 to store configu
10b35 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 ration parameter
10b36 73 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 s that affect th
10b37 65 20 77 61 79 20 74 68 65 20 70 61 67 65 72 20 e way the pager
10b38 0a 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a . ** operates..
10b39 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73 **. ** The 's
10b3a 74 61 74 65 27 20 76 61 72 69 61 62 6c 65 20 69 tate' variable i
10b3b 73 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 6d s described in m
10b3c 6f 72 65 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67 ore detail along
10b3d 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 64 with the. ** d
10b3e 65 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 20 74 escriptions of t
10b3f 68 65 20 76 61 6c 75 65 73 20 69 74 20 6d 61 79 he values it may
10b40 20 74 61 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e take - PAGER_UN
10b41 4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f LOCK etc. Many o
10b42 66 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 f the. ** other
10b43 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 variables in th
10b44 69 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 65 73 is block are des
10b45 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 63 6f cribed in the co
10b46 6d 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 20 0a mment directly .
10b47 20 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 ** above this
10b48 63 6c 61 73 73 20 64 65 66 69 6e 69 74 69 6f 6e class definition
10b49 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 .. */. u8 stat
10b4a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
10b4b 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e /* PAGER_UN
10b4c 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f LOCK, _SHARED, _
10b4d 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a RESERVED, etc. *
10b4e 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 /. u8 dbModifie
10b4f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
10b50 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 /* True if there
10b51 20 61 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 are any changes
10b52 20 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 to the Db */.
10b53 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 u8 needSync;
10b54 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
10b55 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 28 rue if an fsync(
10b56 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 ) is needed on t
10b57 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 he journal */.
10b58 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 u8 journalStarte
10b59 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 d; /* T
10b5a 72 75 65 20 69 66 20 68 65 61 64 65 72 20 6f 66 rue if header of
10b5b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 journal is sync
10b5c 65 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 ed */. u8 chang
10b5d 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 eCountDone;
10b5e 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74 65 72 /* Set after
10b5f 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 incrementing th
10b60 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
10b61 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 73 74 */. u8 setMast
10b62 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 er;
10b63 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6d /* True if a m
10b64 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e -j name has been
10b65 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c written to jrnl
10b66 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79 */. u8 doNotSy
10b67 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
10b68 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 /* Boolean. Wh
10b69 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74 ile true, do not
10b6a 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 spill the cache
10b6b 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a 65 56 */. u8 dbSizeV
10b6c 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 alid;
10b6d 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 64 62 /* Set when db
10b6e 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63 74 20 Size is correct
10b6f 2a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e 4d 65 */. u8 subjInMe
10b70 6d 6f 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 mory;
10b71 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 65 20 /* True to use
10b72 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f in-memory sub-jo
10b73 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f urnals */. Pgno
10b74 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 dbSize;
10b75 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
10b76 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
10b77 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
10b78 50 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b Pgno dbOrigSize;
10b79 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 /* d
10b7a 62 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 bSize before the
10b7b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
10b7c 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 tion */. Pgno d
10b7d 62 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 bFileSize;
10b7e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10b7f 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
10b80 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
10b81 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 . int errCode;
10b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10b83 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c * One of several
10b84 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 kinds of errors
10b85 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 */. int nRec;
10b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b87 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e /* Pages journ
10b88 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 alled since last
10b89 20 6a 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 j-header writte
10b8a 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d n */. u32 cksum
10b8b 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 Init;
10b8c 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 /* Quasi-rand
10b8d 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 om value added t
10b8e 6f 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d o every checksum
10b8f 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 */. u32 nSubRe
10b90 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
10b91 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
10b92 65 63 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 ecords written t
10b93 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f o sub-journal */
10b94 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f . Bitvec *pInJo
10b95 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f urnal; /
10b96 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 * One bit for ea
10b97 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ch page in the d
10b98 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
10b99 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
10b9a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a fd; /*
10b9b 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
10b9c 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f for database */
10b9d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
10b9e 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f *jfd; /
10b9f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f * File descripto
10ba0 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e r for main journ
10ba1 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f al */. sqlite3_
10ba2 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 file *sjfd;
10ba3 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 /* File desc
10ba4 72 69 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a riptor for sub-j
10ba5 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 ournal */. i64
10ba6 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 journalOff;
10ba7 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
10ba8 6e 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 nt write offset
10ba9 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
10baa 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 ile */. i64 jou
10bab 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 rnalHdr;
10bac 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 /* Byte off
10bad 73 65 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 set to previous
10bae 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a journal header *
10baf 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 /. PagerSavepoi
10bb0 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 nt *aSavepoint;
10bb1 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 /* Array of acti
10bb2 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f ve savepoints */
10bb3 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e . int nSavepoin
10bb4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t; /
10bb5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d * Number of elem
10bb6 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 ents in aSavepoi
10bb7 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 nt[] */. char d
10bb8 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 bFileVers[16];
10bb9 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 /* Changes
10bba 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 whenever databa
10bbb 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 se file changes
10bbc 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 */. u32 sectorS
10bbd 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
10bbe 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 /* Assumed sect
10bbf 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 or size during r
10bc0 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 69 6e ollback */.. in
10bc1 74 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 t nExtra;
10bc2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
10bc3 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 this many bytes
10bc4 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f to each in-memo
10bc5 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 ry page */. u32
10bc6 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 vfsFlags;
10bc7 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
10bc8 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 s for sqlite3_vf
10bc9 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 s.xOpen() */. i
10bca 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 nt pageSize;
10bcb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
10bcc 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
10bcd 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e a page */. Pgn
10bce 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 o mxPgno;
10bcf 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
10bd0 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 mum allowed size
10bd1 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
10bd2 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c */. char *zFil
10bd3 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 ename;
10bd4 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
10bd5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
10bd6 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e /. char *zJourn
10bd7 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 al;
10bd8 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a /* Name of the j
10bd9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
10bda 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 int (*xBusyHand
10bdb 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 ler)(void*); /*
10bdc 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c Function to call
10bdd 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 when busy */.
10bde 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c void *pBusyHandl
10bdf 65 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 erArg; /* C
10be0 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 ontext argument
10be1 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 for xBusyHandler
10be2 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
10be3 45 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 E_TEST. int nHi
10be4 74 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 t, nMiss;
10be5 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 /* Cache hi
10be6 74 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a ts and missing *
10be7 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e /. int nRead, n
10be8 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 Write;
10be9 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 /* Database page
10bea 73 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a s read/written *
10beb 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 /.#endif. void
10bec 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 (*xReiniter)(DbP
10bed 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 age*); /* Call t
10bee 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e his routine when
10bef 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 reloading pages
10bf0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
10bf1 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f E_HAS_CODEC. vo
10bf2 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f id *(*xCodec)(vo
10bf3 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 id*,void*,Pgno,i
10bf4 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 nt); /* Routine
10bf5 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 for en/decoding
10bf6 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a data */. void *
10bf7 70 43 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20 pCodecArg;
10bf8 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
10bf9 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 rgument to xCode
10bfa 63 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 c() */.#endif.
10bfb 63 68 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b char *pTmpSpace;
10bfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
10bfd 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 ager.pageSize by
10bfe 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 tes of space for
10bff 20 74 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 tmp use */. i6
10c00 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 4 journalSizeLim
10c01 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a it; /* Siz
10c02 65 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 e limit for pers
10c03 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 istent journal f
10c04 69 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 iles */. PCache
10c05 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 20 *pPCache;
10c06 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
10c07 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f to page cache o
10c08 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 bject */. sqlit
10c09 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b e3_backup *pBack
10c0a 75 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 up; /* Pointe
10c0b 72 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 r to list of ong
10c0c 6f 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f 63 oing backup proc
10c0d 65 73 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a esses */.};../*.
10c0e 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
10c0f 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
10c10 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 s hold counters
10c11 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 used for.** test
10c12 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c ing purposes onl
10c13 79 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 y. These variab
10c14 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 les do not exist
10c15 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 in.** a non-tes
10c16 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 ting build. The
10c17 73 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 se variables are
10c18 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 not thread-safe
10c19 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
10c1a 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
10c1b 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 PI int sqlite3_p
10c1c 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e ager_readdb_coun
10c1d 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d t = 0; /* Num
10c1e 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 ber of full page
10c1f 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a s read from DB *
10c20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
10c21 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 sqlite3_pager_w
10c22 72 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 ritedb_count = 0
10c23 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 ; /* Number of
10c24 20 66 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 full pages writ
10c25 74 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c ten to DB */.SQL
10c26 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
10c27 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a te3_pager_writej
10c28 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f _count = 0; /
10c29 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
10c2a 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 s written to jou
10c2b 72 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 rnal */.# define
10c2c 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 PAGER_INCR(v)
10c2d 76 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 v++.#else.# defi
10c2e 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 ne PAGER_INCR(v)
10c2f 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a .#endif..../*.**
10c30 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 Journal files b
10c31 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f egin with the fo
10c32 6c 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 llowing magic st
10c33 72 69 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a ring. The data.
10c34 2a 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 ** was obtained
10c35 66 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d from /dev/random
10c36 2e 20 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e . It is used on
10c37 6c 79 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 ly as a sanity c
10c38 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 heck..**.** Sinc
10c39 65 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c e version 2.8.0,
10c3a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 the journal for
10c3b 6d 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 mat contains add
10c3c 69 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a itional sanity.*
10c3d 2a 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 * checking infor
10c3e 6d 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 mation. If the
10c3f 70 6f 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c power fails whil
10c40 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 e the journal is
10c41 20 62 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 being.** writte
10c42 6e 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 n, semi-random g
10c43 61 72 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 arbage data migh
10c44 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 t appear in the
10c45 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 journal.** file
10c46 61 66 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 after power is r
10c47 65 73 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 estored. If an
10c48 61 74 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 attempt is then
10c49 6d 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 made.** to roll
10c4a 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b the journal back
10c4b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 , the database c
10c4c 6f 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 ould be corrupte
10c4d 64 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e d. The addition
10c4e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 al.** sanity che
10c4f 63 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e cking data is an
10c50 20 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 attempt to disc
10c51 6f 76 65 72 20 74 68 65 20 67 61 72 62 61 67 65 over the garbage
10c52 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e in the.** journ
10c53 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 al and ignore it
10c54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 ..**.** The sani
10c55 74 79 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f ty checking info
10c56 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 rmation for the
10c57 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d new journal form
10c58 61 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f at consists.** o
10c59 66 20 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b f a 32-bit check
10c5a 73 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 sum on each page
10c5b 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 of data. The c
10c5c 68 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 hecksum covers b
10c5d 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 oth.** the page
10c5e 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 number and the p
10c5f 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 Pager->pageSize
10c60 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f bytes of data fo
10c61 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 r the page..** T
10c62 68 69 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 his cksum is ini
10c63 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 tialized to a 32
10c64 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 -bit random valu
10c65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69 e that appears i
10c66 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c n the.** journal
10c67 20 66 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 file right afte
10c68 72 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 r the header. T
10c69 68 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 he random initia
10c6a 6c 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 lizer is importa
10c6b 6e 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 nt,.** because g
10c6c 61 72 62 61 67 65 20 64 61 74 61 20 74 68 61 74 arbage data that
10c6d 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 appears at the
10c6e 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c end of a journal
10c6f 20 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 is likely.** da
10c70 74 61 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 ta that was once
10c71 20 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 in other files
10c72 74 68 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 that have now be
10c73 65 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 en deleted. If
10c74 74 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 the.** garbage d
10c75 61 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e ata came from an
10c76 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 obsolete journa
10c77 6c 20 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 l file, the chec
10c78 6b 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 ksums might.** b
10c79 65 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 e correct. But
10c7a 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 by initializing
10c7b 74 68 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 the checksum to
10c7c 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 random value whi
10c7d 63 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 ch.** is differe
10c7e 6e 74 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 nt for every jou
10c7f 72 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a rnal, we minimiz
10c80 65 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a e that risk..*/.
10c81 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
10c82 69 67 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 igned char aJour
10c83 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 nalMagic[] = {.
10c84 20 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 0xd9, 0xd5, 0x0
10c85 35 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 5, 0xf9, 0x20, 0
10c86 78 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c xa1, 0x63, 0xd7,
10c87 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 .};../*.** The s
10c88 69 7a 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61 ize of the of ea
10c89 63 68 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 ch page record i
10c8a 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 n the journal is
10c8b 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 given by.** the
10c8c 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
10c8d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 ..*/.#define JOU
10c8e 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
10c8f 72 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 r) ((pPager->pa
10c90 67 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a geSize) + 8)../*
10c91 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 .** The journal
10c92 68 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 header size for
10c93 74 68 69 73 20 70 61 67 65 72 2e 20 54 68 69 73 this pager. This
10c94 20 69 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20 is usually the
10c95 73 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 same .** size as
10c96 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 a single disk s
10c97 65 63 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 ector. See also
10c98 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e setSectorSize().
10c99 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 .*/.#define JOUR
10c9a 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
10c9b 72 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 r) (pPager->sect
10c9c 6f 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 orSize)../*.** T
10c9d 68 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 he macro MEMDB i
10c9e 73 20 74 72 75 65 20 69 66 20 77 65 20 61 72 65 s true if we are
10c9f 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e dealing with an
10ca0 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
10ca1 61 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 ase..** We do th
10ca2 69 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f is as a macro so
10ca3 20 74 68 61 74 20 69 66 20 74 68 65 20 53 51 4c that if the SQL
10ca4 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 ITE_OMIT_MEMORYD
10ca5 42 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a B macro is set,.
10ca6 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
10ca7 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 MEMDB will be a
10ca8 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 constant and the
10ca9 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f compiler will o
10caa 70 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 ptimize.** out c
10cab 6f 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e ode that would n
10cac 65 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f ever execute..*/
10cad 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
10cae 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 MIT_MEMORYDB.# d
10caf 65 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 efine MEMDB 0.#e
10cb0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d lse.# define MEM
10cb1 44 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 DB pPager->memDb
10cb2 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
10cb3 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c he maximum legal
10cb4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
10cb5 28 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 (2^31 - 1)..*/.#
10cb6 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 define PAGER_MAX
10cb7 5f 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 _PGNO 2147483647
10cb8 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
10cb9 20 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a ./*.** Usage:.*
10cba 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 *.** assert( a
10cbb 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 ssert_pager_stat
10cbc 65 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a e(pPager) );.*/.
10cbd 73 74 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 static int asser
10cbe 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 t_pager_state(Pa
10cbf 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 ger *pPager){..
10cc0 20 2f 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 /* A temp-file
10cc1 69 73 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47 is always in PAG
10cc2 45 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20 ER_EXCLUSIVE or
10cc3 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 PAGER_SYNCED sta
10cc4 74 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 te. */. assert(
10cc5 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
10cc6 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e e==0 || pPager->
10cc7 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
10cc8 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 LUSIVE );.. /*
10cc9 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 The changeCountD
10cca 6f 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 one flag is alwa
10ccb 79 73 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d ys set for temp-
10ccc 66 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 files */. asser
10ccd 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 t( pPager->tempF
10cce 69 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 ile==0 || pPager
10ccf 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e ->changeCountDon
10cd0 65 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 e );.. return 1
10cd1 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
10cd2 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
10cd3 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 it is necessary
10cd4 20 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a to write page *
10cd5 70 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 pPg into the sub
10cd6 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 -journal..** A p
10cd7 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 age needs to be
10cd8 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
10cd9 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 sub-journal if
10cda 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 there exists one
10cdb 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e .** or more open
10cdc 20 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 savepoints for
10cdd 77 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a which:.**.** *
10cde 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 The page-number
10cdf 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 is less than or
10ce0 20 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 equal to PagerS
10ce1 61 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 avepoint.nOrig,
10ce2 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 and.** * The b
10ce3 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 it corresponding
10ce4 20 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d to the page-num
10ce5 62 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 ber is not set i
10ce6 6e 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 n.** PagerSa
10ce7 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 vepoint.pInSavep
10ce8 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 oint..*/.static
10ce9 69 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 int subjRequires
10cea 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 Page(PgHdr *pPg)
10ceb 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 {. Pgno pgno =
10cec 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 pPg->pgno;. Pag
10ced 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
10cee 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 ->pPager;. int
10cef 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
10cf0 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
10cf1 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 nt; i++){. Pa
10cf2 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 gerSavepoint *p
10cf3 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 = &pPager->aSave
10cf4 70 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 point[i];. if
10cf5 28 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f ( p->nOrig>=pgno
10cf6 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 && 0==sqlite3Bi
10cf7 74 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 tvecTest(p->pInS
10cf8 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 avepoint, pgno)
10cf9 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
10cfa 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 1;. }. }. r
10cfb 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
10cfc 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
10cfd 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 the page is alr
10cfe 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 eady in the jour
10cff 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 nal file..*/.sta
10d00 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f tic int pageInJo
10d01 75 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 urnal(PgHdr *pPg
10d02 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
10d03 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 te3BitvecTest(pP
10d04 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f g->pPager->pInJo
10d05 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f urnal, pPg->pgno
10d06 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 );.}../*.** Read
10d07 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 a 32-bit intege
10d08 72 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e r from the given
10d09 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
10d0a 2e 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 . Store the int
10d0b 65 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 eger.** that is
10d0c 72 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 read in *pRes.
10d0d 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
10d0e 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 if everything w
10d0f 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 orked, or an.**
10d10 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f error code is so
10d11 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
10d12 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 ng..**.** All va
10d13 6c 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 lues are stored
10d14 6f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 on disk as big-e
10d15 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ndian..*/.static
10d16 20 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 int read32bits(
10d17 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 sqlite3_file *fd
10d18 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 , i64 offset, u3
10d19 32 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 2 *pRes){. unsi
10d1a 67 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b gned char ac[4];
10d1b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 . int rc = sqli
10d1c 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 te3OsRead(fd, ac
10d1d 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 , sizeof(ac), of
10d1e 66 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d fset);. if( rc=
10d1f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
10d20 20 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 *pRes = sqlite
10d21 33 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 3Get4byte(ac);.
10d22 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
10d23 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 }../*.** Write a
10d24 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
10d25 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 into a string bu
10d26 66 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 ffer in big-endi
10d27 61 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a an byte order..*
10d28 2f 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 /.#define put32b
10d29 69 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 its(A,B) sqlite
10d2a 33 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 3Put4byte((u8*)A
10d2b 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ,B)../*.** Write
10d2c 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 a 32-bit intege
10d2d 72 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e r into the given
10d2e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
10d2f 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
10d30 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 _OK.** on succes
10d31 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f s or an error co
10d32 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 de is something
10d33 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 goes wrong..*/.s
10d34 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 tatic int write3
10d35 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 2bits(sqlite3_fi
10d36 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 le *fd, i64 offs
10d37 65 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 et, u32 val){.
10d38 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 char ac[4];. pu
10d39 74 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 t32bits(ac, val)
10d3a 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 ;. return sqlit
10d3b 65 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 e3OsWrite(fd, ac
10d3c 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a , 4, offset);.}.
10d3d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d ./*.** The argum
10d3e 65 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72 ent to this macr
10d3f 6f 20 69 73 20 61 20 66 69 6c 65 20 64 65 73 63 o is a file desc
10d40 72 69 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c riptor (type sql
10d41 69 74 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 ite3_file*)..**
10d42 52 65 74 75 72 6e 20 30 20 69 66 20 69 74 20 69 Return 0 if it i
10d43 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e s not open, or n
10d44 6f 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 on-zero (but not
10d45 20 31 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 1) if it is..**
10d46 0a 2a 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74 .** This is so t
10d47 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 hat expressions
10d48 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 can be written a
10d49 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 s:.**.** if( i
10d4a 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
10d4b 64 29 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 d) ){ ....**.**
10d4c 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a instead of.**.**
10d4d 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a if( pPager->j
10d4e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 fd->pMethods ){
10d4f 2e 2e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 ....*/.#define i
10d50 73 4f 70 65 6e 28 70 46 64 29 20 28 28 70 46 64 sOpen(pFd) ((pFd
10d51 29 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a )->pMethods)../*
10d52 0a 2a 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20 .** If file pFd
10d53 69 73 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 is open, call sq
10d54 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 lite3OsUnlock()
10d55 6f 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 on it..*/.static
10d56 20 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 int osUnlock(sq
10d57 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c lite3_file *pFd,
10d58 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 int eLock){. i
10d59 66 28 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20 f( !isOpen(pFd)
10d5a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
10d5b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 LITE_OK;. }. r
10d5c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 eturn sqlite3OsU
10d5d 6e 6c 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b nlock(pFd, eLock
10d5e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 );.}../*.** This
10d5f 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d function determ
10d60 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 ines whether or
10d61 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 not the atomic-w
10d62 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f rite optimizatio
10d63 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 n.** can be used
10d64 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 72 with this pager
10d65 2e 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 . The optimizati
10d66 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 on can be used i
10d67 66 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 f:.**.** (a) th
10d68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
10d69 20 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 by OsDeviceChar
10d6a 61 63 74 65 72 69 73 74 69 63 73 28 29 20 69 6e acteristics() in
10d6b 64 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 dicates that.**
10d6c 20 20 20 20 20 61 20 64 61 74 61 62 61 73 65 20 a database
10d6d 70 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 page may be writ
10d6e 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 ten atomically,
10d6f 61 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 and.** (b) the
10d70 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
10d71 79 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 y OsSectorSize()
10d72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 is less than or
10d73 20 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 equal.** t
10d74 6f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e o the page size.
10d75 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d .**.** The optim
10d76 69 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 ization is also
10d77 61 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 always enabled f
10d78 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c or temporary fil
10d79 65 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 es. It is.** an
10d7a 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 error to call th
10d7b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 is function if p
10d7c 50 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 Pager is opened
10d7d 6f 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a on an in-memory.
10d7e 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a ** database..**.
10d7f 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 ** If the optimi
10d80 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 zation cannot be
10d81 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 used, 0 is retu
10d82 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 rned. If it can
10d83 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e be used,.** then
10d84 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
10d85 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 ned is the size
10d86 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
10d87 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 ile when it.** c
10d88 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b ontains rollback
10d89 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c data for exactl
10d8a 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 y one page..*/.#
10d8b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
10d8c 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
10d8d 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c .static int jrnl
10d8e 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 BufferSize(Pager
10d8f 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 *pPager){. ass
10d90 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 ert( !MEMDB );.
10d91 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 if( !pPager->te
10d92 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e mpFile ){. in
10d93 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 t dc;
10d94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10d95 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63 /* Device charac
10d96 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 teristics */.
10d97 20 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 int nSector;
10d98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10d99 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a /* Sector siz
10d9a 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 e */. int szP
10d9b 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
10d9c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
10d9d 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 ge size */..
10d9e 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
10d9f 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 Pager->fd) );.
10da0 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 dc = sqlite3Os
10da1 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
10da2 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 stics(pPager->fd
10da3 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d );. nSector =
10da4 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 pPager->sectorS
10da5 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 ize;. szPage
10da6 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
10da7 7a 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 ze;.. assert(
10da8 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
10da9 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 MIC512==(512>>8)
10daa 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 );. assert(SQ
10dab 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
10dac 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 C64K==(65536>>8)
10dad 29 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 );. if( 0==(d
10dae 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f c&(SQLITE_IOCAP_
10daf 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e ATOMIC|(szPage>>
10db0 38 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 8)) || nSector>s
10db1 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 zPage) ){.
10db2 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
10db3 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f }.. return JO
10db4 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
10db5 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 ger) + JOURNAL_P
10db6 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a G_SZ(pPager);.}.
10db7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 #endif../*.** If
10db8 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
10db9 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 GES is defined t
10dba 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 hen we do some s
10dbb 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a anity checking.*
10dbc 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 * on the cache u
10dbd 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 sing a hash func
10dbe 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 tion. This is u
10dbf 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a sed for testing.
10dc0 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ** and debugging
10dc1 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 only..*/.#ifdef
10dc2 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
10dc3 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e GES./*.** Return
10dc4 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f a 32-bit hash o
10dc5 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 f the page data
10dc6 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 for pPage..*/.st
10dc7 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 atic u32 pager_d
10dc8 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 atahash(int nByt
10dc9 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 e, unsigned char
10dca 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 *pData){. u32
10dcb 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 hash = 0;. int
10dcc 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
10dcd 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 nByte; i++){.
10dce 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 hash = (hash*10
10dcf 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 39) + pData[i];.
10dd0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 }. return has
10dd1 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 h;.}.static u32
10dd2 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 pager_pagehash(P
10dd3 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
10dd4 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 return pager_dat
10dd5 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 ahash(pPage->pPa
10dd6 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 ger->pageSize, (
10dd7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
10dd8 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d pPage->pData);.}
10dd9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
10dda 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 er_set_pagehash(
10ddb 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 PgHdr *pPage){.
10ddc 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 pPage->pageHash
10ddd 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
10dde 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a h(pPage);.}../*.
10ddf 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 ** The CHECK_PAG
10de0 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 E macro takes a
10de1 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 PgHdr* as an arg
10de2 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 ument. If SQLITE
10de3 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 _CHECK_PAGES.**
10de4 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 is defined, and
10de5 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 NDEBUG is not de
10de6 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 fined, an assert
10de7 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 () statement che
10de8 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 cks.** that the
10de9 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64 page is either d
10dea 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 irty or still ma
10deb 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c tches the calcul
10dec 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a ated page-hash..
10ded 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b */.#define CHECK
10dee 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 _PAGE(x) checkPa
10def 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 ge(x).static voi
10df0 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 d checkPage(PgHd
10df1 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 r *pPg){. Pager
10df2 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
10df3 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 pPager;. assert
10df4 28 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 ( !pPg->pageHash
10df5 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 || pPager->errC
10df6 6f 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 ode. || (pP
10df7 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
10df8 49 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 IRTY) || pPg->pa
10df9 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 geHash==pager_pa
10dfa 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d gehash(pPg) );.}
10dfb 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ..#else.#define
10dfc 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 pager_datahash(X
10dfd 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 ,Y) 0.#define p
10dfe 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 ager_pagehash(X)
10dff 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 0.#define CHEC
10e00 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 K_PAGE(x).#endif
10e01 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 /* SQLITE_CHEC
10e02 4b 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a K_PAGES */../*.*
10e03 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 * When this is c
10e04 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 alled the journa
10e05 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 l file for pager
10e06 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 pPager must be
10e07 6f 70 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 open..** This fu
10e08 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 nction attempts
10e09 74 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65 72 to read a master
10e0a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
10e0b 6d 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 me from the .**
10e0c 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 end of the file
10e0d 61 6e 64 2c 20 69 66 20 73 75 63 63 65 73 73 66 and, if successf
10e0e 75 6c 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e ul, copies it in
10e0f 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 to memory suppli
10e10 65 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 ed .** by the ca
10e11 6c 6c 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e ller. See commen
10e12 74 73 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61 ts above writeMa
10e13 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f sterJournal() fo
10e14 72 20 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 r the format.**
10e15 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 used to store a
10e16 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
10e17 69 6c 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20 ile name at the
10e18 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c end of a journal
10e19 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 file..**.** zMa
10e1a 73 74 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 ster must point
10e1b 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 to a buffer of a
10e1c 74 20 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20 t least nMaster
10e1d 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 bytes allocated
10e1e 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 by.** the caller
10e1f 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 . This should be
10e20 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 sqlite3_vfs.mxP
10e21 61 74 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e athname+1 (to en
10e22 73 75 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a sure there is.**
10e23 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f enough space to
10e24 20 77 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 write the maste
10e25 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e r journal name).
10e26 20 49 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a If the master j
10e27 6f 75 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 ournal.** name i
10e28 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 n the journal is
10e29 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 longer than nMa
10e2a 73 74 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c ster bytes (incl
10e2b 75 64 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 uding a.** nul-t
10e2c 65 72 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e erminator), then
10e2d 20 74 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 this is handled
10e2e 20 61 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 as if no master
10e2f 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a journal name.**
10e30 20 77 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e were present in
10e31 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a the journal..**
10e32 0a 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 .** If a master
10e33 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
10e34 65 20 69 73 20 70 72 65 73 65 6e 74 20 61 74 20 e is present at
10e35 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a the end of the j
10e36 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 ournal.** file,
10e37 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 then it is copie
10e38 64 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 d into the buffe
10e39 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 r pointed to by
10e3a 7a 4d 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 zMaster. A.** nu
10e3b 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 l-terminator byt
10e3c 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f e is appended to
10e3d 20 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c the buffer foll
10e3e 6f 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 owing the master
10e3f 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
10e40 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 name..**.** If
10e41 69 74 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 it is determined
10e42 20 74 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 that no master
10e43 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
10e44 65 20 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a e is present .**
10e45 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73 zMaster[0] is s
10e46 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 et to 0 and SQLI
10e47 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a TE_OK returned..
10e48 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
10e49 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 r occurs while r
10e4a 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 eading from the
10e4b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e journal file, an
10e4c 20 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 SQLite.** error
10e4d 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
10e4e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
10e4f 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e readMasterJourn
10e50 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al(sqlite3_file
10e51 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d *pJrnl, char *zM
10e52 61 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 aster, u32 nMast
10e53 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 er){. int rc;
10e54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e55 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
10e56 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 */. u32 len;
10e57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e58 20 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 /* Length in by
10e59 74 65 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f tes of master jo
10e5a 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 urnal name */.
10e5b 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 i64 szJ;
10e5c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f /* To
10e5d 74 61 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65 tal size in byte
10e5e 73 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c s of journal fil
10e5f 65 20 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 e pJrnl */. u32
10e60 20 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 cksum;
10e61 20 20 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 /* MJ ch
10e62 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61 ecksum value rea
10e63 64 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a d from journal *
10e64 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 /. u32 u;
10e65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10e66 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 * Unsigned loop
10e67 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 counter */. uns
10e68 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 igned char aMagi
10e69 63 5b 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 c[8]; /* A buf
10e6a 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 fer to hold the
10e6b 6d 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a magic header */.
10e6c 20 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 zMaster[0] = '
10e6d 5c 30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 \0';.. if( SQLI
10e6e 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
10e6f 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
10e70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 Jrnl, &szJ)).
10e71 7c 7c 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 || szJ<16. ||
10e72 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
10e73 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e read32bits(pJrn
10e74 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 l, szJ-16, &len)
10e75 29 0a 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 ). || len>=nMa
10e76 73 74 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 ster . || SQLI
10e77 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 TE_OK!=(rc = rea
10e78 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 d32bits(pJrnl, s
10e79 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a zJ-12, &cksum)).
10e7a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
10e7b 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 =(rc = sqlite3Os
10e7c 52 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 Read(pJrnl, aMag
10e7d 69 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 ic, 8, szJ-8)).
10e7e 20 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 || memcmp(aMag
10e7f 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 ic, aJournalMagi
10e80 63 2c 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 c, 8). || SQLI
10e81 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
10e82 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c ite3OsRead(pJrnl
10e83 2c 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 , zMaster, len,
10e84 73 7a 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 szJ-16-len)). )
10e85 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
10e86 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 . }.. /* See i
10e87 66 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d f the checksum m
10e88 61 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 atches the maste
10e89 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a r journal name *
10e8a 2f 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c /. for(u=0; u<l
10e8b 65 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b en; u++){. ck
10e8c 73 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 sum -= zMaster[u
10e8d 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 ];. }. if( cks
10e8e 75 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 um ){. /* If
10e8f 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 the checksum doe
10e90 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 sn't add up, the
10e91 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 n one or more of
10e92 20 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 the disk sector
10e93 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e s. ** contain
10e94 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a ing the master j
10e95 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 ournal filename
10e96 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 is corrupted. Th
10e97 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 is means. **
10e98 64 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 definitely roll
10e99 62 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 back, so just re
10e9a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 turn SQLITE_OK a
10e9b 6e 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c nd report a (nul
10e9c 29 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d ). ** master-
10e9d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
10e9e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e .. */. len
10e9f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 = 0;. }. zMas
10ea0 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b ter[len] = '\0';
10ea1 0a 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 . . return SQ
10ea2 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
10ea3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 * Return the off
10ea4 73 65 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f set of the secto
10ea5 72 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 r boundary at or
10ea6 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a immediately .**
10ea7 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 following the v
10ea8 61 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e alue in pPager->
10ea9 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 journalOff, assu
10eaa 6d 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a ming a sector .*
10eab 2a 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72 * size of pPager
10eac 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74 ->sectorSize byt
10ead 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f es..**.** i.e fo
10eae 72 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20 r a sector size
10eaf 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 of 512:.**.**
10eb0 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 Pager.journalOff
10eb1 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e Return
10eb2 20 76 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d value.** ----
10eb3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
10eb4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
10eb5 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 ---.** 0
10eb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10eb7 20 20 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 0.** 512
10eb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10eb9 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 512.** 100
10eba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ebb 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 512.**
10ebc 32 30 30 30 20 20 20 20 20 20 20 20 20 20 20 20 2000
10ebd 20 20 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2048.*
10ebe 2a 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 * .*/.static i64
10ebf 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 journalHdrOffse
10ec0 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 t(Pager *pPager)
10ec1 7b 0a 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d {. i64 offset =
10ec2 20 30 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 0;. i64 c = pP
10ec3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
10ec4 3b 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 ;. if( c ){.
10ec5 20 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 offset = ((c-1)
10ec6 2f 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 /JOURNAL_HDR_SZ(
10ec7 70 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a pPager) + 1) * J
10ec8 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
10ec9 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 ager);. }. ass
10eca 65 72 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 ert( offset%JOUR
10ecb 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
10ecc 72 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 r)==0 );. asser
10ecd 74 28 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a t( offset>=c );.
10ece 20 20 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 assert( (offse
10ecf 74 2d 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 t-c)<JOURNAL_HDR
10ed0 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 _SZ(pPager) );.
10ed1 20 72 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a return offset;.
10ed2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 }../*.** The jou
10ed3 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 rnal file must b
10ed4 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 e open when this
10ed5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
10ed6 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 led..**.** This
10ed7 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
10ed8 2d 6f 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e -op if the journ
10ed9 61 6c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 al file has not
10eda 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a been written to.
10edb 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 ** within the cu
10edc 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
10edd 6e 20 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72 n (i.e. if Pager
10ede 2e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e .journalOff==0).
10edf 0a 2a 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e .**.** If doTrun
10ee0 63 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f cate is non-zero
10ee1 20 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f or the Pager.jo
10ee2 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 urnalSizeLimit v
10ee3 61 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 ariable is.** se
10ee4 74 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 t to 0, then tru
10ee5 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 ncate the journa
10ee6 6c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 l file to zero b
10ee7 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 ytes in size. Ot
10ee8 68 65 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f herwise,.** zero
10ee9 20 74 68 65 20 32 38 2d 62 79 74 65 20 68 65 61 the 28-byte hea
10eea 64 65 72 20 61 74 20 74 68 65 20 73 74 61 72 74 der at the start
10eeb 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
10eec 66 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20 file. In either
10eed 63 61 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 case, .** if the
10eee 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e pager is not in
10eef 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 no-sync mode, s
10ef0 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ync the journal
10ef1 66 69 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 file immediately
10ef2 20 0a 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69 .** after writi
10ef3 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 ng or truncating
10ef4 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 it..**.** If Pa
10ef5 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c ger.journalSizeL
10ef6 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 61 imit is set to a
10ef7 20 70 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a positive, non-z
10ef8 65 72 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a ero value, and.*
10ef9 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 * following the
10efa 74 72 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 truncation or ze
10efb 72 6f 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 roing described
10efc 61 62 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f above the size o
10efd 66 20 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 f the .** journa
10efe 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 l file in bytes
10eff 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
10f00 68 69 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 his value, then
10f01 74 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 truncate the.**
10f02 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 journal file to
10f03 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a Pager.journalSiz
10f04 65 4c 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68 eLimit bytes. Th
10f05 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 e journal file d
10f06 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 oes.** not need
10f07 74 6f 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c to be synced fol
10f08 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 lowing this oper
10f09 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ation..**.** If
10f0a 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
10f0b 72 73 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 rs, abandon proc
10f0c 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 essing and retur
10f0d 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 n the IO error c
10f0e 6f 64 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ode..** Otherwis
10f0f 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 e, return SQLITE
10f10 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 _OK..*/.static i
10f11 6e 74 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 nt zeroJournalHd
10f12 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c r(Pager *pPager,
10f13 20 69 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 int doTruncate)
10f14 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
10f15 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
10f16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f17 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
10f18 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 code */. assert
10f19 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
10f1a 3e 6a 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70 >jfd) );. if( p
10f1b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
10f1c 66 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 f ){. const i
10f1d 36 34 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 64 iLimit = pPag
10f1e 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c er->journalSizeL
10f1f 69 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 imit; /* Loca
10f20 6c 20 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a l cache of jsl *
10f21 2f 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 /.. IOTRACE((
10f22 22 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c "JZEROHDR %p\n",
10f23 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 pPager)). if
10f24 28 20 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 ( doTruncate ||
10f25 69 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 iLimit==0 ){.
10f26 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
10f27 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 sTruncate(pPager
10f28 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d ->jfd, 0);. }
10f29 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74 else{. stat
10f2a 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 ic const char ze
10f2b 72 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b roHdr[28] = {0};
10f2c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
10f2d 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
10f2e 72 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c r->jfd, zeroHdr,
10f2f 20 73 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 sizeof(zeroHdr)
10f30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 0);. }.
10f31 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
10f32 4b 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f K && !pPager->no
10f33 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 Sync ){. rc
10f34 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
10f35 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 (pPager->jfd, SQ
10f36 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e LITE_SYNC_DATAON
10f37 4c 59 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f LY|pPager->sync_
10f38 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 flags);. }..
10f39 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f /* At this po
10f3a 69 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 int the transact
10f3b 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
10f3c 20 62 75 74 20 74 68 65 20 77 72 69 74 65 20 6c but the write l
10f3d 6f 63 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 ock . ** is s
10f3e 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 till held on the
10f3f 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 file. If there
10f40 69 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 is a size limit
10f41 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a configured for .
10f42 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 ** the persi
10f43 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e stent journal an
10f44 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 d the journal fi
10f45 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e le currently con
10f46 73 75 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a sumes more. *
10f47 2a 20 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 * space than tha
10f48 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 t limit allows f
10f49 6f 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 or, truncate it
10f4a 6e 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f now. There is no
10f4b 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 need. ** to
10f4c 73 79 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f sync the file fo
10f4d 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 llowing this ope
10f4e 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 ration.. */.
10f4f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
10f50 45 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 E_OK && iLimit>0
10f51 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a ){. i64 sz
10f52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
10f53 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
10f54 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 Pager->jfd, &sz)
10f55 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
10f56 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e SQLITE_OK && sz>
10f57 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 iLimit ){.
10f58 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
10f59 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
10f5a 3e 6a 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 >jfd, iLimit);.
10f5b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
10f5c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
10f5d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e ./*.** The journ
10f5e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
10f5f 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 open when this r
10f60 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
10f61 2e 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 . A journal.** h
10f62 65 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 eader (JOURNAL_H
10f63 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 DR_SZ bytes) is
10f64 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
10f65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 journal file at
10f66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
10f67 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 location..**.**
10f68 54 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 The format for t
10f69 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
10f6a 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a r is as follows:
10f6b 0a 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d .** - 8 bytes: M
10f6c 61 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 agic identifying
10f6d 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e journal format.
10f6e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e .** - 4 bytes: N
10f6f 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 umber of records
10f70 20 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 in journal, or
10f71 2d 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 -1 no-sync mode
10f72 69 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 is on..** - 4 by
10f73 74 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 tes: Random numb
10f74 65 72 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 er used for page
10f75 20 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 hash..** - 4 by
10f76 74 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 tes: Initial dat
10f77 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 abase page count
10f78 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 ..** - 4 bytes:
10f79 53 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 Sector size used
10f7a 20 62 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 by the process
10f7b 74 68 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 that wrote this
10f7c 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 journal..** - 4
10f7d 62 79 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 bytes: Database
10f7e 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a page size..** .*
10f7f 2a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a * Followed by (J
10f80 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 OURNAL_HDR_SZ -
10f81 32 38 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 28) bytes of unu
10f82 73 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 sed space..*/.st
10f83 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f atic int writeJo
10f84 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a urnalHdr(Pager *
10f85 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
10f86 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
10f87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10f88 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
10f89 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72 . char *zHeader
10f8a 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 = pPager->pTmpS
10f8b 70 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 pace; /* Tempor
10f8c 61 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 ary space used t
10f8d 6f 20 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a o build header *
10f8e 2f 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 /. u32 nHeader
10f8f 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
10f90 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 ze; /* Size
10f91 6f 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 of buffer pointe
10f92 64 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20 d to by zHeader
10f93 2a 2f 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b */. u32 nWrite;
10f94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f95 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
10f96 73 20 6f 66 20 68 65 61 64 65 72 20 73 65 63 74 s of header sect
10f97 6f 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 or written */.
10f98 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 int ii;
10f99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f9a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
10f9b 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ter */.. assert
10f9c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
10f9d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a >jfd) ); /*
10f9e 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 Journal file mu
10f9f 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a st be open. */..
10fa0 20 20 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f if( nHeader>JO
10fa1 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
10fa2 67 65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 ger) ){. nHea
10fa3 64 65 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 der = JOURNAL_HD
10fa4 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 R_SZ(pPager);.
10fa5 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 }.. /* If there
10fa6 20 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 are active save
10fa7 70 6f 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f points and any o
10fa8 66 20 74 68 65 6d 20 77 65 72 65 20 63 72 65 61 f them were crea
10fa9 74 65 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 ted . ** since
10faa 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
10fab 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 journal header w
10fac 61 73 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 as written, upda
10fad 74 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 te the . ** Pag
10fae 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 erSavepoint.iHdr
10faf 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f Offset fields no
10fb0 77 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 w.. */. for(ii
10fb1 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e =0; ii<pPager->n
10fb2 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 Savepoint; ii++)
10fb3 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 {. if( pPager
10fb4 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d ->aSavepoint[ii]
10fb5 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 .iHdrOffset==0 )
10fb6 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
10fb7 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 aSavepoint[ii].i
10fb8 48 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 HdrOffset = pPag
10fb9 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a er->journalOff;.
10fba 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 }. }.. pPa
10fbb 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 ger->journalHdr
10fbc 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
10fbd 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 lOff = journalHd
10fbe 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b rOffset(pPager);
10fbf 0a 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 . memcpy(zHeade
10fc0 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 r, aJournalMagic
10fc1 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 , sizeof(aJourna
10fc2 6c 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a 20 lMagic));.. /*
10fc3 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 . ** Write the
10fc4 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 nRec Field - the
10fc5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 number of page
10fc6 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c records that fol
10fc7 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f low this. ** jo
10fc8 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f urnal header. No
10fc9 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 rmally, zero is
10fca 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 written to this
10fcb 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 value at this ti
10fcc 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 me.. ** After t
10fcd 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 he records are a
10fce 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 dded to the jour
10fcf 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 nal (and the jou
10fd0 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 rnal synced, .
10fd1 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 ** if in full-sy
10fd2 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 nc mode), the ze
10fd3 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 ro is overwritte
10fd4 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65 20 n with the true
10fd5 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 number. ** of r
10fd6 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 ecords (see sync
10fd7 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a Journal()).. **
10fd8 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 . ** A faster a
10fd9 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f lternative is to
10fda 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46 46 write 0xFFFFFFF
10fdb 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 F to the nRec fi
10fdc 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 eld. When. ** r
10fdd 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e eading the journ
10fde 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 al this value te
10fdf 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 lls SQLite to as
10fe0 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 sume that the.
10fe1 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a ** rest of the j
10fe2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 ournal file cont
10fe3 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 ains valid page
10fe4 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 records. This as
10fe5 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 sumption. ** is
10fe6 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 dangerous, as i
10fe7 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 f a failure occu
10fe8 72 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 rred whilst writ
10fe9 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ing to the journ
10fea 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 al. ** file it
10feb 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 may contain some
10fec 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 garbage data. T
10fed 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 here are two sce
10fee 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 narios. ** wher
10fef 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 e this risk can
10ff0 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a be ignored:. **
10ff1 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 . ** * When t
10ff2 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e he pager is in n
10ff3 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 o-sync mode. Cor
10ff4 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c ruption can foll
10ff5 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f ow a. ** po
10ff6 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 wer failure in t
10ff7 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e his case anyway.
10ff8 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 . **. ** * W
10ff9 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 hen the SQLITE_I
10ffa 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
10ffb 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 flag is set. Th
10ffc 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 is guarantees.
10ffd 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62 ** that garb
10ffe 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 age data is neve
10fff 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 r appended to th
11000 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
11001 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
11002 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
11003 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 ) || pPager->noS
11004 79 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 ync );. if( (pP
11005 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c ager->noSync) ||
11006 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 (pPager->journa
11007 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
11008 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 RNALMODE_MEMORY)
11009 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f . || (sqlite3O
1100a 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
1100b 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
1100c 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f d)&SQLITE_IOCAP_
1100d 53 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 SAFE_APPEND) .
1100e 29 7b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 ){. put32bits
1100f 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 (&zHeader[sizeof
11010 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d (aJournalMagic)]
11011 2c 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 , 0xffffffff);.
11012 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 }else{. put3
11013 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 2bits(&zHeader[s
11014 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
11015 67 69 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a gic)], 0);. }..
11016 20 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 /* The random
11017 63 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 check-hash initi
11018 61 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c aliser */ . sql
11019 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
1101a 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 sizeof(pPager->c
1101b 6b 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 ksumInit), &pPag
1101c 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a er->cksumInit);.
1101d 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 put32bits(&zHe
1101e 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 ader[sizeof(aJou
1101f 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 rnalMagic)+4], p
11020 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
11021 29 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 );. /* The init
11022 69 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a ial database siz
11023 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 e */. put32bits
11024 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 (&zHeader[sizeof
11025 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b (aJournalMagic)+
11026 38 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 8], pPager->dbOr
11027 69 67 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 igSize);. /* Th
11028 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 e assumed sector
11029 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 size for this p
1102a 72 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 rocess */. put3
1102b 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 2bits(&zHeader[s
1102c 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
1102d 67 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 gic)+12], pPager
1102e 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a ->sectorSize);..
1102f 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 73 69 /* The page si
11030 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 ze */. put32bit
11031 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f s(&zHeader[sizeo
11032 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
11033 2b 31 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 +16], pPager->pa
11034 67 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 geSize);.. /* I
11035 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 nitializing the
11036 74 61 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66 tail of the buff
11037 65 72 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 er is not necess
11038 61 72 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67 ary. Everything
11039 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 . ** works find
1103a 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e if the followin
1103b 67 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d g memset() is om
1103c 69 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74 itted. But init
1103d 69 61 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 ializing. ** th
1103e 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 e memory prevent
1103f 73 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 s valgrind from
11040 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 complaining, so
11041 77 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 we are willing t
11042 6f 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 o. ** take the
11043 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e performance hit.
11044 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 . */. memset(&
11045 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
11046 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 JournalMagic)+20
11047 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 6e ], 0,. n
11048 48 65 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 Header-(sizeof(a
11049 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 JournalMagic)+20
1104a 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 ));.. /* In the
1104b 6f 72 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 ory, it is only
1104c 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 necessary to wri
1104d 74 65 20 74 68 65 20 32 38 20 62 79 74 65 73 20 te the 28 bytes
1104e 74 68 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a that the . ** j
1104f 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 63 6f ournal header co
11050 6e 73 75 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f nsumes to the jo
11051 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e urnal file here.
11052 20 54 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 Then increment
11053 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e the . ** Pager.
11054 6a 6f 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 journalOff varia
11055 62 6c 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 ble by JOURNAL_H
11056 44 52 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 68 DR_SZ so that th
11057 65 20 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 e next . ** rec
11058 6f 72 64 20 69 73 20 77 72 69 74 74 65 6e 20 74 ord is written t
11059 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
1105a 73 65 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 sector (leaving
1105b 61 20 67 61 70 20 69 6e 20 74 68 65 20 66 69 6c a gap in the fil
1105c 65 0a 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c e. ** that will
1105d 20 62 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 be implicitly f
1105e 69 6c 6c 65 64 20 69 6e 20 62 79 20 74 68 65 20 illed in by the
1105f 4f 53 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 OS).. **. ** H
11060 6f 77 65 76 65 72 20 69 74 20 68 61 73 20 62 65 owever it has be
11061 65 6e 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 en discovered th
11062 61 74 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 at on some syste
11063 6d 73 20 74 68 69 73 20 70 61 74 74 65 72 6e 20 ms this pattern
11064 63 61 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 can . ** be sig
11065 6e 69 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 nificantly slowe
11066 72 20 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 r than contiguou
11067 73 6c 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 sly writing data
11068 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 to the file,.
11069 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 ** even if that
1106a 6d 65 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 means explicitly
1106b 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f writing data to
1106c 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 the block of .
1106d 20 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 ** (JOURNAL_HDR
1106e 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 _SZ - 28) bytes
1106f 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 that will not be
11070 20 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69 used. So that i
11071 73 20 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 s what. ** is d
11072 6f 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 one. . **. **
11073 54 68 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 The loop is requ
11074 69 72 65 64 20 68 65 72 65 20 69 6e 20 63 61 73 ired here in cas
11075 65 20 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a e the sector-siz
11076 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e e is larger than
11077 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 the . ** datab
11078 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 53 ase page size. S
11079 69 6e 63 65 20 74 68 65 20 7a 48 65 61 64 65 72 ince the zHeader
1107a 20 62 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 buffer is only
1107b 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 Pager.pageSize.
1107c 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a ** bytes in siz
1107d 65 2c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 e, more than one
1107e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
1107f 4f 73 57 72 69 74 65 28 29 20 6d 61 79 20 62 65 OsWrite() may be
11080 20 72 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 required. ** t
11081 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 65 o populate the e
11082 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 ntire journal he
11083 61 64 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a ader sector.. *
11084 2f 20 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 3d / . for(nWrite=
11085 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0; rc==SQLITE_OK
11086 26 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c &&nWrite<JOURNAL
11087 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b _HDR_SZ(pPager);
11088 20 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 nWrite+=nHeader
11089 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 ){. IOTRACE((
1108a 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 "JHDR %p %lld %d
1108b 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 \n", pPager, pPa
1108c 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c ger->journalHdr,
1108d 20 6e 48 65 61 64 65 72 29 29 0a 20 20 20 20 72 nHeader)). r
1108e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
1108f 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
11090 7a 48 65 61 64 65 72 2c 20 6e 48 65 61 64 65 72 zHeader, nHeader
11091 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 , pPager->journa
11092 6c 4f 66 66 29 3b 0a 20 20 20 20 70 50 61 67 65 lOff);. pPage
11093 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d r->journalOff +=
11094 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 nHeader;. }..
11095 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11096 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
11097 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 file must be op
11098 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 en when this is
11099 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 called. A journa
1109a 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a l header file.**
1109b 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a (JOURNAL_HDR_SZ
1109c 20 62 79 74 65 73 29 20 69 73 20 72 65 61 64 20 bytes) is read
1109d 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 from the current
1109e 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 location in the
1109f 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 journal.** file
110a0 2e 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c 6f . The current lo
110a1 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f cation in the jo
110a2 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 69 urnal file is gi
110a3 76 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 ven by.** pPager
110a4 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 ->journalOff. Se
110a5 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 e comments above
110a6 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a function writeJ
110a7 6f 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a ournalHdr() for.
110a8 2a 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e ** a description
110a9 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
110aa 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a header format..*
110ab 2a 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64 *.** If the head
110ac 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63 65 er is read succe
110ad 73 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 ssfully, *pNRec
110ae 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 is set to the nu
110af 6d 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 mber of.** page
110b0 72 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e records followin
110b1 67 20 74 68 69 73 20 68 65 61 64 65 72 20 61 6e g this header an
110b2 64 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73 65 d *pDbSize is se
110b3 74 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 t to the size of
110b4 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
110b5 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e before the tran
110b6 73 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 saction began, i
110b7 6e 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 n pages. Also, p
110b8 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
110b9 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 .** is set to th
110ba 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f e value read fro
110bb 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 m the journal he
110bc 61 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 ader. SQLITE_OK
110bd 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 is returned.** i
110be 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a n this case..**.
110bf 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
110c0 6c 20 68 65 61 64 65 72 20 66 69 6c 65 20 61 70 l header file ap
110c1 70 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 pears to be corr
110c2 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f upted, SQLITE_DO
110c3 4e 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 NE is.** returne
110c4 64 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 d and *pNRec and
110c5 20 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75 6e *PDbSize are un
110c6 64 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 defined. If JOU
110c7 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 RNAL_HDR_SZ byte
110c8 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 s.** cannot be r
110c9 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ead from the jou
110ca 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 rnal file an err
110cb 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
110cc 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
110cd 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 nt readJournalHd
110ce 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 r(. Pager *pPag
110cf 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 er,
110d0 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 /* Pager objec
110d1 74 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e t */. i64 journ
110d2 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 alSize,
110d3 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
110d4 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 he open journal
110d5 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f file in bytes */
110d6 0a 20 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 . u32 *pNRec,
110d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110d8 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 /* OUT: Value re
110d9 61 64 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 ad from the nRec
110da 20 66 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 field */. u32
110db 2a 70 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 *pDbSize
110dc 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
110dd 20 56 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e Value of origin
110de 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 al database size
110df 20 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 field */.){. i
110e0 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
110e1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
110e2 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
110e3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d unsigned char aM
110e4 61 67 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 agic[8]; /*
110e5 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 A buffer to hold
110e6 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 the magic heade
110e7 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f r */. i64 iHdrO
110e8 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ff;
110e9 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 /* Offset of
110ea 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
110eb 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 being read */..
110ec 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
110ed 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 pPager->jfd) );
110ee 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 /* Journal
110ef 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 file must be ope
110f0 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 n. */.. /* Adva
110f1 6e 63 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 nce Pager.journa
110f2 6c 4f 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 lOff to the star
110f3 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 t of the next se
110f4 63 74 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a ctor. If the. *
110f5 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 * journal file i
110f6 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 s too small for
110f7 74 68 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 there to be a he
110f8 61 64 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 ader stored at t
110f9 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 his. ** point,
110fa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
110fb 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 NE.. */. pPage
110fc 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
110fd 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 journalHdrOffset
110fe 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 (pPager);. if(
110ff 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11100 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 ff+JOURNAL_HDR_S
11101 5a 28 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 Z(pPager) > jour
11102 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 nalSize ){. r
11103 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
11104 45 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 E;. }. iHdrOff
11105 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
11106 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 alOff;.. /* Rea
11107 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 d in the first 8
11108 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f bytes of the jo
11109 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 urnal header. If
1110a 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 they do not mat
1110b 63 68 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 ch. ** the mag
1110c 69 63 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 ic string found
1110d 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
1110e 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 each journal hea
1110f 64 65 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a der, return. **
11110 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 SQLITE_DONE. If
11111 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
11112 75 72 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 urs, return an e
11113 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 rror code. Other
11114 77 69 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 wise,. ** proce
11115 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 ed.. */. rc =
11116 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 sqlite3OsRead(pP
11117 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 ager->jfd, aMagi
11118 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 c, sizeof(aMagic
11119 29 2c 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 69 ), iHdrOff);. i
1111a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 f( rc ){. ret
1111b 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 urn rc;. }. if
1111c 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c ( memcmp(aMagic,
1111d 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
1111e 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 sizeof(aMagic))!
1111f 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
11120 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
11121 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 }.. /* Read the
11122 20 66 69 72 73 74 20 74 68 72 65 65 20 33 32 2d first three 32-
11123 62 69 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 bit fields of th
11124 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
11125 3a 20 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 : The nRec. **
11126 66 69 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b field, the check
11127 73 75 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 sum-initializer
11128 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
11129 20 73 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 size at the sta
1112a 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 rt. ** of the t
1112b 72 61 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 ransaction. Retu
1112c 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 rn an error code
1112d 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
1112e 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 s wrong.. */.
1112f 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 if( SQLITE_OK!=(
11130 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
11131 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
11132 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a rOff+8, pNRec)).
11133 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
11134 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 =(rc = read32bit
11135 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 s(pPager->jfd, i
11136 48 64 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 HdrOff+12, &pPag
11137 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a er->cksumInit)).
11138 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
11139 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 =(rc = read32bit
1113a 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 s(pPager->jfd, i
1113b 48 64 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 HdrOff+16, pDbSi
1113c 7a 65 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 ze)). ){. re
1113d 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
1113e 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
1113f 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 nalOff==0 ){.
11140 20 75 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 u32 iPageSize;
11141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11142 20 50 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 Page-size field
11143 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 of journal head
11144 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 er */. u32 iS
11145 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 ectorSize;
11146 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 /* Sector
11147 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a -size field of j
11148 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f ournal header */
11149 0a 20 20 20 20 75 31 36 20 69 50 61 67 65 53 69 . u16 iPageSi
1114a 7a 65 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 ze16;
1114b 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61 /* Copy of iPa
1114c 67 65 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74 geSize in 16-bit
1114d 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 variable */..
1114e 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 /* Read the pa
1114f 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 ge-size and sect
11150 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20 or-size journal
11151 68 65 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a header fields. *
11152 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 /. if( SQLITE
11153 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
11154 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
11155 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26 d, iHdrOff+20, &
11156 69 53 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20 iSectorSize)).
11157 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
11158 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 =(rc = read32bit
11159 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 s(pPager->jfd, i
1115a 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67 HdrOff+24, &iPag
1115b 65 53 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20 eSize)). ){.
1115c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1115d 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 }.. /* Ch
1115e 65 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c eck that the val
1115f 75 65 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 ues read from th
11160 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 e page-size and
11161 73 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c sector-size fiel
11162 64 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69 ds. ** are wi
11163 74 68 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62 thin range. To b
11164 65 20 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f e 'in range', bo
11165 74 68 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74 th values need t
11166 6f 20 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20 o be a power.
11167 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74 ** of two great
11168 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
11169 20 74 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74 to 512, and not
1116a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
1116b 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 eir . ** resp
1116c 65 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 ective compile t
1116d 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 ime maximum limi
1116e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ts.. */. i
1116f 66 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 f( iPageSize<512
11170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11171 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 || iSectorSize
11172 3c 35 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 <512. || iPa
11173 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 geSize>SQLITE_MA
11174 58 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 X_PAGE_SIZE || i
11175 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 SectorSize>MAX_S
11176 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 ECTOR_SIZE.
11177 7c 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 || ((iPageSize-1
11178 29 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 )&iPageSize)!=0
11179 20 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 || ((iSectorSi
1117a 7a 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a ze-1)&iSectorSiz
1117b 65 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 e)!=0 . ){.
1117c 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 /* If the ei
1117d 74 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 ther the page-si
1117e 7a 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a ze or sector-siz
1117f 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c e in the journal
11180 2d 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 -header is .
11181 20 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 ** invalid, th
11182 65 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 en the process t
11183 68 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f hat wrote the jo
11184 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 urnal-header mus
11185 74 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a t have . **
11186 20 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 crashed before
11187 74 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 the header was s
11188 79 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 ynced. In this c
11189 61 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 ase stop reading
1118a 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a . ** the j
1118b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 ournal file here
1118c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1118d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
1118e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ONE;. }..
1118f 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61 /* Update the pa
11190 67 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 ge-size to match
11191 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 the value read
11192 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c from the journal
11193 2e 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 . . ** Use a
11194 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f testcase() macro
11195 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
11196 61 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 at malloc failur
11197 65 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a e within . **
11198 20 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a PagerSetPagesiz
11199 65 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 e() is tested..
1119a 20 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 */. iPageS
1119b 69 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 ize16 = (u16)iPa
1119c 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d geSize;. rc =
1119d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
1119e 50 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c Pagesize(pPager,
1119f 20 26 69 50 61 67 65 53 69 7a 65 31 36 29 3b 0a &iPageSize16);.
111a0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
111a1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
111a2 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
111a3 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 QLITE_OK || iPag
111a4 65 53 69 7a 65 31 36 3d 3d 28 75 31 36 29 69 50 eSize16==(u16)iP
111a5 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 ageSize );..
111a6 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 73 /* Update the as
111a7 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a sumed sector-siz
111a8 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 e to match the v
111a9 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20 alue used by .
111aa 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 ** the process
111ab 20 74 68 61 74 20 63 72 65 61 74 65 64 20 74 68 that created th
111ac 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 is journal. If t
111ad 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a his journal was.
111ae 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 ** created b
111af 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68 65 y a process othe
111b0 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c r than this one,
111b1 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
111b2 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 65 69 ne. ** is bei
111b3 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 ng called from w
111b4 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 ithin pager_play
111b5 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 back(). The loca
111b6 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f l value. ** o
111b7 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 f Pager.sectorSi
111b8 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 61 ze is restored a
111b9 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 61 t the end of tha
111ba 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a t routine.. *
111bb 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 /. pPager->se
111bc 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74 ctorSize = iSect
111bd 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 orSize;. }.. p
111be 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
111bf 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 f += JOURNAL_HDR
111c0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72 _SZ(pPager);. r
111c1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
111c2 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 75 .** Write the su
111c3 70 70 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f pplied master jo
111c4 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 urnal name into
111c5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
111c6 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 for pager.** pP
111c7 61 67 65 72 20 61 74 20 74 68 65 20 63 75 72 72 ager at the curr
111c8 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 ent location. Th
111c9 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
111ca 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68 name must be th
111cb 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 e last.** thing
111cc 77 72 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 written to a jou
111cd 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 rnal file. If th
111ce 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 66 75 e pager is in fu
111cf 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 ll-sync mode, th
111d0 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c e.** journal fil
111d1 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 e descriptor is
111d2 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20 advanced to the
111d3 6e 65 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e next sector boun
111d4 64 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 dary before.** a
111d5 6e 79 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 nything is writt
111d6 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69 en. The format i
111d7 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 s:.**.** + 4 b
111d8 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 ytes: PAGER_MJ_P
111d9 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 GNO..** + N by
111da 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 tes: Master jour
111db 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 nal filename in
111dc 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 utf-8..** + 4
111dd 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 bytes: N (length
111de 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
111df 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 al name in bytes
111e0 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 , no nul-termina
111e1 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 tor)..** + 4 b
111e2 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 ytes: Master jou
111e3 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 rnal name checks
111e4 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74 um..** + 8 byt
111e5 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 es: aJournalMagi
111e6 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d c[]..**.** The m
111e7 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 aster journal pa
111e8 67 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74 ge checksum is t
111e9 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79 he sum of the by
111ea 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 tes in the maste
111eb 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d r.** journal nam
111ec 65 2c 20 77 68 65 72 65 20 65 61 63 68 20 62 79 e, where each by
111ed 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 te is interprete
111ee 64 20 61 73 20 61 20 73 69 67 6e 65 64 20 38 2d d as a signed 8-
111ef 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a bit integer..**.
111f0 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73 ** If zMaster is
111f1 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
111f2 28 6f 63 63 75 72 73 20 66 6f 72 20 61 20 73 69 (occurs for a si
111f3 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
111f4 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 ansaction), .**
111f5 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e this call is a n
111f6 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
111f7 69 6e 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a int writeMasterJ
111f8 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 ournal(Pager *pP
111f9 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 ager, const char
111fa 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e *zMaster){. in
111fb 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
111fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
111fd 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
111fe 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20 . int nMaster;
111ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11200 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 /* Length of
11201 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 string zMaster
11202 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
11203 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11204 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
11205 6f 66 20 68 65 61 64 65 72 20 69 6e 20 6a 6f 75 of header in jou
11206 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 rnal file */. i
11207 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20 64 jrnlSize;
11208 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11209 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e /* Size of journ
1120a 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 al file on disk
1120b 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d */. u32 cksum =
1120c 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1120d 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 /* Checksu
1120e 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 m of string zMas
1120f 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a ter */.. if( !z
11210 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72 Master || pPager
11211 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20 20 20 7c ->setMaster. |
11212 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 | pPager->journa
11213 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
11214 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
11215 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a . || pPager->j
11216 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
11217 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
11218 46 20 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 F . ){. retu
11219 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1121a 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d }. pPager->setM
1121b 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73 aster = 1;. ass
1121c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
1121d 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f er->jfd) );.. /
1121e 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 * Calculate the
1121f 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 length in bytes
11220 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d and the checksum
11221 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 of zMaster */.
11222 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20 for(nMaster=0;
11223 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d zMaster[nMaster]
11224 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20 ; nMaster++){.
11225 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 cksum += zMast
11226 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d er[nMaster];. }
11227 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c .. /* If in ful
11228 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76 l-sync mode, adv
11229 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 ance to the next
1122a 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65 66 disk sector bef
1122b 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a ore writing. **
1122c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1122d 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 69 nal name. This i
1122e 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 70 72 s in case the pr
1122f 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 69 74 evious page writ
11230 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 ten to. ** the
11231 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 65 journal has alre
11232 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2e ady been synced.
11233 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 . */. if( pPag
11234 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a er->fullSync ){.
11235 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
11236 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c nalOff = journal
11237 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 HdrOffset(pPager
11238 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 );. }. iHdrOff
11239 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
1123a 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69 alOff;.. /* Wri
1123b 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
1123c 75 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 74 68 urnal data to th
1123d 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 e end of the jou
1123e 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20 rnal file. If.
1123f 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 ** an error occu
11240 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 rs, return the e
11241 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 rror code to the
11242 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20 caller.. */.
11243 69 66 28 20 28 30 20 21 3d 20 28 72 63 20 3d 20 if( (0 != (rc =
11244 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
11245 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11246 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 , PAGER_MJ_PGNO(
11247 70 50 61 67 65 72 29 29 29 29 0a 20 20 20 7c 7c pPager)))). ||
11248 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c (0 != (rc = sql
11249 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
1124a 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 er->jfd, zMaster
1124b 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 64 72 4f , nMaster, iHdrO
1124c 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20 28 30 ff+4))). || (0
1124d 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 != (rc = write3
1124e 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
1124f 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 d, iHdrOff+4+nMa
11250 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 29 29 29 ster, nMaster)))
11251 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 . || (0 != (rc
11252 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
11253 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 Pager->jfd, iHdr
11254 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c Off+4+nMaster+4,
11255 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20 cksum))). ||
11256 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 (0 != (rc = sqli
11257 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
11258 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c r->jfd, aJournal
11259 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 72 4f 66 Magic, 8, iHdrOf
1125a 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29 f+4+nMaster+8)))
1125b 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
1125c 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 rc;. }. pPage
1125d 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d r->journalOff +=
1125e 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20 (nMaster+20);.
1125f 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
11260 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 c = !pPager->noS
11261 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ync;.. /* If th
11262 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 e pager is in pe
11263 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 ristent-journal
11264 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 mode, then the p
11265 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f hysical . ** jo
11266 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 urnal-file may e
11267 78 74 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 xtend past the e
11268 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 nd of the master
11269 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 -journal name.
1126a 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f ** and 8 bytes o
1126b 66 20 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 f magic data jus
1126c 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 t written to the
1126d 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a file. This is .
1126e 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 ** dangerous b
1126f 65 63 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 ecause the code
11270 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f to rollback a ho
11271 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 t-journal file.
11272 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 ** will not be
11273 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 able to find the
11274 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
11275 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e name to determin
11276 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 e . ** whether
11277 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e or not the journ
11278 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a al is hot. . **
11279 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 . ** Easiest th
1127a 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 ing to do in thi
1127b 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f s scenario is to
1127c 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f truncate the jo
1127d 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 urnal . ** file
1127e 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 to the required
1127f 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 size.. */ . i
11280 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 f( SQLITE_OK==(r
11281 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
11282 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 eSize(pPager->jf
11283 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20 d, &jrnlSize)).
11284 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50 && jrnlSize>pP
11285 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11286 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 . ){. rc = s
11287 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
11288 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
11289 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
1128a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1128b 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e rc;.}../*.** Fin
1128c 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 d a page in the
1128d 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e hash table given
1128e 20 69 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 its page number
1128f 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f . Return.** a po
11290 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
11291 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 e or NULL if the
11292 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
11293 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61 is not .** alrea
11294 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f dy in memory..*/
11295 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 .static PgHdr *p
11296 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 ager_lookup(Page
11297 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 r *pPager, Pgno
11298 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a pgno){. PgHdr *
11299 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
1129a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1129b 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 turn value */..
1129c 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f /* It is not po
1129d 73 73 69 62 6c 65 20 66 6f 72 20 61 20 63 61 6c ssible for a cal
1129e 6c 20 74 6f 20 50 63 61 63 68 65 46 65 74 63 68 l to PcacheFetch
1129f 28 29 20 77 69 74 68 20 63 72 65 61 74 65 46 6c () with createFl
112a0 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61 ag==0 to. ** fa
112a1 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 61 74 74 il, since no att
112a2 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 empt to allocate
112a3 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 dynamic memory
112a4 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20 will be made..
112a5 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 */. (void)sqlit
112a6 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 e3PcacheFetch(pP
112a7 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
112a8 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 gno, 0, &p);. r
112a9 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
112aa 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67 * Unless the pag
112ab 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 er is in error-s
112ac 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 61 6c tate, discard al
112ad 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 l in-memory page
112ae 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 s. If.** the pag
112af 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 er is in error-s
112b0 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 tate, then this
112b1 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e call is a no-op.
112b2 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79 .**.** TODO: Why
112b3 20 63 61 6e 20 77 65 20 6e 6f 74 20 72 65 73 65 can we not rese
112b4 74 20 74 68 65 20 70 61 67 65 72 20 77 68 69 6c t the pager whil
112b5 65 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 e in error state
112b6 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ?.*/.static void
112b7 20 70 61 67 65 72 5f 72 65 73 65 74 28 50 61 67 pager_reset(Pag
112b8 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
112b9 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50 f( SQLITE_OK==pP
112ba 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b ager->errCode ){
112bb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b . sqlite3Back
112bc 75 70 52 65 73 74 61 72 74 28 70 50 61 67 65 72 upRestart(pPager
112bd 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 20 20 ->pBackup);.
112be 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
112bf 61 72 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ar(pPager->pPCac
112c0 68 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d he);. pPager-
112c1 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 >dbSizeValid = 0
112c2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 ;. }.}../*.** F
112c3 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74 75 72 ree all structur
112c4 65 73 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e es in the Pager.
112c5 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 aSavepoint[] arr
112c6 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74 68 0a ay and set both.
112c7 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f ** Pager.aSavepo
112c8 69 6e 74 20 61 6e 64 20 50 61 67 65 72 2e 6e 53 int and Pager.nS
112c9 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 72 6f avepoint to zero
112ca 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 75 62 2d . Close the sub-
112cb 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 69 74 journal.** if it
112cc 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 is open and the
112cd 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e pager is not in
112ce 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e exclusive mode.
112cf 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
112d0 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f releaseAllSavepo
112d1 69 6e 74 73 28 50 61 67 65 72 20 2a 70 50 61 67 ints(Pager *pPag
112d2 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 er){. int ii;
112d3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
112d4 49 74 65 72 61 74 6f 72 20 66 6f 72 20 6c 6f 6f Iterator for loo
112d5 70 69 6e 67 20 74 68 72 6f 75 67 68 20 50 61 67 ping through Pag
112d6 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 2a 2f er.aSavepoint */
112d7 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c . for(ii=0; ii<
112d8 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
112d9 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 nt; ii++){. s
112da 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
112db 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76 roy(pPager->aSav
112dc 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 epoint[ii].pInSa
112dd 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20 20 vepoint);. }.
112de 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 if( !pPager->exc
112df 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 73 71 lusiveMode || sq
112e0 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 lite3IsMemJourna
112e1 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 l(pPager->sjfd)
112e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ){. sqlite3Os
112e3 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 73 6a Close(pPager->sj
112e4 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 fd);. }. sqlit
112e5 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e e3_free(pPager->
112e6 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70 aSavepoint);. p
112e7 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
112e8 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d t = 0;. pPager-
112e9 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b >nSavepoint = 0;
112ea 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 . pPager->nSubR
112eb 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ec = 0;.}../*.**
112ec 20 53 65 74 20 74 68 65 20 62 69 74 20 6e 75 6d Set the bit num
112ed 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 ber pgno in the
112ee 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 PagerSavepoint.p
112ef 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 InSavepoint .**
112f0 62 69 74 76 65 63 73 20 6f 66 20 61 6c 6c 20 6f bitvecs of all o
112f1 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 pen savepoints.
112f2 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
112f3 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 0a 2a if successful.*
112f4 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 * or SQLITE_NOME
112f5 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 M if a malloc fa
112f6 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 2a 2f ilure occurs..*/
112f7 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54 .static int addT
112f8 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 oSavepointBitvec
112f9 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c s(Pager *pPager,
112fa 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 Pgno pgno){. i
112fb 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
112fc 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
112fd 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
112fe 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
112ff 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c ; /* Resul
11300 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 t code */.. for
11301 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 (ii=0; ii<pPager
11302 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 ->nSavepoint; ii
11303 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61 ++){. PagerSa
11304 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 vepoint *p = &pP
11305 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
11306 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 67 [ii];. if( pg
11307 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a no<=p->nOrig ){.
11308 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71 6c 69 rc |= sqli
11309 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e te3BitvecSet(p->
1130a 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 pInSavepoint, pg
1130b 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 no);. testc
1130c 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ase( rc==SQLITE_
1130d 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 61 NOMEM );. a
1130e 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1130f 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 E_OK || rc==SQLI
11310 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
11311 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
11312 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f c;.}../*.** Unlo
11313 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ck the database
11314 66 69 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 file. This funct
11315 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 ion is a no-op i
11316 66 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 69 f the pager.** i
11317 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d s in exclusive m
11318 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ode..**.** If th
11319 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72 65 e pager is curre
1131a 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72 20 73 74 ntly in error st
1131b 61 74 65 2c 20 64 69 73 63 61 72 64 20 74 68 65 ate, discard the
1131c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a contents of .**
1131d 20 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 72 the cache and r
1131e 65 73 65 74 20 74 68 65 20 50 61 67 65 72 20 73 eset the Pager s
1131f 74 72 75 63 74 75 72 65 20 69 6e 74 65 72 6e 61 tructure interna
11320 6c 20 73 74 61 74 65 2e 20 49 66 20 74 68 65 72 l state. If ther
11321 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 e is.** an open
11322 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 journal-file, th
11323 65 6e 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 en the next time
11324 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 a shared-lock i
11325 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e s obtained.** on
11326 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 the pager file
11327 28 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 (by this or any
11328 6f 74 68 65 72 20 70 72 6f 63 65 73 73 29 2c 20 other process),
11329 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 it will be.** tr
1132a 65 61 74 65 64 20 61 73 20 61 20 68 6f 74 2d 6a eated as a hot-j
1132b 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 ournal and rolle
1132c 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 d back..*/.stati
1132d 63 20 76 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c c void pager_unl
1132e 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 ock(Pager *pPage
1132f 72 29 7b 0a 20 20 69 66 28 20 21 70 50 61 67 65 r){. if( !pPage
11330 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
11331 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 ){. int rc;
11332 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11333 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
11334 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 ode */.. /* A
11335 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 68 65 20 lways close the
11336 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 journal file whe
11337 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 65 20 64 n dropping the d
11338 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a 20 20 atabase lock..
11339 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ** Otherwise,
1133a 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 another connecti
1133b 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f on with journal_
1133c 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69 67 68 mode=delete migh
1133d 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 t. ** delete
1133e 74 68 65 20 66 69 6c 65 20 6f 75 74 20 66 72 6f the file out fro
1133f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 20 20 20 m under us..
11340 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 */. sqlite3Os
11341 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 Close(pPager->jf
11342 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 d);. sqlite3B
11343 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
11344 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 ger->pInJournal)
11345 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 ;. pPager->pI
11346 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 nJournal = 0;.
11347 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 releaseAllSave
11348 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b 0a points(pPager);.
11349 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 . /* If the f
1134a 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c ile is unlocked,
1134b 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73 65 20 6d somebody else m
1134c 69 67 68 74 20 63 68 61 6e 67 65 20 69 74 2e 20 ight change it.
1134d 54 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 The. ** value
1134e 73 20 73 74 6f 72 65 64 20 69 6e 20 50 61 67 65 s stored in Page
1134f 72 2e 64 62 53 69 7a 65 20 65 74 63 2e 20 6d 69 r.dbSize etc. mi
11350 67 68 74 20 62 65 63 6f 6d 65 20 69 6e 76 61 6c ght become inval
11351 69 64 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 69 id if. ** thi
11352 73 20 68 61 70 70 65 6e 73 2e 20 54 4f 44 4f 3a s happens. TODO:
11353 20 52 65 61 6c 6c 79 2c 20 74 68 69 73 20 64 6f Really, this do
11354 65 73 6e 27 74 20 6e 65 65 64 20 74 6f 20 62 65 esn't need to be
11355 20 63 6c 65 61 72 65 64 0a 20 20 20 20 2a 2a 20 cleared. **
11356 75 6e 74 69 6c 20 74 68 65 20 63 68 61 6e 67 65 until the change
11357 2d 63 6f 75 6e 74 65 72 20 63 68 65 63 6b 20 66 -counter check f
11358 61 69 6c 73 20 69 6e 20 70 61 67 65 72 53 68 61 ails in pagerSha
11359 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 20 20 2a redLock().. *
1135a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 /. pPager->db
1135b 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 0a SizeValid = 0;..
1135c 20 20 20 20 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 rc = osUnloc
1135d 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e 4f k(pPager->fd, NO
1135e 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 _LOCK);. if(
1135f 72 63 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 rc ){. pPag
11360 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 er->errCode = rc
11361 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 ;. }. IOTR
11362 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c ACE(("UNLOCK %p\
11363 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 n", pPager))..
11364 20 20 2f 2a 20 49 66 20 50 61 67 65 72 2e 65 72 /* If Pager.er
11365 72 43 6f 64 65 20 69 73 20 73 65 74 2c 20 74 68 rCode is set, th
11366 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
11367 65 20 70 61 67 65 72 20 63 61 63 68 65 20 63 61 e pager cache ca
11368 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a 2a 20 74 nnot be. ** t
11369 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 68 61 74 rusted. Now that
1136a 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 the pager file
1136b 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 is unlocked, the
1136c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1136d 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 20 63 61 . ** cache ca
1136e 6e 20 62 65 20 64 69 73 63 61 72 64 65 64 20 61 n be discarded a
1136f 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 nd the error cod
11370 65 20 73 61 66 65 6c 79 20 63 6c 65 61 72 65 64 e safely cleared
11371 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
11372 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11373 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 ){. if( rc
11374 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
11375 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 pPager->e
11376 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f rrCode = SQLITE_
11377 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OK;. }.
11378 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 pager_reset(pP
11379 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 ager);. }..
1137a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 pPager->change
1137b 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 CountDone = 0;.
1137c 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 pPager->state
1137d 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b = PAGER_UNLOCK;
1137e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
1137f 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 is function shou
11380 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 ld be called whe
11381 6e 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f 52 52 n an IOERR, CORR
11382 55 50 54 20 6f 72 20 46 55 4c 4c 20 65 72 72 6f UPT or FULL erro
11383 72 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20 6f 63 r.** may have oc
11384 63 75 72 72 65 64 2e 20 54 68 65 20 66 69 72 73 curred. The firs
11385 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 t argument is a
11386 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
11387 61 67 65 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 ager .** structu
11388 72 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 74 re, the second t
11389 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62 he error-code ab
1138a 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e out to be return
1138b 65 64 20 62 79 20 61 20 70 61 67 65 72 20 0a 2a ed by a pager .*
1138c 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 * API function.
1138d 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e The value return
1138e 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 ed is a copy of
1138f 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
11390 65 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69 73 20 ent .** to this
11391 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a function. .**.**
11392 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 If the second a
11393 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 rgument is SQLIT
11394 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f E_IOERR, SQLITE_
11395 43 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 CORRUPT, or SQLI
11396 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 TE_FULL.** the e
11397 72 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 rror becomes per
11398 73 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 sistent. Until t
11399 68 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 he persisten err
1139a 6f 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a or is cleared,.*
1139b 2a 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 * subsequent API
1139c 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 calls on this P
1139d 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 ager will immedi
1139e 61 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 ately return the
1139f 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 same .** error
113a0 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 code..**.** A pe
113a1 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 rsistent error i
113a2 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 ndicates that th
113a3 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
113a4 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a e pager-cache .*
113a5 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 * cannot be trus
113a6 74 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 ted. This state
113a7 63 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 can be cleared b
113a8 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 y completely dis
113a9 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 carding .** the
113aa 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
113ab 70 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 pager-cache. If
113ac 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 a transaction wa
113ad 73 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a s active when.**
113ae 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 the persistent
113af 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 error occurred,
113b0 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 then the rollbac
113b1 6b 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 k journal may ne
113b2 65 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c ed.** to be repl
113b3 61 79 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 ayed to restore
113b4 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
113b5 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
113b6 65 20 28 61 73 20 69 66 0a 2a 2a 20 69 74 20 77 e (as if.** it w
113b7 65 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ere a hot-journa
113b8 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e l)..*/.static in
113b9 74 20 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 t pager_error(Pa
113ba 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
113bb 20 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20 rc){. int rc2
113bc 3d 20 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61 = rc & 0xff;. a
113bd 73 73 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 ssert(. pP
113be 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 ager->errCode==S
113bf 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 QLITE_FULL ||.
113c0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 pPager->err
113c1 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
113c2 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 ||. (pPage
113c3 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 r->errCode & 0xf
113c4 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 f)==SQLITE_IOERR
113c5 0a 20 20 29 3b 0a 20 20 69 66 28 0a 20 20 20 20 . );. if(.
113c6 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c rc2==SQLITE_FULL
113c7 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c ||. rc2==SQL
113c8 49 54 45 5f 49 4f 45 52 52 20 7c 7c 0a 20 20 20 ITE_IOERR ||.
113c9 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 rc2==SQLITE_COR
113ca 52 55 50 54 0a 20 20 29 7b 0a 20 20 20 20 70 50 RUPT. ){. pP
113cb 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 ager->errCode =
113cc 72 63 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 rc;. if( pPag
113cd 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
113ce 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20 26 26 _UNLOCK . &&
113cf 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
113d0 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
113d1 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 PCache)==0 .
113d2 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ){. /* If t
113d3 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 65 he pager is alre
113d4 61 64 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 63 61 ady unlocked, ca
113d5 6c 6c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 ll pager_unlock(
113d6 29 20 6e 6f 77 20 74 6f 0a 20 20 20 20 20 20 2a ) now to. *
113d7 2a 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f * clear the erro
113d8 72 20 73 74 61 74 65 20 61 6e 64 20 65 6e 73 75 r state and ensu
113d9 72 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 re that the page
113da 72 2d 63 61 63 68 65 20 69 73 20 0a 20 20 20 20 r-cache is .
113db 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 ** completely
113dc 65 6d 70 74 79 2e 0a 20 20 20 20 20 20 2a 2f 0a empty.. */.
113dd 20 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f pager_unlo
113de 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 ck(pPager);.
113df 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
113e0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 c;.}../*.** Exec
113e1 75 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 ute a rollback i
113e2 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
113e3 69 73 20 61 63 74 69 76 65 20 61 6e 64 20 75 6e is active and un
113e4 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74 lock the .** dat
113e5 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a abase file. .**.
113e6 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
113e7 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 has already ente
113e8 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 red the error st
113e9 61 74 65 2c 20 64 6f 20 6e 6f 74 20 61 74 74 65 ate, do not atte
113ea 6d 70 74 20 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c mpt .** the roll
113eb 62 61 63 6b 20 61 74 20 74 68 69 73 20 74 69 6d back at this tim
113ec 65 2e 20 49 6e 73 74 65 61 64 2c 20 70 61 67 65 e. Instead, page
113ed 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61 r_unlock() is ca
113ee 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c lled. The.** cal
113ef 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 l to pager_unloc
113f0 6b 28 29 20 77 69 6c 6c 20 64 69 73 63 61 72 64 k() will discard
113f1 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 all in-memory p
113f2 61 67 65 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 ages, unlock.**
113f3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
113f4 65 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20 e and clear the
113f5 65 72 72 6f 72 20 73 74 61 74 65 2e 20 49 66 20 error state. If
113f6 74 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a this means that.
113f7 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 6f ** there is a ho
113f8 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69 t-journal left i
113f9 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 n the file-syste
113fa 6d 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 6e m, the next conn
113fb 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 ection.** to obt
113fc 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 ain a shared loc
113fd 6b 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 28 k on the pager (
113fe 77 68 69 63 68 20 6d 61 79 20 62 65 20 74 68 69 which may be thi
113ff 73 20 6f 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 s one) will.** r
11400 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a oll it back..**.
11401 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
11402 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 has not already
11403 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f entered the erro
11404 72 20 73 74 61 74 65 2c 20 62 75 74 20 61 6e 20 r state, but an
11405 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 IO or.** malloc
11406 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
11407 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 ing a rollback,
11408 74 68 65 6e 20 74 68 69 73 20 77 69 6c 6c 20 69 then this will i
11409 74 73 65 6c 66 20 63 61 75 73 65 20 0a 2a 2a 20 tself cause .**
1140a 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74 the pager to ent
1140b 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 er the error sta
1140c 74 65 2e 20 57 68 69 63 68 20 77 69 6c 6c 20 62 te. Which will b
1140d 65 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 e cleared by the
1140e 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 .** call to page
1140f 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 64 r_unlock(), as d
11410 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a escribed above..
11411 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
11412 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c agerUnlockAndRol
11413 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 lback(Pager *pPa
11414 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 ger){. if( pPag
11415 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
11416 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 ITE_OK && pPager
11417 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 ->state>=PAGER_R
11418 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 73 ESERVED ){. s
11419 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
1141a 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 73 nMalloc();. s
1141b 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
1141c 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 ack(pPager);.
1141d 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
1141e 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 nMalloc();. }.
1141f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 pager_unlock(pP
11420 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
11421 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 This routine end
11422 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e s a transaction.
11423 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 A transaction i
11424 73 20 75 73 75 61 6c 6c 79 20 65 6e 64 65 64 20 s usually ended
11425 62 79 20 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 by .** either a
11426 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c COMMIT or a ROLL
11427 42 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 BACK operation.
11428 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79 This routine may
11429 20 62 65 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 be called .** a
1142a 66 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 fter rollback of
1142b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 a hot-journal,
1142c 6f 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f or if an error o
1142d 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e ccurs while open
1142e 69 6e 67 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e ing.** the journ
1142f 61 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74 69 al file or writi
11430 6e 67 20 74 68 65 20 76 65 72 79 20 66 69 72 73 ng the very firs
11431 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 t journal-header
11432 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 of a.** databas
11433 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
11434 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 * .** If the pag
11435 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 er is in PAGER_S
11436 48 41 52 45 44 20 6f 72 20 50 41 47 45 52 5f 55 HARED or PAGER_U
11437 4e 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e NLOCK state when
11438 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
11439 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 is called, it i
1143a 73 20 61 20 6e 6f 2d 6f 70 20 28 72 65 74 75 72 s a no-op (retur
1143b 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a ns SQLITE_OK)..*
1143c 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 *.** Otherwise,
1143d 61 6e 79 20 61 63 74 69 76 65 20 73 61 76 65 70 any active savep
1143e 6f 69 6e 74 73 20 61 72 65 20 72 65 6c 65 61 73 oints are releas
1143f 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
11440 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
11441 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 69 open, then it i
11442 73 20 22 66 69 6e 61 6c 69 7a 65 64 22 2e 20 4f s "finalized". O
11443 6e 63 65 20 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a nce a journal .*
11444 2a 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 * file has been
11445 66 69 6e 61 6c 69 7a 65 64 20 69 74 20 69 73 20 finalized it is
11446 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
11447 75 73 65 20 69 74 20 74 6f 20 72 6f 6c 6c 20 62 use it to roll b
11448 61 63 6b 20 61 20 0a 2a 2a 20 74 72 61 6e 73 61 ack a .** transa
11449 63 74 69 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 ction. Nor will
1144a 69 74 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 it be considered
1144b 20 74 6f 20 62 65 20 61 20 68 6f 74 2d 6a 6f 75 to be a hot-jou
1144c 72 6e 61 6c 20 62 79 20 74 68 69 73 0a 2a 2a 20 rnal by this.**
1144d 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74 or any other dat
1144e 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1144f 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 61 20 . Exactly how a
11450 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c journal is final
11451 69 7a 65 64 0a 2a 2a 20 64 65 70 65 6e 64 73 20 ized.** depends
11452 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f on whether or no
11453 74 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 t the pager is r
11454 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 unning in exclus
11455 69 76 65 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 ive mode and.**
11456 74 68 65 20 63 75 72 72 65 6e 74 20 6a 6f 75 72 the current jour
11457 6e 61 6c 2d 6d 6f 64 65 20 28 50 61 67 65 72 2e nal-mode (Pager.
11458 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 journalMode valu
11459 65 29 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a e), as follows:.
1145a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d **.** journalM
1145b 6f 64 65 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 ode==MEMORY.**
1145c 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 Journal file
1145d 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 73 69 descriptor is si
1145e 6d 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68 69 mply closed. Thi
1145f 73 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0a 2a s destroys an .*
11460 2a 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 * in-memory
11461 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 journal..**.**
11462 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 journalMode==TR
11463 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 20 4a 6f UNCATE.** Jo
11464 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 72 urnal file is tr
11465 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 uncated to zero
11466 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a bytes in size..*
11467 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f *.** journalMo
11468 64 65 3d 3d 50 45 52 53 49 53 54 0a 2a 2a 20 20 de==PERSIST.**
11469 20 20 20 54 68 65 20 66 69 72 73 74 20 32 38 20 The first 28
1146a 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 bytes of the jou
1146b 72 6e 61 6c 20 66 69 6c 65 20 61 72 65 20 7a 65 rnal file are ze
1146c 72 6f 65 64 2e 20 54 68 69 73 20 69 6e 76 61 6c roed. This inval
1146d 69 64 61 74 65 73 0a 2a 2a 20 20 20 20 20 74 68 idates.** th
1146e 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 e first journal
1146f 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 66 69 header in the fi
11470 6c 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 68 le, and hence th
11471 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c e entire journal
11472 0a 2a 2a 20 20 20 20 20 66 69 6c 65 2e 20 41 6e .** file. An
11473 20 69 6e 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c invalid journal
11474 20 66 69 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 file cannot be
11475 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
11476 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ** journalMode
11477 3d 3d 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 ==DELETE.**
11478 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 The journal file
11479 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 is closed and d
1147a 65 6c 65 74 65 64 20 75 73 69 6e 67 20 73 71 6c eleted using sql
1147b 69 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a ite3OsDelete()..
1147c 2a 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74 68 65 **.** If the
1147d 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e pager is runnin
1147e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d g in exclusive m
1147f 6f 64 65 2c 20 74 68 69 73 20 6d 65 74 68 6f 64 ode, this method
11480 20 6f 66 20 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a of finalizing.*
11481 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 * the journa
11482 6c 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72 20 l file is never
11483 75 73 65 64 2e 20 49 6e 73 74 65 61 64 2c 20 69 used. Instead, i
11484 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 f the journalMod
11485 65 20 69 73 0a 2a 2a 20 20 20 20 20 44 45 4c 45 e is.** DELE
11486 54 45 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 TE and the pager
11487 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 is in exclusive
11488 20 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68 6f mode, the metho
11489 64 20 64 65 73 63 72 69 62 65 64 20 75 6e 64 65 d described unde
1148a 72 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c r.** journal
1148b 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69 73 Mode==PERSIST is
1148c 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a used instead..*
1148d 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6a *.** After the j
1148e 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 ournal is finali
1148f 7a 65 64 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20 zed, if running
11490 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65 in non-exclusive
11491 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 70 61 mode, the.** pa
11492 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 ger moves to PAG
11493 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 20 ER_SHARED state
11494 28 61 6e 64 20 64 6f 77 6e 67 72 61 64 65 73 20 (and downgrades
11495 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a the lock on the.
11496 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
11497 20 61 63 63 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a accordingly)..*
11498 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
11499 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 r is running in
1149a 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 exclusive mode a
1149b 6e 64 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 nd is in PAGER_S
1149c 59 4e 43 45 44 20 73 74 61 74 65 2c 0a 2a 2a 20 YNCED state,.**
1149d 69 74 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 it moves to PAGE
1149e 52 5f 45 58 43 4c 55 53 49 56 45 2e 20 4e 6f 20 R_EXCLUSIVE. No
1149f 6c 6f 63 6b 73 20 61 72 65 20 64 6f 77 6e 67 72 locks are downgr
114a0 61 64 65 64 20 77 68 65 6e 20 72 75 6e 6e 69 6e aded when runnin
114a1 67 20 69 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 76 g in.** exclusiv
114a2 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 e mode..**.** SQ
114a3 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
114a4 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 ned if no error
114a5 6f 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65 72 occurs. If an er
114a6 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
114a7 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68 65 20 g.** any of the
114a8 49 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f IO operations to
114a9 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f finalize the jo
114aa 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 75 6e urnal file or un
114ab 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61 lock the.** data
114ac 62 61 73 65 20 74 68 65 6e 20 74 68 65 20 49 4f base then the IO
114ad 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
114ae 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 eturned to the u
114af 73 65 72 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 ser. If the .**
114b0 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 66 69 6e operation to fin
114b1 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 alize the journa
114b2 6c 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 74 68 l file fails, th
114b3 65 6e 20 74 68 65 20 63 6f 64 65 20 73 74 69 6c en the code stil
114b4 6c 0a 2a 2a 20 74 72 69 65 73 20 74 6f 20 75 6e l.** tries to un
114b5 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
114b6 65 20 66 69 6c 65 20 69 66 20 6e 6f 74 20 69 6e e file if not in
114b7 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e exclusive mode.
114b8 20 49 66 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 If the.** unloc
114b9 6b 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c k operation fail
114ba 73 20 61 73 20 77 65 6c 6c 2c 20 74 68 65 6e 20 s as well, then
114bb 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 the first error
114bc 63 6f 64 65 20 72 65 6c 61 74 65 64 0a 2a 2a 20 code related.**
114bd 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 72 72 to the first err
114be 6f 72 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 or encountered (
114bf 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 the journal fina
114c0 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 73 lization one) is
114c1 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f .** returned..*/
114c2 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
114c3 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
114c4 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c n(Pager *pPager,
114c5 20 69 6e 74 20 68 61 73 4d 61 73 74 65 72 29 7b int hasMaster){
114c6 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
114c7 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45 TE_OK; /* E
114c8 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a rror code from j
114c9 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 ournal finalizat
114ca 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f ion operation */
114cb 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c . int rc2 = SQL
114cc 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 ITE_OK; /* E
114cd 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 64 rror code from d
114ce 62 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 b file unlock op
114cf 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 eration */.. if
114d0 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c ( pPager->state<
114d1 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
114d2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
114d3 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 ITE_OK;. }. re
114d4 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e leaseAllSavepoin
114d5 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 61 ts(pPager);.. a
114d6 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
114d7 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 ager->jfd) || pP
114d8 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
114d9 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 4f ==0 );. if( isO
114da 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
114db 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f ){.. /* TODO
114dc 3a 20 54 68 65 72 65 27 73 20 61 20 70 72 6f 62 : There's a prob
114dd 6c 65 6d 20 68 65 72 65 20 69 66 20 61 20 6a 6f lem here if a jo
114de 75 72 6e 61 6c 2d 66 69 6c 65 20 77 61 73 20 6f urnal-file was o
114df 70 65 6e 65 64 20 69 6e 20 4d 45 4d 4f 52 59 0a pened in MEMORY.
114e0 20 20 20 20 2a 2a 20 6d 6f 64 65 20 61 6e 64 20 ** mode and
114e1 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
114e2 2d 6d 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 -mode is changed
114e3 20 74 6f 20 54 52 55 4e 43 41 54 45 20 6f 72 20 to TRUNCATE or
114e4 50 45 52 53 49 53 54 0a 20 20 20 20 2a 2a 20 64 PERSIST. ** d
114e5 75 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 uring the transa
114e6 63 74 69 6f 6e 2e 20 54 68 69 73 20 63 6f 64 65 ction. This code
114e7 20 73 68 6f 75 6c 64 20 62 65 20 63 68 61 6e 67 should be chang
114e8 65 64 20 74 6f 20 61 73 73 75 6d 65 0a 20 20 20 ed to assume.
114e9 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6a 6f 75 ** that the jou
114ea 72 6e 61 6c 20 6d 6f 64 65 20 68 61 73 20 6e 6f rnal mode has no
114eb 74 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 t changed since
114ec 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
114ed 77 61 73 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 was. ** start
114ee 65 64 2e 20 41 6e 64 20 74 68 65 20 73 71 6c 69 ed. And the sqli
114ef 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d te3PagerJournalM
114f0 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 ode() function s
114f1 68 6f 75 6c 64 20 62 65 0a 20 20 20 20 2a 2a 20 hould be. **
114f2 63 68 61 6e 67 65 64 20 74 6f 20 6d 61 6b 65 20 changed to make
114f3 73 75 72 65 20 74 68 61 74 20 74 68 69 73 20 69 sure that this i
114f4 73 20 74 68 65 20 63 61 73 65 20 74 6f 6f 2e 0a s the case too..
114f5 20 20 20 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 */.. /* F
114f6 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 inalize the jour
114f7 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 nal file. */.
114f8 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
114f9 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
114fa 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
114fb 52 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 RY ){. int
114fc 69 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20 isMemoryJournal
114fd 3d 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f = sqlite3IsMemJo
114fe 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 urnal(pPager->jf
114ff 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 d);. sqlite
11500 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
11501 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 >jfd);. if(
11502 20 21 69 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 !isMemoryJourna
11503 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 l ){. rc
11504 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 = sqlite3OsDelet
11505 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 e(pPager->pVfs,
11506 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
11507 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
11508 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 }else if( pPag
11509 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
1150a 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
1150b 44 45 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 DE_TRUNCATE ){.
1150c 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d if( pPager-
1150d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 >journalOff==0 )
1150e 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
1150f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
11510 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
11511 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 c = sqlite3OsTru
11512 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 ncate(pPager->jf
11513 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 d, 0);. }.
11514 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
11515 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 rnalOff = 0;.
11516 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
11517 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 alStarted = 0;.
11518 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 }else if( pPa
11519 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
1151a 64 65 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 de . || pPag
1151b 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
1151c 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
1151d 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29 DE_PERSIST. )
1151e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 {. rc = zer
1151f 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 oJournalHdr(pPag
11520 65 72 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a er, hasMaster);.
11521 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f pager_erro
11522 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 r(pPager, rc);.
11523 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
11524 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 rnalOff = 0;.
11525 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
11526 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 alStarted = 0;.
11527 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
11528 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
11529 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
1152a 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 ER_JOURNALMODE_D
1152b 45 4c 45 54 45 20 7c 7c 20 72 63 20 29 3b 0a 20 ELETE || rc );.
1152c 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
1152d 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ose(pPager->jfd)
1152e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
1152f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 SQLITE_OK && !pP
11530 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
11531 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
11532 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
11533 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 Pager->pVfs, pPa
11534 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 ger->zJournal, 0
11535 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
11536 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
11537 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 CHECK_PAGES.
11538 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 sqlite3PcacheIte
11539 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 rateDirty(pPager
1153a 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72 ->pPCache, pager
1153b 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a _set_pagehash);.
1153c 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69 #endif.. sqli
1153d 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c te3PcacheCleanAl
1153e 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 l(pPager->pPCach
1153f 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 e);. sqlite3B
11540 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
11541 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 ger->pInJournal)
11542 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 ;. pPager->pI
11543 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 nJournal = 0;.
11544 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d pPager->nRec =
11545 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 0;. }.. if( !
11546 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
11547 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 eMode ){. rc2
11548 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 = osUnlock(pPag
11549 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c er->fd, SHARED_L
1154a 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 OCK);. pPager
1154b 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
1154c 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 SHARED;. pPag
1154d 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 er->changeCountD
1154e 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 one = 0;. }else
1154f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
11550 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 te==PAGER_SYNCED
11551 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
11552 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 state = PAGER_EX
11553 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 CLUSIVE;. }. p
11554 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
11555 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
11556 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
11557 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
11558 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f ed = 0;.. /* TO
11559 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69 DO: Is this opti
1155a 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20 mal? Why is the
1155b 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61 db size invalida
1155c 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77 ted here . ** w
1155d 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
1155e 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c file is not unl
1155f 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67 ocked? */. pPag
11560 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d er->dbOrigSize =
11561 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 0;. sqlite3Pca
11562 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 cheTruncate(pPag
11563 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 er->pPCache, pPa
11564 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 ger->dbSize);.
11565 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 if( !MEMDB ){.
11566 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
11567 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a Valid = 0;. }..
11568 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 return (rc==SQ
11569 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b LITE_OK?rc2:rc);
1156a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 .}../*.** Parame
1156b 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70 ter aData must p
1156c 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 oint to a buffer
1156d 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65 of pPager->page
1156e 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 Size bytes.** of
1156f 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61 data. Compute a
11570 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 nd return a chec
11571 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74 ksum based ont t
11572 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
11573 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64 he .** page of d
11574 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72 ata and the curr
11575 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61 ent value of pPa
11576 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a ger->cksumInit..
11577 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f **.** This is no
11578 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 t a real checksu
11579 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20 m. It is really
1157a 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20 just the sum of
1157b 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 the .** random i
1157c 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50 nitial value (pP
1157d 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 ager->cksumInit)
1157e 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68 and every 200th
1157f 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 byte.** of the
11580 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74 page data, start
11581 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66 ing with byte of
11582 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61 fset (pPager->pa
11583 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 geSize%200)..**
11584 45 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 Each byte is int
11585 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38 erpreted as an 8
11586 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e -bit unsigned in
11587 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 teger..**.** Cha
11588 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c nging the formul
11589 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 a used to comput
1158a 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20 e this checksum
1158b 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a results in an.**
1158c 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f incompatible jo
1158d 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 urnal file forma
1158e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 t..**.** If jour
1158f 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f nal corruption o
11590 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70 ccurs due to a p
11591 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 ower failure, th
11592 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a e most likely .*
11593 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68 * scenario is th
11594 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68 at one end or th
11595 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72 e other of the r
11596 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68 ecord will be ch
11597 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73 anged. .** It is
11598 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c much less likel
11599 79 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65 y that the two e
1159a 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e nds of the journ
1159b 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 al record will b
1159c 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64 e.** correct and
1159d 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63 the middle be c
1159e 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74 orrupt. Thus, t
1159f 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73 his "checksum" s
115a0 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 cheme,.** though
115a1 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65 fast and simple
115a2 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f , catches the mo
115a3 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 stly likely kind
115a4 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a of corruption..
115a5 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 */.static u32 pa
115a6 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 ger_cksum(Pager
115a7 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 *pPager, const u
115a8 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 8 *aData){. u32
115a9 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d cksum = pPager-
115aa 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 >cksumInit;
115ab 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 /* Checksum
115ac 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 value to return
115ad 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 */. int i = pPa
115ae 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 ger->pageSize-20
115af 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 0; /* L
115b0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
115b1 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 while( i>0 ){.
115b2 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74 cksum += aDat
115b3 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32 a[i];. i -= 2
115b4 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 00;. }. return
115b5 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cksum;.}../*.**
115b6 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 Read a single p
115b7 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20 age from either
115b8 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
115b9 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d (if isMainJrnl=
115ba 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 =1) or.** from t
115bb 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 he sub-journal (
115bc 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 if isMainJrnl==0
115bd 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74 ) and playback t
115be 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65 hat page..** The
115bf 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20 page begins at
115c0 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20 offset *pOffset
115c1 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 into the file. T
115c2 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76 he *pOffset.** v
115c3 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 alue is increase
115c4 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f d to the start o
115c5 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 f the next page
115c6 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a in the journal..
115c7 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e **.** The isMain
115c8 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75 Jrnl flag is tru
115c9 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 e if this is the
115ca 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a main rollback j
115cb 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 ournal and.** fa
115cc 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 lse for the stat
115cd 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 ement journal.
115ce 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 The main rollbac
115cf 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a k journal uses.*
115d0 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 * checksums - th
115d1 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
115d2 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a nal does not..**
115d3 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 .** If the page
115d4 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 number of the pa
115d5 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66 ge record read f
115d6 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f rom the (sub-)jo
115d7 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 urnal file.** is
115d8 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
115d9 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
115da 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c of Pager.dbSize,
115db 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69 then playback i
115dc 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64 s.** skipped and
115dd 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
115de 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
115df 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 pDone is not NU
115e0 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 LL, then it is a
115e1 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73 record of pages
115e2 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61 that have alrea
115e3 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65 dy.** been playe
115e4 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20 d back. If the
115e5 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74 page at *pOffset
115e6 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
115e7 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a n played back.**
115e8 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 (if the corresp
115e9 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74 onding pDone bit
115ea 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b is set) then sk
115eb 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e ip the playback.
115ec 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
115ed 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72 e pDone bit corr
115ee 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
115ef 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69 *pOffset page i
115f0 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74 s set.** prior t
115f1 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a o returning..**.
115f2 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72 ** If the page r
115f3 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73 ecord is success
115f4 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 fully read from
115f5 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 the (sub-)journa
115f6 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c l file.** and pl
115f7 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20 ayed back, then
115f8 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
115f9 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 urned. If an IO
115fa 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 error occurs.**
115fb 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68 while reading th
115fc 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68 e record from th
115fd 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 e (sub-)journal
115fe 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72 file or while wr
115ff 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 iting.** to the
11600 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
11601 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 hen the IO error
11602 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
11603 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73 d. If data.** is
11604 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 successfully re
11605 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 ad from the (sub
11606 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 -)journal file b
11607 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 ut appears to be
11608 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53 .** corrupted, S
11609 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 QLITE_DONE is re
1160a 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20 turned. Data is
1160b 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75 considered corru
1160c 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 pted in.** two c
1160d 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a ircumstances:.**
1160e 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 .** * If the
1160f 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62 record page-numb
11610 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30 er is illegal (0
11611 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e or PAGER_MJ_PGN
11612 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 O), or.** * If
11613 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62 the record is b
11614 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b eing rolled back
11615 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a from the main j
11616 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 ournal file.**
11617 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b and the check
11618 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e sum field does n
11619 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63 ot match the rec
1161a 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a ord content..**.
1161b 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 ** Neither of th
1161c 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f ese two scenario
1161d 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64 s are possible d
1161e 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e uring a savepoin
1161f 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a t rollback..**.*
11620 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 * If this is a s
11621 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 avepoint rollbac
11622 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d k, then memory m
11623 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79 ay have to be dy
11624 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c namically.** all
11625 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 ocated by this f
11626 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 unction. If this
11627 20 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64 is the case and
11628 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 an allocation f
11629 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f ails,.** SQLITE_
1162a 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 NOMEM is returne
1162b 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
1162c 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
1162d 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65 one_page(. Page
1162e 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
1162f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
11630 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61 pager being pla
11631 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e yed back */. in
11632 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 t isMainJrnl,
11633 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
11634 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c -> main journal
11635 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e . 0 -> sub-journ
11636 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 al. */. int isU
11637 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20 nsync,
11638 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
11639 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75 f reading from u
1163a 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75 nsynced main jou
1163b 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70 rnal */. i64 *p
1163c 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 Offset,
1163d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
1163e 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70 t of record to p
1163f 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 layback */. int
11640 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20 isSavepnt,
11641 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
11642 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69 ue for a savepoi
11643 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 nt rollback */.
11644 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20 Bitvec *pDone
11645 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11646 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65 * Bitvec of page
11647 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64 s already played
11648 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e back */.){. in
11649 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 t rc;. PgHdr *p
1164a 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Pg;
1164b 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 /* An exis
1164c 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 ting page in the
1164d 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f cache */. Pgno
1164e 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
1164f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
11650 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
11651 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 a page in journa
11652 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d l */. u32 cksum
11653 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11654 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d /* Checksum
11655 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 used for sanity
11656 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 checking */. u
11657 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 8 *aData;
11658 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11659 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 Temporary storag
1165a 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a e for the page *
1165b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
1165c 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 *jfd;
1165d 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
1165e 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 scriptor for the
1165f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
11660 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d .. assert( (isM
11661 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 ainJrnl&~1)==0 )
11662 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e ; /* isMain
11663 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a Jrnl is 0 or 1 *
11664 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53 /. assert( (isS
11665 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b avepnt&~1)==0 );
11666 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65 /* isSave
11667 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f pnt is 0 or 1 */
11668 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69 . assert( isMai
11669 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 nJrnl || pDone )
1166a 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61 ; /* pDone a
1166b 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75 lways used on su
1166c 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 b-journals */.
1166d 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e assert( isSavepn
1166e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b t || pDone==0 );
1166f 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65 /* pDone neve
11670 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 r used on non-sa
11671 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 vepoint */.. aD
11672 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65 ata = (u8*)pPage
11673 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 r->pTmpSpace;.
11674 61 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b assert( aData );
11675 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 /* Temp
11676 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61 storage must ha
11677 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
11678 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 allocated */..
11679 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 /* Read the page
1167a 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 number and page
1167b 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a data from the j
1167c 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f ournal or sub-jo
1167d 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e urnal. ** file.
1167e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
1167f 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c code to the cal
11680 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72 ler if an IO err
11681 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a or occurs.. */.
11682 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 jfd = isMainJr
11683 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 nl ? pPager->jfd
11684 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b : pPager->sjfd;
11685 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 . rc = read32bi
11686 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 ts(jfd, *pOffset
11687 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 , &pgno);. if(
11688 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
11689 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 return rc;. rc
1168a 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
1168b 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 jfd, aData, pPag
1168c 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a er->pageSize, (*
1168d 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69 pOffset)+4);. i
1168e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1168f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
11690 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67 *pOffset += pPag
11691 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34 er->pageSize + 4
11692 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b + isMainJrnl*4;
11693 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 .. /* Sanity ch
11694 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 ecking on the pa
11695 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72 ge. This is mor
11696 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 e important that
11697 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 I originally.
11698 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20 ** thought. If
11699 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 a power failure
1169a 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 occurs while the
1169b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e journal is bein
1169c 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 g written,. **
1169d 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69 it could cause i
1169e 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62 nvalid data to b
1169f 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
116a0 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20 he journal. We
116a1 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74 need to. ** det
116a2 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64 ect this invalid
116a3 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68 data (with high
116a4 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e probability) an
116a5 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a d ignore it.. *
116a6 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 /. if( pgno==0
116a7 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d || pgno==PAGER_M
116a8 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 J_PGNO(pPager) )
116a9 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 {. assert( !i
116aa 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 sSavepnt );.
116ab 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
116ac 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 NE;. }. if( pg
116ad 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d no>(Pgno)pPager-
116ae 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74 >dbSize || sqlit
116af 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f e3BitvecTest(pDo
116b0 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 ne, pgno) ){.
116b1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
116b2 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d K;. }. if( isM
116b3 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72 ainJrnl ){. r
116b4 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a c = read32bits(j
116b5 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34 fd, (*pOffset)-4
116b6 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69 , &cksum);. i
116b7 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
116b8 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61 c;. if( !isSa
116b9 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63 vepnt && pager_c
116ba 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 ksum(pPager, aDa
116bb 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 ta)!=cksum ){.
116bc 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
116bd 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 E_DONE;. }.
116be 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26 }.. if( pDone &
116bf 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 & (rc = sqlite3B
116c0 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 itvecSet(pDone,
116c1 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f pgno))!=SQLITE_O
116c2 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
116c3 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 rc;. }.. asser
116c4 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
116c5 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ==PAGER_RESERVED
116c6 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 || pPager->stat
116c7 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 e>=PAGER_EXCLUSI
116c8 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 VE );.. /* If t
116c9 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52 he pager is in R
116ca 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 ESERVED state, t
116cb 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 hen there must b
116cc 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 e a copy of this
116cd 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 . ** page in th
116ce 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 e pager cache. I
116cf 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 n this case just
116d0 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 update the page
116d1 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f r cache,. ** no
116d2 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
116d3 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 ile. The page is
116d4 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 left marked dir
116d5 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ty in this case.
116d6 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 . **. ** An ex
116d7 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 ception to the a
116d8 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 bove rule: If th
116d9 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e e database is in
116da 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 no-sync mode.
116db 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 ** and a page is
116dc 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e moved during an
116dd 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 incremental vac
116de 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 uum then the pag
116df 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 e may. ** not b
116e0 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 e in the pager c
116e1 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 ache. Later: if
116e2 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f a malloc() or IO
116e3 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 error occurs.
116e4 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 ** during a Move
116e5 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 page() call, the
116e6 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e n the page may n
116e7 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 ot be in the cac
116e8 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 he. ** either.
116e9 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e So the condition
116ea 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 described in th
116eb 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 e above paragrap
116ec 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 h is not. ** as
116ed 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a sert()able.. **
116ee 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c . ** If in EXCL
116ef 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 USIVE state, the
116f0 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20 n we update the
116f1 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69 pager cache if i
116f2 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e t exists. ** an
116f3 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e d the main file.
116f4 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65 The page is the
116f5 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 n marked not dir
116f6 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 ty.. **. ** Ti
116f7 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 cket #1171: The
116f8 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
116f9 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e al might contain
116fa 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 page content th
116fb 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 at is. ** diffe
116fc 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 rent from the pa
116fd 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 ge content at th
116fe 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 e start of the t
116ff 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a ransaction.. **
11700 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 This occurs whe
11701 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e n a page is chan
11702 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 ged prior to the
11703 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 start of a stat
11704 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 ement. ** then
11705 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 changed again wi
11706 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 thin the stateme
11707 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e nt. When rollin
11708 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 g back such a.
11709 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 ** statement we
1170a 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 must not write t
1170b 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 o the original d
1170c 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 atabase unless w
1170d 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 e know. ** for
1170e 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 certain that ori
1170f 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 ginal page conte
11710 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 nts are synced i
11711 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c nto the main rol
11712 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e lback. ** journ
11713 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 al. Otherwise,
11714 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 a power loss mig
11715 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 ht leave modifie
11716 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 d data in the.
11717 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
11718 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 without an entr
11719 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 y in the rollbac
1171a 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 k journal that c
1171b 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 an. ** restore
1171c 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
1171d 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 its original for
1171e 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f m. Two conditio
1171f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 ns must be. **
11720 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 met before writi
11721 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
11722 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 se files. (1) th
11723 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 e database must
11724 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 be. ** locked.
11725 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 (2) we know tha
11726 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 t the original p
11727 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 age content is f
11728 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a ully synced. **
11729 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 in the main jou
1172a 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 rnal either beca
1172b 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20 use the page is
1172c 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 not in cache or
1172d 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 else. ** the pa
1172e 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ge is marked as
1172f 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a needSync==0.. *
11730 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 *. ** 2008-04-1
11731 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 4: When attempt
11732 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 ing to vacuum a
11733 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
11734 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 file, it. ** i
11735 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 s possible to fa
11736 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f il a statement o
11737 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61 n a database tha
11738 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 t does not yet e
11739 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f xist.. ** Do no
1173a 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 t attempt to wri
1173b 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66 te if database f
1173c 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 ile has never be
1173d 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a en opened.. */.
1173e 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f pPg = pager_lo
1173f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e okup(pPager, pgn
11740 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 o);. assert( pP
11741 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 g || !MEMDB );.
11742 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c PAGERTRACE(("PL
11743 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25 AYBACK %d page %
11744 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c d hash(%08x) %s\
11745 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
11746 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 PAGERID(pPage
11747 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f r), pgno, pager_
11748 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d datahash(pPager-
11749 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61 >pageSize, aData
1174a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
1174b 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d (isMainJrnl?"m
1174c 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 ain-journal":"su
1174d 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 b-journal"). ))
1174e 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d ;. if( (pPager-
1174f 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
11750 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28 CLUSIVE). && (
11751 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 pPg==0 || 0==(pP
11752 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
11753 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26 EED_SYNC)). &&
11754 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
11755 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73 fd). && !isUns
11756 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 ync. ){. i64
11757 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 ofst = (pgno-1)
11758 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 *(i64)pPager->pa
11759 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d geSize;. rc =
1175a 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1175b 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74 pPager->fd, aDat
1175c 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
1175d 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20 ize, ofst);.
1175e 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d if( pgno>pPager-
1175f 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 >dbFileSize ){.
11760 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 pPager->dbF
11761 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a ileSize = pgno;.
11762 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
11763 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 3BackupUpdate(pP
11764 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 ager->pBackup, p
11765 67 6e 6f 2c 20 61 44 61 74 61 29 3b 0a 20 20 7d gno, aData);. }
11766 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e else if( !isMain
11767 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29 Jrnl && pPg==0 )
11768 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
11769 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f is a rollback o
1176a 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e f a savepoint an
1176b 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77 d data was not w
1176c 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a ritten to. **
1176d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e the database an
1176e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f d the page is no
1176f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 t in-memory, the
11770 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61 re is a potentia
11771 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d l. ** problem
11772 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20 . When the page
11773 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20 is next fetched
11774 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 by the b-tree la
11775 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 yer, it . **
11776 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f will be read fro
11777 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
11778 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f ile, which may o
11779 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20 r may not be .
1177a 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20 ** current. .
1177b 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
1177c 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20 re are a couple
1177d 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 of different way
1177e 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 s this can happe
1177f 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65 n. All are quite
11780 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e . ** obscure.
11781 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e When running in
11782 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 synchronous mod
11783 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 e, this can only
11784 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20 happen . **
11785 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f if the page is o
11786 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
11787 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
11788 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c the transaction,
11789 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 then. ** pop
1178a 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76 ulated, then mov
1178b 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
1178c 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e PagerMovepage().
1178d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1178e 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 he solution is t
1178f 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f o add an in-memo
11790 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 ry page to the c
11791 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a ache containing.
11792 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20 ** the data
11793 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74 just read from t
11794 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 he sub-journal.
11795 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 Mark the page as
11796 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61 dirty . ** a
11797 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 nd if the pager
11798 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e requires a journ
11799 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61 al-sync, then ma
1179a 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a rk the page as .
1179b 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 ** requiring
1179c 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 a journal-sync
1179d 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69 before it is wri
1179e 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tten.. */.
1179f 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 assert( isSavep
117a0 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72 nt );. if( (r
117a1 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
117a2 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 Acquire(pPager,
117a3 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 21 pgno, &pPg, 1))!
117a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
117a5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
117a6 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 6c }. pPg->fl
117a7 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 ags &= ~PGHDR_NE
117a8 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 6c ED_READ;. sql
117a9 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 ite3PcacheMakeDi
117aa 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 rty(pPg);. }.
117ab 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f if( pPg ){. /
117ac 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 * No page should
117ad 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 69 ever be explici
117ae 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 tly rolled back
117af 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20 that is in use,
117b0 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f except. ** fo
117b1 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20 69 r page 1 which i
117b2 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e s held in use in
117b3 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 order to keep t
117b4 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 he lock on the.
117b5 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61 ** database a
117b6 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73 ctive. However s
117b7 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 62 uch a page may b
117b8 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 e rolled back as
117b9 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a a result. **
117ba 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 of an internal
117bb 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 error resulting
117bc 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 in an automatic
117bd 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 call to. ** s
117be 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
117bf 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 ack().. */.
117c0 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 void *pData;.
117c1 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d 3e pData = pPg->
117c2 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 pData;. memcp
117c3 79 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c 20 y(pData, aData,
117c4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
117c5 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
117c6 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 29 7b 0a r->xReiniter ){.
117c7 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 78 52 pPager->xR
117c8 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a 20 20 einiter(pPg);.
117c9 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4d 61 }. if( isMa
117ca 69 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 inJrnl && (!isSa
117cb 76 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 vepnt || *pOffse
117cc 74 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e t<=pPager->journ
117cd 61 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 alHdr) ){.
117ce 2f 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e /* If the conten
117cf 74 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 ts of this page
117d0 77 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 were just restor
117d1 65 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ed from the main
117d2 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e . ** journ
117d3 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 al file, then it
117d4 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 s content must b
117d5 65 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77 e as they were w
117d6 68 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a hen the . *
117d7 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 * transaction wa
117d8 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 s first opened.
117d9 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 In this case we
117da 63 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 can mark the pag
117db 65 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c e. ** as cl
117dc 65 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 ean, since there
117dd 20 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 will be no need
117de 20 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 to write it out
117df 20 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a to the.. *
117e0 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 *. ** There
117e1 20 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f is one exceptio
117e2 6e 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 n to this rule.
117e3 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 If the page is b
117e4 65 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 eing rolled.
117e5 20 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 ** back as par
117e6 74 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 t of a savepoint
117e7 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 (or statement)
117e8 72 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e rollback from an
117e9 20 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e . ** unsyn
117ea 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 ced portion of t
117eb 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
117ec 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 file, then it is
117ed 20 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 not safe.
117ee 2a 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 ** to mark the p
117ef 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 age as clean. Th
117f0 69 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 is is because ma
117f1 72 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 rking the page a
117f2 73 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e s. ** clean
117f3 20 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 will clear the
117f4 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
117f5 66 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 flag. Since the
117f6 70 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a page is. **
117f7 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
117f8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 journal file (re
117f9 63 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e corded in Pager.
117fa 70 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a pInJournal) and.
117fb 20 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 ** the PGH
117fc 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
117fd 67 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 g is cleared, if
117fe 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 the page is wri
117ff 74 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a tten to. **
11800 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 again within th
11801 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 is transaction,
11802 69 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 it will be marke
11803 64 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 d as dirty but.
11804 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 ** the PGHD
11805 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
11806 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 will not be set
11807 2e 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 . It could then
11808 70 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 potentially.
11809 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 ** be written
1180a 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 out into the dat
1180b 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
1180c 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 e its journal fi
1180d 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d le. ** segm
1180e 65 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 ent is synced. I
1180f 66 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 f a crash occurs
11810 20 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f during or follo
11811 77 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 wing this,.
11812 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 ** database cor
11813 72 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 ruption may ensu
11814 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
11815 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
11816 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 akeClean(pPg);.
11817 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
11818 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 TE_CHECK_PAGES.
11819 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 pPg->pageHash
1181a 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
1181b 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 h(pPg);.#endif.
1181c 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 /* If this wa
1181d 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 s page 1, then r
1181e 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 estore the value
1181f 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 of Pager.dbFile
11820 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 Vers.. ** Do
11821 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 this before any
11822 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 decoding. */.
11823 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a if( pgno==1 ){.
11824 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 memcpy(&pP
11825 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
11826 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b , &((u8*)pData)[
11827 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 24],sizeof(pPage
11828 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b r->dbFileVers));
11829 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 . }.. /* D
1182a 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a ecode the page j
1182b 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 ust read from di
1182c 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 sk */. CODEC1
1182d 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 (pPager, pData,
1182e 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 pPg->pgno, 3);.
1182f 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
11830 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 Release(pPg);.
11831 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
11832 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e ..#if !defined(N
11833 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 DEBUG) || define
11834 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 d(SQLITE_COVERAG
11835 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 E_TEST)./*.** Th
11836 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 is routine looks
11837 20 61 68 65 61 64 20 69 6e 74 6f 20 74 68 65 20 ahead into the
11838 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c main journal fil
11839 65 20 61 6e 64 20 64 65 74 65 72 6d 69 6e 65 73 e and determines
1183a 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e .** whether or n
1183b 6f 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f ot the next reco
1183c 72 64 20 28 74 68 65 20 72 65 63 6f 72 64 20 74 rd (the record t
1183d 68 61 74 20 62 65 67 69 6e 73 20 61 74 20 66 69 hat begins at fi
1183e 6c 65 0a 2a 2a 20 6f 66 66 73 65 74 20 70 50 61 le.** offset pPa
1183f 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 ger->journalOff)
11840 20 69 73 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 is a well-forme
11841 64 20 70 61 67 65 20 72 65 63 6f 72 64 20 63 6f d page record co
11842 6e 73 69 73 74 69 6e 67 0a 2a 2a 20 6f 66 20 61 nsisting.** of a
11843 20 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 valid page numb
11844 65 72 2c 20 70 50 61 67 65 2d 3e 70 61 67 65 53 er, pPage->pageS
11845 69 7a 65 20 62 79 74 65 73 20 6f 66 20 63 6f 6e ize bytes of con
11846 74 65 6e 74 2c 20 66 6f 6c 6c 6f 77 65 64 0a 2a tent, followed.*
11847 2a 20 62 79 20 61 20 76 61 6c 69 64 20 63 68 65 * by a valid che
11848 63 6b 73 75 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cksum..**.** The
11849 20 70 61 67 65 72 20 6e 65 76 65 72 20 6e 65 65 pager never nee
1184a 64 73 20 74 6f 20 6b 6e 6f 77 20 74 68 69 73 20 ds to know this
1184b 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 20 69 in order to do i
1184c 74 73 20 6a 6f 62 2e 20 20 20 54 68 69 73 0a 2a ts job. This.*
1184d 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c * routine is onl
1184e 79 20 75 73 65 64 20 66 72 6f 6d 20 77 69 74 68 y used from with
1184f 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 74 65 assert() and te
11850 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 73 2e stcase() macros.
11851 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
11852 61 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 agerNextJournalP
11853 61 67 65 49 73 56 61 6c 69 64 28 50 61 67 65 72 ageIsValid(Pager
11854 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67 6e *pPager){. Pgn
11855 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 o pgno;
11856 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75 /* The page nu
11857 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 mber of the page
11858 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b */. u32 cksum;
11859 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1185a 65 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 e page checksum
1185b 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
1185c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
1185d 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 72 65 urn code from re
1185e 61 64 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f ad operations */
1185f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
11860 2a 66 64 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 *fd; /* The f
11861 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 ile descriptor f
11862 72 6f 6d 20 77 68 69 63 68 20 77 65 20 61 72 65 rom which we are
11863 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 75 38 reading */. u8
11864 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 *aData;
11865 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 /* Content of
11866 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 the page */..
11867 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 /* Read the page
11868 20 6e 75 6d 62 65 72 20 68 65 61 64 65 72 20 2a number header *
11869 2f 0a 20 20 66 64 20 3d 20 70 50 61 67 65 72 2d /. fd = pPager-
1186a 3e 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 >jfd;. rc = rea
1186b 64 33 32 62 69 74 73 28 66 64 2c 20 70 50 61 67 d32bits(fd, pPag
1186c 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 er->journalOff,
1186d 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 &pgno);. if( rc
1186e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72 !=SQLITE_OK ){ r
1186f 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 eturn 0; }
11870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11871 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f /*NO
11872 5f 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 _TEST*/. if( pg
11873 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 no==0 || pgno==P
11874 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
11875 67 65 72 29 20 29 7b 20 72 65 74 75 72 6e 20 30 ger) ){ return 0
11876 3b 20 7d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f ; } /*NO
11877 5f 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67 _TEST*/. if( pg
11878 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d no>(Pgno)pPager-
11879 3e 64 62 53 69 7a 65 20 29 7b 20 72 65 74 75 72 >dbSize ){ retur
1187a 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 n 0; }
1187b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f /*NO
1187c 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65 _TEST*/.. /* Re
1187d 61 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 ad the checksum
1187e 2a 2f 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 */. rc = read32
1187f 62 69 74 73 28 66 64 2c 20 70 50 61 67 65 72 2d bits(fd, pPager-
11880 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 70 50 61 67 >journalOff+pPag
11881 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20 er->pageSize+4,
11882 26 63 6b 73 75 6d 29 3b 0a 20 20 69 66 28 20 72 &cksum);. if( r
11883 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 c!=SQLITE_OK ){
11884 72 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 return 0; }
11885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11886 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e /*N
11887 4f 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 O_TEST*/.. /* R
11888 65 61 64 20 74 68 65 20 64 61 74 61 20 61 6e 64 ead the data and
11889 20 76 65 72 69 66 79 20 74 68 65 20 63 68 65 63 verify the chec
1188a 6b 73 75 6d 20 2a 2f 0a 20 20 61 44 61 74 61 20 ksum */. aData
1188b 3d 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 = (u8*)pPager->p
1188c 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d TmpSpace;. rc =
1188d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 sqlite3OsRead(f
1188e 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 d, aData, pPager
1188f 2d 3e 70 61 67 65 53 69 7a 65 2c 20 70 50 61 67 ->pageSize, pPag
11890 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 34 er->journalOff+4
11891 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
11892 49 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e ITE_OK ){ return
11893 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 0; }
11894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11895 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 /*NO_TEST
11896 2a 2f 0a 20 20 69 66 28 20 70 61 67 65 72 5f 63 */. if( pager_c
11897 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 ksum(pPager, aDa
11898 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 20 72 65 ta)!=cksum ){ re
11899 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 turn 0; }
1189a 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 /*NO_TEST
1189b 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 63 68 20 74 */.. /* Reach t
1189c 68 69 73 20 70 6f 69 6e 74 20 6f 6e 6c 79 20 69 his point only i
1189d 66 20 74 68 65 20 70 61 67 65 20 69 73 20 76 61 f the page is va
1189e 6c 69 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 lid */. return
1189f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 1;.}.#endif /* !
118a0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 defined(NDEBUG)
118a1 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
118a2 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 E_COVERAGE_TEST)
118a3 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d */../*.** Param
118a4 65 74 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 eter zMaster is
118a5 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 the name of a ma
118a6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
118a7 65 2e 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 e. A single jour
118a8 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 nal.** file that
118a9 20 72 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 referred to the
118aa 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
118ab 66 69 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 file has just be
118ac 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a en rolled back..
118ad 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
118ae 63 68 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 checks if it is
118af 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 possible to dele
118b0 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
118b1 75 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 urnal file,.** a
118b2 6e 64 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 nd does so if it
118b3 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d is..**.** Argum
118b4 65 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 ent zMaster may
118b5 70 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 point to Pager.p
118b6 54 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 TmpSpace. So tha
118b7 74 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 t buffer is not
118b8 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f .** available fo
118b9 72 20 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 r use within thi
118ba 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a s function..**.*
118bb 2a 20 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20 * When a master
118bc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
118bd 63 72 65 61 74 65 64 2c 20 69 74 20 69 73 20 70 created, it is p
118be 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 opulated with th
118bf 65 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 e names .** of a
118c0 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 ll of its child
118c1 6a 6f 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 journals, one af
118c2 74 65 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72 ter another, for
118c3 6d 61 74 74 65 64 20 61 73 20 75 74 66 2d 38 20 matted as utf-8
118c4 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74 .** encoded text
118c5 2e 20 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63 . The end of eac
118c6 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 h child journal
118c7 66 69 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77 file is marked w
118c8 69 74 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 ith a .** nul-te
118c9 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30 rminator byte (0
118ca 78 30 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65 x00). i.e. the e
118cb 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f ntire contents o
118cc 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e f a master journ
118cd 61 6c 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61 al.** file for a
118ce 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76 transaction inv
118cf 6f 6c 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62 olving two datab
118d0 61 73 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a ases might be:.*
118d1 2a 0a 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69 *.** "/home/bi
118d2 6c 6c 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c ll/a.db-journal\
118d3 78 30 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e x00/home/bill/b.
118d4 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a db-journal\x00".
118d5 2a 2a 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a **.** A master j
118d6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 ournal file may
118d7 6f 6e 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 only be deleted
118d8 6f 6e 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 once all of its
118d9 63 68 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 child .** journa
118da 6c 73 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c ls have been rol
118db 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 led back..**.**
118dc 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
118dd 61 64 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ads the contents
118de 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a of the master-j
118df 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f ournal file into
118e0 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 .** memory and
118e1 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 loops through ea
118e2 63 68 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 ch of the child
118e3 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 journal names. F
118e4 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 or.** each child
118e5 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65 journal, it che
118e6 63 6b 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 cks if:.**.**
118e7 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a * if the child j
118e8 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61 ournal exists, a
118e9 6e 64 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 nd if so.** *
118ea 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 if the child jou
118eb 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 rnal contains a
118ec 72 65 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73 reference to mas
118ed 74 65 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 ter journal .**
118ee 20 20 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 file zMaster
118ef 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c .**.** If a chil
118f0 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 d journal can be
118f1 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63 found that matc
118f2 68 65 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20 hes both of the
118f3 63 72 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76 criteria.** abov
118f4 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e e, this function
118f5 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 returns without
118f6 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e doing anything.
118f7 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a Otherwise, if.*
118f8 2a 20 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20 * no such child
118f9 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 journal can be f
118fa 6f 75 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74 ound, file zMast
118fb 65 72 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 er is deleted fr
118fc 6f 6d 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 om.** the file-s
118fd 79 73 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69 ystem using sqli
118fe 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a te3OsDelete()..*
118ff 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 *.** If an IO er
11900 72 6f 72 20 77 69 74 68 69 6e 20 74 68 69 73 20 ror within this
11901 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72 function, an err
11902 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
11903 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e ned. This.** fun
11904 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 ction allocates
11905 6d 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e memory by callin
11906 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 g sqlite3Malloc(
11907 29 2e 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 ). If an allocat
11908 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 ion.** fails, SQ
11909 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 LITE_NOMEM is re
1190a 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 turned. Otherwis
1190b 65 2c 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d e, if no IO or m
1190c 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a alloc errors .**
1190d 20 6f 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f occur, SQLITE_O
1190e 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a K is returned..*
1190f 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 *.** TODO: This
11910 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 function allocat
11911 65 73 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63 es a single bloc
11912 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c k of memory to l
11913 6f 61 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 oad.** the entir
11914 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
11915 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11916 20 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c file. This coul
11917 64 20 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 d be.** a couple
11918 20 6f 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72 of kilobytes or
11919 20 73 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c so - potentiall
1191a 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 y larger than th
1191b 65 20 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e e page .** size.
1191c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
1191d 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 ager_delmaster(P
1191e 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f ager *pPager, co
1191f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nst char *zMaste
11920 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 r){. sqlite3_vf
11921 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 s *pVfs = pPager
11922 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 ->pVfs;. int rc
11923 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11924 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
11925 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f de */. sqlite3_
11926 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20 file *pMaster;
11927 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 /* Malloc'd ma
11928 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c ster-journal fil
11929 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a e descriptor */.
1192a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1192b 70 4a 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d pJournal; /* M
1192c 61 6c 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f alloc'd child-jo
1192d 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
1192e 69 70 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 iptor */. char
1192f 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 *zMasterJournal
11930 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 = 0; /* Contents
11931 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
11932 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 al file */. i64
11933 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b nMasterJournal;
11934 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
11935 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c f master journal
11936 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 file */.. /* A
11937 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
11938 72 20 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72 r both the pJour
11939 6e 61 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20 nal and pMaster
1193a 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
1193b 2e 0a 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73 .. ** If succes
1193c 73 66 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d sful, open the m
1193d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1193e 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a le for reading..
1193f 20 20 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d */. pMaster =
11940 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a (sqlite3_file *
11941 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 )sqlite3MallocZe
11942 72 6f 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c ro(pVfs->szOsFil
11943 65 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e e * 2);. pJourn
11944 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 al = (sqlite3_fi
11945 6c 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 le *)(((u8 *)pMa
11946 73 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a ster) + pVfs->sz
11947 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 OsFile);. if( !
11948 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 pMaster ){. r
11949 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1194a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 ;. }else{. c
1194b 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d onst int flags =
1194c 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 (SQLITE_OPEN_RE
1194d 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 ADONLY|SQLITE_OP
1194e 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 EN_MASTER_JOURNA
1194f 4c 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c L);. rc = sql
11950 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c ite3OsOpen(pVfs,
11951 20 7a 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65 zMaster, pMaste
11952 72 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 r, flags, 0);.
11953 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 }. if( rc!=SQLI
11954 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c TE_OK ) goto del
11955 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 master_out;.. r
11956 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
11957 65 53 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 eSize(pMaster, &
11958 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b nMasterJournal);
11959 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1195a 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d E_OK ) goto delm
1195b 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 aster_out;.. if
1195c 28 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c ( nMasterJournal
1195d 3e 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a >0 ){. char *
1195e 7a 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 zJournal;. ch
1195f 61 72 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d ar *zMasterPtr =
11960 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 0;. int nMas
11961 74 65 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d terPtr = pVfs->m
11962 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 xPathname+1;..
11963 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e /* Load the en
11964 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 tire master jour
11965 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 nal file into sp
11966 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ace obtained fro
11967 6d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 m. ** sqlite3
11968 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f _malloc() and po
11969 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 inted to by zMas
1196a 74 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 terJournal. .
1196b 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a */. zMasterJ
1196c 6f 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a ournal = (char *
1196d 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28 )sqlite3Malloc((
1196e 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e int)nMasterJourn
1196f 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 72 29 al + nMasterPtr)
11970 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 ;. if( !zMast
11971 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 erJournal ){.
11972 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
11973 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f OMEM;. goto
11974 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
11975 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65 }. zMaste
11976 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a rPtr = &zMasterJ
11977 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f ournal[nMasterJo
11978 75 72 6e 61 6c 5d 3b 0a 20 20 20 20 72 63 20 3d urnal];. rc =
11979 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
1197a 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72 4a Master, zMasterJ
1197b 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 ournal, (int)nMa
1197c 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b sterJournal, 0);
1197d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1197e 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 ITE_OK ) goto de
1197f 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 lmaster_out;..
11980 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61 zJournal = zMa
11981 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 sterJournal;.
11982 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61 while( (zJourna
11983 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c l-zMasterJournal
11984 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c )<nMasterJournal
11985 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 ){. int ex
11986 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d ists;. rc =
11987 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
11988 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c (pVfs, zJournal,
11989 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
1198a 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b XISTS, &exists);
1198b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1198c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1198d 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
1198e 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a er_out;. }.
1198f 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73 if( exists
11990 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f ){. /* O
11991 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ne of the journa
11992 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ls pointed to by
11993 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11994 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20 nal exists..
11995 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61 ** Open it a
11996 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70 nd check if it p
11997 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73 oints at the mas
11998 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a ter journal. If.
11999 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 ** so, r
1199a 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 eturn without de
1199b 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 leting the maste
1199c 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a r journal file..
1199d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1199e 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 int c;.
1199f 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 int flags = (S
119a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
119a1 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f NLY|SQLITE_OPEN_
119a2 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 MAIN_JOURNAL);.
119a3 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
119a4 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
119a5 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e zJournal, pJourn
119a6 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 al, flags, 0);.
119a7 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
119a8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
119a9 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
119aa 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
119ab 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20 }.. rc
119ac 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
119ad 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d nal(pJournal, zM
119ae 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 asterPtr, nMaste
119af 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73 rPtr);. s
119b0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a qlite3OsClose(pJ
119b1 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20 ournal);.
119b2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
119b3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
119b4 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f goto delmaster_o
119b5 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ut;. }..
119b6 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 c = zMast
119b7 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73 erPtr[0]!=0 && s
119b8 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72 trcmp(zMasterPtr
119b9 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20 , zMaster)==0;.
119ba 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a if( c ){.
119bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 /* We
119bc 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f have a match. Do
119bd 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 not delete the
119be 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
119bf 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ile. */.
119c0 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
119c1 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a _out;. }.
119c2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a }. zJ
119c3 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74 ournal += (sqlit
119c4 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72 e3Strlen30(zJour
119c5 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 nal)+1);. }.
119c6 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c }. . rc = sql
119c7 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 ite3OsDelete(pVf
119c8 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a s, zMaster, 0);.
119c9 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a .delmaster_out:.
119ca 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75 if( zMasterJou
119cb 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 rnal ){. sqli
119cc 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 te3_free(zMaster
119cd 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a Journal);. } .
119ce 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b if( pMaster ){
119cf 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
119d0 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ose(pMaster);.
119d1 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 assert( !isOpe
119d2 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 n(pJournal) );.
119d3 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
119d4 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 e(pMaster);. re
119d5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
119d6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
119d7 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e is used to chan
119d8 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 ge the actual si
119d9 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
119da 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 se .** file in t
119db 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 he file-system.
119dc 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e This only happen
119dd 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e s when committin
119de 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c g a transaction,
119df 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 .** or rolling b
119e0 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f ack a transactio
119e1 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c n (including rol
119e2 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d ling back a hot-
119e3 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 journal)..**.**
119e4 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 If the main data
119e5 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 base file is not
119e6 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63 open, or an exc
119e7 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e lusive lock is n
119e8 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73 ot.** held, this
119e9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
119ea 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c o-op. Otherwise,
119eb 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
119ec 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e file is.** chan
119ed 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67 ged to nPage pag
119ee 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72 es (nPage*pPager
119ef 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 ->pageSize bytes
119f0 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a ). If the file.*
119f1 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72 * on disk is cur
119f2 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 rently larger th
119f3 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20 an nPage pages,
119f4 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53 then use the VFS
119f5 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20 .** xTruncate()
119f6 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61 method to trunca
119f7 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c te it..**.** Or,
119f8 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20 it might might
119f9 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
119fa 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
119fb 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 k is smaller tha
119fc 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65 n .** nPage page
119fd 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e s. Some operatin
119fe 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 g system impleme
119ff 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 ntations can get
11a00 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a confused if .**
11a01 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e you try to trun
11a02 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73 cate a file to s
11a03 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73 ome size that is
11a04 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20 larger than it
11a05 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73 .** currently is
11a06 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73 , so detect this
11a07 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20 case and write
11a08 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79 a single zero by
11a09 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e te to .** the en
11a0a 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c d of the new fil
11a0b 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a e instead..**.**
11a0c 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
11a0d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
11a0e 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 . If an IO error
11a0f 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f occurs while mo
11a10 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64 difying.** the d
11a11 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65 atabase file, re
11a12 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 turn the error c
11a13 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ode to the calle
11a14 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
11a15 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 pager_truncate(
11a16 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
11a17 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e gno nPage){. in
11a18 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
11a19 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
11a1a 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
11a1b 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e LUSIVE && isOpen
11a1c 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a (pPager->fd) ){.
11a1d 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 i64 currentS
11a1e 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 ize, newSize;.
11a1f 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 /* TODO: Is it
11a20 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67 safe to use Pag
11a21 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65 er.dbFileSize he
11a22 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 re? */. rc =
11a23 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
11a24 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63 e(pPager->fd, &c
11a25 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 urrentSize);.
11a26 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65 newSize = pPage
11a27 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34 r->pageSize*(i64
11a28 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20 )nPage;. if(
11a29 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
11a2a 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65 currentSize!=ne
11a2b 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69 wSize ){. i
11a2c 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e f( currentSize>n
11a2d 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 ewSize ){.
11a2e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11a2f 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
11a30 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20 >fd, newSize);.
11a31 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
11a32 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
11a33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
11a34 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69 fd, "", 1, newSi
11a35 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ze-1);. }.
11a36 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
11a37 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
11a38 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 pPager->dbFile
11a39 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Size = nPage;.
11a3a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
11a3b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11a3c 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 /*.** Set the va
11a3d 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72 lue of the Pager
11a3e 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69 .sectorSize vari
11a3f 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76 able for the giv
11a40 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65 en.** pager base
11a41 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72 d on the value r
11a42 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 eturned by the x
11a43 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f SectorSize metho
11a44 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e d.** of the open
11a45 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
11a46 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 The sector size
11a47 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65 will be used use
11a48 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 d .** to determi
11a49 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20 ne the size and
11a4a 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 alignment of jou
11a4b 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20 rnal header and
11a4c 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e .** master journ
11a4d 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 al pointers with
11a4e 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e in created journ
11a4f 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 al files..**.**
11a50 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 For temporary fi
11a51 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76 les the effectiv
11a52 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 e sector size is
11a53 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
11a54 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 s..**.** Otherwi
11a55 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 se, for non-temp
11a56 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65 orary files, the
11a57 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f effective secto
11a58 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 r size is.** the
11a59 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
11a5a 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69 by the xSectorSi
11a5b 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e ze() method roun
11a5c 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66 ded up to 512 if
11a5d 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74 .** it is less t
11a5e 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e han 512, or roun
11a5f 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f ded down to MAX_
11a60 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69 SECTOR_SIZE if i
11a61 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 t.** is greater
11a62 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f than MAX_SECTOR_
11a63 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 SIZE..*/.static
11a64 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 void setSectorSi
11a65 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
11a66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f ){. assert( isO
11a67 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
11a68 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
11a69 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 ile );.. if( !p
11a6a 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
11a6b 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 ){. /* Sector
11a6c 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 size doesn't ma
11a6d 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 tter for tempora
11a6e 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 ry files. Also,
11a6f 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 the file. **
11a70 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 may not have bee
11a71 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e n opened yet, in
11a72 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 which case the
11a73 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 OsSectorSize().
11a74 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 ** call will
11a75 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f segfault.. */
11a76 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
11a77 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 torSize = sqlite
11a78 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 3OsSectorSize(pP
11a79 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 ager->fd);. }.
11a7a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 if( pPager->sec
11a7b 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20 torSize<512 ){.
11a7c 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f pPager->secto
11a7d 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d rSize = 512;. }
11a7e 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
11a7f 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 ectorSize>MAX_SE
11a80 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20 CTOR_SIZE ){.
11a81 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43 assert( MAX_SEC
11a82 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b TOR_SIZE>=512 );
11a83 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
11a84 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45 torSize = MAX_SE
11a85 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d CTOR_SIZE;. }.}
11a86 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b ../*.** Playback
11a87 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 the journal and
11a88 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68 thus restore th
11a89 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
11a8a 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 to.** the state
11a8b 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65 it was in before
11a8c 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69 we started maki
11a8d 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a ng changes. .**
11a8e 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 .** The journal
11a8f 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61 file format is a
11a90 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a s follows: .**.*
11a91 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70 * (1) 8 byte p
11a92 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f refix. A copy o
11a93 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b f aJournalMagic[
11a94 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79 ]..** (2) 4 by
11a95 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
11a96 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
11a97 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c he number of val
11a98 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a id page records.
11a99 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20 ** in the
11a9a 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69 journal. If thi
11a9b 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66 s value is 0xfff
11a9c 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70 fffff, then comp
11a9d 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 ute the.**
11a9e 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 number of page
11a9f 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 records from the
11aa0 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a journal size..*
11aa1 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62 * (3) 4 byte b
11aa2 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
11aa3 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 r which is the i
11aa4 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 nitial value for
11aa5 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73 the .** s
11aa6 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a anity checksum..
11aa7 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20 ** (4) 4 byte
11aa8 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
11aa9 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
11aaa 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65 ages to truncate
11aab 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 the.** da
11aac 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67 tabase to during
11aad 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 a rollback..**
11aae 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67 (5) 4 byte big
11aaf 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
11ab0 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63 which is the sec
11ab1 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68 tor size. The h
11ab2 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69 eader.** i
11ab3 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 s this many byte
11ab4 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 s in size..** (
11ab5 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6) 4 byte big-e
11ab6 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 ndian integer wh
11ab7 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20 ich is the page
11ab8 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34 case..** (7) 4
11ab9 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 byte integer wh
11aba 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ich is the numbe
11abb 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
11abc 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11abd 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20 .** name.
11abe 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62 The value may b
11abf 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65 e zero (indicate
11ac0 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e that there is n
11ac1 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 o master.**
11ac2 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20 journal.).**
11ac3 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20 (8) N bytes of
11ac4 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
11ac5 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61 al name. The na
11ac6 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74 me will be nul-t
11ac7 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 erminated.**
11ac8 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20 and might be
11ac9 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65 shorter than the
11aca 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d value read from
11acb 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69 (5). If the fi
11acc 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 rst byte.**
11acd 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 of the name is
11ace 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65 \000 then there
11acf 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f is no master jo
11ad0 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74 urnal. The mast
11ad1 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 er.** jour
11ad2 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72 nal name is stor
11ad3 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20 ed in UTF-8..**
11ad4 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f (9) Zero or mo
11ad5 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63 re pages instanc
11ad6 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c es, each as foll
11ad7 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b ows:.** +
11ad8 20 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75 4 byte page nu
11ad9 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 mber..**
11ada 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 + pPager->pageS
11adb 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 ize bytes of dat
11adc 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 a..** +
11add 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 4 byte checksum.
11ade 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 **.** When we sp
11adf 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e eak of the journ
11ae0 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65 al header, we me
11ae1 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69 an the first 8 i
11ae2 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 tems above..** E
11ae3 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ach entry in the
11ae4 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 journal is an i
11ae5 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39 nstance of the 9
11ae6 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 th item..**.** C
11ae7 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72 all the value fr
11ae8 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 om the second bu
11ae9 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 llet "nRec". nR
11aea 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ec is the number
11aeb 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 of.** valid pag
11aec 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 e entries in the
11aed 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f journal. In mo
11aee 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61 st cases, you ca
11aef 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a n compute the.**
11af0 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 value of nRec f
11af1 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 rom the size of
11af2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11af3 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65 . But if a powe
11af4 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 r.** failure occ
11af5 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20 urred while the
11af6 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e journal was bein
11af7 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f g written, it co
11af8 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 uld be the.** ca
11af9 73 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 se that the size
11afa 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11afb 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79 file had already
11afc 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20 been increased
11afd 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 but.** the extra
11afe 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74 entries had not
11aff 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66 yet made it saf
11b00 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e ely to disk. In
11b01 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a such a case,.**
11b02 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 the value of nR
11b03 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d ec computed from
11b04 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77 the file size w
11b05 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 ould be too larg
11b06 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 e. For.** that
11b07 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 reason, we alway
11b08 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76 s use the nRec v
11b09 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64 alue in the head
11b0a 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 er..**.** If the
11b0b 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30 nRec value is 0
11b0c 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61 xffffffff it mea
11b0d 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f ns that nRec sho
11b0e 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a uld be computed.
11b0f 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 ** from the file
11b10 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c size. This val
11b11 75 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 ue is used when
11b12 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73 the user selects
11b13 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 the.** no-sync
11b14 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a option for the j
11b15 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 ournal. A power
11b16 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c failure could l
11b17 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f ead to corruptio
11b18 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 n.** in this cas
11b19 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e e. But for thin
11b1a 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 gs like temporar
11b1b 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77 y table (which w
11b1c 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 ill be.** delete
11b1d 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72 d when the power
11b1e 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65 is restored) we
11b1f 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a don't care. .*
11b20 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 *.** If the file
11b21 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a opened as the j
11b22 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
11b23 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 ot a well-formed
11b24 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
11b25 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 then all pages
11b26 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 up to the first
11b27 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61 corrupted page a
11b28 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 re rolled.** bac
11b29 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 k (or no pages i
11b2a 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 f the journal he
11b2b 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65 ader is corrupte
11b2c 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 d). The journal
11b2d 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 file.** is then
11b2e 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49 deleted and SQLI
11b2f 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 TE_OK returned,
11b30 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f just as if no co
11b31 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 rruption had.**
11b32 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 been encountered
11b33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f ..**.** If an I/
11b34 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 O or malloc() er
11b35 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 ror occurs, the
11b36 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 journal-file is
11b37 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 not deleted.** a
11b38 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 nd an error code
11b39 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
11b3a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61 .** The isHot pa
11b3b 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 rameter indicate
11b3c 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72 s that we are tr
11b3d 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b ying to rollback
11b3e 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 a journal.** th
11b3f 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f at might be a ho
11b40 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 t journal. Or,
11b41 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 it could be that
11b42 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
11b43 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65 .** preserved be
11b44 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c cause of JOURNAL
11b45 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20 MODE_PERSIST or
11b46 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e JOURNALMODE_TRUN
11b47 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 CATE..** If the
11b48 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 journal really i
11b49 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65 s hot, reset the
11b4a 20 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69 pager cache pri
11b4b 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 or rolling.** ba
11b4c 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 ck any content.
11b4d 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
11b4e 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73 is merely persis
11b4f 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 tent, no reset i
11b50 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a s.** needed..*/.
11b51 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
11b52 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20 _playback(Pager
11b53 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48 *pPager, int isH
11b54 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 ot){. sqlite3_v
11b55 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 fs *pVfs = pPage
11b56 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73 r->pVfs;. i64 s
11b57 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 zJ;
11b58 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
11b59 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11b5a 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 in bytes */. u3
11b5b 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 2 nRec;
11b5c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
11b5d 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74 of Records in t
11b5e 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 he journal */.
11b5f 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 u32 u;
11b60 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 /* Unsi
11b61 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 gned loop counte
11b62 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 r */. Pgno mxPg
11b63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
11b64 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f /* Size of the o
11b65 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 riginal file in
11b66 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 pages */. int r
11b67 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
11b68 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f /* Result co
11b69 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 de of a subrouti
11b6a 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 ne */. int res
11b6b 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 1;
11b6c 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e /* Value return
11b6d 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41 ed by sqlite3OsA
11b6e 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 ccess() */. cha
11b6f 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 r *zMaster = 0;
11b70 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
11b71 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11b72 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 file if any */.
11b73 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65 int needPagerRe
11b74 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 set; /* Tru
11b75 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20 e to reset page
11b76 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70 prior to first p
11b77 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a age rollback */.
11b78 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 . /* Figure out
11b79 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 how many record
11b7a 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 s are in the jou
11b7b 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 rnal. Abort ear
11b7c 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a ly if. ** the j
11b7d 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e ournal is empty.
11b7e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
11b7f 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
11b80 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 fd) );. rc = sq
11b81 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
11b82 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a pPager->jfd, &sz
11b83 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 J);. if( rc!=SQ
11b84 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d LITE_OK || szJ==
11b85 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 0 ){. goto en
11b86 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a d_playback;. }.
11b87 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d . /* Read the m
11b88 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
11b89 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 me from the jour
11b8a 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72 nal, if it is pr
11b8b 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61 esent.. ** If a
11b8c 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11b8d 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65 file name is spe
11b8e 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20 cified, but the
11b8f 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a file is not. **
11b90 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b present on disk
11b91 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
11b92 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e al is not hot an
11b93 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 d does not need
11b94 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65 to be. ** playe
11b95 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a d back.. **. *
11b96 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61 * TODO: Technica
11b97 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e lly the followin
11b98 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65 g is an error be
11b99 63 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73 cause it assumes
11b9a 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65 that. ** buffe
11b9b 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 r Pager.pTmpSpac
11b9c 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65 e is (mxPathname
11b9d 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72 +1) bytes or lar
11b9e 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20 ger. i.e. that.
11b9f 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67 ** (pPager->pag
11ba0 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d eSize >= pPager-
11ba1 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d >pVfs->mxPathnam
11ba2 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75 e+1). Using os_u
11ba3 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50 nix.c,. ** mxP
11ba4 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20 athname is 512,
11ba5 77 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d which is the sam
11ba6 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d e as the minimum
11ba7 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65 allowable value
11ba8 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69 . ** for pageSi
11ba9 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 ze.. */. zMast
11baa 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d er = pPager->pTm
11bab 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 pSpace;. rc = r
11bac 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c eadMasterJournal
11bad 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d (pPager->jfd, zM
11bae 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 aster, pPager->p
11baf 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
11bb0 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 1);. if( rc==SQ
11bb1 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 LITE_OK && zMast
11bb2 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20 er[0] ){. rc
11bb3 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 = sqlite3OsAcces
11bb4 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c s(pVfs, zMaster,
11bb5 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
11bb6 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 XISTS, &res);.
11bb7 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b }. zMaster = 0;
11bb8 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
11bb9 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a E_OK || !res ){.
11bba 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
11bbb 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61 yback;. }. pPa
11bbc 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
11bbd 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72 = 0;. needPager
11bbe 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a Reset = isHot;..
11bbf 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 /* This loop t
11bc0 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72 erminates either
11bc1 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72 when a readJour
11bc2 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a nalHdr() or . *
11bc3 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b * pager_playback
11bc4 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c _one_page() call
11bc5 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
11bc6 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 DONE or an IO er
11bc7 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73 ror . ** occurs
11bc8 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 . . */. while(
11bc9 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 1 ){. int is
11bca 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 Unsync = 0;..
11bcb 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78 /* Read the nex
11bcc 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
11bcd 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
11bce 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 l file. If ther
11bcf 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 e are. ** not
11bd0 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65 enough bytes le
11bd1 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ft in the journa
11bd2 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d l file for a com
11bd3 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72 plete header, or
11bd4 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f . ** it is co
11bd5 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20 rrupted, then a
11bd6 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20 process must of
11bd7 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69 failed while wri
11bd8 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 ting it.. **
11bd9 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e This indicates n
11bda 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 othing more need
11bdb 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 s to be rolled b
11bdc 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ack.. */.
11bdd 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c rc = readJournal
11bde 48 64 72 28 70 50 61 67 65 72 2c 20 73 7a 4a 2c Hdr(pPager, szJ,
11bdf 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a &nRec, &mxPg);.
11be0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
11be1 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20 TE_OK ){ .
11be2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 if( rc==SQLITE_D
11be3 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ONE ){. r
11be4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
11be5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
11be6 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
11be7 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
11be8 20 6e 52 65 63 20 69 73 20 30 78 66 66 66 66 66 nRec is 0xfffff
11be9 66 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a fff, then this j
11bea 6f 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74 ournal was creat
11beb 65 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a ed by a process.
11bec 20 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69 ** working i
11bed 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 n no-sync mode.
11bee 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 This means that
11bef 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 the rest of the
11bf0 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 journal. ** f
11bf1 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 ile consists of
11bf2 70 61 67 65 73 2c 20 74 68 65 72 65 20 61 72 65 pages, there are
11bf3 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c no more journal
11bf4 20 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74 headers. Comput
11bf5 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c e. ** the val
11bf6 75 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64 ue of nRec based
11bf7 20 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74 on this assumpt
11bf8 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
11bf9 69 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66 if( nRec==0xffff
11bfa 66 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73 ffff ){. as
11bfb 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f sert( pPager->jo
11bfc 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 urnalOff==JOURNA
11bfd 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
11bfe 20 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d );. nRec =
11bff 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f (int)((szJ - JO
11c00 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11c01 67 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 ger))/JOURNAL_PG
11c02 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 _SZ(pPager));.
11c03 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e }.. /* If n
11c04 52 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 Rec is 0 and thi
11c05 73 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 s rollback is of
11c06 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 a transaction c
11c07 72 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 reated by this.
11c08 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e ** process an
11c09 64 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 d if this is the
11c0a 20 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e final header in
11c0b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 the journal, th
11c0c 65 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 en it means.
11c0d 2a 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72 ** that this par
11c0e 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
11c0f 20 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 was being fille
11c10 64 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 d but has not ye
11c11 74 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 t been. ** sy
11c12 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 nced to disk. C
11c13 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 ompute the numbe
11c14 72 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 r of pages based
11c15 20 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e on the remainin
11c16 67 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 g. ** size of
11c17 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a the file.. *
11c18 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 *. ** The thi
11c19 72 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 rd term of the t
11c1a 65 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f est was added to
11c1b 20 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 fix ticket #256
11c1c 35 2e 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72 5.. ** When r
11c1d 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f olling back a ho
11c1e 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d t journal, nRec=
11c1f 3d 30 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20 =0 always means
11c20 74 68 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20 that the next.
11c21 20 20 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68 ** chunk of th
11c22 65 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 e journal contai
11c23 6e 73 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f ns zero pages to
11c24 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
11c25 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65 But. ** whe
11c26 6e 20 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41 n doing a ROLLBA
11c27 43 4b 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d CK and the nRec=
11c28 3d 30 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20 =0 chunk is the
11c29 6c 61 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20 last chunk in.
11c2a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
11c2b 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
11c2c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 the journal migh
11c2d 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 t contain additi
11c2e 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 onal. ** page
11c2f 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 s that need to b
11c30 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e e rolled back an
11c31 64 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 d that the numbe
11c32 72 20 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20 r of pages .
11c33 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d ** should be com
11c34 70 75 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 puted based on t
11c35 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11c36 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 size.. */.
11c37 20 74 65 73 74 63 61 73 65 28 20 6e 52 65 63 3d testcase( nRec=
11c38 3d 30 20 26 26 20 21 69 73 48 6f 74 0a 20 20 20 =0 && !isHot.
11c39 20 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d && pPager-
11c3a 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 >journalHdr+JOUR
11c3b 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
11c3c 72 29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r)!=pPager->jour
11c3d 6e 61 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20 nalOff.
11c3e 26 26 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 && ((szJ - pPage
11c3f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f r->journalOff) /
11c40 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 JOURNAL_PG_SZ(p
11c41 50 61 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20 Pager))>0.
11c42 20 20 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a && pagerNextJ
11c43 6f 75 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69 ournalPageIsVali
11c44 64 28 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b d(pPager). );
11c45 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 . if( nRec==0
11c46 20 26 26 20 21 69 73 48 6f 74 20 26 26 0a 20 20 && !isHot &&.
11c47 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
11c48 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c urnalHdr+JOURNAL
11c49 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d _HDR_SZ(pPager)=
11c4a 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c =pPager->journal
11c4b 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 6e 52 65 Off ){. nRe
11c4c 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d c = (int)((szJ -
11c4d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11c4e 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 Off) / JOURNAL_P
11c4f 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 G_SZ(pPager));.
11c50 20 20 20 20 20 69 73 55 6e 73 79 6e 63 20 3d 20 isUnsync =
11c51 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 1;. }.. /*
11c52 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
11c53 66 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61 first header rea
11c54 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e d from the journ
11c55 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65 al, truncate the
11c56 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
11c57 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 file back to it
11c58 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e s original size.
11c59 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
11c5a 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11c5b 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ff==JOURNAL_HDR_
11c5c 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 SZ(pPager) ){.
11c5d 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 rc = pager_t
11c5e 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 runcate(pPager,
11c5f 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 mxPg);. if(
11c60 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11c61 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 {. goto e
11c62 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 nd_playback;.
11c63 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 }. pPage
11c64 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67 r->dbSize = mxPg
11c65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
11c66 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 Copy original pa
11c67 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a ges out of the j
11c68 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 ournal and back
11c69 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a into the . **
11c6a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
11c6b 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 63 68 65 nd/or page cache
11c6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
11c6d 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 2b (u=0; u<nRec; u+
11c6e 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 +){. if( ne
11c6f 65 64 50 61 67 65 72 52 65 73 65 74 20 29 7b 0a edPagerReset ){.
11c70 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 pager_re
11c71 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
11c72 20 20 20 20 20 6e 65 65 64 50 61 67 65 72 52 65 needPagerRe
11c73 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d set = 0;. }
11c74 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
11c75 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
11c76 61 67 65 28 70 50 61 67 65 72 2c 31 2c 69 73 55 age(pPager,1,isU
11c77 6e 73 79 6e 63 2c 26 70 50 61 67 65 72 2d 3e 6a nsync,&pPager->j
11c78 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a ournalOff,0,0);.
11c79 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
11c7a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
11c7b 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
11c7c 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 E_DONE ){.
11c7d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
11c7e 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 OK;. pP
11c7f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11c80 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20 = szJ;.
11c81 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
11c82 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
11c83 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 75 /* If we are u
11c84 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 nable to rollbac
11c85 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74 75 k, quit and retu
11c86 72 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20 20 rn the error.
11c87 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20 ** code.
11c88 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 This will cause
11c89 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e the pager to en
11c8a 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 ter the error st
11c8b 61 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ate. **
11c8c 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 74 so that no furt
11c8d 68 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 65 her harm will be
11c8e 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 20 done. Perhaps
11c8f 74 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20 20 the next.
11c90 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f ** process to
11c91 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c come along will
11c92 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c be able to roll
11c93 62 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 back the databas
11c94 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
11c95 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
11c96 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 nd_playback;.
11c97 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
11c98 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 }. }. /*NOT
11c99 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73 65 REACHED*/. asse
11c9a 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c rt( 0 );..end_pl
11c9b 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c ayback:. /* Fol
11c9c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 lowing a rollbac
11c9d 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 k, the database
11c9e 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 62 file should be b
11c9f 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67 69 ack in its origi
11ca0 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20 70 nal. ** state p
11ca1 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 rior to the star
11ca2 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 t of the transac
11ca3 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 tion, so invoke
11ca4 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f the. ** SQLITE_
11ca5 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 FCNTL_DB_UNCHANG
11ca6 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 ED file-control
11ca7 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62 6c method to disabl
11ca8 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 e the. ** asser
11ca9 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74 72 tion that the tr
11caa 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
11cab 72 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e 0a r was modified..
11cac 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 0a 20 */. assert(.
11cad 20 20 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 pPager->fd->p
11cae 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20 Methods==0 ||.
11caf 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 sqlite3OsFileC
11cb0 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 2d 3e 66 ontrol(pPager->f
11cb1 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 d,SQLITE_FCNTL_D
11cb2 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30 29 3e 3d B_UNCHANGED,0)>=
11cb3 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a SQLITE_OK. );..
11cb4 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 6c 61 /* If this pla
11cb5 79 62 61 63 6b 20 69 73 20 68 61 70 70 65 6e 69 yback is happeni
11cb6 6e 67 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ng automatically
11cb7 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 as a result of
11cb8 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d an IO or . ** m
11cb9 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74 68 61 74 alloc error that
11cba 20 6f 63 63 75 72 72 65 64 20 61 66 74 65 72 20 occurred after
11cbb 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
11cbc 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20 62 er was updated b
11cbd 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 ut . ** before
11cbe 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
11cbf 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 was committed, t
11cc0 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 hen the change-c
11cc1 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20 6d 6f 64 ounter . ** mod
11cc2 69 66 69 63 61 74 69 6f 6e 20 6d 61 79 20 6a 75 ification may ju
11cc3 73 74 20 68 61 76 65 20 62 65 65 6e 20 72 65 76 st have been rev
11cc4 65 72 74 65 64 2e 20 49 66 20 74 68 69 73 20 68 erted. If this h
11cc5 61 70 70 65 6e 73 20 69 6e 20 65 78 63 6c 75 73 appens in exclus
11cc6 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20 ive . ** mode,
11cc7 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 then subsequent
11cc8 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 70 65 72 transactions per
11cc9 66 6f 72 6d 65 64 20 62 79 20 74 68 65 20 63 6f formed by the co
11cca 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f nnection will no
11ccb 74 0a 20 20 2a 2a 20 75 70 64 61 74 65 20 74 68 t. ** update th
11ccc 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
11ccd 20 61 74 20 61 6c 6c 2e 20 54 68 69 73 20 6d 61 at all. This ma
11cce 79 20 6c 65 61 64 20 74 6f 20 63 61 63 68 65 20 y lead to cache
11ccf 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 20 inconsistency.
11cd0 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 ** problems for
11cd1 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 other processes
11cd2 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e at some point in
11cd3 20 74 68 65 20 66 75 74 75 72 65 2e 20 53 6f 2c the future. So,
11cd4 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e 20 63 61 just. ** in ca
11cd5 73 65 20 74 68 69 73 20 68 61 73 20 68 61 70 70 se this has happ
11cd6 65 6e 65 64 2c 20 63 6c 65 61 72 20 74 68 65 20 ened, clear the
11cd7 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
11cd8 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 flag now.. */.
11cd9 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
11cda 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 ountDone = pPage
11cdb 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 0a 20 20 r->tempFile;..
11cdc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
11cdd 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 K ){. zMaster
11cde 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 = pPager->pTmpS
11cdf 70 61 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72 pace;. rc = r
11ce0 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c eadMasterJournal
11ce1 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d (pPager->jfd, zM
11ce2 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 aster, pPager->p
11ce3 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
11ce4 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 1);. testcase
11ce5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
11ce6 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
11ce7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11ce8 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 rc = pager_end
11ce9 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
11cea 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 ger, zMaster[0]!
11ceb 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74 65 73 74 ='\0');. test
11cec 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 case( rc!=SQLITE
11ced 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 _OK );. }. if(
11cee 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
11cef 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20 & zMaster[0] &&
11cf0 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 res ){. /* If
11cf1 20 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73 there was a mas
11cf2 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 ter journal and
11cf3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
11cf4 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73 l return success
11cf5 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 ,. ** see if
11cf6 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
11cf7 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 o delete the mas
11cf8 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 ter journal..
11cf9 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 */. rc = pag
11cfa 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61 er_delmaster(pPa
11cfb 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 ger, zMaster);.
11cfc 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 testcase( rc!
11cfd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
11cfe 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67 65 }.. /* The Page
11cff 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 r.sectorSize var
11d00 69 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20 62 iable may have b
11d01 65 65 6e 20 75 70 64 61 74 65 64 20 77 68 69 6c een updated whil
11d02 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62 e rolling. ** b
11d03 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63 72 ack a journal cr
11d04 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 eated by a proce
11d05 73 73 20 77 69 74 68 20 61 20 64 69 66 66 65 72 ss with a differ
11d06 65 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65 0a ent sector size.
11d07 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73 65 ** value. Rese
11d08 74 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72 72 t it to the corr
11d09 65 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ect value for th
11d0a 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f is process.. */
11d0b 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 . setSectorSize
11d0c 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 (pPager);. retu
11d0d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
11d0e 50 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f 69 Playback savepoi
11d0f 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 4f nt pSavepoint. O
11d10 72 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e 74 r, if pSavepoint
11d11 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c 61 ==NULL, then pla
11d12 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e 74 yback.** the ent
11d13 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e ire master journ
11d14 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 73 al file. The cas
11d15 65 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 e pSavepoint==NU
11d16 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 0a LL occurs when .
11d17 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f ** a ROLLBACK TO
11d18 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 6f command is invo
11d19 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f 49 ked on a SAVEPOI
11d1a 4e 54 20 74 68 61 74 20 69 73 20 61 20 74 72 61 NT that is a tra
11d1b 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 76 nsaction .** sav
11d1c 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 epoint..**.** Wh
11d1d 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73 en pSavepoint is
11d1e 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 not NULL (meani
11d1f 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 ng a non-transac
11d20 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 tion savepoint i
11d21 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c s .** being roll
11d22 65 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74 ed back), then t
11d23 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 he rollback cons
11d24 69 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68 ists of up to th
11d25 72 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 70 ree stages,.** p
11d26 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20 erformed in the
11d27 6f 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a order specified:
11d28 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 .**.** * Pages
11d29 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b are played back
11d2a 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a from the main j
11d2b 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 ournal starting
11d2c 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 6f at byte.** o
11d2d 66 66 73 65 74 20 50 61 67 65 72 53 61 76 65 70 ffset PagerSavep
11d2e 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 oint.iOffset and
11d2f 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 0a continuing to .
11d30 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 ** PagerSave
11d31 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 point.iHdrOffset
11d32 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 20 , or to the end
11d33 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
11d34 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 nal.** file
11d35 69 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e if PagerSavepoin
11d36 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 t.iHdrOffset is
11d37 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 zero..**.** *
11d38 49 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e If PagerSavepoin
11d39 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 t.iHdrOffset is
11d3a 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 70 not zero, then p
11d3b 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 0a ages are played.
11d3c 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 72 ** back star
11d3d 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f ting from the jo
11d3e 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d 6d urnal header imm
11d3f 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 ediately followi
11d40 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 ng .** Pager
11d41 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 Savepoint.iHdrOf
11d42 66 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 20 fset to the end
11d43 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
11d44 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 nal file..**.**
11d45 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 68 * Pages are th
11d46 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 en played back f
11d47 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 rom the sub-jour
11d48 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 69 nal file, starti
11d49 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 74 ng.** with t
11d4a 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
11d4b 74 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 6f t.iSubRec and co
11d4c 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 ntinuing to the
11d4d 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 68 end of.** th
11d4e 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
11d4f 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74 **.** Throughout
11d50 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72 the rollback pr
11d51 6f 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65 ocess, each time
11d52 20 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 a page is rolle
11d53 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 63 d back, the.** c
11d54 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 orresponding bit
11d55 20 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74 is set in a bit
11d56 76 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76 vec structure (v
11d57 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e ariable pDone in
11d58 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e the.** implemen
11d59 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54 tation below). T
11d5a 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65 his is used to e
11d5b 6e 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67 nsure that a pag
11d5c 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c e is only.** rol
11d5d 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 72 led back the fir
11d5e 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e st time it is en
11d5f 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 74 countered in eit
11d60 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a her journal..**.
11d61 2a 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e 74 ** If pSavepoint
11d62 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 is NULL, then p
11d63 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 6c ages are only pl
11d64 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 ayed back from t
11d65 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e he main.** journ
11d66 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 69 al file. There i
11d67 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20 s no need for a
11d68 62 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 63 bitvec in this c
11d69 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 ase..**.** In ei
11d6a 74 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f 72 ther case, befor
11d6b 65 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d 65 e playback comme
11d6c 6e 63 65 73 20 74 68 65 20 50 61 67 65 72 2e 64 nces the Pager.d
11d6d 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a bSize variable.*
11d6e 2a 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 68 * is reset to th
11d6f 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 20 e value that it
11d70 68 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 72 held at the star
11d71 74 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 t of the savepoi
11d72 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e 73 nt .** (or trans
11d73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 65 action). No page
11d74 20 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 6d with a page-num
11d75 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e ber greater than
11d76 20 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 69 this value.** i
11d77 73 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 49 s played back. I
11d78 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e 74 f one is encount
11d79 65 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 6c ered it is simpl
11d7a 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 74 y skipped..*/.st
11d7b 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 6c atic int pagerPl
11d7c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 aybackSavepoint(
11d7d 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
11d7e 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 agerSavepoint *p
11d7f 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 36 Savepoint){. i6
11d80 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
11d81 20 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 74 /* Effect
11d82 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 ive size of the
11d83 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a main journal */.
11d84 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 i64 iHdrOff;
11d85 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
11d86 64 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d 65 d of first segme
11d87 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e nt of main-journ
11d88 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 20 al records */.
11d89 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
11d8a 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 OK; /* Retu
11d8b 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 74 rn code */. Bit
11d8c 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 vec *pDone = 0;
11d8d 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 /* Bitvec
11d8e 74 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20 to ensure pages
11d8f 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79 played back only
11d90 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 once */.. asse
11d91 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
11d92 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e>=PAGER_SHARED
11d93 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 );.. /* Allocat
11d94 65 20 61 20 62 69 74 76 65 63 20 74 6f 20 75 73 e a bitvec to us
11d95 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 e to store the s
11d96 65 74 20 6f 66 20 70 61 67 65 73 20 72 6f 6c 6c et of pages roll
11d97 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 ed back */. if(
11d98 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 pSavepoint ){.
11d99 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c 69 74 pDone = sqlit
11d9a 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 e3BitvecCreate(p
11d9b 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 Savepoint->nOrig
11d9c 29 3b 0a 20 20 20 20 69 66 28 20 21 70 44 6f 6e );. if( !pDon
11d9d 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 e ){. retur
11d9e 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
11d9f 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
11da0 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 Set the database
11da1 20 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 size back to th
11da2 65 20 76 61 6c 75 65 20 69 74 20 77 61 73 20 62 e value it was b
11da3 65 66 6f 72 65 20 74 68 65 20 73 61 76 65 70 6f efore the savepo
11da4 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e 67 20 int . ** being
11da5 72 65 76 65 72 74 65 64 20 77 61 73 20 6f 70 65 reverted was ope
11da6 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 ned.. */. pPag
11da7 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61 er->dbSize = pSa
11da8 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70 vepoint ? pSavep
11da9 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50 oint->nOrig : pP
11daa 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
11dab 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70 50 61 67 ;.. /* Use pPag
11dac 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 61 er->journalOff a
11dad 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 s the effective
11dae 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e size of the main
11daf 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a rollback. ** j
11db0 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 61 63 74 ournal. The act
11db1 75 61 6c 20 66 69 6c 65 20 6d 69 67 68 74 20 62 ual file might b
11db2 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 e larger than th
11db3 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41 47 45 52 is in. ** PAGER
11db4 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 _JOURNALMODE_TRU
11db5 4e 43 41 54 45 20 6f 72 20 50 41 47 45 52 5f 4a NCATE or PAGER_J
11db6 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
11db7 53 54 2e 20 20 42 75 74 20 61 6e 79 74 68 69 6e ST. But anythin
11db8 67 0a 20 20 2a 2a 20 70 61 73 74 20 70 50 61 67 g. ** past pPag
11db9 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 er->journalOff i
11dba 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20 74 6f 20 s off-limits to
11dbb 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d us.. */. szJ =
11dbc 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11dbd 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e Off;.. /* Begin
11dbe 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b by rolling back
11dbf 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 records from th
11dc0 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 e main journal s
11dc1 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 tarting at. **
11dc2 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 PagerSavepoint.i
11dc3 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 Offset and conti
11dc4 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 78 nuing to the nex
11dc5 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
11dc6 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 6d 69 67 .. ** There mig
11dc7 68 74 20 62 65 20 72 65 63 6f 72 64 73 20 69 6e ht be records in
11dc8 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
11dc9 6c 20 74 68 61 74 20 68 61 76 65 20 61 20 70 61 l that have a pa
11dca 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 67 ge number. ** g
11dcb 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
11dcc 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 current database
11dcd 20 73 69 7a 65 20 28 70 50 61 67 65 72 2d 3e 64 size (pPager->d
11dce 62 53 69 7a 65 29 20 62 75 74 20 74 68 6f 73 65 bSize) but those
11dcf 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b . ** will be sk
11dd0 69 70 70 65 64 20 61 75 74 6f 6d 61 74 69 63 61 ipped automatica
11dd1 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 72 65 20 lly. Pages are
11dd2 61 64 64 65 64 20 74 6f 20 70 44 6f 6e 65 20 61 added to pDone a
11dd3 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 72 65 20 s they. ** are
11dd4 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a played back.. *
11dd5 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 /. if( pSavepoi
11dd6 6e 74 20 29 7b 0a 20 20 20 20 69 48 64 72 4f 66 nt ){. iHdrOf
11dd7 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e f = pSavepoint->
11dd8 69 48 64 72 4f 66 66 73 65 74 20 3f 20 70 53 61 iHdrOffset ? pSa
11dd9 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 vepoint->iHdrOff
11dda 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 set : szJ;. p
11ddb 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11ddc 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e f = pSavepoint->
11ddd 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 iOffset;. whi
11dde 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
11ddf 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 K && pPager->jou
11de0 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20 rnalOff<iHdrOff
11de1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 ){. rc = pa
11de2 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 ger_playback_one
11de3 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c _page(pPager, 1,
11de4 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 0, &pPager->jou
11de5 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e rnalOff, 1, pDon
11de6 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 e);. }. as
11de7 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
11de8 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 6c 73 65 _DONE );. }else
11de9 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
11dea 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 urnalOff = 0;.
11deb 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 }.. /* Continue
11dec 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 rolling back re
11ded 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 74 68 65 cords out of the
11dee 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 main journal st
11def 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 74 arting at. ** t
11df0 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c he first journal
11df1 20 68 65 61 64 65 72 20 73 65 65 6e 20 61 6e 64 header seen and
11df2 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e 74 69 continuing unti
11df3 6c 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 l the effective
11df4 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 end. ** of the
11df5 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c main journal fil
11df6 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74 6f 20 e. Continue to
11df7 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 skip out-of-rang
11df8 65 20 70 61 67 65 73 20 61 6e 64 0a 20 20 2a 2a e pages and. **
11df9 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 69 6e 67 continue adding
11dfa 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 pages rolled ba
11dfb 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a ck to pDone.. *
11dfc 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 /. while( rc==S
11dfd 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 QLITE_OK && pPag
11dfe 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 er->journalOff<s
11dff 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69 zJ ){. u32 ii
11e00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
11e01 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
11e02 20 20 20 20 75 33 32 20 6e 4a 52 65 63 20 3d 20 u32 nJRec =
11e03 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 0; /* Number
11e04 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f of Journal Reco
11e05 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64 rds */. u32 d
11e06 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72 ummy;. rc = r
11e07 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 eadJournalHdr(pP
11e08 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 ager, szJ, &nJRe
11e09 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 c, &dummy);.
11e0a 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
11e0b 54 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 TE_DONE );..
11e0c 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 22 70 /*. ** The "p
11e0d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
11e0e 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a r+JOURNAL_HDR_SZ
11e0f 28 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 (pPager)==pPager
11e10 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 ->journalOff".
11e11 20 20 2a 2a 20 74 65 73 74 20 69 73 20 72 65 6c ** test is rel
11e12 61 74 65 64 20 74 6f 20 74 69 63 6b 65 74 20 23 ated to ticket #
11e13 32 35 36 35 2e 20 20 53 65 65 20 74 68 65 20 64 2565. See the d
11e14 69 73 63 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 iscussion in the
11e15 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c . ** pager_pl
11e16 61 79 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f ayback() functio
11e17 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c n for additional
11e18 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 information..
11e19 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
11e1a 20 21 28 6e 4a 52 65 63 3d 3d 30 0a 20 20 20 20 !(nJRec==0.
11e1b 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e && pPager->
11e1c 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e journalHdr+JOURN
11e1d 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
11e1e 29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e )!=pPager->journ
11e1f 61 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20 26 alOff. &
11e20 26 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 & ((szJ - pPager
11e21 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 ->journalOff) /
11e22 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
11e23 61 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20 20 ager))>0.
11e24 20 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a 6f && pagerNextJo
11e25 75 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69 64 urnalPageIsValid
11e26 28 70 50 61 67 65 72 29 29 0a 20 20 20 20 29 3b (pPager)). );
11e27 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d . if( nJRec==
11e28 30 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 0 . && pPage
11e29 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f r->journalHdr+JO
11e2a 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
11e2b 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f ger)==pPager->jo
11e2c 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a urnalOff. ){.
11e2d 20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 nJRec = (u
11e2e 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 32)((szJ - pPage
11e2f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a r->journalOff)/J
11e30 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 OURNAL_PG_SZ(pPa
11e31 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ger));. }.
11e32 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 for(ii=0; rc==S
11e33 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e QLITE_OK && ii<n
11e34 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e JRec && pPager->
11e35 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 journalOff<szJ;
11e36 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 ii++){. rc
11e37 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
11e38 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 _one_page(pPager
11e39 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d , 1, 0, &pPager-
11e3a 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 >journalOff, 1,
11e3b 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 pDone);. }.
11e3c 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
11e3d 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d LITE_DONE );. }
11e3e 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
11e3f 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
11e40 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
11e41 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e szJ );.. /* Fin
11e42 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 ally, rollback
11e43 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 pages from the s
11e44 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 ub-journal. Pag
11e45 65 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a e that were. **
11e46 20 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c previously roll
11e47 65 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 ed back out of t
11e48 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
11e49 28 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 (and are hence i
11e4a 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 n pDone). ** wi
11e4b 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 ll be skipped.
11e4c 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 Out-of-range pag
11e4d 65 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 es are also skip
11e4e 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ped.. */. if(
11e4f 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 pSavepoint ){.
11e50 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 u32 ii;
11e51 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
11e52 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 nter */. i64
11e53 6f 66 66 73 65 74 20 3d 20 70 53 61 76 65 70 6f offset = pSavepo
11e54 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b int->iSubRec*(4+
11e55 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11e56 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 53 );. for(ii=pS
11e57 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 avepoint->iSubRe
11e58 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b c; rc==SQLITE_OK
11e59 20 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e && ii<pPager->n
11e5a 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 SubRec; ii++){.
11e5b 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 66 66 assert( off
11e5c 73 65 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65 set==ii*(4+pPage
11e5d 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 3b 0a r->pageSize) );.
11e5e 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
11e5f 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 _playback_one_pa
11e60 67 65 28 70 50 61 67 65 72 2c 20 30 2c 20 30 2c ge(pPager, 0, 0,
11e61 20 26 6f 66 66 73 65 74 2c 20 31 2c 20 70 44 6f &offset, 1, pDo
11e62 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 ne);. }. a
11e63 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
11e64 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 E_DONE );. }..
11e65 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
11e66 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20 stroy(pDone);.
11e67 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
11e68 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d K ){. pPager-
11e69 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a >journalOff = sz
11e6a 4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 J;. }. return
11e6b 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 rc;.}../*.** Cha
11e6c 6e 67 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 nge the maximum
11e6d 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d number of in-mem
11e6e 6f 72 79 20 70 61 67 65 73 20 74 68 61 74 20 61 ory pages that a
11e6f 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 re allowed..*/.S
11e70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
11e71 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
11e72 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 etCachesize(Page
11e73 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d r *pPager, int m
11e74 78 50 61 67 65 29 7b 0a 20 20 73 71 6c 69 74 65 xPage){. sqlite
11e75 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73 3PcacheSetCaches
11e76 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ize(pPager->pPCa
11e77 63 68 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a che, mxPage);.}.
11e78 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 ./*.** Adjust th
11e79 65 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 e robustness of
11e7a 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
11e7b 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 damage due to OS
11e7c 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 crashes.** or p
11e7d 6f 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 ower failures by
11e7e 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 changing the nu
11e7f 6d 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 mber of syncs()s
11e80 20 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a when writing.**
11e81 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
11e82 75 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 urnal. There ar
11e83 65 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a e three levels:.
11e84 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 **.** OFF
11e85 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 sqlite3OsSync
11e86 28 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c () is never call
11e87 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 ed. This is the
11e88 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 default.**
11e89 20 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d for tem
11e8a 70 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 porary and trans
11e8b 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a ient files..**.*
11e8c 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 * NORMAL T
11e8d 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 he journal is sy
11e8e 6e 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 nced once before
11e8f 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e writes begin on
11e90 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
11e91 20 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 database.
11e92 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 This is normally
11e93 20 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 adequate protec
11e94 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 tion, but.**
11e95 20 20 20 20 20 20 20 20 20 20 69 74 20 69 73 20 it is
11e96 74 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f theoretically po
11e97 73 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 ssible, though v
11e98 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a ery unlikely,.**
11e99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
11e9a 61 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 at an inopertune
11e9b 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 power failure c
11e9c 6f 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a ould leave the j
11e9d 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 ournal.**
11e9e 20 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 in a stat
11e9f 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 e which would ca
11ea0 75 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 use damage to th
11ea1 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 e database.**
11ea2 20 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 when
11ea3 69 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 it is rolled bac
11ea4 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c k..**.** FULL
11ea5 20 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 The journa
11ea6 6c 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 l is synced twic
11ea7 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 e before writes
11ea8 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 begin on the.**
11ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 dat
11eaa 61 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 abase (with some
11eab 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
11eac 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 rmation - the nR
11ead 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 ec field.**
11eae 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 of the
11eaf 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d journal header -
11eb0 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 being written i
11eb1 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 n between the tw
11eb2 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 o.**
11eb3 20 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 syncs). If we
11eb4 20 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 assume that wri
11eb5 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 ting a.**
11eb6 20 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 single di
11eb7 73 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f sk sector is ato
11eb8 6d 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d mic, then this m
11eb9 6f 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 ode provides.**
11eba 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 ass
11ebb 75 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 urance that the
11ebc 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 journal will not
11ebd 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f be corrupted to
11ebe 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
11ebf 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 point of ca
11ec0 75 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 using damage to
11ec1 74 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72 the database dur
11ec2 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a ing rollback..**
11ec3 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c 75 .** Numeric valu
11ec4 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 es associated wi
11ec5 74 68 20 74 68 65 73 65 20 73 74 61 74 65 73 20 th these states
11ec6 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d are OFF==1, NORM
11ec7 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c AL=2,.** and FUL
11ec8 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 L=3..*/.#ifndef
11ec9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 SQLITE_OMIT_PAGE
11eca 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 R_PRAGMAS.SQLITE
11ecb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
11ecc 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 lite3PagerSetSaf
11ecd 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 20 2a etyLevel(Pager *
11ece 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 pPager, int leve
11ecf 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e l, int bFullFsyn
11ed0 63 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f c){. pPager->no
11ed1 53 79 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d Sync = (level==
11ed2 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 1 || pPager->tem
11ed3 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 pFile) ?1:0;. p
11ed4 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 Pager->fullSync
11ed5 3d 20 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 = (level==3 && !
11ed6 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
11ed7 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 ) ?1:0;. pPager
11ed8 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 ->sync_flags = (
11ed9 62 46 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 bFullFsync?SQLIT
11eda 45 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 E_SYNC_FULL:SQLI
11edb 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b TE_SYNC_NORMAL);
11edc 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e . if( pPager->n
11edd 6f 53 79 6e 63 20 29 20 70 50 61 67 65 72 2d 3e oSync ) pPager->
11ede 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a needSync = 0;.}.
11edf 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
11ee0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 e following glob
11ee1 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 al variable is i
11ee2 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 ncremented whene
11ee3 76 65 72 20 74 68 65 20 6c 69 62 72 61 72 79 0a ver the library.
11ee4 2a 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6f ** attempts to o
11ee5 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 pen a temporary
11ee6 66 69 6c 65 2e 20 20 54 68 69 73 20 69 6e 66 6f file. This info
11ee7 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 rmation is used
11ee8 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 for.** testing a
11ee9 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 nd analysis only
11eea 2e 20 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 . .*/.#ifdef SQ
11eeb 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
11eec 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
11eed 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 _opentemp_count
11eee 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
11eef 2a 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 ** Open a tempor
11ef0 61 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ary file..**.**
11ef1 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 Write the file d
11ef2 65 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a escriptor into *
11ef3 70 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 pFile. Return SQ
11ef4 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
11ef5 73 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f ss .** or some o
11ef6 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 ther error code
11ef7 69 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20 if we fail. The
11ef8 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 OS will automati
11ef9 63 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 cally .** delete
11efa 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 the temporary f
11efb 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 ile when it is c
11efc 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 losed..**.** The
11efd 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f flags passed to
11efe 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 20 78 the VFS layer x
11eff 4f 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 Open() call are
11f00 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a those specified.
11f01 2a 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ** by parameter
11f02 76 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69 vfsFlags ORed wi
11f03 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 th the following
11f04 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 :.**.** SQLI
11f05 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
11f06 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f E.** SQLITE_
11f07 4f 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 OPEN_CREATE.**
11f08 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 SQLITE_OPEN_E
11f09 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 XCLUSIVE.**
11f0a 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
11f0b 54 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 TEONCLOSE.*/.sta
11f0c 74 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65 tic int pagerOpe
11f0d 6e 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a ntemp(. Pager *
11f0e 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f pPager, /
11f0f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65 * The pager obje
11f10 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ct */. sqlite3_
11f11 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a file *pFile, /*
11f12 20 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 Write the file
11f13 64 65 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 descriptor here
11f14 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 */. int vfsFlag
11f15 73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c s /* Fl
11f16 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 ags passed throu
11f17 67 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f gh to the VFS */
11f18 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 .){. int rc;
11f19 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11f1a 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 eturn code */..#
11f1b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
11f1c 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e T. sqlite3_open
11f1d 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f temp_count++; /
11f1e 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 * Used for testi
11f1f 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 ng and analysis
11f20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a only */.#endif..
11f21 20 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 vfsFlags |= S
11f22 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
11f23 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 RITE | SQLITE_OP
11f24 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 EN_CREATE |.
11f25 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f SQLITE_O
11f26 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 PEN_EXCLUSIVE |
11f27 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
11f28 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 TEONCLOSE;. rc
11f29 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 = sqlite3OsOpen(
11f2a 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c pPager->pVfs, 0,
11f2b 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 pFile, vfsFlags
11f2c 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 , 0);. assert(
11f2d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
11f2e 20 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 isOpen(pFile) )
11f2f 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
11f30 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
11f31 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e busy handler fun
11f32 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
11f33 20 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 pager invokes t
11f34 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 he busy-handler
11f35 69 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b if sqlite3OsLock
11f36 28 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 () returns .** S
11f37 51 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20 QLITE_BUSY when
11f38 74 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 trying to upgrad
11f39 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 e from no-lock t
11f3a 6f 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c o a SHARED lock,
11f3b 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69 .** or when tryi
11f3c 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 ng to upgrade fr
11f3d 6f 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f om a RESERVED lo
11f3e 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 ck to an EXCLUSI
11f3f 56 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 VE .** lock. It
11f40 64 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b does *not* invok
11f41 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c e the busy handl
11f42 65 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e er when upgradin
11f43 67 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 g from.** SHARED
11f44 20 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72 to RESERVED, or
11f45 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 when upgrading
11f46 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45 from SHARED to E
11f47 58 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 XCLUSIVE.** (whi
11f48 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 ch occurs during
11f49 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
11f4a 6c 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a lback). Summary:
11f4b 0a 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 .**.** Transit
11f4c 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ion
11f4d 20 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76 | Inv
11f4e 6f 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65 okes xBusyHandle
11f4f 72 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d r.** ---------
11f50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11f51 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11f52 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
11f53 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 ** NO_LOCK
11f54 20 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 -> SHARED_LOC
11f55 4b 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 K | Yes.**
11f56 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 SHARED_LOCK
11f57 2d 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b -> RESERVED_LOCK
11f58 20 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 | No.** SH
11f59 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 ARED_LOCK -> E
11f5a 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 XCLUSIVE_LOCK
11f5b 7c 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 | No.** RESERV
11f5c 45 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 ED_LOCK -> EXCLU
11f5d 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 SIVE_LOCK | Ye
11f5e 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 s.**.** If the b
11f5f 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c usy-handler call
11f60 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e back returns non
11f61 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 -zero, the lock
11f62 69 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 is .** retried.
11f63 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 If it returns ze
11f64 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c ro, then the SQL
11f65 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69 ITE_BUSY error i
11f66 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f s.** returned to
11f67 20 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 the caller of t
11f68 68 65 20 70 61 67 65 72 20 41 50 49 20 66 75 6e he pager API fun
11f69 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
11f6a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
11f6b 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 lite3PagerSetBus
11f6c 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 yhandler(. Page
11f6d 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
11f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11f6f 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 /* Pager object
11f70 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 */. int (*xBus
11f71 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a yHandler)(void *
11f72 29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f ), /* Po
11f73 69 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61 inter to busy-ha
11f74 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a ndler function *
11f75 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 /. void *pBusyH
11f76 61 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20 20 andlerArg
11f77 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
11f78 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 ment to pass to
11f79 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a xBusyHandler */.
11f7a 29 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 ){ . pPager->x
11f7b 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 BusyHandler = xB
11f7c 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 usyHandler;. pP
11f7d 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c ager->pBusyHandl
11f7e 65 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e erArg = pBusyHan
11f7f 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a dlerArg;.}../*.*
11f80 2a 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74 * Set the reinit
11f81 69 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73 ializer for this
11f82 20 70 61 67 65 72 2e 20 49 66 20 6e 6f 74 20 4e pager. If not N
11f83 55 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74 69 ULL, the reiniti
11f84 61 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61 6c alizer.** is cal
11f85 6c 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f 6e led when the con
11f86 74 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20 69 tent of a page i
11f87 6e 20 63 61 63 68 65 20 69 73 20 6d 6f 64 69 66 n cache is modif
11f88 69 65 64 20 28 72 65 73 74 6f 72 65 64 29 0a 2a ied (restored).*
11f89 2a 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 74 * as part of a t
11f8a 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 73 61 ransaction or sa
11f8b 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b vepoint rollback
11f8c 2e 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 67 . The callback g
11f8d 69 76 65 73 20 0a 2a 2a 20 68 69 67 68 65 72 2d ives .** higher-
11f8e 6c 65 76 65 6c 20 63 6f 64 65 20 61 6e 20 6f 70 level code an op
11f8f 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65 73 portunity to res
11f90 74 6f 72 65 20 74 68 65 20 45 58 54 52 41 20 73 tore the EXTRA s
11f91 65 63 74 69 6f 6e 20 74 6f 20 0a 2a 2a 20 61 67 ection to .** ag
11f92 72 65 65 20 77 69 74 68 20 74 68 65 20 72 65 73 ree with the res
11f93 74 6f 72 65 64 20 70 61 67 65 20 64 61 74 61 2e tored page data.
11f94 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
11f95 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
11f96 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28 agerSetReiniter(
11f97 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 76 Pager *pPager, v
11f98 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 oid (*xReinit)(D
11f99 62 50 61 67 65 2a 29 29 7b 0a 20 20 70 50 61 67 bPage*)){. pPag
11f9a 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 er->xReiniter =
11f9b 78 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a xReinit;.}../*.*
11f9c 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 * Change the pag
11f9d 65 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74 e size used by t
11f9e 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e he Pager object.
11f9f 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 The new page si
11fa0 7a 65 20 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 ze .** is passed
11fa1 20 69 6e 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a in *pPageSize..
11fa2 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
11fa3 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 er is in the err
11fa4 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 or state when th
11fa5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
11fa6 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a 20 69 73 20 alled, it.** is
11fa7 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 76 61 6c a no-op. The val
11fa8 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 ue returned is t
11fa9 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 he error state e
11faa 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e 20 rror code (i.e.
11fab 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 .** one of SQLIT
11fac 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f E_IOERR, SQLITE_
11fad 43 4f 52 52 55 50 54 20 6f 72 20 53 51 4c 49 54 CORRUPT or SQLIT
11fae 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f E_FULL)..**.** O
11faf 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6c 6c therwise, if all
11fb0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
11fb1 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a g are true:.**.*
11fb2 2a 20 20 20 2a 20 74 68 65 20 6e 65 77 20 70 61 * * the new pa
11fb3 67 65 20 73 69 7a 65 20 28 76 61 6c 75 65 20 6f ge size (value o
11fb4 66 20 2a 70 50 61 67 65 53 69 7a 65 29 20 69 73 f *pPageSize) is
11fb5 20 76 61 6c 69 64 20 28 61 20 70 6f 77 65 72 20 valid (a power
11fb6 0a 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f 20 62 .** of two b
11fb7 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 53 etween 512 and S
11fb8 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
11fb9 49 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65 29 2c IZE, inclusive),
11fba 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 and.**.** * t
11fbb 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 here are no outs
11fbc 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 tanding page ref
11fbd 65 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a 0a erences, and.**.
11fbe 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 ** * the datab
11fbf 61 73 65 20 69 73 20 65 69 74 68 65 72 20 6e 6f ase is either no
11fc0 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 t an in-memory d
11fc1 61 74 61 62 61 73 65 20 6f 72 20 69 74 20 69 73 atabase or it is
11fc2 0a 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d 6d 65 .** an in-me
11fc3 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 74 68 mory database th
11fc4 61 74 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e at currently con
11fc5 73 69 73 74 73 20 6f 66 20 7a 65 72 6f 20 70 61 sists of zero pa
11fc6 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 ges..**.** then
11fc7 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 the pager object
11fc8 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 65 page size is se
11fc9 74 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65 2e t to *pPageSize.
11fca 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
11fcb 67 65 20 73 69 7a 65 20 69 73 20 63 68 61 6e 67 ge size is chang
11fcc 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 ed, then this fu
11fcd 6e 63 74 69 6f 6e 20 75 73 65 73 20 73 71 6c 69 nction uses sqli
11fce 74 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29 te3PagerMalloc()
11fcf 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 .** to obtain a
11fd0 20 6e 65 77 20 50 61 67 65 72 2e 70 54 6d 70 53 new Pager.pTmpS
11fd1 70 61 63 65 20 62 75 66 66 65 72 2e 20 49 66 20 pace buffer. If
11fd2 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 this allocation
11fd3 61 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61 69 6c attempt .** fail
11fd4 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 s, SQLITE_NOMEM
11fd5 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
11fd6 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 72 65 the page size re
11fd7 6d 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e mains unchanged.
11fd8 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 65 .** In all othe
11fd9 72 20 63 61 73 65 73 2c 20 53 51 4c 49 54 45 5f r cases, SQLITE_
11fda 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a OK is returned..
11fdb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
11fdc 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 63 68 e size is not ch
11fdd 61 6e 67 65 64 2c 20 65 69 74 68 65 72 20 62 65 anged, either be
11fde 63 61 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 65 cause one of the
11fdf 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 63 enumerated.** c
11fe0 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 onditions above
11fe1 69 73 20 6e 6f 74 20 74 72 75 65 2c 20 74 68 65 is not true, the
11fe2 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 72 pager was in er
11fe3 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 ror state when t
11fe4 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 his.** function
11fe5 77 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72 20 62 was called, or b
11fe6 65 63 61 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 ecause the memor
11fe7 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 y allocation att
11fe8 65 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a 2a 2a empt failed, .**
11fe9 20 74 68 65 6e 20 2a 70 50 61 67 65 53 69 7a 65 then *pPageSize
11fea 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f is set to the o
11feb 6c 64 2c 20 72 65 74 61 69 6e 65 64 20 70 61 67 ld, retained pag
11fec 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 72 65 e size before re
11fed 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 turning..*/.SQLI
11fee 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
11fef 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
11ff0 67 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 gesize(Pager *pP
11ff1 61 67 65 72 2c 20 75 31 36 20 2a 70 50 61 67 65 ager, u16 *pPage
11ff2 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 Size){. int rc
11ff3 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 = pPager->errCod
11ff4 65 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c e;. if( rc==SQL
11ff5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 31 ITE_OK ){. u1
11ff6 36 20 70 61 67 65 53 69 7a 65 20 3d 20 2a 70 50 6 pageSize = *pP
11ff7 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 61 73 73 ageSize;. ass
11ff8 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d 3d 30 ert( pageSize==0
11ff9 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e 3d 35 || (pageSize>=5
11ffa 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 12 && pageSize<=
11ffb 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
11ffc 53 49 5a 45 29 20 29 3b 0a 20 20 20 20 69 66 28 SIZE) );. if(
11ffd 20 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 pageSize && pag
11ffe 65 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 eSize!=pPager->p
11fff 61 67 65 53 69 7a 65 20 0a 20 20 20 20 20 26 26 ageSize . &&
12000 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d (pPager->memDb=
12001 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 =0 || pPager->db
12002 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20 20 26 26 Size==0). &&
12003 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
12004 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
12005 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 PCache)==0 .
12006 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 ){. char *p
12007 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71 New = (char *)sq
12008 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
12009 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 pageSize);.
1200a 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 if( !pNew ){.
1200b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1200c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
1200d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 61 else{. pa
1200e 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
1200f 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 );. pPage
12010 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 r->pageSize = pa
12011 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 geSize;.
12012 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
12013 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
12014 65 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 e);. pPag
12015 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 er->pTmpSpace =
12016 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 pNew;. sq
12017 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 lite3PcacheSetPa
12018 67 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 geSize(pPager->p
12019 50 43 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 PCache, pageSize
1201a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1201b 0a 20 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 . *pPageSize
1201c 3d 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70 = (u16)pPager->p
1201d 61 67 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 ageSize;. }. r
1201e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1201f 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
12020 74 65 72 20 74 6f 20 74 68 65 20 22 74 65 6d 70 ter to the "temp
12021 6f 72 61 72 79 20 70 61 67 65 22 20 62 75 66 66 orary page" buff
12022 65 72 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c er held internal
12023 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 67 ly.** by the pag
12024 65 72 2e 20 20 54 68 69 73 20 69 73 20 61 20 62 er. This is a b
12025 75 66 66 65 72 20 74 68 61 74 20 69 73 20 62 69 uffer that is bi
12026 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 g enough to hold
12027 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 the.** entire c
12028 6f 6e 74 65 6e 74 20 6f 66 20 61 20 64 61 74 61 ontent of a data
12029 62 61 73 65 20 70 61 67 65 2e 20 20 54 68 69 73 base page. This
1202a 20 62 75 66 66 65 72 20 69 73 20 75 73 65 64 20 buffer is used
1202b 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 internally.** du
1202c 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e ring rollback an
1202d 64 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 d will be overwr
1202e 69 74 74 65 6e 20 77 68 65 6e 65 76 65 72 20 61 itten whenever a
1202f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 rollback.** occ
12030 75 72 73 2e 20 20 42 75 74 20 6f 74 68 65 72 20 urs. But other
12031 6d 6f 64 75 6c 65 73 20 61 72 65 20 66 72 65 65 modules are free
12032 20 74 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 to use it too,
12033 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f as long as.** no
12034 20 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 rollbacks are h
12035 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c appening..*/.SQL
12036 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
12037 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 *sqlite3PagerTe
12038 6d 70 53 70 61 63 65 28 50 61 67 65 72 20 2a 70 mpSpace(Pager *p
12039 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
1203a 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 pPager->pTmpSpa
1203b 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 ce;.}../*.** Att
1203c 65 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65 20 empt to set the
1203d 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 maximum database
1203e 20 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d page count if m
1203f 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 xPage is positiv
12040 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 e. .** Make no c
12041 68 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67 65 hanges if mxPage
12042 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 is zero or nega
12043 74 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 tive. And never
12044 20 72 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d reduce the.** m
12045 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e aximum page coun
12046 74 20 62 65 6c 6f 77 20 74 68 65 20 63 75 72 72 t below the curr
12047 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 ent size of the
12048 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
12049 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 Regardless of mx
1204a 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 Page, return the
1204b 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d current maximum
1204c 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a page count..*/.
1204d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1204e 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d nt sqlite3PagerM
1204f 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 65 axPageCount(Page
12050 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d r *pPager, int m
12051 78 50 61 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 xPage){. if( mx
12052 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 Page>0 ){. pP
12053 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d ager->mxPgno = m
12054 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c xPage;. }. sql
12055 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
12056 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 nt(pPager, 0);.
12057 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
12058 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a mxPgno;.}../*.**
12059 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 The following s
1205a 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 et of routines a
1205b 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 62 re used to disab
1205c 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 le the simulated
1205d 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 .** I/O error me
1205e 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 chanism. These
1205f 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 routines are use
12060 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c d to avoid simul
12061 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 ated.** errors i
12062 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 77 n places where w
12063 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 e do not care ab
12064 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a out errors..**.*
12065 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 * Unless -DSQLIT
12066 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 64 E_TEST=1 is used
12067 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 , these routines
12068 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a are all no-ops.
12069 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 ** and generate
1206a 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 no code..*/.#ifd
1206b 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
1206c 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e QLITE_API extern
1206d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
1206e 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53 error_pending;.S
1206f 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e QLITE_API extern
12070 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
12071 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69 error_hit;.stati
12072 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b c int saved_cnt;
12073 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 .void disable_si
12074 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
12075 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 64 s(void){. saved
12076 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 69 _cnt = sqlite3_i
12077 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b o_error_pending;
12078 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 . sqlite3_io_er
12079 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31 ror_pending = -1
1207a 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f ;.}.void enable_
1207b 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
1207c 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c ors(void){. sql
1207d 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
1207e 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e nding = saved_cn
1207f 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 t;.}.#else.# def
12080 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 ine disable_simu
12081 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
12082 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c ).# define enabl
12083 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
12084 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a rrors().#endif..
12085 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66 /*.** Read the f
12086 69 72 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f irst N bytes fro
12087 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 m the beginning
12088 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f of the file into
12089 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20 memory.** that
1208a 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e pDest points to.
1208b 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 .**.** If the p
1208c 61 67 65 72 20 77 61 73 20 6f 70 65 6e 65 64 20 ager was opened
1208d 6f 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 on a transient f
1208e 69 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d ile (zFilename==
1208f 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e 65 ""), or.** opene
12090 64 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65 73 73 d on a file less
12091 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 69 6e than N bytes in
12092 20 73 69 7a 65 2c 20 74 68 65 20 6f 75 74 70 75 size, the outpu
12093 74 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 7a t buffer is.** z
12094 65 72 6f 65 64 20 61 6e 64 20 53 51 4c 49 54 45 eroed and SQLITE
12095 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 54 68 _OK returned. Th
12096 65 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f 72 20 e rationale for
12097 74 68 69 73 20 69 73 20 74 68 61 74 20 74 68 69 this is that thi
12098 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 s .** function i
12099 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 s used to read d
1209a 61 74 61 62 61 73 65 20 68 65 61 64 65 72 73 2c atabase headers,
1209b 20 61 6e 64 20 61 20 6e 65 77 20 74 72 61 6e 73 and a new trans
1209c 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 ient or.** zero
1209d 73 69 7a 65 64 20 64 61 74 61 62 61 73 65 20 68 sized database h
1209e 61 73 20 61 20 68 65 61 64 65 72 20 74 68 61 6e as a header than
1209f 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 consists entire
120a0 6c 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a 2a ly of zeroes..**
120a1 0a 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20 65 72 .** If any IO er
120a2 72 6f 72 20 61 70 61 72 74 20 66 72 6f 6d 20 53 ror apart from S
120a3 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
120a4 54 5f 52 45 41 44 20 69 73 20 65 6e 63 6f 75 6e T_READ is encoun
120a5 74 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 tered,.** the er
120a6 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
120a7 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c rned to the call
120a8 65 72 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 er and the conte
120a9 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 75 nts of the.** ou
120aa 74 70 75 74 20 62 75 66 66 65 72 20 75 6e 64 65 tput buffer unde
120ab 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fined..*/.SQLITE
120ac 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
120ad 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c ite3PagerReadFil
120ae 65 68 65 61 64 65 72 28 50 61 67 65 72 20 2a 70 eheader(Pager *p
120af 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75 6e Pager, int N, un
120b0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 65 signed char *pDe
120b1 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 st){. int rc =
120b2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d SQLITE_OK;. mem
120b3 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e 29 set(pDest, 0, N)
120b4 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 ;. assert( isOp
120b5 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
120b6 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 | pPager->tempFi
120b7 6c 65 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 le );. if( isOp
120b8 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 en(pPager->fd) )
120b9 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 {. IOTRACE(("
120ba 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22 DBHDR %p 0 %d\n"
120bb 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20 , pPager, N)).
120bc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
120bd 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c Read(pPager->fd,
120be 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 pDest, N, 0);.
120bf 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
120c0 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 E_IOERR_SHORT_RE
120c1 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d AD ){. rc =
120c2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
120c3 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
120c4 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
120c5 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d rn the total num
120c6 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
120c7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
120c8 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a e associated .**
120c9 20 77 69 74 68 20 70 50 61 67 65 72 2e 20 4e 6f with pPager. No
120ca 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 rmally, this is
120cb 63 61 6c 63 75 6c 61 74 65 64 20 61 73 20 28 3c calculated as (<
120cc 64 62 20 66 69 6c 65 20 73 69 7a 65 3e 2f 3c 70 db file size>/<p
120cd 61 67 65 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20 48 age-size>)..** H
120ce 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 66 owever, if the f
120cf 69 6c 65 20 69 73 20 62 65 74 77 65 65 6e 20 31 ile is between 1
120d0 20 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 65 3e and <page-size>
120d1 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 bytes in size,
120d2 74 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 69 73 then .** this is
120d3 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 31 20 considered a 1
120d4 70 61 67 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a page file..**.**
120d5 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
120d6 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 in error state
120d7 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
120d8 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 on is called, th
120d9 65 6e 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 en the.** error
120da 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 state error code
120db 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
120dc 20 2a 70 6e 50 61 67 65 20 6c 65 66 74 20 75 6e *pnPage left un
120dd 63 68 61 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a 20 changed. Or,.**
120de 69 66 20 74 68 65 20 66 69 6c 65 20 73 79 73 74 if the file syst
120df 65 6d 20 68 61 73 20 74 6f 20 62 65 20 71 75 65 em has to be que
120e0 72 69 65 64 20 66 6f 72 20 74 68 65 20 73 69 7a ried for the siz
120e1 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e e of the file an
120e2 64 0a 2a 2a 20 74 68 65 20 71 75 65 72 79 20 61 d.** the query a
120e3 74 74 65 6d 70 74 20 72 65 74 75 72 6e 73 20 61 ttempt returns a
120e4 6e 20 49 4f 20 65 72 72 6f 72 2c 20 74 68 65 20 n IO error, the
120e5 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 IO error code is
120e6 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 returned.** and
120e7 20 2a 70 6e 50 61 67 65 20 69 73 20 6c 65 66 74 *pnPage is left
120e8 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a unchanged..**.*
120e9 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 * Otherwise, if
120ea 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 everything is su
120eb 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 53 ccessful, then S
120ec 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
120ed 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 rned.** and *pnP
120ee 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 age is set to th
120ef 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
120f0 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
120f1 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
120f2 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
120f3 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 PagerPagecount(P
120f4 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
120f5 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 50 67 t *pnPage){. Pg
120f6 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 no nPage;
120f7 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
120f8 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a to return via *
120f9 70 6e 50 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 pnPage */.. /*
120fa 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
120fb 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 already in the e
120fc 72 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 75 rror state, retu
120fd 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 rn the error cod
120fe 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 e. */. if( pPag
120ff 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 er->errCode ){.
12100 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 return pPager
12101 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a ->errCode;. }..
12102 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 /* Determine t
12103 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
12104 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 es in the file.
12105 53 74 6f 72 65 20 74 68 69 73 20 69 6e 20 6e 50 Store this in nP
12106 61 67 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 age. */. if( pP
12107 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
12108 64 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d d ){. nPage =
12109 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b pPager->dbSize;
1210a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
1210b 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
1210c 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 72 /* Error r
1210d 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c eturned by OsFil
1210e 65 53 69 7a 65 28 29 20 2a 2f 0a 20 20 20 20 69 eSize() */. i
1210f 36 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 64 n = 0;
12110 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 73 /* File s
12111 69 7a 65 20 69 6e 20 62 79 74 65 73 20 72 65 74 ize in bytes ret
12112 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 urned by OsFileS
12113 69 7a 65 28 29 20 2a 2f 0a 0a 20 20 20 20 61 73 ize() */.. as
12114 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
12115 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 ger->fd) || pPag
12116 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a er->tempFile );.
12117 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 if( isOpen(p
12118 50 61 67 65 72 2d 3e 66 64 29 20 26 26 20 28 30 Pager->fd) && (0
12119 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 != (rc = sqlite
1211a 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 3OsFileSize(pPag
1211b 65 72 2d 3e 66 64 2c 20 26 6e 29 29 29 20 29 7b er->fd, &n))) ){
1211c 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 . pager_err
1211d 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
1211e 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1211f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
12120 3e 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d 3e >0 && n<pPager->
12121 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 pageSize ){.
12122 20 20 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 nPage = 1;.
12123 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 }else{. nP
12124 61 67 65 20 3d 20 28 50 67 6e 6f 29 28 6e 20 2f age = (Pgno)(n /
12125 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
12126 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 e);. }. if
12127 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 ( pPager->state!
12128 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b =PAGER_UNLOCK ){
12129 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
1212a 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 bSize = nPage;.
1212b 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 pPager->dbF
1212c 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b ileSize = nPage;
1212d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
1212e 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 31 3b 0a bSizeValid = 1;.
1212f 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
12130 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e If the current n
12131 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
12132 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 67 72 n the file is gr
12133 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 0a eater than the .
12134 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 ** configured
12135 6d 61 78 69 6d 75 6d 20 70 61 67 65 72 20 6e 75 maximum pager nu
12136 6d 62 65 72 2c 20 69 6e 63 72 65 61 73 65 20 74 mber, increase t
12137 68 65 20 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74 he allowed limit
12138 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 so. ** that th
12139 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 72 65 e file can be re
1213a 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e ad.. */. if( n
1213b 50 61 67 65 3e 70 50 61 67 65 72 2d 3e 6d 78 50 Page>pPager->mxP
1213c 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 gno ){. pPage
1213d 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e r->mxPgno = (Pgn
1213e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 20 o)nPage;. }..
1213f 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 75 /* Set the outpu
12140 74 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 72 t variable and r
12141 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
12142 2a 2f 0a 20 20 69 66 28 20 70 6e 50 61 67 65 20 */. if( pnPage
12143 29 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20 3d ){. *pnPage =
12144 20 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 72 65 nPage;. }. re
12145 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
12146 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f }.../*.** Try to
12147 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f obtain a lock o
12148 66 20 74 79 70 65 20 6c 6f 63 6b 74 79 70 65 20 f type locktype
12149 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
1214a 66 69 6c 65 2e 20 49 66 0a 2a 2a 20 61 20 73 69 file. If.** a si
1214b 6d 69 6c 61 72 20 6f 72 20 67 72 65 61 74 65 72 milar or greater
1214c 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 lock is already
1214d 20 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 held, this func
1214e 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 0a tion is a no-op.
1214f 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 53 51 ** (returning SQ
12150 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 LITE_OK immediat
12151 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 ely)..**.** Othe
12152 72 77 69 73 65 2c 20 61 74 74 65 6d 70 74 20 74 rwise, attempt t
12153 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 o obtain the loc
12154 6b 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f k using sqlite3O
12155 73 4c 6f 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 sLock(). Invoke
12156 0a 2a 2a 20 74 68 65 20 62 75 73 79 20 63 61 6c .** the busy cal
12157 6c 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f 63 lback if the loc
12158 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6e k is currently n
12159 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 52 65 ot available. Re
1215a 70 65 61 74 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 peat .** until t
1215b 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b he busy callback
1215c 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 6f returns false o
1215d 72 20 75 6e 74 69 6c 20 74 68 65 20 61 74 74 65 r until the atte
1215e 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 mpt to .** obtai
1215f 6e 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 n the lock succe
12160 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 eds..**.** Retur
12161 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
12162 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 72 uccess and an er
12163 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 63 ror code if we c
12164 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 annot obtain.**
12165 74 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 the lock. If the
12166 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 lock is obtaine
12167 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 d successfully,
12168 73 65 74 20 74 68 65 20 50 61 67 65 72 2e 73 74 set the Pager.st
12169 61 74 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 ate .** variable
1216a 20 74 6f 20 6c 6f 63 6b 74 79 70 65 20 62 65 66 to locktype bef
1216b 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
1216c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
1216d 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 er_wait_on_lock(
1216e 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
1216f 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 nt locktype){.
12170 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
12171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12172 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
12173 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 ode */.. /* The
12174 20 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 OS lock values
12175 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 must be the same
12176 20 61 73 20 74 68 65 20 50 61 67 65 72 20 6c 6f as the Pager lo
12177 63 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61 ck values */. a
12178 73 73 65 72 74 28 20 50 41 47 45 52 5f 53 48 41 ssert( PAGER_SHA
12179 52 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b RED==SHARED_LOCK
1217a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 );. assert( PA
1217b 47 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52 45 GER_RESERVED==RE
1217c 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SERVED_LOCK );.
1217d 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 45 assert( PAGER_E
1217e 58 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55 53 XCLUSIVE==EXCLUS
1217f 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f IVE_LOCK );.. /
12180 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 * If the file is
12181 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 currently unloc
12182 6b 65 64 20 74 68 65 6e 20 74 68 65 20 73 69 7a ked then the siz
12183 65 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77 e must be unknow
12184 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 n */. assert( p
12185 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
12186 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 GER_SHARED || pP
12187 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
12188 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 d==0 );.. /* Ch
12189 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 eck that this is
1218a 20 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 20 either a no-op
1218b 28 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 (because the req
1218c 75 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 0a uested lock is .
1218d 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c ** already hel
1218e 64 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 d, or one of the
1218f 20 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 68 transistions th
12190 61 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 at the busy-hand
12191 6c 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 ler. ** may be
12192 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c 20 invoked during,
12193 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 according to the
12194 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 20 comment above.
12195 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 ** sqlite3Pager
12196 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 29 SetBusyhandler()
12197 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
12198 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e (pPager->state>
12199 3d 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 20 =locktype).
1219a 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 || (pPager->st
1219b 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 ate==PAGER_UNLOC
1219c 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 K && locktype==P
1219d 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 AGER_SHARED).
1219e 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e || (pPager->
1219f 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 state==PAGER_RES
121a0 45 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 70 ERVED && locktyp
121a1 65 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 e==PAGER_EXCLUSI
121a2 56 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 VE). );.. if(
121a3 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c pPager->state>=l
121a4 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 ocktype ){. r
121a5 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
121a6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b }else{. do {
121a7 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
121a8 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 te3OsLock(pPager
121a9 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b ->fd, locktype);
121aa 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d . }while( rc=
121ab 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 =SQLITE_BUSY &&
121ac 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e pPager->xBusyHan
121ad 64 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 dler(pPager->pBu
121ae 73 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b syHandlerArg) );
121af 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
121b0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
121b1 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 pPager->state =
121b2 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 (u8)locktype;.
121b3 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f IOTRACE(("LO
121b4 43 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 CK %p %d\n", pPa
121b5 67 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 0a ger, locktype)).
121b6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
121b7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
121b8 54 72 75 6e 63 61 74 65 20 74 68 65 20 69 6e 2d Truncate the in-
121b9 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 memory database
121ba 66 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e 50 file image to nP
121bb 61 67 65 20 70 61 67 65 73 2e 20 54 68 69 73 20 age pages. This
121bc 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 .** function doe
121bd 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d s not actually m
121be 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 odify the databa
121bf 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e se file on disk.
121c0 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65 74 It .** just set
121c1 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 s the internal s
121c2 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 tate of the page
121c3 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 61 74 r object so that
121c4 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61 74 the .** truncat
121c5 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 ion will be done
121c6 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e when the curren
121c7 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 t transaction is
121c8 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53 committed..*/.S
121c9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
121ca 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 id sqlite3PagerT
121cb 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67 runcateImage(Pag
121cc 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
121cd 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 nPage){. asser
121ce 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a t( pPager->dbSiz
121cf 65 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 73 65 eValid );. asse
121d0 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 rt( pPager->dbSi
121d1 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20 61 ze>=nPage );. a
121d2 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
121d3 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 tate>=PAGER_RESE
121d4 52 56 45 44 20 29 3b 0a 20 20 70 50 61 67 65 72 RVED );. pPager
121d5 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 ->dbSize = nPage
121d6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 ;.}../*.** Shutd
121d7 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 own the page cac
121d8 68 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d 65 he. Free all me
121d9 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 61 mory and close a
121da 6c 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 ll files..**.**
121db 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e If a transaction
121dc 20 77 61 73 20 69 6e 20 70 72 6f 67 72 65 73 73 was in progress
121dd 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
121de 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 ne is called, th
121df 61 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f at.** transactio
121e0 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b n is rolled back
121e1 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 69 . All outstandi
121e2 6e 67 20 70 61 67 65 73 20 61 72 65 20 69 6e 76 ng pages are inv
121e3 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 20 alidated.** and
121e4 74 68 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 20 their memory is
121e5 66 72 65 65 64 2e 20 20 41 6e 79 20 61 74 74 65 freed. Any atte
121e6 6d 70 74 20 74 6f 20 75 73 65 20 61 20 70 61 67 mpt to use a pag
121e7 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 e associated.**
121e8 77 69 74 68 20 74 68 69 73 20 70 61 67 65 20 63 with this page c
121e9 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73 20 ache after this
121ea 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
121eb 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 will likely.**
121ec 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 65 result in a core
121ed 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 dump..**.** This
121ee 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 function always
121ef 20 73 75 63 63 65 65 64 73 2e 20 49 66 20 61 20 succeeds. If a
121f0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
121f1 63 74 69 76 65 20 61 6e 20 61 74 74 65 6d 70 74 ctive an attempt
121f2 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 72 .** is made to r
121f3 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 20 oll it back. If
121f4 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
121f5 64 75 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 during the rollb
121f6 61 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f ack .** a hot jo
121f7 75 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 66 urnal may be lef
121f8 74 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79 73 t in the filesys
121f9 74 65 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 tem but no error
121fa 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 is returned.**
121fb 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a to the caller..*
121fc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
121fd 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
121fe 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 rClose(Pager *pP
121ff 61 67 65 72 29 7b 0a 20 20 64 69 73 61 62 6c 65 ager){. disable
12200 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
12201 72 6f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 rors();. sqlite
12202 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
12203 6f 63 28 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e oc();. pPager->
12204 65 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70 errCode = 0;. p
12205 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
12206 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 70 61 67 65 Mode = 0;. page
12207 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b r_reset(pPager);
12208 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a . if( MEMDB ){.
12209 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b pager_unlock
1220a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 (pPager);. }els
1220b 65 7b 0a 20 20 20 20 2f 2a 20 53 65 74 20 50 61 e{. /* Set Pa
1220c 67 65 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 ger.journalHdr t
1220d 6f 20 2d 31 20 66 6f 72 20 74 68 65 20 62 65 6e o -1 for the ben
1220e 65 66 69 74 20 6f 66 20 74 68 65 20 70 61 67 65 efit of the page
1220f 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 0a 20 20 r_playback() .
12210 20 20 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68 20 ** call which
12211 6d 61 79 20 62 65 20 6d 61 64 65 20 66 72 6f 6d may be made from
12212 20 77 69 74 68 69 6e 20 70 61 67 65 72 55 6e 6c within pagerUnl
12213 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 ockAndRollback()
12214 2e 20 49 66 20 69 74 0a 20 20 20 20 2a 2a 20 69 . If it. ** i
12215 73 20 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20 74 s not -1, then t
12216 68 65 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 he unsynced port
12217 69 6f 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20 6a ion of an open j
12218 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 0a ournal file may.
12219 20 20 20 20 2a 2a 20 62 65 20 70 6c 61 79 65 64 ** be played
1221a 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 back into the d
1221b 61 74 61 62 61 73 65 2e 20 49 66 20 61 20 70 6f atabase. If a po
1221c 77 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 wer failure occu
1221d 72 73 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 rs while. **
1221e 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e this is happenin
1221f 67 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 g, the database
12220 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 may become corru
12221 70 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 pt.. */. p
12222 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
12223 72 20 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67 65 r = -1;. page
12224 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 rUnlockAndRollba
12225 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a ck(pPager);. }.
12226 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
12227 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e gnMalloc();. en
12228 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
12229 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 o_errors();. PA
1222a 47 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 GERTRACE(("CLOSE
1222b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
1222c 70 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 pPager)));. IOT
1222d 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c RACE(("CLOSE %p\
1222e 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 n", pPager)). s
1222f 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
12230 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c ager->fd);. sql
12231 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50 61 ite3PageFree(pPa
12232 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b ger->pTmpSpace);
12233 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
12234 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 50 Close(pPager->pP
12235 43 61 63 68 65 29 3b 0a 0a 20 20 61 73 73 65 72 Cache);.. asser
12236 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76 t( !pPager->aSav
12237 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65 epoint && !pPage
12238 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b r->pInJournal );
12239 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 . assert( !isOp
1223a 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
1223b 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 && !isOpen(pPage
1223c 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73 r->sjfd) );.. s
1223d 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 qlite3_free(pPag
1223e 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 er);. return SQ
1223f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 LITE_OK;.}..#if
12240 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
12241 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
12242 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 TE_TEST)./*.** R
12243 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e eturn the page n
12244 75 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70 umber for page p
12245 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 Pg..*/.SQLITE_PR
12246 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 IVATE Pgno sqlit
12247 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 e3PagerPagenumbe
12248 72 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a r(DbPage *pPg){.
12249 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 return pPg->pg
1224a 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a no;.}.#endif../*
1224b 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 .** Increment th
1224c 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
1224d 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a t for page pPg..
1224e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1224f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
12250 67 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70 gerRef(DbPage *p
12251 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 Pg){. sqlite3Pc
12252 61 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a acheRef(pPg);.}.
12253 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 ./*.** Sync the
12254 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 journal. In othe
12255 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 r words, make su
12256 72 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73 re all the pages
12257 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 that have.** be
12258 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 en written to th
12259 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 e journal have a
1225a 63 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20 ctually reached
1225b 74 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74 the surface of t
1225c 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63 he.** disk and c
1225d 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69 an be restored i
1225e 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
1225f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
12260 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 lback..**.** If
12261 74 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 the Pager.needSy
12262 6e 63 20 66 6c 61 67 20 69 73 20 6e 6f 74 20 73 nc flag is not s
12263 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 et, then this fu
12264 6e 63 74 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e nction is a.** n
12265 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c o-op. Otherwise,
12266 20 74 68 65 20 61 63 74 69 6f 6e 73 20 72 65 71 the actions req
12267 75 69 72 65 64 20 64 65 70 65 6e 64 20 6f 6e 20 uired depend on
12268 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 the journal-mode
12269 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 65 76 69 .** and the devi
1226a 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 ce characteristi
1226b 63 73 20 6f 66 20 74 68 65 20 74 68 65 20 66 69 cs of the the fi
1226c 6c 65 2d 73 79 73 74 65 6d 2c 20 61 73 20 66 6f le-system, as fo
1226d 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a llows:.**.** *
1226e 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
1226f 66 69 6c 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65 file is an in-me
12270 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c mory journal fil
12271 65 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65 e, no action nee
12272 64 0a 2a 2a 20 20 20 20 20 62 65 20 74 61 6b 65 d.** be take
12273 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68 n..**.** * Oth
12274 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 64 erwise, if the d
12275 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 evice does not s
12276 75 70 70 6f 72 74 20 74 68 65 20 53 41 46 45 5f upport the SAFE_
12277 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2c APPEND property,
12278 0a 2a 2a 20 20 20 20 20 74 68 65 6e 20 74 68 65 .** then the
12279 20 6e 52 65 63 20 66 69 65 6c 64 20 6f 66 20 74 nRec field of t
1227a 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
1227b 20 77 72 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c written journal
1227c 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 69 header.** i
1227d 73 20 75 70 64 61 74 65 64 20 74 6f 20 63 6f 6e s updated to con
1227e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 tain the number
1227f 6f 66 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 of journal recor
12280 64 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 ds that have.**
12281 20 20 20 20 62 65 65 6e 20 77 72 69 74 74 65 6e been written
12282 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49 following it. I
12283 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6f f the pager is o
12284 70 65 72 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c perating in full
12285 2d 73 79 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64 -sync.** mod
12286 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 e, then the jour
12287 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 nal file is sync
12288 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 ed before this f
12289 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2e ield is updated.
1228a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 .**.** * If th
1228b 65 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e 6f e device does no
1228c 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 53 45 t support the SE
1228d 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 QUENTIAL propert
1228e 79 2c 20 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20 y, then .**
1228f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
12290 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 synced..**.** Or
12291 2c 20 69 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65 , in pseudo-code
12292 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f :.**.** if( NO
12293 54 20 3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 T <in-memory jou
12294 72 6e 61 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20 rnal> ){.**
12295 69 66 28 20 4e 4f 54 20 53 41 46 45 5f 41 50 50 if( NOT SAFE_APP
12296 45 4e 44 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20 END ){.**
12297 69 66 28 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d if( <full-sync m
12298 6f 64 65 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f ode> ) xSync(<jo
12299 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a urnal file>);.**
1229a 20 20 20 20 20 20 20 3c 75 70 64 61 74 65 20 6e <update n
1229b 52 65 63 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20 Rec field>.**
1229c 20 20 7d 20 0a 2a 2a 20 20 20 20 20 69 66 28 20 } .** if(
1229d 4e 4f 54 20 53 45 51 55 45 4e 54 49 41 4c 20 29 NOT SEQUENTIAL )
1229e 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 xSync(<journal
1229f 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a file>);.** }.*
122a0 2a 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 6e *.** The Pager.n
122a1 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 eedSync flag is
122a2 6e 65 76 65 72 20 62 65 20 73 65 74 20 66 6f 72 never be set for
122a3 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
122a4 2c 20 6f 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65 , or any.** file
122a5 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f operating in no
122a6 2d 73 79 6e 63 20 6d 6f 64 65 20 28 50 61 67 65 -sync mode (Page
122a7 72 2e 6e 6f 53 79 6e 63 20 73 65 74 20 74 6f 20 r.noSync set to
122a8 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a non-zero)..**.**
122a9 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
122aa 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 this routine cle
122ab 61 72 73 20 74 68 65 20 50 47 48 44 52 5f 4e 45 ars the PGHDR_NE
122ac 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 66 20 ED_SYNC flag of
122ad 65 76 65 72 79 20 0a 2a 2a 20 70 61 67 65 20 63 every .** page c
122ae 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e urrently held in
122af 20 6d 65 6d 6f 72 79 20 62 65 66 6f 72 65 20 72 memory before r
122b0 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f eturning SQLITE_
122b1 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 OK. If an IO.**
122b2 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 error is encount
122b3 65 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 49 ered, then the I
122b4 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
122b5 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
122b6 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 caller..*/.stati
122b7 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 c int syncJourna
122b8 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 l(Pager *pPager)
122b9 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e {. if( pPager->
122ba 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 needSync ){.
122bb 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d assert( !pPager-
122bc 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 >tempFile );.
122bd 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
122be 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f rnalMode!=PAGER_
122bf 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
122c0 52 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 RY ){. int
122c1 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
122c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
122c3 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
122c4 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 */. const i
122c5 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 nt iDc = sqlite3
122c6 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
122c7 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e ristics(pPager->
122c8 66 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 fd);. asser
122c9 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
122ca 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 ->jfd) );..
122cb 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c if( 0==(iDc&SQL
122cc 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
122cd 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 PPEND) ){.
122ce 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 69 4e /* Variable iN
122cf 52 65 63 4f 66 66 73 65 74 20 69 73 20 73 65 74 RecOffset is set
122d0 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 to the offset i
122d1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
122d2 6c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 le. ** of
122d3 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 the nRec field
122d4 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
122d5 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 6a 6f 75 ntly written jou
122d6 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 rnal header..
122d7 20 20 20 20 20 2a 2a 20 54 68 69 73 20 66 69 65 ** This fie
122d8 6c 64 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 ld will be updat
122d9 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ed following the
122da 20 78 53 79 6e 63 28 29 20 6f 70 65 72 61 74 69 xSync() operati
122db 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e on. ** on
122dc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
122dd 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 e. */. i6
122de 34 20 69 4e 52 65 63 4f 66 66 73 65 74 20 3d 20 4 iNRecOffset =
122df 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
122e0 64 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75 dr + sizeof(aJou
122e1 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 0a 20 20 20 rnalMagic);..
122e2 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f /* This blo
122e3 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 61 6e ck deals with an
122e4 20 6f 62 73 63 75 72 65 20 70 72 6f 62 6c 65 6d obscure problem
122e5 2e 20 49 66 20 74 68 65 20 6c 61 73 74 20 63 6f . If the last co
122e6 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 nnection.
122e7 20 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 20 74 ** that wrote t
122e8 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 o this database
122e9 77 61 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e was operating in
122ea 20 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72 persistent-jour
122eb 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d nal. ** m
122ec 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f ode, then the jo
122ed 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 61 urnal file may a
122ee 74 20 74 68 69 73 20 70 6f 69 6e 74 20 61 63 74 t this point act
122ef 75 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 72 0a ually be larger.
122f0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 ** than
122f1 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 Pager.journalOff
122f2 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 20 6e bytes. If the n
122f3 65 78 74 20 74 68 69 6e 67 20 69 6e 20 74 68 65 ext thing in the
122f4 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 journal.
122f5 20 2a 2a 20 66 69 6c 65 20 68 61 70 70 65 6e 73 ** file happens
122f6 20 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e 61 6c to be a journal
122f7 2d 68 65 61 64 65 72 20 28 77 72 69 74 74 65 6e -header (written
122f8 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a as part of the.
122f9 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69 ** previ
122fa 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ous connections
122fb 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 61 6e transaction), an
122fc 64 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f 77 d a crash or pow
122fd 65 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 20 20 er-failure .
122fe 20 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 66 ** occurs af
122ff 74 65 72 20 6e 52 65 63 20 69 73 20 75 70 64 61 ter nRec is upda
12300 74 65 64 20 62 75 74 20 62 65 66 6f 72 65 20 74 ted but before t
12301 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 his connection w
12302 72 69 74 65 73 20 0a 20 20 20 20 20 20 20 20 2a rites . *
12303 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 * anything else
12304 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
12305 69 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 73 2f ile (or commits/
12306 72 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 20 0a rolls back its .
12307 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 ** trans
12308 61 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 53 51 action), then SQ
12309 4c 69 74 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 Lite may become
1230a 63 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 64 6f confused when do
1230b 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20 ing the .
1230c 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 ** hot-journal
1230d 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 rollback followi
1230e 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 49 74 20 ng recovery. It
1230f 6d 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6c may roll back al
12310 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 l. ** of
12311 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 this connections
12312 20 64 61 74 61 2c 20 74 68 65 6e 20 70 72 6f 63 data, then proc
12313 65 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 eed to rolling b
12314 61 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 20 20 ack the old,.
12315 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 ** out-of-d
12316 61 74 65 20 64 61 74 61 20 74 68 61 74 20 66 6f ate data that fo
12317 6c 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 62 61 llows it. Databa
12318 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 se corruption..
12319 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
1231a 20 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f ** To work aro
1231b 75 6e 64 20 74 68 69 73 2c 20 69 66 20 74 68 65 und this, if the
1231c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f journal file do
1231d 65 73 20 61 70 70 65 61 72 20 74 6f 20 63 6f 6e es appear to con
1231e 74 61 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 tain. **
1231f 61 20 76 61 6c 69 64 20 68 65 61 64 65 72 20 66 a valid header f
12320 6f 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a ollowing Pager.j
12321 6f 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20 ournalOff, then
12322 77 72 69 74 65 20 61 20 30 78 30 30 0a 20 20 20 write a 0x00.
12323 20 20 20 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 ** byte to
12324 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 20 the start of it
12325 74 6f 20 70 72 65 76 65 6e 74 20 69 74 20 66 72 to prevent it fr
12326 6f 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 6e 69 om being recogni
12327 7a 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a zed.. **.
12328 20 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69 61 ** Varia
12329 62 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66 73 ble iNextHdrOffs
1232a 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 et is set to the
1232b 20 6f 66 66 73 65 74 20 61 74 20 77 68 69 63 68 offset at which
1232c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a this. **
1232d 20 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65 61 problematic hea
1232e 64 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c 20 der will occur,
1232f 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 61 4d if it exists. aM
12330 61 67 69 63 20 69 73 20 75 73 65 64 20 0a 20 20 agic is used .
12331 20 20 20 20 20 20 2a 2a 20 61 73 20 61 20 74 65 ** as a te
12332 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 74 mporary buffer t
12333 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 66 69 o inspect the fi
12334 72 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 62 79 rst couple of by
12335 74 65 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a tes of. *
12336 2a 20 74 68 65 20 70 6f 74 65 6e 74 69 61 6c 20 * the potential
12337 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a journal header..
12338 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
12339 20 20 20 69 36 34 20 69 4e 65 78 74 48 64 72 4f i64 iNextHdrO
1233a 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 ffset = journalH
1233b 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 drOffset(pPager)
1233c 3b 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d 61 ;. u8 aMa
1233d 67 69 63 5b 38 5d 3b 0a 20 20 20 20 20 20 20 20 gic[8];.
1233e 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
1233f 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ad(pPager->jfd,
12340 61 4d 61 67 69 63 2c 20 38 2c 20 69 4e 65 78 74 aMagic, 8, iNext
12341 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 HdrOffset);.
12342 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12343 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63 TE_OK && 0==memc
12344 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 mp(aMagic, aJour
12345 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a nalMagic, 8) ){.
12346 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 static
12347 20 63 6f 6e 73 74 20 75 38 20 7a 65 72 6f 62 79 const u8 zeroby
12348 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 te = 0;.
12349 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1234a 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 Write(pPager->jf
1234b 64 2c 20 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c d, &zerobyte, 1,
1234c 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 iNextHdrOffset)
1234d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1234e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1234f 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c TE_OK && rc!=SQL
12350 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
12351 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20 READ ){.
12352 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
12353 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 }..
12354 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 /* Write the nRe
12355 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 c value into the
12356 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 journal file he
12357 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 20 20 ader. If in.
12358 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63 ** full-sync
12359 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79 hronous mode, sy
1235a 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
1235b 69 72 73 74 2e 20 54 68 69 73 20 65 6e 73 75 72 irst. This ensur
1235c 65 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 es that.
1235d 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 73 20 ** all data has
1235e 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65 20 64 really hit the d
1235f 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 63 20 isk before nRec
12360 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61 is updated to ma
12361 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 rk. ** it
12362 20 61 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 as a candidate
12363 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 for rollback..
12364 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
12365 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 ** This is not
12366 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65 20 required if the
12367 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61 persistent media
12368 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a 20 20 supports the.
12369 20 20 20 20 20 20 2a 2a 20 53 41 46 45 5f 41 50 ** SAFE_AP
1236a 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2e 20 42 PEND property. B
1236b 65 63 61 75 73 65 20 69 6e 20 74 68 69 73 20 63 ecause in this c
1236c 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 70 6f ase it is not po
1236d 73 73 69 62 6c 65 20 0a 20 20 20 20 20 20 20 20 ssible .
1236e 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 65 20 64 ** for garbage d
1236f 61 74 61 20 74 6f 20 62 65 20 61 70 70 65 6e 64 ata to be append
12370 65 64 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 ed to the file,
12371 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a 20 the nRec field.
12372 20 20 20 20 20 20 20 2a 2a 20 69 73 20 70 6f 70 ** is pop
12373 75 6c 61 74 65 64 20 77 69 74 68 20 30 78 46 46 ulated with 0xFF
12374 46 46 46 46 46 46 20 77 68 65 6e 20 74 68 65 20 FFFFFF when the
12375 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
12376 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20 s written.
12377 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e ** and never n
12378 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 eeds to be updat
12379 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ed.. */.
1237a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
1237b 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 30 r->fullSync && 0
1237c 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f ==(iDc&SQLITE_IO
1237d 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 CAP_SEQUENTIAL)
1237e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41 47 ){. PAG
1237f 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a ERTRACE(("SYNC j
12380 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c ournal of %d\n",
12381 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
12382 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f ));. IO
12383 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70 TRACE(("JSYNC %p
12384 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 \n", pPager)).
12385 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
12386 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 ite3OsSync(pPage
12387 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e r->jfd, pPager->
12388 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 sync_flags);.
12389 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1238a 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
1238b 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
1238c 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 IOTRACE(
1238d 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 ("JHDR %p %lld %
1238e 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 69 4e d\n", pPager, iN
1238f 52 65 63 4f 66 66 73 65 74 2c 20 34 29 29 3b 0a RecOffset, 4));.
12390 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 rc = wri
12391 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d te32bits(pPager-
12392 3e 6a 66 64 2c 20 69 4e 52 65 63 4f 66 66 73 65 >jfd, iNRecOffse
12393 74 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 t, pPager->nRec)
12394 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
12395 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
12396 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
12397 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 . if( 0==(i
12398 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f Dc&SQLITE_IOCAP_
12399 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 SEQUENTIAL) ){.
1239a 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
1239b 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c E(("SYNC journal
1239c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 of %d\n", PAGER
1239d 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
1239e 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
1239f 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 JSYNC %p\n", pPa
123a0 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 ger)). rc
123a1 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
123a2 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
123a3 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 ager->sync_flags
123a4 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 | . (pP
123a5 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 ager->sync_flags
123a6 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 ==SQLITE_SYNC_FU
123a7 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 LL?SQLITE_SYNC_D
123a8 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 ATAONLY:0).
123a9 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 );. if
123aa 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
123ab 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
123ac 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
123ad 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
123ae 69 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 ile was just suc
123af 63 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 cessfully synced
123b0 2e 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 . Set Pager.need
123b1 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 Sync . ** to
123b2 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 zero and clear t
123b3 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
123b4 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 NC flag on all p
123b5 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 agess.. */.
123b6 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 pPager->needSy
123b7 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 nc = 0;. pPag
123b8 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 er->journalStart
123b9 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 ed = 1;. sqli
123ba 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 te3PcacheClearSy
123bb 6e 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e ncFlags(pPager->
123bc 70 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 pPCache);. }..
123bd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
123be 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K;.}../*.** The
123bf 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
123c0 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 first in a linke
123c1 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 d list of dirty
123c2 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a pages connected.
123c3 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e ** by the PgHdr.
123c4 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 pDirty pointer.
123c5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 This function wr
123c6 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 ites each one of
123c7 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 the.** in-memor
123c8 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c y pages in the l
123c9 69 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 ist to the datab
123ca 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 ase file. The ar
123cb 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 gument may.** be
123cc 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 NULL, represent
123cd 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 ing an empty lis
123ce 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 t. In this case
123cf 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
123d0 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a .** a no-op..**.
123d1 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 ** The pager mus
123d2 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 t hold at least
123d3 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
123d4 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
123d5 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e on.** is called.
123d6 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 Before writing
123d7 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 anything to the
123d8 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
123d9 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 his lock.** is u
123da 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 pgraded to an EX
123db 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 CLUSIVE lock. If
123dc 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 the lock cannot
123dd 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a be obtained,.**
123de 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
123df 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
123e0 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 data is written
123e1 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
123e2 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 file..** .** If
123e3 74 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 the pager is a t
123e4 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 emp-file pager a
123e5 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 nd the actual fi
123e6 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a le-system file.*
123e7 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 * is not yet ope
123e8 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 n, it is created
123e9 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f and opened befo
123ea 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a re any data is .
123eb 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a ** written out..
123ec 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c **.** Once the l
123ed 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 ock has been upg
123ee 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 raded and, if ne
123ef 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c cessary, the fil
123f0 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 e opened,.** the
123f1 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 pages are writt
123f2 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 en out to the da
123f3 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c tabase file in l
123f4 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 ist order. Writi
123f5 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 ng.** a page is
123f6 73 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 skipped if it me
123f7 65 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 ets either of th
123f8 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 e following crit
123f9 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 eria:.**.** *
123fa 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 The page number
123fb 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
123fc 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 Pager.dbSize, or
123fd 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 .** * The PGHD
123fe 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 R_DONT_WRITE fla
123ff 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 g is set on the
12400 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 page..**.** If w
12401 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 riting out a pag
12402 65 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 e causes the dat
12403 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 abase file to gr
12404 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 ow, Pager.dbFile
12405 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 Size.** is updat
12406 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 ed accordingly.
12407 49 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 If page 1 is wri
12408 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 tten out, then t
12409 68 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a he value cached.
1240a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 ** in Pager.dbFi
1240b 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 leVers[] is upda
1240c 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 ted to match the
1240d 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 new value store
1240e 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 d in.** the data
1240f 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
12410 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 If everything i
12411 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 s successful, SQ
12412 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
12413 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 ned. If an IO er
12414 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 ror .** occurs,
12415 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
12416 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 is returned. Or
12417 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 , if the EXCLUSI
12418 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a VE lock cannot.*
12419 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 * be obtained, S
1241a 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 QLITE_BUSY is re
1241b 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
1241c 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 c int pager_writ
1241d 65 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 e_pagelist(PgHdr
1241e 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 *pList){. Page
1241f 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 r *pPager;
12420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12421 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 /* Pager object
12422 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
12423 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12424 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
12425 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 turn code */..
12426 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 if( pList==0 ) r
12427 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
12428 0a 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73 . pPager = pLis
12429 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a t->pPager;.. /*
1242a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 At this point t
1242b 68 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 68 here may be eith
1242c 65 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 er a RESERVED or
1242d 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
1242e 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 on the. ** data
1242f 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 base file. If th
12430 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ere is already a
12431 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
12432 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a , the following.
12433 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20 61 20 6e ** call is a n
12434 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 o-op.. **. **
12435 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 Moving the lock
12436 66 72 6f 6d 20 52 45 53 45 52 56 45 44 20 74 6f from RESERVED to
12437 20 45 58 43 4c 55 53 49 56 45 20 61 63 74 75 61 EXCLUSIVE actua
12438 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 6f 69 lly involves goi
12439 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 ng. ** through
1243a 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 an intermediate
1243b 73 74 61 74 65 20 50 45 4e 44 49 4e 47 2e 20 20 state PENDING.
1243c 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 A PENDING lock
1243d 70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 20 2a prevents new. *
1243e 2a 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20 61 * readers from a
1243f 74 74 61 63 68 69 6e 67 20 74 6f 20 74 68 65 20 ttaching to the
12440 64 61 74 61 62 61 73 65 20 62 75 74 20 69 73 20 database but is
12441 75 6e 73 75 66 66 69 63 69 65 6e 74 20 66 6f 72 unsufficient for
12442 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 69 74 us to. ** writ
12443 65 2e 20 20 54 68 65 20 69 64 65 61 20 6f 66 20 e. The idea of
12444 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 a PENDING lock i
12445 73 20 74 6f 20 70 72 65 76 65 6e 74 20 6e 65 77 s to prevent new
12446 20 72 65 61 64 65 72 73 20 66 72 6f 6d 0a 20 20 readers from.
12447 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 68 69 ** coming in whi
12448 6c 65 20 77 65 20 77 61 69 74 20 66 6f 72 20 65 le we wait for e
12449 78 69 73 74 69 6e 67 20 72 65 61 64 65 72 73 20 xisting readers
1244a 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a 20 to clear.. **.
1244b 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 70 61 ** While the pa
1244c 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 52 45 ger is in the RE
1244d 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 SERVED state, th
1244e 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 e original datab
1244f 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 ase file. ** is
12450 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 77 unchanged and w
12451 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 77 e can rollback w
12452 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
12453 20 70 6c 61 79 62 61 63 6b 20 74 68 65 0a 20 20 playback the.
12454 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20 ** journal into
12455 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 the original dat
12456 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4f 6e 63 abase file. Onc
12457 65 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 e we transition
12458 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 to. ** EXCLUSIV
12459 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 E, it means the
1245a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
1245b 73 20 62 65 65 6e 20 63 68 61 6e 67 65 64 20 61 s been changed a
1245c 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 0a nd any rollback.
1245d 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 69 72 ** will requir
1245e 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 e a journal play
1245f 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 back.. */. ass
12460 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
12461 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te>=PAGER_RESERV
12462 45 44 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 ED );. rc = pag
12463 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 er_wait_on_lock(
12464 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 pPager, EXCLUSIV
12465 45 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20 49 E_LOCK);.. /* I
12466 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 61 20 f the file is a
12467 74 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 6e 6f temp-file has no
12468 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 t yet been opene
12469 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 d, open it now.
1246a 49 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 It. ** is not p
1246b 6f 73 73 69 62 6c 65 20 66 6f 72 20 72 63 20 74 ossible for rc t
1246c 6f 20 62 65 20 6f 74 68 65 72 20 74 68 61 6e 20 o be other than
1246d 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 69 SQLITE_OK if thi
1246e 73 20 62 72 61 6e 63 68 0a 20 20 2a 2a 20 69 73 s branch. ** is
1246f 20 74 61 6b 65 6e 2c 20 61 73 20 70 61 67 65 72 taken, as pager
12470 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20 _wait_on_lock()
12471 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 is a no-op for t
12472 65 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a emp-files.. */.
12473 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 if( !isOpen(pP
12474 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 ager->fd) ){.
12475 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12476 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 72 63 3d >tempFile && rc=
12477 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
12478 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e rc = pagerOpen
12479 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 temp(pPager, pPa
1247a 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d ger->fd, pPager-
1247b 3e 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 7d 0a >vfsFlags);. }.
1247c 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 . while( rc==SQ
1247d 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 73 74 LITE_OK && pList
1247e 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e ){. Pgno pgn
1247f 6f 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b o = pList->pgno;
12480 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 .. /* If ther
12481 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67 65 e are dirty page
12482 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 s in the page ca
12483 63 68 65 20 77 69 74 68 20 70 61 67 65 20 6e 75 che with page nu
12484 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a 20 20 mbers greater.
12485 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e ** than Pager.
12486 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65 61 dbSize, this mea
12487 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 ns sqlite3PagerT
12488 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 77 runcateImage() w
12489 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 as called to.
1248a 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c ** make the fil
1248b 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 e smaller (presu
1248c 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 mably by auto-va
1248d 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e cuum code). Do n
1248e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 ot write. **
1248f 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 74 any such pages t
12490 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 o the file..
12491 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 **. ** Also,
12492 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f 75 74 do not write out
12493 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 68 any page that h
12494 61 73 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e as the PGHDR_DON
12495 54 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 20 20 T_WRITE flag.
12496 20 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79 20 ** set (set by
12497 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
12498 57 72 69 74 65 28 29 29 2e 0a 20 20 20 20 2a 2f Write()).. */
12499 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 . if( pgno<=p
1249a 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 Pager->dbSize &&
1249b 20 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 0==(pList->flag
1249c 73 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 s&PGHDR_DONT_WRI
1249d 54 45 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 TE) ){. i64
1249e 20 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d offset = (pgno-
1249f 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 1)*(i64)pPager->
124a0 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 pageSize;
124a1 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77 /* Offset to w
124a2 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68 rite */. ch
124a3 61 72 20 2a 70 44 61 74 61 20 3d 20 43 4f 44 45 ar *pData = CODE
124a4 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74 C2(pPager, pList
124a5 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36 ->pData, pgno, 6
124a6 29 3b 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72 ); /* Data to wr
124a7 69 74 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a ite */.. /*
124a8 20 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 Write out the p
124a9 61 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 age data. */.
124aa 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
124ab 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 sWrite(pPager->f
124ac 64 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 d, pData, pPager
124ad 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 ->pageSize, offs
124ae 65 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 et);.. /* I
124af 66 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 f page 1 was jus
124b0 74 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 t written, updat
124b1 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 e Pager.dbFileVe
124b2 72 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 rs to match.
124b3 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e ** the value n
124b4 6f 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 ow stored in the
124b5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
124b6 49 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 If writing this
124b7 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 . ** page c
124b8 61 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 aused the databa
124b9 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c se file to grow,
124ba 20 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 update dbFileSi
124bb 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ze. . */.
124bc 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 if( pgno==1
124bd 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
124be 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c y(&pPager->dbFil
124bf 65 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 eVers, &pData[24
124c0 5d 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 ], sizeof(pPager
124c1 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a ->dbFileVers));.
124c2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
124c3 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 ( pgno>pPager->d
124c4 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 bFileSize ){.
124c5 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 pPager->dbF
124c6 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a ileSize = pgno;.
124c7 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
124c8 2a 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 * Update any bac
124c9 6b 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 kup objects copy
124ca 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ing the contents
124cb 20 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 of this pager.
124cc 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
124cd 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 BackupUpdate(pPa
124ce 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 ger->pBackup, pg
124cf 6e 6f 2c 20 28 75 38 20 2a 29 70 44 61 74 61 29 no, (u8 *)pData)
124d0 3b 0a 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 ;.. PAGERTR
124d1 41 43 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 ACE(("STORE %d p
124d2 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 age %d hash(%08x
124d3 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 )\n",.
124d4 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 PAGERID
124d5 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 (pPager), pgno,
124d6 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
124d7 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 List)));. I
124d8 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 OTRACE(("PGOUT %
124d9 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
124da 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 pgno));. P
124db 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 AGER_INCR(sqlite
124dc 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 3_pager_writedb_
124dd 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 count);. PA
124de 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d GER_INCR(pPager-
124df 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 >nWrite);. }e
124e0 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 lse{. PAGER
124e1 54 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 TRACE(("NOSTORE
124e2 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 %d page %d\n", P
124e3 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
124e4 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 pgno));. }.#i
124e5 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
124e6 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 K_PAGES. pLis
124e7 74 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 t->pageHash = pa
124e8 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 ger_pagehash(pLi
124e9 73 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 st);.#endif.
124ea 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 pList = pList->p
124eb 44 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 Dirty;. }.. re
124ec 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
124ed 2a 20 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 * Append a recor
124ee 64 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 d of the current
124ef 20 73 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 state of page p
124f0 50 67 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f Pg to the sub-jo
124f1 75 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 urnal. .** It is
124f2 20 74 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 the callers res
124f3 70 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 ponsibility to u
124f4 73 65 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 se subjRequiresP
124f5 61 67 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a age() to check .
124f6 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 72 65 ** that it is re
124f7 61 6c 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 ally required be
124f8 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 fore calling thi
124f9 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a s function..**.*
124fa 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c * If successful,
124fb 20 73 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 set the bit cor
124fc 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 responding to pP
124fd 67 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 g->pgno in the b
124fe 69 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c itvecs.** for al
124ff 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 l open savepoint
12500 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 s before returni
12501 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ng..**.** This f
12502 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
12503 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 SQLITE_OK if eve
12504 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 rything is succe
12505 73 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 ssful, an IO.**
12506 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 error code if th
12507 65 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 e attempt to wri
12508 74 65 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f te to the sub-jo
12509 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 urnal fails, or
1250a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
1250b 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 if a malloc fai
1250c 6c 73 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 ls while setting
1250d 20 61 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 a bit in a save
1250e 70 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e point.** bitvec.
1250f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
12510 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 ubjournalPage(Pg
12511 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 Hdr *pPg){. int
12512 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
12513 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
12514 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
12515 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
12516 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 ger->sjfd) ){.
12517 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 void *pData =
12518 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 pPg->pData;.
12519 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 i64 offset = pPa
1251a 67 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b ger->nSubRec*(4+
1251b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
1251c 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 );. char *pDa
1251d 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50 61 ta2 = CODEC2(pPa
1251e 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d ger, pData, pPg-
1251f 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 0a 20 20 >pgno, 7);. .
12520 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 PAGERTRACE(("S
12521 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 TMT-JOURNAL %d p
12522 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 age %d\n", PAGER
12523 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
12524 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20 >pgno));. .
12525 61 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f assert( pageInJo
12526 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 urnal(pPg) || pP
12527 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e g->pgno>pPager->
12528 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 dbOrigSize );.
12529 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 rc = write32bi
1252a 74 73 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c ts(pPager->sjfd,
1252b 20 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 offset, pPg->pg
1252c 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d no);. if( rc=
1252d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1252e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1252f 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
12530 73 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 sjfd, pData2, pP
12531 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
12532 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d offset+4);. }
12533 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
12534 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12535 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b pPager->nSubRec+
12536 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 +;. assert( p
12537 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
12538 74 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 t>0 );. rc =
12539 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 addToSavepointBi
1253a 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 tvecs(pPager, pP
1253b 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 74 65 g->pgno);. te
1253c 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 stcase( rc!=SQLI
1253d 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 72 TE_OK );. }. r
1253e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
1253f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
12540 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 n is called by t
12541 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 he pcache layer
12542 77 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63 when it has reac
12543 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 hed some.** soft
12544 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 memory limit. T
12545 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
12546 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 t is a pointer t
12547 6f 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 o a Pager object
12548 0a 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20 76 .** (cast as a v
12549 6f 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65 72 oid*). The pager
1254a 20 69 73 20 61 6c 77 61 79 73 20 27 70 75 72 67 is always 'purg
1254b 65 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 eable' (not an i
1254c 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 n-memory.** data
1254d 62 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f 6e base). The secon
1254e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 d argument is a
1254f 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 70 reference to a p
12550 61 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 age that is .**
12551 63 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 20 currently dirty
12552 62 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 but has no outst
12553 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 anding reference
12554 73 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 s. The page.** i
12555 73 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69 61 s always associa
12556 74 65 64 20 77 69 74 68 20 74 68 65 20 50 61 67 ted with the Pag
12557 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 er object passed
12558 20 61 73 20 74 68 65 20 66 69 72 73 74 20 0a 2a as the first .*
12559 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a * argument..**.*
1255a 2a 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 * The job of thi
1255b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f s function is to
1255c 20 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 make pPg clean
1255d 62 79 20 77 72 69 74 69 6e 67 20 69 74 73 20 63 by writing its c
1255e 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 ontents.** out t
1255f 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
12560 69 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 ile, if possible
12561 2e 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c . This may invol
12562 76 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a ve syncing the.*
12563 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 * journal file.
12564 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 .**.** If succes
12565 73 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 sful, sqlite3Pca
12566 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 cheMakeClean() i
12567 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 s called on the
12568 70 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 page and.** SQLI
12569 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 TE_OK returned.
1256a 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f If an IO error o
1256b 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 ccurs while tryi
1256c 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a ng to make the.*
1256d 2a 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 * page clean, th
1256e 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 e IO error code
1256f 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
12570 74 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 the page cannot
12571 62 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e be.** made clean
12572 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 for some other
12573 72 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 reason, but no e
12574 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 rror occurs, the
12575 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 n SQLITE_OK.** i
12576 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 s returned by sq
12577 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
12578 6c 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 lean() is not ca
12579 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lled..*/.static
1257a 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 28 int pagerStress(
1257b 76 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 2a void *p, PgHdr *
1257c 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 pPg){. Pager *p
1257d 50 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 2a Pager = (Pager *
1257e 29 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 )p;. int rc = S
1257f 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
12580 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 ert( pPg->pPager
12581 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61 73 ==pPager );. as
12582 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 sert( pPg->flags
12583 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a &PGHDR_DIRTY );.
12584 0a 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 . /* The doNotS
12585 79 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 ync flag is set
12586 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 by the sqlite3Pa
12587 67 65 72 57 72 69 74 65 28 29 20 66 75 6e 63 74 gerWrite() funct
12588 69 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20 2a ion while it. *
12589 2a 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 * is journalling
1258a 20 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 a set of two or
1258b 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 more database p
1258c 61 67 65 73 20 74 68 61 74 20 61 72 65 20 73 74 ages that are st
1258d 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 ored. ** on the
1258e 20 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74 6f same disk secto
1258f 72 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20 6a r. Syncing the j
12590 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c ournal is not al
12591 6c 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a lowed while. **
12592 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 this is happeni
12593 6e 67 20 61 73 20 69 74 20 69 73 20 69 6d 70 6f ng as it is impo
12594 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d rtant that all m
12595 65 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20 61 embers of such a
12596 0a 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 . ** set of pag
12597 65 73 20 61 72 65 20 73 79 6e 63 65 64 20 74 6f es are synced to
12598 20 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 disk together.
12599 53 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 So, if the page
1259a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 this function.
1259b 2a 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 ** is trying to
1259c 6d 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 make clean will
1259d 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 require a journa
1259e 6c 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 64 l sync and the d
1259f 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c oNotSync. ** fl
125a0 61 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 72 ag is set, retur
125a1 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 n without doing
125a2 61 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 63 anything. The pc
125a3 61 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a ache layer will.
125a4 20 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20 74 ** just have t
125a5 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 61 o go ahead and a
125a6 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 llocate a new pa
125a7 67 65 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 ge buffer instea
125a8 64 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e d of. ** reusin
125a9 67 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a g pPg.. **. **
125aa 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 Similarly, if t
125ab 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 he pager has alr
125ac 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 eady entered the
125ad 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f error state, do
125ae 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f not. ** try to
125af 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 write the conte
125b0 6e 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64 69 nts of pPg to di
125b1 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 sk.. */. if( p
125b2 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 7c Pager->errCode |
125b3 7c 20 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 | (pPager->doNot
125b4 53 79 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 Sync && pPg->fla
125b5 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
125b6 4e 43 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 NC) ){. retur
125b7 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
125b8 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 .. /* Sync the
125b9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 journal file if
125ba 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
125bb 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 f( pPg->flags&PG
125bc 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b HDR_NEED_SYNC ){
125bd 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f . rc = syncJo
125be 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
125bf 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
125c0 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
125c1 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 fullSync && .
125c2 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 !(pPager->jou
125c3 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
125c4 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
125c5 52 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73 RY) &&. !(s
125c6 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
125c7 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 aracteristics(pP
125c8 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 ager->fd)&SQLITE
125c9 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
125ca 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ND). ){.
125cb 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 pPager->nRec =
125cc 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 0;. rc = wr
125cd 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 iteJournalHdr(pP
125ce 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ager);. }. }
125cf 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
125d0 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 ge number of thi
125d1 73 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72 s page is larger
125d2 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e than the curren
125d3 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74 t size of. ** t
125d4 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 he database imag
125d5 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 e, it may need t
125d6 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 o be written to
125d7 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
125d8 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 . ** This is be
125d9 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 cause the call t
125da 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 o pager_write_pa
125db 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77 gelist() below w
125dc 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74 ill not. ** act
125dd 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61 ually write data
125de 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20 to the file in
125df 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a this case.. **.
125e0 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 ** Consider th
125e1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75 e following sequ
125e2 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a ence of events:.
125e3 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 **. ** BEGI
125e4 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 N;. ** <jou
125e5 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a rnal page X>. *
125e6 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 * <modify pa
125e7 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53 ge X>. ** S
125e8 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a AVEPOINT sp;. *
125e9 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 * <shrink
125ea 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
125eb 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20 Y pages>. **
125ec 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73 pagerStress
125ed 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20 (page X). **
125ee 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 ROLLBACK TO sp
125ef 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28 ;. **. ** If (
125f0 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 X>Y), then when
125f1 70 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63 pagerStress is c
125f2 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c alled page X wil
125f3 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e l not be written
125f4 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 . ** out to the
125f5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
125f6 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 but will be drop
125f7 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 ped from the cac
125f8 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66 he. Then,. ** f
125f9 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f ollowing the "RO
125fa 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74 LLBACK TO sp" st
125fb 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 atement, reading
125fc 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61 page X will rea
125fd 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d d. ** data from
125fe 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
125ff 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 le. This will be
12600 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67 the copy of pag
12601 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77 e X as it. ** w
12602 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e as when the tran
12603 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c saction started,
12604 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77 not as it was w
12605 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73 hen "SAVEPOINT s
12606 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63 p". ** was exec
12607 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 uted.. **. **
12608 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
12609 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72 to write the cur
1260a 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61 rent data for pa
1260b 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20 ge X into the .
1260c 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 ** sub-journal
1260d 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20 file now (if it
1260e 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 is not already t
1260f 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69 here), so that i
12610 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 t will. ** be r
12611 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63 estored to its c
12612 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65 urrent value whe
12613 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 n the "ROLLBACK
12614 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20 TO sp" is . **
12615 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20 executed.. */.
12616 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
12617 4f 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e OK && pPg->pgno>
12618 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 pPager->dbSize &
12619 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 & subjRequiresPa
1261a 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 72 ge(pPg) ){. r
1261b 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 c = subjournalPa
1261c 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 ge(pPg);. }..
1261d 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e /* Write the con
1261e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 tents of the pag
1261f 65 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 e out to the dat
12620 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 abase file. */.
12621 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
12622 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 70 OK ){. pPg->p
12623 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 Dirty = 0;. r
12624 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f c = pager_write_
12625 70 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20 pagelist(pPg);.
12626 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 }.. /* Mark th
12627 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e e page as clean.
12628 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
12629 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 LITE_OK ){. P
1262a 41 47 45 52 54 52 41 43 45 28 28 22 53 54 52 45 AGERTRACE(("STRE
1262b 53 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 SS %d page %d\n"
1262c 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 , PAGERID(pPager
1262d 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a ), pPg->pgno));.
1262e 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
1262f 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b eMakeClean(pPg);
12630 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 . }.. return p
12631 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
12632 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a r, rc);.}.../*.*
12633 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 * Allocate and i
12634 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20 nitialize a new
12635 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64 Pager object and
12636 20 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74 put a pointer t
12637 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 o it.** in *ppPa
12638 67 65 72 2e 20 54 68 65 20 70 61 67 65 72 20 73 ger. The pager s
12639 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 hould eventually
1263a 20 62 65 20 66 72 65 65 64 20 62 79 20 70 61 73 be freed by pas
1263b 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 71 sing it.** to sq
1263c 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 lite3PagerClose(
1263d 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 )..**.** The zFi
1263e 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 lename argument
1263f 69 73 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 is the path to t
12640 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12641 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 to open..** If
12642 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c zFilename is NUL
12643 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c L then a randoml
12644 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 y-named temporar
12645 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 y file is create
12646 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73 d.** and used as
12647 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 the file to be
12648 63 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 72 cached. Temporar
12649 79 20 66 69 6c 65 73 20 61 72 65 20 62 65 20 64 y files are be d
1264a 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 eleted.** automa
1264b 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 tically when the
1264c 79 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 66 y are closed. If
1264d 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a zFilename is ":
1264e 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a 2a memory:" then .*
1264f 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f * all informatio
12650 6e 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61 63 n is held in cac
12651 68 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72 20 he. It is never
12652 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e written to disk.
12653 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 65 .** This can be
12654 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
12655 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 nt an in-memory
12656 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
12657 54 68 65 20 6e 45 78 74 72 61 20 70 61 72 61 6d The nExtra param
12658 65 74 65 72 20 73 70 65 63 69 66 69 65 73 20 74 eter specifies t
12659 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
1265a 65 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f es of space allo
1265b 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 cated.** along w
1265c 69 74 68 20 65 61 63 68 20 70 61 67 65 20 72 65 ith each page re
1265d 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73 70 ference. This sp
1265e 61 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ace is available
1265f 20 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 to the user.**
12660 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 50 via the sqlite3P
12661 61 67 65 72 47 65 74 45 78 74 72 61 28 29 20 41 agerGetExtra() A
12662 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c PI..**.** The fl
12663 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ags argument is
12664 75 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 20 used to specify
12665 70 72 6f 70 65 72 74 69 65 73 20 74 68 61 74 20 properties that
12666 61 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f 70 affect the.** op
12667 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 eration of the p
12668 61 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 ager. It should
12669 62 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62 be passed some b
1266a 69 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69 itwise combinati
1266b 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47 on.** of the PAG
1266c 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 ER_OMIT_JOURNAL
1266d 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 and PAGER_NO_REA
1266e 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a DLOCK flags..**.
1266f 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73 20 ** The vfsFlags
12670 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 parameter is a b
12671 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 74 itmask to pass t
12672 6f 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 o the flags para
12673 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 meter.** of the
12674 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f xOpen() method o
12675 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 56 f the supplied V
12676 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 FS when opening
12677 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 files. .**.** If
12678 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 the pager objec
12679 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 t is allocated a
1267a 6e 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64 nd the specified
1267b 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a 2a file opened .**
1267c 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 53 successfully, S
1267d 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
1267e 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 rned and *ppPage
1267f 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 r set to point t
12680 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 67 o.** the new pag
12681 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 6e er object. If an
12682 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a error occurs, *
12683 70 70 50 61 67 65 72 20 69 73 20 73 65 74 20 74 ppPager is set t
12684 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 o NULL.** and er
12685 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 ror code returne
12686 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e d. This function
12687 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
12688 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c TE_NOMEM.** (sql
12689 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 20 ite3Malloc() is
1268a 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 used to allocate
1268b 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 45 memory), SQLITE
1268c 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a _CANTOPEN or .**
1268d 20 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45 5f various SQLITE_
1268e 49 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a 2a IO_XXX errors..*
1268f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
12690 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
12691 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 rOpen(. sqlite3
12692 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
12693 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c /* The virtual
12694 20 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f 20 file system to
12695 75 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a use */. Pager *
12696 2a 70 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *ppPager,
12697 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e /* OUT: Return
12698 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 the Pager struc
12699 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 ture here */. c
1269a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
1269b 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 name, /* Name
1269c 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1269d 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
1269e 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 int nExtra,
1269f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
126a0 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 tra bytes append
126a1 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f to each in-memo
126a2 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ry page */. int
126a3 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
126a4 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 /* flags c
126a5 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 ontrolling this
126a6 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 file */. int vf
126a7 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 sFlags
126a8 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73 /* flags pass
126a9 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 ed through to sq
126aa 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
126ab 29 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50 ) */.){. u8 *pP
126ac 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 tr;. Pager *pPa
126ad 67 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f ger = 0; /
126ae 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 74 * Pager object t
126af 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 o allocate and r
126b0 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 eturn */. int r
126b1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
126b2 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
126b3 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d 70 de */. int temp
126b4 46 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20 File = 0;
126b5 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65 6d /* True for tem
126b6 70 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 69 p files (incl. i
126b7 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 20 n-memory files)
126b8 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d */. int memDb =
126b9 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
126ba 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 True if this is
126bb 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 an in-memory fi
126bc 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 64 le */. int read
126bd 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 20 Only = 0;
126be 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
126bf 20 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 is a read-only
126c0 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f file */. int jo
126c1 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 20 urnalFileSize;
126c2 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 /* Bytes to a
126c3 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63 68 llocate for each
126c4 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 journal fd */.
126c5 20 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 char *zPathname
126c6 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 6c = 0; /* Ful
126c7 6c 20 70 61 74 68 20 74 6f 20 64 61 74 61 62 61 l path to databa
126c8 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 se file */. int
126c9 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 nPathname = 0;
126ca 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
126cb 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61 74 of bytes in zPat
126cc 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 75 hname */. int u
126cd 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 seJournal = (fla
126ce 67 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f gs & PAGER_OMIT_
126cf 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 JOURNAL)==0; /*
126d0 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f False to omit jo
126d1 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e urnal */. int n
126d2 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 oReadlock = (fla
126d3 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 gs & PAGER_NO_RE
126d4 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 ADLOCK)!=0; /*
126d5 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 61 True to omit rea
126d6 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 d-lock */. int
126d7 70 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71 6c pcacheSize = sql
126d8 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 ite3PcacheSize()
126d9 3b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 ; /* Bytes
126da 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 to allocate for
126db 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 36 PCache */. u16
126dc 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 szPageDflt = SQ
126dd 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
126de 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61 E_SIZE; /* Defa
126df 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a 2f ult page size */
126e0 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 .. /* Figure ou
126e1 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 t how much space
126e2 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 is required for
126e3 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 69 each journal fi
126e4 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 28 le-handle. ** (
126e5 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f 66 there are two of
126e6 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e 20 them, the main
126e7 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 journal and the
126e8 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 sub-journal). Th
126e9 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 6d is. ** is the m
126ea 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65 71 aximum space req
126eb 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d uired for an in-
126ec 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 memory journal f
126ed 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a 2a ile handle . **
126ee 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 6a and a regular j
126ef 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 ournal file-hand
126f0 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20 le. Note that a
126f1 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c "regular journal
126f2 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d 61 -handle". ** ma
126f3 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20 63 y be a wrapper c
126f4 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 6e apable of cachin
126f5 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 74 g the first port
126f6 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ion of the journ
126f7 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e 20 al. ** file in
126f8 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 6d memory to implem
126f9 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 ent the atomic-w
126fa 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f rite optimizatio
126fb 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f 75 n (see . ** sou
126fc 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c rce file journal
126fd 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 .c).. */. if(
126fe 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 sqlite3JournalSi
126ff 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65 33 ze(pVfs)>sqlite3
12700 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 MemJournalSize()
12701 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 ){. journalF
12702 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 ileSize = ROUND8
12703 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 (sqlite3JournalS
12704 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d 65 ize(pVfs));. }e
12705 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c lse{. journal
12706 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 FileSize = ROUND
12707 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 8(sqlite3MemJour
12708 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d 0a nalSize());. }.
12709 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 . /* Set the ou
1270a 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74 6f tput variable to
1270b 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 6e NULL in case an
1270c 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a error occurs. *
1270d 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 30 /. *ppPager = 0
1270e 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 ;.. /* Compute
1270f 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66 75 and store the fu
12710 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 61 ll pathname in a
12711 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 n allocated buff
12712 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a 20 er pointed. **
12713 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 2c to by zPathname,
12714 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 6d length nPathnam
12715 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69 e. Or, if this i
12716 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 s a temporary fi
12717 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 62 le,. ** leave b
12718 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 6e oth nPathname an
12719 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 20 d zPathname set
1271a 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 to 0.. */. if(
1271b 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 zFilename && zF
1271c 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 ilename[0] ){.
1271d 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 nPathname = pV
1271e 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
1271f 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 20 ;. zPathname
12720 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
12721 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 nPathname*2);.
12722 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d if( zPathname=
12723 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =0 ){. retu
12724 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
12725 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
12726 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 QLITE_OMIT_MEMOR
12727 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 63 YDB. if( strc
12728 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d mp(zFilename,":m
12729 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20 emory:")==0 ){.
1272a 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a memDb = 1;.
1272b 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b zPathname[
1272c 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 0] = 0;. }els
1272d 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 e.#endif. {.
1272e 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 zPathname[0
1272f 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 73 ] = 0; /* Make s
12730 75 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 ure initialized
12731 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 68 even if FullPath
12732 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f 0a name() fails */.
12733 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12734 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e3OsFullPathname
12735 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 (pVfs, zFilename
12736 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61 , nPathname, zPa
12737 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a thname);. }..
12738 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 nPathname =
12739 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1273a 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 zPathname);.
1273b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1273c 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b 38 K && nPathname+8
1273d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d >pVfs->mxPathnam
1273e 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 e ){. /* Th
1273f 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b is branch is tak
12740 65 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 en when the jour
12741 6e 61 6c 20 70 61 74 68 20 72 65 71 75 69 72 65 nal path require
12742 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 d by. ** th
12743 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 e database being
12744 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 20 opened will be
12745 6d 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d 3e more than pVfs->
12746 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 20 mxPathname.
12747 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 6e ** bytes in len
12748 67 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 gth. This means
12749 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e the database can
1274a 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a 20 not be opened,.
1274b 20 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77 69 ** as it wi
1274c 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 ll not be possib
1274d 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 6a le to open the j
1274e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 65 ournal file or e
1274f 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 65 ven. ** che
12750 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 ck for a hot-jou
12751 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61 64 rnal before read
12752 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ing.. */.
12753 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
12754 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a CANTOPEN;. }.
12755 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
12756 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
12757 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 qlite3_free(zPat
12758 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 hname);. re
12759 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
1275a 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 }.. /* Allocat
1275b 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 e memory for the
1275c 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 Pager structure
1275d 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c , PCache object,
1275e 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 the. ** three
1275f 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
12760 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 , the database f
12761 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 ile name and the
12762 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 journal . ** f
12763 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 ile name. The la
12764 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 yout in memory i
12765 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 s as follows:.
12766 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65 **. ** Page
12767 72 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 r object
12768 20 20 20 20 20 20 20 20 20 20 20 20 28 73 69 7a (siz
12769 65 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 73 eof(Pager) bytes
1276a 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68 ). ** PCach
1276b 65 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 e object
1276c 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 (sqli
1276d 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 20 te3PcacheSize()
1276e 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 bytes). **
1276f 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 Database file ha
12770 6e 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 ndle
12771 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 (pVfs->szOsFile
12772 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 bytes). **
12773 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 Sub-journal file
12774 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 handle
12775 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 (journalFileSize
12776 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 bytes). **
12777 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 Main journal fi
12778 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 le handle
12779 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a (journalFileSiz
1277a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 e bytes). **
1277b 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 Database file
1277c 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 name
1277d 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 (nPathname+1 b
1277e 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a ytes). ** J
1277f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
12780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
12781 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 nPathname+8+1 by
12782 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 tes). */. pPtr
12783 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 = (u8 *)sqlite3
12784 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 MallocZero(.
12785 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 ROUND8(sizeof(*p
12786 50 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f Pager)) + /
12787 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 * Pager structur
12788 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 e */. ROUND8(
12789 70 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20 pcacheSize) +
1278a 20 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68 /* PCach
1278b 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 e object */.
1278c 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f ROUND8(pVfs->szO
1278d 73 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f sFile) + /
1278e 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 * The main db fi
1278f 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 le */. journa
12790 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 lFileSize * 2 +
12791 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
12792 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 two journal file
12793 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e s */ . nPathn
12794 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20 20 ame + 1 +
12795 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c /* zFil
12796 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 ename */. nPa
12797 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 20 thname + 8 + 1
12798 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a /* z
12799 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a Journal */. );.
1279a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
1279b 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 BYTE_ALIGNMENT(S
1279c 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 QLITE_INT_TO_PTR
1279d 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 (journalFileSize
1279e 29 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50 74 )) );. if( !pPt
1279f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
127a0 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 _free(zPathname)
127a1 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
127a2 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
127a3 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 20 pPager =
127a4 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 28 (Pager*)(
127a5 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d pPtr);. pPager-
127a6 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 50 >pPCache = (P
127a7 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20 Cache*)(pPtr +=
127a8 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 ROUND8(sizeof(*p
127a9 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67 Pager)));. pPag
127aa 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69 er->fd = (sqli
127ab 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 te3_file*)(pPtr
127ac 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 += ROUND8(pcache
127ad 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72 Size));. pPager
127ae 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 ->sjfd = (sqlite
127af 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 3_file*)(pPtr +=
127b0 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a ROUND8(pVfs->sz
127b1 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67 OsFile));. pPag
127b2 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69 er->jfd = (sqli
127b3 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 te3_file*)(pPtr
127b4 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 += journalFileSi
127b5 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a ze);. pPager->z
127b6 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 Filename = (c
127b7 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f har*)(pPtr += jo
127b8 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a urnalFileSize);.
127b9 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
127ba 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 BYTE_ALIGNMENT(p
127bb 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a Pager->jfd) );..
127bc 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
127bd 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 Pager.zFilename
127be 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72 and Pager.zJour
127bf 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20 nal buffers, if
127c0 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
127c1 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a f( zPathname ){.
127c2 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 pPager->zJou
127c3 72 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 rnal = (char*)
127c4 28 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 (pPtr += nPathna
127c5 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 6d me + 1);. mem
127c6 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c cpy(pPager->zFil
127c7 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 ename, zPathname
127c8 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 , nPathname);.
127c9 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d memcpy(pPager-
127ca 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 68 >zJournal, zPath
127cb 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 name, nPathname)
127cc 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 ;. memcpy(&pP
127cd 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e ager->zJournal[n
127ce 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 Pathname], "-jou
127cf 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 73 rnal", 8);. s
127d0 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 qlite3_free(zPat
127d1 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 50 hname);. }. pP
127d2 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 66 ager->pVfs = pVf
127d3 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 66 73 s;. pPager->vfs
127d4 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 67 73 Flags = vfsFlags
127d5 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 ;.. /* Open the
127d6 20 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a pager file.. *
127d7 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d /. if( zFilenam
127d8 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 e && zFilename[0
127d9 5d 20 26 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20 ] && !memDb ){.
127da 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b int fout = 0;
127db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
127dc 20 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 /* VFS flags
127dd 20 72 65 74 75 72 6e 65 64 20 62 79 20 78 4f 70 returned by xOp
127de 65 6e 28 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d en() */. rc =
127df 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
127e0 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 Vfs, pPager->zFi
127e1 6c 65 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e lename, pPager->
127e2 66 64 2c 20 76 66 73 46 6c 61 67 73 2c 20 26 66 fd, vfsFlags, &f
127e3 6f 75 74 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e out);. readOn
127e4 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 ly = (fout&SQLIT
127e5 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 E_OPEN_READONLY)
127e6 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ;.. /* If the
127e7 20 66 69 6c 65 20 77 61 73 20 73 75 63 63 65 73 file was succes
127e8 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f sfully opened fo
127e9 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 r read/write acc
127ea 65 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f ess,. ** choo
127eb 73 65 20 61 20 64 65 66 61 75 6c 74 20 70 61 67 se a default pag
127ec 65 20 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77 e size in case w
127ed 65 20 68 61 76 65 20 74 6f 20 63 72 65 61 74 65 e have to create
127ee 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 the. ** data
127ef 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 64 base file. The d
127f0 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 efault page size
127f1 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 is the maximum
127f2 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a of:. **. *
127f3 2a 20 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 * + SQLITE_DE
127f4 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c FAULT_PAGE_SIZE,
127f5 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 . ** + The
127f6 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
127f7 62 79 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 by sqlite3OsSect
127f8 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 orSize(). **
127f9 20 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 + The largest
127fa 20 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 page size that
127fb 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 can be written a
127fc 74 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a tomically.. *
127fd 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
127fe 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 LITE_OK && !read
127ff 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 65 Only ){. se
12800 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 tSectorSize(pPag
12801 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 er);. asser
12802 74 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 t(SQLITE_DEFAULT
12803 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 _PAGE_SIZE<=SQLI
12804 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 TE_MAX_DEFAULT_P
12805 41 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 AGE_SIZE);.
12806 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c if( szPageDflt<
12807 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
12808 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ze ){. if
12809 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 ( pPager->sector
1280a 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Size>SQLITE_MAX_
1280b 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1280c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 E ){. s
1280d 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 zPageDflt = SQLI
1280e 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 TE_MAX_DEFAULT_P
1280f 41 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 AGE_SIZE;.
12810 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
12811 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 szPageDflt =
12812 28 75 31 36 29 70 50 61 67 65 72 2d 3e 73 65 63 (u16)pPager->sec
12813 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 torSize;.
12814 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 65 }. }.#ifde
12815 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
12816 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 ATOMIC_WRITE.
12817 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 {. int
12818 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 iDc = sqlite3Os
12819 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
1281a 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 stics(pPager->fd
1281b 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 );. int i
1281c 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 i;. asser
1281d 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 t(SQLITE_IOCAP_A
1281e 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e TOMIC512==(512>>
1281f 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 8));. ass
12820 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 ert(SQLITE_IOCAP
12821 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 _ATOMIC64K==(655
12822 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 36>>8));.
12823 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 4d assert(SQLITE_M
12824 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
12825 53 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20 SIZE<=65536);.
12826 20 20 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 for(ii=szP
12827 61 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c ageDflt; ii<=SQL
12828 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f ITE_MAX_DEFAULT_
12829 50 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 PAGE_SIZE; ii=ii
1282a 2a 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 *2){. i
1282b 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 f( iDc&(SQLITE_I
1282c 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e OCAP_ATOMIC|(ii>
1282d 3e 38 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 >8)) ){.
1282e 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d szPageDflt =
1282f 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ii;. }
12830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
12831 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a }.#endif. }.
12832 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
12833 49 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 If a temporary f
12834 69 6c 65 20 69 73 20 72 65 71 75 65 73 74 65 64 ile is requested
12835 2c 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e , it is not open
12836 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a ed immediately..
12837 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 ** In this c
12838 61 73 65 20 77 65 20 61 63 63 65 70 74 20 74 68 ase we accept th
12839 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 e default page s
1283a 69 7a 65 20 61 6e 64 20 64 65 6c 61 79 20 61 63 ize and delay ac
1283b 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 tually. ** op
1283c 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 75 ening the file u
1283d 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 63 ntil the first c
1283e 61 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 29 all to OsWrite()
1283f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
12840 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 61 This branch is a
12841 6c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 69 lso run for an i
12842 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
12843 65 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a e. An in-memory.
12844 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
12845 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 is the same as a
12846 20 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 74 20 temp-file that
12847 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e is never written
12848 20 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 out to. ** d
12849 69 73 6b 20 61 6e 64 20 75 73 65 73 20 61 6e 20 isk and uses an
1284a 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 in-memory rollba
1284b 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 ck journal..
1284c 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 6c 65 */ . tempFile
1284d 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 72 = 1;. pPager
1284e 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
1284f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 0a EXCLUSIVE;. }..
12850 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 /* The followi
12851 6e 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 ng call to Pager
12852 53 65 74 50 61 67 65 73 69 7a 65 28 29 20 73 65 SetPagesize() se
12853 72 76 65 73 20 74 6f 20 73 65 74 20 74 68 65 20 rves to set the
12854 76 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50 value of . ** P
12855 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e ager.pageSize an
12856 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 d to allocate th
12857 65 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 e Pager.pTmpSpac
12858 65 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 e buffer.. */.
12859 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1285a 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 OK ){. assert
1285b 28 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d ( pPager->memDb=
1285c 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 =0 );. rc = s
1285d 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
1285e 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 gesize(pPager, &
1285f 73 7a 50 61 67 65 44 66 6c 74 29 3b 0a 20 20 20 szPageDflt);.
12860 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 testcase( rc!=S
12861 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a QLITE_OK );. }.
12862 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f . /* If an erro
12863 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69 r occurred in ei
12864 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 ther of the bloc
12865 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74 ks above, free t
12866 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73 he . ** Pager s
12867 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f tructure and clo
12868 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a se the file.. *
12869 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 /. if( rc!=SQLI
1286a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 TE_OK ){. ass
1286b 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54 ert( !pPager->pT
1286c 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73 mpSpace );. s
1286d 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
1286e 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73 ager->fd);. s
1286f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 qlite3_free(pPag
12870 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 er);. return
12871 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e rc;. }.. /* In
12872 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61 itialize the PCa
12873 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 che object. */.
12874 20 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 nExtra = ROUND8
12875 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69 (nExtra);. sqli
12876 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a te3PcacheOpen(sz
12877 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 PageDflt, nExtra
12878 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20 , !memDb,.
12879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d !m
1287a 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73 emDb?pagerStress
1287b 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 :0, (void *)pPag
1287c 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61 er, pPager->pPCa
1287d 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 che);.. PAGERTR
1287e 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73 ACE(("OPEN %d %s
1287f 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 \n", FILEHANDLEI
12880 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 D(pPager->fd), p
12881 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 Pager->zFilename
12882 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 ));. IOTRACE(("
12883 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 OPEN %p %s\n", p
12884 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a Pager, pPager->z
12885 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50 Filename)).. pP
12886 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c ager->useJournal
12887 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 = (u8)useJourna
12888 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52 l;. pPager->noR
12889 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61 eadlock = (noRea
1288a 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c dlock && readOnl
1288b 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50 y) ?1:0;. /* pP
1288c 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d ager->stmtOpen =
1288d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
1288e 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 er->stmtInUse =
1288f 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
12890 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a r->nRef = 0; */.
12891 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
12892 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44 Valid = (u8)memD
12893 62 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e b;. /* pPager->
12894 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f stmtSize = 0; */
12895 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 . /* pPager->st
12896 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a mtJSize = 0; */.
12897 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 /* pPager->nPa
12898 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 ge = 0; */. pPa
12899 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 ger->mxPgno = SQ
1289a 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f LITE_MAX_PAGE_CO
1289b 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 UNT;. /* pPager
1289c 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
1289d 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 UNLOCK; */. ass
1289e 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
1289f 74 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 te == (tempFile
128a0 3f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 ? PAGER_EXCLUSIV
128a1 45 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b E : PAGER_UNLOCK
128a2 29 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 ) );. /* pPager
128a3 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a ->errMask = 0; *
128a4 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 /. pPager->temp
128a5 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46 File = (u8)tempF
128a6 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74 ile;. assert( t
128a7 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c empFile==PAGER_L
128a8 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 OCKINGMODE_NORMA
128a9 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 L . ||
128aa 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f tempFile==PAGER_
128ab 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c LOCKINGMODE_EXCL
128ac 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 USIVE );. asser
128ad 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 t( PAGER_LOCKING
128ae 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d MODE_EXCLUSIVE==
128af 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 1 );. pPager->e
128b0 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 xclusiveMode = (
128b1 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 u8)tempFile; .
128b2 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
128b3 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 untDone = pPager
128b4 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 ->tempFile;. pP
128b5 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75 ager->memDb = (u
128b6 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 8)memDb;. pPage
128b7 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75 r->readOnly = (u
128b8 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 8)readOnly;. /*
128b9 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
128ba 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 c = 0; */. pPag
128bb 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 28 70 50 er->noSync = (pP
128bc 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 7c ager->tempFile |
128bd 7c 20 21 75 73 65 4a 6f 75 72 6e 61 6c 29 20 3f | !useJournal) ?
128be 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 1:0;. pPager->f
128bf 75 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72 ullSync = pPager
128c0 2d 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 ->noSync ?0:1;.
128c1 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
128c2 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e ags = SQLITE_SYN
128c3 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 C_NORMAL;. /* p
128c4 50 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 Pager->pFirst =
128c5 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
128c6 72 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 r->pFirstSynced
128c7 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 = 0; */. /* pPa
128c8 67 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 ger->pLast = 0;
128c9 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 */. pPager->nEx
128ca 74 72 61 20 3d 20 6e 45 78 74 72 61 3b 0a 20 20 tra = nExtra;.
128cb 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 pPager->journalS
128cc 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 izeLimit = SQLIT
128cd 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 E_DEFAULT_JOURNA
128ce 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 L_SIZE_LIMIT;.
128cf 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
128d0 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 Pager->fd) || te
128d1 6d 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 mpFile );. setS
128d2 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 ectorSize(pPager
128d3 29 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62 20 29 );. if( memDb )
128d4 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
128d5 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 urnalMode = PAGE
128d6 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
128d7 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 MORY;. }. /* p
128d8 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 Pager->xBusyHand
128d9 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a ler = 0; */. /*
128da 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 pPager->pBusyHa
128db 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f ndlerArg = 0; */
128dc 0a 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 . /* memset(pPa
128dd 67 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 ger->aHash, 0, s
128de 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 izeof(pPager->aH
128df 61 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 ash)); */. *ppP
128e0 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 ager = pPager;.
128e1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
128e2 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 K;.}..../*.** Th
128e3 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
128e4 61 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e alled after tran
128e5 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 sitioning from P
128e6 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a AGER_UNLOCK to.*
128e7 2a 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 * PAGER_SHARED s
128e8 74 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 tate. It tests i
128e9 66 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 f there is a hot
128ea 20 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 journal present
128eb 20 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d in.** the file-
128ec 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 system for the g
128ed 69 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f iven pager. A ho
128ee 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 t journal is one
128ef 20 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 that .** needs
128f0 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 to be played bac
128f1 6b 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 k. According to
128f2 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 this function, a
128f3 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 hot-journal.**
128f4 66 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 file exists if t
128f5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 he following cri
128f6 74 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a teria are met:.*
128f7 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 *.** * The jou
128f8 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 rnal file exists
128f9 20 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 in the file sys
128fa 74 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 tem, and.** *
128fb 4e 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 No process holds
128fc 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 a RESERVED or g
128fd 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 reater lock on t
128fe 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
128ff 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 , and.** * The
12900 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
12901 74 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 tself is greater
12902 20 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e than 0 bytes in
12903 20 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 size, and.**
12904 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 * The first byte
12905 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
12906 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 file exists and
12907 69 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a is not 0x00..**.
12908 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e ** If the curren
12909 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 t size of the da
1290a 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 tabase file is 0
1290b 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 but a journal f
1290c 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 ile.** exists, t
1290d 68 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 hat is probably
1290e 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c an old journal l
1290f 65 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 eft over from a
12910 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 prior.** databas
12911 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 e with the same
12912 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 name. In this ca
12913 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 se the journal f
12914 69 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 ile is.** just d
12915 65 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 eleted using OsD
12916 65 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 elete, *pExists
12917 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 is set to 0 and
12918 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 SQLITE_OK.** is
12919 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
1291a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
1291b 73 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 s not check if t
1291c 68 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 here is a master
1291d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d journal filenam
1291e 65 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 e.** at the end
1291f 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 of the file. If
12920 74 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 there is, and th
12921 61 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 at master journa
12922 6c 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e l file.** does n
12923 6f 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 ot exist, then t
12924 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
12925 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f is not really ho
12926 74 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 t. In this.** ca
12927 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 se this routine
12928 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 will return a fa
12929 6c 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 lse-positive. Th
1292a 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b e pager_playback
1292b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 ().** routine wi
1292c 6c 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 ll discover that
1292d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1292e 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 e is not really
1292f 68 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c hot and .** will
12930 20 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 not roll it bac
12931 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 k. .**.** If a h
12932 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ot-journal file
12933 69 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 is found to exis
12934 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 t, *pExists is s
12935 65 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 et to 1 and .**
12936 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
12937 65 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f ed. If no hot-jo
12938 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 urnal file is pr
12939 65 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 esent, *pExists
1293a 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 is.** set to 0 a
1293b 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
1293c 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 urned. If an IO
1293d 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
1293e 6c 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 le trying.** to
1293f 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 determine whethe
12940 72 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a r or not a hot-j
12941 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 ournal file exis
12942 74 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 ts, the IO error
12943 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 .** code is retu
12944 72 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c rned and the val
12945 75 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 ue of *pExists i
12946 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
12947 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f static int hasHo
12948 74 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a tJournal(Pager *
12949 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 pPager, int *pEx
1294a 69 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 ists){. sqlite3
1294b 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 _vfs * const pVf
1294c 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
1294d 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 ;. int rc;
1294e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1294f 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
12950 20 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 */. int exists
12951 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12952 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
12953 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
12954 20 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 present */.. a
12955 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 ssert( pPager!=0
12956 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
12957 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c ager->useJournal
12958 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 );. assert( is
12959 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
1295a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 );. assert( !i
1295b 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
1295c 64 29 20 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74 d) );.. *pExist
1295d 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 s = 0;. rc = sq
1295e 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 lite3OsAccess(pV
1295f 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
12960 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 rnal, SQLITE_ACC
12961 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 ESS_EXISTS, &exi
12962 73 74 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d sts);. if( rc==
12963 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 78 69 SQLITE_OK && exi
12964 73 74 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c sts ){. int l
12965 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 20 ocked;
12966 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
12967 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68 f some process h
12968 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 20 olds a RESERVED
12969 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 72 63 20 3d lock */. rc =
1296a 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 sqlite3OsCheckR
1296b 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 50 61 67 eservedLock(pPag
1296c 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b 65 64 29 er->fd, &locked)
1296d 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1296e 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b LITE_OK && !lock
1296f 65 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ed ){. int
12970 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 2f 2a nPage;.. /*
12971 20 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20 Check the size
12972 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
12973 66 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73 file. If it cons
12974 69 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c ists of 0 pages,
12975 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64 . ** then d
12976 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
12977 6c 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20 l file. See the
12978 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 header comment a
12979 62 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20 bove for .
1297a 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67 ** the reasoning
1297b 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a here.. */.
1297c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1297d 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
1297e 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 (pPager, &nPage)
1297f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
12980 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
12981 20 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d if( nPage==
12982 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
12983 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c c = sqlite3OsDel
12984 65 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 ete(pVfs, pPager
12985 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a ->zJournal, 0);.
12986 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
12987 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
12988 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 journal file exi
12989 73 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 sts and no other
1298a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 connection has
1298b 61 20 72 65 73 65 72 76 65 64 0a 20 20 20 20 20 a reserved.
1298c 20 20 20 20 20 2a 2a 20 6f 72 20 67 72 65 61 74 ** or great
1298d 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 er lock on the d
1298e 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f atabase file. No
1298f 77 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 w check that the
12990 72 65 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 re is.
12991 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 ** at least one
12992 6e 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73 20 61 non-zero bytes a
12993 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
12994 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
12995 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 . ** If
12996 20 74 68 65 72 65 20 69 73 2c 20 74 68 65 6e 20 there is, then
12997 77 65 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 we consider this
12998 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 68 journal to be h
12999 6f 74 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 20 20 ot. If not, .
1299a 20 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e ** it can
1299b 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20 be ignored..
1299c 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1299d 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 int f = SQLI
1299e 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
1299f 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 |SQLITE_OPEN_MAI
129a0 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 N_JOURNAL;.
129a1 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
129a2 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 3OsOpen(pVfs, pP
129a3 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
129a4 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 pPager->jfd, f,
129a5 26 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 &f);. i
129a6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
129a7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
129a8 75 38 20 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 u8 first = 0;.
129a9 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
129aa 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 qlite3OsRead(pPa
129ab 67 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 64 20 ger->jfd, (void
129ac 2a 29 26 66 69 72 73 74 2c 20 31 2c 20 30 29 3b *)&first, 1, 0);
129ad 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
129ae 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 rc==SQLITE_IOER
129af 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a R_SHORT_READ ){.
129b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 rc
129b1 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
129b2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
129b3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f sqlite3O
129b4 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a sClose(pPager->j
129b5 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 fd);.
129b6 20 2a 70 45 78 69 73 74 73 20 3d 20 28 66 69 72 *pExists = (fir
129b7 73 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 st!=0);.
129b8 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
129b9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
129ba 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
129bb 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 ./*.** Read the
129bc 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 65 content for page
129bd 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 pPg out of the
129be 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
129bf 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e d into .** pPg->
129c0 70 44 61 74 61 2e 20 41 20 73 68 61 72 65 64 20 pData. A shared
129c1 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 lock or greater
129c2 6d 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 must be held on
129c3 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
129c4 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73 file before this
129c5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
129c6 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 led..**.** If pa
129c7 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 74 68 ge 1 is read, th
129c8 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 en the value of
129c9 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 Pager.dbFileVers
129ca 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 [] is set to.**
129cb 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 the value read f
129cc 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
129cd 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
129ce 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
129cf 72 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 rs, then the IO
129d0 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
129d1 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e d to the caller.
129d2 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 53 .** Otherwise, S
129d3 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
129d4 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rned..*/.static
129d5 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 28 50 int readDbPage(P
129d6 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 gHdr *pPg){. Pa
129d7 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
129d8 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 g->pPager; /* Pa
129d9 67 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 ger object assoc
129da 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65 20 iated with page
129db 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 pPg */. Pgno pg
129dc 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 no = pPg->pgno;
129dd 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
129de 6d 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a mber to read */.
129df 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
129e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
129e1 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
129e2 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 . i64 iOffset;
129e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129e4 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f /* Byte offset o
129e5 66 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20 66 f file to read f
129e6 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 rom */.. assert
129e7 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
129e8 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 26 26 =PAGER_SHARED &&
129e9 20 21 4d 45 4d 44 42 20 29 3b 0a 0a 20 20 69 66 !MEMDB );.. if
129ea 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
129eb 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 ->fd) ){. ass
129ec 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d ert( pPager->tem
129ed 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d pFile );. mem
129ee 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 set(pPg->pData,
129ef 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 0, pPager->pageS
129f0 69 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ize);. return
129f1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
129f2 20 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e iOffset = (pgn
129f3 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 o-1)*(i64)pPager
129f4 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 ->pageSize;. rc
129f5 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
129f6 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 (pPager->fd, pPg
129f7 2d 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d ->pData, pPager-
129f8 3e 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73 >pageSize, iOffs
129f9 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 et);. if( rc==S
129fa 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
129fb 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63 T_READ ){. rc
129fc 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
129fd 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 }. if( pgno==1
129fe 29 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c ){. u8 *dbFil
129ff 65 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 eVers = &((u8*)p
12a00 50 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a Pg->pData)[24];.
12a01 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 memcpy(&pPag
12a02 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 er->dbFileVers,
12a03 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 dbFileVers, size
12a04 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c of(pPager->dbFil
12a05 65 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 eVers));. }. C
12a06 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 ODEC1(pPager, pP
12a07 67 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 g->pData, pgno,
12a08 33 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43 3);.. PAGER_INC
12a09 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f R(sqlite3_pager_
12a0a 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 readdb_count);.
12a0b 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 PAGER_INCR(pPag
12a0c 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f er->nRead);. IO
12a0d 54 52 41 43 45 28 28 22 50 47 49 4e 20 25 70 20 TRACE(("PGIN %p
12a0e 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
12a0f 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 54 52 gno));. PAGERTR
12a10 41 43 45 28 28 22 46 45 54 43 48 20 25 64 20 70 ACE(("FETCH %d p
12a11 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 age %d hash(%08x
12a12 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 )\n",.
12a13 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 PAGERID(pPa
12a14 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 ger), pgno, page
12a15 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29 r_pagehash(pPg))
12a16 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );.. return rc;
12a17 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
12a18 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
12a19 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 75 d whenever the u
12a1a 70 70 65 72 20 6c 61 79 65 72 20 72 65 71 75 65 pper layer reque
12a1b 73 74 73 20 61 20 64 61 74 61 62 61 73 65 0a 2a sts a database.*
12a1c 2a 20 70 61 67 65 20 69 73 20 72 65 71 75 65 73 * page is reques
12a1d 74 65 64 2c 20 62 65 66 6f 72 65 20 74 68 65 20 ted, before the
12a1e 63 61 63 68 65 20 69 73 20 63 68 65 63 6b 65 64 cache is checked
12a1f 20 66 6f 72 20 61 20 73 75 69 74 61 62 6c 65 20 for a suitable
12a20 70 61 67 65 0a 2a 2a 20 6f 72 20 61 6e 79 20 64 page.** or any d
12a21 61 74 61 20 69 73 20 72 65 61 64 20 66 72 6f 6d ata is read from
12a22 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 the database. I
12a23 74 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 66 t performs the f
12a24 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20 ollowing.** two
12a25 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a functions:.**.**
12a26 20 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 1) If the pag
12a27 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 er is currently
12a28 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 in PAGER_UNLOCK
12a29 73 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 state (no lock h
12a2a 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 eld.** on t
12a2b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12a2c 29 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d ), then an attem
12a2d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 pt is made to ob
12a2e 74 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 tain a.** S
12a2f 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 HARED lock on th
12a30 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
12a31 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 Immediately aft
12a32 65 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 er obtaining.**
12a33 20 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20 the SHARED
12a34 6c 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 lock, the file-s
12a35 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 ystem is checked
12a36 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e for a hot-journ
12a37 61 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 al,.** whic
12a38 68 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b h is played back
12a39 20 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c if present. Fol
12a3a 6c 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a lowing any hot-j
12a3b 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 ournal .**
12a3c 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f rollback, the co
12a3d 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 ntents of the ca
12a3e 63 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 che are validate
12a3f 64 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a d by checking.**
12a40 20 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 the 'chang
12a41 65 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 e-counter' field
12a42 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
12a43 20 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 file header and
12a44 0a 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 .** discard
12a45 65 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66 ed if they are f
12a46 6f 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c ound to be inval
12a47 69 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 id..**.** 2) I
12a48 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 f the pager is r
12a49 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 unning in exclus
12a4a 69 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 ive-mode, and th
12a4b 65 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c ere are currentl
12a4c 79 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 y.** no out
12a4d 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e standing referen
12a4e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 ces to any pages
12a4f 2c 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 , and is in the
12a50 65 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 error state,.**
12a51 20 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 then an att
12a52 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 empt is made to
12a53 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 clear the error
12a54 73 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 state by discard
12a55 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 ing.** the
12a56 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
12a57 70 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 page cache and r
12a58 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 olling back any
12a59 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 open journal.**
12a5a 20 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a file..**.**
12a5b 20 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f If the operatio
12a5c 6e 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28 n described by (
12a5d 32 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 2) above is not
12a5e 61 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69 attempted, and i
12a5f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69 f the.** pager i
12a60 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 s in an error st
12a61 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 ate other than S
12a62 51 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20 QLITE_FULL when
12a63 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a this is called,.
12a64 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 ** the error sta
12a65 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
12a66 20 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73 returned. It is
12a67 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 permitted to re
12a68 61 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 ad the.** databa
12a69 73 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54 se when in SQLIT
12a6a 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61 E_FULL error sta
12a6b 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 te..**.** Otherw
12a6c 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 ise, if everythi
12a6d 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c ng is successful
12a6e 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
12a6f 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a eturned. If an.*
12a70 2a 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 * IO error occur
12a71 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 s while locking
12a72 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68 the database, ch
12a73 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74 ecking for a hot
12a74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 -journal.** file
12a75 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b or rolling back
12a76 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c a journal file,
12a77 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f the IO error co
12a78 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a de is returned..
12a79 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
12a7a 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 gerSharedLock(Pa
12a7b 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
12a7c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
12a7d 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
12a7e 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
12a7f 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 e */. int isErr
12a80 6f 72 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 orReset = 0;
12a81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
12a82 65 20 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 e if recovering
12a83 66 72 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 from error state
12a84 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 */.. /* If thi
12a85 73 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 s database is op
12a86 65 6e 65 64 20 66 6f 72 20 65 78 63 6c 75 73 69 ened for exclusi
12a87 76 65 20 61 63 63 65 73 73 2c 20 68 61 73 20 6e ve access, has n
12a88 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 0a 20 o outstanding .
12a89 20 2a 2a 20 70 61 67 65 20 72 65 66 65 72 65 6e ** page referen
12a8a 63 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 6e ces and is in an
12a8b 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68 error-state, th
12a8c 69 73 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 is is a chance t
12a8d 6f 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 o clear. ** the
12a8e 20 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64 20 error. Discard
12a8f 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
12a90 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 the pager-cache
12a91 61 6e 64 20 74 72 65 61 74 20 61 6e 79 0a 20 20 and treat any.
12a92 2a 2a 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 ** open journal
12a93 66 69 6c 65 20 61 73 20 61 20 68 6f 74 2d 6a 6f file as a hot-jo
12a94 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 urnal.. */. if
12a95 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 ( !MEMDB && pPag
12a96 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
12a97 65 20 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33 e . && sqlite3
12a98 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 PcacheRefCount(p
12a99 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d Pager->pPCache)=
12a9a 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 =0 && pPager->er
12a9b 72 43 6f 64 65 20 0a 20 20 29 7b 0a 20 20 20 20 rCode . ){.
12a9c 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
12a9d 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 r->jfd) ){.
12a9e 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 isErrorReset =
12a9f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 1;. }. pPa
12aa0 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 ger->errCode = S
12aa1 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 QLITE_OK;. pa
12aa2 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
12aa3 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
12aa4 74 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69 the pager is sti
12aa5 6c 6c 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 ll in an error s
12aa6 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20 70 72 6f tate, do not pro
12aa7 63 65 65 64 2e 20 54 68 65 20 65 72 72 6f 72 20 ceed. The error
12aa8 0a 20 20 2a 2a 20 73 74 61 74 65 20 77 69 6c 6c . ** state will
12aa9 20 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 73 be cleared at s
12aaa 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 ome point in the
12aab 20 66 75 74 75 72 65 20 77 68 65 6e 20 61 6c 6c future when all
12aac 20 70 61 67 65 20 0a 20 20 2a 2a 20 72 65 66 65 page . ** refe
12aad 72 65 6e 63 65 73 20 61 72 65 20 64 72 6f 70 70 rences are dropp
12aae 65 64 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 ed and the cache
12aaf 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 can be discarde
12ab0 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 d.. */. if( pP
12ab1 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 ager->errCode &&
12ab2 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
12ab3 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b !=SQLITE_FULL ){
12ab4 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 . return pPag
12ab5 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d er->errCode;. }
12ab6 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e .. if( pPager->
12ab7 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c state==PAGER_UNL
12ab8 4f 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 OCK || isErrorRe
12ab9 73 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 set ){. sqlit
12aba 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 e3_vfs * const p
12abb 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 Vfs = pPager->pV
12abc 66 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f fs;. int isHo
12abd 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 tJournal = 0;.
12abe 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 assert( !MEMDB
12abf 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
12ac0 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
12ac1 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 Count(pPager->pP
12ac2 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 Cache)==0 );.
12ac3 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f if( !pPager->no
12ac4 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 Readlock ){.
12ac5 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 rc = pager_wai
12ac6 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 t_on_lock(pPager
12ac7 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a , SHARED_LOCK);.
12ac8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
12ac9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12aca 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
12acb 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
12acc 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 UNLOCK );.
12acd 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 return pager_e
12ace 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
12acf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
12ad0 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e lse if( pPager->
12ad1 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c state==PAGER_UNL
12ad2 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 OCK ){. pPa
12ad3 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 ger->state = PAG
12ad4 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d ER_SHARED;. }
12ad5 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
12ad6 67 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 ger->state>=SHAR
12ad7 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 ED_LOCK );..
12ad8 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 /* If a journal
12ad9 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 file exists, and
12ada 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 there is no RES
12adb 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 ERVED lock on th
12adc 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 e. ** databas
12add 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 e file, then it
12ade 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 either needs to
12adf 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f be played back o
12ae0 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a r deleted.. *
12ae1 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72 /. if( !isErr
12ae2 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 orReset ){.
12ae3 20 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 rc = hasHotJour
12ae4 6e 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48 nal(pPager, &isH
12ae5 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 otJournal);.
12ae6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12ae7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
12ae8 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
12ae9 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
12aea 28 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c ( isErrorReset |
12aeb 7c 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 | isHotJournal )
12aec 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 {. /* Get a
12aed 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
12aee 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
12aef 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70 file. At this p
12af0 6f 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20 oint it is.
12af1 20 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 ** important th
12af2 61 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f at a RESERVED lo
12af3 63 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e ck is not obtain
12af4 65 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f ed on the way to
12af5 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 the. ** EX
12af6 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 CLUSIVE lock. If
12af7 20 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 it were, anothe
12af8 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 r process might
12af9 6f 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a open the. *
12afa 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c * database file,
12afb 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45 detect the RESE
12afc 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 RVED lock, and c
12afd 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65 onclude that the
12afe 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 . ** databa
12aff 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 se is safe to re
12b00 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72 ad while this pr
12b01 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72 ocess is still r
12b02 6f 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 20 olling the .
12b03 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ** hot-journal
12b04 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 back.. **
12b05 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 73 . ** Becaus
12b06 65 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 e the intermedia
12b07 74 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b te RESERVED lock
12b08 20 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 65 is not requeste
12b09 64 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 d, any. **
12b0a 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 74 other process at
12b0b 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 65 tempting to acce
12b0c 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ss the database
12b0d 66 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 6f file will get to
12b0e 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 . ** this
12b0f 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 point in the cod
12b10 65 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 e and fail to ob
12b11 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 tain its own EXC
12b12 4c 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 LUSIVE lock .
12b13 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 ** on the dat
12b14 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 abase file..
12b15 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 */. if( p
12b16 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 Pager->state<EXC
12b17 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 LUSIVE_LOCK ){.
12b18 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
12b19 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 te3OsLock(pPager
12b1a 2d 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f ->fd, EXCLUSIVE_
12b1b 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 LOCK);. i
12b1c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12b1d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
12b1e 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 = pager_error(p
12b1f 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 Pager, rc);.
12b20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 goto faile
12b21 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 d;. }.
12b22 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 pPager->sta
12b23 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 te = PAGER_EXCLU
12b24 53 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a SIVE;. }. .
12b25 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 /* Open th
12b26 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 e journal for re
12b27 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e ad/write access.
12b28 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
12b29 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 in . ** ex
12b2a 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d clusive-access m
12b2b 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 ode the file des
12b2c 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 criptor will be
12b2d 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 kept open and.
12b2e 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 ** possibly
12b2f 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e 73 used for a trans
12b30 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e action later on.
12b31 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 On some systems
12b32 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f , the. ** O
12b33 73 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c sTruncate() call
12b34 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69 used in exclusi
12b35 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61 ve-access mode a
12b36 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20 lso requires.
12b37 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 ** a read/wri
12b38 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a te file handle..
12b39 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
12b3a 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 f( !isOpen(pPage
12b3b 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 r->jfd) ){.
12b3c 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 int res;.
12b3d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12b3e 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50 OsAccess(pVfs,pP
12b3f 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 ager->zJournal,S
12b40 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 QLITE_ACCESS_EXI
12b41 53 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 STS,&res);.
12b42 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
12b43 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
12b44 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
12b45 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 int fou
12b46 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 t = 0;.
12b47 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 int f = SQLIT
12b48 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
12b49 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 |SQLITE_OPEN_MAI
12b4a 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 N_JOURNAL;.
12b4b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 assert( !
12b4c 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
12b4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
12b4e 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
12b4f 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d en(pVfs, pPager-
12b50 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 >zJournal, pPage
12b51 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 r->jfd, f, &fout
12b52 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 );. a
12b53 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
12b54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 E_OK || isOpen(p
12b55 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 Pager->jfd) );.
12b56 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
12b57 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
12b58 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e fout&SQLITE_OPEN
12b59 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 _READONLY ){.
12b5a 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
12b5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
12b5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 . s
12b5d 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
12b5e 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
12b5f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12b60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
12b61 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 /* If the
12b62 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f journal does no
12b63 74 20 65 78 69 73 74 2c 20 74 68 61 74 20 6d 65 t exist, that me
12b64 61 6e 73 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 ans some other p
12b65 72 6f 63 65 73 73 0a 20 20 20 20 20 20 20 20 20 rocess.
12b66 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 ** has alread
12b67 79 20 72 6f 6c 6c 65 64 20 69 74 20 62 61 63 6b y rolled it back
12b68 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
12b69 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
12b6a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
12b6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
12b6c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
12b6d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12b6e 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
12b6f 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
12b70 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20 * TODO: Why are
12b71 74 68 65 73 65 20 63 6c 65 61 72 65 64 20 68 65 these cleared he
12b72 72 65 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73 re? Is it necess
12b73 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50 ary? */. pP
12b74 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 ager->journalSta
12b75 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 rted = 0;.
12b76 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12b77 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 ff = 0;. pP
12b78 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
12b79 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 = 0;. pPage
12b7a 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 r->journalHdr =
12b7b 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 0;. . /* Pl
12b7c 61 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 ayback and delet
12b7d 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 e the journal.
12b7e 44 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73 Drop the databas
12b7f 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a e write. **
12b80 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 lock and reacqu
12b81 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 ire the read loc
12b82 6b 2e 20 50 75 72 67 65 20 74 68 65 20 63 61 63 k. Purge the cac
12b83 68 65 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 he before.
12b84 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 ** playing back
12b85 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 the hot-journal
12b86 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 so that we don't
12b87 20 65 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20 end up with.
12b88 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 ** an inconsi
12b89 73 74 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20 stent cache..
12b8a 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d */. rc =
12b8b 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
12b8c 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 pPager, 1);.
12b8d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12b8e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
12b8f 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 c = pager_error(
12b90 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
12b91 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
12b92 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
12b93 61 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d assert( (pPager-
12b94 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 >state==PAGER_SH
12b95 41 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 ARED).
12b96 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 || (pPager->exc
12b97 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 lusiveMode && pP
12b98 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 ager->state>PAGE
12b99 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 R_SHARED).
12b9a 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 );. }.. if
12b9b 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 ( sqlite3PcacheP
12b9c 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d agecount(pPager-
12b9d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0a 20 >pPCache)>0 ){.
12b9e 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 72 /* The shar
12b9f 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 73 74 ed-lock has just
12ba0 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 20 6f been acquired o
12ba1 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
12ba2 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 ile. ** and
12ba3 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 61 there are alrea
12ba4 64 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 dy pages in the
12ba5 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20 70 72 cache (from a pr
12ba6 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a 2a 20 evious. **
12ba7 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 read or write tr
12ba8 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 68 65 ansaction). Che
12ba9 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ck to see if the
12baa 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 database.
12bab 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 ** has been modi
12bac 66 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61 fied. If the da
12bad 74 61 62 61 73 65 20 68 61 73 20 63 68 61 6e 67 tabase has chang
12bae 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a 20 20 ed, flush the.
12baf 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20 ** cache..
12bb0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
12bb1 44 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 Database changes
12bb2 20 69 73 20 64 65 74 65 63 74 65 64 20 62 79 20 is detected by
12bb3 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 62 79 looking at 15 by
12bb4 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 tes beginning.
12bb5 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 65 74 ** at offset
12bb6 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 24 into the fil
12bb7 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 34 20 e. The first 4
12bb8 6f 66 20 74 68 65 73 65 20 31 36 20 62 79 74 65 of these 16 byte
12bb9 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 s are. ** a
12bba 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 72 20 32-bit counter
12bbb 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e that is incremen
12bbc 74 65 64 20 77 69 74 68 20 65 61 63 68 20 63 68 ted with each ch
12bbd 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 20 20 ange. The.
12bbe 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 ** other bytes
12bbf 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c 79 20 change randomly
12bc0 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 20 63 with each file c
12bc1 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 20 20 hange when.
12bc2 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 20 69 ** a codec is i
12bc3 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a 20 n use.. **
12bc4 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 . ** There
12bc5 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79 is a vanishingly
12bc6 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68 small chance th
12bc7 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c at a change will
12bc8 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 20 2a not be . *
12bc9 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 65 * detected. The
12bca 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 6e chance of an un
12bcb 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 20 detected change
12bcc 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74 is so small that
12bcd 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e . ** it can
12bce 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e 0a 20 be neglected..
12bcf 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 */. ch
12bd0 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b 73 69 ar dbFileVers[si
12bd1 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 zeof(pPager->dbF
12bd2 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 20 20 ileVers)];.
12bd3 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12bd4 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 ecount(pPager, 0
12bd5 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 50 );.. if( pP
12bd6 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b ager->errCode ){
12bd7 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 50 . rc = pP
12bd8 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
12bd9 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c goto fail
12bda 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 ed;. }..
12bdb 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
12bdc 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 r->dbSizeValid )
12bdd 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 ;. if( pPag
12bde 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a er->dbSize>0 ){.
12bdf 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 IOTRACE(
12be0 28 22 43 4b 56 45 52 53 20 25 70 20 25 64 5c 6e ("CKVERS %p %d\n
12be1 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a 65 6f ", pPager, sizeo
12be2 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 29 3b f(dbFileVers)));
12be3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
12be4 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
12be5 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 er->fd, &dbFileV
12be6 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 ers, sizeof(dbFi
12be7 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20 leVers), 24);.
12be8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
12be9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12bea 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
12beb 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
12bec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
12bed 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65 memset(dbFileVe
12bee 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62 rs, 0, sizeof(db
12bef 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 FileVers));.
12bf0 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d }.. if( m
12bf1 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 62 emcmp(pPager->db
12bf2 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 FileVers, dbFile
12bf3 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 Vers, sizeof(dbF
12bf4 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29 7b 0a ileVers))!=0 ){.
12bf5 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 pager_re
12bf6 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
12bf7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 }. }. a
12bf8 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 ssert( pPager->e
12bf9 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 xclusiveMode ||
12bfa 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
12bfb 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 AGER_SHARED );.
12bfc 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 20 69 }.. failed:. i
12bfd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12bfe 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 ){. /* pager
12bff 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e _unlock() is a n
12c00 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 73 69 o-op for exclusi
12c01 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e 2d 6d ve mode and in-m
12c02 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 2e emory databases.
12c03 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 75 6e */. pager_un
12c04 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 lock(pPager);.
12c05 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
12c06 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ../*.** If the r
12c07 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68 eference count h
12c08 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c as reached zero,
12c09 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 rollback any ac
12c0a 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 tive.** transact
12c0b 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 ion and unlock t
12c0c 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 he pager..**.**
12c0d 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 6b 69 Except, in locki
12c0e 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 ng_mode=EXCLUSIV
12c0f 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 E when there is
12c10 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a 2a 2a nothing to in.**
12c11 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
12c12 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c 6f 63 urnal, the unloc
12c13 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f 72 6d k is not perform
12c14 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 73 0a ed and there is.
12c15 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 6f ** nothing to ro
12c16 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 73 20 llback, so this
12c17 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
12c18 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 op..*/ .static v
12c19 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 oid pagerUnlockI
12c1a 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70 fUnused(Pager *p
12c1b 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 28 73 Pager){. if( (s
12c1c 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
12c1d 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
12c1e 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 26 20 ache)==0). &&
12c1f 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 (!pPager->exclus
12c20 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 iveMode || pPage
12c21 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 r->journalOff>0)
12c22 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 65 72 . ){. pager
12c23 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 UnlockAndRollbac
12c24 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d k(pPager);. }.}
12c25 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70 ../*.** Drop a p
12c26 61 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 age from the cac
12c27 68 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 he using sqlite3
12c28 50 63 61 63 68 65 44 72 6f 70 28 29 2e 0a 2a 2a PcacheDrop()..**
12c29 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d 65 61 6e .** If this mean
12c2a 73 20 74 68 65 72 65 20 61 72 65 20 6e 6f 77 20 s there are now
12c2b 6e 6f 20 70 61 67 65 73 20 77 69 74 68 20 72 65 no pages with re
12c2c 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 6d ferences to them
12c2d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 , a rollback.**
12c2e 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 20 6c occurs and the l
12c2f 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
12c30 61 73 65 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a ase is removed..
12c31 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
12c32 61 67 65 72 44 72 6f 70 50 61 67 65 28 44 62 50 agerDropPage(DbP
12c33 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 age *pPg){. Pag
12c34 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
12c35 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 71 6c 69 ->pPager;. sqli
12c36 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 te3PcacheDrop(pP
12c37 67 29 3b 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63 g);. pagerUnloc
12c38 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 kIfUnused(pPager
12c39 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 );.}../*.** Acqu
12c3a 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 ire a reference
12c3b 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 to page number p
12c3c 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61 gno in pager pPa
12c3d 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 ger (a page.** r
12c3e 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 79 70 eference has typ
12c3f 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74 e DbPage*). If t
12c40 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 66 he requested ref
12c41 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 erence is .** su
12c42 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 ccessfully obtai
12c43 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65 ned, it is copie
12c44 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 d to *ppPage and
12c45 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
12c46 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ned..**.** This
12c47 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 70 function calls p
12c48 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 agerSharedLock()
12c49 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 to obtain a SHA
12c4a 52 45 44 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 74 RED lock on.** t
12c4b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12c4c 20 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 if such a lock
12c4d 6f 72 20 67 72 65 61 74 65 72 20 69 73 20 6e 6f or greater is no
12c4e 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a t already held..
12c4f 2a 2a 20 54 68 69 73 20 6d 61 79 20 63 61 75 73 ** This may caus
12c50 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f e hot-journal ro
12c51 6c 6c 62 61 63 6b 20 6f 72 20 61 20 63 61 63 68 llback or a cach
12c52 65 20 70 75 72 67 65 2e 20 53 65 65 20 63 6f 6d e purge. See com
12c53 6d 65 6e 74 73 0a 2a 2a 20 61 62 6f 76 65 20 66 ments.** above f
12c54 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 53 68 61 unction pagerSha
12c55 72 65 64 4c 6f 63 6b 28 29 20 66 6f 72 20 64 65 redLock() for de
12c56 74 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 tails..**.** If
12c57 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
12c58 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e ge is already in
12c59 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 20 69 the cache, it i
12c5a 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 s returned. .**
12c5b 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77 Otherwise, a new
12c5c 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 page object is
12c5d 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f allocated and po
12c5e 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 61 74 pulated with dat
12c5f 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74 a.** read from t
12c60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12c61 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c . In some cases,
12c62 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 the pcache modu
12c63 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 le may.** choose
12c64 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 not to allocate
12c65 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 a new page obje
12c66 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65 ct and may reuse
12c67 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 an existing.**
12c68 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f object with no o
12c69 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
12c6a 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ences..**.** The
12c6b 20 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65 extra data appe
12c6c 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 nded to a page i
12c6d 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c s always initial
12c6e 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68 ized to zeros th
12c6f 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 e .** first time
12c70 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 a page is loade
12c71 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 d into memory. I
12c72 66 20 74 68 65 20 70 61 67 65 20 72 65 71 75 65 f the page reque
12c73 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65 sted is .** alre
12c74 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 ady in the cache
12c75 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
12c76 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ion is called, t
12c77 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a hen the extra.**
12c78 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 61 73 data is left as
12c79 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 68 65 it was when the
12c7a 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73 page object was
12c7b 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a last used..**.*
12c7c 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
12c7d 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c e image is small
12c7e 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 er than the requ
12c7f 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66 ested page or if
12c80 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 a .** non-zero
12c81 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 value is passed
12c82 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 as the noContent
12c83 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74 parameter and t
12c84 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 he .** requested
12c85 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
12c86 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74 eady stored in t
12c87 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e he cache, then n
12c88 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73 o .** actual dis
12c89 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49 k read occurs. I
12c8a 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
12c8b 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 memory image of
12c8c 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20 the .** page is
12c8d 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 initialized to a
12c8e 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a ll zeros. .**.**
12c8f 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 If noContent is
12c90 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20 true, it means
12c91 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 that we do not c
12c92 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f are about the co
12c93 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 ntents.** of the
12c94 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75 page. This occu
12c95 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61 rs in two sepera
12c96 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a te scenarios:.**
12c97 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65 .** a) When re
12c98 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73 ading a free-lis
12c99 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d t leaf page from
12c9a 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 61 the database, a
12c9b 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68 nd.**.** b) Wh
12c9c 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 en a savepoint i
12c9d 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 s being rolled b
12c9e 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20 ack and we need
12c9f 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 to load.**
12ca0 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20 a new page into
12ca1 74 68 65 20 63 61 63 68 65 20 74 6f 20 70 6f 70 the cache to pop
12ca2 75 6c 61 74 65 20 77 69 74 68 20 74 68 65 20 64 ulate with the d
12ca3 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 ata read.**
12ca4 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 70 6f from the savepo
12ca5 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a int journal..**.
12ca6 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 ** If noContent
12ca7 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
12ca8 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 e data returned
12ca9 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 61 is zeroed instea
12caa 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 65 d of.** being re
12cab 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ad from the data
12cac 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 6c base. Additional
12cad 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f 72 ly, the bits cor
12cae 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f responding.** to
12caf 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e 70 pgno in Pager.p
12cb0 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 65 InJournal (bitve
12cb1 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61 c of pages alrea
12cb2 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 dy written to th
12cb3 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c e.** journal fil
12cb4 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 65 72 e) and the Pager
12cb5 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 Savepoint.pInSav
12cb6 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 20 6f epoint bitvecs o
12cb7 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 f any open.** sa
12cb8 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 65 74 vepoints are set
12cb9 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 . This means if
12cba 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 64 65 the page is made
12cbb 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 6e 79 writable at any
12cbc 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 .** point in the
12cbd 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 20 61 future, using a
12cbe 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
12cbf 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 69 74 PagerWrite(), it
12cc0 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 s contents.** wi
12cc1 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e 61 ll not be journa
12cc2 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 73 20 led. This saves
12cc3 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 IO..**.** The ac
12cc4 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 quisition might
12cc5 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c fail for several
12cc6 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c reasons. In al
12cc7 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 l cases,.** an a
12cc8 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 ppropriate error
12cc9 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
12cca 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73 d and *ppPage is
12ccb 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a set to NULL..**
12ccc 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c .** See also sql
12ccd 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
12cce 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f ). Both this ro
12ccf 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 utine and Lookup
12cd0 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f () attempt.** to
12cd1 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 find a page in
12cd2 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 the in-memory ca
12cd3 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 74 che first. If t
12cd4 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
12cd5 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d lready.** in mem
12cd6 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ory, this routin
12cd7 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 e goes to disk t
12cd8 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 68 65 o read it in whe
12cd9 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a reas Lookup().**
12cda 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e just returns 0.
12cdb 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 This routine a
12cdc 63 71 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c cquires a read-l
12cdd 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 ock the first ti
12cde 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 me it.** has to
12cdf 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 go to disk, and
12ce0 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 could also playb
12ce1 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e ack an old journ
12ce2 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e al if necessary.
12ce3 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 .** Since Lookup
12ce4 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f () never goes to
12ce5 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 disk, it never
12ce6 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68 has to deal with
12ce7 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 locks.** or jou
12ce8 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 rnal files..*/.S
12ce9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
12cea 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 t sqlite3PagerAc
12ceb 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20 2a quire(. Pager *
12cec 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a 20 pPager, /*
12ced 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20 6f The pager open o
12cee 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
12cef 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ile */. Pgno pg
12cf0 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 no, /*
12cf1 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 66 Page number to f
12cf2 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 65 etch */. DbPage
12cf3 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a **ppPage, /*
12cf4 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 Write a pointer
12cf5 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 72 to the page her
12cf6 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e e */. int noCon
12cf7 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 6f tent /* Do
12cf8 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61 64 not bother read
12cf9 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d ing content from
12cfa 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a 2f disk if true */
12cfb 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 .){. PgHdr *pPg
12cfc 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a = 0;. int rc;.
12cfd 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 . assert( asser
12cfe 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 t_pager_state(pP
12cff 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 ager) );. asser
12d00 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
12d01 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 0a ==PAGER_UNLOCK .
12d02 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
12d03 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
12d04 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
12d05 3e 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 70 67 >0 . || pg
12d06 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a 20 20 2f 2a no==1. );.. /*
12d07 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 The maximum pag
12d08 65 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 31 e number is 2^31
12d09 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f . Return SQLITE_
12d0a 43 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 67 CORRUPT if a pag
12d0b 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72 e. ** number gr
12d0c 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 2c eater than this,
12d0d 20 6f 72 20 7a 65 72 6f 2c 20 69 73 20 72 65 71 or zero, is req
12d0e 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 uested.. */. i
12d0f 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 f( pgno>PAGER_MA
12d10 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d X_PGNO || pgno==
12d11 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 0 || pgno==PAGER
12d12 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 _MJ_PGNO(pPager)
12d13 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
12d14 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
12d15 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 PT;. }.. /* Ma
12d16 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65 20 ke sure we have
12d17 6e 6f 74 20 68 69 74 20 61 6e 79 20 63 72 69 74 not hit any crit
12d18 69 63 61 6c 20 65 72 72 6f 72 73 2e 0a 20 20 2a ical errors.. *
12d19 2f 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 / . assert( pPa
12d1a 67 65 72 21 3d 30 20 29 3b 0a 20 20 2a 70 70 50 ger!=0 );. *ppP
12d1b 61 67 65 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 age = 0;.. /* I
12d1c 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 f this is the fi
12d1d 72 73 74 20 70 61 67 65 20 61 63 63 65 73 73 65 rst page accesse
12d1e 64 2c 20 74 68 65 6e 20 67 65 74 20 61 20 53 48 d, then get a SH
12d1f 41 52 45 44 20 6c 6f 63 6b 0a 20 20 2a 2a 20 6f ARED lock. ** o
12d20 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
12d21 69 6c 65 2e 20 70 61 67 65 72 53 68 61 72 65 64 ile. pagerShared
12d22 4c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f Lock() is a no-o
12d23 70 20 69 66 20 0a 20 20 2a 2a 20 61 20 64 61 74 p if . ** a dat
12d24 61 62 61 73 65 20 6c 6f 63 6b 20 69 73 20 61 6c abase lock is al
12d25 72 65 61 64 79 20 68 65 6c 64 2e 0a 20 20 2a 2f ready held.. */
12d26 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53 68 61 . rc = pagerSha
12d27 72 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b redLock(pPager);
12d28 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
12d29 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
12d2a 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
12d2b 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
12d2c 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te!=PAGER_UNLOCK
12d2d 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 );.. rc = sqli
12d2e 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70 te3PcacheFetch(p
12d2f 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 Pager->pPCache,
12d30 70 67 6e 6f 2c 20 31 2c 20 26 70 50 67 29 3b 0a pgno, 1, &pPg);.
12d31 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12d32 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
12d33 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 n rc;. }. asse
12d34 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 3d 3d 70 rt( pPg->pgno==p
12d35 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 gno );. assert(
12d36 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 pPg->pPager==pP
12d37 61 67 65 72 20 7c 7c 20 70 50 67 2d 3e 70 50 61 ager || pPg->pPa
12d38 67 65 72 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 ger==0 );. if(
12d39 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 pPg->pPager==0 )
12d3a 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 {. /* The pag
12d3b 65 72 20 63 61 63 68 65 20 68 61 73 20 63 72 65 er cache has cre
12d3c 61 74 65 64 20 61 20 6e 65 77 20 70 61 67 65 2e ated a new page.
12d3d 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65 Its content nee
12d3e 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65 ds to . ** be
12d3f 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 initialized..
12d40 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d 61 */. int nMa
12d41 78 3b 0a 20 20 20 20 50 41 47 45 52 5f 49 4e 43 x;. PAGER_INC
12d42 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 29 R(pPager->nMiss)
12d43 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 67 65 ;. pPg->pPage
12d44 72 20 3d 20 70 50 61 67 65 72 3b 0a 0a 20 20 20 r = pPager;..
12d45 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
12d46 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
12d47 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 er, &nMax);.
12d48 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12d49 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
12d4a 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 e3PagerUnref(pPg
12d4b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
12d4c 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 rc;. }.. i
12d4d 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e f( nMax<(int)pgn
12d4e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f o || MEMDB || no
12d4f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 Content ){.
12d50 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 if( pgno>pPager
12d51 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 ->mxPgno ){.
12d52 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
12d53 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 Unref(pPg);.
12d54 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
12d55 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a E_FULL;. }.
12d56 20 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 if( noCont
12d57 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ent ){. /
12d58 2a 20 46 61 69 6c 75 72 65 20 74 6f 20 73 65 74 * Failure to set
12d59 20 74 68 65 20 62 69 74 73 20 69 6e 20 74 68 65 the bits in the
12d5a 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 InJournal bit-v
12d5b 65 63 74 6f 72 73 20 69 73 20 62 65 6e 69 67 6e ectors is benign
12d5c 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20 .. ** It
12d5d 6d 65 72 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 merely means tha
12d5e 74 20 77 65 20 6d 69 67 68 74 20 64 6f 20 73 6f t we might do so
12d5f 6d 65 20 65 78 74 72 61 20 77 6f 72 6b 20 74 6f me extra work to
12d60 20 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 journal a .
12d61 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 ** page that
12d62 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
12d63 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 o be journaled.
12d64 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 62 Nevertheless, b
12d65 65 20 73 75 72 65 20 0a 20 20 20 20 20 20 20 20 e sure .
12d66 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 63 ** to test the c
12d67 61 73 65 20 77 68 65 72 65 20 61 20 6d 61 6c 6c ase where a mall
12d68 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 oc error occurs
12d69 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 while trying to
12d6a 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 set . **
12d6b 61 20 62 69 74 20 69 6e 20 61 20 62 69 74 20 76 a bit in a bit v
12d6c 65 63 74 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a ector.. *
12d6d 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 /. sqlite
12d6e 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
12d6f 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 69 66 oc();. if
12d70 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e ( pgno<=pPager->
12d71 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 dbOrigSize ){.
12d72 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 TESTONLY
12d73 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 33 ( rc = ) sqlite3
12d74 42 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 BitvecSet(pPager
12d75 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 ->pInJournal, pg
12d76 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 no);. t
12d77 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c estcase( rc==SQL
12d78 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 ITE_NOMEM );.
12d79 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 54 }. T
12d7a 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 ESTONLY( rc = )
12d7b 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 addToSavepointBi
12d7c 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 67 tvecs(pPager, pg
12d7d 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 no);. tes
12d7e 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 tcase( rc==SQLIT
12d7f 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
12d80 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e sqlite3EndBen
12d81 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 ignMalloc();.
12d82 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
12d83 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 memset(pPg->pD
12d84 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e ata, 0, pPager->
12d85 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 pageSize);.
12d86 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 }. IOTRACE
12d87 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 (("ZERO %p %d\n"
12d88 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 , pPager, pgno))
12d89 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
12d8a 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e assert( pPg->
12d8b 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 pPager==pPager )
12d8c 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 ;. rc = rea
12d8d 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a 20 20 dDbPage(pPg);.
12d8e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
12d8f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
12d90 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28 70 pagerDropPage(p
12d91 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 Pg);. ret
12d92 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
12d93 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }.#ifdef SQL
12d94 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
12d95 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 pPg->pageHas
12d96 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 h = pager_pageha
12d97 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a sh(pPg);.#endif.
12d98 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
12d99 54 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 The requested pa
12d9a 67 65 20 69 73 20 69 6e 20 74 68 65 20 70 61 67 ge is in the pag
12d9b 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 e cache. */.
12d9c 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
12d9d 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 7d 0a 0a 20 r->nHit);. }..
12d9e 20 2a 70 70 50 61 67 65 20 3d 20 70 50 67 3b 0a *ppPage = pPg;.
12d9f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12da0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 OK;.}../*.** Acq
12da1 75 69 72 65 20 61 20 70 61 67 65 20 69 66 20 69 uire a page if i
12da2 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 t is already in
12da3 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 the in-memory ca
12da4 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 che. Do.** not
12da5 72 65 61 64 20 74 68 65 20 70 61 67 65 20 66 72 read the page fr
12da6 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 72 6e om disk. Return
12da7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
12da8 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 20 e page,.** or 0
12da9 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e if the page is n
12daa 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41 6c 73 ot in cache. Als
12dab 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66 20 74 o, return 0 if t
12dac 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 he .** pager is
12dad 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 in PAGER_UNLOCK
12dae 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 state when this
12daf 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
12db0 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65 ed,.** or if the
12db1 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 pager is in an
12db2 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 error state othe
12db3 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 r than SQLITE_FU
12db4 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c LL..**.** See al
12db5 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 so sqlite3PagerG
12db6 65 74 28 29 2e 20 20 54 68 65 20 64 69 66 66 65 et(). The diffe
12db7 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 rence between th
12db8 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e is routine.** an
12db9 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 d sqlite3PagerGe
12dba 74 28 29 20 69 73 20 74 68 61 74 20 5f 67 65 74 t() is that _get
12dbb 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 () will go to th
12dbc 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61 64 0a e disk and read.
12dbd 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 69 ** in the page i
12dbe 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f f the page is no
12dbf 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 t already in cac
12dc0 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e he. This routin
12dc1 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c e.** returns NUL
12dc2 4c 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 L if the page is
12dc3 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 not in cache or
12dc4 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 if a disk I/O e
12dc5 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 rror .** has eve
12dc6 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 r happened..*/.S
12dc7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 QLITE_PRIVATE Db
12dc8 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 Page *sqlite3Pag
12dc9 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a erLookup(Pager *
12dca 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e pPager, Pgno pgn
12dcb 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 o){. PgHdr *pPg
12dcc 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
12dcd 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 pPager!=0 );. a
12dce 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 ssert( pgno!=0 )
12dcf 3b 0a 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 ;.. if( (pPager
12dd0 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 ->state!=PAGER_U
12dd1 4e 4c 4f 43 4b 29 0a 20 20 20 26 26 20 28 70 50 NLOCK). && (pP
12dd2 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 ager->errCode==S
12dd3 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
12dd4 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
12dd5 49 54 45 5f 46 55 4c 4c 29 0a 20 20 29 7b 0a 20 ITE_FULL). ){.
12dd6 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
12dd7 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 Fetch(pPager->pP
12dd8 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 Cache, pgno, 0,
12dd9 26 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 &pPg);. }.. re
12dda 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a turn pPg;.}../*.
12ddb 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 61 67 ** Release a pag
12ddc 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a e reference..**.
12ddd 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72 ** If the number
12dde 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
12ddf 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70 20 o the page drop
12de0 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 to zero, then th
12de1 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64 64 e.** page is add
12de2 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c 69 ed to the LRU li
12de3 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 65 st. When all re
12de4 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c 20 ferences to all
12de5 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 6c pages.** are rel
12de6 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 63 eased, a rollbac
12de7 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 k occurs and the
12de8 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
12de9 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d 6f abase is.** remo
12dea 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ved..*/.SQLITE_P
12deb 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
12dec 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 te3PagerUnref(Db
12ded 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69 66 Page *pPg){. if
12dee 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61 67 ( pPg ){. Pag
12def 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
12df0 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73 71 ->pPager;. sq
12df1 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 lite3PcacheRelea
12df2 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61 67 se(pPg);. pag
12df3 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 erUnlockIfUnused
12df4 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a (pPager);. }.}.
12df5 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61 ./*.** If the ma
12df6 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 in journal file
12df7 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
12df8 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72 65 20 opened, ensure
12df9 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75 62 2d that the.** sub-
12dfa 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
12dfb 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68 65 open too. If the
12dfc 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 main journal is
12dfd 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74 68 not open,.** th
12dfe 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
12dff 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 no-op..**.** SQ
12e00 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
12e01 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e ned if everythin
12e02 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69 6e 67 g goes according
12e03 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41 6e to plan. .** An
12e04 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 SQLITE_IOERR_XX
12e05 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 X error code is
12e06 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 63 61 returned if a ca
12e07 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 ll to .** sqlite
12e08 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c 73 2e 3OsOpen() fails.
12e09 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
12e0a 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50 61 penSubJournal(Pa
12e0b 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
12e0c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
12e0d 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e OK;. if( isOpen
12e0e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 (pPager->jfd) &&
12e0f 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d !isOpen(pPager-
12e10 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66 >sjfd) ){. if
12e11 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
12e12 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
12e13 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
12e14 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 || pPager->subjI
12e15 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 20 nMemory ){.
12e16 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e sqlite3MemJourn
12e17 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 alOpen(pPager->s
12e18 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b jfd);. }else{
12e19 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
12e1a 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72 rOpentemp(pPager
12e1b 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 , pPager->sjfd,
12e1c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a SQLITE_OPEN_SUBJ
12e1d 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 OURNAL);. }.
12e1e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
12e1f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
12e20 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
12e21 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
12e22 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72 61 every write tra
12e23 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 nsaction..** The
12e24 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 re must already
12e25 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 be a RESERVED or
12e26 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
12e27 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
12e28 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 68 .** file when th
12e29 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
12e2a 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e lled..**.** Open
12e2b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12e2c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 67 e for pager pPag
12e2d 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20 6a er and write a j
12e2e 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a ournal header.**
12e2f 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
12e30 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61 72 it. If there ar
12e31 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 e active savepoi
12e32 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 75 nts, open the su
12e33 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 20 b-journal.** as
12e34 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 74 well. This funct
12e35 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ion is only used
12e36 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 when the journa
12e37 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 20 l file is being
12e38 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 72 .** opened to wr
12e39 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6c ite a rollback l
12e3a 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 og for a transac
12e3b 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 20 tion. It is not
12e3c 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f 70 used .** when op
12e3d 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 72 ening a hot jour
12e3e 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c 6c nal file to roll
12e3f 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 it back..**.**
12e40 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 If the journal f
12e41 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f ile is already o
12e42 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20 62 pen (as it may b
12e43 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d e in exclusive m
12e44 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 ode),.** then th
12e45 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 74 is function just
12e46 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e 61 writes a journa
12e47 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20 l header to the
12e48 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 start of the.**
12e49 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69 6c already open fil
12e4a 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 e. .**.** Whethe
12e4b 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 r or not the jou
12e4c 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 rnal file is ope
12e4d 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 ned by this func
12e4e 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 67 tion, the.** Pag
12e4f 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 er.pInJournal bi
12e50 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 69 tvec structure i
12e51 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a s allocated..**.
12e52 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
12e53 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e _OK if everythin
12e54 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e g is successful.
12e55 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 Otherwise, retu
12e56 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f rn .** SQLITE_NO
12e57 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65 6d MEM if the attem
12e58 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 50 pt to allocate P
12e59 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 ager.pInJournal
12e5a 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e fails, or .** an
12e5b 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
12e5c 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 69 f opening or wri
12e5d 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c ting the journal
12e5e 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a file fails..*/.
12e5f 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
12e60 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 _open_journal(Pa
12e61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
12e62 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
12e63 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
12e64 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
12e65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 turn code */. s
12e66 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e qlite3_vfs * con
12e67 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 st pVfs = pPager
12e68 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f 63 ->pVfs; /* Loc
12e69 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73 20 al cache of vfs
12e6a 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73 pointer */.. as
12e6b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
12e6c 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 ate>=PAGER_RESER
12e6d 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 VED );. assert(
12e6e 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 pPager->useJour
12e6f 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nal );. assert(
12e70 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
12e71 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f nal==0 );. . /
12e72 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20 * If already in
12e73 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c the error state,
12e74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
12e75 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 s a no-op. */.
12e76 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
12e77 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ode ){. retur
12e78 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 n pPager->errCod
12e79 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 4f 44 e;. }.. /* TOD
12e7a 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c 79 20 O: Is it really
12e7b 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 74 20 possible to get
12e7c 68 65 72 65 20 77 69 74 68 20 64 62 53 69 7a 65 here with dbSize
12e7d 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 Valid==0? If not
12e7e 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 ,. ** the call
12e7f 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f 75 6e to PagerPagecoun
12e80 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d 6f 76 t() can be remov
12e81 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 ed.. */. testc
12e82 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64 62 53 ase( pPager->dbS
12e83 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 izeValid==0 );.
12e84 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12e85 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 ecount(pPager, 0
12e86 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 );.. pPager->pI
12e87 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 nJournal = sqlit
12e88 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 e3BitvecCreate(p
12e89 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a Pager->dbSize);.
12e8a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 if( pPager->pI
12e8b 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 nJournal==0 ){.
12e8c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
12e8d 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f _NOMEM;. }.. /
12e8e 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e * Open the journ
12e8f 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20 69 73 al file if it is
12e90 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 not already ope
12e91 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f n. */. if( !isO
12e92 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
12e93 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ){. if( pPag
12e94 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
12e95 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
12e96 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 DE_MEMORY ){.
12e97 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 sqlite3MemJou
12e98 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d rnalOpen(pPager-
12e99 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 >jfd);. }else
12e9a 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e {. const in
12e9b 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20 20 20 t flags =
12e9c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
12e9d 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e FS flags to open
12e9e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
12e9f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f . SQLITE_
12ea0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 OPEN_READWRITE|S
12ea1 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
12ea2 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 E|. (pPag
12ea3 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a er->tempFile ? .
12ea4 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
12ea5 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
12ea6 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e LOSE|SQLITE_OPEN
12ea7 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a _TEMP_JOURNAL):.
12ea8 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
12ea9 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
12eaa 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29 3b 0a NAL). );.
12eab 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
12eac 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
12ead 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c E. rc = sql
12eae 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 ite3JournalOpen(
12eaf 0a 20 20 20 20 20 20 20 20 20 20 70 56 66 73 2c . pVfs,
12eb0 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 pPager->zJourna
12eb1 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 l, pPager->jfd,
12eb2 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 flags, jrnlBuffe
12eb3 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a 20 20 rSize(pPager).
12eb4 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 );.#else.
12eb5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
12eb6 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 sOpen(pVfs, pPag
12eb7 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 er->zJournal, pP
12eb8 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 ager->jfd, flags
12eb9 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 , 0);.#endif.
12eba 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 }. assert( r
12ebb 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
12ebc 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
12ebd 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f fd) );. }... /
12ebe 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 72 73 * Write the firs
12ebf 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
12ec0 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
12ec1 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20 file and open .
12ec2 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 ** the sub-jour
12ec3 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 nal if necessary
12ec4 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d .. */. if( rc=
12ec5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12ec6 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b /* TODO: Check
12ec7 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 if all of these
12ec8 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65 71 75 are really requ
12ec9 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70 50 61 ired. */. pPa
12eca 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
12ecb 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 = pPager->dbSize
12ecc 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f ;. pPager->jo
12ecd 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 urnalStarted = 0
12ece 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 ;. pPager->ne
12ecf 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 edSync = 0;.
12ed0 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 pPager->nRec = 0
12ed1 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f ;. pPager->jo
12ed2 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 urnalOff = 0;.
12ed3 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 pPager->setMas
12ed4 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 ter = 0;. pPa
12ed5 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 ger->journalHdr
12ed6 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 = 0;. rc = wr
12ed7 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 iteJournalHdr(pP
12ed8 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ager);. }. if(
12ed9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
12eda 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 & pPager->nSavep
12edb 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63 20 3d oint ){. rc =
12edc 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 openSubJournal(
12edd 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 pPager);. }..
12ede 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12edf 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
12ee0 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 BitvecDestroy(pP
12ee1 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
12ee2 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 );. pPager->p
12ee3 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 InJournal = 0;.
12ee4 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
12ee5 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 }../*.** Begin a
12ee6 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
12ee7 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 on on the specif
12ee8 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65 63 74 ied pager object
12ee9 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 74 65 . If a .** write
12eea 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 -transaction has
12eeb 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 70 already been op
12eec 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 ened, this funct
12eed 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
12eee 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 46 **.** If the exF
12eef 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 lag argument is
12ef0 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 71 75 false, then acqu
12ef1 69 72 65 20 61 74 20 6c 65 61 73 74 20 61 20 52 ire at least a R
12ef2 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 ESERVED.** lock
12ef3 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
12ef4 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 67 20 file. If exFlag
12ef5 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 63 is true, then ac
12ef6 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 0a 2a quire at least.*
12ef7 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c * an EXCLUSIVE l
12ef8 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61 20 6c ock. If such a l
12ef9 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 ock is already h
12efa 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 eld, no locking
12efb 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65 .** functions ne
12efc 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a ed be called..**
12efd 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 6e .** If this is n
12efe 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 20 6f ot a temporary o
12eff 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 r in-memory file
12f00 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 6e 61 and, the journa
12f01 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 l file is .** op
12f02 65 6e 65 64 20 69 66 20 69 74 20 68 61 73 20 6e ened if it has n
12f03 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64 79 2e ot been already.
12f04 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 79 For a temporary
12f05 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 6e 69 file, the openi
12f06 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f ng .** of the jo
12f07 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 urnal file is de
12f08 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65 ferred until the
12f09 72 65 20 69 73 20 61 6e 20 61 63 74 75 61 6c 20 re is an actual
12f0a 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 69 74 need to .** writ
12f0b 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c e to the journal
12f0c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 6e 64 . TODO: Why hand
12f0d 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c le temporary fil
12f0e 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 3f 0a es differently?.
12f0f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 **.** If the jou
12f10 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 rnal file is ope
12f11 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20 69 73 ned (or if it is
12f12 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 2c 20 already open),
12f13 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 then a.** journa
12f14 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74 l-header is writ
12f15 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61 72 74 ten to the start
12f16 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 of it..**.** If
12f17 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 the subjInMemor
12f18 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f y argument is no
12f19 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 n-zero, then any
12f1a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 sub-journal ope
12f1b 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 ned.** within th
12f1c 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 is transaction w
12f1d 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 61 73 ill be opened as
12f1e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 an in-memory fi
12f1f 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 73 20 le. This.** has
12f20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74 68 65 no effect if the
12f21 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 sub-journal is
12f22 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 28 already opened (
12f23 61 73 20 69 74 20 6d 61 79 20 62 65 20 77 68 65 as it may be whe
12f24 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 6e 20 n.** running in
12f25 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 20 exclusive mode)
12f26 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 or if the transa
12f27 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 ction does not r
12f28 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 62 2d equire a.** sub-
12f29 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 65 20 journal. If the
12f2a 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 subjInMemory arg
12f2b 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c 20 74 ument is zero, t
12f2c 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 hen any required
12f2d 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 .** sub-journal
12f2e 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 is implemented i
12f2f 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 61 67 n-memory if pPag
12f30 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f er is an in-memo
12f31 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a 2a 2a ry database, .**
12f32 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65 6d 70 or using a temp
12f33 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68 65 72 orary file other
12f34 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f wise..*/.SQLITE_
12f35 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
12f36 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 50 61 te3PagerBegin(Pa
12f37 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
12f38 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 75 62 exFlag, int sub
12f39 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 69 6e jInMemory){. in
12f3a 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
12f3b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
12f3c 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 er->state!=PAGER
12f3d 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 50 61 _UNLOCK );. pPa
12f3e 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 ger->subjInMemor
12f3f 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e 4d 65 y = (u8)subjInMe
12f40 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50 61 67 mory;. if( pPag
12f41 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
12f42 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61 _SHARED ){. a
12f43 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
12f44 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a InJournal==0 );.
12f45 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d assert( !MEM
12f46 44 42 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74 DB && !pPager->t
12f47 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 20 20 empFile );..
12f48 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 53 45 /* Obtain a RESE
12f49 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 RVED lock on the
12f4a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
12f4b 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 70 61 If the exFlag pa
12f4c 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a 20 69 rameter. ** i
12f4d 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d s true, then imm
12f4e 65 64 69 61 74 65 6c 79 20 75 70 67 72 61 64 65 ediately upgrade
12f4f 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58 43 4c this to an EXCL
12f50 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a USIVE lock. The.
12f51 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 ** busy-hand
12f52 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e ler callback can
12f53 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 75 70 be used when up
12f54 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20 45 grading to the E
12f55 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a 2a 20 XCLUSIVE. **
12f56 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 lock, but not wh
12f57 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 en obtaining the
12f58 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a RESERVED lock..
12f59 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
12f5a 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 sqlite3OsLock(pP
12f5b 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 ager->fd, RESERV
12f5c 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 ED_LOCK);. if
12f5d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
12f5e 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
12f5f 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 >state = PAGER_R
12f60 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20 69 ESERVED;. i
12f61 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 f( exFlag ){.
12f62 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
12f63 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 wait_on_lock(pPa
12f64 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c ger, EXCLUSIVE_L
12f65 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 OCK);. }.
12f66 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
12f67 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b he required lock
12f68 73 20 77 65 72 65 20 73 75 63 63 65 73 73 66 75 s were successfu
12f69 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 6f 70 lly obtained, op
12f6a 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 en the journal.
12f6b 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 20 77 ** file and w
12f6c 72 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a rite the first j
12f6d 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 74 6f ournal-header to
12f6e 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 it.. */.
12f6f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12f70 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 K && pPager->use
12f71 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26 26 20 Journal. &&
12f72 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
12f73 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode!=PAGER_JOURN
12f74 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 ALMODE_OFF .
12f75 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 ){. rc = pa
12f76 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c ger_open_journal
12f77 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
12f78 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4f 70 }else if( isOp
12f79 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
12f7a 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
12f7b 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 alOff==0 ){.
12f7c 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
12f7d 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 77 when the pager w
12f7e 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d as in exclusive-
12f7f 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 access mode the
12f80 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 last. ** time
12f81 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 69 74 a (read or writ
12f82 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 e) transaction w
12f83 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 as successfully
12f84 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a concluded. **
12f85 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 by this connect
12f86 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 ion. Instead of
12f87 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 deleting the jou
12f88 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 61 73 rnal file it was
12f89 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 . ** kept op
12f8a 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 77 61 en and either wa
12f8b 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 30 s truncated to 0
12f8c 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 68 65 bytes or its he
12f8d 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 ader was. **
12f8e 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 overwritten with
12f8f 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 zeros.. */.
12f90 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
12f91 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 r->nRec==0 );.
12f92 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12f93 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 ->dbOrigSize==0
12f94 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
12f95 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
12f96 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d l==0 );. rc =
12f97 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 pager_open_jour
12f98 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d nal(pPager);. }
12f99 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 .. PAGERTRACE((
12f9a 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c "TRANSACTION %d\
12f9b 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
12f9c 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 er)));. assert(
12f9d 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d !isOpen(pPager-
12f9e 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d >jfd) || pPager-
12f9f 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c >journalOff>0 ||
12fa0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
12fa1 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
12fa2 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73 ../*.** Mark a s
12fa3 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 20 ingle data page
12fa4 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 as writeable. Th
12fa5 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 e page is writte
12fa6 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d n into the .** m
12fa7 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 ain journal or s
12fa8 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65 ub-journal as re
12fa9 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 70 quired. If the p
12faa 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 age is written i
12fab 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 nto.** one of th
12fac 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20 e journals, the
12fad 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
12fae 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 t is set in the
12faf 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 .** Pager.pInJou
12fb0 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 20 rnal bitvec and
12fb1 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 the PagerSavepoi
12fb2 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 nt.pInSavepoint
12fb3 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e bitvecs.** of an
12fb4 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 y open savepoint
12fb5 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 s as appropriate
12fb6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
12fb7 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64 pager_write(PgHd
12fb8 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20 r *pPg){. void
12fb9 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 *pData = pPg->pD
12fba 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 ata;. Pager *pP
12fbb 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
12fbc 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 er;. int rc = S
12fbd 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 QLITE_OK;.. /*
12fbe 43 68 65 63 6b 20 66 6f 72 20 65 72 72 6f 72 73 Check for errors
12fbf 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 . */. if( pPag
12fc0 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 20 0a er->errCode ){ .
12fc1 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 return pPage
12fc2 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a r->errCode;. }.
12fc3 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 72 65 if( pPager->re
12fc4 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 65 adOnly ){. re
12fc5 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d turn SQLITE_PERM
12fc6 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
12fc7 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 !pPager->setMas
12fc8 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b 5f ter );.. CHECK_
12fc9 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a PAGE(pPg);.. /*
12fca 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 Mark the page a
12fcb 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 65 s dirty. If the
12fcc 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 64 page has alread
12fcd 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 20 y been written.
12fce 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ** to the journ
12fcf 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 al then we can r
12fd0 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 79 eturn right away
12fd1 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
12fd2 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 PcacheMakeDirty(
12fd3 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 65 pPg);. if( page
12fd4 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 InJournal(pPg) &
12fd5 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 & !subjRequiresP
12fd6 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 age(pPg) ){.
12fd7 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
12fd8 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b ed = 1;. }else{
12fd9 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67 .. /* If we g
12fda 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 et this far, it
12fdb 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 means that the p
12fdc 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a age needs to be.
12fdd 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 ** written t
12fde 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f o the transactio
12fdf 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 n journal or the
12fe0 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 ckeckpoint jour
12fe1 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f nal. ** or bo
12fe2 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a th.. **. *
12fe3 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f * First check to
12fe4 20 73 65 65 20 74 68 61 74 20 74 68 65 20 74 72 see that the tr
12fe5 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 ansaction journa
12fe6 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20 20 20 l exists and.
12fe7 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20 69 66 ** create it if
12fe8 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 it does not..
12fe9 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
12fea 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d pPager->state!=
12feb 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a PAGER_UNLOCK );.
12fec 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12fed 50 61 67 65 72 42 65 67 69 6e 28 70 50 61 67 65 PagerBegin(pPage
12fee 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 73 75 r, 0, pPager->su
12fef 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 20 20 bjInMemory);.
12ff0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12ff1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
12ff2 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
12ff3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12ff4 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 >state>=PAGER_RE
12ff5 53 45 52 56 45 44 20 29 3b 0a 20 20 20 20 69 66 SERVED );. if
12ff6 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
12ff7 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 65 72 ->jfd) && pPager
12ff8 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 0a 20 20 20 ->useJournal.
12ff9 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 72 && pPager
12ffa 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 ->journalMode!=P
12ffb 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
12ffc 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 72 63 _OFF ){. rc
12ffd 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f = pager_open_jo
12ffe 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
12fff 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13000 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
13001 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
13002 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
13003 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 = 1;. . /*
13004 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 The transaction
13005 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73 journal now exis
13006 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61 ts and we have a
13007 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a RESERVED or an.
13008 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 ** EXCLUSIVE
13009 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 lock on the mai
1300a 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
1300b 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 Write the curr
1300c 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20 ent page to.
1300d 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ** the transacti
1300e 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74 on journal if it
1300f 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c is not there al
13010 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 ready.. */.
13011 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f 75 if( !pageInJou
13012 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 73 4f rnal(pPg) && isO
13013 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13014 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
13015 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d g->pgno<=pPager-
13016 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 >dbOrigSize ){.
13017 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75 6d u32 cksum
13018 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a ;. char *
13019 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20 20 pData2;..
1301a 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e 65 /* We should ne
1301b 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68 65 ver write to the
1301c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
1301d 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20 20 e page that.
1301e 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 ** contains
1301f 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 the database loc
13020 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ks. The followi
13021 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66 69 ng assert verifi
13022 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 es. ** th
13023 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f at we do not. */
13024 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
13025 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45 pPg->pgno!=PAGE
13026 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
13027 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44 61 ) );. pDa
13028 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50 61 ta2 = CODEC2(pPa
13029 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d ger, pData, pPg-
1302a 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20 20 20 >pgno, 7);.
1302b 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65 72 cksum = pager
1302c 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 28 _cksum(pPager, (
1302d 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20 20 u8*)pData2);.
1302e 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 rc = write3
1302f 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
13030 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e d, pPager->journ
13031 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e 6f alOff, pPg->pgno
13032 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
13033 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13034 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
13035 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
13036 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61 ager->jfd, pData
13037 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 2, pPager->pageS
13038 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ize,.
13039 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1303a 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
1303b 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 20 20 alOff + 4);.
1303c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
1303d 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61 67 urnalOff += pPag
1303e 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 3b 0a er->pageSize+4;.
1303f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13040 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13041 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
13042 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 rc = write32bit
13043 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 s(pPager->jfd, p
13044 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
13045 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 20 f, cksum);.
13046 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
13047 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 20 rnalOff += 4;.
13048 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13049 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 20 25 IOTRACE(("JOUT %
1304a 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22 2c p %d %lld %d\n",
1304b 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 pPager, pPg->pg
1304c 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 no, .
1304d 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
1304e 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 65 72 urnalOff, pPager
1304f 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a 20 20 ->pageSize));.
13050 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 PAGER_INCR
13051 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 (sqlite3_pager_w
13052 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20 20 ritej_count);.
13053 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
13054 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 (("JOURNAL %d pa
13055 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 ge %d needSync=%
13056 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c d hash(%08x)\n",
13057 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 . PA
13058 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
13059 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 Pg->pgno, .
1305a 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 ((pPg->f
1305b 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
1305c 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 67 65 SYNC)?1:0), page
1305d 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29 r_pagehash(pPg))
1305e 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 );.. /* E
1305f 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f 72 20 ven if an IO or
13060 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 20 6f diskfull error o
13061 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 6a 6f ccurred while jo
13062 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a 20 20 urnalling the.
13063 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e ** page in
13064 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76 65 the block above
13065 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 2d 73 , set the need-s
13066 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 68 65 ync flag for the
13067 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a page.. *
13068 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68 65 * Otherwise, whe
13069 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f n the transactio
1306a 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b n is rolled back
1306b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e 0a 20 , the logic in.
1306c 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 62 61 ** playba
1306d 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 77 69 ck_one_page() wi
1306e 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68 ll think that th
1306f 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 e page needs to
13070 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 20 20 be restored.
13071 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 64 61 ** in the da
13072 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 6e 64 tabase file. And
13073 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 if an IO error
13074 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69 occurs while doi
13075 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a ng so,. *
13076 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 69 6f * then corruptio
13077 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20 20 n may follow..
13078 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
13079 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f if( !pPager->no
1307a 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 Sync ){.
1307b 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
1307c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b PGHDR_NEED_SYNC;
1307d 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
1307e 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b r->needSync = 1;
1307f 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 . }..
13080 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 /* An error
13081 68 61 73 20 6f 63 63 75 72 72 65 64 20 77 72 69 has occurred wri
13082 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 ting to the jour
13083 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 0a 20 nal file. The .
13084 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 ** transa
13085 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f ction will be ro
13086 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 lled back by the
13087 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a 20 20 layer above..
13088 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
13089 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1308a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1308b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1308c 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 70 50 }.. pP
1308d 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 ager->nRec++;.
1308e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1308f 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
13090 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 !=0 );. r
13091 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 c = sqlite3Bitve
13092 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e cSet(pPager->pIn
13093 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 Journal, pPg->pg
13094 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 no);. tes
13095 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 tcase( rc==SQLIT
13096 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
13097 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
13098 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d QLITE_OK || rc==
13099 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
1309a 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20 61 64 rc |= ad
1309b 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 dToSavepointBitv
1309c 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d ecs(pPager, pPg-
1309d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
1309e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1309f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 K ){. a
130a0 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
130a1 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
130a2 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
130a3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
130a4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 }else{. i
130a5 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 f( !pPager->jour
130a6 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20 21 70 nalStarted && !p
130a7 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b Pager->noSync ){
130a8 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e . pPg->
130a9 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e flags |= PGHDR_N
130aa 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 EED_SYNC;.
130ab 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 pPager->need
130ac 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 Sync = 1;.
130ad 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41 47 45 }. PAGE
130ae 52 54 52 41 43 45 28 28 22 41 50 50 45 4e 44 20 RTRACE(("APPEND
130af 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 %d page %d needS
130b0 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 ync=%d\n",.
130b1 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 PAGER
130b2 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
130b3 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 >pgno,.
130b4 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 ((pPg->fla
130b5 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
130b6 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20 20 20 NC)?1:0)));.
130b7 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
130b8 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 /* If the state
130b9 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 ment journal is
130ba 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 67 open and the pag
130bb 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c 0a e is not in it,.
130bc 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69 74 ** then writ
130bd 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 e the current pa
130be 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65 6d ge to the statem
130bf 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f ent journal. No
130c0 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 te that. ** t
130c1 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 he statement jou
130c2 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66 66 rnal format diff
130c3 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 ers from the sta
130c4 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66 6f ndard journal fo
130c5 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20 74 rmat. ** in t
130c6 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68 65 hat it omits the
130c7 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20 74 checksums and t
130c8 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 2a he header.. *
130c9 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a 52 65 /. if( subjRe
130ca 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20 quiresPage(pPg)
130cb 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 75 ){. rc = su
130cc 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 bjournalPage(pPg
130cd 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
130ce 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 61 /* Update the da
130cf 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e 64 20 tabase size and
130d0 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61 return.. */. a
130d1 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
130d2 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 tate>=PAGER_SHAR
130d3 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 ED );. if( pPag
130d4 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e er->dbSize<pPg->
130d5 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 pgno ){. pPag
130d6 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 er->dbSize = pPg
130d7 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72 65 ->pgno;. }. re
130d8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
130d9 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20 70 61 * Mark a data pa
130da 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e ge as writeable.
130db 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 This routine mu
130dc 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 st be called bef
130dd 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20 63 ore .** making c
130de 68 61 6e 67 65 73 20 74 6f 20 61 20 70 61 67 65 hanges to a page
130df 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 . The caller mus
130e0 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 t check the retu
130e1 72 6e 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 rn value .** of
130e2 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e this function an
130e3 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e 6f 74 d be careful not
130e4 20 74 6f 20 63 68 61 6e 67 65 20 61 6e 79 20 70 to change any p
130e5 61 67 65 20 64 61 74 61 20 75 6e 6c 65 73 73 20 age data unless
130e6 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
130e7 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
130e8 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 OK..**.** The di
130e9 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e fference between
130ea 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
130eb 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 nd pager_write()
130ec 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a is that this.**
130ed 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 function also d
130ee 65 61 6c 73 20 77 69 74 68 20 74 68 65 20 73 70 eals with the sp
130ef 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 65 ecial case where
130f0 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 2 or more pages
130f1 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 69 6e .** fit on a sin
130f2 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e gle disk sector.
130f3 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c In this case al
130f4 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 l co-resident pa
130f5 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 ges.** must have
130f6 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
130f7 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
130f8 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 e before returni
130f9 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ng..**.** If an
130fa 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 error occurs, SQ
130fb 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e LITE_NOMEM or an
130fc 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
130fd 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 73 s returned.** as
130fe 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 4f 74 appropriate. Ot
130ff 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f herwise, SQLITE_
13100 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 OK..*/.SQLITE_PR
13101 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
13102 33 50 61 67 65 72 57 72 69 74 65 28 44 62 50 61 3PagerWrite(DbPa
13103 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 ge *pDbPage){.
13104 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13105 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 OK;.. PgHdr *pP
13106 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 50 g = pDbPage;. P
13107 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
13108 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 Pg->pPager;. Pg
13109 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f no nPagePerSecto
1310a 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65 63 r = (pPager->sec
1310b 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e torSize/pPager->
1310c 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 69 66 pageSize);.. if
1310d 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 ( nPagePerSector
1310e 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e >1 ){. Pgno n
1310f 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20 PageCount;
13110 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
13111 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
13112 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
13113 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20 . Pgno pg1;
13114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13115 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 * First page of
13116 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 the sector pPg i
13117 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f s located on. */
13118 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 . int nPage;
13119 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1311a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
1311b 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 s starting at pg
1311c 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 1 to journal */.
1311d 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 int ii;
1311e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1311f 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
13120 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e . int needSyn
13121 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f c = 0; /
13122 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 * True if any pa
13123 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e 45 45 ge has PGHDR_NEE
13124 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20 20 2f D_SYNC */.. /
13125 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f 74 53 * Set the doNotS
13126 79 6e 63 20 66 6c 61 67 20 74 6f 20 31 2e 20 54 ync flag to 1. T
13127 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 77 his is because w
13128 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 61 e cannot allow a
13129 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 journal. **
1312a 68 65 61 64 65 72 20 74 6f 20 62 65 20 77 72 69 header to be wri
1312b 74 74 65 6e 20 62 65 74 77 65 65 6e 20 74 68 65 tten between the
1312c 20 70 61 67 65 73 20 6a 6f 75 72 6e 61 6c 65 64 pages journaled
1312d 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
1312e 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 n.. */. as
1312f 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a sert( !MEMDB );.
13130 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
13131 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 er->doNotSync==0
13132 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e );. pPager->
13133 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 31 3b 0a 0a doNotSync = 1;..
13134 20 20 20 20 2f 2a 20 54 68 69 73 20 74 72 69 63 /* This tric
13135 6b 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 62 k assumes that b
13136 6f 74 68 20 74 68 65 20 70 61 67 65 2d 73 69 7a oth the page-siz
13137 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a e and sector-siz
13138 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61 6e 20 e are. ** an
13139 69 6e 74 65 67 65 72 20 70 6f 77 65 72 20 6f 66 integer power of
1313a 20 32 2e 20 49 74 20 73 65 74 73 20 76 61 72 69 2. It sets vari
1313b 61 62 6c 65 20 70 67 31 20 74 6f 20 74 68 65 20 able pg1 to the
1313c 69 64 65 6e 74 69 66 69 65 72 0a 20 20 20 20 2a identifier. *
1313d 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 70 * of the first p
1313e 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74 6f age of the secto
1313f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65 64 r pPg is located
13140 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 on.. */.
13141 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70 67 6e pg1 = ((pPg->pgn
13142 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65 50 65 o-1) & ~(nPagePe
13143 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20 31 3b rSector-1)) + 1;
13144 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 .. sqlite3Pag
13145 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
13146 65 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61 67 er, (int *)&nPag
13147 65 43 6f 75 6e 74 29 3b 0a 20 20 20 20 69 66 28 eCount);. if(
13148 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 pPg->pgno>nPage
13149 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e Count ){. n
1314a 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70 67 6e Page = (pPg->pgn
1314b 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20 20 20 o - pg1)+1;.
1314c 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31 2b 6e }else if( (pg1+n
1314d 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 PagePerSector-1)
1314e 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 >nPageCount ){.
1314f 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 nPage = nPa
13150 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a 20 geCount+1-pg1;.
13151 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
13152 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50 65 72 nPage = nPagePer
13153 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 Sector;. }.
13154 20 20 61 73 73 65 72 74 28 6e 50 61 67 65 3e 30 assert(nPage>0
13155 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 67 );. assert(pg
13156 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 1<=pPg->pgno);.
13157 20 20 20 61 73 73 65 72 74 28 28 70 67 31 2b 6e assert((pg1+n
13158 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e 6f 29 Page)>pPg->pgno)
13159 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b ;.. for(ii=0;
1315a 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72 63 3d ii<nPage && rc=
1315b 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b =SQLITE_OK; ii++
1315c 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 ){. Pgno pg
1315d 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20 20 20 = pg1+ii;.
1315e 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 0a 20 PgHdr *pPage;.
1315f 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70 50 67 if( pg==pPg
13160 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c 69 74 ->pgno || !sqlit
13161 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61 e3BitvecTest(pPa
13162 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c ger->pInJournal,
13163 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 pg) ){.
13164 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f 4d 4a if( pg!=PAGER_MJ
13165 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b _PGNO(pPager) ){
13166 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
13167 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
13168 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70 50 61 pPager, pg, &pPa
13169 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
1316a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1316b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1316c 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 rc = pager_write
1316d 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
1316e 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
1316f 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
13170 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 _SYNC ){.
13171 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 needSync
13172 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 1;.
13173 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72 assert(pPager
13174 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 20 ->needSync);.
13175 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
13176 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
13177 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b gerUnref(pPage);
13178 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
13179 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1317a 73 65 20 69 66 28 20 28 70 50 61 67 65 20 3d 20 se if( (pPage =
1317b 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 pager_lookup(pPa
1317c 67 65 72 2c 20 70 67 29 29 21 3d 30 20 29 7b 0a ger, pg))!=0 ){.
1317d 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 if( pPag
1317e 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e e->flags&PGHDR_N
1317f 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 EED_SYNC ){.
13180 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d needSync =
13181 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1;. }.
13182 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
13183 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a erUnref(pPage);.
13184 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
13185 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 47 48 /* If the PGH
13186 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
13187 67 20 69 73 20 73 65 74 20 66 6f 72 20 61 6e 79 g is set for any
13188 20 6f 66 20 74 68 65 20 6e 50 61 67 65 20 70 61 of the nPage pa
13189 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 74 61 72 ges . ** star
1318a 74 69 6e 67 20 61 74 20 70 67 31 2c 20 74 68 65 ting at pg1, the
1318b 6e 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 n it needs to be
1318c 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 6f 66 20 set for all of
1318d 74 68 65 6d 2e 20 42 65 63 61 75 73 65 0a 20 20 them. Because.
1318e 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 ** writing to
1318f 61 6e 79 20 6f 66 20 74 68 65 73 65 20 6e 50 61 any of these nPa
13190 67 65 20 70 61 67 65 73 20 6d 61 79 20 64 61 6d ge pages may dam
13191 61 67 65 20 74 68 65 20 6f 74 68 65 72 73 2c 20 age the others,
13192 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e the. ** journ
13193 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 63 6f 6e al file must con
13194 74 61 69 6e 20 73 79 6e 63 28 29 65 64 20 63 6f tain sync()ed co
13195 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f 66 20 74 pies of all of t
13196 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 hem. ** befor
13197 65 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 61 e any of them ca
13198 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 n be written out
13199 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
1319a 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 file.. */.
1319b 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20 29 if( needSync )
1319c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1319d 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 !MEMDB && pPager
1319e 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 ->noSync==0 );.
1319f 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 for(ii=0; i
131a0 69 3c 6e 50 61 67 65 20 26 26 20 6e 65 65 64 53 i<nPage && needS
131a1 79 6e 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 ync; ii++){.
131a2 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 PgHdr *pPage
131a3 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 = pager_lookup(
131a4 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b pPager, pg1+ii);
131a5 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
131a6 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ge ){.
131a7 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPage->flags |=
131a8 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b PGHDR_NEED_SYNC;
131a9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
131aa 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
131ab 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ge);. }.
131ac 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
131ad 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 ert(pPager->need
131ae 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 Sync);. }..
131af 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
131b0 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 ->doNotSync==1 )
131b1 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f ;. pPager->do
131b2 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d NotSync = 0;. }
131b3 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 else{. rc = p
131b4 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 50 61 ager_write(pDbPa
131b5 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ge);. }. retur
131b6 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
131b7 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
131b8 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20 e page given in
131b9 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73 the argument was
131ba 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73 previously pass
131bb 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 ed.** to sqlite3
131bc 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 20 49 PagerWrite(). I
131bd 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 n other words, r
131be 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 eturn TRUE if it
131bf 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 is ok.** to cha
131c0 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 nge the content
131c1 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a of the page..*/.
131c2 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 #ifndef NDEBUG.S
131c3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
131c4 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
131c5 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 writeable(DbPage
131c6 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e *pPg){. return
131c7 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
131c8 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 R_DIRTY;.}.#endi
131c9 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 f../*.** A call
131ca 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 to this routine
131cb 74 65 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 tells the pager
131cc 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e that it is not n
131cd 65 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 ecessary to.** w
131ce 72 69 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 rite the informa
131cf 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 tion on page pPg
131d0 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 back to the dis
131d1 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a k, even though.*
131d2 2a 20 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 * that page migh
131d3 74 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 t be marked as d
131d4 69 72 74 79 2e 20 20 54 68 69 73 20 68 61 70 70 irty. This happ
131d5 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 ens, for example
131d6 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 , when.** the pa
131d7 67 65 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 ge has been adde
131d8 64 20 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74 d as a leaf of t
131d9 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 he freelist and
131da 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e so its.** conten
131db 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 t no longer matt
131dc 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f ers..**.** The o
131dd 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 verlying softwar
131de 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 e layer calls th
131df 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 is routine when
131e0 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a all of the data.
131e1 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 ** on the given
131e2 70 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 page is unused.
131e3 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 The pager marks
131e4 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 the page as clea
131e5 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 n so.** that it
131e6 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 does not get wri
131e7 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a tten to disk..**
131e8 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 .** Tests show t
131e9 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a hat this optimiz
131ea 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 ation can quadru
131eb 70 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 ple the speed of
131ec 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 large .** DELET
131ed 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f E operations..*/
131ee 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
131ef 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
131f0 72 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72 rDontWrite(PgHdr
131f1 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 *pPg){. Pager
131f2 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
131f3 50 61 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50 Pager;. if( (pP
131f4 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
131f5 49 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d IRTY) && pPager-
131f6 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 >nSavepoint==0 )
131f7 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 {. PAGERTRACE
131f8 28 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 (("DONT_WRITE pa
131f9 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 ge %d of %d\n",
131fa 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 pPg->pgno, PAGER
131fb 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
131fc 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 IOTRACE(("CLEA
131fd 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 N %p %d\n", pPag
131fe 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a er, pPg->pgno)).
131ff 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c pPg->flags |
13200 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 = PGHDR_DONT_WRI
13201 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 TE;.#ifdef SQLIT
13202 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
13203 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 pPg->pageHash
13204 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
13205 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 (pPg);.#endif.
13206 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
13207 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
13208 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 d to increment t
13209 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1320a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a 2a database file .*
1320b 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 * change-counter
1320c 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d , stored as a 4-
1320d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
1320e 69 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67 integer starting
1320f 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66 at .** byte off
13210 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61 set 24 of the pa
13211 67 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ger file..**.**
13212 49 66 20 74 68 65 20 69 73 44 69 72 65 63 74 20 If the isDirect
13213 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 flag is zero, th
13214 65 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 en this is done
13215 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 by calling .** s
13216 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
13217 28 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 () on page 1, th
13218 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 en modifying the
13219 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1321a 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20 49 .** page data. I
1321b 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
1321c 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70 64 file will be upd
1321d 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75 ated when the cu
1321e 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 rrent.** transac
1321f 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 tion is committe
13220 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 d..**.** The isD
13221 69 72 65 63 74 20 66 6c 61 67 20 6d 61 79 20 6f irect flag may o
13222 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 20 nly be non-zero
13223 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 if the library w
13224 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 as compiled.** w
13225 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 45 ith the SQLITE_E
13226 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 NABLE_ATOMIC_WRI
13227 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 TE macro defined
13228 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a . In this case,.
13229 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 69 ** if isDirect i
1322a 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1322b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1322c 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 69 le is updated di
1322d 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 69 rectly.** by wri
1322e 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 20 ting an updated
1322f 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20 version of page
13230 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 1 using a call t
13231 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 o the .** sqlite
13232 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 74 3OsWrite() funct
13233 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
13234 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 nt pager_incr_ch
13235 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65 angecounter(Page
13236 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 r *pPager, int i
13237 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 20 sDirectMode){.
13238 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13239 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 72 OK;.. /* Declar
1323a 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 e and initialize
1323b 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 constant intege
1323c 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49 66 r 'isDirect'. If
1323d 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69 63 the. ** atomic
1323e 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 -write optimizat
1323f 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 69 ion is enabled i
13240 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74 68 n this build, th
13241 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a 2a en isDirect. **
13242 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 is initialized
13243 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 to the value pas
13244 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69 72 sed as the isDir
13245 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74 65 ectMode paramete
13246 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 66 r. ** to this f
13247 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 unction. Otherwi
13248 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79 73 se, it is always
13249 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 set to zero..
1324a 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65 61 **. ** The idea
1324b 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65 20 is that if the
1324c 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 atomic-write opt
1324d 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 imization is not
1324e 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61 74 . ** enabled at
1324f 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 74 compile time, t
13250 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e 20 he compiler can
13251 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20 6f omit the tests o
13252 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63 74 f. ** 'isDirect
13253 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c 6c ' below, as well
13254 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65 6e as the block en
13255 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20 20 closed in the.
13256 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63 74 ** "if( isDirect
13257 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 20 )" condition..
13258 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
13259 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 TE_ENABLE_ATOMIC
1325a 5f 57 52 49 54 45 0a 20 20 63 6f 6e 73 74 20 69 _WRITE. const i
1325b 6e 74 20 69 73 44 69 72 65 63 74 20 3d 20 30 3b nt isDirect = 0;
1325c 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 69 72 . assert( isDir
1325d 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 ectMode==0 );.
1325e 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1325f 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a (isDirectMode);.
13260 23 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20 69 6e #else. const in
13261 74 20 69 73 44 69 72 65 63 74 20 3d 20 69 73 44 t isDirect = isD
13262 69 72 65 63 74 4d 6f 64 65 3b 0a 23 65 6e 64 69 irectMode;.#endi
13263 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 f.. assert( pPa
13264 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
13265 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 R_RESERVED );.
13266 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68 61 if( !pPager->cha
13267 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 ngeCountDone &&
13268 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 pPager->dbSize>0
13269 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 ){. PgHdr *p
1326a 50 67 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 PgHdr;
1326b 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e /* Referen
1326c 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a ce to page 1 */.
1326d 20 20 20 20 75 33 32 20 63 68 61 6e 67 65 5f 63 u32 change_c
1326e 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 20 20 20 ounter;
1326f 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 76 61 6c /* Initial val
13270 75 65 20 6f 66 20 63 68 61 6e 67 65 2d 63 6f 75 ue of change-cou
13271 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f 0a 0a 20 nter field */..
13272 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 assert( !pPag
13273 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 er->tempFile &&
13274 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
13275 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 d) );.. /* Op
13276 65 6e 20 70 61 67 65 20 31 20 6f 66 20 74 68 65 en page 1 of the
13277 20 66 69 6c 65 20 66 6f 72 20 77 72 69 74 69 6e file for writin
13278 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 g. */. rc = s
13279 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
1327a 50 61 67 65 72 2c 20 31 2c 20 26 70 50 67 48 64 Pager, 1, &pPgHd
1327b 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 r);. assert(
1327c 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 72 63 3d pPgHdr==0 || rc=
1327d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 =SQLITE_OK );..
1327e 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 6f 6e /* If page on
1327f 65 20 77 61 73 20 66 65 74 63 68 65 64 20 73 75 e was fetched su
13280 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 6e 64 20 ccessfully, and
13281 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
13282 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f 70 65 72 not. ** oper
13283 61 74 69 6e 67 20 69 6e 20 64 69 72 65 63 74 2d ating in direct-
13284 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 67 65 20 mode, make page
13285 31 20 77 72 69 74 61 62 6c 65 2e 0a 20 20 20 20 1 writable..
13286 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 */. if( rc==S
13287 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 69 73 44 QLITE_OK && !isD
13288 69 72 65 63 74 20 29 7b 0a 20 20 20 20 20 20 72 irect ){. r
13289 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1328a 57 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20 Write(pPgHdr);.
1328b 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 }.. if( rc
1328c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1328d 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e /* Incremen
1328e 74 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74 t the value just
1328f 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 read and write
13290 69 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65 20 it back to byte
13291 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 24. */. cha
13292 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 nge_counter = sq
13293 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75 lite3Get4byte((u
13294 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 8*)pPager->dbFil
13295 65 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63 68 eVers);. ch
13296 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a ange_counter++;.
13297 20 20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 put32bits(
13298 28 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e ((char*)pPgHdr->
13299 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 pData)+24, chang
1329a 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20 e_counter);..
1329b 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 /* If running
1329c 20 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c in direct mode,
1329d 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 write the conte
1329e 6e 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f nts of page 1 to
1329f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 the file. */.
132a0 20 20 20 20 69 66 28 20 69 73 44 69 72 65 63 74 if( isDirect
132a1 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 ){. cons
132a2 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d 20 70 t void *zBuf = p
132a3 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a 20 20 PgHdr->pData;.
132a4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
132a5 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 ager->dbFileSize
132a6 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 >0 );. rc
132a7 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
132a8 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 7a 42 e(pPager->fd, zB
132a9 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 uf, pPager->page
132aa 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Size, 0);.
132ab 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65 }.. /* If e
132ac 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 verything worked
132ad 2c 20 73 65 74 20 74 68 65 20 63 68 61 6e 67 65 , set the change
132ae 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 2e 20 CountDone flag.
132af 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
132b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
132b1 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 pPager->ch
132b2 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 angeCountDone =
132b3 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
132b4 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 .. /* Release
132b5 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65 the page refere
132b6 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 nce. */. sqli
132b7 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
132b8 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 gHdr);. }. ret
132b9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
132ba 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72 20 Sync the pager
132bb 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54 68 file to disk. Th
132bc 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f is is a no-op fo
132bd 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 r in-memory file
132be 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 69 s.** or pages wi
132bf 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 th the Pager.noS
132c0 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a ync flag set..**
132c1 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
132c2 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f 6e 20 l, or called on
132c3 61 20 70 61 67 65 72 20 66 6f 72 20 77 68 69 63 a pager for whic
132c4 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2c h it is a no-op,
132c5 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f this.** functio
132c6 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 n returns SQLITE
132c7 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 _OK. Otherwise,
132c8 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
132c9 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
132ca 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
132cb 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
132cc 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 Sync(Pager *pPag
132cd 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 er){. int rc;
132ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
132d0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
132d1 69 66 28 20 4d 45 4d 44 42 20 7c 7c 20 70 50 61 if( MEMDB || pPa
132d2 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 ger->noSync ){.
132d3 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
132d4 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
132d5 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 rc = sqlite3OsSy
132d6 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 nc(pPager->fd, p
132d7 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 Pager->sync_flag
132d8 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e s);. }. return
132d9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 rc;.}../*.** Sy
132da 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
132db 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70 61 67 file for the pag
132dc 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61 73 74 er pPager. zMast
132dd 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 er points to the
132de 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 6d 61 name.** of a ma
132df 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
132e0 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 e that should be
132e1 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
132e2 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 e individual.**
132e3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d journal file. zM
132e4 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e 55 4c aster may be NUL
132e5 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e 74 65 L, which is inte
132e6 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20 6d 61 rpreted as no ma
132e7 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 ster.** journal
132e8 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 (a single databa
132e9 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e se transaction).
132ea 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
132eb 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74 ine ensures that
132ec 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 :.**.** * The
132ed 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 database file ch
132ee 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 ange-counter is
132ef 75 70 64 61 74 65 64 2c 0a 2a 2a 20 20 20 2a 20 updated,.** *
132f0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 the journal is s
132f1 79 6e 63 65 64 20 28 75 6e 6c 65 73 73 20 74 68 ynced (unless th
132f2 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f e atomic-write o
132f3 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 75 ptimization is u
132f4 73 65 64 29 2c 0a 2a 2a 20 20 20 2a 20 61 6c 6c sed),.** * all
132f5 20 64 69 72 74 79 20 70 61 67 65 73 20 61 72 65 dirty pages are
132f6 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
132f7 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 0a database file, .
132f8 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 ** * the datab
132f9 61 73 65 20 66 69 6c 65 20 69 73 20 74 72 75 6e ase file is trun
132fa 63 61 74 65 64 20 28 69 66 20 72 65 71 75 69 72 cated (if requir
132fb 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 ed), and.** *
132fc 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
132fd 65 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a e synced. .**.**
132fe 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 The only thing
132ff 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20 that remains to
13300 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 commit the trans
13301 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 66 69 6e action is to fin
13302 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 65 6c 65 74 alize .** (delet
13303 65 2c 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a e, truncate or z
13304 65 72 6f 20 74 68 65 20 66 69 72 73 74 20 70 61 ero the first pa
13305 72 74 20 6f 66 29 20 74 68 65 20 6a 6f 75 72 6e rt of) the journ
13306 61 6c 20 66 69 6c 65 20 28 6f 72 20 0a 2a 2a 20 al file (or .**
13307 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 delete the maste
13308 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 r journal file i
13309 66 20 73 70 65 63 69 66 69 65 64 29 2e 0a 2a 2a f specified)..**
1330a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 .** Note that if
1330b 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20 zMaster==NULL,
1330c 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 6f 76 this does not ov
1330d 65 72 77 72 69 74 65 20 61 20 70 72 65 76 69 6f erwrite a previo
1330e 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 61 73 73 us value.** pass
1330f 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 ed to an sqlite3
13310 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
13311 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a One() call..**.*
13312 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c 20 70 * If the final p
13313 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e arameter - noSyn
13314 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74 68 65 c - is true, the
13315 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13316 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73 ile itself.** is
13317 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54 68 65 not synced. The
13318 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c caller must cal
13319 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 l sqlite3PagerSy
1331a 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20 74 6f nc() directly to
1331b 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64 61 74 .** sync the dat
1331c 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
1331d 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 e calling Commit
1331e 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20 64 65 PhaseTwo() to de
1331f 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 lete the.** jour
13320 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73 nal file in this
13321 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 case..*/.SQLITE
13322 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
13323 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
13324 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67 65 72 haseOne(. Pager
13325 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
13326 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
13327 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 ger object */.
13328 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
13329 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ter,
1332a 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 /* If not NULL,
1332b 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1332c 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 al name */. int
1332d 20 6e 6f 53 79 6e 63 20 20 20 20 20 20 20 20 20 noSync
1332e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1332f 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 74 68 65 True to omit the
13330 20 78 53 79 6e 63 20 6f 6e 20 74 68 65 20 64 62 xSync on the db
13331 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e file */.){. in
13332 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
13333 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
13334 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
13335 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
13336 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 rrCode ){. re
13337 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
13338 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 Code;. }.. PAG
13339 45 52 54 52 41 43 45 28 28 22 44 41 54 41 42 41 ERTRACE(("DATABA
1333a 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 SE SYNC: File=%s
1333b 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a zMaster=%s nSiz
1333c 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 e=%d\n", .
1333d 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
1333e 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 e, zMaster, pPag
1333f 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 er->dbSize));..
13340 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
13341 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 n in-memory db,
13342 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65 or no pages have
13343 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
13344 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a 20 66 , or this. ** f
13345 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 unction has alre
13346 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c ady been called,
13347 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a it is a no-op..
13348 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 */. if( MEMDB
13349 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f && pPager->dbMo
1334a 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 73 71 dified ){. sq
1334b 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 lite3BackupResta
1334c 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b rt(pPager->pBack
1334d 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 up);. }else if(
1334e 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d pPager->state!=
1334f 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 26 20 PAGER_SYNCED &&
13350 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
13351 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 68 ed ){.. /* Th
13352 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
13353 6b 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68 k updates the ch
13354 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20 45 78 ange-counter. Ex
13355 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20 20 20 actly how it.
13356 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 64 65 ** does this de
13357 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72 pends on whether
13358 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d or not the atom
13359 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 ic-update optimi
1335a 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 77 61 zation. ** wa
1335b 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d s enabled at com
1335c 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 20 69 pile time, and i
1335d 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 f this transacti
1335e 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a 20 20 on meets the .
1335f 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 72 69 ** runtime cri
13360 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 68 65 teria to use the
13361 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 20 20 operation: .
13362 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 **. ** *
13363 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 The file-system
13364 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 74 6f supports the ato
13365 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72 mic-write proper
13366 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20 ty for. **
13367 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a blocks of siz
13368 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64 e page-size, and
13369 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 54 68 . ** * Th
1336a 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 is commit is not
1336b 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69 part of a multi
1336c 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74 69 6f -file transactio
1336d 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 20 20 n, and. **
1336e 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70 * Exactly one p
1336f 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 age has been mod
13370 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20 ified and store
13371 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
13372 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
13373 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 ** If the optimi
13374 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 65 zation was not e
13375 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c nabled at compil
13376 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 68 65 e time, then the
13377 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 6e . ** pager_in
13378 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 cr_changecounter
13379 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 () function is c
1337a 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 65 20 alled to update
1337b 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 20 2a the change. *
1337c 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27 69 6e * counter in 'in
1337d 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20 49 66 direct-mode'. If
1337e 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f the optimizatio
1337f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e n is compiled in
13380 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e but. ** is n
13381 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20 74 6f ot applicable to
13382 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
13383 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4a n, call sqlite3J
13384 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 0a 20 ournalCreate().
13385 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 73 75 ** to make su
13386 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 re the journal f
13387 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c 6c 79 ile has actually
13388 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 74 been created, t
13389 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 hen call. **
1338a 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 pager_incr_chang
1338b 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70 ecounter() to up
1338c 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d date the change-
1338d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64 69 72 counter in indir
1338e 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64 65 2e ect. ** mode.
1338f 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 . **. **
13390 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 Otherwise, if th
13391 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
13392 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 20 61 s both enabled a
13393 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c 0a 20 nd applicable,.
13394 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 ** then call
13395 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 pager_incr_chang
13396 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70 ecounter() to up
13397 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d date the change-
13398 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 69 counter. ** i
13399 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64 65 2e n 'direct' mode.
1339a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
1339b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 e journal file w
1339c 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20 20 20 ill never be.
1339d 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f 72 20 ** created for
1339e 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
1339f 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 .. */.#ifdef
133a0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
133a1 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 50 OMIC_WRITE. P
133a2 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 61 gHdr *pPg;. a
133a3 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
133a4 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 ager->jfd) || pP
133a5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
133a6 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
133a7 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20 MODE_OFF );.
133a8 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 26 20 if( !zMaster &&
133a9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
133aa 66 64 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 fd) . && pPa
133ab 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
133ac 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 =jrnlBufferSize(
133ad 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 26 26 pPager) . &&
133ae 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e pPager->dbSize>
133af 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 =pPager->dbFileS
133b0 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30 3d 3d ize. && (0==
133b1 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 (pPg = sqlite3Pc
133b2 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 acheDirtyList(pP
133b3 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 29 20 ager->pPCache))
133b4 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 72 74 || 0==pPg->pDirt
133b5 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 y). ){.
133b6 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 62 /* Update the db
133b7 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 file change cou
133b8 6e 74 65 72 20 76 69 61 20 74 68 65 20 64 69 72 nter via the dir
133b9 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68 6f 64 ect-write method
133ba 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 . The . **
133bb 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 77 following call w
133bc 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 69 ill modify the i
133bd 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 n-memory represe
133be 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 65 20 ntation of page
133bf 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 69 1 . ** to i
133c0 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 61 74 nclude the updat
133c1 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 ed change counte
133c2 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 74 65 r and then write
133c3 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a page 1 . *
133c4 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 * directly to th
133c5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
133c6 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 Because of the
133c7 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a 20 20 atomic-write .
133c8 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 79 20 ** property
133c9 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65 of the host file
133ca 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 69 73 -system, this is
133cb 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a 2f 0a safe.. */.
133cc 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
133cd 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
133ce 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 3b 0a ter(pPager, 1);.
133cf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
133d0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 rc = sqlite3Jou
133d1 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 67 65 rnalCreate(pPage
133d2 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 r->jfd);. i
133d3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
133d4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
133d5 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
133d6 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 gecounter(pPager
133d7 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
133d8 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 }.#else. rc
133d9 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 = pager_incr_ch
133da 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 angecounter(pPag
133db 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 er, 0);.#endif.
133dc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
133dd 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d E_OK ) goto comm
133de 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 it_phase_one_exi
133df 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 t;.. /* If th
133e0 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 is transaction h
133e1 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 74 61 as made the data
133e2 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 74 68 base smaller, th
133e3 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 20 20 en all pages.
133e4 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 61 72 ** being discar
133e5 64 65 64 20 62 79 20 74 68 65 20 74 72 75 6e 63 ded by the trunc
133e6 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 77 72 ation must be wr
133e7 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
133e8 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 rnal. ** file
133e9 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 . This can only
133ea 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76 happen in auto-v
133eb 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20 20 20 acuum mode..
133ec 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f 72 65 **. ** Before
133ed 20 72 65 61 64 69 6e 67 20 74 68 65 20 70 61 67 reading the pag
133ee 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d es with page num
133ef 62 65 72 73 20 6c 61 72 67 65 72 20 74 68 61 6e bers larger than
133f0 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 the . ** cur
133f1 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61 rent value of Pa
133f2 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65 74 20 ger.dbSize, set
133f3 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 dbSize back to t
133f4 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 he value. **
133f5 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61 74 20 that it took at
133f6 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
133f7 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 transaction. Ot
133f8 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20 20 20 herwise, the.
133f9 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ** calls to sql
133fa 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 72 ite3PagerGet() r
133fb 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67 eturn zeroed pag
133fc 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 20 es instead of .
133fd 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 64 61 ** reading da
133fe 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ta from the data
133ff 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a base file.. *
13400 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
13401 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
13402 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
13403 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e >dbSize<pPager->
13404 64 62 4f 72 69 67 53 69 7a 65 0a 20 20 20 20 20 dbOrigSize.
13405 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
13406 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f alMode!=PAGER_JO
13407 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 URNALMODE_OFF .
13408 20 20 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f ){. Pgno
13409 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1340a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1340b 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f /* Iterato
1340c 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 r variable */.
1340d 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 const Pgno i
1340e 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f Skip = PAGER_MJ_
1340f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 20 2f 2a PGNO(pPager); /*
13410 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 70 61 Pending lock pa
13411 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 ge */. cons
13412 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 20 3d 20 t Pgno dbSize =
13413 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 20 pPager->dbSize;
13414 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
13415 65 20 69 6d 61 67 65 20 73 69 7a 65 20 2a 2f 20 e image size */
13416 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
13417 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e bSize = pPager->
13418 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 20 20 dbOrigSize;.
13419 20 20 66 6f 72 28 20 69 3d 64 62 53 69 7a 65 2b for( i=dbSize+
1341a 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e 64 62 1; i<=pPager->db
1341b 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b 20 29 7b OrigSize; i++ ){
1341c 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71 . if( !sq
1341d 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
1341e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
1341f 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69 53 6b al, i) && i!=iSk
13420 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ip ){.
13421 50 67 48 64 72 20 2a 70 50 61 67 65 3b 20 20 20 PgHdr *pPage;
13422 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
13423 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a e to journal */.
13424 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
13425 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
13426 50 61 67 65 72 2c 20 69 2c 20 26 70 50 61 67 65 Pager, i, &pPage
13427 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
13428 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13429 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 goto commit_pha
1342a 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 se_one_exit;.
1342b 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1342c 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
1342d 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
1342e 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1342f 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 f(pPage);.
13430 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
13431 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d TE_OK ) goto com
13432 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
13433 69 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 it;. }.
13434 20 20 20 20 7d 20 0a 20 20 20 20 20 20 70 50 61 } . pPa
13435 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 64 62 ger->dbSize = db
13436 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 Size;. }.#end
13437 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 if.. /* Write
13438 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
13439 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 nal name into th
1343a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
1343b 49 66 20 61 20 6d 61 73 74 65 72 20 0a 20 20 20 If a master .
1343c 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ** journal file
1343d 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 65 61 64 name has alread
1343e 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 y been written t
1343f 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
13440 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f 72 20 69 le, . ** or i
13441 66 20 7a 4d 61 73 74 65 72 20 69 73 20 4e 55 4c f zMaster is NUL
13442 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 L (no master jou
13443 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 68 69 73 rnal), then this
13444 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 call is a no-op
13445 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
13446 3d 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 = writeMasterJou
13447 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 rnal(pPager, zMa
13448 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ster);. if( r
13449 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 c!=SQLITE_OK ) g
1344a 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 oto commit_phase
1344b 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 _one_exit;..
1344c 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 /* Sync the jour
1344d 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 nal file. If the
1344e 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f atomic-update o
1344f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 ptimization is b
13450 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73 65 64 eing. ** used
13451 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 69 6c 6c , this call will
13452 20 6e 6f 74 20 63 72 65 61 74 65 20 74 68 65 20 not create the
13453 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 journal file or
13454 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 20 20 20 perform any.
13455 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 20 20 20 ** real IO..
13456 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 */. rc = sync
13457 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b Journal(pPager);
13458 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
13459 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
1345a 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
1345b 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57 72 69 xit;.. /* Wri
1345c 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 te all dirty pag
1345d 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 es to the databa
1345e 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 se file. */.
1345f 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 rc = pager_write
13460 5f 70 61 67 65 6c 69 73 74 28 73 71 6c 69 74 65 _pagelist(sqlite
13461 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 3PcacheDirtyList
13462 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
13463 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d ));. if( rc!=
13464 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13465 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
13466 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 QLITE_IOERR_BLOC
13467 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 67 6f 74 KED );. got
13468 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f o commit_phase_o
13469 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a 20 ne_exit;. }.
1346a 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
1346b 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d CleanAll(pPager-
1346c 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 20 20 >pPCache);..
1346d 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f /* If the file o
1346e 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 74 68 n disk is not th
1346f 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 74 e same size as t
13470 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 he database imag
13471 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 75 e,. ** then u
13472 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 se pager_truncat
13473 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73 68 72 e to grow or shr
13474 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68 65 72 ink the file her
13475 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 e.. */. if
13476 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
13477 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 !=pPager->dbFile
13478 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 50 67 Size ){. Pg
13479 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 67 65 72 no nNew = pPager
1347a 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 50 61 67 ->dbSize - (pPag
1347b 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 41 47 45 er->dbSize==PAGE
1347c 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
1347d 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ));. assert
1347e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
1347f 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 =PAGER_EXCLUSIVE
13480 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 );. rc = p
13481 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 ager_truncate(pP
13482 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 ager, nNew);.
13483 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13484 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d E_OK ) goto comm
13485 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 it_phase_one_exi
13486 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a t;. }.. /*
13487 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e 63 20 74 Finally, sync t
13488 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13489 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 . */. if( !pP
1348a 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 20 ager->noSync &&
1348b 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 !noSync ){.
1348c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
1348d 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ync(pPager->fd,
1348e 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 pPager->sync_fla
1348f 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 gs);. }. I
13490 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e 43 20 OTRACE(("DBSYNC
13491 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a %p\n", pPager)).
13492 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 . pPager->sta
13493 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e 43 45 te = PAGER_SYNCE
13494 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 D;. }..commit_p
13495 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3a 0a 20 hase_one_exit:.
13496 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13497 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 7b IOERR_BLOCKED ){
13498 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 69 6e . /* pager_in
13499 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 cr_changecounter
1349a 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 () may attempt t
1349b 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78 63 6c o obtain an excl
1349c 75 73 69 76 65 0a 20 20 20 20 2a 2a 20 6c 6f 63 usive. ** loc
1349d 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65 20 63 k to spill the c
1349e 61 63 68 65 20 61 6e 64 20 72 65 74 75 72 6e 20 ache and return
1349f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e 20 42 IOERR_BLOCKED. B
134a0 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20 2a 2a ut since . **
134a1 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 there is no cha
134a2 6e 63 65 20 74 68 65 20 63 61 63 68 65 20 69 73 nce the cache is
134a3 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c 20 69 inconsistent, i
134a4 74 20 69 73 0a 20 20 20 20 2a 2a 20 62 65 74 74 t is. ** bett
134a5 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c er to return SQL
134a6 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 2a 2a ITE_BUSY.. **
134a7 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 /. rc = SQLIT
134a8 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 72 65 E_BUSY;. }. re
134a9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
134aa 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e ** When this fun
134ab 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
134ac 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
134ad 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 le has been comp
134ae 6c 65 74 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65 letely.** update
134af 64 20 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65 d to reflect the
134b0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 changes made by
134b1 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
134b2 6e 73 61 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 nsaction and.**
134b3 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 synced to disk.
134b4 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 The journal file
134b5 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 69 6e still exists in
134b6 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
134b7 20 0a 2a 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64 .** though, and
134b8 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 if a failure oc
134b9 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 curs at this poi
134ba 6e 74 20 69 74 20 77 69 6c 6c 20 65 76 65 6e 74 nt it will event
134bb 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64 ually.** be used
134bc 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 as a hot-journa
134bd 6c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e l and the curren
134be 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f t transaction ro
134bf 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a lled back..**.**
134c0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 This function f
134c1 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 inalizes the jou
134c2 72 6e 61 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 rnal file, eithe
134c3 72 20 62 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a r by deleting, .
134c4 2a 2a 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 ** truncating or
134c5 20 70 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 partially zeroi
134c6 6e 67 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69 ng it, so that i
134c7 74 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 t cannot be used
134c8 20 0a 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 .** for hot-jou
134c9 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f rnal rollback. O
134ca 6e 63 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65 nce this is done
134cb 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
134cc 20 69 73 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62 is.** irrevocab
134cd 6c 79 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a ly committed..**
134ce 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
134cf 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 occurs, an IO er
134d0 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
134d1 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 rned and the pag
134d2 65 72 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f er.** moves into
134d3 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
134d4 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
134d5 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
134d6 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
134d7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
134d8 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
134d9 65 54 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67 eTwo(Pager *pPag
134da 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
134db 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
134dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
134dd 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
134de 20 2f 2a 20 44 6f 20 6e 6f 74 20 70 72 6f 63 65 /* Do not proce
134df 65 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 ed if the pager
134e0 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 is already in th
134e1 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a e error state. *
134e2 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
134e3 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 errCode ){. r
134e4 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
134e5 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rCode;. }.. /*
134e6 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
134e7 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c hould not be cal
134e8 6c 65 64 20 69 66 20 74 68 65 20 70 61 67 65 72 led if the pager
134e9 20 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c 65 is not in at le
134ea 61 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f 52 ast. ** PAGER_R
134eb 45 53 45 52 56 45 44 20 73 74 61 74 65 2e 20 41 ESERVED state. A
134ec 6e 64 20 69 6e 64 65 65 64 20 53 51 4c 69 74 65 nd indeed SQLite
134ed 20 6e 65 76 65 72 20 64 6f 65 73 20 74 68 69 73 never does this
134ee 2e 20 42 75 74 20 69 74 20 69 73 0a 20 20 2a 2a . But it is. **
134ef 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20 74 68 nice to have th
134f0 69 73 20 64 65 66 65 6e 73 69 76 65 20 62 6c 6f is defensive blo
134f1 63 6b 20 68 65 72 65 20 61 6e 79 77 61 79 2e 0a ck here anyway..
134f2 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 */. if( NEVER
134f3 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 (pPager->state<P
134f4 41 47 45 52 5f 52 45 53 45 52 56 45 44 29 20 29 AGER_RESERVED) )
134f5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
134f6 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a ITE_ERROR;. }..
134f7 20 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 /* An optimiza
134f8 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 74 tion. If the dat
134f9 61 62 61 73 65 20 77 61 73 20 6e 6f 74 20 61 63 abase was not ac
134fa 74 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 tually modified
134fb 64 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 during. ** this
134fc 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
134fd 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 e pager is runni
134fe 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d ng in exclusive-
134ff 6d 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a mode and is. **
13500 20 75 73 69 6e 67 20 70 65 72 73 69 73 74 65 6e using persisten
13501 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e t journals, then
13502 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13503 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a s a no-op.. **.
13504 20 20 2a 2a 20 54 68 65 20 73 74 61 72 74 20 6f ** The start o
13505 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
13506 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e le currently con
13507 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a tains a single j
13508 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 ournal . ** hea
13509 64 65 72 20 77 69 74 68 20 74 68 65 20 6e 52 65 der with the nRe
1350a 63 20 66 69 65 6c 64 20 73 65 74 20 74 6f 20 30 c field set to 0
1350b 2e 20 49 66 20 73 75 63 68 20 61 20 6a 6f 75 72 . If such a jour
1350c 6e 61 6c 20 69 73 20 75 73 65 64 20 61 73 0a 20 nal is used as.
1350d 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ** a hot-journa
1350e 6c 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 l during hot-jou
1350f 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 rnal rollback, 0
13510 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 changes will be
13511 20 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 made. ** to th
13512 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13513 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 So there is no
13514 6e 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 need to zero the
13515 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 journal . ** h
13516 65 61 64 65 72 2e 20 53 69 6e 63 65 20 74 68 65 eader. Since the
13517 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 pager is in exc
13518 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 lusive mode, the
13519 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 re is no need.
1351a 2a 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c ** to drop any l
1351b 6f 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 2a ocks either.. *
1351c 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
1351d 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 dbModified==0 &&
1351e 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
1351f 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 50 veMode . && pP
13520 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
13521 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
13522 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 29 MODE_PERSIST. )
13523 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
13524 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13525 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a ==JOURNAL_HDR_SZ
13526 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
13527 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
13528 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 ;. }.. PAGERTR
13529 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c ACE(("COMMIT %d\
1352a 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
1352b 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 er)));. assert(
1352c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
1352d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 PAGER_SYNCED ||
1352e 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 72 MEMDB || !pPager
1352f 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a ->dbModified );.
13530 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 rc = pager_end
13531 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
13532 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 ger, pPager->set
13533 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 Master);. retur
13534 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 n pager_error(pP
13535 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a ager, rc);.}../*
13536 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c .** Rollback all
13537 20 63 68 61 6e 67 65 73 2e 20 54 68 65 20 64 61 changes. The da
13538 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 63 tabase falls bac
13539 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 k to PAGER_SHARE
1353a 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 D mode..**.** Th
1353b 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 is function perf
1353c 6f 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a orms two tasks:.
1353d 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 6f **.** 1) It ro
1353e 6c 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 lls back the jou
1353f 72 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f rnal file, resto
13540 72 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 73 ring all databas
13541 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 e file and .**
13542 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 in-memory ca
13543 63 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 65 che pages to the
13544 20 73 74 61 74 65 20 74 68 65 79 20 77 65 72 65 state they were
13545 20 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 61 in when the tra
13546 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 nsaction.**
13547 20 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 was opened, and
13548 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e 61 .** 2) It fina
13549 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 lizes the journa
1354a 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 l file, so that
1354b 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 66 it is not used f
1354c 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 or hot.** r
1354d 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 20 70 ollback at any p
1354e 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 oint in the futu
1354f 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 re..**.** subjec
13550 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 t to the followi
13551 6e 67 20 71 75 61 6c 69 66 69 63 61 74 69 6f 6e ng qualification
13552 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 s:.**.** * If th
13553 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
13554 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 s not yet open w
13555 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
13556 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 n is called,.**
13557 20 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 then only (2)
13558 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e is performed. In
13559 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
1355a 20 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 is no journal f
1355b 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c ile.** to roll
1355c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 back..**.** * I
1355d 66 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 f in an error st
1355e 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 ate other than S
1355f 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e QLITE_FULL, then
13560 20 74 61 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a task (1) is .**
13561 20 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 performed. If
13562 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 61 73 successful, tas
13563 6b 20 28 32 29 2e 20 52 65 67 61 72 64 6c 65 73 k (2). Regardles
13564 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 s of the outcome
13565 0a 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 72 2c .** of either,
13566 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
13567 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
13568 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 eturned to the c
13569 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e aller.** (i.e.
1356a 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 49 either SQLITE_I
1356b 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 OERR or SQLITE_C
1356c 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a ORRUPT)..**.** *
1356d 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
1356e 20 69 6e 20 50 41 47 45 52 5f 52 45 53 45 52 56 in PAGER_RESERV
1356f 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 61 ED state, then a
13570 74 74 65 6d 70 74 20 28 31 29 2e 20 57 68 65 74 ttempt (1). Whet
13571 68 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 her.** or not
13572 28 31 29 20 69 73 20 73 75 63 63 75 73 73 66 75 (1) is succussfu
13573 6c 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 l, also attempt
13574 28 32 29 2e 20 49 66 20 73 75 63 63 65 73 73 66 (2). If successf
13575 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 ul, return.**
13576 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 SQLITE_OK. Other
13577 77 69 73 65 2c 20 65 6e 74 65 72 20 74 68 65 20 wise, enter the
13578 65 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 error state and
13579 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 return the first
1357a 20 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 .** error cod
1357b 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a e encountered. .
1357c 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 **.** In this
1357d 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f case there is no
1357e 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 chance that the
1357f 20 64 61 74 61 62 61 73 65 20 77 61 73 20 77 72 database was wr
13580 69 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 itten to. .**
13581 53 6f 20 69 73 20 73 61 66 65 20 74 6f 20 66 69 So is safe to fi
13582 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e nalize the journ
13583 61 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 66 20 al file even if
13584 74 68 65 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a the playback .**
13585 20 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 (operation 1)
13586 20 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 failed. However
13587 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 the pager must
13588 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 enter the error
13589 73 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 state.** as th
1358a 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
1358b 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 e in-memory cach
1358c 65 20 61 72 65 20 6e 6f 77 20 73 75 73 70 65 63 e are now suspec
1358d 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c t..**.** * Final
1358e 6c 79 2c 20 69 66 20 69 6e 20 50 41 47 45 52 5f ly, if in PAGER_
1358f 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c EXCLUSIVE state,
13590 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 then attempt (1
13591 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 ). Only.** att
13592 65 6d 70 74 20 28 32 29 20 69 66 20 28 31 29 20 empt (2) if (1)
13593 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 52 is successful. R
13594 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
13595 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a if successful,.*
13596 2a 20 20 20 6f 74 68 65 72 77 69 73 65 20 65 6e * otherwise en
13597 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 ter the error st
13598 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ate and return t
13599 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 72 he error code fr
1359a 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 om the .** fai
1359b 6c 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a ling operation..
1359c 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 **.** In this
1359d 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 case the databas
1359e 65 20 66 69 6c 65 20 6d 61 79 20 68 61 76 65 20 e file may have
1359f 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e been written to.
135a0 20 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 So if the.**
135a1 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 playback operati
135a2 6f 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 63 65 on did not succe
135a3 65 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 ed it would not
135a4 62 65 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c be safe to final
135a5 69 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 ize.** the jou
135a6 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 rnal file. It ne
135a7 65 64 73 20 74 6f 20 62 65 20 6c 65 66 74 20 69 eds to be left i
135a8 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 n the file-syste
135a9 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 m so that.** s
135aa 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ome other proces
135ab 73 20 63 61 6e 20 75 73 65 20 69 74 20 74 6f 20 s can use it to
135ac 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 restore the data
135ad 62 61 73 65 20 73 74 61 74 65 20 28 62 79 0a 2a base state (by.*
135ae 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 * hot-journal
135af 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51 rollback)..*/.SQ
135b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
135b1 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
135b2 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 lback(Pager *pPa
135b3 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
135b4 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
135b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
135b6 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
135b7 20 50 41 47 45 52 54 52 41 43 45 28 28 22 52 4f PAGERTRACE(("RO
135b8 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 LLBACK %d\n", PA
135b9 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
135ba 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
135bb 64 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 21 69 dbModified || !i
135bc 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
135bd 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 d) ){. rc = p
135be 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 ager_end_transac
135bf 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 tion(pPager, pPa
135c0 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b ger->setMaster);
135c1 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 . }else if( pPa
135c2 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 ger->errCode &&
135c3 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 pPager->errCode!
135c4 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a =SQLITE_FULL ){.
135c5 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
135c6 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
135c7 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20 20 LUSIVE ){.
135c8 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
135c9 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d Pager, 0);. }
135ca 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 . rc = pPager
135cb 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c ->errCode;. }el
135cc 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 se{. if( pPag
135cd 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
135ce 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 _RESERVED ){.
135cf 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 int rc2;.
135d0 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
135d1 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 yback(pPager, 0)
135d2 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 70 61 ;. rc2 = pa
135d3 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
135d4 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 ion(pPager, pPag
135d5 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a er->setMaster);.
135d6 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
135d7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
135d8 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 rc = rc2;.
135d9 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
135da 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
135db 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 _playback(pPager
135dc 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 , 0);. }..
135dd 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 if( !MEMDB ){.
135de 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 pPager->dbS
135df 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 izeValid = 0;.
135e0 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 }.. /* If a
135e1 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 n error occurs d
135e2 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b uring a ROLLBACK
135e3 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 , we can no long
135e4 65 72 20 74 72 75 73 74 20 74 68 65 20 70 61 67 er trust the pag
135e5 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e er. ** cache.
135e6 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 So call pager_e
135e7 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77 61 rror() on the wa
135e8 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e y out to make an
135e9 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20 y error . **
135ea 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20 20 persistent..
135eb 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 */. rc = page
135ec 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 r_error(pPager,
135ed 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 rc);. }. retur
135ee 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
135ef 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
135f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
135f1 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f is opened read-o
135f2 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41 4c nly. Return FAL
135f3 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 SE.** if the dat
135f4 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 68 65 abase is (in the
135f5 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a ory) writable..*
135f6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
135f7 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 65 72 u8 sqlite3Pager
135f8 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 Isreadonly(Pager
135f9 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
135fa 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 61 64 urn pPager->read
135fb 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 Only;.}../*.** R
135fc 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
135fd 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
135fe 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a o the pager..*/.
135ff 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
13600 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 nt sqlite3PagerR
13601 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 efcount(Pager *p
13602 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
13603 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
13604 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
13605 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a PCache);.}../*.*
13606 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
13607 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
13608 73 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 s to the specifi
13609 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 ed page..*/.SQLI
1360a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1360b 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 qlite3PagerPageR
1360c 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 20 2a efcount(DbPage *
1360d 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e pPage){. return
1360e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
1360f 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 geRefcount(pPage
13610 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c );.}..#ifdef SQL
13611 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 ITE_TEST./*.** T
13612 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
13613 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 sed for testing
13614 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c and analysis onl
13615 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
13616 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 VATE int *sqlite
13617 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65 3PagerStats(Page
13618 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 74 r *pPager){. st
13619 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a atic int a[11];.
1361a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 a[0] = sqlite3
1361b 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 PcacheRefCount(p
1361c 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b Pager->pPCache);
1361d 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65 . a[1] = sqlite
1361e 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 3PcachePagecount
1361f 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
13620 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69 );. a[2] = sqli
13621 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 te3PcacheGetCach
13622 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 esize(pPager->pP
13623 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d Cache);. a[3] =
13624 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
13625 61 6c 69 64 20 3f 20 28 69 6e 74 29 20 70 50 61 alid ? (int) pPa
13626 67 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31 ger->dbSize : -1
13627 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 ;. a[4] = pPage
13628 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d r->state;. a[5]
13629 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f = pPager->errCo
1362a 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 de;. a[6] = pPa
1362b 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 ger->nHit;. a[7
1362c 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 ] = pPager->nMis
1362d 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 s;. a[8] = 0;
1362e 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 50 /* Used to be pP
1362f 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 ager->nOvfl */.
13630 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[9] = pPager->
13631 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d nRead;. a[10] =
13632 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b pPager->nWrite;
13633 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 . return a;.}.#
13634 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 endif../*.** Ret
13635 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 urn true if this
13636 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 is an in-memory
13637 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 pager..*/.SQLIT
13638 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13639 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 lite3PagerIsMemd
1363a 62 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 b(Pager *pPager)
1363b 7b 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 {. return MEMDB
1363c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b ;.}../*.** Check
1363d 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 that there are
1363e 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70 6f at least nSavepo
1363f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 6f int savepoints o
13640 70 65 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72 pen. If there ar
13641 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c e.** currently l
13642 65 73 73 20 74 68 61 6e 20 6e 53 61 76 65 70 6f ess than nSavepo
13643 69 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 ints open, then
13644 6f 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 open one or more
13645 20 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 savepoints.** t
13646 6f 20 6d 61 6b 65 20 75 70 20 74 68 65 20 64 69 o make up the di
13647 66 66 65 72 65 6e 63 65 2e 20 49 66 20 74 68 65 fference. If the
13648 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 number of savep
13649 6f 69 6e 74 73 20 69 73 20 61 6c 72 65 61 64 79 oints is already
1364a 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 .** equal to nSa
1364b 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 vepoint, then th
1364c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
1364d 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 no-op..**.** If
1364e 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
1364f 74 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49 tion fails, SQLI
13650 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 TE_NOMEM is retu
13651 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f rned. If an erro
13652 72 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 r .** occurs whi
13653 6c 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 73 le opening the s
13654 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c ub-journal file,
13655 20 74 68 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f then an IO erro
13656 72 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74 r code is.** ret
13657 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 urned. Otherwise
13658 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a , SQLITE_OK..*/.
13659 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1365a 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f nt sqlite3PagerO
1365b 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67 penSavepoint(Pag
1365c 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
1365d 6e 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 nSavepoint){. i
1365e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1365f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K;
13660 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
13661 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 rn code */. int
13662 20 6e 43 75 72 72 65 6e 74 20 3d 20 70 50 61 67 nCurrent = pPag
13663 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 er->nSavepoint;
13664 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
13665 74 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 t number of save
13666 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 points */.. if(
13667 20 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 nSavepoint>nCur
13668 72 65 6e 74 20 26 26 20 70 50 61 67 65 72 2d 3e rent && pPager->
13669 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 useJournal ){.
1366a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 int ii;
1366b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1366c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
1366d 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a rator variable *
1366e 2f 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 /. PagerSavep
1366f 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 oint *aNew;
13670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13671 20 4e 65 77 20 50 61 67 65 72 2e 61 53 61 76 65 New Pager.aSave
13672 70 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a point array */..
13673 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 /* Either th
13674 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76 65 ere is no active
13675 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 journal or the
13676 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f sub-journal is o
13677 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 pen or . ** t
13678 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c he journal is al
13679 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20 6d ways stored in m
1367a 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 emory */. ass
1367b 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 ert( pPager->nSa
1367c 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 vepoint==0 || is
1367d 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 Open(pPager->sjf
1367e 64 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 d) ||.
1367f 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
13680 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
13681 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
13682 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 );.. /* Grow
13683 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 the Pager.aSavep
13684 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e 67 oint array using
13685 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 realloc(). Retu
13686 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a rn SQLITE_NOMEM.
13687 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61 6c ** if the al
13688 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 location fails.
13689 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 Otherwise, zero
1368a 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 the new portion
1368b 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20 2a in case a . *
1368c 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 * malloc failure
1368d 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 6f occurs while po
1368e 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20 74 pulating it in t
1368f 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 he for(...) loop
13690 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 below.. */.
13691 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65 72 aNew = (Pager
13692 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 Savepoint *)sqli
13693 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 te3Realloc(.
13694 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 pPager->aSav
13695 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 epoint, sizeof(P
13696 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e agerSavepoint)*n
13697 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b Savepoint. );
13698 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 20 29 . if( !aNew )
13699 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1369a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1369b 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61 }. memset(&a
1369c 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 New[nCurrent], 0
1369d 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 , (nSavepoint-nC
1369e 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 urrent) * sizeof
1369f 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 (PagerSavepoint)
136a0 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 61 );. pPager->a
136a1 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 Savepoint = aNew
136a2 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 ;. pPager->nS
136a3 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 avepoint = nSave
136a4 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 point;.. /* P
136a5 6f 70 75 6c 61 74 65 20 74 68 65 20 50 61 67 65 opulate the Page
136a6 72 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 rSavepoint struc
136a7 74 75 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 tures just alloc
136a8 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 ated. */. for
136a9 28 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 (ii=nCurrent; ii
136aa 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b <nSavepoint; ii+
136ab 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 +){. assert
136ac 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
136ad 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20 61 Valid );. a
136ae 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 New[ii].nOrig =
136af 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a pPager->dbSize;.
136b0 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e if( isOpen
136b1 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 (pPager->jfd) &&
136b2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
136b3 4f 66 66 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 Off>0 ){.
136b4 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 aNew[ii].iOffse
136b5 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 t = pPager->jour
136b6 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20 20 7d 65 nalOff;. }e
136b7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 lse{. aNe
136b8 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 w[ii].iOffset =
136b9 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
136ba 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a Pager);. }.
136bb 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 aNew[ii].i
136bc 53 75 62 52 65 63 20 3d 20 70 50 61 67 65 72 2d SubRec = pPager-
136bd 3e 6e 53 75 62 52 65 63 3b 0a 20 20 20 20 20 20 >nSubRec;.
136be 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 aNew[ii].pInSave
136bf 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 point = sqlite3B
136c0 69 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67 itvecCreate(pPag
136c1 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 20 er->dbSize);.
136c2 20 20 20 69 66 28 20 21 61 4e 65 77 5b 69 69 5d if( !aNew[ii]
136c3 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b .pInSavepoint ){
136c4 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
136c5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
136c6 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
136c7 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 75 62 /* Open the sub
136c8 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 -journal, if it
136c9 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
136ca 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20 72 63 pened. */. rc
136cb 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 = openSubJourna
136cc 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a l(pPager);. }..
136cd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
136ce 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
136cf 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f ion is called to
136d0 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c rollback or rel
136d1 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 ease (commit) a
136d2 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 savepoint..** Th
136d3 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 e savepoint to r
136d4 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 elease or rollba
136d5 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 ck need not be t
136d6 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
136d7 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 73 61 76 .** created sav
136d8 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 epoint..**.** Pa
136d9 72 61 6d 65 74 65 72 20 6f 70 20 69 73 20 61 6c rameter op is al
136da 77 61 79 73 20 65 69 74 68 65 72 20 53 41 56 45 ways either SAVE
136db 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f POINT_ROLLBACK o
136dc 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 r SAVEPOINT_RELE
136dd 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 ASE..** If it is
136de 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 SAVEPOINT_RELEA
136df 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 61 73 65 SE, then release
136e0 20 61 6e 64 20 64 65 73 74 72 6f 79 20 74 68 65 and destroy the
136e1 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a savepoint with.
136e2 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 65 70 6f ** index iSavepo
136e3 69 6e 74 2e 20 49 66 20 69 74 20 69 73 20 53 41 int. If it is SA
136e4 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
136e5 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 , then rollback
136e6 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 all changes.** t
136e7 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 hat have occurre
136e8 64 20 73 69 6e 63 65 20 74 68 65 20 73 70 65 63 d since the spec
136e9 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 20 ified savepoint
136ea 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a was created..**.
136eb 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 ** The savepoint
136ec 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 to rollback or
136ed 72 65 6c 65 61 73 65 20 69 73 20 69 64 65 6e 74 release is ident
136ee 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
136ef 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f 69 6e er .** iSavepoin
136f0 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 t. A value of 0
136f1 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 61 74 65 means to operate
136f2 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 on the outermos
136f3 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 28 t savepoint.** (
136f4 74 68 65 20 66 69 72 73 74 20 63 72 65 61 74 65 the first create
136f5 64 29 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 28 d). A value of (
136f6 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 Pager.nSavepoint
136f7 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 72 61 74 -1) means operat
136f8 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f 73 74 e.** on the most
136f9 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 recently create
136fa 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 d savepoint. If
136fb 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 iSavepoint is gr
136fc 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 28 50 eater than.** (P
136fd 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d ager.nSavepoint-
136fe 31 29 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 1), then this fu
136ff 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
13700 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6e 65 p..**.** If a ne
13701 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 73 20 gative value is
13702 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 passed to this f
13703 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 unction, then th
13704 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 e current.** tra
13705 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c nsaction is roll
13706 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73 ed back. This is
13707 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 63 61 different to ca
13708 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 lling .** sqlite
13709 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 3PagerRollback()
1370a 20 62 65 63 61 75 73 65 20 74 68 69 73 20 66 75 because this fu
1370b 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 nction does not
1370c 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 65 terminate.** the
1370d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 transaction or
1370e 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 unlock the datab
1370f 61 73 65 2c 20 69 74 20 6a 75 73 74 20 72 65 73 ase, it just res
13710 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20 63 6f tores the .** co
13711 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 ntents of the da
13712 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 tabase to its or
13713 69 67 69 6e 61 6c 20 73 74 61 74 65 2e 20 0a 2a iginal state. .*
13714 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61 73 65 *.** In any case
13715 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 , all savepoints
13716 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20 67 with an index g
13717 72 65 61 74 65 72 20 74 68 61 6e 20 69 53 61 76 reater than iSav
13718 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65 20 64 epoint .** are d
13719 65 73 74 72 6f 79 65 64 2e 20 49 66 20 74 68 69 estroyed. If thi
1371a 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f s is a release o
1371b 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d 53 41 peration (op==SA
1371c 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 VEPOINT_RELEASE)
1371d 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65 70 6f ,.** then savepo
1371e 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 69 int iSavepoint i
1371f 73 20 61 6c 73 6f 20 64 65 73 74 72 6f 79 65 64 s also destroyed
13720 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
13721 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e ction may return
13722 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 SQLITE_NOMEM if
13723 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
13724 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 6f tion fails,.** o
13725 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f r an IO error co
13726 64 65 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f de if an IO erro
13727 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 r occurs while r
13728 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 0a 2a olling back a .*
13729 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 * savepoint. If
1372a 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c no errors occur,
1372b 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1372c 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51 4c 49 turned..*/ .SQLI
1372d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1372e 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 qlite3PagerSavep
1372f 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 oint(Pager *pPag
13730 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 er, int op, int
13731 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 iSavepoint){. i
13732 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
13733 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6f 70 K;.. assert( op
13734 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 ==SAVEPOINT_RELE
13735 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 ASE || op==SAVEP
13736 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b OINT_ROLLBACK );
13737 0a 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65 . assert( iSave
13738 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d point>=0 || op==
13739 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1373a 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69 53 61 CK );.. if( iSa
1373b 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72 2d 3e vepoint<pPager->
1373c 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 nSavepoint ){.
1373d 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 int ii;
1373e 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
1373f 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 variable */.
13740 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 20 20 int nNew;
13741 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
13742 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76 65 70 remaining savep
13743 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68 69 73 oints after this
13744 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 op. */.. /*
13745 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d Figure out how m
13746 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 77 any savepoints w
13747 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61 63 74 ill still be act
13748 69 76 65 20 61 66 74 65 72 20 74 68 69 73 0a 20 ive after this.
13749 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e ** operation.
1374a 20 53 74 6f 72 65 20 74 68 69 73 20 76 61 6c 75 Store this valu
1374b 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65 6e 20 e in nNew. Then
1374c 66 72 65 65 20 72 65 73 6f 75 72 63 65 73 20 61 free resources a
1374d 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20 20 2a ssociated . *
1374e 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76 65 70 * with any savep
1374f 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65 20 64 oints that are d
13750 65 73 74 72 6f 79 65 64 20 62 79 20 74 68 69 73 estroyed by this
13751 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 operation..
13752 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20 69 53 */. nNew = iS
13753 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d avepoint + (op==
13754 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
13755 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d CK);. for(ii=
13756 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 72 2d nNew; ii<pPager-
13757 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b >nSavepoint; ii+
13758 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
13759 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
1375a 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
1375b 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 t[ii].pInSavepoi
1375c 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 nt);. }. p
1375d 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
1375e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f t = nNew;.. /
1375f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 72 * If this is a r
13760 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 69 6f ollback operatio
13761 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74 68 65 20 n, playback the
13762 73 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f specified savepo
13763 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 int.. ** If t
13764 68 69 73 20 69 73 20 61 20 74 65 6d 70 2d 66 69 his is a temp-fi
13765 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 le, it is possib
13766 6c 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 le that the jour
13767 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a 20 20 20 nal file has.
13768 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e ** not yet been
13769 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 opened. In this
1376a 20 63 61 73 65 20 74 68 65 72 65 20 68 61 76 65 case there have
1376b 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73 been no changes
1376c 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 to. ** the d
1376d 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 73 6f atabase file, so
1376e 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 6f 70 the playback op
1376f 65 72 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 73 eration can be s
13770 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 kipped.. */.
13771 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 45 50 if( op==SAVEP
13772 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 OINT_ROLLBACK &&
13773 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
13774 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 50 61 jfd) ){. Pa
13775 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 gerSavepoint *pS
13776 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 avepoint = (nNew
13777 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e ==0)?0:&pPager->
13778 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d aSavepoint[nNew-
13779 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 1];. rc = p
1377a 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 agerPlaybackSave
1377b 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 70 53 point(pPager, pS
1377c 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 avepoint);.
1377d 20 61 73 73 65 72 74 28 72 63 21 3d 53 51 4c 49 assert(rc!=SQLI
1377e 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a TE_DONE);. }.
1377f 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 . /* If thi
13780 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f s is a release o
13781 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 f the outermost
13782 73 61 76 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63 savepoint, trunc
13783 61 74 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 ate . ** the
13784 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 7a sub-journal to z
13785 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a ero bytes in siz
13786 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4e e. */. if( nN
13787 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53 41 56 ew==0 && op==SAV
13788 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 26 EPOINT_RELEASE &
13789 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d & isOpen(pPager-
1378a 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 >sjfd) ){.
1378b 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1378c 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 TE_OK );. r
1378d 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 c = sqlite3OsTru
1378e 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 73 6a ncate(pPager->sj
1378f 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 fd, 0);. pP
13790 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 ager->nSubRec =
13791 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
13792 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
13793 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 ** Return the fu
13794 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 ll pathname of t
13795 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13796 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
13797 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
13798 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
13799 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 name(Pager *pPag
1379a 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
1379b 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b ager->zFilename;
1379c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1379d 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 the VFS structu
1379e 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 re for the pager
1379f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
137a0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
137a1 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 3_vfs *sqlite3Pa
137a2 67 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50 gerVfs(Pager *pP
137a3 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
137a4 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a pPager->pVfs;.}.
137a5 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
137a6 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f e file handle fo
137a7 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 r the database f
137a8 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a ile associated.*
137a9 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72 * with the pager
137aa 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 . This might re
137ab 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 turn NULL if the
137ac 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 file has.** not
137ad 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 yet been opened
137ae 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
137af 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ATE sqlite3_file
137b0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
137b1 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 le(Pager *pPager
137b2 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
137b3 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a er->fd;.}../*.**
137b4 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c Return the full
137b5 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 pathname of the
137b6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
137b7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
137b8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
137b9 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
137ba 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 name(Pager *pPag
137bb 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
137bc 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a ager->zJournal;.
137bd 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
137be 74 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20 true if fsync()
137bf 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c calls are disabl
137c0 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 ed for this page
137c1 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 r. Return FALSE
137c2 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 .** if fsync()s
137c3 61 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 are executed nor
137c4 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 mally..*/.SQLITE
137c5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
137c6 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 ite3PagerNosync(
137c7 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
137c8 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
137c9 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 >noSync;.}..#ifd
137ca 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ef SQLITE_HAS_CO
137cb 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 DEC./*.** Set th
137cc 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68 69 73 e codec for this
137cd 20 70 61 67 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 pager.*/.SQLITE
137ce 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
137cf 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 lite3PagerSetCod
137d0 65 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 ec(. Pager *pPa
137d1 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 ger,. void *(*x
137d2 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 Codec)(void*,voi
137d3 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 d*,Pgno,int),.
137d4 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 72 67 0a void *pCodecArg.
137d5 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f ){. pPager->xCo
137d6 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a 20 20 dec = xCodec;.
137d7 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 41 72 pPager->pCodecAr
137d8 67 20 3d 20 70 43 6f 64 65 63 41 72 67 3b 0a 7d g = pCodecArg;.}
137d9 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
137da 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
137db 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f OVACUUM./*.** Mo
137dc 76 65 20 74 68 65 20 70 61 67 65 20 70 50 67 20 ve the page pPg
137dd 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f to location pgno
137de 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a in the file..**
137df 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 .** There must b
137e0 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73 20 e no references
137e1 74 6f 20 74 68 65 20 70 61 67 65 20 70 72 65 76 to the page prev
137e2 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 iously located a
137e3 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 t.** pgno (which
137e4 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 we call pPgOld)
137e5 20 74 68 6f 75 67 68 20 74 68 61 74 20 70 61 67 though that pag
137e6 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 e is allowed to
137e7 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 be.** in cache.
137e8 20 49 66 20 74 68 65 20 70 61 67 65 20 70 72 65 If the page pre
137e9 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 viously located
137ea 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 at pgno is not a
137eb 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 lready.** in the
137ec 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
137ed 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75 74 l, it is not put
137ee 20 74 68 65 72 65 20 62 79 20 62 79 20 74 68 69 there by by thi
137ef 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a s routine..**.**
137f0 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 References to t
137f1 68 65 20 70 61 67 65 20 70 50 67 20 72 65 6d 61 he page pPg rema
137f2 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74 69 in valid. Updati
137f3 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 ng any.** meta-d
137f4 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
137f5 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64 61 ith pPg (i.e. da
137f6 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 ta stored in the
137f7 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a 2a nExtra bytes.**
137f8 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 allocated along
137f9 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29 20 with the page)
137fa 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 is the responsib
137fb 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c ility of the cal
137fc 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 ler..**.** A tra
137fd 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 nsaction must be
137fe 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 68 69 active when thi
137ff 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
13800 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f 20 led. It used to
13801 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 74 be.** required t
13802 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 hat a statement
13803 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
13804 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74 20 not active, but
13805 74 68 69 73 20 72 65 73 74 72 69 63 74 69 6f 6e this restriction
13806 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65 6d .** has been rem
13807 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 4e 44 oved (CREATE IND
13808 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 EX needs to move
13809 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 20 73 a page when a s
1380a 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e tatement.** tran
1380b 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
1380c 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 e)..**.** If the
1380d 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 fourth argument
1380e 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e , isCommit, is n
1380f 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 on-zero, then th
13810 69 73 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 is page is being
13811 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61 72 .** moved as par
13812 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 t of a database
13813 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a reorganization j
13814 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 74 ust before the t
13815 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69 ransaction .** i
13816 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 s being committe
13817 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c d. In this case,
13818 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 it is guarantee
13819 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 d that the datab
1381a 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50 67 ase page .** pPg
1381b 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c 20 refers to will
1381c 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 not be written t
1381d 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 o again within t
1381e 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e his transaction.
1381f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
13820 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 tion may return
13821 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 SQLITE_NOMEM or
13822 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
13823 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 if an error.**
13824 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69 73 occurs. Otherwis
13825 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 e, it returns SQ
13826 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 LITE_OK..*/.SQLI
13827 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
13828 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 qlite3PagerMovep
13829 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 age(Pager *pPage
1382a 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20 r, DbPage *pPg,
1382b 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69 Pgno pgno, int i
1382c 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64 sCommit){. PgHd
1382d 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20 20 r *pPgOld;
1382e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1382f 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72 77 page being overw
13830 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e ritten. */. Pgn
13831 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d o needSyncPgno =
13832 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c 64 0; /* Old
13833 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e 70 value of pPg->p
13834 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73 20 gno, if sync is
13835 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e required */. in
13836 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
13837 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
13838 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 turn code */. P
13839 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20 20 gno origPgno;
1383a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1383b 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 he original page
1383c 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 73 number */.. as
1383d 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e sert( pPg->nRef>
1383e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 0 );.. /* If th
1383f 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f 76 e page being mov
13840 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64 20 ed is dirty and
13841 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61 76 has not been sav
13842 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73 74 ed by the latest
13843 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2c . ** savepoint,
13844 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20 63 then save the c
13845 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 urrent contents
13846 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f of the page into
13847 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d 6a the . ** sub-j
13848 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69 73 ournal now. This
13849 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 is required to
1384a 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c 6f handle the follo
1384b 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a 20 wing scenario:.
1384c 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e **. ** BEGIN
1384d 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 ;. ** <jour
1384e 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 6e nal page X, then
1384f 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d 65 modify it in me
13850 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20 53 mory>. ** S
13851 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 AVEPOINT one;.
13852 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20 70 ** <Move p
13853 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69 6f age X to locatio
13854 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52 4f n Y>. ** RO
13855 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20 LLBACK TO one;.
13856 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67 65 **. ** If page
13857 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69 74 X were not writ
13858 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d 6a ten to the sub-j
13859 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74 20 ournal here, it
1385a 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20 62 would not. ** b
1385b 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 e possible to re
1385c 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65 6e store its conten
1385d 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f 4c ts when the "ROL
1385e 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20 20 LBACK TO one".
1385f 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 72 ** statement wer
13860 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e 0a e is processed..
13861 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f 75 **. ** subjou
13862 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20 6e rnalPage() may n
13863 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 eed to allocate
13864 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 70 space to store p
13865 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20 20 Pg->pgno into.
13866 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 ** one or more s
13867 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 avepoint bitvecs
13868 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 65 . This is the re
13869 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 ason this functi
1386a 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74 75 on. ** may retu
1386b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e rn SQLITE_NOMEM.
1386c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d . */. if( pPg-
1386d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
1386e 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a 52 65 TY . && subjRe
1386f 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a quiresPage(pPg).
13870 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 && SQLITE_OK!
13871 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 =(rc = subjourna
13872 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 7b lPage(pPg)). ){
13873 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
13874 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 }.. PAGERTRAC
13875 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 65 E(("MOVE %d page
13876 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 64 %d (needSync=%d
13877 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22 ) moves to %d\n"
13878 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 44 , . PAGERID
13879 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
1387a 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73 gno, (pPg->flags
1387b 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
1387c 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a 20 )?1:0, pgno));.
1387d 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 20 IOTRACE(("MOVE
1387e 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 61 %p %d %d\n", pPa
1387f 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 ger, pPg->pgno,
13880 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 20 pgno)).. /* If
13881 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 the journal need
13882 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 s to be sync()ed
13883 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 67 before page pPg
13884 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a 20 ->pgno can. **
13885 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 73 be written to, s
13886 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 tore pPg->pgno i
13887 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 n local variable
13888 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 needSyncPgno..
13889 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 **. ** If the
1388a 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 isCommit flag is
1388b 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 6e set, there is n
1388c 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 o need to rememb
1388d 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 er that. ** the
1388e 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 journal needs t
1388f 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 o be sync()ed be
13890 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 fore database pa
13891 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 20 ge pPg->pgno .
13892 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 ** can be writte
13893 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 n to. The caller
13894 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f has already pro
13895 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 mised not to wri
13896 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a 20 te to it.. */.
13897 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 if( (pPg->flags
13898 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
13899 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 29 ) && !isCommit )
1389a 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 67 {. needSyncPg
1389b 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a no = pPg->pgno;.
1389c 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 assert( page
1389d 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c InJournal(pPg) |
1389e 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 | pPg->pgno>pPag
1389f 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
138a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
138a1 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
138a2 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73 65 IRTY );. asse
138a3 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 rt( pPager->need
138a4 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f Sync );. }.. /
138a5 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20 63 * If the cache c
138a6 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 77 ontains a page w
138a7 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 ith page-number
138a8 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a pgno, remove it.
138a9 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 61 ** from its ha
138aa 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20 sh chain. Also,
138ab 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 if the PgHdr.nee
138ac 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 66 6f dSync was set fo
138ad 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67 6e r . ** page pgn
138ae 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d 6f o before the 'mo
138af 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69 ve' operation, i
138b0 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 t needs to be re
138b1 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f 72 tained . ** for
138b2 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64 20 the page moved
138b3 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 50 there.. */. pP
138b4 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 g->flags &= ~PGH
138b5 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
138b6 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f 6c pPgOld = pager_l
138b7 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 ookup(pPager, pg
138b8 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 no);. assert( !
138b9 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c 64 pPgOld || pPgOld
138ba 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 ->nRef==1 );. i
138bb 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20 20 f( pPgOld ){.
138bc 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 pPg->flags |= (
138bd 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 47 pPgOld->flags&PG
138be 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a HDR_NEED_SYNC);.
138bf 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
138c0 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a 20 eDrop(pPgOld);.
138c1 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f 20 3d }.. origPgno =
138c2 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73 71 pPg->pgno;. sq
138c3 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 lite3PcacheMove(
138c4 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 73 71 pPg, pgno);. sq
138c5 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
138c6 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 61 irty(pPg);. pPa
138c7 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 ger->dbModified
138c8 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 64 = 1;.. if( need
138c9 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 20 SyncPgno ){.
138ca 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 67 /* If needSyncPg
138cb 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 no is non-zero,
138cc 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
138cd 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 62 file needs to b
138ce 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 29 e . ** sync()
138cf 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61 ed before any da
138d0 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f ta is written to
138d1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 70 database file p
138d2 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f age needSyncPgno
138d3 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e 74 .. ** Current
138d4 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 65 ly, no such page
138d5 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 70 exists in the p
138d6 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 68 age-cache and th
138d7 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a 6f e . ** "is jo
138d8 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63 20 urnaled" bitvec
138d9 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73 65 flag has been se
138da 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 6f t. This needs to
138db 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 0a be remedied by.
138dc 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 74 ** loading t
138dd 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 he page into the
138de 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 pager-cache and
138df 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 48 setting the PgH
138e0 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 dr.needSync .
138e1 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a 2a ** flag.. **
138e2 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 61 . ** If the a
138e3 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 74 ttempt to load t
138e4 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 he page into the
138e5 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69 6c page-cache fail
138e6 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20 74 s, (due. ** t
138e7 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 o a malloc() or
138e8 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c 65 IO failure), cle
138e9 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74 68 ar the bit in th
138ea 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a 20 e pInJournal[].
138eb 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74 68 ** array. Oth
138ec 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 70 erwise, if the p
138ed 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61 6e age is loaded an
138ee 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e 20 d written again
138ef 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 74 in. ** this t
138f0 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 6d ransaction, it m
138f1 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f ay be written to
138f2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
138f3 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a le before. **
138f4 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69 6e it is synced in
138f5 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
138f6 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20 69 ile. This way, i
138f7 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e 0a t may end up in.
138f8 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e ** the journ
138f9 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20 62 al file twice, b
138fa 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 ut that is not a
138fb 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a 2a problem.. **
138fc 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 . ** The sqli
138fd 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63 61 te3PagerGet() ca
138fe 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 ll may cause the
138ff 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e 63 journal to sync
13900 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a . So make. **
13901 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72 2e sure the Pager.
13902 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 needSync flag is
13903 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a 2f set too.. */
13904 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 . PgHdr *pPgH
13905 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 dr;. assert(
13906 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
13907 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
13908 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 ite3PagerGet(pPa
13909 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e ger, needSyncPgn
1390a 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 o, &pPgHdr);.
1390b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1390c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 OK ){. if(
1390d 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
1390e 61 6c 20 26 26 20 6e 65 65 64 53 79 6e 63 50 67 al && needSyncPg
1390f 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 no<=pPager->dbOr
13910 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 igSize ){.
13911 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 sqlite3BitvecC
13912 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 49 6e lear(pPager->pIn
13913 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53 79 6e Journal, needSyn
13914 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 7d 0a cPgno);. }.
13915 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
13916 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
13917 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b r->needSync = 1;
13918 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13919 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 ger->noSync==0 &
1391a 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 & !MEMDB );.
1391b 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d pPgHdr->flags |=
1391c 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
1391d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 ;. sqlite3Pca
1391e 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 cheMakeDirty(pPg
1391f 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Hdr);. sqlite
13920 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 3PagerUnref(pPgH
13921 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 dr);. }.. /*.
13922 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 ** For an in-me
13923 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6d mory database, m
13924 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72 69 ake sure the ori
13925 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 69 ginal page conti
13926 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69 nues. ** to exi
13927 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 st, in case the
13928 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 transaction need
13929 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 s to roll back.
1392a 20 57 65 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a We allocate. *
1392b 2a 20 74 68 65 20 70 61 67 65 20 6e 6f 77 2c 20 * the page now,
1392c 69 6e 73 74 65 61 64 20 6f 66 20 61 74 20 72 6f instead of at ro
1392d 6c 6c 62 61 63 6b 2c 20 62 65 63 61 75 73 65 20 llback, because
1392e 77 65 20 63 61 6e 20 62 65 74 74 65 72 20 64 65 we can better de
1392f 61 6c 0a 20 20 2a 2a 20 77 69 74 68 20 61 6e 20 al. ** with an
13930 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 out-of-memory er
13931 72 6f 72 20 6e 6f 77 2e 20 20 54 69 63 6b 65 74 ror now. Ticket
13932 20 23 33 37 36 31 2e 0a 20 20 2a 2f 0a 20 20 69 #3761.. */. i
13933 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 f( MEMDB ){.
13934 44 62 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 DbPage *pNew;.
13935 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
13936 67 65 72 41 63 71 75 69 72 65 28 70 50 61 67 65 gerAcquire(pPage
13937 72 2c 20 6f 72 69 67 50 67 6e 6f 2c 20 26 70 4e r, origPgno, &pN
13938 65 77 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 ew, 1);. if(
13939 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1393a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 73 return rc;. s
1393b 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
1393c 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 (pNew);. }.. r
1393d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1393e 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1393f 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
13940 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f r to the data fo
13941 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 r the specified
13942 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f page..*/.SQLITE_
13943 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
13944 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
13945 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a a(DbPage *pPg){.
13946 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e assert( pPg->n
13947 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 Ref>0 || pPg->pP
13948 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 ager->memDb );.
13949 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 return pPg->pDa
1394a 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ta;.}../*.** Ret
1394b 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
1394c 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72 the Pager.nExtr
1394d 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72 a bytes of "extr
1394e 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c a" space .** all
1394f 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 ocated along wit
13950 68 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 h the specified
13951 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f page..*/.SQLITE_
13952 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
13953 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
13954 72 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b ra(DbPage *pPg){
13955 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
13956 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
13957 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 72 return (pPager
13958 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a 30 29 3b ?pPg->pExtra:0);
13959 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 .}../*.** Get/se
1395a 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f t the locking-mo
1395b 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 de for this page
1395c 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f r. Parameter eMo
1395d 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a de must be one.*
1395e 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49 * of PAGER_LOCKI
1395f 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41 NGMODE_QUERY, PA
13960 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
13961 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41 NORMAL or .** PA
13962 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
13963 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68 EXCLUSIVE. If th
13964 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e e parameter is n
13965 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a ot _QUERY, then.
13966 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d ** the locking-m
13967 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 ode is set to th
13968 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 e value specifie
13969 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 d..**.** The ret
1396a 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65 urned value is e
1396b 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b ither PAGER_LOCK
1396c 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f INGMODE_NORMAL o
1396d 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 r.** PAGER_LOCKI
1396e 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
1396f 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 , indicating the
13970 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 current (possib
13971 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c ly updated).** l
13972 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a ocking-mode..*/.
13973 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
13974 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c nt sqlite3PagerL
13975 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 ockingMode(Pager
13976 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d *pPager, int eM
13977 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ode){. assert(
13978 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 eMode==PAGER_LOC
13979 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20 KINGMODE_QUERY.
1397a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
1397b 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 ode==PAGER_LOCKI
1397c 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20 NGMODE_NORMAL.
1397d 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
1397e 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e de==PAGER_LOCKIN
1397f 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 GMODE_EXCLUSIVE
13980 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
13981 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 ER_LOCKINGMODE_Q
13982 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65 UERY<0 );. asse
13983 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e rt( PAGER_LOCKIN
13984 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 GMODE_NORMAL>=0
13985 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 && PAGER_LOCKING
13986 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d MODE_EXCLUSIVE>=
13987 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65 0 );. if( eMode
13988 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e >=0 && !pPager->
13989 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 tempFile ){.
1398a 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
1398b 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 eMode = (u8)eMod
1398c 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
1398d 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63 (int)pPager->exc
1398e 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f lusiveMode;.}../
1398f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 *.** Get/set the
13990 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f journal-mode fo
13991 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 r this pager. Pa
13992 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 rameter eMode mu
13993 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a st be one of:.**
13994 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 .** PAGER_JOU
13995 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a RNALMODE_QUERY.*
13996 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
13997 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a ALMODE_DELETE.**
13998 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
13999 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a LMODE_TRUNCATE.*
1399a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
1399b 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a ALMODE_PERSIST.*
1399c 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
1399d 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 20 ALMODE_OFF.**
1399e 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
1399f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a 20 DE_MEMORY.**.**
139a0 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 If the parameter
139a1 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 is not _QUERY,
139a2 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
139a3 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 _mode is set to
139a4 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65 the.** value spe
139a5 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63 68 cified if the ch
139a6 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 2e ange is allowed.
139a7 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73 20 The change is
139a8 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66 6f disallowed.** fo
139a9 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 r the following
139aa 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 reasons:.**.**
139ab 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 * An in-memory
139ac 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f 6e database can on
139ad 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 72 ly have its jour
139ae 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f 20 nal_mode set to
139af 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 20 _OFF.** or
139b0 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 20 _MEMORY..**.**
139b1 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 * The journal
139b2 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 mode may not be
139b3 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61 20 changed while a
139b4 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
139b5 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ctive..**.** The
139b6 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61 returned indica
139b7 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 te the current (
139b8 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 possibly updated
139b9 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0a ) journal-mode..
139ba 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
139bb 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
139bc 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 erJournalMode(Pa
139bd 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
139be 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 eMode){. asser
139bf 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f t( eMode==PAGER_
139c0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 JOURNALMODE_QUER
139c1 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c Y. ||
139c2 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
139c3 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 URNALMODE_DELETE
139c4 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
139c5 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
139c6 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
139c7 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c E. ||
139c8 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
139c9 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 URNALMODE_PERSIS
139ca 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c T. ||
139cb 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
139cc 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 URNALMODE_OFF .
139cd 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
139ce 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
139cf 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b ALMODE_MEMORY );
139d0 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 . assert( PAGER
139d1 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 _JOURNALMODE_QUE
139d2 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65 4d RY<0 );. if( eM
139d3 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21 4d ode>=0. && (!M
139d4 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 EMDB || eMode==P
139d5 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
139d6 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 20 _MEMORY .
139d7 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d || eMode=
139d8 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
139d9 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21 70 DE_OFF). && !p
139da 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
139db 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65 6e d. && (!isOpen
139dc 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c (pPager->jfd) ||
139dd 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 0==pPager->jour
139de 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20 20 nalOff). ){.
139df 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
139e0 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 er->jfd) ){.
139e1 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
139e2 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
139e3 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d }. pPager-
139e4 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 28 >journalMode = (
139e5 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 u8)eMode;. }.
139e6 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 return (int)pPag
139e7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b er->journalMode;
139e8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 .}../*.** Get/se
139e9 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 74 t the size-limit
139ea 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69 73 used for persis
139eb 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c tent journal fil
139ec 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e es..**.** Settin
139ed 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 74 g the size limit
139ee 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f 20 to -1 means no
139ef 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 65 limit is enforce
139f0 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 d..** An attempt
139f1 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74 20 to set a limit
139f2 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31 20 smaller than -1
139f3 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 is a no-op..*/.S
139f4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 QLITE_PRIVATE i6
139f5 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 4 sqlite3PagerJo
139f6 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 urnalSizeLimit(P
139f7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 36 ager *pPager, i6
139f8 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 28 4 iLimit){. if(
139f9 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a 20 iLimit>=-1 ){.
139fa 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
139fb 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 4c alSizeLimit = iL
139fc 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 imit;. }. retu
139fd 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e rn pPager->journ
139fe 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a alSizeLimit;.}..
139ff 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 /*.** Return a p
13a00 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 50 ointer to the pP
13a01 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 61 ager->pBackup va
13a02 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 6b riable. The back
13a03 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e 20 up module.** in
13a04 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69 backup.c maintai
13a05 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f ns the content o
13a06 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e f this variable.
13a07 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a 20 This module.**
13a08 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c 79 uses it opaquely
13a09 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 as an argument
13a0a 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 to sqlite3Backup
13a0b 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a 2a Restart() and.**
13a0c 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 sqlite3BackupUp
13a0d 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f 0a date() only..*/.
13a0e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
13a0f 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a qlite3_backup **
13a10 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b sqlite3PagerBack
13a11 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 61 upPtr(Pager *pPa
13a12 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 26 ger){. return &
13a13 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 3b pPager->pBackup;
13a14 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
13a15 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f LITE_OMIT_DISKIO
13a16 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
13a17 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 **** End of page
13a18 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r.c ************
13a19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a1b 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
13a1c 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
13a1d 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a btmutex.c ******
13a1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a20 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
13a21 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20 August 27.**.**
13a22 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
13a23 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
13a24 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
13a25 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
13a26 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
13a27 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
13a28 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
13a29 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
13a2a 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
13a2b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
13a2c 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
13a2d 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
13a2e 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
13a2f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
13a30 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
13a31 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
13a32 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
13a33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a37 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 ********.**.** $
13a38 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76 20 Id: btmutex.c,v
13a39 31 2e 31 35 20 32 30 30 39 2f 30 34 2f 31 30 20 1.15 2009/04/10
13a3a 31 32 3a 35 35 3a 31 37 20 64 61 6e 69 65 6c 6b 12:55:17 danielk
13a3b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 1977 Exp $.**.**
13a3c 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
13a3d 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f ins code used to
13a3e 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
13a3f 65 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 es on Btree obje
13a40 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 cts..** This cod
13a41 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 e really belongs
13a42 20 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75 in btree.c. Bu
13a43 74 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74 t btree.c is get
13a44 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 ting too.** big
13a45 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62 and we want to b
13a46 72 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d reak it down som
13a47 65 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65 e. This package
13a48 64 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a d seemed like.**
13a49 20 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 a good breakout
13a4a 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..*/./**********
13a4b 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 **** Include btr
13a4c 65 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d eeInt.h in the m
13a4d 69 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 iddle of btmutex
13a4e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
13a4f 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
13a50 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
13a51 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a btreeInt.h *****
13a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a54 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
13a55 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 April 6.**.** Th
13a56 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
13a57 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
13a58 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
13a59 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
13a5a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
13a5b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
13a5c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
13a5d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
13a5e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
13a5f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
13a60 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
13a61 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
13a62 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
13a63 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
13a64 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
13a65 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
13a66 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
13a67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13a6b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 ******.** $Id: b
13a6c 74 72 65 65 49 6e 74 2e 68 2c 76 20 31 2e 34 36 treeInt.h,v 1.46
13a6d 20 32 30 30 39 2f 30 33 2f 32 30 20 31 34 3a 31 2009/03/20 14:1
13a6e 38 3a 35 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 8:52 danielk1977
13a6f 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 Exp $.**.** Thi
13a70 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
13a71 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 s a external (di
13a72 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 sk-based) databa
13a73 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e se using BTrees.
13a74 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69 6c .** For a detail
13a75 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66 ed discussion of
13a76 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20 74 BTrees, refer t
13a77 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 o.**.** Dona
13a78 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48 45 ld E. Knuth, THE
13a79 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45 52 ART OF COMPUTER
13a7a 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56 6f PROGRAMMING, Vo
13a7b 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20 22 lume 3:.** "
13a7c 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61 72 Sorting And Sear
13a7d 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34 37 ching", pages 47
13a7e 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d 57 3-480. Addison-W
13a7f 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75 62 esley.** Pub
13a80 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79 2c lishing Company,
13a81 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61 63 Reading, Massac
13a82 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 husetts..**.** T
13a83 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69 73 he basic idea is
13a84 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65 20 that each page
13a85 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 of the file cont
13a86 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65 0a ains N database.
13a87 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20 4e ** entries and N
13a88 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 +1 pointers to s
13a89 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 ubpages..**.**
13a8a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
13a8b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a8c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a8d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a8e 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30 29 -.** | Ptr(0)
13a8f 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72 28 | Key(0) | Ptr(
13a90 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e 2e 1) | Key(1) | ..
13a91 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20 50 . | Key(N-1) | P
13a92 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d tr(N) |.** ---
13a93 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a94 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a95 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13a96 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
13a97 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b .** All of the k
13a98 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 eys on the page
13a99 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69 6e that Ptr(0) poin
13a9a 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75 65 ts to have value
13a9b 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 4b s less.** than K
13a9c 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20 74 ey(0). All of t
13a9d 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65 20 he keys on page
13a9e 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20 73 Ptr(1) and its s
13a9f 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a 20 ubpages have.**
13aa0 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74 values greater t
13aa1 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20 6c han Key(0) and l
13aa2 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29 2e ess than Key(1).
13aa3 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 All of the key
13aa4 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20 61 s.** on Ptr(N) a
13aa5 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20 nd its subpages
13aa6 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65 61 have values grea
13aa7 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d 31 ter than Key(N-1
13aa8 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f ). And.** so fo
13aa9 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 rth..**.** Findi
13aaa 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 ng a particular
13aab 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65 61 key requires rea
13aac 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20 70 ding O(log(M)) p
13aad 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a ages from the .*
13aae 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20 69 * disk where M i
13aaf 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
13ab0 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 entries in the t
13ab1 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 ree..**.** In th
13ab2 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
13ab3 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 n, a single file
13ab4 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 can hold one or
13ab5 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 0a more separate .
13ab6 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63 68 ** BTrees. Each
13ab7 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74 69 BTree is identi
13ab8 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64 65 fied by the inde
13ab9 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70 61 x of its root pa
13aba 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79 20 ge. The.** key
13abb 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e 79 and data for any
13abc 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62 69 entry are combi
13abd 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 20 ned to form the
13abe 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a "payload". A.**
13abf 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 fixed amount of
13ac0 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65 20 payload can be
13ac1 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c 79 carried directly
13ac2 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
13ac3 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74 68 .** page. If th
13ac4 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61 72 e payload is lar
13ac5 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 ger than the pre
13ac6 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e 20 set amount then
13ac7 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65 73 surplus.** bytes
13ac8 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 6f are stored on o
13ac9 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 verflow pages.
13aca 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 The payload for
13acb 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 an entry.** and
13acc 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70 6f the preceding po
13acd 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69 6e inter are combin
13ace 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43 65 ed to form a "Ce
13acf 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20 70 ll". Each .** p
13ad0 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c 20 age has a small
13ad1 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f 6e header which con
13ad2 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e 29 tains the Ptr(N)
13ad3 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74 68 pointer and oth
13ad4 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f er.** informatio
13ad5 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73 69 n such as the si
13ad6 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61 ze of key and da
13ad7 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 ta..**.** FORMAT
13ad8 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 DETAILS.**.** T
13ad9 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69 64 he file is divid
13ada 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20 20 ed into pages.
13adb 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 The first page i
13adc 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2c s called page 1,
13add 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 69 .** the second i
13ade 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73 6f s page 2, and so
13adf 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65 20 forth. A page
13ae0 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 69 number of zero i
13ae1 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 ndicates.** "no
13ae2 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68 65 such page". The
13ae3 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 62 page size can b
13ae4 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 77 65 e anything betwe
13ae5 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 en 512 and 65536
13ae6 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63 ..** Each page c
13ae7 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 62 an be either a b
13ae8 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 65 tree page, a fre
13ae9 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 61 6e elist page or an
13aea 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 overflow.** pag
13aeb 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 e..**.** The fir
13aec 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 61 79 st page is alway
13aed 73 20 61 20 62 74 72 65 65 20 70 61 67 65 2e 20 s a btree page.
13aee 20 54 68 65 20 66 69 72 73 74 20 31 30 30 20 62 The first 100 b
13aef 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 72 73 ytes of the firs
13af0 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 t.** page contai
13af1 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 61 64 n a special head
13af2 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 68 65 er (the "file he
13af3 61 64 65 72 22 29 20 74 68 61 74 20 64 65 73 63 ader") that desc
13af4 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 2e 0a ribes the file..
13af5 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 ** The format of
13af6 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 65 72 the file header
13af7 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a is as follows:.
13af8 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 **.** OFFSET
13af9 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 SIZE DESCRIP
13afa 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 TION.** 0
13afb 20 20 20 20 31 36 20 20 20 20 20 48 65 61 64 65 16 Heade
13afc 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c 69 74 r string: "SQLit
13afd 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 22 0a e format 3\000".
13afe 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 20 20 ** 16
13aff 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 65 20 2 Page size
13b00 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20 20 in bytes. .**
13b01 20 20 20 31 38 20 20 20 20 20 20 20 31 20 20 20 18 1
13b02 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77 72 File format wr
13b03 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 ite version.**
13b04 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20 20 19 1
13b05 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72 65 File format re
13b06 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 ad version.**
13b07 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20 20 20 1
13b08 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 Bytes of unused
13b09 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 6e space at the en
13b0a 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0a 2a d of each page.*
13b0b 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 20 31 * 21 1
13b0c 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 64 65 Max embedde
13b0d 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
13b0e 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 20 20 on.** 22
13b0f 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d 62 1 Min emb
13b10 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 edded payload fr
13b11 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 33 action.** 23
13b12 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 1 Min
13b13 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 leaf payload fr
13b14 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 34 action.** 24
13b15 20 20 20 20 20 20 20 34 20 20 20 20 20 46 69 6c 4 Fil
13b16 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 e change counter
13b17 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 20 20 .** 28
13b18 20 34 20 20 20 20 20 52 65 73 65 72 76 65 64 20 4 Reserved
13b19 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 0a 2a for future use.*
13b1a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 20 34 * 32 4
13b1b 20 20 20 20 20 46 69 72 73 74 20 66 72 65 65 6c First freel
13b1c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 ist page.**
13b1d 33 36 20 20 20 20 20 20 20 34 20 20 20 20 20 4e 36 4 N
13b1e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73 umber of freelis
13b1f 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 t pages in the f
13b20 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 ile.** 40
13b21 20 20 20 36 30 20 20 20 20 20 31 35 20 34 2d 62 60 15 4-b
13b22 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 73 20 yte meta values
13b23 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 65 72 passed to higher
13b24 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 41 6c layers.**.** Al
13b25 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 l of the integer
13b26 20 76 61 6c 75 65 73 20 61 72 65 20 62 69 67 2d values are big-
13b27 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73 69 67 endian (most sig
13b28 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 66 69 nificant byte fi
13b29 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rst)..**.** The
13b2a 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e file change coun
13b2b 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 ter is increment
13b2c 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 ed when the data
13b2d 62 61 73 65 20 69 73 20 63 68 61 6e 67 65 64 0a base is changed.
13b2e 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65 72 20 ** This counter
13b2f 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f allows other pro
13b30 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77 cesses to know w
13b31 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68 61 73 hen the file has
13b32 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 changed.** and
13b33 74 68 75 73 20 77 68 65 6e 20 74 68 65 79 20 6e thus when they n
13b34 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74 68 65 eed to flush the
13b35 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 ir cache..**.**
13b36 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64 65 64 The max embedded
13b37 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
13b38 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 n is the amount
13b39 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75 73 61 of the total usa
13b3a 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69 6e 20 ble.** space in
13b3b 61 20 70 61 67 65 20 74 68 61 74 20 63 61 6e 20 a page that can
13b3c 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 61 be consumed by a
13b3d 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66 6f 72 single cell for
13b3e 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 standard.** B-t
13b3f 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 ree (non-LEAFDAT
13b40 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20 76 61 A) tables. A va
13b41 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61 6e 73 lue of 255 means
13b42 20 31 30 30 25 2e 20 20 54 68 65 20 64 65 66 61 100%. The defa
13b43 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c 69 6d ult.** is to lim
13b44 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 it the maximum c
13b45 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68 61 74 ell size so that
13b46 20 61 74 20 6c 65 61 73 74 20 34 20 63 65 6c 6c at least 4 cell
13b47 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e s will fit.** on
13b48 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68 75 73 one page. Thus
13b49 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 61 78 the default max
13b4a 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 embedded payloa
13b4b 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 36 34 d fraction is 64
13b4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
13b4d 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63 65 6c ayload for a cel
13b4e 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e l is larger than
13b4f 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f 61 64 the max payload
13b50 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a 2a 20 , then extra.**
13b51 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69 6c 6c payload is spill
13b52 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 ed to overflow p
13b53 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e 20 6f ages. Once an o
13b54 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 verflow page is
13b55 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 allocated,.** as
13b56 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73 20 70 many bytes as p
13b57 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f 76 65 ossible are move
13b58 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65 72 66 d into the overf
13b59 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68 6f 75 low pages withou
13b5a 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 68 65 t letting.** the
13b5b 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f 70 20 cell size drop
13b5c 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20 65 6d below the min em
13b5d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 bedded payload f
13b5e 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 raction..**.** T
13b5f 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c he min leaf payl
13b60 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 oad fraction is
13b61 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d 62 like the min emb
13b62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 edded payload fr
13b63 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 action.** except
13b64 20 74 68 61 74 20 69 74 20 61 70 70 6c 69 65 73 that it applies
13b65 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 to leaf nodes i
13b66 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 n a LEAFDATA tre
13b67 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75 6d 0a e. The maximum.
13b68 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ** payload fract
13b69 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46 44 41 ion for a LEAFDA
13b6a 54 41 20 74 72 65 65 20 69 73 20 61 6c 77 61 79 TA tree is alway
13b6b 73 20 31 30 30 25 20 28 6f 72 20 32 35 35 29 20 s 100% (or 255)
13b6c 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 and it.** not sp
13b6d 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 68 ecified in the h
13b6e 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 eader..**.** Eac
13b6f 68 20 62 74 72 65 65 20 70 61 67 65 73 20 69 73 h btree pages is
13b70 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 68 divided into th
13b71 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20 20 54 ree sections: T
13b72 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 0a 2a he header, the.*
13b73 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 * cell pointer a
13b74 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20 63 65 rray, and the ce
13b75 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e ll content area.
13b76 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20 68 61 Page 1 also ha
13b77 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a 2a 20 s a 100-byte.**
13b78 66 69 6c 65 20 68 65 61 64 65 72 20 74 68 61 74 file header that
13b79 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20 74 occurs before t
13b7a 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a he page header..
13b7b 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d **.** |----
13b7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a ------------|.**
13b7d 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68 65 61 | file hea
13b7e 64 65 72 20 20 20 20 7c 20 20 20 31 30 30 20 62 der | 100 b
13b7f 79 74 65 73 2e 20 20 50 61 67 65 20 31 20 6f 6e ytes. Page 1 on
13b80 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d ly..** |---
13b81 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a -------------|.*
13b82 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20 68 65 * | page he
13b83 61 64 65 72 20 20 20 20 7c 20 20 20 38 20 62 79 ader | 8 by
13b84 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73 2e 20 tes for leaves.
13b85 20 31 32 20 62 79 74 65 73 20 66 6f 72 20 69 6e 12 bytes for in
13b86 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 terior nodes.**
13b87 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------
13b88 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 ------|.**
13b89 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 20 | cell pointer
13b8a 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65 73 20 | | 2 bytes
13b8b 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72 74 65 per cell. Sorte
13b8c 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 20 20 d order..**
13b8d 20 7c 20 61 72 72 61 79 20 20 20 20 20 20 20 20 | array
13b8e 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73 20 64 | | Grows d
13b8f 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20 20 20 ownward.**
13b90 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |
13b91 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20 20 7c | v.** |
13b92 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
13b93 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c |.** | unal
13b94 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a 2a 20 located |.**
13b95 20 20 20 20 20 7c 20 73 70 61 63 65 20 20 20 20 | space
13b96 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 |.**
13b97 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------------
13b98 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20 75 70 -| ^ Grows up
13b99 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20 7c 20 wards.** |
13b9a 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 20 7c cell content |
13b9b 20 20 20 7c 20 20 41 72 62 69 74 72 61 72 79 20 | Arbitrary
13b9c 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65 72 73 order interspers
13b9d 65 64 20 77 69 74 68 20 66 72 65 65 62 6c 6f 63 ed with freebloc
13b9e 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 ks..** | ar
13b9f 65 61 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 ea |
13ba0 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73 70 61 | and free spa
13ba1 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a ce fragments..**
13ba2 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d |---------
13ba3 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 -------|.**.** T
13ba4 68 65 20 70 61 67 65 20 68 65 61 64 65 72 73 20 he page headers
13ba5 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a looks like this:
13ba6 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 .**.** OFFSET
13ba7 20 20 53 49 5a 45 20 20 20 20 20 44 45 53 43 52 SIZE DESCR
13ba8 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 IPTION.** 0
13ba9 20 20 20 20 20 20 20 31 20 20 20 20 20 20 46 6c 1 Fl
13baa 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 ags. 1: intkey,
13bab 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34 3a 20 2: zerodata, 4:
13bac 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c 65 61 leafdata, 8: lea
13bad 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 f.** 1
13bae 20 20 32 20 20 20 20 20 20 62 79 74 65 20 6f 66 2 byte of
13baf 66 73 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 fset to the firs
13bb0 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 t freeblock.**
13bb1 20 20 20 20 33 20 20 20 20 20 20 20 32 20 20 20 3 2
13bb2 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c number of cel
13bb3 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a ls on this page.
13bb4 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20 20 20 ** 5
13bb5 32 20 20 20 20 20 20 66 69 72 73 74 20 62 79 74 2 first byt
13bb6 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
13bb7 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20 20 20 ntent area.**
13bb8 20 20 20 37 20 20 20 20 20 20 20 31 20 20 20 20 7 1
13bb9 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 number of frag
13bba 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79 74 65 mented free byte
13bbb 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 s.** 8
13bbc 20 20 34 20 20 20 20 20 20 52 69 67 68 74 20 63 4 Right c
13bbd 68 69 6c 64 20 28 74 68 65 20 50 74 72 28 4e 29 hild (the Ptr(N)
13bbe 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65 value). Omitte
13bbf 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a d on leaves..**.
13bc0 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64 65 66 ** The flags def
13bc1 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74 20 6f ine the format o
13bc2 66 20 74 68 69 73 20 62 74 72 65 65 20 70 61 67 f this btree pag
13bc3 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66 6c 61 e. The leaf fla
13bc4 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 g means that.**
13bc5 74 68 69 73 20 70 61 67 65 20 68 61 73 20 6e 6f this page has no
13bc6 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68 65 20 children. The
13bc7 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 6d 65 zerodata flag me
13bc8 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 70 61 ans that this pa
13bc9 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20 6f 6e ge carries.** on
13bca 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f 20 64 ly keys and no d
13bcb 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b 65 79 ata. The intkey
13bcc 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 flag means that
13bcd 20 74 68 65 20 6b 65 79 20 69 73 20 61 20 69 6e the key is a in
13bce 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68 20 69 teger.** which i
13bcf 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
13bd0 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79 20 6f key size entry o
13bd1 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61 64 65 f the cell heade
13bd2 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 69 6e r rather than in
13bd3 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61 64 20 .** the payload
13bd4 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 area..**.** The
13bd5 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 cell pointer arr
13bd6 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74 68 65 ay begins on the
13bd7 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 first byte afte
13bd8 72 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 r the page heade
13bd9 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 r..** The cell p
13bda 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63 6f 6e ointer array con
13bdb 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f tains zero or mo
13bdc 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62 65 72 re 2-byte number
13bdd 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 6f s which are.** o
13bde 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68 65 20 ffsets from the
13bdf 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
13be0 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 65 6c page to the cel
13be1 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65 l content in the
13be2 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 cell.** content
13be3 20 61 72 65 61 2e 20 20 54 68 65 20 63 65 6c 6c area. The cell
13be4 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75 72 20 pointers occur
13be5 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e in sorted order.
13be6 20 20 54 68 65 20 73 79 73 74 65 6d 20 73 74 72 The system str
13be7 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 ives.** to keep
13be8 66 72 65 65 20 73 70 61 63 65 20 61 66 74 65 72 free space after
13be9 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 the last cell p
13bea 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 20 6e ointer so that n
13beb 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 ew cells can.**
13bec 62 65 20 65 61 73 69 6c 79 20 61 64 64 65 64 20 be easily added
13bed 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 without having t
13bee 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74 68 65 o defragment the
13bef 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c page..**.** Cel
13bf0 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f l content is sto
13bf1 72 65 64 20 61 74 20 74 68 65 20 76 65 72 79 20 red at the very
13bf2 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 end of the page
13bf3 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61 72 64 and grows toward
13bf4 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e the.** beginnin
13bf5 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a g of the page..*
13bf6 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70 61 63 *.** Unused spac
13bf7 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 65 6c e within the cel
13bf8 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 l content area i
13bf9 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f s collected into
13bfa 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f a linked list o
13bfb 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e f.** freeblocks.
13bfc 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f 63 6b Each freeblock
13bfd 20 69 73 20 61 74 20 6c 65 61 73 74 20 34 20 62 is at least 4 b
13bfe 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 ytes in size. T
13bff 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a 2a he byte offset.*
13c00 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 * to the first f
13c01 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69 76 65 reeblock is give
13c02 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e n in the header.
13c03 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f 63 63 Freeblocks occ
13c04 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 ur in.** increas
13c05 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65 63 61 ing order. Beca
13c06 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 use a freeblock
13c07 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 must be at least
13c08 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 4 bytes in size
13c09 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70 20 6f ,.** any group o
13c0a 66 20 33 20 6f 72 20 66 65 77 65 72 20 75 6e 75 f 3 or fewer unu
13c0b 73 65 64 20 62 79 74 65 73 20 69 6e 20 74 68 65 sed bytes in the
13c0c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
13c0d 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 ea cannot.** exi
13c0e 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65 62 6c st on the freebl
13c0f 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20 67 72 ock chain. A gr
13c10 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65 oup of 3 or fewe
13c11 72 20 66 72 65 65 20 62 79 74 65 73 20 69 73 20 r free bytes is
13c12 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 61 67 called.** a frag
13c13 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74 61 6c ment. The total
13c14 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
13c15 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65 6e 74 in all fragment
13c16 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e 0a 2a s is recorded..*
13c17 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 * in the page he
13c18 61 64 65 72 20 61 74 20 6f 66 66 73 65 74 20 37 ader at offset 7
13c19 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 ..**.** SIZE
13c1a 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a DESCRIPTION.*
13c1b 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 79 74 * 2 Byt
13c1c 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 e offset of the
13c1d 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a next freeblock.*
13c1e 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 79 74 * 2 Byt
13c1f 65 73 20 69 6e 20 74 68 69 73 20 66 72 65 65 62 es in this freeb
13c20 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 lock.**.** Cells
13c21 20 61 72 65 20 6f 66 20 76 61 72 69 61 62 6c 65 are of variable
13c22 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20 length. Cells
13c23 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 are stored in th
13c24 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 e cell content a
13c25 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e rea at.** the en
13c26 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 d of the page.
13c27 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 Pointers to the
13c28 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74 68 65 cells are in the
13c29 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
13c2a 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65 ray.** that imme
13c2b 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 diately follows
13c2c 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e the page header.
13c2d 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e Cells is not n
13c2e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 63 6f ecessarily.** co
13c2f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e 20 6f ntiguous or in o
13c30 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c 20 70 rder, but cell p
13c31 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f 6e 74 ointers are cont
13c32 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f 72 iguous and in or
13c33 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 der..**.** Cell
13c34 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20 75 73 content makes us
13c35 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 e of variable le
13c36 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 ngth integers.
13c37 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 A variable.** le
13c38 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69 73 20 ngth integer is
13c39 31 20 74 6f 20 39 20 62 79 74 65 73 20 77 68 65 1 to 9 bytes whe
13c3a 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 re the lower 7 b
13c3b 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a 2a 20 its of each .**
13c3c 62 79 74 65 20 61 72 65 20 75 73 65 64 2e 20 20 byte are used.
13c3d 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 The integer cons
13c3e 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79 74 65 ists of all byte
13c3f 73 20 74 68 61 74 20 68 61 76 65 20 62 69 74 20 s that have bit
13c40 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74 68 65 8 set and.** the
13c41 20 66 69 72 73 74 20 62 79 74 65 20 77 69 74 68 first byte with
13c42 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20 20 54 bit 8 clear. T
13c43 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 he most signific
13c44 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 ant byte of the
13c45 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70 65 61 integer.** appea
13c46 72 73 20 66 69 72 73 74 2e 20 20 41 20 76 61 72 rs first. A var
13c47 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 iable-length int
13c48 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 eger may not be
13c49 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79 74 65 more than 9 byte
13c4a 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 s long..** As a
13c4b 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20 61 6c special case, al
13c4c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 l 8 bytes of the
13c4d 20 39 74 68 20 62 79 74 65 20 61 72 65 20 75 73 9th byte are us
13c4e 65 64 20 61 73 20 64 61 74 61 2e 20 20 54 68 69 ed as data. Thi
13c4f 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 36 34 s.** allows a 64
13c50 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 6f 20 -bit integer to
13c51 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 39 20 be encoded in 9
13c52 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 bytes..**.**
13c53 30 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 0x00
13c54 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
13c55 73 20 20 30 78 30 30 30 30 30 30 30 30 0a 2a 2a s 0x00000000.**
13c56 20 20 20 20 30 78 37 66 20 20 20 20 20 20 20 20 0x7f
13c57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
13c58 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 comes 0x0000007
13c59 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 78 30 f.** 0x81 0x0
13c5a 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
13c5b 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
13c5c 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78 38 32 00080.** 0x82
13c5d 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20 20 0x00
13c5e 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
13c5f 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20 20 20 x00000100.**
13c60 30 78 38 30 20 30 78 37 66 20 20 20 20 20 20 20 0x80 0x7f
13c61 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
13c62 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a s 0x0000007f.**
13c63 20 20 20 20 30 78 38 61 20 30 78 39 31 20 30 78 0x8a 0x91 0x
13c64 64 31 20 30 78 61 63 20 30 78 37 38 20 20 62 65 d1 0xac 0x78 be
13c65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35 36 37 comes 0x1234567
13c66 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 78 38 8.** 0x81 0x8
13c67 31 20 30 78 38 31 20 30 78 38 31 20 30 78 30 31 1 0x81 0x81 0x01
13c68 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 30 32 becomes 0x102
13c69 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 04081.**.** Vari
13c6a 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 able length inte
13c6b 67 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f gers are used fo
13c6c 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74 6f 20 r rowids and to
13c6d 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65 72 20 hold the number
13c6e 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 6b of.** bytes of k
13c6f 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e 20 61 ey and data in a
13c70 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a btree cell..**.
13c71 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f ** The content o
13c72 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c f a cell looks l
13c73 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
13c74 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
13c75 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
13c76 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
13c77 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63 68 69 of the left chi
13c78 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 6c ld. Omitted if l
13c79 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 2e eaf flag is set.
13c7a 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e .** var N
13c7b 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
13c7c 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65 64 20 f data. Omitted
13c7d 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74 61 20 if the zerodata
13c7e 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 flag is set..**
13c7f 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 var Numbe
13c80 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6b 65 r of bytes of ke
13c81 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20 69 74 y. Or the key it
13c82 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79 20 66 self if intkey f
13c83 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 lag is set..**
13c84 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c 6f 61 * Payloa
13c85 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 d.** 4
13c86 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 First page of th
13c87 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e e overflow chain
13c88 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f . Omitted if no
13c89 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 overflow.**.**
13c8a 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 66 Overflow pages f
13c8b 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 orm a linked lis
13c8c 74 2e 20 20 45 61 63 68 20 70 61 67 65 20 65 78 t. Each page ex
13c8d 63 65 70 74 20 74 68 65 20 6c 61 73 74 20 69 73 cept the last is
13c8e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 completely.** f
13c8f 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74 61 20 illed with data
13c90 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20 62 79 (pagesize - 4 by
13c91 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73 74 20 tes). The last
13c92 70 61 67 65 20 63 61 6e 20 68 61 76 65 20 61 73 page can have as
13c93 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 31 20 little.** as 1
13c94 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a 2a 2a byte of data..**
13c95 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 .** SIZE D
13c96 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 ESCRIPTION.**
13c97 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 4 Page nu
13c98 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f 76 65 mber of next ove
13c99 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 20 20 rflow page.**
13c9a 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a 2a 2a * Data.**
13c9b 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70 61 67 .** Freelist pag
13c9c 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 73 es come in two s
13c9d 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b 20 70 ubtypes: trunk p
13c9e 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20 70 61 ages and leaf pa
13c9f 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 6c ges. The.** fil
13ca0 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74 73 20 e header points
13ca1 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 to the first in
13ca2 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 a linked list of
13ca3 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20 45 61 trunk page. Ea
13ca4 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 ch trunk.** page
13ca5 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c 74 69 points to multi
13ca6 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73 2e 20 ple leaf pages.
13ca7 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 The content of
13ca8 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 0a 2a a leaf page is.*
13ca9 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20 * unspecified.
13caa 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c 6f 6f A trunk page loo
13cab 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a ks like this:.**
13cac 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 .** SIZE D
13cad 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 ESCRIPTION.**
13cae 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 4 Page nu
13caf 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74 72 75 mber of next tru
13cb0 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 nk page.**
13cb1 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 4 Number of
13cb2 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e leaf pointers on
13cb3 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 this page.**
13cb4 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72 * zero or
13cb5 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75 6d 62 more pages numb
13cb6 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a 2a 2f ers of leaves.*/
13cb7 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 .../* The follow
13cb8 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74 68 65 ing value is the
13cb9 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 maximum cell si
13cba 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61 ze assuming a ma
13cbb 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69 ximum page.** si
13cbc 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a ze give above..*
13cbd 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c /.#define MX_CEL
13cbe 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28 70 42 L_SIZE(pBt) (pB
13cbf 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 0a 0a t->pageSize-8)..
13cc0 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e /* The maximum n
13cc1 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
13cc2 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 n a single page
13cc3 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
13cc4 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75 6d 65 This.** assume
13cc5 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c s a minimum cell
13cc6 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73 size of 6 bytes
13cc7 20 20 28 34 20 62 79 74 65 73 20 66 6f 72 20 74 (4 bytes for t
13cc8 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 0a 2a he cell itself.*
13cc9 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73 20 66 * plus 2 bytes f
13cca 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 or the index to
13ccb 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 the cell in the
13ccc 70 61 67 65 20 68 65 61 64 65 72 29 2e 20 20 53 page header). S
13ccd 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c uch.** small cel
13cce 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72 65 2c ls will be rare,
13ccf 20 62 75 74 20 74 68 65 79 20 61 72 65 20 70 6f but they are po
13cd0 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 ssible..*/.#defi
13cd1 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 ne MX_CELL(pBt)
13cd2 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d ((pBt->pageSize-
13cd3 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 8)/6)../* Forwar
13cd4 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a d declarations *
13cd5 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
13cd6 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61 67 65 MemPage MemPage
13cd7 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
13cd8 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a BtLock BtLock;.
13cd9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 ./*.** This is a
13cda 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 74 68 magic string th
13cdb 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 at appears at th
13cdc 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 e beginning of e
13cdd 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 very.** SQLite d
13cde 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64 65 72 atabase in order
13cdf 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 to identify the
13ce0 20 66 69 6c 65 20 61 73 20 61 20 72 65 61 6c 20 file as a real
13ce1 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
13ce2 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74 You can change t
13ce3 68 69 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d his value at com
13ce4 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65 pile-time by spe
13ce5 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 cifying a.** -DS
13ce6 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 QLITE_FILE_HEADE
13ce7 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63 R="..." on the c
13ce8 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d ompiler command-
13ce9 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 68 65 line. The.** he
13cea 61 64 65 72 20 6d 75 73 74 20 62 65 20 65 78 61 ader must be exa
13ceb 63 74 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e ctly 16 bytes in
13cec 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f cluding the zero
13ced 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a -terminator so.*
13cee 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 74 73 * the string its
13cef 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 31 35 elf should be 15
13cf0 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 characters long
13cf1 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e 67 65 . If you change
13cf2 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 .** the header,
13cf3 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74 6f 6d then your custom
13cf4 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 6e 6f library will no
13cf5 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61 t be able to rea
13cf6 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 d .** databases
13cf7 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 generated by the
13cf8 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 20 standard tools
13cf9 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61 72 64 and the standard
13cfa 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e tools.** will n
13cfb 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 ot be able to re
13cfc 61 64 20 64 61 74 61 62 61 73 65 73 20 63 72 65 ad databases cre
13cfd 61 74 65 64 20 62 79 20 79 6f 75 72 20 63 75 73 ated by your cus
13cfe 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a tom library..*/.
13cff 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 46 #ifndef SQLITE_F
13d00 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20 31 32 ILE_HEADER /* 12
13d01 33 34 35 36 37 38 39 20 31 32 33 34 35 36 20 2a 3456789 123456 *
13d02 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 /.# define SQLI
13d03 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 22 TE_FILE_HEADER "
13d04 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 22 SQLite format 3"
13d05 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 .#endif../*.** P
13d06 61 67 65 20 74 79 70 65 20 66 6c 61 67 73 2e 20 age type flags.
13d07 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69 6e 61 An ORed combina
13d08 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 66 6c tion of these fl
13d09 61 67 73 20 61 70 70 65 61 72 20 61 73 20 74 68 ags appear as th
13d0a 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74 65 20 e.** first byte
13d0b 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 67 65 of on-disk image
13d0c 20 6f 66 20 65 76 65 72 79 20 42 54 72 65 65 20 of every BTree
13d0d 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 page..*/.#define
13d0e 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30 PTF_INTKEY 0
13d0f 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54 46 5f x01.#define PTF_
13d10 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32 0a 23 ZERODATA 0x02.#
13d11 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44 define PTF_LEAFD
13d12 41 54 41 20 20 30 78 30 34 0a 23 64 65 66 69 6e ATA 0x04.#defin
13d13 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20 20 20 e PTF_LEAF
13d14 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 0x08../*.** As e
13d15 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 ach page of the
13d16 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64 20 69 file is loaded i
13d17 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 nto memory, an i
13d18 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
13d19 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
13d1a 63 74 75 72 65 20 69 73 20 61 70 70 65 6e 64 65 cture is appende
13d1b 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 d and initialize
13d1c 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 69 73 d to zero. This
13d1d 20 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 structure store
13d1e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e s.** information
13d1f 20 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 20 about the page
13d20 74 68 61 74 20 69 73 20 64 65 63 6f 64 65 64 20 that is decoded
13d21 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66 69 6c from the raw fil
13d22 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e page..**.** Th
13d23 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c 64 20 e pParent field
13d24 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f 20 74 points back to t
13d25 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 he parent page.
13d26 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75 73 20 This allows us
13d27 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 74 68 to.** walk up th
13d28 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61 6e 79 e BTree from any
13d29 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72 6f 6f leaf to the roo
13d2a 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20 62 65 t. Care must be
13d2b 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 taken to.** unr
13d2c 65 66 28 29 20 74 68 65 20 70 61 72 65 6e 74 20 ef() the parent
13d2d 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77 68 65 page pointer whe
13d2e 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 6e n this page is n
13d2f 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72 65 6e o longer referen
13d30 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 ced..** The page
13d31 44 65 73 74 72 75 63 74 6f 72 28 29 20 72 6f 75 Destructor() rou
13d32 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74 68 61 tine handles tha
13d33 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 t chore..**.** A
13d34 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66 69 65 ccess to all fie
13d35 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 lds of this stru
13d36 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c cture is control
13d37 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 led by the mutex
13d38 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 .** stored in Me
13d39 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78 mPage.pBt->mutex
13d3a 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50 ..*/.struct MemP
13d3b 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69 age {. u8 isIni
13d3c 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
13d3d 54 72 75 65 20 69 66 20 70 72 65 76 69 6f 75 73 True if previous
13d3e 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 ly initialized.
13d3f 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 20 2a MUST BE FIRST! *
13d40 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77 /. u8 nOverflow
13d41 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
13d42 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63 er of overflow c
13d43 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43 ell bodies in aC
13d44 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e ell[] */. u8 in
13d45 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 tKey;
13d46 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65 /* True if intke
13d47 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f y flag is set */
13d48 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20 . u8 leaf;
13d49 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13d4a 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 if leaf flag is
13d4b 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61 73 44 set */. u8 hasD
13d4c 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ata; /*
13d4d 20 54 72 75 65 20 69 66 20 74 68 69 73 20 70 61 True if this pa
13d4e 67 65 20 73 74 6f 72 65 73 20 64 61 74 61 20 2a ge stores data *
13d4f 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73 65 74 /. u8 hdrOffset
13d50 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30 30 20 ; /* 100
13d51 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30 20 6f for page 1. 0 o
13d52 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38 therwise */. u8
13d53 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 20 20 childPtrSize;
13d54 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61 66 3d /* 0 if leaf=
13d55 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66 3d 3d =1. 4 if leaf==
13d56 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 0 */. u16 maxLo
13d57 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 cal; /* C
13d58 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e opy of BtShared.
13d59 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 maxLocal or BtSh
13d5a 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a ared.maxLeaf */.
13d5b 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 u16 minLocal;
13d5c 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
13d5d 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 6f f BtShared.minLo
13d5e 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e cal or BtShared.
13d5f 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 minLeaf */. u16
13d60 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 cellOffset;
13d61 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 44 /* Index in aD
13d62 61 74 61 20 6f 66 20 66 69 72 73 74 20 63 65 6c ata of first cel
13d63 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 l pointer */. u
13d64 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 16 nFree;
13d65 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
13d66 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e 20 74 free bytes on t
13d67 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 he page */. u16
13d68 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 nCell;
13d69 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
13d6a 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 ells on this pag
13d6b 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f 76 66 e, local and ovf
13d6c 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73 6b 50 l */. u16 maskP
13d6d 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d age; /* M
13d6e 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f 66 66 ask for page off
13d6f 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 set */. struct
13d70 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a _OvflCell { /*
13d71 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c Cells that will
13d72 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74 not fit on aDat
13d73 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70 a[] */. u8 *p
13d74 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f Cell; /
13d75 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 * Pointers to th
13d76 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76 e body of the ov
13d77 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 erflow cell */.
13d78 20 20 20 75 31 36 20 69 64 78 3b 20 20 20 20 20 u16 idx;
13d79 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 /* Insert
13d7a 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72 this cell befor
13d7b 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 e idx-th non-ove
13d7c 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 rflow cell */.
13d7d 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 } aOvfl[5];. Bt
13d7e 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 Shared *pBt;
13d7f 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
13d80 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 74 BtShared that t
13d81 68 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 his page is part
13d82 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 of */. u8 *aDa
13d83 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ta; /*
13d84 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b Pointer to disk
13d85 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61 image of the pa
13d86 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44 62 50 ge data */. DbP
13d87 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 age *pDbPage;
13d88 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67 65 20 /* Pager page
13d89 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f handle */. Pgno
13d8a 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
13d8b 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
13d8c 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f for this page */
13d8d 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 .};../*.** The i
13d8e 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f n-memory image o
13d8f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20 68 61 f a disk page ha
13d90 73 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 s the auxiliary
13d91 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65 information appe
13d92 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 65 nded.** to the e
13d93 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20 nd. EXTRA_SIZE
13d94 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
13d95 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
13d96 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 0a 2a needed to hold.*
13d97 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 * that extra inf
13d98 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 ormation..*/.#de
13d99 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a 45 20 fine EXTRA_SIZE
13d9a 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 0a sizeof(MemPage).
13d9b 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64 ./* A Btree hand
13d9c 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 le.**.** A datab
13d9d 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
13d9e 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
13d9f 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
13da0 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 of.** this obje
13da1 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 ct for every dat
13da2 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 abase file that
13da3 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 it has open. Th
13da4 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
13da5 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65 is opaque to the
13da6 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
13da7 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62 tion. The datab
13da8 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
13da9 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 annot.** see the
13daa 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 internals of th
13dab 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 is structure and
13dac 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68 only deals with
13dad 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
13dae 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a this structure..
13daf 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 **.** For some d
13db0 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74 atabase files, t
13db1 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 he same underlyi
13db2 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 63 68 ng database cach
13db3 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 e might be .** s
13db4 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75 hared between mu
13db5 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f ltiple connectio
13db6 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 ns. In that cas
13db7 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 63 74 69 e, each contecti
13db8 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e on.** has it own
13db9 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
13dba 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61 object. But ea
13dbb 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ch instance of t
13dbc 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f his object.** po
13dbd 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 ints to the same
13dbe 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
13dbf 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 . The database
13dc0 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a cache and the.**
13dc1 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 schema associat
13dc2 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 ed with the data
13dc3 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c base file are al
13dc4 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 l contained with
13dc5 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 in.** the BtShar
13dc6 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a ed object..**.**
13dc7 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74 All fields in t
13dc8 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 his structure ar
13dc9 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 e accessed under
13dca 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a sqlite3.mutex..
13dcb 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74 ** The pBt point
13dcc 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f er itself may no
13dcd 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 t be changed whi
13dce 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 73 20 le there exists
13dcf 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74 cursors .** in t
13dd0 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42 74 he referenced Bt
13dd1 53 68 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e Shared that poin
13dd2 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42 t back to this B
13dd3 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65 tree since those
13dd4 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65 .** cursors have
13dd5 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 67 to do go throug
13dd6 68 20 74 68 69 73 20 42 74 72 65 65 20 74 6f 20 h this Btree to
13dd7 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 68 61 find their BtSha
13dd8 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20 red and.** they
13dd9 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68 often do so with
13dda 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69 out holding sqli
13ddb 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 te3.mutex..*/.st
13ddc 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73 ruct Btree {. s
13ddd 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
13dde 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
13ddf 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c e connection hol
13de0 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 ding this btree
13de1 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 */. BtShared *p
13de2 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61 Bt; /* Shara
13de3 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 ble content of t
13de4 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75 his btree */. u
13de5 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 8 inTrans;
13de6 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c /* TRANS_NONE,
13de7 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54 TRANS_READ or T
13de8 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20 RANS_WRITE */.
13de9 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20 u8 sharable;
13dea 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 /* True if we
13deb 20 63 61 6e 20 73 68 61 72 65 20 70 42 74 20 77 can share pBt w
13dec 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a ith another db *
13ded 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20 /. u8 locked;
13dee 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
13def 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68 f db currently h
13df0 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f as pBt locked */
13df1 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f 63 . int wantToLoc
13df2 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 k; /* Number
13df3 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 of nested calls
13df4 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 to sqlite3BtreeE
13df5 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 6e 74 20 nter() */. int
13df6 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 20 20 2f nBackup; /
13df7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b * Number of back
13df8 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 20 72 65 up operations re
13df9 61 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 ading this btree
13dfa 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e 65 */. Btree *pNe
13dfb 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 xt; /* List
13dfc 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72 61 62 of other sharab
13dfd 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d 20 74 le Btrees from t
13dfe 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20 20 he same db */.
13dff 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20 20 20 Btree *pPrev;
13e00 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74 /* Back point
13e01 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c er of the same l
13e02 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ist */.};../*.**
13e03 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 6d Btree.inTrans m
13e04 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 74 ay take one of t
13e05 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c he following val
13e06 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ues..**.** If th
13e07 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 78 e shared-data ex
13e08 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 6c tension is enabl
13e09 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 ed, there may be
13e0a 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 0a multiple users.
13e0b 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 20 ** of the Btree
13e0c 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d 6f structure. At mo
13e0d 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 st one of these
13e0e 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 65 may open a write
13e0f 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a transaction,.**
13e10 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 20 but any number
13e11 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65 20 may have active
13e12 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e read transaction
13e13 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 s..*/.#define TR
13e14 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66 ANS_NONE 0.#def
13e15 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 20 ine TRANS_READ
13e16 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 1.#define TRANS_
13e17 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 WRITE 2../*.** A
13e18 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
13e19 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 is object repres
13e1a 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 61 ents a single da
13e1b 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
13e1c 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 .** A single dat
13e1d 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 62 abase file can b
13e1e 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 20 e in use as the
13e1f 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 6f same time by two
13e20 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 .** or more data
13e21 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
13e22 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d . When two or m
13e23 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ore connections
13e24 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74 are.** sharing t
13e25 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
13e26 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e 6e file, each conn
13e27 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f 77 ection has it ow
13e28 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 72 n.** private Btr
13e29 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 ee object for th
13e2a 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 20 e file and each
13e2b 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 20 of those Btrees
13e2c 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 69 points.** to thi
13e2d 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 6f s one BtShared o
13e2e 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 64 bject. BtShared
13e2f 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 6d .nRef is the num
13e30 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 ber of.** connec
13e31 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 tions currently
13e32 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 74 sharing this dat
13e33 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
13e34 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 * Fields in this
13e35 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61 structure are a
13e36 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 ccessed under th
13e37 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 e BtShared.mutex
13e38 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 70 .** mutex, excep
13e39 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 70 t for nRef and p
13e3a 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20 61 Next which are a
13e3b 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 ccessed under th
13e3c 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c 49 e.** global SQLI
13e3d 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
13e3e 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 54 MASTER mutex. T
13e3f 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 0a he pPager field.
13e40 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f ** may not be mo
13e41 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 69 dified once it i
13e42 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 20 s initially set
13e43 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e as long as nRef>
13e44 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 6d 0..** The pSchem
13e45 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 73 a field may be s
13e46 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 74 et once under Bt
13e47 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e 64 Shared.mutex and
13e48 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 69 .** thereafter i
13e49 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 6c s unchanged as l
13e4a 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a ong as nRef>0..*
13e4b 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a 0a *.** isPending:.
13e4c 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74 53 **.** If a BtS
13e4d 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61 69 hared client fai
13e4e 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 ls to obtain a w
13e4f 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20 64 rite-lock on a d
13e50 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61 62 atabase.** tab
13e51 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 65 72 le (because ther
13e52 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 20 e exists one or
13e53 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73 20 more read-locks
13e54 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a 2a on the table),.*
13e55 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 2d 63 * the shared-c
13e56 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 65 6e ache enters 'pen
13e57 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 ding-lock' state
13e58 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20 69 and isPending i
13e59 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74 72 s.** set to tr
13e5a 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 ue..**.** The
13e5b 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 61 shared-cache lea
13e5c 76 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e 67 ves the 'pending
13e5d 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68 65 lock' state whe
13e5e 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 20 n either of.**
13e5f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f the following o
13e60 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ccur:.**.**
13e61 31 29 20 54 68 65 20 63 75 72 72 65 6e 74 20 77 1) The current w
13e62 72 69 74 65 72 20 28 42 74 53 68 61 72 65 64 2e riter (BtShared.
13e63 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75 64 pWriter) conclud
13e64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 es its transacti
13e65 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32 29 on, OR.** 2)
13e66 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c The number of l
13e67 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74 68 ocks held by oth
13e68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 er connections d
13e69 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a rops to zero..**
13e6a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20 74 .** while in t
13e6b 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b he 'pending-lock
13e6c 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e 6e ' state, no conn
13e6d 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72 74 ection may start
13e6e 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61 6e a new.** tran
13e6f 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 saction..**.**
13e70 20 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73 This feature is
13e71 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65 6c included to hel
13e72 70 20 70 72 65 76 65 6e 74 20 77 72 69 74 65 72 p prevent writer
13e73 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f 0a -starvation..*/.
13e74 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 struct BtShared
13e75 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 {. Pager *pPage
13e76 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 r; /* The
13e77 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a 20 page cache */.
13e78 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
13e79 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
13e7a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 se connection cu
13e7b 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74 68 rrently using th
13e7c 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42 74 is Btree */. Bt
13e7d 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b Cursor *pCursor;
13e7e 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 /* A list of
13e7f 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 all open cursor
13e80 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a s */. MemPage *
13e81 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 pPage1; /*
13e82 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 First page of th
13e83 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
13e84 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 u8 readOnly;
13e85 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
13e86 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
13e87 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 file is readonly
13e88 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69 7a */. u8 pageSiz
13e89 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20 54 eFixed; /* T
13e8a 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 rue if the page
13e8b 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 size can no long
13e8c 65 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f er be changed */
13e8d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
13e8e 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
13e8f 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d 3b u8 autoVacuum;
13e90 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
13e91 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 if auto-vacuum i
13e92 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 s enabled */. u
13e93 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 8 incrVacuum;
13e94 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
13e95 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65 incr-vacuum is e
13e96 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 66 nabled */.#endif
13e97 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 3b . u16 pageSize;
13e98 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
13e99 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 l number of byte
13e9a 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 s on a page */.
13e9b 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b u16 usableSize;
13e9c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
13e9d 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 of usable bytes
13e9e 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f on each page */
13e9f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b . u16 maxLocal;
13ea0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
13ea1 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 mum local payloa
13ea2 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 d in non-LEAFDAT
13ea3 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 A tables */. u1
13ea4 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 6 minLocal;
13ea5 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c /* Minimum l
13ea6 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 ocal payload in
13ea7 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 non-LEAFDATA tab
13ea8 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 les */. u16 max
13ea9 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f Leaf; /
13eaa 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 * Maximum local
13eab 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 payload in a LEA
13eac 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 FDATA table */.
13ead 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20 20 u16 minLeaf;
13eae 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 /* Minimu
13eaf 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 m local payload
13eb0 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 in a LEAFDATA ta
13eb1 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 ble */. u8 inTr
13eb2 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f ansaction; /
13eb3 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 * Transaction st
13eb4 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 ate */. int nTr
13eb5 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f ansaction; /
13eb6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e * Number of open
13eb7 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 72 transactions (r
13eb8 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f 0a ead + write) */.
13eb9 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 3b void *pSchema;
13eba 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
13ebb 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c 6f er to space allo
13ebc 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 cated by sqlite3
13ebd 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a 2f BtreeSchema() */
13ebe 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 53 . void (*xFreeS
13ebf 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 20 chema)(void*);
13ec0 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
13ec1 72 20 42 74 53 68 61 72 65 64 2e 70 53 63 68 65 r BtShared.pSche
13ec2 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ma */. sqlite3_
13ec3 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f 2a mutex *mutex; /*
13ec4 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d Non-recursive m
13ec5 75 74 65 78 20 72 65 71 75 69 72 65 64 20 74 6f utex required to
13ec6 20 61 63 63 65 73 73 20 74 68 69 73 20 73 74 72 access this str
13ec7 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 uct */. Bitvec
13ec8 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20 2f *pHasContent; /
13ec9 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 6d * Set of pages m
13eca 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69 73 oved to free-lis
13ecb 74 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 t this transacti
13ecc 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 on */.#ifndef SQ
13ecd 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
13ece 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 65 _CACHE. int nRe
13ecf 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f f; /
13ed0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 * Number of refe
13ed1 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 73 rences to this s
13ed2 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 tructure */. Bt
13ed3 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 20 Shared *pNext;
13ed4 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 61 /* Next on a
13ed5 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 6c list of sharabl
13ed6 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 e BtShared struc
13ed7 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a ts */. BtLock *
13ed8 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a pLock; /*
13ed9 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 68 List of locks h
13eda 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 72 eld on this shar
13edb 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 20 ed-btree struct
13edc 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72 69 */. Btree *pWri
13edd 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42 74 ter; /* Bt
13ede 72 65 65 20 77 69 74 68 20 63 75 72 72 65 6e 74 ree with current
13edf 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 ly open write tr
13ee0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 ansaction */. u
13ee1 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20 20 8 isExclusive;
13ee2 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
13ee3 70 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 45 pWriter has an E
13ee4 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
13ee5 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38 20 the db */. u8
13ee6 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 isPending;
13ee7 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e 67 /* If waiting
13ee8 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 20 for read-locks
13ee9 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e 64 to clear */.#end
13eea 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70 61 if. u8 *pTmpSpa
13eeb 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74 ce; /* Bt
13eec 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 20 Shared.pageSize
13eed 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 bytes of space f
13eee 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d 3b or tmp use */.};
13eef 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
13ef0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
13ef1 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 wing structure i
13ef2 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 s used to hold i
13ef3 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 nformation.** ab
13ef4 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 65 out a cell. The
13ef5 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 parseCellPtr()
13ef6 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 69 function fills i
13ef7 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
13ef8 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e 66 .** based on inf
13ef9 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 74 ormation extract
13efa 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 69 from the raw di
13efb 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 65 sk page..*/.type
13efc 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c 49 def struct CellI
13efd 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 74 nfo CellInfo;.st
13efe 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b 0a ruct CellInfo {.
13eff 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 u8 *pCell;
13f00 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 /* Pointer to t
13f01 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c 6c he start of cell
13f02 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 36 content */. i6
13f03 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 4 nKey; /*
13f04 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 4b The key for INTK
13f05 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e 75 EY tables, or nu
13f06 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
13f07 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e 44 key */. u32 nD
13f08 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ata; /* Numb
13f09 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 er of bytes of d
13f0a 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 ata */. u32 nPa
13f0b 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 6c yload; /* Total
13f0c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f amount of paylo
13f0d 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 61 ad */. u16 nHea
13f0e 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f der; /* Size o
13f0f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 f the cell conte
13f10 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 74 nt header in byt
13f11 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f 63 es */. u16 nLoc
13f12 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 al; /* Amount
13f13 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
13f14 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 31 locally */. u1
13f15 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 20 6 iOverflow; /*
13f16 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 6c Offset to overfl
13f17 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 ow page number.
13f18 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 72 Zero if no over
13f19 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e 53 flow */. u16 nS
13f1a 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 ize; /* Size
13f1b 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e of the cell con
13f1c 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e tent on the main
13f1d 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f 0a b-tree page */.
13f1e 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 };../*.** Maximu
13f1f 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 51 m depth of an SQ
13f20 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 75 Lite B-Tree stru
13f21 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 65 cture. Any B-Tre
13f22 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a 2a e deeper than.**
13f23 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64 65 this will be de
13f24 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e 20 clared corrupt.
13f25 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63 61 This value is ca
13f26 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20 6f lculated based o
13f27 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 64 n a.** maximum d
13f28 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66 20 atabase size of
13f29 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69 6e 2^31 pages a min
13f2a 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20 32 imum fanout of 2
13f2b 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d 6e for a.** root-n
13f2c 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61 6c ode and 3 for al
13f2d 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61 6c l other internal
13f2e 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 nodes..**.** If
13f2f 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70 70 a tree that app
13f30 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c 65 ears to be talle
13f31 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20 65 r than this is e
13f32 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20 69 ncountered, it i
13f33 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 s.** assumed tha
13f34 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 t the database i
13f35 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23 64 s corrupt..*/.#d
13f36 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f 4d efine BTCURSOR_M
13f37 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a 0a AX_DEPTH 20../*.
13f38 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 ** A cursor is a
13f39 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61 pointer to a pa
13f3a 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20 77 rticular entry w
13f3b 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75 6c ithin a particul
13f3c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69 74 ar.** b-tree wit
13f3d 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hin a database f
13f3e 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 ile..**.** The e
13f3f 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66 69 ntry is identifi
13f40 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61 67 ed by its MemPag
13f41 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 e and the index
13f42 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 43 in.** MemPage.aC
13f43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e 74 ell[] of the ent
13f44 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 ry..**.** When a
13f45 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
13f46 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 64 file can shared
13f47 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 74 by two more dat
13f48 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13f49 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f 72 s,.** but cursor
13f4a 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 72 s cannot be shar
13f4b 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f 72 ed. Each cursor
13f4c 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 is associated w
13f4d 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 ith a.** particu
13f4e 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e lar database con
13f4f 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 69 nection identifi
13f50 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 72 ed BtCursor.pBtr
13f51 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 ee.db..**.** Fie
13f52 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 lds in this stru
13f53 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 cture are access
13f54 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 ed under the BtS
13f55 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 66 hared.mutex.** f
13f56 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 42 ound at self->pB
13f57 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 74 t->mutex. .*/.st
13f58 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b 0a ruct BtCursor {.
13f59 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b Btree *pBtree;
13f5a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
13f5b 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 63 he Btree to whic
13f5c 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 h this cursor be
13f5d 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 61 longs */. BtSha
13f5e 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 red *pBt;
13f5f 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 68 /* The BtSh
13f60 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f 72 ared this cursor
13f61 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 20 points to */.
13f62 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 2c BtCursor *pNext,
13f63 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f 72 *pPrev; /* For
13f64 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 ms a linked list
13f65 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 of all cursors
13f66 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 */. struct KeyI
13f67 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 2f nfo *pKeyInfo; /
13f68 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 65 * Argument passe
13f69 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 d to comparison
13f6a 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 function */. Pg
13f6b 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20 no pgnoRoot;
13f6c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
13f6d 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 73 oot page of this
13f6e 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 74 tree */. sqlit
13f6f 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 52 e3_int64 cachedR
13f70 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 6f owid; /* Next ro
13f71 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d 65 wid cache. 0 me
13f72 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a 2f ans not valid */
13f73 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f . CellInfo info
13f74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
13f75 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 63 A parse of the c
13f76 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74 ell we are point
13f77 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 77 ing at */. u8 w
13f78 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 20 rFlag;
13f79 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
13f7a 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 75 writable */. u
13f7b 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 20 8 atLast;
13f7c 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 /* Curs
13f7d 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 or pointing to t
13f7e 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a 2f he last entry */
13f7f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 3b . u8 validNKey;
13f80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13f81 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65 True if info.nKe
13f82 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 y is valid */.
13f83 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 20 u8 eState;
13f84 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
13f85 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f 58 of the CURSOR_X
13f86 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 65 XX constants (se
13f87 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 6f e below) */. vo
13f88 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 2f id *pKey; /
13f89 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 74 * Saved key that
13f8a 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c 61 was cursor's la
13f8b 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 6f st known positio
13f8c 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b n */. i64 nKey;
13f8d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
13f8e 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 74 of pKey, or last
13f8f 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f 0a integer key */.
13f90 20 20 69 6e 74 20 73 6b 69 70 3b 20 20 20 20 20 int skip;
13f91 20 20 20 2f 2a 20 28 73 6b 69 70 3c 30 29 20 2d /* (skip<0) -
13f92 3e 20 50 72 65 76 28 29 20 69 73 20 61 20 6e 6f > Prev() is a no
13f93 2d 6f 70 2e 20 28 73 6b 69 70 3e 30 29 20 2d 3e -op. (skip>0) ->
13f94 20 4e 65 78 74 28 29 20 69 73 20 2a 2f 0a 23 69 Next() is */.#i
13f95 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
13f96 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75 38 20 T_INCRBLOB. u8
13f97 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 isIncrblobHandle
13f98 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 ; /* True i
13f99 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73 f this cursor is
13f9a 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68 61 6e an incr. io han
13f9b 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 61 dle */. Pgno *a
13f9c 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 Overflow;
13f9d 20 20 20 2f 2a 20 43 61 63 68 65 20 6f 66 20 6f /* Cache of o
13f9e 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 6f 63 verflow page loc
13f9f 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 ations */.#endif
13fa0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
13fa1 20 20 75 38 20 70 61 67 65 73 53 68 75 66 66 6c u8 pagesShuffl
13fa2 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ed; /* T
13fa3 72 75 65 20 69 66 20 42 74 72 65 65 20 70 61 67 rue if Btree pag
13fa4 65 73 20 61 72 65 20 72 65 61 72 72 61 6e 67 65 es are rearrange
13fa5 64 20 62 79 20 62 61 6c 61 6e 63 65 28 29 2a 2f d by balance()*/
13fa6 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20 69 50 .#endif. i16 iP
13fa7 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
13fa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fa9 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 /* Index of curr
13faa 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70 50 61 ent page in apPa
13fab 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ge */. MemPage
13fac 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 4f 52 *apPage[BTCURSOR
13fad 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a _MAX_DEPTH]; /*
13fae 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74 Pages from root
13faf 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 67 65 to current page
13fb0 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64 78 5b */. u16 aiIdx[
13fb1 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 BTCURSOR_MAX_DEP
13fb2 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 TH]; /* C
13fb3 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 urrent index in
13fb4 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a apPage[i] */.};.
13fb5 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c ./*.** Potential
13fb6 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75 values for BtCu
13fb7 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a rsor.eState..**.
13fb8 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a ** CURSOR_VALID:
13fb9 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69 .** Cursor poi
13fba 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65 nts to a valid e
13fbb 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 ntry. getPayload
13fbc 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63 () etc. may be c
13fbd 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 alled..**.** CUR
13fbe 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 SOR_INVALID:.**
13fbf 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f Cursor does no
13fc0 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c t point to a val
13fc1 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63 id entry. This c
13fc2 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65 an happen (for e
13fc3 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 xample) .** be
13fc4 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 cause the table
13fc5 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61 is empty or beca
13fc6 75 73 65 20 42 74 72 65 65 43 75 72 73 6f 72 46 use BtreeCursorF
13fc7 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62 irst() has not b
13fc8 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e een.** called.
13fc9 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 .**.** CURSOR_RE
13fca 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 QUIRESEEK:.**
13fcb 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 The table that t
13fcc 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f his cursor was o
13fcd 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 pened on still e
13fce 78 69 73 74 73 2c 20 62 75 74 20 68 61 73 20 62 xists, but has b
13fcf 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 een .** modifi
13fd0 65 64 20 73 69 6e 63 65 20 74 68 65 20 63 75 72 ed since the cur
13fd1 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75 73 65 sor was last use
13fd2 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f d. The cursor po
13fd3 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a sition is saved.
13fd4 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65 ** in variable
13fd5 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 s BtCursor.pKey
13fd6 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 and BtCursor.nKe
13fd7 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72 y. When a cursor
13fd8 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 is in .** thi
13fd9 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65 s state, restore
13fda 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
13fdb 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 can be called t
13fdc 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 o attempt to.**
13fdd 20 20 73 65 65 6b 20 74 68 65 20 63 75 72 73 6f seek the curso
13fde 72 20 74 6f 20 74 68 65 20 73 61 76 65 64 20 70 r to the saved p
13fdf 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 osition..**.** C
13fe0 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 URSOR_FAULT:.**
13fe1 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61 62 6c A unrecoverabl
13fe2 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20 e error (an I/O
13fe3 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f error or a mallo
13fe4 63 20 66 61 69 6c 75 72 65 29 20 68 61 73 20 6f c failure) has o
13fe5 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 ccurred.** on
13fe6 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e a different conn
13fe7 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72 ection that shar
13fe8 65 73 20 74 68 65 20 42 74 53 68 61 72 65 64 20 es the BtShared
13fe9 63 61 63 68 65 20 77 69 74 68 20 74 68 69 73 0a cache with this.
13fea 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20 54 68 ** cursor. Th
13feb 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65 66 74 e error has left
13fec 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 61 6e the cache in an
13fed 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 inconsistent st
13fee 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 ate..** Do not
13fef 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68 20 74 hing else with t
13ff0 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 his cursor. Any
13ff1 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
13ff2 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20 the cursor.**
13ff3 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 should return th
13ff4 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f e error code sto
13ff5 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e red in BtCursor.
13ff6 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 skip.*/.#define
13ff7 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20 CURSOR_INVALID
13ff8 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 0.#defi
13ff9 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 ne CURSOR_VALID
13ffa 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 1.#d
13ffb 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52 45 51 efine CURSOR_REQ
13ffc 55 49 52 45 53 45 45 4b 20 20 20 20 20 20 20 32 UIRESEEK 2
13ffd 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f .#define CURSOR_
13ffe 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 FAULT
13fff 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 3../* .** The
14000 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68 database page th
14001 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f e PENDING_BYTE o
14002 63 63 75 70 69 65 73 2e 20 54 68 69 73 20 70 61 ccupies. This pa
14003 67 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 ge is never used
14004 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 45 ..*/.# define PE
14005 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
14006 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f 50 47 pBt) PAGER_MJ_PG
14007 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 41 NO(pBt)../*.** A
14008 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
14009 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
1400a 72 75 63 74 75 72 65 73 20 69 73 20 73 74 6f 72 ructures is stor
1400b 65 64 20 61 74 20 42 74 53 68 61 72 65 64 2e 70 ed at BtShared.p
1400c 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61 Lock..** Locks a
1400d 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 70 67 re added (or upg
1400e 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 44 5f raded from READ_
1400f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f LOCK to WRITE_LO
14010 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 73 6f CK) when a curso
14011 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20 r .** is opened
14012 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 on the table wit
14013 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 53 68 h root page BtSh
14014 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63 ared.iTable. Loc
14015 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a ks are removed.*
14016 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 * from this list
14017 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 when a transact
14018 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
14019 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c or rolled back,
1401a 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74 or when.** a bt
1401b 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c ree handle is cl
1401c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 osed..*/.struct
1401d 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65 BtLock {. Btree
1401e 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 *pBtree;
1401f 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 6c 65 /* Btree handle
14020 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f holding this lo
14021 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61 ck */. Pgno iTa
14022 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ble; /*
14023 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 Root page of ta
14024 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63 ble */. u8 eLoc
14025 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k; /
14026 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 * READ_LOCK or W
14027 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42 RITE_LOCK */. B
14028 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20 tLock *pNext;
14029 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 /* Next in
1402a 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c BtShared.pLock l
1402b 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 ist */.};../* Ca
1402c 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20 66 ndidate values f
1402d 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 or BtLock.eLock
1402e 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f */.#define READ_
1402f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69 LOCK 1.#defi
14030 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20 ne WRITE_LOCK
14031 20 32 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 2../*.** These
14032 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68 macros define th
14033 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 e location of th
14034 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e e pointer-map en
14035 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64 61 try for a .** da
14036 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 65 tabase page. The
14037 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
14038 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20 6e to each is the n
14039 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 0a umber of usable.
1403a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 ** bytes on each
1403b 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 page of the dat
1403c 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30 32 abase (often 102
1403d 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 4). The second i
1403e 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 s the.** page nu
1403f 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 mber to look up
14040 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d in the pointer m
14041 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 ap..**.** PTRMAP
14042 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73 20 _PAGENO returns
14043 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 the database pag
14044 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
14045 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 70 pointer-map.** p
14046 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73 20 age that stores
14047 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f 69 the required poi
14048 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54 52 nter. PTRMAP_PTR
14049 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a 2a OFFSET returns.*
1404a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 * the offset of
1404b 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d 61 the requested ma
1404c 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 p entry..**.** I
1404d 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75 6d f the pgno argum
1404e 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50 54 ent passed to PT
1404f 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20 61 RMAP_PAGENO is a
14050 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
14051 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f 20 e,.** then pgno
14052 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f 20 is returned. So
14053 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 (pgno==PTRMAP_PA
14054 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f 29 GENO(pgsz, pgno)
14055 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 ) can be.** used
14056 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e 6f to test if pgno
14057 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 is a pointer-ma
14058 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f 49 p page. PTRMAP_I
14059 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74 73 SPAGE implements
1405a 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a 2a .** this test..*
1405b 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 /.#define PTRMAP
1405c 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 6e _PAGENO(pBt, pgn
1405d 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 o) ptrmapPageno(
1405e 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66 69 pBt, pgno).#defi
1405f 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 ne PTRMAP_PTROFF
14060 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70 67 SET(pgptrmap, pg
14061 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67 70 no) (5*(pgno-pgp
14062 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69 6e trmap-1)).#defin
14063 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 e PTRMAP_ISPAGE(
14064 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d pBt, pgno) (PTRM
14065 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 2c AP_PAGENO((pBt),
14066 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29 (pgno))==(pgno))
14067 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e ../*.** The poin
14068 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f ter map is a loo
14069 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 69 kup table that i
1406a 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 dentifies the pa
1406b 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a rent page for.**
1406c 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 65 each child page
1406d 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1406e 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 65 file. The pare
1406f 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20 70 nt page is the p
14070 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74 age that.** cont
14071 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ains a pointer t
14072 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 76 o the child. Ev
14073 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
14074 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
14075 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 65 s.** 0 or 1 pare
14076 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 74 nt pages. (In t
14077 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 74 his context 'dat
14078 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66 65 abase page' refe
14079 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67 rs.** to any pag
1407a 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 e that is not pa
1407b 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 rt of the pointe
1407c 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 20 r map itself.)
1407d 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 70 Each pointer map
1407e 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 73 .** entry consis
1407f 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 62 ts of a single b
14080 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 61 yte 'type' and a
14081 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 70 4 byte parent p
14082 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54 age number..** T
14083 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 64 he PTRMAP_XXX id
14084 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 20 entifiers below
14085 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 79 are the valid ty
14086 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 pes..**.** The p
14087 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 6f urpose of the po
14088 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f 20 inter map is to
14089 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20 facility moving
1408a 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a pages from one.*
1408b 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 * position in th
1408c 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65 e file to anothe
1408d 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 74 r as part of aut
1408e 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 61 ovacuum. When a
1408f 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65 page.** is move
14090 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 d, the pointer i
14091 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 73 n its parent mus
14092 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f 20 t be updated to
14093 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 point to the.**
14094 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 new location. T
14095 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
14096 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 s used to locate
14097 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
14098 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 quickly..**.**
14099 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a PTRMAP_ROOTPAGE:
1409a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
1409b 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 ge is a root-pag
1409c 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 e. The page-numb
1409d 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 er is not.**
1409e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 us
1409f 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ed in this case.
140a0 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52 .**.** PTRMAP_FR
140a1 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 EEPAGE: The data
140a2 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e 20 base page is an
140a3 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 61 unused (free) pa
140a4 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d ge. The page-num
140a5 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ber .**
140a6 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 20 is not
140a7 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 used in this cas
140a8 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f e..**.** PTRMAP_
140a9 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 64 OVERFLOW1: The d
140aa 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 atabase page is
140ab 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 the first page i
140ac 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20 n a list of .**
140ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140ae 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 overflow pages
140af 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 . The page numbe
140b0 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 r identifies the
140b1 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 20 page that.**
140b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140b3 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c contains the cel
140b4 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 l with a pointer
140b5 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f to this overflo
140b6 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 w page..**.** PT
140b7 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 RMAP_OVERFLOW2:
140b8 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 The database pag
140b9 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 e is the second
140ba 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 6e or later page in
140bb 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 a list of.**
140bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140bd 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
140be 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 The page-number
140bf 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 identifies the p
140c0 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20 revious.**
140c1 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
140c2 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f e in the overflo
140c3 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a w page list..**.
140c4 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a ** PTRMAP_BTREE:
140c5 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
140c6 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74 ge is a non-root
140c7 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 65 btree page. The
140c8 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 page number.**
140c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 64 id
140ca 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72 entifies the par
140cb 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ent page in the
140cc 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e btree..*/.#defin
140cd 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 e PTRMAP_ROOTPAG
140ce 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 4d E 1.#define PTRM
140cf 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 64 AP_FREEPAGE 2.#d
140d0 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 efine PTRMAP_OVE
140d1 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e 65 RFLOW1 3.#define
140d2 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
140d3 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 2 4.#define PTRM
140d4 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41 AP_BTREE 5../* A
140d5 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 74 bunch of assert
140d6 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f () statements to
140d7 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e 73 check the trans
140d8 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 72 action state var
140d9 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e iables.** of han
140da 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 65 dle p (type Btre
140db 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 6c e*) are internal
140dc 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a ly consistent..*
140dd 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 49 /.#define btreeI
140de 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 20 ntegrity(p) \.
140df 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e assert( p->pBt->
140e0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 inTransaction!=T
140e1 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e RANS_NONE || p->
140e2 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f pBt->nTransactio
140e3 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 65 n==0 ); \. asse
140e4 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 rt( p->pBt->inTr
140e5 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e ansaction>=p->in
140e6 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a Trans ); .../*.*
140e7 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 55 * The ISAUTOVACU
140e8 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 UM macro is used
140e9 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f within balance_
140ea 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 74 nonroot() to det
140eb 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65 ermine.** if the
140ec 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 database suppor
140ed 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f ts auto-vacuum o
140ee 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 69 r not. Because i
140ef 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 74 t is used.** wit
140f0 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f hin an expressio
140f1 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 67 n that is an arg
140f2 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72 ument to another
140f3 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69 macro .** (sqli
140f4 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74 teMallocRaw), it
140f5 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
140f6 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 6f to use conditio
140f7 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e nal compilation.
140f8 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 63 .** So, this mac
140f9 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e ro is defined in
140fa 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 stead..*/.#ifnde
140fb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
140fc 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 TOVACUUM.#define
140fd 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 70 ISAUTOVACUUM (p
140fe 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a Bt->autoVacuum).
140ff 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 53 #else.#define IS
14100 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e AUTOVACUUM 0.#en
14101 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 dif.../*.** This
14102 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 structure is pa
14103 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f ssed around thro
14104 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69 ugh all the sani
14105 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74 ty checking rout
14106 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 ines.** in order
14107 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f to keep track o
14108 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74 f some global st
14109 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e ate information.
1410a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
1410b 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49 ct IntegrityCk I
1410c 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75 ntegrityCk;.stru
1410d 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b ct IntegrityCk {
1410e 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1410f 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65 ; /* The tree
14110 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f being checked o
14111 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 ut */. Pager *p
14112 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65 Pager; /* The
14113 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65 associated page
14114 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69 r. Also accessi
14115 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67 ble by pBt->pPag
14116 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 er */. Pgno nPa
14117 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ge; /* Num
14118 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
14119 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
1411a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 20 int *anRef;
1411b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1411c 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 65 times each page
1411d 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a is referenced *
1411e 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 20 /. int mxErr;
1411f 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 63 /* Stop ac
14120 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f 72 cumulating error
14121 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 63 s when this reac
14122 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e hes zero */. in
14123 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
14124 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 73 /* Number of mes
14125 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f sages written to
14126 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72 20 zErrMsg so far
14127 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 46 */. int mallocF
14128 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d 6f ailed; /* A memo
14129 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
1412a 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 ror has occurred
1412b 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20 65 */. StrAccum e
1412c 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75 6d rrMsg; /* Accum
1412d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72 20 ulate the error
1412e 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65 72 message text her
1412f 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 e */.};../*.** R
14130 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 74 ead or write a t
14131 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79 74 wo- and four-byt
14132 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
14133 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a eger values..*/.
14134 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 #define get2byte
14135 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c 38 (x) ((x)[0]<<8
14136 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66 69 | (x)[1]).#defi
14137 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 29 ne put2byte(p,v)
14138 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 28 ((p)[0] = (u8)(
14139 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d 20 (v)>>8), (p)[1]
1413a 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66 69 = (u8)(v)).#defi
1413b 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c 69 ne get4byte sqli
1413c 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 66 te3Get4byte.#def
1413d 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 6c ine put4byte sql
1413e 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f 2a ite3Put4byte../*
1413f 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 72 6f 75 .** Internal rou
14140 74 69 6e 65 73 20 74 68 61 74 20 73 68 6f 75 6c tines that shoul
14141 64 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 d be accessed by
14142 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 the btree layer
14143 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 only..*/.SQLITE
14144 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
14145 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
14146 28 42 74 53 68 61 72 65 64 2a 2c 20 50 67 6e 6f (BtShared*, Pgno
14147 2c 20 4d 65 6d 50 61 67 65 2a 2a 2c 20 69 6e 74 , MemPage**, int
14148 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
14149 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1414a 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d 50 61 eeInitPage(MemPa
1414b 67 65 20 2a 70 50 61 67 65 29 3b 0a 53 51 4c 49 ge *pPage);.SQLI
1414c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1414d 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 sqlite3BtreePars
1414e 65 43 65 6c 6c 50 74 72 28 4d 65 6d 50 61 67 65 eCellPtr(MemPage
1414f 2a 2c 20 75 38 2a 2c 20 43 65 6c 6c 49 6e 66 6f *, u8*, CellInfo
14150 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
14151 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14152 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 4d 65 treeParseCell(Me
14153 6d 50 61 67 65 2a 2c 20 69 6e 74 2c 20 43 65 6c mPage*, int, Cel
14154 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f lInfo*);.SQLITE_
14155 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14156 74 65 33 42 74 72 65 65 52 65 73 74 6f 72 65 43 te3BtreeRestoreC
14157 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 ursorPosition(Bt
14158 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 53 Cursor *pCur);.S
14159 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1415a 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 id sqlite3BtreeG
1415b 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 etTempCursor(BtC
1415c 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 ursor *pCur, BtC
1415d 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 ursor *pTempCur)
1415e 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
1415f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
14160 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 eeReleaseTempCur
14161 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 sor(BtCursor *pC
14162 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ur);.SQLITE_PRIV
14163 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14164 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e BtreeMoveToParen
14165 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 t(BtCursor *pCur
14166 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a );../***********
14167 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 *** End of btree
14168 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
14169 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1416a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1416b 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1416c 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
1416d 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
1416e 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a in btmutex.c **
1416f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14170 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 **/.#ifndef SQLI
14171 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
14172 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54 45 5f ACHE.#if SQLITE_
14173 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a 0a 2a THREADSAFE../*.*
14174 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42 74 53 * Obtain the BtS
14175 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f hared mutex asso
14176 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 ciated with B-Tr
14177 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 6c 73 ee handle p. Als
14178 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 61 72 o,.** set BtShar
14179 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64 61 74 ed.db to the dat
1417a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61 73 73 abase handle ass
1417b 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 20 61 ociated with p a
1417c 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 nd the.** p->loc
1417d 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f 20 74 ked boolean to t
1417e 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 rue..*/.static v
1417f 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 oid lockBtreeMut
14180 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ex(Btree *p){.
14181 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 assert( p->locke
14182 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 d==0 );. assert
14183 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
14184 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e notheld(p->pBt->
14185 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
14186 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14187 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14188 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 tex) );.. sqlit
14189 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
1418a 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 ->pBt->mutex);.
1418b 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d p->pBt->db = p-
1418c 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 >db;. p->locked
1418d 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 = 1;.}../*.** R
1418e 65 6c 65 61 73 65 20 74 68 65 20 42 74 53 68 61 elease the BtSha
1418f 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 69 red mutex associ
14190 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 65 ated with B-Tree
14191 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a 2a 2a handle p and.**
14192 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e 6c 6f clear the p->lo
14193 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f cked boolean..*/
14194 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c .static void unl
14195 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 ockBtreeMutex(Bt
14196 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
14197 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20 t( p->locked==1
14198 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
14199 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1419a 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 p->pBt->mutex) )
1419b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1419c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1419d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
1419e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 3d assert( p->db=
1419f 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 0a =p->pBt->db );..
141a0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
141a1 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 leave(p->pBt->mu
141a2 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 tex);. p->locke
141a3 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 d = 0;.}../*.**
141a4 45 6e 74 65 72 20 61 20 6d 75 74 65 78 20 6f 6e Enter a mutex on
141a5 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 65 65 the given BTree
141a6 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 object..**.** I
141a7 66 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 f the object is
141a8 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c 20 74 68 not sharable, th
141a9 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 65 en no mutex is e
141aa 76 65 72 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 ver required.**
141ab 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 and this routine
141ac 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 is a no-op. Th
141ad 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74 e underlying mut
141ae 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73 ex is non-recurs
141af 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b ive..** But we k
141b0 65 65 70 20 61 20 72 65 66 65 72 65 6e 63 65 20 eep a reference
141b1 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 65 2e 77 count in Btree.w
141b2 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65 antToLock so the
141b3 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 behavior.** of
141b4 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 this interface i
141b5 73 20 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a s recursive..**.
141b6 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 ** To avoid dead
141b7 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20 locks, multiple
141b8 42 74 72 65 65 73 20 61 72 65 20 6c 6f 63 6b 65 Btrees are locke
141b9 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 d in the same or
141ba 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61 der.** by all da
141bb 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
141bc 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e 65 78 ns. The p->pNex
141bd 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6f t is a list of o
141be 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 73 20 62 ther.** Btrees b
141bf 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 elonging to the
141c0 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
141c1 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 65 20 nnection as the
141c2 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 69 63 68 p Btree.** which
141c3 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b need to be lock
141c4 65 64 20 61 66 74 65 72 20 70 2e 20 20 49 66 20 ed after p. If
141c5 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 20 61 20 we cannot get a
141c6 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68 lock on.** p, th
141c7 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 en first unlock
141c8 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 72 all of the other
141c9 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 s on p->pNext, t
141ca 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20 hen wait.** for
141cb 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f the lock to beco
141cc 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 me available on
141cd 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 p, then relock a
141ce 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 ll of the.** sub
141cf 73 65 71 75 65 6e 74 20 42 74 72 65 65 73 20 74 sequent Btrees t
141d0 68 61 74 20 64 65 73 69 72 65 20 61 20 6c 6f 63 hat desire a loc
141d1 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 k..*/.SQLITE_PRI
141d2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
141d3 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65 3BtreeEnter(Btre
141d4 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 65 20 2a e *p){. Btree *
141d5 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f pLater;.. /* So
141d6 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 me basic sanity
141d7 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 checking on the
141d8 42 74 72 65 65 2e 20 20 54 68 65 20 6c 69 73 74 Btree. The list
141d9 20 6f 66 20 42 74 72 65 65 73 0a 20 20 2a 2a 20 of Btrees. **
141da 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 4e 65 connected by pNe
141db 78 74 20 61 6e 64 20 70 50 72 65 76 20 73 68 6f xt and pPrev sho
141dc 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 74 65 64 uld be in sorted
141dd 20 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a 20 42 order by. ** B
141de 74 72 65 65 2e 70 42 74 20 76 61 6c 75 65 2e 20 tree.pBt value.
141df 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 All elements of
141e0 74 68 65 20 6c 69 73 74 20 73 68 6f 75 6c 64 20 the list should
141e1 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 belong to. ** t
141e2 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 he same connecti
141e3 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65 64 20 on. Only shared
141e4 42 74 72 65 65 73 20 61 72 65 20 6f 6e 20 74 68 Btrees are on th
141e5 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73 e list. */. ass
141e6 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 ert( p->pNext==0
141e7 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42 || p->pNext->pB
141e8 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 t>p->pBt );. as
141e9 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d sert( p->pPrev==
141ea 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 0 || p->pPrev->p
141eb 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 Bt<p->pBt );. a
141ec 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d ssert( p->pNext=
141ed 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e =0 || p->pNext->
141ee 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 db==p->db );. a
141ef 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d ssert( p->pPrev=
141f0 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e =0 || p->pPrev->
141f1 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 db==p->db );. a
141f2 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
141f3 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d le || (p->pNext=
141f4 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d =0 && p->pPrev==
141f5 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 0) );.. /* Chec
141f6 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f k for locking co
141f7 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61 nsistency */. a
141f8 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 ssert( !p->locke
141f9 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f d || p->wantToLo
141fa 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 ck>0 );. assert
141fb 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c ( p->sharable ||
141fc 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d p->wantToLock==
141fd 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68 0 );.. /* We sh
141fe 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c ould already hol
141ff 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 d a lock on the
14200 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14201 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ion */. assert(
14202 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14203 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
14204 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 ) );.. /* Unles
14205 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 s the database i
14206 73 20 73 68 61 72 61 62 6c 65 20 61 6e 64 20 75 s sharable and u
14207 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 42 74 nlocked, then Bt
14208 53 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a 20 73 Shared.db. ** s
14209 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 hould already be
1420a 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 set correctly.
1420b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d */. assert( (p-
1420c 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d >locked==0 && p-
1420d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d >sharable) || p-
1420e 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 >pBt->db==p->db
1420f 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 );.. if( !p->sh
14210 61 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b arable ) return;
14211 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b . p->wantToLock
14212 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 ++;. if( p->loc
14213 6b 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 ked ) return;..
14214 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 /* In most case
14215 73 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20 s, we should be
14216 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 able to acquire
14217 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a the lock we. **
14218 20 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 want without ha
14219 76 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 ving to go throu
1421a 67 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e ght the ascendin
1421b 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 g lock. ** proc
1421c 65 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f edure that follo
1421d 77 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 ws. Just be sur
1421e 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a e not to block..
1421f 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
14220 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e e3_mutex_try(p->
14221 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c pBt->mutex)==SQL
14222 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d ITE_OK ){. p-
14223 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 >pBt->db = p->db
14224 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 ;. p->locked
14225 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b = 1;. return;
14226 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 . }.. /* To av
14227 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 oid deadlock, fi
14228 72 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 rst release all
14229 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61 72 locks with a lar
1422a 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 ger. ** BtShare
1422b 64 20 61 64 64 72 65 73 73 2e 20 20 54 68 65 6e d address. Then
1422c 20 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 acquire our loc
1422d 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75 69 k. Then reacqui
1422e 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 re. ** the othe
1422f 72 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 r BtShared locks
14230 20 74 68 61 74 20 77 65 20 75 73 65 64 20 74 6f that we used to
14231 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 hold in ascendi
14232 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 ng. ** order..
14233 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 */. for(pLater
14234 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 =p->pNext; pLate
14235 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 r; pLater=pLater
14236 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 ->pNext){. as
14237 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 sert( pLater->sh
14238 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 arable );. as
14239 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e sert( pLater->pN
1423a 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 ext==0 || pLater
1423b 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 ->pNext->pBt>pLa
1423c 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 ter->pBt );.
1423d 61 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 2d assert( !pLater-
1423e 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 >locked || pLate
1423f 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 r->wantToLock>0
14240 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 );. if( pLate
14241 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 r->locked ){.
14242 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 unlockBtreeMu
14243 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 tex(pLater);.
14244 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42 74 72 }. }. lockBtr
14245 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 66 6f eeMutex(p);. fo
14246 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 r(pLater=p->pNex
14247 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 t; pLater; pLate
14248 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 r=pLater->pNext)
14249 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 {. if( pLater
1424a 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a ->wantToLock ){.
1424b 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65 4d lockBtreeM
1424c 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 utex(pLater);.
1424d 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1424e 20 45 78 69 74 20 74 68 65 20 72 65 63 75 72 73 Exit the recurs
1424f 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 ive mutex on a B
14250 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tree..*/.SQLITE_
14251 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
14252 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 ite3BtreeLeave(B
14253 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 tree *p){. if(
14254 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
14255 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 assert( p->wa
14256 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 ntToLock>0 );.
14257 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d p->wantToLock-
14258 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61 -;. if( p->wa
14259 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 ntToLock==0 ){.
1425a 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 unlockBtree
1425b 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a Mutex(p);. }.
1425c 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e }.}..#ifndef N
1425d 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 DEBUG./*.** Retu
1425e 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 42 rn true if the B
1425f 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 69 73 tShared mutex is
14260 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 74 72 held on the btr
14261 65 65 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a ee, or if the.**
14262 20 42 2d 54 72 65 65 20 69 73 20 6e 6f 74 20 6d B-Tree is not m
14263 61 72 6b 65 64 20 61 73 20 73 68 61 72 61 62 6c arked as sharabl
14264 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
14265 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e utine is used on
14266 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 ly from within a
14267 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
14268 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ts..*/.SQLITE_PR
14269 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1426a 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
1426b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 (Btree *p){. as
1426c 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
1426d 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 e==0 || p->locke
1426e 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 d==0 || p->wantT
1426f 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 oLock>0 );. ass
14270 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
14271 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 ==0 || p->locked
14272 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70 2d ==0 || p->db==p-
14273 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 >pBt->db );. as
14274 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
14275 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 e==0 || p->locke
14276 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f d==0 || sqlite3_
14277 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 mutex_held(p->pB
14278 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
14279 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
1427a 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b le==0 || p->lock
1427b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 ed==0 || sqlite3
1427c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
1427d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 b->mutex) );..
1427e 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 return (p->shara
1427f 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 ble==0 || p->loc
14280 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ked);.}.#endif..
14281 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14282 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a OMIT_INCRBLOB./*
14283 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 6c 65 .** Enter and le
14284 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e 20 61 ave a mutex on a
14285 20 42 74 72 65 65 20 67 69 76 65 6e 20 61 20 63 Btree given a c
14286 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 20 74 ursor owned by t
14287 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 20 54 hat.** Btree. T
14288 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 hese entry point
14289 73 20 61 72 65 20 75 73 65 64 20 62 79 20 69 6e s are used by in
1428a 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 6e cremental I/O an
1428b 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74 d can be.** omit
1428c 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f 64 75 ted if that modu
1428d 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a le is not used..
1428e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1428f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14290 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 reeEnterCursor(B
14291 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
14292 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
14293 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72 65 65 ter(pCur->pBtree
14294 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
14295 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14296 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 BtreeLeaveCursor
14297 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
14298 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 {. sqlite3Btree
14299 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42 74 72 Leave(pCur->pBtr
1429a 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a ee);.}.#endif /*
1429b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
1429c 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a RBLOB */.../*.**
1429d 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 Enter the mutex
1429e 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65 65 20 on every Btree
1429f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
142a0 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f a database.** co
142a1 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 nnection. This
142a2 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72 20 65 is needed (for e
142a3 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 74 6f xample) prior to
142a4 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 73 74 parsing.** a st
142a5 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 77 65 atement since we
142a6 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 72 69 will be compari
142a7 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c ng table and col
142a8 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 67 61 umn names.** aga
142a9 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d 61 73 inst all schemas
142aa 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 and we do not w
142ab 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65 6d 61 ant those schema
142ac 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 65 74 s being.** reset
142ad 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 out from under
142ae 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 us..**.** There
142af 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 is a correspondi
142b0 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 72 6f ng leave-all pro
142b1 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 cedures..**.** E
142b2 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73 nter the mutexes
142b3 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 in accending or
142b4 64 65 72 20 62 79 20 42 74 53 68 61 72 65 64 20 der by BtShared
142b5 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73 73 0a pointer address.
142b6 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20 ** to avoid the
142b7 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 64 possibility of d
142b8 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 77 6f eadlock when two
142b9 20 74 68 72 65 61 64 73 20 77 69 74 68 0a 2a 2a threads with.**
142ba 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 74 72 two or more btr
142bb 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f ees in common bo
142bc 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b 20 61 th try to lock a
142bd 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65 73 0a ll their btrees.
142be 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20 69 ** at the same i
142bf 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 nstant..*/.SQLIT
142c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
142c1 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
142c2 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 All(sqlite3 *db)
142c3 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 {. int i;. Btr
142c4 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 3b 0a ee *p, *pLater;.
142c5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
142c6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3_mutex_held(db-
142c7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 >mutex) );. for
142c8 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
142c9 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 i++){. p = d
142ca 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
142cb 20 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c assert( !p ||
142cc 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 (p->locked==0 &
142cd 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c & p->sharable) |
142ce 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d | p->pBt->db==p-
142cf 3e 64 62 20 29 3b 0a 20 20 20 20 69 66 28 20 70 >db );. if( p
142d0 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 && p->sharable
142d1 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 ){. p->want
142d2 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 ToLock++;.
142d3 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 if( !p->locked )
142d4 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
142d5 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
142d6 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 =1 );. wh
142d7 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29 20 ile( p->pPrev )
142d8 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 p = p->pPrev;.
142d9 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e while( p->
142da 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 70 4e 65 locked && p->pNe
142db 78 74 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 xt ) p = p->pNex
142dc 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 t;. for(p
142dd 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 Later = p->pNext
142de 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 ; pLater; pLater
142df 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b =pLater->pNext){
142e0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
142e1 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b Later->locked ){
142e2 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c . unl
142e3 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c ockBtreeMutex(pL
142e4 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 ater);.
142e5 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
142e6 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b while( p ){
142e7 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 . lockB
142e8 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
142e9 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 p = p->p
142ea 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a Next;. }.
142eb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
142ec 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 }.}.SQLITE_PRIVA
142ed 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
142ee 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c treeLeaveAll(sql
142ef 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 ite3 *db){. int
142f0 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a i;. Btree *p;.
142f1 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
142f2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3_mutex_held(db-
142f3 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 >mutex) );. for
142f4 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
142f5 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 i++){. p = d
142f6 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
142f7 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 if( p && p->s
142f8 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 harable ){.
142f9 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 assert( p->want
142fa 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 ToLock>0 );.
142fb 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d p->wantToLock-
142fc 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e -;. if( p->
142fd 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b wantToLock==0 ){
142fe 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 . unlockB
142ff 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14300 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
14301 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }..#ifndef NDEBU
14302 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 G./*.** Return t
14303 72 75 65 20 69 66 20 74 68 65 20 63 75 72 72 65 rue if the curre
14304 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 nt thread holds
14305 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
14306 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 nection.** mutex
14307 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 and all require
14308 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 d BtShared mutex
14309 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 es..**.** This r
1430a 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 outine is used i
1430b 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 nside assert() s
1430c 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a tatements only..
1430d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1430e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1430f 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
14310 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a s(sqlite3 *db){.
14311 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 int i;. if( !
14312 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14313 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b ld(db->mutex) ){
14314 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
14315 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
14316 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
14317 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 Btree *p;.
14318 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e p = db->aDb[i].
14319 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 pBt;. if( p &
1431a 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 & p->sharable &&
1431b 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 . (p->wa
1431c 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 ntToLock==0 || !
1431d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1431e 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 ld(p->pBt->mutex
1431f 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 )) ){. retu
14320 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 0;. }. }.
14321 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
14322 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a ndif /* NDEBUG *
14323 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e /../*.** Add a n
14324 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 ew Btree pointer
14325 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 to a BtreeMutex
14326 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 Array. .** if th
14327 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f e pointer can po
14328 73 73 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 ssibly be shared
14329 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 with.** another
1432a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1432b 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
1432c 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 pointers are kep
1432d 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 t in sorted orde
1432e 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 r by pBtree->pBt
1432f 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 . That.** way w
14330 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 hen we go to ent
14331 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 er all the mutex
14332 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 es, we can enter
14333 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 them.** in orde
14334 72 20 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 r without every
14335 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 having to backup
14336 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 and retry and w
14337 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 ithout.** worryi
14338 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 ng about deadloc
14339 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d k..**.** The num
1433a 62 65 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 ber of shared bt
1433b 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 rees will always
1433c 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c be small (usual
1433d 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f ly 0 or 1).** so
1433e 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f an insertion so
1433f 72 74 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 rt is an adequat
14340 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 e algorithm here
14341 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14342 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14343 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 BtreeMutexArrayI
14344 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 nsert(BtreeMutex
14345 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 Array *pArray, B
14346 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 tree *pBtree){.
14347 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 int i, j;. BtS
14348 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 hared *pBt;. if
14349 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 ( pBtree==0 || p
1434a 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d Btree->sharable=
1434b 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 =0 ) return;.#if
1434c 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a ndef NDEBUG. {.
1434d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1434e 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 Array->nMutex; i
1434f 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
14350 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 t( pArray->aBtre
14351 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a e[i]!=pBtree );.
14352 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
14353 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 . assert( pArra
14354 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a y->nMutex>=0 );.
14355 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 assert( pArray
14356 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 ->nMutex<ArraySi
14357 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 ze(pArray->aBtre
14358 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 e)-1 );. pBt =
14359 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 pBtree->pBt;. f
1435a 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 or(i=0; i<pArray
1435b 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a ->nMutex; i++){.
1435c 20 20 20 20 61 73 73 65 72 74 28 20 70 41 72 72 assert( pArr
1435d 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 ay->aBtree[i]!=p
1435e 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 Btree );. if(
1435f 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14360 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 i]->pBt>pBt ){.
14361 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 for(j=pArra
14362 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 y->nMutex; j>i;
14363 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 j--){. pA
14364 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 rray->aBtree[j]
14365 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 = pArray->aBtree
14366 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 [j-1];. }.
14367 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 pArray->aBt
14368 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b ree[i] = pBtree;
14369 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e . pArray->n
1436a 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 Mutex++;. r
1436b 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d eturn;. }. }
1436c 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 . pArray->aBtre
1436d 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 e[pArray->nMutex
1436e 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a ++] = pBtree;.}.
1436f 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 ./*.** Enter the
14370 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 mutex of every
14371 62 74 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 btree in the arr
14372 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ay. This routin
14373 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 e is.** called a
14374 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
14375 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 of sqlite3VdbeEx
14376 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 ec(). The mutex
14377 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 es are.** exited
14378 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
14379 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e he same function
1437a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1437b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1437c 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 BtreeMutexArrayE
1437d 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 nter(BtreeMutexA
1437e 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 rray *pArray){.
1437f 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
14380 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 0; i<pArray->nMu
14381 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 tex; i++){. B
14382 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 tree *p = pArray
14383 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 ->aBtree[i];.
14384 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 /* Some basic s
14385 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
14386 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d /. assert( i=
14387 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 =0 || pArray->aB
14388 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 tree[i-1]->pBt<p
14389 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
1438a 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 ert( !p->locked
1438b 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b || p->wantToLock
1438c 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 >0 );.. /* We
1438d 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
1438e 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 hold a lock on t
1438f 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
14390 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 ection */. as
14391 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14392 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
14393 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 mutex) );.. p
14394 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a ->wantToLock++;.
14395 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b if( !p->lock
14396 65 64 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c ed && p->sharabl
14397 65 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 e ){. lockB
14398 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14399 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1439a 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 Leave the mutex
1439b 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20 of every btree
1439c 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f in the group..*/
1439d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1439e 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
1439f 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
143a0 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 (BtreeMutexArray
143a1 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 *pArray){. int
143a2 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 i;. for(i=0; i
143a3 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b <pArray->nMutex;
143a4 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 i++){. Btree
143a5 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 *p = pArray->aB
143a6 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 tree[i];. /*
143a7 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 Some basic sanit
143a8 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 y checking */.
143a9 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c assert( i==0 |
143aa 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 | pArray->aBtree
143ab 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 [i-1]->pBt<p->pB
143ac 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
143ad 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 21 70 p->locked || !p
143ae 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 ->sharable );.
143af 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
143b0 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 tToLock>0 );..
143b1 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 /* We should a
143b2 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f lready hold a lo
143b3 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
143b4 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
143b5 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
143b6 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
143b7 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
143b8 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c .. p->wantToL
143b9 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 ock--;. if( p
143ba 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
143bb 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a && p->locked ){.
143bc 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 unlockBtre
143bd 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d eMutex(p);. }
143be 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53 51 . }.}..#else.SQ
143bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
143c0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
143c1 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ter(Btree *p){.
143c2 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d p->pBt->db = p-
143c3 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 >db;.}.SQLITE_PR
143c4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
143c5 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
143c6 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
143c7 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
143c8 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
143c9 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 ){. Btree *p
143ca 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
143cb 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 ;. if( p ){.
143cc 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 p->pBt->db
143cd 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a 20 = p->db;. }.
143ce 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 }.}.#endif /* i
143cf 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
143d0 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a AFE */.#endif /*
143d1 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f ifndef SQLITE_O
143d2 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
143d3 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
143d4 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d 75 **** End of btmu
143d5 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tex.c **********
143d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143d8 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
143d9 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
143da 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a btree.c ********
143db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143dd 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
143de 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 April 6.**.** Th
143df 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
143e0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
143e1 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
143e2 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
143e3 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
143e4 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
143e5 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
143e6 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
143e7 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
143e8 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
143e9 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
143ea 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
143eb 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
143ec 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
143ed 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
143ee 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
143ef 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
143f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
143f4 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 ******.** $Id: b
143f5 74 72 65 65 2e 63 2c 76 20 31 2e 36 30 38 20 32 tree.c,v 1.608 2
143f6 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 35 37 3a 009/05/06 18:57:
143f7 31 30 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 10 shane Exp $.*
143f8 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 *.** This file i
143f9 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 mplements a exte
143fa 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 rnal (disk-based
143fb 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 ) database using
143fc 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 20 BTrees..** See
143fd 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 the header comme
143fe 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 2e nt on "btreeInt.
143ff 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 h" for additiona
14400 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
14401 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 65 * Including a de
14402 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 6c scription of fil
14403 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e 20 e format and an
14404 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 72 overview of oper
14405 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ation..*/../*.**
14406 20 54 68 65 20 68 65 61 64 65 72 20 73 74 72 69 The header stri
14407 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ng that appears
14408 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
14409 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c of every.** SQL
1440a 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f ite database..*/
1440b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 .static const ch
1440c 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72 5b ar zMagicHeader[
1440d 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f ] = SQLITE_FILE_
1440e 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 53 HEADER;../*.** S
1440f 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 et this global v
14410 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f 20 ariable to 1 to
14411 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 75 enable tracing u
14412 73 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a 2a sing the TRACE.*
14413 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 * macro..*/.#if
14414 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 0.int sqlite3Btr
14415 65 65 54 72 61 63 65 3d 30 3b 20 20 2f 2a 20 54 eeTrace=0; /* T
14416 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 rue to enable tr
14417 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69 6e acing */.# defin
14418 65 20 54 52 41 43 45 28 58 29 20 20 69 66 28 73 e TRACE(X) if(s
14419 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 qlite3BtreeTrace
1441a 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75 73 ){printf X;fflus
1441b 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c 73 h(stdout);}.#els
1441c 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 e.# define TRACE
1441d 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 (X).#endif....#i
1441e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1441f 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
14420 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 42 *.** A list of B
14421 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 20 tShared objects
14422 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 6c that are eligibl
14423 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 74 e for participat
14424 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 64 ion.** in shared
14425 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76 61 cache. This va
14426 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65 20 riable has file
14427 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f 72 scope during nor
14428 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 62 mal builds,.** b
14429 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72 6e ut the test harn
1442a 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 63 ess needs to acc
1442b 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61 6b ess it so we mak
1442c 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 20 e it global for
1442d 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 2e .** test builds.
1442e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f .**.** Access to
1442f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 this variable i
14430 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 53 s protected by S
14431 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
14432 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23 69 IC_MASTER..*/.#i
14433 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
14434 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14435 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 BtShared *SQLITE
14436 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 _WSD sqlite3Shar
14437 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b edCacheList = 0;
14438 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 74 .#else.static Bt
14439 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 Shared *SQLITE_W
1443a 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 SD sqlite3Shared
1443b 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 CacheList = 0;.#
1443c 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 endif.#endif /*
1443d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
1443e 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 ED_CACHE */..#if
1443f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14440 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a _SHARED_CACHE./*
14441 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 .** Enable or di
14442 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65 64 sable the shared
14443 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65 6d pager and schem
14444 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a a features..**.*
14445 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 * This routine h
14446 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 as no effect on
14447 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 existing databas
14448 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a e connections..*
14449 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61 63 * The shared cac
1444a 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 63 he setting effec
1444b 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63 ts only future c
1444c 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 alls to.** sqlit
1444d 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74 e3_open(), sqlit
1444e 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20 e3_open16(), or
1444f 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
14450 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 )..*/.SQLITE_API
14451 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 int sqlite3_ena
14452 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 ble_shared_cache
14453 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 20 (int enable){.
14454 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
14455 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 fig.sharedCacheE
14456 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 3b nabled = enable;
14457 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
14458 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a _OK;.}.#endif...
14459 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 /*.** Forward de
1445a 63 6c 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 claration.*/.sta
1445b 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 tic int checkFor
1445c 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74 ReadConflicts(Bt
1445d 72 65 65 2a 2c 20 50 67 6e 6f 2c 20 42 74 43 75 ree*, Pgno, BtCu
1445e 72 73 6f 72 2a 2c 20 69 36 34 29 3b 0a 0a 0a 23 rsor*, i64);...#
1445f 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
14460 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
14461 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75 6e /*. ** The fun
14462 63 74 69 6f 6e 73 20 71 75 65 72 79 53 68 61 72 ctions queryShar
14463 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
14464 28 29 2c 20 73 65 74 53 68 61 72 65 64 43 61 63 (), setSharedCac
14465 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0a 20 heTableLock(),.
14466 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c 6c ** and clearAll
14467 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
14468 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 6d 61 6e Locks(). ** man
14469 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65 73 20 ipulate entries
1446a 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 2e in the BtShared.
1446b 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 73 pLock linked lis
1446c 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a t used to store.
1446d 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 ** shared-cach
1446e 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c 6f e table level lo
1446f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69 62 72 cks. If the libr
14470 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ary is compiled
14471 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 68 with the. ** sh
14472 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 ared-cache featu
14473 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 re disabled, the
14474 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 n there is only
14475 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0a 20 20 ever one user.
14476 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 53 68 61 ** of each BtSha
14477 72 65 64 20 73 74 72 75 63 74 75 72 65 20 61 6e red structure an
14478 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 6b 69 6e d so this lockin
14479 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 g is not necessa
1447a 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 65 66 ry. . ** So def
1447b 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 72 65 6c ine the lock rel
1447c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 61 ated functions a
1447d 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20 s no-ops.. */.
1447e 20 23 64 65 66 69 6e 65 20 71 75 65 72 79 53 68 #define querySh
1447f 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
14480 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 ck(a,b,c) SQLITE
14481 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 73 65 _OK. #define se
14482 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
14483 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c eLock(a,b,c) SQL
14484 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 ITE_OK. #define
14485 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 clearAllSharedC
14486 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 acheTableLocks(a
14487 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 ).#endif..#ifnde
14488 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
14489 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
1448a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66 Query to see if
1448b 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20 btree handle p
1448c 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 may obtain a loc
1448d 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 k of type eLock
1448e 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f .** (READ_LOCK o
1448f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e r WRITE_LOCK) on
14490 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 the table with
14491 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20 root-page iTab.
14492 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 Return.** SQLITE
14493 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 _OK if the lock
14494 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 may be obtained
14495 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 (by calling.** s
14496 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
14497 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 leLock()), or SQ
14498 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e LITE_LOCKED if n
14499 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ot..*/.static in
1449a 74 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 t querySharedCac
1449b 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 heTableLock(Btre
1449c 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c e *p, Pgno iTab,
1449d 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 u8 eLock){. Bt
1449e 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
1449f 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a >pBt;. BtLock *
144a0 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 pIter;.. assert
144a1 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
144a2 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
144a3 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
144a4 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f READ_LOCK || eLo
144a5 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
144a6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 ;. assert( p->d
144a7 62 21 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 b!=0 );. . /*
144a8 49 66 20 72 65 71 75 65 73 74 69 6e 67 20 61 20 If requesting a
144a9 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 6e write-lock, then
144aa 20 74 68 65 20 42 74 72 65 65 20 6d 75 73 74 20 the Btree must
144ab 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 have an open wri
144ac 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 te. ** transact
144ad 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 ion on this file
144ae 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 6c 79 . And, obviously
144af 2c 20 66 6f 72 20 74 68 69 73 20 74 6f 20 62 65 , for this to be
144b0 20 73 6f 20 74 68 65 72 65 20 0a 20 20 2a 2a 20 so there . **
144b1 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 must be an open
144b2 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
144b3 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 69 74 n on the file it
144b4 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 self.. */. ass
144b5 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 ert( eLock==READ
144b6 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 74 _LOCK || (p==pBt
144b7 2d 3e 70 57 72 69 74 65 72 20 26 26 20 70 2d 3e ->pWriter && p->
144b8 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
144b9 52 49 54 45 29 20 29 3b 0a 20 20 61 73 73 65 72 RITE) );. asser
144ba 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c t( eLock==READ_L
144bb 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 OCK || pBt->inTr
144bc 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
144bd 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 20 2f _WRITE );. . /
144be 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f * This is a no-o
144bf 70 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d p if the shared-
144c0 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 cache is not ena
144c1 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 bled */. if( !p
144c2 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
144c3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
144c4 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 OK;. }.. /* If
144c5 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e some other conn
144c6 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e ection is holdin
144c7 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c g an exclusive l
144c8 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 72 65 ock, the. ** re
144c9 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79 quested lock may
144ca 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 not be obtained
144cb 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 .. */. if( pBt
144cc 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 26 26 20 ->pWriter!=p &&
144cd 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 pBt->isExclusive
144ce 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 ){. sqlite3C
144cf 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 onnectionBlocked
144d0 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 57 72 (p->db, pBt->pWr
144d1 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 20 72 iter->db);. r
144d2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 eturn SQLITE_LOC
144d3 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b KED_SHAREDCACHE;
144d4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 . }.. /* This
144d5 28 61 6c 6f 6e 67 20 77 69 74 68 20 73 65 74 53 (along with setS
144d6 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
144d7 6f 63 6b 28 29 29 20 69 73 20 77 68 65 72 65 0a ock()) is where.
144d8 20 20 2a 2a 20 74 68 65 20 52 65 61 64 55 6e 63 ** the ReadUnc
144d9 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 ommitted flag is
144da 20 64 65 61 6c 74 20 77 69 74 68 2e 0a 20 20 2a dealt with.. *
144db 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 * If the caller
144dc 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f 72 20 is querying for
144dd 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 61 a read-lock on a
144de 6e 79 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 74 ny table. ** ot
144df 68 65 72 20 74 68 61 6e 20 74 68 65 20 73 71 6c her than the sql
144e0 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
144e1 20 28 74 61 62 6c 65 20 31 29 20 61 6e 64 20 69 (table 1) and i
144e2 66 20 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d f the ReadUncomm
144e3 69 74 74 65 64 0a 20 20 2a 2a 20 66 6c 61 67 20 itted. ** flag
144e4 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
144e5 20 6c 6f 63 6b 20 67 72 61 6e 74 65 64 20 65 76 lock granted ev
144e6 65 6e 20 69 66 20 74 68 65 72 65 20 61 72 65 20 en if there are
144e7 77 72 69 74 65 2d 6c 6f 63 6b 73 0a 20 20 2a 2a write-locks. **
144e8 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49 on the table. I
144e9 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 f a write-lock i
144ea 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 s requested, the
144eb 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 ReadUncommitted
144ec 20 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20 6e 6f flag. ** is no
144ed 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 20 20 t considered..
144ee 2a 2a 0a 20 20 2a 2a 20 49 6e 20 66 75 6e 63 74 **. ** In funct
144ef 69 6f 6e 20 73 65 74 53 68 61 72 65 64 43 61 63 ion setSharedCac
144f0 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 69 heTableLock(), i
144f1 66 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 f a read-lock is
144f2 20 64 65 6d 61 6e 64 65 64 20 61 6e 64 20 74 68 demanded and th
144f3 65 20 0a 20 20 2a 2a 20 52 65 61 64 55 6e 63 6f e . ** ReadUnco
144f4 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 mmitted flag is
144f5 73 65 74 2c 20 6e 6f 20 65 6e 74 72 79 20 69 73 set, no entry is
144f6 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 6f added to the lo
144f7 63 6b 73 20 6c 69 73 74 20 0a 20 20 2a 2a 20 28 cks list . ** (
144f8 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 29 2e BtShared.pLock).
144f9 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 73 75 . **. ** To su
144fa 6d 6d 61 72 69 7a 65 3a 20 49 66 20 74 68 65 20 mmarize: If the
144fb 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 ReadUncommitted
144fc 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
144fd 6e 20 72 65 61 64 20 63 75 72 73 6f 72 73 0a 20 n read cursors.
144fe 20 2a 2a 20 6f 6e 20 6e 6f 6e 2d 73 63 68 65 6d ** on non-schem
144ff 61 20 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 a tables do not
14500 63 72 65 61 74 65 20 6f 72 20 72 65 73 70 65 63 create or respec
14501 74 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2e 20 54 t table locks. T
14502 68 65 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 he locking. **
14503 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20 61 20 procedure for a
14504 77 72 69 74 65 2d 63 75 72 73 6f 72 20 64 6f 65 write-cursor doe
14505 73 20 6e 6f 74 20 63 68 61 6e 67 65 2e 0a 20 20 s not change..
14506 2a 2f 0a 20 20 69 66 28 20 0a 20 20 20 20 30 3d */. if( . 0=
14507 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 =(p->db->flags&S
14508 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
14509 69 74 74 65 64 29 20 7c 7c 20 0a 20 20 20 20 65 itted) || . e
1450a 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
1450b 20 7c 7c 0a 20 20 20 20 69 54 61 62 3d 3d 4d 41 ||. iTab==MA
1450c 53 54 45 52 5f 52 4f 4f 54 0a 20 20 29 7b 0a 20 STER_ROOT. ){.
1450d 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 for(pIter=pBt
1450e 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 ->pLock; pIter;
1450f 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 pIter=pIter->pNe
14510 78 74 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 xt){. /* Th
14511 65 20 63 6f 6e 64 69 74 69 6f 6e 20 28 70 49 74 e condition (pIt
14512 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b er->eLock!=eLock
14513 29 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ) in the followi
14514 6e 67 20 69 66 28 2e 2e 2e 29 20 0a 20 20 20 20 ng if(...) .
14515 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 ** statement i
14516 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61 74 69 s a simplificati
14517 6f 6e 20 6f 66 3a 0a 20 20 20 20 20 20 2a 2a 0a on of:. **.
14518 20 20 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 ** (eLoc
14519 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c k==WRITE_LOCK ||
1451a 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 pIter->eLock==W
1451b 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 RITE_LOCK).
1451c 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 73 69 6e **. ** sin
1451d 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 ce we know that
1451e 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f if eLock==WRITE_
1451f 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 LOCK, then no ot
14520 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 her connection.
14521 20 20 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 ** may hold
14522 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e a WRITE_LOCK on
14523 20 61 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68 any table in th
14524 69 73 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74 is file (since t
14525 68 65 72 65 20 63 61 6e 0a 20 20 20 20 20 20 2a here can. *
14526 2a 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 * only be a sing
14527 6c 65 20 77 72 69 74 65 72 29 2e 0a 20 20 20 20 le writer)..
14528 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 */. asser
14529 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d t( pIter->eLock=
1452a 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 =READ_LOCK || pI
1452b 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 ter->eLock==WRIT
1452c 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 E_LOCK );.
1452d 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
1452e 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 EAD_LOCK || pIte
1452f 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c 20 r->pBtree==p ||
14530 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 pIter->eLock==RE
14531 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 AD_LOCK);.
14532 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 if( pIter->pBtre
14533 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 e!=p && pIter->i
14534 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 Table==iTab && p
14535 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f Iter->eLock!=eLo
14536 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ck ){. sq
14537 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
14538 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 locked(p->db, pI
14539 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 ter->pBtree->db)
1453a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 4c ;. if( eL
1453b 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
1453c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 ){. ass
1453d 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 ert( p==pBt->pWr
1453e 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 iter );.
1453f 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 pBt->isPending
14540 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
14541 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
14542 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
14543 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 20 20 REDCACHE;.
14544 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
14545 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
14546 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
14547 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14548 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
14549 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
1454a 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
1454b 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 Add a lock on t
1454c 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f he table with ro
1454d 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 ot-page iTable t
1454e 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 o the shared-btr
1454f 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 ee used.** by Bt
14550 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 ree handle p. Pa
14551 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 rameter eLock mu
14552 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41 st be either REA
14553 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 D_LOCK or .** WR
14554 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 ITE_LOCK..**.**
14555 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
14556 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 urned if the loc
14557 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65 k is added succe
14558 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f ssfully. SQLITE_
14559 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 BUSY and.** SQLI
1455a 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 TE_NOMEM may als
1455b 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a o be returned..*
1455c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 /.static int set
1455d 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
1455e 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 Lock(Btree *p, P
1455f 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 gno iTable, u8 e
14560 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 Lock){. BtShare
14561 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
14562 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b . BtLock *pLock
14563 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a = 0;. BtLock *
14564 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 pIter;.. assert
14565 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
14566 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
14567 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
14568 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f READ_LOCK || eLo
14569 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
1456a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 ;. assert( p->d
1456b 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 b!=0 );.. /* Th
1456c 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 is is a no-op if
1456d 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 the shared-cach
1456e 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 e is not enabled
1456f 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 */. if( !p->sh
14570 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 arable ){. re
14571 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
14572 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53 }.. assert( S
14573 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 QLITE_OK==queryS
14574 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
14575 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 ock(p, iTable, e
14576 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 Lock) );.. /* I
14577 66 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d f the read-uncom
14578 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 73 mitted flag is s
14579 65 74 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f et and a read-lo
1457a 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 20 ck is requested
1457b 6f 6e 0a 20 20 2a 2a 20 61 20 6e 6f 6e 2d 73 63 on. ** a non-sc
1457c 68 65 6d 61 20 74 61 62 6c 65 2c 20 74 68 65 6e hema table, then
1457d 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 6c 77 the lock is alw
1457e 61 79 73 20 67 72 61 6e 74 65 64 2e 20 20 52 65 ays granted. Re
1457f 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a 2a 20 turn early. **
14580 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20 61 without adding a
14581 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 42 n entry to the B
14582 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 tShared.pLock li
14583 73 74 2e 20 53 65 65 0a 20 20 2a 2a 20 63 6f 6d st. See. ** com
14584 6d 65 6e 74 20 69 6e 20 66 75 6e 63 74 69 6f 6e ment in function
14585 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 querySharedCach
14586 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 eTableLock() for
14587 20 6d 6f 72 65 20 69 6e 66 6f 0a 20 20 2a 2a 20 more info. **
14588 6f 6e 20 68 61 6e 64 6c 69 6e 67 20 74 68 65 20 on handling the
14589 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 ReadUncommitted
1458a 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 flag.. */. if(
1458b 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c . (p->db->fl
1458c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 ags&SQLITE_ReadU
1458d 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a ncommitted) && .
1458e 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 (eLock==READ
1458f 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54 _LOCK) &&. iT
14590 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f able!=MASTER_ROO
14591 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 T. ){. retur
14592 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
14593 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 .. /* First sea
14594 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 rch the list for
14595 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 an existing loc
14596 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e k on this table.
14597 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d */. for(pIter=
14598 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 pBt->pLock; pIte
14599 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e r; pIter=pIter->
1459a 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
1459b 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 pIter->iTable==i
1459c 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e Table && pIter->
1459d 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 pBtree==p ){.
1459e 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 pLock = pIter
1459f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
145a0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
145a1 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 f the above sear
145a2 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 ch did not find
145a3 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 a BtLock struct
145a4 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 associating Btre
145a5 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 e p. ** with ta
145a6 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f ble iTable, allo
145a7 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e cate one and lin
145a8 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 k it into the li
145a9 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 st.. */. if( !
145aa 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f pLock ){. pLo
145ab 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 ck = (BtLock *)s
145ac 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
145ad 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 (sizeof(BtLock))
145ae 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b ;. if( !pLock
145af 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
145b0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
145b1 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e }. pLock->
145b2 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b iTable = iTable;
145b3 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 . pLock->pBtr
145b4 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 ee = p;. pLoc
145b5 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e k->pNext = pBt->
145b6 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e pLock;. pBt->
145b7 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 pLock = pLock;.
145b8 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
145b9 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 BtLock.eLock va
145ba 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 riable to the ma
145bb 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 ximum of the cur
145bc 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 rent lock. ** a
145bd 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64 nd the requested
145be 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e lock. This mean
145bf 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 s if a write-loc
145c0 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 k was already he
145c1 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 ld. ** and a re
145c2 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 ad-lock requeste
145c3 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f d, we don't inco
145c4 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 rrectly downgrad
145c5 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f e the lock.. */
145c6 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 54 45 . assert( WRITE
145c7 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 _LOCK>READ_LOCK
145c8 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 );. if( eLock>p
145c9 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 Lock->eLock ){.
145ca 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 pLock->eLock
145cb 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 = eLock;. }..
145cc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
145cd 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 ;.}.#endif /* !S
145ce 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
145cf 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e D_CACHE */..#ifn
145d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
145d1 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a SHARED_CACHE./*.
145d2 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 ** Release all t
145d3 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 he table locks (
145d4 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 locks obtained v
145d5 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 ia calls to.** t
145d6 68 65 20 73 65 74 53 68 61 72 65 64 43 61 63 68 he setSharedCach
145d7 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f eTableLock() pro
145d8 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 cedure) held by
145d9 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a Btree handle p..
145da 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
145db 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 ion assumes that
145dc 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20 61 6e handle p has an
145dd 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20 77 72 open read or wr
145de 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 ite .** transact
145df 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65 73 20 ion. If it does
145e0 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 42 74 not, then the Bt
145e1 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69 6e 67 Shared.isPending
145e2 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 79 variable.** may
145e3 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 be incorrectly
145e4 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 cleared..*/.stat
145e5 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 6c 6c ic void clearAll
145e6 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
145e7 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b Locks(Btree *p){
145e8 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
145e9 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c = p->pBt;. BtL
145ea 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d 20 26 ock **ppIter = &
145eb 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 61 pBt->pLock;.. a
145ec 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
145ed 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
145ee 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
145ef 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 3d 3d >sharable || 0==
145f0 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 73 73 *ppIter );. ass
145f1 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e ert( p->inTrans>
145f2 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 0 );.. while( *
145f3 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74 ppIter ){. Bt
145f4 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 Lock *pLock = *p
145f5 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72 pIter;. asser
145f6 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 t( pBt->isExclus
145f7 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 ive==0 || pBt->p
145f8 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 Writer==pLock->p
145f9 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 73 73 Btree );. ass
145fa 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 ert( pLock->pBtr
145fb 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c 6f ee->inTrans>=pLo
145fc 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 ck->eLock );.
145fd 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 if( pLock->pBtr
145fe 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a ee==p ){. *
145ff 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e ppIter = pLock->
14600 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c pNext;. sql
14601 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 ite3_free(pLock)
14602 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
14603 20 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f ppIter = &pLo
14604 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d ck->pNext;. }
14605 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
14606 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d 3d pBt->isPending==
14607 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 0 || pBt->pWrite
14608 72 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e r );. if( pBt->
14609 70 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 pWriter==p ){.
1460a 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d pBt->pWriter =
1460b 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 0;. pBt->isE
1460c 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 xclusive = 0;.
1460d 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 pBt->isPending
1460e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
1460f 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 ( pBt->nTransact
14610 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a ion==2 ){. /*
14611 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
14612 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 63 6f s called when co
14613 6e 6e 65 63 74 69 6f 6e 20 70 20 69 73 20 63 6f nnection p is co
14614 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 ncluding its .
14615 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
14616 2e 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65 . If there curre
14617 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 ntly exists a wr
14618 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e iter, and p is n
14619 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 ot. ** that w
1461a 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 riter, then the
1461b 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 number of locks
1461c 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 held by connecti
1461d 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a ons other. **
1461e 20 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 than the writer
1461f 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 must be about t
14620 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 o drop to zero.
14621 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 In this case.
14622 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 ** set the isPe
14623 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e nding flag to 0.
14624 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
14625 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 f there is not c
14626 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 urrently a write
14627 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 r, then BtShared
14628 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a .isPending must.
14629 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 ** be zero a
1462a 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 lready. So this
1462b 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 next line is har
1462c 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 mless in that ca
1462d 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 se.. */. p
1462e 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 Bt->isPending =
1462f 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 0;. }.}.#endif
14630 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 /* SQLITE_OMIT_S
14631 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a HARED_CACHE */..
14632 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 static void rele
14633 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 asePage(MemPage
14634 2a 70 50 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 *pPage); /* For
14635 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a ward reference *
14636 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66 79 20 /../*.** Verify
14637 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 that the cursor
14638 68 6f 6c 64 73 20 61 20 6d 75 74 65 78 20 6f 6e holds a mutex on
14639 20 74 68 65 20 42 74 53 68 61 72 65 64 0a 2a 2f the BtShared.*/
1463a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
1463b 73 74 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f static int curso
1463c 72 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75 rHoldsMutex(BtCu
1463d 72 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 rsor *p){. retu
1463e 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 rn sqlite3_mutex
1463f 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 _held(p->pBt->mu
14640 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a tex);.}.#endif..
14641 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14642 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a OMIT_INCRBLOB./*
14643 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 .** Invalidate t
14644 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
14645 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 -list cache for
14646 63 75 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20 cursor pCur, if
14647 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 any..*/.static v
14648 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 oid invalidateOv
14649 65 72 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75 erflowCache(BtCu
1464a 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 rsor *pCur){. a
1464b 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
1464c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
1464d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
1464e 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 pCur->aOverflow)
1464f 3b 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 ;. pCur->aOverf
14650 6c 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a low = 0;.}../*.*
14651 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 * Invalidate the
14652 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
14653 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c ist cache for al
14654 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 l cursors opened
14655 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 .** on the share
14656 64 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 d btree structur
14657 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 e pBt..*/.static
14658 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 void invalidate
14659 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
1465a 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
1465b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a . BtCursor *p;.
1465c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1465d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1465e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f ->mutex) );. fo
1465f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 r(p=pBt->pCursor
14660 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
14661 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 {. invalidate
14662 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29 OverflowCache(p)
14663 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.}.#else.
14664 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 #define invalida
14665 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
14666 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 x). #define inv
14667 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c alidateAllOverfl
14668 6f 77 43 61 63 68 65 28 78 29 0a 23 65 6e 64 69 owCache(x).#endi
14669 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62 69 74 f../*.** Set bit
1466a 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42 74 53 pgno of the BtS
1466b 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e hared.pHasConten
1466c 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 20 69 t bitvec. This i
1466d 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77 68 65 s called .** whe
1466e 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 70 72 n a page that pr
1466f 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e eviously contain
14670 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65 73 20 ed data becomes
14671 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 a free-list leaf
14672 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a .** page..**.**
14673 20 54 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 The BtShared.pH
14674 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 asContent bitvec
14675 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72 6b 20 exists to work
14676 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63 75 72 around an obscur
14677 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65 64 20 e.** bug caused
14678 62 79 20 74 68 65 20 69 6e 74 65 72 61 63 74 69 by the interacti
14679 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66 75 6c on of two useful
1467a 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e IO optimization
1467b 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a 2a 2a s surrounding.**
1467c 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
1467d 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 pages:.**.** 1
1467e 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 61 20 ) When all data
1467f 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 is deleted from
14680 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 70 a page and the p
14681 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 20 age becomes.**
14682 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 74 20 a free-list
14683 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 20 70 leaf page, the p
14684 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 74 74 age is not writt
14685 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 en to the databa
14686 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73 20 66 se.** (as f
14687 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 ree-list leaf pa
14688 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f 20 6d ges contain no m
14689 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61 29 2e eaningful data).
1468a 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 20 20 Sometimes.**
1468b 20 20 20 73 75 63 68 20 61 20 70 61 67 65 20 69 such a page i
1468c 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75 72 6e s not even journ
1468d 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77 69 6c alled (as it wil
1468e 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 l not be modifie
1468f 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79 20 62 d,.** why b
14690 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c 69 6e other journallin
14691 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 g it?)..**.**
14692 32 29 20 57 68 65 6e 20 61 20 66 72 65 65 2d 6c 2) When a free-l
14693 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 73 ist leaf page is
14694 20 72 65 75 73 65 64 2c 20 69 74 73 20 63 6f 6e reused, its con
14695 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65 61 64 tent is not read
14696 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 74 68 .** from th
14697 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 77 72 e database or wr
14698 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
14699 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79 20 73 rnal file (why s
1469a 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20 20 20 hould it.**
1469b 20 62 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f be, if it is no
1469c 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69 6e 67 t at all meaning
1469d 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 79 20 ful?)..**.** By
1469e 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68 65 73 themselves, thes
1469f 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 e optimizations
146a0 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20 70 72 work fine and pr
146a1 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a 2a 2a ovide a handy.**
146a2 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 6f 6f performance boo
146a3 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c 65 74 st to bulk delet
146a4 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70 65 72 e or insert oper
146a5 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65 72 2c ations. However,
146a6 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 if.** a page is
146a7 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 moved to the fr
146a8 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68 65 6e ee-list and then
146a9 20 72 65 75 73 65 64 20 77 69 74 68 69 6e 20 74 reused within t
146aa 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61 6e 73 he same.** trans
146ab 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62 6c 65 action, a proble
146ac 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66 20 74 m comes up. If t
146ad 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 6a he page is not j
146ae 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e 0a 2a ournalled when.*
146af 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20 74 6f * it is moved to
146b0 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 the free-list a
146b1 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20 6e 6f nd it is also no
146b2 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 t journalled whe
146b3 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74 72 61 n it.** is extra
146b4 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 cted from the fr
146b5 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 75 73 ee-list and reus
146b6 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 72 69 ed, then the ori
146b7 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 6d 61 ginal data.** ma
146b8 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 74 68 y be lost. In th
146b9 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 6f 6c e event of a rol
146ba 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 6e 6f lback, it may no
146bb 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a t be possible.**
146bc 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
146bd 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 database to its
146be 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 67 75 original configu
146bf 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ration..**.** Th
146c0 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 68 e solution is th
146c1 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 e BtShared.pHasC
146c2 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 57 ontent bitvec. W
146c3 68 65 6e 65 76 65 72 20 61 20 70 61 67 65 20 69 henever a page i
146c4 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f 20 62 s .** moved to b
146c5 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c 69 73 ecome a free-lis
146c6 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 t leaf page, the
146c7 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 corresponding b
146c8 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69 6e 20 it is.** set in
146c9 74 68 65 20 62 69 74 76 65 63 2e 20 57 68 65 6e the bitvec. When
146ca 65 76 65 72 20 61 20 6c 65 61 66 20 70 61 67 65 ever a leaf page
146cb 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72 is extracted fr
146cc 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 om the free-list
146cd 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f ,.** optimizatio
146ce 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f 6d 6d n 2 above is omm
146cf 69 74 74 65 64 20 69 66 20 74 68 65 20 63 6f 72 itted if the cor
146d0 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 responding bit i
146d1 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 65 74 s already.** set
146d2 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 48 61 in BtShared.pHa
146d3 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 63 6f sContent. The co
146d4 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 69 ntents of the bi
146d5 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 65 64 tvec are cleared
146d6 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f .** at the end o
146d7 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 63 74 f every transact
146d8 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
146d9 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 43 6f nt btreeSetHasCo
146da 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a ntent(BtShared *
146db 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b pBt, Pgno pgno){
146dc 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
146dd 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 42 TE_OK;. if( !pB
146de 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 t->pHasContent )
146df 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b {. int nPage;
146e0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
146e1 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
146e2 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 pBt->pPager, &nP
146e3 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
146e4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
146e5 20 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43 6f pBt->pHasCo
146e6 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 ntent = sqlite3B
146e7 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33 32 itvecCreate((u32
146e8 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 )nPage);. i
146e9 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e f( !pBt->pHasCon
146ea 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 tent ){.
146eb 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
146ec 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d M;. }. }
146ed 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
146ee 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 67 6e 6f QLITE_OK && pgno
146ef 3c 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 <=sqlite3BitvecS
146f0 69 7a 65 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e ize(pBt->pHasCon
146f1 74 65 6e 74 29 20 29 7b 0a 20 20 20 20 72 63 20 tent) ){. rc
146f2 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 = sqlite3BitvecS
146f3 65 74 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 et(pBt->pHasCont
146f4 65 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a ent, pgno);. }.
146f5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
146f6 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 /*.** Query the
146f7 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e BtShared.pHasCon
146f8 74 65 6e 74 20 76 65 63 74 6f 72 2e 0a 2a 2a 0a tent vector..**.
146f9 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
146fa 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
146fb 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 a free-list leaf
146fc 20 70 61 67 65 20 69 73 20 72 65 6d 6f 76 65 64 page is removed
146fd 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 66 72 65 from the.** fre
146fe 65 2d 6c 69 73 74 20 66 6f 72 20 72 65 75 73 65 e-list for reuse
146ff 2e 20 49 74 20 72 65 74 75 72 6e 73 20 66 61 6c . It returns fal
14700 73 65 20 69 66 20 69 74 20 69 73 20 73 61 66 65 se if it is safe
14701 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 to retrieve the
14702 0a 2a 2a 20 70 61 67 65 20 66 72 6f 6d 20 74 68 .** page from th
14703 65 20 70 61 67 65 72 20 6c 61 79 65 72 20 77 69 e pager layer wi
14704 74 68 20 74 68 65 20 27 6e 6f 2d 63 6f 6e 74 65 th the 'no-conte
14705 6e 74 27 20 66 6c 61 67 20 73 65 74 2e 20 54 72 nt' flag set. Tr
14706 75 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f ue otherwise..*/
14707 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
14708 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 eGetHasContent(B
14709 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 tShared *pBt, Pg
1470a 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 42 69 74 76 no pgno){. Bitv
1470b 65 63 20 2a 70 20 3d 20 70 42 74 2d 3e 70 48 61 ec *p = pBt->pHa
1470c 73 43 6f 6e 74 65 6e 74 3b 0a 20 20 72 65 74 75 sContent;. retu
1470d 72 6e 20 28 70 20 26 26 20 28 70 67 6e 6f 3e 73 rn (p && (pgno>s
1470e 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 qlite3BitvecSize
1470f 28 70 29 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 (p) || sqlite3Bi
14710 74 76 65 63 54 65 73 74 28 70 2c 20 70 67 6e 6f tvecTest(p, pgno
14711 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c )));.}../*.** Cl
14712 65 61 72 20 28 64 65 73 74 72 6f 79 29 20 74 68 ear (destroy) th
14713 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 e BtShared.pHasC
14714 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54 ontent bitvec. T
14715 68 69 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a his should be.**
14716 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 20 invoked at the
14717 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 65 61 conclusion of ea
14718 63 68 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 ch write-transac
14719 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
1471a 76 6f 69 64 20 62 74 72 65 65 43 6c 65 61 72 48 void btreeClearH
1471b 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 asContent(BtShar
1471c 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 ed *pBt){. sqli
1471d 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
1471e 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e (pBt->pHasConten
1471f 74 29 3b 0a 20 20 70 42 74 2d 3e 70 48 61 73 43 t);. pBt->pHasC
14720 6f 6e 74 65 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f ontent = 0;.}../
14721 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 *.** Save the cu
14722 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 rrent cursor pos
14723 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 61 72 ition in the var
14724 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e iables BtCursor.
14725 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42 74 43 nKey .** and BtC
14726 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68 65 20 ursor.pKey. The
14727 63 75 72 73 6f 72 27 73 20 73 74 61 74 65 20 69 cursor's state i
14728 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f 52 5f s set to CURSOR_
14729 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a 2f 0a REQUIRESEEK..*/.
1472a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 43 static int saveC
1472b 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 ursorPosition(Bt
1472c 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
1472d 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 int rc;.. asse
1472e 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 rt( CURSOR_VALID
1472f 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 ==pCur->eState )
14730 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 ;. assert( 0==p
14731 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 Cur->pKey );. a
14732 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
14733 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
14734 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 .. rc = sqlite3
14735 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 BtreeKeySize(pCu
14736 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b r, &pCur->nKey);
14737 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
14738 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c s an intKey tabl
14739 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 6f 76 e, then the abov
1473a 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 4b e call to BtreeK
1473b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20 73 74 eySize(). ** st
1473c 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 65 72 ores the integer
1473d 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e 6e 4b key in pCur->nK
1473e 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ey. In this case
1473f 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 0a 20 this value is.
14740 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73 20 ** all that is
14741 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65 72 77 required. Otherw
14742 69 73 65 2c 20 69 66 20 70 43 75 72 20 69 73 20 ise, if pCur is
14743 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 not open on an i
14744 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62 6c 65 ntKey. ** table
14745 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20 73 70 , then malloc sp
14746 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74 6f 72 ace for and stor
14747 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b 65 79 e the pCur->nKey
14748 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 0a 20 bytes of key .
14749 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f 0a 20 ** data.. */.
1474a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1474b 4f 4b 20 26 26 20 30 3d 3d 70 43 75 72 2d 3e 61 OK && 0==pCur->a
1474c 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 pPage[0]->intKey
1474d 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b 65 ){. void *pKe
1474e 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f y = sqlite3Mallo
1474f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b c( (int)pCur->nK
14750 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4b ey );. if( pK
14751 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ey ){. rc =
14752 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
14753 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 70 (pCur, 0, (int)p
14754 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 Cur->nKey, pKey)
14755 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
14756 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
14757 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 pCur->pKey
14758 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 = pKey;. }e
14759 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
1475a 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 29 3b ite3_free(pKey);
1475b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1475c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
1475d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1475e 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
1475f 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 !pCur->apPage[0
14760 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 70 43 ]->intKey || !pC
14761 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 69 ur->pKey );.. i
14762 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
14763 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
14764 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 for(i=0; i<=p
14765 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 Cur->iPage; i++)
14766 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
14767 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 age(pCur->apPage
14768 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43 75 72 [i]);. pCur
14769 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b ->apPage[i] = 0;
1476a 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d . }. pCur-
1476b 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 20 >iPage = -1;.
1476c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
1476d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
1476e 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 61 6c EK;. }.. inval
1476f 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 idateOverflowCac
14770 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 he(pCur);. retu
14771 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
14772 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f Save the positio
14773 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 ns of all cursor
14774 73 20 65 78 63 65 70 74 20 70 45 78 63 65 70 74 s except pExcept
14775 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62 open on the tab
14776 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f 6f 74 le .** with root
14777 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75 -page iRoot. Usu
14778 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 ally, this is ca
14779 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65 lled just before
1477a 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65 cursor.** pExce
1477b 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f pt is used to mo
1477c 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20 28 dify the table (
1477d 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 BtreeDelete() or
1477e 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e BtreeInsert()).
1477f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
14780 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 aveAllCursors(Bt
14781 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
14782 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f o iRoot, BtCurso
14783 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42 r *pExcept){. B
14784 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 tCursor *p;. as
14785 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14786 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
14787 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
14788 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20 ( pExcept==0 ||
14789 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42 pExcept->pBt==pB
1478a 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 t );. for(p=pBt
1478b 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d ->pCursor; p; p=
1478c 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 p->pNext){. i
1478d 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 26 26 f( p!=pExcept &&
1478e 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d (0==iRoot || p-
1478f 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 >pgnoRoot==iRoot
14790 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d ) && . p-
14791 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
14792 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69 VALID ){. i
14793 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 nt rc = saveCurs
14794 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 orPosition(p);.
14795 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f if( SQLITE_
14796 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 OK!=rc ){.
14797 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
14798 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
14799 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1479a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 K;.}../*.** Clea
1479b 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 r the current cu
1479c 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a rsor position..*
1479d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1479e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
1479f 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 eeClearCursor(Bt
147a0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
147a1 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
147a2 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
147a3 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
147a4 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 e(pCur->pKey);.
147a5 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b pCur->pKey = 0;
147a6 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 . pCur->eState
147a7 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
147a8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f ;.}../*.** Resto
147a9 72 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f re the cursor to
147aa 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 the position it
147ab 20 77 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63 was in (or as c
147ac 6c 6f 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69 lose to as possi
147ad 62 6c 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 ble).** when sav
147ae 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
147af 29 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f ) was called. No
147b0 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6c te that this cal
147b1 6c 20 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a l deletes the .*
147b2 2a 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e * saved position
147b3 20 69 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20 info stored by
147b4 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 saveCursorPositi
147b5 6f 6e 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63 on(), so there c
147b6 61 6e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 an be.** at most
147b7 20 6f 6e 65 20 65 66 66 65 63 74 69 76 65 20 72 one effective r
147b8 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
147b9 74 69 6f 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 tion() call afte
147ba 72 20 65 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 r each .** saveC
147bb 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e ursorPosition().
147bc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
147bd 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
147be 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 reeRestoreCursor
147bf 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f Position(BtCurso
147c0 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 r *pCur){. int
147c1 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 rc;. assert( cu
147c2 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
147c3 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
147c4 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d ( pCur->eState>=
147c5 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
147c6 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 EK );. if( pCur
147c7 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
147c8 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 _FAULT ){. re
147c9 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b turn pCur->skip;
147ca 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 . }. pCur->eSt
147cb 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
147cc 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 73 71 6c ALID;. rc = sql
147cd 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 ite3BtreeMoveto(
147ce 70 43 75 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79 pCur, pCur->pKey
147cf 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c , pCur->nKey, 0,
147d0 20 26 70 43 75 72 2d 3e 73 6b 69 70 29 3b 0a 20 &pCur->skip);.
147d1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
147d2 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
147d3 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 3_free(pCur->pKe
147d4 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b y);. pCur->pK
147d5 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 ey = 0;. asse
147d6 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
147d7 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c ==CURSOR_VALID |
147d8 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d | pCur->eState==
147d9 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 CURSOR_INVALID )
147da 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
147db 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 c;.}..#define re
147dc 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
147dd 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 ion(p) \. (p->e
147de 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 State>=CURSOR_RE
147df 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 QUIRESEEK ? \.
147e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
147e1 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 reeRestoreCursor
147e2 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20 5c 0a Position(p) : \.
147e3 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
147e4 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 OK)../*.** Deter
147e5 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 mine whether or
147e6 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68 61 73 not a cursor has
147e7 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 moved from the
147e8 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a 20 77 position it.** w
147e9 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64 20 61 as last placed a
147ea 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61 6e 20 t. Cursors can
147eb 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20 72 6f move when the ro
147ec 77 20 74 68 65 79 20 61 72 65 20 70 6f 69 6e 74 w they are point
147ed 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64 65 6c ing.** at is del
147ee 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e eted out from un
147ef 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 der them..**.**
147f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 This routine ret
147f1 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f urns an error co
147f2 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 de if something
147f3 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54 68 65 goes wrong. The
147f4 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70 48 61 .** integer *pHa
147f5 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f sMoved is set to
147f6 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75 72 73 one if the curs
147f7 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61 6e 64 or has moved and
147f8 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 0 if not..*/.SQ
147f9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
147fa 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
147fb 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 sorHasMoved(BtCu
147fc 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
147fd 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20 20 69 *pHasMoved){. i
147fe 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 nt rc;.. rc = r
147ff 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
14800 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 tion(pCur);. if
14801 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 48 61 ( rc ){. *pHa
14802 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 sMoved = 1;.
14803 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
14804 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
14805 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
14806 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 21 3d 30 || pCur->skip!=0
14807 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 ){. *pHasMov
14808 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b ed = 1;. }else{
14809 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 . *pHasMoved
1480a 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
1480b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1480c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1480d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f MIT_AUTOVACUUM./
1480e 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 70 61 67 *.** Given a pag
1480f 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 72 65 e number of a re
14810 67 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 70 gular database p
14811 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 age, return the
14812 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 page.** number f
14813 6f 72 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d or the pointer-m
14814 61 70 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e ap page that con
14815 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20 tains the entry
14816 66 6f 72 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 for the.** input
14817 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f page number..*/
14818 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 74 72 .static Pgno ptr
14819 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68 61 72 mapPageno(BtShar
1481a 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 ed *pBt, Pgno pg
1481b 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 no){. int nPage
1481c 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 sPerMapPage;. P
1481d 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72 65 74 gno iPtrMap, ret
1481e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1481f 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14820 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
14821 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 nPagesPerMapPage
14822 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 = (pBt->usableS
14823 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72 ize/5)+1;. iPtr
14824 4d 61 70 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e Map = (pgno-2)/n
14825 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b PagesPerMapPage;
14826 0a 20 20 72 65 74 20 3d 20 28 69 50 74 72 4d 61 . ret = (iPtrMa
14827 70 2a 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 p*nPagesPerMapPa
14828 67 65 29 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 ge) + 2; . if(
14829 72 65 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 ret==PENDING_BYT
1482a 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
1482b 20 20 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 ret++;. }.
1482c 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f return ret;.}../
1482d 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e *.** Write an en
1482e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 try into the poi
1482f 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 nter map..**.**
14830 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 70 64 This routine upd
14831 61 74 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 ates the pointer
14832 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 map entry for p
14833 61 67 65 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 age number 'key'
14834 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 6d .** so that it m
14835 61 70 73 20 74 6f 20 74 79 70 65 20 27 65 54 79 aps to type 'eTy
14836 70 65 27 20 61 6e 64 20 70 61 72 65 6e 74 20 70 pe' and parent p
14837 61 67 65 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f age number 'pgno
14838 27 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 '..** An error c
14839 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
1483a 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 if something goe
1483b 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 s wrong, otherwi
1483c 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f se SQLITE_OK..*/
1483d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d .static int ptrm
1483e 61 70 50 75 74 28 42 74 53 68 61 72 65 64 20 2a apPut(BtShared *
1483f 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 pBt, Pgno key, u
14840 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 8 eType, Pgno pa
14841 72 65 6e 74 29 7b 0a 20 20 44 62 50 61 67 65 20 rent){. DbPage
14842 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20 54 68 *pDbPage; /* Th
14843 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 e pointer map pa
14844 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 ge */. u8 *pPtr
14845 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 map; /* The
14846 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64 61 74 pointer map dat
14847 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 a */. Pgno iPtr
14848 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 map; /* The
14849 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
1484a 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 number */. int
1484b 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f offset; /
1484c 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f 69 6e * Offset in poin
1484d 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a ter map page */.
1484e 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 int rc;.. ass
1484f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14850 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
14851 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 ex) );. /* The
14852 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 master-journal p
14853 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 age number must
14854 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 73 never be used as
14855 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 a pointer map p
14856 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 age */. assert(
14857 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 0==PTRMAP_ISPAG
14858 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 E(pBt, PENDING_B
14859 59 54 45 5f 50 41 47 45 28 70 42 74 29 29 20 29 YTE_PAGE(pBt)) )
1485a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 ;.. assert( pBt
1485b 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a ->autoVacuum );.
1485c 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a if( key==0 ){.
1485d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1485e 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1485f 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 }. iPtrmap =
14860 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
14861 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 t, key);. rc =
14862 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
14863 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 pBt->pPager, iPt
14864 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b rmap, &pDbPage);
14865 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
14866 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
14867 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6f 66 66 rn rc;. }. off
14868 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 set = PTRMAP_PTR
14869 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 OFFSET(iPtrmap,
1486a 6b 65 79 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 key);. pPtrmap
1486b 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 = (u8 *)sqlite3P
1486c 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 agerGetData(pDbP
1486d 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 age);.. if( eTy
1486e 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 pe!=pPtrmap[offs
1486f 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 et] || get4byte(
14870 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b &pPtrmap[offset+
14871 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 1])!=parent ){.
14872 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 TRACE(("PTRMA
14873 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 P_UPDATE: %d->(%
14874 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 d,%d)\n", key, e
14875 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a Type, parent));.
14876 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
14877 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61 PagerWrite(pDbPa
14878 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ge);. if( rc=
14879 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1487a 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 pPtrmap[offs
1487b 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 et] = eType;.
1487c 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 74 put4byte(&pPt
1487d 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 rmap[offset+1],
1487e 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 parent);. }.
1487f 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 }.. sqlite3Pag
14880 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 erUnref(pDbPage)
14881 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
14882 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 ../*.** Read an
14883 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 70 entry from the p
14884 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a ointer map..**.*
14885 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
14886 65 74 72 69 65 76 65 73 20 74 68 65 20 70 6f 69 etrieves the poi
14887 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 nter map entry f
14888 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c 20 77 or page 'key', w
14889 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 riting.** the ty
1488a 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 pe and parent pa
1488b 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 ge number to *pE
1488c 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 Type and *pPgno
1488d 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a respectively..**
1488e 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 An error code i
1488f 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f s returned if so
14890 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
14891 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 ng, otherwise SQ
14892 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 LITE_OK..*/.stat
14893 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74 ic int ptrmapGet
14894 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
14895 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 Pgno key, u8 *pE
14896 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e Type, Pgno *pPgn
14897 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
14898 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 bPage; /* The
14899 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
1489a 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 */. int iPtrma
1489b 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e p; /* Poin
1489c 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 6e 64 ter map page ind
1489d 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 ex */. u8 *pPtr
1489e 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f map; /* Po
1489f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 64 inter map page d
148a0 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 ata */. int off
148a1 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f set; /* O
148a2 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 20 69 ffset of entry i
148a3 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f n pointer map */
148a4 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
148a5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
148a6 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
148a7 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d tex) );.. iPtrm
148a8 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 ap = PTRMAP_PAGE
148a9 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 NO(pBt, key);.
148aa 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
148ab 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 rGet(pBt->pPager
148ac 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 , iPtrmap, &pDbP
148ad 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d age);. if( rc!=
148ae 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
148af 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 rc;. }. pPtrma
148b0 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 p = (u8 *)sqlite
148b1 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 3PagerGetData(pD
148b2 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65 bPage);.. offse
148b3 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 t = PTRMAP_PTROF
148b4 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 FSET(iPtrmap, ke
148b5 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 y);. assert( pE
148b6 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45 Type!=0 );. *pE
148b7 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f Type = pPtrmap[o
148b8 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20 70 50 ffset];. if( pP
148b9 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67 gno ) *pPgno = g
148ba 65 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 et4byte(&pPtrmap
148bb 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 [offset+1]);..
148bc 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
148bd 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 f(pDbPage);. if
148be 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a ( *pEType<1 || *
148bf 70 45 54 79 70 65 3e 35 20 29 20 72 65 74 75 72 pEType>5 ) retur
148c0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
148c1 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72 6e 20 _BKPT;. return
148c2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 SQLITE_OK;.}..#e
148c3 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 lse /* if define
148c4 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 d SQLITE_OMIT_AU
148c5 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64 TOVACUUM */. #d
148c6 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 28 efine ptrmapPut(
148c7 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f w,x,y,z) SQLITE_
148c8 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 OK. #define ptr
148c9 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 mapGet(w,x,y,z)
148ca 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 SQLITE_OK. #def
148cb 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 ine ptrmapPutOvf
148cc 6c 28 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b l(y,z) SQLITE_OK
148cd 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 .#endif../*.** G
148ce 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67 iven a btree pag
148cf 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 e and a cell ind
148d0 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20 ex (0 means the
148d1 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a first cell on.**
148d2 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61 the page, 1 mea
148d3 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65 ns the second ce
148d4 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 ll, and so forth
148d5 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 ) return a point
148d6 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c er.** to the cel
148d7 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a l content..**.**
148d8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f This routine wo
148d9 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 rks only for pag
148da 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 es that do not c
148db 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 ontain overflow
148dc 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e cells..*/.#defin
148dd 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 e findCell(P,I)
148de 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 20 \. ((P)->aData
148df 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 + ((P)->maskPage
148e0 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50 29 & get2byte(&(P)
148e1 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c ->aData[(P)->cel
148e2 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 lOffset+2*(I)]))
148e3 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 )../*.** This a
148e4 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 more complex ver
148e5 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c sion of findCell
148e6 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 66 6f () that works fo
148e7 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 r.** pages that
148e8 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 do contain overf
148e9 6c 6f 77 20 63 65 6c 6c 73 2e 20 20 53 65 65 20 low cells. See
148ea 69 6e 73 65 72 74 0a 2a 2f 0a 73 74 61 74 69 63 insert.*/.static
148eb 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f u8 *findOverflo
148ec 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 wCell(MemPage *p
148ed 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 Page, int iCell)
148ee 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 {. int i;. ass
148ef 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
148f0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
148f1 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
148f2 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 for(i=pPage->nOv
148f3 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 erflow-1; i>=0;
148f4 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b i--){. int k;
148f5 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 66 . struct _Ovf
148f6 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 lCell *pOvfl;.
148f7 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 pOvfl = &pPage
148f8 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 ->aOvfl[i];.
148f9 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a k = pOvfl->idx;.
148fa 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c if( k<=iCell
148fb 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d ){. if( k=
148fc 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 =iCell ){.
148fd 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e return pOvfl->
148fe 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 pCell;. }.
148ff 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 iCell--;.
14900 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
14901 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
14902 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a iCell);.}../*.*
14903 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 63 * Parse a cell c
14904 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 ontent block and
14905 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c fill in the Cel
14906 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e lInfo structure.
14907 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 There.** are t
14908 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 wo versions of t
14909 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 73 his function. s
1490a 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
1490b 43 65 6c 6c 28 29 20 74 61 6b 65 73 20 61 20 0a Cell() takes a .
1490c 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 ** cell index as
1490d 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
1490e 6d 65 6e 74 20 61 6e 64 20 73 71 6c 69 74 65 33 ment and sqlite3
1490f 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 BtreeParseCellPt
14910 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 r() .** takes a
14911 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62 pointer to the b
14912 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 ody of the cell
14913 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 as its second ar
14914 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 gument..**.** Wi
14915 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 thin this file,
14916 74 68 65 20 70 61 72 73 65 43 65 6c 6c 28 29 20 the parseCell()
14917 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 63 61 6c macro can be cal
14918 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a led instead of.*
14919 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 * sqlite3BtreePa
1491a 72 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 55 73 rseCellPtr(). Us
1491b 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 ing some compile
1491c 72 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 rs, this will be
1491d 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 faster..*/.SQLI
1491e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1491f 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 sqlite3BtreePars
14920 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 eCellPtr(. MemP
14921 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 age *pPage,
14922 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 /* Page cont
14923 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 aining the cell
14924 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 */. u8 *pCell,
14925 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14926 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 Pointer to the c
14927 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 ell text. */. C
14928 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 ellInfo *pInfo
14929 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 /* Fill i
1492a 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
1492b 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 */.){. u16 n;
1492c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1492d 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 65 73 /* Number bytes
1492e 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 in cell content
1492f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 header */. u32
14930 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 nPayload;
14931 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14932 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 f bytes of cell
14933 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 payload */.. as
14934 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14935 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
14936 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a pBt->mutex) );..
14937 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d pInfo->pCell =
14938 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 pCell;. assert
14939 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 ( pPage->leaf==0
1493a 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d || pPage->leaf=
1493b 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 =1 );. n = pPag
1493c 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b e->childPtrSize;
1493d 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d . assert( n==4-
1493e 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 4*pPage->leaf );
1493f 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e . if( pPage->in
14940 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66 28 20 tKey ){. if(
14941 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 pPage->hasData )
14942 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 {. n += get
14943 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b Varint32(&pCell[
14944 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 n], nPayload);.
14945 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
14946 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 nPayload = 0;.
14947 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 }. n += get
14948 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d Varint(&pCell[n]
14949 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e , (u64*)&pInfo->
1494a 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f nKey);. pInfo
1494b 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f ->nData = nPaylo
1494c 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ad;. }else{.
1494d 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 pInfo->nData =
1494e 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 0;. n += getV
1494f 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e arint32(&pCell[n
14950 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 ], nPayload);.
14951 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 pInfo->nKey =
14952 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 nPayload;. }.
14953 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 pInfo->nPayload
14954 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 = nPayload;. pI
14955 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e nfo->nHeader = n
14956 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e ;. if( likely(n
14957 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e Payload<=pPage->
14958 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20 maxLocal) ){.
14959 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 /* This is the
1495a 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 (easy) common ca
1495b 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e 74 se where the ent
1495c 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 73 ire payload fits
1495d 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c . ** on the l
1495e 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f ocal page. No o
1495f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 69 verflow is requi
14960 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 red.. */.
14961 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 int nSize;
14962 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a /* Total siz
14963 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e e of cell conten
14964 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 t in bytes */.
14965 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f nSize = nPaylo
14966 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66 ad + n;. pInf
14967 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 o->nLocal = (u16
14968 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 )nPayload;. p
14969 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 Info->iOverflow
1496a 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e 53 = 0;. if( (nS
1496b 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a ize & ~3)==0 ){.
1496c 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b nSize = 4;
1496d 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
1496e 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
1496f 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70 4 */. }. p
14970 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 75 Info->nSize = (u
14971 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 16)nSize;. }els
14972 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 e{. /* If the
14973 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f payload will no
14974 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c 79 t fit completely
14975 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 on the local pa
14976 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 ge, we have.
14977 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f 77 ** to decide how
14978 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 6c much to store l
14979 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 6d ocally and how m
1497a 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e 74 uch to spill ont
1497b 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f o. ** overflo
1497c 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 74 w pages. The st
1497d 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 6e rategy is to min
1497e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 imize the amount
1497f 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 2a of unused. *
14980 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 66 * space on overf
14981 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 20 low pages while
14982 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f 75 keeping the amou
14983 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72 nt of local stor
14984 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 65 age. ** in be
14985 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 tween minLocal a
14986 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 20 nd maxLocal..
14987 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e 69 **. ** Warni
14988 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 68 ng: changing th
14989 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 70 e way overflow p
1498a 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 69 ayload is distri
1498b 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 20 buted in any.
1498c 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 73 ** way will res
1498d 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 ult in an incomp
1498e 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 6d atible file form
1498f 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 at.. */. i
14990 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a nt minLocal; /*
14991 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 Minimum amount
14992 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 of payload held
14993 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 locally */. i
14994 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a nt maxLocal; /*
14995 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 Maximum amount
14996 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 of payload held
14997 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 locally */. i
14998 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a nt surplus; /*
14999 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 Overflow payloa
1499a 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 d available for
1499b 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f local storage */
1499c 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d .. minLocal =
1499d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
1499e 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 3d ;. maxLocal =
1499f 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c pPage->maxLocal
149a0 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d 20 ;. surplus =
149a1 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 79 minLocal + (nPay
149a2 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 load - minLocal)
149a3 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 %(pPage->pBt->us
149a4 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 ableSize - 4);.
149a5 20 20 20 69 66 28 20 73 75 72 70 6c 75 73 20 3c if( surplus <
149a6 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 = maxLocal ){.
149a7 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 pInfo->nLoca
149a8 6c 20 3d 20 28 75 31 36 29 73 75 72 70 6c 75 73 l = (u16)surplus
149a9 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
149aa 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c pInfo->nLocal
149ab 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f 63 61 6c = (u16)minLocal
149ac 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 ;. }. pInf
149ad 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28 o->iOverflow = (
149ae 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 u16)(pInfo->nLoc
149af 61 6c 20 2b 20 6e 29 3b 0a 20 20 20 20 70 49 6e al + n);. pIn
149b0 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 49 6e 66 fo->nSize = pInf
149b1 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 2b 20 34 o->iOverflow + 4
149b2 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 ;. }.}.#define
149b3 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c parseCell(pPage,
149b4 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 20 5c iCell, pInfo) \
149b5 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 . sqlite3BtreeP
149b6 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 50 61 arseCellPtr((pPa
149b7 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 28 70 ge), findCell((p
149b8 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 29 2c Page), (iCell)),
149b9 20 28 70 49 6e 66 6f 29 29 0a 53 51 4c 49 54 45 (pInfo)).SQLITE
149ba 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
149bb 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 lite3BtreeParseC
149bc 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a ell(. MemPage *
149bd 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f pPage, /
149be 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
149bf 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 g the cell */.
149c0 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 20 20 int iCell,
149c1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
149c2 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 72 73 ell index. Firs
149c3 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f 0a 20 t cell is 0 */.
149c4 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f CellInfo *pInfo
149c5 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c /* Fill
149c6 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 in this structu
149c7 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 73 65 re */.){. parse
149c8 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c Cell(pPage, iCel
149c9 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a l, pInfo);.}../*
149ca 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 .** Compute the
149cb 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
149cc 62 79 74 65 73 20 74 68 61 74 20 61 20 43 65 6c bytes that a Cel
149cd 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65 20 63 l needs in the c
149ce 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 65 61 ell.** data area
149cf 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d 70 61 of the btree-pa
149d0 67 65 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ge. The return
149d1 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 73 20 number includes
149d2 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 the cell.** data
149d3 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 65 20 header and the
149d4 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62 local payload, b
149d5 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66 ut not any overf
149d6 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 low page or.** t
149d7 68 65 20 73 70 61 63 65 20 75 73 65 64 20 62 79 he space used by
149d8 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 the cell pointe
149d9 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 r..*/.static u16
149da 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d cellSizePtr(Mem
149db 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 Page *pPage, u8
149dc 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 *pCell){. u8 *p
149dd 49 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b 70 50 Iter = &pCell[pP
149de 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
149df 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a 65 3b e];. u32 nSize;
149e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
149e1 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 76 DEBUG. /* The v
149e2 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
149e3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 this function s
149e4 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 hould always be
149e5 74 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a the same as. **
149e6 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e the (CellInfo.n
149e7 53 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75 6e Size) value foun
149e8 64 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75 6c d by doing a ful
149e9 6c 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a 20 l parse of the.
149ea 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c ** cell. If SQL
149eb 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 ITE_DEBUG is def
149ec 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 ined, an assert(
149ed 29 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 ) at the bottom
149ee 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75 6e of. ** this fun
149ef 63 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20 74 ction verifies t
149f0 68 61 74 20 74 68 69 73 20 69 6e 76 61 72 69 61 hat this invaria
149f1 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74 nt is not violat
149f2 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 ed. */. CellInf
149f3 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20 73 o debuginfo;. s
149f4 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
149f5 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
149f6 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e 66 6f Cell, &debuginfo
149f7 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 );.#endif.. if(
149f8 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
149f9 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 0a {. u8 *pEnd;.
149fa 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 if( pPage->h
149fb 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 asData ){.
149fc 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 pIter += getVari
149fd 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a nt32(pIter, nSiz
149fe 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 e);. }else{.
149ff 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 30 3b 0a nSize = 0;.
14a00 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 }.. /* pI
14a01 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61 ter now points a
14a02 74 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74 t the 64-bit int
14a03 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65 2c 20 eger key value,
14a04 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 a variable lengt
14a05 68 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 h . ** intege
14a06 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 r. The following
14a07 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 49 74 block moves pIt
14a08 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 er to point at t
14a09 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 he first byte.
14a0a 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e ** past the en
14a0b 64 20 6f 66 20 74 68 65 20 6b 65 79 20 76 61 6c d of the key val
14a0c 75 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e 64 20 ue. */. pEnd
14a0d 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 20 20 = &pIter[9];.
14a0e 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 2b while( (*pIter+
14a0f 2b 29 26 30 78 38 30 20 26 26 20 70 49 74 65 72 +)&0x80 && pIter
14a10 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c 73 65 <pEnd );. }else
14a11 7b 0a 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67 {. pIter += g
14a12 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 etVarint32(pIter
14a13 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 , nSize);. }..
14a14 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 if( nSize>pPage
14a15 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 ->maxLocal ){.
14a16 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d int minLocal =
14a17 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c pPage->minLocal
14a18 3b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 ;. nSize = mi
14a19 6e 4c 6f 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20 nLocal + (nSize
14a1a 2d 20 6d 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70 - minLocal) % (p
14a1b 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
14a1c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 eSize - 4);.
14a1d 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d if( nSize>pPage-
14a1e 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 >maxLocal ){.
14a1f 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f nSize = minLo
14a20 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e cal;. }. n
14a21 53 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 Size += 4;. }.
14a22 20 6e 53 69 7a 65 20 2b 3d 20 28 70 49 74 65 72 nSize += (pIter
14a23 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a - pCell);.. /*
14a24 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a The minimum siz
14a25 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 73 e of any cell is
14a26 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 4 bytes. */. i
14a27 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 20 f( nSize<4 ){.
14a28 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 7d nSize = 4;. }
14a29 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 7a .. assert( nSiz
14a2a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 e==debuginfo.nSi
14a2b 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 ze );. return (
14a2c 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23 69 66 u16)nSize;.}.#if
14a2d 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 ndef NDEBUG.stat
14a2e 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 28 ic u16 cellSize(
14a2f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
14a30 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72 65 int iCell){. re
14a31 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 turn cellSizePtr
14a32 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c 6c (pPage, findCell
14a33 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29 3b (pPage, iCell));
14a34 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
14a35 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
14a36 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 UTOVACUUM./*.**
14a37 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c If the cell pCel
14a38 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 l, part of page
14a39 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 pPage contains a
14a3a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 pointer.** to a
14a3b 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c n overflow page,
14a3c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79 insert an entry
14a3d 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 into the pointe
14a3e 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 r-map.** for the
14a3f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a overflow page..
14a40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 */.static int pt
14a41 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d rmapPutOvflPtr(M
14a42 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 emPage *pPage, u
14a43 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 8 *pCell){. Cel
14a44 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73 lInfo info;. as
14a45 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 sert( pCell!=0 )
14a46 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
14a47 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 ParseCellPtr(pPa
14a48 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f ge, pCell, &info
14a49 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e );. assert( (in
14a4a 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d fo.nData+(pPage-
14a4b 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e >intKey?0:info.n
14a4c 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 Key))==info.nPay
14a4d 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 28 69 load );. if( (i
14a4e 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 nfo.nData+(pPage
14a4f 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e ->intKey?0:info.
14a50 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 nKey))>info.nLoc
14a51 61 6c 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f al ){. Pgno o
14a52 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 vfl = get4byte(&
14a53 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 pCell[info.iOver
14a54 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 72 65 74 75 flow]);. retu
14a55 72 6e 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 rn ptrmapPut(pPa
14a56 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 ge->pBt, ovfl, P
14a57 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c TRMAP_OVERFLOW1,
14a58 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 pPage->pgno);.
14a59 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
14a5a 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49 TE_OK;.}./*.** I
14a5b 66 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 f the cell with
14a5c 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70 index iCell on p
14a5d 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 age pPage contai
14a5e 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 ns a pointer.**
14a5f 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 to an overflow p
14a60 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 age, insert an e
14a61 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f ntry into the po
14a62 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 inter-map.** for
14a63 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
14a64 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ge..*/.static in
14a65 74 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 t ptrmapPutOvfl(
14a66 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
14a67 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 int iCell){. u8
14a68 20 2a 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 *pCell;. asser
14a69 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14a6a 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
14a6b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 ->mutex) );. pC
14a6c 65 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c ell = findOverfl
14a6d 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 owCell(pPage, iC
14a6e 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ell);. return p
14a6f 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 trmapPutOvflPtr(
14a70 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d pPage, pCell);.}
14a71 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
14a72 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 Defragment the p
14a73 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 age given. All
14a74 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 Cells are moved
14a75 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 to the.** end of
14a76 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c the page and al
14a77 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73 20 l free space is
14a78 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f collected into o
14a79 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c ne.** big FreeBl
14a7a 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e k that occurs in
14a7b 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 61 between the hea
14a7c 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 der and cell.**
14a7d 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e pointer array an
14a7e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 d the cell conte
14a7f 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 nt area..*/.stat
14a80 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 6e ic int defragmen
14a81 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 tPage(MemPage *p
14a82 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 Page){. int i;
14a83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a84 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
14a85 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b ter */. int pc;
14a86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a87 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f /* Address o
14a88 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f f a i-th cell */
14a89 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 . int addr;
14a8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14a8b 20 4f 66 66 73 65 74 20 6f 66 20 66 69 72 73 74 Offset of first
14a8c 20 62 79 74 65 20 61 66 74 65 72 20 63 65 6c 6c byte after cell
14a8d 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a pointer array *
14a8e 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 /. int hdr;
14a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14a90 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 * Offset to the
14a91 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 page header */.
14a92 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 int size;
14a93 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
14a94 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f ize of a cell */
14a95 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a . int usableSiz
14a96 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
14a97 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c Number of usabl
14a98 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 e bytes on a pag
14a99 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f e */. int cellO
14a9a 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
14a9b 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 /* Offset to t
14a9c 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
14a9d 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 array */. int c
14a9e 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 brk;
14a9f 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
14aa0 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 to the cell cont
14aa1 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e ent area */. in
14aa2 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 t nCell;
14aa3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
14aa4 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 er of cells on t
14aa5 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 he page */. uns
14aa6 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
14aa7 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 ; /* The p
14aa8 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e age data */. un
14aa9 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d signed char *tem
14aaa 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 p; /* Temp
14aab 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 area for cell c
14aac 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 ontent */.. ass
14aad 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
14aae 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
14aaf 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
14ab0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
14ab1 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 >pBt!=0 );. ass
14ab2 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d ert( pPage->pBt-
14ab3 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 >usableSize <= S
14ab4 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
14ab5 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 IZE );. assert(
14ab6 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
14ab7 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 w==0 );. assert
14ab8 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
14ab9 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
14aba 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d >mutex) );. tem
14abb 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 p = sqlite3Pager
14abc 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d TempSpace(pPage-
14abd 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 >pBt->pPager);.
14abe 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
14abf 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 Data;. hdr = pP
14ac0 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
14ac1 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 cellOffset = p
14ac2 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
14ac3 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 ;. nCell = pPag
14ac4 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 e->nCell;. asse
14ac5 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 rt( nCell==get2b
14ac6 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
14ac7 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a ) );. usableSiz
14ac8 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e e = pPage->pBt->
14ac9 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 usableSize;. cb
14aca 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 rk = get2byte(&d
14acb 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d ata[hdr+5]);. m
14acc 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b emcpy(&temp[cbrk
14acd 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 ], &data[cbrk],
14ace 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 usableSize - cbr
14acf 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 k);. cbrk = usa
14ad0 62 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 bleSize;. for(i
14ad1 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b =0; i<nCell; i++
14ad2 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 ){. u8 *pAddr
14ad3 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 ; /* The i-t
14ad4 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a h cell pointer *
14ad5 2f 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 /. pAddr = &d
14ad6 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b ata[cellOffset +
14ad7 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 i*2];. pc =
14ad8 67 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b get2byte(pAddr);
14ad9 0a 20 20 20 20 69 66 28 20 70 63 3e 3d 75 73 61 . if( pc>=usa
14ada 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 bleSize ){.
14adb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
14adc 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
14add 20 7d 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65 }. size = ce
14ade 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
14adf 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 &temp[pc]);.
14ae0 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 20 cbrk -= size;.
14ae1 20 20 20 69 66 28 20 63 62 72 6b 3c 63 65 6c 6c if( cbrk<cell
14ae2 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 7c Offset+2*nCell |
14ae3 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 | pc+size>usable
14ae4 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65 Size ){. re
14ae5 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
14ae6 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
14ae7 20 20 20 20 61 73 73 65 72 74 28 20 63 62 72 6b assert( cbrk
14ae8 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a +size<=usableSiz
14ae9 65 20 26 26 20 63 62 72 6b 3e 3d 30 20 29 3b 0a e && cbrk>=0 );.
14aea 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 memcpy(&data
14aeb 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 [cbrk], &temp[pc
14aec 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 ], size);. pu
14aed 74 32 62 79 74 65 28 70 41 64 64 72 2c 20 63 62 t2byte(pAddr, cb
14aee 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rk);. }. asser
14aef 74 28 20 63 62 72 6b 3e 3d 63 65 6c 6c 4f 66 66 t( cbrk>=cellOff
14af0 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 set+2*nCell );.
14af1 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
14af2 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 hdr+5], cbrk);.
14af3 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 data[hdr+1] = 0
14af4 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 ;. data[hdr+2]
14af5 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b = 0;. data[hdr+
14af6 37 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d 7] = 0;. addr =
14af7 20 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 cellOffset+2*nC
14af8 65 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 ell;. memset(&d
14af9 61 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20 63 62 ata[addr], 0, cb
14afa 72 6b 2d 61 64 64 72 29 3b 0a 20 20 61 73 73 65 rk-addr);. asse
14afb 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
14afc 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
14afd 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
14afe 20 69 66 28 20 63 62 72 6b 2d 61 64 64 72 21 3d if( cbrk-addr!=
14aff 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a pPage->nFree ){.
14b00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14b01 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
14b02 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
14b03 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
14b04 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 Allocate nByte
14b05 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 bytes of space f
14b06 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42 rom within the B
14b07 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 65 -Tree page passe
14b08 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 d.** as the firs
14b09 74 20 61 72 67 75 6d 65 6e 74 2e 20 52 65 74 75 t argument. Retu
14b0a 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 rn the index int
14b0b 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d o pPage->aData[]
14b0c 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 of the .** firs
14b0d 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61 t byte of alloca
14b0e 74 65 64 20 73 70 61 63 65 2e 20 0a 2a 2a 0a 2a ted space. .**.*
14b0f 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 * The caller gua
14b10 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 rantees that the
14b11 20 73 70 61 63 65 20 62 65 74 77 65 65 6e 20 74 space between t
14b12 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 63 65 he end of the ce
14b13 6c 6c 2d 6f 66 66 73 65 74 20 0a 2a 2a 20 61 72 ll-offset .** ar
14b14 72 61 79 20 61 6e 64 20 74 68 65 20 73 74 61 72 ray and the star
14b15 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f t of the cell-co
14b16 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 61 74 ntent area is at
14b17 20 6c 65 61 73 74 20 6e 42 79 74 65 20 62 79 74 least nByte byt
14b18 65 73 0a 2a 2a 20 69 6e 20 73 69 7a 65 2e 20 53 es.** in size. S
14b19 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 o this routine c
14b1a 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a 2a an never fail..*
14b1b 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 *.** If there ar
14b1c 65 20 61 6c 72 65 61 64 79 20 36 30 20 6f 72 20 e already 60 or
14b1d 6d 6f 72 65 20 62 79 74 65 73 20 6f 66 20 66 72 more bytes of fr
14b1e 61 67 6d 65 6e 74 73 20 77 69 74 68 69 6e 20 74 agments within t
14b1f 68 65 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 20 he page,.** the
14b20 70 61 67 65 20 69 73 20 64 65 66 72 61 67 6d 65 page is defragme
14b21 6e 74 65 64 20 62 65 66 6f 72 65 20 72 65 74 75 nted before retu
14b22 72 6e 69 6e 67 2e 20 49 66 20 74 68 69 73 20 77 rning. If this w
14b23 65 72 65 20 6e 6f 74 20 64 6f 6e 65 20 74 68 65 ere not done the
14b24 72 65 0a 2a 2a 20 69 73 20 61 20 63 68 61 6e 63 re.** is a chanc
14b25 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 e that the numbe
14b26 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 r of fragmented
14b27 62 79 74 65 73 20 63 6f 75 6c 64 20 65 76 65 6e bytes could even
14b28 74 75 61 6c 6c 79 20 0a 2a 2a 20 6f 76 65 72 66 tually .** overf
14b29 6c 6f 77 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 low the single-b
14b2a 79 74 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65 yte field of the
14b2b 20 70 61 67 65 2d 68 65 61 64 65 72 20 69 6e 20 page-header in
14b2c 77 68 69 63 68 20 74 68 69 73 20 76 61 6c 75 65 which this value
14b2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e 0a 2a .** is stored..*
14b2e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c /.static int all
14b2f 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 ocateSpace(MemPa
14b30 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e ge *pPage, int n
14b31 42 79 74 65 29 7b 0a 20 20 63 6f 6e 73 74 20 69 Byte){. const i
14b32 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e nt hdr = pPage->
14b33 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a hdrOffset; /*
14b34 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 Local cache of
14b35 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
14b36 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 */. u8 * const
14b37 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
14b38 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c 6f Data; /* Lo
14b39 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61 cal cache of pPa
14b3a 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 69 ge->aData */. i
14b3b 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20 20 nt nFrag;
14b3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b3d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14b3e 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 fragmented byte
14b3f 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 s on pPage */.
14b40 69 6e 74 20 74 6f 70 3b 0a 20 20 0a 20 20 61 73 int top;. . as
14b41 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
14b42 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
14b43 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
14b44 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
14b45 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 ->pBt );. asser
14b46 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14b47 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
14b48 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
14b49 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 sert( nByte>=0 )
14b4a 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 ; /* Minimum ce
14b4b 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a ll size is 4 */.
14b4c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
14b4d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b >nFree>=nByte );
14b4e 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
14b4f 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 ->nOverflow==0 )
14b50 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 ;.. /* Assert t
14b51 68 61 74 20 74 68 65 20 73 70 61 63 65 20 62 65 hat the space be
14b52 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 2d 6f tween the cell-o
14b53 66 66 73 65 74 20 61 72 72 61 79 20 61 6e 64 20 ffset array and
14b54 74 68 65 20 0a 20 20 2a 2a 20 63 65 6c 6c 2d 63 the . ** cell-c
14b55 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 67 ontent area is g
14b56 72 65 61 74 65 72 20 74 68 61 6e 20 6e 42 79 74 reater than nByt
14b57 65 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 e bytes.. */.
14b58 61 73 73 65 72 74 28 20 6e 42 79 74 65 20 3c 3d assert( nByte <=
14b59 20 28 0a 20 20 20 20 20 20 67 65 74 32 62 79 74 (. get2byt
14b5a 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 2d e(&data[hdr+5])-
14b5b 28 68 64 72 2b 38 2b 28 70 50 61 67 65 2d 3e 6c (hdr+8+(pPage->l
14b5c 65 61 66 3f 30 3a 34 29 2b 32 2a 67 65 74 32 62 eaf?0:4)+2*get2b
14b5d 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
14b5e 29 29 0a 20 20 29 29 3b 0a 0a 20 20 70 50 61 67 )). ));.. pPag
14b5f 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 75 31 36 e->nFree -= (u16
14b60 29 6e 42 79 74 65 3b 0a 20 20 6e 46 72 61 67 20 )nByte;. nFrag
14b61 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 = data[hdr+7];.
14b62 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20 29 if( nFrag>=60 )
14b63 7b 0a 20 20 20 20 64 65 66 72 61 67 6d 65 6e 74 {. defragment
14b64 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d Page(pPage);. }
14b65 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 65 61 else{. /* Sea
14b66 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 rch the freelist
14b67 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 looking for a f
14b68 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f ree slot big eno
14b69 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a ugh to satisfy .
14b6a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 ** the reque
14b6b 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 st. The allocati
14b6c 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20 on is made from
14b6d 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 73 the first free s
14b6e 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74 lot in . ** t
14b6f 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 73 20 he list that is
14b70 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 large enough to
14b71 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20 accomadate it..
14b72 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63 */. int pc
14b73 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28 , addr;. for(
14b74 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20 addr=hdr+1; (pc
14b75 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
14b76 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72 [addr]))>0; addr
14b77 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =pc){. int
14b78 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 size = get2byte(
14b79 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20 &data[pc+2]);
14b7a 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65 /* Size of fre
14b7b 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20 e slot */.
14b7c 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 if( size>=nByte
14b7d 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 ){. int x
14b7e 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b = size - nByte;
14b7f 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 3c 34 . if( x<4
14b80 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
14b81 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c 6f 74 Remove the slot
14b82 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c from the free-l
14b83 69 73 74 2e 20 55 70 64 61 74 65 20 74 68 65 20 ist. Update the
14b84 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 number of.
14b85 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e 74 65 ** fragmente
14b86 64 20 62 79 74 65 73 20 77 69 74 68 69 6e 20 74 d bytes within t
14b87 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 he page. */.
14b88 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 memcpy(&da
14b89 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 61 5b ta[addr], &data[
14b8a 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 pc], 2);.
14b8b 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d data[hdr+7] =
14b8c 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20 78 29 (u8)(nFrag + x)
14b8d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
14b8e 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
14b8f 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73 20 6f e slot remains o
14b90 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e n the free-list.
14b91 20 52 65 64 75 63 65 20 69 74 73 20 73 69 7a 65 Reduce its size
14b92 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20 20 20 to account.
14b93 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 ** for the
14b94 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 62 79 portion used by
14b95 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 the new allocat
14b96 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ion. */.
14b97 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
14b98 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 [pc+2], x);.
14b99 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65 }. re
14b9a 74 75 72 6e 20 70 63 20 2b 20 78 3b 0a 20 20 20 turn pc + x;.
14b9b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
14b9c 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 /* Allocate me
14b9d 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 mory from the ga
14b9e 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 p in between the
14b9f 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
14ba0 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 ray. ** and the
14ba1 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
14ba2 65 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 3d ea.. */. top =
14ba3 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
14ba4 68 64 72 2b 35 5d 29 20 2d 20 6e 42 79 74 65 3b hdr+5]) - nByte;
14ba5 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
14ba6 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a a[hdr+5], top);.
14ba7 20 20 72 65 74 75 72 6e 20 74 6f 70 3b 0a 7d 0a return top;.}.
14ba8 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
14ba9 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 section of the p
14baa 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f 20 74 Page->aData to t
14bab 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 20 he freelist..**
14bac 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f The first byte o
14bad 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 20 62 f the new free b
14bae 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d 3e 61 lock is pPage->a
14baf 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20 61 Disk[start].** a
14bb0 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 nd the size of t
14bb1 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 69 7a he block is "siz
14bb2 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 e" bytes..**.**
14bb3 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66 6f Most of the effo
14bb4 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f 6c rt here is invol
14bb5 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e 67 ved in coalesing
14bb6 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72 65 adjacent.** fre
14bb7 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 20 e blocks into a
14bb8 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65 20 single big free
14bb9 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 block..*/.static
14bba 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28 4d int freeSpace(M
14bbb 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
14bbc 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73 69 nt start, int si
14bbd 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c ze){. int addr,
14bbe 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20 20 pbegin, hdr;.
14bbf 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 unsigned char *d
14bc0 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ata = pPage->aDa
14bc1 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 ta;.. assert( p
14bc2 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a Page->pBt!=0 );.
14bc3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14bc4 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
14bc5 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
14bc6 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
14bc7 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 tart>=pPage->hdr
14bc8 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 67 65 2d Offset+6+(pPage-
14bc9 3e 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a 20 20 >leaf?0:4) );.
14bca 61 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b assert( (start +
14bcb 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 size)<=pPage->p
14bcc 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
14bcd 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14bce 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14bcf 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
14bd0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
14bd1 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d ize>=0 ); /* M
14bd2 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
14bd3 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66 is 4 */..#ifdef
14bd4 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
14bd5 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77 ELETE. /* Overw
14bd6 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 rite deleted inf
14bd7 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 ormation with ze
14bd8 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43 ros when the SEC
14bd9 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a URE_DELETE . **
14bda 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c option is enabl
14bdb 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 ed at compile-ti
14bdc 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 me */. memset(&
14bdd 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20 data[start], 0,
14bde 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 size);.#endif..
14bdf 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 /* Add the spac
14be0 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 e back into the
14be1 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 linked list of f
14be2 72 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 68 reeblocks */. h
14be3 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
14be4 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 ffset;. addr =
14be5 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 hdr + 1;. while
14be6 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 ( (pbegin = get2
14be7 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d byte(&data[addr]
14be8 29 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 ))<start && pbeg
14be9 69 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 in>0 ){. asse
14bea 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 rt( pbegin<=pPag
14beb 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
14bec 7a 65 2d 34 20 29 3b 0a 20 20 20 20 69 66 28 20 ze-4 );. if(
14bed 70 62 65 67 69 6e 3c 3d 61 64 64 72 20 29 20 7b pbegin<=addr ) {
14bee 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
14bef 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
14bf0 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 T;. }. add
14bf1 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a r = pbegin;. }.
14bf2 20 20 69 66 20 28 20 70 62 65 67 69 6e 3e 70 50 if ( pbegin>pP
14bf3 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
14bf4 53 69 7a 65 2d 34 20 29 20 7b 0a 20 20 20 20 72 Size-4 ) {. r
14bf5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
14bf6 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
14bf7 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3e assert( pbegin>
14bf8 61 64 64 72 20 7c 7c 20 70 62 65 67 69 6e 3d 3d addr || pbegin==
14bf9 30 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 0 );. put2byte(
14bfa 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 73 74 61 &data[addr], sta
14bfb 72 74 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 rt);. put2byte(
14bfc 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 70 62 &data[start], pb
14bfd 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 62 79 74 egin);. put2byt
14bfe 65 28 26 64 61 74 61 5b 73 74 61 72 74 2b 32 5d e(&data[start+2]
14bff 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 , size);. pPage
14c00 2d 3e 6e 46 72 65 65 20 2b 3d 20 28 75 31 36 29 ->nFree += (u16)
14c01 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 61 6c size;.. /* Coal
14c02 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 esce adjacent fr
14c03 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 61 ee blocks */. a
14c04 64 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 ddr = pPage->hdr
14c05 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 20 77 68 Offset + 1;. wh
14c06 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 ile( (pbegin = g
14c07 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 et2byte(&data[ad
14c08 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 dr]))>0 ){. i
14c09 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 7a 65 2c nt pnext, psize,
14c0a 20 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 x;. assert(
14c0b 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 pbegin>addr );.
14c0c 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 assert( pbegi
14c0d 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 n<=pPage->pBt->u
14c0e 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 sableSize-4 );.
14c0f 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62 pnext = get2b
14c10 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
14c11 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20 ]);. psize =
14c12 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
14c13 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 begin+2]);. i
14c14 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a f( pbegin + psiz
14c15 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26 e + 3 >= pnext &
14c16 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 & pnext>0 ){.
14c17 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e int frag = pn
14c18 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73 ext - (pbegin+ps
14c19 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ize);. if(
14c1a 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 72 61 (frag<0) || (fra
14c1b 67 3e 28 69 6e 74 29 64 61 74 61 5b 70 50 61 67 g>(int)data[pPag
14c1c 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d 29 e->hdrOffset+7])
14c1d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
14c1e 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
14c1f 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a T_BKPT;. }.
14c20 20 20 20 20 20 20 64 61 74 61 5b 70 50 61 67 65 data[pPage
14c21 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d 20 2d ->hdrOffset+7] -
14c22 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20 20 = (u8)frag;.
14c23 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28 26 x = get2byte(&
14c24 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20 20 data[pnext]);.
14c25 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
14c26 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b 0a ta[pbegin], x);.
14c27 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 20 x = pnext
14c28 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 + get2byte(&data
14c29 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 65 [pnext+2]) - pbe
14c2a 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32 62 gin;. put2b
14c2b 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
14c2c 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65 6c +2], x);. }el
14c2d 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 3d se{. addr =
14c2e 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a 20 pbegin;. }.
14c2f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
14c30 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
14c31 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 20 a begins with a
14c32 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76 freeblock, remov
14c33 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 64 e it. */. if( d
14c34 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 61 ata[hdr+1]==data
14c35 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 5b [hdr+5] && data[
14c36 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 72 hdr+2]==data[hdr
14c37 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 74 +6] ){. int t
14c38 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 3d op;. pbegin =
14c39 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
14c3a 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d hdr+1]);. mem
14c3b 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 5d cpy(&data[hdr+1]
14c3c 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c , &data[pbegin],
14c3d 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 2);. top = g
14c3e 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
14c3f 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74 65 r+5]) + get2byte
14c40 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d (&data[pbegin+2]
14c41 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 );. put2byte(
14c42 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f &data[hdr+5], to
14c43 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 p);. }. assert
14c44 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
14c45 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
14c46 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 72 >pDbPage) );. r
14c47 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
14c48 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 .}../*.** Decode
14c49 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 the flags byte
14c4a 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 (the first byte
14c4b 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 66 of the header) f
14c4c 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e 64 or a page.** and
14c4d 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 6c initialize fiel
14c4e 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67 ds of the MemPag
14c4f 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 6f e structure acco
14c50 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f rdingly..**.** O
14c51 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e nly the followin
14c52 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 61 g combinations a
14c53 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 41 re supported. A
14c54 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 6e nything differen
14c55 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 61 t.** indicates a
14c56 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
14c57 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 e files:.**.**
14c58 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f 44 PTF_ZEROD
14c59 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 ATA.** P
14c5a 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 54 TF_ZERODATA | PT
14c5b 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 20 F_LEAF.**
14c5c 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c PTF_LEAFDATA |
14c5d 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 PTF_INTKEY.**
14c5e 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 PTF_LEAFD
14c5f 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 ATA | PTF_INTKEY
14c60 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 | PTF_LEAF.*/.s
14c61 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 65 tatic int decode
14c62 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a 70 Flags(MemPage *p
14c63 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 79 Page, int flagBy
14c64 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 te){. BtShared
14c65 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 63 *pBt; /* A c
14c66 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 42 opy of pPage->pB
14c67 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
14c68 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
14c69 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d ==(pPage->pgno==
14c6a 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b 0a 1 ? 100 : 0) );.
14c6b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14c6c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
14c6d 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
14c6e 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 66 );. pPage->leaf
14c6f 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 65 = (u8)(flagByte
14c70 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 50 >>3); assert( P
14c71 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 20 TF_LEAF == 1<<3
14c72 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 3d );. flagByte &=
14c73 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 50 ~PTF_LEAF;. pP
14c74 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
14c75 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e 6c e = 4-4*pPage->l
14c76 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 eaf;. pBt = pPa
14c77 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66 ge->pBt;. if( f
14c78 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c 45 lagByte==(PTF_LE
14c79 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 AFDATA | PTF_INT
14c7a 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 67 KEY) ){. pPag
14c7b 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 e->intKey = 1;.
14c7c 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 pPage->hasDat
14c7d 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b a = pPage->leaf;
14c7e 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c . pPage->maxL
14c7f 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c ocal = pBt->maxL
14c80 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e eaf;. pPage->
14c81 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e minLocal = pBt->
14c82 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65 minLeaf;. }else
14c83 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 50 if( flagByte==P
14c84 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20 TF_ZERODATA ){.
14c85 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 pPage->intKey
14c86 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d = 0;. pPage-
14c87 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 20 >hasData = 0;.
14c88 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 pPage->maxLoca
14c89 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 l = pBt->maxLoca
14c8a 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 l;. pPage->mi
14c8b 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 nLocal = pBt->mi
14c8c 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b nLocal;. }else{
14c8d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
14c8e 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
14c8f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
14c90 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
14c91 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
14c92 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 auxiliary infor
14c93 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 73 mation for a dis
14c94 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 k block..**.** R
14c95 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
14c96 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 on success. If
14c97 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65 20 we see that the
14c98 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 page does.** not
14c99 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d contain a well-
14c9a 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 formed database
14c9b 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 page, then retur
14c9c 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 n .** SQLITE_COR
14c9d 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74 RUPT. Note that
14c9e 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c a return of SQL
14c9f 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a ITE_OK does not.
14ca0 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 ** guarantee tha
14ca1 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77 65 t the page is we
14ca2 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f ll-formed. It o
14ca3 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a 2a nly shows that.*
14ca4 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 64 * we failed to d
14ca5 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75 70 etect any corrup
14ca6 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
14ca7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14ca8 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 te3BtreeInitPage
14ca9 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
14caa 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 {.. assert( pPa
14cab 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 ge->pBt!=0 );.
14cac 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14cad 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
14cae 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
14caf 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
14cb0 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 ->pgno==sqlite3P
14cb1 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70 agerPagenumber(p
14cb2 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
14cb3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
14cb4 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 e == sqlite3Page
14cb5 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d rGetExtra(pPage-
14cb6 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
14cb7 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 ssert( pPage->aD
14cb8 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 ata == sqlite3Pa
14cb9 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 gerGetData(pPage
14cba 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
14cbb 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 73 49 if( !pPage->isI
14cbc 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 20 70 nit ){. u16 p
14cbd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a c; /*
14cbe 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 66 72 Address of a fr
14cbf 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 eeblock within p
14cc0 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f Page->aData[] */
14cc1 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 20 20 . u8 hdr;
14cc2 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
14cc3 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f t to beginning o
14cc4 66 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f f page header */
14cc5 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b 20 20 . u8 *data;
14cc6 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61 6c /* Equal
14cc7 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 to pPage->aData
14cc8 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 65 64 */. BtShared
14cc9 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 2f 2a *pBt; /*
14cca 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 65 20 The main btree
14ccb 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 structure */.
14ccc 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b u16 usableSize;
14ccd 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 /* Amount of
14cce 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 6f 6e usable space on
14ccf 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 each page */.
14cd0 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 u16 cellOffset
14cd1 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 ; /* Offset f
14cd2 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 61 67 rom start of pag
14cd3 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 e to first cell
14cd4 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 pointer */. u
14cd5 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 16 nFree;
14cd6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
14cd7 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e 20 74 nused bytes on t
14cd8 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 he page */. u
14cd9 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 16 top;
14cda 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
14cdb 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
14cdc 65 6e 74 20 61 72 65 61 20 2a 2f 0a 0a 20 20 20 ent area */..
14cdd 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 pBt = pPage->pB
14cde 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50 t;.. hdr = pP
14cdf 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
14ce0 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 data = pPage
14ce1 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 ->aData;. if(
14ce2 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 decodeFlags(pPa
14ce3 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 ge, data[hdr]) )
14ce4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
14ce5 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
14ce6 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 assert( pBt->pa
14ce7 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 geSize>=512 && p
14ce8 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 Bt->pageSize<=32
14ce9 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65 768 );. pPage
14cea 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74 ->maskPage = pBt
14ceb 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a ->pageSize - 1;.
14cec 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 pPage->nOver
14ced 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 flow = 0;. us
14cee 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e ableSize = pBt->
14cef 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 usableSize;.
14cf0 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 pPage->cellOffse
14cf1 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d t = cellOffset =
14cf2 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 hdr + 12 - 4*pP
14cf3 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74 age->leaf;. t
14cf4 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 op = get2byte(&d
14cf5 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 ata[hdr+5]);.
14cf6 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 pPage->nCell =
14cf7 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
14cf8 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 dr+3]);. if(
14cf9 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f pPage->nCell>MX_
14cfa 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 CELL(pBt) ){.
14cfb 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 /* To many ce
14cfc 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 lls for a single
14cfd 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 page. The page
14cfe 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 must be corrupt
14cff 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
14d00 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
14d01 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 BKPT;. }. .
14d02 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 /* Compute th
14d03 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 e total free spa
14d04 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a ce on the page *
14d05 2f 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 /. pc = get2b
14d06 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
14d07 29 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 64 );. nFree = d
14d08 61 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 ata[hdr+7] + top
14d09 20 2d 20 28 63 65 6c 6c 4f 66 66 73 65 74 20 2b - (cellOffset +
14d0a 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 2*pPage->nCell)
14d0b 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 63 3e ;. while( pc>
14d0c 30 20 29 7b 0a 20 20 20 20 20 20 75 31 36 20 6e 0 ){. u16 n
14d0d 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 ext, size;.
14d0e 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69 if( pc>usableSi
14d0f 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 ze-4 ){.
14d10 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 20 69 73 /* Free block is
14d11 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 2a 2f off the page */
14d12 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
14d13 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
14d14 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 KPT; . }.
14d15 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 32 62 next = get2b
14d16 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a yte(&data[pc]);.
14d17 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 size = get
14d18 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 2byte(&data[pc+2
14d19 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 ]);. if( ne
14d1a 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 xt>0 && next<=pc
14d1b 2b 73 69 7a 65 2b 33 20 29 7b 0a 20 20 20 20 20 +size+3 ){.
14d1c 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b /* Free block
14d1d 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61 63 63 s must be in acc
14d1e 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a ending order */.
14d1f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
14d20 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
14d21 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 PT; . }.
14d22 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 69 7a 65 nFree += size
14d23 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e 65 78 ;. pc = nex
14d24 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 t;. }. pPa
14d25 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31 36 ge->nFree = (u16
14d26 29 6e 46 72 65 65 3b 0a 20 20 20 20 69 66 28 20 )nFree;. if(
14d27 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53 69 7a nFree>=usableSiz
14d28 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 72 e ){. /* Fr
14d29 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f 74 20 ee space cannot
14d2a 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70 61 67 exceed total pag
14d2b 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 e size */.
14d2c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
14d2d 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 RRUPT_BKPT; .
14d2e 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a 20 43 }..#if 0. /* C
14d2f 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20 74 68 heck that all th
14d30 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74 68 65 e offsets in the
14d31 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 72 72 cell offset arr
14d32 61 79 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 ay are within ra
14d33 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20 2a 2a nge. . ** . **
14d34 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73 20 63 Omitting this c
14d35 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b onsistency check
14d36 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65 20 70 and using the p
14d37 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 6d Page->maskPage m
14d38 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72 65 76 ask. ** to prev
14d39 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e 67 20 ent overrunning
14d3a 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72 20 the page buffer
14d3b 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20 72 65 in findCell() re
14d3c 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a 2a 20 sults in a. **
14d3d 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e 63 65 2.5% performance
14d3e 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20 7b 0a gain.. */. {.
14d3f 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20 20 20 u8 *pOff;
14d40 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
14d41 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 61 used to check a
14d42 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 ll cell offsets
14d43 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a 2f 0a are in range */.
14d44 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20 20 20 u8 *pEnd;
14d45 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
14d46 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c 20 6f to end of cell o
14d47 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f 0a 20 ffset array */.
14d48 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20 20 20 u8 mask;
14d49 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 62 /* Mask of b
14d4a 69 74 73 20 74 68 61 74 20 6d 75 73 74 20 62 65 its that must be
14d4b 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f 66 20 zero in MSB of
14d4c 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a 2f 0a cell offsets */.
14d4d 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28 28 75 mask = ~(((u
14d4e 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 8)(pBt->pageSize
14d4f 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20 70 45 >>8))-1);. pE
14d50 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f nd = &data[cellO
14d51 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d 3e 6e ffset + pPage->n
14d52 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66 6f 72 Cell*2];. for
14d53 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65 6c 6c (pOff=&data[cell
14d54 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21 3d 70 Offset]; pOff!=p
14d55 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66 66 29 End && !((*pOff)
14d56 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d 32 29 &mask); pOff+=2)
14d57 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66 21 3d ;. if( pOff!=
14d58 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 72 65 pEnd ){. re
14d59 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
14d5a 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
14d5b 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
14d5c 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
14d5d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
14d5e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
14d5f 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 .** Set up a raw
14d60 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 page so that it
14d61 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 looks like a da
14d62 74 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 tabase page hold
14d63 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 ing.** no entrie
14d64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
14d65 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 d zeroPage(MemPa
14d66 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 ge *pPage, int f
14d67 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 lags){. unsigne
14d68 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 d char *data = p
14d69 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 Page->aData;. B
14d6a 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
14d6b 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 Page->pBt;. u8
14d6c 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
14d6d 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69 Offset;. u16 fi
14d6e 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rst;.. assert(
14d6f 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
14d70 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 number(pPage->pD
14d71 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 bPage)==pPage->p
14d72 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 gno );. assert(
14d73 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
14d74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 Extra(pPage->pDb
14d75 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 Page) == (void*)
14d76 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 pPage );. asser
14d77 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
14d78 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 etData(pPage->pD
14d79 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 bPage) == data )
14d7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14d7b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
14d7c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
14d7d 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
14d7e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14d7f 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
14d80 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64 );. /*memset(&d
14d81 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 ata[hdr], 0, pBt
14d82 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 ->usableSize - h
14d83 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64 dr);*/. data[hd
14d84 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 67 73 r] = (char)flags
14d85 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64 72 20 ;. first = hdr
14d86 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 73 26 + 8 + 4*((flags&
14d87 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a PTF_LEAF)==0 ?1:
14d88 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 0);. memset(&da
14d89 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 ta[hdr+1], 0, 4)
14d8a 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 ;. data[hdr+7]
14d8b 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 = 0;. put2byte(
14d8c 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 &data[hdr+5], pB
14d8d 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a t->usableSize);.
14d8e 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d pPage->nFree =
14d8f 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
14d90 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f - first;. deco
14d91 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 deFlags(pPage, f
14d92 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e lags);. pPage->
14d93 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b hdrOffset = hdr;
14d94 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 . pPage->cellOf
14d95 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 fset = first;.
14d96 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
14d97 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
14d98 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 pBt->pageSize>=5
14d99 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 12 && pBt->pageS
14d9a 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 ize<=32768 );.
14d9b 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 pPage->maskPage
14d9c 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
14d9d 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 - 1;. pPage->nC
14d9e 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 ell = 0;. pPage
14d9f 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a ->isInit = 1;.}.
14da0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
14da1 61 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65 a DbPage obtaine
14da2 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 d from the pager
14da3 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 into a MemPage
14da4 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 used by.** the b
14da5 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 tree layer..*/.s
14da6 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 tatic MemPage *b
14da7 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 treePageFromDbPa
14da8 67 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 ge(DbPage *pDbPa
14da9 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 ge, Pgno pgno, B
14daa 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
14dab 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
14dac 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 = (MemPage*)sqli
14dad 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
14dae 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 (pDbPage);. pPa
14daf 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 ge->aData = sqli
14db0 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
14db1 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 pDbPage);. pPag
14db2 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62 e->pDbPage = pDb
14db3 50 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 Page;. pPage->p
14db4 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 Bt = pBt;. pPag
14db5 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a e->pgno = pgno;.
14db6 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 pPage->hdrOffs
14db7 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f et = pPage->pgno
14db8 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 ==1 ? 100 : 0;.
14db9 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a return pPage; .
14dba 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 }../*.** Get a p
14dbb 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
14dbc 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 er. Initialize
14dbd 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 the MemPage.pBt
14dbe 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 and.** MemPage.a
14dbf 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 Data elements if
14dc0 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 needed..**.** I
14dc1 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 f the noContent
14dc2 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20 flag is set, it
14dc3 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f means that we do
14dc4 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a not care about.
14dc5 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f ** the content o
14dc6 66 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68 f the page at th
14dc7 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 is time. So do
14dc8 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69 not go to the di
14dc9 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 sk.** to fetch t
14dca 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 he content. Jus
14dcb 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f t fill in the co
14dcc 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 ntent with zeros
14dcd 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 for now..** If
14dce 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65 in the future we
14dcf 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 call sqlite3Pag
14dd0 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69 erWrite() on thi
14dd1 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 s page, that.**
14dd2 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74 means we have st
14dd3 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 arted to be conc
14dd4 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 erned about cont
14dd5 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b ent and the disk
14dd6 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20 .** read should
14dd7 6f 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f occur at that po
14dd8 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 int..*/.SQLITE_P
14dd9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
14dda 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 0a e3BtreeGetPage(.
14ddb 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c BtShared *pBt,
14ddc 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
14ddd 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ree */. Pgno pg
14dde 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a no, /*
14ddf 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 Number of the p
14de0 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a age to fetch */.
14de1 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 MemPage **ppPa
14de2 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e ge, /* Return
14de3 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 the page in thi
14de4 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 s parameter */.
14de5 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 int noContent
14de6 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 /* Do not
14de7 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e load page conten
14de8 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a t if true */.){.
14de9 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 int rc;. DbPa
14dea 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 ge *pDbPage;..
14deb 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14dec 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
14ded 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
14dee 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
14def 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 uire(pBt->pPager
14df0 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a , pgno, (DbPage*
14df1 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f *)&pDbPage, noCo
14df2 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 ntent);. if( rc
14df3 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
14df4 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 *ppPage = btreeP
14df5 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 ageFromDbPage(pD
14df6 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 bPage, pgno, pBt
14df7 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
14df8 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
14df9 52 65 74 72 69 65 76 65 20 61 20 70 61 67 65 20 Retrieve a page
14dfa 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63 from the pager c
14dfb 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 65 71 ache. If the req
14dfc 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e uested page is n
14dfd 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e ot.** already in
14dfe 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
14dff 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e return NULL. In
14e00 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d itialize the Mem
14e01 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 Page.pBt and.**
14e02 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c MemPage.aData el
14e03 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 ements if needed
14e04 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 ..*/.static MemP
14e05 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f age *btreePageLo
14e06 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70 okup(BtShared *p
14e07 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
14e08 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
14e09 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c e;. assert( sql
14e0a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14e0b 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
14e0c 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74 pDbPage = sqlit
14e0d 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42 e3PagerLookup(pB
14e0e 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 t->pPager, pgno)
14e0f 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20 ;. if( pDbPage
14e10 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 ){. return bt
14e11 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
14e12 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
14e13 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 pBt);. }. ret
14e14 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
14e15 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
14e16 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
14e17 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 file in pages. I
14e18 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b f there is any k
14e19 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c ind of.** error,
14e1a 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e return ((unsign
14e1b 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 ed int)-1)..*/.s
14e1c 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72 tatic Pgno pager
14e1d 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 Pagecount(BtShar
14e1e 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 ed *pBt){. int
14e1f 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e nPage = -1;. in
14e20 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
14e21 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 pBt->pPage1 );.
14e22 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
14e23 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
14e24 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
14e25 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d ;. assert( rc==
14e26 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 SQLITE_OK || nPa
14e27 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75 ge==-1 );. retu
14e28 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a rn (Pgno)nPage;.
14e29 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 }../*.** Get a p
14e2a 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
14e2b 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a er and initializ
14e2c 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 e it. This rout
14e2d 69 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74 20 61 ine.** is just a
14e2e 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 convenience wra
14e2f 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 pper around sepa
14e30 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a rate calls to.**
14e31 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
14e32 50 61 67 65 28 29 20 61 6e 64 20 73 71 6c 69 74 Page() and sqlit
14e33 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 e3BtreeInitPage(
14e34 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
14e35 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
14e36 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
14e37 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 , /* Th
14e38 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
14e39 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
14e3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
14e3b 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 ber of the page
14e3c 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 to get */. MemP
14e3d 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 age **ppPage
14e3e 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 /* Write the pa
14e3f 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 ge pointer here
14e40 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
14e41 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
14e42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
14e43 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14e44 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
14e45 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a if( pgno==0 ){.
14e46 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14e47 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 E_CORRUPT_BKPT;
14e48 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 69 73 . }.. /* It is
14e49 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73 65 20 often the case
14e4a 74 68 61 74 20 74 68 65 20 70 61 67 65 20 77 65 that the page we
14e4b 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61 64 79 want is already
14e4c 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20 in cache.. **
14e4d 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20 64 69 If so, get it di
14e4e 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 73 61 rectly. This sa
14e4f 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61 76 69 ves us from havi
14e50 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a 20 ng to call. **
14e51 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 pagerPagecount()
14e52 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 70 67 to make sure pg
14e53 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c 69 6d no is within lim
14e54 69 74 73 2c 20 77 68 69 63 68 20 72 65 73 75 6c its, which resul
14e55 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d 65 61 ts. ** in a mea
14e56 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f 72 6d sureable perform
14e57 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 ance improvement
14e58 73 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 50 61 67 s.. */. *ppPag
14e59 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74 72 65 e = pPage = btre
14e5a 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 74 2c ePageLookup(pBt,
14e5b 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50 pgno);. if( pP
14e5c 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20 50 61 age ){. /* Pa
14e5d 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e ge is already in
14e5e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20 72 63 cache */. rc
14e5f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
14e60 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 50 61 }else{. /* Pa
14e61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e ge not in cache.
14e62 20 20 41 63 71 75 69 72 65 20 69 74 2e 20 2a 2f Acquire it. */
14e63 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 61 . if( pgno>pa
14e64 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
14e65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
14e66 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
14e67 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 _BKPT; . }.
14e68 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
14e69 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
14e6a 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 pgno, ppPage, 0)
14e6b 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
14e6c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 50 eturn rc;. pP
14e6d 61 67 65 20 3d 20 2a 70 70 50 61 67 65 3b 0a 20 age = *ppPage;.
14e6e 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d }. if( !pPage-
14e6f 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 72 >isInit ){. r
14e70 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
14e71 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b InitPage(pPage);
14e72 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 . }. if( rc!=S
14e73 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
14e74 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
14e75 65 29 3b 0a 20 20 20 20 2a 70 70 50 61 67 65 20 e);. *ppPage
14e76 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
14e77 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
14e78 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67 65 elease a MemPage
14e79 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 . This should b
14e7a 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f e called once fo
14e7b 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a 20 r each prior.**
14e7c 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 call to sqlite3B
14e7d 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f 0a treeGetPage..*/.
14e7e 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 static void rele
14e7f 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 asePage(MemPage
14e80 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 70 *pPage){. if( p
14e81 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 Page ){. asse
14e82 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 rt( pPage->nOver
14e83 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 flow==0 || sqlit
14e84 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f e3PagerPageRefco
14e85 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50 61 unt(pPage->pDbPa
14e86 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73 73 ge)>1 );. ass
14e87 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 ert( pPage->aDat
14e88 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 a );. assert(
14e89 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 pPage->pBt );.
14e8a 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
14e8b 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
14e8c 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
14e8d 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 == (void*)pPage
14e8e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
14e8f 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
14e90 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 ta(pPage->pDbPag
14e91 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 e)==pPage->aData
14e92 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
14e93 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14e94 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
14e95 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c utex) );. sql
14e96 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
14e97 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
14e98 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 }.}../*.** Dur
14e99 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 ing a rollback,
14e9a 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 72 when the pager r
14e9b 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 69 eloads informati
14e9c 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 on into the cach
14e9d 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 e.** so that the
14e9e 20 63 61 63 68 65 20 69 73 20 72 65 73 74 6f 72 cache is restor
14e9f 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e ed to its origin
14ea0 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 20 al state at the
14ea1 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 start of.** the
14ea2 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72 transaction, for
14ea3 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74 6f each page resto
14ea4 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 red this routine
14ea5 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a is called..**.*
14ea6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e * This routine n
14ea7 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 74 68 eeds to reset th
14ea8 65 20 65 78 74 72 61 20 64 61 74 61 20 73 65 63 e extra data sec
14ea9 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 20 tion at the end
14eaa 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 of the.** page t
14eab 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65 o agree with the
14eac 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e 0a restored data..
14ead 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
14eae 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 67 65 ageReinit(DbPage
14eaf 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d 50 *pData){. MemP
14eb0 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 age *pPage;. pP
14eb1 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a age = (MemPage *
14eb2 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 )sqlite3PagerGet
14eb3 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20 20 Extra(pData);.
14eb4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
14eb5 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 agerPageRefcount
14eb6 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20 69 (pData)>0 );. i
14eb7 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 f( pPage->isInit
14eb8 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
14eb9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14eba 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
14ebb 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50 61 utex) );. pPa
14ebc 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a ge->isInit = 0;.
14ebd 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 if( sqlite3P
14ebe 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 agerPageRefcount
14ebf 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20 20 (pData)>1 ){.
14ec0 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67 68 /* pPage migh
14ec1 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65 65 t not be a btree
14ec2 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68 74 page; it might
14ec3 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 be an overflow
14ec4 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 page. ** or
14ec5 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72 20 ptrmap page or
14ec6 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 49 6e a free page. In
14ec7 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74 68 those cases, th
14ec8 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 e following.
14ec9 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c ** call to sql
14eca 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
14ecb 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 e() will likely
14ecc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
14ecd 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20 RRUPT.. **
14ece 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 64 But no harm is d
14ecf 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20 41 6e one by this. An
14ed0 64 20 69 74 20 69 73 20 76 65 72 79 20 69 6d 70 d it is very imp
14ed1 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20 20 20 ortant that.
14ed2 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 ** sqlite3Btre
14ed3 65 49 6e 69 74 50 61 67 65 28 29 20 62 65 20 63 eInitPage() be c
14ed4 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 20 62 alled on every b
14ed5 74 72 65 65 20 70 61 67 65 20 73 6f 20 77 65 20 tree page so we
14ed6 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 make. ** th
14ed7 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 72 79 e call for every
14ed8 20 70 61 67 65 20 74 68 61 74 20 63 6f 6d 65 73 page that comes
14ed9 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 74 69 in for re-initi
14eda 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c ng. */. sql
14edb 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
14edc 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a e(pPage);. }.
14edd 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 }.}../*.** Inv
14ede 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e oke the busy han
14edf 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65 dler for a btree
14ee0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
14ee1 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 btreeInvokeBusyH
14ee2 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72 andler(void *pAr
14ee3 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a g){. BtShared *
14ee4 70 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a pBt = (BtShared*
14ee5 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 )pArg;. assert(
14ee6 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 pBt->db );. as
14ee7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14ee8 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 tex_held(pBt->db
14ee9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 ->mutex) );. re
14eea 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f turn sqlite3Invo
14eeb 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70 keBusyHandler(&p
14eec 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 Bt->db->busyHand
14eed 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ler);.}../*.** O
14eee 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 pen a database f
14eef 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c ile..** .** zFil
14ef0 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d ename is the nam
14ef1 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
14ef2 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c e file. If zFil
14ef3 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a ename is NULL.**
14ef4 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 a new database
14ef5 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 with a random na
14ef6 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 me is created.
14ef7 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 This randomly na
14ef8 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 med.** database
14ef9 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c file will be del
14efa 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65 eted when sqlite
14efb 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73 3BtreeClose() is
14efc 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a called..** If z
14efd 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 Filename is ":me
14efe 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 mory:" then an i
14eff 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
14f00 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 e is created.**
14f01 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 that is automati
14f02 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20 cally destroyed
14f03 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 when it is close
14f04 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
14f05 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65 database is alre
14f06 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 ady opened in th
14f07 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
14f08 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e connection.** an
14f09 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72 d we are in shar
14f0a 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 ed cache mode, t
14f0b 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c hen the open wil
14f0c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a l fail with an.*
14f0d 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 * SQLITE_CONSTRA
14f0e 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63 INT error. We c
14f0f 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 annot allow two
14f10 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64 or more BtShared
14f11 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 .** objects in t
14f12 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
14f13 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 connection sinc
14f14 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 e doing so will
14f15 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c lead.** to probl
14f16 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 ems with locking
14f17 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14f18 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
14f19 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 treeOpen(. cons
14f1a 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
14f1b 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 e, /* Name of t
14f1c 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 he file containi
14f1d 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74 ng the BTree dat
14f1e 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 abase */. sqlit
14f1f 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
14f20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 /* Associated
14f21 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
14f22 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 */. Btree **pp
14f23 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a Btree, /*
14f24 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 Pointer to new
14f25 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 Btree object wri
14f26 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 tten here */. i
14f27 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
14f28 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e /* Option
14f29 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c s */. int vfsFl
14f2a 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
14f2b 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 * Flags passed t
14f2c 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 hrough to sqlite
14f2d 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
14f2e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 .){. sqlite3_vf
14f2f 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 s *pVfs;
14f30 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 /* The VFS
14f31 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 to use for this
14f32 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 btree */. BtSha
14f33 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 red *pBt = 0;
14f34 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 /* Sha
14f35 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 red part of btre
14f36 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
14f37 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 Btree *p;
14f38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14f39 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 /* Handle to ret
14f3a 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 urn */. sqlite3
14f3b 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 _mutex *mutexOpe
14f3c 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 n = 0; /* Preve
14f3d 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 nts a race condi
14f3e 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 tion. Ticket #35
14f3f 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 37 */. int rc =
14f40 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
14f41 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
14f42 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 code from this
14f43 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 function */. u8
14f44 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 nReserve;
14f45 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14f46 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73 Byte of unused s
14f47 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 pace on each pag
14f48 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
14f49 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 char zDbHeader[1
14f4a 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 00]; /* Databas
14f4b 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74 e header content
14f4c 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 */.. /* Set th
14f4d 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d e variable isMem
14f4e 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61 db to true for a
14f4f 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
14f50 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 base, or . ** f
14f51 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d alse for a file-
14f52 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20 based database.
14f53 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f This symbol is o
14f54 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a nly required if.
14f55 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74 ** either of t
14f56 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f he shared-data o
14f57 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61 r autovacuum fea
14f58 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c tures are compil
14f59 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 ed . ** into th
14f5a 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a e library.. */.
14f5b 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
14f5c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14f5d 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e CACHE) || !defin
14f5e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ed(SQLITE_OMIT_A
14f5f 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66 UTOVACUUM). #if
14f60 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14f61 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e MEMORYDB. con
14f62 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d st int isMemdb =
14f63 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 0;. #else.
14f64 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 const int isMemd
14f65 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 b = zFilename &&
14f66 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 !strcmp(zFilena
14f67 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b me, ":memory:");
14f68 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 . #endif.#endif
14f69 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d .. assert( db!=
14f6a 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
14f6b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14f6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
14f6d 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 . pVfs = db->pV
14f6e 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 fs;. p = sqlite
14f6f 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 3MallocZero(size
14f70 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 of(Btree));. if
14f71 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 ( !p ){. retu
14f72 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
14f73 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e . }. p->inTran
14f74 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a s = TRANS_NONE;.
14f75 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 23 p->db = db;..#
14f76 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
14f77 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
14f78 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 ACHE) && !define
14f79 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 d(SQLITE_OMIT_DI
14f7a 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 SKIO). /*. **
14f7b 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73 If this Btree is
14f7c 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 a candidate for
14f7d 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 shared cache, t
14f7e 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20 ry to find an.
14f7f 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68 ** existing BtSh
14f80 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74 ared object that
14f81 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69 we can share wi
14f82 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 th. */. if( is
14f83 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c Memdb==0 && zFil
14f84 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 ename && zFilena
14f85 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28 me[0] ){. if(
14f86 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
14f87 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 nfig.sharedCache
14f88 45 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20 20 20 Enabled ){.
14f89 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 int nFullPathna
14f8a 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 me = pVfs->mxPat
14f8b 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 hname+1;. c
14f8c 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 har *zFullPathna
14f8d 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c me = sqlite3Mall
14f8e 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 oc(nFullPathname
14f8f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
14f90 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 _mutex *mutexSha
14f91 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 red;. p->sh
14f92 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 arable = 1;.
14f93 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 db->flags |= S
14f94 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 QLITE_SharedCach
14f95 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 e;. if( !zF
14f96 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 ullPathname ){.
14f97 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
14f98 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ree(p);.
14f99 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
14f9a 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 MEM;. }.
14f9b 20 20 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c sqlite3OsFull
14f9c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a Pathname(pVfs, z
14f9d 46 69 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 Filename, nFullP
14f9e 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 athname, zFullPa
14f9f 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 6d thname);. m
14fa0 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 6c 69 74 utexOpen = sqlit
14fa1 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
14fa2 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
14fa3 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 20 73 71 _OPEN);. sq
14fa4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
14fa5 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 r(mutexOpen);.
14fa6 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 mutexShared
14fa7 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
14fa8 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
14fa9 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b _STATIC_MASTER);
14faa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
14fab 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
14fac 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 66 Shared);. f
14fad 6f 72 28 70 42 74 3d 47 4c 4f 42 41 4c 28 42 74 or(pBt=GLOBAL(Bt
14fae 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
14faf 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b haredCacheList);
14fb0 20 70 42 74 3b 20 70 42 74 3d 70 42 74 2d 3e 70 pBt; pBt=pBt->p
14fb1 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 61 Next){. a
14fb2 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66 ssert( pBt->nRef
14fb3 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 >0 );. if
14fb4 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a 46 75 6c ( 0==strcmp(zFul
14fb5 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71 6c 69 74 lPathname, sqlit
14fb6 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 e3PagerFilename(
14fb7 70 42 74 2d 3e 70 50 61 67 65 72 29 29 0a 20 20 pBt->pPager)).
14fb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
14fb9 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 & sqlite3PagerVf
14fba 73 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d s(pBt->pPager)==
14fbb 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 20 20 pVfs ){.
14fbc 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
14fbd 20 20 20 20 20 66 6f 72 28 69 44 62 3d 64 62 2d for(iDb=db-
14fbe 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d 30 3b 20 >nDb-1; iDb>=0;
14fbf 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 iDb--){.
14fc0 20 20 20 20 42 74 72 65 65 20 2a 70 45 78 69 73 Btree *pExis
14fc1 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44 62 5b 69 ting = db->aDb[i
14fc2 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 20 Db].pBt;.
14fc3 20 20 20 20 20 69 66 28 20 70 45 78 69 73 74 69 if( pExisti
14fc4 6e 67 20 26 26 20 70 45 78 69 73 74 69 6e 67 2d ng && pExisting-
14fc5 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a 20 20 20 >pBt==pBt ){.
14fc6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
14fc7 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
14fc8 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 utexShared);.
14fc9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
14fca 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
14fcb 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 utexOpen);.
14fcc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
14fcd 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e _free(zFullPathn
14fce 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ame);.
14fcf 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
14fd0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 (p);.
14fd1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
14fd2 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 _CONSTRAINT;.
14fd3 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
14fd4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
14fd5 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 p->pBt = pBt;.
14fd6 20 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 pBt->nR
14fd7 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 ef++;.
14fd8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
14fd9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
14fda 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
14fdb 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b ve(mutexShared);
14fdc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
14fdd 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d ree(zFullPathnam
14fde 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 e);. }.#ifdef
14fdf 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
14fe0 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a else{. /*
14fe1 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 In debug mode,
14fe2 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 65 72 73 we mark all pers
14fe3 69 73 74 65 6e 74 20 64 61 74 61 62 61 73 65 73 istent databases
14fe4 20 61 73 20 73 68 61 72 61 62 6c 65 0a 20 20 20 as sharable.
14fe5 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 ** even when
14fe6 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 54 they are not. T
14fe7 68 69 73 20 65 78 65 72 63 69 73 65 73 20 74 68 his exercises th
14fe8 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64 65 20 61 e locking code a
14fe9 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67 69 76 65 nd. ** give
14fea 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74 75 6e 69 s more opportuni
14feb 74 79 20 66 6f 72 20 61 73 73 65 72 74 73 28 73 ty for asserts(s
14fec 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14fed 64 28 29 29 0a 20 20 20 20 20 20 2a 2a 20 73 74 d()). ** st
14fee 61 74 65 6d 65 6e 74 73 20 74 6f 20 66 69 6e 64 atements to find
14fef 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d locking problem
14ff0 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 s.. */.
14ff1 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 p->sharable =
14ff2 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 1;. }.#endif.
14ff3 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 }.#endif. if(
14ff4 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f pBt==0 ){. /
14ff5 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c *. ** The fol
14ff6 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 20 6d lowing asserts m
14ff7 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 73 74 ake sure that st
14ff8 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62 79 ructures used by
14ff9 20 74 68 65 20 62 74 72 65 65 20 61 72 65 0a 20 the btree are.
14ffa 20 20 20 2a 2a 20 74 68 65 20 72 69 67 68 74 20 ** the right
14ffb 73 69 7a 65 2e 20 20 54 68 69 73 20 69 73 20 74 size. This is t
14ffc 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 o guard against
14ffd 73 69 7a 65 20 63 68 61 6e 67 65 73 20 74 68 61 size changes tha
14ffe 74 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 t result. **
14fff 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f when compiling o
15000 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 72 n a different ar
15001 63 68 69 74 65 63 74 75 72 65 2e 0a 20 20 20 20 chitecture..
15002 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 */. assert( s
15003 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20 7c 7c izeof(i64)==8 ||
15004 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 34 20 sizeof(i64)==4
15005 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
15006 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38 20 7c 7c izeof(u64)==8 ||
15007 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 34 20 sizeof(u64)==4
15008 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
15009 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b izeof(u32)==4 );
1500a 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a . assert( siz
1500b 65 6f 66 28 75 31 36 29 3d 3d 32 20 29 3b 0a 20 eof(u16)==2 );.
1500c 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
1500d 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 f(Pgno)==4 );.
1500e 0a 20 20 20 20 70 42 74 20 3d 20 73 71 6c 69 74 . pBt = sqlit
1500f 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 e3MallocZero( si
15010 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b 0a 20 20 zeof(*pBt) );.
15011 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a if( pBt==0 ){.
15012 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
15013 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 E_NOMEM;. g
15014 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f oto btree_open_o
15015 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 ut;. }. rc
15016 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f = sqlite3PagerO
15017 70 65 6e 28 70 56 66 73 2c 20 26 70 42 74 2d 3e pen(pVfs, &pBt->
15018 70 50 61 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d pPager, zFilenam
15019 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
1501a 20 20 20 20 20 20 20 20 20 20 20 20 20 45 58 54 EXT
1501b 52 41 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c 20 RA_SIZE, flags,
1501c 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 vfsFlags);. i
1501d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1501e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1501f 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 qlite3PagerReadF
15020 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e 70 ileheader(pBt->p
15021 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62 Pager,sizeof(zDb
15022 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64 65 Header),zDbHeade
15023 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 r);. }. if
15024 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
15025 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 ){. goto bt
15026 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 ree_open_out;.
15027 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 }. pBt->db
15028 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 = db;. sqlite
15029 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
1502a 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 dler(pBt->pPager
1502b 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 , btreeInvokeBus
1502c 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b 0a yHandler, pBt);.
1502d 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 p->pBt = pBt
1502e 3b 0a 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 ;. . sqlite3
1502f 50 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 PagerSetReiniter
15030 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 61 (pBt->pPager, pa
15031 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20 70 geReinit);. p
15032 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b Bt->pCursor = 0;
15033 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 . pBt->pPage1
15034 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72 = 0;. pBt->r
15035 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 eadOnly = sqlite
15036 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 3PagerIsreadonly
15037 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 (pBt->pPager);.
15038 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 pBt->pageSize
15039 20 3d 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 = get2byte(&zDb
1503a 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20 Header[16]);.
1503b 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 if( pBt->pageSi
1503c 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 ze<512 || pBt->p
1503d 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d ageSize>SQLITE_M
1503e 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 AX_PAGE_SIZE.
1503f 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e || ((pBt->
15040 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d pageSize-1)&pBt-
15041 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b >pageSize)!=0 ){
15042 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 . pBt->page
15043 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 Size = 0;.#ifnde
15044 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
15045 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f TOVACUUM. /
15046 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e * If the magic n
15047 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 ame ":memory:" w
15048 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e ill create an in
15049 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1504a 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 , then. **
1504b 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 leave the autoVa
1504c 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 cuum mode at 0 (
1504d 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 do not auto-vacu
1504e 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 um), even if.
1504f 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 ** SQLITE_DEF
15050 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 AULT_AUTOVACUUM
15051 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 is true. On the
15052 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 other hand, if.
15053 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f ** SQLITE_O
15054 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 MIT_MEMORYDB has
15055 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 been defined, t
15056 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 hen ":memory:" i
15057 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a s just a. *
15058 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e * regular file-n
15059 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ame. In this cas
1505a 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 e the auto-vacuu
1505b 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 m applies as per
1505c 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a normal.. *
1505d 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c /. if( zFil
1505e 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 ename && !isMemd
1505f 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 b ){. pBt
15060 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 ->autoVacuum = (
15061 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 SQLITE_DEFAULT_A
15062 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 UTOVACUUM ? 1 :
15063 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 0);. pBt-
15064 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 >incrVacuum = (S
15065 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
15066 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 TOVACUUM==2 ? 1
15067 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 : 0);. }.#e
15068 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 ndif. nRese
15069 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c rve = 0;. }el
1506a 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 se{. nReser
1506b 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 ve = zDbHeader[2
1506c 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 0];. pBt->p
1506d 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 ageSizeFixed = 1
1506e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1506f 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
15070 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f . pBt->auto
15071 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 Vacuum = (get4by
15072 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 te(&zDbHeader[36
15073 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 + 4*4])?1:0);.
15074 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 pBt->incrVa
15075 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 cuum = (get4byte
15076 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b (&zDbHeader[36 +
15077 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 7*4])?1:0);.#en
15078 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 dif. }. rc
15079 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 = sqlite3PagerS
1507a 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e etPagesize(pBt->
1507b 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 pPager, &pBt->pa
1507c 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 geSize);. if(
1507d 20 72 63 20 29 20 67 6f 74 6f 20 62 74 72 65 65 rc ) goto btree
1507e 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 70 _open_out;. p
1507f 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d Bt->usableSize =
15080 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d pBt->pageSize -
15081 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61 nReserve;. a
15082 73 73 65 72 74 28 20 28 70 42 74 2d 3e 70 61 67 ssert( (pBt->pag
15083 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b eSize & 7)==0 );
15084 20 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 /* 8-byte alig
15085 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65 53 69 7a nment of pageSiz
15086 65 20 2a 2f 0a 20 20 20 0a 23 69 66 20 21 64 65 e */. .#if !de
15087 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
15088 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 T_SHARED_CACHE)
15089 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
1508a 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a TE_OMIT_DISKIO).
1508b 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e /* Add the n
1508c 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 ew BtShared obje
1508d 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 ct to the linked
1508e 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 20 42 list sharable B
1508f 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f tShareds.. */
15090 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 . if( p->shar
15091 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 able ){. sq
15092 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
15093 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 exShared;.
15094 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 pBt->nRef = 1;.
15095 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 mutexShared
15096 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
15097 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
15098 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
15099 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 ;. if( SQLI
1509a 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
1509b 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
1509c 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 nfig.bCoreMutex
1509d 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e ){. pBt->
1509e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
1509f 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
150a0 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 _MUTEX_FAST);.
150a1 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d if( pBt->m
150a2 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 utex==0 ){.
150a3 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
150a4 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
150a5 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
150a6 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ed = 0;.
150a7 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 goto btree_ope
150a8 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d n_out;. }
150a9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
150aa 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
150ab 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b er(mutexShared);
150ac 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 . pBt->pNex
150ad 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 t = GLOBAL(BtSha
150ae 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
150af 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 edCacheList);.
150b0 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 GLOBAL(BtSha
150b1 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
150b2 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 edCacheList) = p
150b3 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 Bt;. sqlite
150b4 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
150b5 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 texShared);.
150b6 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 }.#endif. }..#i
150b7 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
150b8 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
150b9 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 CHE) && !defined
150ba 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 (SQLITE_OMIT_DIS
150bb 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 KIO). /* If the
150bc 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20 new Btree uses
150bd 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 a sharable pBtSh
150be 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 ared, then link
150bf 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 the new. ** Btr
150c0 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 ee into the list
150c1 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 of all sharable
150c2 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 Btrees for the
150c3 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e same connection.
150c4 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 . ** The list i
150c5 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 s kept in ascend
150c6 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 ing order by pBt
150c7 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 address.. */.
150c8 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 if( p->sharable
150c9 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
150ca 20 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a Btree *pSib;.
150cb 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 for(i=0; i<d
150cc 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
150cd 20 20 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 if( (pSib =
150ce 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 db->aDb[i].pBt)!
150cf 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 =0 && pSib->shar
150d0 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 able ){.
150d1 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 while( pSib->pPr
150d2 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 ev ){ pSib = pSi
150d3 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 b->pPrev; }.
150d4 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 if( p->pBt<p
150d5 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 Sib->pBt ){.
150d6 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
150d7 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 pSib;.
150d8 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 p->pPrev = 0;.
150d9 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 pSib->p
150da 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 Prev = p;.
150db 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
150dc 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e while( pSib->
150dd 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 pNext && pSib->p
150de 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 Next->pBt<p->pBt
150df 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
150e0 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 pSib = pSib->pNe
150e1 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a xt;. }.
150e2 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 p->pNe
150e3 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 xt = pSib->pNext
150e4 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 ;. p->p
150e5 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 Prev = pSib;.
150e6 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e if( p->pN
150e7 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ext ){.
150e8 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 p->pNext->pPr
150e9 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 ev = p;.
150ea 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 }. pS
150eb 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 ib->pNext = p;.
150ec 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
150ed 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
150ee 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
150ef 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b . *ppBtree = p;
150f0 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 ..btree_open_out
150f1 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 :. if( rc!=SQLI
150f2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
150f3 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 pBt && pBt->pPa
150f4 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ger ){. sql
150f5 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 ite3PagerClose(p
150f6 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
150f7 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
150f8 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 ree(pBt);. sq
150f9 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
150fa 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b *ppBtree = 0;
150fb 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74 65 78 . }. if( mutex
150fc 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 65 Open ){. asse
150fd 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
150fe 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70 65 6e x_held(mutexOpen
150ff 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) );. sqlite3
15100 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
15101 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 20 72 exOpen);. }. r
15102 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
15103 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 ** Decrement the
15104 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 BtShared.nRef c
15105 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74 ounter. When it
15106 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a reaches zero,.*
15107 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53 * remove the BtS
15108 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 hared structure
15109 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67 from the sharing
1510a 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a list. Return.*
1510b 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74 * true if the Bt
1510c 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e Shared.nRef coun
1510d 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f ter reaches zero
1510e 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66 and return.** f
1510f 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 74 alse if it is st
15110 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f ill positive..*/
15111 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f .static int remo
15112 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 veFromSharingLis
15113 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
15114 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
15115 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
15116 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 HE. sqlite3_mut
15117 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42 ex *pMaster;. B
15118 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a tShared *pList;.
15119 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20 int removed =
1511a 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 0;.. assert( sq
1511b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
1511c 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
1511d 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73 );. pMaster = s
1511e 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
1511f 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
15120 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 ATIC_MASTER);.
15121 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
15122 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ter(pMaster);.
15123 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 pBt->nRef--;. i
15124 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 f( pBt->nRef<=0
15125 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41 ){. if( GLOBA
15126 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
15127 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
15128 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 st)==pBt ){.
15129 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 GLOBAL(BtShare
1512a 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
1512b 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 CacheList) = pBt
1512c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c ->pNext;. }el
1512d 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20 se{. pList
1512e 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 = GLOBAL(BtShare
1512f 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
15130 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 CacheList);.
15131 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 while( ALWAYS(
15132 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d pList) && pList-
15133 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20 >pNext!=pBt ){.
15134 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69 pList=pLi
15135 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 st->pNext;.
15136 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 }. if( ALW
15137 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20 AYS(pList) ){.
15138 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65 pList->pNe
15139 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b xt = pBt->pNext;
1513a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1513b 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 if( SQLITE_TH
1513c 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20 READSAFE ){.
1513d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1513e 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29 free(pBt->mutex)
1513f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f ;. }. remo
15140 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 ved = 1;. }. s
15141 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
15142 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 ve(pMaster);. r
15143 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 eturn removed;.#
15144 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b else. return 1;
15145 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
15146 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e Make sure pBt->
15147 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73 pTmpSpace points
15148 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f to an allocatio
15149 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c n of .** MX_CELL
1514a 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73 _SIZE(pBt) bytes
1514b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1514c 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 allocateTempSpa
1514d 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ce(BtShared *pBt
1514e 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 ){. if( !pBt->p
1514f 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 TmpSpace ){.
15150 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d pBt->pTmpSpace =
15151 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
15152 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a oc( pBt->pageSiz
15153 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a e );. }.}../*.*
15154 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e * Free the pBt->
15155 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61 pTmpSpace alloca
15156 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tion.*/.static v
15157 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 61 63 oid freeTempSpac
15158 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 e(BtShared *pBt)
15159 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 {. sqlite3PageF
1515a 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 ree( pBt->pTmpSp
1515b 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d ace);. pBt->pTm
1515c 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f pSpace = 0;.}../
1515d 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 *.** Close an op
1515e 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 en database and
1515f 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63 invalidate all c
15160 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ursors..*/.SQLIT
15161 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
15162 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 lite3BtreeClose(
15163 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 Btree *p){. BtS
15164 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
15165 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 pBt;. BtCursor
15166 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f *pCur;.. /* Clo
15167 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f se all cursors o
15168 70 65 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 pened via this h
15169 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 andle. */. ass
1516a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1516b 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
1516c 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1516d 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
1516e 0a 20 20 70 43 75 72 20 3d 20 70 42 74 2d 3e 70 . pCur = pBt->p
1516f 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 6c 65 28 Cursor;. while(
15170 20 70 43 75 72 20 29 7b 0a 20 20 20 20 42 74 43 pCur ){. BtC
15171 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43 ursor *pTmp = pC
15172 75 72 3b 0a 20 20 20 20 70 43 75 72 20 3d 20 70 ur;. pCur = p
15173 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Cur->pNext;.
15174 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65 if( pTmp->pBtree
15175 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==p ){. sql
15176 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 ite3BtreeCloseCu
15177 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 20 20 20 rsor(pTmp);.
15178 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c }. }.. /* Roll
15179 62 61 63 6b 20 61 6e 79 20 61 63 74 69 76 65 20 back any active
1517a 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 transaction and
1517b 66 72 65 65 20 74 68 65 20 68 61 6e 64 6c 65 20 free the handle
1517c 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 structure.. **
1517d 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 The call to sqli
1517e 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b te3BtreeRollback
1517f 28 29 20 64 72 6f 70 73 20 61 6e 79 20 74 61 62 () drops any tab
15180 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 le-locks held by
15181 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 6e 64 6c . ** this handl
15182 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 e.. */. sqlite
15183 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 3BtreeRollback(p
15184 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
15185 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a eLeave(p);.. /*
15186 20 49 66 20 74 68 65 72 65 20 61 72 65 20 73 74 If there are st
15187 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61 ill other outsta
15188 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 nding references
15189 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 to the shared-b
1518a 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74 tree. ** struct
1518b 75 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e ure, return now.
1518c 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f The remainder o
1518d 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 f this procedure
1518e 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70 cleans . ** up
1518f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 the shared-btre
15190 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 e.. */. assert
15191 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
15192 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d =0 && p->locked=
15193 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e =0 );. if( !p->
15194 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f sharable || remo
15195 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 veFromSharingLis
15196 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a t(pBt) ){. /*
15197 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c The pBt is no l
15198 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61 onger on the sha
15199 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65 ring list, so we
1519a 20 63 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20 can access.
1519b 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 20 68 61 ** it without ha
1519c 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65 ving to hold the
1519d 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 mutex.. **.
1519e 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20 ** Clean out
1519f 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 42 and delete the B
151a0 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a tShared object..
151a1 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
151a2 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 t( !pBt->pCursor
151a3 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 );. sqlite3P
151a4 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 agerClose(pBt->p
151a5 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
151a6 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 pBt->xFreeSchema
151a7 20 26 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 && pBt->pSchema
151a8 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78 ){. pBt->x
151a9 46 72 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e FreeSchema(pBt->
151aa 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a pSchema);. }.
151ab 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
151ac 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a (pBt->pSchema);.
151ad 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 freeTempSpac
151ae 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 e(pBt);. sqli
151af 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 te3_free(pBt);.
151b0 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
151b1 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
151b2 41 43 48 45 0a 20 20 61 73 73 65 72 74 28 20 70 ACHE. assert( p
151b3 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
151b4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
151b5 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 locked==0 );. i
151b6 66 28 20 70 2d 3e 70 50 72 65 76 20 29 20 70 2d f( p->pPrev ) p-
151b7 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
151b8 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 66 28 20 p->pNext;. if(
151b9 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d 3e 70 4e p->pNext ) p->pN
151ba 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e ext->pPrev = p->
151bb 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 0a 0a 20 pPrev;.#endif..
151bc 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
151bd 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
151be 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
151bf 68 61 6e 67 65 20 74 68 65 20 6c 69 6d 69 74 20 hange the limit
151c0 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 on the number of
151c1 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 64 20 69 pages allowed i
151c2 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a 0a n the cache..**.
151c3 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
151c4 75 6d 62 65 72 20 6f 66 20 63 61 63 68 65 20 70 umber of cache p
151c5 61 67 65 73 20 69 73 20 73 65 74 20 74 6f 20 74 ages is set to t
151c6 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a 2a 20 76 he absolute.** v
151c7 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2e 20 alue of mxPage.
151c8 20 49 66 20 6d 78 50 61 67 65 20 69 73 20 6e 65 If mxPage is ne
151c9 67 61 74 69 76 65 2c 20 74 68 65 20 70 61 67 65 gative, the page
151ca 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 72 61 74 r will.** operat
151cb 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 6c 79 e asynchronously
151cc 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 73 - it will not s
151cd 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 6e 63 28 top to do fsync(
151ce 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 72 65 20 )s.** to insure
151cf 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 data is written
151d0 74 6f 20 74 68 65 20 64 69 73 6b 20 73 75 72 66 to the disk surf
151d1 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f ace before.** co
151d2 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 61 6e 73 ntinuing. Trans
151d3 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c 20 77 6f actions still wo
151d4 72 6b 20 69 66 20 73 79 6e 63 68 72 6f 6e 6f 75 rk if synchronou
151d5 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 61 6e 64 s is off,.** and
151d6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 the database ca
151d7 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65 nnot be corrupte
151d8 64 20 69 66 20 74 68 69 73 20 70 72 6f 67 72 61 d if this progra
151d9 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e 20 20 42 m.** crashes. B
151da 75 74 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 ut if the operat
151db 69 6e 67 20 73 79 73 74 65 6d 20 63 72 61 73 68 ing system crash
151dc 65 73 20 6f 72 20 74 68 65 72 65 20 69 73 0a 2a es or there is.*
151dd 2a 20 61 6e 20 61 62 72 75 70 74 20 70 6f 77 65 * an abrupt powe
151de 72 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20 73 r failure when s
151df 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 ynchronous is of
151e0 66 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 0a f, the database.
151e1 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c 65 66 74 ** could be left
151e2 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 in an inconsist
151e3 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 6f 76 65 ent and unrecove
151e4 72 61 62 6c 65 20 73 74 61 74 65 2e 0a 2a 2a 20 rable state..**
151e5 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f Synchronous is o
151e6 6e 20 62 79 20 64 65 66 61 75 6c 74 20 73 6f 20 n by default so
151e7 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
151e8 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 6e 6f ion is not.** no
151e9 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 79 2e 0a rmally a worry..
151ea 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
151eb 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
151ec 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 eeSetCacheSize(B
151ed 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 tree *p, int mxP
151ee 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 age){. BtShared
151ef 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
151f0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
151f1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
151f2 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
151f3 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
151f4 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 r(p);. sqlite3P
151f5 61 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 agerSetCachesize
151f6 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 (pBt->pPager, mx
151f7 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Page);. sqlite3
151f8 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
151f9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
151fa 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e K;.}../*.** Chan
151fb 67 65 20 74 68 65 20 77 61 79 20 64 61 74 61 20 ge the way data
151fc 69 73 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 is synced to dis
151fd 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e k in order to in
151fe 63 72 65 61 73 65 20 6f 72 20 64 65 63 72 65 61 crease or decrea
151ff 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c 20 74 se.** how well t
15200 68 65 20 64 61 74 61 62 61 73 65 20 72 65 73 69 he database resi
15201 73 74 73 20 64 61 6d 61 67 65 20 64 75 65 20 74 sts damage due t
15202 6f 20 4f 53 20 63 72 61 73 68 65 73 20 61 6e 64 o OS crashes and
15203 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 power.** failur
15204 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 69 73 20 es. Level 1 is
15205 74 68 65 20 73 61 6d 65 20 61 73 20 61 73 79 6e the same as asyn
15206 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 73 79 6e chronous (no syn
15207 63 73 28 29 20 6f 63 63 75 72 20 61 6e 64 0a 2a cs() occur and.*
15208 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 69 67 * there is a hig
15209 68 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 h probability of
1520a 20 64 61 6d 61 67 65 29 20 20 4c 65 76 65 6c 20 damage) Level
1520b 32 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2 is the default
1520c 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 73 20 61 . There.** is a
1520d 20 76 65 72 79 20 6c 6f 77 20 62 75 74 20 6e 6f very low but no
1520e 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 n-zero probabili
1520f 74 79 20 6f 66 20 64 61 6d 61 67 65 2e 20 20 4c ty of damage. L
15210 65 76 65 6c 20 33 20 72 65 64 75 63 65 73 20 74 evel 3 reduces t
15211 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 6c 69 74 he.** probabilit
15212 79 20 6f 66 20 64 61 6d 61 67 65 20 74 6f 20 6e y of damage to n
15213 65 61 72 20 7a 65 72 6f 20 62 75 74 20 77 69 74 ear zero but wit
15214 68 20 61 20 77 72 69 74 65 20 70 65 72 66 6f 72 h a write perfor
15215 6d 61 6e 63 65 20 72 65 64 75 63 74 69 6f 6e 2e mance reduction.
15216 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
15217 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
15218 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 AGMAS.SQLITE_PRI
15219 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1521a 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 BtreeSetSafetyLe
1521b 76 65 6c 28 42 74 72 65 65 20 2a 70 2c 20 69 6e vel(Btree *p, in
1521c 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 66 75 6c t level, int ful
1521d 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 68 61 72 lSync){. BtShar
1521e 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1521f 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
15220 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
15221 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
15222 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15223 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ter(p);. sqlite
15224 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 3PagerSetSafetyL
15225 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 67 65 72 evel(pBt->pPager
15226 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 79 6e , level, fullSyn
15227 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 c);. sqlite3Btr
15228 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
15229 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1522a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1522b 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
1522c 68 65 20 67 69 76 65 6e 20 62 74 72 65 65 20 69 he given btree i
1522d 73 20 73 65 74 20 74 6f 20 73 61 66 65 74 79 20 s set to safety
1522e 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 level 1. In oth
1522f 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 er.** words, ret
15230 75 72 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73 urn TRUE if no s
15231 79 6e 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 ync() occurs on
15232 74 68 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a the disk files..
15233 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
15234 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
15235 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 eeSyncDisabled(B
15236 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 tree *p){. BtSh
15237 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
15238 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 Bt;. int rc;.
15239 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1523a 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
1523b 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a 20 20 ->mutex) ); .
1523c 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1523d 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
1523e 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 pBt && pBt->pPag
1523f 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c er );. rc = sql
15240 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 ite3PagerNosync(
15241 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
15242 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
15243 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
15244 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e c;.}..#if !defin
15245 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ed(SQLITE_OMIT_P
15246 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c AGER_PRAGMAS) ||
15247 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
15248 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a 2f 2a _OMIT_VACUUM)./*
15249 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 64 .** Change the d
1524a 65 66 61 75 6c 74 20 70 61 67 65 73 20 73 69 7a efault pages siz
1524b 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 e and the number
1524c 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 79 74 of reserved byt
1524d 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a 2a 20 es per page..**
1524e 4f 72 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 Or, if the page
1524f 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79 size has already
15250 20 62 65 65 6e 20 66 69 78 65 64 2c 20 72 65 74 been fixed, ret
15251 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
15252 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20 NLY .** without
15253 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 68 69 6e changing anythin
15254 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 g..**.** The pag
15255 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 e size must be a
15256 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 power of 2 betw
15257 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 een 512 and 6553
15258 36 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 0a 6. If the page.
15259 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c 69 65 64 ** size supplied
1525a 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 does not meet t
1525b 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 his constraint t
1525c 68 65 6e 20 74 68 65 20 70 61 67 65 20 73 69 7a hen the page siz
1525d 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e e is not.** chan
1525e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 ged..**.** Page
1525f 73 69 7a 65 73 20 61 72 65 20 63 6f 6e 73 74 72 sizes are constr
15260 61 69 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f ained to be a po
15261 77 65 72 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 wer of two so th
15262 61 74 20 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a at the region.**
15263 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
15264 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 6c file used for l
15265 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e ocking (beginnin
15266 67 20 61 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 g at PENDING_BYT
15267 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 E,.** the first
15268 62 79 74 65 20 70 61 73 74 20 74 68 65 20 31 47 byte past the 1G
15269 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 B boundary, 0x40
1526a 30 30 30 30 30 30 29 20 6e 65 65 64 73 20 74 6f 000000) needs to
1526b 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 occur.** at the
1526c 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 beginning of a
1526d 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 page..**.** If p
1526e 61 72 61 6d 65 74 65 72 20 6e 52 65 73 65 72 76 arameter nReserv
1526f 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a e is less than z
15270 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 ero, then the nu
15271 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 mber of reserved
15272 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 20 70 61 .** bytes per pa
15273 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 ge is left uncha
15274 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 nged..**.** If t
15275 68 65 20 69 46 69 78 21 3d 30 20 74 68 65 6e 20 he iFix!=0 then
15276 74 68 65 20 70 61 67 65 53 69 7a 65 46 69 78 65 the pageSizeFixe
15277 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 73 6f d flag is set so
15278 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 73 that the page s
15279 69 7a 65 0a 2a 2a 20 61 6e 64 20 61 75 74 6f 76 ize.** and autov
1527a 61 63 75 75 6d 20 6d 6f 64 65 20 63 61 6e 20 6e acuum mode can n
1527b 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e o longer be chan
1527c 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ged..*/.SQLITE_P
1527d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1527e 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 e3BtreeSetPageSi
1527f 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ze(Btree *p, int
15280 20 70 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e pageSize, int n
15281 52 65 73 65 72 76 65 2c 20 69 6e 74 20 69 46 69 Reserve, int iFi
15282 78 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 x){. int rc = S
15283 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 QLITE_OK;. BtSh
15284 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
15285 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52 Bt;. assert( nR
15286 65 73 65 72 76 65 3e 3d 2d 31 20 26 26 20 6e 52 eserve>=-1 && nR
15287 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 eserve<=255 );.
15288 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
15289 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 er(p);. if( pBt
1528a 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 ->pageSizeFixed
1528b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
1528c 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
1528d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 return SQLITE_R
1528e 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 EADONLY;. }. i
1528f 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29 7b f( nReserve<0 ){
15290 0a 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 . nReserve =
15291 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
15292 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
15293 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
15294 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 Reserve>=0 && nR
15295 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 eserve<=255 );.
15296 20 69 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 if( pageSize>=5
15297 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 12 && pageSize<=
15298 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
15299 53 49 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 SIZE &&.
1529a 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 ((pageSize-1)&pa
1529b 67 65 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 geSize)==0 ){.
1529c 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 assert( (pageS
1529d 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 ize & 7)==0 );.
1529e 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d assert( !pBt-
1529f 3e 70 50 61 67 65 31 20 26 26 20 21 70 42 74 2d >pPage1 && !pBt-
152a0 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 >pCursor );.
152a1 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 pBt->pageSize =
152a2 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 (u16)pageSize;.
152a3 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 freeTempSpace
152a4 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 (pBt);. rc =
152a5 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
152a6 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 agesize(pBt->pPa
152a7 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 ger, &pBt->pageS
152a8 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 42 74 2d ize);. }. pBt-
152a9 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 >usableSize = pB
152aa 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28 75 t->pageSize - (u
152ab 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 69 16)nReserve;. i
152ac 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e 70 f( iFix ) pBt->p
152ad 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 ageSizeFixed = 1
152ae 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
152af 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
152b0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
152b1 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 Return the curre
152b2 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67 ntly defined pag
152b3 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 e size.*/.SQLITE
152b4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
152b5 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
152b6 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a Size(Btree *p){.
152b7 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d return p->pBt-
152b8 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a >pageSize;.}../*
152b9 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
152ba 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
152bb 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 f space at the e
152bc 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 nd of every page
152bd 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 that.** are int
152be 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e entually left un
152bf 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 used. This is t
152c0 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73 70 he "reserved" sp
152c1 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 ace that is.** s
152c2 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 79 ometimes used by
152c3 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
152c4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
152c5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
152c6 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 20 etReserve(Btree
152c7 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 *p){. int n;.
152c8 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
152c9 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 r(p);. n = p->p
152ca 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 Bt->pageSize - p
152cb 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
152cc 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 e;. sqlite3Btre
152cd 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
152ce 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn n;.}../*.**
152cf 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 Set the maximum
152d0 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 page count for a
152d1 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 50 database if mxP
152d2 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e age is positive.
152d3 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 .** No changes a
152d4 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 67 re made if mxPag
152d5 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 69 e is 0 or negati
152d6 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 ve..** Regardles
152d7 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f s of the value o
152d8 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e f mxPage, return
152d9 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 the maximum pag
152da 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 e count..*/.SQLI
152db 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
152dc 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 qlite3BtreeMaxPa
152dd 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 geCount(Btree *p
152de 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
152df 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 int n;. sqlite
152e0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
152e1 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 n = sqlite3Pag
152e2 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70 erMaxPageCount(p
152e3 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d ->pBt->pPager, m
152e4 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 xPage);. sqlite
152e5 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
152e6 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 return n;.}.#e
152e7 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
152e8 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 (SQLITE_OMIT_PAG
152e9 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 ER_PRAGMAS) || !
152ea 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
152eb 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a MIT_VACUUM) */..
152ec 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
152ed 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 'auto-vacuum' p
152ee 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 64 roperty of the d
152ef 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 atabase. If the
152f0 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 'autoVacuum'.**
152f1 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
152f2 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f -zero, then auto
152f3 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 -vacuum mode is
152f4 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f enabled. If zero
152f5 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 , it.** is disab
152f6 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74 led. The default
152f7 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 61 value for the a
152f8 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 uto-vacuum prope
152f9 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 rty is .** deter
152fa 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c mined by the SQL
152fb 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
152fc 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f VACUUM macro..*/
152fd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
152fe 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
152ff 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 SetAutoVacuum(Bt
15300 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f ree *p, int auto
15301 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 Vacuum){.#ifdef
15302 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
15303 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 VACUUM. return
15304 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b SQLITE_READONLY;
15305 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65 .#else. BtShare
15306 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
15307 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
15308 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d TE_OK;. u8 av =
15309 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b (u8)autoVacuum;
1530a 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
1530b 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 Enter(p);. if(
1530c 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 pBt->pageSizeFix
1530d 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21 ed && (av ?1:0)!
1530e 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d =pBt->autoVacuum
1530f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
15310 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
15311 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e }else{. pBt->
15312 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 autoVacuum = av
15313 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 ?1:0;. pBt->i
15314 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d ncrVacuum = av==
15315 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 2 ?1:0;. }. sq
15316 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15317 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
15318 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
15319 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 Return the valu
1531a 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76 e of the 'auto-v
1531b 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e acuum' property.
1531c 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 If auto-vacuum
1531d 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 is .** enabled 1
1531e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
1531f 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 herwise 0..*/.SQ
15320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15321 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
15322 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 AutoVacuum(Btree
15323 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c *p){.#ifdef SQL
15324 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
15325 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 52 UUM. return BTR
15326 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f EE_AUTOVACUUM_NO
15327 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 NE;.#else. int
15328 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 rc;. sqlite3Btr
15329 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 eeEnter(p);. rc
1532a 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 = (. (!p->pB
1532b 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 t->autoVacuum)?B
1532c 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
1532d 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 NONE:. (!p->p
1532e 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f Bt->incrVacuum)?
1532f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
15330 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 _FULL:. BTREE
15331 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 _AUTOVACUUM_INCR
15332 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 . );. sqlite3B
15333 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
15334 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 return rc;.#endi
15335 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 f.}.../*.** Get
15336 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 a reference to p
15337 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 74 Page1 of the dat
15338 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 abase file. Thi
15339 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 s will.** also a
1533a 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f 63 cquire a readloc
1533b 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a k on that file..
1533c 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
1533d 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
1533e 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 20 uccess. If the
1533f 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a file is not a.**
15340 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 well-formed dat
15341 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e abase file, then
15342 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
15343 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
15344 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
15345 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 64 eturned if the d
15346 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 atabase is locke
15347 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d d. SQLITE_NOMEM
15348 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 .** is returned
15349 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 if we run out of
1534a 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 memory. .*/.sta
1534b 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 tic int lockBtre
1534c 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 e(BtShared *pBt)
1534d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 {. int rc;. Me
1534e 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 mPage *pPage1;.
1534f 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 int nPage;.. a
15350 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15351 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
15352 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
15353 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d t( pBt->pPage1==
15354 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 0 );. rc = sqli
15355 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
15356 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c pBt, 1, &pPage1,
15357 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 0);. if( rc!=S
15358 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
15359 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 n rc;.. /* Do s
1535a 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 ome checking to
1535b 68 65 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20 help insure the
1535c 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72 file we opened r
1535d 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20 eally is. ** a
1535e 76 61 6c 69 64 20 64 61 74 61 62 61 73 65 20 66 valid database f
1535f 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72 63 20 ile. . */. rc
15360 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 = sqlite3PagerPa
15361 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 gecount(pBt->pPa
15362 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 ger, &nPage);.
15363 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15364 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61 K ){. goto pa
15365 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b ge1_init_failed;
15366 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 50 61 . }else if( nPa
15367 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 ge>0 ){. int
15368 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 6e pageSize;. in
15369 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 t usableSize;.
1536a 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70 50 u8 *page1 = pP
1536b 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 20 age1->aData;.
1536c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 rc = SQLITE_NOT
1536d 41 44 42 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d ADB;. if( mem
1536e 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 67 69 cmp(page1, zMagi
1536f 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d 30 20 cHeader, 16)!=0
15370 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
15371 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b ge1_init_failed;
15372 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
15373 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 age1[18]>1 ){.
15374 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c pBt->readOnl
15375 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 y = 1;. }.
15376 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 31 if( page1[19]>1
15377 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
15378 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
15379 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1537a 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d 62 65 The maximum embe
1537b 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 dded fraction mu
1537c 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 32 35 st be exactly 25
1537d 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 69 6e 69 %. And the mini
1537e 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62 65 64 mum. ** embed
1537f 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 ded fraction mus
15380 74 20 62 65 20 31 32 2e 35 25 20 66 6f 72 20 62 t be 12.5% for b
15381 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 20 61 6e oth leaf-data an
15382 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 2e d non-leaf-data.
15383 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 69 67 . ** The orig
15384 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c 6c 6f inal design allo
15385 77 65 64 20 74 68 65 73 65 20 61 6d 6f 75 6e 74 wed these amount
15386 73 20 74 6f 20 76 61 72 79 2c 20 62 75 74 20 61 s to vary, but a
15387 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65 72 73 s of. ** vers
15388 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 72 65 ion 3.6.0, we re
15389 71 75 69 72 65 20 74 68 65 6d 20 74 6f 20 62 65 quire them to be
1538a 20 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 fixed.. */.
1538b 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 if( memcmp(&p
1538c 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30 30 5c age1[21], "\100\
1538d 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 30 20 29 040\040",3)!=0 )
1538e 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 {. goto pag
1538f 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a e1_init_failed;.
15390 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 53 69 }. pageSi
15391 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 70 ze = get2byte(&p
15392 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20 20 69 age1[16]);. i
15393 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29 f( ((pageSize-1)
15394 26 70 61 67 65 53 69 7a 65 29 21 3d 30 20 7c 7c &pageSize)!=0 ||
15395 20 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c pageSize<512 ||
15396 0a 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 . (SQLITE
15397 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 3c 33 _MAX_PAGE_SIZE<3
15398 32 37 36 38 20 26 26 20 70 61 67 65 53 69 7a 65 2768 && pageSize
15399 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 >SQLITE_MAX_PAGE
1539a 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a 20 20 _SIZE). ){.
1539b 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 goto page1_i
1539c 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 nit_failed;.
1539d 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 }. assert( (p
1539e 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 ageSize & 7)==0
1539f 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a );. usableSiz
153a0 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70 e = pageSize - p
153a1 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66 age1[20];. if
153a2 28 20 70 61 67 65 53 69 7a 65 21 3d 70 42 74 2d ( pageSize!=pBt-
153a3 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 >pageSize ){.
153a4 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61 64 /* After read
153a5 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 ing the first pa
153a6 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
153a7 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70 61 se assuming a pa
153a8 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a 2a ge size. **
153a9 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61 67 of BtShared.pag
153aa 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20 64 eSize, we have d
153ab 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 74 iscovered that t
153ac 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 0a he page-size is.
153ad 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c ** actuall
153ae 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c 6f y pageSize. Unlo
153af 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c ck the database,
153b0 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67 leave pBt->pPag
153b1 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 7a e1 at. ** z
153b2 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 53 ero and return S
153b3 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63 61 QLITE_OK. The ca
153b4 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 74 ller will call t
153b5 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 his function.
153b6 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 68 ** again with
153b7 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 61 67 the correct pag
153b8 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a 2f e-size.. */
153b9 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
153ba 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20 ge(pPage1);.
153bb 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a pBt->usableSiz
153bc 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 e = (u16)usableS
153bd 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e ize;. pBt->
153be 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 pageSize = (u16)
153bf 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 pageSize;.
153c0 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 freeTempSpace(pB
153c1 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 t);. rc = s
153c2 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
153c3 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 gesize(pBt->pPag
153c4 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 er, &pBt->pageSi
153c5 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ze);. if( r
153c6 63 20 29 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 c ) goto page1_i
153c7 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 nit_failed;.
153c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
153c9 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 OK;. }. if
153ca 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 35 30 30 ( usableSize<500
153cb 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
153cc 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
153cd 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d ;. }. pBt-
153ce 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 >pageSize = (u16
153cf 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 )pageSize;. p
153d0 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d Bt->usableSize =
153d1 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a 65 (u16)usableSize
153d2 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
153d3 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
153d4 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 . pBt->autoVa
153d5 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 cuum = (get4byte
153d6 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 (&page1[36 + 4*4
153d7 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 ])?1:0);. pBt
153d8 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 ->incrVacuum = (
153d9 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31 5b get4byte(&page1[
153da 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 36 + 7*4])?1:0);
153db 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f .#endif. }.. /
153dc 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 * maxLocal is th
153dd 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 e maximum amount
153de 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 of payload to s
153df 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 tore locally for
153e0 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d . ** a cell. M
153e1 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 73 ake sure it is s
153e2 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 mall enough so t
153e3 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69 6e hat at least min
153e4 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c Fanout. ** cell
153e5 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f s can will fit o
153e6 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 20 n one page. We
153e7 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74 65 assume a 10-byte
153e8 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20 20 page header..
153e9 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20 70 ** Besides the p
153ea 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c ayload, the cell
153eb 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a must store:. *
153ec 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f 69 * 2-byte poi
153ed 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c nter to the cell
153ee 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 . ** 4-byte
153ef 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 child pointer.
153f0 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20 6e ** 9-byte n
153f1 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 Key value. **
153f2 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61 20 4-byte nData
153f3 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 value. ** 4
153f4 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 -byte overflow p
153f5 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a age pointer. **
153f6 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 So a cell consi
153f7 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65 20 sts of a 2-byte
153f8 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65 72 poiner, a header
153f9 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75 63 which is as muc
153fa 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 h as. ** 17 byt
153fb 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 es long, 0 to N
153fc 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 bytes of payload
153fd 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 , and an optiona
153fe 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c 6f l 4 byte overflo
153ff 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e w. ** page poin
15400 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d ter.. */. pBt-
15401 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 >maxLocal = (pBt
15402 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 ->usableSize-12)
15403 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 *64/255 - 23;.
15404 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 pBt->minLocal =
15405 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
15406 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 -12)*32/255 - 23
15407 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 ;. pBt->maxLeaf
15408 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
15409 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e ze - 35;. pBt->
1540a 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e minLeaf = (pBt->
1540b 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 usableSize-12)*3
1540c 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 2/255 - 23;. as
1540d 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 sert( pBt->maxLe
1540e 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 af + 23 <= MX_CE
1540f 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a LL_SIZE(pBt) );.
15410 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 pBt->pPage1 =
15411 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72 6e pPage1;. return
15412 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 SQLITE_OK;..pag
15413 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a e1_init_failed:.
15414 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
15415 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 age1);. pBt->pP
15416 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 age1 = 0;. retu
15417 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
15418 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 This routine wor
15419 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b 42 74 72 65 ks like lockBtre
1541a 65 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 e() except that
1541b 69 74 20 61 6c 73 6f 20 69 6e 76 6f 6b 65 73 20 it also invokes
1541c 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c the.** busy call
1541d 62 61 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 back if there is
1541e 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e lock contention
1541f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15420 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52 65 74 lockBtreeWithRet
15421 72 79 28 42 74 72 65 65 20 2a 70 52 65 66 29 7b ry(Btree *pRef){
15422 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
15423 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 TE_OK;.. assert
15424 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
15425 6c 64 73 4d 75 74 65 78 28 70 52 65 66 29 20 29 ldsMutex(pRef) )
15426 3b 0a 20 20 69 66 28 20 70 52 65 66 2d 3e 69 6e ;. if( pRef->in
15427 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e Trans==TRANS_NON
15428 45 20 29 7b 0a 20 20 20 20 75 38 20 69 6e 54 72 E ){. u8 inTr
15429 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 52 65 66 ansaction = pRef
1542a 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ->pBt->inTransac
1542b 74 69 6f 6e 3b 0a 20 20 20 20 62 74 72 65 65 49 tion;. btreeI
1542c 6e 74 65 67 72 69 74 79 28 70 52 65 66 29 3b 0a ntegrity(pRef);.
1542d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1542e 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 BtreeBeginTrans(
1542f 70 52 65 66 2c 20 30 29 3b 0a 20 20 20 20 70 52 pRef, 0);. pR
15430 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 ef->pBt->inTrans
15431 61 63 74 69 6f 6e 20 3d 20 69 6e 54 72 61 6e 73 action = inTrans
15432 61 63 74 69 6f 6e 3b 0a 20 20 20 20 70 52 65 66 action;. pRef
15433 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e ->inTrans = TRAN
15434 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 69 66 28 20 S_NONE;. if(
15435 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
15436 0a 20 20 20 20 20 20 70 52 65 66 2d 3e 70 42 74 . pRef->pBt
15437 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d ->nTransaction--
15438 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 74 72 65 ;. }. btre
15439 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 66 29 eIntegrity(pRef)
1543a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1543b 63 3b 0a 7d 0a 20 20 20 20 20 20 20 0a 0a 2f 2a c;.}. ../*
1543c 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 .** If there are
1543d 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
1543e 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65 20 61 cursors and we a
1543f 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 re not in the mi
15440 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 ddle.** of a tra
15441 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74 68 65 nsaction but the
15442 72 65 20 69 73 20 61 20 72 65 61 64 20 6c 6f 63 re is a read loc
15443 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
15444 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 e, then.** this
15445 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73 20 74 routine unrefs t
15446 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 he first page of
15447 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
15448 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 le which .** has
15449 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 72 the effect of r
1544a 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72 65 61 eleasing the rea
1544b 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 d lock..**.** If
1544c 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f there are any o
1544d 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73 6f utstanding curso
1544e 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 rs, this routine
1544f 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
15450 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 ** If there is a
15451 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 transaction in
15452 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73 20 72 progress, this r
15453 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
15454 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 p..*/.static voi
15455 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 d unlockBtreeIfU
15456 6e 75 73 65 64 28 42 74 53 68 61 72 65 64 20 2a nused(BtShared *
15457 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 pBt){. assert(
15458 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
15459 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
1545a 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 ;. if( pBt->inT
1545b 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e ransaction==TRAN
1545c 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70 S_NONE && pBt->p
1545d 43 75 72 73 6f 72 3d 3d 30 20 26 26 20 70 42 74 Cursor==0 && pBt
1545e 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 ->pPage1!=0 ){.
1545f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 if( sqlite3Pa
15460 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d gerRefcount(pBt-
15461 3e 70 50 61 67 65 72 29 3e 3d 31 20 29 7b 0a 20 >pPager)>=1 ){.
15462 20 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 assert( pBt
15463 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 ->pPage1->aData
15464 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 );. release
15465 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31 Page(pBt->pPage1
15466 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 );. }. pBt
15467 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 ->pPage1 = 0;.
15468 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 }.}../*.** Creat
15469 65 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 e a new database
1546a 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 by initializing
1546b 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
1546c 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 2e 0a of the.** file..
1546d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 */.static int ne
1546e 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 72 wDatabase(BtShar
1546f 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 ed *pBt){. MemP
15470 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69 age *pP1;. unsi
15471 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b gned char *data;
15472 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
15473 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 nPage;.. asser
15474 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15475 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
15476 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ) );. rc = sqli
15477 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
15478 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 t(pBt->pPager, &
15479 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 nPage);. if( rc
1547a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e !=SQLITE_OK || n
1547b 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 65 Page>0 ){. re
1547c 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 turn rc;. }. p
1547d 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 P1 = pBt->pPage1
1547e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 31 21 ;. assert( pP1!
1547f 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 =0 );. data = p
15480 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 P1->aData;. rc
15481 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
15482 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67 65 ite(pP1->pDbPage
15483 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
15484 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 70 turn rc;. memcp
15485 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 65 y(data, zMagicHe
15486 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 ader, sizeof(zMa
15487 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20 61 gicHeader));. a
15488 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a 4d ssert( sizeof(zM
15489 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36 20 agicHeader)==16
1548a 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 );. put2byte(&d
1548b 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 61 ata[16], pBt->pa
1548c 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b geSize);. data[
1548d 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 18] = 1;. data[
1548e 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 19] = 1;. asser
1548f 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 t( pBt->usableSi
15490 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a ze<=pBt->pageSiz
15491 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 e && pBt->usable
15492 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 Size+255>=pBt->p
15493 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 ageSize);. data
15494 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d [20] = (u8)(pBt-
15495 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d >pageSize - pBt-
15496 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 >usableSize);.
15497 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 data[21] = 64;.
15498 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a data[22] = 32;.
15499 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b data[23] = 32;
1549a 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b . memset(&data[
1549b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 24], 0, 100-24);
1549c 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c . zeroPage(pP1,
1549d 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f PTF_INTKEY|PTF_
1549e 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 LEAF|PTF_LEAFDAT
1549f 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 65 A );. pBt->page
154a0 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 SizeFixed = 1;.#
154a1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
154a2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
154a3 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 assert( pBt->aut
154a4 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 oVacuum==1 || pB
154a5 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 30 t->autoVacuum==0
154a6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
154a7 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 31 t->incrVacuum==1
154a8 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 || pBt->incrVac
154a9 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 34 uum==0 );. put4
154aa 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 byte(&data[36 +
154ab 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 56 4*4], pBt->autoV
154ac 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 79 acuum);. put4by
154ad 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 2a te(&data[36 + 7*
154ae 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 4], pBt->incrVac
154af 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 uum);.#endif. r
154b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
154b1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 .}../*.** Attemp
154b2 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77 t to start a new
154b3 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 transaction. A
154b4 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
154b5 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 20 n.** is started
154b6 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 if the second ar
154b7 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 72 gument is nonzer
154b8 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 72 o, otherwise a r
154b9 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 ead-.** transact
154ba 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 63 ion. If the sec
154bb 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ond argument is
154bc 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 78 2 or more and ex
154bd 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e 73 clusive.** trans
154be 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 action is starte
154bf 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 d, meaning that
154c0 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 no other process
154c1 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 is allowed.** t
154c2 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 o access the dat
154c3 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 69 abase. A preexi
154c4 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f sting transactio
154c5 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20 n may not be.**
154c6 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 6c upgraded to excl
154c7 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e 67 usive by calling
154c8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 20 this routine a
154c9 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 68 second time - th
154ca 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 79 e.** exclusivity
154cb 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b 73 flag only works
154cc 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e 73 for a new trans
154cd 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 action..**.** A
154ce 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
154cf 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 n must be starte
154d0 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 d before attempt
154d1 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 6e ing any .** chan
154d2 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 ges to the datab
154d3 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 ase. None of th
154d4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
154d5 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 6f ines .** will wo
154d6 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e rk unless a tran
154d7 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
154d8 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a 20 ed first:.**.**
154d9 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
154da 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a 2a eCreateTable().*
154db 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 * sqlite3Bt
154dc 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 29 reeCreateIndex()
154dd 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
154de 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 BtreeClearTable(
154df 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
154e0 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 3BtreeDropTable(
154e1 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
154e2 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a 2a 3BtreeInsert().*
154e3 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 * sqlite3Bt
154e4 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 20 reeDelete().**
154e5 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
154e6 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a 0a UpdateMeta().**.
154e7 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 6c ** If an initial
154e8 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 75 attempt to acqu
154e9 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 69 ire the lock fai
154ea 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c 6f ls because of lo
154eb 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a 2a ck contention.**
154ec 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 and the databas
154ed 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 e was previously
154ee 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 unlocked, then
154ef 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 invoke the busy
154f0 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 68 handler.** if th
154f1 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 74 ere is one. But
154f2 20 69 66 20 74 68 65 72 65 20 77 61 73 20 70 72 if there was pr
154f3 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 2d eviously a read-
154f4 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 lock, do not.**
154f5 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 invoke the busy
154f6 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 72 handler - just r
154f7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
154f8 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 20 Y. SQLITE_BUSY
154f9 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 is .** returned
154fa 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6c when there is al
154fb 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 ready a read-loc
154fc 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 k in order to av
154fd 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a oid a deadlock..
154fe 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 **.** Suppose th
154ff 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f 63 ere are two proc
15500 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 20 esses A and B.
15501 41 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 A has a read loc
15502 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 61 k and B has.** a
15503 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 reserved lock.
15504 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d B tries to prom
15505 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 65 ote to exclusive
15506 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 but is blocked
15507 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 27 because.** of A'
15508 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 20 s read lock. A
15509 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 tries to promote
1550a 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 74 to reserved but
1550b 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 42 is blocked by B
1550c 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 20 ..** One or the
1550d 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f other of the two
1550e 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 20 processes must
1550f 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 72 give way or ther
15510 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 70 e can be.** no p
15511 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 74 rogress. By ret
15512 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 55 urning SQLITE_BU
15513 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f 6b SY and not invok
15514 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 6c ing the busy cal
15515 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 20 lback.** when A
15516 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65 already has a re
15517 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 6f ad lock, we enco
15518 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 20 urage A to give
15519 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a 20 up and let B.**
1551a 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49 proceed..*/.SQLI
1551b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1551c 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e qlite3BtreeBegin
1551d 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c 20 Trans(Btree *p,
1551e 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 73 int wrflag){. s
1551f 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 3d qlite3 *pBlock =
15520 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 0;. BtShared *
15521 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
15522 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
15523 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 OK;.. sqlite3Bt
15524 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 reeEnter(p);. b
15525 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
15526 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 ;.. /* If the b
15527 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 tree is already
15528 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 in a write-trans
15529 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 20 action, or it.
1552a 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e ** is already in
1552b 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 a read-transact
1552c 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d 74 ion and a read-t
1552d 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 ransaction. **
1552e 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 is requested, th
1552f 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 is is a no-op..
15530 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 */. if( p->inT
15531 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
15532 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73 E || (p->inTrans
15533 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 20 ==TRANS_READ &&
15534 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 20 !wrflag) ){.
15535 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e goto trans_begun
15536 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 ;. }.. /* Writ
15537 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 e transactions a
15538 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 re not possible
15539 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 on a read-only d
1553a 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 28 atabase */. if(
1553b 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 pBt->readOnly &
1553c 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 & wrflag ){.
1553d 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 rc = SQLITE_READ
1553e 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 ONLY;. goto t
1553f 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a rans_begun;. }.
15540 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
15541 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
15542 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 E. /* If anothe
15543 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c r database handl
15544 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70 e has already op
15545 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61 ened a write tra
15546 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f nsaction . ** o
15547 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74 n this shared-bt
15548 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e ree structure an
15549 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65 d a second write
1554a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a transaction is.
1554b 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c 20 ** requested,
1554c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f return SQLITE_LO
1554d 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 CKED.. */. if(
1554e 20 28 77 72 66 6c 61 67 20 26 26 20 70 42 74 2d (wrflag && pBt-
1554f 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
15550 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20 TRANS_WRITE) ||
15551 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 29 pBt->isPending )
15552 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 {. pBlock = p
15553 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b Bt->pWriter->db;
15554 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 66 . }else if( wrf
15555 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c lag>1 ){. BtL
15556 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 ock *pIter;.
15557 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 for(pIter=pBt->p
15558 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 Lock; pIter; pIt
15559 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 er=pIter->pNext)
1555a 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 {. if( pIte
1555b 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b 0a r->pBtree!=p ){.
1555c 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d pBlock =
1555d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e pIter->pBtree->
1555e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 db;. brea
1555f 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
15560 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63 . }. if( pBloc
15561 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 k ){. sqlite3
15562 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 ConnectionBlocke
15563 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 d(p->db, pBlock)
15564 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
15565 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
15566 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74 ACHE;. goto t
15567 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a rans_begun;. }.
15568 23 65 6e 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20 #endif.. do {.
15569 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42 /* Call lockB
1556a 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69 74 tree() until eit
1556b 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31 20 her pBt->pPage1
1556c 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a is populated or.
1556d 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65 ** lockBtree
1556e 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 () returns somet
1556f 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 hing other than
15570 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 SQLITE_OK. lockB
15571 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61 tree(). ** ma
15572 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f y return SQLITE_
15573 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42 74 OK but leave pBt
15574 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f 20 ->pPage1 set to
15575 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 20 2a 0 if after. *
15576 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 31 * reading page 1
15577 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 74 68 it discovers th
15578 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 at the page-size
15579 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1557a 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69 73 . ** file is
1557b 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53 69 not pBt->pageSi
1557c 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ze. In this case
1557d 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69 6c lockBtree() wil
1557e 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a 20 l update. **
1557f 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f pBt->pageSize to
15580 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f the page-size o
15581 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 f the file on di
15582 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 sk.. */. w
15583 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 65 hile( pBt->pPage
15584 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 1==0 && SQLITE_O
15585 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 K==(rc = lockBtr
15586 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 20 ee(pBt)) );..
15587 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
15588 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a OK && wrflag ){.
15589 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 72 if( pBt->r
1558a 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 eadOnly ){.
1558b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 rc = SQLITE_R
1558c 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d EADONLY;. }
1558d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
1558e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 = sqlite3PagerB
1558f 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 65 72 egin(pBt->pPager
15590 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 74 65 ,wrflag>1,sqlite
15591 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 2d 3TempInMemory(p-
15592 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 20 69 >db));. i
15593 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15594 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
15595 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 = newDatabase(p
15596 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 Bt);. }.
15597 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a }. }. .
15598 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
15599 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 TE_OK ){. u
1559a 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 nlockBtreeIfUnus
1559b 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 ed(pBt);. }.
1559c 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c }while( rc==SQL
1559d 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d ITE_BUSY && pBt-
1559e 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
1559f 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 TRANS_NONE &&.
155a0 20 20 20 20 20 20 20 20 62 74 72 65 65 49 6e 76 btreeInv
155a1 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 70 okeBusyHandler(p
155a2 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 Bt) );.. if( rc
155a3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
155a4 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e if( p->inTran
155a5 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b s==TRANS_NONE ){
155a6 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 . pBt->nTra
155a7 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20 20 20 nsaction++;.
155a8 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 }. p->inTrans
155a9 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 = (wrflag?TRANS
155aa 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 _WRITE:TRANS_REA
155ab 44 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 D);. if( p->i
155ac 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 nTrans>pBt->inTr
155ad 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 ansaction ){.
155ae 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 pBt->inTransa
155af 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 ction = p->inTra
155b0 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 ns;. }.#ifnde
155b1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
155b2 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69 ARED_CACHE. i
155b3 66 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 f( wrflag ){.
155b4 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d assert( !pBt-
155b5 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 >pWriter );.
155b6 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d pBt->pWriter =
155b7 20 70 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 p;. pBt->i
155b8 73 45 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38 sExclusive = (u8
155b9 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 )(wrflag>1);.
155ba 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a }.#endif. }...
155bb 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 trans_begun:. i
155bc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
155bd 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 && wrflag ){.
155be 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d /* This call m
155bf 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74 akes sure that t
155c0 68 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65 he pager has the
155c1 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 correct number
155c2 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 of. ** open s
155c3 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 avepoints. If th
155c4 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
155c5 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 er is greater th
155c6 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 an 0 and. **
155c7 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
155c8 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
155c9 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c pen, then it wil
155ca 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 l be opened here
155cb 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
155cc 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 = sqlite3PagerOp
155cd 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d enSavepoint(pBt-
155ce 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e >pPager, p->db->
155cf 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d nSavepoint);. }
155d0 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 .. btreeIntegri
155d1 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 ty(p);. sqlite3
155d2 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
155d3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
155d4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
155d5 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f IT_AUTOVACUUM../
155d6 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 *.** Set the poi
155d7 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
155d8 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 for all childre
155d9 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e n of page pPage.
155da 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 Also, if.** pPa
155db 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c ge contains cell
155dc 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 s that point to
155dd 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 overflow pages,
155de 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a set the pointer.
155df 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 ** map entries f
155e0 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
155e1 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a pages as well..*
155e2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 /.static int set
155e3 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d ChildPtrmaps(Mem
155e4 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 Page *pPage){.
155e5 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
155e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
155e7 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 /* Counter va
155e8 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 riable */. int
155e9 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 nCell;
155ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
155eb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c * Number of cell
155ec 73 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 s in page pPage
155ed 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
155ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
155ef 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
155f0 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 n code */. BtSh
155f1 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 ared *pBt = pPag
155f2 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 e->pBt;. u8 isI
155f3 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d nitOrig = pPage-
155f4 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 >isInit;. Pgno
155f5 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 pgno = pPage->pg
155f6 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 no;.. assert( s
155f7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
155f8 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
155f9 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 tex) );. rc = s
155fa 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
155fb 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 69 66 age(pPage);. if
155fc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
155fd 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 5f ){. goto set_
155fe 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 child_ptrmaps_ou
155ff 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d t;. }. nCell =
15600 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a pPage->nCell;..
15601 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
15602 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 ll; i++){. u8
15603 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 *pCell = findCe
15604 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 ll(pPage, i);..
15605 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
15606 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c 20 tOvflPtr(pPage,
15607 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 pCell);. if(
15608 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15609 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 74 5f . goto set_
1560a 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 child_ptrmaps_ou
1560b 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 t;. }.. if
1560c 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
1560d 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 {. Pgno chi
1560e 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 ldPgno = get4byt
1560f 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 e(pCell);.
15610 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 rc = ptrmapPut(p
15611 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 Bt, childPgno, P
15612 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e TRMAP_BTREE, pgn
15613 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 o);. if( rc
15614 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
15615 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 to set_child_ptr
15616 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a maps_out;. }.
15617 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 }.. if( !pPag
15618 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 e->leaf ){. P
15619 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 gno childPgno =
1561a 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
1561b 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
1561c 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
1561d 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 rc = ptrmapPut(
1561e 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 pBt, childPgno,
1561f 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 PTRMAP_BTREE, pg
15620 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 no);. }..set_ch
15621 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a ild_ptrmaps_out:
15622 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 . pPage->isInit
15623 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 = isInitOrig;.
15624 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15625 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f *.** Somewhere o
15626 6e 20 70 50 61 67 65 2c 20 77 68 69 63 68 20 69 n pPage, which i
15627 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 s guaranteed to
15628 62 65 20 61 20 62 74 72 65 65 20 70 61 67 65 2c be a btree page,
15629 20 6e 6f 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 not an overflow
1562a 0a 2a 2a 20 70 61 67 65 2c 20 69 73 20 61 20 70 .** page, is a p
1562b 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 ointer to page i
1562c 46 72 6f 6d 2e 20 4d 6f 64 69 66 79 20 74 68 69 From. Modify thi
1562d 73 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 s pointer so tha
1562e 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a t it points to.*
1562f 2a 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 * iTo. Parameter
15630 20 65 54 79 70 65 20 64 65 73 63 72 69 62 65 73 eType describes
15631 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 the type of poi
15632 6e 74 65 72 20 74 6f 20 62 65 20 6d 6f 64 69 66 nter to be modif
15633 69 65 64 2c 20 61 73 20 0a 2a 2a 20 66 6f 6c 6c ied, as .** foll
15634 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 ows:.**.** PTRMA
15635 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 P_BTREE: pPa
15636 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 ge is a btree-pa
15637 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 ge. The pointer
15638 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c points at a chil
15639 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 d .**
1563a 20 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 page of
1563b 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 pPage..**.** PTR
1563c 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 MAP_OVERFLOW1: p
1563d 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d Page is a btree-
1563e 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 page. The pointe
1563f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f r points at an o
15640 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 verflow.**
15641 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
15642 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 e pointed to by
15643 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 one of the cells
15644 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a on pPage..**.**
15645 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
15646 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 2: pPage is an o
15647 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 verflow-page. Th
15648 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 e pointer points
15649 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 at the next.**
1564a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1564b 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 overflow page
1564c 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a in the list..*/.
1564d 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 static int modif
1564e 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d yPagePointer(Mem
1564f 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e Page *pPage, Pgn
15650 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 o iFrom, Pgno iT
15651 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 o, u8 eType){.
15652 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15653 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
15654 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
15655 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15656 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
15657 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
15658 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 e) );. if( eTyp
15659 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
1565a 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 OW2 ){. /* Th
1565b 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 e pointer is alw
1565c 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 20 ays the first 4
1565d 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 bytes of the pag
1565e 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 e in this case.
1565f 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 */. if( get4
15660 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 byte(pPage->aDat
15661 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 a)!=iFrom ){.
15662 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15663 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15664 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 }. put4byt
15665 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 e(pPage->aData,
15666 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 iTo);. }else{.
15667 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 u8 isInitOrig
15668 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 = pPage->isInit
15669 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 ;. int i;.
1566a 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 int nCell;..
1566b 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
1566c 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 tPage(pPage);.
1566d 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d nCell = pPage-
1566e 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 >nCell;.. for
1566f 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 (i=0; i<nCell; i
15670 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 ++){. u8 *p
15671 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
15672 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 pPage, i);.
15673 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
15674 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a AP_OVERFLOW1 ){.
15675 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f CellInfo
15676 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73 info;. s
15677 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
15678 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
15679 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 Cell, &info);.
1567a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 if( info.i
1567b 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
1567c 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d if( iFrom=
1567d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c =get4byte(&pCell
1567e 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
1567f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
15680 20 70 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c put4byte(&pCell
15681 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
15682 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 , iTo);.
15683 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
15684 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
15685 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
15686 20 20 20 20 20 20 69 66 28 20 67 65 74 34 62 79 if( get4by
15687 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d te(pCell)==iFrom
15688 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 ){. pu
15689 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 t4byte(pCell, iT
1568a 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 o);. br
1568b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
1568c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a }. }. .
1568d 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c if( i==nCell
1568e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54 ){. if( eT
1568f 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 ype!=PTRMAP_BTRE
15690 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 E || .
15691 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
15692 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
15693 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 rOffset+8])!=iFr
15694 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 om ){. re
15695 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15696 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
15697 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 }. put4byte
15698 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
15699 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
1569a 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 8], iTo);. }.
1569b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
1569c 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b it = isInitOrig;
1569d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1569e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
1569f 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e ** Move the open
156a0 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 database page p
156a1 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69 DbPage to locati
156a2 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 6e 20 on iFreePage in
156a3 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 the .** database
156a4 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 72 65 . The pDbPage re
156a5 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20 ference remains
156a6 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 valid..*/.static
156a7 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 int relocatePag
156a8 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
156a9 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a Bt, /*
156aa 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 Btree */. MemP
156ab 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 age *pDbPage,
156ac 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 /* Open pag
156ad 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 e to move */. u
156ae 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 8 eType,
156af 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
156b0 65 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e er map 'type' en
156b1 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 try for pDbPage
156b2 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 */. Pgno iPtrPa
156b3 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ge, /*
156b4 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 Pointer map 'pa
156b5 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 ge-no' entry for
156b6 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 pDbPage */. Pg
156b7 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 no iFreePage,
156b8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f /* The lo
156b9 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 cation to move p
156ba 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 DbPage to */. i
156bb 6e 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b 0a 20 nt isCommit.){.
156bc 20 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 MemPage *pPtrPa
156bd 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 ge; /* The pag
156be 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
156bf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 a pointer to pDb
156c0 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 Page */. Pgno i
156c1 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 DbPage = pDbPage
156c2 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 ->pgno;. Pager
156c3 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 *pPager = pBt->p
156c4 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b Pager;. int rc;
156c5 0a 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 .. assert( eTyp
156c6 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
156c7 4f 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 OW2 || eType==PT
156c8 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c RMAP_OVERFLOW1 |
156c9 7c 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d | . eType==
156ca 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 PTRMAP_BTREE ||
156cb 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f eType==PTRMAP_RO
156cc 4f 54 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 OTPAGE );. asse
156cd 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
156ce 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
156cf 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
156d0 70 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 pDbPage->pBt==pB
156d1 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 t );.. /* Move
156d2 70 61 67 65 20 69 44 62 50 61 67 65 20 66 72 6f page iDbPage fro
156d3 6d 20 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f m its current lo
156d4 63 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e cation to page n
156d5 75 6d 62 65 72 20 69 46 72 65 65 50 61 67 65 20 umber iFreePage
156d6 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 */. TRACE(("AUT
156d7 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 OVACUUM: Moving
156d8 25 64 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 %d to free page
156d9 25 64 20 28 70 74 72 20 70 61 67 65 20 25 64 20 %d (ptr page %d
156da 74 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 type %d)\n", .
156db 20 20 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 iDbPage, iFr
156dc 65 65 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 eePage, iPtrPage
156dd 2c 20 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 , eType));. rc
156de 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f = sqlite3PagerMo
156df 76 65 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 vepage(pPager, p
156e0 44 62 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c DbPage->pDbPage,
156e1 20 69 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f iFreePage, isCo
156e2 6d 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 mmit);. if( rc!
156e3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
156e4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
156e5 0a 20 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f . pDbPage->pgno
156e6 20 3d 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 = iFreePage;..
156e7 20 2f 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 /* If pDbPage w
156e8 61 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c as a btree-page,
156e9 20 74 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 then it may hav
156ea 65 20 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e e child pages an
156eb 64 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 d/or cells. **
156ec 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 that point to ov
156ed 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 erflow pages. Th
156ee 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e e pointer map en
156ef 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 tries for all th
156f0 65 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e ese. ** pages n
156f1 65 65 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 eed to be change
156f2 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 d.. **. ** If
156f3 70 44 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 pDbPage is an ov
156f4 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 erflow page, the
156f5 6e 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 n the first 4 by
156f6 74 65 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a tes may store a.
156f7 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** pointer to
156f8 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 a subsequent ove
156f9 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 rflow page. If t
156fa 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c his is the case,
156fb 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 then. ** the p
156fc 6f 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 ointer map needs
156fd 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 to be updated f
156fe 6f 72 20 74 68 65 20 73 75 62 73 65 71 75 65 6e or the subsequen
156ff 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e t overflow page.
15700 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 . */. if( eTyp
15701 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 e==PTRMAP_BTREE
15702 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 || eType==PTRMAP
15703 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 _ROOTPAGE ){.
15704 20 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 rc = setChildPt
15705 72 6d 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a rmaps(pDbPage);.
15706 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
15707 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
15708 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
15709 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e }else{. Pgn
1570a 6f 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 o nextOvfl = get
1570b 34 62 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 4byte(pDbPage->a
1570c 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e Data);. if( n
1570d 65 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 extOvfl!=0 ){.
1570e 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 rc = ptrmapP
1570f 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c ut(pBt, nextOvfl
15710 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f , PTRMAP_OVERFLO
15711 57 32 2c 20 69 46 72 65 65 50 61 67 65 29 3b 0a W2, iFreePage);.
15712 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
15713 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15714 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
15715 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
15716 0a 20 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 . /* Fix the da
15717 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f tabase pointer o
15718 6e 20 70 61 67 65 20 69 50 74 72 50 61 67 65 20 n page iPtrPage
15719 74 68 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 that pointed at
1571a 69 44 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 iDbPage so. **
1571b 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 that it points a
1571c 74 20 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 t iFreePage. Als
1571d 6f 20 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 o fix the pointe
1571e 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a r map entry for.
1571f 20 20 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 ** iPtrPage..
15720 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 */. if( eType!
15721 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 =PTRMAP_ROOTPAGE
15722 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
15723 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
15724 28 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 (pBt, iPtrPage,
15725 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 &pPtrPage, 0);.
15726 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
15727 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
15728 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
15729 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1572a 61 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 agerWrite(pPtrPa
1572b 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
1572c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1572d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c _OK ){. rel
1572e 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 easePage(pPtrPag
1572f 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e e);. return
15730 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 rc;. }. r
15731 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f c = modifyPagePo
15732 69 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 inter(pPtrPage,
15733 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 iDbPage, iFreePa
15734 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 ge, eType);.
15735 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 releasePage(pPtr
15736 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
15737 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
15738 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
15739 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 pPut(pBt, iFreeP
1573a 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 age, eType, iPtr
1573b 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Page);. }. }
1573c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1573d 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c ./* Forward decl
1573e 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 aration required
1573f 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 by incrVacuumSt
15740 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 ep(). */.static
15741 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 int allocateBtre
15742 65 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a ePage(BtShared *
15743 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 , MemPage **, Pg
15744 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b no *, Pgno, u8);
15745 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 ../*.** Perform
15746 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 a single step of
15747 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d an incremental-
15748 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 vacuum. If succe
15749 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e ssful,.** return
1574a 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 SQLITE_OK. If t
1574b 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 here is no work
1574c 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 to do (and there
1574d 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 fore no.** point
1574e 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 in calling this
1574f 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 function again)
15750 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
15751 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 DONE..**.** More
15752 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 specificly, thi
15753 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d s function attem
15754 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 pts to re-organi
15755 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 ze the .** datab
15756 61 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 ase so that the
15757 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 last page of the
15758 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 file currently
15759 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 in use.** is no
1575a 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a longer in use..*
1575b 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e *.** If the nFin
1575c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
1575d 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d 70 6c n-zero, the impl
1575e 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d ementation assum
1575f 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 es.** that the c
15760 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 aller will keep
15761 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 calling incrVacu
15762 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a umStep() until.*
15763 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c * it returns SQL
15764 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 ITE_DONE or an e
15765 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e rror, and that n
15766 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 Fin is the.** nu
15767 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 mber of pages th
15768 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
15769 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 will contain aft
1576a 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 er this .** proc
1576b 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e ess is complete.
1576c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 .*/.static int i
1576d 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 42 74 ncrVacuumStep(Bt
1576e 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
1576f 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 o nFin, Pgno iLa
15770 73 74 50 67 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 stPg){. Pgno nF
15771 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 reeList;
15772 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
15773 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 pages still on t
15774 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a he free-list */.
15775 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15776 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
15777 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
15778 73 73 65 72 74 28 20 69 4c 61 73 74 50 67 3e 6e ssert( iLastPg>n
15779 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 20 21 50 Fin );.. if( !P
1577a 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
1577b 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20 69 4c , iLastPg) && iL
1577c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 astPg!=PENDING_B
1577d 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
1577e 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 . int rc;.
1577f 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 50 u8 eType;. P
15780 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a 0a 20 gno iPtrPage;..
15781 20 20 20 6e 46 72 65 65 4c 69 73 74 20 3d 20 67 nFreeList = g
15782 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
15783 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 age1->aData[36])
15784 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 4c ;. if( nFreeL
15785 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ist==0 ){.
15786 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
15787 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 NE;. }.. r
15788 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 c = ptrmapGet(pB
15789 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 65 54 79 t, iLastPg, &eTy
1578a 70 65 2c 20 26 69 50 74 72 50 61 67 65 29 3b 0a pe, &iPtrPage);.
1578b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1578c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1578d 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1578e 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
1578f 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 TRMAP_ROOTPAGE )
15790 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
15791 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15792 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 PT;. }.. i
15793 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
15794 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 _FREEPAGE ){.
15795 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29 if( nFin==0 )
15796 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d {. /* Rem
15797 6f 76 65 20 74 68 65 20 70 61 67 65 20 66 72 6f ove the page fro
15798 6d 20 74 68 65 20 66 69 6c 65 73 20 66 72 65 65 m the files free
15799 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 6e -list. This is n
1579a 6f 74 20 72 65 71 75 69 72 65 64 0a 20 20 20 20 ot required.
1579b 20 20 20 20 2a 2a 20 69 66 20 6e 46 69 6e 20 69 ** if nFin i
1579c 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 s non-zero. In t
1579d 68 61 74 20 63 61 73 65 2c 20 74 68 65 20 66 72 hat case, the fr
1579e 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 0a ee-list will be.
1579f 20 20 20 20 20 20 20 20 2a 2a 20 74 72 75 6e 63 ** trunc
157a0 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 61 66 74 ated to zero aft
157a1 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e er this function
157a2 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 69 74 20 returns, so it
157a3 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 20 20 20 doesn't .
157a4 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 20 69 74 ** matter if it
157a5 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 still contains
157a6 73 6f 6d 65 20 67 61 72 62 61 67 65 20 65 6e 74 some garbage ent
157a7 72 69 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f ries.. */
157a8 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 . Pgno iF
157a9 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 4d reePg;. M
157aa 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b emPage *pFreePg;
157ab 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c . rc = al
157ac 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
157ad 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 pBt, &pFreePg, &
157ae 69 46 72 65 65 50 67 2c 20 69 4c 61 73 74 50 67 iFreePg, iLastPg
157af 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 1);. if
157b0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
157b1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
157b2 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
157b3 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
157b4 28 20 69 46 72 65 65 50 67 3d 3d 69 4c 61 73 74 ( iFreePg==iLast
157b5 50 67 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 Pg );. re
157b6 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50 leasePage(pFreeP
157b7 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 g);. }.
157b8 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 50 } else {. P
157b9 67 6e 6f 20 69 46 72 65 65 50 67 3b 20 20 20 20 gno iFreePg;
157ba 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
157bb 78 20 6f 66 20 66 72 65 65 20 70 61 67 65 20 74 x of free page t
157bc 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50 67 20 74 o move pLastPg t
157bd 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 o */. MemPa
157be 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a 0a 20 20 ge *pLastPg;..
157bf 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
157c0 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
157c1 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 , iLastPg, &pLas
157c2 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 tPg, 0);. i
157c3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
157c4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
157c5 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a rn rc;. }..
157c6 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e /* If nFin
157c7 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c is zero, this l
157c8 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79 oop runs exactly
157c9 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70 once and page p
157ca 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 LastPg. **
157cb 69 73 20 73 77 61 70 70 65 64 20 77 69 74 68 20 is swapped with
157cc 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 70 the first free p
157cd 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 age pulled off t
157ce 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 he free list..
157cf 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
157d0 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
157d1 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72 d, if nFin is gr
157d2 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c eater than zero,
157d3 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 then keep.
157d4 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 ** looping unti
157d5 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f l a free-page lo
157d6 63 61 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 cated within the
157d7 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65 first nFin page
157d8 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 s. ** of th
157d9 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e e file is found.
157da 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
157db 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d do {. Mem
157dc 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 Page *pFreePg;.
157dd 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f rc = allo
157de 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
157df 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 t, &pFreePg, &iF
157e0 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 reePg, 0, 0);.
157e1 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
157e2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
157e3 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
157e4 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 (pLastPg);.
157e5 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
157e6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
157e7 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 releasePage(pF
157e8 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 reePg);. }w
157e9 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 hile( nFin!=0 &&
157ea 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b iFreePg>nFin );
157eb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
157ec 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 FreePg<iLastPg )
157ed 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 ;. . r
157ee 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
157ef 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 Write(pLastPg->p
157f0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
157f1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
157f2 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
157f3 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 relocatePage(pB
157f4 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70 t, pLastPg, eTyp
157f5 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69 46 72 e, iPtrPage, iFr
157f6 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a eePg, nFin!=0);.
157f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
157f8 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 leasePage(pLastP
157f9 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 g);. if( rc
157fa 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
157fb 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
157fc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
157fd 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d }.. if( nFin=
157fe 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50 =0 ){. iLastP
157ff 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 g--;. while(
15800 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 iLastPg==PENDING
15801 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c _BYTE_PAGE(pBt)|
15802 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 |PTRMAP_ISPAGE(p
15803 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a Bt, iLastPg) ){.
15804 20 20 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 if( PTRMAP
15805 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 _ISPAGE(pBt, iLa
15806 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20 stPg) ){.
15807 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20 MemPage *pPg;.
15808 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 int rc =
15809 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
1580a 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 age(pBt, iLastPg
1580b 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 20 20 , &pPg, 0);.
1580c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1580d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1580e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1580f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
15810 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15811 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 rWrite(pPg->pDbP
15812 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 age);. re
15813 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a leasePage(pPg);.
15814 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
15815 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15816 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
15817 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
15818 20 20 7d 0a 20 20 20 20 20 20 69 4c 61 73 74 50 }. iLastP
15819 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 g--;. }. s
1581a 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 qlite3PagerTrunc
1581b 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 ateImage(pBt->pP
1581c 61 67 65 72 2c 20 69 4c 61 73 74 50 67 29 3b 0a ager, iLastPg);.
1581d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
1581e 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1581f 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 A write-transac
15820 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 tion must be ope
15821 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 ned before calli
15822 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ng this function
15823 2e 0a 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 ..** It performs
15824 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f a single unit o
15825 66 20 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 f work towards a
15826 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 n incremental va
15827 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 cuum..**.** If t
15828 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 he incremental v
15829 61 63 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 acuum is finishe
1582a 64 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e d after this fun
1582b 63 74 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a ction has run,.*
1582c 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 * SQLITE_DONE is
1582d 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 returned. If it
1582e 20 69 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 is not finished
1582f 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f , but no error o
15830 63 63 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 ccurred,.** SQLI
15831 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
15832 64 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 d. Otherwise an
15833 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
15834 65 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 e. .*/.SQLITE_PR
15835 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15836 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 3BtreeIncrVacuum
15837 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e (Btree *p){. in
15838 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
15839 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1583a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1583b 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 nter(p);. asser
1583c 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
1583d 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
1583e 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 TE && p->inTrans
1583f 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
15840 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74 . if( !pBt->aut
15841 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 oVacuum ){. r
15842 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
15843 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
15844 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 validateAllOverf
15845 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 lowCache(pBt);.
15846 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 rc = incrVacu
15847 75 6d 53 74 65 70 28 70 42 74 2c 20 30 2c 20 70 umStep(pBt, 0, p
15848 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
15849 74 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 t));. }. sqlit
1584a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1584b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1584c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
1584d 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 70 72 ine is called pr
1584e 69 6f 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 ior to sqlite3Pa
1584f 67 65 72 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61 gerCommit when a
15850 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
15851 69 73 20 63 6f 6d 6d 69 74 65 64 20 66 6f 72 20 is commited for
15852 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
15853 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 atabase..**.** I
15854 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 f SQLITE_OK is r
15855 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 eturned, then *p
15856 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f nTrunc is set to
15857 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
15858 61 67 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 ages.** the data
15859 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 base file should
1585a 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f be truncated to
1585b 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 6d during the comm
1585c 69 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 it process. .**
1585d 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62 61 73 i.e. the databas
1585e 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f 72 67 e has been reorg
1585f 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74 20 6f anized so that o
15860 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 2a 70 nly the first *p
15861 6e 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65 73 20 nTrunc.** pages
15862 61 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 are in use..*/.s
15863 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f 56 61 tatic int autoVa
15864 63 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53 68 61 cuumCommit(BtSha
15865 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 red *pBt){. int
15866 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
15867 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
15868 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a = pBt->pPager;.
15869 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 VVA_ONLY( int
1586a 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61 nRef = sqlite3Pa
1586b 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 gerRefcount(pPag
1586c 65 72 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 er) );.. assert
1586d 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1586e 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
1586f 20 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 );. invalidate
15870 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
15871 28 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28 (pBt);. assert(
15872 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
15873 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e ;. if( !pBt->in
15874 63 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 crVacuum ){.
15875 50 67 6e 6f 20 6e 46 69 6e 3b 0a 20 20 20 20 50 Pgno nFin;. P
15876 67 6e 6f 20 6e 46 72 65 65 3b 0a 20 20 20 20 50 gno nFree;. P
15877 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 0a 20 20 20 gno nPtrmap;.
15878 20 50 67 6e 6f 20 69 46 72 65 65 3b 0a 20 20 20 Pgno iFree;.
15879 20 63 6f 6e 73 74 20 69 6e 74 20 70 67 73 7a 20 const int pgsz
1587a 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b = pBt->pageSize;
1587b 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67 20 . Pgno nOrig
1587c 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 = pagerPagecount
1587d 28 70 42 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 (pBt);.. if(
1587e 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
1587f 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e 4f 72 t, nOrig) || nOr
15880 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 ig==PENDING_BYTE
15881 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 _PAGE(pBt) ){.
15882 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 /* It is not
15883 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 72 65 possible to cre
15884 61 74 65 20 61 20 64 61 74 61 62 61 73 65 20 66 ate a database f
15885 6f 72 20 77 68 69 63 68 20 74 68 65 20 66 69 6e or which the fin
15886 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a al page. **
15887 20 69 73 20 65 69 74 68 65 72 20 61 20 70 6f 69 is either a poi
15888 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 6f 72 nter-map page or
15889 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62 79 74 the pending-byt
1588a 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 0a 20 e page. If one.
1588b 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 ** is encou
1588c 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69 6e 64 ntered, this ind
1588d 69 63 61 74 65 73 20 63 6f 72 72 75 70 74 69 6f icates corruptio
1588e 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n.. */.
1588f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15890 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
15891 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65 20 3d }.. nFree =
15892 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e get4byte(&pBt->
15893 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
15894 5d 29 3b 0a 20 20 20 20 6e 50 74 72 6d 61 70 20 ]);. nPtrmap
15895 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b 50 = (nFree-nOrig+P
15896 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
15897 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f 35 29 , nOrig)+pgsz/5)
15898 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20 20 6e /(pgsz/5);. n
15899 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46 Fin = nOrig - nF
1589a 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20 ree - nPtrmap;.
1589b 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e if( nOrig>PEN
1589c 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
1589d 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44 Bt) && nFin<PEND
1589e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
1589f 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e t) ){. nFin
158a0 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 --;. }. wh
158a1 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 ile( PTRMAP_ISPA
158a2 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c GE(pBt, nFin) ||
158a3 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 nFin==PENDING_B
158a4 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
158a5 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 . nFin--;.
158a6 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 46 }.. for(iF
158a7 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72 65 65 ree=nOrig; iFree
158a8 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53 51 4c >nFin && rc==SQL
158a9 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d 29 ITE_OK; iFree--)
158aa 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e 63 {. rc = inc
158ab 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74 2c rVacuumStep(pBt,
158ac 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b 0a 20 nFin, iFree);.
158ad 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72 63 }. if( (rc
158ae 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c 7c ==SQLITE_DONE ||
158af 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 rc==SQLITE_OK)
158b0 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 20 && nFree>0 ){.
158b1 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
158b2 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 OK;. rc = s
158b3 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
158b4 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 (pBt->pPage1->pD
158b5 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 75 bPage);. pu
158b6 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
158b7 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 ge1->aData[32],
158b8 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 0);. put4by
158b9 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
158ba 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b 0a >aData[36], 0);.
158bb 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
158bc 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 erTruncateImage(
158bd 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 46 69 pBt->pPager, nFi
158be 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 n);. }. if
158bf 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
158c0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
158c1 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 PagerRollback(pP
158c2 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ager);. }. }
158c3 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 66 .. assert( nRef
158c4 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 ==sqlite3PagerRe
158c5 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 fcount(pPager) )
158c6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
158c7 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 ..#endif /* ifnd
158c8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
158c9 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a UTOVACUUM */../*
158ca 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
158cb 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 does the first
158cc 70 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 phase of a two-p
158cd 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 hase commit. Th
158ce 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 is routine.** ca
158cf 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 uses a rollback
158d0 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 journal to be cr
158d1 65 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 eated (if it doe
158d2 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 s not already ex
158d3 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 ist).** and popu
158d4 6c 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 lated with enoug
158d5 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f h information so
158d6 20 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 that if a power
158d7 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 loss occurs.**
158d8 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e the database can
158d9 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 be restored to
158da 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 its original sta
158db 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 te by playing ba
158dc 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 ck.** the journa
158dd 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e l. Then the con
158de 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 tents of the jou
158df 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 rnal are flushed
158e0 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 out to.** the d
158e1 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 isk. After the
158e2 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c journal is safel
158e3 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 y on oxide, the
158e4 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a changes to the.*
158e5 2a 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 * database are w
158e6 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
158e7 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
158e8 64 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 d flushed to oxi
158e9 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e de..** At the en
158ea 64 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 d of this call,
158eb 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
158ec 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 rnal still exist
158ed 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b s on the.** disk
158ee 20 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c and we are stil
158ef 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f l holding all lo
158f0 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e cks, so the tran
158f1 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a saction has not.
158f2 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 ** committed. S
158f3 65 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ee sqlite3BtreeC
158f4 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 ommitPhaseTwo()
158f5 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 for the second p
158f6 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 hase of the.** c
158f7 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a ommit process..*
158f8 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 *.** This call i
158f9 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 s a no-op if no
158fa 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
158fb 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 n is currently a
158fc 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a ctive on pBt..**
158fd 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 .** Otherwise, s
158fe 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 ync the database
158ff 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 file for the bt
15900 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 ree pBt. zMaster
15901 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 points to.** th
15902 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 e name of a mast
15903 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
15904 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 that should be w
15905 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a ritten into the.
15906 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f ** individual jo
15907 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 urnal file, or i
15908 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 s NULL, indicati
15909 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 ng no master jou
1590a 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 rnal file .** (s
1590b 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 ingle database t
1590c 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a ransaction)..**.
1590d 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 ** When this is
1590e 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 called, the mast
1590f 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c er journal shoul
15910 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 d already have b
15911 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 een.** created,
15912 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 populated with t
15913 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e his journal poin
15914 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 ter and synced t
15915 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e o disk..**.** On
15916 63 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 ce this is routi
15917 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c ne has returned,
15918 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 the only thing
15919 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d required to comm
1591a 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d it.** the write-
1591b 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 transaction for
1591c 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 this database fi
1591d 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 le is to delete
1591e 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a the journal..*/.
1591f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15920 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
15921 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 ommitPhaseOne(Bt
15922 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 ree *p, const ch
15923 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
15924 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
15925 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 OK;. if( p->inT
15926 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
15927 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 E ){. BtShare
15928 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
15929 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1592a 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 eEnter(p);.#ifnd
1592b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1592c 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
1592d 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
1592e 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 m ){. rc =
1592f 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 autoVacuumCommit
15930 28 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 (pBt);. if(
15931 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15932 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
15933 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
15934 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
15935 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
15936 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d .#endif. rc =
15937 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
15938 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d mitPhaseOne(pBt-
15939 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 >pPager, zMaster
1593a 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
1593b 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1593c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1593d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 .}../*.** Commit
1593e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1593f 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 72 currently in pr
15940 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ogress..**.** Th
15941 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 is routine imple
15942 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f 6e 64 ments the second
15943 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d 70 68 phase of a 2-ph
15944 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 65 ase commit. The
15945 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 .** sqlite3Btree
15946 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 CommitPhaseOne()
15947 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 routine does th
15948 65 20 66 69 72 73 74 20 70 68 61 73 65 20 61 6e e first phase an
15949 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 69 d should.** be i
1594a 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 nvoked prior to
1594b 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 calling this rou
1594c 74 69 6e 65 2e 20 20 54 68 65 20 73 71 6c 69 74 tine. The sqlit
1594d 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1594e 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 69 seOne().** routi
1594f 6e 65 20 64 69 64 20 61 6c 6c 20 74 68 65 20 77 ne did all the w
15950 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 ork of writing i
15951 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 nformation out t
15952 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 o disk and flush
15953 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 ing the.** conte
15954 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 nts so that they
15955 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 are written ont
15956 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 o the disk platt
15957 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a er. All this.**
15958 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 routine has to
15959 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 do is delete or
1595a 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f truncate or zero
1595b 20 74 68 65 20 68 65 61 64 65 72 20 69 6e 20 74 the header in t
1595c 68 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 he.** the rollba
1595d 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 69 63 ck journal (whic
1595e 68 20 63 61 75 73 65 73 20 74 68 65 20 74 72 61 h causes the tra
1595f 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d nsaction to comm
15960 69 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 20 it) and.** drop
15961 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 locks..**.** Thi
15962 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 s will release t
15963 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e he write lock on
15964 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
15965 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a le. If there.**
15966 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 are no active c
15967 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 ursors, it also
15968 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 releases the rea
15969 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 d lock..*/.SQLIT
1596a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1596b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
1596c 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 20 2a PhaseTwo(Btree *
1596d 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a p){. BtShared *
1596e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 pBt = p->pBt;..
1596f 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
15970 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e er(p);. btreeIn
15971 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f tegrity(p);.. /
15972 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 * If the handle
15973 68 61 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e has a write-tran
15974 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f saction open, co
15975 6d 6d 69 74 20 74 68 65 20 73 68 61 72 65 64 2d mmit the shared-
15976 62 74 72 65 65 73 20 0a 20 20 2a 2a 20 74 72 61 btrees . ** tra
15977 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 73 65 74 nsaction and set
15978 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 the shared stat
15979 65 20 74 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e e to TRANS_READ.
1597a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 . */. if( p->i
1597b 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
1597c 49 54 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ITE ){. int r
1597d 63 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 c;. assert( p
1597e 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1597f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
15980 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
15981 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e t->nTransaction>
15982 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 0 );. rc = sq
15983 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
15984 50 68 61 73 65 54 77 6f 28 70 42 74 2d 3e 70 50 PhaseTwo(pBt->pP
15985 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ager);. if( r
15986 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
15987 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
15988 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 eeLeave(p);.
15989 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1598a 20 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 }. pBt->inTr
1598b 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
1598c 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 2f S_READ;. }.. /
1598d 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 * If the handle
1598e 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 has any kind of
1598f 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e transaction open
15990 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 , decrement the
15991 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a transaction. **
15992 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 count of the sh
15993 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 ared btree. If t
15994 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 he transaction c
15995 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30 2c 20 ount reaches 0,
15996 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 68 61 set. ** the sha
15997 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 red state to TRA
15998 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c NS_NONE. The unl
15999 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 ockBtreeIfUnused
1599a 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a 20 20 () call below.
1599b 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 ** will unlock t
1599c 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a 20 he pager.. */.
1599d 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 if( p->inTrans!
1599e 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 =TRANS_NONE ){.
1599f 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 clearAllShare
159a0 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 dCacheTableLocks
159a1 28 70 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54 (p);. pBt->nT
159a2 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 ransaction--;.
159a3 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 if( 0==pBt->nT
159a4 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 ransaction ){.
159a5 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 pBt->inTrans
159a6 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e action = TRANS_N
159a7 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ONE;. }. }..
159a8 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63 75 72 /* Set the cur
159a9 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
159aa 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f state to TRANS_
159ab 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 6b 0a NONE and unlock.
159ac 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72 20 69 ** the pager i
159ad 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73 f this call clos
159ae 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64 ed the only read
159af 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 or write transa
159b0 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 62 74 ction.. */. bt
159b1 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 reeClearHasConte
159b2 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d 3e 69 6e nt(pBt);. p->in
159b3 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
159b4 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 NE;. unlockBtre
159b5 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
159b6 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
159b7 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 y(p);. sqlite3B
159b8 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
159b9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
159ba 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f ;.}../*.** Do bo
159bb 74 68 20 70 68 61 73 65 73 20 6f 66 20 61 20 63 th phases of a c
159bc 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ommit..*/.SQLITE
159bd 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
159be 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 ite3BtreeCommit(
159bf 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
159c0 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
159c1 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
159c2 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
159c3 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
159c4 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , 0);. if( rc==
159c5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
159c6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
159c7 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
159c8 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 (p);. }. sqlit
159c9 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
159ca 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
159cb 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
159cc 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
159cd 20 6e 75 6d 62 65 72 20 6f 66 20 77 72 69 74 65 number of write
159ce 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e -cursors open on
159cf 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 54 68 this handle. Th
159d0 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a is is for use.**
159d1 20 69 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 in assert() exp
159d2 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20 ressions, so it
159d3 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 is only compiled
159d4 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f if NDEBUG is no
159d5 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a t.** defined..**
159d6 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 .** For the purp
159d7 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75 oses of this rou
159d8 74 69 6e 65 2c 20 61 20 77 72 69 74 65 2d 63 75 tine, a write-cu
159d9 72 73 6f 72 20 69 73 20 61 6e 79 20 63 75 72 73 rsor is any curs
159da 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 61 or that.** is ca
159db 70 61 62 6c 65 20 6f 66 20 77 72 69 74 69 6e 67 pable of writing
159dc 20 74 6f 20 74 68 65 20 64 61 74 61 62 73 65 2e to the databse.
159dd 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68 65 That means the
159de 20 63 75 72 73 6f 72 20 77 61 73 0a 2a 2a 20 6f cursor was.** o
159df 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 riginally opened
159e0 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 64 for writing and
159e1 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 the cursor has
159e2 6e 6f 74 20 62 65 20 64 69 73 61 62 6c 65 64 0a not be disabled.
159e3 2a 2a 20 62 79 20 68 61 76 69 6e 67 20 69 74 73 ** by having its
159e4 20 73 74 61 74 65 20 63 68 61 6e 67 65 64 20 74 state changed t
159e5 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 2e 0a o CURSOR_FAULT..
159e6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f */.static int co
159e7 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28 untWriteCursors(
159e8 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
159e9 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
159ea 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 ;. int r = 0;.
159eb 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70 for(pCur=pBt->p
159ec 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43 Cursor; pCur; pC
159ed 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b ur=pCur->pNext){
159ee 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 77 . if( pCur->w
159ef 72 46 6c 61 67 20 26 26 20 70 43 75 72 2d 3e 65 rFlag && pCur->e
159f0 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 46 41 State!=CURSOR_FA
159f1 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a ULT ) r++; . }.
159f2 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 return r;.}.#e
159f3 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
159f4 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
159f5 65 20 73 74 61 74 65 20 74 6f 20 43 55 52 53 4f e state to CURSO
159f6 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 68 65 20 R_FAULT and the
159f7 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 74 6f error.** code to
159f8 20 65 72 72 43 6f 64 65 20 66 6f 72 20 65 76 65 errCode for eve
159f9 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 42 74 53 ry cursor on BtS
159fa 68 61 72 65 64 20 74 68 61 74 20 70 42 74 72 65 hared that pBtre
159fb 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e e.** references.
159fc 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 .**.** Every cur
159fd 73 6f 72 20 69 73 20 74 72 69 70 70 65 64 2c 20 sor is tripped,
159fe 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73 6f 72 including cursor
159ff 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a s that belong.**
15a00 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61 62 61 to other databa
15a01 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 se connections t
15a02 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20 62 65 hat happen to be
15a03 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74 68 65 20 sharing.** the
15a04 63 61 63 68 65 20 77 69 74 68 20 70 42 74 72 65 cache with pBtre
15a05 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
15a06 75 74 69 6e 65 20 67 65 74 73 20 63 61 6c 6c 65 utine gets calle
15a07 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 d when a rollbac
15a08 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c k occurs..** All
15a09 20 63 75 72 73 6f 72 73 20 75 73 69 6e 67 20 74 cursors using t
15a0a 68 65 20 73 61 6d 65 20 63 61 63 68 65 20 6d 75 he same cache mu
15a0b 73 74 20 62 65 20 74 72 69 70 70 65 64 0a 2a 2a st be tripped.**
15a0c 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 6d to prevent them
15a0d 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 from trying to
15a0e 75 73 65 20 74 68 65 20 62 74 72 65 65 20 61 66 use the btree af
15a0f 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 ter.** the rollb
15a10 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61 ack. The rollba
15a11 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65 ck may have dele
15a12 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 ted tables.** or
15a13 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70 61 67 65 moved root page
15a14 73 2c 20 73 6f 20 69 74 20 69 73 20 6e 6f 74 20 s, so it is not
15a15 73 75 66 66 69 63 69 65 6e 74 20 74 6f 0a 2a 2a sufficient to.**
15a16 20 73 61 76 65 20 74 68 65 20 73 74 61 74 65 20 save the state
15a17 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 of the cursor.
15a18 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 The cursor must
15a19 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 be.** invalidate
15a1a 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
15a1b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
15a1c 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 3BtreeTripAllCur
15a1d 73 6f 72 73 28 42 74 72 65 65 20 2a 70 42 74 72 sors(Btree *pBtr
15a1e 65 65 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 ee, int errCode)
15a1f 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
15a20 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
15a21 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 nter(pBtree);.
15a22 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 for(p=pBtree->pB
15a23 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 t->pCursor; p; p
15a24 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
15a25 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 int i;. sqlit
15a26 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
15a27 6f 72 28 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53 or(p);. p->eS
15a28 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 46 41 tate = CURSOR_FA
15a29 55 4c 54 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70 ULT;. p->skip
15a2a 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 = errCode;.
15a2b 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 for(i=0; i<=p->i
15a2c 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Page; i++){.
15a2d 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d releasePage(p-
15a2e 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 >apPage[i]);.
15a2f 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20 p->apPage[i]
15a30 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 0;. }. }.
15a31 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
15a32 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f ve(pBtree);.}../
15a33 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68 *.** Rollback th
15a34 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e e transaction in
15a35 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20 progress. All
15a36 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a cursors will be.
15a37 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20 ** invalided by
15a38 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 this operation.
15a39 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
15a3a 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 use a cursor.**
15a3b 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74 that was open at
15a3c 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
15a3d 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e f this operation
15a3e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 will result.**
15a3f 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a in an error..**.
15a40 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c ** This will rel
15a41 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c ease the write l
15a42 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
15a43 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 ase file. If th
15a44 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 ere.** are no ac
15a45 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 tive cursors, it
15a46 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 also releases t
15a47 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f he read lock..*/
15a48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15a49 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
15a4a 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a Rollback(Btree *
15a4b 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 p){. int rc;.
15a4c 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
15a4d 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 p->pBt;. MemPag
15a4e 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71 e *pPage1;.. sq
15a4f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
15a50 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 p);. rc = saveA
15a51 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 llCursors(pBt, 0
15a52 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 , 0);.#ifndef SQ
15a53 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
15a54 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21 _CACHE. if( rc!
15a55 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15a56 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68 /* This is a h
15a57 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f orrible situatio
15a58 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c n. An IO or mall
15a59 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 oc() error occur
15a5a 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a red whilst. *
15a5b 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 * trying to save
15a5c 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e cursor position
15a5d 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e s. If this is an
15a5e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 automatic rollb
15a5f 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74 ack (as. ** t
15a60 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63 he result of a c
15a61 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f onstraint, mallo
15a62 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49 c() failure or I
15a63 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20 O error) then .
15a64 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 ** the cache
15a65 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c may be internall
15a66 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28 y inconsistent (
15a67 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69 not contain vali
15a68 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20 d trees) so.
15a69 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d ** we cannot sim
15a6a 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65 ply return the e
15a6b 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c rror to the call
15a6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f er. Instead, abo
15a6d 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71 rt . ** all q
15a6e 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20 ueries that may
15a6f 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20 be using any of
15a70 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 the cursors that
15a71 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e failed to save.
15a72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
15a73 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 te3BtreeTripAllC
15a74 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20 ursors(p, rc);.
15a75 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65 }.#endif. btre
15a76 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a eIntegrity(p);..
15a77 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
15a78 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b ==TRANS_WRITE ){
15a79 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 . int rc2;..
15a7a 20 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 assert( TRANS
15a7b 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 _WRITE==pBt->inT
15a7c 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 ransaction );.
15a7d 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 rc2 = sqlite3P
15a7e 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 agerRollback(pBt
15a7f 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
15a80 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f f( rc2!=SQLITE_O
15a81 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
15a82 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc2;. }..
15a83 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 /* The rollback
15a84 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 may have destroy
15a85 65 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 ed the pPage1->a
15a86 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a Data value. So.
15a87 20 20 20 20 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 ** call sqli
15a88 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
15a89 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67 61 69 ) on page 1 agai
15a8a 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a n to make. **
15a8b 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e 61 44 sure pPage1->aD
15a8c 61 74 61 20 69 73 20 73 65 74 20 63 6f 72 72 65 ata is set corre
15a8d 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 ctly. */. if(
15a8e 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
15a8f 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 Page(pBt, 1, &pP
15a90 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 age1, 0)==SQLITE
15a91 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c _OK ){. rel
15a92 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29 easePage(pPage1)
15a93 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
15a94 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 rt( countWriteCu
15a95 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b rsors(pBt)==0 );
15a96 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e . pBt->inTran
15a97 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f saction = TRANS_
15a98 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 READ;. }.. if(
15a99 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 p->inTrans!=TRA
15a9a 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 63 NS_NONE ){. c
15a9b 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 learAllSharedCac
15a9c 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b heTableLocks(p);
15a9d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 . assert( pBt
15a9e 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 ->nTransaction>0
15a9f 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72 );. pBt->nTr
15aa0 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 ansaction--;.
15aa1 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 if( 0==pBt->nTr
15aa2 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 ansaction ){.
15aa3 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 pBt->inTransa
15aa4 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f ction = TRANS_NO
15aa5 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 NE;. }. }..
15aa6 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f btreeClearHasCo
15aa7 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d ntent(pBt);. p-
15aa8 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 >inTrans = TRANS
15aa9 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42 _NONE;. unlockB
15aaa 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 treeIfUnused(pBt
15aab 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 );.. btreeInteg
15aac 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 rity(p);. sqlit
15aad 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
15aae 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15aaf 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 ./*.** Start a s
15ab0 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e tatement subtran
15ab1 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 saction. The sub
15ab2 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 transaction can
15ab3 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a can be rolled.**
15ab4 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e back independen
15ab5 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 tly of the main
15ab6 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 transaction. You
15ab7 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 74 72 must start a tr
15ab8 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 ansaction .** be
15ab9 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 fore starting a
15aba 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 subtransaction.
15abb 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 The subtransacti
15abc 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f on is ended auto
15abd 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 matically .** if
15abe 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 the main transa
15abf 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 ction commits or
15ac0 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a rolls back..**.
15ac1 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 ** Statement sub
15ac2 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 transactions are
15ac3 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 used around ind
15ac4 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 ividual SQL stat
15ac5 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 ements.** that a
15ac6 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 re contained wit
15ac7 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f hin a BEGIN...CO
15ac8 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 MMIT block. If
15ac9 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 a constraint.**
15aca 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 error occurs wit
15acb 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e hin the statemen
15acc 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 t, the effect of
15acd 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d that one statem
15ace 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f ent.** can be ro
15acf 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75 lled back withou
15ad0 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c t having to roll
15ad1 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20 back the entire
15ad2 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
15ad3 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73 ** A statement s
15ad4 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ub-transaction i
15ad5 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 s implemented as
15ad6 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 an anonymous sa
15ad7 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 vepoint. The.**
15ad8 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
15ad9 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
15ada 65 74 65 72 20 69 73 20 74 68 65 20 74 6f 74 61 eter is the tota
15adb 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 l number of save
15adc 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 points,.** inclu
15add 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f ding the new ano
15ade 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 nymous savepoint
15adf 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d , open on the B-
15ae0 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 Tree. i.e. if th
15ae1 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 ere.** are no ac
15ae2 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 tive savepoints
15ae3 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 and no other sta
15ae4 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 tement-transacti
15ae5 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 ons open,.** iSt
15ae6 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 atement is 1. Th
15ae7 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 is anonymous sav
15ae8 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65 epoint can be re
15ae9 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 leased or rolled
15aea 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 back.** using t
15aeb 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 he sqlite3BtreeS
15aec 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 avepoint() funct
15aed 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
15aee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15aef 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 e3BtreeBeginStmt
15af0 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 (Btree *p, int i
15af1 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e Statement){. in
15af2 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
15af3 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
15af4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15af5 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
15af6 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
15af7 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
15af8 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 ssert( pBt->read
15af9 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Only==0 );. ass
15afa 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e ert( iStatement>
15afb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 0 );. assert( i
15afc 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d Statement>p->db-
15afd 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 >nSavepoint );.
15afe 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e if( NEVER(p->in
15aff 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 Trans!=TRANS_WRI
15b00 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f TE || pBt->readO
15b01 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d nly) ){. rc =
15b02 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c SQLITE_INTERNAL
15b03 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
15b04 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
15b05 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
15b06 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a _WRITE );. /*
15b07 20 41 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 At the pager le
15b08 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 vel, a statement
15b09 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
15b0a 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 a savepoint with
15b0b 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 . ** an index
15b0c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c greater than al
15b0d 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 l savepoints cre
15b0e 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 ated explicitly
15b0f 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c using. ** SQL
15b10 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 statements. It
15b11 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 is illegal to op
15b12 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 en, release or r
15b13 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 ollback any.
15b14 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e ** such savepoin
15b15 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 ts while the sta
15b16 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
15b17 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 on savepoint is
15b18 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 active.. */.
15b19 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
15b1a 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e agerOpenSavepoin
15b1b 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
15b1c 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a Statement);. }.
15b1d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
15b1e 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
15b1f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
15b20 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
15b21 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 t to this functi
15b22 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 on, op, is alway
15b23 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c s SAVEPOINT_ROLL
15b24 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 BACK.** or SAVEP
15b25 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 OINT_RELEASE. Th
15b26 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 is function eith
15b27 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 er releases or r
15b28 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a olls back the.**
15b29 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 savepoint ident
15b2a 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
15b2b 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 er iSavepoint, d
15b2c 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
15b2d 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e value .** of op.
15b2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c .**.** Normally,
15b2f 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 iSavepoint is g
15b30 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
15b31 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f qual to zero. Ho
15b32 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a wever, if op is.
15b33 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c ** SAVEPOINT_ROL
15b34 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 LBACK, then iSav
15b35 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 epoint may also
15b36 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 be -1. In this c
15b37 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 ase the .** cont
15b38 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 ents of the enti
15b39 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 re transaction a
15b3a 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 re rolled back.
15b3b 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e This is differen
15b3c 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d t.** from a norm
15b3d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 al transaction r
15b3e 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c ollback, as no l
15b3f 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 ocks are release
15b40 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 d and the.** tra
15b41 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 nsaction remains
15b42 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 open..*/.SQLITE
15b43 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
15b44 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
15b45 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 nt(Btree *p, int
15b46 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f op, int iSavepo
15b47 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d int){. int rc =
15b48 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
15b49 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e ( p && p->inTran
15b4a 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
15b4b 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
15b4c 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
15b4d 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 assert( op==SA
15b4e 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 VEPOINT_RELEASE
15b4f 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 || op==SAVEPOINT
15b50 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 _ROLLBACK );.
15b51 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f assert( iSavepo
15b52 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 int>=0 || (iSave
15b53 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d point==-1 && op=
15b54 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
15b55 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 ACK) );. sqli
15b56 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
15b57 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
15b58 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e3PagerSavepoint
15b59 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 (pBt->pPager, op
15b5a 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 , iSavepoint);.
15b5b 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
15b5c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
15b5d 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 = newDatabase(p
15b5e 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 Bt);. }. s
15b5f 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
15b60 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 (p);. }. retur
15b61 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
15b62 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 reate a new curs
15b63 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 or for the BTree
15b64 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f whose root is o
15b65 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 n the page.** iT
15b66 61 62 6c 65 2e 20 20 54 68 65 20 61 63 74 20 6f able. The act o
15b67 66 20 61 63 71 75 69 72 69 6e 67 20 61 20 63 75 f acquiring a cu
15b68 72 73 6f 72 20 67 65 74 73 20 61 20 72 65 61 64 rsor gets a read
15b69 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74 68 65 lock on .** the
15b6a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
15b6b 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d **.** If wrFlag=
15b6c 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 =0, then the cur
15b6d 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 sor can only be
15b6e 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 used for reading
15b6f 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d ..** If wrFlag==
15b70 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 1, then the curs
15b71 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 or can be used f
15b72 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66 6f or reading or fo
15b73 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69 66 20 r.** writing if
15b74 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73 other conditions
15b75 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 72 65 for writing are
15b76 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 73 also met. Thes
15b77 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f 6e e.** are the con
15b78 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 73 ditions that mus
15b79 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 65 t be met in orde
15b7a 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 74 6f r for writing to
15b7b 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a .** be allowed:.
15b7c 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 75 **.** 1: The cu
15b7d 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 rsor must have b
15b7e 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 20 een opened with
15b7f 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 wrFlag==1.**.**
15b80 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 62 61 2: Other databa
15b81 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 se connections t
15b82 68 61 74 20 73 68 61 72 65 20 74 68 65 20 73 61 hat share the sa
15b83 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 0a 2a me pager cache.*
15b84 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 68 20 * but which
15b85 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 are not in the R
15b86 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 EAD_UNCOMMITTED
15b87 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 68 61 state may not ha
15b88 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f 72 ve.** cursor
15b89 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 46 6c s open with wrFl
15b8a 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d ag==0 on the sam
15b8b 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 72 77 e table. Otherw
15b8c 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 20 63 ise.** the c
15b8d 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 hanges made by t
15b8e 68 69 73 20 77 72 69 74 65 20 63 75 72 73 6f 72 his write cursor
15b8f 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 62 6c would be visibl
15b90 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 e to.** the
15b91 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 6e 20 read cursors in
15b92 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62 61 the other databa
15b93 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a se connection..*
15b94 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 74 *.** 3: The dat
15b95 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 77 72 abase must be wr
15b96 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 itable (not on r
15b97 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a ead-only media).
15b98 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 20 **.** 4: There
15b99 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 69 76 must be an activ
15b9a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
15b9b 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67 *.** No checking
15b9c 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 is done to make
15b9d 20 73 75 72 65 20 74 68 61 74 20 70 61 67 65 20 sure that page
15b9e 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20 69 73 iTable really is
15b9f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 the.** root pag
15ba0 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e 20 20 e of a b-tree.
15ba1 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 If it is not, th
15ba2 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 en the cursor ac
15ba3 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e quired.** will n
15ba4 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c ot work correctl
15ba5 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 y..**.** It is a
15ba6 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 ssumed that the
15ba7 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
15ba8 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73 20 6f orSize() bytes o
15ba9 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69 f memory .** poi
15baa 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75 72 20 nted to by pCur
15bab 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f 65 64 have been zeroed
15bac 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a by the caller..
15bad 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 */.static int bt
15bae 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 reeCursor(. Btr
15baf 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 ee *p,
15bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15bb1 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
15bb2 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
15bb3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
15bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15bb5 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 Root page of tab
15bb6 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 le to open */.
15bb7 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 int wrFlag,
15bb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15bb9 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 /* 1 to w
15bba 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c rite. 0 read-onl
15bbb 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 y */. struct Ke
15bbc 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c yInfo *pKeyInfo,
15bbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15bbe 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f First arg to co
15bbf 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f mparison functio
15bc0 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 n */. BtCursor
15bc1 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 *pCur
15bc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15bc3 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63 Space for new c
15bc4 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e ursor */.){. in
15bc5 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 t rc;. Pgno nPa
15bc6 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge;. BtShared *
15bc7 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 pBt = p->pBt;..
15bc8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15bc9 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
15bca 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
15bcb 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 wrFlag==0 || wrF
15bcc 6c 61 67 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 lag==1 );. if(
15bcd 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 61 73 wrFlag ){. as
15bce 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 sert( !pBt->read
15bcf 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 69 66 28 20 Only );. if(
15bd0 4e 45 56 45 52 28 70 42 74 2d 3e 72 65 61 64 4f NEVER(pBt->readO
15bd1 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 72 65 nly) ){. re
15bd2 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 turn SQLITE_READ
15bd3 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ONLY;. }.
15bd4 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65 61 rc = checkForRea
15bd5 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 dConflicts(p, iT
15bd6 61 62 6c 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 able, 0, 0);.
15bd7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
15bd8 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 OK ){. asse
15bd9 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c rt( rc==SQLITE_L
15bda 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
15bdb 45 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 E );. retur
15bdc 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n rc;. }. }.
15bdd 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 . if( pBt->pPag
15bde 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 e1==0 ){. rc
15bdf 3d 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52 = lockBtreeWithR
15be0 65 74 72 79 28 70 29 3b 0a 20 20 20 20 69 66 28 etry(p);. if(
15be1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15be2 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
15be3 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 c;. }. }. p
15be4 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 Cur->pgnoRoot =
15be5 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20 (Pgno)iTable;.
15be6 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15be7 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e rPagecount(pBt->
15be8 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26 pPager, (int *)&
15be9 6e 50 61 67 65 29 3b 20 0a 20 20 69 66 28 20 72 nPage); . if( r
15bea 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
15beb 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
15bec 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3d }. if( iTable=
15bed 3d 31 20 26 26 20 6e 50 61 67 65 3d 3d 30 20 29 =1 && nPage==0 )
15bee 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
15bef 45 5f 45 4d 50 54 59 3b 0a 20 20 20 20 67 6f 74 E_EMPTY;. got
15bf0 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f 72 5f o create_cursor_
15bf1 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 exception;. }.
15bf2 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 rc = getAndInit
15bf3 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e Page(pBt, pCur->
15bf4 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d pgnoRoot, &pCur-
15bf5 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 69 >apPage[0]);. i
15bf6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
15bf7 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65 ){. goto cre
15bf8 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 ate_cursor_excep
15bf9 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tion;. }.. /*
15bfa 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 Now that no othe
15bfb 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63 r errors can occ
15bfc 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69 ur, finish filli
15bfd 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73 ng in the BtCurs
15bfe 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 or. ** variable
15bff 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 s, link the curs
15c00 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68 or into the BtSh
15c01 61 72 65 64 20 6c 69 73 74 20 61 6e 64 20 73 65 ared list and se
15c02 74 20 2a 70 70 43 75 72 20 28 74 68 65 0a 20 20 t *ppCur (the.
15c03 2a 2a 20 6f 75 74 70 75 74 20 61 72 67 75 6d 65 ** output argume
15c04 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 nt to this funct
15c05 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20 70 43 75 ion).. */. pCu
15c06 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b r->pKeyInfo = pK
15c07 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e eyInfo;. pCur->
15c08 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43 pBtree = p;. pC
15c09 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 ur->pBt = pBt;.
15c0a 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20 pCur->wrFlag =
15c0b 28 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43 (u8)wrFlag;. pC
15c0c 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d ur->pNext = pBt-
15c0d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 >pCursor;. if(
15c0e 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 pCur->pNext ){.
15c0f 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e pCur->pNext->
15c10 70 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 pPrev = pCur;.
15c11 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 }. pBt->pCursor
15c12 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d = pCur;. pCur-
15c13 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
15c14 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72 _INVALID;. pCur
15c15 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20 ->cachedRowid =
15c16 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 0;.. return SQL
15c17 49 54 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74 65 5f ITE_OK;..create_
15c18 63 75 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e cursor_exception
15c19 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 :. releasePage(
15c1a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 pCur->apPage[0])
15c1b 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 ;. unlockBtreeI
15c1c 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 fUnused(pBt);.
15c1d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.SQL
15c1e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15c1f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
15c20 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 or(. Btree *p,
15c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c23 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a /* The btree *
15c24 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 /. int iTable,
15c25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c27 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 /* Root page of
15c28 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f table to open */
15c29 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 . int wrFlag,
15c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15c2c 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 * 1 to write. 0
15c2d 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 read-only */. s
15c2e 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 truct KeyInfo *p
15c2f 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 KeyInfo,
15c30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
15c31 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 rst arg to xComp
15c32 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 are() */. BtCur
15c33 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 sor *pCur
15c34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15c35 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
15c36 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 new cursor here
15c37 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
15c38 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15c39 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 ter(p);. rc = b
15c3a 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69 54 treeCursor(p, iT
15c3b 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70 4b able, wrFlag, pK
15c3c 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a 20 eyInfo, pCur);.
15c3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
15c3e 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
15c3f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
15c40 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 urn the size of
15c41 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 a BtCursor objec
15c42 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a t in bytes..**.*
15c43 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
15c44 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20 74 s is needed so t
15c45 68 61 74 20 75 73 65 72 73 20 6f 66 20 63 75 72 hat users of cur
15c46 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c 6f sors can preallo
15c47 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 cate.** sufficie
15c48 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68 6f nt storage to ho
15c49 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 ld a cursor. Th
15c4a 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 e BtCursor objec
15c4b 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20 74 t is opaque.** t
15c4c 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65 79 20 o users so they
15c4d 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73 69 cannot do the si
15c4e 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76 65 zeof() themselve
15c4f 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63 61 s - they must ca
15c50 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 ll.** this routi
15c51 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ne..*/.SQLITE_PR
15c52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15c53 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
15c54 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e (void){. return
15c55 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73 6f 72 sizeof(BtCursor
15c56 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 );.}../*.** Set
15c57 74 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 the cached rowid
15c58 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 20 value of every
15c59 63 75 72 73 6f 72 20 69 6e 20 74 68 65 20 73 61 cursor in the sa
15c5a 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 me database file
15c5b 0a 2a 2a 20 61 73 20 70 43 75 72 20 61 6e 64 20 .** as pCur and
15c5c 68 61 76 69 6e 67 20 74 68 65 20 73 61 6d 65 20 having the same
15c5d 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
15c5e 20 61 73 20 70 43 75 72 2e 20 20 54 68 65 20 76 as pCur. The v
15c5f 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 alue is.** set t
15c60 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 o iRowid..**.**
15c61 4f 6e 6c 79 20 70 6f 73 69 74 69 76 65 20 72 6f Only positive ro
15c62 77 69 64 20 76 61 6c 75 65 73 20 61 72 65 20 63 wid values are c
15c63 6f 6e 73 69 64 65 72 65 64 20 76 61 6c 69 64 20 onsidered valid
15c64 66 6f 72 20 74 68 69 73 20 63 61 63 68 65 2e 0a for this cache..
15c65 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20 ** The cache is
15c66 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a initialized to z
15c67 65 72 6f 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 ero, indicating
15c68 61 6e 20 69 6e 76 61 6c 69 64 20 63 61 63 68 65 an invalid cache
15c69 2e 0a 2a 2a 20 41 20 62 74 72 65 65 20 77 69 6c ..** A btree wil
15c6a 6c 20 77 6f 72 6b 20 66 69 6e 65 20 77 69 74 68 l work fine with
15c6b 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 zero or negativ
15c6c 65 20 72 6f 77 69 64 73 2e 20 20 57 65 20 6a 75 e rowids. We ju
15c6d 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20 63 61 63 st cannot.** cac
15c6e 68 65 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 he zero or negat
15c6f 69 76 65 20 72 6f 77 69 64 73 2c 20 77 68 69 63 ive rowids, whic
15c70 68 20 6d 65 61 6e 73 20 74 61 62 6c 65 73 20 74 h means tables t
15c71 68 61 74 20 75 73 65 20 7a 65 72 6f 20 6f 72 0a hat use zero or.
15c72 2a 2a 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 ** negative rowi
15c73 64 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20 6c ds might run a l
15c74 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20 42 ittle slower. B
15c75 75 74 20 69 6e 20 70 72 61 63 74 69 63 65 2c 20 ut in practice,
15c76 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65 67 61 74 zero.** or negat
15c77 69 76 65 20 72 6f 77 69 64 73 20 61 72 65 20 76 ive rowids are v
15c78 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 ery uncommon so
15c79 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 this should not
15c7a 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f be a problem..*/
15c7b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15c7c 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
15c7d 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 eSetCachedRowid(
15c7e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
15c7f 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 sqlite3_int64 iR
15c80 6f 77 69 64 29 7b 0a 20 20 42 74 43 75 72 73 6f owid){. BtCurso
15c81 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 r *p;. for(p=pC
15c82 75 72 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 ur->pBt->pCursor
15c83 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
15c84 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e {. if( p->pgn
15c85 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e 70 67 6e oRoot==pCur->pgn
15c86 6f 52 6f 6f 74 20 29 20 70 2d 3e 63 61 63 68 65 oRoot ) p->cache
15c87 64 52 6f 77 69 64 20 3d 20 69 52 6f 77 69 64 3b dRowid = iRowid;
15c88 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
15c89 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 Cur->cachedRowid
15c8a 3d 3d 69 52 6f 77 69 64 20 29 3b 0a 7d 0a 0a 2f ==iRowid );.}../
15c8b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
15c8c 63 61 63 68 65 64 20 72 6f 77 69 64 20 66 6f 72 cached rowid for
15c8d 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f the given curso
15c8e 72 2e 20 20 41 20 6e 65 67 61 74 69 76 65 20 6f r. A negative o
15c8f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65 74 75 72 6e r zero.** return
15c90 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 value indicates
15c91 20 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 that the rowid
15c92 63 61 63 68 65 20 69 73 20 69 6e 76 61 6c 69 64 cache is invalid
15c93 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 0a 2a and should be.*
15c94 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49 66 20 74 * ignored. If t
15c95 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 68 he rowid cache h
15c96 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 as never before
15c97 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 61 been set, then a
15c98 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 72 65 74 75 .** zero is retu
15c99 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
15c9a 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
15c9b 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74 72 int64 sqlite3Btr
15c9c 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 eeGetCachedRowid
15c9d 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
15c9e 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d {. return pCur-
15c9f 3e 63 61 63 68 65 64 52 6f 77 69 64 3b 0a 7d 0a >cachedRowid;.}.
15ca0 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 ./*.** Close a c
15ca1 75 72 73 6f 72 2e 20 20 54 68 65 20 72 65 61 64 ursor. The read
15ca2 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
15ca3 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 72 65 abase file is re
15ca4 6c 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74 leased.** when t
15ca5 68 65 20 6c 61 73 74 20 63 75 72 73 6f 72 20 69 he last cursor i
15ca6 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c s closed..*/.SQL
15ca7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15ca8 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
15ca9 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 eCursor(BtCursor
15caa 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65 65 *pCur){. Btree
15cab 20 2a 70 42 74 72 65 65 20 3d 20 70 43 75 72 2d *pBtree = pCur-
15cac 3e 70 42 74 72 65 65 3b 0a 20 20 69 66 28 20 70 >pBtree;. if( p
15cad 42 74 72 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 Btree ){. int
15cae 20 69 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64 i;. BtShared
15caf 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 *pBt = pCur->pB
15cb0 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 t;. sqlite3Bt
15cb1 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 reeEnter(pBtree)
15cb2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
15cb3 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 eeClearCursor(pC
15cb4 75 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 ur);. if( pCu
15cb5 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 r->pPrev ){.
15cb6 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 pCur->pPrev->p
15cb7 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 Next = pCur->pNe
15cb8 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 xt;. }else{.
15cb9 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f pBt->pCurso
15cba 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b r = pCur->pNext;
15cbb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
15cbc 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 Cur->pNext ){.
15cbd 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d pCur->pNext-
15cbe 3e 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 >pPrev = pCur->p
15cbf 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Prev;. }.
15cc0 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 for(i=0; i<=pCur
15cc1 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 ->iPage; i++){.
15cc2 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
15cc3 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d (pCur->apPage[i]
15cc4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c );. }. unl
15cc5 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 ockBtreeIfUnused
15cc6 28 70 42 74 29 3b 0a 20 20 20 20 69 6e 76 61 6c (pBt);. inval
15cc7 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 idateOverflowCac
15cc8 68 65 28 70 43 75 72 29 3b 0a 20 20 20 20 2f 2a he(pCur);. /*
15cc9 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
15cca 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 ur); */. sqli
15ccb 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 te3BtreeLeave(pB
15ccc 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 tree);. }. ret
15ccd 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
15cce 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 74 ../*.** Make a t
15ccf 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 emporary cursor
15cd0 62 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 by filling in th
15cd1 65 20 66 69 65 6c 64 73 20 6f 66 20 70 54 65 6d e fields of pTem
15cd2 70 43 75 72 2e 0a 2a 2a 20 54 68 65 20 74 65 6d pCur..** The tem
15cd3 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 73 porary cursor is
15cd4 20 6e 6f 74 20 6f 6e 20 74 68 65 20 63 75 72 73 not on the curs
15cd5 6f 72 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 or list for the
15cd6 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Btree..*/.SQLITE
15cd7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
15cd8 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d lite3BtreeGetTem
15cd9 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 pCursor(BtCursor
15cda 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 6f 72 *pCur, BtCursor
15cdb 20 2a 70 54 65 6d 70 43 75 72 29 7b 0a 20 20 69 *pTempCur){. i
15cdc 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
15cdd 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
15cde 28 70 43 75 72 29 20 29 3b 0a 20 20 6d 65 6d 63 (pCur) );. memc
15cdf 70 79 28 70 54 65 6d 70 43 75 72 2c 20 70 43 75 py(pTempCur, pCu
15ce0 72 2c 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73 r, sizeof(BtCurs
15ce1 6f 72 29 29 3b 0a 20 20 70 54 65 6d 70 43 75 72 or));. pTempCur
15ce2 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 ->pNext = 0;. p
15ce3 54 65 6d 70 43 75 72 2d 3e 70 50 72 65 76 20 3d TempCur->pPrev =
15ce4 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
15ce5 3c 3d 70 54 65 6d 70 43 75 72 2d 3e 69 50 61 67 <=pTempCur->iPag
15ce6 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c e; i++){. sql
15ce7 69 74 65 33 50 61 67 65 72 52 65 66 28 70 54 65 ite3PagerRef(pTe
15ce8 6d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d mpCur->apPage[i]
15ce9 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a ->pDbPage);. }.
15cea 20 20 61 73 73 65 72 74 28 20 70 54 65 6d 70 43 assert( pTempC
15ceb 75 72 2d 3e 70 4b 65 79 3d 3d 30 20 29 3b 0a 7d ur->pKey==0 );.}
15cec 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
15ced 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f temporary curso
15cee 72 20 73 75 63 68 20 61 73 20 77 61 73 20 6d 61 r such as was ma
15cef 64 65 20 62 79 20 74 68 65 20 43 72 65 61 74 65 de by the Create
15cf0 54 65 6d 70 6f 72 61 72 79 43 75 72 73 6f 72 28 TemporaryCursor(
15cf1 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 62 ).** function ab
15cf2 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ove..*/.SQLITE_P
15cf3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
15cf4 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 65 54 te3BtreeReleaseT
15cf5 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 empCursor(BtCurs
15cf6 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 or *pCur){. int
15cf7 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 i;. assert( cu
15cf8 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
15cf9 43 75 72 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d Cur) );. for(i=
15cfa 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 0; i<=pCur->iPag
15cfb 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c e; i++){. sql
15cfc 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
15cfd 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d 3e Cur->apPage[i]->
15cfe 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 pDbPage);. }.
15cff 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 sqlite3_free(pCu
15d00 72 2d 3e 70 4b 65 79 29 3b 0a 7d 0a 0a 0a 0a 2f r->pKey);.}..../
15d01 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 *.** Make sure t
15d02 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 he BtCursor* giv
15d03 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 en in the argume
15d04 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a nt has a valid.*
15d05 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 * BtCursor.info
15d06 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 structure. If i
15d07 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 t is not already
15d08 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 valid, call.**
15d09 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 sqlite3BtreePars
15d0a 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c 6c 20 eCell() to fill
15d0b 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 74 43 it in..**.** BtC
15d0c 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 61 20 ursor.info is a
15d0d 63 61 63 68 65 20 6f 66 20 74 68 65 20 69 6e 66 cache of the inf
15d0e 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 ormation in the
15d0f 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a 2a 2a current cell..**
15d10 20 55 73 69 6e 67 20 74 68 69 73 20 63 61 63 68 Using this cach
15d11 65 20 72 65 64 75 63 65 73 20 74 68 65 20 6e 75 e reduces the nu
15d12 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f mber of calls to
15d13 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
15d14 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 seCell()..**.**
15d15 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 2007-06-25: The
15d16 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 re is a bug in s
15d17 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ome versions of
15d18 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20 MSVC that cause
15d19 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 the.** compiler
15d1a 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 to crash when ge
15d1b 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 tCellInfo() is i
15d1c 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 mplemented as a
15d1d 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 macro..** But th
15d1e 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 ere is a measure
15d1f 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e able speed advan
15d20 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 tage to using th
15d21 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a e macro on gcc.*
15d22 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d * (when less com
15d23 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 piler optimizati
15d24 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 ons like -Os or
15d25 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 -O0 are used and
15d26 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 the.** compiler
15d27 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 is not doing ag
15d28 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 ressive inlining
15d29 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 .) So we use a
15d2a 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a real function.**
15d2b 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 for MSVC and a
15d2c 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 macro for everyt
15d2d 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b hing else. Tick
15d2e 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 et #2457..*/.#if
15d2f 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 ndef NDEBUG. st
15d30 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 atic void assert
15d31 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f CellInfo(BtCurso
15d32 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 r *pCur){. Ce
15d33 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 llInfo info;.
15d34 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 int iPage = pCu
15d35 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 r->iPage;. me
15d36 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 mset(&info, 0, s
15d37 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 izeof(info));.
15d38 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 sqlite3BtreePa
15d39 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 rseCell(pCur->ap
15d3a 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70 43 75 Page[iPage], pCu
15d3b 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c r->aiIdx[iPage],
15d3c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73 &info);. ass
15d3d 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69 6e 66 ert( memcmp(&inf
15d3e 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 2c 20 o, &pCur->info,
15d3f 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d 3d 30 sizeof(info))==0
15d40 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 );. }.#else.
15d41 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 43 65 #define assertCe
15d42 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 69 66 llInfo(x).#endif
15d43 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56 45 52 .#ifdef _MSC_VER
15d44 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65 61 6c . /* Use a real
15d45 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d 53 56 function in MSV
15d46 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 C to work around
15d47 20 62 75 67 73 20 69 6e 20 74 68 61 74 20 63 6f bugs in that co
15d48 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73 74 61 mpiler. */. sta
15d49 74 69 63 20 76 6f 69 64 20 67 65 74 43 65 6c 6c tic void getCell
15d4a 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 Info(BtCursor *p
15d4b 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20 70 43 Cur){. if( pC
15d4c 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d ur->info.nSize==
15d4d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 0 ){. int i
15d4e 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 Page = pCur->iPa
15d4f 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ge;. sqlite
15d50 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 3BtreeParseCell(
15d51 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 pCur->apPage[iPa
15d52 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b ge],pCur->aiIdx[
15d53 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e iPage],&pCur->in
15d54 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d fo);. pCur-
15d55 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a >validNKey = 1;.
15d56 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
15d57 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
15d58 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pCur);. }. }
15d59 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74 .#else /* if not
15d5a 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f _MSC_VER */. /
15d5b 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e * Use a macro in
15d5c 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69 all other compi
15d5d 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65 lers so that the
15d5e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c function is inl
15d5f 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ined */.#define
15d60 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 getCellInfo(pCur
15d61 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
15d62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d64 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 70 \. if( p
15d65 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
15d66 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 =0 ){
15d67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d69 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e \. in
15d6a 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e t iPage = pCur->
15d6b 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 iPage;
15d6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d6e 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 \. s
15d6f 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
15d70 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 Cell(pCur->apPag
15d71 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 e[iPage],pCur->a
15d72 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 iIdx[iPage],&pCu
15d73 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20 20 20 r->info); \.
15d74 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
15d75 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 1;
15d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d78 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
15d79 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 else{
15d7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d7d 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
15d7e 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f assertCellInfo
15d7f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20 20 20 (pCur);
15d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d82 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
15d83 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 }.#endif /* _MS
15d84 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 C_VER */../*.**
15d85 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 Set *pSize to th
15d86 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 e size of the bu
15d87 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 ffer needed to h
15d88 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 old the value of
15d89 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 .** the key for
15d8a 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 the current entr
15d8b 79 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f y. If the curso
15d8c 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e r is not pointin
15d8d 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 g.** to a valid
15d8e 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 entry, *pSize is
15d8f 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a set to 0. .**.*
15d90 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 * For a table wi
15d91 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c th the INTKEY fl
15d92 61 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 ag set, this rou
15d93 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
15d94 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 key.** itself,
15d95 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f not the number o
15d96 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b f bytes in the k
15d97 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ey..*/.SQLITE_PR
15d98 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15d99 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 3BtreeKeySize(Bt
15d9a 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 Cursor *pCur, i6
15d9b 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 4 *pSize){. int
15d9c 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
15d9d 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
15d9e 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d (pCur) );. rc =
15d9f 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
15da0 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
15da1 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15da2 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
15da3 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
15da4 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c URSOR_INVALID ||
15da5 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
15da6 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
15da7 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 if( pCur->eSt
15da8 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
15da9 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2a 70 53 LID ){. *pS
15daa 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c ize = 0;. }el
15dab 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43 65 6c se{. getCel
15dac 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 lInfo(pCur);.
15dad 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 *pSize = pCur
15dae 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 ->info.nKey;.
15daf 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
15db0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 rc;.}../*.** Set
15db1 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 6e *pSize to the n
15db2 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
15db3 66 20 64 61 74 61 20 69 6e 20 74 68 65 20 65 6e f data in the en
15db4 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f try the.** curso
15db5 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e r currently poin
15db6 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 73 20 72 ts to. Always r
15db7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
15db8 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73 20 6e .** Failure is n
15db9 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 49 66 ot possible. If
15dba 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e the cursor is n
15dbb 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 ot currently.**
15dbc 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e 20 65 pointing to an e
15dbd 6e 74 72 79 20 28 77 68 69 63 68 20 63 61 6e 20 ntry (which can
15dbe 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78 61 6d happen, for exam
15dbf 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 65 20 64 ple, if.** the d
15dc0 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 74 79 atabase is empty
15dc1 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 20 69 73 ) then *pSize is
15dc2 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 53 51 set to 0..*/.SQ
15dc3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15dc4 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
15dc5 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 2a aSize(BtCursor *
15dc6 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69 7a 65 pCur, u32 *pSize
15dc7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
15dc8 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
15dc9 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
15dca 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 ;. rc = restore
15dcb 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
15dcc 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d Cur);. if( rc==
15dcd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15dce 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
15dcf 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
15dd0 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 VALID || pCur->e
15dd1 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
15dd2 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70 LID );. if( p
15dd3 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
15dd4 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 SOR_INVALID ){.
15dd5 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 6f 69 6e /* Not poin
15dd6 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20 ting at a valid
15dd7 65 6e 74 72 79 20 2d 20 73 65 74 20 2a 70 53 69 entry - set *pSi
15dd8 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 20 20 20 ze to 0. */.
15dd9 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 *pSize = 0;.
15dda 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 }else{. g
15ddb 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 etCellInfo(pCur)
15ddc 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d ;. *pSize =
15ddd 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 pCur->info.nDat
15dde 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 a;. }. }. r
15ddf 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
15de0 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 ** Given the pag
15de1 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f e number of an o
15de2 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 verflow page in
15de3 74 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61 the database (pa
15de4 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 rameter.** ovfl)
15de5 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
15de6 66 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e finds the page n
15de7 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 umber of the nex
15de8 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a t page in the .*
15de9 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 * linked list of
15dea 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
15deb 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 If possible, it
15dec 20 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 uses the auto-v
15ded 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 acuum.** pointer
15dee 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 -map data instea
15def 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 d of reading the
15df0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 content of page
15df1 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 ovfl to do so.
15df2 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 .**.** If an err
15df3 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c or occurs an SQL
15df4 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ite error code i
15df5 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 s returned. Othe
15df6 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 rwise:.**.** The
15df7 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
15df8 74 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f the next overflo
15df9 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 w page in the li
15dfa 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a nked list is .**
15dfb 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 written to *pPg
15dfc 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 noNext. If page
15dfd 6f 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 ovfl is the last
15dfe 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e page in its lin
15dff 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 ked .** list, *p
15e00 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 PgnoNext is set
15e01 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 to zero. .**.**
15e02 49 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 If ppPage is not
15e03 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 NULL, and a ref
15e04 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 erence to the Me
15e05 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 mPage object cor
15e06 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f responding.** to
15e07 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 page number pOv
15e08 66 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c fl was obtained,
15e09 20 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 then *ppPage is
15e0a 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f set to point to
15e0b 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e that.** referen
15e0c 63 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 ce. It is the re
15e0d 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 sponsibility of
15e0e 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 the caller to ca
15e0f 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 ll releasePage()
15e10 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 .** on *ppPage t
15e11 6f 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72 o free the refer
15e12 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 ence. In no refe
15e13 72 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e rence was obtain
15e14 65 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 ed (because.** t
15e15 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 he pointer-map w
15e16 61 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 as used to obtai
15e17 6e 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 n the value for
15e18 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 *pPgnoNext), the
15e19 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 n.** *ppPage is
15e1a 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a set to zero..*/.
15e1b 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 static int getOv
15e1c 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 erflowPage(. Bt
15e1d 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 Shared *pBt, .
15e1e 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 20 20 Pgno ovfl,
15e1f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15e20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 2a 2f Overflow page */
15e21 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 . MemPage **ppP
15e22 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 age,
15e23 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 65 20 /* OUT: MemPage
15e24 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 20 4e handle (may be N
15e25 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a ULL) */. Pgno *
15e26 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 20 20 pPgnoNext
15e27 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e /* OUT: N
15e28 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ext overflow pag
15e29 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 e number */.){.
15e2a 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b 0a Pgno next = 0;.
15e2b 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
15e2c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d = 0;. int rc =
15e2d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 SQLITE_OK;.. a
15e2e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15e2f 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
15e30 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
15e31 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 23 t(pPgnoNext);..#
15e32 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
15e33 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
15e34 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 74 /* Try to find t
15e35 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
15e36 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 the overflow lis
15e37 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a t using the. **
15e38 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 6e autovacuum poin
15e39 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 47 ter-map pages. G
15e3a 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e 65 uess that the ne
15e3b 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a 2a xt page in . **
15e3c 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 the overflow li
15e3d 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62 65 st is page numbe
15e3e 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 74 r (ovfl+1). If t
15e3f 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73 20 hat guess turns
15e40 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 . ** out to be
15e41 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 6b wrong, fall back
15e42 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 to loading the
15e43 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20 20 data of page .
15e44 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 74 ** number ovfl t
15e45 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
15e46 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65 72 next page number
15e47 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 .. */. if( pBt
15e48 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
15e49 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 Pgno pgno;.
15e4a 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20 3d Pgno iGuess =
15e4b 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 20 ovfl+1;. u8
15e4c 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 6c eType;.. whil
15e4d 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 e( PTRMAP_ISPAGE
15e4e 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c 7c (pBt, iGuess) ||
15e4f 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e 47 iGuess==PENDING
15e50 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
15e51 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73 2b ){. iGuess+
15e52 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 +;. }.. if
15e53 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72 50 ( iGuess<=pagerP
15e54 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b agecount(pBt) ){
15e55 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d . rc = ptrm
15e56 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65 73 apGet(pBt, iGues
15e57 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e 6f s, &eType, &pgno
15e58 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
15e59 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 54 =SQLITE_OK && eT
15e5a 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
15e5b 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f FLOW2 && pgno==o
15e5c 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e vfl ){. n
15e5d 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20 ext = iGuess;.
15e5e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
15e5f 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a E_DONE;. }.
15e60 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
15e61 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
15e62 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
15e63 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
15e64 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c tPage(pBt, ovfl,
15e65 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 &pPage, 0);.
15e66 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 assert(rc==SQLI
15e67 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d TE_OK || pPage==
15e68 30 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74 0);. if( next
15e69 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 ==0 && rc==SQLIT
15e6a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 65 E_OK ){. ne
15e6b 78 74 20 3d 20 67 65 74 34 62 79 74 65 28 70 50 xt = get4byte(pP
15e6c 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 age->aData);.
15e6d 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f }. }.. *pPgno
15e6e 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 69 Next = next;. i
15e6f 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20 f( ppPage ){.
15e70 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 *ppPage = pPage
15e71 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
15e72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
15e73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
15e74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 (rc==SQLITE_DONE
15e75 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 ? SQLITE_OK : r
15e76 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 c);.}../*.** Cop
15e77 79 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 y data from a bu
15e78 66 66 65 72 20 74 6f 20 61 20 70 61 67 65 2c 20 ffer to a page,
15e79 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 65 20 74 or from a page t
15e7a 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a o a buffer..**.*
15e7b 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 20 61 20 * pPayload is a
15e7c 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 pointer to data
15e7d 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 61 62 61 stored on databa
15e7e 73 65 20 70 61 67 65 20 70 44 62 50 61 67 65 2e se page pDbPage.
15e7f 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 .** If argument
15e80 65 4f 70 20 69 73 20 66 61 6c 73 65 2c 20 74 68 eOp is false, th
15e81 65 6e 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f en nByte bytes o
15e82 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 f data are copie
15e83 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79 6c 6f d.** from pPaylo
15e84 61 64 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 ad to the buffer
15e85 20 70 6f 69 6e 74 65 64 20 61 74 20 62 79 20 70 pointed at by p
15e86 42 75 66 2e 20 49 66 20 65 4f 70 20 69 73 20 74 Buf. If eOp is t
15e87 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c rue,.** then sql
15e88 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
15e89 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44 is called on pD
15e8a 62 50 61 67 65 20 61 6e 64 20 6e 42 79 74 65 20 bPage and nByte
15e8b 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 bytes.** of data
15e8c 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d are copied from
15e8d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 75 66 the buffer pBuf
15e8e 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a to pPayload..**
15e8f 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
15e90 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 returned on suc
15e91 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20 cess, otherwise
15e92 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a an error code..*
15e93 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 70 /.static int cop
15e94 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f 69 64 yPayload(. void
15e95 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20 20 20 *pPayload,
15e96 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
15e97 20 74 6f 20 70 61 67 65 20 64 61 74 61 20 2a 2f to page data */
15e98 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 . void *pBuf,
15e99 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15e9a 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 Pointer to buffe
15e9b 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 r */. int nByte
15e9c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
15e9d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
15e9e 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 tes to copy */.
15e9f 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 20 20 20 int eOp,
15ea0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 /* 0
15ea1 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70 61 67 -> copy from pag
15ea2 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74 6f 20 e, 1 -> copy to
15ea3 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61 67 65 page */. DbPage
15ea4 20 2a 70 44 62 50 61 67 65 20 20 20 20 20 20 20 *pDbPage
15ea5 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 /* Page cont
15ea6 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61 64 20 aining pPayload
15ea7 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f 70 20 */.){. if( eOp
15ea8 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 ){. /* Copy d
15ea9 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65 72 20 ata from buffer
15eaa 74 6f 20 70 61 67 65 20 28 61 20 77 72 69 74 65 to page (a write
15eab 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 operation) */.
15eac 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 int rc = sqli
15ead 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 te3PagerWrite(pD
15eae 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
15eaf 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15eb0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
15eb1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 ;. }. memc
15eb2 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75 py(pPayload, pBu
15eb3 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 65 6c f, nByte);. }el
15eb4 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 se{. /* Copy
15eb5 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65 20 74 data from page t
15eb6 6f 20 62 75 66 66 65 72 20 28 61 20 72 65 61 64 o buffer (a read
15eb7 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 operation) */.
15eb8 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 memcpy(pBuf,
15eb9 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74 65 29 pPayload, nByte)
15eba 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
15ebb 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
15ebc 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
15ebd 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 is used to read
15ebe 20 6f 72 20 6f 76 65 72 77 72 69 74 65 20 70 61 or overwrite pa
15ebf 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f yload informatio
15ec0 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 74 n.** for the ent
15ec1 72 79 20 74 68 61 74 20 74 68 65 20 70 43 75 72 ry that the pCur
15ec2 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 cursor is point
15ec3 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 65 20 65 ing to. If the e
15ec4 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 Op.** parameter
15ec5 69 73 20 30 2c 20 74 68 69 73 20 69 73 20 61 20 is 0, this is a
15ec6 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 20 28 read operation (
15ec7 64 61 74 61 20 63 6f 70 69 65 64 20 69 6e 74 6f data copied into
15ec8 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 .** buffer pBuf)
15ec9 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 6e 2d 7a . If it is non-z
15eca 65 72 6f 2c 20 61 20 77 72 69 74 65 20 28 64 61 ero, a write (da
15ecb 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d 0a 2a ta copied from.*
15ecc 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 2e 0a * buffer pBuf)..
15ecd 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66 **.** A total of
15ece 20 22 61 6d 74 22 20 62 79 74 65 73 20 61 72 65 "amt" bytes are
15ecf 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
15ed0 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f beginning at "o
15ed1 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 20 ffset"..** Data
15ed2 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72 is read to or fr
15ed3 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 om the buffer pB
15ed4 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 uf..**.** This r
15ed5 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 outine does not
15ed6 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 make a distincti
15ed7 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65 79 20 61 on between key a
15ed8 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49 74 20 6a nd data..** It j
15ed9 75 73 74 20 72 65 61 64 73 20 6f 72 20 77 72 69 ust reads or wri
15eda 74 65 73 20 62 79 74 65 73 20 66 72 6f 6d 20 74 tes bytes from t
15edb 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e he payload area.
15edc 20 20 44 61 74 61 20 6d 69 67 68 74 20 0a 2a 2a Data might .**
15edd 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 6d appear on the m
15ede 61 69 6e 20 70 61 67 65 20 6f 72 20 62 65 20 73 ain page or be s
15edf 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 cattered out on
15ee0 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f multiple overflo
15ee1 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a 2a 0a w .** pages..**.
15ee2 2a 2a 20 49 66 20 74 68 65 20 42 74 43 75 72 73 ** If the BtCurs
15ee3 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e or.isIncrblobHan
15ee4 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c dle flag is set,
15ee5 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 and the current
15ee6 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74 72 79 .** cursor entry
15ee7 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 uses one or mor
15ee8 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 e overflow pages
15ee9 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a , this function.
15eea 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 ** allocates spa
15eeb 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c ce for and lazil
15eec 79 20 70 6f 70 6c 75 61 74 65 73 20 74 68 65 20 y popluates the
15eed 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 overflow page-li
15eee 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 61 72 72 st .** cache arr
15eef 61 79 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76 ay (BtCursor.aOv
15ef0 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 65 71 75 erflow). Subsequ
15ef1 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 20 74 68 ent calls use th
15ef2 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f 20 6d is.** cache to m
15ef3 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 74 ake seeking to t
15ef4 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66 66 73 he supplied offs
15ef5 65 74 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e et more efficien
15ef6 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e t..**.** Once an
15ef7 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
15ef8 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 ist cache has be
15ef9 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 en allocated, it
15efa 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76 61 6c may be.** inval
15efb 69 64 61 74 65 64 20 69 66 20 73 6f 6d 65 20 6f idated if some o
15efc 74 68 65 72 20 63 75 72 73 6f 72 20 77 72 69 74 ther cursor writ
15efd 65 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 es to the same t
15efe 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 able, or if.** t
15eff 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76 he cursor is mov
15f00 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e ed to a differen
15f01 74 20 72 6f 77 2e 20 41 64 64 69 74 69 6f 6e 61 t row. Additiona
15f02 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76 61 63 lly, in auto-vac
15f03 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 uum.** mode, the
15f04 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74 following event
15f05 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 61 74 65 s may invalidate
15f06 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
15f07 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a 2a 2a e-list cache..**
15f08 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63 72 65 .** * An incre
15f09 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c 0a 2a mental vacuum,.*
15f0a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69 * * A commit i
15f0b 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66 n auto_vacuum="f
15f0c 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 ull" mode,.**
15f0d 2a 20 43 72 65 61 74 69 6e 67 20 61 20 74 61 62 * Creating a tab
15f0e 6c 65 20 28 6d 61 79 20 72 65 71 75 69 72 65 20 le (may require
15f0f 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72 66 6c moving an overfl
15f10 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 ow page)..*/.sta
15f11 74 69 63 20 69 6e 74 20 61 63 63 65 73 73 50 61 tic int accessPa
15f12 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f yload(. BtCurso
15f13 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a r *pCur, /*
15f14 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 Cursor pointing
15f15 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 to entry to rea
15f16 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 d from */. u32
15f17 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 offset,
15f18 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e /* Begin readin
15f19 67 20 74 68 69 73 20 66 61 72 20 69 6e 74 6f 20 g this far into
15f1a 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 33 32 payload */. u32
15f1b 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 amt,
15f1c 20 20 2f 2a 20 52 65 61 64 20 74 68 69 73 20 6d /* Read this m
15f1d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 any bytes */. u
15f1e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42 nsigned char *pB
15f1f 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 uf, /* Write the
15f20 20 62 79 74 65 73 20 69 6e 74 6f 20 74 68 69 73 bytes into this
15f21 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 20 69 6e buffer */ . in
15f22 74 20 73 6b 69 70 4b 65 79 2c 20 20 20 20 20 20 t skipKey,
15f23 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 62 65 67 /* offset beg
15f24 69 6e 73 20 61 74 20 64 61 74 61 20 69 66 20 74 ins at data if t
15f25 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 his is true */.
15f26 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 20 20 int eOp
15f27 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 6f /* zero to
15f28 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20 read. non-zero
15f29 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a to write. */.){.
15f2a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
15f2b 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e 74 *aPayload;. int
15f2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
15f2d 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 69 . u32 nKey;. i
15f2e 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d nt iIdx = 0;. M
15f2f 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
15f30 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
15f31 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42 74 r->iPage]; /* Bt
15f32 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 72 72 ree page of curr
15f33 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 ent entry */. B
15f34 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
15f35 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 Cur->pBt;
15f36 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 /* Bt
15f37 72 65 65 20 74 68 69 73 20 63 75 72 73 6f 72 20 ree this cursor
15f38 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 belongs to */..
15f39 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 29 assert( pPage )
15f3a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
15f3b 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
15f3c 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
15f3d 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
15f3e 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 pCur->iPage]<pPa
15f3f 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 ge->nCell );. a
15f40 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
15f41 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
15f42 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 .. getCellInfo(
15f43 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f 61 pCur);. aPayloa
15f44 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 d = pCur->info.p
15f45 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e 66 Cell + pCur->inf
15f46 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b 65 o.nHeader;. nKe
15f47 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b y = (pPage->intK
15f48 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 70 43 ey ? 0 : (int)pC
15f49 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a ur->info.nKey);.
15f4a 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 . if( skipKey )
15f4b 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 {. offset +=
15f4c 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 nKey;. }. if(
15f4d 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 offset+amt > nKe
15f4e 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 y+pCur->info.nDa
15f4f 74 61 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c ta . || &aPayl
15f50 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e oad[pCur->info.n
15f51 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d Local] > &pPage-
15f52 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 >aData[pBt->usab
15f53 6c 65 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 leSize]. ){.
15f54 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 /* Trying to re
15f55 61 64 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 ad or write past
15f56 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
15f57 64 61 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 data is an error
15f58 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 */. return S
15f59 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15f5a 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 PT;. }.. /* Ch
15f5b 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 eck if data must
15f5c 20 62 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e be read/written
15f5d 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 to/from the btr
15f5e 65 65 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 ee page itself.
15f5f 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c */. if( offset<
15f60 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
15f61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d l ){. int a =
15f62 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b amt;. if( a+
15f63 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 offset>pCur->inf
15f64 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 o.nLocal ){.
15f65 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f a = pCur->info
15f66 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 .nLocal - offset
15f67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
15f68 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 copyPayload(&aP
15f69 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 ayload[offset],
15f6a 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 pBuf, a, eOp, pP
15f6b 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
15f6c 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 offset = 0;.
15f6d 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 pBuf += a;.
15f6e 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 amt -= a;. }e
15f6f 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 lse{. offset
15f70 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c -= pCur->info.nL
15f71 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 ocal;. }.. if(
15f72 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
15f73 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 & amt>0 ){. c
15f74 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a onst u32 ovflSiz
15f75 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
15f76 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 ize - 4; /* Byt
15f77 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f es content per o
15f78 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 vfl page */.
15f79 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a Pgno nextPage;..
15f7a 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 nextPage = g
15f7b 65 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 et4byte(&aPayloa
15f7c 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f d[pCur->info.nLo
15f7d 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 cal]);..#ifndef
15f7e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
15f7f 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 BLOB. /* If t
15f80 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e he isIncrblobHan
15f81 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 dle flag is set
15f82 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 and the BtCursor
15f83 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 .aOverflow[].
15f84 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ** has not been
15f85 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f allocated, allo
15f86 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 cate it now. The
15f87 20 61 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 array is sized
15f88 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e at. ** one en
15f89 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 try for each ove
15f8a 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 rflow page in th
15f8b 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e e overflow chain
15f8c 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 . The. ** pag
15f8d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
15f8e 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 first overflow p
15f8f 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e age is stored in
15f90 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 aOverflow[0],.
15f91 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c ** etc. A val
15f92 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 ue of 0 in the a
15f93 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 Overflow[] array
15f94 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 means "not yet
15f95 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 known". ** (t
15f96 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 he cache is lazi
15f97 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 ly populated)..
15f98 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 */. if( pC
15f99 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 ur->isIncrblobHa
15f9a 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 ndle && !pCur->a
15f9b 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
15f9c 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 int nOvfl = (p
15f9d 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f Cur->info.nPaylo
15f9e 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c ad-pCur->info.nL
15f9f 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 ocal+ovflSize-1)
15fa0 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 /ovflSize;.
15fa1 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
15fa2 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 = (Pgno *)sqlit
15fa3 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a e3MallocZero(siz
15fa4 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 eof(Pgno)*nOvfl)
15fa5 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66 ;. if( nOvf
15fa6 6c 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 l && !pCur->aOve
15fa7 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 rflow ){.
15fa8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
15fa9 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
15faa 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
15fab 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
15fac 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 ist cache has be
15fad 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 en allocated and
15fae 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 the. ** entr
15faf 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 y for the first
15fb0 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f required overflo
15fb1 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c w page is valid,
15fb2 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 skip. ** dir
15fb3 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 ectly to it..
15fb4 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 */. if( pCur
15fb5 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 ->aOverflow && p
15fb6 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f Cur->aOverflow[o
15fb7 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 ffset/ovflSize]
15fb8 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 ){. iIdx =
15fb9 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 (offset/ovflSize
15fba 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 );. nextPag
15fbb 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 e = pCur->aOverf
15fbc 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 low[iIdx];.
15fbd 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 offset = (offse
15fbe 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 t%ovflSize);.
15fbf 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 }.#endif.. f
15fc0 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 or( ; rc==SQLITE
15fc1 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 _OK && amt>0 &&
15fc2 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b nextPage; iIdx++
15fc3 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){..#ifndef SQLI
15fc4 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
15fc5 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 . /* If req
15fc6 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 uired, populate
15fc7 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
15fc8 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f e-list cache. */
15fc9 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d . if( pCur-
15fca 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 >aOverflow ){.
15fcb 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70 43 assert(!pC
15fcc 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 ur->aOverflow[iI
15fcd 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 dx] || pCur->aOv
15fce 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 erflow[iIdx]==ne
15fcf 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 xtPage);.
15fd0 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
15fd1 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 [iIdx] = nextPag
15fd2 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 e;. }.#endi
15fd3 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 f.. if( off
15fd4 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b set>=ovflSize ){
15fd5 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
15fd6 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 only reason to r
15fd7 65 61 64 20 74 68 69 73 20 70 61 67 65 20 69 73 ead this page is
15fd8 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 to obtain the p
15fd9 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e age. ** n
15fda 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 umber for the ne
15fdb 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f xt page in the o
15fdc 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 verflow chain. T
15fdd 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 he page.
15fde 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 ** data is not r
15fdf 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 equired. So firs
15fe0 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 t try to lookup
15fe1 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 the overflow.
15fe2 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 ** page-lis
15fe3 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c t cache, if any,
15fe4 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 then fall back
15fe5 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c to the getOverfl
15fe6 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 owPage().
15fe7 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 ** function..
15fe8 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 */.#ifndef
15fe9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
15fea 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 RBLOB. if
15feb 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f ( pCur->aOverflo
15fec 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 w && pCur->aOver
15fed 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a flow[iIdx+1] ){.
15fee 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 nextPa
15fef 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 ge = pCur->aOver
15ff0 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 flow[iIdx+1];.
15ff1 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 } else .#e
15ff2 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 ndif. r
15ff3 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 c = getOverflowP
15ff4 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 age(pBt, nextPag
15ff5 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 e, 0, &nextPage)
15ff6 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 ;. offset
15ff7 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 -= ovflSize;.
15ff8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
15ff9 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 /* Need to re
15ffa 61 64 20 74 68 69 73 20 70 61 67 65 20 70 72 6f ad this page pro
15ffb 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 perly. It contai
15ffc 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 ns some of the.
15ffd 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 ** range
15ffe 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 73 20 of data that is
15fff 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d being read (eOp=
16000 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 =0) or written (
16001 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 eOp!=0)..
16002 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 */. DbPa
16003 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 ge *pDbPage;.
16004 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 int a = amt
16005 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
16006 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
16007 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 Bt->pPager, next
16008 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b Page, &pDbPage);
16009 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
1600a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1600b 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 aPayload
1600c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
1600d 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b etData(pDbPage);
1600e 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 . nextP
1600f 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 age = get4byte(a
16010 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 Payload);.
16011 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 if( a + offs
16012 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b et > ovflSize ){
16013 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 20 3d . a =
16014 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 ovflSize - offs
16015 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a et;. }.
16016 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 rc = c
16017 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 opyPayload(&aPay
16018 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 load[offset+4],
16019 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 pBuf, a, eOp, pD
1601a 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
1601b 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
1601c 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ref(pDbPage);.
1601d 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d offset =
1601e 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 0;. am
1601f 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 t -= a;.
16020 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 pBuf += a;.
16021 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
16022 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
16023 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
16024 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 amt>0 ){. re
16025 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
16026 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 UPT_BKPT;. }.
16027 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
16028 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 .** Read part of
16029 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 the key associa
1602a 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 ted with cursor
1602b 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a pCur. Exactly.*
1602c 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 * "amt" bytes wi
1602d 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 ll be transfered
1602e 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 into pBuf[]. T
1602f 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 he transfer.** b
16030 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 egins at "offset
16031 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 "..**.** Return
16032 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
16033 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 cess or an error
16034 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e code if anythin
16035 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
16036 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 An error is re
16037 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 turned if "offse
16038 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 t+amt" is larger
16039 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 than.** the ava
1603a 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a ilable payload..
1603b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1603c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1603d 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a eeKey(BtCursor *
1603e 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 pCur, u32 offset
1603f 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 , u32 amt, void
16040 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 *pBuf){. int rc
16041 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
16042 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
16043 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 ur) );. rc = re
16044 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
16045 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 ion(pCur);. if(
16046 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16047 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 {. assert( pC
16048 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
16049 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 OR_VALID );.
1604a 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 assert( pCur->iP
1604b 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e age>=0 && pCur->
1604c 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1604d 67 65 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ge] );. if( p
1604e 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
1604f 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
16050 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
16051 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
16052 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
16053 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
16054 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 iPage]<pCur->apP
16055 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
16056 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 ->nCell );. r
16057 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 c = accessPayloa
16058 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 d(pCur, offset,
16059 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 amt, (unsigned c
1605a 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29 har*)pBuf, 0, 0)
1605b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1605c 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 c;.}../*.** Read
1605d 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
1605e 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
1605f 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 h cursor pCur.
16060 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 Exactly.** "amt"
16061 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 bytes will be t
16062 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 ransfered into p
16063 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e Buf[]. The tran
16064 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 sfer.** begins a
16065 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a t "offset"..**.*
16066 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
16067 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 OK on success or
16068 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
16069 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a f anything goes.
1606a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 ** wrong. An er
1606b 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
1606c 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 if "offset+amt"
1606d 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a is larger than.*
1606e 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 * the available
1606f 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 payload..*/.SQLI
16070 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16071 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
16072 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
16073 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
16074 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 amt, void *pBuf)
16075 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 {. int rc;..#if
16076 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16077 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 _INCRBLOB. if (
16078 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16079 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
1607a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1607b 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 TE_ABORT;. }.#e
1607c 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 ndif.. assert(
1607d 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
1607e 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d (pCur) );. rc =
1607f 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
16080 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
16081 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16082 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
16083 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
16084 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
16085 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
16086 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 >iPage>=0 && pCu
16087 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16088 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 iPage] );. as
16089 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
1608a 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
1608b 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
1608c 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 ->iPage]->nCell
1608d 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 );. rc = acce
1608e 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 ssPayload(pCur,
1608f 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 offset, amt, pBu
16090 66 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 f, 1, 0);. }.
16091 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
16092 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
16093 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 nter to payload
16094 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d information from
16095 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 the entry that
16096 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 the .** pCur cur
16097 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
16098 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 to. The pointer
16099 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e is to the begin
1609a 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b ning of.** the k
1609b 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 ey if skipKey==0
1609c 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 and it points t
1609d 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 o the beginning
1609e 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b of data if.** sk
1609f 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e ipKey==1. The n
160a0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
160a1 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f f available key/
160a2 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a data is written.
160a3 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 ** into *pAmt.
160a4 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 If *pAmt==0, the
160a5 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
160a6 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 rned will not be
160a7 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e .** a valid poin
160a8 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ter..**.** This
160a9 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 routine is an op
160aa 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 timization. It
160ab 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 is common for th
160ac 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 e entire key.**
160ad 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 and data to fit
160ae 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 on the local pag
160af 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 e and for there
160b0 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f to be no overflo
160b1 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 w.** pages. Whe
160b2 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 n that is so, th
160b3 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 is routine can b
160b4 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 e used to access
160b5 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 the.** key and
160b6 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b data without mak
160b7 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 ing a copy. If
160b8 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 the key and/or d
160b9 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e ata spills.** on
160ba 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
160bb 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 s, then accessPa
160bc 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 yload() must be
160bd 75 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 used to reassemb
160be 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 le.** the key/da
160bf 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 ta and copy it i
160c0 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 nto a preallocat
160c1 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a ed buffer..**.**
160c2 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 The pointer ret
160c3 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f urned by this ro
160c4 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 utine looks dire
160c5 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 ctly into the ca
160c6 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 ched.** page of
160c7 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
160c8 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 he data might ch
160c9 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 ange or move the
160ca 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e next time.** an
160cb 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 y btree routine
160cc 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 is called..*/.st
160cd 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
160ce 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 ned char *fetchP
160cf 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 ayload(. BtCurs
160d0 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f or *pCur, /
160d1 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e * Cursor pointin
160d2 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 g to entry to re
160d3 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 ad from */. int
160d4 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 *pAmt,
160d5 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e /* Write the n
160d6 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 umber of availab
160d7 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f le bytes here */
160d8 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 . int skipKey
160d9 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 /* read
160da 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 beginning at dat
160db 61 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 a if this is tru
160dc 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e e */.){. unsign
160dd 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 ed char *aPayloa
160de 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 d;. MemPage *pP
160df 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b age;. u32 nKey;
160e0 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a . u32 nLocal;..
160e1 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d assert( pCur!=
160e2 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 0 && pCur->iPage
160e3 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 >=0 && pCur->apP
160e4 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
160e5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
160e6 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
160e7 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 R_VALID );. ass
160e8 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
160e9 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
160ea 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
160eb 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
160ec 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 e];. assert( pC
160ed 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
160ee 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 iPage]<pPage->nC
160ef 65 6c 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c ell );. getCell
160f0 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 Info(pCur);. aP
160f1 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 ayload = pCur->i
160f2 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 nfo.pCell;. aPa
160f3 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 yload += pCur->i
160f4 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 nfo.nHeader;. i
160f5 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
160f6 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 ){. nKey = 0
160f7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
160f8 4b 65 79 20 3d 20 28 69 6e 74 29 70 43 75 72 2d Key = (int)pCur-
160f9 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a >info.nKey;. }.
160fa 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b if( skipKey ){
160fb 0a 20 20 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d . aPayload +=
160fc 20 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f 63 61 nKey;. nLoca
160fd 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e l = pCur->info.n
160fe 4c 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20 Local - nKey;.
160ff 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f 63 61 }else{. nLoca
16100 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e l = pCur->info.n
16101 4c 6f 63 61 6c 3b 0a 20 20 20 20 69 66 28 20 6e Local;. if( n
16102 4c 6f 63 61 6c 3e 6e 4b 65 79 20 29 7b 0a 20 20 Local>nKey ){.
16103 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 6e 4b 65 nLocal = nKe
16104 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a y;. }. }. *
16105 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b 0a 20 pAmt = nLocal;.
16106 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f 61 64 return aPayload
16107 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 ;.}.../*.** For
16108 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 63 the entry that c
16109 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 70 6f ursor pCur is po
1610a 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e 20 61 int to, return a
1610b 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65 73 20 s.** many bytes
1610c 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61 of the key or da
1610d 74 61 20 61 73 20 61 72 65 20 61 76 61 69 6c 61 ta as are availa
1610e 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c ble on the local
1610f 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 65 2e .** b-tree page.
16110 20 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 Write the numb
16111 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 er of available
16112 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41 6d 74 bytes into *pAmt
16113 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e ..**.** The poin
16114 74 65 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 ter returned is
16115 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68 65 20 ephemeral. The
16116 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d 6f 76 key/data may mov
16117 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73 74 72 e.** or be destr
16118 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65 78 74 oyed on the next
16119 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42 74 72 call to any Btr
1611a 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 69 ee routine,.** i
1611b 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 20 66 ncluding calls f
1611c 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65 61 64 rom other thread
1611d 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 73 61 s against the sa
1611e 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48 65 6e me cache..** Hen
1611f 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 ce, a mutex on t
16120 68 65 20 42 74 53 68 61 72 65 64 20 73 68 6f 75 he BtShared shou
16121 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69 6f 72 ld be held prior
16122 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 to calling.** t
16123 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a his routine..**.
16124 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
16125 73 20 69 73 20 75 73 65 64 20 74 6f 20 67 65 74 s is used to get
16126 20 71 75 69 63 6b 20 61 63 63 65 73 73 20 74 6f quick access to
16127 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a 2a 2a key and data.**
16128 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 in the common c
16129 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f 76 65 ase where no ove
1612a 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 20 rflow pages are
1612b 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f used..*/.SQLITE_
1612c 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f PRIVATE const vo
1612d 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 id *sqlite3Btree
1612e 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f KeyFetch(BtCurso
1612f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 r *pCur, int *pA
16130 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 mt){. assert( c
16131 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16132 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 70 pCur) );. if( p
16133 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
16134 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 SOR_VALID ){.
16135 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 76 return (const v
16136 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 oid*)fetchPayloa
16137 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 30 29 d(pCur, pAmt, 0)
16138 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
16139 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
1613a 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 TE const void *s
1613b 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 qlite3BtreeDataF
1613c 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 etch(BtCursor *p
1613d 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b Cur, int *pAmt){
1613e 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
1613f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
16140 29 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d ) );. if( pCur-
16141 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16142 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 VALID ){. ret
16143 75 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a urn (const void*
16144 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 )fetchPayload(pC
16145 75 72 2c 20 70 41 6d 74 2c 20 31 29 3b 0a 20 20 ur, pAmt, 1);.
16146 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
16147 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
16148 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 cursor down to
16149 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 a new child page
1614a 2e 20 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61 . The newPgno a
1614b 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a rgument is the.*
1614c 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 * page number of
1614d 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 the child page
1614e 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2f 0a 73 to move to..*/.s
1614f 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f tatic int moveTo
16150 43 68 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a Child(BtCursor *
16151 70 43 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e pCur, u32 newPgn
16152 6f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 o){. int rc;.
16153 69 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e 69 50 int i = pCur->iP
16154 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a age;. MemPage *
16155 70 4e 65 77 50 61 67 65 3b 0a 20 20 42 74 53 68 pNewPage;. BtSh
16156 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 ared *pBt = pCur
16157 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 ->pBt;.. assert
16158 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16159 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
1615a 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
1615b 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
1615c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
1615d 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53 ur->iPage<BTCURS
1615e 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a OR_MAX_DEPTH );.
1615f 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
16160 65 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58 e>=(BTCURSOR_MAX
16161 5f 44 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20 _DEPTH-1) ){.
16162 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
16163 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
16164 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e . rc = getAndIn
16165 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65 77 50 itPage(pBt, newP
16166 67 6e 6f 2c 20 26 70 4e 65 77 50 61 67 65 29 3b gno, &pNewPage);
16167 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
16168 72 6e 20 72 63 3b 0a 20 20 70 43 75 72 2d 3e 61 rn rc;. pCur->a
16169 70 50 61 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 pPage[i+1] = pNe
1616a 77 50 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 61 wPage;. pCur->a
1616b 69 49 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 iIdx[i+1] = 0;.
1616c 20 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a pCur->iPage++;.
1616d 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 . pCur->info.nS
1616e 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d ize = 0;. pCur-
1616f 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
16170 20 20 69 66 28 20 70 4e 65 77 50 61 67 65 2d 3e if( pNewPage->
16171 6e 43 65 6c 6c 3c 31 20 29 7b 0a 20 20 20 20 72 nCell<1 ){. r
16172 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
16173 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
16174 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
16175 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 K;.}..#ifndef ND
16176 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 EBUG./*.** Page
16177 70 50 61 72 65 6e 74 20 69 73 20 61 6e 20 69 6e pParent is an in
16178 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66 ternal (non-leaf
16179 29 20 74 72 65 65 20 70 61 67 65 2e 20 54 68 69 ) tree page. Thi
1617a 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61 s function .** a
1617b 73 73 65 72 74 73 20 74 68 61 74 20 70 61 67 65 sserts that page
1617c 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69 number iChild i
1617d 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69 6c 64 s the left-child
1617e 20 69 66 20 74 68 65 20 69 49 64 78 27 74 68 0a if the iIdx'th.
1617f 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67 65 20 ** cell in page
16180 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69 66 20 pParent. Or, if
16181 69 49 64 78 20 69 73 20 65 71 75 61 6c 20 74 6f iIdx is equal to
16182 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
16183 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 6e r of.** cells in
16184 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74 20 70 pParent, that p
16185 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c age number iChil
16186 64 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 d is the right-c
16187 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 70 hild of.** the p
16188 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
16189 6f 69 64 20 61 73 73 65 72 74 50 61 72 65 6e 74 oid assertParent
1618a 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 20 2a 70 Index(MemPage *p
1618b 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 49 64 78 Parent, int iIdx
1618c 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 29 7b 0a , Pgno iChild){.
1618d 20 20 61 73 73 65 72 74 28 20 69 49 64 78 3c 3d assert( iIdx<=
1618e 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 pParent->nCell )
1618f 3b 0a 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50 ;. if( iIdx==pP
16190 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a arent->nCell ){.
16191 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74 34 assert( get4
16192 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 byte(&pParent->a
16193 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 Data[pParent->hd
16194 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 69 43 68 rOffset+8])==iCh
16195 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a ild );. }else{.
16196 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74 34 assert( get4
16197 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 byte(findCell(pP
16198 61 72 65 6e 74 2c 20 69 49 64 78 29 29 3d 3d 69 arent, iIdx))==i
16199 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d 0a 23 Child );. }.}.#
1619a 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 61 else.# define a
1619b 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 ssertParentIndex
1619c 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 69 66 0a (x,y,z) .#endif.
1619d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 ./*.** Move the
1619e 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74 68 65 cursor up to the
1619f 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 2a 2a parent page..**
161a0 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 20 69 73 .** pCur->idx is
161a1 20 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c set to the cell
161a2 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f 6e 74 index that cont
161a3 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 ains the pointer
161a4 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 20 .** to the page
161a5 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 we are coming fr
161a6 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 65 20 63 om. If we are c
161a7 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 65 0a 2a oming from the.*
161a8 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 * right-most chi
161a9 6c 64 20 70 61 67 65 20 74 68 65 6e 20 70 43 75 ld page then pCu
161aa 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f r->idx is set to
161ab 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e 0a 2a one more than.*
161ac 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20 63 65 * the largest ce
161ad 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c ll index..*/.SQL
161ae 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
161af 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
161b0 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 eToParent(BtCurs
161b1 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
161b2 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
161b3 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
161b4 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
161b5 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
161b6 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 LID );. assert(
161b7 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 pCur->iPage>0 )
161b8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
161b9 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
161ba 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 Page] );. asser
161bb 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 tParentIndex(.
161bc 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 pCur->apPage[p
161bd 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a Cur->iPage-1], .
161be 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
161bf 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 pCur->iPage-1],
161c0 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 . pCur->apPag
161c1 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
161c2 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 pgno. );. rele
161c3 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 asePage(pCur->ap
161c4 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
161c5 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 ]);. pCur->iPag
161c6 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 e--;. pCur->inf
161c7 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 o.nSize = 0;. p
161c8 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
161c9 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 0;.}../*.** Mov
161ca 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
161cb 74 68 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2f the root page.*/
161cc 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 .static int move
161cd 54 6f 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 ToRoot(BtCursor
161ce 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 *pCur){. MemPag
161cf 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 e *pRoot;. int
161d0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
161d1 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 Btree *p = pCu
161d2 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 r->pBtree;. BtS
161d3 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
161d4 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pBt;.. assert(
161d5 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
161d6 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
161d7 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c rt( CURSOR_INVAL
161d8 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 ID < CURSOR_REQU
161d9 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 IRESEEK );. ass
161da 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 ert( CURSOR_VALI
161db 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 D < CURSOR_REQ
161dc 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 UIRESEEK );. as
161dd 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55 sert( CURSOR_FAU
161de 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 LT > CURSOR_RE
161df 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 QUIRESEEK );. i
161e0 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e f( pCur->eState>
161e1 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 =CURSOR_REQUIRES
161e2 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 EEK ){. if( p
161e3 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
161e4 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 SOR_FAULT ){.
161e5 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e return pCur->
161e6 73 6b 69 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 skip;. }.
161e7 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
161e8 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 rCursor(pCur);.
161e9 20 7d 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e }.. if( pCur->
161ea 69 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 iPage>=0 ){.
161eb 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
161ec 3d 31 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 =1; i<=pCur->iPa
161ed 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ge; i++){.
161ee 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 releasePage(pCur
161ef 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 ->apPage[i]);.
161f0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
161f1 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49 if( . SQLI
161f2 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 67 65 74 TE_OK!=(rc = get
161f3 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c AndInitPage(pBt,
161f4 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
161f5 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 &pCur->apPage[0
161f6 5d 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ])). ){.
161f7 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
161f8 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
161f9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
161fa 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 52 . }. }.. pR
161fb 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 oot = pCur->apPa
161fc 67 65 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74 28 ge[0];. assert(
161fd 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 pRoot->pgno==pC
161fe 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b 0a ur->pgnoRoot );.
161ff 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 pCur->iPage =
16200 30 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 0;. pCur->aiIdx
16201 5b 30 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d [0] = 0;. pCur-
16202 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
16203 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 . pCur->atLast
16204 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c = 0;. pCur->val
16205 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 69 idNKey = 0;.. i
16206 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d f( pRoot->nCell=
16207 3d 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 =0 && !pRoot->le
16208 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73 af ){. Pgno s
16209 75 62 70 61 67 65 3b 0a 20 20 20 20 61 73 73 65 ubpage;. asse
1620a 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d rt( pRoot->pgno=
1620b 3d 31 20 29 3b 0a 20 20 20 20 73 75 62 70 61 67 =1 );. subpag
1620c 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 e = get4byte(&pR
1620d 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 oot->aData[pRoot
1620e 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
1620f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 75 62 . assert( sub
16210 70 61 67 65 3e 30 20 29 3b 0a 20 20 20 20 70 43 page>0 );. pC
16211 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
16212 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 SOR_VALID;. r
16213 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 c = moveToChild(
16214 70 43 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a pCur, subpage);.
16215 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 }else{. pCu
16216 72 2d 3e 65 53 74 61 74 65 20 3d 20 28 28 70 52 r->eState = ((pR
16217 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55 oot->nCell>0)?CU
16218 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55 52 53 4f RSOR_VALID:CURSO
16219 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20 20 7d 0a R_INVALID);. }.
1621a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1621b 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 /*.** Move the c
1621c 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 ursor down to th
1621d 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 e left-most leaf
1621e 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 entry beneath t
1621f 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74 6f 20 77 he.** entry to w
16220 68 69 63 68 20 69 74 20 69 73 20 63 75 72 72 65 hich it is curre
16221 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a ntly pointing..*
16222 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f *.** The left-mo
16223 73 74 20 6c 65 61 66 20 69 73 20 74 68 65 20 6f st leaf is the o
16224 6e 65 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c ne with the smal
16225 6c 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 66 lest key - the f
16226 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73 63 65 6e irst.** in ascen
16227 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 ding order..*/.s
16228 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f tatic int moveTo
16229 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f Leftmost(BtCurso
1622a 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f r *pCur){. Pgno
1622b 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 pgno;. int rc
1622c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d = SQLITE_OK;. M
1622d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a emPage *pPage;..
1622e 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
1622f 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
16230 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
16231 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
16232 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 OR_VALID );. wh
16233 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
16234 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 OK && !(pPage =
16235 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16236 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 r->iPage])->leaf
16237 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
16238 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
16239 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e ->iPage]<pPage->
1623a 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e nCell );. pgn
1623b 6f 20 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e o = get4byte(fin
1623c 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 dCell(pPage, pCu
1623d 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
1623e 50 61 67 65 5d 29 29 3b 0a 20 20 20 20 72 63 20 Page]));. rc
1623f 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
16240 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 ur, pgno);. }.
16241 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
16242 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 *.** Move the cu
16243 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 rsor down to the
16244 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 right-most leaf
16245 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 entry beneath t
16246 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68 he.** page to wh
16247 69 63 68 20 69 74 20 69 73 20 63 75 72 72 65 6e ich it is curren
16248 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e tly pointing. N
16249 6f 74 69 63 65 20 74 68 65 20 64 69 66 66 65 72 otice the differ
1624a 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20 ence.** between
1624b 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 moveToLeftmost()
1624c 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74 and moveToRight
1624d 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c most(). moveToL
1624e 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e eftmost().** fin
1624f 64 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 ds the left-most
16250 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 entry beneath t
16251 68 65 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65 he *entry* where
16252 61 73 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f as moveToRightmo
16253 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 st().** finds th
16254 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 e right-most ent
16255 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a ry beneath the *
16256 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 page*..**.** The
16257 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 right-most entr
16258 79 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 y is the one wit
16259 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 h the largest ke
1625a 79 20 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 y - the last.**
1625b 6b 65 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 key in ascending
1625c 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 order..*/.stati
1625d 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68 c int moveToRigh
1625e 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a tmost(BtCursor *
1625f 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 pCur){. Pgno pg
16260 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 no;. int rc = S
16261 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 QLITE_OK;. MemP
16262 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a age *pPage = 0;.
16263 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
16264 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
16265 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
16266 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
16267 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 SOR_VALID );. w
16268 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
16269 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d _OK && !(pPage =
1626a 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1626b 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 ur->iPage])->lea
1626c 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 f ){. pgno =
1626d 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
1626e 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
1626f 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
16270 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
16271 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 r->iPage] = pPag
16272 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 e->nCell;. rc
16273 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 = moveToChild(p
16274 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a Cur, pgno);. }.
16275 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16276 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 75 72 2d _OK ){. pCur-
16277 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
16278 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 ge] = pPage->nCe
16279 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e ll-1;. pCur->
1627a 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
1627b 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e pCur->validN
1627c 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 Key = 0;. }. r
1627d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
1627e 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
1627f 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 to the first ent
16280 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ry in the table.
16281 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
16282 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 OK.** on success
16283 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 . Set *pRes to
16284 30 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 0 if the cursor
16285 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 actually points
16286 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 to something.**
16287 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 or set *pRes to
16288 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 1 if the table i
16289 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 s empty..*/.SQLI
1628a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1628b 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74 qlite3BtreeFirst
1628c 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
1628d 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 int *pRes){. i
1628e 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
1628f 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16290 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
16291 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
16292 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
16293 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
16294 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 ) );. rc = move
16295 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 ToRoot(pCur);.
16296 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16297 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 K ){. if( pCu
16298 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
16299 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 R_INVALID ){.
1629a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
1629b 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1629c 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 age]->nCell==0 )
1629d 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 ;. *pRes =
1629e 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 1;. rc = SQ
1629f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c LITE_OK;. }el
162a0 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
162a1 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
162a2 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
162a3 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 ll>0 );. *p
162a4 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 Res = 0;. r
162a5 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f c = moveToLeftmo
162a6 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a st(pCur);. }.
162a7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
162a8 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 .}../* Move the
162a9 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 cursor to the la
162aa 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
162ab 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 table. Return S
162ac 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 QLITE_OK.** on s
162ad 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 uccess. Set *pR
162ae 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 es to 0 if the c
162af 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 ursor actually p
162b0 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 oints to somethi
162b1 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 ng.** or set *pR
162b2 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 es to 1 if the t
162b3 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a able is empty..*
162b4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
162b5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
162b6 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 20 2a eLast(BtCursor *
162b7 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 pCur, int *pRes)
162b8 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20 {. int rc;. .
162b9 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
162ba 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
162bb 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
162bc 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
162bd 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
162be 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a >mutex) );.. /*
162bf 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 If the cursor a
162c0 6c 72 65 61 64 79 20 70 6f 69 6e 74 73 20 74 6f lready points to
162c1 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 2c the last entry,
162c2 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 this is a no-op
162c3 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55 52 53 4f . */. if( CURSO
162c4 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 R_VALID==pCur->e
162c5 53 74 61 74 65 20 26 26 20 70 43 75 72 2d 3e 61 State && pCur->a
162c6 74 4c 61 73 74 20 29 7b 0a 23 69 66 64 65 66 20 tLast ){.#ifdef
162c7 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
162c8 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 /* This block s
162c9 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 74 28 erves to assert(
162ca 29 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f ) that the curso
162cb 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f r really does po
162cc 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74 int . ** to t
162cd 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
162ce 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f 0a the b-tree. */.
162cf 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 int ii;.
162d0 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 for(ii=0; ii<pCu
162d1 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 7b r->iPage; ii++){
162d2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
162d3 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d 3d Cur->aiIdx[ii]==
162d4 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 5d pCur->apPage[ii]
162d5 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 7d ->nCell );. }
162d6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
162d7 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
162d8 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 Page]==pCur->apP
162d9 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
162da 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 20 ->nCell-1 );.
162db 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
162dc 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
162dd 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 6e 64 e]->leaf );.#end
162de 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 if. return SQ
162df 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
162e0 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 rc = moveToRoot(
162e1 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d pCur);. if( rc=
162e2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
162e3 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 if( CURSOR_INV
162e4 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
162e5 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 te ){. asse
162e6 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 rt( pCur->apPage
162e7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
162e8 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Cell==0 );.
162e9 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 *pRes = 1;.
162ea 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
162eb 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
162ec 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
162ed 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d );. *pRes =
162ee 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 0;. rc = m
162ef 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 oveToRightmost(p
162f0 43 75 72 29 3b 0a 20 20 20 20 20 20 70 43 75 72 Cur);. pCur
162f1 2d 3e 61 74 4c 61 73 74 20 3d 20 72 63 3d 3d 53 ->atLast = rc==S
162f2 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a 30 3b 0a 20 QLITE_OK ?1:0;.
162f3 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
162f4 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 n rc;.}../* Move
162f5 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f 20 74 the cursor so t
162f6 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f hat it points to
162f7 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72 20 74 an entry near t
162f8 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70 65 63 69 he key .** speci
162f9 66 69 65 64 20 62 79 20 70 49 64 78 4b 65 79 20 fied by pIdxKey
162fa 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 52 65 74 or intKey. Ret
162fb 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63 6f urn a success co
162fc 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 4e de..**.** For IN
162fd 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 74 68 65 TKEY tables, the
162fe 20 69 6e 74 4b 65 79 20 70 61 72 61 6d 65 74 65 intKey paramete
162ff 72 20 69 73 20 75 73 65 64 2e 20 20 70 49 64 78 r is used. pIdx
16300 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20 62 65 20 Key .** must be
16301 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e 64 65 78 NULL. For index
16302 20 74 61 62 6c 65 73 2c 20 70 49 64 78 4b 65 79 tables, pIdxKey
16303 20 69 73 20 75 73 65 64 20 61 6e 64 20 69 6e 74 is used and int
16304 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 Key.** is ignore
16305 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 d..**.** If an e
16306 78 61 63 74 20 6d 61 74 63 68 20 69 73 20 6e 6f xact match is no
16307 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 t found, then th
16308 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61 e cursor is alwa
16309 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 6e 74 ys.** left point
1630a 69 6e 67 20 61 74 20 61 20 6c 65 61 66 20 70 61 ing at a leaf pa
1630b 67 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 68 ge which would h
1630c 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 20 69 66 old the entry if
1630d 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 it.** were pres
1630e 65 6e 74 2e 20 20 54 68 65 20 63 75 72 73 6f 72 ent. The cursor
1630f 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 might point to
16310 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 63 6f an entry that co
16311 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 20 6f 72 mes.** before or
16312 20 61 66 74 65 72 20 74 68 65 20 6b 65 79 2e 0a after the key..
16313 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 65 72 **.** An integer
16314 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
16315 20 2a 70 52 65 73 20 77 68 69 63 68 20 69 73 20 *pRes which is
16316 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
16317 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 6b comparing the k
16318 65 79 20 77 69 74 68 20 74 68 65 20 65 6e 74 72 ey with the entr
16319 79 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 63 y to which the c
1631a 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20 70 6f 69 ursor is .** poi
1631b 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d 65 61 6e nting. The mean
1631c 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 ing of the integ
1631d 65 72 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a er written into.
1631e 2a 2a 20 2a 70 52 65 73 20 69 73 20 61 73 20 66 ** *pRes is as f
1631f 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
16320 20 20 2a 70 52 65 73 3c 30 20 20 20 20 20 20 54 *pRes<0 T
16321 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 he cursor is lef
16322 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e t pointing at an
16323 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 entry that.**
16324 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16325 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 is smaller than
16326 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 20 6f intKey/pIdxKey o
16327 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 r if the table i
16328 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 20 s empty.**
16329 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 and
1632a 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 74 68 the cursor is th
1632b 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 6f 69 erefore left poi
1632c 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a nt to nothing..*
1632d 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d 3d *.** *pRes==
1632e 30 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 0 The cursor
1632f 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
16330 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 g at an entry th
16331 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 at.**
16332 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 6d exactly m
16333 61 74 63 68 65 73 20 69 6e 74 4b 65 79 2f 70 49 atches intKey/pI
16334 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 dxKey..**.**
16335 20 2a 70 52 65 73 3e 30 20 20 20 20 20 20 54 68 *pRes>0 Th
16336 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 e cursor is left
16337 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 pointing at an
16338 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 entry that.**
16339 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
1633a 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 6e s larger than in
1633b 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a tKey/pIdxKey..**
1633c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1633d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1633e 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1633f 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 d(. BtCursor *p
16340 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Cur, /*
16341 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f 20 62 The cursor to b
16342 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 6e 70 e moved */. Unp
16343 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 ackedRecord *pId
16344 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 xKey, /* Unpacke
16345 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 d index key */.
16346 20 69 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20 i64 intKey,
16347 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
16348 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 table key */.
16349 69 6e 74 20 62 69 61 73 52 69 67 68 74 2c 20 20 int biasRight,
1634a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 /* If t
1634b 72 75 65 2c 20 62 69 61 73 20 74 68 65 20 73 65 rue, bias the se
1634c 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 arch to the high
1634d 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 end */. int *p
1634e 52 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20 Res
1634f 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 /* Write sear
16350 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 ch results here
16351 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
16352 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
16353 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
16354 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
16355 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
16356 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e d(pCur->pBtree->
16357 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
16358 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f /* If the curso
16359 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 73 r is already pos
1635a 69 74 69 6f 6e 65 64 20 61 74 20 74 68 65 20 70 itioned at the p
1635b 6f 69 6e 74 20 77 65 20 61 72 65 20 74 72 79 69 oint we are tryi
1635c 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 ng. ** to move
1635d 74 6f 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 to, then just re
1635e 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 turn without doi
1635f 6e 67 20 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 ng any work */.
16360 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
16361 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
16362 26 26 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b && pCur->validNK
16363 65 79 20 0a 20 20 20 26 26 20 70 43 75 72 2d 3e ey . && pCur->
16364 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 apPage[0]->intKe
16365 79 20 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 y . ){. if(
16366 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d pCur->info.nKey=
16367 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 =intKey ){.
16368 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 *pRes = 0;.
16369 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1636a 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 OK;. }. if
1636b 28 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 26 ( pCur->atLast &
1636c 26 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 & pCur->info.nKe
1636d 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 y<intKey ){.
1636e 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 *pRes = -1;.
1636f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16370 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a E_OK;. }. }.
16371 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f . rc = moveToRo
16372 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 ot(pCur);. if(
16373 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e rc ){. return
16374 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rc;. }. asser
16375 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
16376 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a pCur->iPage] );.
16377 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16378 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
16379 67 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 ge]->isInit );.
1637a 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
1637b 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 e==CURSOR_INVALI
1637c 44 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d D ){. *pRes =
1637d 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 -1;. assert(
1637e 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1637f 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
16380 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 l==0 );. retu
16381 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
16382 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 }. assert( pCur
16383 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 ->apPage[0]->int
16384 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 29 Key || pIdxKey )
16385 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 ;. for(;;){.
16386 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 3b 0a 20 int lwr, upr;.
16387 20 20 20 50 67 6e 6f 20 63 68 6c 64 50 67 3b 0a Pgno chldPg;.
16388 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 MemPage *pPa
16389 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
1638a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
1638b 20 20 20 20 69 6e 74 20 63 20 3d 20 2d 31 3b 20 int c = -1;
1638c 20 2f 2a 20 70 52 65 73 20 72 65 74 75 72 6e 20 /* pRes return
1638d 69 66 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 if table is empt
1638e 79 20 6d 75 73 74 20 62 65 20 2d 31 20 2a 2f 0a y must be -1 */.
1638f 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20 20 lwr = 0;.
16390 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e 43 upr = pPage->nC
16391 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 28 ell-1;. if( (
16392 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 !pPage->intKey &
16393 26 20 70 49 64 78 4b 65 79 3d 3d 30 29 20 7c 7c & pIdxKey==0) ||
16394 20 75 70 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 upr<0 ){.
16395 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
16396 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
16397 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 goto moveto_fini
16398 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 sh;. }. if
16399 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20 ( biasRight ){.
1639a 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 pCur->aiIdx
1639b 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 [pCur->iPage] =
1639c 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 (u16)upr;. }e
1639d 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d lse{. pCur-
1639e 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
1639f 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72 ge] = (u16)((upr
163a0 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a +lwr)/2);. }.
163a1 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 for(;;){.
163a2 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 int idx = pCu
163a3 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
163a4 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 Page]; /* Index
163a5 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 of current cell
163a6 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 in pPage */.
163a7 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 u8 *pCell;
163a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
163a9 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
163aa 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c to current cell
163ab 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 in pPage */..
163ac 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e pCur->info.n
163ad 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 Size = 0;.
163ae 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
163af 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70 (pPage, idx) + p
163b0 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
163b1 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ze;. if( pP
163b2 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
163b3 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c i64 nCell
163b4 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Key;. if(
163b5 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 pPage->hasData
163b6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 ){. u32
163b7 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 dummy;.
163b8 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 pCell += getVa
163b9 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 rint32(pCell, du
163ba 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a mmy);. }.
163bb 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e getVarin
163bc 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 t(pCell, (u64*)&
163bd 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 nCellKey);.
163be 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d if( nCellKey=
163bf 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 =intKey ){.
163c0 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 c = 0;.
163c1 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 }else if( nC
163c2 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b ellKey<intKey ){
163c3 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d . c = -
163c4 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 1;. }else
163c5 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
163c6 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 rt( nCellKey>int
163c7 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 Key );.
163c8 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 c = +1;.
163c9 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d }. pCur-
163ca 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a >validNKey = 1;.
163cb 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e pCur->in
163cc 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b fo.nKey = nCellK
163cd 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ey;. }else{
163ce 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
163cf 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 maximum supporte
163d0 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33 d page-size is 3
163d1 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73 2768 bytes. This
163d2 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 means that.
163d3 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d ** the maxim
163d4 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 um number of rec
163d5 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 ord bytes stored
163d6 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 on an index B-T
163d7 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 ree. ** p
163d8 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38 age is at most 8
163d9 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68 198 bytes, which
163da 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 may be stored a
163db 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 s a 2-byte.
163dc 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 ** varint. Th
163dd 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 is information i
163de 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 s used to attemp
163df 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 t to avoid parsi
163e0 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ng . ** t
163e1 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 he entire cell b
163e2 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 y checking for t
163e3 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74 he cases where t
163e4 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 he record is .
163e5 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 ** stored
163e6 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 entirely within
163e7 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 the b-tree page
163e8 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 by inspecting th
163e9 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 e first .
163ea 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 ** 2 bytes of t
163eb 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 he cell..
163ec 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 */. int
163ed 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d nCell = pCell[0]
163ee 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28 ;. if( !(
163ef 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26 nCell & 0x80) &&
163f0 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d nCell<=pPage->m
163f1 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 axLocal ){.
163f2 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
163f3 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 nch runs if the
163f4 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c record-size fiel
163f5 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 d of the cell is
163f6 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 a. **
163f7 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 single byte vari
163f8 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 nt and the recor
163f9 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 d fits entirely
163fa 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 on the main.
163fb 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 ** b-tree
163fc 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 page. */.
163fd 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
163fe 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
163ff 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 (nCell, (void*)&
16400 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 pCell[1], pIdxKe
16401 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 y);. }els
16402 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d e if( !(pCell[1]
16403 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 & 0x80) .
16404 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 && (nCell =
16405 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 ((nCell&0x7f)<<7
16406 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 ) + pCell[1])<=p
16407 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 Page->maxLocal.
16408 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ){.
16409 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 /* The recor
1640a 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 d-size field is
1640b 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 a 2 byte varint
1640c 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a and the record .
1640d 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 ** fit
1640e 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 s entirely on th
1640f 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 e main b-tree pa
16410 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ge. */.
16411 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 c = sqlite3Vdb
16412 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e eRecordCompare(n
16413 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 Cell, (void*)&pC
16414 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 ell[2], pIdxKey)
16415 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
16416 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
16417 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f e record flows o
16418 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 ver onto one or
16419 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
1641a 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 ges. In.
1641b 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 ** this case t
1641c 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 he whole cell ne
1641d 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 eds to be parsed
1641e 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 , a buffer alloc
1641f 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a ated. *
16420 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c * and accessPayl
16421 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 oad() used to re
16422 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 trieve the recor
16423 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 d into the.
16424 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 ** buffer b
16425 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 efore VdbeRecord
16426 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 Compare() can be
16427 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 called. */.
16428 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c void *pCel
16429 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 lKey;.
1642a 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c u8 * const pCell
1642b 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 Body = pCell - p
1642c 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
1642d 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ze;. sq
1642e 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 lite3BtreeParseC
1642f 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
16430 65 6c 6c 42 6f 64 79 2c 20 26 70 43 75 72 2d 3e ellBody, &pCur->
16431 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 info);.
16432 20 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29 70 43 nCell = (int)pC
16433 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 ur->info.nKey;.
16434 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 pCellKe
16435 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f y = sqlite3Mallo
16436 63 28 20 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 c( nCell );.
16437 20 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c 4b if( pCellK
16438 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ey==0 ){.
16439 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1643a 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
1643b 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f goto moveto_
1643c 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 finish;.
1643d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 }. rc
1643e 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 = accessPayload
1643f 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c 6c 2c (pCur, 0, nCell,
16440 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a (unsigned char*
16441 29 70 43 65 6c 6c 4b 65 79 2c 20 30 2c 20 30 29 )pCellKey, 0, 0)
16442 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 ;. c =
16443 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
16444 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 dCompare(nCell,
16445 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 pCellKey, pIdxKe
16446 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 y);. sq
16447 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c lite3_free(pCell
16448 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Key);.
16449 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f if( rc ) goto mo
1644a 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 veto_finish;.
1644b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1644c 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b if( c==0 ){
1644d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
1644e 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 ge->intKey && !p
1644f 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
16450 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 lwr = id
16451 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 70 72 x;. upr
16452 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 20 20 = lwr - 1;.
16453 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
16454 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
16455 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b *pRes = 0;
16456 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
16457 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
16458 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f goto moveto
16459 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 _finish;.
1645a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1645b 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 if( c<0 ){.
1645c 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b lwr = idx+1;
1645d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1645e 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d upr = idx-
1645f 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
16460 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a if( lwr>upr ){.
16461 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
16462 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 }. pCu
16463 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
16464 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 6c Page] = (u16)((l
16465 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 20 wr+upr)/2);.
16466 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 }. assert( lw
16467 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20 20 r==upr+1 );.
16468 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
16469 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 28 sInit );. if(
1646a 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a pPage->leaf ){.
1646b 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 30 chldPg = 0
1646c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1646d 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c lwr>=pPage->nCel
1646e 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 l ){. chldP
1646f 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 g = get4byte(&pP
16470 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
16471 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
16472 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
16473 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62 chldPg = get4b
16474 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 yte(findCell(pPa
16475 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d ge, lwr));. }
16476 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 3d . if( chldPg=
16477 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
16478 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
16479 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 pCur->iPage]<pCu
1647a 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
1647b 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b iPage]->nCell );
1647c 0a 20 20 20 20 20 20 69 66 28 20 70 52 65 73 20 . if( pRes
1647d 29 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 20 20 ) *pRes = c;.
1647e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1647f 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f K;. goto mo
16480 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 veto_finish;.
16481 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 }. pCur->aiI
16482 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
16483 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 20 20 = (u16)lwr;.
16484 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
16485 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
16486 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
16487 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
16488 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 67 ild(pCur, chldPg
16489 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
1648a 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 goto moveto_fini
1648b 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66 sh;. }.moveto_f
1648c 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72 6e 20 inish:. return
1648d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 rc;.}../*.** In
1648e 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 this version of
1648f 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 BtreeMoveto, pKe
16490 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e y is a packed in
16491 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 dex record.** su
16492 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 ch as is generat
16493 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b ed by the OP_Mak
16494 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 eRecord opcode.
16495 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 Unpack the.** r
16496 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 ecord and then c
16497 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 all BtreeMovetoU
16498 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 npacked() to do
16499 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 53 51 4c the work..*/.SQL
1649a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1649b 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1649c 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a to(. BtCursor *
1649d 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 pCur, /* Cur
1649e 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 sor open on the
1649f 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 72 btree to be sear
164a0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ched */. const
164a1 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a void *pKey, /*
164a2 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 74 Packed key if t
164a3 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 69 he btree is an i
164a4 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b ndex */. i64 nK
164a5 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ey, /*
164a6 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 Integer key for
164a7 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f tables. Size o
164a8 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 63 f pKey for indic
164a9 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 es */. int bias
164aa 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 , /* B
164ab 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 68 ias search to th
164ac 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 e high end */.
164ad 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 int *pRes
164ae 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 /* Write sea
164af 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65 rch results here
164b0 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
164b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164b2 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 6f /* Status co
164b3 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 de */. Unpacked
164b4 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b Record *pIdxKey;
164b5 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 /* Unpacked i
164b6 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68 ndex key */. ch
164b7 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b 20 ar aSpace[150];
164b8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 /* Temp
164b9 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 4b space for pIdxK
164ba 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 ey - to avoid a
164bb 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 0a 20 20 69 66 malloc */... if
164bc 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 ( pKey ){. as
164bd 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 sert( nKey==(i64
164be 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 )(int)nKey );.
164bf 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 pIdxKey = sqli
164c0 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
164c1 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e ack(pCur->pKeyIn
164c2 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 fo, (int)nKey, p
164c3 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
164c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164c5 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63 aSpac
164c6 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 e, sizeof(aSpace
164c7 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 ));. if( pIdx
164c8 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 Key==0 ) return
164c9 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
164ca 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
164cb 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
164cc 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
164cd 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
164ce 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
164cf 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
164d0 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 if( pKey ){.
164d1 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
164d2 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
164d3 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a d(pIdxKey);. }.
164d4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
164d5 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
164d6 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 UE if the cursor
164d7 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 is not pointing
164d8 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 at an entry of
164d9 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
164da 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 TRUE will be re
164db 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 turned after a c
164dc 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 all to sqlite3Bt
164dd 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a reeNext() moves.
164de 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 ** past the last
164df 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
164e0 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 ble or sqlite3Bt
164e1 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 reePrev() moves
164e2 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 past.** the firs
164e3 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 t entry. TRUE i
164e4 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 s also returned
164e5 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
164e6 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 empty..*/.SQLITE
164e7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
164e8 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 ite3BtreeEof(BtC
164e9 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
164ea 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 /* TODO: What if
164eb 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 the cursor is i
164ec 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 n CURSOR_REQUIRE
164ed 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 SEEK but all tab
164ee 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 le entries. **
164ef 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 have been delete
164f0 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c d? This API will
164f1 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 need to change
164f2 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 to return an err
164f3 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 or code. ** as
164f4 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c well as the bool
164f5 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 ean result value
164f6 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 .. */. return
164f7 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 (CURSOR_VALID!=p
164f8 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a Cur->eState);.}.
164f9 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
164fa 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
164fb 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72 ction handle for
164fc 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51 a cursor..*/.SQ
164fd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
164fe 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 74 72 ite3 *sqlite3Btr
164ff 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e 73 74 eeCursorDb(const
16500 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 BtCursor *pCur)
16501 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
16502 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
16503 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
16504 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 >mutex) );. ret
16505 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 72 65 65 urn pCur->pBtree
16506 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ->db;.}../*.** A
16507 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f dvance the curso
16508 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e r to the next en
16509 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
1650a 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 ase. If.** succ
1650b 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 essful then set
1650c 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 *pRes=0. If the
1650d 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 cursor.** was a
1650e 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 lready pointing
1650f 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 to the last entr
16510 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
16511 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 e before.** this
16512 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c routine was cal
16513 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 led, then set *p
16514 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Res=1..*/.SQLITE
16515 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16516 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 ite3BtreeNext(Bt
16517 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
16518 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 t *pRes){. int
16519 72 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 rc;. int idx;.
1651a 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
1651b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
1651c 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
1651d 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 r) );. rc = res
1651e 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
1651f 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 on(pCur);. if(
16520 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
16521 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
16522 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52 }. assert( pR
16523 65 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43 es!=0 );. if( C
16524 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 URSOR_INVALID==p
16525 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 Cur->eState ){.
16526 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
16527 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16528 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 OK;. }. if( pC
16529 75 72 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a 20 20 ur->skip>0 ){.
1652a 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 pCur->skip = 0
1652b 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b ;. *pRes = 0;
1652c 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1652d 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 TE_OK;. }. pCu
1652e 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a 20 20 r->skip = 0;..
1652f 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
16530 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
16531 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 ];. idx = ++pCu
16532 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
16533 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 Page];. assert(
16534 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
16535 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c ;. assert( idx<
16536 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b =pPage->nCell );
16537 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e .. pCur->info.n
16538 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 Size = 0;. pCur
16539 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b ->validNKey = 0;
1653a 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67 . if( idx>=pPag
1653b 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 e->nCell ){.
1653c 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
1653d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d ){. rc = m
1653e 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
1653f 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
16540 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
16541 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 drOffset+8]));.
16542 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
16543 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 turn rc;. r
16544 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f c = moveToLeftmo
16545 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 st(pCur);.
16546 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
16547 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
16548 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 }. do{.
16549 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d if( pCur->iPage=
1654a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 =0 ){. *p
1654b 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 Res = 1;.
1654c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
1654d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
1654e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
1654f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
16550 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
16551 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 treeMoveToParent
16552 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 (pCur);. pP
16553 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
16554 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
16555 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 . }while( pCu
16556 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
16557 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 Page]>=pPage->nC
16558 65 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 ell );. *pRes
16559 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 = 0;. if( pP
1655a 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
1655b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1655c 33 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 3BtreeNext(pCur,
1655d 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 pRes);. }els
1655e 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 e{. rc = SQ
1655f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
16560 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
16561 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 }. *pRes = 0;.
16562 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 if( pPage->leaf
16563 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16564 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
16565 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d rc = moveToLeftm
16566 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 ost(pCur);. ret
16567 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
16568 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 73 6f * Step the curso
16569 72 20 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f r to the back to
1656a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e the previous en
1656b 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 try in the datab
1656c 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 ase. If.** succ
1656d 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 essful then set
1656e 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 *pRes=0. If the
1656f 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 cursor.** was a
16570 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 lready pointing
16571 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 to the first ent
16572 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ry in the databa
16573 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 se before.** thi
16574 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 s routine was ca
16575 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a lled, then set *
16576 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 pRes=1..*/.SQLIT
16577 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16578 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f lite3BtreePrevio
16579 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 us(BtCursor *pCu
1657a 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
1657b 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 int rc;. MemPa
1657c 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 ge *pPage;.. as
1657d 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
1657e 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
1657f 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 rc = restoreCu
16580 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 rsorPosition(pCu
16581 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 r);. if( rc!=SQ
16582 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
16583 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
16584 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 pCur->atLast = 0
16585 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 ;. if( CURSOR_I
16586 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
16587 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 tate ){. *pRe
16588 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 s = 1;. retur
16589 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1658a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 . if( pCur->ski
1658b 70 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d p<0 ){. pCur-
1658c 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a >skip = 0;. *
1658d 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 pRes = 0;. re
1658e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1658f 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 }. pCur->skip
16590 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d = 0;.. pPage =
16591 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
16592 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 ur->iPage];. as
16593 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
16594 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 nit );. if( !pP
16595 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
16596 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d int idx = pCur-
16597 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
16598 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f ge];. rc = mo
16599 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
1659a 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c get4byte(findCel
1659b 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b l(pPage, idx)));
1659c 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
1659d 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1659e 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d }. rc = m
1659f 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 oveToRightmost(p
165a0 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Cur);. }else{.
165a1 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e while( pCur->
165a2 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
165a3 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 e]==0 ){. i
165a4 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d f( pCur->iPage==
165a5 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 0 ){. pCu
165a6 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
165a7 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
165a8 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 *pRes = 1;.
165a9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
165aa 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
165ab 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
165ac 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 reeMoveToParent(
165ad 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 pCur);. }.
165ae 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
165af 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d e = 0;. pCur-
165b0 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
165b1 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
165b2 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b [pCur->iPage]--;
165b3 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 . pPage = pCu
165b4 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
165b5 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 iPage];. if(
165b6 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 pPage->intKey &&
165b7 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
165b8 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
165b9 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
165ba 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 (pCur, pRes);.
165bb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
165bc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
165bd 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 }. }. *pRes
165be 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 = 0;. return r
165bf 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f c;.}../*.** Allo
165c0 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 cate a new page
165c1 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
165c2 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
165c3 65 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 e new page is ma
165c4 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 rked as dirty.
165c5 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c (In other words,
165c6 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
165c7 74 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 te().** has alre
165c8 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 ady been called
165c9 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e on the new page.
165ca 29 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 ) The new page
165cb 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e has also.** been
165cc 20 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 referenced and
165cd 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 the calling rout
165ce 69 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 ine is responsib
165cf 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a le for calling.*
165d0 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e * sqlite3PagerUn
165d1 72 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 ref() on the new
165d2 20 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 page when it is
165d3 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c done..**.** SQL
165d4 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
165d5 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 ed on success.
165d6 41 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e Any other return
165d7 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 value indicates
165d8 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a .** an error. *
165d9 70 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e ppPage and *pPgn
165da 6f 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 o are undefined
165db 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 in the event of
165dc 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 an error..** Do
165dd 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 not invoke sqlit
165de 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f e3PagerUnref() o
165df 6e 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 n *ppPage if an
165e0 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
165e1 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
165e2 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 "nearby" paramet
165e3 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 er is not 0, the
165e4 6e 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 n a (feeble) eff
165e5 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a ort is made to .
165e6 2a 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 ** locate a page
165e7 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 close to the pa
165e8 67 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 ge number "nearb
165e9 79 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 y". This can be
165ea 20 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 used in an.** a
165eb 74 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 ttempt to keep r
165ec 65 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f elated pages clo
165ed 73 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 se to each other
165ee 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
165ef 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 file,.** which
165f0 69 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 in turn can make
165f1 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 database access
165f2 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 faster..**.** I
165f3 66 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61 f the "exact" pa
165f4 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 rameter is not 0
165f5 2c 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e , and the page-n
165f6 75 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 umber nearby exi
165f7 73 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 sts .** anywhere
165f8 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
165f9 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 t, then it is gu
165fa 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 arenteed to be r
165fb 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a eturned. This.**
165fc 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 is only used by
165fd 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 auto-vacuum dat
165fe 61 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f abases when allo
165ff 63 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 cating a new tab
16600 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
16601 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 t allocateBtreeP
16602 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 age(. BtShared
16603 2a 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 *pBt, . MemPage
16604 20 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 **ppPage, . Pg
16605 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 no *pPgno, . Pg
16606 6e 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 no nearby,. u8
16607 65 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 exact.){. MemPa
16608 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e ge *pPage1;. in
16609 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 t rc;. int n;
1660a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1660b 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 pages on the fre
1660c 65 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b elist */. int k
1660d 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
1660e 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 of leaves on the
1660f 20 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 trunk of the fr
16610 65 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 eelist */. MemP
16611 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b age *pTrunk = 0;
16612 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 . MemPage *pPre
16613 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61 vTrunk = 0;.. a
16614 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
16615 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
16616 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 utex) );. pPage
16617 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
16618 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 . n = get4byte(
16619 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
1661a 36 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20 29 6]);. if( n>0 )
1661b 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 {. /* There a
1661c 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 re pages on the
1661d 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 freelist. Reuse
1661e 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 one of those pa
1661f 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f ges. */. Pgno
16620 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 iTrunk;. u8
16621 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 searchList = 0;
16622 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c /* If the free-l
16623 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 ist must be sear
16624 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 ched for 'nearby
16625 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a ' */. . /*
16626 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20 If the 'exact'
16627 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 parameter was tr
16628 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f ue and a query o
16629 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 f the pointer-ma
1662a 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 p. ** shows t
1662b 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 hat the page 'ne
1662c 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 arby' is somewhe
1662d 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c re on the free-l
1662e 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a ist, then. **
1662f 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 the entire-list
16630 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 will be searche
16631 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e d for that page.
16632 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 . */.#ifndef
16633 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
16634 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 VACUUM. if( e
16635 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d xact && nearby<=
16636 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
16637 42 74 29 20 29 7b 0a 20 20 20 20 20 20 75 38 20 Bt) ){. u8
16638 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61 73 73 eType;. ass
16639 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20 29 3b ert( nearby>0 );
1663a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1663b 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
1663c 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 ;. rc = ptr
1663d 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72 mapGet(pBt, near
1663e 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a by, &eType, 0);.
1663f 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
16640 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
16641 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 if( eType==PTRMA
16642 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 P_FREEPAGE ){.
16643 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 searchList
16644 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
16645 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 *pPgno = nea
16646 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 rby;. }.#endi
16647 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d f.. /* Decrem
16648 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73 ent the free-lis
16649 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 t count by 1. Se
1664a 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 t iTrunk to the
1664b 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20 index of the.
1664c 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c ** first free-l
1664d 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 ist trunk page.
1664e 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e iPrevTrunk is in
1664f 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a itially 1.. *
16650 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
16651 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
16652 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
16653 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
16654 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62 rn rc;. put4b
16655 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
16656 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 ta[36], n-1);..
16657 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 /* The code w
16658 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 ithin this loop
16659 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 is run only once
1665a 20 69 66 20 74 68 65 20 27 73 65 61 72 63 68 4c if the 'searchL
1665b 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20 ist' variable.
1665c 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 ** is not true
1665d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 . Otherwise, it
1665e 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 runs once for ea
1665f 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e ch trunk-page on
16660 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 the. ** free
16661 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20 -list until the
16662 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 page 'nearby' is
16663 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20 2a 2f located.. */
16664 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 . do {.
16665 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 pPrevTrunk = pTr
16666 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 unk;. if( p
16667 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 PrevTrunk ){.
16668 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 iTrunk = ge
16669 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 t4byte(&pPrevTru
1666a 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 nk->aData[0]);.
1666b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1666c 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 iTrunk = get
1666d 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
1666e 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 Data[32]);.
1666f 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
16670 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
16671 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 e(pBt, iTrunk, &
16672 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 pTrunk, 0);.
16673 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
16674 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a pTrunk = 0;.
16675 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
16676 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
16677 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 6b }.. k
16678 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 = get4byte(&pTr
16679 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a unk->aData[4]);.
1667a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 26 if( k==0 &
1667b 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29 7b & !searchList ){
1667c 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
1667d 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 trunk has no lea
1667e 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73 74 ves and the list
1667f 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 65 is not being se
16680 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20 20 arched. .
16681 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20 74 ** So extract t
16682 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 74 he trunk page it
16683 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74 20 self and use it
16684 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 20 as the newly .
16685 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 ** allocat
16686 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 ed page */.
16687 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76 assert( pPrev
16688 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 Trunk==0 );.
16689 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1668a 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
1668b 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
1668c 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
1668d 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
1668e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
1668f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
16690 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 *pPgno = iTru
16691 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 nk;. memc
16692 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 py(&pPage1->aDat
16693 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e a[32], &pTrunk->
16694 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
16695 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
16696 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 pTrunk;.
16697 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 pTrunk = 0;.
16698 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f TRACE(("ALLO
16699 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d CATE: %d trunk -
1669a 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c %d free pages l
1669b 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 eft\n", *pPgno,
1669c 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c n-1));. }el
1669d 73 65 20 69 66 28 20 6b 3e 70 42 74 2d 3e 75 73 se if( k>pBt->us
1669e 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29 ableSize/4 - 2 )
1669f 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c {. /* Val
166a0 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f ue of k is out o
166a1 66 20 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61 f range. Databa
166a2 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f se corruption */
166a3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
166a4 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
166a5 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 T;. goto
166a6 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
166a7 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 e;.#ifndef SQLIT
166a8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
166a9 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 M. }else if
166aa 28 20 73 65 61 72 63 68 4c 69 73 74 20 26 26 20 ( searchList &&
166ab 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 29 nearby==iTrunk )
166ac 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
166ad 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67 20 73 list is being s
166ae 65 61 72 63 68 65 64 20 61 6e 64 20 74 68 69 73 earched and this
166af 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 74 trunk page is t
166b0 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 he page.
166b1 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 ** to allocate,
166b2 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 regardless of wh
166b3 65 74 68 65 72 20 69 74 20 68 61 73 20 6c 65 61 ether it has lea
166b4 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ves.. */.
166b5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
166b6 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 *pPgno==iTrunk )
166b7 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 ;. *ppPag
166b8 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 e = pTrunk;.
166b9 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d searchList =
166ba 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 0;. rc =
166bb 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
166bc 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
166bd 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
166be 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
166bf 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
166c0 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
166c1 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b }. if( k
166c2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
166c3 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b if( !pPrevTrunk
166c4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
166c5 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e memcpy(&pPage1->
166c6 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 aData[32], &pTru
166c7 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 nk->aData[0], 4)
166c8 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
166c9 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d e{. m
166ca 65 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 6e emcpy(&pPrevTrun
166cb 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 k->aData[0], &pT
166cc 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 runk->aData[0],
166cd 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 4);. }.
166ce 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
166cf 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
166d0 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65 trunk page is re
166d1 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61 quired by the ca
166d2 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74 ller but it cont
166d3 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20 ains .
166d4 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66 ** pointers to f
166d5 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e ree-list leaves.
166d6 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20 The first leaf
166d7 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a becomes a trunk.
166d8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 ** pag
166d9 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a e in this case..
166da 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
166db 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a MemPage *
166dc 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20 pNewTrunk;.
166dd 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72 Pgno iNewTr
166de 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 unk = get4byte(&
166df 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d pTrunk->aData[8]
166e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
166e1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
166e2 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65 77 54 tPage(pBt, iNewT
166e3 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b runk, &pNewTrunk
166e4 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
166e5 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
166e6 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
166e7 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
166e8 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
166e9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 }. r
166ea 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
166eb 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d Write(pNewTrunk-
166ec 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
166ed 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
166ee 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
166ef 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
166f0 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 e(pNewTrunk);.
166f1 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
166f2 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
166f3 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
166f4 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
166f5 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 pNewTrunk->aData
166f6 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 [0], &pTrunk->aD
166f7 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 ata[0], 4);.
166f8 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
166f9 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 pNewTrunk->aData
166fa 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 [4], k-1);.
166fb 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 memcpy(&pNe
166fc 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d wTrunk->aData[8]
166fd 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 , &pTrunk->aData
166fe 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a [12], (k-1)*4);.
166ff 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
16700 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 ePage(pNewTrunk)
16701 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
16702 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 !pPrevTrunk ){.
16703 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
16704 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
16705 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
16706 31 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 1->pDbPage) );.
16707 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 put4b
16708 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
16709 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e ta[32], iNewTrun
1670a 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 k);. }e
1670b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
1670c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1670d 65 72 57 72 69 74 65 28 70 50 72 65 76 54 72 75 erWrite(pPrevTru
1670e 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
1670f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
16710 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
16711 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
16712 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
16713 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
16714 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
16715 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b revTrunk->aData[
16716 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 0], iNewTrunk);.
16717 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
16718 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54 }. pT
16719 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 runk = 0;.
1671a 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 TRACE(("ALLOCA
1671b 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 TE: %d trunk - %
1671c 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 d free pages lef
1671d 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d t\n", *pPgno, n-
1671e 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 1));.#endif.
1671f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
16720 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 /* Extract a le
16721 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e af from the trun
16722 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 k */. int
16723 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 closest;.
16724 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 Pgno iPage;.
16725 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 unsigned c
16726 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 har *aData = pTr
16727 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 unk->aData;.
16728 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16729 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
1672a 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
1672b 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
1672c 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
1672d 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
1672e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1672f 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 if( nearby>0
16730 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
16731 20 69 2c 20 64 69 73 74 3b 0a 20 20 20 20 20 20 i, dist;.
16732 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b closest = 0;
16733 0a 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 . dist
16734 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 = get4byte(&aDat
16735 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a a[8]) - nearby;.
16736 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 69 if( di
16737 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 st<0 ) dist = -d
16738 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 ist;. f
16739 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b or(i=1; i<k; i++
1673a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
1673b 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 nt d2 = get4byte
1673c 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 (&aData[8+i*4])
1673d 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 - nearby;.
1673e 20 20 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 if( d2<0 )
1673f 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 d2 = -d2;.
16740 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 if( d2<di
16741 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 st ){.
16742 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b closest = i;
16743 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 . d
16744 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 ist = d2;.
16745 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
16746 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
16747 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f e{. clo
16748 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 sest = 0;.
16749 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 }.. iPa
1674a 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 ge = get4byte(&a
1674b 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 Data[8+closest*4
1674c 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ]);. if(
1674d 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 !searchList || i
1674e 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a Page==nearby ){.
1674f 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f int no
16750 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 Content;.
16751 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 0a 20 Pgno nPage;.
16752 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 *pPgno
16753 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 = iPage;.
16754 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 nPage = pager
16755 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
16756 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 if( *p
16757 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a 20 20 Pgno>nPage ){.
16758 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 /* Fre
16759 65 20 70 61 67 65 20 6f 66 66 20 74 68 65 20 65 e page off the e
1675a 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a nd of the file *
1675b 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 /. rc
1675c 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
1675d 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 T_BKPT;.
1675e 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
1675f 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
16760 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
16761 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 TRACE(("ALLOCA
16762 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 TE: %d was leaf
16763 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e %d of %d on trun
16764 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 k %d".
16765 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 ": %d mor
16766 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c e free pages\n",
16767 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
16768 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 *pPgno, closes
16769 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e t+1, k, pTrunk->
1676a 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 pgno, n-1));.
1676b 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 if( close
1676c 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 st<k-1 ){.
1676d 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44 memcpy(&aD
1676e 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d ata[8+closest*4]
1676f 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c , &aData[4+k*4],
16770 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 4);. }
16771 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 . put4b
16772 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b yte(&aData[4], k
16773 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 -1);. a
16774 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
16775 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
16776 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20 Trunk->pDbPage)
16777 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 );. noC
16778 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 ontent = !btreeG
16779 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 etHasContent(pBt
1677a 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20 , *pPgno);.
1677b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1677c 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 3BtreeGetPage(pB
1677d 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 t, *pPgno, ppPag
1677e 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 e, noContent);.
1677f 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
16780 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16781 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
16782 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
16783 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 ((*ppPage)->pDbP
16784 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
16785 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16786 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
16787 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
16788 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 (*ppPage);.
16789 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1678a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 }. s
1678b 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 earchList = 0;.
1678c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1678d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1678e 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a ge(pPrevTrunk);.
1678f 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b pPrevTrunk
16790 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65 = 0;. }while
16791 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a ( searchList );.
16792 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
16793 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 There are no pag
16794 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 es on the freeli
16795 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20 st, so create a
16796 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a new page at the.
16797 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68 ** end of th
16798 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e e file */. in
16799 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 t nPage = pagerP
1679a 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 agecount(pBt);.
1679b 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61 67 *pPgno = nPag
1679c 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 28 20 e + 1;.. if(
1679d 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f *pPgno==PENDING_
1679e 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
1679f 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29 {. (*pPgno)
167a0 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 ++;. }..#ifnd
167a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
167a2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
167a3 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
167a4 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50 41 m && PTRMAP_ISPA
167a5 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 20 GE(pBt, *pPgno)
167a6 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 2a ){. /* If *
167a7 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f 20 pPgno refers to
167a8 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 a pointer-map pa
167a9 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 6f ge, allocate two
167aa 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20 20 new pages.
167ab 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f ** at the end o
167ac 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74 65 f the file inste
167ad 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 66 ad of one. The f
167ae 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 70 irst allocated p
167af 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 63 age. ** bec
167b0 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e 74 omes a new point
167b1 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 65 er-map page, the
167b2 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64 20 second is used
167b3 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 by the caller..
167b4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d 65 */. Me
167b5 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b 0a mPage *pPg = 0;.
167b6 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c TRACE(("AL
167b7 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 LOCATE: %d from
167b8 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f 69 end of file (poi
167b9 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c 6e nter-map page)\n
167ba 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 20 ", *pPgno));.
167bb 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e assert( *pPgn
167bc 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f o!=PENDING_BYTE_
167bd 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 PAGE(pBt) );.
167be 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
167bf 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
167c0 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 *pPgno, &pPg, 0
167c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
167c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
167c3 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
167c4 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 e3PagerWrite(pPg
167c5 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
167c6 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
167c7 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 pPg);. }.
167c8 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
167c9 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 28 2a urn rc;. (*
167ca 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 pPgno)++;.
167cb 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 if( *pPgno==PEND
167cc 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
167cd 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b t) ){ (*pPgno)++
167ce 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ; }. }.#endif
167cf 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 .. assert( *p
167d0 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
167d1 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
167d2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
167d3 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
167d4 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 , *pPgno, ppPage
167d5 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
167d6 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
167d7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
167d8 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 gerWrite((*ppPag
167d9 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e)->pDbPage);.
167da 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
167db 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c _OK ){. rel
167dc 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 easePage(*ppPage
167dd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 41 );. }. TRA
167de 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 CE(("ALLOCATE: %
167df 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 d from end of fi
167e0 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b le\n", *pPgno));
167e1 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
167e2 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f *pPgno!=PENDING_
167e3 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
167e4 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f ;..end_allocate_
167e5 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 50 page:. releaseP
167e6 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 age(pTrunk);. r
167e7 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76 eleasePage(pPrev
167e8 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72 63 Trunk);. if( rc
167e9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
167ea 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 if( sqlite3Pa
167eb 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
167ec 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 (*ppPage)->pDbPa
167ed 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 72 ge)>1 ){. r
167ee 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 eleasePage(*ppPa
167ef 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ge);. retur
167f0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
167f1 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
167f2 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 6e (*ppPage)->isIn
167f3 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 it = 0;. }. re
167f4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
167f5 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
167f6 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 is used to add p
167f7 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 age iPage to the
167f8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
167f9 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 ree-list. .** It
167fa 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
167fb 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
167fc 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 already a part
167fd 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 of the free-list
167fe 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ..**.** The valu
167ff 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
16800 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
16801 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
16802 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a is optional..**
16803 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 If the caller h
16804 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 appens to have a
16805 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
16806 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a MemPage object .
16807 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
16808 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 to page iPage h
16809 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 andy, it may pas
1680a 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f s it as the seco
1680b 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 nd value. .** Ot
1680c 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 herwise, it may
1680d 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a pass NULL..**.**
1680e 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f If a pointer to
1680f 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 a MemPage objec
16810 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 t is passed as t
16811 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
16812 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 nt,.** its refer
16813 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f ence count is no
16814 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 t altered by thi
16815 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 s function..*/.s
16816 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 tatic int freePa
16817 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 ge2(BtShared *pB
16818 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d t, MemPage *pMem
16819 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 Page, Pgno iPage
1681a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 ){. MemPage *pT
1681b 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 runk = 0;
1681c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
1681d 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
1681e 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e */. Pgno iTrun
1681f 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 k = 0;
16820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
16821 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 e number of free
16822 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
16823 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a */ . MemPage *
16824 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 pPage1 = pBt->pP
16825 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f age1; /* Lo
16826 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f cal reference to
16827 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d page 1 */. Mem
16828 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 Page *pPage;
16829 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1682a 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 /* Page being f
1682b 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c reed. May be NUL
1682c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 L. */. int rc;
1682d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1682e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1682f 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 eturn Code */.
16830 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 int nFree;
16831 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16832 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e /* Initial n
16833 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
16834 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a n free-list */..
16835 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16836 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
16837 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
16838 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b sert( iPage>1 );
16839 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d . assert( !pMem
1683a 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 Page || pMemPage
1683b 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b ->pgno==iPage );
1683c 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 .. if( pMemPage
1683d 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
1683e 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 pMemPage;. sq
1683f 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 lite3PagerRef(pP
16840 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
16841 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 }else{. pPag
16842 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f e = btreePageLoo
16843 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b kup(pBt, iPage);
16844 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 . }.. /* Incre
16845 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 ment the free pa
16846 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 ge count on pPag
16847 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c e1 */. rc = sql
16848 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
16849 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
1684a 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f . if( rc ) goto
1684b 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
1684c 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 nFree = get4byt
1684d 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
1684e 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 [36]);. put4byt
1684f 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
16850 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a [36], nFree+1);.
16851 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 .#ifdef SQLITE_S
16852 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f ECURE_DELETE. /
16853 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f * If the SQLITE_
16854 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f SECURE_DELETE co
16855 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f mpile-time optio
16856 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 n is enabled, th
16857 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 en. ** always f
16858 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 ully overwrite d
16859 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 eleted informati
1685a 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 on with zeros..
1685b 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 */. if( (!pPag
1685c 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 e && (rc = sqlit
1685d 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
1685e 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 67 Bt, iPage, &pPag
1685f 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20 20 20 e, 0))). ||
16860 20 20 20 20 20 20 20 20 20 28 72 63 20 3d 20 73 (rc = s
16861 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
16862 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
16863 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ). ){. goto
16864 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
16865 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65 }. memset(pPage
16866 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67 ->aData, 0, pPag
16867 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e->pBt->pageSize
16868 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 );.#endif.. /*
16869 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
1686a 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 supports auto-va
1686b 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 65 cuum, write an e
1686c 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e ntry in the poin
1686d 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 ter-map. ** to
1686e 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 indicate that th
1686f 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e 0a e page is free..
16870 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54 */. if( ISAUT
16871 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 72 OVACUUM ){. r
16872 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 c = ptrmapPut(pB
16873 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 t, iPage, PTRMAP
16874 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b 0a 20 _FREEPAGE, 0);.
16875 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
16876 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
16877 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e }.. /* Now man
16878 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 74 75 ipulate the actu
16879 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 65 65 al database free
1687a 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 65 2e -list structure.
1687b 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 0a 20 There are two.
1687c 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 69 65 ** possibilitie
1687d 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c s. If the free-l
1687e 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ist is currently
1687f 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 74 68 empty, or if th
16880 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 72 75 e first. ** tru
16881 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 nk page in the f
16882 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c ree-list is full
16883 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 , then this page
16884 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 will become a.
16885 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 ** new free-lis
16886 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74 t trunk page. Ot
16887 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 6c 6c herwise, it will
16888 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f become a leaf o
16889 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 f the. ** first
1688a 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 trunk page in t
1688b 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 65 2d he current free-
1688c 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b list. This block
1688d 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 20 2a tests if it. *
1688e 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f * is possible to
1688f 20 61 64 64 20 74 68 65 20 70 61 67 65 20 61 73 add the page as
16890 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 a new free-list
16891 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 leaf.. */. if
16892 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a 20 20 ( nFree!=0 ){.
16893 20 20 69 6e 74 20 6e 4c 65 61 66 3b 20 20 20 20 int nLeaf;
16894 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
16895 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 nitial number of
16896 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74 leaf cells on t
16897 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20 runk page */..
16898 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 iTrunk = get4b
16899 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
1689a 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 63 20 ta[32]);. rc
1689b 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
1689c 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e tPage(pBt, iTrun
1689d 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a k, &pTrunk, 0);.
1689e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1689f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 TE_OK ){. g
168a0 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 oto freepage_out
168a1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65 ;. }.. nLe
168a2 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 af = get4byte(&p
168a3 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 Trunk->aData[4])
168a4 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 3c ;. if( nLeaf<
168a5 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
168a6 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
168a7 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
168a8 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
168a9 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 }. if( nLea
168aa 66 3c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a f<pBt->usableSiz
168ab 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20 e/4 - 8 ){.
168ac 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
168ad 20 74 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f there is room o
168ae 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 n the trunk page
168af 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 70 to insert the p
168b0 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 age. ** bei
168b1 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6e 65 ng freed as a ne
168b2 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a w leaf.. **
168b3 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 . ** Note t
168b4 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 hat the trunk pa
168b5 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 ge is not really
168b6 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63 full until it c
168b7 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a ontains. **
168b8 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
168b9 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75 2 entries, not u
168ba 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 sableSize/4 - 8
168bb 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 61 entries as we ha
168bc 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 ve. ** code
168bd 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20 61 d. But due to a
168be 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 6e coding error in
168bf 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
168c0 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 20 ite prior to.
168c1 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74 ** 3.6.0, dat
168c2 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65 65 abases with free
168c3 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 73 list trunk pages
168c4 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68 holding more th
168c5 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 an. ** usab
168c6 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 leSize/4 - 8 ent
168c7 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65 70 ries will be rep
168c8 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70 74 orted as corrupt
168c9 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 20 . In order.
168ca 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e ** to maintain
168cb 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
168cc 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f 6c tibility with ol
168cd 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 der versions of
168ce 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a SQLite,. **
168cf 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e we will contain
168d0 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 65 to restrict the
168d1 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
168d2 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a 65 es to usableSize
168d3 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a 20 /4 - 8. **
168d4 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f 6d for now. At som
168d5 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 e point in the f
168d6 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 72 uture (once ever
168d7 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 65 yone has upgrade
168d8 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 2e d. ** to 3.
168d9 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 65 6.0 or later) we
168da 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 72 should consider
168db 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 64 fixing the cond
168dc 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 20 itional above.
168dd 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 22 ** to read "
168de 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 20 usableSize/4-2"
168df 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 62 instead of "usab
168e0 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 20 leSize/4-8"..
168e1 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d */. rc =
168e2 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
168e3 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
168e4 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
168e5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
168e6 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
168e7 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
168e8 34 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a 20 20 4], nLeaf+1);.
168e9 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
168ea 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b pTrunk->aData[8+
168eb 6e 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67 65 29 nLeaf*4], iPage)
168ec 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
168ed 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 _SECURE_DELETE.
168ee 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
168ef 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
168f0 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
168f1 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
168f2 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 ge);. }.#
168f3 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 63 endif. rc
168f4 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f = btreeSetHasCo
168f5 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65 ntent(pBt, iPage
168f6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
168f7 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 TRACE(("FREE-PA
168f8 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 GE: %d leaf on t
168f9 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c runk page %d\n",
168fa 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 pPage->pgno,pTru
168fb 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 nk->pgno));.
168fc 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
168fd 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a out;. }. }..
168fe 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
168ff 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f flows to this po
16900 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73 int, then it was
16901 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
16902 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68 add the. ** th
16903 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 e page being fre
16904 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67 ed as a leaf pag
16905 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 e of the first t
16906 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 runk in the free
16907 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 -list.. ** Poss
16908 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 ibly because the
16909 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d free-list is em
1690a 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79 pty, or possibly
1690b 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20 because the .
1690c 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 ** first trunk i
1690d 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
1690e 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20 is full. Either
1690f 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65 way, the page be
16910 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77 ing freed. ** w
16911 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e ill become the n
16912 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 ew first trunk p
16913 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d age in the free-
16914 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 list.. */. if(
16915 20 20 20 28 28 21 70 50 61 67 65 29 20 26 26 20 ((!pPage) &&
16916 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 (0 != (rc = sqli
16917 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
16918 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 pBt, iPage, &pPa
16919 67 65 2c 20 30 29 29 29 29 0a 20 20 20 20 20 7c ge, 0)))). |
1691a 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 | (0 != (rc = sq
1691b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1691c 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 pPage->pDbPage))
1691d 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ). ){. goto
1691e 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
1691f 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61 }. put4byte(pPa
16920 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e ge->aData, iTrun
16921 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 k);. put4byte(&
16922 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c pPage->aData[4],
16923 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 0);. put4byte(
16924 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
16925 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52 2], iPage);. TR
16926 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a ACE(("FREE-PAGE:
16927 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61 %d new trunk pa
16928 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c ge replacing %d\
16929 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c n", pPage->pgno,
1692a 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65 iTrunk));..free
1692b 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 page_out:. if(
1692c 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 pPage ){. pPa
1692d 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a ge->isInit = 0;.
1692e 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 }. releasePag
1692f 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 e(pPage);. rele
16930 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b asePage(pTrunk);
16931 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
16932 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 static int freeP
16933 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 age(MemPage *pPa
16934 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 66 72 ge){. return fr
16935 65 65 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 eePage2(pPage->p
16936 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 Bt, pPage, pPage
16937 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ->pgno);.}../*.*
16938 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 * Free any overf
16939 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 low pages associ
1693a 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 ated with the gi
1693b 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 ven Cell..*/.sta
1693c 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c tic int clearCel
1693d 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 l(MemPage *pPage
1693e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 , unsigned char
1693f 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 *pCell){. BtSha
16940 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
16941 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 ->pBt;. CellInf
16942 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f o info;. Pgno o
16943 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 vflPgno;. int r
16944 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a c;. int nOvfl;.
16945 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 u16 ovflPageSi
16946 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ze;.. assert( s
16947 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
16948 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
16949 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
1694a 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
1694b 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
1694c 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 &info);. if( i
1694d 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 nfo.iOverflow==0
1694e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1694f 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f QLITE_OK; /* No
16950 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
16951 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 Return without
16952 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a doing anything *
16953 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f /. }. ovflPgno
16954 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 = get4byte(&pCe
16955 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
16956 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 w]);. assert( p
16957 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e Bt->usableSize >
16958 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 4 );. ovflPage
16959 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Size = pBt->usab
1695a 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f leSize - 4;. nO
1695b 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 vfl = (info.nPay
1695c 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 load - info.nLoc
1695d 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a al + ovflPageSiz
1695e 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 e - 1)/ovflPageS
1695f 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f ize;. assert( o
16960 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f vflPgno==0 || nO
16961 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 vfl>0 );. while
16962 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 ( nOvfl-- ){.
16963 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 3b Pgno iNext = 0;
16964 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f . MemPage *pO
16965 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 vfl = 0;. if(
16966 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c 20 6f ovflPgno<2 || o
16967 76 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50 61 67 vflPgno>pagerPag
16968 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 ecount(pBt) ){.
16969 20 20 20 20 20 2f 2a 20 30 20 69 73 20 6e 6f 74 /* 0 is not
1696a 20 61 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 a legal page nu
1696b 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 31 20 mber and page 1
1696c 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a 20 20 cannot be an .
1696d 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 ** overflow
1696e 70 61 67 65 2e 20 54 68 65 72 65 66 6f 72 65 20 page. Therefore
1696f 69 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 6f 72 if ovflPgno<2 or
16970 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
16971 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 the . ** f
16972 69 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ile the database
16973 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 must be corrupt
16974 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 . */. retur
16975 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
16976 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
16977 20 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a 20 20 if( nOvfl ){.
16978 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 rc = getOver
16979 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76 flowPage(pBt, ov
1697a 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 flPgno, &pOvfl,
1697b 26 69 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 69 &iNext);. i
1697c 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1697d 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 c;. }. rc
1697e 3d 20 66 72 65 65 50 61 67 65 32 28 70 42 74 2c = freePage2(pBt,
1697f 20 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f pOvfl, ovflPgno
16980 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 76 66 6c );. if( pOvfl
16981 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
16982 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 3PagerUnref(pOvf
16983 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 l->pDbPage);.
16984 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 }. if( rc )
16985 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6f return rc;. o
16986 76 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 74 3b vflPgno = iNext;
16987 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
16988 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16989 2a 20 43 72 65 61 74 65 20 74 68 65 20 62 79 74 * Create the byt
1698a 65 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 e sequence used
1698b 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 63 to represent a c
1698c 65 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67 ell on page pPag
1698d 65 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 e.** and write t
1698e 68 61 74 20 62 79 74 65 20 73 65 71 75 65 6e 63 hat byte sequenc
1698f 65 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 e into pCell[].
16990 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 Overflow pages
16991 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 are.** allocated
16992 20 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e 20 61 and filled in a
16993 73 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 s necessary. Th
16994 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 e calling proced
16995 75 72 65 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e ure.** is respon
16996 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 sible for making
16997 20 73 75 72 65 20 73 75 66 66 69 63 69 65 6e 74 sure sufficient
16998 20 73 70 61 63 65 20 68 61 73 20 62 65 65 6e 20 space has been
16999 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 allocated.** for
1699a 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 pCell[]..**.**
1699b 4e 6f 74 65 20 74 68 61 74 20 70 43 65 6c 6c 20 Note that pCell
1699c 64 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 does not necessa
1699d 72 79 20 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74 ry need to point
1699e 20 74 6f 20 74 68 65 20 70 50 61 67 65 2d 3e 61 to the pPage->a
1699f 44 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70 Data.** area. p
169a0 43 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74 Cell might point
169a1 20 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61 to some tempora
169a2 72 79 20 73 74 6f 72 61 67 65 2e 20 20 54 68 65 ry storage. The
169a3 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 cell will.** be
169a4 20 63 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 20 constructed in
169a5 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 61 this temporary a
169a6 72 65 61 20 74 68 65 6e 20 63 6f 70 69 65 64 20 rea then copied
169a7 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 into pPage->aDat
169a8 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 a.** later..*/.s
169a9 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e tatic int fillIn
169aa 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 Cell(. MemPage
169ab 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 *pPage,
169ac 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
169ad 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ge that contains
169ae 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 the cell */. u
169af 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 nsigned char *pC
169b0 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ell, /*
169b1 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f Complete text o
169b2 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 f the cell */.
169b3 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
169b4 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f , i64 nKey, /
169b5 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63 * The key */. c
169b6 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 onst void *pData
169b7 2c 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a ,int nData, /*
169b8 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 The data */. i
169b9 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 nt nZero,
169ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
169bb 20 45 78 74 72 61 20 7a 65 72 6f 20 62 79 74 65 Extra zero byte
169bc 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 70 s to append to p
169bd 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 Data */. int *p
169be 6e 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 nSize
169bf 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
169c0 65 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 e cell size here
169c1 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 */.){. int nPa
169c2 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 yload;. const u
169c3 38 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 8 *pSrc;. int n
169c4 53 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e Src, n, rc;. in
169c5 74 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d t spaceLeft;. M
169c6 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 emPage *pOvfl =
169c7 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 0;. MemPage *pT
169c8 6f 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20 oRelease = 0;.
169c9 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
169ca 50 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65 Prior;. unsigne
169cb 64 20 63 68 61 72 20 2a 70 50 61 79 6c 6f 61 64 d char *pPayload
169cc 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
169cd 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
169ce 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 Pgno pgnoOvfl
169cf 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64 = 0;. int nHead
169d0 65 72 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 er;. CellInfo i
169d1 6e 66 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 nfo;.. assert(
169d2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
169d3 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
169d4 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 70 utex) );.. /* p
169d5 50 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 Page is not nece
169d6 73 73 61 72 69 6c 79 20 77 72 69 74 65 61 62 6c ssarily writeabl
169d7 65 20 73 69 6e 63 65 20 70 43 65 6c 6c 20 6d 69 e since pCell mi
169d8 67 68 74 20 62 65 20 61 75 78 69 6c 69 61 72 79 ght be auxiliary
169d9 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 73 70 61 . ** buffer spa
169da 63 65 20 74 68 61 74 20 69 73 20 73 65 70 61 72 ce that is separ
169db 61 74 65 20 66 72 6f 6d 20 74 68 65 20 70 50 61 ate from the pPa
169dc 67 65 20 62 75 66 66 65 72 20 61 72 65 61 20 2a ge buffer area *
169dd 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c /. assert( pCel
169de 6c 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c l<pPage->aData |
169df 7c 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67 65 2d | pCell>=&pPage-
169e0 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 >aData[pBt->page
169e1 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 Size].
169e2 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 || sqlite3Page
169e3 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
169e4 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
169e5 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 . /* Fill in th
169e6 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e e header. */. n
169e7 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 69 66 Header = 0;. if
169e8 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
169e9 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d {. nHeader +=
169ea 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 4;. }. if( pP
169eb 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a age->hasData ){.
169ec 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 nHeader += p
169ed 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b utVarint(&pCell[
169ee 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 61 2b nHeader], nData+
169ef 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b nZero);. }else{
169f0 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e 5a 65 . nData = nZe
169f1 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 48 ro = 0;. }. nH
169f2 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69 eader += putVari
169f3 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 nt(&pCell[nHeade
169f4 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b 65 79 r], *(u64*)&nKey
169f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
169f6 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
169f7 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
169f8 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e o);. assert( in
169f9 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 fo.nHeader==nHea
169fa 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 der );. assert(
169fb 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 info.nKey==nKey
169fc 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e );. assert( in
169fd 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 fo.nData==(u32)(
169fe 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a nData+nZero) );.
169ff 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 . /* Fill in
16a00 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 the payload */.
16a01 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 nPayload = nDat
16a02 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 a + nZero;. if(
16a03 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
16a04 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 {. pSrc = pDa
16a05 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e ta;. nSrc = n
16a06 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 Data;. nData
16a07 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 = 0;. }else{ .
16a08 20 20 20 69 66 28 20 6e 4b 65 79 3e 30 78 37 66 if( nKey>0x7f
16a09 66 66 66 66 66 66 20 7c 7c 20 70 4b 65 79 3d 3d ffffff || pKey==
16a0a 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
16a0b 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
16a0c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 ;. }. nPay
16a0d 6c 6f 61 64 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 load += (int)nKe
16a0e 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4b y;. pSrc = pK
16a0f 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 28 ey;. nSrc = (
16a10 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 int)nKey;. }.
16a11 2a 70 6e 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e *pnSize = info.n
16a12 53 69 7a 65 3b 0a 20 20 73 70 61 63 65 4c 65 66 Size;. spaceLef
16a13 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b t = info.nLocal;
16a14 0a 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 . pPayload = &p
16a15 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 Cell[nHeader];.
16a16 20 70 50 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c pPrior = &pCell
16a17 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
16a18 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 50 61 79 ;.. while( nPay
16a19 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 20 69 66 load>0 ){. if
16a1a 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30 20 29 ( spaceLeft==0 )
16a1b 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
16a1c 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
16a1d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f . Pgno pgno
16a1e 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f 76 66 Ptrmap = pgnoOvf
16a1f 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 l; /* Overflow p
16a20 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 age pointer-map
16a21 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 entry page */.
16a22 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
16a23 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
16a24 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 do{.
16a25 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 pgnoOvfl++;.
16a26 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 0a 20 } while( .
16a27 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 50 5f PTRMAP_
16a28 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f ISPAGE(pBt, pgno
16a29 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 Ovfl) || pgnoOvf
16a2a 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f l==PENDING_BYTE_
16a2b 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 20 20 PAGE(pBt) .
16a2c 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 );. }.#e
16a2d 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 ndif. rc =
16a2e 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
16a2f 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 e(pBt, &pOvfl, &
16a30 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 pgnoOvfl, pgnoOv
16a31 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 fl, 0);.#ifndef
16a32 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
16a33 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 VACUUM. /*
16a34 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
16a35 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 supports auto-va
16a36 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 73 65 cuum, and the se
16a37 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 75 65 cond or subseque
16a38 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 nt. ** over
16a39 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 65 69 flow page is bei
16a3a 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64 ng allocated, ad
16a3b 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 d an entry to th
16a3c 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 e pointer-map.
16a3d 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 74 20 ** for that
16a3e 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20 20 20 page now. .
16a3f 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 **. ** If
16a40 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 this is the firs
16a41 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c t overflow page,
16a42 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 70 61 then write a pa
16a43 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20 20 20 rtial entry .
16a44 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 6f 69 ** to the poi
16a45 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77 65 20 nter-map. If we
16a46 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20 74 6f write nothing to
16a47 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d 6d 61 this pointer-ma
16a48 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20 2a 2a p slot,. **
16a49 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69 6d 69 then the optimi
16a4a 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20 63 68 stic overflow ch
16a4b 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69 ain processing i
16a4c 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a 20 20 n clearCell().
16a4d 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73 69 6e ** may misin
16a4e 74 65 72 70 72 65 74 20 74 68 65 20 75 6e 69 6e terpret the unin
16a4f 69 74 69 61 6c 69 73 65 64 20 76 61 6c 75 65 73 itialised values
16a50 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 0a and delete the.
16a51 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67 20 70 ** wrong p
16a52 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 64 61 ages from the da
16a53 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f tabase.. */
16a54 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
16a55 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 72 63 autoVacuum && rc
16a56 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
16a57 20 20 20 20 20 20 20 75 38 20 65 54 79 70 65 20 u8 eType
16a58 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f 50 54 = (pgnoPtrmap?PT
16a59 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 RMAP_OVERFLOW2:P
16a5a 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 29 TRMAP_OVERFLOW1)
16a5b 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 ;. rc = p
16a5c 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 trmapPut(pBt, pg
16a5d 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 65 2c 20 70 noOvfl, eType, p
16a5e 67 6e 6f 50 74 72 6d 61 70 29 3b 0a 20 20 20 20 gnoPtrmap);.
16a5f 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
16a60 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
16a61 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 age(pOvfl);.
16a62 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 }. }.#e
16a63 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 72 ndif. if( r
16a64 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c c ){. rel
16a65 65 61 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 easePage(pToRele
16a66 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 ase);. re
16a67 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
16a68 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 54 .. /* If pT
16a69 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 oRelease is not
16a6a 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69 6f 72 zero than pPrior
16a6b 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 65 points into the
16a6c 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 20 data area.
16a6d 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 ** of pToReleas
16a6e 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 e. Make sure pT
16a6f 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c oRelease is stil
16a70 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a l writeable. */.
16a71 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 assert( pT
16a72 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 oRelease==0 || s
16a73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
16a74 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 teable(pToReleas
16a75 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
16a76 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50 72 69 /* If pPri
16a77 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 or is part of th
16a78 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 e data area of p
16a79 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 Page, then make
16a7a 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 20 sure pPage.
16a7b 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 ** is still wri
16a7c 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 teable */.
16a7d 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 3c 70 assert( pPrior<p
16a7e 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 Page->aData || p
16a7f 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d 3e 61 Prior>=&pPage->a
16a80 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 Data[pBt->pageSi
16a81 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze].
16a82 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
16a83 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
16a84 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
16a85 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50 put4byte(pP
16a86 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b rior, pgnoOvfl);
16a87 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
16a88 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a ge(pToRelease);.
16a89 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 73 65 pToRelease
16a8a 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20 = pOvfl;.
16a8b 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e pPrior = pOvfl->
16a8c 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70 75 74 aData;. put
16a8d 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 30 29 4byte(pPrior, 0)
16a8e 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f 61 64 ;. pPayload
16a8f 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61 = &pOvfl->aData
16a90 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61 63 65 [4];. space
16a91 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73 61 62 Left = pBt->usab
16a92 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20 leSize - 4;.
16a93 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f }. n = nPaylo
16a94 61 64 3b 0a 20 20 20 20 69 66 28 20 6e 3e 73 70 ad;. if( n>sp
16a95 61 63 65 4c 65 66 74 20 29 20 6e 20 3d 20 73 70 aceLeft ) n = sp
16a96 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 20 2f 2a aceLeft;.. /*
16a97 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65 20 69 If pToRelease i
16a98 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20 s not zero than
16a99 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e 74 73 20 pPayload points
16a9a 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61 72 into the data ar
16a9b 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f ea. ** of pTo
16a9c 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65 20 73 Release. Make s
16a9d 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65 20 69 ure pToRelease i
16a9e 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
16a9f 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 e. */. assert
16aa0 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 ( pToRelease==0
16aa1 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
16aa2 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65 swriteable(pToRe
16aa3 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20 lease->pDbPage)
16aa4 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 50 );.. /* If pP
16aa5 61 79 6c 6f 61 64 20 69 73 20 70 61 72 74 20 6f ayload is part o
16aa6 66 20 74 68 65 20 64 61 74 61 20 61 72 65 61 20 f the data area
16aa7 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d of pPage, then m
16aa8 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 0a 20 ake sure pPage.
16aa9 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 ** is still w
16aaa 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 riteable */.
16aab 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f 61 64 assert( pPayload
16aac 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c <pPage->aData ||
16aad 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50 61 67 pPayload>=&pPag
16aae 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 e->aData[pBt->pa
16aaf 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 geSize].
16ab0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 || sqlite3Pa
16ab1 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
16ab2 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
16ab3 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3e ;.. if( nSrc>
16ab4 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 0 ){. if( n
16ab5 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 72 63 >nSrc ) n = nSrc
16ab6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
16ab7 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 6d 65 pSrc );. me
16ab8 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 mcpy(pPayload, p
16ab9 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c Src, n);. }el
16aba 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 se{. memset
16abb 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 (pPayload, 0, n)
16abc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 ;. }. nPay
16abd 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 load -= n;. p
16abe 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 Payload += n;.
16abf 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 pSrc += n;.
16ac0 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 nSrc -= n;.
16ac1 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a spaceLeft -= n;.
16ac2 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30 20 if( nSrc==0
16ac3 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 3d 20 ){. nSrc =
16ac4 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 53 72 nData;. pSr
16ac5 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 7d c = pData;. }
16ac6 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 . }. releasePa
16ac7 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a ge(pToRelease);.
16ac8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16ac9 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d OK;.}../*.** Rem
16aca 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 65 6c ove the i-th cel
16acb 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 20 54 l from pPage. T
16acc 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 66 65 his routine effe
16acd 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 2e 0a cts pPage only..
16ace 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f 6e 74 ** The cell cont
16acf 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 65 64 ent is not freed
16ad0 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e or deallocated.
16ad1 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 It is assumed
16ad2 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 6c 6c that.** the cell
16ad3 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 65 65 content has bee
16ad4 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 6c 61 n copied somepla
16ad5 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 20 72 ce else. This r
16ad6 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 outine just.** r
16ad7 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 65 72 emoves the refer
16ad8 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 6c 6c ence to the cell
16ad9 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a from pPage..**.
16ada 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 65 20 ** "sz" must be
16adb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
16adc 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 2e tes in the cell.
16add 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
16ade 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 ropCell(MemPage
16adf 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 78 2c *pPage, int idx,
16ae0 20 69 6e 74 20 73 7a 29 7b 0a 20 20 69 6e 74 20 int sz){. int
16ae1 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c i; /* L
16ae2 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
16ae3 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 int pc;
16ae4 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 65 /* Offset to ce
16ae5 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 63 65 ll content of ce
16ae6 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 ll being deleted
16ae7 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 */. u8 *data;
16ae8 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e /* pPage->
16ae9 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a 70 aData */. u8 *p
16aea 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 tr; /* Us
16aeb 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 65 73 ed to move bytes
16aec 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e 20 64 around within d
16aed 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 ata[] */. int r
16aee 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 c; /* Th
16aef 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f e return code */
16af0 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e .. assert( idx>
16af1 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67 65 2d =0 && idx<pPage-
16af2 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 >nCell );. asse
16af3 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 rt( sz==cellSize
16af4 28 70 50 61 67 65 2c 20 69 64 78 29 20 29 3b 0a (pPage, idx) );.
16af5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16af6 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
16af7 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
16af8 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
16af9 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
16afa 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
16afb 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61 20 3d tex) );. data =
16afc 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
16afd 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50 61 ptr = &data[pPa
16afe 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b ge->cellOffset +
16aff 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d 20 2*idx];. pc =
16b00 67 65 74 32 62 79 74 65 28 70 74 72 29 3b 0a 20 get2byte(ptr);.
16b01 20 69 66 28 20 28 70 63 3c 70 50 61 67 65 2d 3e if( (pc<pPage->
16b02 68 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 hdrOffset+6+(pPa
16b03 67 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 29 0a 20 ge->leaf?0:4)).
16b04 20 20 20 20 7c 7c 20 28 70 63 2b 73 7a 3e 70 50 || (pc+sz>pP
16b05 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
16b06 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 72 65 74 Size) ){. ret
16b07 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
16b08 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 PT_BKPT;. }. r
16b09 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 50 c = freeSpace(pP
16b0a 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 20 age, pc, sz);.
16b0b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16b0c 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
16b0d 72 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d rc;. }. for(i=
16b0e 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d 3e idx+1; i<pPage->
16b0f 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72 2b nCell; i++, ptr+
16b10 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d 20 =2){. ptr[0]
16b11 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70 74 = ptr[2];. pt
16b12 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a 20 r[1] = ptr[3];.
16b13 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c }. pPage->nCel
16b14 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65 28 l--;. put2byte(
16b15 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 &data[pPage->hdr
16b16 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67 65 Offset+3], pPage
16b17 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67 ->nCell);. pPag
16b18 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 20 e->nFree += 2;.
16b19 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
16b1a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 K;.}../*.** Inse
16b1b 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e rt a new cell on
16b1c 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20 69 pPage at cell i
16b1d 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c 6c ndex "i". pCell
16b1e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a points to the.*
16b1f 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 * content of the
16b20 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 cell..**.** If
16b21 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
16b22 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 will fit on the
16b23 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74 20 page, then put
16b24 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69 74 it there. If it
16b25 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 .** will not fit
16b26 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f , then make a co
16b27 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 py of the cell c
16b28 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d ontent into pTem
16b29 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69 73 p if.** pTemp is
16b2a 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61 not null. Rega
16b2b 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70 2c rdless of pTemp,
16b2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
16b2d 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61 67 entry.** in pPag
16b2e 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d e->aOvfl[] and m
16b2f 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f 20 ake it point to
16b30 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
16b31 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70 (either.** in p
16b32 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69 67 Temp or the orig
16b33 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20 inal pCell) and
16b34 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73 20 also record its
16b35 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 index. .** Alloc
16b36 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 ating a new entr
16b37 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c y in pPage->aCel
16b38 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61 74 l[] implies that
16b39 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65 .** pPage->nOve
16b3a 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d 65 rflow is increme
16b3b 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e nted..**.** If n
16b3c 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f Skip is non-zero
16b3d 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f , then do not co
16b3e 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b py the first nSk
16b3f 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 0a ip bytes of the.
16b40 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c ** cell. The cal
16b41 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 ler will overwri
16b42 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74 68 te them after th
16b43 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
16b44 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 rns. If.** nSkip
16b45 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
16b46 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 en pCell may not
16b47 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76 point to an inv
16b48 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 alid memory loca
16b49 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43 tion .** (but pC
16b4a 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77 ell+nSkip is alw
16b4b 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 ays valid)..*/.s
16b4c 74 61 74 69 63 20 69 6e 74 20 69 6e 73 65 72 74 tatic int insert
16b4d 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 Cell(. MemPage
16b4e 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 *pPage, /* Pag
16b4f 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 e into which we
16b50 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 are copying */.
16b51 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 int i,
16b52 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 /* New cell b
16b53 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 68 20 ecomes the i-th
16b54 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 cell of the page
16b55 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c */. u8 *pCell,
16b56 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 /* Conte
16b57 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 63 65 nt of the new ce
16b58 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 ll */. int sz,
16b59 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
16b5a 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e es of content in
16b5b 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a pCell */. u8 *
16b5c 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a pTemp, /*
16b5d 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73 70 Temp storage sp
16b5e 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 ace for pCell, i
16b5f 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 75 38 f needed */. u8
16b60 20 6e 53 6b 69 70 20 20 20 20 20 20 20 20 20 20 nSkip
16b61 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 /* Do not write
16b62 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 the first nSkip
16b63 62 79 74 65 73 20 6f 66 20 74 68 65 20 63 65 6c bytes of the cel
16b64 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64 l */.){. int id
16b65 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 x; /* W
16b66 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e 65 here to write ne
16b67 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 w cell content i
16b68 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e n data[] */. in
16b69 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 t j;
16b6a 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
16b6b 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 */. int top;
16b6c 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
16b6d 62 79 74 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 byte of content
16b6e 66 6f 72 20 61 6e 79 20 63 65 6c 6c 20 69 6e 20 for any cell in
16b6f 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 data[] */. int
16b70 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a end; /*
16b71 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74 First byte past
16b72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 the last cell p
16b73 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d ointer in data[]
16b74 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 */. int ins;
16b75 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
16b76 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65 in data[] where
16b77 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 new cell pointe
16b78 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f r is inserted */
16b79 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 . int hdr;
16b7a 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
16b7b 6e 74 6f 20 64 61 74 61 5b 5d 20 6f 66 20 74 68 nto data[] of th
16b7c 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f e page header */
16b7d 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 . int cellOffse
16b7e 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 t; /* Address
16b7f 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f of first cell po
16b80 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 inter in data[]
16b81 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 */. u8 *data;
16b82 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
16b83 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f ntent of the who
16b84 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 le page */. u8
16b85 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f *ptr; /
16b86 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e * Used for movin
16b87 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 g information ar
16b88 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a ound in data[] *
16b89 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d /.. assert( i>=
16b8a 30 20 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 0 && i<=pPage->n
16b8b 43 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 Cell+pPage->nOve
16b8c 72 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 65 72 rflow );. asser
16b8d 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c t( pPage->nCell<
16b8e 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e =MX_CELL(pPage->
16b8f 70 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 pBt) && MX_CELL(
16b90 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 pPage->pBt)<=546
16b91 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
16b92 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3c Page->nOverflow<
16b93 3d 41 72 72 61 79 53 69 7a 65 28 70 50 61 67 65 =ArraySize(pPage
16b94 2d 3e 61 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73 ->aOvfl) );. as
16b95 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 sert( sz==cellSi
16b96 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 zePtr(pPage, pCe
16b97 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ll) );. assert(
16b98 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16b99 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
16b9a 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
16b9b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
16b9c 20 7c 7c 20 73 7a 2b 32 3e 70 50 61 67 65 2d 3e || sz+2>pPage->
16b9d 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 69 66 28 nFree ){. if(
16b9e 20 70 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 pTemp ){.
16b9f 6d 65 6d 63 70 79 28 70 54 65 6d 70 2b 6e 53 6b memcpy(pTemp+nSk
16ba0 69 70 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c ip, pCell+nSkip,
16ba1 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 sz-nSkip);.
16ba2 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b pCell = pTemp;
16ba3 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 . }. j = p
16ba4 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b Page->nOverflow+
16ba5 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a +;. assert( j
16ba6 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 <(int)(sizeof(pP
16ba7 61 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 age->aOvfl)/size
16ba8 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b of(pPage->aOvfl[
16ba9 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 0])) );. pPag
16baa 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c e->aOvfl[j].pCel
16bab 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 l = pCell;. p
16bac 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 Page->aOvfl[j].i
16bad 64 78 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 20 dx = (u16)i;.
16bae 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 pPage->nFree =
16baf 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
16bb0 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
16bb1 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
16bb2 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
16bb3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16bb4 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
16bb5 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
16bb6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
16bb7 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
16bb8 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
16bb9 29 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 );. data = pP
16bba 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 age->aData;.
16bbb 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
16bbc 4f 66 66 73 65 74 3b 0a 20 20 20 20 74 6f 70 20 Offset;. top
16bbd 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
16bbe 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 63 65 [hdr+5]);. ce
16bbf 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 llOffset = pPage
16bc0 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 ->cellOffset;.
16bc1 20 20 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73 end = cellOffs
16bc2 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 et + 2*pPage->nC
16bc3 65 6c 6c 20 2b 20 32 3b 0a 20 20 20 20 69 6e 73 ell + 2;. ins
16bc4 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 = cellOffset +
16bc5 32 2a 69 3b 0a 20 20 20 20 69 66 28 20 65 6e 64 2*i;. if( end
16bc6 20 3e 20 74 6f 70 20 2d 20 73 7a 20 29 7b 0a 20 > top - sz ){.
16bc7 20 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 rc = defrag
16bc8 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b mentPage(pPage);
16bc9 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
16bca 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16bcb 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
16bcc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 6f 70 }. top
16bcd 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
16bce 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 20 a[hdr+5]);.
16bcf 20 61 73 73 65 72 74 28 20 65 6e 64 20 2b 20 73 assert( end + s
16bd0 7a 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 20 20 20 z <= top );.
16bd1 7d 0a 20 20 20 20 69 64 78 20 3d 20 61 6c 6c 6f }. idx = allo
16bd2 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65 2c cateSpace(pPage,
16bd3 20 73 7a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 sz);. assert
16bd4 28 20 69 64 78 3e 30 20 29 3b 0a 20 20 20 20 61 ( idx>0 );. a
16bd5 73 73 65 72 74 28 20 65 6e 64 20 3c 3d 20 67 65 ssert( end <= ge
16bd6 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
16bd7 2b 35 5d 29 20 29 3b 0a 20 20 20 20 69 66 20 28 +5]) );. if (
16bd8 69 64 78 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e idx+sz > pPage->
16bd9 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 pBt->usableSize)
16bda 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 {. return
16bdb 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16bdc 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 KPT;. }. p
16bdd 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 Page->nCell++;.
16bde 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 pPage->nFree
16bdf 2d 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 63 70 79 -= 2;. memcpy
16be0 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 (&data[idx+nSkip
16be1 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 ], pCell+nSkip,
16be2 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 66 sz-nSkip);. f
16be3 6f 72 28 6a 3d 65 6e 64 2d 32 2c 20 70 74 72 3d or(j=end-2, ptr=
16be4 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b &data[j]; j>ins;
16be5 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29 7b 0a j-=2, ptr-=2){.
16be6 20 20 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 ptr[0] = p
16be7 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74 tr[-2];. pt
16be8 72 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d 3b 0a r[1] = ptr[-1];.
16be9 20 20 20 20 7d 0a 20 20 20 20 70 75 74 32 62 79 }. put2by
16bea 74 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20 69 te(&data[ins], i
16beb 64 78 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 dx);. put2byt
16bec 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 e(&data[hdr+3],
16bed 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 23 pPage->nCell);.#
16bee 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16bef 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
16bf0 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74 if( pPage->pBt
16bf1 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
16bf2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c /* The cel
16bf3 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20 l may contain a
16bf4 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 76 pointer to an ov
16bf5 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 erflow page. If
16bf6 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20 20 so, write.
16bf7 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 ** the entry for
16bf8 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 the overflow pa
16bf9 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e ge into the poin
16bfa 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20 2a ter map.. *
16bfb 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f /. CellInfo
16bfc 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71 6c info;. sql
16bfd 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
16bfe 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 llPtr(pPage, pCe
16bff 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 ll, &info);.
16c00 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e assert( (info.
16c01 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e nData+(pPage->in
16c02 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 tKey?0:info.nKey
16c03 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 ))==info.nPayloa
16c04 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 d );. if( i
16c05 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b nfo.iOverflow ){
16c06 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70 67 . Pgno pg
16c07 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 noOvfl = get4byt
16c08 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f e(&pCell[info.iO
16c09 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 verflow]);.
16c0a 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
16c0b 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 67 t(pPage->pBt, pg
16c0c 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f noOvfl, PTRMAP_O
16c0d 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d VERFLOW1, pPage-
16c0e 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
16c0f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16c10 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
16c11 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e }. }.#en
16c12 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 dif. }.. retur
16c13 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
16c14 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 /*.** Add a list
16c15 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 of cells to a p
16c16 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 73 age. The page s
16c17 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c hould be initial
16c18 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 ly empty..** The
16c19 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72 61 cells are guara
16c1a 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 nteed to fit on
16c1b 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 the page..*/.sta
16c1c 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 6c tic void assembl
16c1d 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 ePage(. MemPage
16c1e 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 *pPage, /* Th
16c1f 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 73 73 e page to be ass
16c20 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 emblied */. int
16c21 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f nCell, /
16c22 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * The number of
16c23 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 cells to add to
16c24 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 this page */. u
16c25 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 8 **apCell,
16c26 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 /* Pointers to
16c27 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 cell bodies */.
16c28 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20 20 u16 *aSize
16c29 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 /* Sizes of t
16c2a 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 he cells */.){.
16c2b 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
16c2c 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
16c2d 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c er */. u8 *pCel
16c2e 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41 64 64 lptr; /* Add
16c2f 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c ress of next cel
16c30 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 l pointer */. i
16c31 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20 nt cellbody;
16c32 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e /* Address of n
16c33 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f ext cell body */
16c34 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 . u8 * const da
16c35 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
16c36 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f a; /
16c37 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 * Pointer to dat
16c38 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f 0a 20 a for pPage */.
16c39 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d const int hdr =
16c3a 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
16c3b 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
16c3c 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 65 72 Offset of header
16c3d 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 63 on pPage */. c
16c3e 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62 6c 65 onst int nUsable
16c3f 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 = pPage->pBt->u
16c40 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73 sableSize; /* Us
16c41 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70 61 67 able size of pag
16c42 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
16c43 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
16c44 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
16c45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16c46 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
16c47 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
16c48 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26 26 20 rt( nCell>=0 &&
16c49 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 nCell<=MX_CELL(p
16c4a 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 Page->pBt) && MX
16c4b 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 _CELL(pPage->pBt
16c4c 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73 )<=5460 );. ass
16c4d 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
16c4e 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
16c4f 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
16c50 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 . /* Check that
16c51 20 74 68 65 20 70 61 67 65 20 68 61 73 20 6a 75 the page has ju
16c52 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64 20 62 st been zeroed b
16c53 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a 2f 0a y zeroPage() */.
16c54 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
16c55 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61 >nCell==0 );. a
16c56 73 73 65 72 74 28 20 67 65 74 32 62 79 74 65 28 ssert( get2byte(
16c57 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d 3d 6e &data[hdr+5])==n
16c58 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70 43 65 Usable );.. pCe
16c59 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50 llptr = &data[pP
16c5a 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
16c5b 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 63 65 + nCell*2];. ce
16c5c 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62 6c 65 llbody = nUsable
16c5d 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d ;. for(i=nCell-
16c5e 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 1; i>=0; i--){.
16c5f 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d 20 32 pCellptr -= 2
16c60 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79 20 2d ;. cellbody -
16c61 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 20 20 = aSize[i];.
16c62 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c 70 74 put2byte(pCellpt
16c63 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 r, cellbody);.
16c64 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 memcpy(&data[c
16c65 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65 6c 6c ellbody], apCell
16c66 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29 3b 0a [i], aSize[i]);.
16c67 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65 28 26 }. put2byte(&
16c68 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e 43 65 data[hdr+3], nCe
16c69 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 ll);. put2byte(
16c6a 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 65 &data[hdr+5], ce
16c6b 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 67 65 llbody);. pPage
16c6c 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43 65 6c ->nFree -= (nCel
16c6d 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20 2d 20 l*2 + nUsable -
16c6e 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 cellbody);. pPa
16c6f 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75 31 36 ge->nCell = (u16
16c70 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a )nCell;.}../*.**
16c71 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 The following p
16c72 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 72 6d arameters determ
16c73 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a ine how many adj
16c74 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 74 20 acent pages get
16c75 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61 involved.** in a
16c76 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 balancing opera
16c77 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65 tion. NN is the
16c78 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 number of neigh
16c79 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 bors on either s
16c7a 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 ide.** of the pa
16c7b 67 65 20 74 68 61 74 20 70 61 72 74 69 63 69 70 ge that particip
16c7c 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e ate in the balan
16c7d 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 cing operation.
16c7e 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f NB is the.** to
16c7f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
16c80 67 65 73 20 74 68 61 74 20 70 61 72 74 69 63 69 ges that partici
16c81 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 pate, including
16c82 74 68 65 20 74 61 72 67 65 74 20 70 61 67 65 20 the target page
16c83 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62 and.** NN neighb
16c84 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 ors on either si
16c85 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 de..**.** The mi
16c86 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e nimum value of N
16c87 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 72 73 N is 1 (of cours
16c88 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e 67 20 e). Increasing
16c89 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74 NN above 1.** (t
16c8a 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 73 20 o 2 or 3) gives
16c8b 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f 76 65 a modest improve
16c8c 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 20 61 ment in SELECT a
16c8d 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 6f 72 nd DELETE perfor
16c8e 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68 mance.** in exch
16c8f 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 67 65 ange for a large
16c90 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 69 6e r degradation in
16c91 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41 INSERT and UPDA
16c92 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a TE performance..
16c93 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 ** The value of
16c94 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 69 NN appears to gi
16c95 76 65 20 74 68 65 20 62 65 73 74 20 72 65 73 75 ve the best resu
16c96 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a lts overall..*/.
16c97 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20 20 #define NN 1
16c98 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
16c99 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 er of neighbors
16c9a 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f on either side o
16c9b 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 66 69 f pPage */.#defi
16c9c 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20 ne NB (NN*2+1)
16c9d 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67 /* Total pag
16c9e 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 es involved in t
16c9f 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 2f he balance */../
16ca0 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 * Forward refere
16ca1 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e nce */.static in
16ca2 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73 t balance(BtCurs
16ca3 6f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 6e or*, int);..#ifn
16ca4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16ca5 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a QUICKBALANCE./*.
16ca6 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
16ca7 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 6e of balance() han
16ca8 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 dles the common
16ca9 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 special case whe
16caa 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 re.** a new entr
16cab 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 y is being inser
16cac 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 65 ted on the extre
16cad 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 me right-end of
16cae 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 the.** tree, in
16caf 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 65 other words, whe
16cb0 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 n the new entry
16cb1 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 will become the
16cb2 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 largest.** entry
16cb3 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a in the tree..**
16cb4 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74 .** Instead of t
16cb5 72 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 74 68 rying balance th
16cb6 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c e 3 right-most l
16cb7 65 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20 eaf pages, just
16cb8 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 add.** a new pag
16cb9 65 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68 e to the right-h
16cba 61 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74 and side and put
16cbb 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 the one new ent
16cbc 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 ry in.** that pa
16cbd 67 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 ge. This leaves
16cbe 20 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20 the right side
16cbf 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65 of the tree some
16cc0 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 what.** unbalanc
16cc1 65 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72 ed. But odds ar
16cc2 65 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 e that we will b
16cc3 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 e inserting new
16cc4 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 entries.** at th
16cc5 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 e end soon after
16cc6 77 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61 wards so the nea
16cc7 72 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77 rly empty page w
16cc8 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 ill quickly.** f
16cc9 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 ill up. On aver
16cca 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 age..**.** pPage
16ccb 20 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67 is the leaf pag
16ccc 65 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 e which is the r
16ccd 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 ight-most page i
16cce 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 n the tree..** p
16ccf 50 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61 Parent is its pa
16cd0 72 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73 rent. pPage mus
16cd1 74 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 t have a single
16cd2 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a overflow entry.*
16cd3 2a 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20 * which is also
16cd4 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 the right-most e
16cd5 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65 ntry on the page
16cd6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
16cd7 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 42 74 balance_quick(Bt
16cd8 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
16cd9 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 int rc;. MemPa
16cda 67 65 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 ge *pNew = 0;.
16cdb 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 0a 20 20 Pgno pgnoNew;.
16cdc 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 75 31 36 u8 *pCell;. u16
16cdd 20 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 6c 6c 49 szCell;. CellI
16cde 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d 65 6d 50 nfo info;. MemP
16cdf 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 age *pPage = pCu
16ce0 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16ce1 69 50 61 67 65 5d 3b 0a 20 20 4d 65 6d 50 61 67 iPage];. MemPag
16ce2 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 43 75 e *pParent = pCu
16ce3 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16ce4 69 50 61 67 65 2d 31 5d 3b 0a 20 20 42 74 53 68 iPage-1];. BtSh
16ce5 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 ared *pBt = pPag
16ce6 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 70 61 e->pBt;. int pa
16ce7 72 65 6e 74 49 64 78 20 3d 20 70 50 61 72 65 6e rentIdx = pParen
16ce8 74 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f 2a 20 70 t->nCell; /* p
16ce9 50 61 72 65 6e 74 20 6e 65 77 20 64 69 76 69 64 Parent new divid
16cea 65 72 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f er cell index */
16ceb 0a 20 20 69 6e 74 20 70 61 72 65 6e 74 53 69 7a . int parentSiz
16cec 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
16ced 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
16cee 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c new divider cell
16cef 20 2a 2f 0a 20 20 75 38 20 70 61 72 65 6e 74 43 */. u8 parentC
16cf0 65 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 20 20 20 ell[64];
16cf1 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
16cf2 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 69 76 for the new div
16cf3 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 ider cell */..
16cf4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16cf5 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
16cf6 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
16cf7 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
16cf8 61 20 6e 65 77 20 70 61 67 65 2e 20 49 6e 73 65 a new page. Inse
16cf9 72 74 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 rt the overflow
16cfa 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 0a cell from pPage.
16cfb 20 20 2a 2a 20 69 6e 74 6f 20 69 74 2e 20 54 68 ** into it. Th
16cfc 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 6f 76 en remove the ov
16cfd 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d erflow cell from
16cfe 20 70 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 pPage.. */. r
16cff 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
16d00 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 ePage(pBt, &pNew
16d01 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 , &pgnoNew, 0, 0
16d02 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
16d03 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 ITE_OK ){. pC
16d04 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 ell = pPage->aOv
16d05 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 fl[0].pCell;.
16d06 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 szCell = cellSi
16d07 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 zePtr(pPage, pCe
16d08 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ll);. assert(
16d09 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
16d0a 72 69 74 65 61 62 6c 65 28 70 4e 65 77 2d 3e 70 riteable(pNew->p
16d0b 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 7a DbPage) );. z
16d0c 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 50 eroPage(pNew, pP
16d0d 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a age->aData[0]);.
16d0e 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 assemblePage
16d0f 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65 6c 6c (pNew, 1, &pCell
16d10 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 , &szCell);.
16d11 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
16d12 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 = 0;. . /*
16d13 70 50 61 67 65 20 69 73 20 63 75 72 72 65 6e 74 pPage is current
16d14 6c 79 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 ly the right-chi
16d15 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 2e 20 43 ld of pParent. C
16d16 68 61 6e 67 65 20 74 68 69 73 0a 20 20 20 20 2a hange this. *
16d17 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 69 * so that the ri
16d18 67 68 74 2d 63 68 69 6c 64 20 69 73 20 74 68 65 ght-child is the
16d19 20 6e 65 77 20 70 61 67 65 20 61 6c 6c 6f 63 61 new page alloca
16d1a 74 65 64 20 61 62 6f 76 65 20 61 6e 64 0a 20 20 ted above and.
16d1b 20 20 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 ** pPage is th
16d1c 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67 68 74 20 e next-to-right
16d1d 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a 2a 0a 20 child. . **.
16d1e 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20 74 68 65 ** Ignore the
16d1f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
16d20 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 66 69 6c the call to fil
16d21 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69 6c 6c 49 lInCell(). fillI
16d22 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a 2a 20 6d nCell(). ** m
16d23 61 79 20 6f 6e 6c 79 20 72 65 74 75 72 6e 20 6f ay only return o
16d24 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 ther than SQLITE
16d25 5f 4f 4b 20 69 66 20 69 74 20 69 73 20 72 65 71 _OK if it is req
16d26 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 uired to allocat
16d27 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 e. ** one or
16d28 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
16d29 67 65 73 2e 20 53 69 6e 63 65 20 61 6e 20 69 6e ges. Since an in
16d2a 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 42 2d 54 ternal table B-T
16d2b 72 65 65 20 63 65 6c 6c 20 0a 20 20 20 20 2a 2a ree cell . **
16d2c 20 6d 61 79 20 6e 65 76 65 72 20 73 70 69 6c 6c may never spill
16d2d 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e 20 6f 76 over onto an ov
16d2e 65 72 66 6c 6f 77 20 70 61 67 65 20 28 69 74 20 erflow page (it
16d2f 69 73 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 is a maximum of
16d30 0a 20 20 20 20 2a 2a 20 31 33 20 62 79 74 65 73 . ** 13 bytes
16d31 20 69 6e 20 73 69 7a 65 29 2c 20 69 74 20 69 73 in size), it is
16d32 20 6e 6f 74 20 6e 65 63 63 65 73 73 61 72 79 20 not neccessary
16d33 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 to check the ret
16d34 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2a urn code.. **
16d35 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c . ** Similarl
16d36 79 2c 20 74 68 65 20 69 6e 73 65 72 74 43 65 6c y, the insertCel
16d37 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e l() function can
16d38 6e 6f 74 20 66 61 69 6c 20 69 66 20 74 68 65 20 not fail if the
16d39 70 61 67 65 0a 20 20 20 20 2a 2a 20 62 65 69 6e page. ** bein
16d3a 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 g inserted into
16d3b 69 73 20 61 6c 72 65 61 64 79 20 77 72 69 74 61 is already writa
16d3c 62 6c 65 20 61 6e 64 20 74 68 65 20 63 65 6c 6c ble and the cell
16d3d 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20 20 20 2a does not . *
16d3e 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 6f 76 65 * contain an ove
16d3f 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72 2e 20 53 rflow pointer. S
16d40 6f 20 69 67 6e 6f 72 65 20 74 68 69 73 20 72 65 o ignore this re
16d41 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f 2e 0a 20 turn code too..
16d42 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
16d43 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 ( pPage->nCell>0
16d44 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 );. pCell =
16d45 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
16d46 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b pPage->nCell-1);
16d47 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
16d48 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
16d49 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
16d4a 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49 6e 43 65 o);. fillInCe
16d4b 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72 65 ll(pParent, pare
16d4c 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e ntCell, 0, info.
16d4d 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 nKey, 0, 0, 0, &
16d4e 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 parentSize);.
16d4f 20 61 73 73 65 72 74 28 20 70 61 72 65 6e 74 53 assert( parentS
16d50 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20 20 61 73 ize<64 );. as
16d51 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
16d52 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
16d53 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 arent->pDbPage)
16d54 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43 65 6c );. insertCel
16d55 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72 65 6e l(pParent, paren
16d56 74 49 64 78 2c 20 70 61 72 65 6e 74 43 65 6c 6c tIdx, parentCell
16d57 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c 20 30 2c , parentSize, 0,
16d58 20 34 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74 4);. put4byt
16d59 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 e(findOverflowCe
16d5a 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 72 65 6e ll(pParent,paren
16d5b 74 49 64 78 29 2c 20 70 50 61 67 65 2d 3e 70 67 tIdx), pPage->pg
16d5c 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74 no);. put4byt
16d5d 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 e(&pParent->aDat
16d5e 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 a[pParent->hdrOf
16d5f 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 fset+8], pgnoNew
16d60 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 );. . /* If
16d61 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d this is an auto-
16d62 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c vacuum database,
16d63 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e update the poin
16d64 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 ter map. ** w
16d65 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 ith entries for
16d66 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e the new page, an
16d67 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 d any pointer fr
16d68 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 om the . ** c
16d69 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 ell on the page
16d6a 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 to an overflow p
16d6b 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 age.. */.
16d6c 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
16d6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 ){. rc = p
16d6e 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 trmapPut(pBt, pg
16d6f 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42 54 noNew, PTRMAP_BT
16d70 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 REE, pParent->pg
16d71 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 no);. if( r
16d72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16d73 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 rc = ptr
16d74 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c mapPutOvfl(pNew,
16d75 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
16d76 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 }.. /* Relea
16d77 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 se the reference
16d78 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 to the new page
16d79 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61 73 65 . */. release
16d7a 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a Page(pNew);. }.
16d7b 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f . /* At this po
16d7c 69 6e 74 20 74 68 65 20 70 50 61 67 65 2d 3e 6e int the pPage->n
16d7d 46 72 65 65 20 76 61 72 69 61 62 6c 65 20 69 73 Free variable is
16d7e 20 6e 6f 74 20 73 65 74 20 63 6f 72 72 65 63 74 not set correct
16d7f 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 72 65 73 ly with. ** res
16d80 70 65 63 74 20 74 6f 20 74 68 65 20 63 6f 6e 74 pect to the cont
16d81 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 ent of the page
16d82 28 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20 (because it was
16d83 73 65 74 20 74 6f 20 30 20 62 79 20 0a 20 20 2a set to 0 by . *
16d84 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29 2e 20 53 * insertCell). S
16d85 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 42 74 o call sqlite3Bt
16d86 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 74 6f reeInitPage() to
16d87 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 make sure it is
16d88 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 2e 0a 20 . ** correct..
16d89 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 68 61 **. ** This ha
16d8a 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 65 76 65 s to be done eve
16d8b 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20 77 69 n if an error wi
16d8c 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 ll be returned.
16d8d 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a 20 20 2a Normally, if. *
16d8e 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 * an error occur
16d8f 73 20 64 75 72 69 6e 67 20 74 72 65 65 20 62 61 s during tree ba
16d90 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20 63 6f 6e lancing, the con
16d91 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50 61 67 65 tents of MemPage
16d92 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74 20 69 6d are. ** not im
16d93 70 6f 72 74 61 6e 74 2c 20 61 73 20 74 68 65 79 portant, as they
16d94 20 77 69 6c 6c 20 62 65 20 72 65 63 61 6c 63 75 will be recalcu
16d95 6c 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 lated when the p
16d96 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 0a 20 20 age is rolled.
16d97 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20 68 65 72 ** back. But her
16d98 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65 5f 71 75 e, in balance_qu
16d99 69 63 6b 28 29 2c 20 69 74 20 69 73 20 70 6f 73 ick(), it is pos
16d9a 73 69 62 6c 65 20 74 68 61 74 20 70 50 61 67 65 sible that pPage
16d9b 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f 74 20 79 has . ** not y
16d9c 65 74 20 62 65 65 6e 20 6d 61 72 6b 65 64 20 64 et been marked d
16d9d 69 72 74 79 20 6f 72 20 77 72 69 74 74 65 6e 20 irty or written
16d9e 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
16d9f 20 66 69 6c 65 2e 20 54 68 65 72 65 66 6f 72 65 file. Therefore
16da0 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c 20 6e 6f . ** it will no
16da1 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b t be rolled back
16da2 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20 69 6d and so it is im
16da3 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61 6b 65 20 portant to make
16da4 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a 20 74 sure that. ** t
16da5 68 65 20 70 61 67 65 20 64 61 74 61 20 61 6e 64 he page data and
16da6 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d contents of Mem
16da7 50 61 67 65 20 61 72 65 20 63 6f 6e 73 69 73 74 Page are consist
16da8 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 ent.. */. pPag
16da9 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 e->isInit = 0;.
16daa 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 sqlite3BtreeIni
16dab 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 tPage(pPage);.
16dac 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
16dad 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a Overflow==0 );..
16dae 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68 69 /* If everythi
16daf 6e 67 20 65 6c 73 65 20 73 75 63 63 65 65 64 65 ng else succeede
16db0 64 2c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70 d, balance the p
16db1 61 72 65 6e 74 20 70 61 67 65 2c 20 69 6e 20 0a arent page, in .
16db2 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 64 69 ** case the di
16db3 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 73 65 72 vider cell inser
16db4 74 65 64 20 63 61 75 73 65 64 20 69 74 20 74 6f ted caused it to
16db5 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c become overfull
16db6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d .. */. if( rc=
16db7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16db8 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
16db9 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e age);. pCur->
16dba 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 72 63 20 iPage--;. rc
16dbb 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 = balance(pCur,
16dbc 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0);. }. return
16dbd 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a rc;.}.#endif /*
16dbe 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 SQLITE_OMIT_QUI
16dbf 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 2f 2a CKBALANCE */../*
16dc0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16dc1 20 72 65 64 69 73 74 72 69 62 75 74 65 73 20 43 redistributes C
16dc2 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 20 61 6e ells on pPage an
16dc3 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 73 69 62 d up to NN*2 sib
16dc4 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 50 61 67 lings.** of pPag
16dc5 65 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 61 e so that all pa
16dc6 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 74 ges have about t
16dc7 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f he same amount o
16dc8 66 20 66 72 65 65 20 73 70 61 63 65 2e 0a 2a 2a f free space..**
16dc9 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 69 62 6c Usually NN sibl
16dca 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 20 73 ings on either s
16dcb 69 64 65 20 6f 66 20 70 50 61 67 65 20 69 73 20 ide of pPage is
16dcc 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 used in the bala
16dcd 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f 75 67 68 ncing,.** though
16dce 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 6d more siblings m
16dcf 69 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20 6f ight come from o
16dd0 6e 65 20 73 69 64 65 20 69 66 20 70 50 61 67 65 ne side if pPage
16dd1 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a is the first.**
16dd2 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f or last child o
16dd3 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 49 f its parent. I
16dd4 66 20 70 50 61 67 65 20 68 61 73 20 66 65 77 65 f pPage has fewe
16dd5 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 69 62 6c r than 2*NN sibl
16dd6 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 74 68 69 ings.** (somethi
16dd7 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c ng which can onl
16dd8 79 20 68 61 70 70 65 6e 20 69 66 20 70 50 61 67 y happen if pPag
16dd9 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 e is the root pa
16dda 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 68 69 6c ge or a .** chil
16ddb 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 65 6e 20 d of root) then
16ddc 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69 all available si
16ddd 62 6c 69 6e 67 73 20 70 61 72 74 69 63 69 70 61 blings participa
16dde 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 te in the balanc
16ddf 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ing..**.** The n
16de0 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 umber of sibling
16de1 73 20 6f 66 20 70 50 61 67 65 20 6d 69 67 68 74 s of pPage might
16de2 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 6f 72 be increased or
16de3 20 64 65 63 72 65 61 73 65 64 20 62 79 20 6f 6e decreased by on
16de4 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 6e 20 61 e or.** two in a
16de5 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 n effort to keep
16de6 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66 75 pages nearly fu
16de7 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 20 ll but not over
16de8 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f 74 20 70 full. The root p
16de9 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 63 69 61 age.** is specia
16dea 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f 77 65 64 l and is allowed
16deb 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 20 65 6d to be nearly em
16dec 70 74 79 2e 20 49 66 20 70 50 61 67 65 20 69 73 pty. If pPage is
16ded 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 .** the root pa
16dee 67 65 2c 20 74 68 65 6e 20 74 68 65 20 64 65 70 ge, then the dep
16def 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 6d th of the tree m
16df0 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 ight be increase
16df1 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 61 73 65 d.** or decrease
16df2 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 6e 65 63 d by one, as nec
16df3 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 65 70 20 essary, to keep
16df4 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 66 72 the root page fr
16df5 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 65 72 om being.** over
16df6 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c 65 74 65 full or complete
16df7 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 ly empty..**.**
16df8 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 74 Note that when t
16df9 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
16dfa 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 20 74 alled, some of t
16dfb 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67 he Cells on pPag
16dfc 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61 e.** might not a
16dfd 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72 65 ctually be store
16dfe 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 d in pPage->aDat
16dff 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 6e 20 68 a[]. This can h
16e00 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20 appen.** if the
16e01 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c page is overfull
16e02 2e 20 20 50 61 72 74 20 6f 66 20 74 68 65 20 6a . Part of the j
16e03 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 ob of this routi
16e04 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d 61 6b 65 ne is to.** make
16e05 20 73 75 72 65 20 61 6c 6c 20 43 65 6c 6c 73 20 sure all Cells
16e06 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 65 20 61 for pPage once a
16e07 67 61 69 6e 20 66 69 74 20 69 6e 20 70 50 61 67 gain fit in pPag
16e08 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a 2a 0a 2a e->aData[]..**.*
16e09 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 73 65 20 * In the course
16e0a 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 of balancing the
16e0b 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 50 61 siblings of pPa
16e0c 67 65 2c 20 74 68 65 20 70 61 72 65 6e 74 20 6f ge, the parent o
16e0d 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74 f pPage.** might
16e0e 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c become overfull
16e0f 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 20 or underfull.
16e10 49 66 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c If that happens,
16e11 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
16e12 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 ne.** is called
16e13 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 74 recursively on t
16e14 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a he parent..**.**
16e15 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 If this routine
16e16 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 fails for any r
16e17 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20 eason, it might
16e18 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 61 leave the databa
16e19 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 se.** in a corru
16e1a 70 74 65 64 20 73 74 61 74 65 2e 20 20 53 6f 20 pted state. So
16e1b 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 if this routine
16e1c 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62 fails, the datab
16e1d 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 ase should.** be
16e1e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f rolled back..*/
16e1f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 .static int bala
16e20 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42 74 43 75 nce_nonroot(BtCu
16e21 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d rsor *pCur){. M
16e22 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 emPage *pPage;
16e23 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
16e24 68 65 20 6f 76 65 72 20 6f 72 20 75 6e 64 65 72 he over or under
16e25 66 75 6c 6c 20 70 61 67 65 20 74 6f 20 62 61 6c full page to bal
16e26 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ance */. MemPag
16e27 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 e *pParent;
16e28 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
16e29 72 65 6e 74 20 6f 66 20 70 50 61 67 65 20 2a 2f rent of pPage */
16e2a 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
16e2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16e2c 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 74 /* The whole dat
16e2d 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e abase */. int n
16e2e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 Cell = 0;
16e2f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
16e30 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 r of cells in ap
16e31 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 Cell[] */. int
16e32 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20 nMaxCells = 0;
16e33 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f /* Allo
16e34 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 70 cated size of ap
16e35 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46 Cell, szCell, aF
16e36 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f rom. */. int nO
16e37 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ld = 0;
16e38 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
16e39 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4f of pages in apO
16e3a 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e ld[] */. int nN
16e3b 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ew = 0;
16e3c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
16e3d 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4e of pages in apN
16e3e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 ew[] */. int nD
16e3f 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 iv;
16e40 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
16e41 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 44 of cells in apD
16e42 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c iv[] */. int i,
16e43 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20 j, k;
16e44 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
16e45 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 ounters */. int
16e46 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 idx;
16e47 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
16e48 65 78 20 6f 66 20 70 50 61 67 65 20 69 6e 20 70 ex of pPage in p
16e49 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 Parent->aCell[]
16e4a 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 */. int nxDiv;
16e4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16e4c 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 65 /* Next divide
16e4d 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e r slot in pParen
16e4e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 t->aCell[] */.
16e4f 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
16e50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16e51 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 The return code
16e52 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 43 6f 72 */. int leafCor
16e53 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 rection;
16e54 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65 20 /* 4 if pPage
16e55 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 is a leaf. 0 if
16e56 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 not */. int le
16e57 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 afData;
16e58 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
16e59 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 f pPage is a lea
16e5a 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41 20 f of a LEAFDATA
16e5b 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 tree */. int us
16e5c 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20 20 20 ableSpace;
16e5d 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
16e5e 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e 64 20 in pPage beyond
16e5f 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
16e60 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b 20 20 int pageFlags;
16e61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16e62 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65 2d 3e Value of pPage->
16e63 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e aData[0] */. in
16e64 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 t subtotal;
16e65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 /* Su
16e66 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 73 20 btotal of bytes
16e67 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 in cells on one
16e68 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 page */. int iS
16e69 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20 20 20 pace1 = 0;
16e6a 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
16e6b 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 unused byte of a
16e6c 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e Space1[] */. in
16e6d 74 20 69 53 70 61 63 65 32 20 3d 20 30 3b 20 20 t iSpace2 = 0;
16e6e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
16e6f 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 rst unused byte
16e70 6f 66 20 61 53 70 61 63 65 32 5b 5d 20 2a 2f 0a of aSpace2[] */.
16e71 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b int szScratch;
16e72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16e73 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63 * Size of scratc
16e74 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74 h memory request
16e75 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ed */. MemPage
16e76 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 *apOld[NB];
16e77 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e /* pPage an
16e78 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c d up to two sibl
16e79 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 ings */. Pgno p
16e7a 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 gnoOld[NB];
16e7b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
16e7c 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68 20 umbers for each
16e7d 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 page in apOld[]
16e7e 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 */. MemPage *ap
16e7f 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 Copy[NB];
16e80 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f 70 /* Private cop
16e81 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70 ies of apOld[] p
16e82 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ages */. MemPag
16e83 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20 e *apNew[NB+2];
16e84 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 /* pPage
16e85 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 69 62 and up to NB sib
16e86 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 6c 61 lings after bala
16e87 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 ncing */. Pgno
16e88 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 pgnoNew[NB+2];
16e89 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
16e8a 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68 numbers for each
16e8b 20 70 61 67 65 20 69 6e 20 61 70 4e 65 77 5b 5d page in apNew[]
16e8c 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 5b */. u8 *apDiv[
16e8d 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 NB];
16e8e 20 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63 65 /* Divider ce
16e8f 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 2a lls in pParent *
16e90 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e /. int cntNew[N
16e91 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 B+2];
16e92 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 65 /* Index in aCe
16e93 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74 ll[] of cell aft
16e94 65 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a er i-th page */.
16e95 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 int szNew[NB+2
16e96 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ]; /
16e97 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65 20 * Combined size
16e98 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 20 6f of cells place o
16e99 6e 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 n i-th page */.
16e9a 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 u8 **apCell = 0
16e9b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
16e9c 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e All cells begin
16e9d 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 75 balanced */. u
16e9e 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20 16 *szCell;
16e9f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
16ea0 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c ocal size of all
16ea1 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c cells in apCell
16ea2 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 43 6f 70 [] */. u8 *aCop
16ea3 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 2f y[NB]; /
16ea4 2a 20 53 70 61 63 65 20 66 6f 72 20 68 6f 6c 64 * Space for hold
16ea5 69 6e 67 20 64 61 74 61 20 6f 66 20 61 70 43 6f ing data of apCo
16ea6 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 py[] */. u8 *aS
16ea7 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20 20 pace1;
16ea8 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f /* Space for co
16ea9 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73 pies of dividers
16eaa 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 62 61 cells before ba
16eab 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a 61 lance */. u8 *a
16eac 53 70 61 63 65 32 20 3d 20 30 3b 20 20 20 20 20 Space2 = 0;
16ead 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6f /* Space for o
16eae 76 65 72 66 6c 6f 77 20 64 69 76 69 64 65 72 73 verflow dividers
16eaf 20 63 65 6c 6c 73 20 61 66 74 65 72 20 62 61 6c cells after bal
16eb0 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a 61 46 ance */. u8 *aF
16eb1 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 rom = 0;.. pPag
16eb2 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
16eb3 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
16eb4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16eb5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
16eb6 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
16eb7 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 43 ;. VVA_ONLY( pC
16eb8 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 ur->pagesShuffle
16eb9 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f 2a 20 0a d = 1 );.. /* .
16eba 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 70 61 ** Find the pa
16ebb 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a rent page.. */.
16ebc 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16ebd 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73 iPage>0 );. ass
16ebe 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e ert( pPage->isIn
16ebf 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 it );. assert(
16ec0 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
16ec1 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
16ec2 44 62 50 61 67 65 29 20 7c 7c 20 70 50 61 67 65 DbPage) || pPage
16ec3 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 ->nOverflow==1 )
16ec4 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d ;. pBt = pPage-
16ec5 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e 74 20 >pBt;. pParent
16ec6 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
16ec7 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 Cur->iPage-1];.
16ec8 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 assert( pParent
16ec9 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 );. if( SQLITE
16eca 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
16ecb 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
16ecc 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 29 20 rent->pDbPage))
16ecd 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 ){. goto bala
16ece 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d nce_cleanup;. }
16ecf 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 .. TRACE(("BALA
16ed0 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20 NCE: begin page
16ed1 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e %d child of %d\n
16ed2 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 ", pPage->pgno,
16ed3 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b pParent->pgno));
16ed4 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
16ed5 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e _OMIT_QUICKBALAN
16ed6 43 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 20 73 CE. /*. ** A s
16ed7 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 49 66 pecial case: If
16ed8 20 61 20 6e 65 77 20 65 6e 74 72 79 20 68 61 73 a new entry has
16ed9 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 73 65 72 just been inser
16eda 74 65 64 20 69 6e 74 6f 20 61 0a 20 20 2a 2a 20 ted into a. **
16edb 74 61 62 6c 65 20 28 74 68 61 74 20 69 73 2c 20 table (that is,
16edc 61 20 62 74 72 65 65 20 77 69 74 68 20 69 6e 74 a btree with int
16edd 65 67 65 72 20 6b 65 79 73 20 61 6e 64 20 61 6c eger keys and al
16ede 6c 20 64 61 74 61 20 61 74 20 74 68 65 20 6c 65 l data at the le
16edf 61 76 65 73 29 0a 20 20 2a 2a 20 61 6e 64 20 74 aves). ** and t
16ee0 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 he new entry is
16ee1 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 the right-most e
16ee2 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 65 ntry in the tree
16ee3 20 28 69 74 20 68 61 73 20 74 68 65 0a 20 20 2a (it has the. *
16ee4 2a 20 6c 61 72 67 65 73 74 20 6b 65 79 29 20 74 * largest key) t
16ee5 68 65 6e 20 75 73 65 20 74 68 65 20 73 70 65 63 hen use the spec
16ee6 69 61 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 ial balance_quic
16ee7 6b 28 29 20 72 6f 75 74 69 6e 65 20 66 6f 72 0a k() routine for.
16ee8 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 2e 20 ** balancing.
16ee9 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 balance_quick()
16eea 20 69 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 is much faster
16eeb 61 6e 64 20 72 65 73 75 6c 74 73 20 69 6e 20 61 and results in a
16eec 20 74 69 67 68 74 65 72 0a 20 20 2a 2a 20 70 61 tighter. ** pa
16eed 63 6b 69 6e 67 20 6f 66 20 64 61 74 61 20 69 6e cking of data in
16eee 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 the common case
16eef 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
16ef0 67 65 2d 3e 6c 65 61 66 20 26 26 0a 20 20 20 20 ge->leaf &&.
16ef1 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 pPage->intKey
16ef2 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e &&. pPage->
16ef3 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 26 26 0a nOverflow==1 &&.
16ef4 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 pPage->aOv
16ef5 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 fl[0].idx==pPage
16ef6 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 20 20 20 ->nCell &&.
16ef7 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d pParent->pgno!=
16ef8 31 20 26 26 0a 20 20 20 20 20 20 67 65 74 34 62 1 &&. get4b
16ef9 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 yte(&pParent->aD
16efa 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 ata[pParent->hdr
16efb 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 70 50 61 67 Offset+8])==pPag
16efc 65 2d 3e 70 67 6e 6f 0a 20 20 29 7b 0a 20 20 20 e->pgno. ){.
16efd 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
16efe 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 2f 2a intKey );. /*
16eff 0a 20 20 20 20 2a 2a 20 54 4f 44 4f 3a 20 43 68 . ** TODO: Ch
16f00 65 63 6b 20 74 68 65 20 73 69 62 6c 69 6e 67 73 eck the siblings
16f01 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 to the left of
16f02 70 50 61 67 65 2e 20 49 74 20 6d 61 79 20 62 65 pPage. It may be
16f03 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 that. ** the
16f04 79 20 61 72 65 20 6e 6f 74 20 66 75 6c 6c 20 61 y are not full a
16f05 6e 64 20 6e 6f 20 6e 65 77 20 70 61 67 65 20 69 nd no new page i
16f06 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 s required..
16f07 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 62 61 */. return ba
16f08 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 43 75 72 lance_quick(pCur
16f09 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 );. }.#endif..
16f0a 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
16f0b 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
16f0c 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 erWrite(pPage->p
16f0d 44 62 50 61 67 65 29 29 20 29 7b 0a 20 20 20 20 DbPage)) ){.
16f0e 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
16f0f 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a anup;. }.. /*.
16f10 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 65 ** Find the ce
16f11 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 ll in the parent
16f12 20 70 61 67 65 20 77 68 6f 73 65 20 6c 65 66 74 page whose left
16f13 20 63 68 69 6c 64 20 70 6f 69 6e 74 73 20 62 61 child points ba
16f14 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50 61 67 65 ck. ** to pPage
16f15 2e 20 20 54 68 65 20 22 69 64 78 22 20 76 61 72 . The "idx" var
16f16 69 61 62 6c 65 20 69 73 20 74 68 65 20 69 6e 64 iable is the ind
16f17 65 78 20 6f 66 20 74 68 61 74 20 63 65 6c 6c 2e ex of that cell.
16f18 20 20 49 66 20 70 50 61 67 65 0a 20 20 2a 2a 20 If pPage. **
16f19 69 73 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74 is the rightmost
16f1a 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e child of pParen
16f1b 74 20 74 68 65 6e 20 73 65 74 20 69 64 78 20 74 t then set idx t
16f1c 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c o pParent->nCell
16f1d 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20 3d 20 70 . */. idx = p
16f1e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
16f1f 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 61 73 73 >iPage-1];. ass
16f20 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 70 ertParentIndex(p
16f21 50 61 72 65 6e 74 2c 20 69 64 78 2c 20 70 50 61 Parent, idx, pPa
16f22 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a ge->pgno);.. /*
16f23 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69 62 6c 69 . ** Find sibli
16f24 6e 67 20 70 61 67 65 73 20 74 6f 20 70 50 61 67 ng pages to pPag
16f25 65 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 73 20 e and the cells
16f26 69 6e 20 70 50 61 72 65 6e 74 20 74 68 61 74 20 in pParent that
16f27 64 69 76 69 64 65 0a 20 20 2a 2a 20 74 68 65 20 divide. ** the
16f28 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e 20 61 74 siblings. An at
16f29 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f tempt is made to
16f2a 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67 find NN sibling
16f2b 73 20 6f 6e 20 65 69 74 68 65 72 0a 20 20 2a 2a s on either. **
16f2c 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 2e 20 side of pPage.
16f2d 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61 More siblings a
16f2e 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e re taken from on
16f2f 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c e side, however,
16f30 20 69 66 0a 20 20 2a 2a 20 70 50 61 67 65 20 74 if. ** pPage t
16f31 68 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 here are fewer t
16f32 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 han NN siblings
16f33 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 on the other sid
16f34 65 2e 20 20 49 66 20 70 50 61 72 65 6e 74 0a 20 e. If pParent.
16f35 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20 66 65 ** has NB or fe
16f36 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74 68 65 wer children the
16f37 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f n all children o
16f38 66 20 70 50 61 72 65 6e 74 20 61 72 65 20 74 61 f pParent are ta
16f39 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 78 44 69 ken.. */. nxDi
16f3a 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b 0a 20 20 v = idx - NN;.
16f3b 69 66 28 20 6e 78 44 69 76 20 2b 20 4e 42 20 3e if( nxDiv + NB >
16f3c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 pParent->nCell
16f3d 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 70 ){. nxDiv = p
16f3e 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 2d 20 Parent->nCell -
16f3f 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 69 66 NB + 1;. }. if
16f40 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a 20 20 20 ( nxDiv<0 ){.
16f41 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 7d 0a nxDiv = 0;. }.
16f42 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20 20 66 6f nDiv = 0;. fo
16f43 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69 76 3b 20 r(i=0, k=nxDiv;
16f44 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b 2b 29 7b i<NB; i++, k++){
16f45 0a 20 20 20 20 69 66 28 20 6b 3c 70 50 61 72 65 . if( k<pPare
16f46 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 nt->nCell ){.
16f47 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 apDiv[i] = fi
16f48 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
16f49 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69 76 2b 2b k);. nDiv++
16f4a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
16f4b 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61 66 20 29 !pParent->leaf )
16f4c 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b ;. pgnoOld[
16f4d 69 5d 20 3d 20 67 65 74 34 62 79 74 65 28 61 70 i] = get4byte(ap
16f4e 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 7d 65 6c Div[i]);. }el
16f4f 73 65 20 69 66 28 20 6b 3d 3d 70 50 61 72 65 6e se if( k==pParen
16f50 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 t->nCell ){.
16f51 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d 20 67 pgnoOld[i] = g
16f52 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 et4byte(&pParent
16f53 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d ->aData[pParent-
16f54 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a >hdrOffset+8]);.
16f55 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
16f56 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
16f57 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 rc = getAndIni
16f58 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 4f tPage(pBt, pgnoO
16f59 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64 5b 69 5d ld[i], &apOld[i]
16f5a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
16f5b 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
16f5c 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20 61 70 4f anup;. /* apO
16f5d 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 65 6e 74 ld[i]->idxParent
16f5e 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20 61 70 43 = k; */. apC
16f5f 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 opy[i] = 0;.
16f60 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f 6c 64 20 assert( i==nOld
16f61 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b 3b 0a 20 );. nOld++;.
16f62 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d 20 nMaxCells +=
16f63 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 6c 1+apOld[i]->nCel
16f64 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 65 l+apOld[i]->nOve
16f65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rflow;. }.. /*
16f66 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 Make nMaxCells
16f67 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 34 20 a multiple of 4
16f68 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 in order to pres
16f69 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a erve 8-byte. **
16f6a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 alignment */.
16f6b 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 nMaxCells = (nMa
16f6c 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a xCells + 3)&~3;.
16f6d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 . /*. ** Alloc
16f6e 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 ate space for me
16f6f 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 0a mory structures.
16f70 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61 74 63 68 */. szScratch
16f71 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 43 65 =. nMaxCe
16f72 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 lls*sizeof(u8*)
16f73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16f74 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c 20 /* apCell
16f75 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43 65 */. + nMaxCe
16f76 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 29 20 lls*sizeof(u16)
16f77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16f78 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 /* szCell
16f79 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f 55 4e 44 */. + (ROUND
16f7a 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 8(sizeof(MemPage
16f7b 29 29 2b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 ))+pBt->pageSize
16f7c 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70 79 20 2a )*NB /* aCopy *
16f7d 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 /. + pBt->pa
16f7e 67 65 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 geSize
16f7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16f80 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65 31 20 /* aSpace1
16f81 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53 41 55 54 */. + (ISAUT
16f82 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61 78 43 65 OVACUUM ? nMaxCe
16f83 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20 20 20 20 lls : 0);
16f84 20 20 20 20 20 20 2f 2a 20 61 46 72 6f 6d 20 2a /* aFrom *
16f85 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c /. apCell = sql
16f86 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f ite3ScratchMallo
16f87 63 28 20 73 7a 53 63 72 61 74 63 68 20 29 3b 20 c( szScratch );
16f88 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 . if( apCell==0
16f89 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
16f8a 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 ITE_NOMEM;. g
16f8b 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
16f8c 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 65 6c nup;. }. szCel
16f8d 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c l = (u16*)&apCel
16f8e 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 l[nMaxCells];.
16f8f 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75 38 2a 29 aCopy[0] = (u8*)
16f90 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c &szCell[nMaxCell
16f91 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 s];. assert( EI
16f92 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
16f93 4e 54 28 61 43 6f 70 79 5b 30 5d 29 20 29 3b 0a NT(aCopy[0]) );.
16f94 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 42 3b for(i=1; i<NB;
16f95 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43 6f 70 79 i++){. aCopy
16f96 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b 69 2d 31 [i] = &aCopy[i-1
16f97 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2b ][pBt->pageSize+
16f98 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 ROUND8(sizeof(Me
16f99 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20 20 61 73 mPage))];. as
16f9a 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b 69 5d sert( ((aCopy[i]
16f9b 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d - (u8*)0) & 7)=
16f9c 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 =0 ); /* 8-byte
16f9d 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 alignment requir
16f9e 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 70 61 ed */. }. aSpa
16f9f 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e 42 2d ce1 = &aCopy[NB-
16fa0 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 1][pBt->pageSize
16fa1 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d +ROUND8(sizeof(M
16fa2 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61 73 73 emPage))];. ass
16fa3 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
16fa4 41 4c 49 47 4e 4d 45 4e 54 28 61 53 70 61 63 65 ALIGNMENT(aSpace
16fa5 31 29 20 29 3b 0a 20 20 69 66 28 20 49 53 41 55 1) );. if( ISAU
16fa6 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
16fa7 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 31 aFrom = &aSpace1
16fa8 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3b [pBt->pageSize];
16fa9 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 32 20 3d . }. aSpace2 =
16faa 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
16fab 6f 63 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 oc(pBt->pageSize
16fac 29 3b 0a 20 20 69 66 28 20 61 53 70 61 63 65 32 );. if( aSpace2
16fad 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
16fae 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
16faf 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 goto balance_c
16fb0 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a 20 leanup;. }. .
16fb1 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 6f /*. ** Make co
16fb2 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f 6e 74 pies of the cont
16fb3 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61 6e 64 ent of pPage and
16fb4 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 69 6e its siblings in
16fb5 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a 20 to aOld[].. **
16fb6 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 The rest of this
16fb7 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75 function will u
16fb8 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 se data from the
16fb9 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20 copies rather.
16fba 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69 ** that the ori
16fbb 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63 ginal pages sinc
16fbc 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 e the original p
16fbd 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 ages will be in
16fbe 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 the. ** process
16fbf 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 of being overwr
16fc0 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f itten.. */. fo
16fc1 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 r(i=0; i<nOld; i
16fc2 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 ++){. MemPage
16fc3 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20 *p = apCopy[i]
16fc4 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f 70 = (MemPage*)aCop
16fc5 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 y[i];. memcpy
16fc6 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 (p, apOld[i], si
16fc7 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a zeof(MemPage));.
16fc8 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 28 p->aData = (
16fc9 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 void*)&p[1];.
16fca 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 61 memcpy(p->aData
16fcb 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 , apOld[i]->aDat
16fcc 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 a, pBt->pageSize
16fcd 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a );. }.. /*. *
16fce 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 * Load pointers
16fcf 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 to all cells on
16fd0 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e sibling pages an
16fd1 64 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 d the divider ce
16fd2 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 lls. ** into th
16fd3 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d e local apCell[]
16fd4 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f array. Make co
16fd5 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 pies of the divi
16fd6 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 der cells. ** i
16fd7 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e nto space obtain
16fd8 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65 31 5b ed form aSpace1[
16fd9 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 ] and remove the
16fda 20 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c the divider Cel
16fdb 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 ls. ** from pPa
16fdc 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rent.. **. **
16fdd 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 If the siblings
16fde 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 are on leaf page
16fdf 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c s, then the chil
16fe0 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 d pointers of th
16fe1 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 e. ** divider c
16fe2 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 ells are strippe
16fe3 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 d from the cells
16fe4 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 before they are
16fe5 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 copied. ** int
16fe6 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e o aSpace1[]. In
16fe7 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 this way, all c
16fe8 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d ells in apCell[]
16fe9 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a are without. *
16fea 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 * child pointers
16feb 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 . If siblings a
16fec 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 re not leaves, t
16fed 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a hen all cell in.
16fee 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e ** apCell[] in
16fef 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e clude child poin
16ff0 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 ters. Either wa
16ff1 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 y, all cells in
16ff2 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 apCell[]. ** ar
16ff3 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 e alike.. **.
16ff4 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f ** leafCorrectio
16ff5 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69 n: 4 if pPage i
16ff6 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 s a leaf. 0 if
16ff7 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c pPage is not a l
16ff8 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 eaf.. **
16ff9 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 leafData: 1 if
16ffa 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b pPage holds key+
16ffb 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 data and pParent
16ffc 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 holds only keys
16ffd 2e 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d .. */. nCell =
16ffe 20 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 0;. leafCorrec
16fff 74 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65 tion = pPage->le
17000 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 af*4;. leafData
17001 20 3d 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 = pPage->hasDat
17002 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c a;. for(i=0; i<
17003 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nOld; i++){.
17004 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 MemPage *pOld =
17005 61 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69 apCopy[i];. i
17006 6e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d nt limit = pOld-
17007 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 >nCell+pOld->nOv
17008 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 erflow;. for(
17009 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b j=0; j<limit; j+
1700a 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 +){. assert
1700b 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c ( nCell<nMaxCell
1700c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c s );. apCel
1700d 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f l[nCell] = findO
1700e 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 verflowCell(pOld
1700f 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 , j);. szCe
17010 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c ll[nCell] = cell
17011 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 SizePtr(pOld, ap
17012 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20 Cell[nCell]);.
17013 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
17014 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 CUUM ){.
17015 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61 int a;. a
17016 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 28 75 From[nCell] = (u
17017 38 29 69 3b 20 20 20 61 73 73 65 72 74 28 20 69 8)i; assert( i
17018 3e 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a 20 20 >=0 && i<6 );.
17019 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20 61 for(a=0; a
1701a 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 <pOld->nOverflow
1701b 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; a++){.
1701c 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76 66 if( pOld->aOvf
1701d 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43 65 l[a].pCell==apCe
1701e 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20 20 ll[nCell] ){.
1701f 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e aFrom[n
17020 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 Cell] = 0xFF;.
17021 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
17022 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
17023 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
17024 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 nCell++;.
17025 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f }. if( i<nO
17026 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75 31 ld-1 ){. u1
17027 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 6 sz = cellSizeP
17028 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 tr(pParent, apDi
17029 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 v[i]);. if(
1702a 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 leafData ){.
1702b 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68 65 /* With the
1702c 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c 20 LEAFDATA flag,
1702d 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68 6f pParent cells ho
1702e 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73 20 ld only INTKEYs
1702f 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 that. **
17030 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20 6f are duplicates o
17031 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63 68 f keys on the ch
17032 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20 6e ild pages. We n
17033 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20 20 eed to remove.
17034 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69 76 ** the div
17035 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20 ider cells from
17036 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68 65 pParent, but the
17037 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 dividers cells
17038 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20 20 are not.
17039 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43 65 ** added to apCe
1703a 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68 65 ll[] because the
1703b 79 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 y are duplicates
1703c 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73 2e of child cells.
1703d 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1703e 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 dropCell(pPa
1703f 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a 29 rent, nxDiv, sz)
17040 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
17041 20 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70 u8 *pTemp
17042 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
17043 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c ( nCell<nMaxCell
17044 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 s );. szC
17045 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b ell[nCell] = sz;
17046 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 3d . pTemp =
17047 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65 &aSpace1[iSpace
17048 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70 61 1];. iSpa
17049 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 ce1 += sz;.
1704a 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 assert( sz<=p
1704b 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 Bt->pageSize/4 )
1704c 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1704d 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e ( iSpace1<=pBt->
1704e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 pageSize );.
1704f 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 memcpy(pTemp
17050 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b , apDiv[i], sz);
17051 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b . apCell[
17052 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c nCell] = pTemp+l
17053 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 eafCorrection;.
17054 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 if( ISAUT
17055 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 OVACUUM ){.
17056 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c aFrom[nCell
17057 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20 20 ] = 0xFF;.
17058 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f 70 }. drop
17059 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 Cell(pParent, nx
1705a 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 Div, sz);.
1705b 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f assert( leafCo
1705c 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c rrection==0 || l
1705d 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 eafCorrection==4
1705e 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65 );. szCe
1705f 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28 75 31 ll[nCell] -= (u1
17060 36 29 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 6)leafCorrection
17061 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
17062 28 20 67 65 74 34 62 79 74 65 28 70 54 65 6d 70 ( get4byte(pTemp
17063 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 29 3b )==pgnoOld[i] );
17064 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4f . if( !pO
17065 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ld->leaf ){.
17066 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 assert( le
17067 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 afCorrection==0
17068 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 );. /*
17069 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74 65 The right pointe
1706a 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 r of the child p
1706b 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 73 age pOld becomes
1706c 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20 20 the left.
1706d 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 6f ** pointer o
1706e 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 f the divider ce
1706f 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ll */.
17070 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 memcpy(apCell[nC
17071 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 ell], &pOld->aDa
17072 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 ta[pOld->hdrOffs
17073 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 et+8], 4);.
17074 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17075 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 assert( leaf
17076 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b Correction==4 );
17077 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
17078 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 zCell[nCell]<4 )
17079 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a {. /*
1707a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e Do not allow an
1707b 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 y cells smaller
1707c 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f than 4 bytes. */
1707d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a 43 . szC
1707e 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a ell[nCell] = 4;.
1707f 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
17080 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 43 }. nC
17081 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 ell++;. }.
17082 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 }. }.. /*.
17083 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 ** Figure out t
17084 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
17085 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c es needed to hol
17086 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c d all nCell cell
17087 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 s.. ** Store th
17088 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 is number in "k"
17089 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 . Also compute
1708a 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 szNew[] which is
1708b 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 the total. **
1708c 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c size of all cell
1708d 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 70 61 s on the i-th pa
1708e 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 ge and cntNew[]
1708f 77 68 69 63 68 20 69 73 20 74 68 65 20 69 6e 64 which is the ind
17090 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c ex. ** in apCel
17091 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 l[] of the cell
17092 74 68 61 74 20 64 69 76 69 64 65 73 20 70 61 67 that divides pag
17093 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20 69 2b e i from page i+
17094 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 1. . ** cntNew
17095 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 6c [k] should equal
17096 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a nCell.. **. *
17097 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 * Values compute
17098 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a d by this block:
17099 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
1709a 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 k: The tota
1709b 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c l number of sibl
1709c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 ing pages. **
1709d 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 szNew[i]: Spac
1709e 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65 20 69 ed used on the i
1709f 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 -th sibling page
170a0 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b .. ** cntNew[
170a1 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 70 43 i]: Index in apC
170a2 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c ell[] and szCell
170a3 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 [] for the first
170a4 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 cell to. **
170a5 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 the r
170a6 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d 74 68 ight of the i-th
170a7 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 sibling page..
170a8 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 65 3a ** usableSpace:
170a9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
170aa 20 6f 66 20 73 70 61 63 65 20 61 76 61 69 6c 61 of space availa
170ab 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 62 6c ble on each sibl
170ac 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a ing.. ** . */.
170ad 20 20 75 73 61 62 6c 65 53 70 61 63 65 20 3d 20 usableSpace =
170ae 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
170af 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 - 12 + leafCorre
170b0 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 ction;. for(sub
170b1 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e total=k=i=0; i<n
170b2 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
170b3 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 assert( i<nMaxCe
170b4 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 74 6f lls );. subto
170b5 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d tal += szCell[i]
170b6 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 73 75 + 2;. if( su
170b7 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 btotal > usableS
170b8 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 73 7a pace ){. sz
170b9 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 New[k] = subtota
170ba 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 l - szCell[i];.
170bb 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d cntNew[k] =
170bc 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 i;. if( le
170bd 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d afData ){ i--; }
170be 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61 6c 20 . subtotal
170bf 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a = 0;. k++;.
170c0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 }. }. szNe
170c1 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b w[k] = subtotal;
170c2 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e . cntNew[k] = n
170c3 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 Cell;. k++;..
170c4 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 6b /*. ** The pack
170c5 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 20 ing computed by
170c6 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c 6f the previous blo
170c7 63 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f 77 ck is biased tow
170c8 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 ard the siblings
170c9 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66 . ** on the lef
170ca 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 66 t side. The lef
170cb 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 61 t siblings are a
170cc 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 6c lways nearly ful
170cd 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 2a l, while the. *
170ce 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 * right-most sib
170cf 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e 65 ling might be ne
170d0 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 arly empty. Thi
170d1 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 s block of code
170d2 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f attempts. ** to
170d3 20 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 6b adjust the pack
170d4 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 ing of siblings
170d5 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20 to get a better
170d6 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 balance.. **.
170d7 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 ** This adjustme
170d8 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 nt is more than
170d9 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e an optimization.
170da 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 62 The packing ab
170db 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 ove might. ** b
170dc 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61 e so out of bala
170dd 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c 6c nce as to be ill
170de 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70 egal. For examp
170df 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d 6f le, the right-mo
170e0 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 st. ** sibling
170e1 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 74 might be complet
170e2 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 ely empty. This
170e3 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e adjustment is n
170e4 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a ot optional.. *
170e5 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 /. for(i=k-1; i
170e6 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e >0; i--){. in
170e7 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65 t szRight = szNe
170e8 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f w[i]; /* Size o
170e9 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 f sibling on the
170ea 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e right */. in
170eb 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 t szLeft = szNew
170ec 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f [i-1]; /* Size o
170ed 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 f sibling on the
170ee 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74 left */. int
170ef 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
170f0 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67 /* Index of rig
170f1 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 ht-most cell in
170f2 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a left sibling */.
170f3 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20 int d;
170f4 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
170f5 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74 of first cell t
170f6 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69 o the left of ri
170f7 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a ght sibling */..
170f8 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 r = cntNew[i
170f9 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d -1] - 1;. d =
170fa 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 r + 1 - leafDat
170fb 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 a;. assert( d
170fc 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 <nMaxCells );.
170fd 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 78 assert( r<nMax
170fe 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 69 Cells );. whi
170ff 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 7c le( szRight==0 |
17100 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c 6c | szRight+szCell
17101 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 [d]+2<=szLeft-(s
17102 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 zCell[r]+2) ){.
17103 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d 20 szRight +=
17104 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 szCell[d] + 2;.
17105 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20 73 szLeft -= s
17106 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 zCell[r] + 2;.
17107 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d cntNew[i-1]-
17108 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e 74 -;. r = cnt
17109 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 New[i-1] - 1;.
1710a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 d = r + 1 -
1710b 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d 0a leafData;. }.
1710c 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 szNew[i] = s
1710d 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e 65 zRight;. szNe
1710e 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b w[i-1] = szLeft;
1710f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 65 . }.. /* Eithe
17110 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f r we found one o
17111 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e r more cells (cn
17112 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 77 tnew[0])>0) or w
17113 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 61 e are the. ** a
17114 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 virtual root pa
17115 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20 72 ge. A virtual r
17116 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 65 6e oot page is when
17117 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a 20 the real root.
17118 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67 65 ** page is page
17119 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 74 68 1 and we are th
1711a 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20 e only child of
1711b 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a that page.. */.
1711c 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65 77 assert( cntNew
1711d 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65 6e [0]>0 || (pParen
1711e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70 50 t->pgno==1 && pP
1711f 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 arent->nCell==0)
17120 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 );.. /*. ** A
17121 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 61 llocate k new pa
17122 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 ges. Reuse old
17123 70 61 67 65 73 20 77 68 65 72 65 20 70 6f 73 73 pages where poss
17124 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 ible.. */. ass
17125 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f ert( pPage->pgno
17126 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c 61 67 >1 );. pageFlag
17127 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 s = pPage->aData
17128 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 [0];. for(i=0;
17129 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d i<k; i++){. M
1712a 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 emPage *pNew;.
1712b 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a if( i<nOld ){.
1712c 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e pNew = apN
1712d 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d ew[i] = apOld[i]
1712e 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b ;. pgnoNew[
1712f 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 5d 3b i] = pgnoOld[i];
17130 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 . apOld[i]
17131 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
17132 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
17133 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 e(pNew->pDbPage)
17134 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a ;. nNew++;.
17135 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 if( rc ) g
17136 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
17137 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a nup;. }else{.
17138 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e assert( i>
17139 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 );. rc =
1713a 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
1713b 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 e(pBt, &pNew, &p
1713c 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e 6f 4e gnoNew[i], pgnoN
1713d 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 20 20 ew[i-1], 0);.
1713e 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
1713f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 balance_cleanup
17140 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d ;. apNew[i]
17141 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e = pNew;. n
17142 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d New++;. }. }
17143 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 .. /* Free any
17144 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 20 77 old pages that w
17145 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 20 61 ere not reused a
17146 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a s new pages.. *
17147 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c /. while( i<nOl
17148 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 72 d ){. rc = fr
17149 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 eePage(apOld[i])
1714a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
1714b 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 oto balance_clea
1714c 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 nup;. release
1714d 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a Page(apOld[i]);.
1714e 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 apOld[i] = 0
1714f 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a ;. i++;. }..
17150 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 68 /*. ** Put th
17151 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e 20 61 e new pages in a
17152 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 ccending order.
17153 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f 0a 20 This helps to.
17154 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 73 ** keep entries
17155 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c in the disk fil
17156 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 68 e in order so th
17157 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 6f at a scan. ** o
17158 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 f the table is a
17159 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 linear scan thr
1715a 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e 20 20 ough the file.
1715b 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 72 That. ** in tur
1715c 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 65 72 n helps the oper
1715d 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20 ating system to
1715e 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a 20 20 deliver pages.
1715f 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b ** from the disk
17160 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a 20 more rapidly..
17161 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e **. ** An O(n^
17162 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 2) insertion sor
17163 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 75 t algorithm is u
17164 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 0a 20 sed, but since.
17165 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 6d ** n is never m
17166 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 20 73 ore than NB (a s
17167 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20 mall constant),
17168 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a 2a that should. **
17169 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 not be a proble
1716a 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 m.. **. ** Whe
1716b 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e n NB==3, this on
1716c 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d e optimization m
1716d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 61 73 akes the databas
1716e 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 25 e. ** about 25%
1716f 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72 67 faster for larg
17170 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e 64 e insertions and
17171 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f deletions.. */
17172 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d . for(i=0; i<k-
17173 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 1; i++){. int
17174 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b minV = pgnoNew[
17175 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 i];. int minI
17176 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d = i;. for(j=
17177 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a i+1; j<k; j++){.
17178 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 4e 65 if( pgnoNe
17179 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 29 6d w[j]<(unsigned)m
1717a 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d inV ){. m
1717b 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 inI = j;.
1717c 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b minV = pgnoNew[
1717d 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 j];. }.
1717e 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69 }. if( minI>i
1717f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 3b ){. int t;
17180 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a . MemPage *
17181 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 70 67 pT;. t = pg
17182 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 noNew[i];.
17183 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 pT = apNew[i];.
17184 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 pgnoNew[i]
17185 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 3b = pgnoNew[minI];
17186 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 . apNew[i]
17187 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 = apNew[minI];.
17188 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e pgnoNew[min
17189 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 61 70 I] = t;. ap
1718a 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a New[minI] = pT;.
1718b 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 }. }. TRAC
1718c 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 E(("BALANCE: old
1718d 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65 77 3a : %d %d %d new:
1718e 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 %d(%d) %d(%d) %
1718f 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28 d(%d) %d(%d) %d(
17190 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 6e 6f %d)\n",. pgno
17191 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e 4f 6c Old[0], . nOl
17192 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 5b 31 d>=2 ? pgnoOld[1
17193 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e ] : 0,. nOld>
17194 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 5d 20 =3 ? pgnoOld[2]
17195 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e 65 77 : 0,. pgnoNew
17196 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 [0], szNew[0],.
17197 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 67 6e nNew>=2 ? pgn
17198 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e 4e 65 oNew[1] : 0, nNe
17199 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 w>=2 ? szNew[1]
1719a 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 : 0,. nNew>=3
1719b 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 3a 20 ? pgnoNew[2] :
1719c 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 4e 0, nNew>=3 ? szN
1719d 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 6e ew[2] : 0,. n
1719e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e 65 77 New>=4 ? pgnoNew
1719f 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34 [3] : 0, nNew>=4
171a0 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c ? szNew[3] : 0,
171a1 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20 70 . nNew>=5 ? p
171a2 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c 20 6e gnoNew[4] : 0, n
171a3 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 New>=5 ? szNew[4
171a4 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a 0a 20 ] : 0));.. /*.
171a5 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 ** Evenly distr
171a6 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 ibute the data i
171a7 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 n apCell[] acros
171a8 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e s the new pages.
171a9 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 . ** Insert div
171aa 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 ider cells into
171ab 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 pParent as neces
171ac 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d sary.. */. j =
171ad 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
171ae 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nNew; i++){.
171af 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 /* Assemble the
171b0 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 new sibling pag
171b1 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 e. */. MemPag
171b2 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b e *pNew = apNew[
171b3 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 i];. assert(
171b4 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 j<nMaxCells );.
171b5 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d assert( pNew-
171b6 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 5b 69 >pgno==pgnoNew[i
171b7 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 ] );. zeroPag
171b8 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61 67 e(pNew, pageFlag
171b9 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 s);. assemble
171ba 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65 Page(pNew, cntNe
171bb 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b w[i]-j, &apCell[
171bc 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b j], &szCell[j]);
171bd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 . assert( pNe
171be 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e w->nCell>0 || (n
171bf 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77 New==1 && cntNew
171c0 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20 61 [0]==0) );. a
171c1 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f 76 ssert( pNew->nOv
171c2 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 20 erflow==0 );..
171c3 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
171c4 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
171c5 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20 atabase, update
171c6 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
171c7 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 74 entries. ** t
171c8 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 hat point to the
171c9 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 77 siblings that w
171ca 65 72 65 20 72 65 61 72 72 61 6e 67 65 64 2e 20 ere rearranged.
171cb 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20 6c 65 These can be: le
171cc 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 72 ft. ** childr
171cd 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 68 65 en of cells, the
171ce 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 right-child of
171cf 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f 76 65 the page, or ove
171d0 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20 rflow pages.
171d1 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ** pointed to by
171d2 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f 0a 20 cells.. */.
171d3 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
171d4 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 UUM ){. for
171d5 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b 69 (k=j; k<cntNew[i
171d6 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 ]; k++){.
171d7 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 78 43 assert( k<nMaxC
171d8 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 ells );.
171d9 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 78 if( aFrom[k]==0x
171da 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 46 72 FF || apCopy[aFr
171db 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 4e om[k]]->pgno!=pN
171dc 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 ew->pgno ){.
171dd 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
171de 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c 20 6b pPutOvfl(pNew, k
171df 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 -j);. i
171e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
171e1 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63 74 69 && leafCorrecti
171e2 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 on==0 ){.
171e3 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
171e4 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 79 74 Put(pBt, get4byt
171e5 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 50 54 e(apCell[k]), PT
171e6 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 RMAP_BTREE, pNew
171e7 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
171e8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 }. i
171e9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
171ea 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
171eb 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
171ec 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 20 20 anup;.
171ed 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
171ee 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6a }. }.. j
171ef 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 = cntNew[i];..
171f0 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69 62 /* If the sib
171f1 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d 62 ling page assemb
171f2 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e 6f led above was no
171f3 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 t the right-most
171f4 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a sibling,. **
171f5 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 64 65 insert a divide
171f6 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 r cell into the
171f7 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 parent page..
171f8 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c 6e 4e */. if( i<nN
171f9 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c 6c 20 ew-1 && j<nCell
171fa 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 ){. u8 *pCe
171fb 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 ll;. u8 *pT
171fc 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 emp;. int s
171fd 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 z;.. assert
171fe 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( j<nMaxCells );
171ff 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 61 . pCell = a
17200 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 pCell[j];.
17201 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b sz = szCell[j] +
17202 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b leafCorrection;
17203 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 . pTemp = &
17204 61 53 70 61 63 65 32 5b 69 53 70 61 63 65 32 5d aSpace2[iSpace2]
17205 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 ;. if( !pNe
17206 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 w->leaf ){.
17207 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 2d memcpy(&pNew-
17208 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c 6c >aData[8], pCell
17209 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 4);. if
1720a 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 0a ( ISAUTOVACUUM .
1720b 20 20 20 20 20 20 20 20 20 26 26 20 28 61 46 72 && (aFr
1720c 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c 20 61 om[j]==0xFF || a
1720d 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d 5d 2d pCopy[aFrom[j]]-
1720e 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e >pgno!=pNew->pgn
1720f 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 o). ){.
17210 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 rc = ptr
17211 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 mapPut(pBt, get4
17212 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 54 52 byte(pCell), PTR
17213 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d MAP_BTREE, pNew-
17214 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 >pgno);.
17215 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
17216 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
17217 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f goto balance_
17218 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 cleanup;.
17219 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1721a 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c }else if( l
1721b 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 eafData ){.
1721c 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 /* If the tre
1721d 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 e is a leaf-data
1721e 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 tree, and the s
1721f 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 iblings are leav
17220 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 es, . **
17221 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f then there is no
17222 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e divider cell in
17223 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 apCell[]. Inste
17224 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 ad, the divider
17225 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c . ** cell
17226 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 consists of the
17227 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 integer key for
17228 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 the right-most
17229 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 cell of .
1722a 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d ** the sibling-
1722b 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 page assembled a
1722c 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 bove only..
1722d 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 */. Ce
1722e 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 llInfo info;.
1722f 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 j--;.
17230 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 sqlite3BtreePa
17231 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c rseCellPtr(pNew,
17232 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 apCell[j], &inf
17233 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c o);. pCel
17234 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 20 l = pTemp;.
17235 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 rc = fillInCe
17236 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c ll(pParent, pCel
17237 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 2c l, 0, info.nKey,
17238 20 30 2c 20 30 2c 20 30 2c 20 26 73 7a 29 3b 0a 0, 0, 0, &sz);.
17239 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1723a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1723b 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 goto bala
1723c 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
1723d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
1723e 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 Temp = 0;.
1723f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
17240 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 Cell -= 4;.
17241 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 /* Obscure ca
17242 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d se for non-leaf-
17243 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 74 data trees: If t
17244 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c he cell at pCell
17245 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 was. **
17246 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 previously store
17247 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 d on a leaf node
17248 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74 , and its report
17249 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20 ed size was 4.
1724a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 ** bytes,
1724b 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75 then it may actu
1724c 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 ally be smaller
1724d 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20 than this .
1724e 20 20 20 2a 2a 20 28 73 65 65 20 73 71 6c 69 74 ** (see sqlit
1724f 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
17250 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 20 69 Ptr(), 4 bytes i
17251 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 s the minimum si
17252 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a ze of. **
17253 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74 20 any cell). But
17254 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 it is important
17255 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f 72 72 to pass the corr
17256 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 20 20 ect size to .
17257 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 ** insertCe
17258 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 73 65 ll(), so reparse
17259 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20 the cell now..
1725a 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
1725b 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 ** Note that t
1725c 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 68 61 his can never ha
1725d 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74 ppen in an SQLit
1725e 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 73 20 e data file, as
1725f 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 all. ** c
17260 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 61 73 ells are at leas
17261 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 6f 6e t 4 bytes. It on
17262 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 62 2d ly happens in b-
17263 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 20 20 trees used.
17264 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74 ** to evaluat
17265 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e e "IN (SELECT ..
17266 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 .)" and similar
17267 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 20 20 clauses..
17268 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
17269 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a szCell[j]==4 ){.
1726a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1726b 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d (leafCorrection=
1726c 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 =4);. s
1726d 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 z = cellSizePtr(
1726e 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b pParent, pCell);
1726f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
17270 20 7d 0a 20 20 20 20 20 20 69 53 70 61 63 65 32 }. iSpace2
17271 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 += sz;. as
17272 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 sert( sz<=pBt->p
17273 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 ageSize/4 );.
17274 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61 63 assert( iSpac
17275 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a e2<=pBt->pageSiz
17276 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 e );. rc =
17277 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 insertCell(pPare
17278 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c nt, nxDiv, pCell
17279 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 34 29 3b , sz, pTemp, 4);
1727a 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1727b 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
1727c 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
1727d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
1727e 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1727f 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
17280 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 pDbPage) );.
17281 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f put4byte(findO
17282 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72 verflowCell(pPar
17283 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e 65 77 ent,nxDiv), pNew
17284 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 20 20 ->pgno);..
17285 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e /* If this is an
17286 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 auto-vacuum dat
17287 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 20 61 abase, and not a
17288 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 2c leaf-data tree,
17289 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 75 . ** then u
1728a 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 pdate the pointe
1728b 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20 65 6e r map with an en
1728c 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65 72 try for the over
1728d 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 flow page.
1728e 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c ** that the cell
1728f 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20 70 just inserted p
17290 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 6e 79 oints to (if any
17291 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ).. */.
17292 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
17293 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74 61 20 UM && !leafData
17294 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
17295 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 50 ptrmapPutOvfl(pP
17296 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b 0a 20 arent, nxDiv);.
17297 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
17298 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17299 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e goto balan
1729a 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 ce_cleanup;.
1729b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1729c 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 6e j++;. n
1729d 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 xDiv++;. }..
1729e 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 70 6f /* Set the po
1729f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 inter-map entry
172a0 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 62 6c for the new sibl
172a1 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 ing page. */.
172a2 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
172a3 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 M ){. rc =
172a4 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 ptrmapPut(pBt, p
172a5 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 New->pgno, PTRMA
172a6 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 P_BTREE, pParent
172a7 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 ->pgno);. i
172a8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
172a9 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
172aa 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 balance_cleanup
172ab 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
172ac 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 3d }. assert( j=
172ad 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 =nCell );. asse
172ae 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20 rt( nOld>0 );.
172af 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 29 assert( nNew>0 )
172b0 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 6c 61 ;. if( (pageFla
172b1 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d gs & PTF_LEAF)==
172b2 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68 0 ){. u8 *zCh
172b3 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f ild = &apCopy[nO
172b4 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b ld-1]->aData[8];
172b5 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 70 4e . memcpy(&apN
172b6 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74 ew[nNew-1]->aDat
172b7 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29 a[8], zChild, 4)
172b8 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f ;. if( ISAUTO
172b9 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
172ba 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 rc = ptrmapPut(p
172bb 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a 43 68 Bt, get4byte(zCh
172bc 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 ild), PTRMAP_BTR
172bd 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 EE, apNew[nNew-1
172be 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 ]->pgno);.
172bf 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
172c0 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
172c1 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
172c2 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d p;. }. }
172c3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 . }. assert( s
172c4 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
172c5 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
172c6 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 pDbPage) );. if
172c7 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 ( nxDiv==pParent
172c8 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 6e 74 2d ->nCell+pParent-
172c9 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 >nOverflow ){.
172ca 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 /* Right-most
172cb 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 72 sibling is the r
172cc 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 ight-most child
172cd 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 of pParent */.
172ce 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72 put4byte(&pPar
172cf 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
172d0 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
172d1 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d 31 , pgnoNew[nNew-1
172d2 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ]);. }else{.
172d3 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 /* Right-most s
172d4 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 6c 65 ibling is the le
172d5 66 74 20 63 68 69 6c 64 20 6f 66 20 74 68 65 20 ft child of the
172d6 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 70 first entry in p
172d7 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a 20 70 61 Parent. ** pa
172d8 73 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 st the right-mos
172d9 74 20 64 69 76 69 64 65 72 20 65 6e 74 72 79 20 t divider entry
172da 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 */. put4byte(
172db 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c findOverflowCell
172dc 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 (pParent, nxDiv)
172dd 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d 31 , pgnoNew[nNew-1
172de 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 ]);. }.. /*.
172df 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68 65 20 70 ** Balance the p
172e0 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e 6f 74 arent page. Not
172e1 65 20 74 68 61 74 20 74 68 65 20 63 75 72 72 65 e that the curre
172e2 6e 74 20 70 61 67 65 20 28 70 50 61 67 65 29 20 nt page (pPage)
172e3 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61 76 65 20 might. ** have
172e4 62 65 65 6e 20 61 64 64 65 64 20 74 6f 20 74 68 been added to th
172e5 65 20 66 72 65 65 6c 69 73 74 20 73 6f 20 69 74 e freelist so it
172e6 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e 67 65 72 might no longer
172e7 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e be initialized.
172e8 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 70 61 . ** But the pa
172e9 72 65 6e 74 20 70 61 67 65 20 77 69 6c 6c 20 61 rent page will a
172ea 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69 61 6c lways be initial
172eb 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 ized.. */. ass
172ec 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69 73 ert( pParent->is
172ed 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c 69 74 65 Init );. sqlite
172ee 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70 43 3ScratchFree(apC
172ef 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c 6c 20 3d ell);. apCell =
172f0 20 30 3b 0a 20 20 54 52 41 43 45 28 28 22 42 41 0;. TRACE(("BA
172f1 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64 20 LANCE: finished
172f2 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25 64 20 with %d: old=%d
172f3 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c new=%d cells=%d\
172f4 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 50 n",. pP
172f5 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c 64 2c age->pgno, nOld,
172f6 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b 0a nNew, nCell));.
172f7 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
172f8 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 73 ow = 0;. releas
172f9 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
172fa 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 pCur->iPage--;.
172fb 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 rc = balance(pC
172fc 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a 0a ur, 0);. . /*.
172fd 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65 66 ** Cleanup bef
172fe 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20 ore returning..
172ff 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61 */.balance_clea
17300 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 50 61 nup:. sqlite3Pa
17301 67 65 46 72 65 65 28 61 53 70 61 63 65 32 29 3b geFree(aSpace2);
17302 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 . sqlite3Scratc
17303 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 hFree(apCell);.
17304 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 for(i=0; i<nOld
17305 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 ; i++){. rele
17306 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d asePage(apOld[i]
17307 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 );. }. for(i=0
17308 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a ; i<nNew; i++){.
17309 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1730a 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 20 apNew[i]);. }.
1730b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1730c 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 ur->iPage]->nOve
1730d 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 72 65 rflow = 0;.. re
1730e 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1730f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
17310 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 68 65 s called for the
17311 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 root page of a
17312 62 74 72 65 65 20 77 68 65 6e 20 74 68 65 20 72 btree when the r
17313 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 oot.** page cont
17314 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 20 ains no cells.
17315 54 68 69 73 20 69 73 20 61 6e 20 6f 70 70 6f 72 This is an oppor
17316 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 74 tunity to make t
17317 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 61 6c 6c he tree.** shall
17318 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c 65 76 65 ower by one leve
17319 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
1731a 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f 77 balance_shallow
1731b 65 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 er(BtCursor *pCu
1731c 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 r){. MemPage *p
1731d 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
1731e 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 /* Root page
1731f 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 4d of B-Tree */. M
17320 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 emPage *pChild;
17321 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
17322 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 70 61 he only child pa
17323 67 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 20 ge of pPage */.
17324 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 3b Pgno pgnoChild;
17325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17326 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 Page number for
17327 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69 6e 74 pChild */. int
17328 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
17329 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
1732a 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 urn code from su
1732b 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 bprocedures */.
1732c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 BtShared *pBt;
1732d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1732e 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42 54 72 /* The main BTr
1732f 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a ee structure */.
17330 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65 72 50 int mxCellPerP
17331 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f age; /
17332 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 * Maximum number
17333 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 20 70 61 of cells per pa
17334 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 ge */. u8 **apC
17335 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 ell;
17336 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c /* All cell
17337 73 20 66 72 6f 6d 20 70 61 67 65 73 20 62 65 69 s from pages bei
17338 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 ng balanced */.
17339 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 u16 *szCell;
1733a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1733b 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 Local size of a
1733c 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20 20 61 ll cells */.. a
1733d 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 ssert( pCur->iPa
1733e 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 65 ge==0 );. pPage
1733f 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
17340 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 0];.. assert( p
17341 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 Page->nCell==0 )
17342 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
17343 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
17344 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
17345 29 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 ) );. pBt = pPa
17346 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43 65 6c ge->pBt;. mxCel
17347 6c 50 65 72 50 61 67 65 20 3d 20 4d 58 5f 43 45 lPerPage = MX_CE
17348 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70 43 65 6c LL(pBt);. apCel
17349 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f l = sqlite3Mallo
1734a 63 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 c( mxCellPerPage
1734b 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b 73 69 *(sizeof(u8*)+si
1734c 7a 65 6f 66 28 75 31 36 29 29 20 29 3b 0a 20 20 zeof(u16)) );.
1734d 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 20 if( apCell==0 )
1734e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1734f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 MEM;. szCell =
17350 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6d 78 (u16*)&apCell[mx
17351 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a 20 20 CellPerPage];.
17352 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 if( pPage->leaf
17353 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 ){. /* The ta
17354 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c ble is completel
17355 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 54 y empty */. T
17356 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 RACE(("BALANCE:
17357 65 6d 70 74 79 20 74 61 62 6c 65 20 25 64 5c 6e empty table %d\n
17358 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 ", pPage->pgno))
17359 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
1735a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 * The root page
1735b 69 73 20 65 6d 70 74 79 20 62 75 74 20 68 61 73 is empty but has
1735c 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 72 61 one child. Tra
1735d 6e 73 66 65 72 20 74 68 65 0a 20 20 20 20 2a 2a nsfer the. **
1735e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
1735f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68 69 6c 64 m that one child
17360 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 into the root p
17361 61 67 65 20 69 66 20 69 74 20 0a 20 20 20 20 2a age if it . *
17362 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 68 69 * will fit. Thi
17363 73 20 72 65 64 75 63 65 73 20 74 68 65 20 64 65 s reduces the de
17364 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 pth of the tree
17365 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 by one.. **.
17366 20 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f ** If the roo
17367 74 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31 t page is page 1
17368 2c 20 69 74 20 68 61 73 20 6c 65 73 73 20 73 70 , it has less sp
17369 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 74 68 ace available th
1736a 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 63 68 an. ** its ch
1736b 69 6c 64 20 28 64 75 65 20 74 6f 20 74 68 65 20 ild (due to the
1736c 31 30 30 20 62 79 74 65 20 68 65 61 64 65 72 20 100 byte header
1736d 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74 that occurs at t
1736e 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 he beginning.
1736f 20 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 ** of the datab
17370 61 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 74 20 ase fle), so it
17371 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62 6c might not be abl
17372 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6f 66 e to hold all of
17373 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 6e 66 the . ** inf
17374 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 6e 74 ormation current
17375 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 ly contained in
17376 74 68 65 20 63 68 69 6c 64 2e 20 20 49 66 20 74 the child. If t
17377 68 69 73 20 69 73 20 74 68 65 20 0a 20 20 20 20 his is the .
17378 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 64 6f ** case, then do
17379 20 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 61 6e not do the tran
1737a 73 66 65 72 2e 20 20 4c 65 61 76 65 20 70 61 67 sfer. Leave pag
1737b 65 20 31 20 65 6d 70 74 79 20 65 78 63 65 70 74 e 1 empty except
1737c 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 . ** for the
1737d 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 74 6f right-pointer to
1737e 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 2e the child page.
1737f 20 20 54 68 65 20 63 68 69 6c 64 20 70 61 67 65 The child page
17380 20 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a 2a 20 becomes. **
17381 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 the virtual root
17382 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 20 20 of the tree..
17383 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f 4e 4c */. VVA_ONL
17384 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 Y( pCur->pagesSh
17385 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20 20 uffled = 1 );.
17386 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 67 65 pgnoChild = ge
17387 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
17388 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
17389 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 61 ffset+8]);. a
1738a 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 ssert( pgnoChild
1738b 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 >0 );. assert
1738c 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70 61 67 ( pgnoChild<=pag
1738d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
1738e 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20 20 72 e->pBt) );. r
1738f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
17390 47 65 74 50 61 67 65 28 70 50 61 67 65 2d 3e 70 GetPage(pPage->p
17391 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 26 Bt, pgnoChild, &
17392 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 20 pChild, 0);.
17393 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e if( rc ) goto en
17394 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 d_shallow_balanc
17395 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 e;. if( pPage
17396 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ->pgno==1 ){.
17397 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
17398 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 43 68 treeInitPage(pCh
17399 69 6c 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ild);. if(
1739a 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 68 rc ) goto end_sh
1739b 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 allow_balance;.
1739c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 68 assert( pCh
1739d 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d ild->nOverflow==
1739e 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 0 );. if( p
1739f 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31 30 Child->nFree>=10
173a0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
173a1 54 68 65 20 63 68 69 6c 64 20 69 6e 66 6f 72 6d The child inform
173a2 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20 6f ation will fit o
173a3 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c n the root page,
173a4 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 20 20 20 so do the.
173a5 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20 20 ** copy */.
173a6 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
173a7 20 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 zeroPage(pP
173a8 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 age, pChild->aDa
173a9 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 ta[0]);.
173aa 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 69 6c for(i=0; i<pChil
173ab 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a d->nCell; i++){.
173ac 20 20 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c apCell
173ad 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 [i] = findCell(p
173ae 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20 20 Child,i);.
173af 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d 20 szCell[i] =
173b0 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 43 68 69 cellSizePtr(pChi
173b1 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b 0a ld, apCell[i]);.
173b2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
173b3 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 assemblePage(p
173b4 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e 43 Page, pChild->nC
173b5 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a 43 ell, apCell, szC
173b6 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a ell);. /*
173b7 20 43 6f 70 79 20 74 68 65 20 72 69 67 68 74 2d Copy the right-
173b8 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 pointer of the c
173b9 68 69 6c 64 20 74 6f 20 74 68 65 20 70 61 72 65 hild to the pare
173ba 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 nt. */. a
173bb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
173bc 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
173bd 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
173be 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 ;. put4by
173bf 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
173c0 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
173c1 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 20 20 20 t+8], .
173c2 20 20 20 67 65 74 34 62 79 74 65 28 26 70 43 68 get4byte(&pCh
173c3 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 68 69 6c ild->aData[pChil
173c4 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 d->hdrOffset+8])
173c5 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
173c6 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 freePage(pChild)
173c7 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 ;. TRACE(
173c8 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64 ("BALANCE: child
173c9 20 25 64 20 74 72 61 6e 73 66 65 72 20 74 6f 20 %d transfer to
173ca 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 6c page 1\n", pChil
173cb 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 d->pgno));.
173cc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
173cd 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 68 61 73 /* The child has
173ce 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f more informatio
173cf 6e 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 n that will fit
173d0 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20 20 on the root..
173d1 20 20 20 20 20 2a 2a 20 54 68 65 20 74 72 65 65 ** The tree
173d2 20 69 73 20 61 6c 72 65 61 64 79 20 62 61 6c 61 is already bala
173d3 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69 6e nced. Do nothin
173d4 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 52 g. */. TR
173d5 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 ACE(("BALANCE: c
173d6 68 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f 74 hild %d will not
173d7 20 66 69 74 20 6f 6e 20 70 61 67 65 20 31 5c 6e fit on page 1\n
173d8 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 ", pChild->pgno)
173d9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
173da 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 else{. memc
173db 70 79 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c py(pPage->aData,
173dc 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c 20 pChild->aData,
173dd 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
173de 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 leSize);. p
173df 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
173e0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
173e1 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 ite3BtreeInitPag
173e2 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
173e3 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
173e4 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 66 TE_OK );. f
173e5 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b reePage(pChild);
173e6 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 42 . TRACE(("B
173e7 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66 65 72 ALANCE: transfer
173e8 20 63 68 69 6c 64 20 25 64 20 69 6e 74 6f 20 72 child %d into r
173e9 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 oot %d\n",.
173ea 20 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d pChild-
173eb 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70 67 >pgno, pPage->pg
173ec 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 no));. }.
173ed 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
173ee 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 23 Overflow==0 );.#
173ef 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
173f0 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
173f1 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
173f2 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 UM && rc==SQLITE
173f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
173f4 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 = setChildPtrmap
173f5 73 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a s(pPage);. }.
173f6 23 65 6e 64 69 66 0a 20 20 20 20 72 65 6c 65 61 #endif. relea
173f7 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a sePage(pChild);.
173f8 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f }.end_shallow_
173f9 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71 6c 69 74 balance:. sqlit
173fa 65 33 5f 66 72 65 65 28 61 70 43 65 6c 6c 29 3b e3_free(apCell);
173fb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
173fc 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 ../*.** The root
173fd 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c page is overful
173fe 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 l.**.** When thi
173ff 73 20 68 61 70 70 65 6e 73 2c 20 43 72 65 61 74 s happens, Creat
17400 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 e a new child pa
17401 67 65 20 61 6e 64 20 63 6f 70 79 20 74 68 65 0a ge and copy the.
17402 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 ** contents of t
17403 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20 74 68 65 he root into the
17404 20 63 68 69 6c 64 2e 20 20 54 68 65 6e 20 6d 61 child. Then ma
17405 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 ke the root.** p
17406 61 67 65 20 61 6e 20 65 6d 70 74 79 20 70 61 67 age an empty pag
17407 65 20 77 69 74 68 20 72 69 67 68 74 43 68 69 6c e with rightChil
17408 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 d pointing to th
17409 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c 64 2e 20 e new.** child.
1740a 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61 6c 6c 20 Finally, call
1740b 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72 6e 61 6c balance_internal
1740c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 63 68 () on the new ch
1740d 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75 73 65 20 ild.** to cause
1740e 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a 2a 2f 0a it to split..*/.
1740f 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
17410 63 65 5f 64 65 65 70 65 72 28 42 74 43 75 72 73 ce_deeper(BtCurs
17411 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 or *pCur){. int
17412 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
17413 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 /* Return value
17414 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 from subprocedu
17415 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 res */. MemPage
17416 20 2a 70 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 *pPage; /*
17417 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 Pointer to the r
17418 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65 oot page */. Me
17419 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20 mPage *pChild;
1741a 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
1741b 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 a new child page
1741c 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 */. Pgno pgnoC
1741d 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61 67 hild; /* Pag
1741e 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
1741f 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a new child page *
17420 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
17421 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 t; /* Th
17422 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e 74 e BTree */. int
17423 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 usableSize;
17424 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c 65 /* Total usable
17425 20 73 69 7a 65 20 6f 66 20 61 20 70 61 67 65 20 size of a page
17426 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 */. u8 *data;
17427 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 /* Cont
17428 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 65 6e ent of the paren
17429 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a t page */. u8 *
1742a 63 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 cdata;
1742b 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 /* Content of th
1742c 65 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a e child page */.
1742d 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 int hdr;
1742e 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1742f 74 6f 20 70 61 67 65 20 68 65 61 64 65 72 20 69 to page header i
17430 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e n parent */. in
17431 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20 t cbrk;
17432 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 /* Offset to c
17433 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20 ontent of first
17434 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20 2a cell in parent *
17435 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 /.. assert( pCu
17436 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20 r->iPage==0 );.
17437 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
17438 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65 72 66 pPage[0]->nOverf
17439 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56 56 41 5f low>0 );.. VVA_
1743a 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 ONLY( pCur->page
1743b 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b sShuffled = 1 );
1743c 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
1743d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 70 42 >apPage[0];. pB
1743e 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
1743f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17440 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
17441 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
17442 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
17443 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
17444 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
17445 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 . rc = allocate
17446 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
17447 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 68 69 pChild, &pgnoChi
17448 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c ld, pPage->pgno,
17449 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 0);. if( rc )
1744a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73 return rc;. ass
1744b 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
1744c 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68 rIswriteable(pCh
1744d 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b ild->pDbPage) );
1744e 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 . usableSize =
1744f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
17450 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d . data = pPage-
17451 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 >aData;. hdr =
17452 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
17453 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62 ;. cbrk = get2b
17454 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
17455 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70 43 68 );. cdata = pCh
17456 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20 6d 65 ild->aData;. me
17457 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64 61 74 mcpy(cdata, &dat
17458 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d 3e 63 a[hdr], pPage->c
17459 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50 61 67 ellOffset+2*pPag
1745a 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b 0a 20 e->nCell-hdr);.
1745b 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61 5b 63 memcpy(&cdata[c
1745c 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b brk], &data[cbrk
1745d 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 62 ], usableSize-cb
1745e 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rk);.. assert(
1745f 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69 74 3d 3d pChild->isInit==
17460 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 0 );. rc = sqli
17461 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 te3BtreeInitPage
17462 28 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20 (pChild);. if(
17463 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17464 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d . int nCopy =
17465 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
17466 77 2a 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e w*sizeof(pPage->
17467 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20 20 20 6d aOvfl[0]);. m
17468 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f emcpy(pChild->aO
17469 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 4f 76 66 vfl, pPage->aOvf
1746a 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 70 l, nCopy);. p
1746b 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 Child->nOverflow
1746c 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 = pPage->nOverf
1746d 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70 43 68 low;. if( pCh
1746e 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 ild->nOverflow )
1746f 7b 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e {. pChild->
17470 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 7d nFree = 0;. }
17471 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 68 . assert( pCh
17472 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 50 61 67 ild->nCell==pPag
17473 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 e->nCell );.
17474 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
17475 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
17476 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
17477 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 );. zeroPage(
17478 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 pPage, pChild->a
17479 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c Data[0] & ~PTF_L
1747a 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34 62 79 EAF);. put4by
1747b 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
1747c 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
1747d 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 t+8], pgnoChild)
1747e 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 42 41 ;. TRACE(("BA
1747f 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 LANCE: copy root
17480 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 %d into %d\n",
17481 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 43 68 pPage->pgno, pCh
17482 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 ild->pgno));.
17483 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
17484 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 M ){. rc =
17485 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 ptrmapPut(pBt, p
17486 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 54 52 Child->pgno, PTR
17487 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 67 65 MAP_BTREE, pPage
17488 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 65 66 ->pgno);.#ifndef
17489 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1748a 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 OVACUUM. if
1748b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1748c 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1748d 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
1748e 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 7d pChild);. }
1748f 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b . if( rc ){
17490 0a 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d . pChild-
17491 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a >nOverflow = 0;.
17492 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
17493 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
17494 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17495 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 . pCur->iPage
17496 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 ++;. pCur->ap
17497 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69 6c 64 Page[1] = pChild
17498 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 ;. pCur->aiId
17499 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 72 63 x[0] = 0;. rc
1749a 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f = balance_nonro
1749b 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 ot(pCur);. }els
1749c 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 e{. releasePa
1749d 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 7d 0a ge(pChild);. }.
1749e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1749f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 ./*.** The page
174a0 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65 6e that pCur curren
174a1 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 tly points to ha
174a2 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64 69 s just been modi
174a3 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 fied in.** some
174a4 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 way. This functi
174a5 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20 69 on figures out i
174a6 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61 74 f this modificat
174a7 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a ion means the.**
174a8 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20 62 tree needs to b
174a9 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64 20 e balanced, and
174aa 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65 20 if so calls the
174ab 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c 61 appropriate bala
174ac 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 6e ncing .** routin
174ad 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61 6d 65 e..** .** Parame
174ae 74 65 72 20 69 73 49 6e 73 65 72 74 20 69 73 20 ter isInsert is
174af 74 72 75 65 20 69 66 20 61 20 6e 65 77 20 63 65 true if a new ce
174b0 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73 65 ll was just inse
174b1 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a 2a rted into the.**
174b2 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73 65 20 page, or false
174b3 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 otherwise..*/.st
174b4 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 atic int balance
174b5 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
174b6 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29 7b 0a int isInsert){.
174b7 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
174b8 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 E_OK;. MemPage
174b9 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 *pPage = pCur->a
174ba 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
174bb 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 e];.. assert( s
174bc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
174bd 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
174be 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 43 tex) );. if( pC
174bf 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a ur->iPage==0 ){.
174c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
174c1 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
174c2 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
174c3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
174c4 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 K && pPage->nOve
174c5 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 20 20 rflow>0 ){.
174c6 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 65 rc = balance_de
174c7 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20 20 20 eper(pCur);.
174c8 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
174c9 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50 61 67 65 apPage[0]==pPage
174ca 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
174cb 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
174cc 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d 53 51 4c ow==0 || rc!=SQL
174cd 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a ITE_OK );. }.
174ce 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
174cf 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e TE_OK && pPage->
174d0 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 nCell==0 ){.
174d1 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 73 rc = balance_s
174d2 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 29 3b 0a hallower(pCur);.
174d3 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
174d4 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70 ur->apPage[0]==p
174d5 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 61 73 Page );. as
174d6 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
174d7 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 erflow==0 || rc!
174d8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
174d9 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
174da 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 if( pPage->nOve
174db 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 20 20 rflow>0 || .
174dc 20 20 20 20 28 21 69 73 49 6e 73 65 72 74 20 26 (!isInsert &
174dd 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e 70 & pPage->nFree>p
174de 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
174df 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a 20 20 eSize*2/3) ){.
174e0 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 rc = balance
174e1 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29 3b 0a _nonroot(pCur);.
174e2 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
174e3 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
174e4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
174e5 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 cks all cursors
174e6 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 61 that point to ta
174e7 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a ble pgnoRoot..**
174e8 20 49 66 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 If any of those
174e9 20 63 75 72 73 6f 72 73 20 77 65 72 65 20 6f 70 cursors were op
174ea 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 ened with wrFlag
174eb 3d 3d 30 20 69 6e 20 61 20 64 69 66 66 65 72 65 ==0 in a differe
174ec 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 nt.** database c
174ed 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 61 74 onnection (a dat
174ee 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
174ef 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65 that shares the
174f0 20 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 65 20 pager.** cache
174f1 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 with the current
174f2 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 6e 64 connection) and
174f3 20 74 68 61 74 20 6f 74 68 65 72 20 63 6f 6e 6e that other conn
174f4 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 6e 6f ection .** is no
174f5 74 20 69 6e 20 74 68 65 20 52 65 61 64 55 6e 63 t in the ReadUnc
174f6 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 65 2c ommmitted state,
174f7 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
174f8 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 ne returns .** S
174f9 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2a QLITE_LOCKED..**
174fa 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 63 .** As well as c
174fb 75 72 73 6f 72 73 20 77 69 74 68 20 77 72 46 6c ursors with wrFl
174fc 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73 20 77 ag==0, cursors w
174fd 69 74 68 20 0a 2a 2a 20 69 73 49 6e 63 72 62 6c ith .** isIncrbl
174fe 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72 65 20 obHandle==1 are
174ff 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 20 also considered
17500 27 72 65 61 64 27 20 63 75 72 73 6f 72 73 20 62 'read' cursors b
17501 65 63 61 75 73 65 0a 2a 2a 20 69 6e 63 72 65 6d ecause.** increm
17502 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f ental blob curso
17503 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 rs are used for
17504 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 both reading and
17505 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 writing..**.**
17506 57 68 65 6e 20 70 67 6e 6f 52 6f 6f 74 20 69 73 When pgnoRoot is
17507 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
17508 66 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c f an intkey tabl
17509 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e e, this function
1750a 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 70 is also.** resp
1750b 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76 61 onsible for inva
1750c 6c 69 64 61 74 69 6e 67 20 69 6e 63 72 65 6d 65 lidating increme
1750d 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 ntal blob cursor
1750e 73 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 s when the table
1750f 20 72 6f 77 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 row.** on which
17510 20 74 68 65 79 20 61 72 65 20 6f 70 65 6e 65 64 they are opened
17511 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20 6d is deleted or m
17512 6f 64 69 66 69 65 64 2e 20 43 75 72 73 6f 72 73 odified. Cursors
17513 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 are invalidated
17514 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74 6f .** according to
17515 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 the following r
17516 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 ules:.**.** 1)
17517 20 57 68 65 6e 20 42 74 72 65 65 43 6c 65 61 72 When BtreeClear
17518 54 61 62 6c 65 28 29 20 69 73 20 63 61 6c 6c 65 Table() is calle
17519 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 d to completely
1751a 64 65 6c 65 74 65 20 74 68 65 20 63 6f 6e 74 65 delete the conte
1751b 6e 74 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61 nts.** of a
1751c 20 42 2d 54 72 65 65 20 74 61 62 6c 65 2c 20 70 B-Tree table, p
1751d 45 78 63 6c 75 64 65 20 69 73 20 73 65 74 20 74 Exclude is set t
1751e 6f 20 7a 65 72 6f 20 61 6e 64 20 70 61 72 61 6d o zero and param
1751f 65 74 65 72 20 69 52 6f 77 20 69 73 20 0a 2a 2a eter iRow is .**
17520 20 20 20 20 20 20 73 65 74 20 74 6f 20 6e 6f 6e set to non
17521 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63 -zero. In this c
17522 61 73 65 20 61 6c 6c 20 69 6e 63 72 65 6d 65 6e ase all incremen
17523 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 tal blob cursors
17524 20 6f 70 65 6e 0a 2a 2a 20 20 20 20 20 20 6f 6e open.** on
17525 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 the table roote
17526 64 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 61 72 d at pgnoRoot ar
17527 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a e invalidated..*
17528 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 42 *.** 2) When B
17529 74 72 65 65 49 6e 73 65 72 74 28 29 2c 20 42 74 treeInsert(), Bt
1752a 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 reeDelete() or B
1752b 74 72 65 65 50 75 74 44 61 74 61 28 29 20 69 73 treePutData() is
1752c 20 63 61 6c 6c 65 64 20 74 6f 20 0a 2a 2a 20 20 called to .**
1752d 20 20 20 20 6d 6f 64 69 66 79 20 61 20 74 61 62 modify a tab
1752e 6c 65 20 72 6f 77 20 76 69 61 20 61 6e 20 53 51 le row via an SQ
1752f 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 45 78 L statement, pEx
17530 63 6c 75 64 65 20 69 73 20 73 65 74 20 74 6f 20 clude is set to
17531 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 77 72 69 the .** wri
17532 74 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 te cursor used t
17533 6f 20 64 6f 20 74 68 65 20 6d 6f 64 69 66 69 63 o do the modific
17534 61 74 69 6f 6e 20 61 6e 64 20 70 61 72 61 6d 65 ation and parame
17535 74 65 72 20 69 52 6f 77 20 69 73 20 73 65 74 0a ter iRow is set.
17536 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 69 ** to the i
17537 6e 74 65 67 65 72 20 72 6f 77 20 69 64 20 6f 66 nteger row id of
17538 20 74 68 65 20 42 2d 54 72 65 65 20 65 6e 74 72 the B-Tree entr
17539 79 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 y being modified
1753a 2e 20 55 6e 6c 65 73 73 0a 2a 2a 20 20 20 20 20 . Unless.**
1753b 20 70 45 78 63 6c 75 64 65 20 69 73 20 69 74 73 pExclude is its
1753c 65 6c 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 elf an increment
1753d 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 2c 20 al blob cursor,
1753e 74 68 65 6e 20 61 6c 6c 20 69 6e 63 72 65 6d 65 then all increme
1753f 6e 74 61 6c 0a 2a 2a 20 20 20 20 20 20 62 6c 6f ntal.** blo
17540 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f b cursors open o
17541 6e 20 72 6f 77 20 69 52 6f 77 20 6f 66 20 74 68 n row iRow of th
17542 65 20 42 2d 54 72 65 65 20 61 72 65 20 69 6e 76 e B-Tree are inv
17543 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 alidated..**.**
17544 20 20 33 29 20 49 66 20 62 6f 74 68 20 70 45 78 3) If both pEx
17545 63 6c 75 64 65 20 61 6e 64 20 69 52 6f 77 20 61 clude and iRow a
17546 72 65 20 73 65 74 20 74 6f 20 7a 65 72 6f 2c 20 re set to zero,
17547 6e 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 no incremental b
17548 6c 6f 62 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 lob .** cur
17549 73 6f 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 sors are invalid
1754a 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ated..*/.static
1754b 69 6e 74 20 63 68 65 63 6b 46 6f 72 52 65 61 64 int checkForRead
1754c 43 6f 6e 66 6c 69 63 74 73 28 0a 20 20 42 74 72 Conflicts(. Btr
1754d 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 20 ee *pBtree,
1754e 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1754f 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68 65 base file to che
17550 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e ck */. Pgno pgn
17551 6f 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 oRoot,
17552 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 65 61 64 /* Look for read
17553 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 69 73 cursors on this
17554 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 75 btree */. BtCu
17555 72 73 6f 72 20 2a 70 45 78 63 6c 75 64 65 2c 20 rsor *pExclude,
17556 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 /* Ignore th
17557 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69 is cursor */. i
17558 36 34 20 69 52 6f 77 20 20 20 20 20 20 20 20 20 64 iRow
17559 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f /* The ro
1755a 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 20 62 wid that might b
1755b 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 29 7b e changing */.){
1755c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a . BtCursor *p;.
1755d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1755e 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 = pBtree->pBt;.
1755f 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
17560 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61 73 73 Btree->db;. ass
17561 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
17562 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 72 eHoldsMutex(pBtr
17563 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 ee) );. for(p=p
17564 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
17565 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
17566 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75 64 65 if( p==pExclude
17567 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
17568 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 if( p->pgnoRoot
17569 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63 6f 6e !=pgnoRoot ) con
1756a 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 tinue;.#ifndef S
1756b 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
1756c 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 LOB. if( p->i
1756d 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
1756e 26 26 20 28 20 0a 20 20 20 20 20 20 20 20 20 28 && ( . (
1756f 21 70 45 78 63 6c 75 64 65 20 26 26 20 69 52 6f !pExclude && iRo
17570 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70 45 78 w). || (pEx
17571 63 6c 75 64 65 20 26 26 20 21 70 45 78 63 6c 75 clude && !pExclu
17572 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 de->isIncrblobHa
17573 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66 6f 2e ndle && p->info.
17574 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20 20 20 nKey==iRow).
17575 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53 74 )){. p->eSt
17576 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
17577 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 ALID;. }.#end
17578 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 53 if. if( p->eS
17579 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
1757a 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 ID ) continue;.
1757b 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c 61 67 if( p->wrFlag
1757c 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53 51 4c ==0 .#ifndef SQL
1757d 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1757e 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69 73 49 B. || p->isI
1757f 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a 23 65 ncrblobHandle.#e
17580 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 ndif. ){.
17581 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f 74 68 sqlite3 *dbOth
17582 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65 2d 3e er = p->pBtree->
17583 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 db;. assert
17584 28 64 62 4f 74 68 65 72 29 3b 0a 20 20 20 20 20 (dbOther);.
17585 20 69 66 28 20 64 62 4f 74 68 65 72 21 3d 64 62 if( dbOther!=db
17586 20 26 26 20 28 64 62 4f 74 68 65 72 2d 3e 66 6c && (dbOther->fl
17587 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 ags & SQLITE_Rea
17588 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 3d 3d 30 dUncommitted)==0
17589 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1758a 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f te3ConnectionBlo
1758b 63 6b 65 64 28 64 62 2c 20 64 62 4f 74 68 65 72 cked(db, dbOther
1758c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1758d 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f n SQLITE_LOCKED_
1758e 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 SHAREDCACHE;.
1758f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
17590 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
17591 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 K;.}../*.** Inse
17592 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 rt a new record
17593 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 into the BTree.
17594 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 The key is give
17595 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 n by (pKey,nKey)
17596 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 .** and the data
17597 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 is given by (pD
17598 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 ata,nData). The
17599 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 cursor is used
1759a 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e only to.** defin
1759b 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65 e what table the
1759c 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 record should b
1759d 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e e inserted into.
1759e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 The cursor.**
1759f 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
175a0 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 at a random loc
175a1 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ation..**.** For
175a2 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 an INTKEY table
175a3 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 , only the nKey
175a4 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 value of the key
175a5 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20 is used. pKey
175a6 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 is.** ignored.
175a7 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74 For a ZERODATA t
175a8 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20 able, the pData
175a9 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f and nData are bo
175aa 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a th ignored..**.*
175ab 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 * If the seekRes
175ac 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 ult parameter is
175ad 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
175ae 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c a successful cal
175af 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 42 l to.** sqlite3B
175b0 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 74 6f 20 treeMoveto() to
175b1 73 65 65 6b 20 63 75 72 73 6f 72 20 70 43 75 72 seek cursor pCur
175b2 20 74 6f 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 to (pKey, nKey)
175b3 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 has already.**
175b4 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 been performed.
175b5 73 65 65 6b 52 65 73 75 6c 74 20 69 73 20 74 68 seekResult is th
175b6 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 20 e search result
175b7 72 65 74 75 72 6e 65 64 20 28 61 20 6e 65 67 61 returned (a nega
175b8 74 69 76 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 tive.** number i
175b9 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74 f pCur points at
175ba 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 69 an entry that i
175bb 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 s smaller than (
175bc 70 4b 65 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a pKey, nKey), or.
175bd 2a 2a 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 ** a positive va
175be 6c 75 65 20 69 66 20 70 43 75 72 20 70 6f 69 6e lue if pCur poin
175bf 74 73 20 61 74 20 61 6e 20 65 74 72 79 20 74 68 ts at an etry th
175c0 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 at is larger tha
175c1 6e 20 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 n .** (pKey, nKe
175c2 79 29 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 y)). .**.** If t
175c3 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 he seekResult pa
175c4 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 rameter is 0, th
175c5 65 6e 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d en cursor pCur m
175c6 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 20 ay point to any
175c7 0a 2a 2a 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 .** entry or to
175c8 6e 6f 20 65 6e 74 72 79 20 61 74 20 61 6c 6c 2e no entry at all.
175c9 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
175ca 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 is function has
175cb 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68 65 20 63 to seek.** the c
175cc 75 72 73 6f 72 20 62 65 66 6f 72 65 20 74 68 65 ursor before the
175cd 20 6e 65 77 20 6b 65 79 20 63 61 6e 20 62 65 20 new key can be
175ce 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c inserted..*/.SQL
175cf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
175d0 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 sqlite3BtreeInse
175d1 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a rt(. BtCursor *
175d2 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 pCur,
175d3 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 64 /* Insert d
175d4 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61 62 ata into the tab
175d5 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f le of this curso
175d6 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 r */. const voi
175d7 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 d *pKey, i64 nKe
175d8 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 y, /* The key
175d9 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f of the new reco
175da 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f rd */. const vo
175db 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e id *pData, int n
175dc 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 61 Data, /* The da
175dd 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 ta of the new re
175de 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a cord */. int nZ
175df 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 ero,
175e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
175e1 65 72 20 6f 66 20 65 78 74 72 61 20 30 20 62 79 er of extra 0 by
175e2 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f tes to append to
175e3 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 61 data */. int a
175e4 70 70 65 6e 64 42 69 61 73 2c 20 20 20 20 20 20 ppendBias,
175e5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
175e6 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b e if this is lik
175e7 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f ely an append */
175e8 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c . int seekResul
175e9 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
175ea 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 /* Result of p
175eb 72 69 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 rior sqlite3Btre
175ec 65 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 2a eMoveto() call *
175ed 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
175ee 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52 int loc = seekR
175ef 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 73 7a 4e esult;. int szN
175f0 65 77 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 ew;. int idx;.
175f1 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
175f2 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 . Btree *p = pC
175f3 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 ur->pBtree;. Bt
175f4 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
175f5 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 >pBt;. unsigned
175f6 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a char *oldCell;.
175f7 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
175f8 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 *newCell = 0;..
175f9 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
175fa 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
175fb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
175fc 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
175fd 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
175fe 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e assert( !pBt->
175ff 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 readOnly );. as
17600 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c sert( pCur->wrFl
17601 61 67 20 29 3b 0a 20 20 72 63 20 3d 20 63 68 65 ag );. rc = che
17602 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 ckForReadConflic
17603 74 73 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2c ts(pCur->pBtree,
17604 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
17605 20 70 43 75 72 2c 20 6e 4b 65 79 29 3b 0a 20 20 pCur, nKey);.
17606 69 66 28 20 72 63 20 29 7b 20 20 20 20 20 20 20 if( rc ){
17607 20 20 20 20 20 20 0a 20 20 20 20 2f 2a 20 54 68 . /* Th
17608 65 20 74 61 62 6c 65 20 70 43 75 72 20 70 6f 69 e table pCur poi
17609 6e 74 73 20 74 6f 20 68 61 73 20 61 20 72 65 61 nts to has a rea
1760a 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 61 73 d lock */. as
1760b 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1760c 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
1760d 43 48 45 20 29 3b 0a 20 20 20 20 72 65 74 75 72 CHE );. retur
1760e 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 n rc;. }. if(
1760f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
17610 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 RSOR_FAULT ){.
17611 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 return pCur->s
17612 6b 69 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 kip;. }.. /* S
17613 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e ave the position
17614 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 63 s of any other c
17615 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 ursors open on t
17616 68 69 73 20 74 61 62 6c 65 2e 0a 20 20 2a 2a 0a his table.. **.
17617 20 20 2a 2a 20 49 6e 20 73 6f 6d 65 20 63 61 73 ** In some cas
17618 65 73 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 es, the call to
17619 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1761a 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20 to() below is a
1761b 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 no-op. For. **
1761c 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e example, when in
1761d 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74 serting data int
1761e 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61 o a table with a
1761f 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e uto-generated in
17620 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c teger. ** keys,
17621 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20 the VDBE layer
17622 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42 invokes sqlite3B
17623 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69 treeLast() to fi
17624 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20 gure out the .
17625 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74 ** integer key t
17626 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63 o use. It then c
17627 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 alls this functi
17628 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69 on to actually i
17629 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20 nsert the . **
1762a 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e data into the in
1762b 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20 tkey B-Tree. In
1762c 74 68 69 73 20 63 61 73 65 20 73 71 6c 69 74 65 this case sqlite
1762d 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 72 3BtreeMoveto() r
1762e 65 63 6f 67 6e 69 7a 65 73 0a 20 20 2a 2a 20 74 ecognizes. ** t
1762f 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 hat the cursor i
17630 73 20 61 6c 72 65 61 64 79 20 77 68 65 72 65 20 s already where
17631 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 it needs to be a
17632 6e 64 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f nd returns witho
17633 75 74 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 6e ut. ** doing an
17634 79 20 77 6f 72 6b 2e 20 54 6f 20 61 76 6f 69 64 y work. To avoid
17635 20 74 68 77 61 72 74 69 6e 67 20 74 68 65 73 65 thwarting these
17636 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2c 20 optimizations,
17637 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 0a it is important.
17638 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 63 6c 65 61 ** not to clea
17639 72 20 74 68 65 20 63 75 72 73 6f 72 20 68 65 72 r the cursor her
1763a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 0a 20 20 e.. */. if(.
1763b 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 SQLITE_OK!=(rc
1763c 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 = saveAllCursor
1763d 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e s(pBt, pCur->pgn
1763e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 20 7c 7c oRoot, pCur)) ||
1763f 20 28 21 6c 6f 63 20 26 26 0a 20 20 20 20 53 51 (!loc &&. SQ
17640 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 LITE_OK!=(rc = s
17641 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
17642 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e 4b o(pCur, pKey, nK
17643 65 79 2c 20 61 70 70 65 6e 64 42 69 61 73 2c 20 ey, appendBias,
17644 26 6c 6f 63 29 29 0a 20 20 29 29 7b 0a 20 20 20 &loc)). )){.
17645 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
17646 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
17647 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
17648 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 age];. assert(
17649 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c pPage->intKey ||
1764a 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 nKey>=0 );. as
1764b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 sert( pPage->lea
1764c 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 f || !pPage->int
1764d 4b 65 79 20 29 3b 0a 20 20 54 52 41 43 45 28 28 Key );. TRACE((
1764e 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 "INSERT: table=%
1764f 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 d nkey=%lld ndat
17650 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c a=%d page=%d %s\
17651 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 n",. pC
17652 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b ur->pgnoRoot, nK
17653 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 ey, nData, pPage
17654 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 ->pgno,.
17655 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 loc==0 ? "over
17656 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e write" : "new en
17657 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 try"));. assert
17658 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
17659 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d );. allocateTem
1765a 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e pSpace(pBt);. n
1765b 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 ewCell = pBt->pT
1765c 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e mpSpace;. if( n
1765d 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 ewCell==0 ) retu
1765e 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1765f 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 . rc = fillInCe
17660 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c ll(pPage, newCel
17661 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 l, pKey, nKey, p
17662 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 Data, nData, nZe
17663 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 ro, &szNew);. i
17664 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 f( rc ) goto end
17665 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 _insert;. asser
17666 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 t( szNew==cellSi
17667 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 zePtr(pPage, new
17668 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Cell) );. asser
17669 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c t( szNew<=MX_CEL
1766a 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 L_SIZE(pBt) );.
1766b 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 idx = pCur->aiI
1766c 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b dx[pCur->iPage];
1766d 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 26 26 . if( loc==0 &&
1766e 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 CURSOR_VALID==p
1766f 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 Cur->eState ){.
17670 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 20 u16 szOld;.
17671 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 50 assert( idx<pP
17672 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
17673 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
17674 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e gerWrite(pPage->
17675 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
17676 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67 6f ( rc ){. go
17677 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 to end_insert;.
17678 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c 6c }. oldCell
17679 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
1767a 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66 28 e, idx);. if(
1767b 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
1767c 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e 65 . memcpy(ne
1767d 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c 20 wCell, oldCell,
1767e 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4);. }. sz
1767f 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 Old = cellSizePt
17680 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c r(pPage, oldCell
17681 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 );. rc = clea
17682 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c 64 rCell(pPage, old
17683 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 Cell);. if( r
17684 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 c ) goto end_ins
17685 65 72 74 3b 0a 20 20 20 20 72 63 20 3d 20 64 72 ert;. rc = dr
17686 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 opCell(pPage, id
17687 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 20 20 20 69 x, szOld);. i
17688 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
17689 20 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ) {. goto
1768a 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 end_insert;.
1768b 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f }. }else if( lo
1768c 63 3c 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 c<0 && pPage->nC
1768d 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 ell>0 ){. ass
1768e 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 ert( pPage->leaf
1768f 20 29 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b );. idx = ++
17690 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
17691 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 70 43 ->iPage];. pC
17692 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
17693 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 0;. pCur->va
17694 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d lidNKey = 0;. }
17695 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
17696 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b ( pPage->leaf );
17697 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e 73 65 . }. rc = inse
17698 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 rtCell(pPage, id
17699 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e 65 x, newCell, szNe
1769a 77 2c 20 30 2c 20 30 29 3b 0a 20 20 61 73 73 65 w, 0, 0);. asse
1769b 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
1769c 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 43 65 6c K || pPage->nCel
1769d 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f l>0 || pPage->nO
1769e 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 verflow>0 );..
1769f 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 68 /* If no error h
176a0 61 73 20 6f 63 63 75 72 65 64 2c 20 63 61 6c 6c as occured, call
176a1 20 62 61 6c 61 6e 63 65 28 29 20 74 6f 20 64 65 balance() to de
176a2 61 6c 20 77 69 74 68 20 61 6e 79 20 6f 76 65 72 al with any over
176a3 66 6c 6f 77 20 61 6e 64 0a 20 20 2a 2a 20 6d 6f flow and. ** mo
176a4 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
176a5 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 72 6f point at the ro
176a6 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ot of the table
176a7 28 73 69 6e 63 65 20 62 61 6c 61 6e 63 65 20 6d (since balance m
176a8 61 79 0a 20 20 2a 2a 20 68 61 76 65 20 72 65 61 ay. ** have rea
176a9 72 72 61 6e 67 65 64 20 74 68 65 20 74 61 62 6c rranged the tabl
176aa 65 20 69 6e 20 73 75 63 68 20 61 20 77 61 79 20 e in such a way
176ab 61 73 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 as to invalidate
176ac 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61 67 65 BtCursor.apPage
176ad 5b 5d 0a 20 20 2a 2a 20 6f 72 20 42 74 43 75 72 []. ** or BtCur
176ae 73 6f 72 2e 61 69 49 64 78 5b 5d 29 2e 0a 20 20 sor.aiIdx[])..
176af 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 2c 20 **. ** Except,
176b0 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f if all of the fo
176b1 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 llowing are true
176b2 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 3a 0a 20 20 , do nothing:.
176b3 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 49 6e 73 65 **. ** * Inse
176b4 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20 63 65 rting the new ce
176b5 6c 6c 20 64 69 64 20 6e 6f 74 20 63 61 75 73 65 ll did not cause
176b6 20 6f 76 65 72 66 6c 6f 77 2c 0a 20 20 2a 2a 0a overflow,. **.
176b7 20 20 2a 2a 20 20 20 2a 20 42 65 66 6f 72 65 20 ** * Before
176b8 69 6e 73 65 72 74 69 6e 67 20 74 68 65 20 6e 65 inserting the ne
176b9 77 20 63 65 6c 6c 20 74 68 65 20 63 75 72 73 6f w cell the curso
176ba 72 20 77 61 73 20 70 6f 69 6e 74 69 6e 67 20 61 r was pointing a
176bb 74 20 74 68 65 20 0a 20 20 2a 2a 20 20 20 20 20 t the . **
176bc 6c 61 72 67 65 73 74 20 6b 65 79 20 69 6e 20 61 largest key in a
176bd 6e 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2c n intkey B-Tree,
176be 20 61 6e 64 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 and. **. **
176bf 20 2a 20 54 68 65 20 6b 65 79 20 76 61 6c 75 65 * The key value
176c0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
176c1 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 69 73 the new cell is
176c2 20 6e 6f 77 20 74 68 65 20 6c 61 72 67 65 73 74 now the largest
176c3 20 0a 20 20 2a 2a 20 20 20 20 20 69 6e 20 74 68 . ** in th
176c4 65 20 42 2d 54 72 65 65 2e 0a 20 20 2a 2a 0a 20 e B-Tree.. **.
176c5 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 ** In this case
176c6 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 the cursor can
176c7 62 65 20 73 61 66 65 6c 79 20 6c 65 66 74 20 70 be safely left p
176c8 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 28 ointing at the (
176c9 6e 65 77 29 20 0a 20 20 2a 2a 20 6c 61 72 67 65 new) . ** large
176ca 73 74 20 6b 65 79 20 76 61 6c 75 65 20 69 6e 20 st key value in
176cb 74 68 65 20 42 2d 54 72 65 65 2e 20 44 6f 69 6e the B-Tree. Doin
176cc 67 20 73 6f 20 73 70 65 65 64 73 20 75 70 20 69 g so speeds up i
176cd 6e 73 65 72 74 69 6e 67 20 61 20 73 65 74 0a 20 nserting a set.
176ce 20 2a 2a 20 6f 66 20 65 6e 74 72 69 65 73 20 77 ** of entries w
176cf 69 74 68 20 69 6e 63 72 65 61 73 69 6e 67 20 69 ith increasing i
176d0 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65 nteger key value
176d1 73 20 76 69 61 20 61 20 73 69 6e 67 6c 65 20 63 s via a single c
176d2 75 72 73 6f 72 0a 20 20 2a 2a 20 28 63 6f 6d 65 ursor. ** (come
176d3 73 20 75 70 20 77 69 74 68 20 22 49 4e 53 45 52 s up with "INSER
176d4 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 T INTO ... SELEC
176d5 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 T ..." statement
176d6 73 29 2c 20 61 73 20 0a 20 20 2a 2a 20 74 68 65 s), as . ** the
176d7 20 6e 65 78 74 20 69 6e 73 65 72 74 20 6f 70 65 next insert ope
176d8 72 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 ration is not re
176d9 71 75 69 72 65 64 20 74 6f 20 73 65 65 6b 20 74 quired to seek t
176da 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a he cursor.. */.
176db 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
176dc 5f 4f 4b 20 0a 20 20 20 26 26 20 28 70 50 61 67 _OK . && (pPag
176dd 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 e->nOverflow ||
176de 21 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 7c 7c !pCur->atLast ||
176df 20 6c 6f 63 3e 3d 30 20 7c 7c 20 21 70 43 75 72 loc>=0 || !pCur
176e0 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 ->apPage[0]->int
176e1 4b 65 79 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 Key). ){. rc
176e2 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c = balance(pCur,
176e3 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 1);. if( rc=
176e4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
176e5 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 moveToRoot(p
176e6 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Cur);. }. }.
176e7 20 20 0a 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b . /* Must mak
176e8 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 e sure nOverflow
176e9 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 is reset to zer
176ea 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 o even if the ba
176eb 6c 61 6e 63 65 28 29 0a 20 20 2a 2a 20 66 61 69 lance(). ** fai
176ec 6c 73 2e 20 20 49 6e 74 65 72 6e 61 6c 20 64 61 ls. Internal da
176ed 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 ta structure cor
176ee 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 ruption will res
176ef 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 2a ult otherwise. *
176f0 2f 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 /. pCur->apPage
176f1 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
176f2 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 65 Overflow = 0;..e
176f3 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74 nd_insert:. ret
176f4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
176f5 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72 Delete the entr
176f6 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f y that the curso
176f7 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
176f8 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a . The cursor.**
176f9 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
176fa 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 79 g at a arbitrary
176fb 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 location..*/.SQ
176fc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
176fd 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
176fe 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ete(BtCursor *pC
176ff 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a ur){. MemPage *
17700 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
17701 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
17702 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 ];. int idx;.
17703 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
17704 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a Cell;. int rc;.
17705 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 Pgno pgnoChild
17706 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 2a 70 = 0;. Btree *p
17707 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b = pCur->pBtree;
17708 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
17709 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 = p->pBt;.. as
1770a 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
1770b 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
1770c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1770d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 >isInit );. ass
1770e 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
1770f 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
17710 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 RITE );. assert
17711 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 ( !pBt->readOnly
17712 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e );. if( pCur->
17713 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 eState==CURSOR_F
17714 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 AULT ){. retu
17715 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 rn pCur->skip;.
17716 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 }. if( NEVER(p
17717 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
17718 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e >iPage]>=pPage->
17719 6e 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20 72 65 nCell) ){. re
1771a 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1771b 52 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f R; /* The curso
1771c 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e r is not pointin
1771d 67 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 2a 2f g to anything */
1771e 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1771f 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b 0a 20 Cur->wrFlag );.
17720 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65 rc = checkForRe
17721 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 70 adConflicts(p, p
17722 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 Cur->pgnoRoot, p
17723 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e Cur, pCur->info.
17724 6e 4b 65 79 29 3b 0a 20 20 69 66 28 20 72 63 21 nKey);. if( rc!
17725 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17726 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70 /* The table p
17727 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 Cur points to ha
17728 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f s a read lock */
17729 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d . assert( rc=
1772a 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 =SQLITE_LOCKED_S
1772b 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 HAREDCACHE );.
1772c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1772d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74 .. /* Restore t
1772e 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f he current curso
1772f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 6e 6f r position (a no
17730 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 73 6f -op if the curso
17731 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 20 2a r is not in . *
17732 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 * CURSOR_REQUIRE
17733 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e 64 20 SEEK state) and
17734 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f save the positio
17735 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 ns of any other
17736 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 6f 70 cursors . ** op
17737 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 en on the same t
17738 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c 6c 20 able. Then call
17739 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1773a 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 65 0a e() on the page.
1773b 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 65 6e ** that the en
1773c 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 try will be dele
1773d 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f 0a 20 ted from.. */.
1773e 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 3d 20 if( . (rc =
1773f 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
17740 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d 30 20 ition(pCur))!=0
17741 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 61 76 ||. (rc = sav
17742 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
17743 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
17744 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20 20 pCur))!=0 ||.
17745 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 (rc = sqlite3P
17746 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
17747 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a 20 20 >pDbPage))!=0.
17748 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
17749 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 ;. }.. /* Loca
1774a 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 te the cell with
1774b 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e 64 20 in its page and
1774c 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f 69 6e leave pCell poin
1774d 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 2a 2a ting to the. **
1774e 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 61 72 data. The clear
1774f 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 65 65 Cell() call free
17750 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 s any overflow p
17751 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ages associated
17752 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 63 65 with the. ** ce
17753 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 74 73 ll. The cell its
17754 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 6e 74 elf is still int
17755 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 78 20 act.. */. idx
17756 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 = pCur->aiIdx[pC
17757 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 70 43 ur->iPage];. pC
17758 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
17759 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 69 66 Page, idx);. if
1775a 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
1775b 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 {. pgnoChild
1775c 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c = get4byte(pCell
1775d 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c );. }. rc = cl
1775e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 earCell(pPage, p
1775f 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 63 20 Cell);. if( rc
17760 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
17761 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 ;. }.. if( !pP
17762 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
17763 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 65 /*. ** The e
17764 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 6f 75 ntry we are abou
17765 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e t to delete is n
17766 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66 20 ot a leaf so if
17767 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a 2a we do not. **
17768 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 do something we
17769 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68 6f will leave a ho
1776a 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61 le on an interna
1776b 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20 57 l page.. ** W
1776c 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20 74 e have to fill t
1776d 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69 6e he hole by movin
1776e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f 6d g in a cell from
1776f 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20 20 a leaf. The.
17770 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20 61 ** next Cell a
17771 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f 20 fter the one to
17772 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 67 75 be deleted is gu
17773 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73 aranteed to exis
17774 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f 20 t and. ** to
17775 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65 20 be a leaf so we
17776 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20 can use it..
17777 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 */. BtCursor
17778 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d 65 6d leafCur;. Mem
17779 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 65 20 Page *pLeafPage
1777a 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69 67 6e = 0;.. unsign
1777b 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74 3b 0a ed char *pNext;.
1777c 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b int notUsed;
1777d 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 . unsigned ch
1777e 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 30 ar *tempCell = 0
1777f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 ;. assert( !p
17780 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a Page->intKey );.
17781 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
17782 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 70 43 GetTempCursor(pC
17783 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a 20 ur, &leafCur);.
17784 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
17785 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75 treeNext(&leafCu
17786 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 r, ¬Used);.
17787 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
17788 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 _OK ){. ass
17789 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 69 49 ert( leafCur.aiI
1778a 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 dx[leafCur.iPage
1778b 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c ]==0 );. pL
1778c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43 75 eafPage = leafCu
1778d 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75 72 r.apPage[leafCur
1778e 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 72 .iPage];. r
1778f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
17790 57 72 69 74 65 28 70 4c 65 61 66 50 61 67 65 2d Write(pLeafPage-
17791 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 7d >pDbPage);. }
17792 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
17793 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
17794 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72 49 6e int leafCursorIn
17795 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 valid = 0;.
17796 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20 20 u16 szNext;.
17797 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45 54 TRACE(("DELET
17798 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65 E: table=%d dele
17799 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f 6d te internal from
1779a 20 25 64 20 72 65 70 6c 61 63 65 20 66 72 6f 6d %d replace from
1779b 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 leaf %d\n",.
1779c 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f pCur->pgno
1779d 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e Root, pPage->pgn
1779e 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 o, pLeafPage->pg
1779f 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72 6f 70 no));. drop
177a0 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c Cell(pPage, idx,
177a1 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
177a2 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 ge, pCell));.
177a3 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 43 pNext = findC
177a4 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30 ell(pLeafPage, 0
177a5 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20 );. szNext
177a6 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c = cellSizePtr(pL
177a7 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74 29 3b eafPage, pNext);
177a8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 4d . assert( M
177a9 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
177aa 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20 20 >=szNext+4 );.
177ab 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 allocateTemp
177ac 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 Space(pBt);.
177ad 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42 74 tempCell = pBt
177ae 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 ->pTmpSpace;.
177af 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c 6c 3d if( tempCell=
177b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
177b1 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
177b2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
177b3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
177b4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
177b5 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 insertCell(pPag
177b6 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d 34 2c e, idx, pNext-4,
177b7 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70 43 szNext+4, tempC
177b8 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d ell, 0);. }
177b9 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ... /* The
177ba 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69 "if" statement i
177bb 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64 65 20 n the next code
177bc 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69 63 61 block is critica
177bd 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a l. The. **
177be 20 73 6c 69 67 68 74 65 73 74 20 65 72 72 6f 72 slightest error
177bf 20 69 6e 20 74 68 61 74 20 73 74 61 74 65 6d 65 in that stateme
177c0 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20 53 nt would allow S
177c1 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61 74 65 QLite to operate
177c2 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63 . ** correc
177c3 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 tly most of the
177c4 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75 63 65 time but produce
177c5 20 76 65 72 79 20 72 61 72 65 20 66 61 69 6c 75 very rare failu
177c6 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20 20 2a res. To. *
177c7 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 * guard against
177c8 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 this, the follow
177c9 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c 70 20 ing macros help
177ca 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 0a 20 to verify that.
177cb 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69 66 22 ** the "if"
177cc 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 77 65 statement is we
177cd 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20 20 20 ll tested..
177ce 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 */. testca
177cf 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 se( pPage->nOver
177d0 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 flow==0 && pPage
177d1 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73 61 ->nFree<pBt->usa
177d2 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20 bleSize*2/3 .
177d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 &&
177d4 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 pLeafPage->nFre
177d5 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74 e+2+szNext > pBt
177d6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 ->usableSize*2/3
177d7 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
177d8 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 se( pPage->nOver
177d9 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 flow==0 && pPage
177da 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73 ->nFree==pBt->us
177db 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 ableSize*2/3 .
177dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
177dd 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 & pLeafPage->nFr
177de 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 ee+2+szNext > pB
177df 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
177e0 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 3 );. testc
177e1 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ase( pPage->nOve
177e2 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 rflow==0 && pPag
177e3 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 e->nFree==pBt->u
177e4 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 sableSize*2/3+1
177e5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
177e6 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e && pLeafPage->
177e7 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e nFree+2+szNext >
177e8 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
177e9 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 *2/3 );. te
177ea 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e stcase( pPage->n
177eb 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20 70 50 Overflow>0 && pP
177ec 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74 2d age->nFree<=pBt-
177ed 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 0a >usableSize*2/3.
177ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
177ef 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e && pLeafPage->n
177f0 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 Free+2+szNext >
177f1 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a pBt->usableSize*
177f2 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 2/3 );. tes
177f3 74 63 61 73 65 28 20 28 70 50 61 67 65 2d 3e 6e tcase( (pPage->n
177f4 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70 Overflow>0 || (p
177f5 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42 Page->nFree > pB
177f6 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
177f7 33 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 3)).
177f8 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 && pLeafPag
177f9 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 e->nFree+2+szNex
177fa 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 t == pBt->usable
177fb 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20 20 Size*2/3 );...
177fc 20 20 20 20 69 66 28 20 28 70 50 61 67 65 2d 3e if( (pPage->
177fd 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 nOverflow>0 || (
177fe 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 pPage->nFree > p
177ff 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
17800 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20 20 20 /3)) &&.
17801 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 (pLeafPage->nF
17802 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 ree+2+szNext > p
17803 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 Bt->usableSize*2
17804 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 /3). ){.
17805 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
17806 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69 66 20 nch is taken if
17807 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 the internal nod
17808 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65 72 20 e is now either
17809 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20 20 overflowing.
1780a 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72 66 ** or underf
1780b 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65 61 66 ull and the leaf
1780c 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 6e node will be un
1780d 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20 74 68 derfull after th
1780e 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20 20 e just cell .
1780f 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 74 ** copied t
17810 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e o the internal n
17811 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64 20 66 ode is deleted f
17812 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69 73 20 rom it. This is
17813 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 a special.
17814 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61 75 73 ** case becaus
17815 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 61 e the call to ba
17816 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72 72 65 lance() to corre
17817 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ct the internal
17818 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 node. **
17819 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 74 may change the t
1781a 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e ree structure an
1781b 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 d invalidate the
1781c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20 20 contents of.
1781d 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66 ** the leaf
1781e 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 Cur.apPage[] and
1781f 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d leafCur.aiIdx[]
17820 20 61 72 72 61 79 73 2c 20 77 68 69 63 68 20 77 arrays, which w
17821 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a ill be. *
17822 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62 61 * used by the ba
17823 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72 65 64 lance() required
17824 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 65 20 to correct the
17825 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a 20 underfull leaf.
17826 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e 0a ** node..
17827 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
17828 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c ** The formul
17829 61 20 75 73 65 64 20 69 6e 20 74 68 65 20 65 78 a used in the ex
1782a 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65 20 61 pression above a
1782b 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61 63 65 re based on face
1782c 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a ts of. **
1782d 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 the SQLite file
1782e 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64 6f 20 -format that do
1782f 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72 20 not change over
17830 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f time.. */
17831 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
17832 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d e( pPage->nFree=
17833 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 =pBt->usableSize
17834 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20 20 *2/3+1 );.
17835 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 61 testcase( pLea
17836 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 fPage->nFree+2+s
17837 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61 62 zNext==pBt->usab
17838 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a leSize*2/3+1 );.
17839 20 20 20 20 20 20 20 20 6c 65 61 66 43 75 72 73 leafCurs
1783a 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a 20 orInvalid = 1;.
1783b 20 20 20 20 20 7d 20 20 20 20 20 20 20 20 0a 0a } ..
1783c 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1783d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1783e 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
1783f 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17840 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
17841 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75 e) );. pu
17842 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 t4byte(findOverf
17843 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 lowCell(pPage, i
17844 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b dx), pgnoChild);
17845 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c . VVA_ONL
17846 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 Y( pCur->pagesSh
17847 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20 uffled = 0 );.
17848 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e rc = balan
17849 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 20 ce(pCur, 0);.
1784a 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
1784b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1784c 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61 6c leafCursorInval
1784d 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a id ){. /*
1784e 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65 20 69 The leaf-node i
1784f 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c 6c 20 s now underfull
17850 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65 65 20 and so the tree
17851 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20 needs to be .
17852 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61 6e 63 ** rebalanc
17853 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68 65 ed. However, the
17854 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65 72 61 balance() opera
17855 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 74 65 tion on the inte
17856 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 rnal. **
17857 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79 20 68 node above may h
17858 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 68 65 ave modified the
17859 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 structure of th
1785a 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20 20 20 e B-Tree and.
1785b 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65 20 63 ** so the c
1785c 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 urrent contents
1785d 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 of leafCur.apPag
1785e 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75 72 2e e[] and leafCur.
1785f 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 aiIdx[].
17860 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72 ** may not be tr
17861 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a usted.. *
17862 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20 *. ** It
17863 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
17864 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e 63 65 to copy the ance
17865 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72 2c 20 stry from pCur,
17866 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 as the same.
17867 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 28 29 ** balance()
17868 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61 6c 69 call has invali
17869 64 61 74 65 64 20 74 68 65 20 70 43 75 72 2d 3e dated the pCur->
1786a 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49 apPage[] and aiI
1786b 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a 2a 20 dx[]. **
1786c 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20 20 20 arrays. .
1786d 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 **. ** T
1786e 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76 65 43 he call to saveC
1786f 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 ursorPosition()
17870 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c 6c 79 below internally
17871 20 73 61 76 65 73 20 74 68 65 20 0a 20 20 20 20 saves the .
17872 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61 74 20 ** key that
17873 6c 65 61 66 43 75 72 20 69 73 20 63 75 72 72 65 leafCur is curre
17874 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
17875 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 . Currently, the
17876 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 72 re. ** ar
17877 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 20 e two copies of
17878 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68 65 20 that key in the
17879 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72 65 20 tree - one here
1787a 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20 20 20 on the leaf.
1787b 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e 64 20 ** page and
1787c 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e 74 65 one on some inte
1787d 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74 68 65 rnal node in the
1787e 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70 79 20 tree. The copy
1787f 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 on. ** th
17880 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20 61 e leaf node is a
17881 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74 20 6b lways the next k
17882 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64 65 72 ey in tree-order
17883 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20 20 after the .
17884 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e 20 74 ** copy on t
17885 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 he internal node
17886 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c 20 74 . So, the call t
17887 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 o sqlite3BtreeNe
17888 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 xt(). **
17889 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43 75 72 calls restoreCur
1788a 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 74 6f sorPosition() to
1788b 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72 73 6f point the curso
1788c 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a 20 20 r to the copy.
1788d 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 ** stored
1788e 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 on the internal
1788f 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76 61 6e node, then advan
17890 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ces to the next
17891 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20 20 2a entry,. *
17892 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e 73 20 * which happens
17893 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79 20 6f to be the copy o
17894 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74 68 65 f the key on the
17895 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 0a internal node..
17896 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74 20 65 ** Net e
17897 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72 20 69 ffect: leafCur i
17898 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63 6b 20 s pointing back
17899 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61 74 65 to the duplicate
1789a 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a cell. **
1789b 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 that needs to b
1789c 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64 20 74 e removed, and t
1789d 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 he leafCur.apPag
1789e 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20 20 20 e[] and.
1789f 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 ** leafCur.aiIdx
178a0 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20 63 6f [] arrays are co
178a1 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20 20 2a rrect.. *
178a2 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e /. VVA_ON
178a3 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50 67 6e LY( Pgno leafPgn
178a4 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 o = pLeafPage->p
178a5 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20 72 gno );. r
178a6 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f c = saveCursorPo
178a7 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75 72 29 sition(&leafCur)
178a8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
178a9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
178aa 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
178ab 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 26 lite3BtreeNext(&
178ac 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 65 leafCur, ¬Use
178ad 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 d);. }.
178ae 20 20 20 20 20 20 70 4c 65 61 66 50 61 67 65 20 pLeafPage
178af 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 65 = leafCur.apPage
178b0 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3b [leafCur.iPage];
178b1 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
178b2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
178b3 7c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e | pLeafPage->pgn
178b4 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b 0a 20 o==leafPgno );.
178b5 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 assert( r
178b6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
178b7 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 6c 65 leafCur.aiIdx[le
178b8 61 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30 20 afCur.iPage]==0
178b9 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
178ba 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
178bb 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 =rc. && SQ
178bc 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 LITE_OK==(rc = s
178bd 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
178be 28 70 4c 65 61 66 50 61 67 65 2d 3e 70 44 62 50 (pLeafPage->pDbP
178bf 61 67 65 29 29 20 0a 20 20 20 20 20 20 29 7b 0a age)) . ){.
178c0 20 20 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c dropCell
178c1 28 70 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73 (pLeafPage, 0, s
178c2 7a 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 zNext);.
178c3 56 56 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75 VVA_ONLY( leafCu
178c4 72 2e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 r.pagesShuffled
178c5 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 = 0 );. r
178c6 63 20 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61 c = balance(&lea
178c7 66 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 fCur, 0);.
178c8 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 assert( leafCu
178c9 72 73 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21 rsorInvalid || !
178ca 6c 65 61 66 43 75 72 2e 70 61 67 65 73 53 68 75 leafCur.pagesShu
178cb 66 66 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 ffled.
178cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
178cd 20 20 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75 || !pCu
178ce 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 r->pagesShuffled
178cf 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 );. }.
178d0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
178d1 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 eeReleaseTempCur
178d2 73 6f 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20 sor(&leafCur);.
178d3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 }else{. TRAC
178d4 45 28 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c E(("DELETE: tabl
178d5 65 3d 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d e=%d delete from
178d6 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 leaf %d\n",.
178d7 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f pCur->pgnoRo
178d8 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 ot, pPage->pgno)
178d9 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70 );. rc = drop
178da 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c Cell(pPage, idx,
178db 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
178dc 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 ge, pCell));.
178dd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
178de 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
178df 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 balance(pCur, 0
178e0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 );. }. }. i
178e1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
178e2 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f ){. moveToRo
178e3 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 ot(pCur);. }.
178e4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
178e5 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
178e6 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 BTree table. W
178e7 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 rite into *piTab
178e8 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e le the page.** n
178e9 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f umber for the ro
178ea 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e ot page of the n
178eb 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ew table..**.**
178ec 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 The type of type
178ed 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
178ee 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 y the flags para
178ef 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 meter. Only the
178f0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 .** following va
178f1 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 lues of flags ar
178f2 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
178f3 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 se. Other value
178f4 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d s for.** flags m
178f5 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a ight not work:.*
178f6 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 *.** BTREE_I
178f7 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 NTKEY|BTREE_LEAF
178f8 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f DATA Used fo
178f9 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 r SQL tables wit
178fa 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 h rowid keys.**
178fb 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 BTREE_ZERODA
178fc 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TA
178fd 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c Used for SQL
178fe 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 indices.*/.stat
178ff 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 ic int btreeCrea
17900 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 teTable(Btree *p
17901 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 , int *piTable,
17902 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 int flags){. Bt
17903 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
17904 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 >pBt;. MemPage
17905 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 *pRoot;. Pgno p
17906 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 gnoRoot;. int r
17907 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 c;.. assert( sq
17908 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
17909 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 utex(p) );. ass
1790a 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
1790b 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
1790c 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 RITE );. assert
1790d 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 ( !pBt->readOnly
1790e 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );..#ifdef SQLI
1790f 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
17910 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 UM. rc = alloca
17911 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
17912 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f &pRoot, &pgnoRo
17913 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 ot, 1, 0);. if(
17914 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 rc ){. retur
17915 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a n rc;. }.#else.
17916 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
17917 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e acuum ){. Pgn
17918 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 o pgnoMove;
17919 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 /* Move a page
1791a 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f here to make roo
1791b 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 m for the root-p
1791c 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 age */. MemPa
1791d 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f ge *pPageMove; /
1791e 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f * The page to mo
1791f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f ve to. */.. /
17920 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 * Creating a new
17921 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 table may proba
17922 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 bly require movi
17923 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 ng an existing d
17924 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 atabase. ** t
17925 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 o make room for
17926 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 the new tables r
17927 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 oot page. In cas
17928 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e e this page turn
17929 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 s. ** out to
1792a 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 be an overflow p
1792b 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 age, delete all
1792c 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 overflow page-ma
1792d 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 p caches. **
1792e 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 held by open cur
1792f 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 sors.. */.
17930 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 invalidateAllOv
17931 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 erflowCache(pBt)
17932 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 ;.. /* Read t
17933 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 he value of meta
17934 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 [3] from the dat
17935 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 abase to determi
17936 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 ne where the.
17937 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 ** root page of
17938 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 the new table s
17939 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 hould go. meta[3
1793a 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 ] is the largest
1793b 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a root-page. *
1793c 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 * created so far
1793d 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f , so the new roo
1793e 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b t-page is (meta[
1793f 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 3]+1).. */.
17940 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
17941 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c reeGetMeta(p, 4,
17942 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 &pgnoRoot);.
17943 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17944 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
17945 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
17946 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 pgnoRoot++;..
17947 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f /* The new roo
17948 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 t-page may not b
17949 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 e allocated on a
1794a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
1794b 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a e, or the. **
1794c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 PENDING_BYTE pa
1794d 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 ge.. */. w
1794e 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d hile( pgnoRoot==
1794f 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 PTRMAP_PAGENO(pB
17950 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a t, pgnoRoot) ||.
17951 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 pgnoRoot
17952 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
17953 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
17954 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 pgnoRoot++;.
17955 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
17956 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a pgnoRoot>=3 );..
17957 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 /* Allocate
17958 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 a page. The page
17959 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 that currently
1795a 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 resides at pgnoR
1795b 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 oot will. **
1795c 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 be moved to the
1795d 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 allocated page (
1795e 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 unless the alloc
1795f 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e ated page happen
17960 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 s. ** to resi
17961 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e de at pgnoRoot).
17962 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
17963 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
17964 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f ge(pBt, &pPageMo
17965 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 ve, &pgnoMove, p
17966 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 gnoRoot, 1);.
17967 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17968 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
17969 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
1796a 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d if( pgnoMove!=
1796b 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 pgnoRoot ){.
1796c 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 /* pgnoRoot is
1796d 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77 the page that w
1796e 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 ill be used for
1796f 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 the root-page of
17970 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 . ** the ne
17971 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e w table (assumin
17972 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e g an error did n
17973 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 ot occur). But w
17974 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 e were. **
17975 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f allocated pgnoMo
17976 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 ve. If required
17977 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 (i.e. if it was
17978 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 not allocated.
17979 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 ** by extend
1797a 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 ing the file), t
1797b 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 he current page
1797c 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f at position pgno
1797d 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 Move. ** is
1797e 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c already journal
1797f 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
17980 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 u8 eType;.
17981 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 Pgno iPtrPage
17982 3b 0a 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ;.. release
17983 50 61 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b Page(pPageMove);
17984 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 .. /* Move
17985 74 68 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 the page current
17986 6c 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 ly at pgnoRoot t
17987 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 o pgnoMove. */.
17988 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
17989 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 3BtreeGetPage(pB
1798a 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 t, pgnoRoot, &pR
1798b 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 oot, 0);. i
1798c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1798d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
1798e 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
1798f 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
17990 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f Get(pBt, pgnoRoo
17991 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 t, &eType, &iPtr
17992 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
17993 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
17994 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
17995 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 ROOTPAGE || eTyp
17996 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 e==PTRMAP_FREEPA
17997 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 GE ){. re
17998 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 leasePage(pRoot)
17999 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
1799a 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
1799b 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 assert( eType
1799c 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 !=PTRMAP_ROOTPAG
1799d 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 E );. asser
1799e 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 t( eType!=PTRMAP
1799f 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20 _FREEPAGE );.
179a0 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 rc = relocate
179a1 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c Page(pBt, pRoot,
179a2 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 eType, iPtrPage
179a3 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a , pgnoMove, 0);.
179a4 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
179a5 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20 e(pRoot);..
179a6 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 /* Obtain the p
179a7 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 age at pgnoRoot
179a8 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 */. if( rc!
179a9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
179aa 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
179ab 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
179ac 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
179ad 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e GetPage(pBt, pgn
179ae 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 oRoot, &pRoot, 0
179af 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
179b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
179b1 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
179b2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
179b3 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
179b4 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 Write(pRoot->pDb
179b5 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
179b6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
179b7 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 {. releas
179b8 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 ePage(pRoot);.
179b9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
179ba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
179bb 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 se{. pRoot
179bc 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 = pPageMove;.
179bd 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 } .. /* Upda
179be 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d te the pointer-m
179bf 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 ap and meta-data
179c0 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f with the new ro
179c1 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 ot-page number.
179c2 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d */. rc = ptrm
179c3 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 apPut(pBt, pgnoR
179c4 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 oot, PTRMAP_ROOT
179c5 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 PAGE, 0);. if
179c6 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
179c7 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 leasePage(pRoot)
179c8 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
179c9 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 c;. }. rc
179ca 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 = sqlite3BtreeUp
179cb 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 dateMeta(p, 4, p
179cc 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 gnoRoot);. if
179cd 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
179ce 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 leasePage(pRoot)
179cf 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
179d0 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 c;. }.. }els
179d1 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f e{. rc = allo
179d2 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
179d3 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f t, &pRoot, &pgno
179d4 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 Root, 1, 0);.
179d5 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
179d6 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a rc;. }.#endif.
179d7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
179d8 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
179d9 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 e(pRoot->pDbPage
179da 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 ) );. zeroPage(
179db 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 pRoot, flags | P
179dc 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 TF_LEAF);. sqli
179dd 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 te3PagerUnref(pR
179de 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 oot->pDbPage);.
179df 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 *piTable = (int
179e0 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 )pgnoRoot;. ret
179e1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
179e2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
179e3 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
179e4 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 CreateTable(Btre
179e5 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 e *p, int *piTab
179e6 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a le, int flags){.
179e7 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 int rc;. sqli
179e8 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
179e9 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 ;. rc = btreeCr
179ea 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 eateTable(p, piT
179eb 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 able, flags);.
179ec 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
179ed 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
179ee 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 c;.}../*.** Eras
179ef 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 e the given data
179f0 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c base page and al
179f1 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 l its children.
179f2 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 Return.** the p
179f3 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c age to the freel
179f4 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ist..*/.static i
179f5 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 nt clearDatabase
179f6 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
179f7 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 *pBt,
179f8 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 /* The BTree th
179f9 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
179fa 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 table */. Pgno
179fb 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 pgno,
179fc 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
179fd 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e to clear */. in
179fe 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20 t freePageFlag,
179ff 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 /* Deallocat
17a00 65 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a e page if true *
17a01 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 /. int *pnChang
17a02 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a e.){. MemPage *
17a03 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 pPage = 0;. int
17a04 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 rc;. unsigned
17a05 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 char *pCell;. i
17a06 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 nt i;.. assert(
17a07 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
17a08 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
17a09 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61 );. if( pgno>pa
17a0a 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
17a0b 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
17a0c 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
17a0d 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d KPT;. }.. rc =
17a0e 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
17a0f 70 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 pBt, pgno, &pPag
17a10 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 e);. if( rc ) g
17a11 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 oto cleardatabas
17a12 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 66 6f 72 epage_out;. for
17a13 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e (i=0; i<pPage->n
17a14 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
17a15 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
17a16 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 (pPage, i);.
17a17 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
17a18 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63 ){. rc = c
17a19 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 learDatabasePage
17a1a 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 (pBt, get4byte(p
17a1b 43 65 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e Cell), 1, pnChan
17a1c 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ge);. if( r
17a1d 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 c ) goto clearda
17a1e 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a tabasepage_out;.
17a1f 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 }. rc = c
17a20 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 learCell(pPage,
17a21 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 pCell);. if(
17a22 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 rc ) goto cleard
17a23 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b atabasepage_out;
17a24 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 . }. if( !pPag
17a25 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 e->leaf ){. r
17a26 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 c = clearDatabas
17a27 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 ePage(pBt, get4b
17a28 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
17a29 61 5b 38 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e a[8]), 1, pnChan
17a2a 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ge);. if( rc
17a2b 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 ) goto cleardata
17a2c 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 basepage_out;.
17a2d 7d 65 6c 73 65 20 69 66 28 20 70 6e 43 68 61 6e }else if( pnChan
17a2e 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ge ){. assert
17a2f 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
17a30 29 3b 0a 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 );. *pnChange
17a31 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c += pPage->nCell
17a32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 72 65 65 ;. }. if( free
17a33 50 61 67 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 PageFlag ){.
17a34 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 rc = freePage(pP
17a35 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 age);. }else if
17a36 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 ( (rc = sqlite3P
17a37 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
17a38 3e 70 44 62 50 61 67 65 29 29 3d 3d 30 20 29 7b >pDbPage))==0 ){
17a39 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 . zeroPage(pP
17a3a 61 67 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 age, pPage->aDat
17a3b 61 5b 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 a[0] | PTF_LEAF)
17a3c 3b 0a 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 ;. }..cleardata
17a3d 62 61 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 basepage_out:.
17a3e 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
17a3f 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b e);. return rc;
17a40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
17a41 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e all information
17a42 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 from a single t
17a43 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 able in the data
17a44 62 61 73 65 2e 20 20 69 54 61 62 6c 65 20 69 73 base. iTable is
17a45 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d .** the page num
17a46 62 65 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 ber of the root
17a47 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 41 of the table. A
17a48 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e fter this routin
17a49 65 20 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 e returns,.** th
17a4a 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65 e root page is e
17a4b 6d 70 74 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 mpty, but still
17a4c 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 exists..**.** Th
17a4d 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
17a4e 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 fail with SQLITE
17a4f 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 _LOCKED if there
17a50 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a are any open.**
17a51 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 6f 6e read cursors on
17a52 20 74 68 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 the table. Ope
17a53 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 n write cursors
17a54 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 are moved to the
17a55 0a 2a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 .** root of the
17a56 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 table..**.** If
17a57 70 6e 43 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 pnChange is not
17a58 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 NULL, then table
17a59 20 69 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 iTable must be
17a5a 61 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e an intkey table.
17a5b 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 The.** integer
17a5c 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f value pointed to
17a5d 20 62 79 20 70 6e 43 68 61 6e 67 65 20 69 73 20 by pnChange is
17a5e 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 incremented by t
17a5f 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
17a60 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 entries in the t
17a61 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f able..*/.SQLITE_
17a62 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17a63 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 te3BtreeClearTab
17a64 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 le(Btree *p, int
17a65 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e iTable, int *pn
17a66 43 68 61 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 Change){. int r
17a67 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 c;. BtShared *p
17a68 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 Bt = p->pBt;. s
17a69 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
17a6a 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
17a6b 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
17a6c 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 _WRITE );. if(
17a6d 28 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65 (rc = checkForRe
17a6e 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 adConflicts(p, i
17a6f 54 61 62 6c 65 2c 20 30 2c 20 31 29 29 21 3d 53 Table, 0, 1))!=S
17a70 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17a71 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f /* nothing to do
17a72 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
17a73 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
17a74 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
17a75 70 42 74 2c 20 69 54 61 62 6c 65 2c 20 30 29 29 pBt, iTable, 0))
17a76 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74 68 69 ){. /* nothi
17a77 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 ng to do */. }e
17a78 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c lse{. rc = cl
17a79 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
17a7a 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c pBt, (Pgno)iTabl
17a7b 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 29 3b e, 0, pnChange);
17a7c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
17a7d 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
17a7e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
17a7f 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 6e 66 ** Erase all inf
17a80 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 74 61 ormation in a ta
17a81 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 65 20 ble and add the
17a82 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c root of the tabl
17a83 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 65 65 e to.** the free
17a84 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20 74 list. Except, t
17a85 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 70 he root of the p
17a86 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 20 28 rinciple table (
17a87 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 the one on.** pa
17a88 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 20 61 ge 1) is never a
17a89 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 dded to the free
17a8a 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 list..**.** This
17a8b 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 routine will fa
17a8c 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c il with SQLITE_L
17a8d 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 OCKED if there a
17a8e 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 re any open.** c
17a8f 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 61 ursors on the ta
17a90 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 ble..**.** If AU
17a91 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 TOVACUUM is enab
17a92 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 led and the page
17a93 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 6e 6f at iTable is no
17a94 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 72 6f t the last.** ro
17a95 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ot page in the d
17a96 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
17a97 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 en the last root
17a98 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 68 65 page .** in the
17a99 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
17a9a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 s moved into the
17a9b 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f slot formerly o
17a9c 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 69 54 ccupied by.** iT
17a9d 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 6c 61 able and that la
17a9e 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 st slot formerly
17a9f 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 68 65 occupied by the
17aa0 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 0a last root page.
17aa1 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 ** is added to t
17aa2 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e 73 74 he freelist inst
17aa3 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e 20 20 ead of iTable.
17aa4 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 6c 6c In this say, all
17aa5 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 61 .** root pages a
17aa6 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 20 62 re kept at the b
17aa7 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
17aa8 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 database file, w
17aa9 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 65 73 hich.** is neces
17aaa 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 41 43 sary for AUTOVAC
17aab 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 67 68 UUM to work righ
17aac 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 t. *piMoved is
17aad 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 set to the .** p
17aae 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 74 20 age number that
17aaf 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c used to be the l
17ab0 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 6e ast root page in
17ab1 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f 72 65 the file before
17ab2 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 20 49 .** the move. I
17ab3 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 20 6d f no page gets m
17ab4 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69 oved, *piMoved i
17ab5 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 s set to 0..** T
17ab6 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
17ab7 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 69 6e e is recorded in
17ab8 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 68 65 meta[3] and the
17ab9 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 value of.** met
17aba 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 64 20 a[3] is updated
17abb 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 by this procedur
17abc 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
17abd 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 btreeDropTable(
17abe 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 Btree *p, Pgno i
17abf 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f Table, int *piMo
17ac0 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ved){. int rc;.
17ac1 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
17ac2 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 = 0;. BtShared
17ac3 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
17ac4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17ac5 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
17ac6 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
17ac7 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
17ac8 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a 20 20 ANS_WRITE );..
17ac9 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c /* It is illegal
17aca 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 6c 65 to drop a table
17acb 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73 20 if any cursors
17acc 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a are open on the.
17acd 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 ** database. T
17ace 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 his is because i
17acf 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f n auto-vacuum mo
17ad0 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 20 6d de the backend m
17ad1 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 ay. ** need to
17ad2 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 6f 6f move another roo
17ad3 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 61 t-page to fill a
17ad4 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 gap left by the
17ad5 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 6f deleted. ** ro
17ad6 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e 20 6f ot page. If an o
17ad7 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 20 75 pen cursor was u
17ad8 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 61 sing this page a
17ad9 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0a problem would .
17ada 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a 2f ** occur.. */
17adb 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43 75 72 . if( pBt->pCur
17adc 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 sor ){. sqlit
17add 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
17ade 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e ked(p->db, pBt->
17adf 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d pCursor->pBtree-
17ae0 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e >db);. return
17ae1 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 SQLITE_LOCKED_S
17ae2 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a HAREDCACHE;. }.
17ae3 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
17ae4 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
17ae5 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26 (Pgno)iTable, &
17ae6 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 pPage, 0);. if(
17ae7 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
17ae8 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
17ae9 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 70 treeClearTable(p
17aea 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 , iTable, 0);.
17aeb 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
17aec 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
17aed 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
17aee 0a 20 20 7d 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 . }.. *piMoved
17aef 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 69 54 61 = 0;.. if( iTa
17af0 62 6c 65 3e 31 20 29 7b 0a 23 69 66 64 65 66 20 ble>1 ){.#ifdef
17af1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
17af2 56 41 43 55 55 4d 0a 20 20 20 20 72 63 20 3d 20 VACUUM. rc =
17af3 66 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b freePage(pPage);
17af4 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
17af5 28 70 50 61 67 65 29 3b 0a 23 65 6c 73 65 0a 20 (pPage);.#else.
17af6 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f if( pBt->auto
17af7 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 Vacuum ){.
17af8 50 67 6e 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f Pgno maxRootPgno
17af9 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
17afa 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
17afb 28 70 2c 20 34 2c 20 26 6d 61 78 52 6f 6f 74 50 (p, 4, &maxRootP
17afc 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 gno);. if(
17afd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
17afe 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
17aff 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
17b00 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
17b01 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 }.. i
17b02 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78 52 6f f( iTable==maxRo
17b03 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 otPgno ){.
17b04 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c /* If the tabl
17b05 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 e being dropped
17b06 69 73 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 is the table wit
17b07 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f h the largest ro
17b08 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20 ot-page.
17b09 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 ** number in the
17b0a 20 64 61 74 61 62 61 73 65 2c 20 70 75 74 20 74 database, put t
17b0b 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 6e 20 he root page on
17b0c 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 20 0a the free list. .
17b0d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
17b0e 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 rc = freePage
17b0f 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
17b10 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
17b11 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
17b12 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
17b13 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
17b14 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
17b15 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
17b16 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 /* The tab
17b17 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 le being dropped
17b18 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 does not have t
17b19 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d he largest root-
17b1a 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 page. **
17b1b 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 number in the da
17b1c 74 61 62 61 73 65 2e 20 53 6f 20 6d 6f 76 65 20 tabase. So move
17b1d 74 68 65 20 70 61 67 65 20 74 68 61 74 20 64 6f the page that do
17b1e 65 73 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 es into the .
17b1f 20 20 20 20 20 2a 2a 20 67 61 70 20 6c 65 66 74 ** gap left
17b20 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 20 by the deleted
17b21 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 20 20 20 20 root-page..
17b22 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 4d 65 */. Me
17b23 6d 50 61 67 65 20 2a 70 4d 6f 76 65 3b 0a 20 20 mPage *pMove;.
17b24 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
17b25 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
17b26 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
17b27 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
17b28 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d maxRootPgno, &pM
17b29 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ove, 0);.
17b2a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17b2b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
17b2c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
17b2d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 }. rc
17b2e 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 = relocatePage(p
17b2f 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d 41 Bt, pMove, PTRMA
17b30 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 69 P_ROOTPAGE, 0, i
17b31 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20 Table, 0);.
17b32 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
17b33 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Move);. i
17b34 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
17b35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
17b36 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
17b37 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
17b38 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
17b39 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 age(pBt, maxRoot
17b3a 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 Pgno, &pMove, 0)
17b3b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
17b3c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17b3d 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
17b3e 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
17b3f 20 20 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 rc = freeP
17b40 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 age(pMove);.
17b41 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
17b42 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20 pMove);.
17b43 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
17b44 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
17b45 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
17b46 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69 4d }. *piM
17b47 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50 67 oved = maxRootPg
17b48 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 no;. }..
17b49 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e 65 /* Set the ne
17b4a 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 65 w 'max-root-page
17b4b 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 64 ' value in the d
17b4c 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e 20 atabase header.
17b4d 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 73 This. ** is
17b4e 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 6c the old value l
17b4f 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e ess one, less on
17b50 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20 68 e more if that h
17b51 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20 20 appens to.
17b52 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61 67 ** be a root-pag
17b53 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20 6f e number, less o
17b54 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61 74 ne again if that
17b55 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a 2a is the. **
17b56 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 PENDING_BYTE_PA
17b57 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 GE.. */.
17b58 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d maxRootPgno--
17b59 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 61 78 52 ;. if( maxR
17b5a 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 ootPgno==PENDING
17b5b 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
17b5c 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f ){. maxRo
17b5d 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 otPgno--;.
17b5e 7d 0a 20 20 20 20 20 20 69 66 28 20 6d 61 78 52 }. if( maxR
17b5f 6f 6f 74 50 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f ootPgno==PTRMAP_
17b60 50 41 47 45 4e 4f 28 70 42 74 2c 20 6d 61 78 52 PAGENO(pBt, maxR
17b61 6f 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 ootPgno) ){.
17b62 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d maxRootPgno-
17b63 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 -;. }.
17b64 20 61 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74 assert( maxRoot
17b65 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
17b66 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
17b67 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
17b68 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 te3BtreeUpdateMe
17b69 74 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 ta(p, 4, maxRoot
17b6a 50 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Pgno);. }else
17b6b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 72 65 {. rc = fre
17b6c 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
17b6d 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
17b6e 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 pPage);. }.#e
17b6f 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ndif. }else{.
17b70 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42 /* If sqlite3B
17b71 74 72 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61 treeDropTable wa
17b72 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 s called on page
17b73 20 31 2e 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 1. */. zeroP
17b74 61 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f 49 age(pPage, PTF_I
17b75 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 NTKEY|PTF_LEAF )
17b76 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 ;. releasePag
17b77 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 e(pPage);. }.
17b78 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 return rc; .}.S
17b79 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
17b7a 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 t sqlite3BtreeDr
17b7b 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 opTable(Btree *p
17b7c 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e , int iTable, in
17b7d 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 t *piMoved){. i
17b7e 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 nt rc;. sqlite3
17b7f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
17b80 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54 rc = btreeDropT
17b81 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 able(p, iTable,
17b82 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 piMoved);. sqli
17b83 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
17b84 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
17b85 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 .../*.** Read th
17b86 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 e meta-informati
17b87 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61 on out of a data
17b88 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 base file. Meta
17b89 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 [0].** is the nu
17b8a 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 mber of free pag
17b8b 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 es currently in
17b8c 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d the database. M
17b8d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 eta[1].** throug
17b8e 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61 h meta[15] are a
17b8f 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 vailable for use
17b90 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 65 72 by higher layer
17b91 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 s. Meta[0].** i
17b92 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 s read-only, the
17b93 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 61 64 others are read
17b94 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 /write..** .** T
17b95 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 he schema layer
17b96 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c numbers meta val
17b97 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e ues differently.
17b98 20 20 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a At the schema.
17b99 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68 ** layer (and th
17b9a 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 e SetCookie and
17b9b 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 ReadCookie opcod
17b9c 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f es) the number o
17b9d 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20 f.** free pages
17b9e 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 is not visible.
17b9f 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 So Cookie[0] is
17ba0 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74 the same as Met
17ba1 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f a[1]..*/.SQLITE_
17ba2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17ba3 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 te3BtreeGetMeta(
17ba4 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 Btree *p, int id
17ba5 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a x, u32 *pMeta){.
17ba6 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
17ba7 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b e = 0;. int rc;
17ba8 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
17ba9 20 2a 70 50 31 3b 0a 20 20 42 74 53 68 61 72 65 *pP1;. BtShare
17baa 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
17bab 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
17bac 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 2f 2a 20 Enter(p);.. /*
17bad 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61 2d 64 Reading a meta-d
17bae 61 74 61 20 76 61 6c 75 65 20 72 65 71 75 69 72 ata value requir
17baf 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f es a read-lock o
17bb0 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20 68 65 n page 1 (and he
17bb1 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c nce. ** the sql
17bb2 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
17bb3 2e 20 57 65 20 67 72 61 62 20 74 68 69 73 20 6c . We grab this l
17bb4 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73 20 6f ock regardless o
17bb5 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20 20 2a f whether or. *
17bb6 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49 54 45 * not the SQLITE
17bb7 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 _ReadUncommitted
17bb8 20 66 6c 61 67 20 69 73 20 73 65 74 20 28 74 68 flag is set (th
17bb9 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61 e table rooted a
17bba 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20 69 73 t page. ** 1 is
17bbb 20 74 72 65 61 74 65 64 20 61 73 20 61 20 73 70 treated as a sp
17bbc 65 63 69 61 6c 20 63 61 73 65 20 62 79 20 71 75 ecial case by qu
17bbd 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 erySharedCacheTa
17bbe 62 6c 65 4c 6f 63 6b 28 29 0a 20 20 2a 2a 20 61 bleLock(). ** a
17bbf 6e 64 20 73 65 74 53 68 61 72 65 64 43 61 63 68 nd setSharedCach
17bc0 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 20 eTableLock())..
17bc1 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 */. rc = query
17bc2 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
17bc3 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41 44 5f Lock(p, 1, READ_
17bc4 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63 21 LOCK);. if( rc!
17bc5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17bc6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
17bc7 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 ave(p);. retu
17bc8 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 rn rc;. }.. as
17bc9 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 sert( idx>=0 &&
17bca 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69 66 28 idx<=15 );. if(
17bcb 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 7b 0a pBt->pPage1 ){.
17bcc 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 72 65 /* The b-tre
17bcd 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 6f 6c e is already hol
17bce 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 ding a reference
17bcf 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20 74 68 to page 1 of th
17bd0 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a e database. *
17bd1 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 73 20 * file. In this
17bd2 63 61 73 65 20 74 68 65 20 72 65 71 75 69 72 65 case the require
17bd3 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61 6c 75 d meta-data valu
17bd4 65 20 63 61 6e 20 62 65 20 72 65 61 64 20 64 69 e can be read di
17bd5 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20 66 72 rectly. ** fr
17bd6 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61 74 61 om the page data
17bd7 20 6f 66 20 74 68 69 73 20 72 65 66 65 72 65 6e of this referen
17bd8 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c 69 67 ce. This is slig
17bd9 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 61 6e htly faster than
17bda 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73 74 69 . ** requesti
17bdb 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72 65 6e ng a new referen
17bdc 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ce from the page
17bdd 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a 2f 0a r layer.. */.
17bde 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 69 67 pP1 = (unsig
17bdf 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74 2d 3e ned char *)pBt->
17be0 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 pPage1->aData;.
17be1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
17be2 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20 6e he b-tree does n
17be3 6f 74 20 68 61 76 65 20 61 20 72 65 66 65 72 65 ot have a refere
17be4 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66 nce to page 1 of
17be5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
17be6 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74 61 69 le.. ** Obtai
17be7 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20 70 n one from the p
17be8 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 ager layer..
17be9 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
17bea 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d te3PagerGet(pBt-
17beb 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70 44 62 >pPager, 1, &pDb
17bec 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
17bed 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 c ){. sqlit
17bee 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
17bef 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
17bf0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 31 20 ;. }. pP1
17bf1 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
17bf2 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
17bf3 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b etData(pDbPage);
17bf4 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20 3d 20 . }. *pMeta =
17bf5 67 65 74 34 62 79 74 65 28 26 70 50 31 5b 33 36 get4byte(&pP1[36
17bf6 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20 2f + idx*4]);.. /
17bf7 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20 * If the b-tree
17bf8 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67 20 61 is not holding a
17bf9 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 reference to pa
17bfa 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65 20 77 ge 1, then one w
17bfb 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 as . ** request
17bfc 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ed from the page
17bfd 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65 20 61 r layer in the a
17bfe 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65 6c 65 bove block. Rele
17bff 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f ase it now.. */
17c00 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 50 61 . if( !pBt->pPa
17c01 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ge1 ){. sqlit
17c02 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 e3PagerUnref(pDb
17c03 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a Page);. }.. /*
17c04 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d 65 64 If autovacuumed
17c05 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 is disabled in
17c06 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 20 77 this build but w
17c07 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 e are trying to
17c08 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 6e 20 . ** access an
17c09 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 61 74 autovacuumed dat
17c0a 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 6b 65 abase, then make
17c0b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 the database re
17c0c 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a 23 69 adonly. . */.#i
17c0d 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
17c0e 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 _AUTOVACUUM. if
17c0f 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 4d 65 ( idx==4 && *pMe
17c10 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61 64 ta>0 ) pBt->read
17c11 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69 66 Only = 1;.#endif
17c12 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
17c13 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 is currently an
17c14 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e open transaction
17c15 2c 20 67 72 61 62 20 61 20 72 65 61 64 2d 6c 6f , grab a read-lo
17c16 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70 61 67 65 ck . ** on page
17c17 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 1 of the databa
17c18 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 se file. This is
17c19 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 done to make su
17c1a 72 65 20 74 68 61 74 0a 20 20 2a 2a 20 6e 6f 20 re that. ** no
17c1b 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
17c1c 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 can modify the
17c1d 6d 65 74 61 20 76 61 6c 75 65 20 6a 75 73 74 20 meta value just
17c1e 72 65 61 64 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 read from. ** t
17c1f 68 65 20 64 61 74 61 62 61 73 65 20 75 6e 74 69 he database unti
17c20 6c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f l the transactio
17c21 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 0a n is concluded..
17c22 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e */. if( p->in
17c23 54 72 61 6e 73 3e 30 20 29 7b 0a 20 20 20 20 72 Trans>0 ){. r
17c24 63 20 3d 20 73 65 74 53 68 61 72 65 64 43 61 63 c = setSharedCac
17c25 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 31 heTableLock(p, 1
17c26 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 , READ_LOCK);.
17c27 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 }. sqlite3Btree
17c28 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
17c29 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
17c2a 57 72 69 74 65 20 6d 65 74 61 2d 69 6e 66 6f 72 Write meta-infor
17c2b 6d 61 74 69 6f 6e 20 62 61 63 6b 20 69 6e 74 6f mation back into
17c2c 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
17c2d 4d 65 74 61 5b 30 5d 20 69 73 0a 2a 2a 20 72 65 Meta[0] is.** re
17c2e 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61 79 20 ad-only and may
17c2f 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 2e 0a not be written..
17c30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17c31 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
17c32 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 eeUpdateMeta(Btr
17c33 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 ee *p, int idx,
17c34 75 33 32 20 69 4d 65 74 61 29 7b 0a 20 20 42 74 u32 iMeta){. Bt
17c35 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
17c36 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 >pBt;. unsigned
17c37 20 63 68 61 72 20 2a 70 50 31 3b 0a 20 20 69 6e char *pP1;. in
17c38 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
17c39 69 64 78 3e 3d 31 20 26 26 20 69 64 78 3c 3d 31 idx>=1 && idx<=1
17c3a 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 5 );. sqlite3Bt
17c3b 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 reeEnter(p);. a
17c3c 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
17c3d 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
17c3e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
17c3f 3e 70 50 61 67 65 31 21 3d 30 20 29 3b 0a 20 20 >pPage1!=0 );.
17c40 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 pP1 = pBt->pPage
17c41 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 1->aData;. rc =
17c42 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
17c43 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e te(pBt->pPage1->
17c44 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 pDbPage);. if(
17c45 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17c46 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 . put4byte(&p
17c47 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 2c 20 P1[36 + idx*4],
17c48 69 4d 65 74 61 29 3b 0a 23 69 66 6e 64 65 66 20 iMeta);.#ifndef
17c49 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
17c4a 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 69 VACUUM. if( i
17c4b 64 78 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 61 dx==7 ){. a
17c4c 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f ssert( pBt->auto
17c4d 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74 61 3d Vacuum || iMeta=
17c4e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 =0 );. asse
17c4f 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c 7c 20 rt( iMeta==0 ||
17c50 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20 20 20 iMeta==1 );.
17c51 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 pBt->incrVacuu
17c52 6d 20 3d 20 28 75 38 29 69 4d 65 74 61 3b 0a 20 m = (u8)iMeta;.
17c53 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
17c54 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
17c55 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
17c56 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
17c57 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20 62 79 turn the flag by
17c58 74 65 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e te at the beginn
17c59 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 20 ing of the page
17c5a 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a that the cursor.
17c5b 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ** is currently
17c5c 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f 0a pointing to..*/.
17c5d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
17c5e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 nt sqlite3BtreeF
17c5f 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 2a 70 lags(BtCursor *p
17c60 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 3a Cur){. /* TODO:
17c61 20 57 68 61 74 20 61 62 6f 75 74 20 43 55 52 53 What about CURS
17c62 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 73 OR_REQUIRESEEK s
17c63 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79 20 6e tate? Probably n
17c64 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a eed to call. **
17c65 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
17c66 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e 0a 20 sition() here..
17c67 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 */. MemPage *p
17c68 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72 65 43 Page;. restoreC
17c69 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 ursorPosition(pC
17c6a 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 ur);. pPage = p
17c6b 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
17c6c 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 ->iPage];. asse
17c6d 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
17c6e 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
17c6f 61 73 73 65 72 74 28 20 70 50 61 67 65 21 3d 30 assert( pPage!=0
17c70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
17c71 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72 2d 3e age->pBt==pCur->
17c72 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 pBt );. return
17c73 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
17c74 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d 3b 0a ge->hdrOffset];.
17c75 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
17c76 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e E_OMIT_BTREECOUN
17c77 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 T./*.** The firs
17c78 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 t argument, pCur
17c79 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 , is a cursor op
17c7a 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 ened on some b-t
17c7b 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a ree. Count the.*
17c7c 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * number of entr
17c7d 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 72 65 ies in the b-tre
17c7e 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 e and write the
17c7f 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 result to *pnEnt
17c80 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 ry..**.** SQLITE
17c81 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
17c82 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e if the operation
17c83 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 is successfully
17c84 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f executed. .** O
17c85 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20 therwise, if an
17c86 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 error is encount
17c87 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f ered (i.e. an IO
17c88 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 62 61 error or databa
17c89 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e se.** corruption
17c8a 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f ) an SQLite erro
17c8b 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
17c8c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
17c8d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
17c8e 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75 3BtreeCount(BtCu
17c8f 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 rsor *pCur, i64
17c90 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 *pnEntry){. i64
17c91 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 nEntry = 0;
17c92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c93 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
17c94 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 turn in *pnEntry
17c95 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
17c96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c97 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
17c98 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 turn code */. r
17c99 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 c = moveToRoot(p
17c9a 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 Cur);.. /* Unle
17c9b 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 ss an error occu
17c9c 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e rs, the followin
17c9d 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 g loop runs one
17c9e 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 iteration for ea
17c9f 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 ch. ** page in
17ca0 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 75 63 the B-Tree struc
17ca1 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 ture (not includ
17ca2 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ing overflow pag
17ca3 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 es). . */. whi
17ca4 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
17ca5 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 K ){. int iId
17ca6 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
17ca7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
17ca8 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f ndex of child no
17ca9 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a de in parent */.
17caa 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 MemPage *pPa
17cab 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
17cac 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
17cad 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d t page of the b-
17cae 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 tree */.. /*
17caf 49 66 20 74 68 69 73 20 69 73 20 61 20 6c 65 61 If this is a lea
17cb0 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 74 72 f page or the tr
17cb1 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 ee is not an int
17cb2 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 6e 20 -key tree, then
17cb3 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 61 67 . ** this pag
17cb4 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 e contains count
17cb5 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 49 6e able entries. In
17cb6 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 crement the entr
17cb7 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a y counter. **
17cb8 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 accordingly..
17cb9 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 20 3d */. pPage =
17cba 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
17cbb 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
17cbc 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 if( pPage->leaf
17cbd 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 || !pPage->intKe
17cbe 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 y ){. nEntr
17cbf 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c y += pPage->nCel
17cc0 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a l;. }.. /*
17cc1 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 pPage is a leaf
17cc2 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 node. This loop
17cc3 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 20 63 navigates the c
17cc4 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74 ursor so that it
17cc5 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 . ** points
17cc6 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e 74 to the first int
17cc7 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 erior cell that
17cc8 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
17cc9 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a parent of. *
17cca 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 * the next page
17ccb 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74 in the tree that
17ccc 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 has not yet bee
17ccd 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 0a 20 n visited. The.
17cce 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 ** pCur->aiId
17ccf 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 x[pCur->iPage] v
17cd0 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f 20 74 alue is set to t
17cd1 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
17cd2 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 parent cell.
17cd3 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20 ** of the page,
17cd4 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 or to the number
17cd5 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 of cells in the
17cd6 20 70 61 67 65 20 69 66 20 74 68 65 20 6e 65 78 page if the nex
17cd7 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f t page. ** to
17cd8 20 76 69 73 69 74 20 69 73 20 74 68 65 20 72 69 visit is the ri
17cd9 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 ght-child of its
17cda 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a parent.. **.
17cdb 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 ** If all pa
17cdc 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 ges in the tree
17cdd 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 65 have been visite
17cde 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 d, return SQLITE
17cdf 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 20 2a _OK to the. *
17ce0 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f * caller.. */
17ce1 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
17ce2 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 64 6f leaf ){. do
17ce3 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 {. if( p
17ce4 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b Cur->iPage==0 ){
17ce5 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c . /* Al
17ce6 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 62 l pages of the b
17ce7 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 6e 20 -tree have been
17ce8 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 6e 20 visited. Return
17ce9 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f successfully. */
17cea 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e 45 6e . *pnEn
17ceb 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 try = nEntry;.
17cec 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
17ced 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
17cee 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 }. sqli
17cef 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 te3BtreeMoveToPa
17cf0 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 rent(pCur);.
17cf1 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72 2d }while ( pCur-
17cf2 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
17cf3 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61 67 ge]>=pCur->apPag
17cf4 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
17cf5 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20 20 nCell );..
17cf6 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
17cf7 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20 20 ->iPage]++;.
17cf8 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
17cf9 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17cfa 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ge];. }..
17cfb 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74 68 /* Descend to th
17cfc 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66 20 e child node of
17cfd 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74 68 the cell that th
17cfe 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 e cursor current
17cff 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 ly . ** point
17d00 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74 68 s at. This is th
17d01 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 66 e right-child if
17d02 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e (iIdx==pPage->n
17d03 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 Cell).. */.
17d04 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 iIdx = pCur->a
17d05 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
17d06 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78 3d ];. if( iIdx=
17d07 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b =pPage->nCell ){
17d08 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 . rc = move
17d09 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 ToChild(pCur, ge
17d0a 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
17d0b 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
17d0c 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 ffset+8]));.
17d0d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
17d0e 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
17d0f 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e ur, get4byte(fin
17d10 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49 64 dCell(pPage, iId
17d11 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a x)));. }. }.
17d12 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 . /* An error h
17d13 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65 74 as occurred. Ret
17d14 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 urn an error cod
17d15 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 e. */. return r
17d16 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a c;.}.#endif../*.
17d17 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 ** Return the pa
17d18 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 ger associated w
17d19 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54 68 ith a BTree. Th
17d1a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
17d1b 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e ed for.** testin
17d1c 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 g and debugging
17d1d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f only..*/.SQLITE_
17d1e 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a 73 PRIVATE Pager *s
17d1f 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
17d20 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 (Btree *p){. re
17d21 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61 turn p->pBt->pPa
17d22 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 ger;.}..#ifndef
17d23 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
17d24 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a GRITY_CHECK./*.*
17d25 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73 61 * Append a messa
17d26 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72 20 ge to the error
17d27 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a message string..
17d28 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
17d29 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a 20 heckAppendMsg(.
17d2a 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 IntegrityCk *pC
17d2b 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a 4d heck,. char *zM
17d2c 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 sg1,. const cha
17d2d 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e 2e r *zFormat,. ..
17d2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
17d2f 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63 6b p;. if( !pCheck
17d30 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72 6e ->mxErr ) return
17d31 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 ;. pCheck->mxEr
17d32 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6e r--;. pCheck->n
17d33 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 72 Err++;. va_star
17d34 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
17d35 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 if( pCheck->er
17d36 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20 20 rMsg.nChar ){.
17d37 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
17d38 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d mAppend(&pCheck-
17d39 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 31 >errMsg, "\n", 1
17d3a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d 73 );. }. if( zMs
17d3b 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 g1 ){. sqlite
17d3c 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
17d3d 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c &pCheck->errMsg,
17d3e 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20 7d zMsg1, -1);. }
17d3f 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e . sqlite3VXPrin
17d40 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d tf(&pCheck->errM
17d41 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 sg, 1, zFormat,
17d42 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
17d43 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d );. if( pCheck-
17d44 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 61 >errMsg.mallocFa
17d45 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68 65 iled ){. pChe
17d46 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ck->mallocFailed
17d47 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 = 1;. }.}.#end
17d48 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
17d49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
17d4a 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 K */..#ifndef SQ
17d4b 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
17d4c 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 ITY_CHECK./*.**
17d4d 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 66 Add 1 to the ref
17d4e 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 erence count for
17d4f 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 66 page iPage. If
17d50 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 this is the sec
17d51 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 ond.** reference
17d52 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 64 to the page, ad
17d53 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 d an error messa
17d54 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45 ge to pCheck->zE
17d55 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e rrMsg..** Return
17d56 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20 1 if there are
17d57 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 72 2 ore more refer
17d58 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 ences to the pag
17d59 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66 e and 0 if.** if
17d5a 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 this is the fir
17d5b 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 st reference to
17d5c 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 the page..**.**
17d5d 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 20 Also check that
17d5e 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
17d5f 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f is in bounds..*/
17d60 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 .static int chec
17d61 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 6b kRef(IntegrityCk
17d62 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69 *pCheck, Pgno i
17d63 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e Page, char *zCon
17d64 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50 61 text){. if( iPa
17d65 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 ge==0 ) return 1
17d66 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70 43 ;. if( iPage>pC
17d67 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a 20 heck->nPage ){.
17d68 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
17d69 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 g(pCheck, zConte
17d6a 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 67 xt, "invalid pag
17d6b 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 50 e number %d", iP
17d6c 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e age);. return
17d6d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 1;. }. if( pC
17d6e 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 heck->anRef[iPag
17d6f 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68 65 e]==1 ){. che
17d70 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
17d71 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 32 ck, zContext, "2
17d72 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 nd reference to
17d73 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 page %d", iPage)
17d74 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
17d75 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28 70 }. return (p
17d76 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 Check->anRef[iPa
17d77 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 66 ge]++)>1;.}..#if
17d78 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17d79 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a _AUTOVACUUM./*.*
17d7a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
17d7b 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f entry in the po
17d7c 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70 61 inter-map for pa
17d7d 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20 74 ge iChild maps t
17d7e 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72 65 o .** page iPare
17d7f 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70 65 nt, pointer type
17d80 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f 74 ptrType. If not
17d81 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72 6f , append an erro
17d82 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 r message.** to
17d83 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 pCheck..*/.stati
17d84 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72 6d c void checkPtrm
17d85 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 ap(. IntegrityC
17d86 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a 20 k *pCheck, /*
17d87 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 20 Integrity check
17d88 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67 6e context */. Pgn
17d89 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 20 o iChild,
17d8a 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61 67 /* Child pag
17d8b 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 e number */. u8
17d8c 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 eType,
17d8d 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 /* Expected
17d8e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79 70 pointer map typ
17d8f 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61 72 e */. Pgno iPar
17d90 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ent, /*
17d91 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65 Expected pointe
17d92 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61 67 r map parent pag
17d93 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 e number */. ch
17d94 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20 ar *zContext
17d95 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 /* Context
17d96 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73 65 description (use
17d97 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67 29 d for error msg)
17d98 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
17d99 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79 70 . u8 ePtrmapTyp
17d9a 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 e;. Pgno iPtrma
17d9b 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20 3d pParent;.. rc =
17d9c 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65 63 ptrmapGet(pChec
17d9d 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c 20 k->pBt, iChild,
17d9e 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26 69 &ePtrmapType, &i
17d9f 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 PtrmapParent);.
17da0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
17da1 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 OK ){. if( rc
17da2 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c ==SQLITE_NOMEM |
17da3 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 | rc==SQLITE_IOE
17da4 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 63 RR_NOMEM ) pChec
17da5 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 k->mallocFailed
17da6 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 70 = 1;. checkAp
17da7 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
17da8 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c 65 zContext, "Faile
17da9 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61 70 d to read ptrmap
17daa 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c 64 key=%d", iChild
17dab 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
17dac 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d 61 }.. if( ePtrma
17dad 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c 20 pType!=eType ||
17dae 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d 69 iPtrmapParent!=i
17daf 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 68 Parent ){. ch
17db0 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
17db1 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a eck, zContext, .
17db2 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20 6d "Bad ptr m
17db3 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 ap entry key=%d
17db4 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64 29 expected=(%d,%d)
17db5 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a got=(%d,%d)", .
17db6 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 54 iChild, eT
17db7 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 ype, iParent, eP
17db8 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 6d trmapType, iPtrm
17db9 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d apParent);. }.}
17dba 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
17dbb 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 heck the integri
17dbc 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 ty of the freeli
17dbd 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72 st or of an over
17dbe 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a flow page list..
17dbf 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 ** Verify that t
17dc0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
17dc1 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 69 es on the list i
17dc2 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 s N..*/.static v
17dc3 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a 20 oid checkList(.
17dc4 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 IntegrityCk *pC
17dc5 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72 heck, /* Integr
17dc6 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e ity checking con
17dc7 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 text */. int is
17dc8 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20 20 FreeList,
17dc9 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66 72 /* True for a fr
17dca 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 66 eelist. False f
17dcb 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 or overflow page
17dcc 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 list */. int i
17dcd 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
17dce 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
17dcf 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20 69 for first page i
17dd0 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 n the list */.
17dd1 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20 int N,
17dd2 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 /* Expecte
17dd3 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 d number of page
17dd4 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f s in the list */
17dd5 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 . char *zContex
17dd6 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 t /* Cont
17dd7 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 ext for error me
17dd8 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 ssages */.){. i
17dd9 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 65 nt i;. int expe
17dda 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20 cted = N;. int
17ddb 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b 0a iFirst = iPage;.
17ddc 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 while( N-- > 0
17ddd 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 && pCheck->mxEr
17dde 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 r ){. DbPage
17ddf 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 20 *pOvflPage;.
17de0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
17de1 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 66 OvflData;. if
17de2 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 20 ( iPage<1 ){.
17de3 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
17de4 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 g(pCheck, zConte
17de5 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25 64 xt,. "%d
17de6 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69 73 of %d pages mis
17de7 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c sing from overfl
17de8 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 ow list starting
17de9 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20 20 at %d",.
17dea 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 64 N+1, expected
17deb 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20 20 , iFirst);.
17dec 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
17ded 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 if( checkRef(p
17dee 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43 Check, iPage, zC
17def 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b 3b ontext) ) break;
17df0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
17df1 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b 2d PagerGet(pCheck-
17df2 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 69 >pPager, (Pgno)i
17df3 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 65 Page, &pOvflPage
17df4 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b ) ){. check
17df5 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
17df6 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 , zContext, "fai
17df7 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65 20 led to get page
17df8 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 %d", iPage);.
17df9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
17dfa 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d 20 pOvflData =
17dfb 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
17dfc 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 )sqlite3PagerGet
17dfd 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 3b Data(pOvflPage);
17dfe 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65 4c . if( isFreeL
17dff 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ist ){. int
17e00 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 n = get4byte(&p
17e01 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69 OvflData[4]);.#i
17e02 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17e03 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
17e04 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 if( pCheck->p
17e05 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
17e06 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 {. checkP
17e07 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 50 trmap(pCheck, iP
17e08 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 age, PTRMAP_FREE
17e09 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 PAGE, 0, zContex
17e0a 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 t);. }.#end
17e0b 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 70 if. if( n>p
17e0c 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 Check->pBt->usab
17e0d 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 20 leSize/4-2 ){.
17e0e 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e checkAppen
17e0f 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f dMsg(pCheck, zCo
17e10 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 ntext,.
17e11 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 66 "freelist leaf
17e12 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f count too big o
17e13 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 n page %d", iPag
17e14 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b e);. N--;
17e15 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
17e16 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
17e17 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 <n; i++){.
17e18 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 Pgno iFreePa
17e19 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ge = get4byte(&p
17e1a 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29 OvflData[8+i*4])
17e1b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
17e1c 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
17e1d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
17e1e 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f Check->pBt->auto
17e1f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 Vacuum ){.
17e20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
17e21 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 p(pCheck, iFreeP
17e22 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 age, PTRMAP_FREE
17e23 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 PAGE, 0, zContex
17e24 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a t);. }.
17e25 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 #endif.
17e26 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b checkRef(pCheck
17e27 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43 6f , iFreePage, zCo
17e28 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 ntext);.
17e29 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e }. N -= n
17e2a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
17e2b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17e2c 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
17e2d 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f else{. /
17e2e 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61 * If this databa
17e2f 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f se supports auto
17e30 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 67 -vacuum and iPag
17e31 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 e is not the las
17e32 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 t. ** page
17e33 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 in this overflow
17e34 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68 61 list, check tha
17e35 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 t the pointer-ma
17e36 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 20 p entry for.
17e37 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ** the followi
17e38 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73 20 ng page matches
17e39 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a iPage.. */.
17e3a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b if( pCheck
17e3b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ->pBt->autoVacuu
17e3c 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20 m && N>0 ){.
17e3d 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74 65 i = get4byte
17e3e 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 (pOvflData);.
17e3f 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 checkPtrmap
17e40 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d (pCheck, i, PTRM
17e41 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50 AP_OVERFLOW2, iP
17e42 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a age, zContext);.
17e43 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 }. }.#e
17e44 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20 3d ndif. iPage =
17e45 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c 44 get4byte(pOvflD
17e46 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ata);. sqlite
17e47 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 3PagerUnref(pOvf
17e48 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 lPage);. }.}.#e
17e49 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
17e4a 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
17e4b 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 ECK */..#ifndef
17e4c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
17e4d 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a GRITY_CHECK./*.*
17e4e 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61 6e * Do various san
17e4f 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 20 ity checks on a
17e50 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 61 single page of a
17e51 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a tree. Return.*
17e52 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74 68 * the tree depth
17e53 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72 65 . Root pages re
17e54 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 73 turn 0. Parents
17e55 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a of root pages.*
17e56 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 20 * return 1, and
17e57 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a so forth..** .**
17e58 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61 72 These checks ar
17e59 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 e done:.**.**
17e5a 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 65 1. Make sure
17e5b 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 20 that cells and
17e5c 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f freeblocks do no
17e5d 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20 t overlap.**
17e5e 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69 6e but combin
17e5f 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 e to completely
17e60 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e 0a cover the page..
17e61 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65 ** NO 2. Make
17e62 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 20 sure cell keys
17e63 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a are in order..**
17e64 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73 NO 3. Make s
17e65 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65 ure no key is le
17e66 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
17e67 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e to zLowerBound.
17e68 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b .** NO 4. Mak
17e69 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 e sure no key is
17e6a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
17e6b 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 72 equal to zUpper
17e6c 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35 Bound..** 5
17e6d 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 . Check the int
17e6e 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66 6c egrity of overfl
17e6f 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20 ow pages..**
17e70 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65 6c 6. Recursivel
17e71 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 y call checkTree
17e72 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c Page on all chil
17e73 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e dren..** 7.
17e74 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 Verify that th
17e75 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 e depth of all c
17e76 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 73 hildren is the s
17e77 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20 ame..** 8.
17e78 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 Make sure this
17e79 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 74 page is at least
17e7a 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73 33% full or els
17e7b 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 e it is.**
17e7c 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 the root of
17e7d 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 the tree..*/.sta
17e7e 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72 65 tic int checkTre
17e7f 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72 69 ePage(. Integri
17e80 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f tyCk *pCheck, /
17e81 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 * Context for th
17e82 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 2a e sanity check *
17e83 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 20 /. int iPage,
17e84 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
17e85 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
17e86 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f page to check */
17e87 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 . char *zParent
17e88 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 65 Context /* Pare
17e89 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b nt context */.){
17e8a 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
17e8b 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20 e;. int i, rc,
17e8c 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f 2c depth, d2, pgno,
17e8d 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 2c cnt;. int hdr,
17e8e 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 6e cellStart;. in
17e8f 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a 64 t nCell;. u8 *d
17e90 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 ata;. BtShared
17e91 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 62 *pBt;. int usab
17e92 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 7a leSize;. char z
17e93 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 20 Context[100];.
17e94 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a 0a char *hit = 0;..
17e95 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
17e96 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 tf(sizeof(zConte
17e97 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 xt), zContext, "
17e98 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 67 Page %d: ", iPag
17e99 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 e);.. /* Check
17e9a 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 78 that the page ex
17e9b 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20 ists. */. pBt
17e9c 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20 = pCheck->pBt;.
17e9d 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 usableSize = pB
17e9e 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 t->usableSize;.
17e9f 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20 if( iPage==0 )
17ea0 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
17ea1 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c checkRef(pCheck,
17ea2 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 43 iPage, zParentC
17ea3 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 6e ontext) ) return
17ea4 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 0;. if( (rc =
17ea5 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
17ea6 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 age(pBt, (Pgno)i
17ea7 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 Page, &pPage, 0)
17ea8 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 )!=0 ){. if(
17ea9 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
17eaa 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || rc==SQLITE_I
17eab 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 OERR_NOMEM ) pCh
17eac 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 eck->mallocFaile
17ead 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b d = 1;. check
17eae 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
17eaf 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 , zContext,.
17eb0 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 "unable to ge
17eb1 74 20 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f t the page. erro
17eb2 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b r code=%d", rc);
17eb3 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
17eb4 20 7d 0a 20 20 69 66 28 20 28 72 63 20 3d 20 73 }. if( (rc = s
17eb5 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 qlite3BtreeInitP
17eb6 61 67 65 28 70 50 61 67 65 29 29 21 3d 30 20 29 age(pPage))!=0 )
17eb7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 {. assert( rc
17eb8 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 ==SQLITE_CORRUPT
17eb9 20 29 3b 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 ); /* The only
17eba 20 70 6f 73 73 69 62 6c 65 20 65 72 72 6f 72 20 possible error
17ebb 66 72 6f 6d 20 49 6e 69 74 50 61 67 65 20 2a 2f from InitPage */
17ebc 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 . checkAppend
17ebd 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
17ebe 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 text, .
17ebf 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 69 74 "sqlit
17ec0 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 e3BtreeInitPage(
17ec1 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72 20 ) returns error
17ec2 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a 20 code %d", rc);.
17ec3 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
17ec4 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
17ec5 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 n 0;. }.. /* C
17ec6 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68 65 heck out all the
17ec7 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 64 cells.. */. d
17ec8 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72 28 epth = 0;. for(
17ec9 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 i=0; i<pPage->nC
17eca 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d ell && pCheck->m
17ecb 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 xErr; i++){.
17ecc 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 75 u8 *pCell;. u
17ecd 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c 49 32 sz;. CellI
17ece 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 2f nfo info;.. /
17ecf 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64 20 * Check payload
17ed0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 overflow pages.
17ed1 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
17ed2 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
17ed3 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f f(zContext), zCo
17ed4 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 ntext,.
17ed5 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61 67 "On tree pag
17ed6 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 2c e %d cell %d: ",
17ed7 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 iPage, i);.
17ed8 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
17ed9 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20 73 (pPage,i);. s
17eda 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
17edb 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
17edc 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 Cell, &info);.
17edd 20 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 sz = info.nDat
17ede 61 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 a;. if( !pPag
17edf 65 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b e->intKey ) sz +
17ee0 3d 20 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79 = (int)info.nKey
17ee1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a ;. assert( sz
17ee2 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 ==info.nPayload
17ee3 29 3b 0a 20 20 20 20 69 66 28 20 28 73 7a 3e 69 );. if( (sz>i
17ee4 6e 66 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20 nfo.nLocal) .
17ee5 20 20 26 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66 && (&pCell[inf
17ee6 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 o.iOverflow]<=&p
17ee7 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d Page->aData[pBt-
17ee8 3e 75 73 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20 >usableSize]).
17ee9 20 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e ){. int n
17eea 50 61 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 Page = (sz - inf
17eeb 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c o.nLocal + usabl
17eec 65 53 69 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 eSize - 5)/(usab
17eed 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 leSize - 4);.
17eee 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c Pgno pgnoOvfl
17eef 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 = get4byte(&pCe
17ef0 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
17ef1 77 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c w]);.#ifndef SQL
17ef2 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
17ef3 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 UUM. if( pB
17ef4 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
17ef5 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 . checkPt
17ef6 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e rmap(pCheck, pgn
17ef7 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 oOvfl, PTRMAP_OV
17ef8 45 52 46 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20 ERFLOW1, iPage,
17ef9 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 zContext);.
17efa 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
17efb 63 68 65 63 6b 4c 69 73 74 28 70 43 68 65 63 6b checkList(pCheck
17efc 2c 20 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e , 0, pgnoOvfl, n
17efd 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b Page, zContext);
17efe 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 . }.. /* C
17eff 68 65 63 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c heck sanity of l
17f00 65 66 74 20 63 68 69 6c 64 20 70 61 67 65 2e 0a eft child page..
17f01 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
17f02 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
17f03 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 pgno = get4
17f04 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66 byte(pCell);.#if
17f05 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17f06 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
17f07 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
17f08 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 acuum ){.
17f09 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 checkPtrmap(pCh
17f0a 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 eck, pgno, PTRMA
17f0b 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 P_BTREE, iPage,
17f0c 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 zContext);.
17f0d 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
17f0e 64 32 20 3d 20 63 68 65 63 6b 54 72 65 65 50 61 d2 = checkTreePa
17f0f 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c ge(pCheck, pgno,
17f10 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 zContext);.
17f11 20 20 69 66 28 20 69 3e 30 20 26 26 20 64 32 21 if( i>0 && d2!
17f12 3d 64 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 =depth ){.
17f13 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
17f14 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
17f15 74 2c 20 22 43 68 69 6c 64 20 70 61 67 65 20 64 t, "Child page d
17f16 65 70 74 68 20 64 69 66 66 65 72 73 22 29 3b 0a epth differs");.
17f17 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 }. de
17f18 70 74 68 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a pth = d2;. }.
17f19 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 }. if( !pPage
17f1a 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 ->leaf ){. pg
17f1b 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
17f1c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
17f1d 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
17f1e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e ;. sqlite3_sn
17f1f 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 printf(sizeof(zC
17f20 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 ontext), zContex
17f21 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 t, .
17f22 20 20 20 20 20 20 20 20 20 22 4f 6e 20 70 61 67 "On pag
17f23 65 20 25 64 20 61 74 20 72 69 67 68 74 20 63 68 e %d at right ch
17f24 69 6c 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a ild: ", iPage);.
17f25 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
17f26 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
17f27 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f if( pBt->auto
17f28 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 Vacuum ){.
17f29 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 checkPtrmap(pChe
17f2a 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 ck, pgno, PTRMAP
17f2b 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 30 _BTREE, iPage, 0
17f2c 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
17f2d 20 20 20 20 63 68 65 63 6b 54 72 65 65 50 61 67 checkTreePag
17f2e 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 e(pCheck, pgno,
17f2f 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 zContext);. }.
17f30 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 . /* Check for
17f31 63 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67 complete coverag
17f32 65 20 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20 e of the page.
17f33 2a 2f 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 */. data = pPag
17f34 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 e->aData;. hdr
17f35 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
17f36 65 74 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 et;. hit = sqli
17f37 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 te3PageMalloc( p
17f38 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a Bt->pageSize );.
17f39 20 20 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a if( hit==0 ){.
17f3a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c pCheck->mall
17f3b 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
17f3c 7d 65 6c 73 65 7b 0a 20 20 20 20 75 31 36 20 63 }else{. u16 c
17f3d 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3d 20 67 ontentOffset = g
17f3e 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
17f3f 72 2b 35 5d 29 3b 0a 20 20 20 20 69 66 20 28 63 r+5]);. if (c
17f40 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3e 20 75 ontentOffset > u
17f41 73 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20 20 sableSize) {.
17f42 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
17f43 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 g(pCheck, 0, .
17f44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f45 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 "Corruption d
17f46 65 74 65 63 74 65 64 20 69 6e 20 68 65 61 64 65 etected in heade
17f47 72 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 50 r on page %d",iP
17f48 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 67 6f age,0);. go
17f49 74 6f 20 63 68 65 63 6b 5f 70 61 67 65 5f 61 62 to check_page_ab
17f4a 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d ort;. }. m
17f4b 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74 65 6e emset(hit+conten
17f4c 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73 61 62 tOffset, 0, usab
17f4d 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 4f 66 leSize-contentOf
17f4e 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d 73 65 fset);. memse
17f4f 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74 65 6e t(hit, 1, conten
17f50 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6e 43 tOffset);. nC
17f51 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 ell = get2byte(&
17f52 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 data[hdr+3]);.
17f53 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20 68 64 cellStart = hd
17f54 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 r + 12 - 4*pPage
17f55 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f 72 28 ->leaf;. for(
17f56 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b i=0; i<nCell; i+
17f57 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 63 +){. int pc
17f58 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
17f59 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a 32 5d a[cellStart+i*2]
17f5a 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73 69 7a );. u16 siz
17f5b 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 20 20 e = 1024;.
17f5c 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 int j;. if(
17f5d 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 pc<=usableSize
17f5e 29 7b 0a 20 20 20 20 20 20 20 20 73 69 7a 65 20 ){. size
17f5f 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 = cellSizePtr(pP
17f60 61 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b age, &data[pc]);
17f61 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
17f62 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 3e 3d f( (pc+size-1)>=
17f63 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 70 63 usableSize || pc
17f64 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 <0 ){. ch
17f65 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
17f66 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 eck, 0, .
17f67 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e "Corruption
17f68 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c detected in cel
17f69 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 l %d on page %d"
17f6a 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 ,i,iPage,0);.
17f6b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17f6c 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d for(j=pc+size-
17f6d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 1; j>=pc; j--) h
17f6e 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d it[j]++;. }
17f6f 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 63 . }. for(c
17f70 6e 74 3d 30 2c 20 69 3d 67 65 74 32 62 79 74 65 nt=0, i=get2byte
17f71 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 20 (&data[hdr+1]);
17f72 69 3e 30 20 26 26 20 69 3c 75 73 61 62 6c 65 53 i>0 && i<usableS
17f73 69 7a 65 20 26 26 20 63 6e 74 3c 31 30 30 30 30 ize && cnt<10000
17f74 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 63 6e ; . cn
17f75 74 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 t++){. int
17f76 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 size = get2byte(
17f77 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 20 20 &data[i+2]);.
17f78 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
17f79 69 66 28 20 28 69 2b 73 69 7a 65 2d 31 29 3e 3d if( (i+size-1)>=
17f7a 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 69 3c usableSize || i<
17f7b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 0 ){. che
17f7c 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
17f7d 63 6b 2c 20 30 2c 20 20 0a 20 20 20 20 20 20 20 ck, 0, .
17f7e 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e "Corruption
17f7f 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c detected in cel
17f80 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 l %d on page %d"
17f81 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 ,i,iPage,0);.
17f82 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17f83 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 for(j=i+size-1
17f84 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 ; j>=i; j--) hit
17f85 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 [j]++;. }.
17f86 20 20 20 20 20 69 20 3d 20 67 65 74 32 62 79 74 i = get2byt
17f87 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 20 20 e(&data[i]);.
17f88 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 }. for(i=cnt
17f89 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 =0; i<usableSize
17f8a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
17f8b 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 ( hit[i]==0 ){.
17f8c 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 cnt++;.
17f8d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69 }else if( hi
17f8e 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 t[i]>1 ){.
17f8f 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
17f90 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20 (pCheck, 0,.
17f91 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 20 "Multiple
17f92 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 64 uses for byte %d
17f93 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 2c of page %d", i,
17f94 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 iPage);.
17f95 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
17f96 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e }. if( cn
17f97 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 t!=data[hdr+7] )
17f98 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 {. checkApp
17f99 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 endMsg(pCheck, 0
17f9a 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 72 , . "Fr
17f9b 61 67 6d 65 6e 74 65 64 20 73 70 61 63 65 20 69 agmented space i
17f9c 73 20 25 64 20 62 79 74 65 20 72 65 70 6f 72 74 s %d byte report
17f9d 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61 67 65 ed as %d on page
17f9e 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 %d",.
17f9f 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d cnt, data[hdr+7]
17fa0 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a , iPage);. }.
17fa1 20 20 7d 0a 63 68 65 63 6b 5f 70 61 67 65 5f 61 }.check_page_a
17fa2 62 6f 72 74 3a 0a 20 20 69 66 20 28 68 69 74 29 bort:. if (hit)
17fa3 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 sqlite3PageFree
17fa4 28 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65 61 73 (hit);.. releas
17fa5 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
17fa6 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a return depth+1;.
17fa7 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
17fa8 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 TE_OMIT_INTEGRIT
17fa9 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e Y_CHECK */..#ifn
17faa 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
17fab 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a INTEGRITY_CHECK.
17fac 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
17fad 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 ne does a comple
17fae 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 te check of the
17faf 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 given BTree file
17fb0 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a . aRoot[] is.**
17fb1 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 an array of pag
17fb2 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 es numbers were
17fb3 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 each page number
17fb4 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 is the root pag
17fb5 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e e of.** a table.
17fb6 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e nRoot is the n
17fb7 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
17fb8 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a in aRoot..**.**
17fb9 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 Write the numbe
17fba 72 20 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20 r of error seen
17fbb 69 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 in *pnErr. Exce
17fbc 70 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f pt for some memo
17fbd 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e ry.** allocation
17fbe 20 65 72 72 6f 72 73 2c 20 20 61 6e 20 65 72 72 errors, an err
17fbf 6f 72 20 6d 65 73 73 61 67 65 20 68 65 6c 64 20 or message held
17fc0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e in memory obtain
17fc1 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f ed from.** mallo
17fc2 63 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 c is returned if
17fc3 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a *pnErr is non-z
17fc4 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d ero. If *pnErr=
17fc5 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a =0 then NULL is.
17fc6 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 ** returned. If
17fc7 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
17fc8 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 tion error occur
17fc9 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 s, NULL is retur
17fca 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
17fcb 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
17fcc 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 ite3BtreeIntegri
17fcd 74 79 43 68 65 63 6b 28 0a 20 20 42 74 72 65 65 tyCheck(. Btree
17fce 20 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 *p, /* The
17fcf 62 74 72 65 65 20 74 6f 20 62 65 20 63 68 65 63 btree to be chec
17fd0 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 ked */. int *aR
17fd1 6f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72 oot, /* An arr
17fd2 61 79 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 ay of root pages
17fd3 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 69 6e 64 numbers for ind
17fd4 69 76 69 64 75 61 6c 20 74 72 65 65 73 20 2a 2f ividual trees */
17fd5 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 . int nRoot,
17fd6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
17fd7 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d tries in aRoot[]
17fd8 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c */. int mxErr,
17fd9 20 20 20 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f /* Stop repo
17fda 72 74 69 6e 67 20 65 72 72 6f 72 73 20 61 66 74 rting errors aft
17fdb 65 72 20 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a er this many */.
17fdc 20 20 69 6e 74 20 2a 70 6e 45 72 72 20 20 20 20 int *pnErr
17fdd 2f 2a 20 57 72 69 74 65 20 6e 75 6d 62 65 72 20 /* Write number
17fde 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 74 of errors seen t
17fdf 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 o this variable
17fe0 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a */.){. Pgno i;.
17fe1 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e int nRef;. In
17fe2 74 65 67 72 69 74 79 43 6b 20 73 43 68 65 63 6b tegrityCk sCheck
17fe3 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
17fe4 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68 t = p->pBt;. ch
17fe5 61 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 ar zErr[100];..
17fe6 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
17fe7 65 72 28 70 29 3b 0a 20 20 6e 52 65 66 20 3d 20 er(p);. nRef =
17fe8 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
17fe9 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
17fea 29 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 42 74 72 );. if( lockBtr
17feb 65 65 57 69 74 68 52 65 74 72 79 28 70 29 21 3d eeWithRetry(p)!=
17fec 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17fed 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 *pnErr = 1;.
17fee 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
17fef 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 ve(p);. retur
17ff0 6e 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 n sqlite3DbStrDu
17ff1 70 28 30 2c 20 22 63 61 6e 6e 6f 74 20 61 63 71 p(0, "cannot acq
17ff2 75 69 72 65 20 61 20 72 65 61 64 20 6c 6f 63 6b uire a read lock
17ff3 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
17ff4 22 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b ");. }. sCheck
17ff5 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 .pBt = pBt;. sC
17ff6 68 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 heck.pPager = pB
17ff7 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 68 t->pPager;. sCh
17ff8 65 63 6b 2e 6e 50 61 67 65 20 3d 20 70 61 67 65 eck.nPage = page
17ff9 72 50 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63 rPagecount(sChec
17ffa 6b 2e 70 42 74 29 3b 0a 20 20 73 43 68 65 63 6b k.pBt);. sCheck
17ffb 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a .mxErr = mxErr;.
17ffc 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 sCheck.nErr =
17ffd 30 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 0;. sCheck.mall
17ffe 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 ocFailed = 0;.
17fff 2a 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 *pnErr = 0;. if
18000 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d ( sCheck.nPage==
18001 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 0 ){. unlockB
18002 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 treeIfUnused(pBt
18003 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
18004 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
18005 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
18006 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 sCheck.anRef =
18007 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 sqlite3Malloc( (
18008 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a sCheck.nPage+1)*
18009 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e sizeof(sCheck.an
1800a 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 Ref[0]) );. if(
1800b 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 !sCheck.anRef )
1800c 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 {. unlockBtre
1800d 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
1800e 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a *pnErr = 1;.
1800f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
18010 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 Leave(p);. re
18011 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f turn 0;. }. fo
18012 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b r(i=0; i<=sCheck
18013 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 .nPage; i++){ sC
18014 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 heck.anRef[i] =
18015 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 0; }. i = PENDI
18016 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
18017 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 );. if( i<=sChe
18018 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 ck.nPage ){.
18019 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 sCheck.anRef[i]
1801a 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 = 1;. }. sqlit
1801b 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 e3StrAccumInit(&
1801c 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a sCheck.errMsg, z
1801d 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 Err, sizeof(zErr
1801e 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a ), 20000);.. /*
1801f 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 Check the integ
18020 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 rity of the free
18021 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 list. */. chec
18022 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 kList(&sCheck, 1
18023 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d , get4byte(&pBt-
18024 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
18025 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2]),.
18026 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e get4byte(&pBt->
18027 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
18028 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 ]), "Main freeli
18029 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 st: ");.. /* Ch
1802a 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c eck all the tabl
1802b 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 es.. */. for(i
1802c 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 =0; (int)i<nRoot
1802d 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 && sCheck.mxErr
1802e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
1802f 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f aRoot[i]==0 ) co
18030 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 ntinue;.#ifndef
18031 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
18032 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
18033 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 Bt->autoVacuum &
18034 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a & aRoot[i]>1 ){.
18035 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
18036 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 p(&sCheck, aRoot
18037 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 [i], PTRMAP_ROOT
18038 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 PAGE, 0, 0);.
18039 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 }.#endif. ch
1803a 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 eckTreePage(&sCh
1803b 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 eck, aRoot[i], "
1803c 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f List of tree roo
1803d 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f ts: ");. }.. /
1803e 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 * Make sure ever
1803f 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 y page in the fi
18040 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 le is referenced
18041 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b . */. for(i=1;
18042 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 i<=sCheck.nPage
18043 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 && sCheck.mxErr
18044 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 ; i++){.#ifdef S
18045 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
18046 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 ACUUM. if( sC
18047 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 heck.anRef[i]==0
18048 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 ){. checkA
18049 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b ppendMsg(&sCheck
1804a 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 , 0, "Page %d is
1804b 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 never used", i)
1804c 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.#else.
1804d 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 /* If the data
1804e 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 base supports au
1804f 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 to-vacuum, make
18050 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 sure no tables c
18051 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 ontain. ** re
18052 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e ferences to poin
18053 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 ter-map pages..
18054 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 */. if( sC
18055 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 heck.anRef[i]==0
18056 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 && . (PTR
18057 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 MAP_PAGENO(pBt,
18058 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 i)!=i || !pBt->a
18059 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 utoVacuum) ){.
1805a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
1805b 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 sg(&sCheck, 0, "
1805c 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 Page %d is never
1805d 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 used", i);.
1805e 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b }. if( sCheck
1805f 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 .anRef[i]!=0 &&
18060 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f . (PTRMAP_
18061 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d PAGENO(pBt, i)==
18062 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 i && pBt->autoVa
18063 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 cuum) ){. c
18064 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 heckAppendMsg(&s
18065 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 Check, 0, "Point
18066 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 er map page %d i
18067 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 s referenced", i
18068 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
18069 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
1806a 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 ure this analysi
1806b 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 s did not leave
1806c 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 any unref() page
1806d 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 s.. ** This is
1806e 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 an internal cons
1806f 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 istency check; a
18070 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 n integrity chec
18071 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e k. ** of the in
18072 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 tegrity check..
18073 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 */. unlockBtre
18074 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a eIfUnused(pBt);.
18075 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65 66 if( NEVER(nRef
18076 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 != sqlite3Pager
18077 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 Refcount(pBt->pP
18078 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63 68 ager)) ){. ch
18079 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 eckAppendMsg(&sC
1807a 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 heck, 0, .
1807b 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 "Outstanding pag
1807c 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72 6f e count goes fro
1807d 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69 6e m %d to %d durin
1807e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 22 g this analysis"
1807f 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73 71 ,. nRef, sq
18080 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
18081 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 0a nt(pBt->pPager).
18082 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a );. }.. /*
18083 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20 72 Clean up and r
18084 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20 20 eport errors..
18085 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 */. sqlite3Btre
18086 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71 6c eLeave(p);. sql
18087 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63 6b ite3_free(sCheck
18088 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20 73 .anRef);. if( s
18089 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c Check.mallocFail
1808a 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ed ){. sqlite
1808b 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 26 3StrAccumReset(&
1808c 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a sCheck.errMsg);.
1808d 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 *pnErr = sCh
1808e 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 20 eck.nErr+1;.
1808f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
18090 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e *pnErr = sCheck.
18091 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68 65 nErr;. if( sChe
18092 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71 6c ck.nErr==0 ) sql
18093 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 ite3StrAccumRese
18094 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 t(&sCheck.errMsg
18095 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 );. return sqli
18096 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 te3StrAccumFinis
18097 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 h(&sCheck.errMsg
18098 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
18099 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
1809a 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f RITY_CHECK */../
1809b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1809c 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 full pathname of
1809d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
1809e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1809f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 66 *.** The pager f
180a0 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 ilename is invar
180a1 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 iant as long as
180a2 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 the pager is.**
180a3 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 open so it is sa
180a4 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 fe to access wit
180a5 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65 hout the BtShare
180a6 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 d mutex..*/.SQLI
180a7 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
180a8 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
180a9 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 42 reeGetFilename(B
180aa 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
180ab 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 rt( p->pBt->pPag
180ac 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 er!=0 );. retur
180ad 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 n sqlite3PagerFi
180ae 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 lename(p->pBt->p
180af 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Pager);.}../*.**
180b0 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 Return the path
180b1 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 name of the jour
180b2 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68 69 nal file for thi
180b3 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 s database. The
180b4 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 return.** value
180b5 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 of this routine
180b6 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 61 is the same rega
180b7 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
180b8 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 r the journal fi
180b9 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 le.** has been c
180ba 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a 2a reated or not..*
180bb 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6a *.** The pager j
180bc 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 ournal filename
180bd 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20 is invariant as
180be 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 65 long as the page
180bf 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 r is.** open so
180c0 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 63 it is safe to ac
180c1 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 65 cess without the
180c2 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e BtShared mutex.
180c3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
180c4 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
180c5 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f qlite3BtreeGetJo
180c6 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 urnalname(Btree
180c7 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
180c8 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 ->pBt->pPager!=0
180c9 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c );. return sql
180ca 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
180cb 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 name(p->pBt->pPa
180cc 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ger);.}../*.** R
180cd 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 eturn non-zero i
180ce 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
180cf 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 is active..*/.SQ
180d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
180d1 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
180d2 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 nTrans(Btree *p)
180d3 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 {. assert( p==0
180d4 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
180d5 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
180d6 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e tex) );. return
180d7 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72 61 (p && (p->inTra
180d8 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 ns==TRANS_WRITE)
180d9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
180da 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 rn non-zero if a
180db 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 29 read (or write)
180dc 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
180dd 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 active..*/.SQLIT
180de 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
180df 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 lite3BtreeIsInRe
180e0 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 adTrans(Btree *p
180e1 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 ){. assert( p )
180e2 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
180e3 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
180e4 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
180e5 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54 72 return p->inTr
180e6 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 3b ans!=TRANS_NONE;
180e7 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
180e8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
180e9 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 74 reeIsInBackup(Bt
180ea 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
180eb 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 t( p );. assert
180ec 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
180ed 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
180ee 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 x) );. return p
180ef 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d 0a ->nBackup!=0;.}.
180f0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
180f1 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 tion returns a p
180f2 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 62 ointer to a blob
180f3 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 of memory assoc
180f4 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 iated with.** a
180f5 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 74 single shared-bt
180f6 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20 ree. The memory
180f7 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65 6e is used by clien
180f8 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20 6f t code for its o
180f9 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 wn.** purposes (
180fa 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 for example, to
180fb 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 76 store a high-lev
180fc 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 el schema associ
180fd 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 ated with .** th
180fe 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29 2e e shared-btree).
180ff 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 The btree layer
18100 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65 6e manages referen
18101 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 ce counting issu
18102 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 es..**.** The fi
18103 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 rst time this is
18104 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 called on a sha
18105 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 65 red-btree, nByte
18106 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 s bytes of memor
18107 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74 y.** are allocat
18108 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20 ed, zeroed, and
18109 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
1810a 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 68 caller. For each
1810b 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20 subsequent .**
1810c 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 20 call the nBytes
1810d 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e parameter is ign
1810e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e 74 ored and a point
1810f 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 er to the same b
18110 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 lob.** of memory
18111 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a returned. .**.*
18112 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73 20 * If the nBytes
18113 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 61 parameter is 0 a
18114 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d nd the blob of m
18115 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 65 emory has not ye
18116 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 t been.** alloca
18117 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e ted, a null poin
18118 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
18119 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61 73 If the blob has
1811a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a already been.**
1811b 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 allocated, it i
1811c 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e 6f s returned as no
1811d 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 rmal..**.** Just
1811e 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 72 before the shar
1811f 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f 73 ed-btree is clos
18120 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e ed, the function
18121 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 0a passed as the .
18122 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 6e ** xFree argumen
18123 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 t when the memor
18124 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 y allocation was
18125 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 64 made is invoked
18126 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 on the .** blob
18127 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 of allocated me
18128 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63 74 mory. This funct
18129 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 ion should not c
1812a 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 all sqlite3_free
1812b 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 6d ().** on the mem
1812c 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20 6c ory, the btree l
1812d 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e 0a ayer does that..
1812e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1812f 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 E void *sqlite3B
18130 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 treeSchema(Btree
18131 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c *p, int nBytes,
18132 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76 6f void(*xFree)(vo
18133 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 72 id *)){. BtShar
18134 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
18135 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
18136 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 Enter(p);. if(
18137 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26 26 !pBt->pSchema &&
18138 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 70 nBytes ){. p
18139 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 Bt->pSchema = sq
1813a 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
1813b 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42 74 nBytes);. pBt
1813c 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d 20 ->xFreeSchema =
1813d 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71 6c xFree;. }. sql
1813e 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1813f 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74 2d );. return pBt-
18140 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a 0a >pSchema;.}../*.
18141 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
18142 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
18143 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20 75 CHE if another u
18144 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ser of the same
18145 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65 65 shared .** btree
18146 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 as the argument
18147 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 6e handle holds an
18148 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
18149 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 on the .** sqlit
1814a 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 e_master table.
1814b 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 Otherwise SQLITE
1814c 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 _OK..*/.SQLITE_P
1814d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1814e 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 e3BtreeSchemaLoc
1814f 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ked(Btree *p){.
18150 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
18151 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
18152 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
18153 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
18154 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
18155 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 rc = queryShare
18156 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
18157 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 p, MASTER_ROOT,
18158 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61 73 READ_LOCK);. as
18159 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1815a 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 _OK || rc==SQLIT
1815b 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
1815c 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 ACHE );. sqlite
1815d 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1815e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1815f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18160 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
18161 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 E./*.** Obtain a
18162 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 lock on the tab
18163 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 le whose root pa
18164 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68 65 ge is iTab. The
18165 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77 72 .** lock is a wr
18166 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57 72 ite lock if isWr
18167 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65 20 itelock is true
18168 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a 2a or a read lock.*
18169 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73 65 * if it is false
1816a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1816b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1816c 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 treeLockTable(Bt
1816d 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 ree *p, int iTab
1816e 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b , u8 isWriteLock
1816f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
18170 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 LITE_OK;. if( p
18171 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
18172 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d 20 u8 lockType =
18173 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57 72 READ_LOCK + isWr
18174 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 iteLock;. ass
18175 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b 31 ert( READ_LOCK+1
18176 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a ==WRITE_LOCK );.
18177 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57 72 assert( isWr
18178 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 iteLock==0 || is
18179 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a WriteLock==1 );.
1817a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1817b 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63 Enter(p);. rc
1817c 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61 = querySharedCa
1817d 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 cheTableLock(p,
1817e 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b iTab, lockType);
1817f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
18180 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18181 72 63 20 3d 20 73 65 74 53 68 61 72 65 64 43 61 rc = setSharedCa
18182 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 cheTableLock(p,
18183 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b iTab, lockType);
18184 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
18185 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
18186 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
18187 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ;.}.#endif..#ifn
18188 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18189 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41 INCRBLOB./*.** A
1818a 72 67 75 6d 65 6e 74 20 70 43 73 72 20 6d 75 73 rgument pCsr mus
1818b 74 20 62 65 20 61 20 63 75 72 73 6f 72 20 6f 70 t be a cursor op
1818c 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 ened for writing
1818d 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54 4b 45 on an .** INTKE
1818e 59 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c Y table currentl
1818f 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 y pointing at a
18190 76 61 6c 69 64 20 74 61 62 6c 65 20 65 6e 74 72 valid table entr
18191 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 y. .** This func
18192 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 74 68 tion modifies th
18193 65 20 64 61 74 61 20 73 74 6f 72 65 64 20 61 73 e data stored as
18194 20 70 61 72 74 20 6f 66 20 74 68 61 74 20 65 6e part of that en
18195 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 try..** Only the
18196 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 20 6d 61 data content ma
18197 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 69 66 69 y only be modifi
18198 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f ed, it is not po
18199 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 63 68 61 ssible.** to cha
1819a 6e 67 65 20 74 68 65 20 6c 65 6e 67 74 68 20 6f nge the length o
1819b 66 20 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 f the data store
1819c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1819d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1819e 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43 BtreePutData(BtC
1819f 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 ursor *pCsr, u32
181a0 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 offset, u32 amt
181a1 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e , void *z){. in
181a2 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
181a3 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
181a4 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73 73 x(pCsr) );. ass
181a5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
181a6 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 ex_held(pCsr->pB
181a7 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
181a8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 43 73 );. assert(pCs
181a9 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e r->isIncrblobHan
181aa 64 6c 65 29 3b 0a 0a 20 20 72 65 73 74 6f 72 65 dle);.. restore
181ab 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
181ac 43 73 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Csr);. assert(
181ad 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 pCsr->eState!=CU
181ae 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
181af 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e );. if( pCsr->
181b0 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
181b1 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 ALID ){. retu
181b2 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b rn SQLITE_ABORT;
181b3 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b . }.. /* Check
181b4 20 73 6f 6d 65 20 70 72 65 63 6f 6e 64 69 74 69 some preconditi
181b5 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 ons: . ** (a)
181b6 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f the cursor is o
181b7 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2c pen for writing,
181b8 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 72 . ** (b) ther
181b9 65 20 69 73 20 6e 6f 20 72 65 61 64 2d 6c 6f 63 e is no read-loc
181ba 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 62 k on the table b
181bb 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 61 6e eing modified an
181bc 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 d. ** (c) the
181bd 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 cursor points a
181be 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 t a valid row of
181bf 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 an intKey table
181c0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 .. */. if( !pC
181c1 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 sr->wrFlag ){.
181c2 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
181c3 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 READONLY;. }.
181c4 61 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70 assert( !pCsr->p
181c5 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 0a 20 20 Bt->readOnly .
181c6 20 20 20 20 20 20 20 20 26 26 20 70 43 73 72 2d && pCsr-
181c7 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
181c8 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
181c9 20 29 3b 0a 20 20 72 63 20 3d 20 63 68 65 63 6b );. rc = check
181ca 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 ForReadConflicts
181cb 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 70 (pCsr->pBtree, p
181cc 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 Csr->pgnoRoot, p
181cd 43 73 72 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 Csr, 0);. if( r
181ce 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
181cf 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 /* The table
181d0 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 pCur points to
181d1 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 has a read lock
181d2 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 */. assert( r
181d3 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 c==SQLITE_LOCKED
181d4 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a _SHAREDCACHE );.
181d5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
181d6 20 7d 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 }. if( pCsr->e
181d7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
181d8 56 41 4c 49 44 20 7c 7c 20 21 70 43 73 72 2d 3e VALID || !pCsr->
181d9 61 70 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 apPage[pCsr->iPa
181da 67 65 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 ge]->intKey ){.
181db 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
181dc 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 _ERROR;. }.. r
181dd 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c eturn accessPayl
181de 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74 oad(pCsr, offset
181df 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 , amt, (unsigned
181e0 20 63 68 61 72 20 2a 29 7a 2c 20 30 2c 20 31 29 char *)z, 0, 1)
181e1 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 ;.}../* .** Set
181e2 61 20 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 a flag on this c
181e3 75 72 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 ursor to cache t
181e4 68 65 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 he locations of
181e5 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a pages from the .
181e6 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 ** overflow list
181e7 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
181e8 20 72 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 row. This is us
181e9 65 64 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 ed by cursors op
181ea 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 ened.** for incr
181eb 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 emental blob IO
181ec 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 only..**.** This
181ed 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 function sets a
181ee 20 66 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 flag only. The
181ef 61 63 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 actual page loca
181f0 74 69 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 tion cache.** (s
181f1 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f tored in BtCurso
181f2 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 r.aOverflow[]) i
181f3 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
181f4 75 73 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e used by function
181f5 0a 2a 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 .** accessPayloa
181f6 64 28 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 d() (the worker
181f7 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c function for sql
181f8 69 74 65 33 42 74 72 65 65 44 61 74 61 28 29 20 ite3BtreeData()
181f9 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 and.** sqlite3Bt
181fa 72 65 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a reePutData())..*
181fb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
181fc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
181fd 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 eeCacheOverflow(
181fe 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
181ff 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
18200 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
18201 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
18202 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
18203 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e d(pCur->pBtree->
18204 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
18205 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 assert(!pCur->is
18206 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b IncrblobHandle);
18207 0a 20 20 61 73 73 65 72 74 28 21 70 43 75 72 2d . assert(!pCur-
18208 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 >aOverflow);. p
18209 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 Cur->isIncrblobH
1820a 61 6e 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e andle = 1;.}.#en
1820b 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
1820c 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 **** End of btre
1820d 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
1820e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1820f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18210 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
18211 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
18212 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a backup.c *******
18213 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18214 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18215 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 ***/./*.** 2009
18216 4a 61 6e 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a January 28.**.**
18217 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
18218 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
18219 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1821a 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1821b 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1821c 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1821d 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1821e 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1821f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
18220 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
18221 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
18222 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
18223 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
18224 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
18225 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
18226 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
18227 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
18228 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18229 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1822a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1822b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1822c 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
1822d 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1822e 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
1822f 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
18230 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 0a 3_backup_XXX() .
18231 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 ** API functions
18232 20 61 6e 64 20 74 68 65 20 72 65 6c 61 74 65 64 and the related
18233 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a features..**.**
18234 20 24 49 64 3a 20 62 61 63 6b 75 70 2e 63 2c 76 $Id: backup.c,v
18235 20 31 2e 31 33 20 32 30 30 39 2f 30 33 2f 31 36 1.13 2009/03/16
18236 20 31 33 3a 31 39 3a 33 36 20 64 61 6e 69 65 6c 13:19:36 daniel
18237 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
18238 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 /* Macro to find
18239 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 the minimum of
1823a 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 two numeric valu
1823b 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d es..*/.#ifndef M
1823c 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 IN.# define MIN(
1823d 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 x,y) ((x)<(y)?(x
1823e 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f ):(y)).#endif../
1823f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 *.** Structure a
18240 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 llocated for eac
18241 68 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 h backup operati
18242 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 on..*/.struct sq
18243 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20 lite3_backup {.
18244 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 sqlite3* pDestD
18245 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 b; /* Des
18246 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
18247 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 e handle */. Bt
18248 72 65 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20 ree *pDest;
18249 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e /* Destin
1824a 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c ation b-tree fil
1824b 65 20 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74 e */. u32 iDest
1824c 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20 Schema;
1824d 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65 /* Original sche
1824e 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73 ma cookie in des
1824f 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e tination */. in
18250 74 20 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20 t bDestLocked;
18251 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f /* True o
18252 6e 63 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e nce a write-tran
18253 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 saction is open
18254 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50 on pDest */.. P
18255 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20 gno iNext;
18256 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
18257 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 number of the ne
18258 78 74 20 73 6f 75 72 63 65 20 70 61 67 65 20 74 xt source page t
18259 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69 o copy */. sqli
1825a 74 65 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20 te3* pSrcDb;
1825b 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 /* Source d
1825c 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
1825d 2f 0a 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b /. Btree *pSrc;
1825e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1825f 53 6f 75 72 63 65 20 62 2d 74 72 65 65 20 66 69 Source b-tree fi
18260 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b le */.. int rc;
18261 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18262 20 20 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63 /* Backup proc
18263 65 73 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a ess error code *
18264 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77 /.. /* These tw
18265 6f 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 o variables are
18266 73 65 74 20 62 79 20 65 76 65 72 79 20 63 61 6c set by every cal
18267 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70 l to backup_step
18268 28 29 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a (). They are. *
18269 2a 20 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20 * read by calls
1826a 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e to backup_remain
1826b 69 6e 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70 ing() and backup
1826c 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20 _pagecount()..
1826d 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 */. Pgno nRemai
1826e 6e 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a ning; /*
1826f 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
18270 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f left to copy */
18271 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75 . Pgno nPagecou
18272 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 nt; /* T
18273 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
18274 61 67 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a ages to copy */.
18275 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
18276 70 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e p *pNext; /* N
18277 65 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63 ext backup assoc
18278 69 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63 iated with sourc
18279 65 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f e pager */.};../
1827a 2a 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45 *.** THREAD SAFE
1827b 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 TY NOTES:.**.**
1827c 20 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65 Once it has be
1827d 65 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 en created using
1827e 20 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 backup_init(),
1827f 61 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 a single sqlite3
18280 5f 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 _backup.** str
18281 75 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63 ucture may be ac
18282 63 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67 cessed via two g
18283 72 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d roups of thread-
18284 73 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 safe entry point
18285 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 s:.**.** * V
18286 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 ia the sqlite3_b
18287 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 ackup_XXX() API
18288 66 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f function backup_
18289 73 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 step() and .**
1828a 20 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 backup_fini
1828b 73 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 sh(). Both these
1828c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 functions obtai
1828d 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 n the source dat
1828e 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 abase.** h
1828f 61 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 andle mutex and
18290 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 the mutex associ
18291 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f ated with the so
18292 75 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a urce BtShared .*
18293 2a 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 * structur
18294 65 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72 e, in that order
18295 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 ..**.** * Vi
18296 61 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61 a the BackupUpda
18297 74 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52 te() and BackupR
18298 65 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f estart() functio
18299 6e 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a ns, which are.**
1829a 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 invoked b
1829b 79 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 y the pager laye
1829c 72 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69 r to report vari
1829d 6f 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 ous state change
1829e 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 s in.** th
1829f 65 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73 e page cache ass
182a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
182a1 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
182a2 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 . The mutex.**
182a3 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 associated
182a4 77 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 with the source
182a5 64 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65 database BtShare
182a6 64 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c d structure will
182a7 20 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 always .**
182a8 20 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65 be held when e
182a9 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66 ither of these f
182aa 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 unctions are inv
182ab 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 oked..**.** Th
182ac 65 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f e other sqlite3_
182ad 62 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 backup_XXX() API
182ae 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b functions, back
182af 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 up_remaining() a
182b0 6e 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 nd.** backup_p
182b1 61 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e agecount() are n
182b2 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66 ot thread-safe f
182b3 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 unctions. If the
182b4 79 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 y are called.**
182b5 20 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 while some oth
182b6 65 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c er thread is cal
182b7 6c 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70 ling backup_step
182b8 28 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e () or backup_fin
182b9 69 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 ish(),.** the
182ba 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 values returned
182bb 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 may be invalid.
182bc 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 There is no way
182bd 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a for a call to.**
182be 20 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 BackupUpdate(
182bf 29 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61 ) or BackupResta
182c0 72 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72 rt() to interfer
182c1 65 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65 e with backup_re
182c2 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f maining().** o
182c3 72 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 r backup_pagecou
182c4 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 nt()..**.** De
182c5 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 pending on the S
182c6 51 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 QLite configurat
182c7 69 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 ion, the databas
182c8 65 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 e handles and/or
182c9 0a 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20 .** the Btree
182ca 6f 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65 objects may have
182cb 20 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 their own mutex
182cc 65 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20 es that require
182cd 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f locking..** No
182ce 6e 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 n-sharable Btree
182cf 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 s (in-memory dat
182d0 61 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70 abases for examp
182d1 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 le), do not have
182d2 0a 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64 .** associated
182d3 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a mutexes..*/../*
182d4 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
182d5 6e 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 nter correspondi
182d6 6e 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a ng to database z
182d7 44 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c Db (i.e. "main",
182d8 20 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 "temp").** in c
182d9 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 onnection handle
182da 20 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20 pDb. If such a
182db 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 database cannot
182dc 62 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e be found, return
182dd 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 .** a NULL point
182de 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 er and write an
182df 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f error message to
182e0 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a pErrorDb..**.**
182e1 20 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64 If the "temp" d
182e2 61 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65 atabase is reque
182e3 73 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 sted, it may nee
182e4 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 d to be opened b
182e5 79 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 y this .** funct
182e6 69 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ion. If an error
182e7 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f occurs while do
182e8 69 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 ing so, return 0
182e9 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a and write an .*
182ea 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 * error message
182eb 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a to pErrorDb..*/.
182ec 73 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 static Btree *fi
182ed 6e 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 ndBtree(sqlite3
182ee 2a 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 *pErrorDb, sqlit
182ef 65 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 e3 *pDb, const c
182f0 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 har *zDb){. int
182f1 20 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 i = sqlite3Find
182f2 44 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 DbName(pDb, zDb)
182f3 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b ;.. if( i==1 ){
182f4 0a 20 20 20 20 50 61 72 73 65 20 73 50 61 72 73 . Parse sPars
182f5 65 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 e;. memset(&s
182f6 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 Parse, 0, sizeof
182f7 28 73 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73 (sParse));. s
182f8 50 61 72 73 65 2e 64 62 20 3d 20 70 44 62 3b 0a Parse.db = pDb;.
182f9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
182fa 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 penTempDatabase(
182fb 26 73 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 &sParse) ){.
182fc 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c sqlite3ErrorCl
182fd 65 61 72 28 26 73 50 61 72 73 65 29 3b 0a 20 20 ear(&sParse);.
182fe 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
182ff 28 70 45 72 72 6f 72 44 62 2c 20 73 50 61 72 73 (pErrorDb, sPars
18300 65 2e 72 63 2c 20 22 25 73 22 2c 20 73 50 61 72 e.rc, "%s", sPar
18301 73 65 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 se.zErrMsg);.
18302 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
18303 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 }. assert( s
18304 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 3d 3d 30 Parse.zErrMsg==0
18305 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 );. }.. if( i
18306 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 <0 ){. sqlite
18307 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 3Error(pErrorDb,
18308 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 SQLITE_ERROR, "
18309 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 unknown database
1830a 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 %s", zDb);.
1830b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
1830c 20 72 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 return pDb->aDb
1830d 5b 69 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a [i].pBt;.}../*.*
1830e 2a 20 43 72 65 61 74 65 20 61 6e 20 73 71 6c 69 * Create an sqli
1830f 74 65 33 5f 62 61 63 6b 75 70 20 70 72 6f 63 65 te3_backup proce
18310 73 73 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 ss to copy the c
18311 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 53 72 63 44 ontents of zSrcD
18312 62 20 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65 63 b from.** connec
18313 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72 63 tion handle pSrc
18314 44 62 20 74 6f 20 7a 44 65 73 74 44 62 20 69 6e Db to zDestDb in
18315 20 70 44 65 73 74 44 62 2e 20 49 66 20 73 75 63 pDestDb. If suc
18316 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a cessful, return.
18317 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
18318 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65 33 5f the new sqlite3_
18319 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a 2a backup object..*
1831a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
1831b 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 occurs, NULL is
1831c 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e returned and an
1831d 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
1831e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a error message.**
1831f 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 stored in datab
18320 61 73 65 20 68 61 6e 64 6c 65 20 70 44 65 73 74 ase handle pDest
18321 44 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 Db..*/.SQLITE_AP
18322 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 I sqlite3_backup
18323 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 *sqlite3_backup
18324 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 _init(. sqlite3
18325 2a 20 70 44 65 73 74 44 62 2c 20 20 20 20 20 20 * pDestDb,
18326 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18327 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 77 72 * Database to wr
18328 69 74 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 ite to */. cons
18329 74 20 63 68 61 72 20 2a 7a 44 65 73 74 44 62 2c t char *zDestDb,
1832a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1832b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 /* Name of dat
1832c 61 62 61 73 65 20 77 69 74 68 69 6e 20 70 44 65 abase within pDe
1832d 73 74 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 stDb */. sqlite
1832e 33 2a 20 70 53 72 63 44 62 2c 20 20 20 20 20 20 3* pSrcDb,
1832f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18330 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
18331 65 63 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 66 ection to read f
18332 72 6f 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 rom */. const c
18333 68 61 72 20 2a 7a 53 72 63 44 62 20 20 20 20 20 har *zSrcDb
18334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18335 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 * Name of databa
18336 73 65 20 77 69 74 68 69 6e 20 70 53 72 63 44 62 se within pSrcDb
18337 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
18338 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 _backup *p;
18339 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1833a 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
1833b 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 n */.. /* Lock
1833c 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
1833d 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 ase handle. The
1833e 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
1833f 62 61 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 base. ** handle
18340 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 69 is not locked i
18341 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 n this routine,
18342 62 75 74 20 69 74 20 69 73 20 6c 6f 63 6b 65 64 but it is locked
18343 20 69 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 in. ** sqlite3
18344 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 _backup_step().
18345 54 68 65 20 75 73 65 72 20 69 73 20 72 65 71 75 The user is requ
18346 69 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 ired to ensure t
18347 68 61 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68 65 hat no. ** othe
18348 72 20 74 68 72 65 61 64 20 61 63 63 65 73 73 65 r thread accesse
18349 73 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f s the destinatio
1834a 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 n handle for the
1834b 20 64 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 6f duration. ** o
1834c 66 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 f the backup ope
1834d 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 ration. Any att
1834e 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 empt to use the
1834f 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a destination. **
18350 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
18351 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 tion while a bac
18352 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 kup is in progre
18353 73 73 20 6d 61 79 20 63 61 75 73 65 0a 20 20 2a ss may cause. *
18354 2a 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 * a malfunction
18355 6f 72 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 or a deadlock..
18356 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
18357 74 65 78 5f 65 6e 74 65 72 28 70 53 72 63 44 62 tex_enter(pSrcDb
18358 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 ->mutex);. sqli
18359 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1835a 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b pDestDb->mutex);
1835b 0a 0a 20 20 69 66 28 20 70 53 72 63 44 62 3d 3d .. if( pSrcDb==
1835c 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 pDestDb ){. s
1835d 71 6c 69 74 65 33 45 72 72 6f 72 28 0a 20 20 20 qlite3Error(.
1835e 20 20 20 20 20 70 44 65 73 74 44 62 2c 20 53 51 pDestDb, SQ
1835f 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f 75 LITE_ERROR, "sou
18360 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 rce and destinat
18361 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 69 73 74 ion must be dist
18362 69 6e 63 74 22 0a 20 20 20 20 29 3b 0a 20 20 20 inct". );.
18363 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 p = 0;. }else
18364 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 {. /* Allocat
18365 65 20 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 e space for a ne
18366 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 w sqlite3_backup
18367 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 object */. p
18368 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 61 63 6b = (sqlite3_back
18369 75 70 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c up *)sqlite3_mal
1836a 6c 6f 63 28 73 69 7a 65 6f 66 28 73 71 6c 69 74 loc(sizeof(sqlit
1836b 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 e3_backup));.
1836c 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 if( !p ){.
1836d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 44 sqlite3Error(pD
1836e 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f estDb, SQLITE_NO
1836f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 MEM, 0);. }.
18370 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
18371 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65 allocation succe
18372 65 64 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 eded, populate t
18373 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 2a he new object. *
18374 2f 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 /. if( p ){.
18375 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 memset(p, 0, si
18376 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 zeof(sqlite3_bac
18377 6b 75 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70 53 kup));. p->pS
18378 72 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 rc = findBtree(p
18379 44 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20 DestDb, pSrcDb,
1837a 7a 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e zSrcDb);. p->
1837b 70 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65 pDest = findBtre
1837c 65 28 70 44 65 73 74 44 62 2c 20 70 44 65 73 74 e(pDestDb, pDest
1837d 44 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20 Db, zDestDb);.
1837e 20 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70 p->pDestDb = p
1837f 44 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70 DestDb;. p->p
18380 53 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a SrcDb = pSrcDb;.
18381 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 p->iNext = 1
18382 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 2d ;.. if( 0==p-
18383 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e 70 >pSrc || 0==p->p
18384 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a Dest ){. /*
18385 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29 20 6f One (or both) o
18386 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 f the named data
18387 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20 65 78 bases did not ex
18388 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20 68 61 ist. An error ha
18389 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 s. ** alrea
1838a 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 dy been written
1838b 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74 44 62 into the pDestDb
1838c 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 61 handle. All tha
1838d 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20 20 20 t is left.
1838e 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69 73 ** to do here is
1838f 20 66 72 65 65 20 74 68 65 20 73 71 6c 69 74 65 free the sqlite
18390 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63 74 75 3_backup structu
18391 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 re.. */.
18392 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
18393 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 30 3b p);. p = 0;
18394 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
18395 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 68 If everything h
18396 61 73 20 67 6f 6e 65 20 61 73 20 70 6c 61 6e 6e as gone as plann
18397 65 64 2c 20 61 74 74 61 63 68 20 74 68 65 20 62 ed, attach the b
18398 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 74 6f 20 ackup object to
18399 74 68 65 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 the. ** source
1839a 70 61 67 65 72 2e 20 54 68 65 20 73 6f 75 72 63 pager. The sourc
1839b 65 20 70 61 67 65 72 20 63 61 6c 6c 73 20 42 61 e pager calls Ba
1839c 63 6b 75 70 55 70 64 61 74 65 28 29 20 61 6e 64 ckupUpdate() and
1839d 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 BackupRestart()
1839e 0a 20 20 2a 2a 20 74 6f 20 6e 6f 74 69 66 79 20 . ** to notify
1839f 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 66 20 74 this module if t
183a0 68 65 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 he source file i
183a1 73 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 62 s modified mid-b
183a2 61 63 6b 75 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 ackup.. */. if
183a3 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( p ){. sqlit
183a4 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 20 e3_backup **pp;
183a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
183a6 6f 69 6e 74 65 72 20 74 6f 20 68 65 61 64 20 6f ointer to head o
183a7 66 20 70 61 67 65 72 73 20 62 61 63 6b 75 70 20 f pagers backup
183a8 6c 69 73 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 list */. sqli
183a9 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d te3BtreeEnter(p-
183aa 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 70 20 3d >pSrc);. pp =
183ab 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 sqlite3PagerBac
183ac 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 74 kupPtr(sqlite3Bt
183ad 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 reePager(p->pSrc
183ae 29 29 3b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 ));. p->pNext
183af 20 3d 20 2a 70 70 3b 0a 20 20 20 20 2a 70 70 20 = *pp;. *pp
183b0 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = p;. sqlite3
183b1 42 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 BtreeLeave(p->pS
183b2 72 63 29 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 rc);. p->pSrc
183b3 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d ->nBackup++;. }
183b4 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
183b5 78 5f 6c 65 61 76 65 28 70 44 65 73 74 44 62 2d x_leave(pDestDb-
183b6 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 >mutex);. sqlit
183b7 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
183b8 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 SrcDb->mutex);.
183b9 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
183ba 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 72 63 20 .** Argument rc
183bb 69 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 is an SQLite err
183bc 6f 72 20 63 6f 64 65 2e 20 52 65 74 75 72 6e 20 or code. Return
183bd 74 72 75 65 20 69 66 20 74 68 69 73 20 65 72 72 true if this err
183be 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 or is .** consid
183bf 65 72 65 64 20 66 61 74 61 6c 20 69 66 20 65 6e ered fatal if en
183c0 63 6f 75 6e 74 65 72 65 64 20 64 75 72 69 6e 67 countered during
183c1 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 a backup operat
183c2 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 73 0a ion. All errors.
183c3 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 ** are considere
183c4 64 20 66 61 74 61 6c 20 65 78 63 65 70 74 20 66 d fatal except f
183c5 6f 72 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 or SQLITE_BUSY a
183c6 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 nd SQLITE_LOCKED
183c7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
183c8 69 73 46 61 74 61 6c 45 72 72 6f 72 28 69 6e 74 isFatalError(int
183c9 20 72 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 rc){. return (
183ca 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc!=SQLITE_OK &&
183cb 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc!=SQLITE_BUSY
183cc 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c && rc!=SQLITE_L
183cd 4f 43 4b 45 44 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a OCKED);.}../*.**
183ce 20 50 61 72 61 6d 65 74 65 72 20 7a 53 72 63 44 Parameter zSrcD
183cf 61 74 61 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 ata points to a
183d0 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e buffer containin
183d1 67 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 0a g the data for .
183d2 2a 2a 20 70 61 67 65 20 69 53 72 63 50 67 20 66 ** page iSrcPg f
183d3 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64 rom the source d
183d4 61 74 61 62 61 73 65 2e 20 43 6f 70 79 20 74 68 atabase. Copy th
183d5 69 73 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 is data into the
183d6 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e .** destination
183d7 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 database..*/.st
183d8 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75 70 4f atic int backupO
183d9 6e 65 50 61 67 65 28 73 71 6c 69 74 65 33 5f 62 nePage(sqlite3_b
183da 61 63 6b 75 70 20 2a 70 2c 20 50 67 6e 6f 20 69 ackup *p, Pgno i
183db 53 72 63 50 67 2c 20 63 6f 6e 73 74 20 75 38 20 SrcPg, const u8
183dc 2a 7a 53 72 63 44 61 74 61 29 7b 0a 20 20 50 61 *zSrcData){. Pa
183dd 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 ger * const pDes
183de 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 tPager = sqlite3
183df 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 BtreePager(p->pD
183e0 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e est);. const in
183e1 74 20 6e 53 72 63 50 67 73 7a 20 3d 20 73 71 6c t nSrcPgsz = sql
183e2 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
183e3 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 Size(p->pSrc);.
183e4 20 69 6e 74 20 6e 44 65 73 74 50 67 73 7a 20 3d int nDestPgsz =
183e5 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
183e6 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 PageSize(p->pDes
183e7 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 t);. const int
183e8 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 53 72 63 nCopy = MIN(nSrc
183e9 50 67 73 7a 2c 20 6e 44 65 73 74 50 67 73 7a 29 Pgsz, nDestPgsz)
183ea 3b 0a 20 20 63 6f 6e 73 74 20 69 36 34 20 69 45 ;. const i64 iE
183eb 6e 64 20 3d 20 28 69 36 34 29 69 53 72 63 50 67 nd = (i64)iSrcPg
183ec 2a 28 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 0a *(i64)nSrcPgsz;.
183ed 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
183ee 54 45 5f 4f 4b 3b 0a 20 20 69 36 34 20 69 4f 66 TE_OK;. i64 iOf
183ef 66 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d f;.. assert( p-
183f0 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 29 3b 0a >bDestLocked );.
183f1 20 20 61 73 73 65 72 74 28 20 21 69 73 46 61 74 assert( !isFat
183f2 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 29 alError(p->rc) )
183f3 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 72 63 ;. assert( iSrc
183f4 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 Pg!=PENDING_BYTE
183f5 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 _PAGE(p->pSrc->p
183f6 42 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Bt) );. assert(
183f7 20 7a 53 72 63 44 61 74 61 20 29 3b 0a 0a 20 20 zSrcData );..
183f8 2f 2a 20 43 61 74 63 68 20 74 68 65 20 63 61 73 /* Catch the cas
183f9 65 20 77 68 65 72 65 20 74 68 65 20 64 65 73 74 e where the dest
183fa 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e ination is an in
183fb 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
183fc 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 70 61 and the. ** pa
183fd 67 65 20 73 69 7a 65 73 20 6f 66 20 74 68 65 20 ge sizes of the
183fe 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 source and desti
183ff 6e 61 74 69 6f 6e 20 64 69 66 66 65 72 2e 20 0a nation differ. .
18400 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63 50 */. if( nSrcP
18401 67 73 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 26 gsz!=nDestPgsz &
18402 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 & sqlite3PagerIs
18403 4d 65 6d 64 62 28 73 71 6c 69 74 65 33 42 74 72 Memdb(sqlite3Btr
18404 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 eePager(p->pDest
18405 29 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 )) ){. rc = S
18406 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
18407 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c }.. /* This l
18408 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f oop runs once fo
18409 72 20 65 61 63 68 20 64 65 73 74 69 6e 61 74 69 r each destinati
1840a 6f 6e 20 70 61 67 65 20 73 70 61 6e 6e 65 64 20 on page spanned
1840b 62 79 20 74 68 65 20 73 6f 75 72 63 65 20 0a 20 by the source .
1840c 20 2a 2a 20 70 61 67 65 2e 20 46 6f 72 20 65 61 ** page. For ea
1840d 63 68 20 69 74 65 72 61 74 69 6f 6e 2c 20 76 61 ch iteration, va
1840e 72 69 61 62 6c 65 20 69 4f 66 66 20 69 73 20 73 riable iOff is s
1840f 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f et to the byte o
18410 66 66 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74 68 ffset. ** of th
18411 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 e destination pa
18412 67 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 ge.. */. for(i
18413 4f 66 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e 53 Off=iEnd-(i64)nS
18414 72 63 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c 49 rcPgsz; rc==SQLI
18415 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 TE_OK && iOff<iE
18416 6e 64 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 50 nd; iOff+=nDestP
18417 67 73 7a 29 7b 0a 20 20 20 20 44 62 50 61 67 65 gsz){. DbPage
18418 20 2a 70 44 65 73 74 50 67 20 3d 20 30 3b 0a 20 *pDestPg = 0;.
18419 20 20 20 50 67 6e 6f 20 69 44 65 73 74 20 3d 20 Pgno iDest =
1841a 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65 73 (Pgno)(iOff/nDes
1841b 74 50 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69 66 tPgsz)+1;. if
1841c 28 20 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e 47 ( iDest==PENDING
1841d 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 _BYTE_PAGE(p->pD
1841e 65 73 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e 74 est->pBt) ) cont
1841f 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 53 51 inue;. if( SQ
18420 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 LITE_OK==(rc = s
18421 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
18422 44 65 73 74 50 61 67 65 72 2c 20 69 44 65 73 74 DestPager, iDest
18423 2c 20 26 70 44 65 73 74 50 67 29 29 0a 20 20 20 , &pDestPg)).
18424 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d && SQLITE_OK==
18425 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
18426 65 72 57 72 69 74 65 28 70 44 65 73 74 50 67 29 erWrite(pDestPg)
18427 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 ). ){. c
18428 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20 26 onst u8 *zIn = &
18429 7a 53 72 63 44 61 74 61 5b 69 4f 66 66 25 6e 53 zSrcData[iOff%nS
1842a 72 63 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20 75 rcPgsz];. u
1842b 38 20 2a 7a 44 65 73 74 44 61 74 61 20 3d 20 73 8 *zDestData = s
1842c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
1842d 74 61 28 70 44 65 73 74 50 67 29 3b 0a 20 20 20 ta(pDestPg);.
1842e 20 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26 7a u8 *zOut = &z
1842f 44 65 73 74 44 61 74 61 5b 69 4f 66 66 25 6e 44 DestData[iOff%nD
18430 65 73 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20 20 estPgsz];..
18431 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 64 61 74 /* Copy the dat
18432 61 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 a from the sourc
18433 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 e page into the
18434 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 destination page
18435 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 .. ** Then
18436 63 6c 65 61 72 20 74 68 65 20 42 74 72 65 65 20 clear the Btree
18437 6c 61 79 65 72 20 4d 65 6d 50 61 67 65 2e 69 73 layer MemPage.is
18438 49 6e 69 74 20 66 6c 61 67 2e 20 42 6f 74 68 20 Init flag. Both
18439 74 68 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20 20 this module.
1843a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 67 ** and the pag
1843b 65 72 20 63 6f 64 65 20 75 73 65 20 74 68 69 73 er code use this
1843c 20 74 72 69 63 6b 20 28 63 6c 65 61 72 69 6e 67 trick (clearing
1843d 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a the first byte.
1843e 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 ** of the
1843f 70 61 67 65 20 27 65 78 74 72 61 27 20 73 70 61 page 'extra' spa
18440 63 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 ce to invalidate
18441 20 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 the Btree layer
18442 73 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 s. ** cache
18443 64 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 70 d parse of the p
18444 61 67 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 73 age). MemPage.is
18445 49 6e 69 74 20 69 73 20 6d 61 72 6b 65 64 20 0a Init is marked .
18446 20 20 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 42 ** "MUST B
18447 45 20 46 49 52 53 54 22 20 66 6f 72 20 74 68 69 E FIRST" for thi
18448 73 20 70 75 72 70 6f 73 65 2e 0a 20 20 20 20 20 s purpose..
18449 20 2a 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 */. memcpy
1844a 28 7a 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f 70 (zOut, zIn, nCop
1844b 79 29 3b 0a 20 20 20 20 20 20 28 28 75 38 20 2a y);. ((u8 *
1844c 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 )sqlite3PagerGet
1844d 45 78 74 72 61 28 70 44 65 73 74 50 67 29 29 5b Extra(pDestPg))[
1844e 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0] = 0;. }.
1844f 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
18450 72 65 66 28 70 44 65 73 74 50 67 29 3b 0a 20 20 ref(pDestPg);.
18451 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
18452 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c }../*.** If pFil
18453 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6c e is currently l
18454 61 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 arger than iSize
18455 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 72 75 bytes, then tru
18456 6e 63 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20 65 ncate it to.** e
18457 78 61 63 74 6c 79 20 69 53 69 7a 65 20 62 79 74 xactly iSize byt
18458 65 73 2e 20 49 66 20 70 46 69 6c 65 20 69 73 20 es. If pFile is
18459 6e 6f 74 20 6c 61 72 67 65 72 20 74 68 61 6e 20 not larger than
1845a 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65 iSize bytes, the
1845b 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 n.** this functi
1845c 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
1845d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
1845e 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 TE_OK if everyth
1845f 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 ing is successfu
18460 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 l, or an SQLite
18461 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69 error .** code i
18462 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
18463 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
18464 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 backupTruncateF
18465 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ile(sqlite3_file
18466 20 2a 70 46 69 6c 65 2c 20 69 36 34 20 69 53 69 *pFile, i64 iSi
18467 7a 65 29 7b 0a 20 20 69 36 34 20 69 43 75 72 72 ze){. i64 iCurr
18468 65 6e 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ent;. int rc =
18469 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
1846a 65 28 70 46 69 6c 65 2c 20 26 69 43 75 72 72 65 e(pFile, &iCurre
1846b 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 nt);. if( rc==S
1846c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 43 75 72 QLITE_OK && iCur
1846d 72 65 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20 20 rent>iSize ){.
1846e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1846f 54 72 75 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 Truncate(pFile,
18470 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72 65 iSize);. }. re
18471 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
18472 2a 20 43 6f 70 79 20 6e 50 61 67 65 20 70 61 67 * Copy nPage pag
18473 65 73 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 es from the sour
18474 63 65 20 62 2d 74 72 65 65 20 74 6f 20 74 68 65 ce b-tree to the
18475 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f destination..*/
18476 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
18477 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
18478 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b tep(sqlite3_back
18479 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 up *p, int nPage
1847a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
1847b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1847c 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d ter(p->pSrcDb->m
1847d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 utex);. sqlite3
1847e 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 BtreeEnter(p->pS
1847f 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 rc);. if( p->pD
18480 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c estDb ){. sql
18481 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
18482 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 (p->pDestDb->mut
18483 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d ex);. }.. rc =
18484 20 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20 21 69 p->rc;. if( !i
18485 73 46 61 74 61 6c 45 72 72 6f 72 28 72 63 29 20 sFatalError(rc)
18486 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 20 63 ){. Pager * c
18487 6f 6e 73 74 20 70 53 72 63 50 61 67 65 72 20 3d onst pSrcPager =
18488 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 sqlite3BtreePag
18489 65 72 28 70 2d 3e 70 53 72 63 29 3b 20 20 20 20 er(p->pSrc);
1848a 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 72 /* Source pager
1848b 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 20 2a 20 */. Pager *
1848c 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72 const pDestPager
1848d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
1848e 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 20 ager(p->pDest);
1848f 20 20 2f 2a 20 44 65 73 74 20 70 61 67 65 72 20 /* Dest pager
18490 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 */. int ii;
18491 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18492 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
18493 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a rator variable *
18494 2f 0a 20 20 20 20 69 6e 74 20 6e 53 72 63 50 61 /. int nSrcPa
18495 67 65 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 ge = -1;
18496 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
18497 20 6f 66 20 73 6f 75 72 63 65 20 64 62 20 69 6e of source db in
18498 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 69 6e pages */. in
18499 74 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 t bCloseTrans =
1849a 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1849b 20 2f 2a 20 54 72 75 65 20 69 66 20 73 72 63 20 /* True if src
1849c 64 62 20 72 65 71 75 69 72 65 73 20 75 6e 6c 6f db requires unlo
1849d 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 2f 2a cking */.. /*
1849e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 If the source p
1849f 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c ager is currentl
184a0 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 y in a write-tra
184a1 6e 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 72 6e nsaction, return
184a2 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 . ** SQLITE_B
184a3 55 53 59 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e USY immediately.
184a4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
184a5 70 2d 3e 70 44 65 73 74 44 62 20 26 26 20 70 2d p->pDestDb && p-
184a6 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e 54 72 >pSrc->pBt->inTr
184a7 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
184a8 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 _WRITE ){.
184a9 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
184aa 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
184ab 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
184ac 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a K;. }.. /*
184ad 20 4c 6f 63 6b 20 74 68 65 20 64 65 73 74 69 6e Lock the destin
184ae 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 ation database,
184af 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f 63 if it is not loc
184b0 6b 65 64 20 61 6c 72 65 61 64 79 2e 20 2a 2f 0a ked already. */.
184b1 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
184b2 4b 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44 65 73 K==rc && p->bDes
184b3 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20 20 20 tLocked==0.
184b4 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 && SQLITE_OK==(r
184b5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
184b6 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 44 BeginTrans(p->pD
184b7 65 73 74 2c 20 32 29 29 20 0a 20 20 20 20 29 7b est, 2)) . ){
184b8 0a 20 20 20 20 20 20 70 2d 3e 62 44 65 73 74 4c . p->bDestL
184b9 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 ocked = 1;.
184ba 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
184bb 65 65 47 65 74 4d 65 74 61 28 70 2d 3e 70 44 65 eeGetMeta(p->pDe
184bc 73 74 2c 20 31 2c 20 26 70 2d 3e 69 44 65 73 74 st, 1, &p->iDest
184bd 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a Schema);. }..
184be 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
184bf 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d is no open read-
184c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 transaction on t
184c1 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
184c2 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20 se, open. **
184c3 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 one now. If a tr
184c4 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 ansaction is ope
184c5 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 69 ned here, then i
184c6 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 t will be closed
184c7 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 . ** before t
184c8 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 his function exi
184c9 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ts.. */. i
184ca 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
184cb 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 && 0==sqlite3Bt
184cc 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 reeIsInReadTrans
184cd 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 (p->pSrc) ){.
184ce 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
184cf 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 treeBeginTrans(p
184d0 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 ->pSrc, 0);.
184d1 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 bCloseTrans =
184d2 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 1;. }. .
184d3 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 65 72 /* Now that ther
184d4 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b e is a read-lock
184d5 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 on the source d
184d6 61 74 61 62 61 73 65 2c 20 71 75 65 72 79 20 74 atabase, query t
184d7 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 he. ** source
184d8 20 70 61 67 65 72 20 66 6f 72 20 74 68 65 20 6e pager for the n
184d9 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
184da 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
184db 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
184dc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
184dd 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
184de 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
184df 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e 53 72 (pSrcPager, &nSr
184e0 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 cPage);. }.
184e1 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e 50 61 for(ii=0; (nPa
184e2 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61 67 65 ge<0 || ii<nPage
184e3 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c 3d 28 ) && p->iNext<=(
184e4 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 26 26 Pgno)nSrcPage &&
184e5 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 !rc; ii++){.
184e6 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 const Pgno iS
184e7 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78 74 3b rcPg = p->iNext;
184e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
184e9 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 20 /* Source page
184ea 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 number */.
184eb 69 66 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 if( iSrcPg!=PEND
184ec 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d ING_BYTE_PAGE(p-
184ed 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b 0a 20 >pSrc->pBt) ){.
184ee 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 DbPage *p
184ef 53 72 63 50 67 3b 20 20 20 20 20 20 20 20 20 20 SrcPg;
184f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
184f1 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 /* Source pag
184f2 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 e object */.
184f3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
184f4 50 61 67 65 72 47 65 74 28 70 53 72 63 50 61 67 PagerGet(pSrcPag
184f5 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 er, iSrcPg, &pSr
184f6 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 cPg);. if
184f7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
184f8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
184f9 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 = backupOnePage(
184fa 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c 69 74 p, iSrcPg, sqlit
184fb 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
184fc 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20 20 20 SrcPg));.
184fd 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
184fe 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20 nref(pSrcPg);.
184ff 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
18500 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74 2b 2b p->iNext++
18501 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
18502 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
18503 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67 65 63 . p->nPagec
18504 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67 65 3b ount = nSrcPage;
18505 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d 61 69 . p->nRemai
18506 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67 65 2b ning = nSrcPage+
18507 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20 20 20 1-p->iNext;.
18508 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74 3e 28 if( p->iNext>(
18509 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 29 7b Pgno)nSrcPage ){
1850a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
1850b 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 LITE_DONE;.
1850c 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
1850d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 if( rc==SQLITE_D
1850e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e ONE ){. con
1850f 73 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73 st int nSrcPages
18510 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 ize = sqlite3Btr
18511 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d eeGetPageSize(p-
18512 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f >pSrc);. co
18513 6e 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67 nst int nDestPag
18514 65 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 esize = sqlite3B
18515 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 treeGetPageSize(
18516 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 p->pDest);.
18517 20 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61 int nDestTrunca
18518 74 65 3b 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 te;. . /*
18519 55 70 64 61 74 65 20 74 68 65 20 73 63 68 65 6d Update the schem
1851a 61 20 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 a version field
1851b 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 in the destinati
1851c 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 on database. Thi
1851d 73 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 6f s. ** is to
1851e 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
1851f 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 the schema-versi
18520 6f 6e 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 63 on really does c
18521 68 61 6e 67 65 20 69 6e 0a 20 20 20 20 20 20 2a hange in. *
18522 2a 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 * the case where
18523 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 the source and
18524 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
18525 62 61 73 65 73 20 68 61 76 65 20 74 68 65 0a 20 bases have the.
18526 20 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68 ** same sch
18527 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20 ema version..
18528 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 */. sqli
18529 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 te3BtreeUpdateMe
1852a 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 31 2c 20 ta(p->pDest, 1,
1852b 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b 31 p->iDestSchema+1
1852c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e );. if( p->
1852d 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 20 pDestDb ){.
1852e 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 sqlite3ResetI
1852f 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d nternalSchema(p-
18530 3e 70 44 65 73 74 44 62 2c 20 30 29 3b 0a 20 20 >pDestDb, 0);.
18531 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
18532 53 65 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74 Set nDestTruncat
18533 65 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e e to the final n
18534 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
18535 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f n the destinatio
18536 6e 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 n. ** datab
18537 61 73 65 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 ase. The complic
18538 61 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 ation here is th
18539 61 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 at the destinati
1853a 6f 6e 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a on page. **
1853b 20 73 69 7a 65 20 6d 61 79 20 62 65 20 64 69 66 size may be dif
1853c 66 65 72 65 6e 74 20 74 6f 20 74 68 65 20 73 6f ferent to the so
1853d 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 2e 20 urce page size.
1853e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1853f 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 ** If the source
18540 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 6d page size is sm
18541 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 aller than the d
18542 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 estination page
18543 73 69 7a 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20 size, . **
18544 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20 74 68 69 round up. In thi
18545 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 s case the call
18546 74 6f 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e to sqlite3OsTrun
18547 63 61 74 65 28 29 20 62 65 6c 6f 77 20 77 69 6c cate() below wil
18548 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 78 20 74 l. ** fix t
18549 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
1854a 69 6c 65 2e 20 48 6f 77 65 76 65 72 20 69 74 20 ile. However it
1854b 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 is important to
1854c 63 61 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71 call. ** sq
1854d 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
1854e 74 65 49 6d 61 67 65 28 29 20 68 65 72 65 20 73 teImage() here s
1854f 6f 20 74 68 61 74 20 61 6e 79 20 70 61 67 65 73 o that any pages
18550 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a in the . *
18551 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 * destination fi
18552 6c 65 20 74 68 61 74 20 6c 69 65 20 62 65 79 6f le that lie beyo
18553 6e 64 20 74 68 65 20 6e 44 65 73 74 54 72 75 6e nd the nDestTrun
18554 63 61 74 65 20 70 61 67 65 20 6d 61 72 6b 20 61 cate page mark a
18555 72 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 re. ** jour
18556 6e 61 6c 6c 65 64 20 62 79 20 50 61 67 65 72 43 nalled by PagerC
18557 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 ommitPhaseOne()
18558 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 before they are
18559 64 65 73 74 72 6f 79 65 64 0a 20 20 20 20 20 20 destroyed.
1855a 2a 2a 20 62 79 20 74 68 65 20 66 69 6c 65 20 74 ** by the file t
1855b 72 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 runcation..
1855c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 53 */. if( nS
1855d 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 rcPagesize<nDest
1855e 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 Pagesize ){.
1855f 20 20 20 20 69 6e 74 20 72 61 74 69 6f 20 3d 20 int ratio =
18560 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2f 6e 53 nDestPagesize/nS
18561 72 63 50 61 67 65 73 69 7a 65 3b 0a 20 20 20 20 rcPagesize;.
18562 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 nDestTruncat
18563 65 20 3d 20 28 6e 53 72 63 50 61 67 65 2b 72 61 e = (nSrcPage+ra
18564 74 69 6f 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 tio-1)/ratio;.
18565 20 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54 if( nDestT
18566 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 runcate==(int)PE
18567 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
18568 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 p->pDest->pBt) )
18569 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 {. nDes
1856a 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20 tTruncate--;.
1856b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1856c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 se{. nDes
1856d 74 54 72 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 tTruncate = nSrc
1856e 50 61 67 65 20 2a 20 28 6e 53 72 63 50 61 67 65 Page * (nSrcPage
1856f 73 69 7a 65 2f 6e 44 65 73 74 50 61 67 65 73 69 size/nDestPagesi
18570 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ze);. }.
18571 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 sqlite3PagerT
18572 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 44 65 runcateImage(pDe
18573 73 74 50 61 67 65 72 2c 20 6e 44 65 73 74 54 72 stPager, nDestTr
18574 75 6e 63 61 74 65 29 3b 0a 0a 20 20 20 20 20 20 uncate);..
18575 69 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 if( nSrcPagesize
18576 3c 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 <nDestPagesize )
18577 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 {. /* If
18578 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d the source page-
18579 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 size is smaller
1857a 74 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 than the destina
1857b 74 69 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a tion page-size,.
1857c 20 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 ** two e
1857d 78 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20 xtra things may
1857e 6e 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a need to happen:.
1857f 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
18580 20 20 20 2a 2a 20 20 20 2a 20 54 68 65 20 64 65 ** * The de
18581 73 74 69 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65 stination may ne
18582 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 ed to be truncat
18583 65 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 ed, and.
18584 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 **. **
18585 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 6f 6e * Data stored on
18586 20 74 68 65 20 70 61 67 65 73 20 69 6d 6d 65 64 the pages immed
18587 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 iately following
18588 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a the . **
18589 20 20 20 20 20 70 65 6e 64 69 6e 67 2d 62 79 74 pending-byt
1858a 65 20 70 61 67 65 20 69 6e 20 74 68 65 20 73 6f e page in the so
1858b 75 72 63 65 20 64 61 74 61 62 61 73 65 20 6d 61 urce database ma
1858c 79 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 20 y need to be.
1858d 20 20 20 20 20 2a 2a 20 20 20 20 20 63 6f 70 69 ** copi
1858e 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 ed into the dest
1858f 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
18590 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
18591 20 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 const i64 i
18592 53 69 7a 65 20 3d 20 28 69 36 34 29 6e 53 72 63 Size = (i64)nSrc
18593 50 61 67 65 73 69 7a 65 20 2a 20 28 69 36 34 29 Pagesize * (i64)
18594 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20 nSrcPage;.
18595 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
18596 20 63 6f 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 const pFile = s
18597 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 qlite3PagerFile(
18598 70 44 65 73 74 50 61 67 65 72 29 3b 0a 0a 20 20 pDestPager);..
18599 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 assert( pF
1859a 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 61 ile );. a
1859b 73 73 65 72 74 28 20 28 69 36 34 29 6e 44 65 73 ssert( (i64)nDes
1859c 74 54 72 75 6e 63 61 74 65 2a 28 69 36 34 29 6e tTruncate*(i64)n
1859d 44 65 73 74 50 61 67 65 73 69 7a 65 20 3e 3d 20 DestPagesize >=
1859e 69 53 69 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20 iSize || (.
1859f 20 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 nDestTr
185a0 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 uncate==(int)(PE
185a1 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
185a2 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 p->pDest->pBt)-1
185a3 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 ). &&
185a4 69 53 69 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42 iSize>=PENDING_B
185a5 59 54 45 20 26 26 20 69 53 69 7a 65 3c 3d 50 45 YTE && iSize<=PE
185a6 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74 NDING_BYTE+nDest
185a7 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20 Pagesize.
185a8 20 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ));. if(
185a9 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 SQLITE_OK==(rc
185aa 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f = sqlite3PagerCo
185ab 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 mmitPhaseOne(pDe
185ac 73 74 50 61 67 65 72 2c 20 30 2c 20 31 29 29 0a stPager, 0, 1)).
185ad 20 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 && SQLI
185ae 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63 TE_OK==(rc = bac
185af 6b 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 kupTruncateFile(
185b0 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20 pFile, iSize)).
185b1 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 && SQLIT
185b2 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
185b3 74 65 33 50 61 67 65 72 53 79 6e 63 28 70 44 65 te3PagerSync(pDe
185b4 73 74 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 stPager)).
185b5 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ){. i
185b6 36 34 20 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 64 iOff;.
185b7 20 20 20 69 36 34 20 69 45 6e 64 20 3d 20 4d 49 i64 iEnd = MI
185b8 4e 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b N(PENDING_BYTE +
185b9 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2c 20 nDestPagesize,
185ba 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 iSize);.
185bb 20 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 20 for(.
185bc 20 20 20 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f iOff=PENDING_
185bd 42 59 54 45 2b 6e 53 72 63 50 61 67 65 73 69 7a BYTE+nSrcPagesiz
185be 65 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 e; .
185bf 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
185c0 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20 iOff<iEnd; .
185c1 20 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e iOff+=n
185c2 53 72 63 50 61 67 65 73 69 7a 65 0a 20 20 20 20 SrcPagesize.
185c3 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
185c4 20 20 20 20 20 50 67 48 64 72 20 2a 70 53 72 63 PgHdr *pSrc
185c5 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 Pg = 0;.
185c6 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 const Pgno i
185c7 53 72 63 50 67 20 3d 20 28 50 67 6e 6f 29 28 28 SrcPg = (Pgno)((
185c8 69 4f 66 66 2f 6e 53 72 63 50 61 67 65 73 69 7a iOff/nSrcPagesiz
185c9 65 29 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 e)+1);.
185ca 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
185cb 61 67 65 72 47 65 74 28 70 53 72 63 50 61 67 65 agerGet(pSrcPage
185cc 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63 r, iSrcPg, &pSrc
185cd 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 Pg);.
185ce 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
185cf 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
185d0 20 20 20 20 75 38 20 2a 7a 44 61 74 61 20 3d 20 u8 *zData =
185d1 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
185d2 61 74 61 28 70 53 72 63 50 67 29 3b 0a 20 20 20 ata(pSrcPg);.
185d3 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
185d4 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
185d5 46 69 6c 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72 File, zData, nSr
185d6 63 50 61 67 65 73 69 7a 65 2c 20 69 4f 66 66 29 cPagesize, iOff)
185d7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
185d8 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
185d9 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 53 te3PagerUnref(pS
185da 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 rcPg);.
185db 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
185dc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
185dd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
185de 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e gerCommitPhaseOn
185df 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c e(pDestPager, 0,
185e0 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 0);. }. .
185e1 20 20 20 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 /* Finish
185e2 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20 74 committing the t
185e3 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 74 68 ransaction to th
185e4 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
185e5 74 61 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20 tabase. */.
185e6 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
185e7 72 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c rc. && SQL
185e8 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 ITE_OK==(rc = sq
185e9 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
185ea 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 PhaseTwo(p->pDes
185eb 74 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 t)). ){.
185ec 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
185ed 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 _DONE;. }.
185ee 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
185ef 66 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 f bCloseTrans is
185f0 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73 true, then this
185f1 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 function opened
185f2 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 a read transact
185f3 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 ion. ** on th
185f4 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
185f5 65 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61 e. Close the rea
185f6 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 d transaction he
185f7 72 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20 re. There is.
185f8 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 ** no need to c
185f9 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
185fa 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74 values of the bt
185fb 72 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65 ree methods here
185fc 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d , as. ** "com
185fd 6d 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d mitting" a read-
185fe 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e only transaction
185ff 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 cannot fail..
18600 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c */. if( bCl
18601 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 oseTrans ){.
18602 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 TESTONLY( int
18603 72 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53 rc2 );. TES
18604 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20 TONLY( rc2 = )
18605 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
18606 69 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 itPhaseOne(p->pS
18607 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45 rc, 0);. TE
18608 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 STONLY( rc2 |= )
18609 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1860a 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 mitPhaseTwo(p->p
1860b 53 72 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Src);. asse
1860c 72 74 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f rt( rc2==SQLITE_
1860d 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 OK );. }. .
1860e 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 p->rc = rc;.
1860f 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 }. if( p->pDes
18610 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 tDb ){. sqlit
18611 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
18612 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 ->pDestDb->mutex
18613 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
18614 42 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 BtreeLeave(p->pS
18615 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d rc);. sqlite3_m
18616 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53 utex_leave(p->pS
18617 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 rcDb->mutex);.
18618 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
18619 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 .** Release all
1861a 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 resources associ
1861b 61 74 65 64 20 77 69 74 68 20 61 6e 20 73 71 6c ated with an sql
1861c 69 74 65 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e ite3_backup* han
1861d 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1861e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1861f 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c ackup_finish(sql
18620 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b ite3_backup *p){
18621 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
18622 70 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 p **pp;
18623 20 20 20 20 20 20 20 20 2f 2a 20 50 74 72 20 74 /* Ptr t
18624 6f 20 68 65 61 64 20 6f 66 20 70 61 67 65 72 73 o head of pagers
18625 20 62 61 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a backup list */.
18626 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
18627 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 *mutex;
18628 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 /* Mutex
18629 74 6f 20 70 72 6f 74 65 63 74 20 73 6f 75 72 63 to protect sourc
1862a 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
1862b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
1862c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1862d 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
1862e 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a return */.. /*
1862f 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 Enter the mutex
18630 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f es */. sqlite3_
18631 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 mutex_enter(p->p
18632 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 SrcDb->mutex);.
18633 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
18634 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 6d er(p->pSrc);. m
18635 75 74 65 78 20 3d 20 70 2d 3e 70 53 72 63 44 62 utex = p->pSrcDb
18636 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66 28 20 70 ->mutex;. if( p
18637 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
18638 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
18639 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d nter(p->pDestDb-
1863a 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 >mutex);. }..
1863b 2f 2a 20 44 65 74 61 63 68 20 74 68 69 73 20 62 /* Detach this b
1863c 61 63 6b 75 70 20 66 72 6f 6d 20 74 68 65 20 73 ackup from the s
1863d 6f 75 72 63 65 20 70 61 67 65 72 2e 20 2a 2f 0a ource pager. */.
1863e 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 if( p->pDestDb
1863f 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71 6c ){. pp = sql
18640 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 ite3PagerBackupP
18641 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 tr(sqlite3BtreeP
18642 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a ager(p->pSrc));.
18643 20 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21 3d while( *pp!=
18644 70 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d 20 p ){. pp =
18645 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 &(*pp)->pNext;.
18646 20 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 }. *pp = p
18647 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 2d 3e ->pNext;. p->
18648 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b pSrc->nBackup--;
18649 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 . }.. /* If a
1864a 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
1864b 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 till open on the
1864c 20 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 Btree, roll it
1864d 62 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 back. */. sqlit
1864e 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 e3BtreeRollback(
1864f 70 2d 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a p->pDest);.. /*
18650 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 Set the error c
18651 6f 64 65 20 6f 66 20 74 68 65 20 64 65 73 74 69 ode of the desti
18652 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
18653 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20 handle. */. rc
18654 3d 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 = (p->rc==SQLITE
18655 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f _DONE) ? SQLITE_
18656 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71 OK : p->rc;. sq
18657 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 lite3Error(p->pD
18658 65 73 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a estDb, rc, 0);..
18659 20 20 2f 2a 20 45 78 69 74 20 74 68 65 20 6d 75 /* Exit the mu
1865a 74 65 78 65 73 20 61 6e 64 20 66 72 65 65 20 74 texes and free t
1865b 68 65 20 62 61 63 6b 75 70 20 63 6f 6e 74 65 78 he backup contex
1865c 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a t structure. */.
1865d 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 if( p->pDestDb
1865e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1865f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 mutex_leave(p->p
18660 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a DestDb->mutex);.
18661 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
18662 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29 eeLeave(p->pSrc)
18663 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 ;. if( p->pDest
18664 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 Db ){. sqlite
18665 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 3_free(p);. }.
18666 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
18667 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 eave(mutex);. r
18668 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
18669 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1866a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 mber of pages st
1866b 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 ill to be backed
1866c 20 75 70 20 61 73 20 6f 66 20 74 68 65 20 6d 6f up as of the mo
1866d 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c st recent.** cal
1866e 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
1866f 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 kup_step()..*/.S
18670 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
18671 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d lite3_backup_rem
18672 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 aining(sqlite3_b
18673 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 ackup *p){. ret
18674 75 72 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e urn p->nRemainin
18675 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 g;.}../*.** Retu
18676 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d rn the total num
18677 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
18678 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
18679 61 73 65 20 61 73 20 6f 66 20 74 68 65 20 6d 6f ase as of the mo
1867a 73 74 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 st .** recent ca
1867b 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ll to sqlite3_ba
1867c 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a ckup_step()..*/.
1867d 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1867e 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 qlite3_backup_pa
1867f 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f gecount(sqlite3_
18680 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 backup *p){. re
18681 74 75 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 turn p->nPagecou
18682 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 nt;.}../*.** Thi
18683 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
18684 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 63 lled after the c
18685 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 ontents of page
18686 69 50 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 iPage of the.**
18687 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 source database
18688 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 have been modifi
18689 65 64 2e 20 49 66 20 70 61 67 65 20 69 50 61 67 ed. If page iPag
1868a 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 e has already be
1868b 65 6e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e en .** copied in
1868c 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 to the destinati
1868d 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 on database, the
1868e 6e 20 74 68 65 20 64 61 74 61 20 77 72 69 74 74 n the data writt
1868f 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 en to the.** des
18690 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 tination is now
18691 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 54 68 65 invalidated. The
18692 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 destination cop
18693 79 20 6f 66 20 69 50 61 67 65 20 6e 65 65 64 73 y of iPage needs
18694 0a 2a 2a 20 74 6f 20 62 65 20 75 70 64 61 74 65 .** to be update
18695 64 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 64 d with the new d
18696 61 74 61 20 62 65 66 6f 72 65 20 74 68 65 20 62 ata before the b
18697 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 ackup operation
18698 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a is.** complete..
18699 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 **.** It is assu
1869a 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75 74 med that the mut
1869b 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ex associated wi
1869c 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 20 th the BtShared
1869d 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 object.** corres
1869e 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 ponding to the s
1869f 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 ource database i
186a0 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 s held when this
186a1 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 function is.**
186a2 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 called..*/.SQLIT
186a3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
186a4 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 qlite3BackupUpda
186a5 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 te(sqlite3_backu
186a6 70 20 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f p *pBackup, Pgno
186a7 20 69 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 iPage, const u8
186a8 20 2a 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 *aData){. sqli
186a9 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 te3_backup *p;
186aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
186ab 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
186ac 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 iable */. for(p
186ad 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 =pBackup; p; p=p
186ae 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 ->pNext){. as
186af 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
186b0 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 tex_held(p->pSrc
186b1 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
186b2 0a 20 20 20 20 69 66 28 20 21 69 73 46 61 74 61 . if( !isFata
186b3 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 lError(p->rc) &&
186b4 20 69 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 iPage<p->iNext
186b5 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
186b6 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 70 backup process p
186b7 20 68 61 73 20 61 6c 72 65 61 64 79 20 63 6f 70 has already cop
186b8 69 65 64 20 70 61 67 65 20 69 50 61 67 65 2e 20 ied page iPage.
186b9 42 75 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 But now it.
186ba 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 ** has been mod
186bb 69 66 69 65 64 20 62 79 20 61 20 74 72 61 6e 73 ified by a trans
186bc 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f action on the so
186bd 75 72 63 65 20 70 61 67 65 72 2e 20 43 6f 70 79 urce pager. Copy
186be 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 . ** the ne
186bf 77 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 w data into the
186c0 62 61 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f backup.. */
186c1 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 . int rc =
186c2 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c backupOnePage(p,
186c3 20 69 50 61 67 65 2c 20 61 44 61 74 61 29 3b 0a iPage, aData);.
186c4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
186c5 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 !=SQLITE_BUSY &&
186c6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b rc!=SQLITE_LOCK
186c7 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ED );. if(
186c8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
186c9 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d . p->rc =
186ca 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
186cb 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
186cc 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63 6b Restart the back
186cd 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69 73 up process. This
186ce 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
186cf 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 0a the pager layer.
186d0 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74 20 ** detects that
186d1 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 the database has
186d2 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 been modified b
186d3 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 y an external da
186d4 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
186d5 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 tion. In this ca
186d6 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 se there is no w
186d7 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 ay of knowing wh
186d8 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 ich of the.** pa
186d9 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62 65 ges that have be
186da 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 en copied into t
186db 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
186dc 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69 6c atabase are stil
186dd 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 l .** valid and
186de 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 which are not, s
186df 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 6f o the entire pro
186e0 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65 cess needs to be
186e1 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a restarted..**.*
186e2 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 * It is assumed
186e3 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 61 that the mutex a
186e4 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
186e5 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 he BtShared obje
186e6 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 ct.** correspond
186e7 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 ing to the sourc
186e8 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 65 e database is he
186e9 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ld when this fun
186ea 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c ction is.** call
186eb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
186ec 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
186ed 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 e3BackupRestart(
186ee 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
186ef 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 pBackup){. sqli
186f0 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 te3_backup *p;
186f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
186f2 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
186f3 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 iable */. for(p
186f4 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 =pBackup; p; p=p
186f5 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 ->pNext){. as
186f6 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
186f7 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 tex_held(p->pSrc
186f8 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
186f9 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 . p->iNext =
186fa 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 1;. }.}..#ifnde
186fb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 f SQLITE_OMIT_VA
186fc 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 CUUM./*.** Copy
186fd 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e the complete con
186fe 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 tent of pBtFrom
186ff 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 into pBtTo. A t
18700 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 ransaction.** mu
18701 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f 72 st be active for
18702 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a both files..**.
18703 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 66 ** The size of f
18704 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20 72 ile pTo may be r
18705 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20 6f educed by this o
18706 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 peration. If any
18707 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 thing .** goes w
18708 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61 rong, the transa
18709 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 ction on pTo is
1870a 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 rolled back. If
1870b 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20 successful, the
1870c 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
1870d 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 is committed bef
1870e 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
1870f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18710 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
18711 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 eCopyFile(Btree
18712 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 *pTo, Btree *pFr
18713 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 om){. int rc;.
18714 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
18715 62 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 b;. sqlite3Btre
18716 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20 73 eEnter(pTo);. s
18717 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
18718 28 70 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53 (pFrom);.. /* S
18719 65 74 20 75 70 20 61 6e 20 73 71 6c 69 74 65 33 et up an sqlite3
1871a 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20 _backup object.
1871b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e 70 sqlite3_backup.p
1871c 44 65 73 74 44 62 20 6d 75 73 74 20 62 65 20 73 DestDb must be s
1871d 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68 et. ** to 0. Th
1871e 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68 is is used by th
1871f 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
18720 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 62 61 63 s of sqlite3_bac
18721 6b 75 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20 kup_step(). **
18722 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b and sqlite3_back
18723 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 64 up_finish() to d
18724 65 74 65 63 74 20 74 68 61 74 20 74 68 65 79 20 etect that they
18725 61 72 65 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 are being called
18726 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 . ** from this
18727 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69 function, not di
18728 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 75 73 rectly by the us
18729 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 er.. */. memse
1872a 74 28 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 t(&b, 0, sizeof(
1872b 62 29 29 3b 0a 20 20 62 2e 70 53 72 63 44 62 20 b));. b.pSrcDb
1872c 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62 = pFrom->db;. b
1872d 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20 .pSrc = pFrom;.
1872e 20 62 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a b.pDest = pTo;.
1872f 20 20 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a b.iNext = 1;..
18730 20 20 2f 2a 20 30 78 37 46 46 46 46 46 46 46 20 /* 0x7FFFFFFF
18731 69 73 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69 is the hard limi
18732 74 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 t for the number
18733 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 20 64 of pages in a d
18734 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c atabase. ** fil
18735 65 2e 20 42 79 20 70 61 73 73 69 6e 67 20 74 68 e. By passing th
18736 69 73 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 is as the number
18737 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f 70 of pages to cop
18738 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 y to. ** sqlite
18739 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 3_backup_step(),
1873a 20 77 65 20 63 61 6e 20 67 75 61 72 61 6e 74 65 we can guarante
1873b 65 20 74 68 61 74 20 74 68 65 20 63 6f 70 79 20 e that the copy
1873c 66 69 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20 77 finishes . ** w
1873d 69 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20 63 ithin a single c
1873e 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20 65 all (unless an e
1873f 72 72 6f 72 20 6f 63 63 75 72 73 29 2e 20 54 68 rror occurs). Th
18740 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 e assert() state
18741 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73 ment. ** checks
18742 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e this assumption
18743 20 2d 20 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c - (p->rc) shoul
18744 64 20 62 65 20 73 65 74 20 74 6f 20 65 69 74 68 d be set to eith
18745 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a er SQLITE_DONE .
18746 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 ** or an error
18747 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 code.. */. sq
18748 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
18749 70 28 26 62 2c 20 30 78 37 46 46 46 46 46 46 46 p(&b, 0x7FFFFFFF
1874a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 62 2e 72 );. assert( b.r
1874b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
1874c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 rc = sqlite3_b
1874d 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 26 62 29 ackup_finish(&b)
1874e 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1874f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f TE_OK ){. pTo
18750 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 ->pBt->pageSizeF
18751 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 ixed = 0;. }..
18752 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18753 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c ve(pFrom);. sql
18754 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18755 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 To);. return rc
18756 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
18757 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d LITE_OMIT_VACUUM
18758 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
18759 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b **** End of back
1875a 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a up.c ***********
1875b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1875c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1875d 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1875e 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1875f 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a vdbemem.c ******
18760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18761 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18762 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
18763 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 May 26.**.** The
18764 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
18765 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
18766 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
18767 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
18768 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
18769 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1876a 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1876b 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1876c 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1876d 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1876e 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1876f 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
18770 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
18771 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
18772 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
18773 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
18774 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
18775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18778 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18779 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1877a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
1877b 6f 64 65 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 ode use to manip
1877c 75 6c 61 74 65 20 22 4d 65 6d 22 20 73 74 72 75 ulate "Mem" stru
1877d 63 74 75 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a cture. A "Mem".
1877e 2a 2a 20 73 74 6f 72 65 73 20 61 20 73 69 6e 67 ** stores a sing
1877f 6c 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 le value in the
18780 56 44 42 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e VDBE. Mem is an
18781 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72 opaque structur
18782 65 20 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c e visible.** onl
18783 79 20 77 69 74 68 69 6e 20 74 68 65 20 56 44 42 y within the VDB
18784 45 2e 20 20 49 6e 74 65 72 66 61 63 65 20 72 6f E. Interface ro
18785 75 74 69 6e 65 73 20 72 65 66 65 72 20 74 6f 20 utines refer to
18786 61 20 4d 65 6d 20 75 73 69 6e 67 20 74 68 65 0a a Mem using the.
18787 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 ** name sqlite_v
18788 61 6c 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 alue.**.** $Id:
18789 76 64 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 34 vdbemem.c,v 1.14
1878a 34 20 32 30 30 39 2f 30 35 2f 30 35 20 31 32 3a 4 2009/05/05 12:
1878b 35 34 3a 35 30 20 64 72 68 20 45 78 70 20 24 0a 54:50 drh Exp $.
1878c 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 */../*.** Call s
1878d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
1878e 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 andBlob() on the
1878f 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 supplied value
18790 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 (type Mem*).** P
18791 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f if required..*/
18792 0a 23 64 65 66 69 6e 65 20 65 78 70 61 6e 64 42 .#define expandB
18793 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c lob(P) (((P)->fl
18794 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 ags&MEM_Zero)?sq
18795 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
18796 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a ndBlob(P):0)../*
18797 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61 .** If pMem is a
18798 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20 n object with a
18799 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72 65 70 valid string rep
1879a 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 resentation, thi
1879b 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 s routine.** ens
1879c 75 72 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 ures the interna
1879d 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 l encoding for t
1879e 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 he string repres
1879f 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 entation is.** '
187a0 64 65 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 desiredEnc', one
187a1 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 38 2c of SQLITE_UTF8,
187a2 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 SQLITE_UTF16LE
187a3 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 or SQLITE_UTF16B
187a4 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d E..**.** If pMem
187a5 20 69 73 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 is not a string
187a6 20 6f 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 object, or the
187a7 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 encoding of the
187a8 73 74 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 string.** repres
187a9 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 entation is alre
187aa 61 64 79 20 73 74 6f 72 65 64 20 75 73 69 6e 67 ady stored using
187ab 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 65 the requested e
187ac 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 ncoding, then th
187ad 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 is.** routine is
187ae 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
187af 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
187b0 75 72 6e 65 64 20 69 66 20 74 68 65 20 63 6f 6e urned if the con
187b1 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63 63 65 version is succe
187b2 73 73 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 ssful (or not re
187b3 71 75 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 quired)..** SQLI
187b4 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 TE_NOMEM may be
187b5 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d 61 returned if a ma
187b6 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72 lloc() fails dur
187b7 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a ing conversion.*
187b8 2a 20 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 * between format
187b9 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
187ba 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
187bb 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
187bc 6e 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e ng(Mem *pMem, in
187bd 74 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 t desiredEnc){.
187be 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
187bf 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
187c0 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
187c1 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69 ;. assert( desi
187c2 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
187c3 54 46 38 20 7c 7c 20 64 65 73 69 72 65 64 45 6e TF8 || desiredEn
187c4 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c c==SQLITE_UTF16L
187c5 45 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 E. ||
187c6 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
187c7 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 TE_UTF16BE );.
187c8 69 66 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 if( !(pMem->flag
187c9 73 26 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d s&MEM_Str) || pM
187ca 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 em->enc==desired
187cb 45 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Enc ){. retur
187cc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
187cd 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
187ce 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
187cf 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 3_mutex_held(pMe
187d0 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
187d1 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
187d2 4d 49 54 5f 55 54 46 31 36 0a 20 20 72 65 74 75 MIT_UTF16. retu
187d3 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
187d4 0a 23 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d .#else.. /* Mem
187d5 54 72 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 Translate() may
187d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
187d7 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d or SQLITE_NOMEM
187d8 2e 20 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 . If NOMEM is re
187d9 74 75 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 turned,. ** the
187da 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f n the encoding o
187db 66 20 74 68 65 20 76 61 6c 75 65 20 6d 61 79 20 f the value may
187dc 6e 6f 74 20 68 61 76 65 20 63 68 61 6e 67 65 64 not have changed
187dd 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 .. */. rc = sq
187de 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e lite3VdbeMemTran
187df 73 6c 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 slate(pMem, (u8)
187e0 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 desiredEnc);. a
187e1 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 ssert(rc==SQLITE
187e2 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 _OK || rc==SQ
187e3 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 LITE_NOMEM);. a
187e4 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 ssert(rc==SQLITE
187e5 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e _OK || pMem->
187e6 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 29 enc!=desiredEnc)
187e7 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 ;. assert(rc==S
187e8 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 QLITE_NOMEM || p
187e9 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 Mem->enc==desire
187ea 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 dEnc);. return
187eb 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a rc;.#endif.}../*
187ec 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 4d .** Make sure pM
187ed 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 em->z points to
187ee 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 a writable alloc
187ef 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 ation of at leas
187f0 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a t .** n bytes..*
187f1 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f *.** If the memo
187f2 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c ry cell currentl
187f3 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e y contains strin
187f4 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a g or blob data.*
187f5 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 * and the third
187f6 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 argument passed
187f7 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
187f8 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a 2a is true, the .*
187f9 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e * current conten
187fa 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 t of the cell is
187fb 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68 65 preserved. Othe
187fc 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a rwise, it may.**
187fd 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20 20 be discarded.
187fe 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
187ff 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d 45 tion sets the ME
18800 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 M_Dyn flag and c
18801 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20 63 lears any xDel c
18802 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 allback..** It a
18803 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 lso clears MEM_E
18804 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 phem and MEM_Sta
18805 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65 73 tic. If the pres
18806 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a erve flag is .**
18807 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 not set, Mem.n
18808 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 is zeroed..*/.SQ
18809 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1880a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
1880b 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 row(Mem *pMem, i
1880c 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 nt n, int preser
1880d 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 31 ve){. assert( 1
1880e 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e >=. ((pMem->
1880f 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d zMalloc && pMem-
18810 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e >zMalloc==pMem->
18811 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 z) ? 1 : 0) +.
18812 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 (((pMem->flags
18813 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d &MEM_Dyn)&&pMem-
18814 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 >xDel) ? 1 : 0)
18815 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 + . ((pMem->f
18816 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 lags&MEM_Ephem)
18817 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 ? 1 : 0) + .
18818 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ((pMem->flags&ME
18819 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a 20 M_Static) ? 1 :
1881a 30 29 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 0). );. assert
1881b 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ( (pMem->flags&M
1881c 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
1881d 0a 0a 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e .. if( n<32 ) n
1881e 20 3d 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c = 32;. if( sql
1881f 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 ite3DbMallocSize
18820 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d (pMem->db, pMem-
18821 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 >zMalloc)<n ){.
18822 20 20 20 69 66 28 20 70 72 65 73 65 72 76 65 20 if( preserve
18823 26 26 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d && pMem->z==pMem
18824 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 ->zMalloc ){.
18825 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 pMem->z = pMe
18826 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c m->zMalloc = sql
18827 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
18828 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d ree(pMem->db, pM
18829 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 em->z, n);.
1882a 20 70 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 preserve = 0;.
1882b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1882c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d sqlite3DbFree(pM
1882d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d em->db, pMem->zM
1882e 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d alloc);. pM
1882f 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 em->zMalloc = sq
18830 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
18831 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 (pMem->db, n);.
18832 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
18833 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d preserve && pMem
18834 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 ->z && pMem->zMa
18835 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 lloc && pMem->z!
18836 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 =pMem->zMalloc )
18837 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 {. memcpy(pMe
18838 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d m->zMalloc, pMem
18839 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 ->z, pMem->n);.
1883a 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 }. if( pMem->f
1883b 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 lags&MEM_Dyn &&
1883c 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 pMem->xDel ){.
1883d 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f pMem->xDel((vo
1883e 69 64 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b id *)(pMem->z));
1883f 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 . }.. pMem->z
18840 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b = pMem->zMalloc;
18841 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d . if( pMem->z==
18842 30 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 0 ){. pMem->f
18843 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
18844 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d . }else{. pM
18845 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d em->flags &= ~(M
18846 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 EM_Ephem|MEM_Sta
18847 74 69 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d tic);. }. pMem
18848 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 ->xDel = 0;. re
18849 74 75 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 turn (pMem->z ?
1884a 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
1884b 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a TE_NOMEM);.}../*
1884c 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 76 .** Make the giv
1884d 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 en Mem object ME
1884e 4d 5f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 M_Dyn. In other
1884f 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 words, make it
18850 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 so.** that any T
18851 45 58 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 EXT or BLOB cont
18852 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e ent is stored in
18853 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
18854 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 from.** malloc(
18855 29 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c ). In this way,
18856 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 we know that th
18857 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 65 e memory is safe
18858 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 to be.** overwr
18859 69 74 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 itten or altered
1885a 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
1885b 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
1885c 65 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f ess or SQLITE_NO
1885d 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 MEM if malloc fa
1885e 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
1885f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18860 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 e3VdbeMemMakeWri
18861 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d teable(Mem *pMem
18862 29 7b 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 ){. int f;. as
18863 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
18864 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
18865 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
18866 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
18867 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
18868 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d gs&MEM_RowSet)==
18869 30 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 0 );. expandBlo
1886a 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 b(pMem);. f = p
1886b 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 Mem->flags;. if
1886c 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 ( (f&(MEM_Str|ME
1886d 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d M_Blob)) && pMem
1886e 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c ->z!=pMem->zMall
1886f 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 oc ){. if( sq
18870 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
18871 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b (pMem, pMem->n +
18872 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 2, 1) ){.
18873 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
18874 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 MEM;. }. p
18875 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 Mem->z[pMem->n]
18876 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a = 0;. pMem->z
18877 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b [pMem->n+1] = 0;
18878 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
18879 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
1887a 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
1887b 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1887c 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d If the given Mem
1887d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c * has a zero-fil
1887e 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 led tail, turn i
1887f 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 t into an ordina
18880 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 ry.** blob store
18881 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 d in dynamically
18882 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
18883 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
18884 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
18885 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 B.SQLITE_PRIVATE
18886 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
18887 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 MemExpandBlob(Me
18888 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 m *pMem){. if(
18889 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
1888a 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e M_Zero ){. in
1888b 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 t nByte;. ass
1888c 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 ert( pMem->flags
1888d 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 &MEM_Blob );.
1888e 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
1888f 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 flags&MEM_RowSet
18890 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )==0 );. asse
18891 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
18892 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
18893 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
18894 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f mutex) );.. /
18895 2a 20 53 65 74 20 6e 42 79 74 65 20 74 6f 20 74 * Set nByte to t
18896 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
18897 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 es required to s
18898 74 6f 72 65 20 74 68 65 20 65 78 70 61 6e 64 65 tore the expande
18899 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e d blob. */. n
1889a 42 79 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b Byte = pMem->n +
1889b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a pMem->u.nZero;.
1889c 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 if( nByte<=0
1889d 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 ){. nByte
1889e 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 1;. }. i
1889f 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
188a0 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 mGrow(pMem, nByt
188a1 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 e, 1) ){. r
188a2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
188a3 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d EM;. }.. m
188a4 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 emset(&pMem->z[p
188a5 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d Mem->n], 0, pMem
188a6 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 ->u.nZero);.
188a7 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d pMem->n += pMem-
188a8 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d >u.nZero;. pM
188a9 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d em->flags &= ~(M
188aa 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d EM_Zero|MEM_Term
188ab 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
188ac 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
188ad 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 dif.../*.** Make
188ae 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 sure the given
188af 4d 65 6d 20 69 73 20 5c 75 30 30 30 30 20 74 65 Mem is \u0000 te
188b0 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c rminated..*/.SQL
188b1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
188b2 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
188b3 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a lTerminate(Mem *
188b4 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 pMem){. assert(
188b5 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
188b6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
188b7 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
188b8 65 78 29 20 29 3b 0a 20 20 69 66 28 20 28 70 4d ex) );. if( (pM
188b9 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
188ba 54 65 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 Term)!=0 || (pMe
188bb 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 m->flags & MEM_S
188bc 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 tr)==0 ){. re
188bd 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
188be 20 20 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 /* Nothing to
188bf 64 6f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 do */. }. if(
188c0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
188c1 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e ow(pMem, pMem->n
188c2 2b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 65 +2, 1) ){. re
188c3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
188c4 4d 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a M;. }. pMem->z
188c5 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 [pMem->n] = 0;.
188c6 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e pMem->z[pMem->n
188c7 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d +1] = 0;. pMem-
188c8 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 >flags |= MEM_Te
188c9 72 6d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c rm;. return SQL
188ca 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
188cb 20 41 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 Add MEM_Str to
188cc 74 68 65 20 73 65 74 20 6f 66 20 72 65 70 72 65 the set of repre
188cd 73 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 sentations for t
188ce 68 65 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e he given Mem. N
188cf 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f umbers.** are co
188d0 6e 76 65 72 74 65 64 20 75 73 69 6e 67 20 73 71 nverted using sq
188d1 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
188d2 2e 20 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 . Converting a
188d3 42 4c 4f 42 20 74 6f 20 61 20 73 74 72 69 6e 67 BLOB to a string
188d4 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a .** is a no-op..
188d5 2a 2a 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 **.** Existing r
188d6 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d epresentations M
188d7 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 EM_Int and MEM_R
188d8 65 61 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e eal are *not* in
188d9 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a validated..**.**
188da 20 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 A MEM_Null valu
188db 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 e will never be
188dc 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 passed to this f
188dd 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 unction. This fu
188de 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 nction is.** use
188df 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 d for converting
188e0 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20 values to text
188e1 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f for returning to
188e2 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20 the user (i.e.
188e3 76 69 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 via.** sqlite3_v
188e4 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 alue_text()), or
188e5 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 for ensuring th
188e6 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 at values to be
188e7 75 73 65 64 20 61 73 20 62 74 72 65 65 0a 2a 2a used as btree.**
188e8 20 6b 65 79 73 20 61 72 65 20 73 74 72 69 6e 67 keys are string
188e9 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 s. In the former
188ea 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 case a NULL poi
188eb 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
188ec 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 the.** user and
188ed 20 74 68 65 20 6c 61 74 65 72 20 69 73 20 61 6e the later is an
188ee 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 internal progra
188ef 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a mming error..*/.
188f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
188f1 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
188f2 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a mStringify(Mem *
188f3 70 4d 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a pMem, int enc){.
188f4 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
188f5 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d E_OK;. int fg =
188f6 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 pMem->flags;.
188f7 63 6f 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 20 const int nByte
188f8 3d 20 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 = 32;.. assert(
188f9 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
188fa 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
188fb 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
188fc 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
188fd 20 21 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 !(fg&MEM_Zero)
188fe 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 );. assert( !(f
188ff 67 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 g&(MEM_Str|MEM_B
18900 6c 6f 62 29 29 20 29 3b 0a 20 20 61 73 73 65 72 lob)) );. asser
18901 74 28 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d t( fg&(MEM_Int|M
18902 45 4d 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 EM_Real) );. as
18903 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
18904 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d gs&MEM_RowSet)==
18905 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 0 );. assert( E
18906 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
18907 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 ENT(pMem) );...
18908 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
18909 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 MemGrow(pMem, nB
1890a 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 yte, 0) ){. r
1890b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1890c 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f EM;. }.. /* Fo
1890d 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 r a Real or Inte
1890e 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74 65 33 ger, use sqlite3
1890f 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 70 72 _mprintf() to pr
18910 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d 38 0a oduce the UTF-8.
18911 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 ** string repr
18912 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
18913 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 e value. Then, i
18914 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 65 f the required e
18915 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 ncoding. ** is
18916 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d UTF-16le or UTF-
18917 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 16be do a transl
18918 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a ation.. ** . *
18919 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 * FIX ME: It wou
1891a 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 66 20 ld be better if
1891b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1891c 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d () could do UTF-
1891d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 16.. */. if( f
1891e 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 g & MEM_Int ){.
1891f 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
18920 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d ntf(nByte, pMem-
18921 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d >z, "%lld", pMem
18922 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b ->u.i);. }else{
18923 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 67 20 . assert( fg
18924 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 & MEM_Real );.
18925 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
18926 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e tf(nByte, pMem->
18927 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 z, "%!.15g", pMe
18928 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 m->r);. }. pMe
18929 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 m->n = sqlite3St
1892a 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b rlen30(pMem->z);
1892b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 . pMem->enc = S
1892c 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d QLITE_UTF8;. pM
1892d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d em->flags |= MEM
1892e 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 _Str|MEM_Term;.
1892f 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
18930 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c geEncoding(pMem,
18931 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 enc);. return
18932 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d rc;.}../*.** Mem
18933 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f ory cell pMem co
18934 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 ntains the conte
18935 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 xt of an aggrega
18936 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 te function..**
18937 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c This routine cal
18938 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 ls the finalize
18939 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 method for that
1893a 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a function. The.*
1893b 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 * result of the
1893c 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74 6f aggregate is sto
1893d 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d red back into pM
1893e 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e em..**.** Return
1893f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 SQLITE_ERROR if
18940 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 the finalizer r
18941 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e eports an error.
18942 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f SQLITE_OK.** o
18943 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c therwise..*/.SQL
18944 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
18945 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 sqlite3VdbeMemFi
18946 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d nalize(Mem *pMem
18947 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 , FuncDef *pFunc
18948 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
18949 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 LITE_OK;. if( p
1894a 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 Func && pFunc->x
1894b 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20 Finalize ){.
1894c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1894d 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ctx;. assert(
1894e 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
1894f 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 MEM_Null)!=0 ||
18950 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 pFunc==pMem->u.p
18951 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65 72 Def );. asser
18952 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
18953 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
18954 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
18955 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d utex) );. mem
18956 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69 7a set(&ctx, 0, siz
18957 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20 63 eof(ctx));. c
18958 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d tx.s.flags = MEM
18959 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 _Null;. ctx.s
1895a 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a .db = pMem->db;.
1895b 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 ctx.pMem = p
1895c 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 Mem;. ctx.pFu
1895d 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 nc = pFunc;.
1895e 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 pFunc->xFinalize
1895f 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73 65 (&ctx);. asse
18960 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c rt( 0==(pMem->fl
18961 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 ags&MEM_Dyn) &&
18962 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 !pMem->xDel );.
18963 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
18964 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d (pMem->db, pMem-
18965 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 6d >zMalloc);. m
18966 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 78 emcpy(pMem, &ctx
18967 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e 73 .s, sizeof(ctx.s
18968 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 28 63 74 ));. rc = (ct
18969 78 2e 69 73 45 72 72 6f 72 3f 53 51 4c 49 54 45 x.isError?SQLITE
1896a 5f 45 52 52 4f 52 3a 53 51 4c 49 54 45 5f 4f 4b _ERROR:SQLITE_OK
1896b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1896c 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 rc;.}../*.** If
1896d 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
1896e 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72 69 6e contains a strin
1896f 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d 75 73 g value that mus
18970 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a t be freed by.**
18971 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 invoking an ext
18972 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 ernal callback,
18973 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c free it now. Cal
18974 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 ling this functi
18975 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 on.** does not f
18976 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c ree any Mem.zMal
18977 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 loc buffer..*/.S
18978 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
18979 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
1897a 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c mReleaseExternal
1897b 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 (Mem *p){. asse
1897c 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 rt( p->db==0 ||
1897d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1897e 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
1897f 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 );. if( p->fla
18980 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f gs&(MEM_Agg|MEM_
18981 44 79 6e 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 Dyn|MEM_RowSet)
18982 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c ){. if( p->fl
18983 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a 20 ags&MEM_Agg ){.
18984 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
18985 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70 MemFinalize(p, p
18986 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 20 ->u.pDef);.
18987 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 assert( (p->fla
18988 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 gs & MEM_Agg)==0
18989 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
1898a 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1898b 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 p);. }else if
1898c 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 ( p->flags&MEM_D
1898d 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b yn && p->xDel ){
1898e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
1898f 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 p->flags&MEM_Row
18990 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Set)==0 );.
18991 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a p->xDel((void *
18992 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d )p->z);. p-
18993 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 7d >xDel = 0;. }
18994 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 else if( p->flag
18995 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a s&MEM_RowSet ){.
18996 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 sqlite3Row
18997 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52 SetClear(p->u.pR
18998 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 owSet);. }.
18999 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 }.}../*.** Relea
1899a 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 20 68 65 se any memory he
1899b 6c 64 20 62 79 20 74 68 65 20 4d 65 6d 2e 20 54 ld by the Mem. T
1899c 68 69 73 20 6d 61 79 20 6c 65 61 76 65 20 74 68 his may leave th
1899d 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 69 e Mem in an.** i
1899e 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 nconsistent stat
1899f 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 e, for example w
189a0 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61 ith (Mem.z==0) a
189a1 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d nd.** (Mem.type=
189a2 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 2e 0a 2a =SQLITE_TEXT)..*
189a3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
189a4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
189a5 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 eMemRelease(Mem
189a6 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 *p){. sqlite3Vd
189a7 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 beMemReleaseExte
189a8 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71 6c 69 74 rnal(p);. sqlit
189a9 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 e3DbFree(p->db,
189aa 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 70 p->zMalloc);. p
189ab 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 4d ->z = 0;. p->zM
189ac 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 70 2d 3e alloc = 0;. p->
189ad 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a xDel = 0;.}../*.
189ae 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 36 34 2d ** Convert a 64-
189af 62 69 74 20 49 45 45 45 20 64 6f 75 62 6c 65 20 bit IEEE double
189b0 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 into a 64-bit si
189b1 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a gned integer..**
189b2 20 49 66 20 74 68 65 20 64 6f 75 62 6c 65 20 69 If the double i
189b3 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 72 65 74 s too large, ret
189b4 75 72 6e 20 30 78 38 30 30 30 30 30 30 30 30 30 urn 0x8000000000
189b5 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 000000..**.** Mo
189b6 73 74 20 73 79 73 74 65 6d 73 20 61 70 70 65 61 st systems appea
189b7 72 20 74 6f 20 64 6f 20 74 68 69 73 20 73 69 6d r to do this sim
189b8 70 6c 79 20 62 79 20 61 73 73 69 67 6e 69 6e 67 ply by assigning
189b9 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e .** variables an
189ba 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 65 78 d without the ex
189bb 74 72 61 20 72 61 6e 67 65 20 74 65 73 74 73 2e tra range tests.
189bc 20 20 42 75 74 0a 2a 2a 20 74 68 65 72 65 20 61 But.** there a
189bd 72 65 20 72 65 70 6f 72 74 73 20 74 68 61 74 20 re reports that
189be 77 69 6e 64 6f 77 73 20 74 68 72 6f 77 73 20 61 windows throws a
189bf 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a 2a 20 69 n expection.** i
189c0 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 f the floating p
189c1 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 6f 75 oint value is ou
189c2 74 20 6f 66 20 72 61 6e 67 65 2e 20 28 53 65 65 t of range. (See
189c3 20 74 69 63 6b 65 74 20 23 32 38 38 30 2e 29 0a ticket #2880.).
189c4 2a 2a 20 42 65 63 61 75 73 65 20 77 65 20 64 6f ** Because we do
189c5 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 not completely
189c6 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 70 understand the p
189c7 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69 6c 6c 0a roblem, we will.
189c8 2a 2a 20 74 61 6b 65 20 74 68 65 20 63 6f 6e 73 ** take the cons
189c9 65 72 76 61 74 69 76 65 20 61 70 70 72 6f 61 63 ervative approac
189ca 68 20 61 6e 64 20 61 6c 77 61 79 73 20 64 6f 20 h and always do
189cb 72 61 6e 67 65 20 74 65 73 74 73 0a 2a 2a 20 62 range tests.** b
189cc 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 6e 67 efore attempting
189cd 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e the conversion.
189ce 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 64 .*/.static i64 d
189cf 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 64 6f 75 oubleToInt64(dou
189d0 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a 20 20 2a ble r){. /*. *
189d1 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c 65 72 73 * Many compilers
189d2 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 64 6f we encounter do
189d3 20 6e 6f 74 20 64 65 66 69 6e 65 20 63 6f 6e 73 not define cons
189d4 74 61 6e 74 73 20 66 6f 72 20 74 68 65 0a 20 20 tants for the.
189d5 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e 64 20 6d ** minimum and m
189d6 61 78 69 6d 75 6d 20 36 34 2d 62 69 74 20 69 6e aximum 64-bit in
189d7 74 65 67 65 72 73 2c 20 6f 72 20 74 68 65 79 20 tegers, or they
189d8 64 65 66 69 6e 65 20 74 68 65 6d 0a 20 20 2a 2a define them. **
189d9 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e inconsistently.
189da 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f 20 6e 6f And many do no
189db 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 t understand the
189dc 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e 0a "LL" notation..
189dd 20 20 2a 2a 20 53 6f 20 77 65 20 64 65 66 69 6e ** So we defin
189de 65 20 6f 75 72 20 6f 77 6e 20 73 74 61 74 69 63 e our own static
189df 20 63 6f 6e 73 74 61 6e 74 73 20 68 65 72 65 20 constants here
189e0 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67 0a 20 20 using nothing.
189e1 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61 ** larger than a
189e2 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
189e3 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a 20 constant.. */.
189e4 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 static const i6
189e5 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41 52 47 45 4 maxInt = LARGE
189e6 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73 74 61 74 ST_INT64;. stat
189e7 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d 69 6e ic const i64 min
189e8 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49 Int = SMALLEST_I
189e9 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20 72 3c 28 NT64;.. if( r<(
189ea 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74 20 29 7b double)minInt ){
189eb 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 . return minI
189ec 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 nt;. }else if(
189ed 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78 49 6e 74 r>(double)maxInt
189ee 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69 6e 49 6e ){. /* minIn
189ef 74 20 69 73 20 63 6f 72 72 65 63 74 20 68 65 72 t is correct her
189f0 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e 74 2e 20 e - not maxInt.
189f1 20 49 74 20 74 75 72 6e 73 20 6f 75 74 20 74 68 It turns out th
189f2 61 74 20 61 73 73 69 67 6e 69 6e 67 0a 20 20 20 at assigning.
189f3 20 2a 2a 20 61 20 76 65 72 79 20 6c 61 72 67 65 ** a very large
189f4 20 70 6f 73 69 74 69 76 65 20 6e 75 6d 62 65 72 positive number
189f5 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 72 to an integer r
189f6 65 73 75 6c 74 73 20 69 6e 20 61 20 76 65 72 79 esults in a very
189f7 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a 20 6e 65 large. ** ne
189f8 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 2e 20 gative integer.
189f9 20 54 68 69 73 20 6d 61 6b 65 73 20 6e 6f 20 73 This makes no s
189fa 65 6e 73 65 2c 20 62 75 74 20 69 74 20 69 73 20 ense, but it is
189fb 77 68 61 74 20 78 38 36 20 68 61 72 64 77 61 72 what x86 hardwar
189fc 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 73 6f e. ** does so
189fd 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 for compatibili
189fe 74 79 20 77 65 20 77 69 6c 6c 20 64 6f 20 74 68 ty we will do th
189ff 65 20 73 61 6d 65 20 69 6e 20 73 6f 66 74 77 61 e same in softwa
18a00 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 re. */. retur
18a01 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 n minInt;. }els
18a02 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 69 e{. return (i
18a03 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 64)r;. }.}../*.
18a04 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d 65 20 6b ** Return some k
18a05 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 72 20 76 ind of integer v
18a06 61 6c 75 65 20 77 68 69 63 68 20 69 73 20 74 68 alue which is th
18a07 65 20 62 65 73 74 20 77 65 20 63 61 6e 20 64 6f e best we can do
18a08 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 65 6e 74 .** at represent
18a09 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 74 68 ing the value th
18a0a 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 72 69 62 at *pMem describ
18a0b 65 73 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 es as an integer
18a0c 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 ..** If pMem is
18a0d 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e an integer, then
18a0e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 65 78 the value is ex
18a0f 61 63 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 73 act. If pMem is
18a10 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 .** a floating-p
18a11 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 20 76 61 oint then the va
18a12 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 lue returned is
18a13 74 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 74 the integer part
18a14 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 ..** If pMem is
18a15 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 a string or blob
18a16 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b 65 20 61 , then we make a
18a17 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e n attempt to con
18a18 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e 74 6f 20 vert.** it into
18a19 61 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 65 a integer and re
18a1a 74 75 72 6e 20 74 68 61 74 2e 20 20 49 66 20 70 turn that. If p
18a1b 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 Mem represents a
18a1c 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e 55 4c 4c n.** an SQL-NULL
18a1d 20 76 61 6c 75 65 2c 20 72 65 74 75 72 6e 20 30 value, return 0
18a1e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 ..**.** If pMem
18a1f 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 74 72 represents a str
18a20 69 6e 67 20 76 61 6c 75 65 2c 20 69 74 73 20 65 ing value, its e
18a21 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62 65 ncoding might be
18a22 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c changed..*/.SQL
18a23 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 ITE_PRIVATE i64
18a24 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
18a25 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a lue(Mem *pMem){.
18a26 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 61 int flags;. a
18a27 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
18a28 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
18a29 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
18a2a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
18a2b 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
18a2c 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d E_ALIGNMENT(pMem
18a2d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70 ) );. flags = p
18a2e 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 Mem->flags;. if
18a2f 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ( flags & MEM_In
18a30 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
18a31 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c pMem->u.i;. }el
18a32 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d se if( flags & M
18a33 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 EM_Real ){. r
18a34 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49 6e eturn doubleToIn
18a35 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 t64(pMem->r);.
18a36 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 }else if( flags
18a37 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 & (MEM_Str|MEM_B
18a38 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34 20 lob) ){. i64
18a39 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d 2d value;. pMem-
18a3a 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
18a3b 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 r;. if( sqlit
18a3c 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
18a3d 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 ding(pMem, SQLIT
18a3e 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c E_UTF8). |
18a3f 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d | sqlite3VdbeMem
18a40 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 NulTerminate(pMe
18a41 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 m) ){. retu
18a42 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 0;. }.
18a43 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 assert( pMem->z
18a44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 );. sqlite3At
18a45 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76 oi64(pMem->z, &v
18a46 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75 72 alue);. retur
18a47 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73 65 n value;. }else
18a48 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
18a49 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
18a4a 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65 70 urn the best rep
18a4b 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 resentation of p
18a4c 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e 20 Mem that we can
18a4d 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64 6f get into a.** do
18a4e 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20 69 uble. If pMem i
18a4f 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75 62 s already a doub
18a50 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65 72 le or an integer
18a51 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a 20 , return its.**
18a52 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69 73 value. If it is
18a53 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
18a54 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 b, try to conver
18a55 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c 65 t it to a double
18a56 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61 20 ..** If it is a
18a57 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 30 NULL, return 0.0
18a58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18a59 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 ATE double sqlit
18a5a 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
18a5b 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
18a5c 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
18a5d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
18a5e 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
18a5f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
18a60 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
18a61 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 _ALIGNMENT(pMem)
18a62 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e );. if( pMem->
18a63 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c flags & MEM_Real
18a64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
18a65 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65 20 Mem->r;. }else
18a66 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
18a67 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
18a68 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 return (double)
18a69 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c pMem->u.i;. }el
18a6a 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 se if( pMem->fla
18a6b 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 gs & (MEM_Str|ME
18a6c 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 2f M_Blob) ){. /
18a6d 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 * (double)0 In c
18a6e 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d ase of SQLITE_OM
18a6f 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
18a70 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64 6f 75 62 T... */. doub
18a71 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75 62 6c 65 le val = (double
18a72 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c )0;. pMem->fl
18a73 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a ags |= MEM_Str;.
18a74 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
18a75 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
18a76 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 g(pMem, SQLITE_U
18a77 54 46 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 TF8). || s
18a78 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c qlite3VdbeMemNul
18a79 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 Terminate(pMem)
18a7a 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 ){. /* (dou
18a7b 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 ble)0 In case of
18a7c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
18a7d 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a ATING_POINT... *
18a7e 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 /. return (
18a7f 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 7d 0a double)0;. }.
18a80 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
18a81 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ->z );. sqlit
18a82 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e 7a 2c 20 e3AtoF(pMem->z,
18a83 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 74 75 72 &val);. retur
18a84 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a n val;. }else{.
18a85 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 /* (double)0
18a86 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 In case of SQLI
18a87 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
18a88 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 _POINT... */.
18a89 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 return (double)
18a8a 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
18a8b 54 68 65 20 4d 45 4d 20 73 74 72 75 63 74 75 72 The MEM structur
18a8c 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 4d e is already a M
18a8d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 20 74 6f EM_Real. Try to
18a8e 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 20 61 0a also make it a.
18a8f 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 20 77 65 ** MEM_Int if we
18a90 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f can..*/.SQLITE_
18a91 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
18a92 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 ite3VdbeIntegerA
18a93 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 4d 65 ffinity(Mem *pMe
18a94 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
18a95 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
18a96 52 65 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 Real );. assert
18a97 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
18a98 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
18a99 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
18a9a 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
18a9b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
18a9c 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
18a9d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
18a9e 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
18a9f 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20 70 4d T(pMem) );.. pM
18aa0 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 62 6c 65 em->u.i = double
18aa1 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 ToInt64(pMem->r)
18aa2 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d ;. if( pMem->r=
18aa3 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 =(double)pMem->u
18aa4 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e .i ){. pMem->
18aa5 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 flags |= MEM_Int
18aa6 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
18aa7 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 onvert pMem to t
18aa8 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 6e ype integer. In
18aa9 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 validate any pri
18aaa 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f or representatio
18aab 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ns..*/.SQLITE_PR
18aac 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18aad 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 3VdbeMemIntegeri
18aae 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 fy(Mem *pMem){.
18aaf 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
18ab0 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
18ab1 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
18ab2 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
18ab3 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
18ab4 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
18ab5 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 et)==0 );. asse
18ab6 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
18ab7 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 LIGNMENT(pMem) )
18ab8 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d ;.. pMem->u.i =
18ab9 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
18aba 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 alue(pMem);. Me
18abb 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 mSetTypeFlag(pMe
18abc 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 m, MEM_Int);. r
18abd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
18abe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
18abf 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 t pMem so that i
18ac0 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 4d t is of type MEM
18ac1 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 _Real..** Invali
18ac2 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72 date any prior r
18ac3 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a epresentations..
18ac4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18ac5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
18ac6 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 eMemRealify(Mem
18ac7 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 *pMem){. assert
18ac8 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
18ac9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
18aca 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
18acb 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
18acc 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
18acd 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a GNMENT(pMem) );.
18ace 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c . pMem->r = sql
18acf 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 ite3VdbeRealValu
18ad0 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 e(pMem);. MemSe
18ad1 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 tTypeFlag(pMem,
18ad2 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 MEM_Real);. ret
18ad3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
18ad4 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
18ad5 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 pMem so that it
18ad6 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 has types MEM_Re
18ad7 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 al or MEM_Int or
18ad8 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 both..** Invali
18ad9 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72 date any prior r
18ada 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a epresentations..
18adb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18adc 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
18add 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d eMemNumerify(Mem
18ade 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c *pMem){. doubl
18adf 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 e r1, r2;. i64
18ae0 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d i;. assert( (pM
18ae1 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d em->flags & (MEM
18ae2 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 _Int|MEM_Real|ME
18ae3 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 M_Null))==0 );.
18ae4 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
18ae5 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f flags & (MEM_Blo
18ae6 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 b|MEM_Str))!=0 )
18ae7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d ;. assert( pMem
18ae8 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
18ae9 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
18aea 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
18aeb 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 ;. r1 = sqlite3
18aec 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d VdbeRealValue(pM
18aed 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c em);. i = doubl
18aee 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 eToInt64(r1);.
18aef 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a r2 = (double)i;.
18af0 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a if( r1==r2 ){.
18af1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
18af2 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65 emIntegerify(pMe
18af3 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 m);. }else{.
18af4 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 pMem->r = r1;.
18af5 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
18af6 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c g(pMem, MEM_Real
18af7 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
18af8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
18af9 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 .** Delete any p
18afa 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e revious value an
18afb 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 d set the value
18afc 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 stored in *pMem
18afd 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 to NULL..*/.SQLI
18afe 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18aff 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
18b00 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 tNull(Mem *pMem)
18b01 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c {. if( pMem->fl
18b02 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
18b03 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 ){. sqlite3R
18b04 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d owSetClear(pMem-
18b05 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d >u.pRowSet);. }
18b06 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
18b07 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c g(pMem, MEM_Null
18b08 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 );. pMem->type
18b09 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d = SQLITE_NULL;.}
18b0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
18b0b 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 ny previous valu
18b0c 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 e and set the va
18b0d 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 lue to be a BLOB
18b0e 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 of length.** n
18b0f 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a containing all z
18b10 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eros..*/.SQLITE_
18b11 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
18b12 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 ite3VdbeMemSetZe
18b13 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d roBlob(Mem *pMem
18b14 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 , int n){. sqli
18b15 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
18b16 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d e(pMem);. pMem-
18b17 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f >flags = MEM_Blo
18b18 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d b|MEM_Zero;. pM
18b19 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
18b1a 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e E_BLOB;. pMem->
18b1b 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 n = 0;. if( n<0
18b1c 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d ) n = 0;. pMem
18b1d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20 ->u.nZero = n;.
18b1e 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
18b1f 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a ITE_UTF8;.}../*.
18b20 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 ** Delete any pr
18b21 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 evious value and
18b22 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 set the value s
18b23 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 tored in *pMem t
18b24 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 o val,.** manife
18b25 73 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e st type INTEGER.
18b26 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18b27 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
18b28 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d dbeMemSetInt64(M
18b29 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 em *pMem, i64 va
18b2a 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 l){. sqlite3Vdb
18b2b 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d eMemRelease(pMem
18b2c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d );. pMem->u.i =
18b2d 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c val;. pMem->fl
18b2e 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
18b2f 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
18b30 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a LITE_INTEGER;.}.
18b31 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
18b32 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 y previous value
18b33 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c and set the val
18b34 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d ue stored in *pM
18b35 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 em to val,.** ma
18b36 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c nifest type REAL
18b37 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18b38 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
18b39 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 VdbeMemSetDouble
18b3a 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 (Mem *pMem, doub
18b3b 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 le val){. if( s
18b3c 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 qlite3IsNaN(val)
18b3d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
18b3e 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d dbeMemSetNull(pM
18b3f 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 em);. }else{.
18b40 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
18b41 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
18b42 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c pMem->r = val
18b43 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
18b44 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 s = MEM_Real;.
18b45 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
18b46 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d QLITE_FLOAT;. }
18b47 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
18b48 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 any previous va
18b49 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 lue and set the
18b4a 76 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 74 6f value of pMem to
18b4b 20 62 65 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20 be an.** empty
18b4c 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 2e 0a 2a boolean index..*
18b4d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18b4e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
18b4f 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 eMemSetRowSet(Me
18b50 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 71 6c 69 m *pMem){. sqli
18b51 74 65 33 20 2a 64 62 20 3d 20 70 4d 65 6d 2d 3e te3 *db = pMem->
18b52 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 db;. assert( db
18b53 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 !=0 );. if( pMe
18b54 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 m->flags & MEM_R
18b55 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c owSet ){. sql
18b56 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 ite3RowSetClear(
18b57 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 pMem->u.pRowSet)
18b58 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
18b59 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
18b5a 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
18b5b 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 pMem->zMalloc =
18b5c 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
18b5d 61 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 7d 0a aw(db, 64);. }.
18b5e 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
18b5f 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 4d Failed ){. pM
18b60 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
18b61 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Null;. }else{.
18b62 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
18b63 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 >zMalloc );.
18b64 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 20 pMem->u.pRowSet
18b65 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 = sqlite3RowSetI
18b66 6e 69 74 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d nit(db, pMem->zM
18b67 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 20 20 20 20 alloc, .
18b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
18b6a 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
18b6b 7a 65 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 ze(db, pMem->zMa
18b6c 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 61 73 73 65 lloc));. asse
18b6d 72 74 28 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 rt( pMem->u.pRow
18b6e 53 65 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 4d Set!=0 );. pM
18b6f 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
18b70 52 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f RowSet;. }.}../
18b71 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
18b72 20 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a 65 if the Mem obje
18b73 63 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 45 ct contains a TE
18b74 58 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 20 XT or BLOB that
18b75 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 20 is.** too large
18b76 2d 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78 63 - whose size exc
18b77 65 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f eeds SQLITE_MAX_
18b78 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54 LENGTH..*/.SQLIT
18b79 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
18b7a 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 lite3VdbeMemTooB
18b7b 69 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 ig(Mem *p){. as
18b7c 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 sert( p->db!=0 )
18b7d 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 ;. if( p->flags
18b7e 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f & (MEM_Str|MEM_
18b7f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e 74 Blob) ){. int
18b80 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 20 20 69 n = p->n;. i
18b81 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 f( p->flags & ME
18b82 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
18b83 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b n += p->u.nZero;
18b84 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
18b85 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69 n n>p->db->aLimi
18b86 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
18b87 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 65 ENGTH];. }. re
18b88 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a turn 0; .}../*.*
18b89 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 74 * Size of struct
18b8a 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69 Mem not includi
18b8b 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c ng the Mem.zMall
18b8c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 oc member..*/.#d
18b8d 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a efine MEMCELLSIZ
18b8e 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 4d E (size_t)(&(((M
18b8f 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 em *)0)->zMalloc
18b90 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 ))../*.** Make a
18b91 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f n shallow copy o
18b92 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f f pFrom into pTo
18b93 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 . Prior content
18b94 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20 s of.** pTo are
18b95 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 6f freed. The pFro
18b96 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f m->z field is no
18b97 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 49 t duplicated. I
18b98 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 f.** pFrom->z is
18b99 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d used, then pTo-
18b9a 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 >z points to the
18b9b 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 70 same thing as p
18b9c 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 From->z.** and f
18b9d 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79 70 lags gets srcTyp
18b9e 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 70 e (either MEM_Ep
18b9f 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 hem or MEM_Stati
18ba0 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 c)..*/.SQLITE_PR
18ba1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
18ba2 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
18ba3 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 Copy(Mem *pTo, c
18ba4 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c onst Mem *pFrom,
18ba5 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a 20 int srcType){.
18ba6 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d assert( (pFrom-
18ba7 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
18ba8 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c Set)==0 );. sql
18ba9 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
18baa 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b seExternal(pTo);
18bab 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 . memcpy(pTo, p
18bac 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a From, MEMCELLSIZ
18bad 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c 20 E);. pTo->xDel
18bae 3d 20 30 3b 0a 20 20 69 66 28 20 28 70 46 72 6f = 0;. if( (pFro
18baf 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e m->flags&MEM_Dyn
18bb0 29 21 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 7a )!=0 || pFrom->z
18bb1 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 ==pFrom->zMalloc
18bb2 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 ){. pTo->fla
18bb3 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c gs &= ~(MEM_Dyn|
18bb4 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 MEM_Static|MEM_E
18bb5 70 68 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 phem);. asser
18bb6 74 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f t( srcType==MEM_
18bb7 45 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 65 Ephem || srcType
18bb8 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b 0a ==MEM_Static );.
18bb9 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c pTo->flags |
18bba 3d 20 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a 7d = srcType;. }.}
18bbb 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 ../*.** Make a f
18bbc 75 6c 6c 20 63 6f 70 79 20 6f 66 20 70 46 72 6f ull copy of pFro
18bbd 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 m into pTo. Pri
18bbe 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 or contents of p
18bbf 54 6f 20 61 72 65 0a 2a 2a 20 66 72 65 65 64 20 To are.** freed
18bc0 62 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 20 before the copy
18bc1 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 is made..*/.SQLI
18bc2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18bc3 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 qlite3VdbeMemCop
18bc4 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 y(Mem *pTo, cons
18bc5 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 t Mem *pFrom){.
18bc6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
18bc7 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 _OK;.. assert(
18bc8 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 (pFrom->flags &
18bc9 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
18bca 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
18bcb 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
18bcc 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 l(pTo);. memcpy
18bcd 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d (pTo, pFrom, MEM
18bce 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f CELLSIZE);. pTo
18bcf 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f ->flags &= ~MEM_
18bd0 44 79 6e 3b 0a 0a 20 20 69 66 28 20 70 54 6f 2d Dyn;.. if( pTo-
18bd1 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c >flags&(MEM_Str|
18bd2 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 MEM_Blob) ){.
18bd3 20 69 66 28 20 30 3d 3d 28 70 46 72 6f 6d 2d 3e if( 0==(pFrom->
18bd4 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 flags&MEM_Static
18bd5 29 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 2d 3e ) ){. pTo->
18bd6 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 flags |= MEM_Eph
18bd7 65 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 em;. rc = s
18bd8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b qlite3VdbeMemMak
18bd9 65 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29 3b eWriteable(pTo);
18bda 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 . }. }.. re
18bdb 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
18bdc 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 63 * Transfer the c
18bdd 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d ontents of pFrom
18bde 20 74 6f 20 70 54 6f 2e 20 41 6e 79 20 65 78 69 to pTo. Any exi
18bdf 73 74 69 6e 67 20 76 61 6c 75 65 20 69 6e 20 70 sting value in p
18be0 54 6f 20 69 73 0a 2a 2a 20 66 72 65 65 64 2e 20 To is.** freed.
18be1 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e If pFrom contain
18be2 73 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 s ephemeral data
18be3 2c 20 61 20 63 6f 70 79 20 69 73 20 6d 61 64 65 , a copy is made
18be4 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f ..**.** pFrom co
18be5 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 ntains an SQL NU
18be6 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 LL when this rou
18be7 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f tine returns..*/
18be8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18be9 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
18bea 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a 70 54 6f MemMove(Mem *pTo
18beb 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 , Mem *pFrom){.
18bec 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e assert( pFrom->
18bed 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
18bee 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 46 72 6f _mutex_held(pFro
18bef 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
18bf0 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e . assert( pTo->
18bf1 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
18bf2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 54 6f 2d _mutex_held(pTo-
18bf3 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
18bf4 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e assert( pFrom->
18bf5 64 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d 3e 64 62 db==0 || pTo->db
18bf6 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 64 62 ==0 || pFrom->db
18bf7 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a 0a 20 20 ==pTo->db );..
18bf8 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18bf9 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 20 6d 65 lease(pTo);. me
18bfa 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c mcpy(pTo, pFrom,
18bfb 20 73 69 7a 65 6f 66 28 4d 65 6d 29 29 3b 0a 20 sizeof(Mem));.
18bfc 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 3d 20 pFrom->flags =
18bfd 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 46 72 6f MEM_Null;. pFro
18bfe 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 70 m->xDel = 0;. p
18bff 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 From->zMalloc =
18c00 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 0;.}../*.** Chan
18c01 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
18c02 61 20 4d 65 6d 20 74 6f 20 62 65 20 61 20 73 74 a Mem to be a st
18c03 72 69 6e 67 20 6f 72 20 61 20 42 4c 4f 42 2e 0a ring or a BLOB..
18c04 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 **.** The memory
18c05 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 61 management stra
18c06 74 65 67 79 20 64 65 70 65 6e 64 73 20 6f 6e 20 tegy depends on
18c07 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
18c08 20 78 44 65 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 xDel.** paramet
18c09 65 72 2e 20 49 66 20 74 68 65 20 76 61 6c 75 65 er. If the value
18c0a 20 70 61 73 73 65 64 20 69 73 20 53 51 4c 49 54 passed is SQLIT
18c0b 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20 74 68 65 E_TRANSIENT, the
18c0c 6e 20 74 68 65 20 0a 2a 2a 20 73 74 72 69 6e 67 n the .** string
18c0d 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 is copied into
18c0e 61 20 28 70 6f 73 73 69 62 6c 79 20 65 78 69 73 a (possibly exis
18c0f 74 69 6e 67 29 20 62 75 66 66 65 72 20 6d 61 6e ting) buffer man
18c10 61 67 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 aged by the .**
18c11 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 4f Mem structure. O
18c12 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 65 78 therwise, any ex
18c13 69 73 74 69 6e 67 20 62 75 66 66 65 72 20 69 73 isting buffer is
18c14 20 66 72 65 65 64 20 61 6e 64 20 74 68 65 0a 2a freed and the.*
18c15 2a 20 70 6f 69 6e 74 65 72 20 63 6f 70 69 65 64 * pointer copied
18c16 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18c17 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
18c18 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20 dbeMemSetStr(.
18c19 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20 Mem *pMem,
18c1a 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 /* Memory ce
18c1b 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72 ll to set to str
18c1c 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 ing value */. c
18c1d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20 onst char *z,
18c1e 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69 /* String poi
18c1f 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c nter */. int n,
18c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18c21 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 Bytes in string
18c22 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f , or negative */
18c23 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 . u8 enc,
18c24 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 /* Encodi
18c25 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 ng of z. 0 for
18c26 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 BLOBs */. void
18c27 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f (*xDel)(void*) /
18c28 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e * Destructor fun
18c29 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e ction */.){. in
18c2a 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20 t nByte = n;
18c2b 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66 /* New value f
18c2c 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20 or pMem->n */.
18c2d 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20 int iLimit;
18c2e 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 /* Maximum a
18c2f 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72 llowed string or
18c30 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20 blob size */.
18c31 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20 u16 flags = 0;
18c32 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 /* New value
18c33 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 for pMem->flags
18c34 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
18c35 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
18c36 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
18c37 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
18c38 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
18c39 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
18c3a 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
18c3b 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 . /* If z is a
18c3c 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 NULL pointer, se
18c3d 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 t pMem to contai
18c3e 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a n an SQL NULL. *
18c3f 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 /. if( !z ){.
18c40 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
18c41 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 SetNull(pMem);.
18c42 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
18c43 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 _OK;. }.. if(
18c44 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 pMem->db ){.
18c45 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 iLimit = pMem->d
18c46 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
18c47 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a _LIMIT_LENGTH];.
18c48 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 }else{. iLi
18c49 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 mit = SQLITE_MAX
18c4a 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 _LENGTH;. }. f
18c4b 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d lags = (enc==0?M
18c4c 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 EM_Blob:MEM_Str)
18c4d 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 ;. if( nByte<0
18c4e 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 ){. assert( e
18c4f 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 nc!=0 );. if(
18c50 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 enc==SQLITE_UTF
18c51 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 8 ){. for(n
18c52 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 Byte=0; nByte<=i
18c53 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 Limit && z[nByte
18c54 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 ]; nByte++){}.
18c55 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 }else{. f
18c56 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 or(nByte=0; nByt
18c57 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b e<=iLimit && (z[
18c58 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 nByte] | z[nByte
18c59 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b +1]); nByte+=2){
18c5a 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 }. }. flag
18c5b 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 s |= MEM_Term;.
18c5c 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c }.. /* The fol
18c5d 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 lowing block set
18c5e 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 s the new values
18c5f 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 of Mem.z and Me
18c60 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 m.xDel. It. **
18c61 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67 also sets a flag
18c62 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 in local variab
18c63 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e le "flags" to in
18c64 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 dicate the memor
18c65 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e y. ** managemen
18c66 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 t (one of MEM_Dy
18c67 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 n or MEM_Static)
18c68 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 .. */. if( xDe
18c69 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 l==SQLITE_TRANSI
18c6a 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ENT ){. int n
18c6b 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 Alloc = nByte;.
18c6c 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d if( flags&MEM
18c6d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e _Term ){. n
18c6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 Alloc += (enc==S
18c6f 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b QLITE_UTF8?1:2);
18c70 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
18c71 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 Byte>iLimit ){.
18c72 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
18c73 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d TE_TOOBIG;. }
18c74 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
18c75 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
18c76 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a , nAlloc, 0) ){.
18c77 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
18c78 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
18c79 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d . memcpy(pMem
18c7a 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b ->z, z, nAlloc);
18c7b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 . }else if( xDe
18c7c 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 l==SQLITE_DYNAMI
18c7d 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 C ){. sqlite3
18c7e 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
18c7f 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e Mem);. pMem->
18c80 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e zMalloc = pMem->
18c81 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 z = (char *)z;.
18c82 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 pMem->xDel =
18c83 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
18c84 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
18c85 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 lease(pMem);.
18c86 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 pMem->z = (char
18c87 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e *)z;. pMem->
18c88 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 xDel = xDel;.
18c89 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c flags |= ((xDel
18c8a 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 ==SQLITE_STATIC)
18c8b 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f ?MEM_Static:MEM_
18c8c 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Dyn);. }. if(
18c8d 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a nByte>iLimit ){.
18c8e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
18c8f 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 E_TOOBIG;. }..
18c90 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 pMem->n = nByte
18c91 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 ;. pMem->flags
18c92 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d = flags;. pMem-
18c93 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f >enc = (enc==0 ?
18c94 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 SQLITE_UTF8 : e
18c95 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 nc);. pMem->typ
18c96 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 e = (enc==0 ? SQ
18c97 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 LITE_BLOB : SQLI
18c98 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 TE_TEXT);..#ifnd
18c99 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
18c9a 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d TF16. if( pMem-
18c9b 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 >enc!=SQLITE_UTF
18c9c 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 8 && sqlite3Vdbe
18c9d 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 MemHandleBom(pMe
18c9e 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e m) ){. return
18c9f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
18ca0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 }.#endif.. ret
18ca1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
18ca2 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 ../*.** Compare
18ca3 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 61 the values conta
18ca4 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f 20 ined by the two
18ca5 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65 memory cells, re
18ca6 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74 turning.** negat
18ca7 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 ive, zero or pos
18ca8 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20 69 itive if pMem1 i
18ca9 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 s less than, equ
18caa 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 al to, or greate
18cab 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e r.** than pMem2.
18cac 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 69 Sorting order i
18cad 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 s NULL's first,
18cae 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 followed by numb
18caf 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a ers (integers.**
18cb0 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 and reals) sort
18cb1 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 ed numerically,
18cb2 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 74 followed by text
18cb3 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65 20 ordered by the
18cb4 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 collating.** seq
18cb5 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 uence pColl and
18cb6 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f finally blob's o
18cb7 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 rdered by memcmp
18cb8 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55 ()..**.** Two NU
18cb9 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f LL values are co
18cba 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20 62 nsidered equal b
18cbb 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
18cbc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18cbd 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 TE int sqlite3Me
18cbe 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d mCompare(const M
18cbf 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74 em *pMem1, const
18cc0 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e Mem *pMem2, con
18cc1 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c st CollSeq *pCol
18cc2 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 l){. int rc;.
18cc3 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e int f1, f2;. in
18cc4 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 t combined_flags
18cc5 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 61 ;.. /* Intercha
18cc6 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d nge pMem1 and pM
18cc7 65 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 em2 if the colla
18cc8 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 ting sequence sp
18cc9 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 53 ecifies. ** DES
18cca 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 C order.. */.
18ccb 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 f1 = pMem1->flag
18ccc 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d s;. f2 = pMem2-
18ccd 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e >flags;. combin
18cce 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 ed_flags = f1|f2
18ccf 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f 6d ;. assert( (com
18cd0 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d 45 bined_flags & ME
18cd1 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
18cd2 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 . /* If one va
18cd3 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20 lue is NULL, it
18cd4 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 is less than the
18cd5 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20 other. If both
18cd6 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20 values. ** are
18cd7 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a NULL, return 0..
18cd8 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 */. if( combi
18cd9 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 ned_flags&MEM_Nu
18cda 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ll ){. return
18cdb 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d (f2&MEM_Null) -
18cdc 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a (f1&MEM_Null);.
18cdd 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 }.. /* If one
18cde 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62 value is a numb
18cdf 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 er and the other
18ce0 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d is not, the num
18ce1 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a ber is less.. *
18ce2 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75 * If both are nu
18ce3 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61 mbers, compare a
18ce4 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69 s reals if one i
18ce5 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20 s a real, or as
18ce6 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66 integers. ** if
18ce7 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 both values are
18ce8 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a integers.. */.
18ce9 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 if( combined_f
18cea 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 lags&(MEM_Int|ME
18ceb 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69 M_Real) ){. i
18cec 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74 f( !(f1&(MEM_Int
18ced 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 |MEM_Real)) ){.
18cee 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
18cef 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66 }. if( !(f
18cf0 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 2&(MEM_Int|MEM_R
18cf1 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 eal)) ){. r
18cf2 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a eturn -1;. }.
18cf3 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32 if( (f1 & f2
18cf4 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 & MEM_Int)==0 )
18cf5 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 {. double r
18cf6 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28 1, r2;. if(
18cf7 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d (f1&MEM_Real)==
18cf8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 0 ){. r1
18cf9 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31 2d = (double)pMem1-
18cfa 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 >u.i;. }els
18cfb 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 e{. r1 =
18cfc 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 20 pMem1->r;.
18cfd 7d 0a 20 20 20 20 20 20 69 66 28 20 28 66 32 26 }. if( (f2&
18cfe 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a MEM_Real)==0 ){.
18cff 20 20 20 20 20 20 20 20 72 32 20 3d 20 28 64 6f r2 = (do
18d00 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69 3b uble)pMem2->u.i;
18d01 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
18d02 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 r2 = pMem2
18d03 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ->r;. }.
18d04 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72 if( r1<r2 ) r
18d05 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 eturn -1;.
18d06 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75 if( r1>r2 ) retu
18d07 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 rn 1;. retu
18d08 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b rn 0;. }else{
18d09 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 . assert( f
18d0a 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 1&MEM_Int );.
18d0b 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45 assert( f2&ME
18d0c 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69 M_Int );. i
18d0d 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20 f( pMem1->u.i <
18d0e 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 pMem2->u.i ) ret
18d0f 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 urn -1;. if
18d10 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70 ( pMem1->u.i > p
18d11 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 Mem2->u.i ) retu
18d12 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 rn 1;. retu
18d13 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 0;. }. }.
18d14 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c . /* If one val
18d15 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 ue is a string a
18d16 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 nd the other is
18d17 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69 a blob, the stri
18d18 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a ng is less.. **
18d19 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72 If both are str
18d1a 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73 ings, compare us
18d1b 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e ing the collatin
18d1c 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a g functions.. *
18d1d 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 /. if( combined
18d1e 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 _flags&MEM_Str )
18d1f 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 {. if( (f1 &
18d20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 MEM_Str)==0 ){.
18d21 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
18d22 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32 }. if( (f2
18d23 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 & MEM_Str)==0 )
18d24 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d {. return -
18d25 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 1;. }.. as
18d26 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 sert( pMem1->enc
18d27 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a ==pMem2->enc );.
18d28 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
18d29 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 1->enc==SQLITE_U
18d2a 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 TF8 || .
18d2b 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d pMem1->enc==
18d2c 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c SQLITE_UTF16LE |
18d2d 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 | pMem1->enc==SQ
18d2e 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a LITE_UTF16BE );.
18d2f 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c . /* The coll
18d30 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d ation sequence m
18d31 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 ust be defined a
18d32 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76 t this point, ev
18d33 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65 en if. ** the
18d34 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68 user deletes th
18d35 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 e collation sequ
18d36 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76 ence after the v
18d37 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20 dbe program is.
18d38 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28 ** compiled (
18d39 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77 this was not alw
18d3a 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20 ays the case)..
18d3b 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
18d3c 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c ( !pColl || pCol
18d3d 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20 l->xCmp );..
18d3e 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 if( pColl ){.
18d3f 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e if( pMem1->en
18d40 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b c==pColl->enc ){
18d41 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
18d42 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65 strings are alre
18d43 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65 ady in the corre
18d44 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61 ct encoding. Ca
18d45 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a ll the. *
18d46 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e * comparison fun
18d47 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a ction directly *
18d48 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
18d49 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f pColl->xCmp(pCo
18d4a 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d ll->pUser,pMem1-
18d4b 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d >n,pMem1->z,pMem
18d4c 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 2->n,pMem2->z);.
18d4d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
18d4e 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 const void
18d4f 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20 *v1, *v2;.
18d50 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20 int n1, n2;.
18d51 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20 Mem c1;.
18d52 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20 Mem c2;.
18d53 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31 memset(&c1
18d54 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29 , 0, sizeof(c1))
18d55 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 ;. memset
18d56 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28 (&c2, 0, sizeof(
18d57 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 c2));. sq
18d58 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c lite3VdbeMemShal
18d59 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65 lowCopy(&c1, pMe
18d5a 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a m1, MEM_Ephem);.
18d5b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
18d5c 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
18d5d 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45 y(&c2, pMem2, ME
18d5e 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 M_Ephem);.
18d5f 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 v1 = sqlite3Va
18d60 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 lueText((sqlite3
18d61 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f _value*)&c1, pCo
18d62 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 ll->enc);.
18d63 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30 n1 = v1==0 ? 0
18d64 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20 : c1.n;.
18d65 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c v2 = sqlite3Val
18d66 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f ueText((sqlite3_
18d67 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c value*)&c2, pCol
18d68 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 l->enc);.
18d69 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20 n2 = v2==0 ? 0
18d6a 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20 : c2.n;.
18d6b 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 rc = pColl->xCmp
18d6c 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e (pColl->pUser, n
18d6d 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a 1, v1, n2, v2);.
18d6e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
18d6f 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 dbeMemRelease(&c
18d70 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 1);. sqli
18d71 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
18d72 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20 e(&c2);.
18d73 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18d74 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 }. }. /*
18d75 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 If a NULL pointe
18d76 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 r was passed as
18d77 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63 the collate func
18d78 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75 tion, fall throu
18d79 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 gh. ** to the
18d7a 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75 blob case and u
18d7b 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f se memcmp(). */
18d7c 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68 . }. . /* Both
18d7d 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 values must be
18d7e 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 blobs. Compare
18d7f 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 using memcmp().
18d80 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d */. rc = memcm
18d81 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d p(pMem1->z, pMem
18d82 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 2->z, (pMem1->n>
18d83 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d pMem2->n)?pMem2-
18d84 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20 >n:pMem1->n);.
18d85 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 if( rc==0 ){.
18d86 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d rc = pMem1->n -
18d87 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 pMem2->n;. }.
18d88 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
18d89 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f *.** Move data o
18d8a 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65 ut of a btree ke
18d8b 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20 y or data field
18d8c 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73 and into a Mem s
18d8d 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 tructure..** The
18d8e 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20 data or key is
18d8f 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65 taken from the e
18d90 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69 ntry that pCur i
18d91 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
18d92 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66 ting.** to. off
18d93 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65 set and amt dete
18d94 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69 rmine what porti
18d95 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f on of the data o
18d96 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76 r key to retriev
18d97 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75 e..** key is tru
18d98 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79 e to get the key
18d99 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74 or false to get
18d9a 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75 data. The resu
18d9b 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a lt is written.**
18d9c 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65 into the pMem e
18d9d 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 lement..**.** Th
18d9e 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65 e pMem structure
18d9f 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 is assumed to b
18da0 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e e uninitialized.
18da1 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74 Any prior cont
18da2 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 ent.** is overwr
18da3 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65 itten without be
18da4 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a ing freed..**.**
18da5 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 If this routine
18da6 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 fails for any r
18da7 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 eason (malloc re
18da8 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e turns NULL or un
18da9 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20 able.** to read
18daa 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74 from the disk) t
18dab 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20 hen the pMem is
18dac 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e left in an incon
18dad 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a sistent state..*
18dae 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18daf 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
18db0 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20 MemFromBtree(.
18db1 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
18db2 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e /* Cursor poin
18db3 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74 ting at record t
18db4 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20 o retrieve. */.
18db5 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20 int offset,
18db6 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f /* Offset fro
18db7 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64 m the start of d
18db8 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79 ata to return by
18db9 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 tes from. */. i
18dba 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
18dbb 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
18dbc 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a tes to return. *
18dbd 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20 /. int key,
18dbe 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
18dbf 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20 , retrieve from
18dc0 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e the btree key, n
18dc1 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65 ot data. */. Me
18dc2 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 m *pMem
18dc3 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64 /* OUT: Return d
18dc4 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20 ata in this Mem
18dc5 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b structure. */.){
18dc6 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 . char *zData;
18dc7 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72 /* Data fr
18dc8 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 om the btree lay
18dc9 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 er */. int avai
18dca 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75 lable = 0; /* Nu
18dcb 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 mber of bytes av
18dcc 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c ailable on the l
18dcd 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20 ocal btree page
18dce 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
18dcf 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 ; /* Datab
18dd0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
18dd1 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
18dd2 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20 ITE_OK;.. db =
18dd3 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
18dd4 6f 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73 orDb(pCur);. as
18dd5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
18dd6 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 tex_held(db->mut
18dd7 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
18dd8 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
18dd9 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
18dda 3b 0a 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 ;. if( key ){.
18ddb 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 zData = (char
18ddc 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b *)sqlite3BtreeK
18ddd 65 79 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 eyFetch(pCur, &a
18dde 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 6c vailable);. }el
18ddf 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 se{. zData =
18de0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 (char *)sqlite3B
18de1 74 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43 treeDataFetch(pC
18de2 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b ur, &available);
18de3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a . }. assert( z
18de4 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20 20 69 66 Data!=0 );.. if
18de5 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 61 76 ( offset+amt<=av
18de6 61 69 6c 61 62 6c 65 20 26 26 20 28 28 70 4d 65 ailable && ((pMe
18de7 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e m->flags&MEM_Dyn
18de8 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 78 44 )==0 || pMem->xD
18de9 65 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 el) ){. sqlit
18dea 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
18deb 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d (pMem);. pMem
18dec 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b 6f 66 66 ->z = &zData[off
18ded 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e set];. pMem->
18dee 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 flags = MEM_Blob
18def 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 |MEM_Ephem;. }e
18df0 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f lse if( SQLITE_O
18df1 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K==(rc = sqlite3
18df2 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
18df3 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20 29 7b 0a , amt+2, 0)) ){.
18df4 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
18df5 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 = MEM_Blob|MEM_D
18df6 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 yn|MEM_Term;.
18df7 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b 0a pMem->enc = 0;.
18df8 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
18df9 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 SQLITE_BLOB;.
18dfa 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 if( key ){.
18dfb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
18dfc 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 6f 66 treeKey(pCur, of
18dfd 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d fset, amt, pMem-
18dfe 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a >z);. }else{.
18dff 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
18e00 65 33 42 74 72 65 65 44 61 74 61 28 70 43 75 72 e3BtreeData(pCur
18e01 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 , offset, amt, p
18e02 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a 20 Mem->z);. }.
18e03 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 pMem->z[amt]
18e04 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a = 0;. pMem->z
18e05 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 [amt+1] = 0;.
18e06 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18e07 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
18e08 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
18e09 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 e(pMem);. }.
18e0a 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61 }. pMem->n = a
18e0b 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 mt;.. return rc
18e0c 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e ;.}../* This fun
18e0d 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 76 ction is only av
18e0e 61 69 6c 61 62 6c 65 20 69 6e 74 65 72 6e 61 6c ailable internal
18e0f 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 61 ly, it is not pa
18e10 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 74 rt of the.** ext
18e11 65 72 6e 61 6c 20 41 50 49 2e 20 49 74 20 77 6f ernal API. It wo
18e12 72 6b 73 20 69 6e 20 61 20 73 69 6d 69 6c 61 72 rks in a similar
18e13 20 77 61 79 20 74 6f 20 73 71 6c 69 74 65 33 5f way to sqlite3_
18e14 76 61 6c 75 65 5f 74 65 78 74 28 29 2c 0a 2a 2a value_text(),.**
18e15 20 65 78 63 65 70 74 20 74 68 65 20 64 61 74 61 except the data
18e16 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e 20 returned is in
18e17 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 the encoding spe
18e18 63 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65 cified by the se
18e19 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 cond.** paramete
18e1a 72 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62 65 r, which must be
18e1b 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 one of SQLITE_U
18e1c 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 TF16BE, SQLITE_U
18e1d 54 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 53 51 4c TF16LE or.** SQL
18e1e 49 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 ITE_UTF8..**.**
18e1f 28 32 30 30 36 2d 30 32 2d 31 36 3a 29 20 20 54 (2006-02-16:) T
18e20 68 65 20 65 6e 63 20 76 61 6c 75 65 20 63 61 6e he enc value can
18e21 20 62 65 20 6f 72 2d 65 64 20 77 69 74 68 20 53 be or-ed with S
18e22 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
18e23 4e 45 44 2e 0a 2a 2a 20 49 66 20 74 68 61 74 20 NED..** If that
18e24 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 is the case, the
18e25 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6d 75 73 n the result mus
18e26 74 20 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 t be aligned on
18e27 61 6e 20 65 76 65 6e 20 62 79 74 65 0a 2a 2a 20 an even byte.**
18e28 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51 4c boundary..*/.SQL
18e29 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
18e2a 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 t void *sqlite3V
18e2b 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65 33 alueText(sqlite3
18e2c 5f 76 61 6c 75 65 2a 20 70 56 61 6c 2c 20 75 38 _value* pVal, u8
18e2d 20 65 6e 63 29 7b 0a 20 20 69 66 28 20 21 70 56 enc){. if( !pV
18e2e 61 6c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a al ) return 0;..
18e2f 20 20 61 73 73 65 72 74 28 20 70 56 61 6c 2d 3e assert( pVal->
18e30 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
18e31 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 56 61 6c _mutex_held(pVal
18e32 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
18e33 20 20 61 73 73 65 72 74 28 20 28 65 6e 63 26 33 assert( (enc&3
18e34 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54 45 5f )==(enc&~SQLITE_
18e35 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 UTF16_ALIGNED) )
18e36 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 56 61 ;. assert( (pVa
18e37 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 l->flags & MEM_R
18e38 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 owSet)==0 );..
18e39 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 if( pVal->flags&
18e3a 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
18e3b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
18e3c 61 73 73 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f assert( (MEM_Blo
18e3d 62 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 b>>3) == MEM_Str
18e3e 20 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67 );. pVal->flag
18e3f 73 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67 s |= (pVal->flag
18e40 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 s & MEM_Blob)>>3
18e41 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 ;. expandBlob(p
18e42 56 61 6c 29 3b 0a 20 20 69 66 28 20 70 56 61 6c Val);. if( pVal
18e43 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 ->flags&MEM_Str
18e44 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
18e45 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
18e46 28 70 56 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51 (pVal, enc & ~SQ
18e47 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
18e48 45 44 29 3b 0a 20 20 20 20 69 66 28 20 28 65 6e ED);. if( (en
18e49 63 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 c & SQLITE_UTF16
18e4a 5f 41 4c 49 47 4e 45 44 29 21 3d 30 20 26 26 20 _ALIGNED)!=0 &&
18e4b 31 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 1==(1&SQLITE_PTR
18e4c 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 _TO_INT(pVal->z)
18e4d 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ) ){. asser
18e4e 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 t( (pVal->flags
18e4f 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d & (MEM_Ephem|MEM
18e50 5f 53 74 61 74 69 63 29 29 21 3d 30 20 29 3b 0a _Static))!=0 );.
18e51 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
18e52 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
18e53 65 61 62 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c eable(pVal)!=SQL
18e54 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18e55 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
18e56 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
18e57 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 lite3VdbeMemNulT
18e58 65 72 6d 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a erminate(pVal);.
18e59 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
18e5a 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 ert( (pVal->flag
18e5b 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 s&MEM_Blob)==0 )
18e5c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
18e5d 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 56 eMemStringify(pV
18e5e 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 al, enc);. as
18e5f 73 65 72 74 28 20 30 3d 3d 28 31 26 53 51 4c 49 sert( 0==(1&SQLI
18e60 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 TE_PTR_TO_INT(pV
18e61 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a 20 al->z)) );. }.
18e62 20 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e assert(pVal->en
18e63 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 c==(enc & ~SQLIT
18e64 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 E_UTF16_ALIGNED)
18e65 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a || pVal->db==0.
18e66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
18e67 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f pVal->db->mallo
18e68 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 cFailed );. if(
18e69 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 pVal->enc==(enc
18e6a 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 & ~SQLITE_UTF16
18e6b 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 _ALIGNED) ){.
18e6c 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b return pVal->z;
18e6d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
18e6e 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 0;. }.}../
18e6f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
18e70 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 w sqlite3_value
18e71 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 object..*/.SQLIT
18e72 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
18e73 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 3_value *sqlite3
18e74 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 ValueNew(sqlite3
18e75 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 *db){. Mem *p
18e76 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
18e77 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
18e78 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 (*p));. if( p )
18e79 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d {. p->flags =
18e7a 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 MEM_Null;. p
18e7b 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
18e7c 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 NULL;. p->db
18e7d 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = db;. }. retu
18e7e 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rn p;.}../*.** C
18e7f 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 reate a new sqli
18e80 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
18e81 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 , containing the
18e82 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e value of pExpr.
18e83 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 .**.** This only
18e84 20 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 works for very
18e85 73 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f simple expressio
18e86 6e 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 ns that consist
18e87 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a of one constant.
18e88 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 ** token (i.e. "
18e89 35 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 5", "5.1", "'a s
18e8a 74 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 tring'"). If the
18e8b 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a expression can.
18e8c 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 ** be converted
18e8d 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 directly into a
18e8e 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 value, then the
18e8f 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 value is allocat
18e90 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e ed and.** a poin
18e91 74 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a ter written to *
18e92 70 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 ppVal. The calle
18e93 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 r is responsible
18e94 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e for deallocatin
18e95 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 g.** the value b
18e96 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 y passing it to
18e97 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 sqlite3ValueFree
18e98 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 () later on. If
18e99 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a the expression.*
18e9a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 * cannot be conv
18e9b 65 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 erted to a value
18e9c 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 , then *ppVal is
18e9d 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f set to NULL..*/
18e9e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18e9f 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 int sqlite3Value
18ea0 46 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 FromExpr(. sqli
18ea1 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
18ea2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
18ea3 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
18ea4 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 */. Expr *pExp
18ea5 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
18ea6 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f /* The expressio
18ea7 6e 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f n to evaluate */
18ea8 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 . u8 enc,
18ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18eaa 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 Encoding to use
18eab 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 */. u8 affinity
18eac 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
18ead 2a 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 * Affinity to us
18eae 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 e */. sqlite3_v
18eaf 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 alue **ppVal
18eb0 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 /* Write the ne
18eb1 77 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a w value here */.
18eb2 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 ){. int op;. c
18eb3 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 har *zVal = 0;.
18eb4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
18eb5 70 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 pVal = 0;.. if(
18eb6 20 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a !pExpr ){. *
18eb7 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 ppVal = 0;. r
18eb8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
18eb9 0a 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 . }. op = pExp
18eba 72 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 r->op;.. if( op
18ebb 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f ==TK_STRING || o
18ebc 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f p==TK_FLOAT || o
18ebd 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b p==TK_INTEGER ){
18ebe 0a 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 . zVal = sqli
18ebf 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c te3DbStrNDup(db,
18ec0 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 (char*)pExpr->t
18ec1 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 oken.z, pExpr->t
18ec2 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 oken.n);. pVa
18ec3 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 l = sqlite3Value
18ec4 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 New(db);. if(
18ec5 20 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 !zVal || !pVal
18ec6 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
18ec7 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 sqlite3ValueS
18ec8 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 etStr(pVal, -1,
18ec9 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 zVal, SQLITE_UTF
18eca 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 8, SQLITE_DYNAMI
18ecb 43 29 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 3d C);. if( (op=
18ecc 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f =TK_INTEGER || o
18ecd 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 p==TK_FLOAT ) &&
18ece 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
18ecf 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 E_AFF_NONE ){.
18ed0 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
18ed1 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 ApplyAffinity(pV
18ed2 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e al, SQLITE_AFF_N
18ed3 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 UMERIC, SQLITE_U
18ed4 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b TF8);. }else{
18ed5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
18ed6 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 lueApplyAffinity
18ed7 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c (pVal, affinity,
18ed8 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 SQLITE_UTF8);.
18ed9 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 }. if( enc
18eda 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b !=SQLITE_UTF8 ){
18edb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
18edc 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
18edd 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 (pVal, enc);.
18ede 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f }. }else if( o
18edf 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b p==TK_UMINUS ) {
18ee0 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
18ee1 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 OK==sqlite3Value
18ee2 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70 FromExpr(db,pExp
18ee3 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66 r->pLeft,enc,aff
18ee4 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a inity,&pVal) ){.
18ee5 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 pVal->u.i
18ee6 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69 = -1 * pVal->u.i
18ee7 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 ;. /* (doub
18ee8 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66 le)-1 In case of
18ee9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
18eea 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a ATING_POINT... *
18eeb 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20 /. pVal->r
18eec 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70 = (double)-1 * p
18eed 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 Val->r;. }.
18eee 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
18eef 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
18ef0 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 AL. else if( op
18ef1 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 ==TK_BLOB ){.
18ef2 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 int nVal;. a
18ef3 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f ssert( pExpr->to
18ef4 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 ken.n>=3 );.
18ef5 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 assert( pExpr->t
18ef6 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c oken.z[0]=='x' |
18ef7 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a | pExpr->token.z
18ef8 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 [0]=='X' );.
18ef9 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 assert( pExpr->t
18efa 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 oken.z[1]=='\''
18efb 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
18efc 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 Expr->token.z[pE
18efd 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d xpr->token.n-1]=
18efe 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61 ='\'' );. pVa
18eff 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 l = sqlite3Value
18f00 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 New(db);. if(
18f01 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f !pVal ) goto no
18f02 5f 6d 65 6d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d _mem;. nVal =
18f03 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 pExpr->token.n
18f04 2d 20 33 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 - 3;. zVal =
18f05 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f (char*)pExpr->to
18f06 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 73 ken.z + 2;. s
18f07 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
18f08 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 65 Str(pVal, sqlite
18f09 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 3HexToBlob(db, z
18f0a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c Val, nVal), nVal
18f0b 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 /2,.
18f0c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 0,
18f0d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b SQLITE_DYNAMIC);
18f0e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a . }.#endif.. *
18f0f 70 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 ppVal = pVal;.
18f10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
18f11 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d ;..no_mem:. db-
18f12 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
18f13 31 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 1;. sqlite3DbFr
18f14 65 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20 ee(db, zVal);.
18f15 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 sqlite3ValueFree
18f16 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c (pVal);. *ppVal
18f17 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 = 0;. return S
18f18 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a QLITE_NOMEM;.}..
18f19 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
18f1a 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66 string value of
18f1b 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 an sqlite3_valu
18f1c 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 e object.*/.SQLI
18f1d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18f1e 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 sqlite3ValueSetS
18f1f 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 tr(. sqlite3_va
18f20 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56 lue *v, /* V
18f21 61 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20 2a alue to be set *
18f22 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 /. int n,
18f23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e /* Len
18f24 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 20 gth of string z
18f25 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
18f26 2a 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 *z, /* Te
18f27 78 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73 74 xt of the new st
18f28 72 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63 ring */. u8 enc
18f29 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
18f2a 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 /* Encoding to u
18f2b 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 se */. void (*x
18f2c 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a Del)(void*) /*
18f2d 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 Destructor for
18f2e 74 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b the string */.){
18f2f 0a 20 20 69 66 28 20 76 20 29 20 73 71 6c 69 74 . if( v ) sqlit
18f30 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
18f31 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20 (Mem *)v, z, n,
18f32 65 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f enc, xDel);.}../
18f33 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 6c *.** Free an sql
18f34 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
18f35 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 t.*/.SQLITE_PRIV
18f36 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
18f37 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 ValueFree(sqlite
18f38 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69 3_value *v){. i
18f39 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a f( !v ) return;.
18f3a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
18f3b 52 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 76 Release((Mem *)v
18f3c 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
18f3d 65 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62 ee(((Mem*)v)->db
18f3e 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 , v);.}../*.** R
18f3f 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
18f40 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
18f41 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
18f42 62 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a bject assuming.*
18f43 2a 20 74 68 61 74 20 69 74 20 75 73 65 73 20 74 * that it uses t
18f44 68 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 he encoding "enc
18f45 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ".*/.SQLITE_PRIV
18f46 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
18f47 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 alueBytes(sqlite
18f48 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 3_value *pVal, u
18f49 38 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 8 enc){. Mem *p
18f4a 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 = (Mem*)pVal;.
18f4b 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 if( (p->flags &
18f4c 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c MEM_Blob)!=0 ||
18f4d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 sqlite3ValueTex
18f4e 74 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a t(pVal, enc) ){.
18f4f 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 if( p->flags
18f50 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 & MEM_Zero ){.
18f51 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e return p->n
18f52 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 + p->u.nZero;.
18f53 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
18f54 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 return p->n;.
18f55 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
18f56 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0;.}../*********
18f57 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
18f58 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a emem.c *********
18f59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f5b 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
18f5c 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
18f5d 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a vdbeaux.c *****
18f5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f60 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 ****/./*.** 2003
18f61 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a September 6.**.
18f62 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
18f63 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
18f64 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
18f65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
18f66 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
18f67 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
18f68 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
18f69 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
18f6a 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
18f6b 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
18f6c 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
18f6d 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
18f6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
18f6f 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
18f70 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
18f71 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
18f72 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
18f73 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
18f74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
18f78 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
18f79 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 s code used for
18f7a 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f creating, destro
18f7b 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 ying, and popula
18f7c 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 ting.** a VDBE (
18f7d 6f 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 or an "sqlite3_s
18f7e 74 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e tmt" as it is kn
18f7f 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 own to the outsi
18f80 64 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f de world.) Prio
18f81 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 r.** to version
18f82 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 2.8.7, all this
18f83 63 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 code was combine
18f84 64 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e d into the vdbe.
18f85 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a c source file..*
18f86 2a 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 * But that file
18f87 77 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 was getting too
18f88 62 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 big so this subr
18f89 6f 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c outines were spl
18f8a 69 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 it out..**.** $I
18f8b 64 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 d: vdbeaux.c,v 1
18f8c 2e 34 35 37 20 32 30 30 39 2f 30 35 2f 30 36 20 .457 2009/05/06
18f8d 31 38 3a 35 37 3a 31 30 20 73 68 61 6e 65 20 45 18:57:10 shane E
18f8e 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a xp $.*/..../*.**
18f8f 20 57 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 When debugging
18f90 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 the code generat
18f91 6f 72 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69 63 or in a symbolic
18f92 20 64 65 62 75 67 67 65 72 2c 20 6f 6e 65 20 63 debugger, one c
18f93 61 6e 0a 2a 2a 20 73 65 74 20 74 68 65 20 73 71 an.** set the sq
18f94 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 lite3VdbeAddopTr
18f95 61 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c ace to 1 and all
18f96 20 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 opcodes will be
18f97 20 70 72 69 6e 74 65 64 0a 2a 2a 20 61 73 20 74 printed.** as t
18f98 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74 6f hey are added to
18f99 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e the instruction
18f9a 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 64 stream..*/.#ifd
18f9b 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
18f9c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18f9d 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
18f9e 64 6f 70 54 72 61 63 65 20 3d 20 30 3b 0a 23 65 dopTrace = 0;.#e
18f9f 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 ndif.../*.** Cre
18fa0 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 ate a new virtua
18fa1 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e l database engin
18fa2 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
18fa3 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 VATE Vdbe *sqlit
18fa4 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71 6c e3VdbeCreate(sql
18fa5 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 ite3 *db){. Vdb
18fa6 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 e *p;. p = sqli
18fa7 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
18fa8 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65 29 db, sizeof(Vdbe)
18fa9 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 );. if( p==0 )
18faa 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 2d 3e return 0;. p->
18fab 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20 64 db = db;. if( d
18fac 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 b->pVdbe ){.
18fad 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65 76 db->pVdbe->pPrev
18fae 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 = p;. }. p->p
18faf 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64 62 65 Next = db->pVdbe
18fb0 3b 0a 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 ;. p->pPrev = 0
18fb1 3b 0a 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 ;. db->pVdbe =
18fb2 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 p;. p->magic =
18fb3 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b VDBE_MAGIC_INIT;
18fb4 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
18fb5 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 /*.** Remember t
18fb6 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 66 6f he SQL string fo
18fb7 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 r a prepared sta
18fb8 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 tement..*/.SQLIT
18fb9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
18fba 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71 6c qlite3VdbeSetSql
18fbb 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 (Vdbe *p, const
18fbc 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 char *z, int n,
18fbd 69 6e 74 20 69 73 50 72 65 70 61 72 65 56 32 29 int isPrepareV2)
18fbe 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
18fbf 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 53 51 eturn;.#ifdef SQ
18fc0 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a LITE_OMIT_TRACE.
18fc1 20 20 69 66 28 20 21 69 73 50 72 65 70 61 72 65 if( !isPrepare
18fc2 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65 6e V2 ) return;.#en
18fc3 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 2d dif. assert( p-
18fc4 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 2d >zSql==0 );. p-
18fc5 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 44 >zSql = sqlite3D
18fc6 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 bStrNDup(p->db,
18fc7 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50 72 z, n);. p->isPr
18fc8 65 70 61 72 65 56 32 20 3d 20 69 73 50 72 65 70 epareV2 = isPrep
18fc9 61 72 65 56 32 20 3f 20 31 20 3a 20 30 3b 0a 7d areV2 ? 1 : 0;.}
18fca 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
18fcb 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 he SQL associate
18fcc 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72 65 d with a prepare
18fcd 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 d statement.*/.S
18fce 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
18fcf 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 char *sqlite3_sq
18fd0 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a l(sqlite3_stmt *
18fd1 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a pStmt){. Vdbe *
18fd2 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d p = (Vdbe *)pStm
18fd3 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e t;. return (p->
18fd4 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70 2d isPrepareV2 ? p-
18fd5 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a 2f >zSql : 0);.}../
18fd6 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f *.** Swap all co
18fd7 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77 ntent between tw
18fd8 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72 65 o VDBE structure
18fd9 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
18fda 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
18fdb 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20 2a 3VdbeSwap(Vdbe *
18fdc 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20 pA, Vdbe *pB){.
18fdd 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70 Vdbe tmp, *pTmp
18fde 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a ;. char *zTmp;.
18fdf 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a tmp = *pA;. *
18fe0 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 pA = *pB;. *pB
18fe1 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 = tmp;. pTmp =
18fe2 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d pA->pNext;. pA-
18fe3 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 >pNext = pB->pNe
18fe4 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 xt;. pB->pNext
18fe5 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d = pTmp;. pTmp =
18fe6 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 pA->pPrev;. pA
18fe7 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 ->pPrev = pB->pP
18fe8 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 rev;. pB->pPrev
18fe9 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 = pTmp;. zTmp
18fea 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 = pA->zSql;. pA
18feb 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 ->zSql = pB->zSq
18fec 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 l;. pB->zSql =
18fed 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72 zTmp;. pB->isPr
18fee 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73 epareV2 = pA->is
18fef 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69 PrepareV2;.}..#i
18ff0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
18ff1 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 G./*.** Turn tra
18ff2 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a cing on or off.*
18ff3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18ff4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
18ff5 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 eTrace(Vdbe *p,
18ff6 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 FILE *trace){.
18ff7 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 p->trace = trace
18ff8 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
18ff9 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 * Resize the Vdb
18ffa 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 e.aOp array so t
18ffb 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 hat it is at lea
18ffc 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 st one op larger
18ffd 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 than .** it was
18ffe 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 ..**.** If an ou
18fff 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f t-of-memory erro
19000 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 r occurs while r
19001 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 esizing the arra
19002 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c y, return.** SQL
19003 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 ITE_NOMEM. In th
19004 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 is case Vdbe.aOp
19005 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c and Vdbe.nOpAll
19006 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e oc remain .** un
19007 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 changed (this is
19008 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 so that any opc
19009 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c odes already all
1900a 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a ocated can be .*
1900b 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c * correctly deal
1900c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 located along wi
1900d 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 th the rest of t
1900e 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 he Vdbe)..*/.sta
1900f 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 tic int growOpAr
19010 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ray(Vdbe *p){.
19011 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 VdbeOp *pNew;.
19012 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e int nNew = (p->n
19013 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 OpAlloc ? p->nOp
19014 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 Alloc*2 : (int)(
19015 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 1024/sizeof(Op))
19016 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 );. pNew = sqli
19017 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e te3DbRealloc(p->
19018 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 db, p->aOp, nNew
19019 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 *sizeof(Op));.
1901a 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 if( pNew ){.
1901b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71 p->nOpAlloc = sq
1901c 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
1901d 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73 e(p->db, pNew)/s
1901e 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70 izeof(Op);. p
1901f 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20 ->aOp = pNew;.
19020 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77 }. return (pNew
19021 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
19022 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a QLITE_NOMEM);.}.
19023 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
19024 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 instruction to
19025 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74 the list of inst
19026 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 ructions current
19027 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e in the.** VDBE.
19028 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 Return the add
19029 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20 ress of the new
1902a 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a instruction..**.
1902b 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a ** Parameters:.*
1902c 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20 *.** p
1902d 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 Pointer
1902e 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a to the VDBE.**.*
1902f 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20 20 * op
19030 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20 The opcode
19031 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75 63 for this instruc
19032 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31 tion.**.** p1
19033 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70 , p2, p3 Op
19034 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65 erands.**.** Use
19035 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 the sqlite3Vdbe
19036 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66 ResolveLabel() f
19037 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61 unction to fix a
19038 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a n address and.**
19039 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 the sqlite3Vdbe
1903a 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74 ChangeP4() funct
1903b 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68 ion to change th
1903c 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
1903d 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f 4.** operand..*/
1903e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1903f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
19040 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69 ddOp3(Vdbe *p, i
19041 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 nt op, int p1, i
19042 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a nt p2, int p3){.
19043 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f int i;. VdbeO
19044 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70 p *pOp;.. i = p
19045 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 ->nOp;. assert(
19046 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
19047 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
19048 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 assert( op>0 &&
19049 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 op<0xff );. if(
1904a 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 p->nOpAlloc<=i
1904b 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f ){. if( growO
1904c 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 pArray(p) ){.
1904d 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
1904e 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b }. }. p->nOp+
1904f 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 +;. pOp = &p->a
19050 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70 Op[i];. pOp->op
19051 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20 code = (u8)op;.
19052 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 pOp->p5 = 0;.
19053 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 pOp->p1 = p1;.
19054 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 pOp->p2 = p2;.
19055 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 pOp->p3 = p3;.
19056 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 pOp->p4.p = 0;.
19057 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
19058 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e 4_NOTUSED;. p->
19059 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66 expired = 0;.#if
1905a 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1905b 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 . pOp->zComment
1905c 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 = 0;. if( sqli
1905d 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 te3VdbeAddopTrac
1905e 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 e ) sqlite3VdbeP
1905f 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d rintOp(0, i, &p-
19060 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 >aOp[i]);.#endif
19061 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
19062 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c FILE. pOp->cycl
19063 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 es = 0;. pOp->c
19064 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 nt = 0;.#endif.
19065 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c return i;.}.SQL
19066 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19067 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
19068 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 0(Vdbe *p, int o
19069 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c p){. return sql
1906a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 ite3VdbeAddOp3(p
1906b 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a , op, 0, 0, 0);.
1906c 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
1906d 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1906e 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 AddOp1(Vdbe *p,
1906f 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b int op, int p1){
19070 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
19071 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 3VdbeAddOp3(p, o
19072 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a p, p1, 0, 0);.}.
19073 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19074 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
19075 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e dOp2(Vdbe *p, in
19076 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e t op, int p1, in
19077 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 t p2){. return
19078 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
19079 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 3(p, op, p1, p2,
1907a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 0);.}.../*.** A
1907b 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 dd an opcode tha
1907c 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 t includes the p
1907d 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 4 value as a poi
1907e 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nter..*/.SQLITE_
1907f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19080 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 te3VdbeAddOp4(.
19081 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 Vdbe *p,
19082 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 /* Add the
19083 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 opcode to this V
19084 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 M */. int op,
19085 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
19086 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a e new opcode */.
19087 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 int p1,
19088 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 /* The P1
19089 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 operand */. int
1908a 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 p2,
1908b 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 /* The P2 opera
1908c 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 nd */. int p3,
1908d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1908e 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f he P3 operand */
1908f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
19090 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 P4, /* The P4
19091 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
19092 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20 20 t p4type
19093 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 /* P4 operand
19094 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 type */.){. int
19095 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 addr = sqlite3V
19096 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c dbeAddOp3(p, op,
19097 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 p1, p2, p3);.
19098 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
19099 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 eP4(p, addr, zP4
1909a 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 , p4type);. ret
1909b 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a urn addr;.}../*.
1909c 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
1909d 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 symbolic label f
1909e 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f or an instructio
1909f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 n that has yet t
190a0 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 o be.** coded.
190a1 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 The symbolic lab
190a2 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 el is really jus
190a3 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d t a negative num
190a4 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 ber. The.** lab
190a5 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 el can be used a
190a6 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f s the P2 value o
190a7 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 f an operation.
190a8 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 Later, when.**
190a9 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 the label is res
190aa 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 olved to a speci
190ab 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 fic address, the
190ac 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a VDBE will scan.
190ad 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f ** through its o
190ae 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e peration list an
190af 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c d change all val
190b0 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 ues of P2 which
190b1 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 match.** the lab
190b2 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f el into the reso
190b3 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a lved address..**
190b4 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f .** The VDBE kno
190b5 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61 6c ws that a P2 val
190b6 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 ue is a label be
190b7 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 cause labels are
190b8 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 .** always negat
190b9 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 ive and P2 value
190ba 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f s are suppose to
190bb 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 be non-negative
190bc 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 ..** Hence, a ne
190bd 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 gative P2 value
190be 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 is a label that
190bf 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 has yet to be re
190c0 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 solved..**.** Ze
190c1 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ro is returned i
190c2 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
190c3 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ls..*/.SQLITE_PR
190c4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
190c5 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 3VdbeMakeLabel(V
190c6 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 dbe *p){. int i
190c7 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 ;. i = p->nLabe
190c8 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 l++;. assert( p
190c9 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
190ca 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 GIC_INIT );. if
190cb 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c ( i>=p->nLabelAl
190cc 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e loc ){. int n
190cd 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f = p->nLabelAllo
190ce 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d 3e c*2 + 5;. p->
190cf 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 aLabel = sqlite3
190d0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
190d1 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c p->db, p->aLabel
190d2 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
190d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
190d4 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 6f n*sizeo
190d5 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 f(p->aLabel[0]))
190d6 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 ;. p->nLabelA
190d7 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 lloc = sqlite3Db
190d8 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 MallocSize(p->db
190d9 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 7a , p->aLabel)/siz
190da 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d eof(p->aLabel[0]
190db 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e );. }. if( p->
190dc 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d aLabel ){. p-
190dd 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b >aLabel[i] = -1;
190de 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 . }. return -1
190df 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 -i;.}../*.** Res
190e0 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74 olve label "x" t
190e1 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73 73 o be the address
190e2 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 of the next ins
190e3 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 truction to.** b
190e4 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65 e inserted. The
190e5 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d parameter "x" m
190e6 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62 ust have been ob
190e7 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 tained from.** a
190e8 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 prior call to s
190e9 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 qlite3VdbeMakeLa
190ea 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 bel()..*/.SQLITE
190eb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
190ec 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 lite3VdbeResolve
190ed 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 69 Label(Vdbe *p, i
190ee 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 3d nt x){. int j =
190ef 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 28 -1-x;. assert(
190f0 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
190f1 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
190f2 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20 assert( j>=0 &&
190f3 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 j<p->nLabel );.
190f4 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 if( p->aLabel )
190f5 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b {. p->aLabel[
190f6 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d j] = p->nOp;. }
190f7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 .}../*.** Loop t
190f8 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72 hrough the progr
190f9 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 am looking for P
190fa 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 2 values that ar
190fb 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e e negative.** on
190fc 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f jump instructio
190fd 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 76 ns. Each such v
190fe 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e alue is a label.
190ff 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a Resolve the.**
19100 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e label by settin
19101 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 74 g the P2 value t
19102 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f o its correct no
19103 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a n-zero value..**
19104 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
19105 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 is called once
19106 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 after all opcode
19107 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 s have been inse
19108 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 rted..**.** Vari
19109 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 able *pMaxFuncAr
1910a 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 gs is set to the
1910b 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f maximum value o
1910c 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e f any P2 argumen
1910d 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 t .** to an OP_F
1910e 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 unction, OP_AggS
1910f 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 tep or OP_VFilte
19110 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69 r opcode. This i
19111 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 s used by .** sq
19112 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 lite3VdbeMakeRea
19113 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65 dy() to size the
19114 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 Vdbe.apArg[] ar
19115 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ray..**.** This
19116 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 routine also doe
19117 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 s the following
19118 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20 49 optimization: I
19119 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20 69 t scans for.** i
1911a 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 nstructions that
1911b 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20 73 might cause a s
1911c 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 63 tatement rollbac
1911d 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75 63 k. Such instruc
1911e 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a 2a tions.** are:.**
1911f 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 .** * OP_Halt
19120 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f with P1=SQLITE_
19121 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 CONSTRAINT and P
19122 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 2=OE_Abort..**
19123 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a * OP_Destroy.*
19124 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 * * OP_VUpdat
19125 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 e.** * OP_VRe
19126 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f name.**.** If no
19127 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69 6f such instructio
19128 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 6e n is found, then
19129 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e 74 every Statement
1912a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a 2a instruction .**
1912b 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 61 is changed to a
1912c 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73 20 Noop. In this
1912d 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63 72 way, we avoid cr
1912e 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74 65 eating the state
1912f 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c ment .** journal
19130 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61 72 file unnecessar
19131 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ily..*/.static v
19132 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c oid resolveP2Val
19133 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ues(Vdbe *p, int
19134 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b *pMaxFuncArgs){
19135 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
19136 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20 20 nMaxArgs = 0;.
19137 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 2a Op *pOp;. int *
19138 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 62 aLabel = p->aLab
19139 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53 74 el;. int doesSt
1913a 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 atementRollback
1913b 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53 74 = 0;. int hasSt
1913c 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20 30 atementBegin = 0
1913d 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 ;. p->readOnly
1913e 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53 74 = 1;. p->usesSt
1913f 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 mtJournal = 0;.
19140 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c for(pOp=p->aOp,
19141 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d i=p->nOp-1; i>=
19142 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 0; i--, pOp++){.
19143 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 u8 opcode =
19144 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 pOp->opcode;..
19145 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 if( opcode==OP
19146 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 63 _Function || opc
19147 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70 20 ode==OP_AggStep
19148 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 ){. if( pOp
19149 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29 20 ->p5>nMaxArgs )
1914a 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e nMaxArgs = pOp->
1914b 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 p5;.#ifndef SQLI
1914c 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1914d 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 ABLE. }else i
1914e 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 f( opcode==OP_VU
1914f 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 69 pdate ){. i
19150 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41 f( pOp->p2>nMaxA
19151 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d rgs ) nMaxArgs =
19152 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 66 pOp->p2;.#endif
19153 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f . }. if( o
19154 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 29 pcode==OP_Halt )
19155 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d {. if( pOp-
19156 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 >p1==SQLITE_CONS
19157 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70 TRAINT && pOp->p
19158 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 2==OE_Abort ){.
19159 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 doesState
1915a 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 mentRollback = 1
1915b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
1915c 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d lse if( opcode==
1915d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a OP_Statement ){.
1915e 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d 65 hasStateme
1915f 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20 20 ntBegin = 1;.
19160 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f p->usesStmtJo
19161 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20 7d urnal = 1;. }
19162 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d else if( opcode=
19163 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a 20 =OP_Destroy ){.
19164 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d 65 doesStateme
19165 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a ntRollback = 1;.
19166 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 }else if( op
19167 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 code==OP_Transac
19168 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 tion && pOp->p2!
19169 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 =0 ){. p->r
1916a 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 eadOnly = 0;.#if
1916b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1916c 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1916d 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f }else if( opco
1916e 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c de==OP_VUpdate |
1916f 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65 | opcode==OP_VRe
19170 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64 6f name ){. do
19171 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 esStatementRollb
19172 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c ack = 1;. }el
19173 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f se if( opcode==O
19174 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20 20 P_VFilter ){.
19175 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 int n;.
19176 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20 2d assert( p->nOp -
19177 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20 20 i >= 3 );.
19178 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d assert( pOp[-1]
19179 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74 65 .opcode==OP_Inte
1917a 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20 3d ger );. n =
1917b 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20 20 pOp[-1].p1;.
1917c 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72 67 if( n>nMaxArg
1917d 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 6e s ) nMaxArgs = n
1917e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a ;.#endif. }..
1917f 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 if( sqlite3V
19180 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 dbeOpcodeHasProp
19181 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50 46 erty(opcode, OPF
19182 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70 2d LG_JUMP) && pOp-
19183 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 61 >p2<0 ){. a
19184 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e 70 ssert( -1-pOp->p
19185 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 2<p->nLabel );.
19186 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 61 pOp->p2 = a
19187 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70 32 Label[-1-pOp->p2
19188 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 ];. }. }. s
19189 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e qlite3DbFree(p->
1918a 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a db, p->aLabel);.
1918b 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30 3b p->aLabel = 0;
1918c 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 .. *pMaxFuncArg
1918d 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a 20 s = nMaxArgs;..
1918e 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72 20 /* If we never
1918f 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74 65 rollback a state
19190 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
19191 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 , then statement
19192 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f . ** transactio
19193 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64 65 ns are not neede
19194 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65 76 d. So change ev
19195 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74 ery OP_Statement
19196 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e 74 . ** opcode int
19197 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20 54 o an OP_Noop. T
19198 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c 6c his avoid a call
19199 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 to sqlite3OsOpe
1919a 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20 2a nExclusive(). *
1919b 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 65 * which can be e
1919c 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d 65 xpensive on some
1919d 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a 2f platforms.. */
1919e 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65 6d . if( hasStatem
1919f 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f 65 entBegin && !doe
191a0 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 sStatementRollba
191a1 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73 65 ck ){. p->use
191a2 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 sStmtJournal = 0
191a3 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70 2d ;. for(pOp=p-
191a4 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 >aOp, i=p->nOp-1
191a5 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 ; i>=0; i--, pOp
191a6 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ++){. if( p
191a7 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 Op->opcode==OP_S
191a8 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 tatement ){.
191a9 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 pOp->opcode
191aa 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 = OP_Noop;.
191ab 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a }. }. }.}..
191ac 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
191ad 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 address of the
191ae 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
191af 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e to be inserted.
191b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
191b1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
191b2 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 64 beCurrentAddr(Vd
191b3 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 be *p){. assert
191b4 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
191b5 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
191b6 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b 0a return p->nOp;.
191b7 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 }../*.** Add a w
191b8 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 hole list of ope
191b9 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f rations to the o
191ba 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 peration stack.
191bb 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 Return the.** a
191bc 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66 69 ddress of the fi
191bd 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 rst operation ad
191be 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ded..*/.SQLITE_P
191bf 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
191c0 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 e3VdbeAddOpList(
191c1 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 Vdbe *p, int nOp
191c2 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e , VdbeOpList con
191c3 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 st *aOp){. int
191c4 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28 20 addr;. assert(
191c5 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
191c6 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
191c7 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 f( p->nOp + nOp
191c8 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 > p->nOpAlloc &&
191c9 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 20 growOpArray(p)
191ca 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
191cb 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d . }. addr = p-
191cc 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3e >nOp;. if( nOp>
191cd 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0 ){. int i;.
191ce 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 63 VdbeOpList c
191cf 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 3b onst *pIn = aOp;
191d0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
191d1 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b 29 nOp; i++, pIn++)
191d2 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 3d {. int p2 =
191d3 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 20 pIn->p2;.
191d4 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 26 VdbeOp *pOut = &
191d5 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b 0a p->aOp[i+addr];.
191d6 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 6f pOut->opco
191d7 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 65 de = pIn->opcode
191d8 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 31 ;. pOut->p1
191d9 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 20 = pIn->p1;.
191da 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73 71 if( p2<0 && sq
191db 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 lite3VdbeOpcodeH
191dc 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74 2d asProperty(pOut-
191dd 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a >opcode, OPFLG_J
191de 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20 20 UMP) ){.
191df 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 20 pOut->p2 = addr
191e0 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 20 + ADDR(p2);.
191e1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
191e2 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b 0a pOut->p2 = p2;.
191e3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f }. pO
191e4 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 33 ut->p3 = pIn->p3
191e5 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 34 ;. pOut->p4
191e6 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 type = P4_NOTUSE
191e7 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 D;. pOut->p
191e8 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4.p = 0;. p
191e9 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 66 Out->p5 = 0;.#if
191ea 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
191eb 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 6f . pOut->zCo
191ec 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 mment = 0;.
191ed 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
191ee 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 20 AddopTrace ){.
191ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
191f0 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 64 ePrintOp(0, i+ad
191f1 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 dr, &p->aOp[i+ad
191f2 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 dr]);. }.#e
191f3 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 ndif. }. p
191f4 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 20 ->nOp += nOp;.
191f5 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b }. return addr;
191f6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
191f7 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
191f8 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f 72 e P1 operand for
191f9 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 a specific inst
191fa 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 ruction..** This
191fb 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 routine is usef
191fc 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 ul when a large
191fd 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 program is loade
191fe 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 d from a.** stat
191ff 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 ic array using s
19200 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c qlite3VdbeAddOpL
19201 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 ist but we want
19202 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 to make a.** few
19203 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 minor changes t
19204 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a o the program..*
19205 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19206 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
19207 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 2a eChangeP1(Vdbe *
19208 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 p, int addr, int
19209 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 val){. assert(
1920a 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 p==0 || p->magi
1920b 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
1920c 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26 IT );. if( p &&
1920d 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e addr>=0 && p->n
1920e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 4f Op>addr && p->aO
1920f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b p ){. p->aOp[
19210 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a addr].p1 = val;.
19211 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}../*.** Cha
19212 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 nge the value of
19213 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 the P2 operand
19214 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 for a specific i
19215 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 nstruction..** T
19216 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
19217 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e seful for settin
19218 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 g a jump destina
19219 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
1921a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1921b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 ite3VdbeChangeP2
1921c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
1921d 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 dr, int val){.
1921e 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 assert( p==0 ||
1921f 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
19220 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
19221 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 f( p && addr>=0
19222 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 && p->nOp>addr &
19223 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 & p->aOp ){.
19224 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 p->aOp[addr].p2
19225 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a = val;. }.}../*
19226 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 .** Change the v
19227 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20 6f alue of the P3 o
19228 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 perand for a spe
19229 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f cific instructio
1922a 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1922b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1922c 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 64 3VdbeChangeP3(Vd
1922d 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c be *p, int addr,
1922e 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 int val){. ass
1922f 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e ert( p==0 || p->
19230 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
19231 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 C_INIT );. if(
19232 70 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26 20 p && addr>=0 &&
19233 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 p->nOp>addr && p
19234 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e ->aOp ){. p->
19235 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76 aOp[addr].p3 = v
19236 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a al;. }.}../*.**
19237 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
19238 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72 e of the P5 oper
19239 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 and for the most
1923a 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 recently.** add
1923b 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f ed operation..*/
1923c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1923d 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1923e 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70 ChangeP5(Vdbe *p
1923f 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73 , u8 val){. ass
19240 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e ert( p==0 || p->
19241 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
19242 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 C_INIT );. if(
19243 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 p && p->aOp ){.
19244 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f assert( p->nO
19245 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f p>0 );. p->aO
19246 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d p[p->nOp-1].p5 =
19247 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a val;. }.}../*.
19248 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 32 ** Change the P2
19249 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74 operand of inst
1924a 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20 ruction addr so
1924b 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
1924c 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73 o.** the address
1924d 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 of the next ins
1924e 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 truction to be c
1924f 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oded..*/.SQLITE_
19250 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19251 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
19252 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
19253 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 dr){. sqlite3Vd
19254 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61 64 beChangeP2(p, ad
19255 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a dr, p->nOp);.}..
19256 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e ./*.** If the in
19257 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72 75 put FuncDef stru
19258 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65 72 cture is ephemer
19259 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69 74 al, then free it
1925a 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e . If.** the Fun
1925b 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68 65 cDef is not ephe
1925c 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f rmal, then do no
1925d 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 thing..*/.static
1925e 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d 65 void freeEpheme
1925f 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 ralFunction(sqli
19260 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 66 te3 *db, FuncDef
19261 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20 70 *pDef){. if( p
19262 44 65 66 20 26 26 20 28 70 44 65 66 2d 3e 66 6c Def && (pDef->fl
19263 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e ags & SQLITE_FUN
19264 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a 20 C_EPHEM)!=0 ){.
19265 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
19266 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d 0a (db, pDef);. }.
19267 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
19268 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e 65 a P4 value if ne
19269 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 cessary..*/.stat
1926a 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 73 ic void freeP4(s
1926b 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
1926c 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 34 p4type, void *p4
1926d 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 20 ){. if( p4 ){.
1926e 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79 70 switch( p4typ
1926f 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 e ){. case
19270 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 63 P4_REAL:. c
19271 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 20 ase P4_INT64:.
19272 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 49 case P4_MPRI
19273 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 20 NTF:. case
19274 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 20 P4_DYNAMIC:.
19275 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 case P4_KEYINF
19276 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 O:. case P4
19277 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 20 _INTARRAY:.
19278 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f case P4_KEYINFO
19279 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 20 _HANDOFF: {.
1927a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1927b 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 20 e(db, p4);.
1927c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1927d 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1927e 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 20 VDBEFUNC: {.
1927f 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 VdbeFunc *pV
19280 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46 dbeFunc = (VdbeF
19281 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 20 unc *)p4;.
19282 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 freeEphemeralF
19283 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 62 unction(db, pVdb
19284 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a 20 eFunc->pFunc);.
19285 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
19286 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 beDeleteAuxData(
19287 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a 20 pVdbeFunc, 0);.
19288 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
19289 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 75 Free(db, pVdbeFu
1928a 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 nc);. bre
1928b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1928c 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 45 case P4_FUNCDE
1928d 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 65 F: {. fre
1928e 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 eEphemeralFuncti
1928f 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 2a on(db, (FuncDef*
19290 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 )p4);. br
19291 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
19292 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 case P4_MEM:
19293 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
19294 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c 69 3ValueFree((sqli
19295 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b 0a te3_value*)p4);.
19296 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
19297 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
19298 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 .}.../*.** Chang
19299 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 e N opcodes star
1929a 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 ting at addr to
1929b 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 No-ops..*/.SQLIT
1929c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1929d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1929e 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 ToNoop(Vdbe *p,
1929f 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 int addr, int N)
192a0 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e {. if( p && p->
192a1 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65 4f aOp ){. VdbeO
192a2 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 p *pOp = &p->aOp
192a3 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 [addr];. sqli
192a4 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
192a5 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 . while( N--
192a6 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 ){. freeP4(
192a7 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c db, pOp->p4type,
192a8 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 pOp->p4.p);.
192a9 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30 memset(pOp, 0
192aa 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 , sizeof(pOp[0])
192ab 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 );. pOp->op
192ac 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a code = OP_Noop;.
192ad 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 pOp++;.
192ae 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
192af 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
192b0 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72 61 of the P4 opera
192b1 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 nd for a specifi
192b2 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a c instruction..*
192b3 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
192b4 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 s useful when a
192b5 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 large program is
192b6 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a loaded from a.*
192b7 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75 * static array u
192b8 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 sing sqlite3Vdbe
192b9 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 AddOpList but we
192ba 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a want to make a.
192bb 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 ** few minor cha
192bc 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 nges to the prog
192bd 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e ram..**.** If n>
192be 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20 6f =0 then the P4 o
192bf 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d 69 perand is dynami
192c0 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 c, meaning that
192c1 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 a copy of.** the
192c2 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65 20 string is made
192c3 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
192c4 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
192c5 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 3_malloc()..** A
192c6 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d value of n==0 m
192c7 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73 20 eans copy bytes
192c8 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e 64 of zP4 up to and
192c9 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a including the.*
192ca 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 74 * first null byt
192cb 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e 20 e. If n>0 then
192cc 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20 6f copy n+1 bytes o
192cd 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 f zP4..**.** If
192ce 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74 n==P4_KEYINFO it
192cf 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34 20 means that zP4
192d0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
192d1 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 a KeyInfo struct
192d2 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69 ure..** A copy i
192d3 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b 65 s made of the Ke
192d4 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
192d5 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
192d6 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c ined from.** sql
192d7 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 ite3_malloc, to
192d8 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 be freed when th
192d9 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 e Vdbe is finali
192da 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 zed..** n==P4_KE
192db 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e YINFO_HANDOFF in
192dc 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50 34 dicates that zP4
192dd 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 points to a Key
192de 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a Info structure.*
192df 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f * stored in memo
192e0 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c ry that the call
192e1 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 er has obtained
192e2 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
192e3 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c loc. The .** cal
192e4 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66 ler should not f
192e5 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 ree the allocati
192e6 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 66 on, it will be f
192e7 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 reed when the Vd
192e8 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a be is.** finaliz
192e9 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 ed..** .** Other
192ea 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 34 values of n (P4
192eb 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c _STATIC, P4_COLL
192ec 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63 61 SEQ etc.) indica
192ed 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e te that zP4 poin
192ee 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e ts.** to a strin
192ef 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20 74 g or structure t
192f0 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 hat is guarantee
192f1 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20 74 d to exist for t
192f2 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a he lifetime of.*
192f3 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20 74 * the Vdbe. In t
192f4 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63 61 hese cases we ca
192f5 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65 20 n just copy the
192f6 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 pointer..**.** I
192f7 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63 68 f addr<0 then ch
192f8 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20 6d ange P4 on the m
192f9 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 ost recently ins
192fa 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69 6f erted instructio
192fb 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
192fc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
192fd 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 64 3VdbeChangeP4(Vd
192fe 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c be *p, int addr,
192ff 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 const char *zP4
19300 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a , int n){. Op *
19301 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a pOp;. sqlite3 *
19302 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 21 db;. assert( p!
19303 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e =0 );. db = p->
19304 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d db;. assert( p-
19305 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
19306 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
19307 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 p->aOp==0 || db
19308 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
19309 7b 0a 20 20 20 20 69 66 20 28 6e 20 21 3d 20 50 {. if (n != P
1930a 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a 20 20 20 4_KEYINFO) {.
1930b 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e 2c freeP4(db, n,
1930c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a 2a (void*)*(char**
1930d 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 20 )&zP4);. }.
1930e 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
1930f 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d 3e assert( addr<p->
19310 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64 64 nOp );. if( add
19311 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72 20 r<0 ){. addr
19312 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 = p->nOp - 1;.
19313 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 20 72 if( addr<0 ) r
19314 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 4f 70 eturn;. }. pOp
19315 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d = &p->aOp[addr]
19316 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 ;. freeP4(db, p
19317 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d Op->p4type, pOp-
19318 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d 3e 70 >p4.p);. pOp->p
19319 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4.p = 0;. if( n
1931a 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 ==P4_INT32 ){.
1931b 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 73 20 /* Note: this
1931c 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 62 65 cast is safe, be
1931d 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 69 6e cause the origin
1931e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 73 20 data point was
1931f 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 74 68 an int. ** th
19320 61 74 20 77 61 73 20 63 61 73 74 20 74 6f 20 61 at was cast to a
19321 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 2e (const char *).
19322 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e */. pOp->p4.
19323 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 i = SQLITE_PTR_T
19324 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20 20 20 O_INT(zP4);.
19325 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
19326 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73 65 20 _INT32;. }else
19327 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 if( zP4==0 ){.
19328 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b pOp->p4.p = 0;
19329 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 . pOp->p4type
1932a 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 = P4_NOTUSED;.
1932b 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 }else if( n==P4
1932c 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 _KEYINFO ){.
1932d 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 KeyInfo *pKeyInf
1932e 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 65 6c o;. int nFiel
1932f 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 20 6e d, nByte;.. n
19330 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 6e 66 Field = ((KeyInf
19331 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c 64 3b o*)zP4)->nField;
19332 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69 7a . nByte = siz
19333 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 20 2b eof(*pKeyInfo) +
19334 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 7a 65 (nField-1)*size
19335 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f of(pKeyInfo->aCo
19336 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c 64 3b ll[0]) + nField;
19337 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 . pKeyInfo =
19338 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e sqlite3Malloc( n
19339 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f 70 2d Byte );. pOp-
1933a 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 >p4.pKeyInfo = p
1933b 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66 28 KeyInfo;. if(
1933c 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 pKeyInfo ){.
1933d 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 u8 *aSortOrde
1933e 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 r;. memcpy(
1933f 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c 20 6e pKeyInfo, zP4, n
19340 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 53 6f Byte);. aSo
19341 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49 6e rtOrder = pKeyIn
19342 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b 0a fo->aSortOrder;.
19343 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 74 4f if( aSortO
19344 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 rder ){.
19345 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f pKeyInfo->aSortO
19346 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e 65 64 rder = (unsigned
19347 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e 66 6f char*)&pKeyInfo
19348 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 5d 3b ->aColl[nField];
19349 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
1934a 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f pKeyInfo->aSortO
1934b 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 65 72 rder, aSortOrder
1934c 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20 20 , nField);.
1934d 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34 }. pOp->p4
1934e 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46 type = P4_KEYINF
1934f 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 O;. }else{.
19350 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f p->db->mallo
19351 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
19352 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d pOp->p4type =
19353 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 P4_NOTUSED;.
19354 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }. }else if( n
19355 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e ==P4_KEYINFO_HAN
19356 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f 70 2d DOFF ){. pOp-
19357 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a >p4.p = (void*)z
19358 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 P4;. pOp->p4t
19359 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46 4f ype = P4_KEYINFO
1935a 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3c ;. }else if( n<
1935b 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 0 ){. pOp->p4
1935c 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b .p = (void*)zP4;
1935d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 . pOp->p4type
1935e 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 29 = (signed char)
1935f 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 n;. }else{.
19360 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20 73 if( n==0 ) n = s
19361 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
19362 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 P4);. pOp->p4
19363 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 .z = sqlite3DbSt
19364 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50 34 rNDup(p->db, zP4
19365 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 , n);. pOp->p
19366 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 4type = P4_DYNAM
19367 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 IC;. }.}..#ifnd
19368 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ef NDEBUG./*.**
19369 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d 65 Change the comme
1936a 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d 6f nt on the the mo
1936b 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65 st recently code
1936c 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 d instruction.
1936d 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20 4e Or.** insert a N
1936e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68 65 o-op and add the
1936f 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61 74 comment to that
19370 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e new instruction
19371 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65 73 . This.** makes
19372 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65 72 the code easier
19373 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67 20 to read during
19374 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e 65 debugging. None
19375 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 of this happens
19376 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63 74 .** in a product
19377 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53 51 ion build..*/.SQ
19378 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19379 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d d sqlite3VdbeCom
1937a 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f ment(Vdbe *p, co
1937b 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
1937c 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 t, ...){. va_li
1937d 73 74 20 61 70 3b 0a 20 20 61 73 73 65 72 74 28 st ap;. assert(
1937e 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e p->nOp>0 || p->
1937f 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 aOp==0 );. asse
19380 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c rt( p->aOp==0 ||
19381 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 p->aOp[p->nOp-1
19382 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c ].zComment==0 ||
19383 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
19384 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d iled );. if( p-
19385 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 72 >nOp ){. char
19386 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b **pz = &p->aOp[
19387 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 p->nOp-1].zComme
19388 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74 nt;. va_start
19389 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
1938a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1938b 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 (p->db, *pz);.
1938c 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 56 *pz = sqlite3V
1938d 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a MPrintf(p->db, z
1938e 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 Format, ap);.
1938f 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7d va_end(ap);. }
19390 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
19391 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
19392 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 beNoopComment(Vd
19393 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 be *p, const cha
19394 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 r *zFormat, ...)
19395 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a {. va_list ap;.
19396 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
19397 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b Op0(p, OP_Noop);
19398 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f . assert( p->nO
19399 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 p>0 || p->aOp==0
1939a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1939b 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f >aOp==0 || p->aO
1939c 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d p[p->nOp-1].zCom
1939d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 ment==0 || p->db
1939e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1939f 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 ;. if( p->nOp )
193a0 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 {. char **pz
193a1 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 = &p->aOp[p->nOp
193a2 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 -1].zComment;.
193a3 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
193a4 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c Format);. sql
193a5 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 ite3DbFree(p->db
193a6 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 , *pz);. *pz
193a7 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
193a8 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 f(p->db, zFormat
193a9 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e , ap);. va_en
193aa 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e d(ap);. }.}.#en
193ab 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a dif /* NDEBUG *
193ac 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
193ad 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 the opcode for a
193ae 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e 0a given address..
193af 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
193b0 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 E VdbeOp *sqlite
193b1 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 20 3VdbeGetOp(Vdbe
193b2 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 *p, int addr){.
193b3 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
193b4 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
193b5 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 IT );. assert(
193b6 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72 (addr>=0 && addr
193b7 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e 64 <p->nOp) || p->d
193b8 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
193b9 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 61 64 );. return ((ad
193ba 64 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70 2d dr>=0 && addr<p-
193bb 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61 4f 70 5b 61 >nOp)?(&p->aOp[a
193bc 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a 0a 23 69 66 ddr]):0);.}..#if
193bd 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
193be 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c _OMIT_EXPLAIN) |
193bf 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 | !defined(NDEBU
193c0 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 66 G) \. || def
193c1 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c ined(VDBE_PROFIL
193c2 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 E) || defined(SQ
193c3 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a LITE_DEBUG)./*.*
193c4 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 * Compute a stri
193c5 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62 65 ng that describe
193c6 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74 s the P4 paramet
193c7 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 65 er for an opcode
193c8 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 66 ..** Use zTemp f
193c9 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 or any required
193ca 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 temporary buffer
193cb 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 space..*/.stati
193cc 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 50 c char *displayP
193cd 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 20 4(Op *pOp, char
193ce 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 6d *zTemp, int nTem
193cf 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 20 p){. char *zP4
193d0 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 72 = zTemp;. asser
193d1 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b 0a t( nTemp>=20 );.
193d2 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 70 switch( pOp->p
193d3 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 4type ){. cas
193d4 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 e P4_KEYINFO_STA
193d5 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50 34 TIC:. case P4
193d6 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 20 _KEYINFO: {.
193d7 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 int i, j;.
193d8 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
193d9 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b nfo = pOp->p4.pK
193da 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71 eyInfo;. sq
193db 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
193dc 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b 65 Temp, zTemp, "ke
193dd 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79 49 yinfo(%d", pKeyI
193de 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20 20 nfo->nField);.
193df 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33 53 i = sqlite3S
193e0 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b 0a trlen30(zTemp);.
193e1 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
193e2 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c <pKeyInfo->nFiel
193e3 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 d; j++){.
193e4 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 CollSeq *pColl
193e5 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c = pKeyInfo->aCol
193e6 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 l[j];. if
193e7 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 ( pColl ){.
193e8 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c int n = sql
193e9 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43 6f ite3Strlen30(pCo
193ea 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ll->zName);.
193eb 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e 54 if( i+n>nT
193ec 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20 emp-6 ){.
193ed 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 memcpy(&zTe
193ee 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29 3b mp[i],",...",4);
193ef 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 . bre
193f0 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ak;. }.
193f1 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b zTemp[
193f2 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20 20 i++] = ',';.
193f3 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e if( pKeyIn
193f4 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26 fo->aSortOrder &
193f5 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 & pKeyInfo->aSor
193f6 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 20 tOrder[j] ){.
193f7 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 zTemp[i
193f8 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 ++] = '-';.
193f9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
193fa 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 memcpy(&zTemp[i
193fb 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c ], pColl->zName,
193fc 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 n+1);.
193fd 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 i += n;.
193fe 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e 54 }else if( i+4<nT
193ff 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20 emp-6 ){.
19400 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 memcpy(&zTemp
19401 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a 20 [i],",nil",4);.
19402 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 3b i += 4;
19403 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
19404 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 }. zTemp[i
19405 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 20 ++] = ')';.
19406 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a 20 zTemp[i] = 0;.
19407 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e assert( i<n
19408 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62 72 Temp );. br
19409 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1940a 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a 20 ase P4_COLLSEQ:
1940b 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 {. CollSeq
1940c 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70 34 *pColl = pOp->p4
1940d 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 71 .pColl;. sq
1940e 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
1940f 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63 6f Temp, zTemp, "co
19410 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20 70 llseq(%.20s)", p
19411 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Coll->zName);.
19412 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
19413 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e . case P4_FUN
19414 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46 75 CDEF: {. Fu
19415 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 4f ncDef *pDef = pO
19416 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 20 p->p4.pFunc;.
19417 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
19418 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
19419 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 66 , "%s(%d)", pDef
1941a 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 6e ->zName, pDef->n
1941b 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 Arg);. brea
1941c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1941d 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20 20 e P4_INT64: {.
1941e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1941f 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
19420 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70 2d p, "%lld", *pOp-
19421 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20 20 >p4.pI64);.
19422 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
19423 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32 3a case P4_INT32:
19424 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
19425 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c _snprintf(nTemp,
19426 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70 4f zTemp, "%d", pO
19427 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20 20 p->p4.i);.
19428 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
19429 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20 7b case P4_REAL: {
1942a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1942b 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
1942c 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 2a Temp, "%.16g", *
1942d 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b 0a pOp->p4.pReal);.
1942e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1942f 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4d }. case P4_M
19430 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 EM: {. Mem
19431 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e *pMem = pOp->p4.
19432 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 pMem;. asse
19433 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
19434 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 & MEM_Null)==0
19435 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 );. if( pMe
19436 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 m->flags & MEM_S
19437 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 tr ){. zP
19438 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 4 = pMem->z;.
19439 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 }else if( pMe
1943a 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 m->flags & MEM_I
1943b 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 nt ){. sq
1943c 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
1943d 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c Temp, zTemp, "%l
1943e 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b ld", pMem->u.i);
1943f 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
19440 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
19441 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 EM_Real ){.
19442 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
19443 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
19444 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d 2d , "%.16g", pMem-
19445 3e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 >r);. }.
19446 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
19447 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
19448 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
19449 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 41 . case P4_VTA
1944a 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 B: {. sqlit
1944b 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d e3_vtab *pVtab =
1944c 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a pOp->p4.pVtab;.
1944d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1944e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1944f 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 70 emp, "vtab:%p:%p
19450 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62 2d ", pVtab, pVtab-
19451 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20 >pModule);.
19452 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
19453 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50 34 ndif. case P4
19454 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 20 _INTARRAY: {.
19455 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
19456 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
19457 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a 20 , "intarray");.
19458 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
19459 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
1945a 0a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f 70 . zP4 = pOp
1945b 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 66 ->p4.z;. if
1945c 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 ( zP4==0 ){.
1945d 20 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 3b zP4 = zTemp;
1945e 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 30 . zTemp[0
1945f 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 ] = 0;. }.
19460 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
19461 74 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 72 t( zP4!=0 );. r
19462 65 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e eturn zP4;.}.#en
19463 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 dif../*.** Decla
19464 72 65 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 re to the Vdbe t
19465 68 61 74 20 74 68 65 20 42 54 72 65 65 20 6f 62 hat the BTree ob
19466 6a 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62 5b ject at db->aDb[
19467 69 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a i] is used..**.*
19468 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19469 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1946a 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 20 eUsesBtree(Vdbe
1946b 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e *p, int i){. in
1946c 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65 72 74 t mask;. assert
1946d 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 64 ( i>=0 && i<p->d
1946e 62 2d 3e 6e 44 62 20 26 26 20 69 3c 73 69 7a 65 b->nDb && i<size
1946f 6f 66 28 75 33 32 29 2a 38 20 29 3b 0a 20 20 61 of(u32)*8 );. a
19470 73 73 65 72 74 28 20 69 3c 28 69 6e 74 29 73 69 ssert( i<(int)si
19471 7a 65 6f 66 28 70 2d 3e 62 74 72 65 65 4d 61 73 zeof(p->btreeMas
19472 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 73 6b 20 3d k)*8 );. mask =
19473 20 28 28 75 33 32 29 31 29 3c 3c 69 3b 0a 20 20 ((u32)1)<<i;.
19474 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 if( (p->btreeMas
19475 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a k & mask)==0 ){.
19476 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b p->btreeMask
19477 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 73 71 |= mask;. sq
19478 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
19479 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d 3e 61 rrayInsert(&p->a
1947a 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e 61 44 Mutex, p->db->aD
1947b 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a 7d b[i].pBt);. }.}
1947c 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 56 ...#if defined(V
1947d 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 DBE_PROFILE) ||
1947e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
1947f 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e EBUG)./*.** Prin
19480 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63 6f 64 t a single opcod
19481 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
19482 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 is used for deb
19483 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a ugging only..*/.
19484 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19485 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 oid sqlite3VdbeP
19486 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70 4f 75 rintOp(FILE *pOu
19487 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20 2a 70 t, int pc, Op *p
19488 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 Op){. char *zP4
19489 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b 35 30 ;. char zPtr[50
1948a 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ];. static cons
1948b 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 31 t char *zFormat1
1948c 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20 25 34 = "%4d %-13s %4
1948d 64 20 25 34 64 20 25 34 64 20 25 2d 34 73 20 25 d %4d %4d %-4s %
1948e 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69 66 28 .2X %s\n";. if(
1948f 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75 74 20 pOut==0 ) pOut
19490 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50 34 20 = stdout;. zP4
19491 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 2c = displayP4(pOp,
19492 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28 7a 50 zPtr, sizeof(zP
19493 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 tr));. fprintf(
19494 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31 2c 20 pOut, zFormat1,
19495 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c 69 74 pc, . sqlit
19496 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 e3OpcodeName(pOp
19497 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70 2d 3e ->opcode), pOp->
19498 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 70 p1, pOp->p2, pOp
19499 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70 2d 3e ->p3, zP4, pOp->
1949a 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 p5,.#ifdef SQLIT
1949b 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f E_DEBUG. pO
1949c 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70 4f p->zComment ? pO
1949d 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22 22 p->zComment : ""
1949e 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22 22 0a .#else. "".
1949f 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 66 66 #endif. );. ff
194a0 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 65 lush(pOut);.}.#e
194a1 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 ndif../*.** Rele
194a2 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 ase an array of
194a3 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a 2a N Mem elements.*
194a4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
194a5 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 4d 65 leaseMemArray(Me
194a6 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 m *p, int N){.
194a7 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a 20 20 if( p && N ){.
194a8 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20 20 20 Mem *pEnd;.
194a9 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
194aa 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20 6d 61 6c ->db;. u8 mal
194ab 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64 62 2d loc_failed = db-
194ac 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a 20 >mallocFailed;.
194ad 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b 4e for(pEnd=&p[N
194ae 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29 7b ]; p<pEnd; p++){
194af 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
194b0 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c 20 &p[1])==pEnd ||
194b1 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64 62 p[0].db==p[1].db
194b2 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 );.. /* Th
194b3 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65 61 6c is block is real
194b4 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20 76 65 ly an inlined ve
194b5 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 rsion of sqlite3
194b6 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 29 VdbeMemRelease()
194b7 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74 . ** that t
194b8 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65 20 6f akes advantage o
194b9 66 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 f the fact that
194ba 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
194bb 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20 20 20 value is .
194bc 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74 6f 20 ** being set to
194bd 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c 65 61 NULL after relea
194be 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d 69 63 sing any dynamic
194bf 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20 20 20 resources..
194c0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 **. ** Th
194c1 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 e justification
194c2 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e 67 20 for duplicating
194c3 63 6f 64 65 20 69 73 20 74 68 61 74 20 61 63 63 code is that acc
194c4 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20 20 20 ording to .
194c5 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c 20 74 ** callgrind, t
194c6 68 69 73 20 63 61 75 73 65 73 20 61 20 63 65 72 his causes a cer
194c7 74 61 69 6e 20 74 65 73 74 20 63 61 73 65 20 74 tain test case t
194c8 6f 20 68 69 74 20 74 68 65 20 43 50 55 20 34 2e o hit the CPU 4.
194c9 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65 72 63 7 . ** perc
194ca 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20 6c 69 ent less (x86 li
194cb 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69 6f 6e nux, gcc version
194cc 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74 68 61 4.1.2, -O6) tha
194cd 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a 20 73 n if . ** s
194ce 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61 73 65 qlite3MemRelease
194cf 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64 20 66 () were called f
194d0 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68 20 2d rom here. With -
194d1 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73 0a 20 O2, this jumps.
194d2 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36 20 70 ** to 6.6 p
194d3 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65 73 74 ercent. The test
194d4 20 63 61 73 65 20 69 73 20 69 6e 73 65 72 74 69 case is inserti
194d5 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69 6e 74 ng 1000 rows int
194d6 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20 20 20 o a table .
194d7 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e 64 65 ** with no inde
194d8 78 65 73 20 75 73 69 6e 67 20 61 20 73 69 6e 67 xes using a sing
194d9 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e 53 45 le prepared INSE
194da 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 69 RT statement, bi
194db 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a 20 61 nd() . ** a
194dc 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e 73 65 nd reset(). Inse
194dd 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 rts are grouped
194de 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 74 69 into a transacti
194df 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 on.. */.
194e0 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 if( p->flags&
194e1 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e (MEM_Agg|MEM_Dyn
194e2 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c ) ){. sql
194e3 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
194e4 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c se(p);. }el
194e5 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f se if( p->zMallo
194e6 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c c ){. sql
194e7 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
194e8 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 ->zMalloc);.
194e9 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d p->zMalloc =
194ea 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 0;. }..
194eb 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 p->flags = ME
194ec 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 M_Null;. }.
194ed 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
194ee 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c ed = malloc_fail
194ef 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 ed;. }.}..#ifde
194f0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
194f1 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e MEMORY_MANAGEMEN
194f2 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
194f3 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
194f4 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 56 ReleaseBuffers(V
194f5 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 dbe *p){. int i
194f6 69 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d i;. int nFree =
194f7 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 0;. assert( sq
194f8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
194f9 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
194fa 3b 0a 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 ;. for(ii=1; ii
194fb 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b 29 <=p->nMem; ii++)
194fc 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 {. Mem *pMem
194fd 3d 20 26 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b 0a = &p->aMem[ii];.
194fe 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c if( pMem->fl
194ff 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
19500 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
19501 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 3RowSetClear(pMe
19502 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 m->u.pRowSet);.
19503 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4d 65 }. if( pMe
19504 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 66 6c m->z && pMem->fl
19505 61 67 73 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 ags&MEM_Dyn ){.
19506 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 4d assert( !pM
19507 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20 em->xDel );.
19508 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 nFree += sqlit
19509 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 e3DbMallocSize(p
1950a 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
1950b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1950c 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
1950d 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Mem);. }. }.
1950e 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a return nFree;.
1950f 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
19510 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 f SQLITE_OMIT_EX
19511 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 PLAIN./*.** Give
19512 20 61 20 6c 69 73 74 69 6e 67 20 6f 66 20 74 68 a listing of th
19513 65 20 70 72 6f 67 72 61 6d 20 69 6e 20 74 68 65 e program in the
19514 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
19515 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ..**.** The inte
19516 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d rface is the sam
19517 65 20 61 73 20 73 71 6c 69 74 65 33 56 64 62 65 e as sqlite3Vdbe
19518 45 78 65 63 28 29 2e 20 20 42 75 74 20 69 6e 73 Exec(). But ins
19519 74 65 61 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 tead of.** runni
1951a 6e 67 20 74 68 65 20 63 6f 64 65 2c 20 69 74 20 ng the code, it
1951b 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 61 6c 6c invokes the call
1951c 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 back once for ea
1951d 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ch instruction..
1951e 2a 2a 20 54 68 69 73 20 66 65 61 74 75 72 65 20 ** This feature
1951f 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 is used to imple
19520 6d 65 6e 74 20 22 45 58 50 4c 41 49 4e 22 2e 0a ment "EXPLAIN"..
19521 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 78 **.** When p->ex
19522 70 6c 61 69 6e 3d 3d 31 2c 20 65 61 63 68 20 69 plain==1, each i
19523 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6c 69 nstruction is li
19524 73 74 65 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 sted. When.** p
19525 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e ->explain==2, on
19526 6c 79 20 4f 50 5f 45 78 70 6c 61 69 6e 20 69 6e ly OP_Explain in
19527 73 74 72 75 63 74 69 6f 6e 73 20 61 72 65 20 6c structions are l
19528 69 73 74 65 64 20 61 6e 64 20 74 68 65 73 65 0a isted and these.
19529 2a 2a 20 61 72 65 20 73 68 6f 77 6e 20 69 6e 20 ** are shown in
1952a 61 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 6d a different form
1952b 61 74 2e 20 20 70 2d 3e 65 78 70 6c 61 69 6e 3d at. p->explain=
1952c 3d 32 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d =2 is used to im
1952d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 plement.** EXPLA
1952e 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 2e 0a 2a IN QUERY PLAN..*
1952f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19530 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
19531 4c 69 73 74 28 0a 20 20 56 64 62 65 20 2a 70 20 List(. Vdbe *p
19532 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19533 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f /* The VDBE */
19534 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
19535 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 b = p->db;. int
19536 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 i;. int rc = S
19537 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 QLITE_OK;. Mem
19538 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 *pMem = p->pResu
19539 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d ltSet = &p->aMem
1953a 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 [1];.. assert(
1953b 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 p->explain );.
1953c 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 if( p->magic!=VD
1953d 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 20 72 BE_MAGIC_RUN ) r
1953e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1953f 55 53 45 3b 0a 20 20 61 73 73 65 72 74 28 20 64 USE;. assert( d
19540 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 b->magic==SQLITE
19541 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 _MAGIC_BUSY );.
19542 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d assert( p->rc==
19543 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e SQLITE_OK || p->
19544 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
19545 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 || p->rc==SQLITE
19546 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 _NOMEM );.. /*
19547 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73 Even though this
19548 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 opcode does not
19549 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72 use dynamic str
1954a 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 ings for. ** th
1954b 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 e result, result
1954c 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 columns may bec
1954d 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74 ome dynamic if t
1954e 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 he user calls.
1954f 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
19550 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73 n_text16(), caus
19551 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f ing a translatio
19552 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f n to UTF-16 enco
19553 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c ding.. */. rel
19554 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 easeMemArray(pMe
19555 6d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 0a 20 20 m, p->nMem);..
19556 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 if( p->rc==SQLIT
19557 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f E_NOMEM ){. /
19558 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 69 * This happens i
19559 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 f a malloc() ins
1955a 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ide a call to sq
1955b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
1955c 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 t() or. ** sq
1955d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
1955e 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 2a t16() failed. *
1955f 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 /. db->malloc
19560 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
19561 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
19562 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a ROR;. }.. do{.
19563 20 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b i = p->pc++;
19564 0a 20 20 7d 77 68 69 6c 65 28 20 69 3c 70 2d 3e . }while( i<p->
19565 6e 4f 70 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 nOp && p->explai
19566 6e 3d 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 n==2 && p->aOp[i
19567 5d 2e 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 ].opcode!=OP_Exp
19568 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e lain );. if( i>
19569 3d 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 70 =p->nOp ){. p
1956a 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b ->rc = SQLITE_OK
1956b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1956c 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 E_DONE;. }else
1956d 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 if( db->u1.isInt
1956e 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20 errupted ){.
1956f 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 p->rc = SQLITE_I
19570 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 72 63 NTERRUPT;. rc
19571 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
19572 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
19573 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
19574 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
19575 69 74 65 33 45 72 72 53 74 72 28 70 2d 3e 72 63 ite3ErrStr(p->rc
19576 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ));. }else{.
19577 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 char *z;. Op
19578 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b *pOp = &p->aOp[
19579 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 i];. if( p->e
1957a 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 xplain==1 ){.
1957b 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1957c 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1957d 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1957e 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 ITE_INTEGER;.
1957f 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 pMem->u.i = i
19580 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19581 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19582 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e /* Program coun
19583 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ter */. pMe
19584 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d m++;. . pM
19585 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
19586 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d Static|MEM_Str|M
19587 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 EM_Term;. p
19588 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 Mem->z = (char*)
19589 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d sqlite3OpcodeNam
1958a 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 e(pOp->opcode);
1958b 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 /* Opcode */.
1958c 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
1958d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 ->z!=0 );.
1958e 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 pMem->n = sqlite
1958f 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 3Strlen30(pMem->
19590 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e z);. pMem->
19591 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
19592 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e XT;. pMem->
19593 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
19594 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 8;. pMem++;
19595 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d . }.. pMem
19596 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
19597 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 t;. pMem->u.i
19598 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 = pOp->p1;
19599 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1959a 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 /* P1 */.
1959b 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1959c 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 QLITE_INTEGER;.
1959d 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 pMem++;..
1959e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1959f 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d M_Int;. pMem-
195a0 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 >u.i = pOp->p2;
195a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
195a2 20 20 20 20 20 20 20 20 20 2f 2a 20 50 32 20 2a /* P2 *
195a3 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 /. pMem->type
195a4 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 = SQLITE_INTEGE
195a5 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a R;. pMem++;..
195a6 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 if( p->expla
195a7 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 in==1 ){. p
195a8 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
195a9 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d _Int;. pMem
195aa 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b ->u.i = pOp->p3;
195ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
195ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33 20 /* P3
195ad 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 */. pMem->t
195ae 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
195af 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d EGER;. pMem
195b0 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 ++;. }.. i
195b1 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
195b2 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20 mGrow(pMem, 32,
195b3 30 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 0) ){
195b4 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20 20 20 20 /* P4 */.
195b5 61 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d assert( p->db->m
195b6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
195b7 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
195b8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a TE_ERROR;. }.
195b9 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
195ba 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 = MEM_Dyn|MEM_St
195bb 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 r|MEM_Term;.
195bc 7a 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f z = displayP4(pO
195bd 70 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 3b p, pMem->z, 32);
195be 0a 20 20 20 20 69 66 28 20 7a 21 3d 70 4d 65 6d . if( z!=pMem
195bf 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ->z ){. sql
195c0 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
195c1 72 28 70 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 53 r(pMem, z, -1, S
195c2 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a QLITE_UTF8, 0);.
195c3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
195c4 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a assert( pMem->z
195c5 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 !=0 );. pMe
195c6 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 m->n = sqlite3St
195c7 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b rlen30(pMem->z);
195c8 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 . pMem->enc
195c9 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a = SQLITE_UTF8;.
195ca 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
195cb 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
195cc 58 54 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a XT;. pMem++;.
195cd 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c . if( p->expl
195ce 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 ain==1 ){.
195cf 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
195d0 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 34 2c 20 emGrow(pMem, 4,
195d1 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 0) ){. as
195d2 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c sert( p->db->mal
195d3 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
195d4 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
195d5 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 TE_ERROR;.
195d6 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c }. pMem->fl
195d7 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 ags = MEM_Dyn|ME
195d8 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
195d9 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 pMem->n =
195da 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 2;. sqlite3
195db 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d 65 _snprintf(3, pMe
195dc 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70 4f m->z, "%.2x", pO
195dd 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35 20 p->p5); /* P5
195de 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 */. pMem->t
195df 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 ype = SQLITE_TEX
195e0 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 T;. pMem->e
195e1 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 nc = SQLITE_UTF8
195e2 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a ;. pMem++;.
195e3 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 .#ifdef SQLITE
195e4 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 28 _DEBUG. if(
195e5 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 29 pOp->zComment )
195e6 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e {. pMem->
195e7 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c flags = MEM_Str|
195e8 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 MEM_Term;.
195e9 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d pMem->z = pOp-
195ea 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 20 >zComment;.
195eb 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c pMem->n = sql
195ec 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 ite3Strlen30(pMe
195ed 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 20 70 m->z);. p
195ee 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
195ef 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 20 20 E_UTF8;.
195f0 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
195f1 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 ITE_TEXT;.
195f2 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 }else.#endif.
195f3 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 {. pMe
195f4 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e m->flags = MEM_N
195f5 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 ull;
195f6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
195f7 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 mment */.
195f8 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
195f9 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 LITE_NULL;.
195fa 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 2d }. }.. p-
195fb 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 38 20 >nResColumn = 8
195fc 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69 6e 2d - 5*(p->explain-
195fd 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 1);. p->rc =
195fe 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 SQLITE_OK;. r
195ff 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a c = SQLITE_ROW;.
19600 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
19601 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
19602 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e ITE_OMIT_EXPLAIN
19603 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 */..#ifdef SQLI
19604 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 TE_DEBUG./*.** P
19605 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74 68 61 rint the SQL tha
19606 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 67 65 t was used to ge
19607 6e 65 72 61 74 65 20 61 20 56 44 42 45 20 70 72 nerate a VDBE pr
19608 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ogram..*/.SQLITE
19609 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1960a 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 lite3VdbePrintSq
1960b 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e l(Vdbe *p){. in
1960c 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a t nOp = p->nOp;.
1960d 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 VdbeOp *pOp;.
1960e 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 74 if( nOp<1 ) ret
1960f 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d urn;. pOp = &p-
19610 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70 >aOp[0];. if( p
19611 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 Op->opcode==OP_T
19612 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e race && pOp->p4.
19613 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 z!=0 ){. cons
19614 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d t char *z = pOp-
19615 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69 6c 65 >p4.z;. while
19616 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 ( sqlite3Isspace
19617 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 (*z) ) z++;.
19618 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73 printf("SQL: [%s
19619 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a ]\n", z);. }.}.
1961a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 #endif..#if !def
1961b 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1961c 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e _TRACE) && defin
1961d 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
1961e 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 _IOTRACE)./*.**
1961f 50 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43 45 Print an IOTRACE
19620 20 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e 67 message showing
19621 20 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f SQL content..*/
19622 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19623 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19624 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 20 IOTraceSql(Vdbe
19625 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d *p){. int nOp =
19626 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f p->nOp;. VdbeO
19627 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73 71 p *pOp;. if( sq
19628 6c 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30 20 lite3IoTrace==0
19629 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
1962a 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a nOp<1 ) return;.
1962b 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b pOp = &p->aOp[
1962c 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 0];. if( pOp->o
1962d 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 pcode==OP_Trace
1962e 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 && pOp->p4.z!=0
1962f 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b ){. int i, j;
19630 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 30 . char z[1000
19631 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ];. sqlite3_s
19632 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
19633 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d ), z, "%s", pOp-
19634 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28 >p4.z);. for(
19635 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 73 70 i=0; sqlite3Issp
19636 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b ace(z[i]); i++){
19637 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 7a }. for(j=0; z
19638 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 [i]; i++){.
19639 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 73 70 if( sqlite3Issp
1963a 61 63 65 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 ace(z[i]) ){.
1963b 20 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21 if( z[i-1]!
1963c 3d 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20 20 =' ' ){.
1963d 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a z[j++] = ' ';.
1963e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1963f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a }else{. z
19640 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 [j++] = z[i];.
19641 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
19642 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 z[j] = 0;. sq
19643 6c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 51 lite3IoTrace("SQ
19644 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d L %s\n", z);. }
19645 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 .}.#endif /* !SQ
19646 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 LITE_OMIT_TRACE
19647 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
19648 5f 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a _IOTRACE */../*.
19649 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 ** Allocate spac
1964a 65 20 66 72 6f 6d 20 61 20 66 69 78 65 64 20 73 e from a fixed s
1964b 69 7a 65 20 62 75 66 66 65 72 2e 20 20 4d 61 6b ize buffer. Mak
1964c 65 20 2a 70 70 20 70 6f 69 6e 74 20 74 6f 20 74 e *pp point to t
1964d 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 he.** allocated
1964e 73 70 61 63 65 2e 20 20 28 4e 6f 74 65 3a 20 20 space. (Note:
1964f 70 70 20 69 73 20 61 20 63 68 61 72 2a 20 72 61 pp is a char* ra
19650 74 68 65 72 20 74 68 61 6e 20 61 20 76 6f 69 64 ther than a void
19651 2a 2a 20 74 6f 0a 2a 2a 20 77 6f 72 6b 20 61 72 ** to.** work ar
19652 6f 75 6e 64 20 74 68 65 20 70 6f 69 6e 74 65 72 ound the pointer
19653 20 61 6c 69 61 73 69 6e 67 20 72 75 6c 65 73 20 aliasing rules
19654 6f 66 20 43 2e 29 20 20 2a 70 70 20 73 68 6f 75 of C.) *pp shou
19655 6c 64 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 ld initially.**
19656 62 65 20 7a 65 72 6f 2e 20 20 49 66 20 2a 70 70 be zero. If *pp
19657 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 is not zero, th
19658 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 at means that th
19659 65 20 73 70 61 63 65 20 68 61 73 20 61 6c 72 65 e space has alre
1965a 61 64 79 0a 2a 2a 20 62 65 65 6e 20 61 6c 6c 6f ady.** been allo
1965b 63 61 74 65 64 20 61 6e 64 20 74 68 69 73 20 72 cated and this r
1965c 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 6f 70 outine is a noop
1965d 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 20 69 73 ..**.** nByte is
1965e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
1965f 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65 ytes of space ne
19660 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 46 eded..**.** *ppF
19661 72 6f 6d 20 70 6f 69 6e 74 20 74 6f 20 61 76 61 rom point to ava
19662 69 6c 61 62 6c 65 20 73 70 61 63 65 20 61 6e 64 ilable space and
19663 20 70 45 6e 64 20 70 6f 69 6e 74 73 20 74 6f 20 pEnd points to
19664 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
19665 2a 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 * available spac
19666 65 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 42 79 74 65 e..**.** *pnByte
19667 20 69 73 20 61 20 63 6f 75 6e 74 65 72 20 6f 66 is a counter of
19668 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
19669 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 74 68 ytes of space th
1966a 61 74 20 68 61 76 65 20 66 61 69 6c 65 64 0a 2a at have failed.*
1966b 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 20 * to allocate.
1966c 49 66 20 74 68 65 72 65 20 69 73 20 69 6e 73 75 If there is insu
1966d 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 69 fficient space i
1966e 6e 20 2a 70 70 46 72 6f 6d 20 74 6f 20 73 61 74 n *ppFrom to sat
1966f 69 73 66 79 20 74 68 65 0a 2a 2a 20 72 65 71 75 isfy the.** requ
19670 65 73 74 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d est, then increm
19671 65 6e 74 20 2a 70 6e 42 79 74 65 20 62 79 20 74 ent *pnByte by t
19672 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 he amount of the
19673 20 72 65 71 75 65 73 74 2e 0a 2a 2f 0a 73 74 61 request..*/.sta
19674 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 53 70 tic void allocSp
19675 61 63 65 28 0a 20 20 63 68 61 72 20 2a 70 70 2c ace(. char *pp,
19676 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
19677 4e 2f 4f 55 54 3a 20 53 65 74 20 2a 70 70 20 74 N/OUT: Set *pp t
19678 6f 20 70 6f 69 6e 74 20 74 6f 20 61 6c 6c 6f 63 o point to alloc
19679 61 74 65 64 20 62 75 66 66 65 72 20 2a 2f 0a 20 ated buffer */.
1967a 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 int nByte,
1967b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1967c 6f 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c 6f of bytes to allo
1967d 63 61 74 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 70 cate */. u8 **p
1967e 70 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 2f pFrom, /
1967f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63 61 * IN/OUT: Alloca
19680 74 65 20 66 72 6f 6d 20 2a 70 70 46 72 6f 6d 20 te from *ppFrom
19681 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 2c 20 20 */. u8 *pEnd,
19682 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
19683 6e 74 65 72 20 74 6f 20 31 20 62 79 74 65 20 70 nter to 1 byte p
19684 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 2a ast the end of *
19685 70 70 46 72 6f 6d 20 62 75 66 66 65 72 20 2a 2f ppFrom buffer */
19686 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 20 20 . int *pnByte
19687 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6c /* If al
19688 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 location cannot
19689 62 65 20 6d 61 64 65 2c 20 69 6e 63 72 65 6d 65 be made, increme
1968a 6e 74 20 2a 70 6e 42 79 74 65 20 2a 2f 0a 29 7b nt *pnByte */.){
1968b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
1968c 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
1968d 2a 70 70 46 72 6f 6d 29 20 29 3b 0a 20 20 69 66 *ppFrom) );. if
1968e 28 20 28 2a 28 76 6f 69 64 2a 2a 29 70 70 29 3d ( (*(void**)pp)=
1968f 3d 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 =0 ){. nByte
19690 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b = ROUND8(nByte);
19691 0a 20 20 20 20 69 66 28 20 28 70 45 6e 64 20 2d . if( (pEnd -
19692 20 2a 70 70 46 72 6f 6d 29 3e 3d 6e 42 79 74 65 *ppFrom)>=nByte
19693 20 29 7b 0a 20 20 20 20 20 20 2a 28 76 6f 69 64 ){. *(void
19694 2a 2a 29 70 70 20 3d 20 28 76 6f 69 64 20 2a 29 **)pp = (void *)
19695 2a 70 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20 2a *ppFrom;. *
19696 70 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b ppFrom += nByte;
19697 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
19698 20 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 *pnByte += nBy
19699 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a te;. }. }.}.
1969a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 ./*.** Prepare a
1969b 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1969c 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 for execution.
1969d 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 This involves t
1969e 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 hings such.** as
1969f 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 allocating stac
196a0 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74 k space and init
196a1 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f ializing the pro
196a2 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a gram counter..**
196a3 20 41 66 74 65 72 20 74 68 65 20 56 44 42 45 20 After the VDBE
196a4 68 61 73 20 62 65 20 70 72 65 70 70 65 64 2c 20 has be prepped,
196a5 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 75 74 it can be execut
196a6 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 ed by one or mor
196a7 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 e.** calls to sq
196a8 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e lite3VdbeExec().
196a9 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 .**.** This is
196aa 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f the only way to
196ab 20 6d 6f 76 65 20 61 20 56 44 42 45 20 66 72 6f move a VDBE fro
196ac 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 m VDBE_MAGIC_INI
196ad 54 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 T to.** VDBE_MAG
196ae 49 43 5f 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 IC_RUN..**.** Th
196af 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 is function may
196b0 62 65 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20 74 be called more t
196b1 68 61 6e 20 6f 6e 63 65 20 6f 6e 20 61 20 73 69 han once on a si
196b2 6e 67 6c 65 20 76 69 72 74 75 61 6c 20 6d 61 63 ngle virtual mac
196b3 68 69 6e 65 2e 0a 2a 2a 20 54 68 65 20 66 69 72 hine..** The fir
196b4 73 74 20 63 61 6c 6c 20 69 73 20 6d 61 64 65 20 st call is made
196b5 77 68 69 6c 65 20 63 6f 6d 70 69 6c 69 6e 67 20 while compiling
196b6 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
196b7 74 2e 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a t. Subsequent.**
196b8 20 63 61 6c 6c 73 20 61 72 65 20 6d 61 64 65 20 calls are made
196b9 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 as part of the p
196ba 72 6f 63 65 73 73 20 6f 66 20 72 65 73 65 74 74 rocess of resett
196bb 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ing a statement
196bc 74 6f 20 62 65 0a 2a 2a 20 72 65 2d 65 78 65 63 to be.** re-exec
196bd 75 74 65 64 20 28 66 72 6f 6d 20 61 20 63 61 6c uted (from a cal
196be 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 l to sqlite3_res
196bf 65 74 28 29 29 2e 20 54 68 65 20 6e 56 61 72 2c et()). The nVar,
196c0 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 0a nMem, nCursor .
196c1 2a 2a 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e ** and isExplain
196c2 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 parameters are
196c3 6f 6e 6c 79 20 70 61 73 73 65 64 20 63 6f 72 72 only passed corr
196c4 65 63 74 20 76 61 6c 75 65 73 20 74 68 65 20 66 ect values the f
196c5 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 65 irst time.** the
196c6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
196c7 6c 65 64 2e 20 4f 6e 20 73 75 62 73 65 71 75 65 led. On subseque
196c8 6e 74 20 63 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 nt calls, from s
196c9 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 20 qlite3_reset(),
196ca 6e 56 61 72 0a 2a 2a 20 69 73 20 70 61 73 73 65 nVar.** is passe
196cb 64 20 2d 31 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e d -1 and nMem, n
196cc 43 75 72 73 6f 72 20 61 6e 64 20 69 73 45 78 70 Cursor and isExp
196cd 6c 61 69 6e 20 61 72 65 20 61 6c 6c 20 70 61 73 lain are all pas
196ce 73 65 64 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c sed zero..*/.SQL
196cf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
196d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
196d1 52 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a 70 Ready(. Vdbe *p
196d2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
196d3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
196d4 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 DBE */. int nVa
196d5 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
196d6 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
196d7 72 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e 20 r of '?' see in
196d8 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
196d9 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c t */. int nMem,
196da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
196db 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
196dc 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 of memory cells
196dd 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 to allocate */.
196de 20 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20 20 int nCursor,
196df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
196e0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 /* Number of cur
196e1 73 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 sors to allocate
196e2 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 78 70 6c */. int isExpl
196e3 61 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ain
196e4 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
196e5 74 68 65 20 45 58 50 4c 41 49 4e 20 6b 65 79 77 the EXPLAIN keyw
196e6 6f 72 64 73 20 69 73 20 70 72 65 73 65 6e 74 20 ords is present
196e7 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 */.){. int n;.
196e8 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
196e9 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 ->db;.. assert(
196ea 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 p!=0 );. asser
196eb 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
196ec 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
196ed 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 . /* There shou
196ee 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f ld be at least o
196ef 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a ne opcode.. */.
196f0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 assert( p->nOp
196f1 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 >0 );.. /* Set
196f2 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42 the magic to VDB
196f3 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e E_MAGIC_RUN soon
196f4 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c er rather than l
196f5 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 ater. */. p->ma
196f6 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 gic = VDBE_MAGIC
196f7 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 _RUN;.. /* For
196f8 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71 75 each cursor requ
196f9 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 ired, also alloc
196fa 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c ate a memory cel
196fb 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 l. Memory. ** c
196fc 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 ells (nMem+1-nCu
196fd 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 rsor)..nMem, inc
196fe 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 lusive, will nev
196ff 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 er be used by.
19700 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 ** the vdbe prog
19701 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65 ram. Instead the
19702 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c y are used to al
19703 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
19704 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 . ** VdbeCursor
19705 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63 74 /BtCursor struct
19706 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f ures. The blob o
19707 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 f memory associa
19708 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 ted with . ** c
19709 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65 ursor 0 is store
1970a 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c d in memory cell
1970b 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 nMem. Memory ce
1970c 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a ll (nMem-1). **
1970d 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 stores the blob
1970e 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 of memory assoc
1970f 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f iated with curso
19710 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 r 1, etc.. **.
19711 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c ** See also: al
19712 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a locateCursor()..
19713 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e */. nMem += n
19714 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c Cursor;.. /* Al
19715 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
19716 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 memory register
19717 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 s, SQL variables
19718 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 61 , VDBE cursors a
19719 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 nd . ** an arra
1971a 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c y to marshal SQL
1971b 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
1971c 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20 nts in. This is
1971d 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 only done the.
1971e 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 ** first time th
1971f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
19720 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65 alled for a give
19721 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e n VDBE, not when
19722 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e it is. ** bein
19723 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 g called from sq
19724 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f lite3_reset() to
19725 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74 75 reset the virtu
19726 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f al machine.. */
19727 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 . if( nVar>=0 &
19728 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 & !db->mallocFai
19729 6c 65 64 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a led ){. u8 *z
1972a 43 73 72 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e Csr = (u8 *)&p->
1972b 61 4f 70 5b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 aOp[p->nOp];.
1972c 20 75 38 20 2a 7a 45 6e 64 20 3d 20 28 75 38 20 u8 *zEnd = (u8
1972d 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 *)&p->aOp[p->nOp
1972e 41 6c 6c 6f 63 5d 3b 0a 20 20 20 20 69 6e 74 20 Alloc];. int
1972f 6e 42 79 74 65 3b 0a 20 20 20 20 69 6e 74 20 6e nByte;. int n
19730 41 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 Arg; /* Ma
19731 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
19732 61 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61 args passed to a
19733 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 user function.
19734 2a 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 */. resolveP2
19735 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 Values(p, &nArg)
19736 3b 0a 20 20 20 20 69 66 28 20 69 73 45 78 70 6c ;. if( isExpl
19737 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29 ain && nMem<10 )
19738 7b 0a 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31 {. nMem = 1
19739 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 73 0;. }. zCs
1973a 72 20 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 38 r += (zCsr - (u8
1973b 2a 29 30 29 26 37 3b 0a 20 20 20 20 61 73 73 65 *)0)&7;. asse
1973c 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1973d 4c 49 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29 LIGNMENT(zCsr) )
1973e 3b 0a 20 20 20 20 69 66 28 20 7a 45 6e 64 3c 7a ;. if( zEnd<z
1973f 43 73 72 20 29 20 7a 45 6e 64 20 3d 20 7a 43 73 Csr ) zEnd = zCs
19740 72 3b 0a 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 r;.. do {.
19741 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c 20 memset(zCsr,
19742 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a 20 0, zEnd-zCsr);.
19743 20 20 20 20 20 6e 42 79 74 65 20 3d 20 30 3b 0a nByte = 0;.
19744 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 allocSpace
19745 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 4d 65 6d ((char*)&p->aMem
19746 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 , nMem*sizeof(Me
19747 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c m), &zCsr, zEnd,
19748 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 &nByte);.
19749 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 allocSpace((char
1974a 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e 56 61 72 *)&p->aVar, nVar
1974b 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a *sizeof(Mem), &z
1974c 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 Csr, zEnd, &nByt
1974d 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 e);. allocS
1974e 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e pace((char*)&p->
1974f 61 70 41 72 67 2c 20 6e 41 72 67 2a 73 69 7a 65 apArg, nArg*size
19750 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43 73 72 2c of(Mem*), &zCsr,
19751 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a zEnd, &nByte);.
19752 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 allocSpace
19753 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 7a 56 61 ((char*)&p->azVa
19754 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 63 r, nVar*sizeof(c
19755 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 har*), &zCsr, zE
19756 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 nd, &nByte);.
19757 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 allocSpace((c
19758 68 61 72 2a 29 26 70 2d 3e 61 70 43 73 72 2c 20 har*)&p->apCsr,
19759 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1975a 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a 65 6f 66 nCursor*sizeof
1975b 28 56 64 62 65 43 75 72 73 6f 72 2a 29 2c 20 26 (VdbeCursor*), &
1975c 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 zCsr, zEnd, &nBy
1975d 74 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 te. );.
1975e 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a 20 if( nByte ){.
1975f 20 20 20 20 20 20 20 70 2d 3e 70 46 72 65 65 20 p->pFree
19760 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
19761 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 29 3b cRaw(db, nByte);
19762 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a . }. z
19763 43 73 72 20 3d 20 70 2d 3e 70 46 72 65 65 3b 0a Csr = p->pFree;.
19764 20 20 20 20 20 20 7a 45 6e 64 20 3d 20 26 7a 43 zEnd = &zC
19765 73 72 5b 6e 42 79 74 65 5d 3b 0a 20 20 20 20 7d sr[nByte];. }
19766 77 68 69 6c 65 28 20 6e 42 79 74 65 20 26 26 20 while( nByte &&
19767 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 !db->mallocFaile
19768 64 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e 43 75 d );.. p->nCu
19769 72 73 6f 72 20 3d 20 6e 43 75 72 73 6f 72 3b 0a rsor = nCursor;.
1976a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20 if( p->aVar
1976b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 ){. p->nVar
1976c 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20 66 = nVar;. f
1976d 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 or(n=0; n<nVar;
1976e 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d n++){. p-
1976f 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d >aVar[n].flags =
19770 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 MEM_Null;.
19771 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 p->aVar[n].db
19772 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 = db;. }.
19773 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e }. if( p->
19774 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 2d aMem ){. p-
19775 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 >aMem--;
19776 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19777 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72 6f aMem[] goes fro
19778 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20 20 m 1..nMem */.
19779 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d 65 p->nMem = nMe
1977a 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
1977b 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 /* not
1977c 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a from 0..nMem-1 *
1977d 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b /. for(n=1;
1977e 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a n<=nMem; n++){.
1977f 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b p->aMem[
19780 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e n].flags = MEM_N
19781 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e ull;. p->
19782 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b aMem[n].db = db;
19783 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
19784 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
19785 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31 _DEBUG. for(n=1
19786 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b ; n<p->nMem; n++
19787 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
19788 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 ->aMem[n].db==db
19789 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a );. }.#endif..
1978a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 p->pc = -1;.
1978b 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f p->rc = SQLITE_O
1978c 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 K;. p->errorAct
1978d 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a ion = OE_Abort;.
1978e 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d 20 p->explain |=
1978f 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d 3e isExplain;. p->
19790 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 magic = VDBE_MAG
19791 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 68 IC_RUN;. p->nCh
19792 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 ange = 0;. p->c
19793 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 70 acheCtr = 1;. p
19794 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f ->minWriteFileFo
19795 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 2d rmat = 255;. p-
19796 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b >iStatement = 0;
19797 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
19798 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e 74 FILE. {. int
19799 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b i;. for(i=0;
1979a 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
1979b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d . p->aOp[i]
1979c 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 .cnt = 0;.
1979d 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 p->aOp[i].cycles
1979e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
1979f 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
197a0 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63 75 72 Close a VDBE cur
197a1 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73 65 20 sor and release
197a2 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72 63 65 all the resource
197a3 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 0a 2a s that cursor .*
197a4 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68 6f 6c * happens to hol
197a5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
197a6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
197a7 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 3VdbeFreeCursor(
197a8 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43 75 72 Vdbe *p, VdbeCur
197a9 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 66 28 sor *pCx){. if(
197aa 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 pCx==0 ){. r
197ab 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 eturn;. }. if(
197ac 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 20 pCx->pBt ){.
197ad 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f sqlite3BtreeClo
197ae 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 se(pCx->pBt);.
197af 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 43 /* The pCx->pC
197b0 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 6c ursor will be cl
197b1 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ose automaticall
197b2 79 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2c y, if it exists,
197b3 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 by. ** the c
197b4 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 all above. */.
197b5 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e 70 }else if( pCx->p
197b6 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 Cursor ){. sq
197b7 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 lite3BtreeCloseC
197b8 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 73 ursor(pCx->pCurs
197b9 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 or);. }.#ifndef
197ba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
197bb 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 TUALTABLE. if(
197bc 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 pCx->pVtabCursor
197bd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
197be 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 vtab_cursor *pVt
197bf 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d 3e abCursor = pCx->
197c0 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 pVtabCursor;.
197c1 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
197c2 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d odule *pModule =
197c3 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 pCx->pModule;.
197c4 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 p->inVtabMeth
197c5 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f 69 od = 1;. (voi
197c6 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
197c7 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 70 ff(p->db);. p
197c8 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 Module->xClose(p
197c9 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 VtabCursor);.
197ca 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
197cb 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a 20 fetyOn(p->db);.
197cc 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 p->inVtabMeth
197cd 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 od = 0;. }.#end
197ce 69 66 0a 20 20 69 66 28 20 21 70 43 78 2d 3e 65 if. if( !pCx->e
197cf 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 phemPseudoTable
197d0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 ){. sqlite3Db
197d1 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 78 2d Free(p->db, pCx-
197d2 3e 70 44 61 74 61 29 3b 0a 20 20 7d 0a 7d 0a 0a >pData);. }.}..
197d3 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 /*.** Close all
197d4 63 75 72 73 6f 72 73 20 65 78 63 65 70 74 20 66 cursors except f
197d5 6f 72 20 56 54 61 62 20 63 75 72 73 6f 72 73 20 or VTab cursors
197d6 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 that are current
197d7 6c 79 0a 2a 2a 20 69 6e 20 75 73 65 2e 0a 2a 2f ly.** in use..*/
197d8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f .static void clo
197d9 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 seAllCursorsExce
197da 70 74 41 63 74 69 76 65 56 74 61 62 73 28 56 64 ptActiveVtabs(Vd
197db 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b be *p){. int i;
197dc 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 3d . if( p->apCsr=
197dd 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 =0 ) return;. f
197de 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 or(i=0; i<p->nCu
197df 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 rsor; i++){.
197e0 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d VdbeCursor *pC =
197e1 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 p->apCsr[i];.
197e2 20 20 69 66 28 20 70 43 20 26 26 20 28 21 70 2d if( pC && (!p-
197e3 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 7c 7c >inVtabMethod ||
197e4 20 21 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f !pC->pVtabCurso
197e5 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 r) ){. sqli
197e6 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f te3VdbeFreeCurso
197e7 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 20 20 20 r(p, pC);.
197e8 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b p->apCsr[i] = 0;
197e9 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
197ea 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 68 65 .** Clean up the
197eb 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 75 74 VM after execut
197ec 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion..**.** This
197ed 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 75 74 routine will aut
197ee 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 omatically close
197ef 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 6c 69 any cursors, li
197f0 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 sts, and/or.** s
197f1 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 72 65 orters that were
197f2 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 74 20 left open. It
197f3 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 68 65 also deletes the
197f4 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 61 values of.** va
197f5 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 20 61 riables in the a
197f6 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a Var[] array..*/.
197f7 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c 65 61 static void Clea
197f8 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 nup(Vdbe *p){.
197f9 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 int i;. sqlite3
197fa 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 *db = p->db;.
197fb 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 63 6c 6f Mem *pMem;. clo
197fc 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 seAllCursorsExce
197fd 70 74 41 63 74 69 76 65 56 74 61 62 73 28 70 29 ptActiveVtabs(p)
197fe 3b 0a 20 20 66 6f 72 28 70 4d 65 6d 3d 26 70 2d ;. for(pMem=&p-
197ff 3e 61 4d 65 6d 5b 31 5d 2c 20 69 3d 31 3b 20 69 >aMem[1], i=1; i
19800 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b 2c 20 <=p->nMem; i++,
19801 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 pMem++){. if(
19802 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
19803 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 EM_RowSet ){.
19804 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 sqlite3RowSet
19805 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 Clear(pMem->u.pR
19806 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 owSet);. }.
19807 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
19808 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 (pMem, MEM_Null)
19809 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d ;. }. releaseM
1980a 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d emArray(&p->aMem
1980b 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 [1], p->nMem);.
1980c 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 if( p->contextS
1980d 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 tack ){. sqli
1980e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1980f 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 29 3b 0a >contextStack);.
19810 20 20 7d 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74 }. p->context
19811 53 74 61 63 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e Stack = 0;. p->
19812 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 contextStackDept
19813 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74 h = 0;. p->cont
19814 65 78 74 53 74 61 63 6b 54 6f 70 20 3d 20 30 3b extStackTop = 0;
19815 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
19816 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
19817 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d ;. p->zErrMsg =
19818 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 0;. p->pResult
19819 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Set = 0;.}../*.*
1981a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 * Set the number
1981b 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d of result colum
1981c 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ns that will be
1981d 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 returned by this
1981e 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e SQL.** statemen
1981f 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20 73 t. This is now s
19820 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 et at compile ti
19821 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e 20 me, rather than
19822 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75 74 during.** execut
19823 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65 20 ion of the vdbe
19824 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74 20 program so that
19825 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 sqlite3_column_c
19826 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62 65 ount() can.** be
19827 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53 51 called on an SQ
19828 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 66 6f L statement befo
19829 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 re sqlite3_step(
1982a 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
1982b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1982c 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 3VdbeSetNumCols(
1982d 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52 65 Vdbe *p, int nRe
1982e 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d 20 sColumn){. Mem
1982f 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e 74 *pColName;. int
19830 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 n;. sqlite3 *d
19831 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72 65 b = p->db;.. re
19832 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d leaseMemArray(p-
19833 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 >aColName, p->nR
19834 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 esColumn*COLNAME
19835 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 _N);. sqlite3Db
19836 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f 6c Free(db, p->aCol
19837 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52 65 Name);. n = nRe
19838 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f sColumn*COLNAME_
19839 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 N;. p->nResColu
1983a 6d 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b mn = nResColumn;
1983b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 20 3d . p->aColName =
1983c 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 4d 65 6d pColName = (Mem
1983d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *)sqlite3DbMallo
1983e 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
1983f 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20 69 66 28 (Mem)*n );. if(
19840 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20 p->aColName==0
19841 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c ) return;. whil
19842 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 e( n-- > 0 ){.
19843 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 pColName->flag
19844 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
19845 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64 62 20 3d pColName->db =
19846 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 43 6f 6c p->db;. pCol
19847 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f Name++;. }.}../
19848 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 61 6d *.** Set the nam
19849 65 20 6f 66 20 74 68 65 20 69 64 78 27 74 68 20 e of the idx'th
1984a 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 72 65 74 column to be ret
1984b 75 72 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c urned by the SQL
1984c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 7a statement..** z
1984d 4e 61 6d 65 20 6d 75 73 74 20 62 65 20 61 20 70 Name must be a p
1984e 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 75 6c 20 ointer to a nul
1984f 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
19850 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 g..**.** This ca
19851 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61 64 65 20 ll must be made
19852 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 after a call to
19853 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 sqlite3VdbeSetNu
19854 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 mCols()..**.** T
19855 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 74 he final paramet
19856 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 74 20 62 er, xDel, must b
19857 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f e one of SQLITE_
19858 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 54 45 5f DYNAMIC, SQLITE_
19859 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20 53 51 4c STATIC.** or SQL
1985a 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 20 49 ITE_TRANSIENT. I
1985b 66 20 69 74 20 69 73 20 53 51 4c 49 54 45 5f 44 f it is SQLITE_D
1985c 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 74 68 65 YNAMIC, then the
1985d 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a buffer pointed.
1985e 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d 65 20 77 ** to by zName w
1985f 69 6c 6c 20 62 65 20 66 72 65 65 64 20 62 79 20 ill be freed by
19860 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 sqlite3DbFree()
19861 77 68 65 6e 20 74 68 65 20 76 64 62 65 20 69 73 when the vdbe is
19862 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 53 destroyed..*/.S
19863 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19864 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 t sqlite3VdbeSet
19865 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64 62 65 20 ColName(. Vdbe
19866 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
19867 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
19868 64 62 65 20 62 65 69 6e 67 20 63 6f 6e 66 69 67 dbe being config
19869 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 ured */. int id
1986a 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x,
1986b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1986c 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 7a 4e dex of column zN
1986d 61 6d 65 20 61 70 70 6c 69 65 73 20 74 6f 20 2a ame applies to *
1986e 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20 20 20 20 /. int var,
1986f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19870 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 /* One of t
19871 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20 63 6f 6e he COLNAME_* con
19872 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e 73 stants */. cons
19873 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 t char *zName,
19874 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19875 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 Pointer to buffe
19876 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 61 6d r containing nam
19877 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 e */. void (*xD
19878 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20 20 el)(void*)
19879 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
1987a 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 y management str
1987b 61 74 65 67 79 20 66 6f 72 20 7a 4e 61 6d 65 20 ategy for zName
1987c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
1987d 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b Mem *pColName;
1987e 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 . assert( idx<p
1987f 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a ->nResColumn );.
19880 20 20 61 73 73 65 72 74 28 20 76 61 72 3c 43 4f assert( var<CO
19881 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 69 66 28 LNAME_N );. if(
19882 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
19883 69 6c 65 64 20 29 7b 0a 20 20 20 20 61 73 73 65 iled ){. asse
19884 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 78 44 rt( !zName || xD
19885 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d el!=SQLITE_DYNAM
19886 49 43 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e IC );. return
19887 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
19888 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e }. assert( p->
19889 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 aColName!=0 );.
1988a 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 28 70 2d pColName = &(p-
1988b 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 2b 76 61 >aColName[idx+va
1988c 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 5d r*p->nResColumn]
1988d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1988e 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
1988f 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 ColName, zName,
19890 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c -1, SQLITE_UTF8,
19891 20 78 44 65 6c 29 3b 0a 20 20 61 73 73 65 72 74 xDel);. assert
19892 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a 4e 61 6d ( rc!=0 || !zNam
19893 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d 65 2d 3e e || (pColName->
19894 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d 29 21 flags&MEM_Term)!
19895 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 =0 );. return r
19896 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65 c;.}../*.** A re
19897 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e ad or write tran
19898 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f 72 20 6d saction may or m
19899 61 79 20 6e 6f 74 20 62 65 20 61 63 74 69 76 65 ay not be active
1989a 20 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e on database han
1989b 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66 20 61 20 dle.** db. If a
1989c 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
1989d 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74 20 69 74 ctive, commit it
1989e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 0a . If there is a.
1989f 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 ** write-transac
198a0 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67 20 6d 6f tion spanning mo
198a1 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 re than one data
198a2 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 base file, this
198a3 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61 6b 65 73 routine.** takes
198a4 20 63 61 72 65 20 6f 66 20 74 68 65 20 6d 61 73 care of the mas
198a5 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74 72 69 63 ter journal tric
198a6 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 kery..*/.static
198a7 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 74 28 73 int vdbeCommit(s
198a8 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 62 65 qlite3 *db, Vdbe
198a9 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *p){. int i;.
198aa 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d 20 30 3b int nTrans = 0;
198ab 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 /* Number of d
198ac 61 74 61 62 61 73 65 73 20 77 69 74 68 20 61 6e atabases with an
198ad 20 61 63 74 69 76 65 20 77 72 69 74 65 2d 74 72 active write-tr
198ae 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 ansaction */. i
198af 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
198b0 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64 58 63 6f K;. int needXco
198b1 6d 6d 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 mmit = 0;.. /*
198b2 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 Before doing any
198b3 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c 6c thing else, call
198b4 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61 6c the xSync() cal
198b5 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 20 lback for any.
198b6 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c ** virtual modul
198b7 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65 6e e tables written
198b8 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 in this transac
198b9 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 74 tion. This has t
198ba 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 62 o. ** be done b
198bb 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 6e efore determinin
198bc 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73 74 g whether a mast
198bd 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
198be 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 is . ** require
198bf 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28 29 d, as an xSync()
198c0 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 64 callback may ad
198c1 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 d an attached da
198c2 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 74 tabase. ** to t
198c3 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a he transaction..
198c4 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
198c5 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c 20 te3VtabSync(db,
198c6 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 &p->zErrMsg);.
198c7 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
198c8 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
198c9 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 rc;. }.. /* Th
198ca 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 6e is loop determin
198cb 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63 6f es (a) if the co
198cc 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c 64 mmit hook should
198cd 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 0a be invoked and.
198ce 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 6e ** (b) how man
198cf 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 y database files
198d0 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74 65 have open write
198d1 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 6e transactions, n
198d2 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 69 ot . ** includi
198d3 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 ng the temp data
198d4 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d 70 base. (b) is imp
198d5 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 69 ortant because i
198d6 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 2a f more than . *
198d7 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 * one database f
198d8 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 ile has an open
198d9 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
198da 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 n, a master jour
198db 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73 nal. ** file is
198dc 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e required for an
198dd 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e 0a atomic commit..
198de 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 3b */ . for(i=0;
198df 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
198e0 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 { . Btree *pB
198e1 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 t = db->aDb[i].p
198e2 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 Bt;. if( sqli
198e3 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e te3BtreeIsInTran
198e4 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 s(pBt) ){.
198e5 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 3b needXcommit = 1;
198e6 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31 20 . if( i!=1
198e7 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 20 ) nTrans++;.
198e8 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
198e9 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 69 here are any wri
198ea 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 te-transactions
198eb 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 at all, invoke t
198ec 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a he commit hook *
198ed 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d /. if( needXcom
198ee 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d mit && db->xComm
198ef 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 itCallback ){.
198f0 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e 66 assert( (db->f
198f1 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f lags & SQLITE_Co
198f2 6d 6d 69 74 42 75 73 79 29 3d 3d 30 20 29 3b 0a mmitBusy)==0 );.
198f3 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d db->flags |=
198f4 20 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74 42 75 SQLITE_CommitBu
198f5 73 79 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 sy;. (void)sq
198f6 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
198f7 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 2d b);. rc = db-
198f8 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b >xCommitCallback
198f9 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 (db->pCommitArg)
198fa 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 ;. (void)sqli
198fb 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b te3SafetyOn(db);
198fc 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 . db->flags &
198fd 3d 20 7e 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74 = ~SQLITE_Commit
198fe 42 75 73 79 3b 0a 20 20 20 20 69 66 28 20 72 63 Busy;. if( rc
198ff 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19900 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
19901 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 NT;. }. }..
19902 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63 /* The simple c
19903 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68 ase - no more th
19904 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 an one database
19905 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69 file (not counti
19906 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50 ng the. ** TEMP
19907 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61 database) has a
19908 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 transaction act
19909 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20 ive. There is
1990a 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a no need for the.
1990b 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 ** master-jour
1990c 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 nal.. **. ** I
1990d 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c f the return val
1990e 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72 ue of sqlite3Btr
1990f 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20 eeGetFilename()
19910 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68 is a zero length
19911 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74 . ** string, it
19912 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 means the main
19913 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d database is :mem
19914 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66 ory: or a temp f
19915 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74 ile. In . ** t
19916 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e hat case we do n
19917 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69 ot support atomi
19918 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d c multi-file com
19919 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65 mits, so use the
1991a 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61 . ** simple ca
1991b 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a se then too.. *
1991c 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 /. if( 0==sqlit
1991d 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 74 e3Strlen30(sqlit
1991e 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
1991f 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 me(db->aDb[0].pB
19920 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61 6e 73 t)). || nTrans
19921 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72 <=1. ){. for
19922 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 (i=0; rc==SQLITE
19923 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 _OK && i<db->nDb
19924 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 ; i++){. Bt
19925 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
19926 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
19927 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
19928 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
19929 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1992a 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 20 20 One(pBt, 0);.
1992b 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
1992c 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d 69 74 /* Do the commit
1992d 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 61 74 only if all dat
1992e 61 62 61 73 65 73 20 73 75 63 63 65 73 73 66 75 abases successfu
1992f 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 68 61 lly complete pha
19930 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 49 66 se 1. . ** If
19931 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 72 65 one of the Btre
19932 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 eCommitPhaseOne(
19933 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c 20 74 ) calls fails, t
19934 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 61 6e his indicates an
19935 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 6f 72 . ** IO error
19936 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e 67 20 while deleting
19937 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 61 20 or truncating a
19938 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 journal file. It
19939 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a 20 20 is unlikely,.
1993a 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 20 68 ** but could h
1993b 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 20 63 appen. In this c
1993c 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 ase abandon proc
1993d 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 essing and retur
1993e 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20 20 n the error..
1993f 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b */. for(i=0;
19940 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
19941 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b & i<db->nDb; i++
19942 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a ){. Btree *
19943 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d pBt = db->aDb[i]
19944 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 .pBt;. if(
19945 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 pBt ){. r
19946 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
19947 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
19948 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Bt);. }.
19949 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
1994a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1994b 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d sqlite3VtabCom
1994c 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 mit(db);. }.
1994d 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 6f 6d }.. /* The com
1994e 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 65 72 plex case - Ther
1994f 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 69 6c e is a multi-fil
19950 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 e write-transact
19951 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 2a 2a ion active.. **
19952 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20 61 This requires a
19953 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
19954 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 20 74 file to ensure t
19955 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
19956 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 s. ** committed
19957 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a 2f 0a atomicly.. */.
19958 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
19959 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 6c 73 MIT_DISKIO. els
1995a 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 e{. sqlite3_v
1995b 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 fs *pVfs = db->p
1995c 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e 65 65 Vfs;. int nee
1995d 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 63 dSync = 0;. c
1995e 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 har *zMaster = 0
1995f 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 6d 65 ; /* File-name
19960 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 72 20 for the master
19961 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 63 journal */. c
19962 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 69 6e har const *zMain
19963 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 File = sqlite3Bt
19964 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 reeGetFilename(d
19965 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a b->aDb[0].pBt);.
19966 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 sqlite3_file
19967 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 *pMaster = 0;.
19968 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 i64 offset =
19969 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 0;. int res;.
1996a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 61 . /* Select a
1996b 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1996c 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 file name */.
1996d 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 32 20 do {. u32
1996e 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20 73 iRandom;. s
1996f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
19970 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 zMaster);.
19971 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
19972 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e 64 ess(sizeof(iRand
19973 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b 0a om), &iRandom);.
19974 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 zMaster =
19975 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
19976 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20 b, "%s-mj%08X",
19977 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61 6e 64 zMainFile, iRand
19978 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a om&0x7fffffff);.
19979 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 if( !zMast
1997a 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 er ){. re
1997b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1997c 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 M;. }.
1997d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
1997e 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 ccess(pVfs, zMas
1997f 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 ter, SQLITE_ACCE
19980 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 SS_EXISTS, &res)
19981 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 ;. }while( rc
19982 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 ==SQLITE_OK && r
19983 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 es );. if( rc
19984 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
19985 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 /* Open the
19986 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e master journal.
19987 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 */. rc = s
19988 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c qlite3OsOpenMall
19989 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 oc(pVfs, zMaster
1998a 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20 , &pMaster, .
1998b 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
1998c 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c EN_READWRITE|SQL
1998d 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c ITE_OPEN_CREATE|
1998e 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 . SQLIT
1998f 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
19990 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
19991 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20 TER_JOURNAL, 0.
19992 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 );. }.
19993 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
19994 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
19995 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
19996 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 Master);. r
19997 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
19998 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 . /* Write t
19999 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20 he name of each
1999a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e database file in
1999b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1999c 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 into the new.
1999d 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 ** master jour
1999e 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20 nal file. If an
1999f 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20 error occurs at
199a0 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65 this point close
199a1 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65 . ** and dele
199a2 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
199a3 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20 urnal file. All
199a4 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a the individual j
199a5 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20 ournal files.
199a6 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27 ** still have '
199a7 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73 null' as the mas
199a8 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e ter journal poin
199a9 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c ter, so they wil
199aa 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61 l roll. ** ba
199ab 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 ck independently
199ac 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 if a failure oc
199ad 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 curs.. */.
199ae 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
199af 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nDb; i++){.
199b0 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
199b1 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
199b2 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 if( i==1 ) c
199b3 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67 ontinue; /* Ig
199b4 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 nore the TEMP da
199b5 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 tabase */.
199b6 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 if( sqlite3Btree
199b7 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 IsInTrans(pBt) )
199b8 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63 {. char c
199b9 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71 onst *zFile = sq
199ba 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 lite3BtreeGetJou
199bb 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20 rnalname(pBt);.
199bc 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 if( zFile
199bd 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 [0]==0 ) continu
199be 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d e; /* Ignore :m
199bf 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73 emory: databases
199c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
199c1 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 !needSync && !sq
199c2 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 lite3BtreeSyncDi
199c3 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 sabled(pBt) ){.
199c4 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e needSyn
199c5 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d c = 1;. }
199c6 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
199c7 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 lite3OsWrite(pMa
199c8 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 6c ster, zFile, sql
199c9 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 ite3Strlen30(zFi
199ca 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b 0a le)+1, offset);.
199cb 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b offset +
199cc 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
199cd 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20 0(zFile)+1;.
199ce 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
199cf 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
199d0 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
199d1 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a eFree(pMaster);.
199d2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
199d3 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 3OsDelete(pVfs,
199d4 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 zMaster, 0);.
199d5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
199d6 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
199d7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
199d8 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
199d9 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
199da 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 . /* Sync the
199db 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
199dc 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49 4f 43 file. If the IOC
199dd 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 65 AP_SEQUENTIAL de
199de 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67 vice. ** flag
199df 20 69 73 20 73 65 74 20 74 68 69 73 20 69 73 20 is set this is
199e0 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20 not required..
199e1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 65 */. if( nee
199e2 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20 30 dSync . && 0
199e3 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 ==(sqlite3OsDevi
199e4 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
199e5 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54 s(pMaster)&SQLIT
199e6 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
199e7 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 AL). && SQLI
199e8 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
199e9 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 74 ite3OsSync(pMast
199ea 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f er, SQLITE_SYNC_
199eb 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b 0a NORMAL)). ){.
199ec 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
199ed 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 loseFree(pMaster
199ee 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
199ef 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a OsDelete(pVfs, z
199f0 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 Master, 0);.
199f1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
199f2 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
199f3 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
199f4 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e }.. /* Syn
199f5 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c c all the db fil
199f6 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 es involved in t
199f7 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
199f8 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20 The same call.
199f9 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61 ** sets the ma
199fa 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 ster journal poi
199fb 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64 nter in each ind
199fc 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e ividual journal.
199fd 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 If. ** an er
199fe 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c ror occurs here,
199ff 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 do not delete t
19a00 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
19a01 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 l file.. **.
19a02 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72 ** If the err
19a03 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
19a04 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 the first call
19a05 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 to. ** sqlite
19a06 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
19a07 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65 eOne(), then the
19a08 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 re is a chance t
19a09 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d hat the. ** m
19a0a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
19a0b 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61 le will be orpha
19a0c 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e ned. But we cann
19a0d 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20 ot delete it,.
19a0e 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 ** in case the
19a0f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
19a10 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72 file name was wr
19a11 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
19a12 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 ournal. ** fi
19a13 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 61 le before the fa
19a14 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 2e 0a ilure occurred..
19a15 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 */. for(i
19a16 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f =0; rc==SQLITE_O
19a17 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 K && i<db->nDb;
19a18 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 i++){ . Btr
19a19 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 ee *pBt = db->aD
19a1a 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 b[i].pBt;.
19a1b 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 if( pBt ){.
19a1c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
19a1d 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
19a1e 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 29 ne(pBt, zMaster)
19a1f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
19a20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
19a21 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b seFree(pMaster);
19a22 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
19a23 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
19a24 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
19a25 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
19a26 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
19a27 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 }.. /* Delet
19a28 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
19a29 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 rnal file. This
19a2a 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 6e commits the tran
19a2b 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20 saction. After.
19a2c 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73 ** doing this
19a2d 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 69 the directory i
19a2e 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 62 s synced again b
19a2f 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 69 efore any indivi
19a30 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e dual. ** tran
19a31 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 72 saction files ar
19a32 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a e deleted.. *
19a33 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
19a34 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c e3OsDelete(pVfs,
19a35 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20 20 zMaster, 1);.
19a36 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
19a37 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 db, zMaster);.
19a38 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 zMaster = 0;.
19a39 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
19a3a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
19a3b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 }.. /* All
19a3c 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63 74 files and direct
19a3d 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65 61 ories have alrea
19a3e 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c 20 dy been synced,
19a3f 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 so the following
19a40 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f . ** calls to
19a41 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
19a42 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 72 mitPhaseTwo() ar
19a43 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 66 e only closing f
19a44 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 iles and. **
19a45 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e deleting or trun
19a46 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 2e cating journals.
19a47 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f If something go
19a48 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a 20 es wrong while.
19a49 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 ** this is ha
19a4a 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27 74 ppening we don't
19a4b 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54 68 really care. Th
19a4c 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74 e integrity of t
19a4d 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 he. ** transa
19a4e 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 ction is already
19a4f 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75 74 guaranteed, but
19a50 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f 6c some stray 'col
19a51 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20 d' journals.
19a52 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67 20 ** may be lying
19a53 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e around. Returnin
19a54 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 g an error code
19a55 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74 65 won't help matte
19a56 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 rs.. */. d
19a57 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 isable_simulated
19a58 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 _io_errors();.
19a59 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
19a5a 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
19a5b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
19a5c 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 >nDb; i++){ .
19a5d 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
19a5e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
19a5f 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b if( pBt ){
19a60 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
19a61 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
19a62 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 Two(pBt);.
19a63 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
19a64 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
19a65 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c 65 oc();. enable
19a66 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
19a67 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 6c rors();.. sql
19a68 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 ite3VtabCommit(d
19a69 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a b);. }.#endif..
19a6a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
19a6b 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 /* .** This rout
19a6c 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 20 ine checks that
19a6d 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 69 the sqlite3.acti
19a6e 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 20 veVdbeCnt count
19a6f 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 63 variable.** matc
19a70 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hes the number o
19a71 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 20 f vdbe's in the
19a72 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 64 list sqlite3.pVd
19a73 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63 be that are.** c
19a74 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 2e urrently active.
19a75 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 An assertion fa
19a76 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 63 ils if the two c
19a77 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74 ounts do not mat
19a78 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 ch..** This is a
19a79 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 2d n internal self-
19a7a 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 20 check only - it
19a7b 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e 74 is not an essent
19a7c 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a ial processing.*
19a7d 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 * step..**.** Th
19a7e 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 is is a no-op if
19a7f 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e NDEBUG is defin
19a80 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e ed..*/.#ifndef N
19a81 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69 DEBUG.static voi
19a82 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 d checkActiveVdb
19a83 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 eCnt(sqlite3 *db
19a84 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 ){. Vdbe *p;.
19a85 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 int cnt = 0;. i
19a86 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a 20 nt nWrite = 0;.
19a87 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a p = db->pVdbe;.
19a88 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 while( p ){.
19a89 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d if( p->magic==
19a8a 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 VDBE_MAGIC_RUN &
19a8b 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 & p->pc>=0 ){.
19a8c 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 cnt++;.
19a8d 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 if( p->readOnly
19a8e 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b 0a ==0 ) nWrite++;.
19a8f 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
19a90 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 73 >pNext;. }. as
19a91 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e 61 sert( cnt==db->a
19a92 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b 0a ctiveVdbeCnt );.
19a93 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 65 assert( nWrite
19a94 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 ==db->writeVdbeC
19a95 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 nt );.}.#else.#d
19a96 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76 efine checkActiv
19a97 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e 64 eVdbeCnt(x).#end
19a98 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 76 if../*.** For ev
19a99 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20 69 ery Btree that i
19a9a 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 n database conne
19a9b 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20 0a ction db which .
19a9c 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 ** has been modi
19a9d 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72 20 fied, "trip" or
19a9e 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68 20 invalidate each
19a9f 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68 61 cursor in.** tha
19aa0 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68 61 t Btree might ha
19aa1 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ve been modified
19aa2 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75 72 so that the cur
19aa3 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65 72 sor.** can never
19aa4 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e 20 be used again.
19aa5 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 This happens wh
19aa6 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a en a rollback.**
19aa7 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68 61 * occurs. We ha
19aa8 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20 74 ve to trip all t
19aa9 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 he other cursors
19aaa 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 , even.** cursor
19aab 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73 20 from other VMs
19aac 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 in different dat
19aad 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
19aae 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f s,.** so that no
19aaf 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20 74 ne of them try t
19ab0 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20 61 o use the data a
19ab1 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20 t which they.**
19ab2 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 6e were pointing an
19ab3 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79 20 d which now may
19ab4 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67 65 have been change
19ab5 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65 20 d due.** to the
19ab6 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 rollback..**.**
19ab7 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61 20 Remember that a
19ab8 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65 6c rollback can del
19ab9 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70 6c ete tables compl
19aba 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72 64 ete and.** reord
19abb 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20 53 er rootpages. S
19abc 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 o it is not suff
19abd 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20 73 icient just to s
19abe 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 ave.** the state
19abf 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 of the cursor.
19ac0 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 61 We have to inva
19ac1 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f lidate the curso
19ac2 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 r.** so that it
19ac3 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 67 is never used ag
19ac4 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ain..*/.static v
19ac5 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43 75 oid invalidateCu
19ac6 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 rsorsOnModifiedB
19ac7 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a 64 trees(sqlite3 *d
19ac8 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 b){. int i;. f
19ac9 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
19aca 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 b; i++){. Btr
19acb 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b ee *p = db->aDb[
19acc 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 i].pBt;. if(
19acd 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 p && sqlite3Btre
19ace 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29 7b eIsInTrans(p) ){
19acf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
19ad0 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 reeTripAllCursor
19ad1 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52 s(p, SQLITE_ABOR
19ad2 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a T);. }. }.}.
19ad3 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56 64 ./*.** If the Vd
19ad4 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 be passed as the
19ad5 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
19ad6 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65 opened a stateme
19ad7 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a nt-transaction,.
19ad8 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f 77 2e ** close it now.
19ad9 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20 6d 75 Argument eOp mu
19ada 73 74 20 62 65 20 65 69 74 68 65 72 20 53 41 56 st be either SAV
19adb 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
19adc 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f or.** SAVEPOINT_
19add 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74 20 69 RELEASE. If it i
19ade 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c s SAVEPOINT_ROLL
19adf 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65 20 73 BACK, then the s
19ae0 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e tatement.** tran
19ae1 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 saction is rolle
19ae2 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 20 69 d back. If eOp i
19ae3 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 s SAVEPOINT_RELE
19ae4 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a ASE, then the .*
19ae5 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e * statement tran
19ae6 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 74 saction is commt
19ae7 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ted..**.** If an
19ae8 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
19ae9 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 , an SQLITE_IOER
19aea 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 R_XXX error code
19aeb 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a is returned. .*
19aec 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 * Otherwise SQLI
19aed 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TE_OK..*/.SQLITE
19aee 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
19aef 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 ite3VdbeCloseSta
19af0 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 tement(Vdbe *p,
19af1 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71 6c 69 int eOp){. sqli
19af2 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20 3d 20 te3 *const db =
19af3 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 20 p->db;. int rc
19af4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
19af5 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 f( p->iStatement
19af6 20 26 26 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 && db->nStateme
19af7 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b nt ){. int i;
19af8 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 . const int i
19af9 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d 3e 69 Savepoint = p->i
19afa 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a 20 20 Statement-1;..
19afb 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d 3d 53 assert( eOp==S
19afc 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
19afd 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45 50 4f K || eOp==SAVEPO
19afe 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20 20 INT_RELEASE);.
19aff 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 assert( db->nS
19b00 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 tatement>0 );.
19b01 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 53 74 assert( p->iSt
19b02 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e 6e 53 atement==(db->nS
19b03 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e 53 61 tatement+db->nSa
19b04 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20 20 20 vepoint) );..
19b05 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
19b06 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 nDb; i++){ .
19b07 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 int rc2 = SQLI
19b08 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42 74 72 TE_OK;. Btr
19b09 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 ee *pBt = db->aD
19b0a 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 b[i].pBt;.
19b0b 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 if( pBt ){.
19b0c 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 56 45 if( eOp==SAVE
19b0d 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
19b0e 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 32 20 {. rc2
19b0f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 = sqlite3BtreeSa
19b10 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 vepoint(pBt, SAV
19b11 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c EPOINT_ROLLBACK,
19b12 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 iSavepoint);.
19b13 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
19b14 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f if( rc2==SQLITE_
19b15 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
19b16 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc2 = sqlite3Btr
19b17 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2c eeSavepoint(pBt,
19b18 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 SAVEPOINT_RELEA
19b19 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b SE, iSavepoint);
19b1a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
19b1b 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
19b1c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
19b1d 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 rc = rc2;.
19b1e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
19b1f 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53 74 61 }. db->nSta
19b20 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20 70 2d tement--;. p-
19b21 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b >iStatement = 0;
19b22 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
19b23 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 ;.}../*.** If SQ
19b24 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
19b25 20 74 6f 20 73 75 70 70 6f 72 74 20 73 68 61 72 to support shar
19b26 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 61 6e ed-cache mode an
19b27 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 d to be threadsa
19b28 66 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 fe,.** this rout
19b29 69 6e 65 20 6f 62 74 61 69 6e 73 20 74 68 65 20 ine obtains the
19b2a 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 mutex associated
19b2b 20 77 69 74 68 20 65 61 63 68 20 42 74 53 68 61 with each BtSha
19b2c 72 65 64 20 73 74 72 75 63 74 75 72 65 0a 2a 2a red structure.**
19b2d 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 63 63 that may be acc
19b2e 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 20 essed by the VM
19b2f 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 passed as an arg
19b30 75 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e 67 20 ument. In doing
19b31 73 6f 20 69 74 0a 2a 2a 20 73 65 74 73 20 74 68 so it.** sets th
19b32 65 20 42 74 53 68 61 72 65 64 2e 64 62 20 6d 65 e BtShared.db me
19b33 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f 66 20 mber of each of
19b34 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72 the BtShared str
19b35 75 63 74 75 72 65 73 2c 20 65 6e 73 75 72 69 6e uctures, ensurin
19b36 67 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 6f g.** that the co
19b37 72 72 65 63 74 20 62 75 73 79 2d 68 61 6e 64 6c rrect busy-handl
19b38 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 er callback is i
19b39 6e 76 6f 6b 65 64 20 69 66 20 72 65 71 75 69 72 nvoked if requir
19b3a 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c ed..**.** If SQL
19b3b 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 ite is not threa
19b3c 64 73 61 66 65 20 62 75 74 20 64 6f 65 73 20 73 dsafe but does s
19b3d 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 upport shared-ca
19b3e 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 0a 2a che mode, then.*
19b3f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e * sqlite3BtreeEn
19b40 74 65 72 41 6c 6c 28 29 20 69 73 20 69 6e 76 6f terAll() is invo
19b41 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 42 ked to set the B
19b42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72 69 61 tShared.db varia
19b43 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f bles.** of all o
19b44 66 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 f BtShared struc
19b45 74 75 72 65 73 20 61 63 63 65 73 73 69 62 6c 65 tures accessible
19b46 20 76 69 61 20 74 68 65 20 64 61 74 61 62 61 73 via the databas
19b47 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61 73 73 e handle .** ass
19b48 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
19b49 20 56 4d 2e 20 4f 66 20 63 6f 75 72 73 65 20 6f VM. Of course o
19b4a 6e 6c 79 20 61 20 73 75 62 73 65 74 20 6f 66 20 nly a subset of
19b4b 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 these structures
19b4c 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 61 63 63 65 .** will be acce
19b4d 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 2c 20 ssed by the VM,
19b4e 61 6e 64 20 77 65 20 63 6f 75 6c 64 20 75 73 65 and we could use
19b4f 20 56 64 62 65 2e 62 74 72 65 65 4d 61 73 6b 20 Vdbe.btreeMask
19b50 74 6f 20 66 69 67 75 72 65 0a 2a 2a 20 74 68 61 to figure.** tha
19b51 74 20 73 75 62 73 65 74 20 6f 75 74 2c 20 62 75 t subset out, bu
19b52 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61 64 t there is no ad
19b53 76 61 6e 74 61 67 65 20 74 6f 20 64 6f 69 6e 67 vantage to doing
19b54 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 so..**.** If SQ
19b55 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 65 Lite is not thre
19b56 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65 73 20 adsafe and does
19b57 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68 61 72 not support shar
19b58 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 ed-cache mode, t
19b59 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 his.** function
19b5a 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 23 is a no-op..*/.#
19b5b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
19b5c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
19b5d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19b5e 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
19b5f 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 56 utexArrayEnter(V
19b60 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 53 51 4c dbe *p){.#if SQL
19b61 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 ITE_THREADSAFE.
19b62 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
19b63 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 70 2d exArrayEnter(&p-
19b64 3e 61 4d 75 74 65 78 29 3b 0a 23 65 6c 73 65 0a >aMutex);.#else.
19b65 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
19b66 74 65 72 41 6c 6c 28 70 2d 3e 64 62 29 3b 0a 23 terAll(p->db);.#
19b67 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66 0a 0a endif.}.#endif..
19b68 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
19b69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 ne is called the
19b6a 20 77 68 65 6e 20 61 20 56 44 42 45 20 74 72 69 when a VDBE tri
19b6b 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49 66 20 es to halt. If
19b6c 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61 73 20 the VDBE.** has
19b6d 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61 6e 64 made changes and
19b6e 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 is in autocommi
19b6f 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63 6f 6d t mode, then com
19b70 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63 68 61 mit those.** cha
19b71 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f 6c 6c nges. If a roll
19b72 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64 2c 20 back is needed,
19b73 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f 6c 6c then do the roll
19b74 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 back..**.** This
19b75 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 routine is the
19b76 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 only way to move
19b77 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 61 20 the state of a
19b78 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c 49 54 VM from.** SQLIT
19b79 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f 20 53 E_MAGIC_RUN to S
19b7a 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c 54 QLITE_MAGIC_HALT
19b7b 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c 65 73 . It is harmles
19b7c 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74 68 69 s to.** call thi
19b7d 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74 20 69 s on a VM that i
19b7e 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f s in the SQLITE_
19b7f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61 74 65 MAGIC_HALT state
19b80 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ..**.** Return a
19b81 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 49 n error code. I
19b82 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 6f 75 f the commit cou
19b83 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 20 ld not complete
19b84 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f because of.** lo
19b85 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72 ck contention, r
19b86 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
19b87 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 42 55 Y. If SQLITE_BU
19b88 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 SY is returned,
19b89 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 65 20 it.** means the
19b8a 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20 68 61 close did not ha
19b8b 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73 20 74 ppen and needs t
19b8c 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e 0a 2a o be repeated..*
19b8d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19b8e 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
19b8f 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 Halt(Vdbe *p){.
19b90 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
19b91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19b92 20 2f 2a 20 55 73 65 64 20 74 6f 20 73 74 6f 72 /* Used to stor
19b93 65 20 74 72 61 6e 73 69 65 6e 74 20 72 65 74 75 e transient retu
19b94 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20 73 71 rn codes */. sq
19b95 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
19b96 62 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 b;.. /* This fu
19b97 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 nction contains
19b98 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74 20 64 the logic that d
19b99 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61 20 73 etermines if a s
19b9a 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 2a 2a tatement or. **
19b9b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c transaction wil
19b9c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f l be committed o
19b9d 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 r rolled back as
19b9e 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 a result of the
19b9f 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 . ** execution
19ba0 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 of this virtual
19ba1 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a 0a 20 machine. . **.
19ba2 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 ** If any of th
19ba3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f e following erro
19ba4 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a 0a 20 rs occur:. **.
19ba5 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4e ** SQLITE_N
19ba6 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20 53 51 OMEM. ** SQ
19ba7 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a 2a 20 LITE_IOERR. **
19ba8 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a SQLITE_FULL.
19ba9 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
19baa 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a 0a 20 INTERRUPT. **.
19bab 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69 6e 74 ** Then the int
19bac 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69 67 68 ernal cache migh
19bad 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65 66 74 t have been left
19bae 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 in an inconsist
19baf 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65 2e 20 ent. ** state.
19bb0 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f 6c 6c We need to roll
19bb1 62 61 63 6b 20 74 68 65 20 73 74 61 74 65 6d 65 back the stateme
19bb2 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 nt transaction,
19bb3 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 2a 2a if there is. **
19bb4 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63 6f 6d one, or the com
19bb5 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74 69 6f plete transactio
19bb6 6e 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f n if there is no
19bb7 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
19bb8 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20 20 action.. */..
19bb9 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f if( p->db->mallo
19bba 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 cFailed ){. p
19bbb 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f ->rc = SQLITE_NO
19bbc 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 MEM;. }. close
19bbd 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 70 74 AllCursorsExcept
19bbe 41 63 74 69 76 65 56 74 61 62 73 28 70 29 3b 0a ActiveVtabs(p);.
19bbf 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d if( p->magic!=
19bc0 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 VDBE_MAGIC_RUN )
19bc1 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
19bc2 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63 68 ITE_OK;. }. ch
19bc3 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 eckActiveVdbeCnt
19bc4 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 63 (db);.. /* No c
19bc5 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63 ommit or rollbac
19bc6 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 65 20 k needed if the
19bc7 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 73 74 program never st
19bc8 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 70 arted */. if( p
19bc9 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69 ->pc>=0 ){. i
19bca 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72 69 nt mrc; /* Pri
19bcb 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 65 20 mary error code
19bcc 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a 20 20 from p->rc */.
19bcd 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65 6e 74 int eStatement
19bce 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 Op = 0;. int
19bcf 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b 20 isSpecialError;
19bd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
19bd1 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20 27 t to true if a '
19bd2 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 20 2a special' error *
19bd3 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 61 /.. /* Lock a
19bd4 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64 20 62 ll btrees used b
19bd5 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 y the statement
19bd6 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
19bd7 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 beMutexArrayEnte
19bd8 72 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 r(p);.. /* Ch
19bd9 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 eck for one of t
19bda 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 he special error
19bdb 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 70 s */. mrc = p
19bdc 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20 20 ->rc & 0xff;.
19bdd 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20 isSpecialError
19bde 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f = mrc==SQLITE_NO
19bdf 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 MEM || mrc==SQLI
19be0 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20 TE_IOERR.
19be1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
19be2 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 mrc==SQLITE_INT
19be3 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 ERRUPT || mrc==S
19be4 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 QLITE_FULL;.
19be5 69 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 if( isSpecialErr
19be6 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 or ){. /* I
19be7 66 20 74 68 65 20 71 75 65 72 79 20 77 61 73 20 f the query was
19be8 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65 read-only, we ne
19be9 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 ed do no rollbac
19bea 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77 k at all. Otherw
19beb 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72 ise,. ** pr
19bec 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20 73 oceed with the s
19bed 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e pecial handling.
19bee 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
19bef 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 if( !p->readOnly
19bf0 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f || mrc!=SQLITE_
19bf1 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 INTERRUPT ){.
19bf2 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d if( p->rc==
19bf3 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f SQLITE_IOERR_BLO
19bf4 43 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73 53 CKED && p->usesS
19bf5 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 tmtJournal ){.
19bf6 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 eStateme
19bf7 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 ntOp = SAVEPOINT
19bf8 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 20 _ROLLBACK;.
19bf9 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c p->rc = SQL
19bfa 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
19bfb 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6d 72 63 }else if( (mrc
19bfc 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c ==SQLITE_NOMEM |
19bfd 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 | mrc==SQLITE_FU
19bfe 4c 4c 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 LL).
19bff 20 20 20 20 20 20 20 26 26 20 70 2d 3e 75 73 65 && p->use
19c00 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a sStmtJournal ){.
19c01 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 eState
19c02 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 mentOp = SAVEPOI
19c03 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 NT_ROLLBACK;.
19c04 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
19c05 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 /* We are
19c06 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 forced to roll b
19c07 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 74 ack the active t
19c08 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f ransaction. Befo
19c09 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 re doing.
19c0a 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 ** so, abort
19c0b 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d any other statem
19c0c 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65 ents this handle
19c0d 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 currently has a
19c0e 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 ctive..
19c0f 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e */. in
19c10 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f validateCursorsO
19c11 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 nModifiedBtrees(
19c12 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 db);. s
19c13 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c qlite3RollbackAl
19c14 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 l(db);.
19c15 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 sqlite3CloseSav
19c16 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 epoints(db);.
19c17 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
19c18 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 ommit = 1;.
19c19 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
19c1a 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 }. . /* If
19c1b 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 the auto-commit
19c1c 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 flag is set and
19c1d 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 this is the only
19c1e 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20 0a active writer .
19c1f 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e 20 ** VM, then
19c20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63 we do either a c
19c21 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63 ommit or rollbac
19c22 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 k of the current
19c23 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 transaction. .
19c24 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 **. ** Not
19c25 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c e: This block al
19c26 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f so runs if one o
19c27 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 f the special er
19c28 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20 rors handled .
19c29 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f ** above has o
19c2a 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f ccurred. . */
19c2b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
19c2c 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 3VtabInSync(db)
19c2d 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74 . && db->aut
19c2e 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26 oCommit . &&
19c2f 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
19c30 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d t==(p->readOnly=
19c31 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64 62 =0) . && (db
19c32 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
19c33 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 0a _CommitBusy)==0.
19c34 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ){. if(
19c35 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
19c36 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 K || (p->errorAc
19c37 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 tion==OE_Fail &&
19c38 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 !isSpecialError
19c39 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
19c3a 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 The auto-commit
19c3b 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61 6e flag is true, an
19c3c 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 d the vdbe progr
19c3d 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20 20 am was .
19c3e 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f 72 ** successful or
19c3f 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c hit an 'OR FAIL
19c40 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68 ' constraint. Th
19c41 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d 69 is means a commi
19c42 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 t . ** is
19c43 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20 required..
19c44 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 */. rc
19c45 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 = vdbeCommit(db
19c46 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , p);. if
19c47 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc==SQLITE_BUS
19c48 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 Y ){. s
19c49 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
19c4a 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 ArrayLeave(&p->a
19c4b 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 Mutex);.
19c4c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19c4d 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 BUSY;. }e
19c4e 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 lse if( rc!=SQLI
19c4f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
19c50 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 p->rc = rc;.
19c51 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
19c52 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b RollbackAll(db);
19c53 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
19c54 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
19c55 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 3CommitInternalC
19c56 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 hanges(db);.
19c57 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
19c58 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e{. sqlit
19c59 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 e3RollbackAll(db
19c5a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
19c5b 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 db->nStatement
19c5c 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 = 0;. }else i
19c5d 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d f( eStatementOp=
19c5e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 =0 ){. if(
19c5f 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->rc==SQLITE_OK
19c60 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 || p->errorActi
19c61 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 on==OE_Fail ){.
19c62 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e eStatemen
19c63 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f tOp = SAVEPOINT_
19c64 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d RELEASE;. }
19c65 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f else if( p->erro
19c66 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 rAction==OE_Abor
19c67 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 t ){. eSt
19c68 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 atementOp = SAVE
19c69 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a POINT_ROLLBACK;.
19c6a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
19c6b 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 invalidateC
19c6c 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 ursorsOnModified
19c6d 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 Btrees(db);.
19c6e 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 sqlite3Rollb
19c6f 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 ackAll(db);.
19c70 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 sqlite3Close
19c71 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a Savepoints(db);.
19c72 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
19c73 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
19c74 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
19c75 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e /* If eStatemen
19c76 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c tOp is non-zero,
19c77 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e then a statemen
19c78 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 t transaction ne
19c79 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 eds to. ** be
19c7a 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f committed or ro
19c7b 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 lled back. Call
19c7c 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 sqlite3VdbeClose
19c7d 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 Statement() to.
19c7e 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 ** do so. If
19c7f 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 this operation r
19c80 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c eturns an error,
19c81 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 and the current
19c82 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a statement. *
19c83 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 * error code is
19c84 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c SQLITE_OK or SQL
19c85 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 ITE_CONSTRAINT,
19c86 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 then set the err
19c87 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 or. ** code t
19c88 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e o the new value.
19c89 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
19c8a 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a eStatementOp ){.
19c8b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
19c8c 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 e3VdbeCloseState
19c8d 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 ment(p, eStateme
19c8e 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 ntOp);. if(
19c8f 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 rc && (p->rc==S
19c90 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 QLITE_OK || p->r
19c91 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 c==SQLITE_CONSTR
19c92 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 AINT) ){.
19c93 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 p->rc = rc;.
19c94 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
19c95 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
19c96 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a g);. p->z
19c97 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 ErrMsg = 0;.
19c98 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
19c99 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 /* If this was
19c9a 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 an INSERT, UPDAT
19c9b 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 E or DELETE and
19c9c 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 no statement tra
19c9d 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 nsaction. **
19c9e 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 has been rolled
19c9f 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 back, update the
19ca0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
19ca1 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e tion change-coun
19ca2 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 ter. . */.
19ca3 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e if( p->changeCn
19ca4 74 4f 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 tOn && p->pc>=0
19ca5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53 74 ){. if( eSt
19ca6 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45 50 atementOp!=SAVEP
19ca7 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b OINT_ROLLBACK ){
19ca8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
19ca9 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 VdbeSetChanges(d
19caa 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a b, p->nChange);.
19cab 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
19cac 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
19cad 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 30 SetChanges(db, 0
19cae 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
19caf 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b p->nChange = 0;
19cb0 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
19cb1 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f 6d Rollback or com
19cb2 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20 63 mit any schema c
19cb3 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63 75 hanges that occu
19cb4 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 rred. */. if(
19cb5 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f p->rc!=SQLITE_O
19cb6 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 K && db->flags&S
19cb7 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e QLITE_InternChan
19cb8 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ges ){. sql
19cb9 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
19cba 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a lSchema(db, 0);.
19cbb 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 db->flags
19cbc 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20 53 = (db->flags | S
19cbd 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e QLITE_InternChan
19cbe 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ges);. }..
19cbf 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 /* Release the
19cc0 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71 6c locks */. sql
19cc1 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
19cc2 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 rayLeave(&p->aMu
19cc3 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tex);. }.. /*
19cc4 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73 66 We have successf
19cc5 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64 20 ully halted and
19cc6 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20 20 closed the VM.
19cc7 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63 74 Record this fact
19cc8 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 . */. if( p->pc
19cc9 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 61 >=0 ){. db->a
19cca 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b 0a ctiveVdbeCnt--;.
19ccb 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64 if( !p->read
19ccc 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64 62 Only ){. db
19ccd 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d 2d ->writeVdbeCnt--
19cce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
19ccf 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 rt( db->activeVd
19cd0 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74 65 beCnt>=db->write
19cd1 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a 20 VdbeCnt );. }.
19cd2 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 p->magic = VDBE
19cd3 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20 63 _MAGIC_HALT;. c
19cd4 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e heckActiveVdbeCn
19cd5 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d 3e t(db);. if( p->
19cd6 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
19cd7 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
19cd8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
19cd9 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 }.. /* If the a
19cda 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 uto-commit flag
19cdb 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c 20 is set to true,
19cdc 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20 74 then any locks t
19cdd 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20 20 hat were held.
19cde 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e ** by connection
19cdf 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65 65 db have now bee
19ce0 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c 6c n released. Call
19ce1 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 sqlite3Connecti
19ce2 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20 20 onUnlocked() .
19ce3 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e 79 ** to invoke any
19ce4 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63 6b required unlock
19ce5 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
19ce6 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 s.. */. if( db
19ce7 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a ->autoCommit ){.
19ce8 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 sqlite3Conne
19ce9 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62 ctionUnlocked(db
19cea 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 );. }.. assert
19ceb 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 ( db->activeVdbe
19cec 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74 Cnt>0 || db->aut
19ced 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 oCommit==0 || db
19cee 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 ->nStatement==0
19cef 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
19cf0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
19cf1 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64 73 Each VDBE holds
19cf2 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 the result of t
19cf3 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 73 he most recent s
19cf4 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63 61 qlite3_step() ca
19cf5 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e 20 ll.** in p->rc.
19cf6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
19cf7 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20 62 ts that result b
19cf8 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b ack to SQLITE_OK
19cf9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19cfa 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19cfb 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 VdbeResetStepRes
19cfc 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ult(Vdbe *p){.
19cfd 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f p->rc = SQLITE_O
19cfe 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 K;.}../*.** Clea
19cff 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74 65 n up a VDBE afte
19d00 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74 20 r execution but
19d01 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 do not delete th
19d02 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74 2e e VDBE just yet.
19d03 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72 .** Write any er
19d04 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 74 ror messages int
19d05 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52 65 o *pzErrMsg. Re
19d06 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 20 turn the result
19d07 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 code..**.** Afte
19d08 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 r this routine i
19d09 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45 20 s run, the VDBE
19d0a 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79 20 should be ready
19d0b 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a 2a to be executed.*
19d0c 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 * again..**.** T
19d0d 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e 6f o look at it ano
19d0e 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20 72 ther way, this r
19d0f 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74 68 outine resets th
19d10 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a 2a e state of the.*
19d11 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e * virtual machin
19d12 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47 49 e from VDBE_MAGI
19d13 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d 41 C_RUN or VDBE_MA
19d14 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74 6f GIC_HALT back to
19d15 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 .** VDBE_MAGIC_I
19d16 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 NIT..*/.SQLITE_P
19d17 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19d18 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65 e3VdbeReset(Vdbe
19d19 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 *p){. sqlite3
19d1a 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 *db;. db = p->d
19d1b 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 b;.. /* If the
19d1c 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20 74 VM did not run t
19d1d 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 20 o completion or
19d1e 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72 65 if it encountere
19d1f 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 2c d an. ** error,
19d20 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e then it might n
19d21 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61 6c ot have been hal
19d22 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20 53 ted properly. S
19d23 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20 6e o halt. ** it n
19d24 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 ow.. */. (void
19d25 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e )sqlite3SafetyOn
19d26 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 (db);. sqlite3V
19d27 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28 76 dbeHalt(p);. (v
19d28 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
19d29 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 yOff(db);.. /*
19d2a 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20 If the VDBE has
19d2b 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72 74 be run even part
19d2c 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e ially, then tran
19d2d 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20 63 sfer the error c
19d2e 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72 ode. ** and err
19d2f 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 or message from
19d30 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74 68 the VDBE into th
19d31 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
19d32 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74 0a structure. But.
19d33 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42 45 ** if the VDBE
19d34 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73 has just been s
19d35 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68 61 et to run but ha
19d36 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 65 s not actually e
19d37 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a xecuted any. **
19d38 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 65 instructions ye
19d39 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61 69 t, leave the mai
19d3a 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 n database error
19d3b 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63 information unc
19d3c 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 hanged.. */. i
19d3d 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 f( p->pc>=0 ){.
19d3e 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d 73 if( p->zErrMs
19d3f 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 g ){. sqlit
19d40 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
19d41 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 6c loc();. sql
19d42 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
19d43 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e 7a db->pErr,-1,p->z
19d44 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55 54 ErrMsg,SQLITE_UT
19d45 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 F8,SQLITE_TRANSI
19d46 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ENT);. sqli
19d47 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
19d48 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e oc();. db->
19d49 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b errCode = p->rc;
19d4a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
19d4b 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 Free(db, p->zErr
19d4c 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e 7a Msg);. p->z
19d4d 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 ErrMsg = 0;.
19d4e 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 }else if( p->rc
19d4f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
19d50 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c Error(db, p->rc,
19d51 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0);. }else{.
19d52 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
19d53 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b or(db, SQLITE_OK
19d54 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 , 0);. }. }e
19d55 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26 26 lse if( p->rc &&
19d56 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 p->expired ){.
19d57 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72 65 /* The expire
19d58 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20 6f d flag was set o
19d59 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f 72 n the VDBE befor
19d5a 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c e the first call
19d5b 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 . ** to sqlit
19d5c 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20 63 e3_step(). For c
19d5d 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e 63 onsistency (sinc
19d5e 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 e sqlite3_step()
19d5f 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c 6c was. ** call
19d60 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61 74 ed), set the dat
19d61 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20 74 abase error in t
19d62 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c 6c his case as well
19d63 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c .. */. sql
19d64 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d ite3Error(db, p-
19d65 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c >rc, 0);. sql
19d66 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
19d67 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70 2d db->pErr, -1, p-
19d68 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54 45 >zErrMsg, SQLITE
19d69 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 _UTF8, SQLITE_TR
19d6a 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 ANSIENT);. sq
19d6b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
19d6c 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 p->zErrMsg);.
19d6d 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b p->zErrMsg = 0;
19d6e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c 61 . }.. /* Recla
19d6f 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 im all memory us
19d70 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a 20 ed by the VDBE.
19d71 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70 29 */. Cleanup(p)
19d72 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72 6f ;.. /* Save pro
19d73 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 filing informati
19d74 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44 42 on from this VDB
19d75 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66 64 E run.. */.#ifd
19d76 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a ef VDBE_PROFILE.
19d77 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f 75 {. FILE *ou
19d78 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65 5f t = fopen("vdbe_
19d79 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22 61 profile.out", "a
19d7a 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 20 ");. if( out
19d7b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a ){. int i;.
19d7c 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
19d7d 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20 20 t, "---- ");.
19d7e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d for(i=0; i<p-
19d7f 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nOp; i++){.
19d80 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
19d81 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70 5b "%02x", p->aOp[
19d82 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20 20 i].opcode);.
19d83 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e 74 }. fprint
19d84 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 f(out, "\n");.
19d85 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
19d86 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nOp; i++){.
19d87 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
19d88 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25 38 , "%6d %10lld %8
19d89 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20 20 lld ",.
19d8a 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 2c p->aOp[i].cnt,
19d8b 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 . p->a
19d8c 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20 20 Op[i].cycles,.
19d8d 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b p->aOp[
19d8e 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61 4f i].cnt>0 ? p->aO
19d8f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e 61 p[i].cycles/p->a
19d90 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20 20 Op[i].cnt : 0.
19d91 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
19d92 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
19d93 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d 3e tOp(out, i, &p->
19d94 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d aOp[i]);. }
19d95 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 . fclose(ou
19d96 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 t);. }. }.#e
19d97 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63 20 ndif. p->magic
19d98 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 = VDBE_MAGIC_INI
19d99 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 72 T;. return p->r
19d9a 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b c & db->errMask;
19d9b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e .}. ./*.** Clean
19d9c 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20 61 up and delete a
19d9d 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63 VDBE after exec
19d9e 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 61 ution. Return a
19d9f 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
19da0 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 is.** the result
19da1 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61 6e code. Write an
19da2 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 y error message
19da3 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 72 text into *pzErr
19da4 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 Msg..*/.SQLITE_P
19da5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19da6 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 e3VdbeFinalize(V
19da7 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 dbe *p){. int r
19da8 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
19da9 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 if( p->magic==V
19daa 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c DBE_MAGIC_RUN ||
19dab 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
19dac 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 20 MAGIC_HALT ){.
19dad 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
19dae 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 beReset(p);.
19daf 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70 2d assert( (rc & p-
19db0 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 >db->errMask)==r
19db1 63 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 c );. }else if(
19db2 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f p->magic!=VDBE_
19db3 4d 41 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20 20 MAGIC_INIT ){.
19db4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19db5 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 MISUSE;. }. sq
19db6 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 lite3VdbeDelete(
19db7 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
19db8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 .}../*.** Call t
19db9 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f he destructor fo
19dba 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65 r each auxdata e
19dbb 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e ntry in pVdbeFun
19dbc 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 c for which.** t
19dbd 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
19dbe 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 bit in mask is
19dbf 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20 clear. Auxdata
19dc0 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33 entries beyond 3
19dc1 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20 1.** are always
19dc2 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 destroyed. To d
19dc3 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 estroy all auxda
19dc4 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c ta entries, call
19dc5 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
19dc6 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a with mask==0..*
19dc7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19dc8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
19dc9 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 eDeleteAuxData(V
19dca 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 dbeFunc *pVdbeFu
19dcb 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 nc, int mask){.
19dcc 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
19dcd 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 0; i<pVdbeFunc->
19dce 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nAux; i++){.
19dcf 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a struct AuxData *
19dd0 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e pAux = &pVdbeFun
19dd1 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 c->apAux[i];.
19dd2 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 if( (i>31 || !(
19dd3 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c mask&(((u32)1)<<
19dd4 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 i))) && pAux->pA
19dd5 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ux ){. if(
19dd6 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b pAux->xDelete ){
19dd7 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 . pAux->x
19dd8 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 Delete(pAux->pAu
19dd9 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 x);. }.
19dda 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 pAux->pAux = 0
19ddb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
19ddc 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 *.** Delete an e
19ddd 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 ntire VDBE..*/.S
19dde 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19ddf 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 id sqlite3VdbeDe
19de0 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 lete(Vdbe *p){.
19de1 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 int i;. sqlite
19de2 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3 *db;.. if( p=
19de3 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 =0 ) return;. d
19de4 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 b = p->db;. if(
19de5 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 p->pPrev ){.
19de6 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 p->pPrev->pNext
19de7 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d = p->pNext;. }
19de8 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
19de9 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 ( db->pVdbe==p )
19dea 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 ;. db->pVdbe
19deb 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a = p->pNext;. }.
19dec 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 if( p->pNext )
19ded 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e {. p->pNext->
19dee 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 pPrev = p->pPrev
19def 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 ;. }. if( p->a
19df0 4f 70 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f Op ){. Op *pO
19df1 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 p = p->aOp;.
19df2 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f for(i=0; i<p->nO
19df3 70 3b 20 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a p; i++, pOp++){.
19df4 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c freeP4(db,
19df5 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f pOp->p4type, pO
19df6 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 p->p4.p);.#ifdef
19df7 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
19df8 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
19df9 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d e(db, pOp->zComm
19dfa 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 ent);.#endif
19dfb 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 . }. }. re
19dfc 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d leaseMemArray(p-
19dfd 3e 61 56 61 72 2c 20 70 2d 3e 6e 56 61 72 29 3b >aVar, p->nVar);
19dfe 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
19dff 28 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b (db, p->aLabel);
19e00 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 . releaseMemArr
19e01 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 ay(p->aColName,
19e02 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f p->nResColumn*CO
19e03 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 LNAME_N);. sqli
19e04 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
19e05 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 >aColName);. sq
19e06 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
19e07 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d p->zSql);. p->m
19e08 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
19e09 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 C_DEAD;. sqlite
19e0a 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
19e0b 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 Op);. sqlite3Db
19e0c 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72 65 Free(db, p->pFre
19e0d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 e);. sqlite3DbF
19e0e 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f ree(db, p);.}../
19e0f 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f *.** If a MoveTo
19e10 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65 operation is pe
19e11 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76 nding on the giv
19e12 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 en cursor, then
19e13 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 do that.** MoveT
19e14 6f 20 6e 6f 77 2e 20 20 52 65 74 75 72 6e 20 61 o now. Return a
19e15 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 49 n error code. I
19e16 66 20 6e 6f 20 4d 6f 76 65 54 6f 20 69 73 20 70 f no MoveTo is p
19e17 65 6e 64 69 6e 67 2c 20 74 68 69 73 0a 2a 2a 20 ending, this.**
19e18 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 routine does not
19e19 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 hing and returns
19e1a 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
19e1b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19e1c 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 t sqlite3VdbeCur
19e1d 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 sorMoveto(VdbeCu
19e1e 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 rsor *p){. if(
19e1f 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 p->deferredMovet
19e20 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 o ){. int res
19e21 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c , rc;.#ifdef SQL
19e22 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74 ITE_TEST. ext
19e23 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f ern int sqlite3_
19e24 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 search_count;.#e
19e25 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 ndif. assert(
19e26 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 p->isTable );.
19e27 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
19e28 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
19e29 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 ed(p->pCursor, 0
19e2a 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 , p->movetoTarge
19e2b 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 t, 0, &res);.
19e2c 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
19e2d 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 rc;. p->last
19e2e 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f 49 6e 74 Rowid = keyToInt
19e2f 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 (p->movetoTarget
19e30 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49 );. p->rowidI
19e31 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53 28 sValid = ALWAYS(
19e32 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 20 res==0) ?1:0;.
19e33 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73 3c if( NEVER(res<
19e34 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 0) ){. rc =
19e35 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
19e36 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 t(p->pCursor, &r
19e37 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 es);. if( r
19e38 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
19e39 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
19e3a 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 TE_TEST. sqli
19e3b 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 te3_search_count
19e3c 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 ++;.#endif. p
19e3d 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
19e3e 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 63 = 0;. p->cac
19e3f 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
19e40 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65 20 _STALE;. }else
19e41 69 66 28 20 70 2d 3e 70 43 75 72 73 6f 72 20 29 if( p->pCursor )
19e42 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 4d 6f 76 {. int hasMov
19e43 65 64 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d ed;. int rc =
19e44 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
19e45 73 6f 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e 70 sorHasMoved(p->p
19e46 43 75 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76 65 Cursor, &hasMove
19e47 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 d);. if( rc )
19e48 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
19e49 69 66 28 20 68 61 73 4d 6f 76 65 64 20 29 7b 0a if( hasMoved ){.
19e4a 20 20 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 p->cacheSt
19e4b 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
19e4c 4c 45 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75 6c LE;. p->nul
19e4d 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d 0a lRow = 1;. }.
19e4e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
19e4f 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
19e50 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 The following f
19e51 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 unctions:.**.**
19e52 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
19e53 6c 54 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69 74 lType().** sqlit
19e54 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
19e55 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 Len().** sqlite3
19e56 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28 29 0a VdbeSerialLen().
19e57 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 ** sqlite3VdbeSe
19e58 72 69 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71 6c rialPut().** sql
19e59 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 ite3VdbeSerialGe
19e5a 74 28 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 t().**.** encaps
19e5b 75 6c 61 74 65 20 74 68 65 20 63 6f 64 65 20 74 ulate the code t
19e5c 68 61 74 20 73 65 72 69 61 6c 69 7a 65 73 20 76 hat serializes v
19e5d 61 6c 75 65 73 20 66 6f 72 20 73 74 6f 72 61 67 alues for storag
19e5e 65 20 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 64 e in SQLite.** d
19e5f 61 74 61 20 61 6e 64 20 69 6e 64 65 78 20 72 65 ata and index re
19e60 63 6f 72 64 73 2e 20 45 61 63 68 20 73 65 72 69 cords. Each seri
19e61 61 6c 69 7a 65 64 20 76 61 6c 75 65 20 63 6f 6e alized value con
19e62 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27 73 sists of a.** 's
19e63 65 72 69 61 6c 2d 74 79 70 65 27 20 61 6e 64 20 erial-type' and
19e64 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e 20 a blob of data.
19e65 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 The serial type
19e66 69 73 20 61 6e 20 38 2d 62 79 74 65 20 75 6e 73 is an 8-byte uns
19e67 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 igned.** integer
19e68 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 76 61 , stored as a va
19e69 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 rint..**.** In a
19e6a 6e 20 53 51 4c 69 74 65 20 69 6e 64 65 78 20 72 n SQLite index r
19e6b 65 63 6f 72 64 2c 20 74 68 65 20 73 65 72 69 61 ecord, the seria
19e6c 6c 20 74 79 70 65 20 69 73 20 73 74 6f 72 65 64 l type is stored
19e6d 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 directly before
19e6e 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 .** the blob of
19e6f 64 61 74 61 20 74 68 61 74 20 69 74 20 63 6f 72 data that it cor
19e70 72 65 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 responds to. In
19e71 61 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2c 20 a table record,
19e72 61 6c 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74 79 all serial.** ty
19e73 70 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 pes are stored a
19e74 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
19e75 68 65 20 72 65 63 6f 72 64 2c 20 61 6e 64 20 74 he record, and t
19e76 68 65 20 62 6c 6f 62 73 20 6f 66 20 64 61 74 61 he blobs of data
19e77 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e 20 at.** the end.
19e78 48 65 6e 63 65 20 74 68 65 73 65 20 66 75 6e 63 Hence these func
19e79 74 69 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65 20 tions allow the
19e7a 63 61 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c 65 caller to handle
19e7b 20 74 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 the.** serial-t
19e7c 79 70 65 20 61 6e 64 20 64 61 74 61 20 62 6c 6f ype and data blo
19e7d 62 20 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a 2a b seperately..**
19e7e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
19e7f 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62 65 g table describe
19e80 73 20 74 68 65 20 76 61 72 69 6f 75 73 20 73 74 s the various st
19e81 6f 72 61 67 65 20 63 6c 61 73 73 65 73 20 66 6f orage classes fo
19e82 72 20 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 r data:.**.**
19e83 73 65 72 69 61 6c 20 74 79 70 65 20 20 20 20 20 serial type
19e84 20 20 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 bytes of data
19e85 20 20 20 20 20 20 74 79 70 65 0a 2a 2a 20 20 20 type.**
19e86 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 --------------
19e87 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -------------
19e88 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -- ----------
19e89 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30 20 -----.** 0
19e8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e8b 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 0
19e8c 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31 20 NULL.** 1
19e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e8e 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20 1
19e8f 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a signed integer.
19e90 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 20 20 ** 2
19e91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20 2
19e92 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 signe
19e93 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 d integer.**
19e94 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 20 3
19e95 20 20 20 20 20 20 20 20 33 20 20 20 20 20 20 20 3
19e96 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 signed inte
19e97 67 65 72 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 ger.** 4
19e98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e99 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20 73 4 s
19e9a 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
19e9b 20 20 20 20 20 20 35 20 20 20 20 20 20 20 20 20 5
19e9c 20 20 20 20 20 20 20 20 20 20 20 20 36 20 20 20 6
19e9d 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 signed
19e9e 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
19e9f 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6
19ea0 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20 8
19ea1 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 signed intege
19ea2 72 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20 r.** 7
19ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ea4 38 20 20 20 20 20 20 20 20 20 20 20 20 49 45 45 8 IEE
19ea5 45 20 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20 20 E float.**
19ea6 38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 8
19ea7 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 0
19ea8 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 Integer const
19ea9 61 6e 74 20 30 0a 2a 2a 20 20 20 20 20 20 39 20 ant 0.** 9
19eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19eab 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 0
19eac 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e Integer constan
19ead 74 20 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31 31 t 1.** 10,11
19eae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19eaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 r
19eb0 65 73 65 72 76 65 64 20 66 6f 72 20 65 78 70 61 eserved for expa
19eb1 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 nsion.** N>=1
19eb2 32 20 61 6e 64 20 65 76 65 6e 20 20 20 20 20 20 2 and even
19eb3 20 28 4e 2d 31 32 29 2f 32 20 20 20 20 20 20 20 (N-12)/2
19eb4 20 42 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 BLOB.** N>=1
19eb5 33 20 61 6e 64 20 6f 64 64 20 20 20 20 20 20 20 3 and odd
19eb6 20 28 4e 2d 31 33 29 2f 32 20 20 20 20 20 20 20 (N-13)/2
19eb7 20 74 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 text.**.** The
19eb8 38 20 61 6e 64 20 39 20 74 79 70 65 73 20 77 65 8 and 9 types we
19eb9 72 65 20 61 64 64 65 64 20 69 6e 20 33 2e 33 2e re added in 3.3.
19eba 30 2c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 34 0, file format 4
19ebb 2e 20 20 50 72 69 6f 72 20 76 65 72 73 69 6f 6e . Prior version
19ebc 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 77 s.** of SQLite w
19ebd 69 6c 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 ill not understa
19ebe 6e 64 20 74 68 6f 73 65 20 73 65 72 69 61 6c 20 nd those serial
19ebf 74 79 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a types..*/../*.**
19ec0 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 72 69 Return the seri
19ec1 61 6c 2d 74 79 70 65 20 66 6f 72 20 74 68 65 20 al-type for the
19ec2 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 value stored in
19ec3 70 4d 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f pMem..*/.SQLITE_
19ec4 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 PRIVATE u32 sqli
19ec5 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
19ec6 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 e(Mem *pMem, int
19ec7 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 file_format){.
19ec8 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65 int flags = pMe
19ec9 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 m->flags;. int
19eca 6e 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 26 n;.. if( flags&
19ecb 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
19ecc 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
19ecd 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49 6e if( flags&MEM_In
19ece 74 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 t ){. /* Figu
19ecf 72 65 20 6f 75 74 20 77 68 65 74 68 65 72 20 74 re out whether t
19ed0 6f 20 75 73 65 20 31 2c 20 32 2c 20 34 2c 20 36 o use 1, 2, 4, 6
19ed1 20 6f 72 20 38 20 62 79 74 65 73 2e 20 2a 2f 0a or 8 bytes. */.
19ed2 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 36 # define MAX_6
19ed3 42 59 54 45 20 28 28 28 28 69 36 34 29 30 78 30 BYTE ((((i64)0x0
19ed4 30 30 30 38 30 30 30 29 3c 3c 33 32 29 2d 31 29 0008000)<<32)-1)
19ed5 0a 20 20 20 20 69 36 34 20 69 20 3d 20 70 4d 65 . i64 i = pMe
19ed6 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34 20 m->u.i;. u64
19ed7 75 3b 0a 20 20 20 20 69 66 28 20 66 69 6c 65 5f u;. if( file_
19ed8 66 6f 72 6d 61 74 3e 3d 34 20 26 26 20 28 69 26 format>=4 && (i&
19ed9 31 29 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 72 1)==i ){. r
19eda 65 74 75 72 6e 20 38 2b 28 75 33 32 29 69 3b 0a eturn 8+(u32)i;.
19edb 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 3c }. u = i<
19edc 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 20 0 ? -i : i;.
19edd 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65 74 if( u<=127 ) ret
19ede 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 75 urn 1;. if( u
19edf 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72 6e <=32767 ) return
19ee0 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 38 2;. if( u<=8
19ee1 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e 20 388607 ) return
19ee2 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 31 3;. if( u<=21
19ee3 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75 72 47483647 ) retur
19ee4 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d n 4;. if( u<=
19ee5 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74 75 MAX_6BYTE ) retu
19ee6 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72 6e rn 5;. return
19ee7 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 6;. }. if( fl
19ee8 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a ags&MEM_Real ){.
19ee9 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 return 7;.
19eea 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d }. assert( pMem
19eeb 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
19eec 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45 4d ed || flags&(MEM
19eed 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 _Str|MEM_Blob) )
19eee 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b ;. n = pMem->n;
19eef 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d . if( flags & M
19ef0 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 6e EM_Zero ){. n
19ef1 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 += pMem->u.nZer
19ef2 6f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 o;. }. assert(
19ef3 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 n>=0 );. retur
19ef4 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b 20 n ((n*2) + 12 +
19ef5 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 ((flags&MEM_Str)
19ef6 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 !=0));.}../*.**
19ef7 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 Return the lengt
19ef8 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 63 6f h of the data co
19ef9 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 rresponding to t
19efa 68 65 20 73 75 70 70 6c 69 65 64 20 73 65 72 69 he supplied seri
19efb 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 al-type..*/.SQLI
19efc 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19efd 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
19efe 54 79 70 65 4c 65 6e 28 75 33 32 20 73 65 72 69 TypeLen(u32 seri
19eff 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 66 28 20 al_type){. if(
19f00 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 serial_type>=12
19f01 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 73 ){. return (s
19f02 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 erial_type-12)/2
19f03 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
19f04 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 tatic const u8 a
19f05 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c Size[] = { 0, 1,
19f06 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c 20 2, 3, 4, 6, 8,
19f07 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 8, 0, 0, 0, 0 };
19f08 0a 20 20 20 20 72 65 74 75 72 6e 20 61 53 69 7a . return aSiz
19f09 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b 0a e[serial_type];.
19f0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 }.}../*.** If
19f0b 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 61 72 63 we are on an arc
19f0c 68 69 74 65 63 74 75 72 65 20 77 69 74 68 20 6d hitecture with m
19f0d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 ixed-endian floa
19f0e 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 ting .** points
19f0f 28 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e 20 (ex: ARM7) then
19f10 73 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20 34 swap the lower 4
19f11 20 62 79 74 65 73 20 77 69 74 68 20 74 68 65 20 bytes with the
19f12 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74 65 .** upper 4 byte
19f13 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 s. Return the r
19f14 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 esult..**.** For
19f15 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63 74 75 most architectu
19f16 72 65 73 2c 20 74 68 69 73 20 69 73 20 61 20 6e res, this is a n
19f17 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 o-op..**.** (lat
19f18 65 72 29 3a 20 20 49 74 20 69 73 20 72 65 70 6f er): It is repo
19f19 72 74 65 64 20 74 6f 20 6d 65 20 74 68 61 74 20 rted to me that
19f1a 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e the mixed-endian
19f1b 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 problem.** on A
19f1c 52 4d 37 20 69 73 20 61 6e 20 69 73 73 75 65 20 RM7 is an issue
19f1d 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 20 77 69 with GCC, not wi
19f1e 74 68 20 74 68 65 20 41 52 4d 37 20 63 68 69 70 th the ARM7 chip
19f1f 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20 74 . It seems.** t
19f20 68 61 74 20 65 61 72 6c 79 20 76 65 72 73 69 6f hat early versio
19f21 6e 73 20 6f 66 20 47 43 43 20 73 74 6f 72 65 64 ns of GCC stored
19f22 20 74 68 65 20 74 77 6f 20 77 6f 72 64 73 20 6f the two words o
19f23 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66 6c f a 64-bit.** fl
19f24 6f 61 74 20 69 6e 20 74 68 65 20 77 72 6f 6e 67 oat in the wrong
19f25 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 74 68 61 order. And tha
19f26 74 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e t error has been
19f27 20 70 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20 65 propagated.** e
19f28 76 65 72 20 73 69 6e 63 65 2e 20 20 54 68 65 20 ver since. The
19f29 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65 63 blame is not nec
19f2a 65 73 73 61 72 69 6c 79 20 77 69 74 68 20 47 43 essarily with GC
19f2b 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 C, though..** GC
19f2c 43 20 6d 69 67 68 74 20 68 61 76 65 20 6a 75 73 C might have jus
19f2d 74 20 63 6f 70 79 69 6e 67 20 74 68 65 20 70 72 t copying the pr
19f2e 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72 69 oblem from a pri
19f2f 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 or compiler..**
19f30 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 74 I am also told t
19f31 68 61 74 20 6e 65 77 65 72 20 76 65 72 73 69 6f hat newer versio
19f32 6e 73 20 6f 66 20 47 43 43 20 74 68 61 74 20 66 ns of GCC that f
19f33 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 72 65 6e ollow a differen
19f34 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 74 68 65 t.** ABI get the
19f35 20 62 79 74 65 20 6f 72 64 65 72 20 72 69 67 68 byte order righ
19f36 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 t..**.** Develop
19f37 65 72 73 20 75 73 69 6e 67 20 53 51 4c 69 74 65 ers using SQLite
19f38 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f 75 on an ARM7 shou
19f39 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 ld compile and r
19f3a 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 70 70 6c un their.** appl
19f3b 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d 44 ication using -D
19f3c 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 20 61 SQLITE_DEBUG=1 a
19f3d 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 57 t least once. W
19f3e 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e 61 ith DEBUG.** ena
19f3f 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65 72 bled, some asser
19f40 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65 6e ts below will en
19f41 73 75 72 65 20 74 68 61 74 20 74 68 65 20 62 79 sure that the by
19f42 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 66 te order of.** f
19f43 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
19f44 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74 2e lues is correct.
19f45 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38 2d .**.** (2007-08-
19f46 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e 20 56 30) Frank van V
19f47 75 67 74 20 68 61 73 20 73 74 75 64 69 65 64 20 ugt has studied
19f48 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c 6f this problem clo
19f49 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73 20 sely.** and has
19f4a 73 65 6e 64 20 68 69 73 20 66 69 6e 64 69 6e 67 send his finding
19f4b 73 20 74 6f 20 74 68 65 20 53 51 4c 69 74 65 20 s to the SQLite
19f4c 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 46 72 61 developers. Fra
19f4d 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 74 68 61 nk.** writes tha
19f4e 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 72 t some Linux ker
19f4f 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61 74 nels offer float
19f50 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 64 77 61 ing point hardwa
19f51 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 re.** emulation
19f52 74 68 61 74 20 75 73 65 73 20 6f 6e 6c 79 20 33 that uses only 3
19f53 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 61 73 20 2-bit mantissas
19f54 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 75 6c instead of a ful
19f55 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61 73 l .** 48-bits as
19f56 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 required by the
19f57 20 49 45 45 45 20 73 74 61 6e 64 61 72 64 2e 20 IEEE standard.
19f58 20 28 54 68 69 73 20 69 73 20 74 68 65 0a 2a 2a (This is the.**
19f59 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53 54 CONFIG_FPE_FAST
19f5a 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e FPE option.) On
19f5b 20 73 75 63 68 20 73 79 73 74 65 6d 73 2c 20 66 such systems, f
19f5c 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a loating point.**
19f5d 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20 62 byte swapping b
19f5e 65 63 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d 70 ecomes very comp
19f5f 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 61 76 6f licated. To avo
19f60 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 id problems,.**
19f61 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 62 79 the necessary by
19f62 74 65 20 73 77 61 70 70 69 6e 67 20 69 73 20 63 te swapping is c
19f63 61 72 72 69 65 64 20 6f 75 74 20 75 73 69 6e 67 arried out using
19f64 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 a 64-bit intege
19f65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61 6e r.** rather than
19f66 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 2e a 64-bit float.
19f67 20 20 46 72 61 6e 6b 20 61 73 73 75 72 65 73 20 Frank assures
19f68 75 73 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 us that the code
19f69 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 here.** works f
19f6a 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 74 68 65 or him. We, the
19f6b 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 68 61 76 developers, hav
19f6c 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64 65 e no way to inde
19f6d 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 pendently.** ver
19f6e 69 66 79 20 74 68 69 73 2c 20 62 75 74 20 46 72 ify this, but Fr
19f6f 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e 6f ank seems to kno
19f70 77 20 77 68 61 74 20 68 65 20 69 73 20 74 61 6c w what he is tal
19f71 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73 6f king about.** so
19f72 20 77 65 20 74 72 75 73 74 20 68 69 6d 2e 0a 2a we trust him..*
19f73 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
19f74 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 MIXED_ENDIAN_64B
19f75 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63 20 IT_FLOAT.static
19f76 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75 36 u64 floatSwap(u6
19f77 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 4 in){. union {
19f78 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 20 20 20 . u64 r;.
19f79 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b u32 i[2];. } u;
19f7a 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e 72 . u32 t;.. u.r
19f7b 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 = in;. t = u.i
19f7c 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 [0];. u.i[0] =
19f7d 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d u.i[1];. u.i[1]
19f7e 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e 20 75 = t;. return u
19f7f 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20 73 .r;.}.# define s
19f80 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c wapMixedEndianFl
19f81 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c 6f 61 oat(X) X = floa
19f82 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65 0a 23 tSwap(X).#else.#
19f83 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 define swapMixe
19f84 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 0a dEndianFloat(X).
19f85 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 #endif../*.** Wr
19f86 69 74 65 20 74 68 65 20 73 65 72 69 61 6c 69 7a ite the serializ
19f87 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66 6f 72 ed data blob for
19f88 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 the value store
19f89 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 0a d in pMem into .
19f8a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20 61 73 ** buf. It is as
19f8b 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 63 sumed that the c
19f8c 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63 61 aller has alloca
19f8d 74 65 64 20 73 75 66 66 69 63 69 65 6e 74 20 73 ted sufficient s
19f8e 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 pace..** Return
19f8f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
19f90 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a tes written..**.
19f91 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65 20 61 ** nBuf is the a
19f92 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 6c mount of space l
19f93 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20 6e eft in buf[]. n
19f94 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79 73 20 Buf must always
19f95 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f 75 be.** large enou
19f96 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 65 gh to hold the e
19f97 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20 45 78 ntire field. Ex
19f98 63 65 70 74 2c 20 69 66 20 74 68 65 20 66 69 65 cept, if the fie
19f99 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 ld is.** a blob
19f9a 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c 6c with a zero-fill
19f9b 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20 62 75 ed tail, then bu
19f9c 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a 75 73 f[] might be jus
19f9d 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 73 t the right.** s
19f9e 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 ize to hold ever
19f9f 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 66 6f ything except fo
19fa0 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 r the zero-fille
19fa1 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75 66 5b d tail. If buf[
19fa2 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69 67 ].** is only big
19fa3 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 enough to hold
19fa4 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 the non-zero pre
19fa5 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 fix, then only w
19fa6 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70 72 65 rite that.** pre
19fa7 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 fix into buf[].
19fa8 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20 69 73 But if buf[] is
19fa9 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f large enough to
19faa 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a 2a hold both the.*
19fab 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74 68 65 * prefix and the
19fac 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69 74 65 tail then write
19fad 20 74 68 65 20 70 72 65 66 69 78 20 61 6e 64 20 the prefix and
19fae 73 65 74 20 74 68 65 20 74 61 69 6c 20 74 6f 20 set the tail to
19faf 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a all.** zeros..**
19fb0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
19fb1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 umber of bytes a
19fb2 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 ctually written
19fb3 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68 65 into buf[]. The
19fb4 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 number.** of by
19fb5 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d tes in the zero-
19fb6 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20 69 filled tail is i
19fb7 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 72 ncluded in the r
19fb8 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 eturn value only
19fb9 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62 79 74 .** if those byt
19fba 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64 20 69 es were zeroed i
19fbb 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c n buf[]..*/ .SQL
19fbc 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19fbd 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
19fbe 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20 69 6e lPut(u8 *buf, in
19fbf 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 t nBuf, Mem *pMe
19fc0 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d m, int file_form
19fc1 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72 69 61 at){. u32 seria
19fc2 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 l_type = sqlite3
19fc3 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 VdbeSerialType(p
19fc4 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 Mem, file_format
19fc5 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20 );. int len;..
19fc6 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e 64 20 /* Integer and
19fc7 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73 65 Real */. if( se
19fc8 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26 20 rial_type<=7 &&
19fc9 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20 29 7b serial_type>0 ){
19fca 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20 20 20 . u64 v;.
19fcb 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28 20 73 int i;. if( s
19fcc 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29 7b erial_type==7 ){
19fcd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
19fce 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f 66 izeof(v)==sizeof
19fcf 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 (pMem->r) );.
19fd0 20 20 20 6d 65 6d 63 70 79 28 26 76 2c 20 26 70 memcpy(&v, &p
19fd1 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 76 Mem->r, sizeof(v
19fd2 29 29 3b 0a 20 20 20 20 20 20 73 77 61 70 4d 69 ));. swapMi
19fd3 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 76 xedEndianFloat(v
19fd4 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
19fd5 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e v = pMem->u.
19fd6 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e i;. }. len
19fd7 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 33 56 64 = i = sqlite3Vd
19fd8 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
19fd9 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 serial_type);.
19fda 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d 6e assert( len<=n
19fdb 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 Buf );. while
19fdc 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 ( i-- ){. b
19fdd 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 uf[i] = (u8)(v&0
19fde 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e xFF);. v >>
19fdf 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 = 8;. }. r
19fe0 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a eturn len;. }..
19fe1 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 /* String or b
19fe2 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 lob */. if( ser
19fe3 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a ial_type>=12 ){.
19fe4 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
19fe5 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c ->n + ((pMem->fl
19fe6 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f ags & MEM_Zero)?
19fe7 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 pMem->u.nZero:0)
19fe8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d . ==
19fe9 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
19fea 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c alTypeLen(serial
19feb 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61 73 _type) );. as
19fec 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e sert( pMem->n<=n
19fed 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20 3d Buf );. len =
19fee 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d 65 pMem->n;. me
19fef 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d 3e mcpy(buf, pMem->
19ff0 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66 28 z, len);. if(
19ff1 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
19ff2 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 EM_Zero ){.
19ff3 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e len += pMem->u.
19ff4 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 69 66 28 nZero;. if(
19ff5 20 6c 65 6e 3e 6e 42 75 66 20 29 7b 0a 20 20 20 len>nBuf ){.
19ff6 20 20 20 20 20 6c 65 6e 20 3d 20 6e 42 75 66 3b len = nBuf;
19ff7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
19ff8 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d 2d emset(&buf[pMem-
19ff9 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 6d >n], 0, len-pMem
19ffa 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ->n);. }.
19ffb 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a return len;. }.
19ffc 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f . /* NULL or co
19ffd 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20 2a nstants 0 or 1 *
19ffe 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a /. return 0;.}.
19fff 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 ./*.** Deseriali
1a000 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f 62 ze the data blob
1a001 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 62 pointed to by b
1a002 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79 70 uf as serial typ
1a003 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a 2a e serial_type.**
1a004 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
1a005 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 20 esult in pMem.
1a006 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1a007 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e r of bytes read.
1a008 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/ .SQLITE_PRIV
1a009 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1a00a 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20 20 dbeSerialGet(.
1a00b 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1a00c 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f 2a har *buf, /*
1a00d 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65 72 Buffer to deser
1a00e 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a 20 ialize from */.
1a00f 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
1a010 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1a011 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74 6f * Serial type to
1a012 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f 0a deserialize */.
1a013 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 Mem *pMem
1a014 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a015 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 /* Memory cell t
1a016 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69 6e o write value in
1a017 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 to */.){. switc
1a018 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20 29 h( serial_type )
1a019 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20 20 {. case 10:
1a01a 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72 /* Reserved for
1a01b 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 future use */.
1a01c 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f 2a case 11: /*
1a01d 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 Reserved for fu
1a01e 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 ture use */.
1a01f 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e 55 case 0: { /* NU
1a020 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d LL */. pMem
1a021 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 ->flags = MEM_Nu
1a022 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ll;. break;
1a023 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1a024 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20 73 1: { /* 1-byte s
1a025 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
1a026 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 . pMem->u.i
1a027 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 29 = (signed char)
1a028 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70 4d buf[0];. pM
1a029 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a02a 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 Int;. retur
1a02b 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 n 1;. }. c
1a02c 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 79 ase 2: { /* 2-by
1a02d 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
1a02e 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d r */. pMem-
1a02f 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 >u.i = (((signed
1a030 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 char)buf[0])<<8
1a031 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 ) | buf[1];.
1a032 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1a033 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 MEM_Int;. r
1a034 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a 20 eturn 2;. }.
1a035 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a 20 case 3: { /*
1a036 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 3-byte signed in
1a037 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 teger */. p
1a038 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 Mem->u.i = (((si
1a039 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d gned char)buf[0]
1a03a 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31 5d )<<16) | (buf[1]
1a03b 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a 20 <<8) | buf[2];.
1a03c 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1a03d 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1a03e 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 return 3;.
1a03f 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b 20 }. case 4: {
1a040 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 4-byte signed
1a041 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 integer */.
1a042 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 62 pMem->u.i = (b
1a043 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 uf[0]<<24) | (bu
1a044 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 f[1]<<16) | (buf
1a045 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d [2]<<8) | buf[3]
1a046 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
1a047 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1a048 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 return 4;.
1a049 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35 3a }. case 5:
1a04a 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69 67 { /* 6-byte sig
1a04b 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 ned integer */.
1a04c 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28 u64 x = (((
1a04d 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b signed char)buf[
1a04e 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d 0])<<8) | buf[1]
1a04f 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d 20 ;. u32 y =
1a050 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 28 (buf[2]<<24) | (
1a051 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 62 buf[3]<<16) | (b
1a052 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66 5b uf[4]<<8) | buf[
1a053 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 78 5];. x = (x
1a054 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 20 <<32) | y;.
1a055 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 pMem->u.i = *(i
1a056 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70 4d 64*)&x;. pM
1a057 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a058 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 Int;. retur
1a059 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 n 6;. }. c
1a05a 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62 79 ase 6: /* 8-by
1a05b 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
1a05c 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37 3a r */. case 7:
1a05d 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61 74 { /* IEEE float
1a05e 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 ing point */.
1a05f 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20 20 u64 x;.
1a060 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66 69 u32 y;.#if !defi
1a061 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 ned(NDEBUG) && !
1a062 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1a063 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
1a064 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65 72 NT). /* Ver
1a065 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65 72 ify that integer
1a066 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20 70 s and floating p
1a067 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65 20 oint values use
1a068 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20 2a the same. *
1a069 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 4f * byte order. O
1a06a 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49 54 r, that if SQLIT
1a06b 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 E_MIXED_ENDIAN_6
1a06c 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20 20 4BIT_FLOAT is.
1a06d 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 74 ** defined t
1a06e 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 hat 64-bit float
1a06f 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 ing point values
1a070 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78 65 really are mixe
1a071 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69 61 d. ** endia
1a072 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n.. */.
1a073 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
1a074 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30 78 64 t1 = ((u64)0x
1a075 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a 20 3ff00000)<<32;.
1a076 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 static cons
1a077 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31 2e t double r1 = 1.
1a078 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32 20 0;. u64 t2
1a079 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61 70 = t1;. swap
1a07a 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 MixedEndianFloat
1a07b 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73 65 (t2);. asse
1a07c 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d 3d rt( sizeof(r1)==
1a07d 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d 65 sizeof(t2) && me
1a07e 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20 73 mcmp(&r1, &t2, s
1a07f 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29 3b izeof(r1))==0 );
1a080 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 78 .#endif.. x
1a081 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20 = (buf[0]<<24)
1a082 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c | (buf[1]<<16) |
1a083 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 (buf[2]<<8) | b
1a084 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20 3d uf[3];. y =
1a085 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20 (buf[4]<<24) |
1a086 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28 (buf[5]<<16) | (
1a087 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66 buf[6]<<8) | buf
1a088 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 [7];. x = (
1a089 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 x<<32) | y;.
1a08a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 if( serial_typ
1a08b 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20 20 e==6 ){.
1a08c 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 pMem->u.i = *(i6
1a08d 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20 70 4*)&x;. p
1a08e 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1a08f 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 _Int;. }els
1a090 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
1a091 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38 20 t( sizeof(x)==8
1a092 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e && sizeof(pMem->
1a093 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20 20 r)==8 );.
1a094 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e swapMixedEndian
1a095 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20 20 Float(x);.
1a096 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d 3e memcpy(&pMem->
1a097 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 r, &x, sizeof(x)
1a098 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d );. pMem-
1a099 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 >flags = sqlite3
1a09a 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3f IsNaN(pMem->r) ?
1a09b 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f MEM_Null : MEM_
1a09c 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Real;. }.
1a09d 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 return 8;.
1a09e 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a 20 }. case 8:
1a09f 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30 20 /* Integer 0
1a0a0 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20 7b */. case 9: {
1a0a1 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20 2a /* Integer 1 *
1a0a2 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e /. pMem->u.
1a0a3 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65 2d i = serial_type-
1a0a4 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 8;. pMem->f
1a0a5 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1a0a6 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
1a0a7 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
1a0a8 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6c t: {. int l
1a0a9 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79 70 en = (serial_typ
1a0aa 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20 70 e-12)/2;. p
1a0ab 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a Mem->z = (char *
1a0ac 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65 6d )buf;. pMem
1a0ad 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 ->n = len;.
1a0ae 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b pMem->xDel = 0;
1a0af 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 61 . if( seria
1a0b0 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a 20 l_type&0x01 ){.
1a0b1 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1a0b2 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 4d gs = MEM_Str | M
1a0b3 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 EM_Ephem;.
1a0b4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
1a0b5 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1a0b6 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 65 _Blob | MEM_Ephe
1a0b7 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 m;. }.
1a0b8 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 20 return len;.
1a0b9 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1a0ba 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 0;.}.../*.** Giv
1a0bb 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74 65 en the nKey-byte
1a0bc 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 encoding of a r
1a0bd 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d 2c ecord in pKey[],
1a0be 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72 65 parse the.** re
1a0bf 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70 61 cord into a Unpa
1a0c0 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75 63 ckedRecord struc
1a0c1 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61 20 ture. Return a
1a0c2 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 pointer to.** th
1a0c3 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a at structure..**
1a0c4 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 .** The calling
1a0c5 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20 70 function might p
1a0c6 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20 62 rovide szSpace b
1a0c7 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a ytes of memory.*
1a0c8 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61 63 * space at pSpac
1a0c9 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20 63 e. This space c
1a0ca 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68 6f an be used to ho
1a0cb 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 0a ld the returned.
1a0cc 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65 63 ** VDbeParsedRec
1a0cd 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69 66 ord structure if
1a0ce 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f it is large eno
1a0cf 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a 2a ugh. If it is.*
1a0d0 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 * not big enough
1a0d1 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61 69 , space is obtai
1a0d2 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
1a0d3 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a _malloc()..**.**
1a0d4 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 The returned st
1a0d5 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20 62 ructure should b
1a0d6 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63 61 e closed by a ca
1a0d7 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ll to.** sqlite3
1a0d8 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b VdbeDeleteUnpack
1a0d9 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 0a edRecord()..*/ .
1a0da 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 SQLITE_PRIVATE U
1a0db 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 npackedRecord *s
1a0dc 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 qlite3VdbeRecord
1a0dd 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e 66 Unpack(. KeyInf
1a0de 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 o *pKeyInfo,
1a0df 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
1a0e0 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72 64 about the record
1a0e1 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e 74 format */. int
1a0e2 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 nKey,
1a0e3 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
1a0e4 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 64 he binary record
1a0e5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
1a0e6 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a 20 *pKey, /*
1a0e7 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 The binary recor
1a0e8 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 53 70 d */. char *pSp
1a0e9 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ace, /*
1a0ea 20 55 6e 61 6c 69 67 6e 65 64 20 73 70 61 63 65 Unaligned space
1a0eb 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f available to ho
1a0ec 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f ld the object */
1a0ed 0a 20 20 69 6e 74 20 73 7a 53 70 61 63 65 20 20 . int szSpace
1a0ee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
1a0ef 65 20 6f 66 20 70 53 70 61 63 65 5b 5d 20 69 6e e of pSpace[] in
1a0f0 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 bytes */.){. c
1a0f1 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1a0f2 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73 ar *aKey = (cons
1a0f3 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1a0f4 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b *)pKey;. Unpack
1a0f5 65 64 52 65 63 6f 72 64 20 2a 70 3b 20 20 2f 2a edRecord *p; /*
1a0f6 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72 65 The unpacked re
1a0f7 63 6f 72 64 20 74 68 61 74 20 77 65 20 77 69 6c cord that we wil
1a0f8 6c 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e l return */. in
1a0f9 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1a0fa 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 73 70 61 63 /* Memory spac
1a0fb 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 e needed to hold
1a0fc 20 70 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a p, in bytes */.
1a0fd 20 20 69 6e 74 20 64 3b 0a 20 20 75 33 32 20 69 int d;. u32 i
1a0fe 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20 20 dx;. u16 u;
1a0ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 /* Uns
1a100 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 igned loop count
1a101 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 er */. u32 szHd
1a102 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a r;. Mem *pMem;.
1a103 20 20 69 6e 74 20 6e 4f 66 66 3b 20 20 20 20 20 int nOff;
1a104 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 61 73 /* Increas
1a105 65 20 70 53 70 61 63 65 20 62 79 20 74 68 69 73 e pSpace by this
1a106 20 6d 75 63 68 20 74 6f 20 38 2d 62 79 74 65 20 much to 8-byte
1a107 61 6c 69 67 6e 20 69 74 20 2a 2f 0a 20 20 0a 20 align it */. .
1a108 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 77 61 6e 74 /*. ** We want
1a109 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 70 6f to shift the po
1a10a 69 6e 74 65 72 20 70 53 70 61 63 65 20 75 70 20 inter pSpace up
1a10b 73 75 63 68 20 74 68 61 74 20 69 74 20 69 73 20 such that it is
1a10c 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 2e 0a 8-byte aligned..
1a10d 20 20 2a 2a 20 54 68 75 73 2c 20 77 65 20 6e 65 ** Thus, we ne
1a10e 65 64 20 74 6f 20 63 61 6c 63 75 6c 61 74 65 20 ed to calculate
1a10f 61 20 76 61 6c 75 65 2c 20 6e 4f 66 66 2c 20 62 a value, nOff, b
1a110 65 74 77 65 65 6e 20 30 20 61 6e 64 20 37 2c 20 etween 0 and 7,
1a111 74 6f 20 73 68 69 66 74 20 0a 20 20 2a 2a 20 69 to shift . ** i
1a112 74 20 62 79 2e 20 20 49 66 20 70 53 70 61 63 65 t by. If pSpace
1a113 20 69 73 20 61 6c 72 65 61 64 79 20 38 2d 62 79 is already 8-by
1a114 74 65 20 61 6c 69 67 6e 65 64 2c 20 6e 4f 66 66 te aligned, nOff
1a115 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72 6f 2e should be zero.
1a116 0a 20 20 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20 28 . */. nOff = (
1a117 38 20 2d 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 8 - (SQLITE_PTR_
1a118 54 4f 5f 49 4e 54 28 70 53 70 61 63 65 29 20 26 TO_INT(pSpace) &
1a119 20 37 29 29 20 26 20 37 3b 0a 20 20 70 53 70 61 7)) & 7;. pSpa
1a11a 63 65 20 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73 7a ce += nOff;. sz
1a11b 53 70 61 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a 20 Space -= nOff;.
1a11c 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 nByte = ROUND8(
1a11d 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 sizeof(UnpackedR
1a11e 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 ecord)) + sizeof
1a11f 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f 2d (Mem)*(pKeyInfo-
1a120 3e 6e 46 69 65 6c 64 2b 31 29 3b 0a 20 20 69 66 >nField+1);. if
1a121 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65 20 ( nByte>szSpace
1a122 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 ){. p = sqlit
1a123 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4b e3DbMallocRaw(pK
1a124 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 74 eyInfo->db, nByt
1a125 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 e);. if( p==0
1a126 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
1a127 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 p->flags = UNPA
1a128 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 7c CKED_NEED_FREE |
1a129 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 UNPACKED_NEED_D
1a12a 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 7b ESTROY;. }else{
1a12b 0a 20 20 20 20 70 20 3d 20 28 55 6e 70 61 63 6b . p = (Unpack
1a12c 65 64 52 65 63 6f 72 64 2a 29 70 53 70 61 63 65 edRecord*)pSpace
1a12d 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d ;. p->flags =
1a12e 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 UNPACKED_NEED_D
1a12f 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d ESTROY;. }. p-
1a130 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 >pKeyInfo = pKey
1a131 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c Info;. p->nFiel
1a132 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 d = pKeyInfo->nF
1a133 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61 ield + 1;. p->a
1a134 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 28 4d 65 Mem = pMem = (Me
1a135 6d 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b 52 m*)&((char*)p)[R
1a136 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 OUND8(sizeof(Unp
1a137 61 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b 0a ackedRecord))];.
1a138 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
1a139 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 BYTE_ALIGNMENT(p
1a13a 4d 65 6d 29 20 29 3b 0a 20 20 69 64 78 20 3d 20 Mem) );. idx =
1a13b 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 getVarint32(aKey
1a13c 2c 20 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20 , szHdr);. d =
1a13d 73 7a 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a szHdr;. u = 0;.
1a13e 20 20 77 68 69 6c 65 28 20 69 64 78 3c 73 7a 48 while( idx<szH
1a13f 64 72 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c dr && u<p->nFiel
1a140 64 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72 d ){. u32 ser
1a141 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20 69 ial_type;.. i
1a142 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 dx += getVarint3
1a143 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73 65 2(&aKey[idx], se
1a144 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 rial_type);.
1a145 69 66 28 20 64 3e 3d 6e 4b 65 79 20 26 26 20 73 if( d>=nKey && s
1a146 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1a147 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 TypeLen(serial_t
1a148 79 70 65 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a ype)>0 ) break;.
1a149 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
1a14a 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 pKeyInfo->enc;.
1a14b 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b pMem->db = pK
1a14c 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 eyInfo->db;.
1a14d 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b pMem->flags = 0;
1a14e 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c . pMem->zMall
1a14f 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d oc = 0;. d +=
1a150 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1a151 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 alGet(&aKey[d],
1a152 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 serial_type, pMe
1a153 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a m);. pMem++;.
1a154 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 u++;. }. a
1a155 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e ssert( u<=pKeyIn
1a156 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 fo->nField + 1 )
1a157 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 ;. p->nField =
1a158 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 u;. return (voi
1a159 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 d*)p;.}../*.** T
1a15a 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 his routine dest
1a15b 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 roys a UnpackedR
1a15c 65 63 6f 72 64 20 6f 62 6a 65 63 74 0a 2a 2f 0a ecord object.*/.
1a15d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a15e 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
1a15f 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
1a160 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f ord(UnpackedReco
1a161 72 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 rd *p){. if( p
1a162 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c ){. if( p->fl
1a163 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e ags & UNPACKED_N
1a164 45 45 44 5f 44 45 53 54 52 4f 59 20 29 7b 0a 20 EED_DESTROY ){.
1a165 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
1a166 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 Mem *pMem;.
1a167 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d for(i=0, pMem
1a168 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e =p->aMem; i<p->n
1a169 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d Field; i++, pMem
1a16a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ++){. if(
1a16b 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 pMem->zMalloc )
1a16c 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
1a16d 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1a16e 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 e(pMem);.
1a16f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1a170 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 . if( p->flag
1a171 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 s & UNPACKED_NEE
1a172 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 20 20 D_FREE ){.
1a173 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d sqlite3DbFree(p-
1a174 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 >pKeyInfo->db, p
1a175 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
1a176 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1a177 69 6f 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65 ion compares the
1a178 20 74 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 20 two table rows
1a179 6f 72 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 or index records
1a17a 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 .** specified by
1a17b 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 {nKey1, pKey1}
1a17c 61 6e 64 20 70 50 4b 65 79 32 2e 20 20 49 74 20 and pPKey2. It
1a17d 72 65 74 75 72 6e 73 20 61 20 6e 65 67 61 74 69 returns a negati
1a17e 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 ve, zero.** or p
1a17f 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 20 ositive integer
1a180 69 66 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 if key1 is less
1a181 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f than, equal to o
1a182 72 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 r .** greater th
1a183 61 6e 20 6b 65 79 32 2e 20 20 54 68 65 20 7b 6e an key2. The {n
1a184 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 79 Key1, pKey1} key
1a185 20 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 0a must be a blob.
1a186 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74 68 ** created by th
1a187 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f OP_MakeRecord o
1a188 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 pcode of the VDB
1a189 45 2e 20 20 54 68 65 20 70 50 4b 65 79 32 0a 2a E. The pPKey2.*
1a18a 2a 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 * key must be a
1a18b 70 61 72 73 65 64 20 6b 65 79 20 73 75 63 68 20 parsed key such
1a18c 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d as obtained from
1a18d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 50 .** sqlite3VdbeP
1a18e 61 72 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a 2a arseRecord..**.*
1a18f 2a 20 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 * Key1 and Key2
1a190 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 63 do not have to c
1a191 6f 6e 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 ontain the same
1a192 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 number of fields
1a193 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74 ..** The key wit
1a194 68 20 66 65 77 65 72 20 66 69 65 6c 64 73 20 69 h fewer fields i
1a195 73 20 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72 s usually compar
1a196 65 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 es less than the
1a197 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e .** longer key.
1a198 20 20 48 6f 77 65 76 65 72 20 69 66 20 74 68 65 However if the
1a199 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 UNPACKED_INCRKE
1a19a 59 20 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79 Y flags in pPKey
1a19b 32 20 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 20 2 is set.** and
1a19c 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 the common prefi
1a19d 78 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 xes are equal, t
1a19e 68 65 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73 hen key1 is less
1a19f 20 74 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 4f than key2..** O
1a1a0 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 r if the UNPACKE
1a1a1 44 5f 4d 41 54 43 48 5f 50 52 45 46 49 58 20 66 D_MATCH_PREFIX f
1a1a2 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 lag is set and t
1a1a3 68 65 20 70 72 65 66 69 78 65 73 20 61 72 65 0a he prefixes are.
1a1a4 2a 2a 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 74 ** equal, then t
1a1a5 68 65 20 6b 65 79 73 20 61 72 65 20 63 6f 6e 73 he keys are cons
1a1a6 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 idered to be equ
1a1a7 61 6c 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 al and.** the pa
1a1a8 72 74 73 20 62 65 79 6f 6e 64 20 74 68 65 20 63 rts beyond the c
1a1a9 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 65 ommon prefix are
1a1aa 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 ignored..**.**
1a1ab 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f If the UNPACKED_
1a1ac 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c 61 IGNORE_ROWID fla
1a1ad 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 g is set, then t
1a1ae 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a he last byte of.
1a1af 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 ** the header of
1a1b0 20 70 4b 65 79 31 20 69 73 20 69 67 6e 6f 72 65 pKey1 is ignore
1a1b1 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 d. It is assume
1a1b2 64 20 74 68 61 74 20 70 4b 65 79 31 20 69 73 0a d that pKey1 is.
1a1b3 2a 2a 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 2c ** an index key,
1a1b4 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20 77 and thus ends w
1a1b5 69 74 68 20 61 20 72 6f 77 69 64 20 76 61 6c 75 ith a rowid valu
1a1b6 65 2e 20 20 54 68 65 20 6c 61 73 74 20 62 79 74 e. The last byt
1a1b7 65 0a 2a 2a 20 6f 66 20 74 68 65 20 68 65 61 64 e.** of the head
1a1b8 65 72 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 er will therefor
1a1b9 65 20 62 65 20 74 68 65 20 73 65 72 69 61 6c 20 e be the serial
1a1ba 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 type of the rowi
1a1bb 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c 20 d:.** one of 1,
1a1bc 32 2c 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 38 2, 3, 4, 5, 6, 8
1a1bd 2c 20 6f 72 20 39 20 2d 20 74 68 65 20 69 6e 74 , or 9 - the int
1a1be 65 67 65 72 20 73 65 72 69 61 6c 20 74 79 70 65 eger serial type
1a1bf 73 2e 0a 2a 2a 20 54 68 65 20 73 65 72 69 61 6c s..** The serial
1a1c0 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 69 6e type of the fin
1a1c1 61 6c 20 72 6f 77 69 64 20 77 69 6c 6c 20 61 6c al rowid will al
1a1c2 77 61 79 73 20 62 65 20 61 20 73 69 6e 67 6c 65 ways be a single
1a1c3 20 62 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67 6e byte..** By ign
1a1c4 6f 72 69 6e 67 20 74 68 69 73 20 6c 61 73 74 20 oring this last
1a1c5 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 byte of the head
1a1c6 65 72 2c 20 77 65 20 66 6f 72 63 65 20 74 68 65 er, we force the
1a1c7 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 74 comparison.** t
1a1c8 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 72 6f 77 o ignore the row
1a1c9 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 id at the end of
1a1ca 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 key1..*/.SQLITE
1a1cb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1a1cc 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f ite3VdbeRecordCo
1a1cd 6d 70 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b 65 mpare(. int nKe
1a1ce 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a y1, const void *
1a1cf 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20 6b pKey1, /* Left k
1a1d0 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 ey */. Unpacked
1a1d1 52 65 63 6f 72 64 20 2a 70 50 4b 65 79 32 20 20 Record *pPKey2
1a1d2 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6b /* Right k
1a1d3 65 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 64 ey */.){. int d
1a1d4 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1; /*
1a1d5 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65 Offset into aKe
1a1d6 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61 y[] of next data
1a1d7 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 element */. u3
1a1d8 32 20 69 64 78 31 3b 20 20 20 20 20 20 20 20 20 2 idx1;
1a1d9 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
1a1da 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 68 aKey[] of next h
1a1db 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a 2f eader element */
1a1dc 0a 20 20 75 33 32 20 73 7a 48 64 72 31 3b 20 20 . u32 szHdr1;
1a1dd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1a1de 6f 66 20 62 79 74 65 73 20 69 6e 20 68 65 61 64 of bytes in head
1a1df 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 er */. int i =
1a1e0 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0;. int nField;
1a1e1 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 . int rc = 0;.
1a1e2 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
1a1e3 63 68 61 72 20 2a 61 4b 65 79 31 20 3d 20 28 63 char *aKey1 = (c
1a1e4 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1a1e5 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b 65 ar *)pKey1;. Ke
1a1e6 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
1a1e7 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20 20 . Mem mem1;..
1a1e8 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65 79 pKeyInfo = pPKey
1a1e9 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 6d 2->pKeyInfo;. m
1a1ea 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49 6e em1.enc = pKeyIn
1a1eb 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31 2e fo->enc;. mem1.
1a1ec 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 db = pKeyInfo->d
1a1ed 62 3b 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73 20 b;. mem1.flags
1a1ee 3d 20 30 3b 0a 20 20 6d 65 6d 31 2e 75 2e 69 20 = 0;. mem1.u.i
1a1ef 3d 20 30 3b 20 20 2f 2a 20 6e 6f 74 20 6e 65 65 = 0; /* not nee
1a1f0 64 65 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c ded, here to sil
1a1f1 65 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 ence compiler wa
1a1f2 72 6e 69 6e 67 20 2a 2f 0a 20 20 6d 65 6d 31 2e rning */. mem1.
1a1f3 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 0a zMalloc = 0;. .
1a1f4 20 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 idx1 = getVari
1a1f5 6e 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 nt32(aKey1, szHd
1a1f6 72 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 r1);. d1 = szHd
1a1f7 72 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 r1;. if( pPKey2
1a1f8 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b ->flags & UNPACK
1a1f9 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 ED_IGNORE_ROWID
1a1fa 29 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b ){. szHdr1--;
1a1fb 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 . }. nField =
1a1fc 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1a1fd 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c ;. while( idx1<
1a1fe 73 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 szHdr1 && i<pPKe
1a1ff 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 y2->nField ){.
1a200 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
1a201 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 e1;.. /* Read
1a202 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 the serial type
1a203 73 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 s for the next e
1a204 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b lement in each k
1a205 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 ey. */. idx1
1a206 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 += getVarint32(
1a207 61 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 aKey1+idx1, seri
1a208 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 al_type1 );.
1a209 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 if( d1>=nKey1 &&
1a20a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1a20b 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c alTypeLen(serial
1a20c 5f 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 _type1)>0 ) brea
1a20d 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 k;.. /* Extra
1a20e 63 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f ct the values to
1a20f 20 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 be compared..
1a210 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 */. d1 += s
1a211 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1a212 47 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 Get(&aKey1[d1],
1a213 73 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d serial_type1, &m
1a214 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f em1);.. /* Do
1a215 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a the comparison.
1a216 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
1a217 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 sqlite3MemCompar
1a218 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 e(&mem1, &pPKey2
1a219 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 ->aMem[i],.
1a21a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a21b 20 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f i<nField ?
1a21c 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c pKeyInfo->aColl
1a21d 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 [i] : 0);. if
1a21e 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 ( rc!=0 ){.
1a21f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1a220 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 i++;. }. if(
1a221 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 29 20 mem1.zMalloc )
1a222 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1a223 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a 20 lease(&mem1);..
1a224 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46 49 /* If the PREFI
1a225 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69 73 X_SEARCH flag is
1a226 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69 65 set and all fie
1a227 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20 66 lds except the f
1a228 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64 20 inal. ** rowid
1a229 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61 6c field were equal
1a22a 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 65 , then clear the
1a22b 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 66 PREFIX_SEARCH f
1a22c 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20 2a lag and set . *
1a22d 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 20 * pPKey2->rowid
1a22e 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 to the value of
1a22f 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 20 the rowid field
1a230 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79 31 in (pKey1, nKey1
1a231 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 ).. ** This is
1a232 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f 49 used by the OP_I
1a233 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e 0a sUnique opcode..
1a234 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b 65 */. if( (pPKe
1a235 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 y2->flags & UNPA
1a236 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 CKED_PREFIX_SEAR
1a237 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65 79 CH) && i==(pPKey
1a238 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b 0a 2->nField-1) ){.
1a239 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 31 assert( idx1
1a23a 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20 29 ==szHdr1 && rc )
1a23b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 ;. assert( me
1a23c 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 m1.flags & MEM_I
1a23d 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79 32 nt );. pPKey2
1a23e 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50 41 ->flags &= ~UNPA
1a23f 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 CKED_PREFIX_SEAR
1a240 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d 3e CH;. pPKey2->
1a241 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e 69 rowid = mem1.u.i
1a242 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d ;. }.. if( rc=
1a243 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63 3d =0 ){. /* rc=
1a244 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74 68 =0 here means th
1a245 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b 65 at one of the ke
1a246 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66 69 ys ran out of fi
1a247 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 elds and. **
1a248 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20 75 all the fields u
1a249 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74 20 p to that point
1a24a 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20 74 were equal. If t
1a24b 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 he UNPACKED_INCR
1a24c 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 KEY. ** flag
1a24d 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72 65 is set, then bre
1a24e 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74 72 ak the tie by tr
1a24f 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20 6c eating key2 as l
1a250 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49 66 arger.. ** If
1a251 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52 45 the UPACKED_PRE
1a252 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20 69 FIX_MATCH flag i
1a253 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79 73 s set, then keys
1a254 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72 65 with common pre
1a255 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72 65 fixes. ** are
1a256 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 considered to b
1a257 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72 77 e equal. Otherw
1a258 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72 20 ise, the longer
1a259 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20 20 key is the .
1a25a 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20 69 ** larger. As i
1a25b 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 70 t happens, the p
1a25c 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 79 PKey2 will alway
1a25d 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 0a s be the longer.
1a25e 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 ** if there
1a25f 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 2e is a difference.
1a260 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1a261 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 pPKey2->flags &
1a262 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
1a263 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 2d ){. rc = -
1a264 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 1;. }else if(
1a265 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 pPKey2->flags &
1a266 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 UNPACKED_PREFIX
1a267 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20 20 _MATCH ){.
1a268 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20 2a /* Leave rc==0 *
1a269 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 /. }else if(
1a26a 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a 20 idx1<szHdr1 ){.
1a26b 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 rc = 1;.
1a26c 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }. }else if( p
1a26d 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 KeyInfo->aSortOr
1a26e 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e 66 der && i<pKeyInf
1a26f 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20 20 o->nField.
1a270 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65 79 && pKey
1a271 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
1a272 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 [i] ){. rc =
1a273 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 -rc;. }.. retu
1a274 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a rn rc;.}. ../*.*
1a275 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74 * pCur points at
1a276 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 20 an index entry
1a277 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 created using th
1a278 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 e OP_MakeRecord
1a279 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64 20 opcode..** Read
1a27a 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20 6c the rowid (the l
1a27b 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68 65 ast field in the
1a27c 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74 6f record) and sto
1a27d 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64 2e re it in *rowid.
1a27e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
1a27f 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 E_OK if everythi
1a280 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e 20 ng works, or an
1a281 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 72 error code other
1a282 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 wise..**.** pCur
1a283 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74 69 might be pointi
1a284 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61 69 ng to text obtai
1a285 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72 75 ned from a corru
1a286 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 pt database file
1a287 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e 74 ..** So the cont
1a288 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 ent cannot be tr
1a289 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72 6f usted. Do appro
1a28a 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f 6e priate checks on
1a28b 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f the content..*/
1a28c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a28d 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 int sqlite3VdbeI
1a28e 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 dxRowid(BtCursor
1a28f 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 *pCur, i64 *row
1a290 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c id){. i64 nCell
1a291 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 Key = 0;. int r
1a292 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 c;. u32 szHdr;
1a293 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1a294 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a f the header */.
1a295 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b u32 typeRowid;
1a296 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 /* Serial ty
1a297 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 pe of the rowid
1a298 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 */. u32 lenRowi
1a299 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f d; /* Size o
1a29a 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 f the rowid */.
1a29b 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 2f 2a Mem m, v;.. /*
1a29c 20 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f 66 Get the size of
1a29d 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 the index entry
1a29e 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 20 . Only indices
1a29f 65 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73 0a entries of less.
1a2a0 20 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 61 ** than 2GiB a
1a2a1 72 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e 79 re support - any
1a2a2 74 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 74 thing large must
1a2a3 20 62 65 20 64 61 74 61 62 61 73 65 20 63 6f 72 be database cor
1a2a4 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c ruption */. sql
1a2a5 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
1a2a6 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 (pCur, &nCellKey
1a2a7 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c );. if( unlikel
1a2a8 79 28 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c y(nCellKey<=0 ||
1a2a9 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 nCellKey>0x7fff
1a2aa 66 66 66 66 29 20 29 7b 0a 20 20 20 20 72 65 74 ffff) ){. ret
1a2ab 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
1a2ac 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 PT_BKPT;. }..
1a2ad 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 63 /* Read in the c
1a2ae 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 omplete content
1a2af 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 of the index ent
1a2b0 72 79 20 2a 2f 0a 20 20 6d 2e 66 6c 61 67 73 20 ry */. m.flags
1a2b1 3d 20 30 3b 0a 20 20 6d 2e 64 62 20 3d 20 30 3b = 0;. m.db = 0;
1a2b2 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 . m.zMalloc = 0
1a2b3 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1a2b4 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 VdbeMemFromBtree
1a2b5 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 6e (pCur, 0, (int)n
1a2b6 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b CellKey, 1, &m);
1a2b7 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
1a2b8 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
1a2b9 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 . /* The index
1a2ba 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69 6e entry must begin
1a2bb 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20 73 with a header s
1a2bc 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 ize */. (void)g
1a2bd 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 etVarint32((u8*)
1a2be 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20 74 m.z, szHdr);. t
1a2bf 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d 3d estcase( szHdr==
1a2c0 32 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 2 );. testcase(
1a2c1 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 szHdr==m.n );.
1a2c2 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73 7a if( unlikely(sz
1a2c3 48 64 72 3c 32 20 7c 7c 20 28 69 6e 74 29 73 7a Hdr<2 || (int)sz
1a2c4 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 20 Hdr>m.n) ){.
1a2c5 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 goto idx_rowid_c
1a2c6 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a orruption;. }..
1a2c7 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66 69 /* The last fi
1a2c8 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 eld of the index
1a2c9 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69 6e should be an in
1a2ca 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57 49 teger - the ROWI
1a2cb 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20 74 D.. ** Verify t
1a2cc 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74 hat the last ent
1a2cd 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e 20 ry really is an
1a2ce 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28 76 integer. */. (v
1a2cf 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 28 oid)getVarint32(
1a2d0 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d (u8*)&m.z[szHdr-
1a2d1 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b 0a 1], typeRowid);.
1a2d2 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 testcase( type
1a2d3 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74 65 Rowid==1 );. te
1a2d4 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 stcase( typeRowi
1a2d5 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63 61 d==2 );. testca
1a2d6 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 33 se( typeRowid==3
1a2d7 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
1a2d8 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b 0a typeRowid==4 );.
1a2d9 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 testcase( type
1a2da 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74 65 Rowid==5 );. te
1a2db 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 stcase( typeRowi
1a2dc 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63 61 d==6 );. testca
1a2dd 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 38 se( typeRowid==8
1a2de 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
1a2df 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b 0a typeRowid==9 );.
1a2e0 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 74 if( unlikely(t
1a2e1 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74 79 ypeRowid<1 || ty
1a2e2 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79 70 peRowid>9 || typ
1a2e3 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20 20 eRowid==7) ){.
1a2e4 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 goto idx_rowid
1a2e5 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d _corruption;. }
1a2e6 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 . lenRowid = sq
1a2e7 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1a2e8 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69 64 ypeLen(typeRowid
1a2e9 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d );. testcase( m
1a2ea 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3d 3d 73 7a 48 .n-lenRowid==szH
1a2eb 64 72 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 dr );. if( unli
1a2ec 6b 65 6c 79 28 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 kely(m.n-lenRowi
1a2ed 64 3c 73 7a 48 64 72 29 20 29 7b 0a 20 20 20 20 d<szHdr) ){.
1a2ee 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 goto idx_rowid_c
1a2ef 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a orruption;. }..
1a2f0 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 /* Fetch the i
1a2f1 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65 20 65 nteger off the e
1a2f2 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 nd of the index
1a2f3 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 record */. sqli
1a2f4 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 te3VdbeSerialGet
1a2f5 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c ((u8*)&m.z[m.n-l
1a2f6 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f enRowid], typeRo
1a2f7 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77 wid, &v);. *row
1a2f8 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71 id = v.u.i;. sq
1a2f9 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1a2fa 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 ase(&m);. retur
1a2fb 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 n SQLITE_OK;..
1a2fc 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 /* Jump here if
1a2fd 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
1a2fe 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20 ion is detected
1a2ff 61 66 74 65 72 20 6d 20 68 61 73 20 62 65 65 6e after m has been
1a300 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2e . ** allocated.
1a301 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f 62 6a Free the m obj
1a302 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20 53 ect and return S
1a303 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20 2a QLITE_CORRUPT. *
1a304 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 /.idx_rowid_corr
1a305 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74 63 61 uption:. testca
1a306 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 se( m.zMalloc!=0
1a307 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1a308 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b eMemRelease(&m);
1a309 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1a30a 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 7d _CORRUPT_BKPT;.}
1a30b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 ../*.** Compare
1a30c 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 69 the key of the i
1a30d 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74 20 ndex entry that
1a30e 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f 69 cursor pC is poi
1a30f 6e 74 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a nt to against.**
1a310 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20 the key string
1a311 69 6e 20 70 4b 65 79 20 28 6f 66 20 6c 65 6e 67 in pKey (of leng
1a312 74 68 20 6e 4b 65 79 29 2e 20 20 57 72 69 74 65 th nKey). Write
1a313 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e 75 into *pRes a nu
1a314 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 mber.** that is
1a315 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 negative, zero,
1a316 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 or positive if p
1a317 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 C is less than,
1a318 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 equal to,.** or
1a319 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 4b 65 greater than pKe
1a31a 79 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 y. Return SQLIT
1a31b 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e E_OK on success.
1a31c 0a 2a 2a 0a 2a 2a 20 70 4b 65 79 20 69 73 20 65 .**.** pKey is e
1a31d 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77 69 ither created wi
1a31e 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f 72 thout a rowid or
1a31f 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73 6f is truncated so
1a320 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 that it.** omit
1a321 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 s the rowid at t
1a322 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f 77 he end. The row
1a323 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 id at the end of
1a324 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 the index entry
1a325 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20 61 .** is ignored a
1a326 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 s well. Hence,
1a327 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c this routine onl
1a328 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 70 y compares the p
1a329 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20 74 refixes .** of t
1a32a 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74 6f he keys prior to
1a32b 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 the final rowid
1a32c 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72 65 , not the entire
1a32d 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 key..**.** pUnp
1a32e 61 63 6b 65 64 20 6d 61 79 20 62 65 20 61 6e 20 acked may be an
1a32f 75 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e unpacked version
1a330 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 of pKey,nKey.
1a331 49 66 20 70 55 6e 70 61 63 6b 65 64 20 69 73 0a If pUnpacked is.
1a332 2a 2a 20 73 75 70 70 6c 69 65 64 20 69 74 20 69 ** supplied it i
1a333 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 s used in place
1a334 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 0a 2a 2f of pKey,nKey..*/
1a335 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a336 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 int sqlite3VdbeI
1a337 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20 dxKeyCompare(.
1a338 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c 20 VdbeCursor *pC,
1a339 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1a33a 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d he cursor to com
1a33b 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a pare against */.
1a33c 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1a33d 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f 2a *pUnpacked, /*
1a33e 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f Unpacked versio
1a33f 6e 20 6f 66 20 70 4b 65 79 20 61 6e 64 20 6e 4b n of pKey and nK
1a340 65 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73 ey */. int *res
1a341 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a342 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
1a343 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73 75 comparison resu
1a344 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 lt here */.){.
1a345 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 i64 nCellKey = 0
1a346 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 ;. int rc;. Bt
1a347 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 Cursor *pCur = p
1a348 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65 C->pCursor;. Me
1a349 6d 20 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 m m;.. sqlite3B
1a34a 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 treeKeySize(pCur
1a34b 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 , &nCellKey);.
1a34c 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 if( nCellKey<=0
1a34d 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66 || nCellKey>0x7f
1a34e 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 2a 72 ffffff ){. *r
1a34f 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 es = 0;. retu
1a350 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1a351 7d 0a 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 }. m.db = 0;.
1a352 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d m.flags = 0;. m
1a353 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 .zMalloc = 0;.
1a354 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1a355 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 2d MemFromBtree(pC-
1a356 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 6e >pCursor, 0, (in
1a357 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 t)nCellKey, 1, &
1a358 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a m);. if( rc ){.
1a359 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1a35a 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e }. assert( pUn
1a35b 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20 packed->flags &
1a35c 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
1a35d 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20 ROWID );. *res
1a35e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
1a35f 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 ordCompare(m.n,
1a360 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b m.z, pUnpacked);
1a361 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1a362 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 mRelease(&m);.
1a363 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1a364 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
1a365 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 routine sets the
1a366 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74 value to be ret
1a367 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 urned by subsequ
1a368 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 ent calls to.**
1a369 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 sqlite3_changes(
1a36a 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 ) on the databas
1a36b 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a e handle 'db'. .
1a36c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a36d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a36e 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c beSetChanges(sql
1a36f 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43 ite3 *db, int nC
1a370 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74 hange){. assert
1a371 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1a372 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
1a373 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 );. db->nChange
1a374 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 = nChange;. db
1a375 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b ->nTotalChange +
1a376 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a = nChange;.}../*
1a377 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 .** Set a flag i
1a378 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 n the vdbe to up
1a379 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 date the change
1a37a 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 counter when it
1a37b 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 is finalised.**
1a37c 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c or reset..*/.SQL
1a37d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1a37e 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e sqlite3VdbeCoun
1a37f 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 tChanges(Vdbe *v
1a380 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e ){. v->changeCn
1a381 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a tOn = 1;.}../*.*
1a382 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 * Mark every pre
1a383 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
1a384 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1a385 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 a database conne
1a386 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 ction.** as expi
1a387 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 red..**.** An ex
1a388 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pired statement
1a389 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d means that recom
1a38a 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 pilation of the
1a38b 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 statement is.**
1a38c 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 recommend. Stat
1a38d 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 ements expire wh
1a38e 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e en things happen
1a38f 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 that make their
1a390 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 .** programs obs
1a391 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 olete. Removing
1a392 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 user-defined fu
1a393 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 nctions or colla
1a394 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 ting.** sequence
1a395 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 s, or changing a
1a396 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 n authorization
1a397 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 function are the
1a398 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 types of.** thi
1a399 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 ngs that make pr
1a39a 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1a39b 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 s obsolete..*/.S
1a39c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a39d 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 id sqlite3Expire
1a39e 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
1a39f 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b ts(sqlite3 *db){
1a3a0 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f . Vdbe *p;. fo
1a3a1 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b r(p = db->pVdbe;
1a3a2 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
1a3a3 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 . p->expired
1a3a4 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 1;. }.}../*.*
1a3a5 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 * Return the dat
1a3a6 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 abase associated
1a3a7 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a with the Vdbe..
1a3a8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a3a9 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 E sqlite3 *sqlit
1a3aa 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 e3VdbeDb(Vdbe *v
1a3ab 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 ){. return v->d
1a3ac 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a b;.}../*********
1a3ad 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1a3ae 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eaux.c *********
1a3af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3b1 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1a3b2 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1a3b3 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a vdbeapi.c *****
1a3b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3b6 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
1a3b7 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 May 26.**.** Th
1a3b8 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1a3b9 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1a3ba 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1a3bb 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1a3bc 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1a3bd 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1a3be 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1a3bf 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1a3c0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1a3c1 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1a3c2 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1a3c3 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1a3c4 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1a3c5 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1a3c6 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1a3c7 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1a3c8 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1a3c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a3cd 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
1a3ce 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1a3cf 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c code use to impl
1a3d0 65 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 ement APIs that
1a3d1 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a are part of the.
1a3d2 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 ** VDBE..**.** $
1a3d3 49 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20 Id: vdbeapi.c,v
1a3d4 31 2e 31 36 34 20 32 30 30 39 2f 30 34 2f 32 37 1.164 2009/04/27
1a3d5 20 31 38 3a 34 36 3a 30 36 20 64 72 68 20 45 78 18:46:06 drh Ex
1a3d6 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 30 20 26 26 p $.*/..#if 0 &&
1a3d7 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1a3d8 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
1a3d9 4e 41 47 45 4d 45 4e 54 29 0a 2f 2a 0a 2a 2a 20 NAGEMENT)./*.**
1a3da 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 The following st
1a3db 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
1a3dc 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
1a3dd 20 65 6e 64 20 70 6f 69 6e 74 73 20 6f 66 20 61 end points of a
1a3de 0a 2a 2a 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 .** doubly-linke
1a3df 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f d list of all co
1a3e0 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 mpiled SQL state
1a3e1 6d 65 6e 74 73 20 74 68 61 74 20 6d 61 79 20 62 ments that may b
1a3e2 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 62 75 66 e holding.** buf
1a3e3 66 65 72 73 20 65 6c 69 67 69 62 6c 65 20 66 6f fers eligible fo
1a3e4 72 20 72 65 6c 65 61 73 65 20 77 68 65 6e 20 74 r release when t
1a3e5 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 he sqlite3_relea
1a3e6 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65 se_memory() inte
1a3e7 72 66 61 63 65 20 69 73 0a 2a 2a 20 69 6e 76 6f rface is.** invo
1a3e8 6b 65 64 2e 20 41 63 63 65 73 73 20 74 6f 20 74 ked. Access to t
1a3e9 68 69 73 20 6c 69 73 74 20 69 73 20 70 72 6f 74 his list is prot
1a3ea 65 63 74 65 64 20 62 79 20 74 68 65 20 53 51 4c ected by the SQL
1a3eb 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
1a3ec 5f 4c 52 55 32 0a 2a 2a 20 6d 75 74 65 78 2e 0a _LRU2.** mutex..
1a3ed 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 73 **.** Statements
1a3ee 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 are added to th
1a3ef 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 6c 69 e end of this li
1a3f0 73 74 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f st when sqlite3_
1a3f1 72 65 73 65 74 28 29 20 69 73 0a 2a 2a 20 63 61 reset() is.** ca
1a3f2 6c 6c 65 64 2e 20 54 68 65 79 20 61 72 65 20 72 lled. They are r
1a3f3 65 6d 6f 76 65 64 20 65 69 74 68 65 72 20 77 68 emoved either wh
1a3f4 65 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 en sqlite3_step(
1a3f5 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e ) or sqlite3_fin
1a3f6 61 6c 69 7a 65 28 29 0a 2a 2a 20 69 73 20 63 61 alize().** is ca
1a3f7 6c 6c 65 64 2e 20 57 68 65 6e 20 73 74 61 74 65 lled. When state
1a3f8 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 ments are added
1a3f9 74 6f 20 74 68 69 73 20 6c 69 73 74 2c 20 74 68 to this list, th
1a3fa 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a e associated .**
1a3fb 20 72 65 67 69 73 74 65 72 20 61 72 72 61 79 20 register array
1a3fc 28 70 2d 3e 61 4d 65 6d 5b 31 2e 2e 70 2d 3e 6e (p->aMem[1..p->n
1a3fd 4d 65 6d 5d 29 20 6d 61 79 20 63 6f 6e 74 61 69 Mem]) may contai
1a3fe 6e 20 64 79 6e 61 6d 69 63 20 62 75 66 66 65 72 n dynamic buffer
1a3ff 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 s that.** can be
1a400 20 66 72 65 65 64 20 75 73 69 6e 67 20 73 71 6c freed using sql
1a401 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d ite3VdbeReleaseM
1a402 65 6d 6f 72 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 emory()..**.** W
1a403 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 20 61 hen statements a
1a404 72 65 20 61 64 64 65 64 20 6f 72 20 72 65 6d 6f re added or remo
1a405 76 65 64 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 ved from this li
1a406 73 74 2c 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a st, the mutex.**
1a407 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1a408 20 74 68 65 20 56 64 62 65 20 62 65 69 6e 67 20 the Vdbe being
1a409 61 64 64 65 64 20 6f 72 20 72 65 6d 6f 76 65 64 added or removed
1a40a 20 28 56 64 62 65 2e 64 62 2d 3e 6d 75 74 65 78 (Vdbe.db->mutex
1a40b 29 20 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 ) is.** already
1a40c 68 65 6c 64 2e 20 54 68 65 20 4c 52 55 32 20 6d held. The LRU2 m
1a40d 75 74 65 78 20 69 73 20 74 68 65 6e 20 6f 62 74 utex is then obt
1a40e 61 69 6e 65 64 2c 20 62 6c 6f 63 6b 69 6e 67 20 ained, blocking
1a40f 69 66 20 6e 65 63 65 73 73 61 72 79 2c 0a 2a 2a if necessary,.**
1a410 20 74 68 65 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 the linked-list
1a411 20 70 6f 69 6e 74 65 72 73 20 6d 61 6e 69 70 75 pointers manipu
1a412 6c 61 74 65 64 20 61 6e 64 20 74 68 65 20 4c 52 lated and the LR
1a413 55 32 20 6d 75 74 65 78 20 72 65 6c 69 6e 71 75 U2 mutex relinqu
1a414 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 ished..*/.struct
1a415 20 53 74 61 74 65 6d 65 6e 74 4c 72 75 4c 69 73 StatementLruLis
1a416 74 20 7b 0a 20 20 56 64 62 65 20 2a 70 46 69 72 t {. Vdbe *pFir
1a417 73 74 3b 0a 20 20 56 64 62 65 20 2a 70 4c 61 73 st;. Vdbe *pLas
1a418 74 3b 0a 7d 3b 0a 73 74 61 74 69 63 20 73 74 72 t;.};.static str
1a419 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c 72 75 uct StatementLru
1a41a 4c 69 73 74 20 73 71 6c 69 74 65 33 4c 72 75 53 List sqlite3LruS
1a41b 74 61 74 65 6d 65 6e 74 73 3b 0a 0a 2f 2a 0a 2a tatements;../*.*
1a41c 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
1a41d 20 6c 69 73 74 20 6c 6f 6f 6b 73 20 74 6f 20 62 list looks to b
1a41e 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e e internally con
1a41f 73 69 73 74 65 6e 74 2e 20 54 68 69 73 20 69 73 sistent. This is
1a420 20 75 73 65 64 0a 2a 2a 20 61 73 20 70 61 72 74 used.** as part
1a421 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29 20 of an assert()
1a422 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c statement as fol
1a423 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 lows:.**.** as
1a424 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 sert( stmtLruChe
1a425 63 6b 28 29 20 29 3b 0a 2a 2f 0a 23 69 66 6e 64 ck() );.*/.#ifnd
1a426 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 ef NDEBUG.static
1a427 20 69 6e 74 20 73 74 6d 74 4c 72 75 43 68 65 63 int stmtLruChec
1a428 6b 28 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a k(){. Vdbe *p;.
1a429 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 4c for(p=sqlite3L
1a42a 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 ruStatements.pFi
1a42b 72 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c 72 rst; p; p=p->pLr
1a42c 75 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 uNext){. asse
1a42d 72 74 28 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c rt(p->pLruNext |
1a42e 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 53 | p==sqlite3LruS
1a42f 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 29 tatements.pLast)
1a430 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70 2d ;. assert(!p-
1a431 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 2d 3e >pLruNext || p->
1a432 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72 pLruNext->pLruPr
1a433 65 76 3d 3d 70 29 3b 0a 20 20 20 20 61 73 73 65 ev==p);. asse
1a434 72 74 28 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c rt(p->pLruPrev |
1a435 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 53 | p==sqlite3LruS
1a436 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 tatements.pFirst
1a437 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70 );. assert(!p
1a438 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d ->pLruPrev || p-
1a439 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e >pLruPrev->pLruN
1a43a 65 78 74 3d 3d 70 29 3b 0a 20 20 7d 0a 20 20 72 ext==p);. }. r
1a43b 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 eturn 1;.}.#endi
1a43c 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 76 64 62 f../*.** Add vdb
1a43d 65 20 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f e p to the end o
1a43e 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
1a43f 6c 72 75 20 6c 69 73 74 2e 20 49 74 20 69 73 20 lru list. It is
1a440 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 assumed that.**
1a441 70 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 p is not already
1a442 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 part of the lis
1a443 74 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 t when this is c
1a444 61 6c 6c 65 64 2e 20 54 68 65 20 6c 72 75 20 6c alled. The lru l
1a445 69 73 74 0a 2a 2a 20 69 73 20 70 72 6f 74 65 63 ist.** is protec
1a446 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 ted by the SQLIT
1a447 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
1a448 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 RU mutex..*/.sta
1a449 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72 75 tic void stmtLru
1a44a 41 64 64 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 Add(Vdbe *p){.
1a44b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1a44c 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ter(sqlite3Mutex
1a44d 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
1a44e 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 EX_STATIC_LRU2))
1a44f 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 ;.. if( p->pLru
1a450 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e Prev || p->pLruN
1a451 65 78 74 20 7c 7c 20 73 71 6c 69 74 65 33 4c 72 ext || sqlite3Lr
1a452 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 uStatements.pFir
1a453 73 74 3d 3d 70 20 29 7b 0a 20 20 20 20 73 71 6c st==p ){. sql
1a454 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1a455 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
1a456 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
1a457 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20 STATIC_LRU2));.
1a458 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a return;. }..
1a459 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 assert( stmtLr
1a45a 75 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 69 uCheck() );.. i
1a45b 66 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53 74 f( !sqlite3LruSt
1a45c 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 atements.pFirst
1a45d 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
1a45e 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
1a45f 65 6e 74 73 2e 70 4c 61 73 74 20 29 3b 0a 20 20 ents.pLast );.
1a460 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 sqlite3LruStat
1a461 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 3d 20 ements.pFirst =
1a462 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 p;. sqlite3Lr
1a463 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 uStatements.pLas
1a464 74 20 3d 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a t = p;. }else{.
1a465 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c assert( !sql
1a466 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
1a467 73 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 78 s.pLast->pLruNex
1a468 74 20 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 75 t );. p->pLru
1a469 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 4c 72 Prev = sqlite3Lr
1a46a 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 uStatements.pLas
1a46b 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 t;. sqlite3Lr
1a46c 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 uStatements.pLas
1a46d 74 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 3b t->pLruNext = p;
1a46e 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53 . sqlite3LruS
1a46f 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 tatements.pLast
1a470 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 = p;. }.. asse
1a471 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b rt( stmtLruCheck
1a472 28 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 () );.. sqlite3
1a473 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c _mutex_leave(sql
1a474 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
1a475 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
1a476 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a IC_LRU2));.}../*
1a477 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 .** Assuming the
1a478 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
1a479 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65 78 74 ATIC_LRU2 mutext
1a47a 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 is already held
1a47b 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 74 , remove.** stat
1a47c 65 6d 65 6e 74 20 70 20 66 72 6f 6d 20 74 68 65 ement p from the
1a47d 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d least-recently-
1a47e 75 73 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6c used statement l
1a47f 69 73 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 ist. If the .**
1a480 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 statement is not
1a481 20 63 75 72 72 65 6e 74 6c 79 20 70 61 72 74 20 currently part
1a482 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69 of the list, thi
1a483 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f s call is a no-o
1a484 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 p..*/.static voi
1a485 64 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e d stmtLruRemoveN
1a486 6f 6d 75 74 65 78 28 56 64 62 65 20 2a 70 29 7b omutex(Vdbe *p){
1a487 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 50 72 . if( p->pLruPr
1a488 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65 78 ev || p->pLruNex
1a489 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c t || p==sqlite3L
1a48a 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 ruStatements.pFi
1a48b 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 rst ){. asser
1a48c 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 t( stmtLruCheck(
1a48d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e ) );. if( p->
1a48e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 20 pLruNext ){.
1a48f 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 p->pLruNext->p
1a490 4c 72 75 50 72 65 76 20 3d 20 70 2d 3e 70 4c 72 LruPrev = p->pLr
1a491 75 50 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 uPrev;. }else
1a492 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c {. sqlite3L
1a493 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 ruStatements.pLa
1a494 73 74 20 3d 20 70 2d 3e 70 4c 72 75 50 72 65 76 st = p->pLruPrev
1a495 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1a496 70 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a 20 p->pLruPrev ){.
1a497 20 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 76 p->pLruPrev
1a498 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 2d 3e ->pLruNext = p->
1a499 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 65 pLruNext;. }e
1a49a 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
1a49b 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e e3LruStatements.
1a49c 70 46 69 72 73 74 20 3d 20 70 2d 3e 70 4c 72 75 pFirst = p->pLru
1a49d 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
1a49e 70 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b p->pLruNext = 0;
1a49f 0a 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 76 . p->pLruPrev
1a4a0 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 = 0;. assert
1a4a1 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 ( stmtLruCheck()
1a4a2 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a );. }.}../*.**
1a4a3 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 53 51 Assuming the SQ
1a4a4 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
1a4a5 43 5f 4c 52 55 32 20 6d 75 74 65 78 74 20 69 73 C_LRU2 mutext is
1a4a6 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 6d 6f 76 not held, remov
1a4a7 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 e.** statement p
1a4a8 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 73 74 2d from the least-
1a4a9 72 65 63 65 6e 74 6c 79 2d 75 73 65 64 20 73 74 recently-used st
1a4aa 61 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20 49 66 atement list. If
1a4ab 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 the .** stateme
1a4ac 6e 74 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e nt is not curren
1a4ad 74 6c 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 tly part of the
1a4ae 6c 69 73 74 2c 20 74 68 69 73 20 63 61 6c 6c 20 list, this call
1a4af 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 is a no-op..*/.s
1a4b0 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c tatic void stmtL
1a4b1 72 75 52 65 6d 6f 76 65 28 56 64 62 65 20 2a 70 ruRemove(Vdbe *p
1a4b2 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
1a4b3 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
1a4b4 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
1a4b5 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
1a4b6 52 55 32 29 29 3b 0a 20 20 73 74 6d 74 4c 72 75 RU2));. stmtLru
1a4b7 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 70 29 RemoveNomutex(p)
1a4b8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1a4b9 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d x_leave(sqlite3M
1a4ba 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
1a4bb 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
1a4bc 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 U2));.}../*.** T
1a4bd 72 79 20 74 6f 20 72 65 6c 65 61 73 65 20 6e 20 ry to release n
1a4be 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
1a4bf 62 79 20 66 72 65 65 69 6e 67 20 62 75 66 66 65 by freeing buffe
1a4c0 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a rs associated .*
1a4c1 2a 20 77 69 74 68 20 74 68 65 20 6d 65 6d 6f 72 * with the memor
1a4c2 79 20 72 65 67 69 73 74 65 72 73 20 6f 66 20 63 y registers of c
1a4c3 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 20 urrently unused
1a4c4 76 64 62 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 vdbes..*/.SQLITE
1a4c5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1a4c6 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d ite3VdbeReleaseM
1a4c7 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 20 20 emory(int n){.
1a4c8 56 64 62 65 20 2a 70 3b 0a 20 20 56 64 62 65 20 Vdbe *p;. Vdbe
1a4c9 2a 70 4e 65 78 74 3b 0a 20 20 69 6e 74 20 6e 46 *pNext;. int nF
1a4ca 72 65 65 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 ree = 0;.. sqli
1a4cb 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1a4cc 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
1a4cd 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
1a4ce 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20 20 TATIC_LRU2));.
1a4cf 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 4c 72 75 for(p=sqlite3Lru
1a4d0 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 Statements.pFirs
1a4d1 74 3b 20 70 20 26 26 20 6e 46 72 65 65 3c 6e 3b t; p && nFree<n;
1a4d2 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 p=pNext){. p
1a4d3 4e 65 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65 Next = p->pLruNe
1a4d4 78 74 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f 72 20 xt;.. /* For
1a4d5 65 61 63 68 20 73 74 61 74 65 6d 65 6e 74 20 68 each statement h
1a4d6 61 6e 64 6c 65 20 69 6e 20 74 68 65 20 6c 72 75 andle in the lru
1a4d7 20 6c 69 73 74 2c 20 61 74 74 65 6d 70 74 20 74 list, attempt t
1a4d8 6f 20 6f 62 74 61 69 6e 20 74 68 65 0a 20 20 20 o obtain the.
1a4d9 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 64 ** associated d
1a4da 61 74 61 62 61 73 65 20 6d 75 74 65 78 2e 20 49 atabase mutex. I
1a4db 66 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 6f f it cannot be o
1a4dc 62 74 61 69 6e 65 64 2c 20 63 6f 6e 74 69 6e 75 btained, continu
1a4dd 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 e. ** to the
1a4de 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20 68 next statement h
1a4df 61 6e 64 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74 andle. It is not
1a4e0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 62 6c 6f possible to blo
1a4e1 63 6b 20 6f 6e 0a 20 20 20 20 2a 2a 20 74 68 65 ck on. ** the
1a4e2 20 64 61 74 61 62 61 73 65 20 6d 75 74 65 78 20 database mutex
1a4e3 2d 20 74 68 61 74 20 63 6f 75 6c 64 20 63 61 75 - that could cau
1a4e4 73 65 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 20 se deadlock..
1a4e5 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 */. if( SQLI
1a4e6 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 5f 6d TE_OK==sqlite3_m
1a4e7 75 74 65 78 5f 74 72 79 28 70 2d 3e 64 62 2d 3e utex_try(p->db->
1a4e8 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 20 20 mutex) ){.
1a4e9 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 nFree += sqlite3
1a4ea 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 VdbeReleaseBuffe
1a4eb 72 73 28 70 29 3b 0a 20 20 20 20 20 20 73 74 6d rs(p);. stm
1a4ec 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 tLruRemoveNomute
1a4ed 78 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 x(p);. sqli
1a4ee 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1a4ef 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1a4f0 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
1a4f1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
1a4f2 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
1a4f3 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
1a4f4 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 20 ATIC_LRU2));..
1a4f5 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a return nFree;.}.
1a4f6 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 ./*.** Call sqli
1a4f7 74 65 33 52 65 70 72 65 70 61 72 65 28 29 20 6f te3Reprepare() o
1a4f8 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e n the statement.
1a4f9 20 52 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d 20 Remove it from
1a4fa 74 68 65 0a 2a 2a 20 6c 72 75 20 6c 69 73 74 20 the.** lru list
1a4fb 62 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c before doing so,
1a4fc 20 61 73 20 52 65 70 72 65 70 61 72 65 28 29 20 as Reprepare()
1a4fd 77 69 6c 6c 20 66 72 65 65 20 61 6c 6c 20 74 68 will free all th
1a4fe 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 67 69 e.** memory regi
1a4ff 73 74 65 72 20 62 75 66 66 65 72 73 20 61 6e 79 ster buffers any
1a500 77 61 79 2e 0a 2a 2f 0a 69 6e 74 20 76 64 62 65 way..*/.int vdbe
1a501 52 65 70 72 65 70 61 72 65 28 56 64 62 65 20 2a Reprepare(Vdbe *
1a502 70 29 7b 0a 20 20 73 74 6d 74 4c 72 75 52 65 6d p){. stmtLruRem
1a503 6f 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ove(p);. return
1a504 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 sqlite3Reprepar
1a505 65 28 70 29 3b 0a 7d 0a 0a 23 65 6c 73 65 20 20 e(p);.}..#else
1a506 20 20 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 5f /* !SQLITE_
1a507 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
1a508 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 20 20 23 64 NAGEMENT */. #d
1a509 65 66 69 6e 65 20 73 74 6d 74 4c 72 75 52 65 6d efine stmtLruRem
1a50a 6f 76 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 ove(x). #define
1a50b 20 73 74 6d 74 4c 72 75 41 64 64 28 78 29 0a 20 stmtLruAdd(x).
1a50c 20 23 64 65 66 69 6e 65 20 76 64 62 65 52 65 70 #define vdbeRep
1a50d 72 65 70 61 72 65 28 78 29 20 73 71 6c 69 74 65 repare(x) sqlite
1a50e 33 52 65 70 72 65 70 61 72 65 28 78 29 0a 23 65 3Reprepare(x).#e
1a50f 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 ndif...#ifndef S
1a510 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 QLITE_OMIT_DEPRE
1a511 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 CATED./*.** Retu
1a512 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 rn TRUE (non-zer
1a513 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d o) of the statem
1a514 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 ent supplied as
1a515 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 an argument need
1a516 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d s.** to be recom
1a517 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d piled. A statem
1a518 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ent needs to be
1a519 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 recompiled whene
1a51a 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 ver the.** execu
1a51b 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 tion environment
1a51c 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 changes in a wa
1a51d 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 y that would alt
1a51e 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a er the program.*
1a51f 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 * that sqlite3_p
1a520 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 repare() generat
1a521 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 es. For example
1a522 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f , if new functio
1a523 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 ns or.** collati
1a524 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 ng sequences are
1a525 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 registered or i
1a526 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 f an authorizer
1a527 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 function is.** a
1a528 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e dded or changed.
1a529 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1a52a 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 nt sqlite3_expir
1a52b 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ed(sqlite3_stmt
1a52c 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
1a52d 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *p = (Vdbe*)pStm
1a52e 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 t;. return p==0
1a52f 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a || p->expired;.
1a530 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1a531 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
1a532 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 utine destroys a
1a533 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1a534 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64 that is created
1a535 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 by.** the sqlit
1a536 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 e3_compile() rou
1a537 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 tine. The intege
1a538 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e r returned is an
1a539 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 SQLITE_.** succ
1a53a 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 ess/failure code
1a53b 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
1a53c 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 the result of ex
1a53d 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 ecuting the virt
1a53e 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a ual.** machine..
1a53f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1a540 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f ne sets the erro
1a541 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e r code and strin
1a542 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a g returned by.**
1a543 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 sqlite3_errcode
1a544 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d (), sqlite3_errm
1a545 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 sg() and sqlite3
1a546 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a _errmsg16()..*/.
1a547 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1a548 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
1a549 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1a54a 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a tmt){. int rc;.
1a54b 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 if( pStmt==0 )
1a54c 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1a54d 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
1a54e 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 Vdbe *v = (Vd
1a54f 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 be*)pStmt;. s
1a550 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e qlite3 *db = v->
1a551 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 db;.#if SQLITE_T
1a552 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 HREADSAFE. sq
1a553 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
1a554 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 ex = v->db->mute
1a555 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 x;.#endif. sq
1a556 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1a557 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 74 r(mutex);. st
1a558 6d 74 4c 72 75 52 65 6d 6f 76 65 28 76 29 3b 0a mtLruRemove(v);.
1a559 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1a55a 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b VdbeFinalize(v);
1a55b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1a55c 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3ApiExit(db, rc)
1a55d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1a55e 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
1a55f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1a560 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 6d c;.}../*.** Term
1a561 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65 6e inate the curren
1a562 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 61 t execution of a
1a563 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
1a564 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a 20 and reset it.**
1a565 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61 72 back to its star
1a566 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74 68 ting state so th
1a567 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 75 at it can be reu
1a568 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20 63 sed. A success c
1a569 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 ode from.** the
1a56a 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e 20 prior execution
1a56b 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
1a56c 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1a56d 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 sets the error c
1a56e 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 ode and string r
1a56f 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 eturned by.** sq
1a570 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c lite3_errcode(),
1a571 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 sqlite3_errmsg(
1a572 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 ) and sqlite3_er
1a573 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c rmsg16()..*/.SQL
1a574 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1a575 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 te3_reset(sqlite
1a576 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
1a577 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 int rc;. if(
1a578 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 pStmt==0 ){.
1a579 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1a57a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 }else{. Vdb
1a57b 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 e *v = (Vdbe*)pS
1a57c 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 tmt;. sqlite3
1a57d 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e _mutex_enter(v->
1a57e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 db->mutex);.
1a57f 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1a580 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73 74 Reset(v);. st
1a581 6d 74 4c 72 75 41 64 64 28 76 29 3b 0a 20 20 20 mtLruAdd(v);.
1a582 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1a583 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c 20 Ready(v, -1, 0,
1a584 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 0, 0);. asser
1a585 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62 2d t( (rc & (v->db-
1a586 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 >errMask))==rc )
1a587 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1a588 65 33 41 70 69 45 78 69 74 28 76 2d 3e 64 62 2c e3ApiExit(v->db,
1a589 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rc);. sqlite
1a58a 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3_mutex_leave(v-
1a58b 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
1a58c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a58d 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 ./*.** Set all t
1a58e 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e he parameters in
1a58f 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 the compiled SQ
1a590 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e L statement to N
1a591 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ULL..*/.SQLITE_A
1a592 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
1a593 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 lear_bindings(sq
1a594 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1a595 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 t){. int i;. i
1a596 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1a597 4b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 K;. Vdbe *p = (
1a598 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 Vdbe*)pStmt;.#if
1a599 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
1a59a 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 FE. sqlite3_mut
1a59b 65 78 20 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 ex *mutex = ((Vd
1a59c 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e be*)pStmt)->db->
1a59d 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 mutex;.#endif.
1a59e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1a59f 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f ter(mutex);. fo
1a5a0 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 r(i=0; i<p->nVar
1a5a1 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 ; i++){. sqli
1a5a2 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1a5a3 65 28 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a e(&p->aVar[i]);.
1a5a4 20 20 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 p->aVar[i].f
1a5a5 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1a5a6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
1a5a7 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
1a5a8 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1a5a9 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }.../***********
1a5aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a5ab 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f * sqlite3_value_
1a5ac 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **************
1a5ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a5ae 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1a5af 6e 67 20 72 6f 75 74 69 6e 65 73 20 65 78 74 72 ng routines extr
1a5b0 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 act information
1a5b1 66 72 6f 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71 from a Mem or sq
1a5b2 6c 69 74 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 lite3_value.** s
1a5b3 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c tructure..*/.SQL
1a5b4 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1a5b5 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 id *sqlite3_valu
1a5b6 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 e_blob(sqlite3_v
1a5b7 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d alue *pVal){. M
1a5b8 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 em *p = (Mem*)pV
1a5b9 61 6c 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 al;. if( p->fla
1a5ba 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d gs & (MEM_Blob|M
1a5bb 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 73 EM_Str) ){. s
1a5bc 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
1a5bd 61 6e 64 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20 andBlob(p);.
1a5be 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d p->flags &= ~MEM
1a5bf 5f 53 74 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 _Str;. p->fla
1a5c0 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a gs |= MEM_Blob;.
1a5c1 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b return p->z;
1a5c2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
1a5c3 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c turn sqlite3_val
1a5c4 75 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 ue_text(pVal);.
1a5c5 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 }.}.SQLITE_API
1a5c6 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 int sqlite3_valu
1a5c7 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f e_bytes(sqlite3_
1a5c8 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
1a5c9 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 return sqlite3Va
1a5ca 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53 lueBytes(pVal, S
1a5cb 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53 QLITE_UTF8);.}.S
1a5cc 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1a5cd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
1a5ce 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 s16(sqlite3_valu
1a5cf 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
1a5d0 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 rn sqlite3ValueB
1a5d1 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 ytes(pVal, SQLIT
1a5d2 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a E_UTF16NATIVE);.
1a5d3 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 }.SQLITE_API dou
1a5d4 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ble sqlite3_valu
1a5d5 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 e_double(sqlite3
1a5d6 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
1a5d7 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1a5d8 64 62 65 52 65 61 6c 56 61 6c 75 65 28 28 4d 65 dbeRealValue((Me
1a5d9 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 m*)pVal);.}.SQLI
1a5da 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1a5db 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c e3_value_int(sql
1a5dc 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1a5dd 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 ){. return (int
1a5de 29 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 )sqlite3VdbeIntV
1a5df 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 alue((Mem*)pVal)
1a5e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 ;.}.SQLITE_API s
1a5e1 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 qlite_int64 sqli
1a5e2 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 te3_value_int64(
1a5e3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1a5e4 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 Val){. return s
1a5e5 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1a5e6 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a ue((Mem*)pVal);.
1a5e7 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e }.SQLITE_API con
1a5e8 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1a5e9 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *sqlite3_value_
1a5ea 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c text(sqlite3_val
1a5eb 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
1a5ec 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 urn (const unsig
1a5ed 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74 ned char *)sqlit
1a5ee 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c e3ValueText(pVal
1a5ef 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
1a5f0 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1a5f1 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1a5f2 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1a5f3 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 d *sqlite3_value
1a5f4 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f _text16(sqlite3_
1a5f5 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 value* pVal){.
1a5f6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 return sqlite3Va
1a5f7 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 lueText(pVal, SQ
1a5f8 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
1a5f9 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1a5fa 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
1a5fb 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
1a5fc 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 be(sqlite3_value
1a5fd 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 *pVal){. retur
1a5fe 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 n sqlite3ValueTe
1a5ff 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f xt(pVal, SQLITE_
1a600 55 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 UTF16BE);.}.SQLI
1a601 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1a602 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 d *sqlite3_value
1a603 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 _text16le(sqlite
1a604 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
1a605 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1a606 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 ValueText(pVal,
1a607 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 3b SQLITE_UTF16LE);
1a608 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1a609 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
1a60a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1a60b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1a60c 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ype(sqlite3_valu
1a60d 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e* pVal){. retu
1a60e 72 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d rn pVal->type;.}
1a60f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1a610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
1a611 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 sqlite3_result_
1a612 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1a613 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a614 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1a615 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 g routines are u
1a616 73 65 64 20 62 79 20 75 73 65 72 2d 64 65 66 69 sed by user-defi
1a617 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f ned functions to
1a618 20 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 specify.** the
1a619 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e function result.
1a61a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
1a61b 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1a61c 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 lt_blob(. sqlit
1a61d 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1a61e 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
1a61f 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
1a620 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1a621 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 id *).){. asser
1a622 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 t( n>=0 );. ass
1a623 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1a624 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1a625 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1a626 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1a627 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a tStr(&pCtx->s, z
1a628 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d , n, 0, xDel);.}
1a629 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1a62a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1a62b 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 double(sqlite3_c
1a62c 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 64 6f ontext *pCtx, do
1a62d 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 uble rVal){. as
1a62e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a62f 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1a630 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1a631 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a632 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e etDouble(&pCtx->
1a633 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 s, rVal);.}.SQLI
1a634 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1a635 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
1a636 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1a637 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 *pCtx, const ch
1a638 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 ar *z, int n){.
1a639 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1a63a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1a63b 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1a63c 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f ;. pCtx->isErro
1a63d 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 r = SQLITE_ERROR
1a63e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1a63f 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e emSetStr(&pCtx->
1a640 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f s, z, n, SQLITE_
1a641 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 UTF8, SQLITE_TRA
1a642 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 NSIENT);.}.#ifnd
1a643 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
1a644 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 TF16.SQLITE_API
1a645 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1a646 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 ult_error16(sqli
1a647 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1a648 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a x, const void *z
1a649 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 , int n){. asse
1a64a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1a64b 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1a64c 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
1a64d 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 Ctx->isError = S
1a64e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 QLITE_ERROR;. s
1a64f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1a650 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c Str(&pCtx->s, z,
1a651 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 n, SQLITE_UTF16
1a652 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 NATIVE, SQLITE_T
1a653 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e RANSIENT);.}.#en
1a654 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 dif.SQLITE_API v
1a655 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1a656 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 lt_int(sqlite3_c
1a657 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e ontext *pCtx, in
1a658 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 t iVal){. asser
1a659 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1a65a 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1a65b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
1a65c 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
1a65d 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 nt64(&pCtx->s, (
1a65e 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c i64)iVal);.}.SQL
1a65f 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1a660 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 ite3_result_int6
1a661 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 4(sqlite3_contex
1a662 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 56 61 t *pCtx, i64 iVa
1a663 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 l){. assert( sq
1a664 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1a665 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1a666 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
1a667 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 VdbeMemSetInt64(
1a668 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b &pCtx->s, iVal);
1a669 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1a66a 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1a66b 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 t_null(sqlite3_c
1a66c 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 ontext *pCtx){.
1a66d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1a66e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1a66f 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1a670 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1a671 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d emSetNull(&pCtx-
1a672 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 >s);.}.SQLITE_AP
1a673 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1a674 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 esult_text(. sq
1a675 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1a676 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 Ctx, . const ch
1a677 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c ar *z, . int n,
1a678 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
1a679 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 void *).){. ass
1a67a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1a67b 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1a67c 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1a67d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1a67e 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a tStr(&pCtx->s, z
1a67f 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , n, SQLITE_UTF8
1a680 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e 64 , xDel);.}.#ifnd
1a681 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
1a682 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 TF16.SQLITE_API
1a683 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1a684 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 20 73 71 ult_text16(. sq
1a685 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1a686 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f Ctx, . const vo
1a687 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c id *z, . int n,
1a688 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 . void (*xDel)
1a689 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 (void *).){. as
1a68a 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a68b 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1a68c 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1a68d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a68e 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1a68f 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1a690 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 16NATIVE, xDel);
1a691 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1a692 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1a693 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 71 t_text16be(. sq
1a694 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1a695 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f Ctx, . const vo
1a696 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c id *z, . int n,
1a697 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 . void (*xDel)
1a698 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 (void *).){. as
1a699 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a69a 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1a69b 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1a69c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a69d 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1a69e 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1a69f 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 16BE, xDel);.}.S
1a6a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1a6a1 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
1a6a2 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 xt16le(. sqlite
1a6a3 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
1a6a4 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
1a6a5 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 z, . int n, .
1a6a6 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
1a6a7 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 d *).){. assert
1a6a8 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1a6a9 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1a6aa 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
1a6ab 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
1a6ac 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e r(&pCtx->s, z, n
1a6ad 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 , SQLITE_UTF16LE
1a6ae 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 , xDel);.}.#endi
1a6af 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1a6b0 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 _UTF16 */.SQLITE
1a6b1 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1a6b2 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 3_result_value(s
1a6b3 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1a6b4 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 76 61 pCtx, sqlite3_va
1a6b5 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 lue *pValue){.
1a6b6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1a6b7 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1a6b8 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1a6b9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1a6ba 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c 20 mCopy(&pCtx->s,
1a6bb 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 pValue);.}.SQLIT
1a6bc 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1a6bd 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c e3_result_zerobl
1a6be 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ob(sqlite3_conte
1a6bf 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 6e 29 xt *pCtx, int n)
1a6c0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1a6c1 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1a6c2 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1a6c3 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ) );. sqlite3Vd
1a6c4 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 beMemSetZeroBlob
1a6c5 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d (&pCtx->s, n);.}
1a6c6 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1a6c7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1a6c8 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 error_code(sqlit
1a6c9 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1a6ca 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a , int errCode){.
1a6cb 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 pCtx->isError
1a6cc 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 69 66 28 = errCode;. if(
1a6cd 20 70 43 74 78 2d 3e 73 2e 66 6c 61 67 73 20 26 pCtx->s.flags &
1a6ce 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1a6cf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1a6d0 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
1a6d1 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 65 72 sqlite3ErrStr(er
1a6d2 72 43 6f 64 65 29 2c 20 2d 31 2c 20 0a 20 20 20 rCode), -1, .
1a6d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6d4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 SQLITE_UTF
1a6d5 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 8, SQLITE_STATIC
1a6d6 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f 72 );. }.}../* For
1a6d7 63 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f ce an SQLITE_TOO
1a6d8 42 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 BIG error. */.SQ
1a6d9 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1a6da 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
1a6db 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 or_toobig(sqlite
1a6dc 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 3_context *pCtx)
1a6dd 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1a6de 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1a6df 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1a6e0 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 ) );. pCtx->isE
1a6e1 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54 4f rror = SQLITE_TO
1a6e2 4f 42 49 47 3b 0a 20 20 73 71 6c 69 74 65 33 56 OBIG;. sqlite3V
1a6e3 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 dbeMemSetStr(&pC
1a6e4 74 78 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20 6f tx->s, "string o
1a6e5 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c r blob too big",
1a6e6 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 -1, .
1a6e7 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c SQL
1a6e8 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
1a6e9 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 _STATIC);.}../*
1a6ea 41 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 An SQLITE_NOMEM
1a6eb 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 error. */.SQLITE
1a6ec 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1a6ed 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 3_result_error_n
1a6ee 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f 6e omem(sqlite3_con
1a6ef 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 text *pCtx){. a
1a6f0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1a6f1 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1a6f2 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1a6f3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1a6f4 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 SetNull(&pCtx->s
1a6f5 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 );. pCtx->isErr
1a6f6 6f 72 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 or = SQLITE_NOME
1a6f7 4d 3b 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62 2d M;. pCtx->s.db-
1a6f8 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
1a6f9 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 1;.}../*.** Exec
1a6fa 75 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e ute the statemen
1a6fb 74 20 70 53 74 6d 74 2c 20 65 69 74 68 65 72 20 t pStmt, either
1a6fc 75 6e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20 64 until a row of d
1a6fd 61 74 61 20 69 73 20 72 65 61 64 79 2c 20 74 68 ata is ready, th
1a6fe 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 e.** statement i
1a6ff 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 65 s completely exe
1a700 63 75 74 65 64 20 6f 72 20 61 6e 20 65 72 72 6f cuted or an erro
1a701 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 r occurs..**.**
1a702 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 This routine imp
1a703 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 75 6c 6b lements the bulk
1a704 20 6f 66 20 74 68 65 20 6c 6f 67 69 63 20 62 65 of the logic be
1a705 68 69 6e 64 20 74 68 65 20 73 71 6c 69 74 65 5f hind the sqlite_
1a706 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20 20 step().** API.
1a707 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 6f The only thing o
1a708 6d 69 74 74 65 64 20 69 73 20 74 68 65 20 61 75 mitted is the au
1a709 74 6f 6d 61 74 69 63 20 72 65 63 6f 6d 70 69 6c tomatic recompil
1a70a 65 20 69 66 20 61 20 0a 2a 2a 20 73 63 68 65 6d e if a .** schem
1a70b 61 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 a change has occ
1a70c 75 72 72 65 64 2e 20 20 54 68 61 74 20 64 65 74 urred. That det
1a70d 61 69 6c 20 69 73 20 68 61 6e 64 6c 65 64 20 62 ail is handled b
1a70e 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20 73 y the.** outer s
1a70f 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 77 72 qlite3_step() wr
1a710 61 70 70 65 72 20 70 72 6f 63 65 64 75 72 65 2e apper procedure.
1a711 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
1a712 71 6c 69 74 65 33 53 74 65 70 28 56 64 62 65 20 qlite3Step(Vdbe
1a713 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a *p){. sqlite3 *
1a714 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 db;. int rc;..
1a715 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 69 66 assert(p);. if
1a716 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 ( p->magic!=VDBE
1a717 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 _MAGIC_RUN ){.
1a718 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1a719 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f MISUSE;. }.. /
1a71a 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 6d 61 * Assert that ma
1a71b 6c 6c 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 66 lloc() has not f
1a71c 61 69 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d 20 ailed */. db =
1a71d 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d p->db;. if( db-
1a71e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1a71f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1a720 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 TE_NOMEM;. }..
1a721 20 69 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 26 if( p->pc<=0 &&
1a722 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 p->expired ){.
1a723 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d if( ALWAYS(p-
1a724 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 >rc==SQLITE_OK)
1a725 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d ){. p->rc =
1a726 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a SQLITE_SCHEMA;.
1a727 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 }. rc = S
1a728 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1a729 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 goto end_of_ste
1a72a 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c p;. }. if( sql
1a72b 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1a72c 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1a72d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1a72e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1a72f 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 _MISUSE;. }. i
1a730 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 f( p->pc<0 ){.
1a731 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
1a732 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 e no other state
1a733 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 ments currently
1a734 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20 running, then.
1a735 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69 ** reset the i
1a736 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20 nterrupt flag.
1a737 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 This prevents a
1a738 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
1a739 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a interrupt. **
1a73a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 from interrupti
1a73b 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 ng a statement t
1a73c 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 hat has not yet
1a73d 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a started.. */.
1a73e 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 if( db->acti
1a73f 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a veVdbeCnt==0 ){.
1a740 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 db->u1.isI
1a741 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a nterrupted = 0;.
1a742 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }..#ifndef S
1a743 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
1a744 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 . if( db->xPr
1a745 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e ofile && !db->in
1a746 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 it.busy ){.
1a747 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 double rNow;.
1a748 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 sqlite3OsCur
1a749 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 rentTime(db->pVf
1a74a 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 s, &rNow);.
1a74b 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 p->startTime =
1a74c 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 (u64)((rNow - (i
1a74d 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a nt)rNow)*3600.0*
1a74e 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 24.0*1000000000.
1a74f 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0);. }.#endif
1a750 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 .. db->active
1a751 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 VdbeCnt++;. i
1a752 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d f( p->readOnly==
1a753 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 62 0 ) db->writeVdb
1a754 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 eCnt++;. p->p
1a755 63 20 3d 20 30 3b 0a 20 20 20 20 73 74 6d 74 4c c = 0;. stmtL
1a756 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 7d ruRemove(p);. }
1a757 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a758 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 OMIT_EXPLAIN. i
1a759 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b f( p->explain ){
1a75a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1a75b 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20 3VdbeList(p);.
1a75c 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 }else.#endif /*
1a75d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
1a75e 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 AIN */. {. r
1a75f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 c = sqlite3VdbeE
1a760 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 xec(p);. }.. i
1a761 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1a762 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 Off(db) ){. r
1a763 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 c = SQLITE_MISUS
1a764 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 E;. }..#ifndef
1a765 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1a766 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 E. /* Invoke th
1a767 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 e profile callba
1a768 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ck if there is o
1a769 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 ne. */. if( rc
1a76a 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 !=SQLITE_ROW &&
1a76b 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 db->xProfile &&
1a76c 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 !db->init.busy &
1a76d 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 & p->zSql ){.
1a76e 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 double rNow;.
1a76f 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65 u64 elapseTime
1a770 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ;.. sqlite3Os
1a771 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e CurrentTime(db->
1a772 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 pVfs, &rNow);.
1a773 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 elapseTime = (
1a774 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e u64)((rNow - (in
1a775 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 t)rNow)*3600.0*2
1a776 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 4.0*1000000000.0
1a777 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d );. elapseTim
1a778 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d e -= p->startTim
1a779 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 e;. db->xProf
1a77a 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 ile(db->pProfile
1a77b 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c Arg, p->zSql, el
1a77c 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 apseTime);. }.#
1a77d 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 endif.. db->err
1a77e 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28 Code = rc;. if(
1a77f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 SQLITE_NOMEM==s
1a780 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d qlite3ApiExit(p-
1a781 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20 >db, p->rc) ){.
1a782 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
1a783 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 E_NOMEM;. }.end
1a784 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41 _of_step:. /* A
1a785 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63 t this point loc
1a786 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68 al variable rc h
1a787 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74 olds the value t
1a788 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 hat should be .
1a789 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 ** returned if
1a78a 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77 this statement w
1a78b 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e as compiled usin
1a78c 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20 g the legacy .
1a78d 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 ** sqlite3_prepa
1a78e 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 re() interface.
1a78f 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 According to the
1a790 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20 docs, this can
1a791 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 only. ** be one
1a792 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 of the values i
1a793 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65 n the first asse
1a794 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69 rt() below. Vari
1a795 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a able p->rc . **
1a796 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 contains the va
1a797 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 lue that would b
1a798 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71 e returned if sq
1a799 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
1a79a 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c . ** were call
1a79b 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20 ed on statement
1a79c 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 p.. */. assert
1a79d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 ( rc==SQLITE_ROW
1a79e 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f || rc==SQLITE_
1a79f 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51 DONE || rc==SQ
1a7a0 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20 LITE_ERROR .
1a7a1 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 || rc==SQLITE
1a7a2 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c _BUSY || rc==SQL
1a7a3 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a ITE_MISUSE. );.
1a7a4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21 assert( p->rc!
1a7a5 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70 =SQLITE_ROW && p
1a7a6 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ->rc!=SQLITE_DON
1a7a7 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73 E );. if( p->is
1a7a8 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21 PrepareV2 && rc!
1a7a9 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 =SQLITE_ROW && r
1a7aa 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c!=SQLITE_DONE )
1a7ab 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
1a7ac 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 statement was p
1a7ad 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71 repared using sq
1a7ae 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1a7af 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a (), and an. *
1a7b0 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 * error has occu
1a7b1 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e red, then return
1a7b2 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
1a7b3 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a in p->rc to the.
1a7b4 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 ** caller. S
1a7b5 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 et the error cod
1a7b6 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
1a7b7 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 e handle to the
1a7b8 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20 same value..
1a7b9 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d */ . rc = db-
1a7ba 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 >errCode = p->rc
1a7bb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
1a7bc 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b rc&db->errMask);
1a7bd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 .}../*.** This i
1a7be 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 s the top-level
1a7bf 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1a7c0 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 f sqlite3_step()
1a7c1 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 . Call.** sqlit
1a7c2 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d e3Step() to do m
1a7c3 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e ost of the work.
1a7c4 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 If a schema er
1a7c5 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 ror occurs,.** c
1a7c6 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 all sqlite3Repre
1a7c7 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 pare() and try a
1a7c8 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 gain..*/.#ifdef
1a7c9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 SQLITE_OMIT_PARS
1a7ca 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ER.SQLITE_API in
1a7cb 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 t sqlite3_step(s
1a7cc 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1a7cd 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 mt){. int rc =
1a7ce 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1a7cf 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 if( pStmt ){.
1a7d0 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 76 Vdbe *v;. v
1a7d1 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
1a7d2 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1a7d3 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e ex_enter(v->db->
1a7d4 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d mutex);. rc =
1a7d5 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29 3b sqlite3Step(v);
1a7d6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1a7d7 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e ex_leave(v->db->
1a7d8 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 mutex);. }. re
1a7d9 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65 turn rc;.}.#else
1a7da 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1a7db 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c sqlite3_step(sql
1a7dc 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1a7dd 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1a7de 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 LITE_MISUSE;. i
1a7df 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 f( pStmt ){.
1a7e0 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 int cnt = 0;.
1a7e1 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 Vdbe *v = (Vdbe
1a7e2 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c *)pStmt;. sql
1a7e3 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 ite3 *db = v->db
1a7e4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1a7e5 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 tex_enter(db->mu
1a7e6 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 tex);. while(
1a7e7 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 (rc = sqlite3St
1a7e8 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 ep(v))==SQLITE_S
1a7e9 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 CHEMA.
1a7ea 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 && cnt++ < 5.
1a7eb 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20 && (rc
1a7ec 3d 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28 = vdbeReprepare(
1a7ed 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 v))==SQLITE_OK )
1a7ee 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1a7ef 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 reset(pStmt);.
1a7f0 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 3d v->expired =
1a7f1 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0;. }. if
1a7f2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 ( rc==SQLITE_SCH
1a7f3 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28 76 2d EMA && ALWAYS(v-
1a7f4 3e 69 73 50 72 65 70 61 72 65 56 32 29 20 26 26 >isPrepareV2) &&
1a7f5 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45 72 72 ALWAYS(db->pErr
1a7f6 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 ) ){. /* Th
1a7f7 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 61 is case occurs a
1a7f8 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f 20 fter failing to
1a7f9 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 6c recompile an sql
1a7fa 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 20 statement. .
1a7fb 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 ** The error
1a7fc 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 message from the
1a7fd 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 61 SQL compiler ha
1a7fe 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c s already been l
1a7ff 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 oaded . **
1a800 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
1a801 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 62 e handle. This b
1a802 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20 lock copies the
1a803 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a 20 error message .
1a804 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 ** from the
1a805 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1a806 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 6d into the statem
1a807 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 65 ent and sets the
1a808 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 statement.
1a809 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e ** program coun
1a80a 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 75 ter to 0 to ensu
1a80b 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 re that when the
1a80c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a 20 statement is .
1a80d 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65 ** finalize
1a80e 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 70 d or reset the p
1a80f 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 73 arser error mess
1a810 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 age is available
1a811 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 71 via. ** sq
1a812 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 lite3_errmsg() a
1a813 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f nd sqlite3_errco
1a814 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 de().. */.
1a815 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1a816 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 63 *zErr = (const c
1a817 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 har *)sqlite3_va
1a818 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 lue_text(db->pEr
1a819 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 r); . sqlit
1a81a 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d 3e e3DbFree(db, v->
1a81b 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 zErrMsg);.
1a81c 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
1a81d 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
1a81e 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 v->zErrMsg = sq
1a81f 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
1a820 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d , zErr);. }
1a821 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 else {.
1a822 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a v->zErrMsg = 0;.
1a823 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 v->rc =
1a824 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1a825 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1a826 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 rc = sqlite3ApiE
1a827 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 xit(db, rc);.
1a828 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1a829 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
1a82a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1a82b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1a82c 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73 * Extract the us
1a82d 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 er data from a s
1a82e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 qlite3_context s
1a82f 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 tructure and ret
1a830 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 urn a.** pointer
1a831 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 to it..*/.SQLIT
1a832 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
1a833 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 te3_user_data(sq
1a834 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1a835 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 ){. assert( p &
1a836 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 & p->pFunc );.
1a837 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d return p->pFunc-
1a838 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f >pUserData;.}../
1a839 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 *.** Extract the
1a83a 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 user data from
1a83b 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a sqlite3_contex
1a83c 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 t structure and
1a83d 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e return a.** poin
1a83e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 ter to it..*/.SQ
1a83f 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
1a840 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 *sqlite3_contex
1a841 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 t_db_handle(sqli
1a842 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b te3_context *p){
1a843 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 . assert( p &&
1a844 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 p->pFunc );. re
1a845 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a turn p->s.db;.}.
1a846 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
1a847 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c wing is the impl
1a848 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e ementation of an
1a849 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 SQL function th
1a84a 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 at always.** fai
1a84b 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ls with an error
1a84c 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 message stating
1a84d 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 that the functi
1a84e 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 on is used in th
1a84f 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 e.** wrong conte
1a850 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 xt. The sqlite3
1a851 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 _overload_functi
1a852 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 on() API might c
1a853 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 onstruct.** SQL
1a854 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 function that us
1a855 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 e this routine s
1a856 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 o that the funct
1a857 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a ions will exist.
1a858 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f ** for name reso
1a859 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 lution but are a
1a85a 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 ctually overload
1a85b 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 ed by the xFindF
1a85c 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f unction.** metho
1a85d 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 d of virtual tab
1a85e 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 les..*/.SQLITE_P
1a85f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a860 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 te3InvalidFuncti
1a861 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f on(. sqlite3_co
1a862 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
1a863 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e /* The function
1a864 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 calling context
1a865 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 */. int NotUse
1a866 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
1a867 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 /* Number of ar
1a868 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 guments to the f
1a869 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c unction */. sql
1a86a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
1a86b 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 Used2 /* Value
1a86c 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e of each argumen
1a86d 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 t */.){. const
1a86e 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f char *zName = co
1a86f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e ntext->pFunc->zN
1a870 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 ame;. char *zEr
1a871 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 r;. UNUSED_PARA
1a872 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
1a873 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 NotUsed2);. zEr
1a874 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 r = sqlite3_mpri
1a875 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62 ntf(. "unab
1a876 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 le to use functi
1a877 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 on %s in the req
1a878 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c uested context",
1a879 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 zName);. sqlit
1a87a 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
1a87b 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d context, zErr, -
1a87c 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 1);. sqlite3_fr
1a87d 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a ee(zErr);.}../*.
1a87e 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 ** Allocate or r
1a87f 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 eturn the aggreg
1a880 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ate context for
1a881 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e a user function.
1a882 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 A new.** conte
1a883 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 xt is allocated
1a884 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c on the first cal
1a885 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 l. Subsequent c
1a886 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a alls return the.
1a887 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 ** same context
1a888 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 that was returne
1a889 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 d on prior calls
1a88a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1a88b 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 void *sqlite3_ag
1a88c 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
1a88d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1a88e 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a *p, int nByte){.
1a88f 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 Mem *pMem;. a
1a890 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 ssert( p && p->p
1a891 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 Func && p->pFunc
1a892 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 ->xStep );. ass
1a893 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1a894 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d ex_held(p->s.db-
1a895 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 >mutex) );. pMe
1a896 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 m = p->pMem;. i
1a897 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 f( (pMem->flags
1a898 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b & MEM_Agg)==0 ){
1a899 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d . if( nByte==
1a89a 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1a89b 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1a89c 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a External(pMem);.
1a89d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1a89e 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1a89f 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b pMem->z = 0;
1a8a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1a8a1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1a8a2 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 Grow(pMem, nByte
1a8a3 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d , 0);. pMem
1a8a4 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 ->flags = MEM_Ag
1a8a5 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 g;. pMem->u
1a8a6 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 .pDef = p->pFunc
1a8a7 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d ;. if( pMem
1a8a8 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ->z ){. m
1a8a9 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 emset(pMem->z, 0
1a8aa 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 , nByte);.
1a8ab 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
1a8ac 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d turn (void*)pMem
1a8ad 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ->z;.}../*.** Re
1a8ae 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 turn the auxilar
1a8af 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 y data pointer,
1a8b0 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 if any, for the
1a8b1 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 iArg'th argument
1a8b2 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d to.** the user-
1a8b3 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 function defined
1a8b4 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c by pCtx..*/.SQL
1a8b5 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
1a8b6 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
1a8b7 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a(sqlite3_contex
1a8b8 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 t *pCtx, int iAr
1a8b9 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a g){. VdbeFunc *
1a8ba 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 pVdbeFunc;.. as
1a8bb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1a8bc 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1a8bd 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1a8be 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 pVdbeFunc = pCt
1a8bf 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 x->pVdbeFunc;.
1a8c0 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c if( !pVdbeFunc |
1a8c1 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e | iArg>=pVdbeFun
1a8c2 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c c->nAux || iArg<
1a8c3 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1a8c4 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
1a8c5 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 pVdbeFunc->apAux
1a8c6 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a [iArg].pAux;.}..
1a8c7 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 /*.** Set the au
1a8c8 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e xilary data poin
1a8c9 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 ter and delete f
1a8ca 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 unction, for the
1a8cb 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 iArg'th.** argu
1a8cc 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 ment to the user
1a8cd 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 -function define
1a8ce 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 d by pCtx. Any p
1a8cf 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 revious value is
1a8d0 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 .** deleted by c
1a8d1 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 alling the delet
1a8d2 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 e function speci
1a8d3 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 fied when it was
1a8d4 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f set..*/.SQLITE_
1a8d5 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1a8d6 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 _set_auxdata(.
1a8d7 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1a8d8 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 *pCtx, . int iA
1a8d9 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 rg, . void *pAu
1a8da 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 x, . void (*xDe
1a8db 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a lete)(void*).){.
1a8dc 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 struct AuxData
1a8dd 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 *pAuxData;. Vd
1a8de 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e beFunc *pVdbeFun
1a8df 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 c;. if( iArg<0
1a8e0 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a ) goto failed;..
1a8e1 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1a8e2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1a8e3 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1a8e4 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d );. pVdbeFunc =
1a8e5 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 pCtx->pVdbeFunc
1a8e6 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 ;. if( !pVdbeFu
1a8e7 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d nc || pVdbeFunc-
1a8e8 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 >nAux<=iArg ){.
1a8e9 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 int nAux = (p
1a8ea 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 VdbeFunc ? pVdbe
1a8eb 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b Func->nAux : 0);
1a8ec 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 . int nMalloc
1a8ed 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 = sizeof(VdbeFu
1a8ee 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 nc) + sizeof(str
1a8ef 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 uct AuxData)*iAr
1a8f0 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 g;. pVdbeFunc
1a8f1 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
1a8f2 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 loc(pCtx->s.db,
1a8f3 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c pVdbeFunc, nMall
1a8f4 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 oc);. if( !pV
1a8f5 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 dbeFunc ){.
1a8f6 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
1a8f7 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 }. pCtx->pV
1a8f8 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 dbeFunc = pVdbeF
1a8f9 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 unc;. memset(
1a8fa 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 &pVdbeFunc->apAu
1a8fb 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 x[nAux], 0, size
1a8fc 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74 of(struct AuxDat
1a8fd 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 a)*(iArg+1-nAux)
1a8fe 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 );. pVdbeFunc
1a8ff 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b ->nAux = iArg+1;
1a900 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e . pVdbeFunc->
1a901 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 pFunc = pCtx->pF
1a902 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 unc;. }.. pAux
1a903 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e Data = &pVdbeFun
1a904 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a c->apAux[iArg];.
1a905 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e if( pAuxData->
1a906 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61 pAux && pAuxData
1a907 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 ->xDelete ){.
1a908 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 pAuxData->xDele
1a909 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 te(pAuxData->pAu
1a90a 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 x);. }. pAuxDa
1a90b 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b ta->pAux = pAux;
1a90c 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 . pAuxData->xDe
1a90d 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a lete = xDelete;.
1a90e 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 return;..faile
1a90f 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 d:. if( xDelete
1a910 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 ){. xDelete(
1a911 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 pAux);. }.}..#i
1a912 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1a913 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a T_DEPRECATED./*.
1a914 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1a915 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
1a916 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 e Step function
1a917 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68 of a aggregate h
1a918 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c as been .** call
1a919 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
1a91a 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 unction is depre
1a91b 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 cated. Do not u
1a91c 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f se it for new co
1a91d 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 de. It is.** pr
1a91e 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 ovide only to av
1a91f 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 oid breaking leg
1a920 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 acy code. New a
1a921 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1a922 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 n.** implementat
1a923 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 ions should keep
1a924 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 their own count
1a925 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 s within their a
1a926 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 ggregate.** cont
1a927 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ext..*/.SQLITE_A
1a928 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 PI int sqlite3_a
1a929 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 ggregate_count(s
1a92a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1a92b 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
1a92c 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d && p->pMem && p-
1a92d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 >pFunc && p->pFu
1a92e 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 nc->xStep );. r
1a92f 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e eturn p->pMem->n
1a930 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1a931 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1a932 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
1a933 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
1a934 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 for the stateme
1a935 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c nt pStmt..*/.SQL
1a936 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1a937 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 te3_column_count
1a938 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1a939 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
1a93a 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 Vm = (Vdbe *)pSt
1a93b 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d mt;. return pVm
1a93c 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 ? pVm->nResColu
1a93d 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a mn : 0;.}../*.**
1a93e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1a93f 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 er of values ava
1a940 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 ilable from the
1a941 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 current row of t
1a942 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 he.** currently
1a943 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d executing statem
1a944 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 ent pStmt..*/.SQ
1a945 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1a946 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 ite3_data_count(
1a947 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1a948 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 tmt){. Vdbe *pV
1a949 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d m = (Vdbe *)pStm
1a94a 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 t;. if( pVm==0
1a94b 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 || pVm->pResultS
1a94c 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 et==0 ) return 0
1a94d 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e ;. return pVm->
1a94e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a nResColumn;.}...
1a94f 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 /*.** Check to s
1a950 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f ee if column iCo
1a951 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 l of the given s
1a952 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 tatement is vali
1a953 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c d. If.** it is,
1a954 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 return a pointe
1a955 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 r to the Mem for
1a956 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1a957 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 at column..** If
1a958 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c iCol is not val
1a959 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 id, return a poi
1a95a 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 nter to a Mem wh
1a95b 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a ich has a value.
1a95c 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 ** of NULL..*/.s
1a95d 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d tatic Mem *colum
1a95e 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d nMem(sqlite3_stm
1a95f 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1a960 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 {. Vdbe *pVm;.
1a961 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d int vals;. Mem
1a962 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d *pOut;.. pVm =
1a963 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a (Vdbe *)pStmt;.
1a964 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d if( pVm && pVm
1a965 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 ->pResultSet!=0
1a966 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f && i<pVm->nResCo
1a967 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a lumn && i>=0 ){.
1a968 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1a969 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d x_enter(pVm->db-
1a96a 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c >mutex);. val
1a96b 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 s = sqlite3_data
1a96c 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 _count(pStmt);.
1a96d 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e pOut = &pVm->
1a96e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 pResultSet[i];.
1a96f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 }else{. /* (
1a970 28 64 6f 75 62 6c 65 29 30 29 20 49 6e 20 63 61 (double)0) In ca
1a971 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 se of SQLITE_OMI
1a972 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
1a973 2e 2e 2e 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 ... */. stati
1a974 63 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c c const Mem null
1a975 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75 Mem = {{0}, (dou
1a976 62 6c 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 2c ble)0, 0, "", 0,
1a977 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 MEM_Null, SQLIT
1a978 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 E_NULL, 0, 0, 0
1a979 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 20 26 };. if( pVm &
1a97a 26 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62 & ALWAYS(pVm->db
1a97b 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
1a97c 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
1a97d 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a Vm->db->mutex);.
1a97e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1a97f 6f 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 or(pVm->db, SQLI
1a980 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 TE_RANGE, 0);.
1a981 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 }. pOut = (
1a982 4d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 Mem*)&nullMem;.
1a983 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 }. return pOut
1a984 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
1a985 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1a986 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e ed after invokin
1a987 67 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c g an sqlite3_val
1a988 75 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 ue_XXX function
1a989 6f 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 on a .** column
1a98a 76 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61 value (i.e. a va
1a98b 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
1a98c 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 evaluating an SQ
1a98d 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 L expression in
1a98e 74 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 the.** select li
1a98f 73 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 st of a SELECT s
1a990 74 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d tatement) that m
1a991 61 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f ay cause a mallo
1a992 63 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 c() failure. If
1a993 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 .** malloc() has
1a994 20 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 failed, the thr
1a995 65 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 eads mallocFaile
1a996 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 d flag is cleare
1a997 64 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 d and the result
1a998 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74 .** code of stat
1a999 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20 ement pStmt set
1a99a 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e to SQLITE_NOMEM.
1a99b 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 .**.** Specifica
1a99c 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c lly, this is cal
1a99d 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a led from within:
1a99e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 .**.** sqlit
1a99f 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a e3_column_int().
1a9a0 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1a9a1 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a olumn_int64().**
1a9a2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
1a9a3 75 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 umn_text().**
1a9a4 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
1a9a5 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 _text16().**
1a9a6 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1a9a7 72 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 real().** sq
1a9a8 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
1a9a9 65 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 es().** sqli
1a9aa 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
1a9ab 31 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 16().**.** But n
1a9ac 6f 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 ot for sqlite3_c
1a9ad 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 olumn_blob(), wh
1a9ae 69 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 ich never calls
1a9af 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 malloc()..*/.sta
1a9b0 74 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d tic void columnM
1a9b1 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c allocFailure(sql
1a9b2 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1a9b3 29 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c ).{. /* If mall
1a9b4 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 oc() failed duri
1a9b5 6e 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 ng an encoding c
1a9b6 6f 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e onversion within
1a9b7 20 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 an. ** sqlite3
1a9b8 5f 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c _column_XXX API,
1a9b9 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 then set the re
1a9ba 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 turn code of the
1a9bb 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 statement to.
1a9bc 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e ** SQLITE_NOMEM.
1a9bd 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 The next call t
1a9be 6f 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e o _step() (if an
1a9bf 79 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 y) will return S
1a9c0 51 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a QLITE_ERROR. **
1a9c1 20 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 and _finalize()
1a9c2 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d will return NOM
1a9c3 45 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 EM.. */. Vdbe
1a9c4 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
1a9c5 6d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 mt;. if( p ){.
1a9c6 20 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 p->rc = sqlit
1a9c7 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c e3ApiExit(p->db,
1a9c8 20 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c p->rc);. sql
1a9c9 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1a9ca 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1a9cb 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a }.}../********
1a9cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a9cd 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c **** sqlite3_col
1a9ce 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a umn_ **********
1a9cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1a9d0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c *****.** The fol
1a9d1 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 lowing routines
1a9d2 61 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 are used to acce
1a9d3 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 ss elements of t
1a9d4 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a he current row.*
1a9d5 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 * in the result
1a9d6 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 set..*/.SQLITE_A
1a9d7 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1a9d8 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
1a9d9 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ob(sqlite3_stmt
1a9da 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1a9db 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 const void *va
1a9dc 6c 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 l;. val = sqlit
1a9dd 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 e3_value_blob( c
1a9de 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1a9df 29 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 ) );. /* Even t
1a9e0 68 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e hough there is n
1a9e1 6f 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 o encoding conve
1a9e2 72 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f rsion, value_blo
1a9e3 62 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e b() might. ** n
1a9e4 65 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c eed to call mall
1a9e5 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74 oc() to expand t
1a9e6 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a he result of a z
1a9e7 65 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 eroblob() . **
1a9e8 65 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a expression. . *
1a9e9 2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 /. columnMalloc
1a9ea 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
1a9eb 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
1a9ec 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1a9ed 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
1a9ee 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 tes(sqlite3_stmt
1a9ef 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b *pStmt, int i){
1a9f0 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c . int val = sql
1a9f1 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
1a9f2 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d ( columnMem(pStm
1a9f3 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e t,i) );. column
1a9f4 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1a9f5 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 tmt);. return v
1a9f6 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 al;.}.SQLITE_API
1a9f7 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c int sqlite3_col
1a9f8 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 umn_bytes16(sqli
1a9f9 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1a9fa 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 int i){. int v
1a9fb 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
1a9fc 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75 ue_bytes16( colu
1a9fd 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1a9fe 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
1a9ff 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
1aa00 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
1aa01 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c SQLITE_API doubl
1aa02 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
1aa03 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
1aa04 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1aa05 20 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 i){. double va
1aa06 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1aa07 65 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e e_double( column
1aa08 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a Mem(pStmt,i) );.
1aa09 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 columnMallocFa
1aa0a 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 ilure(pStmt);.
1aa0b 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 return val;.}.SQ
1aa0c 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1aa0d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 ite3_column_int(
1aa0e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1aa0f 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 tmt, int i){. i
1aa10 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 nt val = sqlite3
1aa11 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 _value_int( colu
1aa12 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1aa13 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
1aa14 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
1aa15 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
1aa16 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
1aa17 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f e_int64 sqlite3_
1aa18 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c column_int64(sql
1aa19 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1aa1a 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 , int i){. sqli
1aa1b 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 te_int64 val = s
1aa1c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
1aa1d 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 64( columnMem(pS
1aa1e 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 tmt,i) );. colu
1aa1f 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 mnMallocFailure(
1aa20 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e pStmt);. return
1aa21 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 val;.}.SQLITE_A
1aa22 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 PI const unsigne
1aa23 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f d char *sqlite3_
1aa24 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 column_text(sqli
1aa25 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1aa26 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 int i){. const
1aa27 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
1aa28 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1aa29 6c 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e lue_text( column
1aa2a 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a Mem(pStmt,i) );.
1aa2b 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 columnMallocFa
1aa2c 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 ilure(pStmt);.
1aa2d 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 return val;.}.SQ
1aa2e 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
1aa2f 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f _value *sqlite3_
1aa30 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c column_value(sql
1aa31 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1aa32 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 , int i){. Mem
1aa33 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 *pOut = columnMe
1aa34 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 m(pStmt, i);. i
1aa35 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d f( pOut->flags&M
1aa36 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 EM_Static ){.
1aa37 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 pOut->flags &=
1aa38 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 ~MEM_Static;.
1aa39 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pOut->flags |=
1aa3a 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 MEM_Ephem;. }.
1aa3b 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1aa3c 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1aa3d 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 eturn (sqlite3_v
1aa3e 61 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 alue *)pOut;.}.#
1aa3f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1aa40 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1aa41 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1aa42 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
1aa43 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ext16(sqlite3_st
1aa44 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1aa45 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 ){. const void
1aa46 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 *val = sqlite3_v
1aa47 61 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c alue_text16( col
1aa48 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1aa49 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
1aa4a 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1aa4b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
1aa4c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1aa4d 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
1aa4e 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1aa4f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 qlite3_column_ty
1aa50 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 pe(sqlite3_stmt
1aa51 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1aa52 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71 int iType = sq
1aa53 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
1aa54 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d ( columnMem(pStm
1aa55 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e t,i) );. column
1aa56 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1aa57 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 tmt);. return i
1aa58 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 Type;.}../* The
1aa59 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 following functi
1aa5a 6f 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 on is experiment
1aa5b 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 al and subject t
1aa5c 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 o change or.** r
1aa5d 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 emoval */./*int
1aa5e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
1aa5f 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 umeric_type(sqli
1aa60 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1aa61 20 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 int i){.** ret
1aa62 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 urn sqlite3_valu
1aa63 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20 e_numeric_type(
1aa64 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c columnMem(pStmt,
1aa65 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a i) );.**}.*/../*
1aa66 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 .** Convert the
1aa67 4e 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 N-th element of
1aa68 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b pStmt->pColName[
1aa69 5d 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 ] into a string
1aa6a 75 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 using.** xFunc()
1aa6b 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 then return tha
1aa6c 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 t string. If N
1aa6d 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c is out of range,
1aa6e 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a return 0..**.**
1aa6f 20 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f There are up to
1aa70 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 5 names for eac
1aa71 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 h column. useTy
1aa72 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 pe determines wh
1aa73 69 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 ich.** name is r
1aa74 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61 eturned. Here a
1aa75 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a re the names:.**
1aa76 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68 .** 0 Th
1aa77 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 e column name as
1aa78 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69 it should be di
1aa79 73 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70 splayed for outp
1aa7a 75 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20 ut.** 1
1aa7b 54 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d The datatype nam
1aa7c 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e e for the column
1aa7d 0a 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68 .** 2 Th
1aa7e 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 e name of the da
1aa7f 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 tabase that the
1aa80 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 column derives f
1aa81 72 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 rom.** 3
1aa82 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 The name of the
1aa83 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 table that the
1aa84 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 column derives f
1aa85 72 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 rom.** 4
1aa86 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 The name of the
1aa87 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 table column th
1aa88 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f at the result co
1aa89 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f lumn derives fro
1aa8a 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 m.**.** If the r
1aa8b 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 esult is not a s
1aa8c 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 imple column ref
1aa8d 65 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73 erence (if it is
1aa8e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a an expression.*
1aa8f 2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 * or a constant)
1aa90 20 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32 then useTypes 2
1aa91 2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72 , 3, and 4 retur
1aa92 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 n NULL..*/.stati
1aa93 63 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f c const void *co
1aa94 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 lumnName(. sqli
1aa95 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1aa96 0a 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 . int N,. cons
1aa97 74 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 t void *(*xFunc)
1aa98 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 (Mem*),. int us
1aa99 65 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 eType.){. const
1aa9a 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a void *ret = 0;.
1aa9b 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
1aa9c 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 e *)pStmt;. int
1aa9d 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 n;. sqlite3 *d
1aa9e 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20 b = p->db;. .
1aa9f 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
1aaa0 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 . n = sqlite3_c
1aaa1 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d olumn_count(pStm
1aaa2 74 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26 t);. if( N<n &&
1aaa3 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b N>=0 ){. N +
1aaa4 3d 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 = useType*n;.
1aaa5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1aaa6 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
1aaa7 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d . assert( db-
1aaa8 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 >mallocFailed==0
1aaa9 20 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78 46 );. ret = xF
1aaaa 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 unc(&p->aColName
1aaab 5b 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20 [N]);. /* A
1aaac 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 malloc may have
1aaad 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 failed inside of
1aaae 20 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c the xFunc() cal
1aaaf 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 2a l. If this. *
1aab0 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63 * is the case, c
1aab1 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 lear the mallocF
1aab2 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72 ailed flag and r
1aab3 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 eturn NULL..
1aab4 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d */. if( db->m
1aab5 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1aab6 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 db->mallocF
1aab7 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 ailed = 0;.
1aab8 20 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ret = 0;. }.
1aab9 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1aaba 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 x_leave(db->mute
1aabb 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
1aabc 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ret;.}../*.** R
1aabd 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f eturn the name o
1aabe 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
1aabf 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
1aac0 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 et returned by S
1aac1 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 QL.** statement
1aac2 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pStmt..*/.SQLITE
1aac3 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1aac4 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1aac5 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
1aac6 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 t *pStmt, int N)
1aac7 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d {. return colum
1aac8 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 nName(. pSt
1aac9 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f mt, N, (const vo
1aaca 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c id*(*)(Mem*))sql
1aacb 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c ite3_value_text,
1aacc 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a COLNAME_NAME);.
1aacd 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1aace 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1aacf 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1aad0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
1aad1 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 n_name16(sqlite3
1aad2 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1aad3 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 t N){. return c
1aad4 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 olumnName(.
1aad5 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 pStmt, N, (cons
1aad6 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 t void*(*)(Mem*)
1aad7 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1aad8 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e ext16, COLNAME_N
1aad9 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a AME);.}.#endif..
1aada 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 /*.** Constraint
1aadb 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45 : If you have E
1aadc 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 NABLE_COLUMN_MET
1aadd 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d ADATA then you m
1aade 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e ust.** not defin
1aadf 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e e OMIT_DECLTYPE.
1aae0 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
1aae1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
1aae2 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64 TYPE) && defined
1aae3 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 (SQLITE_ENABLE_C
1aae4 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a OLUMN_METADATA).
1aae5 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f # error "Must no
1aae6 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51 t define both SQ
1aae7 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 LITE_OMIT_DECLTY
1aae8 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e PE \. an
1aae9 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d SQLITE_ENABLE_
1aaea 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 COLUMN_METADATA"
1aaeb 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1aaec 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 SQLITE_OMIT_DEC
1aaed 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 LTYPE./*.** Retu
1aaee 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 rn the column de
1aaef 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28 claration type (
1aaf0 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f if applicable) o
1aaf1 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75 f the 'i'th colu
1aaf2 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 mn.** of the res
1aaf3 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73 ult set of SQL s
1aaf4 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a tatement pStmt..
1aaf5 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1aaf6 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1aaf7 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 3_column_decltyp
1aaf8 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1aaf9 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1aafa 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1aafb 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1aafc 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1aafd 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1aafe 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 3_value_text, CO
1aaff 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b LNAME_DECLTYPE);
1ab00 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1ab01 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1ab02 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1ab03 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
1ab04 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 mn_decltype16(sq
1ab05 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1ab06 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1ab07 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1ab08 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1ab09 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1ab0a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1ab0b 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e lue_text16, COLN
1ab0c 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d AME_DECLTYPE);.}
1ab0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1ab0e 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
1ab0f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1ab10 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a _OMIT_DECLTYPE *
1ab11 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
1ab12 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
1ab13 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 ETADATA./*.** Re
1ab14 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1ab15 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 the database fr
1ab16 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c om which a resul
1ab17 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 t column derives
1ab18 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 ..** NULL is ret
1ab19 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 urned if the res
1ab1a 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e ult column is an
1ab1b 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 expression or c
1ab1c 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e onstant or.** an
1ab1d 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 ything else whic
1ab1e 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 h is not an unab
1ab1f 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 iguous reference
1ab20 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 to a database c
1ab21 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 olumn..*/.SQLITE
1ab22 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1ab23 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1ab24 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 database_name(sq
1ab25 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1ab26 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1ab27 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1ab28 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1ab29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1ab2a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1ab2b 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d lue_text, COLNAM
1ab2c 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 E_DATABASE);.}.#
1ab2d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1ab2e 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1ab2f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1ab30 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
1ab31 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 atabase_name16(s
1ab32 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1ab33 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
1ab34 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
1ab35 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
1ab36 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
1ab37 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
1ab38 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c alue_text16, COL
1ab39 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a NAME_DATABASE);.
1ab3a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1ab3b 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1ab3c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1ab3d 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
1ab3e 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 able from which
1ab3f 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 a result column
1ab40 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c derives..** NULL
1ab41 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1ab42 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d the result colum
1ab43 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 n is an expressi
1ab44 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f on or constant o
1ab45 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c r.** anything el
1ab46 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 se which is not
1ab47 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 an unabiguous re
1ab48 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 ference to a dat
1ab49 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f abase column..*/
1ab4a 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1ab4b 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
1ab4c 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d column_table_nam
1ab4d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1ab4e 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1ab4f 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1ab50 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1ab51 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1ab52 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1ab53 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 3_value_text, CO
1ab54 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a LNAME_TABLE);.}.
1ab55 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ab56 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1ab57 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1ab58 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1ab59 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c table_name16(sql
1ab5a 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1ab5b 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1ab5c 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1ab5d 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1ab5e 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1ab5f 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1ab60 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 ue_text16, COLNA
1ab61 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e ME_TABLE);.}.#en
1ab62 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1ab63 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a IT_UTF16 */../*.
1ab64 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 ** Return the na
1ab65 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1ab66 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 column from whic
1ab67 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d h a result colum
1ab68 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 n derives..** NU
1ab69 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 LL is returned i
1ab6a 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c f the result col
1ab6b 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 umn is an expres
1ab6c 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 sion or constant
1ab6d 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 or.** anything
1ab6e 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f else which is no
1ab6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 t an unabiguous
1ab70 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 reference to a d
1ab71 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a atabase column..
1ab72 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1ab73 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1ab74 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 3_column_origin_
1ab75 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
1ab76 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 t *pStmt, int N)
1ab77 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d {. return colum
1ab78 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 nName(. pSt
1ab79 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f mt, N, (const vo
1ab7a 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c id*(*)(Mem*))sql
1ab7b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c ite3_value_text,
1ab7c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 COLNAME_COLUMN)
1ab7d 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1ab7e 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1ab7f 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1ab80 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
1ab81 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 umn_origin_name1
1ab82 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 6(sqlite3_stmt *
1ab83 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1ab84 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1ab85 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1ab86 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1ab87 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1ab88 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 3_value_text16,
1ab89 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b COLNAME_COLUMN);
1ab8a 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1ab8b 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
1ab8c 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 /.#endif /* SQLI
1ab8d 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
1ab8e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f _METADATA */.../
1ab8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ab90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
1ab91 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a sqlite3_bind_ *
1ab92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ab93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a **********.** .*
1ab94 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 * Routines used
1ab95 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73 to attach values
1ab96 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e to wildcards in
1ab97 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 a compiled SQL
1ab98 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a statement..*/./*
1ab99 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76 .** Unbind the v
1ab9a 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 alue bound to va
1ab9b 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74 riable i in virt
1ab9c 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 ual machine p. T
1ab9d 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 his is the .** t
1ab9e 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69 he same as bindi
1ab9f 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 ng a NULL value
1aba0 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 to the column. I
1aba1 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65 f the "i" parame
1aba2 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 ter is.** out of
1aba3 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c range, then SQL
1aba4 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74 ITE_RANGE is ret
1aba5 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20 urned. Othewise
1aba6 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a SQLITE_OK..**.**
1aba7 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 A successful ev
1aba8 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 aluation of this
1aba9 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 routine acquire
1abaa 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 s the mutex on p
1abab 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69 ..** the mutex i
1abac 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e s released if an
1abad 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 y kind of error
1abae 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 occurs..**.** Th
1abaf 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f e error code sto
1abb0 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 red in database
1abb1 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69 p->db is overwri
1abb2 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65 tten with the re
1abb3 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e turn.** value in
1abb4 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 any case..*/.st
1abb5 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62 atic int vdbeUnb
1abb6 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ind(Vdbe *p, int
1abb7 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 i){. Mem *pVar
1abb8 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
1abb9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1abba 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d USE;. sqlite3_m
1abbb 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 utex_enter(p->db
1abbc 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 ->mutex);. if(
1abbd 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d p->magic!=VDBE_M
1abbe 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 AGIC_RUN || p->p
1abbf 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 c>=0 ){. sqli
1abc0 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 te3Error(p->db,
1abc1 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 SQLITE_MISUSE, 0
1abc2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1abc3 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
1abc4 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 ->mutex);. re
1abc5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1abc6 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c SE;. }. if( i<
1abc7 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 1 || i>p->nVar )
1abc8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1abc9 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 or(p->db, SQLITE
1abca 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 _RANGE, 0);.
1abcb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1abcc 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
1abcd 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
1abce 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a LITE_RANGE;. }.
1abcf 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 i--;. pVar =
1abd0 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 &p->aVar[i];. s
1abd1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1abd2 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56 ease(pVar);. pV
1abd3 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ar->flags = MEM_
1abd4 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 Null;. sqlite3E
1abd5 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 rror(p->db, SQLI
1abd6 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 TE_OK, 0);. ret
1abd7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1abd8 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74 ../*.** Bind a t
1abd9 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 ext or BLOB valu
1abda 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1abdb 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c bindText(. sql
1abdc 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1abdd 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65 , /* The state
1abde 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61 ment to bind aga
1abdf 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c inst */. int i,
1abe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1abe1 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 /* Index of the
1abe2 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69 parameter to bi
1abe3 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f nd */. const vo
1abe4 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f id *zData, /
1abe5 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
1abe6 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e data to be boun
1abe7 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 d */. int nData
1abe8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
1abe9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1abea 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62 of data to be b
1abeb 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 ound */. void (
1abec 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20 *xDel)(void*),
1abed 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 /* Destructor f
1abee 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 or the data */.
1abef 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20 20 u8 encoding
1abf0 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 /* Encod
1abf1 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 ing for the data
1abf2 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 */.){. Vdbe *p
1abf3 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 = (Vdbe *)pStmt
1abf4 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 ;. Mem *pVar;.
1abf5 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d int rc;.. rc =
1abf6 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 vdbeUnbind(p, i
1abf7 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1abf8 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
1abf9 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 ( zData!=0 ){.
1abfa 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 pVar = &p->a
1abfb 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 Var[i-1];.
1abfc 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1abfd 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c 20 MemSetStr(pVar,
1abfe 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e zData, nData, en
1abff 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20 coding, xDel);.
1ac00 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1ac01 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 ITE_OK && encodi
1ac02 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ng!=0 ){.
1ac03 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1ac04 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1ac05 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 pVar, ENC(p->db)
1ac06 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1ac07 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d sqlite3Error(p-
1ac08 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 >db, rc, 0);.
1ac09 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 rc = sqlite3A
1ac0a 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 63 piExit(p->db, rc
1ac0b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
1ac0c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1ac0d 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1ac0e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1ac0f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 .}.../*.** Bind
1ac10 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20 a blob value to
1ac11 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 an SQL statement
1ac12 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51 variable..*/.SQ
1ac13 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1ac14 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a ite3_bind_blob(.
1ac15 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1ac16 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c pStmt, . int i,
1ac17 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
1ac18 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 zData, . int nD
1ac19 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 ata, . void (*x
1ac1a 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 Del)(void*).){.
1ac1b 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 return bindText
1ac1c 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 (pStmt, i, zData
1ac1d 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 30 , nData, xDel, 0
1ac1e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1ac1f 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1ac20 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
1ac21 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1ac22 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 i, double rValu
1ac23 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 e){. int rc;.
1ac24 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 Vdbe *p = (Vdbe
1ac25 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 *)pStmt;. rc =
1ac26 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 vdbeUnbind(p, i)
1ac27 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1ac28 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c TE_OK ){. sql
1ac29 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f ite3VdbeMemSetDo
1ac2a 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d uble(&p->aVar[i-
1ac2b 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 20 1], rValue);.
1ac2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1ac2d 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
1ac2e 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
1ac2f 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 rc;.}.SQLITE_AP
1ac30 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1ac31 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 nd_int(sqlite3_s
1ac32 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69 tmt *p, int i, i
1ac33 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 65 nt iValue){. re
1ac34 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e turn sqlite3_bin
1ac35 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 69 d_int64(p, i, (i
1ac36 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 64)iValue);.}.SQ
1ac37 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1ac38 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 ite3_bind_int64(
1ac39 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1ac3a 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69 tmt, int i, sqli
1ac3b 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 29 te_int64 iValue)
1ac3c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 {. int rc;. Vd
1ac3d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 be *p = (Vdbe *)
1ac3e 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 pStmt;. rc = vd
1ac3f 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a beUnbind(p, i);.
1ac40 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ac41 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
1ac42 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1ac43 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 4(&p->aVar[i-1],
1ac44 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 iValue);. sq
1ac45 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1ac46 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1ac47 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1ac48 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1ac49 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1ac4a 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d null(sqlite3_stm
1ac4b 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1ac4c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 {. int rc;. Vd
1ac4d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 be *p = (Vdbe*)p
1ac4e 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 Stmt;. rc = vdb
1ac4f 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 eUnbind(p, i);.
1ac50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1ac51 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1ac52 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
1ac53 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
1ac54 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1ac55 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1ac56 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
1ac57 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d ( . sqlite3_stm
1ac58 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 t *pStmt, . int
1ac59 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 i, . const cha
1ac5a 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 r *zData, . int
1ac5b 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 nData, . void
1ac5c 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 (*xDel)(void*).)
1ac5d 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 {. return bindT
1ac5e 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 ext(pStmt, i, zD
1ac5f 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c ata, nData, xDel
1ac60 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a , SQLITE_UTF8);.
1ac61 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1ac62 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1ac63 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1ac64 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 0a e3_bind_text16(.
1ac65 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1ac66 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c pStmt, . int i,
1ac67 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
1ac68 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 zData, . int nD
1ac69 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 ata, . void (*x
1ac6a 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 Del)(void*).){.
1ac6b 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 return bindText
1ac6c 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 (pStmt, i, zData
1ac6d 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 , nData, xDel, S
1ac6e 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 QLITE_UTF16NATIV
1ac6f 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 E);.}.#endif /*
1ac70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1ac71 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 6 */.SQLITE_API
1ac72 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1ac73 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 _value(sqlite3_s
1ac74 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1ac75 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 i, const sqlite3
1ac76 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b _value *pValue){
1ac77 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 77 69 . int rc;. swi
1ac78 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74 79 70 tch( pValue->typ
1ac79 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 e ){. case SQ
1ac7a 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a LITE_INTEGER: {.
1ac7b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1ac7c 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 e3_bind_int64(pS
1ac7d 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e tmt, i, pValue->
1ac7e 75 2e 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 u.i);. brea
1ac7f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1ac80 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 e SQLITE_FLOAT:
1ac81 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1ac82 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 ite3_bind_double
1ac83 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 (pStmt, i, pValu
1ac84 65 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62 72 65 e->r);. bre
1ac85 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1ac86 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 se SQLITE_BLOB:
1ac87 7b 0a 20 20 20 20 20 20 69 66 28 20 70 56 61 6c {. if( pVal
1ac88 75 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ue->flags & MEM_
1ac89 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 Zero ){.
1ac8a 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e rc = sqlite3_bin
1ac8b 64 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74 6d 74 d_zeroblob(pStmt
1ac8c 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 6e , i, pValue->u.n
1ac8d 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d 65 6c Zero);. }el
1ac8e 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d se{. rc =
1ac8f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c sqlite3_bind_bl
1ac90 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 ob(pStmt, i, pVa
1ac91 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e lue->z, pValue->
1ac92 6e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 n,SQLITE_TRANSIE
1ac93 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 NT);. }.
1ac94 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1ac95 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
1ac96 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 TEXT: {. rc
1ac97 20 3d 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d = bindText(pStm
1ac98 74 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e 7a 2c t,i, pValue->z,
1ac99 20 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c 49 pValue->n, SQLI
1ac9a 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a 20 20 TE_TRANSIENT,.
1ac9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ac9c 20 20 20 20 20 20 20 20 20 20 20 20 70 56 61 6c pVal
1ac9d 75 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 ue->enc);.
1ac9e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1ac9f 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
1aca0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 rc = sqlite3_b
1aca1 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c 20 ind_null(pStmt,
1aca2 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b i);. break;
1aca3 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1aca4 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 urn rc;.}.SQLITE
1aca5 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1aca6 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 _bind_zeroblob(s
1aca7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1aca8 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e mt, int i, int n
1aca9 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 ){. int rc;. V
1acaa 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a dbe *p = (Vdbe *
1acab 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 )pStmt;. rc = v
1acac 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b dbeUnbind(p, i);
1acad 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1acae 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
1acaf 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 te3VdbeMemSetZer
1acb0 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69 oBlob(&p->aVar[i
1acb1 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c -1], n);. sql
1acb2 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1acb3 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1acb4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1acb5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1acb6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 the number of w
1acb7 69 6c 64 63 61 72 64 73 20 74 68 61 74 20 63 61 ildcards that ca
1acb8 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79 n be potentially
1acb9 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 bound to..** Th
1acba 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 64 is routine is ad
1acbb 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 44 ded to support D
1acbc 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f BD::SQLite. .*/
1acbd 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1acbe 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
1acbf 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c ameter_count(sql
1acc0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1acc1 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
1acc2 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 Vdbe*)pStmt;. r
1acc3 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 eturn p ? p->nVa
1acc4 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r : 0;.}../*.**
1acc5 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e 67 Create a mapping
1acc6 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e from variable n
1acc7 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61 62 umbers to variab
1acc8 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 le names.** in t
1acc9 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 he Vdbe.azVar[]
1acca 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 20 61 array, if such a
1accb 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f mapping does no
1accc 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69 t already.** exi
1accd 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f st..*/.static vo
1acce 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 id createVarMap(
1accf 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 Vdbe *p){. if(
1acd0 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 !p->okVar ){.
1acd1 20 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 20 2a int j;. Op *
1acd2 70 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 pOp;. sqlite3
1acd3 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e _mutex_enter(p->
1acd4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 db->mutex);.
1acd5 2f 2a 20 54 68 65 20 72 61 63 65 20 63 6f 6e 64 /* The race cond
1acd6 69 74 69 6f 6e 20 68 65 72 65 20 69 73 20 68 61 ition here is ha
1acd7 72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77 6f 20 rmless. If two
1acd8 74 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68 69 threads call thi
1acd9 73 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69 6e 65 s. ** routine
1acda 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 56 64 62 on the same Vdb
1acdb 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 e at the same ti
1acdc 6d 65 2c 20 74 68 65 79 20 62 6f 74 68 20 6d 69 me, they both mi
1acdd 67 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a 20 75 ght end. ** u
1acde 70 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 p initializing t
1acdf 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 he Vdbe.azVar[]
1ace0 61 72 72 61 79 2e 20 20 54 68 61 74 20 69 73 20 array. That is
1ace1 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 0a 20 a little extra.
1ace2 20 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74 20 69 ** work but i
1ace3 74 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 t results in the
1ace4 20 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a 20 20 same answer..
1ace5 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 */. for(j=0
1ace6 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c , pOp=p->aOp; j<
1ace7 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 p->nOp; j++, pOp
1ace8 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ++){. if( p
1ace9 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 Op->opcode==OP_V
1acea 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 ariable ){.
1aceb 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1acec 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c p1>0 && pOp->p1<
1aced 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20 =p->nVar );.
1acee 20 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 p->azVar[pOp
1acef 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70 ->p1-1] = pOp->p
1acf0 34 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 4.z;. }.
1acf1 20 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 }. p->okVar
1acf2 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 1;. sqlite3
1acf3 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
1acf4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
1acf5 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1acf6 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 69 the name of a wi
1acf7 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 ldcard parameter
1acf8 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 . Return NULL i
1acf9 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69 f the index.** i
1acfa 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f s out of range o
1acfb 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 72 r if the wildcar
1acfc 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a d is unnamed..**
1acfd 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 .** The result i
1acfe 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a s always UTF-8..
1acff 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1ad00 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1ad01 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
1ad02 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
1ad03 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1ad04 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
1ad05 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 Vdbe*)pStmt;. i
1ad06 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c f( p==0 || i<1 |
1ad07 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 | i>p->nVar ){.
1ad08 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1ad09 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 . createVarMap(
1ad0a 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p);. return p->
1ad0b 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f azVar[i-1];.}../
1ad0c 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 6c *.** Given a wil
1ad0d 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 dcard parameter
1ad0e 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 65 name, return the
1ad0f 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 61 index of the va
1ad10 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74 riable.** with t
1ad11 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 68 hat name. If th
1ad12 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 62 ere is no variab
1ad13 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65 le with the give
1ad14 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72 n name,.** retur
1ad15 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 n 0..*/.SQLITE_A
1ad16 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1ad17 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e ind_parameter_in
1ad18 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 dex(sqlite3_stmt
1ad19 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 *pStmt, const c
1ad1a 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 har *zName){. V
1ad1b 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 dbe *p = (Vdbe*)
1ad1c 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a pStmt;. int i;.
1ad1d 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 if( p==0 ){.
1ad1e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1ad1f 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70 createVarMap(p
1ad20 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 ); . if( zName
1ad21 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 ){. for(i=0;
1ad22 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b i<p->nVar; i++){
1ad23 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
1ad24 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b r *z = p->azVar[
1ad25 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 i];. if( z
1ad26 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d && strcmp(z,zNam
1ad27 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 e)==0 ){.
1ad28 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 return i+1;.
1ad29 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1ad2a 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
1ad2b 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c .** Transfer all
1ad2c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74 bindings from t
1ad2d 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 he first stateme
1ad2e 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 73 nt over to the s
1ad2f 65 63 6f 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 econd..*/.SQLITE
1ad30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1ad31 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 ite3TransferBind
1ad32 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ings(sqlite3_stm
1ad33 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 t *pFromStmt, sq
1ad34 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 lite3_stmt *pToS
1ad35 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 tmt){. Vdbe *pF
1ad36 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 rom = (Vdbe*)pFr
1ad37 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a omStmt;. Vdbe *
1ad38 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f pTo = (Vdbe*)pTo
1ad39 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 Stmt;. int i;.
1ad3a 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 62 assert( pTo->db
1ad3b 3d 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a 20 ==pFrom->db );.
1ad3c 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 6e 56 assert( pTo->nV
1ad3d 61 72 3d 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72 20 ar==pFrom->nVar
1ad3e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
1ad3f 65 78 5f 65 6e 74 65 72 28 70 54 6f 2d 3e 64 62 ex_enter(pTo->db
1ad40 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 ->mutex);. for(
1ad41 69 3d 30 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56 i=0; i<pFrom->nV
1ad42 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 ar; i++){. sq
1ad43 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
1ad44 28 26 70 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20 (&pTo->aVar[i],
1ad45 26 70 46 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29 &pFrom->aVar[i])
1ad46 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
1ad47 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 54 6f 2d mutex_leave(pTo-
1ad48 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 >db->mutex);. r
1ad49 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1ad4a 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
1ad4b 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 TE_OMIT_DEPRECAT
1ad4c 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 ED./*.** Depreca
1ad4d 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 ted external int
1ad4e 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 erface. Interna
1ad4f 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f l/core SQLite co
1ad50 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c de.** should cal
1ad51 6c 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 l sqlite3Transfe
1ad52 72 42 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a rBindings..**.**
1ad53 20 49 73 20 69 73 20 6d 69 73 75 73 65 20 74 6f Is is misuse to
1ad54 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 call this routi
1ad55 6e 65 20 77 69 74 68 20 73 74 61 74 65 6d 65 6e ne with statemen
1ad56 74 73 20 66 72 6f 6d 20 64 69 66 66 65 72 65 6e ts from differen
1ad57 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f t.** database co
1ad58 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 nnections. But
1ad59 61 73 20 74 68 69 73 20 69 73 20 61 20 64 65 70 as this is a dep
1ad5a 72 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 recated interfac
1ad5b 65 2c 20 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e 6f e, we.** will no
1ad5c 74 20 62 6f 74 68 65 72 20 74 6f 20 63 68 65 63 t bother to chec
1ad5d 6b 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 64 69 k for that condi
1ad5e 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 tion..**.** If t
1ad5f 68 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 he two statement
1ad60 73 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 s contain a diff
1ad61 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 erent number of
1ad62 62 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a bindings, then.*
1ad63 2a 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f * an SQLITE_ERRO
1ad64 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 R is returned.
1ad65 4e 6f 74 68 69 6e 67 20 65 6c 73 65 20 63 61 6e Nothing else can
1ad66 20 67 6f 20 77 72 6f 6e 67 2c 20 73 6f 20 6f 74 go wrong, so ot
1ad67 68 65 72 77 69 73 65 0a 2a 2a 20 53 51 4c 49 54 herwise.** SQLIT
1ad68 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
1ad69 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1ad6a 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e int sqlite3_tran
1ad6b 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 sfer_bindings(sq
1ad6c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f lite3_stmt *pFro
1ad6d 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 mStmt, sqlite3_s
1ad6e 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 tmt *pToStmt){.
1ad6f 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 Vdbe *pFrom = (
1ad70 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b Vdbe*)pFromStmt;
1ad71 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 . Vdbe *pTo = (
1ad72 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 Vdbe*)pToStmt;.
1ad73 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 if( pFrom->nVar
1ad74 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20 !=pTo->nVar ){.
1ad75 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1ad76 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 _ERROR;. }. re
1ad77 74 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e turn sqlite3Tran
1ad78 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72 sferBindings(pFr
1ad79 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 omStmt, pToStmt)
1ad7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1ad7b 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c * Return the sql
1ad7c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 ite3* database h
1ad7d 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74 andle to which t
1ad7e 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 he prepared stat
1ad7f 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 ement given.** i
1ad80 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62 n the argument b
1ad81 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73 elongs. This is
1ad82 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
1ad83 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 se handle that w
1ad84 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 as.** the first
1ad85 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
1ad86 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
1ad87 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 ) that was used
1ad88 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 to create.** the
1ad89 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 statement in th
1ad8a 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a e first place..*
1ad8b 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
1ad8c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 ite3 *sqlite3_db
1ad8d 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f _handle(sqlite3_
1ad8e 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
1ad8f 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 return pStmt ? (
1ad90 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 (Vdbe*)pStmt)->d
1ad91 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 b : 0;.}../*.**
1ad92 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
1ad93 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65 to the next pre
1ad94 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
1ad95 61 66 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f after pStmt asso
1ad96 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 ciated.** with d
1ad97 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1ad98 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d on pDb. If pStm
1ad99 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 t is NULL, retur
1ad9a 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 n the first.** p
1ad9b 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1ad9c 74 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 t for the databa
1ad9d 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 se connection.
1ad9e 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 Return NULL if t
1ad9f 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d here.** are no m
1ada0 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ore..*/.SQLITE_A
1ada1 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 PI sqlite3_stmt
1ada2 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 *sqlite3_next_st
1ada3 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c mt(sqlite3 *pDb,
1ada4 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
1ada5 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 Stmt){. sqlite3
1ada6 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 _stmt *pNext;.
1ada7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1ada8 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b ter(pDb->mutex);
1ada9 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 . if( pStmt==0
1adaa 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 ){. pNext = (
1adab 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 sqlite3_stmt*)pD
1adac 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 b->pVdbe;. }els
1adad 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 e{. pNext = (
1adae 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 sqlite3_stmt*)((
1adaf 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e Vdbe*)pStmt)->pN
1adb0 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ext;. }. sqlit
1adb1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1adb2 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 Db->mutex);. re
1adb3 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f turn pNext;.}../
1adb4 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1adb5 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
1adb6 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 s counter for a
1adb7 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1adb8 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 nt.*/.SQLITE_API
1adb9 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d int sqlite3_stm
1adba 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 t_status(sqlite3
1adbb 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1adbc 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46 t op, int resetF
1adbd 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 lag){. Vdbe *pV
1adbe 64 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74 dbe = (Vdbe*)pSt
1adbf 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 mt;. int v = pV
1adc0 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 dbe->aCounter[op
1adc1 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 -1];. if( reset
1adc2 46 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43 Flag ) pVdbe->aC
1adc3 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 ounter[op-1] = 0
1adc4 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a ;. return v;.}.
1adc5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1adc6 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e End of vdbeapi.
1adc7 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1adc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1adca 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1adcb 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
1adcc 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1adcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1adce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1adcf 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
1add0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
1add1 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1add2 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1add3 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1add4 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1add5 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1add6 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1add7 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1add8 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1add9 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1adda 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1addb 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1addc 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1addd 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1adde 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1addf 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1ade0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1ade1 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1ade2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ade3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ade4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ade5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ade6 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 *******.** The c
1ade7 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ode in this file
1ade8 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 implements exec
1ade9 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 ution method of
1adea 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 the .** Virtual
1adeb 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20 Database Engine
1adec 28 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 72 (VDBE). A separ
1aded 61 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 61 ate file ("vdbea
1adee 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 ux.c").** handle
1adef 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 s housekeeping d
1adf0 65 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 63 etails such as c
1adf1 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 reating and dele
1adf2 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 ting.** VDBE ins
1adf3 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69 tances. This fi
1adf4 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 le is solely int
1adf5 65 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 75 erested in execu
1adf6 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 ting.** the VDBE
1adf7 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 program..**.**
1adf8 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20 In the external
1adf9 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73 interface, an "s
1adfa 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 qlite3_stmt*" is
1adfb 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 an opaque point
1adfc 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e er.** to a VDBE.
1adfd 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 .**.** The SQL p
1adfe 61 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 arser generates
1adff 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20 a program which
1ae00 69 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 64 is then executed
1ae01 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 by.** the VDBE
1ae02 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f to do the work o
1ae03 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d f the SQL statem
1ae04 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72 ent. VDBE progr
1ae05 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 ams are .** simi
1ae06 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 lar in form to a
1ae07 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 ssembly language
1ae08 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 . The program c
1ae09 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 onsists of.** a
1ae0a 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 linear sequence
1ae0b 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 of operations.
1ae0c 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 Each operation h
1ae0d 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a as an opcode .**
1ae0e 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e and 5 operands.
1ae0f 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 Operands P1, P
1ae10 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 2, and P3 are in
1ae11 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 tegers. Operand
1ae12 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c P4 .** is a nul
1ae13 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 l-terminated str
1ae14 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 ing. Operand P5
1ae15 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 is an unsigned
1ae16 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 character..** Fe
1ae17 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c w opcodes use al
1ae18 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a l 5 operands..**
1ae19 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 .** Computation
1ae1a 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 results are stor
1ae1b 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 ed on a set of r
1ae1c 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65 egisters numbere
1ae1d 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 d beginning.** w
1ae1e 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 ith 1 and going
1ae1f 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e up to Vdbe.nMem.
1ae20 20 20 45 61 63 68 20 72 65 67 69 73 74 65 72 20 Each register
1ae21 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 can store.** eit
1ae22 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 her an integer,
1ae23 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 a null-terminate
1ae24 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 d string, a floa
1ae25 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 ting point.** nu
1ae26 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c mber, or the SQL
1ae27 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 "NULL" value.
1ae28 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 An implicit conv
1ae29 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a ersion from one.
1ae2a 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f ** type to the o
1ae2b 74 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e ther occurs as n
1ae2c 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a ecessary..** .**
1ae2d 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 Most of the cod
1ae2e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
1ae2f 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68 s taken up by th
1ae30 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 e sqlite3VdbeExe
1ae31 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 c().** function
1ae32 77 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 77 which does the w
1ae33 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74 ork of interpret
1ae34 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 ing a VDBE progr
1ae35 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 am..** But other
1ae36 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c routines are al
1ae37 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68 so provided to h
1ae38 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 elp in building
1ae39 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 up.** a program
1ae3a 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69 instruction by i
1ae3b 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1ae3c 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 * Various script
1ae3d 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 s scan this sour
1ae3e 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 ce file in order
1ae3f 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d to generate HTM
1ae40 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 L.** documentati
1ae41 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 on, headers file
1ae42 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 s, or other deri
1ae43 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 ved files. The
1ae44 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 formatting.** of
1ae45 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 the code in thi
1ae46 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65 s file is, there
1ae47 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e fore, important.
1ae48 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d See other comm
1ae49 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 ents.** in this
1ae4a 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 file for details
1ae4b 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 . If in doubt,
1ae4c 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 do not deviate f
1ae4d 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 rom existing.**
1ae4e 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 commenting and i
1ae4f 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 ndentation pract
1ae50 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69 ices when changi
1ae51 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 ng or adding cod
1ae52 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 e..**.** $Id: vd
1ae53 62 65 2e 63 2c 76 20 31 2e 38 34 32 20 32 30 30 be.c,v 1.842 200
1ae54 39 2f 30 35 2f 30 36 20 31 38 3a 35 37 3a 31 30 9/05/06 18:57:10
1ae55 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a shane Exp $.*/.
1ae56 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
1ae57 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 wing global vari
1ae58 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e able is incremen
1ae59 74 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 ted every time a
1ae5a 20 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 cursor.** moves
1ae5b 2c 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20 , either by the
1ae5c 4f 50 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 OP_SeekXX, OP_Ne
1ae5d 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f xt, or OP_Prev o
1ae5e 70 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 pcodes. The tes
1ae5f 74 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 t.** procedures
1ae60 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 use this informa
1ae61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 tion to make sur
1ae62 65 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 e that indices a
1ae63 72 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f re.** working co
1ae64 72 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 rrectly. This v
1ae65 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 ariable has no f
1ae66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 unction other th
1ae67 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 an to.** help ve
1ae68 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 rify the correct
1ae69 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 operation of th
1ae6a 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 e library..*/.#i
1ae6b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1ae6c 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1ae6d 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 sqlite3_search_c
1ae6e 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
1ae6f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 ../*.** When thi
1ae70 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c s global variabl
1ae71 65 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 e is positive, i
1ae72 74 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 t gets decrement
1ae73 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a ed once before.*
1ae74 2a 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 * each instructi
1ae75 6f 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 on in the VDBE.
1ae76 20 57 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 When reaches ze
1ae77 72 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 ro, the u1.isInt
1ae78 65 72 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c errupted.** fiel
1ae79 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 d of the sqlite3
1ae7a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 65 structure is se
1ae7b 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 t in order to si
1ae7c 6d 75 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 mulate and inter
1ae7d 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rupt..**.** This
1ae7e 20 66 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 facility is use
1ae7f 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 d for testing pu
1ae80 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 rposes only. It
1ae81 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 does not functi
1ae82 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 on.** in an ordi
1ae83 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 nary build..*/.#
1ae84 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1ae85 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
1ae86 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1ae87 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 pt_count = 0;.#e
1ae88 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1ae89 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 next global vari
1ae8a 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e able is incremen
1ae8b 74 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68 ted each type th
1ae8c 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 e OP_Sort opcode
1ae8d 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e .** is executed.
1ae8e 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 The test proce
1ae8f 64 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 dures use this i
1ae90 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 nformation to ma
1ae91 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 ke sure that.**
1ae92 73 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 sorting is occur
1ae93 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 ring or not occu
1ae94 72 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 rring at appropr
1ae95 69 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 iate times. Th
1ae96 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 is variable.** h
1ae97 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f as no function o
1ae98 74 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c ther than to hel
1ae99 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 p verify the cor
1ae9a 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f rect operation o
1ae9b 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 f the.** library
1ae9c 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
1ae9d 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
1ae9e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
1ae9f 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 ort_count = 0;.#
1aea0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
1aea1 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 next global var
1aea2 69 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 iable records th
1aea3 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 e size of the la
1aea4 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a rgest MEM_Blob.*
1aea5 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 * or MEM_Str tha
1aea6 74 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 t has been used
1aea7 62 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 by a VDBE opcode
1aea8 2e 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 . The test proc
1aea9 65 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 edures.** use th
1aeaa 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 is information t
1aeab 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
1aeac 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 the zero-blob f
1aead 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 unctionality.**
1aeae 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 is working corre
1aeaf 63 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 ctly. This var
1aeb0 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e iable has no fun
1aeb1 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e ction other than
1aeb2 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 to.** help veri
1aeb3 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f fy the correct o
1aeb4 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 peration of the
1aeb5 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 library..*/.#ifd
1aeb6 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
1aeb7 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1aeb8 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 lite3_max_blobsi
1aeb9 7a 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 ze = 0;.static v
1aeba 6f 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f oid updateMaxBlo
1aebb 62 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 bsize(Mem *p){.
1aebc 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 if( (p->flags &
1aebd 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c (MEM_Str|MEM_Bl
1aebe 6f 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e ob))!=0 && p->n>
1aebf 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 sqlite3_max_blob
1aec0 73 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 size ){. sqli
1aec1 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 te3_max_blobsize
1aec2 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 = p->n;. }.}.#
1aec3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 endif../*.** Tes
1aec4 74 20 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 t a register to
1aec5 73 65 65 20 69 66 20 69 74 20 65 78 63 65 65 64 see if it exceed
1aec6 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 s the current ma
1aec7 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e ximum blob size.
1aec8 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 .** If it does,
1aec9 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d record the new m
1aeca 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 aximum blob size
1aecb 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
1aecc 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 (SQLITE_TEST) &&
1aecd 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1aece 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 _OMIT_BUILTIN_TE
1aecf 53 54 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 ST).# define UPD
1aed0 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1aed1 28 50 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c (P) updateMaxBl
1aed2 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a obsize(P).#else.
1aed3 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f # define UPDATE_
1aed4 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a MAX_BLOBSIZE(P).
1aed5 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f #endif../*.** Co
1aed6 6e 76 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 nvert the given
1aed7 72 65 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 register into a
1aed8 73 74 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e string if it isn
1aed9 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 't one.** alread
1aeda 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 y. Return non-ze
1aedb 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 ro if a malloc()
1aedc 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 fails..*/.#defi
1aedd 6e 65 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 ne Stringify(P,
1aede 65 6e 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 enc) \. if(((P
1aedf 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 )->flags&(MEM_St
1aee0 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 r|MEM_Blob))==0
1aee1 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 && sqlite3VdbeMe
1aee2 6d 53 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 mStringify(P,enc
1aee3 29 29 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f )) \. { goto
1aee4 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a no_mem; }../*.*
1aee5 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 * An ephemeral s
1aee6 74 72 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 tring value (sig
1aee7 6e 69 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 nified by the ME
1aee8 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f M_Ephem flag) co
1aee9 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e ntains.** a poin
1aeea 74 65 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 ter to a dynamic
1aeeb 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 ally allocated s
1aeec 74 72 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 tring where some
1aeed 20 6f 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a other entity.**
1aeee 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
1aeef 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 for deallocating
1aef0 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 that string. B
1aef1 65 63 61 75 73 65 20 74 68 65 20 72 65 67 69 73 ecause the regis
1aef2 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 ter.** does not
1aef3 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 control the stri
1aef4 6e 67 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 ng, it might be
1aef5 64 65 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 deleted without
1aef6 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 the register.**
1aef7 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a knowing it..**.*
1aef8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
1aef9 6f 6e 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d onverts an ephem
1aefa 65 72 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f eral string into
1aefb 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 a dynamically a
1aefc 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 llocated.** stri
1aefd 6e 67 20 74 68 61 74 20 74 68 65 20 72 65 67 69 ng that the regi
1aefe 73 74 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 ster itself cont
1aeff 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 rols. In other
1af00 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e words, it.** con
1af01 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 verts an MEM_Eph
1af02 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 em string into a
1af03 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 n MEM_Dyn string
1af04 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 ..*/.#define Dee
1af05 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c phemeralize(P) \
1af06 0a 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c . if( ((P)->fl
1af07 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d ags&MEM_Ephem)!=
1af08 30 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 0 \. && sq
1af09 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 lite3VdbeMemMake
1af0a 57 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 Writeable(P) ){
1af0b 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f goto no_mem;}../
1af0c 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 *.** Call sqlite
1af0d 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
1af0e 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 ob() on the supp
1af0f 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 lied value (type
1af10 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 Mem*).** P if r
1af11 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 equired..*/.#def
1af12 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 ine ExpandBlob(P
1af13 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d ) (((P)->flags&M
1af14 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 EM_Zero)?sqlite3
1af15 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
1af16 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 b(P):0)../*.** A
1af17 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 rgument pMem poi
1af18 6e 74 73 20 61 74 20 61 20 72 65 67 69 73 74 65 nts at a registe
1af19 72 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 r that will be p
1af1a 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 assed to a.** us
1af1b 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 er-defined funct
1af1c 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 ion or returned
1af1d 74 6f 20 74 68 65 20 75 73 65 72 20 61 73 20 74 to the user as t
1af1e 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 he result of a q
1af1f 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 uery..** The sec
1af20 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64 ond argument, 'd
1af21 62 5f 65 6e 63 27 20 69 73 20 74 68 65 20 74 65 b_enc' is the te
1af22 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 xt encoding used
1af23 20 62 79 20 74 68 65 20 76 64 62 65 20 66 6f 72 by the vdbe for
1af24 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 76 61 72 .** register var
1af25 69 61 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f iables. This ro
1af26 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 utine sets the p
1af27 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65 Mem->enc and pMe
1af28 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61 m->type.** varia
1af29 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 65 bles used by the
1af2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a sqlite3_value_*
1af2b 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a () routines..*/.
1af2c 23 64 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70 #define storeTyp
1af2d 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72 eInfo(A,B) _stor
1af2e 65 54 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61 eTypeInfo(A).sta
1af2f 74 69 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54 tic void _storeT
1af30 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 ypeInfo(Mem *pMe
1af31 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 m){. int flags
1af32 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 = pMem->flags;.
1af33 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d if( flags & MEM
1af34 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 _Null ){. pMe
1af35 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1af36 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 _NULL;. }. els
1af37 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 e if( flags & ME
1af38 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 M_Int ){. pMe
1af39 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1af3a 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 _INTEGER;. }.
1af3b 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
1af3c 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 MEM_Real ){.
1af3d 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
1af3e 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a LITE_FLOAT;. }.
1af3f 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 else if( flags
1af40 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 & MEM_Str ){.
1af41 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1af42 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 QLITE_TEXT;. }e
1af43 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 lse{. pMem->t
1af44 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f ype = SQLITE_BLO
1af45 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 B;. }.}../*.**
1af46 50 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 Properties of op
1af47 63 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c codes. The OPFL
1af48 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 G_INITIALIZER ma
1af49 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 cro is.** create
1af4a 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 d by mkopcodeh.a
1af4b 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c wk during compil
1af4c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 ation. Data is
1af4d 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d obtained.** from
1af4e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f the comments fo
1af4f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 llowing the "cas
1af50 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 e OP_xxxx:" stat
1af51 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69 ements in.** thi
1af52 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 s file. .*/.sta
1af53 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
1af54 65 64 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72 ed char opcodePr
1af55 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 operty[] = OPFLG
1af56 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f _INITIALIZER;../
1af57 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
1af58 20 69 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61 if an opcode ha
1af59 73 20 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46 s any of the OPF
1af5a 4c 47 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65 LG_xxx propertie
1af5b 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 s.** specified b
1af5c 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 y mask..*/.SQLIT
1af5d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1af5e 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 lite3VdbeOpcodeH
1af5f 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f asProperty(int o
1af60 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29 pcode, int mask)
1af61 7b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f {. assert( opco
1af62 64 65 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28 de>0 && opcode<(
1af63 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64 int)sizeof(opcod
1af64 65 50 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20 eProperty) );.
1af65 72 65 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72 return (opcodePr
1af66 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d operty[opcode]&m
1af67 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a ask)!=0;.}../*.*
1af68 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 * Allocate VdbeC
1af69 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 ursor number iCu
1af6a 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 r. Return a poi
1af6b 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 nter to it. Ret
1af6c 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 urn NULL.** if w
1af6d 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d e run out of mem
1af6e 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 ory..*/.static V
1af6f 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 dbeCursor *alloc
1af70 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 ateCursor(. Vdb
1af71 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 e *p,
1af72 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
1af73 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 l machine */. i
1af74 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 nt iCur,
1af75 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1af76 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 the new VdbeCur
1af77 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 sor */. int nFi
1af78 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f eld, /
1af79 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * Number of fiel
1af7a 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ds in the table
1af7b 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e or index */. in
1af7c 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 t iDb,
1af7d 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61 /* When data
1af7e 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 20 base the cursor
1af7f 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d belongs to, or -
1af80 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 1 */. int isBtr
1af81 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 eeCursor /*
1af82 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 */.){. /* Find
1af83 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
1af84 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 that will be use
1af85 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 d to store the b
1af86 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 lob of memory.
1af87 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 ** required for
1af88 74 68 69 73 20 56 64 62 65 43 75 72 73 6f 72 20 this VdbeCursor
1af89 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 structure. It is
1af8a 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 convenient to u
1af8b 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 se a . ** vdbe
1af8c 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d memory cell to m
1af8d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 anage the memory
1af8e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 allocation requ
1af8f 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 ired for a. **
1af90 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75 63 VdbeCursor struc
1af91 74 75 72 65 20 66 6f 72 20 74 68 65 20 66 6f 6c ture for the fol
1af92 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a lowing reasons:.
1af93 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f **. ** * So
1af94 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72 20 6e metimes cursor n
1af95 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 20 umbers are used
1af96 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 for a couple of
1af97 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 different. **
1af98 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e 20 61 purposes in a
1af99 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 vdbe program. T
1af9a 68 65 20 64 69 66 66 65 72 65 6e 74 20 75 73 65 he different use
1af9b 73 20 6d 69 67 68 74 20 72 65 71 75 69 72 65 0a s might require.
1af9c 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65 72 65 ** differe
1af9d 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 nt sized allocat
1af9e 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c ions. Memory cel
1af9f 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f 77 61 ls provide growa
1afa0 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c ble. ** all
1afa1 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 ocations.. **.
1afa2 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 69 ** * When usi
1afa3 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 ng ENABLE_MEMORY
1afa4 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d _MANAGEMENT, mem
1afa5 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 72 73 ory cell buffers
1afa6 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 can. ** be
1afa7 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 freed lazily vi
1afa8 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 a the sqlite3_re
1afa9 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 lease_memory() A
1afaa 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 PI. This. **
1afab 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 minimizes the
1afac 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 number of malloc
1afad 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 calls made by t
1afae 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a he system.. **.
1afaf 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c ** Memory cell
1afb0 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20 61 72 s for cursors ar
1afb1 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 e allocated at t
1afb2 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 61 64 he top of the ad
1afb3 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 dress. ** space
1afb4 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 . Memory cell (p
1afb5 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f ->nMem) correspo
1afb6 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20 30 2e nds to cursor 0.
1afb7 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 Space for. **
1afb8 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 cursor 1 is mana
1afb9 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 ged by memory ce
1afba 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 ll (p->nMem-1),
1afbb 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 etc.. */. Mem
1afbc 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d *pMem = &p->aMem
1afbd 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a [p->nMem-iCur];.
1afbe 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 . int nByte;.
1afbf 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 20 VdbeCursor *pCx
1afc0 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 0a = 0;. nByte = .
1afc1 20 20 20 20 20 20 73 69 7a 65 6f 66 28 56 64 62 sizeof(Vdb
1afc2 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 20 20 20 eCursor) + .
1afc3 20 20 28 69 73 42 74 72 65 65 43 75 72 73 6f 72 (isBtreeCursor
1afc4 3f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 ?sqlite3BtreeCur
1afc5 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 2b 20 0a sorSize():0) + .
1afc6 20 20 20 20 20 20 32 2a 6e 46 69 65 6c 64 2a 73 2*nField*s
1afc7 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a 20 20 61 izeof(u32);.. a
1afc8 73 73 65 72 74 28 20 69 43 75 72 3c 70 2d 3e 6e ssert( iCur<p->n
1afc9 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 Cursor );. if(
1afca 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 29 p->apCsr[iCur] )
1afcb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1afcc 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 eFreeCursor(p, p
1afcd 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 29 3b 0a ->apCsr[iCur]);.
1afce 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 p->apCsr[iCu
1afcf 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 r] = 0;. }. if
1afd0 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c ( SQLITE_OK==sql
1afd1 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1afd2 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 pMem, nByte, 0)
1afd3 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b ){. p->apCsr[
1afd4 69 43 75 72 5d 20 3d 20 70 43 78 20 3d 20 28 56 iCur] = pCx = (V
1afd5 64 62 65 43 75 72 73 6f 72 2a 29 70 4d 65 6d 2d dbeCursor*)pMem-
1afd6 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 >z;. memset(p
1afd7 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65 Mem->z, 0, nByte
1afd8 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 20 );. pCx->iDb
1afd9 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78 2d 3e = iDb;. pCx->
1afda 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b nField = nField;
1afdb 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c 64 20 . if( nField
1afdc 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 54 ){. pCx->aT
1afdd 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26 70 4d ype = (u32 *)&pM
1afde 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62 em->z[sizeof(Vdb
1afdf 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 20 20 7d eCursor)];. }
1afe0 0a 20 20 20 20 69 66 28 20 69 73 42 74 72 65 65 . if( isBtree
1afe1 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 Cursor ){.
1afe2 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 3d 20 28 pCx->pCursor = (
1afe3 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 20 20 20 BtCursor*).
1afe4 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b 73 69 &pMem->z[si
1afe5 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 zeof(VdbeCursor)
1afe6 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 +2*nField*sizeof
1afe7 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 (u32)];. }.
1afe8 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a }. return pCx;.
1afe9 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 }../*.** Try to
1afea 63 6f 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20 convert a value
1afeb 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 into a numeric r
1afec 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 epresentation if
1afed 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f we can.** do so
1afee 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 without loss of
1afef 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 information. I
1aff0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 n other words, i
1aff1 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 f the string.**
1aff2 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d looks like a num
1aff3 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 ber, convert it
1aff4 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20 into a number.
1aff5 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a If it does not.*
1aff6 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 * look like a nu
1aff7 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61 mber, leave it a
1aff8 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lone..*/.static
1aff9 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69 void applyNumeri
1affa 63 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 cAffinity(Mem *p
1affb 52 65 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65 Rec){. if( (pRe
1affc 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f c->flags & (MEM_
1affd 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d Real|MEM_Int))==
1affe 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61 0 ){. int rea
1afff 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 lnum;. sqlite
1b000 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 3VdbeMemNulTermi
1b001 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20 nate(pRec);.
1b002 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 if( (pRec->flags
1b003 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20 &MEM_Str).
1b004 20 20 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e && sqlite3IsN
1b005 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26 umber(pRec->z, &
1b006 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 realnum, pRec->e
1b007 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 nc) ){. i64
1b008 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71 value;. sq
1b009 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
1b00a 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51 ncoding(pRec, SQ
1b00b 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 LITE_UTF8);.
1b00c 20 20 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26 if( !realnum &
1b00d 26 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 & sqlite3Atoi64(
1b00e 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 pRec->z, &value)
1b00f 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63 ){. pRec
1b010 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20 ->u.i = value;.
1b011 20 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 MemSetTyp
1b012 65 46 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f eFlag(pRec, MEM_
1b013 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Int);. }els
1b014 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e{. sqlit
1b015 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 e3VdbeMemRealify
1b016 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a (pRec);. }.
1b017 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1b018 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 ** Processing is
1b019 20 64 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 determine by th
1b01a 65 20 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d e affinity param
1b01b 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 eter:.**.** SQLI
1b01c 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a TE_AFF_INTEGER:.
1b01d 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 ** SQLITE_AFF_RE
1b01e 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 AL:.** SQLITE_AF
1b01f 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 F_NUMERIC:.**
1b020 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 Try to convert
1b021 70 52 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 pRec to an integ
1b022 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f er representatio
1b023 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c n or a .** fl
1b024 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 oating-point rep
1b025 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 resentation if a
1b026 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 n integer repres
1b027 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 entation.** i
1b028 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 s not possible.
1b029 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 69 Note that the i
1b02a 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 nteger represent
1b02b 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 ation is.** a
1b02c 6c 77 61 79 73 20 70 72 65 66 65 72 72 65 64 2c lways preferred,
1b02d 20 65 76 65 6e 20 69 66 20 74 68 65 20 61 66 66 even if the aff
1b02e 69 6e 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62 inity is REAL, b
1b02f 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 ecause.** an
1b030 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e integer represen
1b031 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 tation is more s
1b032 70 61 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f pace efficient o
1b033 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 n disk..**.** SQ
1b034 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a LITE_AFF_TEXT:.*
1b035 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65 * Convert pRe
1b036 63 20 74 6f 20 61 20 74 65 78 74 20 72 65 70 72 c to a text repr
1b037 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a esentation..**.*
1b038 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e * SQLITE_AFF_NON
1b039 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 E:.** No-op.
1b03a 20 70 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67 pRec is unchang
1b03b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
1b03c 69 64 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 id applyAffinity
1b03d 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20 (. Mem *pRec,
1b03e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 /* The v
1b03f 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66 alue to apply af
1b040 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63 finity to */. c
1b041 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20 har affinity,
1b042 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
1b043 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 ty to be applied
1b044 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20 */. u8 enc
1b045 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
1b046 20 74 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64 this text encod
1b047 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 ing */.){. if(
1b048 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
1b049 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20 _AFF_TEXT ){.
1b04a 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74 /* Only attempt
1b04b 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
1b04c 74 6f 20 54 45 58 54 20 69 66 20 74 68 65 72 65 to TEXT if there
1b04d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f is an integer o
1b04e 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65 r real. ** re
1b04f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c presentation (bl
1b050 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e ob and NULL do n
1b051 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64 ot get converted
1b052 29 20 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a ) but no string.
1b053 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 ** represent
1b054 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
1b055 20 20 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e if( 0==(pRec->
1b056 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26 flags&MEM_Str) &
1b057 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28 & (pRec->flags&(
1b058 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 MEM_Real|MEM_Int
1b059 29 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 )) ){. sqli
1b05a 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 te3VdbeMemString
1b05b 69 66 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a ify(pRec, enc);.
1b05c 20 20 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e }. pRec->
1b05d 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 flags &= ~(MEM_R
1b05e 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 eal|MEM_Int);.
1b05f 7d 65 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69 }else if( affini
1b060 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ty!=SQLITE_AFF_N
1b061 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ONE ){. asser
1b062 74 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c t( affinity==SQL
1b063 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 ITE_AFF_INTEGER
1b064 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c || affinity==SQL
1b065 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 ITE_AFF_REAL.
1b066 20 20 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 || aff
1b067 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 inity==SQLITE_AF
1b068 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 F_NUMERIC );.
1b069 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 applyNumericAff
1b06a 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 inity(pRec);.
1b06b 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 if( pRec->flags
1b06c 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 & MEM_Real ){.
1b06d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1b06e 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 IntegerAffinity(
1b06f 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pRec);. }. }
1b070 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f .}../*.** Try to
1b071 20 63 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70 convert the typ
1b072 65 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 e of a function
1b073 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65 argument or a re
1b074 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 sult column.** i
1b075 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 nto a numeric re
1b076 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55 presentation. U
1b077 73 65 20 65 69 74 68 65 72 20 49 4e 54 45 47 45 se either INTEGE
1b078 52 20 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65 R or REAL whiche
1b079 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70 ver.** is approp
1b07a 72 69 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79 riate. But only
1b07b 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 do the conversi
1b07c 6f 6e 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 on if it is poss
1b07d 69 62 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 ible without.**
1b07e 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 loss of informat
1b07f 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ion and return t
1b080 68 65 20 72 65 76 69 73 65 64 20 74 79 70 65 20 he revised type
1b081 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e of the argument.
1b082 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 .**.** This is a
1b083 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61 n EXPERIMENTAL a
1b084 70 69 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 pi and is subjec
1b085 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72 t to change or r
1b086 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 emoval..*/.SQLIT
1b087 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b088 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 3_value_numeric_
1b089 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c type(sqlite3_val
1b08a 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d ue *pVal){. Mem
1b08b 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 *pMem = (Mem*)p
1b08c 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 Val;. applyNume
1b08d 72 69 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d ricAffinity(pMem
1b08e 29 3b 0a 20 20 73 74 6f 72 65 54 79 70 65 49 6e );. storeTypeIn
1b08f 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a 20 20 72 fo(pMem, 0);. r
1b090 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 eturn pMem->type
1b091 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 ;.}../*.** Expor
1b092 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 ted version of a
1b093 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 2e 20 pplyAffinity().
1b094 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 6f This one works o
1b095 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a n sqlite3_value*
1b096 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69 6e , .** not the in
1b097 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70 65 ternal Mem* type
1b098 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b099 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1b09a 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 ValueApplyAffini
1b09b 74 79 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ty(. sqlite3_va
1b09c 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75 38 lue *pVal, . u8
1b09d 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 20 75 38 affinity, . u8
1b09e 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79 41 enc.){. applyA
1b09f 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29 70 ffinity((Mem *)p
1b0a0 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 Val, affinity, e
1b0a1 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 nc);.}..#ifdef S
1b0a2 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a QLITE_DEBUG./*.*
1b0a3 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65 20 73 * Write a nice s
1b0a4 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
1b0a5 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e 74 tion of the cont
1b0a6 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d 65 ents of cell pMe
1b0a7 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 72 m.** into buffer
1b0a8 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e 42 zBuf, length nB
1b0a9 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 uf..*/.SQLITE_PR
1b0aa 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1b0ab 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 e3VdbeMemPrettyP
1b0ac 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 rint(Mem *pMem,
1b0ad 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 63 char *zBuf){. c
1b0ae 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75 66 har *zCsr = zBuf
1b0af 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d 65 6d ;. int f = pMem
1b0b0 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61 74 ->flags;.. stat
1b0b1 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 ic const char *c
1b0b2 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 onst encnames[]
1b0b3 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29 22 2c = {"(X)", "(8)",
1b0b4 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31 36 42 "(16LE)", "(16B
1b0b5 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26 4d E)"};.. if( f&M
1b0b6 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20 69 EM_Blob ){. i
1b0b7 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 63 nt i;. char c
1b0b8 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 ;. if( f & ME
1b0b9 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 63 M_Dyn ){. c
1b0ba 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 = 'z';. as
1b0bb 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f sert( (f & (MEM_
1b0bc 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d Static|MEM_Ephem
1b0bd 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c ))==0 );. }el
1b0be 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 se if( f & MEM_S
1b0bf 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 63 tatic ){. c
1b0c0 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 = 't';. as
1b0c1 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f sert( (f & (MEM_
1b0c2 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d Dyn|MEM_Ephem))=
1b0c3 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 =0 );. }else
1b0c4 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 if( f & MEM_Ephe
1b0c5 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 m ){. c = '
1b0c6 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 e';. assert
1b0c7 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 ( (f & (MEM_Stat
1b0c8 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 ic|MEM_Dyn))==0
1b0c9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1b0ca 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20 20 20 c = 's';.
1b0cb 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }.. sqlite3_
1b0cc 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 snprintf(100, zC
1b0cd 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20 20 sr, "%c", c);.
1b0ce 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 zCsr += sqlite
1b0cf 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 3Strlen30(zCsr);
1b0d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
1b0d1 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c rintf(100, zCsr,
1b0d2 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 "%d[", pMem->n)
1b0d3 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 ;. zCsr += sq
1b0d4 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 lite3Strlen30(zC
1b0d5 73 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 sr);. for(i=0
1b0d6 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d ; i<16 && i<pMem
1b0d7 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 ->n; i++){.
1b0d8 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1b0d9 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 f(100, zCsr, "%0
1b0da 32 58 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 2X", ((int)pMem-
1b0db 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a >z[i] & 0xFF));.
1b0dc 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 zCsr += sq
1b0dd 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 lite3Strlen30(zC
1b0de 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 sr);. }. f
1b0df 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 or(i=0; i<16 &&
1b0e0 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b i<pMem->n; i++){
1b0e1 0a 20 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 . char z =
1b0e2 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 pMem->z[i];.
1b0e3 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e if( z<32 || z>
1b0e4 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 126 ) *zCsr++ =
1b0e5 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 '.';. else
1b0e6 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 *zCsr++ = z;.
1b0e7 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }.. sqlite3_
1b0e8 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 snprintf(100, zC
1b0e9 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 sr, "]%s", encna
1b0ea 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b mes[pMem->enc]);
1b0eb 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c . zCsr += sql
1b0ec 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 ite3Strlen30(zCs
1b0ed 72 29 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 r);. if( f &
1b0ee 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 MEM_Zero ){.
1b0ef 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1b0f0 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 tf(100, zCsr,"+%
1b0f1 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 dz",pMem->u.nZer
1b0f2 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b o);. zCsr +
1b0f3 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1b0f4 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 0(zCsr);. }.
1b0f5 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c 30 27 3b *zCsr = '\0';
1b0f6 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 . }else if( f &
1b0f7 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 MEM_Str ){.
1b0f8 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42 int j, k;. zB
1b0f9 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 uf[0] = ' ';.
1b0fa 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e if( f & MEM_Dyn
1b0fb 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 ){. zBuf[1
1b0fc 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 ] = 'z';. a
1b0fd 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d ssert( (f & (MEM
1b0fe 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 _Static|MEM_Ephe
1b0ff 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 m))==0 );. }e
1b100 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f lse if( f & MEM_
1b101 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 Static ){.
1b102 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b 0a 20 zBuf[1] = 't';.
1b103 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 assert( (f
1b104 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 & (MEM_Dyn|MEM_E
1b105 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 phem))==0 );.
1b106 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d }else if( f & M
1b107 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 EM_Ephem ){.
1b108 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 65 27 3b zBuf[1] = 'e';
1b109 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
1b10a 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c f & (MEM_Static|
1b10b 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a MEM_Dyn))==0 );.
1b10c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b10d 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 27 3b 0a zBuf[1] = 's';.
1b10e 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d 20 32 3b }. k = 2;
1b10f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
1b110 72 69 6e 74 66 28 31 30 30 2c 20 26 7a 42 75 66 rintf(100, &zBuf
1b111 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d 65 6d 2d [k], "%d", pMem-
1b112 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 >n);. k += sq
1b113 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a lite3Strlen30(&z
1b114 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 Buf[k]);. zBu
1b115 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20 f[k++] = '[';.
1b116 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 for(j=0; j<15
1b117 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b && j<pMem->n; j+
1b118 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 63 20 3d +){. u8 c =
1b119 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20 pMem->z[j];.
1b11a 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26 if( c>=0x20 &
1b11b 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 20 & c<0x7f ){.
1b11c 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 zBuf[k++] =
1b11d 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a c;. }else{.
1b11e 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b zBuf[k++
1b11f 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 7d ] = '.';. }
1b120 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b . }. zBuf[
1b121 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 20 k++] = ']';.
1b122 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1b123 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65 (100,&zBuf[k], e
1b124 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e ncnames[pMem->en
1b125 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 c]);. k += sq
1b126 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a lite3Strlen30(&z
1b127 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 Buf[k]);. zBu
1b128 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a f[k++] = 0;. }.
1b129 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 }.#endif..#ifdef
1b12a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
1b12b 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61 .** Print the va
1b12c 6c 75 65 20 6f 66 20 61 20 72 65 67 69 73 74 65 lue of a registe
1b12d 72 20 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75 r for tracing pu
1b12e 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 rposes:.*/.stati
1b12f 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50 c void memTraceP
1b130 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20 rint(FILE *out,
1b131 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 Mem *p){. if( p
1b132 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1b133 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 ll ){. fprint
1b134 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b f(out, " NULL");
1b135 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d . }else if( (p-
1b136 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e >flags & (MEM_In
1b137 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 t|MEM_Str))==(ME
1b138 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29 M_Int|MEM_Str) )
1b139 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 {. fprintf(ou
1b13a 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70 t, " si:%lld", p
1b13b 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 ->u.i);. }else
1b13c 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d if( p->flags & M
1b13d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70 EM_Int ){. fp
1b13e 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25 rintf(out, " i:%
1b13f 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 lld", p->u.i);.
1b140 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c }else if( p->fl
1b141 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
1b142 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 {. fprintf(ou
1b143 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72 t, " r:%g", p->r
1b144 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 );. }else if( p
1b145 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
1b146 77 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 wSet ){. fpri
1b147 6e 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 ntf(out, " (rows
1b148 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a et)");. }else{.
1b149 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 char zBuf[20
1b14a 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0];. sqlite3V
1b14b 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e dbeMemPrettyPrin
1b14c 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 t(p, zBuf);.
1b14d 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 fprintf(out, " "
1b14e 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f );. fprintf(o
1b14f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b ut, "%s", zBuf);
1b150 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f . }.}.static vo
1b151 69 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 id registerTrace
1b152 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 (FILE *out, int
1b153 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 iReg, Mem *p){.
1b154 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 fprintf(out, "R
1b155 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 EG[%d] = ", iReg
1b156 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 );. memTracePri
1b157 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 nt(out, p);. fp
1b158 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 rintf(out, "\n")
1b159 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 ;.}.#endif..#ifd
1b15a 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1b15b 23 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 # define REGIST
1b15c 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 ER_TRACE(R,M) if
1b15d 28 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 (p->trace)regist
1b15e 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 erTrace(p->trace
1b15f 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 ,R,M).#else.# d
1b160 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 efine REGISTER_T
1b161 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 RACE(R,M).#endif
1b162 0a 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 ...#ifdef VDBE_P
1b163 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 ROFILE../* .** h
1b164 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 wtime.h contains
1b165 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 inline assemble
1b166 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 r code for imple
1b167 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 menting .** high
1b168 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d -performance tim
1b169 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f ing routines..*/
1b16a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1b16b 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e Include hwtime.
1b16c 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
1b16d 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a of vdbe.c ******
1b16e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1b16f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1b170 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 Begin file hwti
1b171 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a me.h ***********
1b172 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1b174 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 ./*.** 2008 May
1b175 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 27.**.** The aut
1b176 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1b177 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1b178 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1b179 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1b17a 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1b17b 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1b17c 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1b17d 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1b17e 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
1b17f 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
1b180 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
1b181 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
1b182 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
1b183 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
1b184 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
1b185 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
1b186 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1b187 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b188 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b189 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b18a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b18b 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
1b18c 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1b18d 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 inline asm code
1b18e 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 for retrieving "
1b18f 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 high-performance
1b190 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f ".** counters fo
1b191 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 r x86 class CPUs
1b192 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 ..**.** $Id: hwt
1b193 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 ime.h,v 1.3 2008
1b194 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 /08/01 14:33:15
1b195 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 shane Exp $.*/.#
1b196 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 ifndef _HWTIME_H
1b197 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d _.#define _HWTIM
1b198 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 E_H_../*.** The
1b199 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
1b19a 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 e only works on
1b19b 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f pentium-class (o
1b19c 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 r newer) process
1b19d 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 ors..** It uses
1b19e 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 the RDTSC opcode
1b19f 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 to read the cyc
1b1a0 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f le count value o
1b1a1 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f ut of the.** pro
1b1a2 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 cessor and retur
1b1a3 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 ns that value.
1b1a4 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 This can be used
1b1a5 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a for high-res.**
1b1a6 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 profiling..*/.#
1b1a7 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
1b1a8 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 UC__) || defined
1b1a9 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c (_MSC_VER)) && \
1b1aa 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 . (defined(
1b1ab 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 i386) || defined
1b1ac 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 (__i386__) || de
1b1ad 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a fined(_M_IX86)).
1b1ae 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f . #if defined(_
1b1af 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e _GNUC__).. __in
1b1b0 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
1b1b1 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
1b1b2 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 me(void){. u
1b1b3 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 nsigned int lo,
1b1b4 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f hi;. __asm__
1b1b5 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
1b1b6 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c rdtsc" : "=a" (l
1b1b7 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a o), "=d" (hi));.
1b1b8 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c return (sql
1b1b9 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c ite_uint64)hi <<
1b1ba 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 32 | lo;. }..
1b1bb 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f #elif defined(_
1b1bc 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 MSC_VER).. __de
1b1bd 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f clspec(naked) __
1b1be 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 inline sqlite_ui
1b1bf 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c nt64 __cdecl sql
1b1c0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
1b1c1 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 {. __asm {.
1b1c2 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 rdtsc.
1b1c3 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b ret ;
1b1c4 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 return value at
1b1c5 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a EDX:EAX. }.
1b1c6 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 }.. #endif..#
1b1c7 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
1b1c8 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
1b1c9 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a ed(__x86_64__)).
1b1ca 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 . __inline__ sq
1b1cb 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
1b1cc 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
1b1cd 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
1b1ce 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 long val;.
1b1cf 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 __asm__ __volati
1b1d0 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 le__ ("rdtsc" :
1b1d1 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 "=A" (val));.
1b1d2 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 return val;.
1b1d3 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 }. .#elif (defi
1b1d4 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 ned(__GNUC__) &&
1b1d5 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f defined(__ppc__
1b1d6 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
1b1d7 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
1b1d8 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
1b1d9 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
1b1da 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 ed long long ret
1b1db 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 val;. unsig
1b1dc 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 ned long junk;.
1b1dd 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
1b1de 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a olatile__ ("\n\.
1b1df 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 1:
1b1e0 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a mftbu %1\n\.
1b1e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b1e2 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c mftb %L0\n\
1b1e3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b1e4 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c mftbu %0\n\
1b1e5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b1e6 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 cmpw %0,%1
1b1e7 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
1b1e8 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 bne 1b
1b1e9 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
1b1ea 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 : "=r" (retv
1b1eb 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 al), "=r" (junk)
1b1ec 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1b1ed 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c retval;. }..#el
1b1ee 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 se.. #error Nee
1b1ef 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e d implementation
1b1f0 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d of sqlite3Hwtim
1b1f1 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 e() for your pla
1b1f2 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a tform... /*. *
1b1f3 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 * To compile wit
1b1f4 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e hout implementin
1b1f5 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 g sqlite3Hwtime(
1b1f6 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
1b1f7 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 orm,. ** you ca
1b1f8 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f n remove the abo
1b1f9 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 ve #error and us
1b1fa 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a e the following.
1b1fb 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 ** stub functi
1b1fc 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f on. You will lo
1b1fd 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 se timing suppor
1b1fe 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 t for many. **
1b1ff 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 of the debugging
1b200 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 and testing uti
1b201 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 lities, but it s
1b202 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 hould at. ** le
1b203 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 ast compile and
1b204 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 run.. */.SQLITE
1b205 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 _PRIVATE sqlit
1b206 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
1b207 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 Hwtime(void){ re
1b208 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 turn ((sqlite_ui
1b209 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 nt64)0); }..#end
1b20a 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 if..#endif /* !d
1b20b 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 efined(_HWTIME_H
1b20c 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _) */../********
1b20d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 ****** End of hw
1b20e 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a time.h *********
1b20f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b211 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1b212 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
1b213 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
1b214 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 20 2a 2a off in vdbe.c **
1b215 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b216 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a *****/..#endif..
1b217 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f /*.** The CHECK_
1b218 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 6d 61 FOR_INTERRUPT ma
1b219 63 72 6f 20 64 65 66 69 6e 65 64 20 68 65 72 65 cro defined here
1b21a 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69 66 looks to see if
1b21b 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
1b21c 69 6e 74 65 72 72 75 70 74 28 29 20 72 6f 75 74 interrupt() rout
1b21d 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c ine has been cal
1b21e 6c 65 64 2e 20 20 49 66 20 69 74 20 68 61 73 20 led. If it has
1b21f 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 been, then.** pr
1b220 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65 20 ocessing of the
1b221 56 44 42 45 20 70 72 6f 67 72 61 6d 20 69 73 20 VDBE program is
1b222 69 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a 0a interrupted..**.
1b223 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 61 64 ** This macro ad
1b224 64 65 64 20 74 6f 20 65 76 65 72 79 20 69 6e 73 ded to every ins
1b225 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 64 6f truction that do
1b226 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72 64 es a jump in ord
1b227 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 er to.** impleme
1b228 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73 nt a loop. This
1b229 20 74 65 73 74 20 75 73 65 64 20 74 6f 20 62 65 test used to be
1b22a 20 6f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c 65 on every single
1b22b 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a instruction,.**
1b22c 20 62 75 74 20 74 68 61 74 20 6d 65 61 6e 74 20 but that meant
1b22d 77 65 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 20 we more testing
1b22e 74 68 61 74 20 77 65 20 6e 65 65 64 65 64 2e 20 that we needed.
1b22f 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67 By only testing
1b230 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 the.** flag on
1b231 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e jump instruction
1b232 73 2c 20 77 65 20 67 65 74 20 61 20 28 73 6d 61 s, we get a (sma
1b233 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 72 6f 76 ll) speed improv
1b234 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e ement..*/.#defin
1b235 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 e CHECK_FOR_INTE
1b236 52 52 55 50 54 20 5c 0a 20 20 20 69 66 28 20 64 RRUPT \. if( d
1b237 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 b->u1.isInterrup
1b238 74 65 64 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 ted ) goto abort
1b239 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 _due_to_interrup
1b23a 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 t;..#ifdef SQLIT
1b23b 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 E_DEBUG.static i
1b23c 6e 74 20 66 69 6c 65 45 78 69 73 74 73 28 73 71 nt fileExists(sq
1b23d 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
1b23e 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a 20 char *zFile){.
1b23f 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20 int res = 0;.
1b240 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1b241 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 OK;.#ifdef SQLIT
1b242 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66 20 77 E_TEST. /* If w
1b243 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 e are currently
1b244 74 65 73 74 69 6e 67 20 49 4f 20 65 72 72 6f 72 testing IO error
1b245 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 s, then do not c
1b246 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 29 20 74 all OsAccess() t
1b247 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f 72 20 o. ** test for
1b248 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 the presence of
1b249 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 73 20 62 zFile. This is b
1b24a 65 63 61 75 73 65 20 61 6e 79 20 49 4f 20 65 72 ecause any IO er
1b24b 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f 63 ror that. ** oc
1b24c 63 75 72 73 20 68 65 72 65 20 77 69 6c 6c 20 6e curs here will n
1b24d 6f 74 20 62 65 20 72 65 70 6f 72 74 65 64 2c 20 ot be reported,
1b24e 63 61 75 73 69 6e 67 20 74 68 65 20 74 65 73 74 causing the test
1b24f 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 to fail.. */.
1b250 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 extern int sqli
1b251 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
1b252 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c 69 ding;. if( sqli
1b253 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
1b254 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69 66 ding<=0 ).#endif
1b255 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1b256 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 3OsAccess(db->pV
1b257 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49 54 fs, zFile, SQLIT
1b258 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
1b259 20 26 72 65 73 29 3b 0a 20 20 72 65 74 75 72 6e &res);. return
1b25a 20 28 72 65 73 20 26 26 20 72 63 3d 3d 53 51 4c (res && rc==SQL
1b25b 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69 ITE_OK);.}.#endi
1b25c 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 f..#ifndef NDEBU
1b25d 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e G./*.** This fun
1b25e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 ction is only ca
1b25f 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
1b260 20 61 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 an assert() exp
1b261 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20 63 ression. It.** c
1b262 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 hecks that the s
1b263 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61 63 74 qlite3.nTransact
1b264 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 ion variable is
1b265 63 6f 72 72 65 63 74 6c 79 20 73 65 74 20 74 6f correctly set to
1b266 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
1b267 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f f non-transactio
1b268 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 63 75 72 n savepoints cur
1b269 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a 2a rently in the .*
1b26a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 * linked list st
1b26b 61 72 74 69 6e 67 20 61 74 20 73 71 6c 69 74 65 arting at sqlite
1b26c 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 3.pSavepoint..**
1b26d 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a .** Usage:.**.*
1b26e 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 63 68 * assert( ch
1b26f 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e eckSavepointCoun
1b270 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 t(db) );.*/.stat
1b271 69 63 20 69 6e 74 20 63 68 65 63 6b 53 61 76 65 ic int checkSave
1b272 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69 74 pointCount(sqlit
1b273 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 6e e3 *db){. int n
1b274 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f 69 6e = 0;. Savepoin
1b275 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64 62 t *p;. for(p=db
1b276 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 70 3b ->pSavepoint; p;
1b277 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b 2b p=p->pNext) n++
1b278 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 28 ;. assert( n==(
1b279 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b db->nSavepoint +
1b27a 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 db->isTransacti
1b27b 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a onSavepoint) );.
1b27c 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
1b27d 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 ndif../*.** Exec
1b27e 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20 61 ute as much of a
1b27f 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 73 VDBE program as
1b280 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65 74 we can then ret
1b281 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 urn..**.** sqlit
1b282 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 e3VdbeMakeReady(
1b283 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 ) must be called
1b284 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 before this rou
1b285 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f tine in order to
1b286 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 72 .** close the pr
1b287 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 69 6e ogram with a fin
1b288 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 74 al OP_Halt and t
1b289 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 61 6c o set up the cal
1b28a 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 68 lbacks.** and th
1b28b 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
1b28c 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 pointer..**.** W
1b28d 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f 72 henever a row or
1b28e 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 73 20 result data is
1b28f 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 available, this
1b290 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 74 routine will eit
1b291 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 her.** invoke th
1b292 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 63 e result callbac
1b293 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f k (if there is o
1b294 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77 69 ne) or return wi
1b295 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 th.** SQLITE_ROW
1b296 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 74 ..**.** If an at
1b297 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f tempt is made to
1b298 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 64 open a locked d
1b299 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 atabase, then th
1b29a 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 is routine.** wi
1b29b 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b 65 ll either invoke
1b29c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 the busy callba
1b29d 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 ck (if there is
1b29e 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c 0a one) or it will.
1b29f 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ** return SQLITE
1b2a0 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 _BUSY..**.** If
1b2a1 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
1b2a2 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1b2a3 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 e is written to
1b2a4 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a memory obtained.
1b2a5 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ** from sqlite3_
1b2a6 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d 3e malloc() and p->
1b2a7 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65 20 zErrMsg is made
1b2a8 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 to point to that
1b2a9 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 memory..** The
1b2aa 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 74 error code is st
1b2ab 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 6e ored in p->rc an
1b2ac 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 d this routine r
1b2ad 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 52 eturns SQLITE_ER
1b2ae 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ROR..**.** If th
1b2af 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 20 e callback ever
1b2b0 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f returns non-zero
1b2b1 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 , then the progr
1b2b2 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d 65 am exits.** imme
1b2b3 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65 20 diately. There
1b2b4 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f 72 will be no error
1b2b5 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 68 65 message but the
1b2b6 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 0a p->rc field is.
1b2b7 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 ** set to SQLITE
1b2b8 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73 20 _ABORT and this
1b2b9 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 routine will ret
1b2ba 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
1b2bb 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 ..**.** A memory
1b2bc 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
1b2bd 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20 74 r causes p->rc t
1b2be 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 o be set to SQLI
1b2bf 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 69 TE_NOMEM and thi
1b2c0 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 s.** routine to
1b2c1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1b2c2 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 ROR..**.** Other
1b2c3 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72 65 fatal errors re
1b2c4 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1b2c5 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 R..**.** After t
1b2c6 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 his routine has
1b2c7 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 65 finished, sqlite
1b2c8 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 20 3VdbeFinalize()
1b2c9 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 65 should be.** use
1b2ca 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68 d to clean up th
1b2cb 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73 20 e mess that was
1b2cc 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f 0a left behind..*/.
1b2cd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1b2ce 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 nt sqlite3VdbeEx
1b2cf 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 ec(. Vdbe *p
1b2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2d1 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a /* The VDBE */.
1b2d2 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 ){. int pc;
1b2d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2d4 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 /* The program c
1b2d5 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a ounter */. Op *
1b2d6 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 pOp;
1b2d7 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
1b2d8 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 t operation */.
1b2d9 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1b2da 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 _OK; /* V
1b2db 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a alue to return *
1b2dc 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 /. sqlite3 *db
1b2dd 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f = p->db; /
1b2de 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2a * The database *
1b2df 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 /. u8 encoding
1b2e0 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f = ENC(db); /
1b2e1 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 65 * The database e
1b2e2 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 6d ncoding */. Mem
1b2e3 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20 20 20 *pIn1 = 0;
1b2e4 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 69 /* 1st i
1b2e5 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a nput operand */.
1b2e6 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30 3b Mem *pIn2 = 0;
1b2e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b2e8 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 2nd input operan
1b2e9 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 33 d */. Mem *pIn3
1b2ea 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1b2eb 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 74 20 6f /* 3rd input o
1b2ec 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 perand */. Mem
1b2ed 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 *pOut = 0;
1b2ee 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 /* Output
1b2ef 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75 38 operand */. u8
1b2f0 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a 20 20 69 opProperty;. i
1b2f1 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 3b nt iCompare = 0;
1b2f2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
1b2f3 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f 43 ult of last OP_C
1b2f4 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f 6e ompare operation
1b2f5 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d */. int *aPerm
1b2f6 75 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ute = 0;
1b2f7 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e 20 /* Permutation
1b2f8 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 4f of columns for O
1b2f9 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 66 P_Compare */.#if
1b2fa 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 def VDBE_PROFILE
1b2fb 0a 20 20 75 36 34 20 73 74 61 72 74 3b 20 20 20 . u64 start;
1b2fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1b2fd 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e 74 CPU clock count
1b2fe 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 at start of opc
1b2ff 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 ode */. int ori
1b300 67 50 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 gPc;
1b301 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 /* Program c
1b302 6f 75 6e 74 65 72 20 61 74 20 73 74 61 72 74 20 ounter at start
1b303 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 6e of opcode */.#en
1b304 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
1b305 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 TE_OMIT_PROGRESS
1b306 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 _CALLBACK. int
1b307 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 nProgressOps = 0
1b308 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 ; /* Opcode
1b309 73 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 s executed since
1b30a 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 progress callba
1b30b 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 ck. */.#endif..
1b30c 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 70 /* Temporary sp
1b30d 61 63 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 ace into which t
1b30e 6f 20 75 6e 70 61 63 6b 20 61 20 72 65 63 6f 72 o unpack a recor
1b30f 64 2e 20 2a 2f 0a 20 20 63 68 61 72 20 61 54 65 d. */. char aTe
1b310 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 7a mpRec[ROUND8(siz
1b311 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f eof(UnpackedReco
1b312 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 rd)) + sizeof(Me
1b313 6d 29 2a 33 20 2b 20 37 5d 3b 0a 0a 20 20 61 73 m)*3 + 7];.. as
1b314 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
1b315 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 VDBE_MAGIC_RUN )
1b316 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 ; /* sqlite3_st
1b317 65 70 28 29 20 76 65 72 69 66 69 65 73 20 74 68 ep() verifies th
1b318 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 is */. assert(
1b319 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 db->magic==SQLIT
1b31a 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a E_MAGIC_BUSY );.
1b31b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 sqlite3VdbeMut
1b31c 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b exArrayEnter(p);
1b31d 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 . if( p->rc==SQ
1b31e 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 LITE_NOMEM ){.
1b31f 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
1b320 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 s if a malloc()
1b321 69 6e 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f inside a call to
1b322 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1b323 74 65 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a text() or. **
1b324 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1b325 74 65 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e text16() failed.
1b326 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f */. goto no
1b327 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 _mem;. }. asse
1b328 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 rt( p->rc==SQLIT
1b329 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 E_OK || p->rc==S
1b32a 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 QLITE_BUSY );.
1b32b 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f p->rc = SQLITE_O
1b32c 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e K;. assert( p->
1b32d 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 explain==0 );.
1b32e 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 p->pResultSet =
1b32f 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 0;. db->busyHan
1b330 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a dler.nBusy = 0;.
1b331 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 CHECK_FOR_INTE
1b332 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 RRUPT;. sqlite3
1b333 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 VdbeIOTraceSql(p
1b334 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
1b335 5f 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33 _DEBUG. sqlite3
1b336 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
1b337 63 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63 c();. if( p->pc
1b338 3d 3d 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e ==0 . && ((p->
1b339 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
1b33a 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20 TE_VdbeListing)
1b33b 7c 7c 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 || fileExists(db
1b33c 2c 20 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22 , "vdbe_explain"
1b33d 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 )). ){. int
1b33e 69 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56 i;. printf("V
1b33f 44 42 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74 DBE Program List
1b340 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71 ing:\n");. sq
1b341 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 lite3VdbePrintSq
1b342 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d l(p);. for(i=
1b343 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 0; i<p->nOp; i++
1b344 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b345 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f VdbePrintOp(stdo
1b346 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 ut, i, &p->aOp[i
1b347 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ]);. }. }.
1b348 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 if( fileExists(d
1b349 62 2c 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 b, "vdbe_trace")
1b34a 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 ){. p->trace
1b34b 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 = stdout;. }.
1b34c 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
1b34d 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 nMalloc();.#endi
1b34e 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 f. for(pc=p->pc
1b34f 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b ; rc==SQLITE_OK;
1b350 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 pc++){. asse
1b351 72 74 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c rt( pc>=0 && pc<
1b352 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 p->nOp );. if
1b353 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1b354 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d ed ) goto no_mem
1b355 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 ;.#ifdef VDBE_PR
1b356 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 OFILE. origPc
1b357 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 = pc;. start
1b358 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 = sqlite3Hwtime
1b359 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 ();.#endif. p
1b35a 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d Op = &p->aOp[pc]
1b35b 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 ;.. /* Only a
1b35c 6c 6c 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 llow tracing if
1b35d 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 SQLITE_DEBUG is
1b35e 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a defined.. */.
1b35f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1b360 42 55 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 BUG. if( p->t
1b361 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 race ){. if
1b362 28 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ( pc==0 ){.
1b363 20 20 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 printf("VDBE
1b364 45 78 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a Execution Trace:
1b365 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 \n");. sq
1b366 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 lite3VdbePrintSq
1b367 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 l(p);. }.
1b368 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 sqlite3VdbeP
1b369 72 69 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c rintOp(p->trace,
1b36a 20 70 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d pc, pOp);. }
1b36b 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 . if( p->trac
1b36c 65 3d 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b e==0 && pc==0 ){
1b36d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 . sqlite3Be
1b36e 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
1b36f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c );. if( fil
1b370 65 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 eExists(db, "vdb
1b371 65 5f 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a e_sqltrace") ){.
1b372 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1b373 64 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a dbePrintSql(p);.
1b374 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
1b375 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
1b376 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 lloc();. }.#e
1b377 6e 64 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 ndif. ..
1b378 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 /* Check to see
1b379 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 if we need to s
1b37a 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 imulate an inter
1b37b 72 75 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 rupt. This only
1b37c 20 68 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 happens. **
1b37d 69 66 20 77 65 20 68 61 76 65 20 61 20 73 70 65 if we have a spe
1b37e 63 69 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e cial test build.
1b37f 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 . */.#ifdef S
1b380 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 QLITE_TEST. i
1b381 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 f( sqlite3_inter
1b382 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a rupt_count>0 ){.
1b383 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e sqlite3_in
1b384 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b terrupt_count--;
1b385 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1b386 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 e3_interrupt_cou
1b387 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 nt==0 ){.
1b388 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1b389 70 74 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a pt(db);. }.
1b38a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 }.#endif..#i
1b38b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1b38c 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 T_PROGRESS_CALLB
1b38d 41 43 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 ACK. /* Call
1b38e 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
1b38f 6c 62 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 lback if it is c
1b390 6f 6e 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 onfigured and th
1b391 65 20 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 e required numbe
1b392 72 0a 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 r. ** of VDBE
1b393 20 6f 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 ops have been e
1b394 78 65 63 75 74 65 64 20 28 65 69 74 68 65 72 20 xecuted (either
1b395 73 69 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 since this invoc
1b396 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 ation of. **
1b397 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
1b398 29 20 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 ) or since last
1b399 74 69 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 time the progres
1b39a 73 20 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 s callback was c
1b39b 61 6c 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 alled).. ** I
1b39c 66 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 f the progress c
1b39d 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
1b39e 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 non-zero, exit t
1b39f 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
1b3a0 6e 65 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 ne with. ** a
1b3a1 20 72 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c return code SQL
1b3a2 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a ITE_ABORT.. *
1b3a3 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 /. if( db->xP
1b3a4 72 6f 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 rogress ){.
1b3a5 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 if( db->nProgre
1b3a6 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 ssOps==nProgress
1b3a7 4f 70 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 Ops ){. i
1b3a8 6e 74 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 nt prc;.
1b3a9 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1b3aa 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 yOff(db) ) goto
1b3ab 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1b3ac 75 73 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 use;. prc
1b3ad 20 3d 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 =db->xProgress(
1b3ae 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 db->pProgressArg
1b3af 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 );. if( s
1b3b0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1b3b1 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1b3b2 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1b3b3 20 20 20 20 20 20 20 69 66 28 20 70 72 63 21 3d if( prc!=
1b3b4 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 0 ){. r
1b3b5 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 c = SQLITE_INTER
1b3b6 52 55 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 RUPT;.
1b3b7 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f goto vdbe_error_
1b3b8 68 61 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a halt;. }.
1b3b9 20 20 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 nProgres
1b3ba 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 sOps = 0;.
1b3bb 7d 0a 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 }. nProgres
1b3bc 73 4f 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 sOps++;. }.#e
1b3bd 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 ndif.. /* Do
1b3be 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f common setup pro
1b3bf 63 65 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20 cessing for any
1b3c0 6f 70 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d opcode that is m
1b3c1 61 72 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74 arked. ** wit
1b3c2 68 20 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72 h the "out2-prer
1b3c3 65 6c 65 61 73 65 22 20 74 61 67 2e 20 20 53 75 elease" tag. Su
1b3c4 63 68 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 ch opcodes have
1b3c5 61 20 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 a single. **
1b3c6 6f 75 74 70 75 74 20 77 68 69 63 68 20 69 73 20 output which is
1b3c7 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
1b3c8 20 50 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20 P2 parameter.
1b3c9 54 68 65 20 50 32 20 72 65 67 69 73 74 65 72 0a The P2 register.
1b3ca 20 20 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 ** is initia
1b3cb 6c 69 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e lized to a NULL.
1b3cc 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72 . */. opPr
1b3cd 6f 70 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50 operty = opcodeP
1b3ce 72 6f 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 roperty[pOp->opc
1b3cf 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f ode];. if( (o
1b3d0 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
1b3d1 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 G_OUT2_PRERELEAS
1b3d2 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 E)!=0 ){. a
1b3d3 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
1b3d4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1b3d5 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d ( pOp->p2<=p->nM
1b3d6 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 em );. pOut
1b3d7 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1b3d8 3e 70 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 >p2];. sqli
1b3d9 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1b3da 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b eExternal(pOut);
1b3db 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 . pOut->fla
1b3dc 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1b3dd 20 20 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20 2f }else. . /
1b3de 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 * Do common setu
1b3df 70 20 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d 61 p for opcodes ma
1b3e0 72 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f 66 rked with one of
1b3e1 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
1b3e2 20 20 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 6f ** combinatio
1b3e3 6e 73 20 6f 66 20 70 72 6f 70 65 72 74 69 65 73 ns of properties
1b3e4 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1b3e5 20 20 20 20 20 20 20 20 20 20 69 6e 31 0a 20 20 in1.
1b3e6 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 ** i
1b3e7 6e 31 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20 20 n1 in2. **
1b3e8 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 20 in1 in2
1b3e9 6f 75 74 33 0a 20 20 20 20 2a 2a 20 20 20 20 20 out3. **
1b3ea 20 20 20 20 20 20 69 6e 31 20 69 6e 33 0a 20 20 in1 in3.
1b3eb 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72 69 **. ** Vari
1b3ec 61 62 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e 32 ables pIn1, pIn2
1b3ed 2c 20 61 6e 64 20 70 49 6e 33 20 61 72 65 20 6d , and pIn3 are m
1b3ee 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ade to point to
1b3ef 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20 appropriate.
1b3f0 2a 2a 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 ** registers for
1b3f1 20 69 6e 70 75 74 73 2e 20 20 56 61 72 69 61 62 inputs. Variab
1b3f2 6c 65 20 70 4f 75 74 20 70 6f 69 6e 74 73 20 74 le pOut points t
1b3f3 6f 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 o the output reg
1b3f4 69 73 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 ister.. */.
1b3f5 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 if( (opPropert
1b3f6 79 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 3d y & OPFLG_IN1)!=
1b3f7 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 0 ){. asser
1b3f8 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a t( pOp->p1>0 );.
1b3f9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1b3fa 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p1<=p->nMem )
1b3fb 3b 0a 20 20 20 20 20 20 70 49 6e 31 20 3d 20 26 ;. pIn1 = &
1b3fc 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d p->aMem[pOp->p1]
1b3fd 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52 ;. REGISTER
1b3fe 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 _TRACE(pOp->p1,
1b3ff 70 49 6e 31 29 3b 0a 20 20 20 20 20 20 69 66 28 pIn1);. if(
1b400 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f (opProperty & O
1b401 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a PFLG_IN2)!=0 ){.
1b402 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1b403 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 pOp->p2>0 );.
1b404 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1b405 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p2<=p->nMem );
1b406 0a 20 20 20 20 20 20 20 20 70 49 6e 32 20 3d 20 . pIn2 =
1b407 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1b408 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 ];. REGIS
1b409 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1b40a 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 20 20 2, pIn2);.
1b40b 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 if( (opPropert
1b40c 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 y & OPFLG_OUT3)!
1b40d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1b40e 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1b40f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 0 );. a
1b410 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d ssert( pOp->p3<=
1b411 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1b412 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e pOut = &p->
1b413 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1b414 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1b415 65 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 else if( (opProp
1b416 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 erty & OPFLG_IN3
1b417 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
1b418 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1b419 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 0 );. ass
1b41a 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d ert( pOp->p3<=p-
1b41b 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 >nMem );.
1b41c 20 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn3 = &p->aMem
1b41d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 [pOp->p3];.
1b41e 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 REGISTER_TRAC
1b41f 45 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 E(pOp->p3, pIn3)
1b420 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
1b421 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 lse if( (opPrope
1b422 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 rty & OPFLG_IN2)
1b423 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 !=0 ){. ass
1b424 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 ert( pOp->p2>0 )
1b425 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1b426 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p2<=p->nMem
1b427 20 29 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d );. pIn2 =
1b428 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1b429 32 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 2];. REGIST
1b42a 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 ER_TRACE(pOp->p2
1b42b 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c , pIn2);. }el
1b42c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 se if( (opProper
1b42d 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 ty & OPFLG_IN3)!
1b42e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1b42f 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b rt( pOp->p3>0 );
1b430 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1b431 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p3<=p->nMem
1b432 29 3b 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 );. pIn3 =
1b433 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1b434 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 ];. REGISTE
1b435 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1b436 20 70 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20 pIn3);. }..
1b437 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e switch( pOp->
1b438 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a opcode ){../****
1b439 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b43a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b43b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b43c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b43d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 *********.** Wha
1b43e 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d t follows is a m
1b43f 61 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74 assive switch st
1b440 61 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 atement where ea
1b441 63 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e ch case implemen
1b442 74 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 ts a.** separate
1b443 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 instruction in
1b444 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
1b445 69 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c ine. If we foll
1b446 6f 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 ow the usual.**
1b447 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 indentation conv
1b448 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 entions, each ca
1b449 73 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 se should be ind
1b44a 65 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65 ented by 6 space
1b44b 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 s. But.** that
1b44c 69 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 is a lot of wast
1b44d 65 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 ed space on the
1b44e 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f left margin. So
1b44f 20 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e the code within
1b450 0a 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 .** the switch s
1b451 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 tatement will br
1b452 65 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 eak with convent
1b453 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 ion and be flush
1b454 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a -left. Another.*
1b455 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 * big comment (s
1b456 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f imilar to this o
1b457 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 ne) will mark th
1b458 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 e point in the c
1b459 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 ode where.** we
1b45a 74 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 transition back
1b45b 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 to normal indent
1b45c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ation..**.** The
1b45d 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 formatting of e
1b45e 61 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f ach case is impo
1b45f 72 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 rtant. The make
1b460 66 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a file for SQLite.
1b461 2a 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f ** generates two
1b462 20 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 C files "opcode
1b463 73 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 s.h" and "opcode
1b464 73 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 s.c" by scanning
1b465 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f this.** file lo
1b466 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 oking for lines
1b467 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 that begin with
1b468 22 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 "case OP_". The
1b469 20 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 opcodes.h files
1b46a 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c .** will be fill
1b46b 65 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 ed with #defines
1b46c 20 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 that give uniqu
1b46d 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 e integer values
1b46e 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f to each.** opco
1b46f 64 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 de and the opcod
1b470 65 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c es.c file is fil
1b471 6c 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 led with an arra
1b472 79 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 y of strings whe
1b473 72 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e re.** each strin
1b474 67 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 g is the symboli
1b475 63 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 c name for the c
1b476 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 orresponding opc
1b477 6f 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 ode. If the.**
1b478 63 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 case statement i
1b479 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 s followed by a
1b47a 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 comment of the f
1b47b 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 orm "/# same as
1b47c 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 ... #/".** that
1b47d 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 comment is used
1b47e 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
1b47f 20 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 particular valu
1b480 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e e of the opcode.
1b481 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 .**.** Other key
1b482 77 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d words in the com
1b483 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 ment that follow
1b484 73 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20 s each case are
1b485 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 used to.** const
1b486 72 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 ruct the OPFLG_I
1b487 4e 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 NITIALIZER value
1b488 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 that initialize
1b489 73 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 s opcodeProperty
1b48a 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 []..** Keywords
1b48b 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e include: in1, in
1b48c 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 2, in3, out2_pre
1b48d 72 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f release, out2, o
1b48e 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 ut3. See.** the
1b48f 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 mkopcodeh.awk s
1b490 63 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 cript for additi
1b491 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1b492 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 ..**.** Document
1b493 61 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 ation about VDBE
1b494 20 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 opcodes is gene
1b495 72 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e rated by scannin
1b496 67 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 g this file.** f
1b497 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 or lines of that
1b498 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 contain "Opcode
1b499 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 :". That line a
1b49a 6e 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e nd all subsequen
1b49b 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e t.** comment lin
1b49c 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 es are used in t
1b49d 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 he generation of
1b49e 20 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c the opcode.html
1b49f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a documentation.*
1b4a0 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 * file..**.** SU
1b4a1 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 MMARY:.**.**
1b4a2 20 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 Formatting is i
1b4a3 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 mportant to scri
1b4a4 70 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 pts that scan th
1b4a5 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 is file..**
1b4a6 44 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 Do not deviate f
1b4a7 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 rom the formatti
1b4a8 6e 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 ng style current
1b4a9 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a ly in use..**.**
1b4aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
1b4af 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a Opcode: Goto *
1b4b0 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1b4b1 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c An unconditional
1b4b2 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 jump to address
1b4b3 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 P2..** The next
1b4b4 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 instruction exe
1b4b5 63 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a cuted will be .*
1b4b6 2a 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 * the one at ind
1b4b7 65 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 ex P2 from the b
1b4b8 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 eginning of.** t
1b4b9 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 he program..*/.c
1b4ba 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 ase OP_Goto: {
1b4bb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1b4bc 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f mp */. CHECK_FO
1b4bd 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 R_INTERRUPT;. p
1b4be 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1b4bf 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1b4c0 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 Opcode: Gosub P
1b4c1 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1b4c2 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65 Write the curre
1b4c3 6e 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 nt address onto
1b4c4 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 register P1.** a
1b4c5 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 nd then jump to
1b4c6 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 address P2..*/.c
1b4c7 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 ase OP_Gosub: {
1b4c8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1b4c9 6d 70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 mp */. assert(
1b4ca 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 pOp->p1>0 );. a
1b4cb 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d ssert( pOp->p1<=
1b4cc 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e p->nMem );. pIn
1b4cd 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 1 = &p->aMem[pOp
1b4ce 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1b4cf 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1b4d0 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 MEM_Dyn)==0 );.
1b4d1 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d pIn1->flags = M
1b4d2 45 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e EM_Int;. pIn1->
1b4d3 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 u.i = pc;. REGI
1b4d4 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1b4d5 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 p1, pIn1);. pc
1b4d6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1b4d7 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1b4d8 63 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 code: Return P1
1b4d9 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a * * * *.**.** J
1b4da 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ump to the next
1b4db 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 instruction afte
1b4dc 72 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e r the address in
1b4dd 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f register P1..*/
1b4de 0a 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a .case OP_Return:
1b4df 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1b4e0 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 in1 */. assert(
1b4e1 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1b4e2 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d EM_Int );. pc =
1b4e3 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b (int)pIn1->u.i;
1b4e4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1b4e5 4f 70 63 6f 64 65 3a 20 20 59 69 65 6c 64 20 50 Opcode: Yield P
1b4e6 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1b4e7 53 77 61 70 20 74 68 65 20 70 72 6f 67 72 61 6d Swap the program
1b4e8 20 63 6f 75 6e 74 65 72 20 77 69 74 68 20 74 68 counter with th
1b4e9 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1b4ea 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 ter P1..*/.case
1b4eb 4f 50 5f 59 69 65 6c 64 3a 20 7b 20 20 20 20 20 OP_Yield: {
1b4ec 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f /* in1 */
1b4ed 0a 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 20 . int pcDest;.
1b4ee 20 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e assert( (pIn1->
1b4ef 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 flags & MEM_Dyn)
1b4f0 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 ==0 );. pIn1->f
1b4f1 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1b4f2 20 20 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 pcDest = (int)
1b4f3 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e pIn1->u.i;. pIn
1b4f4 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 1->u.i = pc;. R
1b4f5 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1b4f6 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 p->p1, pIn1);.
1b4f7 70 63 20 3d 20 70 63 44 65 73 74 3b 0a 20 20 62 pc = pcDest;. b
1b4f8 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1b4f9 64 65 3a 20 20 48 61 6c 74 49 66 4e 75 6c 6c 20 de: HaltIfNull
1b4fa 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1b4fb 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76 *.** Check the v
1b4fc 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1b4fd 20 50 33 2e 20 20 49 66 20 69 73 20 69 73 20 4e P3. If is is N
1b4fe 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74 20 75 73 ULL then Halt us
1b4ff 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 ing.** parameter
1b500 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 34 20 P1, P2, and P4
1b501 61 73 20 69 66 20 74 68 69 73 20 77 65 72 65 20 as if this were
1b502 61 20 48 61 6c 74 20 69 6e 73 74 72 75 63 74 69 a Halt instructi
1b503 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 76 on. If the.** v
1b504 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1b505 20 50 33 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c P3 is not NULL,
1b506 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
1b507 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
1b508 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 49 66 /.case OP_HaltIf
1b509 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 2f 2a 20 Null: { /*
1b50a 69 6e 33 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 in3 */. if( (pI
1b50b 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n3->flags & MEM_
1b50c 4e 75 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b Null)==0 ) break
1b50d 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f ;. /* Fall thro
1b50e 75 67 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 ugh into OP_Halt
1b50f 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 */.}../* Opcode
1b510 3a 20 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20 : Halt P1 P2 *
1b511 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 P4 *.**.** Exit
1b512 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c immediately. Al
1b513 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 l open cursors,
1b514 65 74 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a etc are closed.*
1b515 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e * automatically.
1b516 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 .**.** P1 is the
1b517 20 72 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 result code ret
1b518 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
1b519 5f 65 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 _exec(), sqlite3
1b51a 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 _reset(),.** or
1b51b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
1b51c 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 (). For a norma
1b51d 6c 20 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f l halt, this sho
1b51e 75 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b uld be SQLITE_OK
1b51f 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 (0)..** For err
1b520 6f 72 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 ors, it can be s
1b521 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e ome other value.
1b522 20 20 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 If P1!=0 then
1b523 50 32 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e P2 will determin
1b524 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 e.** whether or
1b525 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 not to rollback
1b526 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e the current tran
1b527 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 saction. Do not
1b528 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 rollback.** if
1b529 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 P2==OE_Fail. Do
1b52a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 the rollback if
1b52b 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e P2==OE_Rollback.
1b52c 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 If P2==OE_Abor
1b52d 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 t,.** then back
1b52e 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 out all changes
1b52f 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 that have occurr
1b530 65 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 ed during this e
1b531 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a xecution of the.
1b532 2a 2a 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 ** VDBE, but do
1b533 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 not rollback the
1b534 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a transaction. .*
1b535 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f *.** If P4 is no
1b536 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 t null then it i
1b537 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 s an error messa
1b538 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a ge string..**.**
1b539 20 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 There is an imp
1b53a 6c 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 lied "Halt 0 0 0
1b53b 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e " instruction in
1b53c 73 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65 serted at the ve
1b53d 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 ry end of.** eve
1b53e 72 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 ry program. So
1b53f 61 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 a jump past the
1b540 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e last instruction
1b541 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a of the program.
1b542 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 ** is the same a
1b543 73 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 s executing Halt
1b544 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c ..*/.case OP_Hal
1b545 74 3a 20 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 70 t: {. p->rc = p
1b546 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 70 63 20 Op->p1;. p->pc
1b547 3d 20 70 63 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 = pc;. p->error
1b548 41 63 74 69 6f 6e 20 3d 20 70 4f 70 2d 3e 70 32 Action = pOp->p2
1b549 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e ;. if( pOp->p4.
1b54a 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 z ){. sqlite3
1b54b 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1b54c 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c rrMsg, db, "%s",
1b54d 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 7d pOp->p4.z);. }
1b54e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
1b54f 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 61 73 dbeHalt(p);. as
1b550 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1b551 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c _BUSY || rc==SQL
1b552 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 ITE_OK );. if(
1b553 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
1b554 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 ){. p->rc = r
1b555 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
1b556 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
1b557 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51 4c 49 54 = p->rc ? SQLIT
1b558 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c 49 54 45 E_ERROR : SQLITE
1b559 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 67 6f 74 _DONE;. }. got
1b55a 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d o vdbe_return;.}
1b55b 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 ../* Opcode: Int
1b55c 65 67 65 72 20 50 31 20 50 32 20 2a 20 2a 20 2a eger P1 P2 * * *
1b55d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 2d 62 69 .**.** The 32-bi
1b55e 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 t integer value
1b55f 50 31 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e P1 is written in
1b560 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1b561 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 */.case OP_Integ
1b562 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a er: { /*
1b563 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1b564 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 */. pOut->flag
1b565 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 s = MEM_Int;. p
1b566 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e Out->u.i = pOp->
1b567 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a p1;. break;.}..
1b568 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 /* Opcode: Int64
1b569 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a * P2 * P4 *.**.
1b56a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1b56b 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 69 er to a 64-bit i
1b56c 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a nteger value..**
1b56d 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 Write that valu
1b56e 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
1b56f 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 P2..*/.case OP_I
1b570 6e 74 36 34 3a 20 7b 20 20 20 20 20 20 20 20 20 nt64: {
1b571 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1b572 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 ease */. assert
1b573 28 20 70 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d ( pOp->p4.pI64!=
1b574 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 0 );. pOut->fla
1b575 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1b576 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a 70 4f 70 pOut->u.i = *pOp
1b577 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20 62 72 65 ->p4.pI64;. bre
1b578 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1b579 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a 20 50 34 : Real * P2 * P4
1b57a 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 *.**.** P4 is a
1b57b 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 pointer to a 64
1b57c 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f -bit floating po
1b57d 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 int value..** Wr
1b57e 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 ite that value i
1b57f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
1b580 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c .*/.case OP_Real
1b581 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1b582 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f * same as TK_FLO
1b583 41 54 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 AT, out2-prerele
1b584 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 ase */. pOut->f
1b585 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b lags = MEM_Real;
1b586 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 . assert( !sqli
1b587 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70 2d 3e 70 te3IsNaN(*pOp->p
1b588 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20 20 70 4f 4.pReal) );. pO
1b589 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d 3e 70 34 ut->r = *pOp->p4
1b58a 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65 61 6b 3b .pReal;. break;
1b58b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1b58c 74 72 69 6e 67 38 20 2a 20 50 32 20 2a 20 50 34 tring8 * P2 * P4
1b58d 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e *.**.** P4 poin
1b58e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d ts to a nul term
1b58f 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 inated UTF-8 str
1b590 69 6e 67 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 ing. This opcode
1b591 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 is transformed
1b592 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 53 .** into an OP_S
1b593 74 72 69 6e 67 20 62 65 66 6f 72 65 20 69 74 20 tring before it
1b594 69 73 20 65 78 65 63 75 74 65 64 20 66 6f 72 20 is executed for
1b595 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 2e 0a the first time..
1b596 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e */.case OP_Strin
1b597 67 38 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a g8: { /*
1b598 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 same as TK_STRI
1b599 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 NG, out2-prerele
1b59a 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ase */. assert(
1b59b 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b pOp->p4.z!=0 );
1b59c 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d . pOp->opcode =
1b59d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 20 70 4f OP_String;. pO
1b59e 70 2d 3e 70 31 20 3d 20 73 71 6c 69 74 65 33 53 p->p1 = sqlite3S
1b59f 74 72 6c 65 6e 33 30 28 70 4f 70 2d 3e 70 34 2e trlen30(pOp->p4.
1b5a0 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c z);..#ifndef SQL
1b5a1 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 ITE_OMIT_UTF16.
1b5a2 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d 53 if( encoding!=S
1b5a3 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 QLITE_UTF8 ){.
1b5a4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b5a5 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 SetStr(pOut, pOp
1b5a6 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 ->p4.z, -1, SQLI
1b5a7 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
1b5a8 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69 66 28 STATIC);. if(
1b5a9 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 SQLITE_OK!=sqli
1b5aa 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1b5ab 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 6f oding(pOut, enco
1b5ac 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f 5f ding) ) goto no_
1b5ad 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 53 51 4c mem;. if( SQL
1b5ae 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 ITE_OK!=sqlite3V
1b5af 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 dbeMemMakeWritea
1b5b0 62 6c 65 28 70 4f 75 74 29 20 29 20 67 6f 74 6f ble(pOut) ) goto
1b5b1 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 70 4f 75 no_mem;. pOu
1b5b2 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a t->zMalloc = 0;.
1b5b3 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 pOut->flags
1b5b4 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 |= MEM_Static;.
1b5b5 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 pOut->flags &
1b5b6 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 = ~MEM_Dyn;.
1b5b7 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d if( pOp->p4type=
1b5b8 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 =P4_DYNAMIC ){.
1b5b9 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1b5ba 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e 7a ee(db, pOp->p4.z
1b5bb 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 70 );. }. pOp
1b5bc 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 ->p4type = P4_DY
1b5bd 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d 3e NAMIC;. pOp->
1b5be 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b 0a p4.z = pOut->z;.
1b5bf 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4f pOp->p1 = pO
1b5c0 75 74 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 ut->n;. if( p
1b5c1 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 Op->p1>db->aLimi
1b5c2 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1b5c3 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 ENGTH] ){.
1b5c4 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 goto too_big;.
1b5c5 20 20 7d 0a 20 20 20 20 55 50 44 41 54 45 5f 4d }. UPDATE_M
1b5c6 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 AX_BLOBSIZE(pOut
1b5c7 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 );. break;.
1b5c8 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 }.#endif. if( p
1b5c9 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 Op->p1>db->aLimi
1b5ca 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1b5cb 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f ENGTH] ){. go
1b5cc 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1b5cd 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 /* Fall throug
1b5ce 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 h to the next ca
1b5cf 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a 2f se, OP_String */
1b5d0 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a .}. ./* Opcode:
1b5d1 20 53 74 72 69 6e 67 20 50 31 20 50 32 20 2a 20 String P1 P2 *
1b5d2 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 P4 *.**.** The s
1b5d3 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34 20 6f tring value P4 o
1b5d4 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62 79 74 f length P1 (byt
1b5d5 65 73 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e es) is stored in
1b5d6 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f register P2..*/
1b5d7 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 3a .case OP_String:
1b5d8 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f { /* o
1b5d9 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1b5da 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1b5db 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f >p4.z!=0 );. pO
1b5dc 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1b5dd 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d Str|MEM_Static|M
1b5de 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d EM_Term;. pOut-
1b5df 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a >z = pOp->p4.z;.
1b5e0 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d pOut->n = pOp-
1b5e1 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 >p1;. pOut->enc
1b5e2 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 = encoding;. U
1b5e3 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1b5e4 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 ZE(pOut);. brea
1b5e5 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1b5e6 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20 2a Null * P2 * * *
1b5e7 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 4e .**.** Write a N
1b5e8 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ULL into registe
1b5e9 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P2..*/.case OP
1b5ea 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 _Null: {
1b5eb 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1b5ec 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72 65 61 6b lease */. break
1b5ed 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a ;.}.../* Opcode:
1b5ee 20 42 6c 6f 62 20 50 31 20 50 32 20 2a 20 50 34 Blob P1 P2 * P4
1b5ef 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 .**.** P4 points
1b5f0 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 to a blob of da
1b5f1 74 61 20 50 31 20 62 79 74 65 73 20 6c 6f 6e 67 ta P1 bytes long
1b5f2 2e 20 20 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a . Store this.**
1b5f3 20 62 6c 6f 62 20 69 6e 20 72 65 67 69 73 74 65 blob in registe
1b5f4 72 20 50 32 2e 20 54 68 69 73 20 69 6e 73 74 72 r P2. This instr
1b5f5 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f uction is not co
1b5f6 64 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 ded directly.**
1b5f7 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e by the compiler.
1b5f8 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 63 6f Instead, the co
1b5f9 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 73 70 65 mpiler layer spe
1b5fa 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f cifies.** an OP_
1b5fb 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 HexBlob opcode,
1b5fc 77 69 74 68 20 74 68 65 20 68 65 78 20 73 74 72 with the hex str
1b5fd 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
1b5fe 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f on of.** the blo
1b5ff 62 20 61 73 20 50 34 2e 20 54 68 69 73 20 6f 70 b as P4. This op
1b600 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 code is transfor
1b601 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f med to an OP_Blo
1b602 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 b.** the first t
1b603 69 6d 65 20 69 74 20 69 73 20 65 78 65 63 75 74 ime it is execut
1b604 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 ed..*/.case OP_B
1b605 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 lob: {
1b606 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1b607 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 erelease */. as
1b608 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d sert( pOp->p1 <=
1b609 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 SQLITE_MAX_LENG
1b60a 54 48 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 TH );. sqlite3V
1b60b 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 dbeMemSetStr(pOu
1b60c 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f t, pOp->p4.z, pO
1b60d 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20 p->p1, 0, 0);.
1b60e 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f pOut->enc = enco
1b60f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d ding;. UPDATE_M
1b610 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 AX_BLOBSIZE(pOut
1b611 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1b612 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72 69 61 62 * Opcode: Variab
1b613 6c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a le P1 P2 P3 P4 *
1b614 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 .**.** Transfer
1b615 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 62 6f the values of bo
1b616 75 6e 64 20 70 61 72 61 6d 65 74 65 72 73 20 50 und parameters P
1b617 31 2e 2e 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20 1..P1+P3-1 into
1b618 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e registers.** P2.
1b619 2e 50 32 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20 .P2+P3-1..**.**
1b61a 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 If the parameter
1b61b 20 69 73 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20 is named, then
1b61c 69 74 73 20 6e 61 6d 65 20 61 70 70 65 61 72 73 its name appears
1b61d 20 69 6e 20 50 34 20 61 6e 64 20 50 33 3d 3d 31 in P4 and P3==1
1b61e 2e 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 ..** The P4 valu
1b61f 65 20 69 73 20 75 73 65 64 20 62 79 20 73 71 6c e is used by sql
1b620 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
1b621 74 65 72 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63 ter_name()..*/.c
1b622 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a ase OP_Variable:
1b623 20 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 70 4f 70 {. int j = pOp
1b624 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 69 6e 74 20 ->p1 - 1;. int
1b625 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 4d k = pOp->p2;. M
1b626 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e 74 20 em *pVar;. int
1b627 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 n = pOp->p3;. a
1b628 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a ssert( j>=0 && j
1b629 2b 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 +n<=p->nVar );.
1b62a 20 61 73 73 65 72 74 28 20 6b 3e 3d 31 20 26 26 assert( k>=1 &&
1b62b 20 6b 2b 6e 2d 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 k+n-1<=p->nMem
1b62c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1b62d 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20 70 4f 70 ->p4.z==0 || pOp
1b62e 2d 3e 70 33 3d 3d 31 20 29 3b 0a 0a 20 20 77 68 ->p3==1 );.. wh
1b62f 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a ile( n-- > 0 ){.
1b630 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 pVar = &p->a
1b631 56 61 72 5b 6a 2b 2b 5d 3b 0a 20 20 20 20 69 66 Var[j++];. if
1b632 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
1b633 54 6f 6f 42 69 67 28 70 56 61 72 29 20 29 7b 0a TooBig(pVar) ){.
1b634 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 goto too_b
1b635 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f ig;. }. pO
1b636 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 6b 2b ut = &p->aMem[k+
1b637 2b 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 +];. sqlite3V
1b638 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 dbeMemReleaseExt
1b639 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20 ernal(pOut);.
1b63a 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1b63b 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 73 71 6c EM_Null;. sql
1b63c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c ite3VdbeMemShall
1b63d 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70 56 61 owCopy(pOut, pVa
1b63e 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a r, MEM_Static);.
1b63f 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 UPDATE_MAX_B
1b640 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 LOBSIZE(pOut);.
1b641 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1b642 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50 * Opcode: Move P
1b643 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1b644 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 * Move the value
1b645 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 s in register P1
1b646 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69 ..P1+P3-1 over i
1b647 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 nto.** registers
1b648 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52 P2..P2+P3-1. R
1b649 65 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b egisters P1..P1+
1b64a 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74 P1-1 are.** left
1b64b 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e holding a NULL.
1b64c 20 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 It is an error
1b64d 20 66 6f 72 20 72 65 67 69 73 74 65 72 20 72 61 for register ra
1b64e 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50 nges.** P1..P1+P
1b64f 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50 3-1 and P2..P2+P
1b650 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a 3-1 to overlap..
1b651 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a */.case OP_Move:
1b652 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c {. char *zMall
1b653 6f 63 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f oc;. int n = pO
1b654 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 70 31 20 p->p3;. int p1
1b655 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 = pOp->p1;. int
1b656 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 p2 = pOp->p2;.
1b657 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 assert( n>0 &&
1b658 70 31 3e 30 20 26 26 20 70 32 3e 30 20 29 3b 0a p1>0 && p2>0 );.
1b659 20 20 61 73 73 65 72 74 28 20 70 31 2b 6e 3c 3d assert( p1+n<=
1b65a 70 32 20 7c 7c 20 70 32 2b 6e 3c 3d 70 31 20 29 p2 || p2+n<=p1 )
1b65b 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e ;.. pIn1 = &p->
1b65c 61 4d 65 6d 5b 70 31 5d 3b 0a 20 20 70 4f 75 74 aMem[p1];. pOut
1b65d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 5d 3b = &p->aMem[p2];
1b65e 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b . while( n-- ){
1b65f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 75 . assert( pOu
1b660 74 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e t<=&p->aMem[p->n
1b661 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 61 73 73 65 Mem] );. asse
1b662 72 74 28 20 70 49 6e 31 3c 3d 26 70 2d 3e 61 4d rt( pIn1<=&p->aM
1b663 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 em[p->nMem] );.
1b664 20 20 20 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 zMalloc = pOu
1b665 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 t->zMalloc;.
1b666 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 pOut->zMalloc =
1b667 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 0;. sqlite3Vd
1b668 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 beMemMove(pOut,
1b669 70 49 6e 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d pIn1);. pIn1-
1b66a 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4d 61 6c 6c >zMalloc = zMall
1b66b 6f 63 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 oc;. REGISTER
1b66c 5f 54 52 41 43 45 28 70 32 2b 2b 2c 20 70 4f 75 _TRACE(p2++, pOu
1b66d 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a t);. pIn1++;.
1b66e 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a pOut++;. }.
1b66f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1b670 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50 pcode: Copy P1 P
1b671 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 2 * * *.**.** Ma
1b672 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67 ke a copy of reg
1b673 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 ister P1 into re
1b674 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a gister P2..**.**
1b675 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1b676 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63 n makes a deep c
1b677 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 opy of the value
1b678 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a . A duplicate.*
1b679 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79 * is made of any
1b67a 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 string or blob
1b67b 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61 constant. See a
1b67c 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f lso OP_SCopy..*/
1b67d 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b .case OP_Copy: {
1b67e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b67f 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 in1 */. assert(
1b680 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 pOp->p2>0 );.
1b681 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c assert( pOp->p2<
1b682 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f =p->nMem );. pO
1b683 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1b684 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 p->p2];. assert
1b685 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a ( pOut!=pIn1 );.
1b686 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b687 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 ShallowCopy(pOut
1b688 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 , pIn1, MEM_Ephe
1b689 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65 72 61 m);. Deephemera
1b68a 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20 52 45 lize(pOut);. RE
1b68b 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1b68c 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 ->p2, pOut);. b
1b68d 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1b68e 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50 32 20 de: SCopy P1 P2
1b68f 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 * * *.**.** Make
1b690 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 a shallow copy
1b691 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 of register P1 i
1b692 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
1b693 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1b694 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20 ruction makes a
1b695 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 shallow copy of
1b696 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66 20 74 the value. If t
1b697 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 61 he value.** is a
1b698 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 2c string or blob,
1b699 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79 20 69 then the copy i
1b69a 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72 s only a pointer
1b69b 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 to the.** origi
1b69c 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20 69 66 nal and hence if
1b69d 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 68 the original ch
1b69e 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20 74 68 anges so will th
1b69f 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72 73 65 e copy..** Worse
1b6a0 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 , if the origina
1b6a1 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74 65 64 l is deallocated
1b6a2 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63 6f 6d , the copy becom
1b6a3 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 20 54 es invalid..** T
1b6a4 68 75 73 20 74 68 65 20 70 72 6f 67 72 61 6d 20 hus the program
1b6a5 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 must guarantee t
1b6a6 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c hat the original
1b6a7 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e 67 65 will not change
1b6a8 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 6c .** during the l
1b6a9 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 63 ifetime of the c
1b6aa 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43 6f 70 opy. Use OP_Cop
1b6ab 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 6d 70 y to make a comp
1b6ac 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a 2a 2f lete.** copy..*/
1b6ad 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79 3a 20 .case OP_SCopy:
1b6ae 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1b6af 69 6e 31 20 2a 2f 0a 20 20 52 45 47 49 53 54 45 in1 */. REGISTE
1b6b0 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1b6b1 20 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 pIn1);. assert
1b6b2 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 ( pOp->p2>0 );.
1b6b3 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1b6b4 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
1b6b5 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
1b6b6 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 Op->p2];. asser
1b6b7 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b t( pOut!=pIn1 );
1b6b8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b6b9 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 mShallowCopy(pOu
1b6ba 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 t, pIn1, MEM_Eph
1b6bb 65 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f em);. REGISTER_
1b6bc 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1b6bd 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1b6be 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 ../* Opcode: Res
1b6bf 75 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a ultRow P1 P2 * *
1b6c0 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 *.**.** The reg
1b6c1 69 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 isters P1 throug
1b6c2 68 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 h P1+P2-1 contai
1b6c3 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f n a single row o
1b6c4 66 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 f.** results. Th
1b6c5 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 is opcode causes
1b6c6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 the sqlite3_ste
1b6c7 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d p() call to term
1b6c8 69 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e inate.** with an
1b6c9 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 SQLITE_ROW retu
1b6ca 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 rn code and it s
1b6cb 65 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 ets up the sqlit
1b6cc 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 e3_stmt.** struc
1b6cd 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 ture to provide
1b6ce 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f access to the to
1b6cf 70 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 p P1 values as t
1b6d0 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 he result.** row
1b6d1 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 ..*/.case OP_Res
1b6d2 75 6c 74 52 6f 77 3a 20 7b 0a 20 20 4d 65 6d 20 ultRow: {. Mem
1b6d3 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a *pMem;. int i;.
1b6d4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
1b6d5 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 32 sColumn==pOp->p2
1b6d6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1b6d7 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 p->p1>0 );. ass
1b6d8 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 ert( pOp->p1+pOp
1b6d9 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 ->p2<=p->nMem+1
1b6da 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
1b6db 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 SQLITE_CountRows
1b6dc 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 flag is set in
1b6dd 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 6d 61 sqlite3.flags ma
1b6de 73 6b 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 44 sk, then . ** D
1b6df 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e ML statements in
1b6e0 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 voke this opcode
1b6e1 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 6e to return the n
1b6e2 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 0a 20 umber of rows .
1b6e3 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20 ** modified to
1b6e4 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 the user. This i
1b6e5 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 s the only way t
1b6e6 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20 hat a VM that.
1b6e7 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65 ** opens a state
1b6e8 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1b6e9 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73 may invoke this
1b6ea 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 opcode.. **.
1b6eb 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73 20 ** In case this
1b6ec 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65 6d is such a statem
1b6ed 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73 ent, close any s
1b6ee 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1b6ef 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 tion. ** opened
1b6f0 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f by this VM befo
1b6f1 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e re returning con
1b6f2 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 trol to the user
1b6f3 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a . This is to. *
1b6f4 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73 74 * ensure that st
1b6f5 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 atement-transact
1b6f6 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20 ions are always
1b6f7 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72 nested, not over
1b6f8 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66 lapping.. ** If
1b6f9 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d the open statem
1b6fa 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent-transaction
1b6fb 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65 is not closed he
1b6fc 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 re, then the use
1b6fd 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20 r. ** may step
1b6fe 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20 another VM that
1b6ff 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74 opens its own st
1b700 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1b701 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d ion. This. ** m
1b702 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c ay lead to overl
1b703 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 apping statement
1b704 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 transactions..
1b705 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
1b706 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 7c >iStatement==0 |
1b707 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 | db->flags&SQLI
1b708 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b 0a TE_CountRows );.
1b709 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
1b70a 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 =(rc = sqlite3Vd
1b70b 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 beCloseStatement
1b70c 28 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 (p, SAVEPOINT_RE
1b70d 4c 45 41 53 45 29 29 20 29 7b 0a 20 20 20 20 62 LEASE)) ){. b
1b70e 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 reak;. }.. /*
1b70f 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 65 Invalidate all e
1b710 70 68 65 6d 65 72 61 6c 20 63 75 72 73 6f 72 20 phemeral cursor
1b711 72 6f 77 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 row caches */.
1b712 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 28 70 p->cacheCtr = (p
1b713 2d 3e 63 61 63 68 65 43 74 72 20 2b 20 32 29 7c ->cacheCtr + 2)|
1b714 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 1;.. /* Make su
1b715 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f re the results o
1b716 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f f the current ro
1b717 77 20 61 72 65 20 5c 30 30 30 20 74 65 72 6d 69 w are \000 termi
1b718 6e 61 74 65 64 0a 20 20 2a 2a 20 61 6e 64 20 68 nated. ** and h
1b719 61 76 65 20 61 6e 20 61 73 73 69 67 6e 65 64 20 ave an assigned
1b71a 74 79 70 65 2e 20 20 54 68 65 20 72 65 73 75 6c type. The resul
1b71b 74 73 20 61 72 65 20 64 65 2d 65 70 68 65 6d 65 ts are de-epheme
1b71c 72 61 6c 69 7a 65 64 20 61 73 0a 20 20 2a 2a 20 ralized as. **
1b71d 61 73 20 73 69 64 65 20 65 66 66 65 63 74 2e 0a as side effect..
1b71e 20 20 2a 2f 0a 20 20 70 4d 65 6d 20 3d 20 70 2d */. pMem = p-
1b71f 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 >pResultSet = &p
1b720 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b ->aMem[pOp->p1];
1b721 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f . for(i=0; i<pO
1b722 70 2d 3e 70 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 p->p2; i++){.
1b723 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
1b724 75 6c 54 65 72 6d 69 6e 61 74 65 28 26 70 4d 65 ulTerminate(&pMe
1b725 6d 5b 69 5d 29 3b 0a 20 20 20 20 73 74 6f 72 65 m[i]);. store
1b726 54 79 70 65 49 6e 66 6f 28 26 70 4d 65 6d 5b 69 TypeInfo(&pMem[i
1b727 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 ], encoding);.
1b728 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1b729 28 70 4f 70 2d 3e 70 31 2b 69 2c 20 26 70 4d 65 (pOp->p1+i, &pMe
1b72a 6d 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 m[i]);. }. if(
1b72b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1b72c 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b d ) goto no_mem;
1b72d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 53 51 .. /* Return SQ
1b72e 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f 0a 20 20 LITE_ROW. */.
1b72f 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20 31 3b 0a p->pc = pc + 1;.
1b730 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f rc = SQLITE_RO
1b731 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 W;. goto vdbe_r
1b732 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 eturn;.}../* Opc
1b733 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 50 ode: Concat P1 P
1b734 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 2 P3 * *.**.** A
1b735 64 64 20 74 68 65 20 74 65 78 74 20 69 6e 20 72 dd the text in r
1b736 65 67 69 73 74 65 72 20 50 31 20 6f 6e 74 6f 20 egister P1 onto
1b737 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 the end of the t
1b738 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 ext in.** regist
1b739 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 er P2 and store
1b73a 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 the result in re
1b73b 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 gister P3..** If
1b73c 20 65 69 74 68 65 72 20 74 68 65 20 50 31 20 6f either the P1 o
1b73d 72 20 50 32 20 74 65 78 74 20 61 72 65 20 4e 55 r P2 text are NU
1b73e 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 4e 55 LL then store NU
1b73f 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 LL in P3..**.**
1b740 20 20 50 33 20 3d 20 50 32 20 7c 7c 20 50 31 0a P3 = P2 || P1.
1b741 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 **.** It is ille
1b742 67 61 6c 20 66 6f 72 20 50 31 20 61 6e 64 20 50 gal for P1 and P
1b743 33 20 74 6f 20 62 65 20 74 68 65 20 73 61 6d 65 3 to be the same
1b744 20 72 65 67 69 73 74 65 72 2e 20 53 6f 6d 65 74 register. Somet
1b745 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50 33 20 69 imes,.** if P3 i
1b746 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 s the same regis
1b747 74 65 72 20 61 73 20 50 32 2c 20 74 68 65 20 69 ter as P2, the i
1b748 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 mplementation is
1b749 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 able.** to avoi
1b74a 64 20 61 20 6d 65 6d 63 70 79 28 29 2e 0a 2a 2f d a memcpy()..*/
1b74b 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63 61 74 3a .case OP_Concat:
1b74c 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1b74d 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 same as TK_CONCA
1b74e 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 T, in1, in2, out
1b74f 33 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 3 */. i64 nByte
1b750 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e ;.. assert( pIn
1b751 31 21 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 1!=pOut );. if(
1b752 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 (pIn1->flags |
1b753 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d pIn2->flags) & M
1b754 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 EM_Null ){. s
1b755 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1b756 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 Null(pOut);.
1b757 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 45 78 70 break;. }. Exp
1b758 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 andBlob(pIn1);.
1b759 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 31 2c Stringify(pIn1,
1b75a 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 45 78 encoding);. Ex
1b75b 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a pandBlob(pIn2);.
1b75c 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 Stringify(pIn2
1b75d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 6e , encoding);. n
1b75e 42 79 74 65 20 3d 20 70 49 6e 31 2d 3e 6e 20 2b Byte = pIn1->n +
1b75f 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66 28 20 pIn2->n;. if(
1b760 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 nByte>db->aLimit
1b761 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
1b762 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 NGTH] ){. got
1b763 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 o too_big;. }.
1b764 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1b765 70 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b 0a pOut, MEM_Str);.
1b766 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1b767 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28 eMemGrow(pOut, (
1b768 69 6e 74 29 6e 42 79 74 65 2b 32 2c 20 70 4f 75 int)nByte+2, pOu
1b769 74 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 t==pIn2) ){.
1b76a 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
1b76b 0a 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e . if( pOut!=pIn
1b76c 32 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 2 ){. memcpy(
1b76d 70 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a pOut->z, pIn2->z
1b76e 2c 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a , pIn2->n);. }.
1b76f 20 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e memcpy(&pOut->
1b770 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 z[pIn2->n], pIn1
1b771 2d 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 ->z, pIn1->n);.
1b772 20 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 5d 20 pOut->z[nByte]
1b773 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e = 0;. pOut->z[n
1b774 42 79 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 Byte+1] = 0;. p
1b775 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Out->flags |= ME
1b776 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e M_Term;. pOut->
1b777 6e 20 3d 20 28 69 6e 74 29 6e 42 79 74 65 3b 0a n = (int)nByte;.
1b778 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e pOut->enc = en
1b779 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 coding;. UPDATE
1b77a 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f _MAX_BLOBSIZE(pO
1b77b 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
1b77c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 ./* Opcode: Add
1b77d 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1b77e 2a 2a 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 ** Add the value
1b77f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1b780 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 to the value in
1b781 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 register P2.** a
1b782 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1b783 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1b784 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1b785 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1b786 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1b787 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1b788 3a 20 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 : Multiply P1 P2
1b789 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a P3 * *.**.**.**
1b78a 20 4d 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61 Multiply the va
1b78b 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1b78c 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 P1 by the value
1b78d 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a in register P2.*
1b78e 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 * and store the
1b78f 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1b790 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1b791 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1b792 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1b793 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1b794 6f 64 65 3a 20 53 75 62 74 72 61 63 74 20 50 31 ode: Subtract P1
1b795 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1b796 20 53 75 62 74 72 61 63 74 20 74 68 65 20 76 61 Subtract the va
1b797 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1b798 50 31 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 P1 from the valu
1b799 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
1b79a 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 .** and store th
1b79b 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
1b79c 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
1b79d 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e ither input is N
1b79e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1b79f 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f is NULL..*/./* O
1b7a0 70 63 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31 pcode: Divide P1
1b7a1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1b7a2 20 44 69 76 69 64 65 20 74 68 65 20 76 61 6c 75 Divide the valu
1b7a3 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1b7a4 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e by the value in
1b7a5 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 register P2.**
1b7a6 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 and store the re
1b7a7 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 sult in register
1b7a8 20 50 33 2e 20 20 49 66 20 74 68 65 20 76 61 6c P3. If the val
1b7a9 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1b7aa 32 0a 2a 2a 20 69 73 20 7a 65 72 6f 2c 20 74 68 2.** is zero, th
1b7ab 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1b7ac 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 NULL..** If eit
1b7ad 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1b7ae 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1b7af 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1b7b0 6f 64 65 3a 20 52 65 6d 61 69 6e 64 65 72 20 50 ode: Remainder P
1b7b1 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1b7b2 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 72 65 * Compute the re
1b7b3 6d 61 69 6e 64 65 72 20 61 66 74 65 72 20 69 6e mainder after in
1b7b4 74 65 67 65 72 20 64 69 76 69 73 69 6f 6e 20 6f teger division o
1b7b5 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 0a 2a f the value in.*
1b7b6 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 * register P1 by
1b7b7 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1b7b8 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74 gister P2 and st
1b7b9 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1b7ba 6e 20 50 33 2e 20 0a 2a 2a 20 49 66 20 74 68 65 n P3. .** If the
1b7bb 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1b7bc 65 72 20 50 32 20 69 73 20 7a 65 72 6f 20 74 68 er P2 is zero th
1b7bd 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1b7be 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f ..** If either o
1b7bf 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 perand is NULL,
1b7c0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1b7c1 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 LL..*/.case OP_A
1b7c2 64 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 dd:
1b7c3 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1b7c4 20 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20 69 TK_PLUS, in1, i
1b7c5 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1b7c6 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 OP_Subtract:
1b7c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1b7c8 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 2c 20 me as TK_MINUS,
1b7c9 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1b7ca 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 /.case OP_Multip
1b7cb 6c 79 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 ly:
1b7cc 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 /* same as TK_S
1b7cd 54 41 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f TAR, in1, in2, o
1b7ce 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 ut3 */.case OP_D
1b7cf 69 76 69 64 65 3a 20 20 20 20 20 20 20 20 20 20 ivide:
1b7d0 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1b7d1 20 54 4b 5f 53 4c 41 53 48 2c 20 69 6e 31 2c 20 TK_SLASH, in1,
1b7d2 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 in2, out3 */.cas
1b7d3 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a 20 e OP_Remainder:
1b7d4 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 { /* s
1b7d5 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20 69 ame as TK_REM, i
1b7d6 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1b7d7 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 . int flags;.
1b7d8 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 applyNumericAffi
1b7d9 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 61 70 nity(pIn1);. ap
1b7da 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 plyNumericAffini
1b7db 74 79 28 70 49 6e 32 29 3b 0a 20 20 66 6c 61 67 ty(pIn2);. flag
1b7dc 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 s = pIn1->flags
1b7dd 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 | pIn2->flags;.
1b7de 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 4d 45 if( (flags & ME
1b7df 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74 M_Null)!=0 ) got
1b7e0 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 o arithmetic_res
1b7e1 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 ult_is_null;. i
1b7e2 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1b7e3 26 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 & pIn2->flags &
1b7e4 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e MEM_Int)==MEM_In
1b7e5 74 20 29 7b 0a 20 20 20 20 69 36 34 20 61 2c 20 t ){. i64 a,
1b7e6 62 3b 0a 20 20 20 20 61 20 3d 20 70 49 6e 31 2d b;. a = pIn1-
1b7e7 3e 75 2e 69 3b 0a 20 20 20 20 62 20 3d 20 70 49 >u.i;. b = pI
1b7e8 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 n2->u.i;. swi
1b7e9 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 tch( pOp->opcode
1b7ea 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ){. case O
1b7eb 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 62 P_Add: b
1b7ec 20 2b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 += a; bre
1b7ed 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1b7ee 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 62 P_Subtract: b
1b7ef 20 2d 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 -= a; bre
1b7f0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1b7f1 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 P_Multiply: b
1b7f2 20 2a 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 *= a; bre
1b7f3 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1b7f4 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 P_Divide: {.
1b7f5 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20 67 if( a==0 ) g
1b7f6 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 oto arithmetic_r
1b7f7 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 esult_is_null;.
1b7f8 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 69 /* Dividi
1b7f9 6e 67 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 ng the largest p
1b7fa 6f 73 73 69 62 6c 65 20 6e 65 67 61 74 69 76 65 ossible negative
1b7fb 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 64-bit integer
1b7fc 28 31 3c 3c 36 33 29 20 62 79 20 0a 20 20 20 20 (1<<63) by .
1b7fd 20 20 20 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e ** -1 return
1b7fe 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 6f s an integer too
1b7ff 20 6c 61 72 67 65 20 74 6f 20 73 74 6f 72 65 20 large to store
1b800 69 6e 20 61 20 36 34 2d 62 69 74 20 64 61 74 61 in a 64-bit data
1b801 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20 -type. On.
1b802 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 ** some archit
1b803 65 63 74 75 72 65 73 2c 20 74 68 65 20 76 61 6c ectures, the val
1b804 75 65 20 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20 ue overflows to
1b805 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65 (1<<63). On othe
1b806 72 73 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 rs,. ** a
1b807 20 53 49 47 46 50 45 20 69 73 20 69 73 73 75 65 SIGFPE is issue
1b808 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 d. The following
1b809 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61 statement norma
1b80a 6c 69 7a 65 73 20 74 68 69 73 0a 20 20 20 20 20 lizes this.
1b80b 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 20 73 ** behavior s
1b80c 6f 20 74 68 61 74 20 61 6c 6c 20 61 72 63 68 69 o that all archi
1b80d 74 65 63 74 75 72 65 73 20 62 65 68 61 76 65 20 tectures behave
1b80e 61 73 20 69 66 20 69 6e 74 65 67 65 72 20 0a 20 as if integer .
1b80f 20 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c ** overfl
1b810 6f 77 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 ow occurred..
1b811 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1b812 69 66 28 20 61 3d 3d 2d 31 20 26 26 20 62 3d 3d if( a==-1 && b==
1b813 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 SMALLEST_INT64 )
1b814 20 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 a = 1;.
1b815 62 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 b /= a;.
1b816 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1b817 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a default: {.
1b818 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d 30 if( a==0
1b819 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 ) goto arithmet
1b81a 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
1b81b 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 l;. if( a
1b81c 3d 3d 2d 31 20 29 20 61 20 3d 20 31 3b 0a 20 20 ==-1 ) a = 1;.
1b81d 20 20 20 20 20 20 62 20 25 3d 20 61 3b 0a 20 20 b %= a;.
1b81e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1b81f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 }. }. p
1b820 4f 75 74 2d 3e 75 2e 69 20 3d 20 62 3b 0a 20 20 Out->u.i = b;.
1b821 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1b822 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1b823 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f . }else{. do
1b824 75 62 6c 65 20 61 2c 20 62 3b 0a 20 20 20 20 61 uble a, b;. a
1b825 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1b826 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 alValue(pIn1);.
1b827 20 20 20 62 20 3d 20 73 71 6c 69 74 65 33 56 64 b = sqlite3Vd
1b828 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 beRealValue(pIn2
1b829 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 );. switch( p
1b82a 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 Op->opcode ){.
1b82b 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a case OP_Add:
1b82c 20 20 20 20 20 20 20 20 20 62 20 2b 3d 20 61 3b b += a;
1b82d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1b82e 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 case OP_Subt
1b82f 72 61 63 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b ract: b -= a;
1b830 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1b831 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 case OP_Mult
1b832 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b iply: b *= a;
1b833 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1b834 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69 case OP_Divi
1b835 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a de: {. /*
1b836 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 (double)0 In ca
1b837 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 se of SQLITE_OMI
1b838 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
1b839 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ... */. i
1b83a 66 28 20 61 3d 3d 28 64 6f 75 62 6c 65 29 30 20 f( a==(double)0
1b83b 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 ) goto arithmeti
1b83c 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c c_result_is_null
1b83d 3b 0a 20 20 20 20 20 20 20 20 62 20 2f 3d 20 61 ;. b /= a
1b83e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
1b83f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
1b840 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
1b841 20 20 69 36 34 20 69 61 20 3d 20 28 69 36 34 29 i64 ia = (i64)
1b842 61 3b 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 a;. i64 i
1b843 62 20 3d 20 28 69 36 34 29 62 3b 0a 20 20 20 20 b = (i64)b;.
1b844 20 20 20 20 69 66 28 20 69 61 3d 3d 30 20 29 20 if( ia==0 )
1b845 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
1b846 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
1b847 20 20 20 20 20 20 20 20 69 66 28 20 69 61 3d 3d if( ia==
1b848 2d 31 20 29 20 69 61 20 3d 20 31 3b 0a 20 20 20 -1 ) ia = 1;.
1b849 20 20 20 20 20 62 20 3d 20 28 64 6f 75 62 6c 65 b = (double
1b84a 29 28 69 62 20 25 20 69 61 29 3b 0a 20 20 20 20 )(ib % ia);.
1b84b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1b84c 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
1b84d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 62 29 sqlite3IsNaN(b)
1b84e 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 ){. goto a
1b84f 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 rithmetic_result
1b850 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a _is_null;. }.
1b851 20 20 20 20 70 4f 75 74 2d 3e 72 20 3d 20 62 3b pOut->r = b;
1b852 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
1b853 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 65 lag(pOut, MEM_Re
1b854 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 66 6c al);. if( (fl
1b855 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d ags & MEM_Real)=
1b856 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1b857 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 te3VdbeIntegerAf
1b858 66 69 6e 69 74 79 28 70 4f 75 74 29 3b 0a 20 20 finity(pOut);.
1b859 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b }. }. break;
1b85a 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 ..arithmetic_res
1b85b 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a 20 20 73 ult_is_null:. s
1b85c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1b85d 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 62 72 Null(pOut);. br
1b85e 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1b85f 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a 20 50 e: CollSeq * * P
1b860 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4.**.** P4 is a
1b861 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c pointer to a Col
1b862 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49 66 20 lSeq struct. If
1b863 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f the next call to
1b864 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e a user function
1b865 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61 74 65 .** or aggregate
1b866 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 47 65 calls sqlite3Ge
1b867 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 2c 20 tFuncCollSeq(),
1b868 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 this collation s
1b869 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a 2a 20 equence will.**
1b86a 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 be returned. Thi
1b86b 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 s is used by the
1b86c 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 29 2c built-in min(),
1b86d 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c 6c 69 max() and nulli
1b86e 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 f().** functions
1b86f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ..**.** The inte
1b870 72 66 61 63 65 20 75 73 65 64 20 62 79 20 74 68 rface used by th
1b871 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
1b872 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d 65 6e of the aforemen
1b873 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 tioned functions
1b874 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76 65 20 .** to retrieve
1b875 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
1b876 71 75 65 6e 63 65 20 73 65 74 20 62 79 20 74 68 quence set by th
1b877 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e 6f 74 is opcode is not
1b878 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 70 75 available.** pu
1b879 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74 6f 20 blicly, only to
1b87a 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 64 user functions d
1b87b 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63 2e 63 efined in func.c
1b87c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c ..*/.case OP_Col
1b87d 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 65 72 74 lSeq: {. assert
1b87e 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1b87f 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 62 4_COLLSEQ );. b
1b880 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1b881 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 50 31 20 de: Function P1
1b882 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1b883 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 65 72 20 * Invoke a user
1b884 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 69 73 20 function (P4 is
1b885 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 46 a pointer to a F
1b886 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 74 75 72 unction structur
1b887 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 69 6e 65 e that.** define
1b888 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 29 20 s the function)
1b889 77 69 74 68 20 50 35 20 61 72 67 75 6d 65 6e 74 with P5 argument
1b88a 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 s taken from reg
1b88b 69 73 74 65 72 20 50 32 20 61 6e 64 0a 2a 2a 20 ister P2 and.**
1b88c 73 75 63 63 65 73 73 6f 72 73 2e 20 20 54 68 65 successors. The
1b88d 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66 result of the f
1b88e 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 6f 72 65 unction is store
1b88f 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1b890 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 ..** Register P3
1b891 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 6f 6e 65 must not be one
1b892 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
1b893 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a 2a 20 50 inputs..**.** P
1b894 31 20 69 73 20 61 20 33 32 2d 62 69 74 20 62 69 1 is a 32-bit bi
1b895 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 tmask indicating
1b896 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1b897 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20 74 6f each argument to
1b898 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f the .** functio
1b899 6e 20 77 61 73 20 64 65 74 65 72 6d 69 6e 65 64 n was determined
1b89a 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 to be constant
1b89b 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e at compile time.
1b89c 20 49 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a If the first.**
1b89d 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 63 6f argument was co
1b89e 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 69 74 20 nstant then bit
1b89f 30 20 6f 66 20 50 31 20 69 73 20 73 65 74 2e 20 0 of P1 is set.
1b8a0 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
1b8a1 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 determine.** whe
1b8a2 74 68 65 72 20 6d 65 74 61 20 64 61 74 61 20 61 ther meta data a
1b8a3 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
1b8a4 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 61 user function a
1b8a5 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 20 74 68 rgument using th
1b8a6 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 65 74 e.** sqlite3_set
1b8a7 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 20 6d _auxdata() API m
1b8a8 61 79 20 62 65 20 73 61 66 65 6c 79 20 72 65 74 ay be safely ret
1b8a9 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 68 65 20 ained until the
1b8aa 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 next.** invocati
1b8ab 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 63 6f 64 on of this opcod
1b8ac 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 e..**.** See als
1b8ad 6f 3a 20 41 67 67 53 74 65 70 20 61 6e 64 20 41 o: AggStep and A
1b8ae 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 73 65 20 ggFinal.*/.case
1b8af 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 7b 0a 20 OP_Function: {.
1b8b0 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 int i;. Mem *p
1b8b1 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 Arg;. sqlite3_c
1b8b2 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 ontext ctx;. sq
1b8b3 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 lite3_value **ap
1b8b4 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 Val;. int n = p
1b8b5 4f 70 2d 3e 70 35 3b 0a 0a 20 20 61 70 56 61 6c Op->p5;.. apVal
1b8b6 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 = p->apArg;. a
1b8b7 73 73 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20 ssert( apVal ||
1b8b8 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65 72 n==0 );.. asser
1b8b9 74 28 20 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d t( n==0 || (pOp-
1b8ba 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
1b8bb 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29 +n<=p->nMem+1) )
1b8bc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1b8bd 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70 >p3<pOp->p2 || p
1b8be 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b Op->p3>=pOp->p2+
1b8bf 6e 20 29 3b 0a 20 20 70 41 72 67 20 3d 20 26 70 n );. pArg = &p
1b8c0 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b ->aMem[pOp->p2];
1b8c1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b . for(i=0; i<n;
1b8c2 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b 0a 20 i++, pArg++){.
1b8c3 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 41 apVal[i] = pA
1b8c4 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 rg;. storeTyp
1b8c5 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e 63 6f eInfo(pArg, enco
1b8c6 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47 49 53 ding);. REGIS
1b8c7 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1b8c8 32 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 2, pArg);. }..
1b8c9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1b8ca 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 type==P4_FUNCDEF
1b8cb 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d || pOp->p4type=
1b8cc 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b 0a =P4_VDBEFUNC );.
1b8cd 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 if( pOp->p4typ
1b8ce 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29 7b e==P4_FUNCDEF ){
1b8cf 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20 3d . ctx.pFunc =
1b8d0 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a pOp->p4.pFunc;.
1b8d1 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75 6e ctx.pVdbeFun
1b8d2 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a c = 0;. }else{.
1b8d3 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75 6e ctx.pVdbeFun
1b8d4 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a 29 70 c = (VdbeFunc*)p
1b8d5 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 Op->p4.pVdbeFunc
1b8d6 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20 ;. ctx.pFunc
1b8d7 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2d = ctx.pVdbeFunc-
1b8d8 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 61 >pFunc;. }.. a
1b8d9 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1b8da 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e && pOp->p3<=p->
1b8db 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d nMem );. pOut =
1b8dc 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1b8dd 33 5d 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61 67 3];. ctx.s.flag
1b8de 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1b8df 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 ctx.s.db = db;.
1b8e0 20 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b ctx.s.xDel = 0;
1b8e1 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 . ctx.s.zMalloc
1b8e2 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 = 0;.. /* The
1b8e3 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 output cell may
1b8e4 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62 already have a b
1b8e5 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e uffer allocated.
1b8e6 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 70 Move. ** the p
1b8e7 6f 69 6e 74 65 72 20 74 6f 20 63 74 78 2e 73 20 ointer to ctx.s
1b8e8 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20 75 so in case the u
1b8e9 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 63 61 6e ser-function can
1b8ea 20 75 73 65 0a 20 20 2a 2a 20 74 68 65 20 61 6c use. ** the al
1b8eb 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 ready allocated
1b8ec 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f buffer instead o
1b8ed 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e f allocating a n
1b8ee 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73 ew one.. */. s
1b8ef 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1b8f0 65 28 26 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b e(&ctx.s, pOut);
1b8f1 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1b8f2 67 28 26 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75 g(&ctx.s, MEM_Nu
1b8f3 6c 6c 29 3b 0a 0a 20 20 63 74 78 2e 69 73 45 72 ll);.. ctx.isEr
1b8f4 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 63 ror = 0;. if( c
1b8f5 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 tx.pFunc->flags
1b8f6 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 & SQLITE_FUNC_NE
1b8f7 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 EDCOLL ){. as
1b8f8 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 sert( pOp>p->aOp
1b8f9 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1b8fa 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d pOp[-1].p4type==
1b8fb 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 P4_COLLSEQ );.
1b8fc 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
1b8fd 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c ].opcode==OP_Col
1b8fe 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78 2e lSeq );. ctx.
1b8ff 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e pColl = pOp[-1].
1b900 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 p4.pColl;. }.
1b901 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1b902 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 yOff(db) ) goto
1b903 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1b904 75 73 65 3b 0a 20 20 28 2a 63 74 78 2e 70 46 75 use;. (*ctx.pFu
1b905 6e 63 2d 3e 78 46 75 6e 63 29 28 26 63 74 78 2c nc->xFunc)(&ctx,
1b906 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66 n, apVal);. if
1b907 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1b908 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 71 6c n(db) ){. sql
1b909 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1b90a 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20 20 se(&ctx.s);.
1b90b 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1b90c 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 o_misuse;. }.
1b90d 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1b90e 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 45 iled ){. /* E
1b90f 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d 61 6c ven though a mal
1b910 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 loc() has failed
1b911 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 , the implementa
1b912 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20 20 tion of the.
1b913 2a 2a 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e ** user function
1b914 20 6d 61 79 20 68 61 76 65 20 63 61 6c 6c 65 64 may have called
1b915 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 an sqlite3_resu
1b916 6c 74 5f 58 58 58 28 29 20 66 75 6e 63 74 69 6f lt_XXX() functio
1b917 6e 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 74 75 n. ** to retu
1b918 72 6e 20 61 20 76 61 6c 75 65 2e 20 54 68 65 20 rn a value. The
1b919 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 72 following call r
1b91a 65 6c 65 61 73 65 73 20 61 6e 79 20 72 65 73 6f eleases any reso
1b91b 75 72 63 65 73 0a 20 20 20 20 2a 2a 20 61 73 73 urces. ** ass
1b91c 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 75 63 ociated with suc
1b91d 68 20 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a h a value.. *
1b91e 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 4d *. ** Note: M
1b91f 61 79 62 65 20 4d 65 6d 52 65 6c 65 61 73 65 28 aybe MemRelease(
1b920 29 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c ) should be call
1b921 65 64 20 69 66 20 73 71 6c 69 74 65 33 53 61 66 ed if sqlite3Saf
1b922 65 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a 20 66 etyOn(). ** f
1b923 61 69 6c 73 20 61 6c 73 6f 20 28 74 68 65 20 69 ails also (the i
1b924 66 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65 6e 74 f(...) statement
1b925 20 61 62 6f 76 65 29 2e 20 42 75 74 20 69 66 20 above). But if
1b926 70 65 6f 70 6c 65 20 61 72 65 0a 20 20 20 20 2a people are. *
1b927 2a 20 6d 69 73 75 73 69 6e 67 20 73 71 6c 69 74 * misusing sqlit
1b928 65 2c 20 74 68 65 79 20 68 61 76 65 20 62 69 67 e, they have big
1b929 67 65 72 20 70 72 6f 62 6c 65 6d 73 20 74 68 61 ger problems tha
1b92a 6e 20 61 20 6c 65 61 6b 65 64 20 76 61 6c 75 65 n a leaked value
1b92b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c .. */. sql
1b92c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1b92d 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20 20 se(&ctx.s);.
1b92e 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
1b92f 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 75 .. /* If any au
1b930 78 69 6c 69 61 72 79 20 64 61 74 61 20 66 75 6e xiliary data fun
1b931 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e ctions have been
1b932 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 20 called by this
1b933 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 user function,.
1b934 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ** immediately
1b935 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 call the destruc
1b936 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d tor for any non-
1b937 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 static values..
1b938 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e 70 56 */. if( ctx.pV
1b939 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 73 dbeFunc ){. s
1b93a 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
1b93b 41 75 78 44 61 74 61 28 63 74 78 2e 70 56 64 62 AuxData(ctx.pVdb
1b93c 65 46 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 29 3b eFunc, pOp->p1);
1b93d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 56 64 . pOp->p4.pVd
1b93e 62 65 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64 beFunc = ctx.pVd
1b93f 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 2d beFunc;. pOp-
1b940 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 44 42 >p4type = P4_VDB
1b941 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a EFUNC;. }.. /*
1b942 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e If the function
1b943 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 returned an err
1b944 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 or, throw an exc
1b945 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 eption */. if(
1b946 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 ctx.isError ){.
1b947 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1b948 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1b949 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
1b94a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 63 e3_value_text(&c
1b94b 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d tx.s));. rc =
1b94c 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 ctx.isError;.
1b94d 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 }.. /* Copy the
1b94e 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66 result of the f
1b94f 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67 unction into reg
1b950 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 73 71 ister P3 */. sq
1b951 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
1b952 6e 63 6f 64 69 6e 67 28 26 63 74 78 2e 73 2c 20 ncoding(&ctx.s,
1b953 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c encoding);. sql
1b954 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1b955 70 4f 75 74 2c 20 26 63 74 78 2e 73 29 3b 0a 20 pOut, &ctx.s);.
1b956 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
1b957 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74 29 20 MemTooBig(pOut)
1b958 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f ){. goto too_
1b959 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47 49 53 big;. }. REGIS
1b95a 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1b95b 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41 3, pOut);. UPDA
1b95c 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1b95d 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a pOut);. break;.
1b95e 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 }../* Opcode: Bi
1b95f 74 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 tAnd P1 P2 P3 *
1b960 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 *.**.** Take the
1b961 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20 6f 66 bit-wise AND of
1b962 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 the values in r
1b963 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 egister P1 and P
1b964 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 2 and.** store t
1b965 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 he result in reg
1b966 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 ister P3..** If
1b967 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 either input is
1b968 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 NULL, the result
1b969 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 is NULL..*/./*
1b96a 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 50 31 Opcode: BitOr P1
1b96b 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1b96c 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77 69 Take the bit-wi
1b96d 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c se OR of the val
1b96e 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 ues in register
1b96f 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a P1 and P2 and.**
1b970 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1b971 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1b972 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1b973 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1b974 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1b975 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1b976 53 68 69 66 74 4c 65 66 74 20 50 31 20 50 32 20 ShiftLeft P1 P2
1b977 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 P3 * *.**.** Shi
1b978 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 ft the integer v
1b979 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1b97a 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66 74 20 P2 to the left
1b97b 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 by the.** number
1b97c 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 69 of bits specifi
1b97d 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 65 ed by the intege
1b97e 72 20 69 6e 20 72 65 67 69 73 65 72 20 50 31 2e r in regiser P1.
1b97f 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 .** Store the re
1b980 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 sult in register
1b981 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 P3..** If eithe
1b982 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c r input is NULL,
1b983 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
1b984 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ULL..*/./* Opcod
1b985 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 50 31 e: ShiftRight P1
1b986 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1b987 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 67 Shift the integ
1b988 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 er value in regi
1b989 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 72 ster P2 to the r
1b98a 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e ight by the.** n
1b98b 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70 umber of bits sp
1b98c 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69 ecified by the i
1b98d 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 74 nteger in regist
1b98e 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 er P1..** Store
1b98f 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 the result in re
1b990 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 gister P3..** If
1b991 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 either input is
1b992 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c NULL, the resul
1b993 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 t is NULL..*/.ca
1b994 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20 se OP_BitAnd:
1b995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1b996 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 same as TK_BITA
1b997 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 ND, in1, in2, ou
1b998 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 t3 */.case OP_Bi
1b999 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20 20 20 tOr:
1b99a 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1b99b 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31 2c 20 TK_BITOR, in1,
1b99c 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 in2, out3 */.cas
1b99d 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a 20 e OP_ShiftLeft:
1b99e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b99f 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 same as TK_LSHIF
1b9a0 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 T, in1, in2, out
1b9a1 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 3 */.case OP_Shi
1b9a2 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20 20 20 ftRight: {
1b9a3 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1b9a4 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31 2c 20 TK_RSHIFT, in1,
1b9a5 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 in2, out3 */. i
1b9a6 36 34 20 61 2c 20 62 3b 0a 0a 20 20 69 66 28 20 64 a, b;.. if(
1b9a7 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 (pIn1->flags | p
1b9a8 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 In2->flags) & ME
1b9a9 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 M_Null ){. sq
1b9aa 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1b9ab 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 ull(pOut);. b
1b9ac 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 20 3d 20 reak;. }. a =
1b9ad 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1b9ae 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 62 20 3d lue(pIn2);. b =
1b9af 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
1b9b0 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 73 77 alue(pIn1);. sw
1b9b1 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 itch( pOp->opcod
1b9b2 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 e ){. case OP
1b9b3 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20 61 20 _BitAnd: a
1b9b4 26 3d 20 62 3b 20 20 20 20 20 62 72 65 61 6b 3b &= b; break;
1b9b5 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 . case OP_Bit
1b9b6 4f 72 3a 20 20 20 20 20 20 20 61 20 7c 3d 20 62 Or: a |= b
1b9b7 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
1b9b8 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 case OP_ShiftLe
1b9b9 66 74 3a 20 20 20 61 20 3c 3c 3d 20 62 3b 20 20 ft: a <<= b;
1b9ba 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 break;. def
1b9bb 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28 20 70 ault: assert( p
1b9bc 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 Op->opcode==OP_S
1b9bd 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 hiftRight );.
1b9be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b9bf 20 20 20 20 20 20 61 20 3e 3e 3d 20 62 3b 20 20 a >>= b;
1b9c0 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 break;. }. p
1b9c1 4f 75 74 2d 3e 75 2e 69 20 3d 20 61 3b 0a 20 20 Out->u.i = a;.
1b9c2 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1b9c3 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Out, MEM_Int);.
1b9c4 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1b9c5 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 50 31 code: AddImm P1
1b9c6 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a P2 * * *.** .**
1b9c7 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61 6e Add the constan
1b9c8 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c 75 t P2 to the valu
1b9c9 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1b9ca 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 ..** The result
1b9cb 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e 74 is always an int
1b9cc 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 66 eger..**.** To f
1b9cd 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 74 65 orce any registe
1b9ce 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 r to be an integ
1b9cf 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 2e 0a er, just add 0..
1b9d0 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 49 6d */.case OP_AddIm
1b9d1 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 m: {
1b9d2 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71 6c 69 /* in1 */. sqli
1b9d3 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
1b9d4 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 49 rify(pIn1);. pI
1b9d5 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e n1->u.i += pOp->
1b9d6 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a p2;. break;.}..
1b9d7 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 74 42 /* Opcode: MustB
1b9d8 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a eInt P1 P2 * * *
1b9d9 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 74 68 .** .** Force th
1b9da 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1b9db 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 6e 20 ter P1 to be an
1b9dc 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74 68 65 integer. If the
1b9dd 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1b9de 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 is not an intege
1b9df 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 65 20 r and cannot be
1b9e0 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 converted into a
1b9e1 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 69 74 n integer.** wit
1b9e2 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 2c 20 hout data loss,
1b9e3 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 then jump immedi
1b9e4 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f 72 20 ately to P2, or
1b9e5 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 69 73 if P2==0.** rais
1b9e6 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 4d e an SQLITE_MISM
1b9e7 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e 2e 0a ATCH exception..
1b9e8 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 74 42 */.case OP_MustB
1b9e9 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 eInt: {
1b9ea 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 /* jump, in1
1b9eb 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 */. applyAffini
1b9ec 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f ty(pIn1, SQLITE_
1b9ed 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 AFF_NUMERIC, enc
1b9ee 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 70 oding);. if( (p
1b9ef 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1b9f0 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 _Int)==0 ){.
1b9f1 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 if( pOp->p2==0 )
1b9f2 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1b9f3 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a 20 20 ITE_MISMATCH;.
1b9f4 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1b9f5 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1b9f6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 }else{. pc
1b9f7 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1b9f8 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1b9f9 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1b9fa 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 g(pIn1, MEM_Int)
1b9fb 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1b9fc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 ../* Opcode: Rea
1b9fd 6c 41 66 66 69 6e 69 74 79 20 50 31 20 2a 20 2a lAffinity P1 * *
1b9fe 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 * *.**.** If re
1b9ff 67 69 73 74 65 72 20 50 31 20 68 6f 6c 64 73 20 gister P1 holds
1ba00 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 65 an integer conve
1ba01 72 74 20 69 74 20 74 6f 20 61 20 72 65 61 6c 20 rt it to a real
1ba02 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 value..**.** Thi
1ba03 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 s opcode is used
1ba04 20 77 68 65 6e 20 65 78 74 72 61 63 74 69 6e 67 when extracting
1ba05 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
1ba06 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a m a column that.
1ba07 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 66 66 69 ** has REAL affi
1ba08 6e 69 74 79 2e 20 20 53 75 63 68 20 63 6f 6c 75 nity. Such colu
1ba09 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 20 73 74 mn values may st
1ba0a 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 61 73 ill be stored as
1ba0b 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c 20 66 6f .** integers, fo
1ba0c 72 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e r space efficien
1ba0d 63 79 2c 20 62 75 74 20 61 66 74 65 72 20 65 78 cy, but after ex
1ba0e 74 72 61 63 74 69 6f 6e 20 77 65 20 77 61 6e 74 traction we want
1ba0f 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 61 76 65 them.** to have
1ba10 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 76 61 6c only a real val
1ba11 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 ue..*/.case OP_R
1ba12 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b 20 20 ealAffinity: {
1ba13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ba14 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 /* in1 */. if(
1ba15 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1ba16 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c M_Int ){. sql
1ba17 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 ite3VdbeMemReali
1ba18 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 fy(pIn1);. }.
1ba19 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 break;.}..#ifnde
1ba1a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 f SQLITE_OMIT_CA
1ba1b 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f ST./* Opcode: To
1ba1c 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a Text P1 * * * *.
1ba1d 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 **.** Force the
1ba1e 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1ba1f 72 20 50 31 20 74 6f 20 62 65 20 74 65 78 74 2e r P1 to be text.
1ba20 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1ba21 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e is numeric, con
1ba22 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 72 vert it to a str
1ba23 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a ing using the.**
1ba24 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 70 equivalent of p
1ba25 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62 20 76 rintf(). Blob v
1ba26 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68 61 6e alues are unchan
1ba27 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 ged and.** are a
1ba28 66 74 65 72 77 61 72 64 73 20 73 69 6d 70 6c 79 fterwards simply
1ba29 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
1ba2a 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 text..**.** A NU
1ba2b 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1ba2c 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1ba2d 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1ba2e 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1ba2f 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20 7b 20 se OP_ToText: {
1ba30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ba31 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
1ba32 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f 0a 20 O_TEXT, in1 */.
1ba33 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 if( pIn1->flags
1ba34 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 & MEM_Null ) br
1ba35 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 4d eak;. assert( M
1ba36 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f EM_Str==(MEM_Blo
1ba37 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 6e 31 2d b>>3) );. pIn1-
1ba38 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 6e 31 2d >flags |= (pIn1-
1ba39 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 >flags&MEM_Blob)
1ba3a 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 >>3;. applyAffi
1ba3b 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 nity(pIn1, SQLIT
1ba3c 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f E_AFF_TEXT, enco
1ba3d 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d 20 45 78 ding);. rc = Ex
1ba3e 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a pandBlob(pIn1);.
1ba3f 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e assert( pIn1->
1ba40 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 flags & MEM_Str
1ba41 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
1ba42 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 led );. pIn1->f
1ba43 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e lags &= ~(MEM_In
1ba44 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 t|MEM_Real|MEM_B
1ba45 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20 lob|MEM_Zero);.
1ba46 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1ba47 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 SIZE(pIn1);. br
1ba48 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ba49 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a 20 2a e: ToBlob P1 * *
1ba4a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 * *.**.** Force
1ba4b 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1ba4c 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 gister P1 to be
1ba4d 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 a BLOB..** If th
1ba4e 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 e value is numer
1ba4f 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 74 ic, convert it t
1ba50 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72 73 74 o a string first
1ba51 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61 72 65 ..** Strings are
1ba52 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65 72 70 simply reinterp
1ba53 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73 20 77 reted as blobs w
1ba54 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a ith no change.**
1ba55 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 69 to the underlyi
1ba56 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 ng data..**.** A
1ba57 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e NULL value is n
1ba58 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 ot changed by th
1ba59 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 is routine. It
1ba5a 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f remains NULL..*/
1ba5b 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a .case OP_ToBlob:
1ba5c 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1ba5d 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1ba5e 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a K_TO_BLOB, in1 *
1ba5f 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c /. if( pIn1->fl
1ba60 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
1ba61 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 28 70 break;. if( (p
1ba62 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1ba63 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20 20 _Blob)==0 ){.
1ba64 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 applyAffinity(p
1ba65 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f In1, SQLITE_AFF_
1ba66 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b TEXT, encoding);
1ba67 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e . assert( pIn
1ba68 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 1->flags & MEM_S
1ba69 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 tr || db->malloc
1ba6a 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 4d 65 Failed );. Me
1ba6b 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e mSetTypeFlag(pIn
1ba6c 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 1, MEM_Blob);.
1ba6d 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 31 2d }else{. pIn1-
1ba6e 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
1ba6f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f 42 6c TypeMask&~MEM_Bl
1ba70 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 ob);. }. UPDAT
1ba71 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1ba72 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d In1);. break;.}
1ba73 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 4e ../* Opcode: ToN
1ba74 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 2a 20 umeric P1 * * *
1ba75 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 *.**.** Force th
1ba76 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1ba77 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e 75 6d ter P1 to be num
1ba78 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e 0a eric (either an.
1ba79 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61 20 ** integer or a
1ba7a 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 6e floating-point n
1ba7b 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74 68 umber.).** If th
1ba7c 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 e value is text
1ba7d 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 or blob, try to
1ba7e 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e convert it to an
1ba7f 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 using the.** eq
1ba80 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 uivalent of atoi
1ba81 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e 64 () or atof() and
1ba82 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 store 0 if no s
1ba83 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 0a uch conversion .
1ba84 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a ** is possible..
1ba85 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c **.** A NULL val
1ba86 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 ue is not change
1ba87 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e d by this routin
1ba88 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e e. It remains N
1ba89 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
1ba8a 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20 20 ToNumeric: {
1ba8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ba8c 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 4e same as TK_TO_N
1ba8d 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0a 20 UMERIC, in1 */.
1ba8e 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1ba8f 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 s & (MEM_Null|ME
1ba90 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 M_Int|MEM_Real))
1ba91 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1ba92 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 e3VdbeMemNumerif
1ba93 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 y(pIn1);. }. b
1ba94 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1ba95 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 * SQLITE_OMIT_CA
1ba96 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ST */../* Opcode
1ba97 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 2a : ToInt P1 * * *
1ba98 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 *.**.** Force t
1ba99 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1ba9a 73 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 6e ster P1 be an in
1ba9b 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 68 teger. If.** Th
1ba9c 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 e value is curre
1ba9d 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d 62 ntly a real numb
1ba9e 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 61 er, drop its fra
1ba9f 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a ctional part..**
1baa0 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 If the value is
1baa1 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 text or blob, t
1baa2 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 ry to convert it
1baa3 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75 to an integer u
1baa4 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 sing the.** equi
1baa5 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 valent of atoi()
1baa6 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 and store 0 if
1baa7 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 no such conversi
1baa8 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a on is possible..
1baa9 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c **.** A NULL val
1baaa 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 ue is not change
1baab 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e d by this routin
1baac 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e e. It remains N
1baad 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
1baae 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 ToInt: {
1baaf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1bab0 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 e as TK_TO_INT,
1bab1 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 in1 */. if( (pI
1bab2 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1bab3 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Null)==0 ){.
1bab4 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
1bab5 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a tegerify(pIn1);.
1bab6 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1bab7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1bab8 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f MIT_CAST./* Opco
1bab9 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20 2a 20 de: ToReal P1 *
1baba 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 * * *.**.** Forc
1babb 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1babc 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 egister P1 to be
1babd 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e a floating poin
1babe 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 66 20 t number..** If
1babf 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 The value is cur
1bac0 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67 65 rently an intege
1bac1 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e 0a 2a r, convert it..*
1bac2 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 * If the value i
1bac3 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 s text or blob,
1bac4 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 try to convert i
1bac5 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 t to an integer
1bac6 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 using the.** equ
1bac7 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 ivalent of atoi(
1bac8 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e 30 20 ) and store 0.0
1bac9 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 if no such conve
1baca 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c rsion is possibl
1bacb 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 e..**.** A NULL
1bacc 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 value is not cha
1bacd 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 nged by this rou
1bace 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e tine. It remain
1bacf 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
1bad0 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20 20 20 OP_ToReal: {
1bad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bad2 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 same as TK_TO_R
1bad3 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 EAL, in1 */. if
1bad4 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1bad5 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b MEM_Null)==0 ){
1bad6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1bad7 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 29 MemRealify(pIn1)
1bad8 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1bad9 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1bada 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a E_OMIT_CAST */..
1badb 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 31 /* Opcode: Lt P1
1badc 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1badd 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 ** Compare the v
1bade 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
1badf 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49 66 r P1 and P3. If
1bae0 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31 29 reg(P3)<reg(P1)
1bae1 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f then.** jump to
1bae2 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a 2a address P2. .*
1bae3 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 *.** If the SQLI
1bae4 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 TE_JUMPIFNULL bi
1bae5 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 61 t of P5 is set a
1bae6 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50 31 nd either reg(P1
1bae7 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 20 ) or.** reg(P3)
1bae8 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1bae9 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 20 e the jump. If
1baea 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 the SQLITE_JUMPI
1baeb 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 73 FNULL .** bit is
1baec 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c 6c clear then fall
1baed 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72 20 thru if either
1baee 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e operand is NULL.
1baef 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 .**.** The SQLIT
1baf0 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 69 E_AFF_MASK porti
1baf1 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62 65 on of P5 must be
1baf2 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 an affinity cha
1baf3 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c 49 racter -.** SQLI
1baf4 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 4c TE_AFF_TEXT, SQL
1baf5 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 2c ITE_AFF_INTEGER,
1baf6 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 41 and so forth. A
1baf7 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 n attempt is mad
1baf8 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 20 e .** to coerce
1baf9 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63 6f both inputs acco
1bafa 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61 66 rding to this af
1bafb 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74 68 finity before th
1bafc 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 e.** comparison
1bafd 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65 20 is made. If the
1bafe 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 SQLITE_AFF_MASK
1baff 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e 75 is 0x00, then nu
1bb00 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 74 meric.** affinit
1bb01 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 20 y is used. Note
1bb02 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 74 that the affinit
1bb03 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72 y conversions ar
1bb04 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 6b e stored.** back
1bb05 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 20 into the input
1bb06 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 registers P1 and
1bb07 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f 70 P3. So this op
1bb08 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a 2a code can cause.*
1bb09 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68 61 * persistent cha
1bb0a 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65 72 nges to register
1bb0b 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a 0a s P1 and P3..**.
1bb0c 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e 76 ** Once any conv
1bb0d 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61 6b ersions have tak
1bb0e 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e 65 en place, and ne
1bb0f 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 4e ither value is N
1bb10 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c ULL, .** the val
1bb11 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65 64 ues are compared
1bb12 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 . If both values
1bb13 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e 20 are blobs then
1bb14 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 75 memcmp() is.** u
1bb15 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
1bb16 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 the results of
1bb17 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 the comparison.
1bb18 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a If both values.
1bb19 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68 65 ** are text, the
1bb1a 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 n the appropriat
1bb1b 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 e collating func
1bb1c 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69 tion specified i
1bb1d 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65 64 n.** P4 is used
1bb1e 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 to do the compa
1bb1f 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 73 rison. If P4 is
1bb20 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 74 not specified t
1bb21 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 20 hen.** memcmp()
1bb22 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 is used to compa
1bb23 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e 20 re text string.
1bb24 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 If both values
1bb25 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c 20 are.** numeric,
1bb26 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 63 then a numeric c
1bb27 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73 65 omparison is use
1bb28 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76 61 d. If the two va
1bb29 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 64 lues.** are of d
1bb2a 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c 20 ifferent types,
1bb2b 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 65 then numbers are
1bb2c 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73 considered less
1bb2d 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 73 than.** strings
1bb2e 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 and strings are
1bb2f 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73 considered less
1bb30 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a 0a than blobs..**.
1bb31 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 ** If the SQLITE
1bb32 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66 20 _STOREP2 bit of
1bb33 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 P5 is set, then
1bb34 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 6e do not jump. In
1bb35 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 20 stead,.** store
1bb36 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c 74 a boolean result
1bb37 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20 31 (either 0, or 1
1bb38 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 65 , or NULL) in re
1bb39 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f 2a gister P2..*/./*
1bb3a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 50 Opcode: Ne P1 P
1bb3b 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1bb3c 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 This works just
1bb3d 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 like the Lt opc
1bb3e 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 ode except that
1bb3f 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 the jump is take
1bb40 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 n if.** the oper
1bb41 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 ands in register
1bb42 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 s P1 and P3 are
1bb43 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65 20 not equal. See
1bb44 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
1bb45 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 r.** additional
1bb46 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
1bb47 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 /* Opcode: Eq P1
1bb48 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1bb49 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 ** This works ju
1bb4a 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f st like the Lt o
1bb4b 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 pcode except tha
1bb4c 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 t the jump is ta
1bb4d 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 ken if.** the op
1bb4e 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 erands in regist
1bb4f 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 ers P1 and P3 ar
1bb50 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 e equal..** See
1bb51 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
1bb52 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
1bb53 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 ormation..*/./*
1bb54 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 50 32 Opcode: Le P1 P2
1bb55 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1bb56 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 This works just
1bb57 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f like the Lt opco
1bb58 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 de except that t
1bb59 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1bb5a 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 if.** the conte
1bb5b 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 nt of register P
1bb5c 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 3 is less than o
1bb5d 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63 r equal to the c
1bb5e 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 ontent of.** reg
1bb5f 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 ister P1. See t
1bb60 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 he Lt opcode for
1bb61 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
1bb62 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f rmation..*/./* O
1bb63 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50 32 20 pcode: Gt P1 P2
1bb64 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 P3 P4 P5.**.** T
1bb65 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c his works just l
1bb66 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 ike the Lt opcod
1bb67 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 e except that th
1bb68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 e jump is taken
1bb69 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e if.** the conten
1bb6a 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 t of register P3
1bb6b 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
1bb6c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a the content of.
1bb6d 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 ** register P1.
1bb6e 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f See the Lt opco
1bb6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 de for additiona
1bb70 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
1bb71 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65 20 /./* Opcode: Ge
1bb72 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1bb73 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 *.** This works
1bb74 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 just like the Lt
1bb75 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 opcode except t
1bb76 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 hat the jump is
1bb77 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 taken if.** the
1bb78 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 content of regis
1bb79 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65 ter P3 is greate
1bb7a 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
1bb7b 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f to the content o
1bb7c 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 f.** register P1
1bb7d 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 . See the Lt op
1bb7e 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f code for additio
1bb7f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
1bb80 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 3a 20 .*/.case OP_Eq:
1bb81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bb82 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 2c 20 same as TK_EQ,
1bb83 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a jump, in1, in3 *
1bb84 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 /.case OP_Ne:
1bb85 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1bb86 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20 6a 75 ame as TK_NE, ju
1bb87 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
1bb88 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 20 case OP_Lt:
1bb89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1bb8a 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75 6d 70 e as TK_LT, jump
1bb8b 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 , in1, in3 */.ca
1bb8c 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20 20 20 se OP_Le:
1bb8d 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1bb8e 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70 2c 20 as TK_LE, jump,
1bb8f 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 in1, in3 */.case
1bb90 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20 20 20 OP_Gt:
1bb91 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1bb92 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20 69 6e TK_GT, jump, in
1bb93 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 1, in3 */.case O
1bb94 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20 20 20 P_Ge: {
1bb95 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1bb96 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c K_GE, jump, in1,
1bb97 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c in3 */. int fl
1bb98 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a ags;. int res;.
1bb99 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b char affinity;
1bb9a 0a 0a 20 20 66 6c 61 67 73 20 3d 20 70 49 6e 31 .. flags = pIn1
1bb9b 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e 66 6c ->flags|pIn3->fl
1bb9c 61 67 73 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 ags;.. if( flag
1bb9d 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 s&MEM_Null ){.
1bb9e 20 20 2f 2a 20 49 66 20 65 69 74 68 65 72 20 6f /* If either o
1bb9f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 perand is NULL t
1bba0 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 hen the result i
1bba1 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 s always NULL..
1bba2 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 ** The jump i
1bba3 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53 s taken if the S
1bba4 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1bba5 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20 bit is set..
1bba6 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d */. if( pOp-
1bba7 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f >p5 & SQLITE_STO
1bba8 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 70 4f REP2 ){. pO
1bba9 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1bbaa 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 4d 65 p->p2];. Me
1bbab 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1bbac 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 t, MEM_Null);.
1bbad 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1bbae 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 CE(pOp->p2, pOut
1bbaf 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
1bbb0 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 pOp->p5 & SQLIT
1bbb1 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 7b 0a E_JUMPIFNULL ){.
1bbb2 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1bbb3 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 p2-1;. }.
1bbb4 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 61 66 break;. }.. af
1bbb5 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 35 finity = pOp->p5
1bbb6 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 & SQLITE_AFF_MA
1bbb7 53 4b 3b 0a 20 20 69 66 28 20 61 66 66 69 6e 69 SK;. if( affini
1bbb8 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 ty ){. applyA
1bbb9 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 61 66 ffinity(pIn1, af
1bbba 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 finity, encoding
1bbbb 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 );. applyAffi
1bbbc 6e 69 74 79 28 70 49 6e 33 2c 20 61 66 66 69 6e nity(pIn3, affin
1bbbd 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a ity, encoding);.
1bbbe 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c if( db->mall
1bbbf 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 ocFailed ) goto
1bbc0 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 no_mem;. }.. a
1bbc1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1bbc2 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c pe==P4_COLLSEQ |
1bbc3 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d | pOp->p4.pColl=
1bbc4 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c =0 );. ExpandBl
1bbc5 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45 78 70 61 ob(pIn1);. Expa
1bbc6 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 ndBlob(pIn3);.
1bbc7 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d res = sqlite3Mem
1bbc8 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20 70 49 Compare(pIn3, pI
1bbc9 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c n1, pOp->p4.pCol
1bbca 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f l);. switch( pO
1bbcb 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 p->opcode ){.
1bbcc 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 case OP_Eq:
1bbcd 72 65 73 20 3d 20 72 65 73 3d 3d 30 3b 20 20 20 res = res==0;
1bbce 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
1bbcf 65 20 4f 50 5f 4e 65 3a 20 20 20 20 72 65 73 20 e OP_Ne: res
1bbd0 3d 20 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72 = res!=0; br
1bbd1 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 eak;. case OP
1bbd2 5f 4c 74 3a 20 20 20 20 72 65 73 20 3d 20 72 65 _Lt: res = re
1bbd3 73 3c 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b s<0; break;
1bbd4 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a . case OP_Le:
1bbd5 20 20 20 20 72 65 73 20 3d 20 72 65 73 3c 3d 30 res = res<=0
1bbd6 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
1bbd7 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 case OP_Gt:
1bbd8 72 65 73 20 3d 20 72 65 73 3e 30 3b 20 20 20 20 res = res>0;
1bbd9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 break;. def
1bbda 61 75 6c 74 3a 20 20 20 20 20 20 20 72 65 73 20 ault: res
1bbdb 3d 20 72 65 73 3e 3d 30 3b 20 20 20 20 20 62 72 = res>=0; br
1bbdc 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 eak;. }.. if(
1bbdd 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 pOp->p5 & SQLITE
1bbde 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 _STOREP2 ){.
1bbdf 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1bbe0 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 4d 65 pOp->p2];. Me
1bbe1 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1bbe2 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 t, MEM_Int);.
1bbe3 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72 65 73 pOut->u.i = res
1bbe4 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 ;. REGISTER_T
1bbe5 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f RACE(pOp->p2, pO
1bbe6 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ut);. }else if(
1bbe7 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d res ){. pc =
1bbe8 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a pOp->p2-1;. }.
1bbe9 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1bbea 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 pcode: Permutati
1bbeb 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a on * * * P4 *.**
1bbec 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d .** Set the perm
1bbed 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 utation used by
1bbee 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f the OP_Compare o
1bbef 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 perator to be th
1bbf0 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e e array.** of in
1bbf1 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a tegers in P4..**
1bbf2 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 .** The permutat
1bbf3 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 ion is only vali
1bbf4 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 d until the next
1bbf5 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c OP_Permutation,
1bbf6 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 OP_Compare,.**
1bbf7 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 OP_Halt, or OP_R
1bbf8 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 esultRow. Typic
1bbf9 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d ally the OP_Perm
1bbfa 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f utation should o
1bbfb 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 ccur.** immediat
1bbfc 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 ely prior to the
1bbfd 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a OP_Compare..*/.
1bbfe 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 case OP_Permutat
1bbff 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 ion: {. assert(
1bc00 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1bc01 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 _INTARRAY );. a
1bc02 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 ssert( pOp->p4.a
1bc03 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 i );. aPermute
1bc04 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 = pOp->p4.ai;.
1bc05 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1bc06 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 ode: Compare P1
1bc07 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 P3 P4 *.**.**
1bc08 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 Compare to vect
1bc09 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 ors of registers
1bc0a 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 in reg(P1)..reg
1bc0b 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 (P1+P3-1) (all t
1bc0c 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 his.** one "A")
1bc0d 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e and in reg(P2)..
1bc0e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 reg(P2+P3-1) ("B
1bc0f 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 "). Save the re
1bc10 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 sult of.** the c
1bc11 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 omparison for us
1bc12 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 e by the next OP
1bc13 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a _Jump instruct..
1bc14 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 **.** P4 is a Ke
1bc15 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
1bc16 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c that defines col
1bc17 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 lating sequences
1bc18 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 and sort.** ord
1bc19 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 ers for the comp
1bc1a 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 arison. The per
1bc1b 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 mutation applies
1bc1c 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a to registers.**
1bc1d 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 only. The KeyI
1bc1e 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 nfo elements are
1bc1f 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c used sequential
1bc20 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ly..**.** The co
1bc21 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f mparison is a so
1bc22 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 rt comparison, s
1bc23 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 o NULLs compare
1bc24 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 equal,.** NULLs
1bc25 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 are less than nu
1bc26 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 mbers, numbers a
1bc27 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 re less than str
1bc28 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 ings,.** and str
1bc29 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 ings are less th
1bc2a 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 an blobs..*/.cas
1bc2b 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a e OP_Compare: {.
1bc2c 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 int n = pOp->p
1bc2d 33 3b 0a 20 20 69 6e 74 20 69 2c 20 70 31 2c 20 3;. int i, p1,
1bc2e 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 p2;. const KeyI
1bc2f 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 nfo *pKeyInfo =
1bc30 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f pOp->p4.pKeyInfo
1bc31 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 ;. assert( n>0
1bc32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 );. assert( pKe
1bc33 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 70 31 yInfo!=0 );. p1
1bc34 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 = pOp->p1;. as
1bc35 73 65 72 74 28 20 70 31 3e 30 20 26 26 20 70 31 sert( p1>0 && p1
1bc36 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b +n<=p->nMem+1 );
1bc37 0a 20 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b . p2 = pOp->p2;
1bc38 0a 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20 . assert( p2>0
1bc39 26 26 20 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d && p2+n<=p->nMem
1bc3a 2b 31 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b +1 );. for(i=0;
1bc3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<n; i++){.
1bc3c 69 6e 74 20 69 64 78 20 3d 20 61 50 65 72 6d 75 int idx = aPermu
1bc3d 74 65 20 3f 20 61 50 65 72 6d 75 74 65 5b 69 5d te ? aPermute[i]
1bc3e 20 3a 20 69 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 : i;. CollSe
1bc3f 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 q *pColl; /*
1bc40 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e Collating sequen
1bc41 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 ce to use on thi
1bc42 73 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e s term */. in
1bc43 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 t bRev;
1bc44 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 /* True for DES
1bc45 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 CENDING sort ord
1bc46 65 72 20 2a 2f 0a 20 20 20 20 52 45 47 49 53 54 er */. REGIST
1bc47 45 52 5f 54 52 41 43 45 28 70 31 2b 69 64 78 2c ER_TRACE(p1+idx,
1bc48 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 &p->aMem[p1+idx
1bc49 5d 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 ]);. REGISTER
1bc4a 5f 54 52 41 43 45 28 70 32 2b 69 64 78 2c 20 26 _TRACE(p2+idx, &
1bc4b 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 5d 29 p->aMem[p2+idx])
1bc4c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c ;. assert( i<
1bc4d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1bc4e 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 );. pColl =
1bc4f 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b pKeyInfo->aColl[
1bc50 69 5d 3b 0a 20 20 20 20 62 52 65 76 20 3d 20 70 i];. bRev = p
1bc51 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 KeyInfo->aSortOr
1bc52 64 65 72 5b 69 5d 3b 0a 20 20 20 20 69 43 6f 6d der[i];. iCom
1bc53 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 pare = sqlite3Me
1bc54 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65 mCompare(&p->aMe
1bc55 6d 5b 70 31 2b 69 64 78 5d 2c 20 26 70 2d 3e 61 m[p1+idx], &p->a
1bc56 4d 65 6d 5b 70 32 2b 69 64 78 5d 2c 20 70 43 6f Mem[p2+idx], pCo
1bc57 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f ll);. if( iCo
1bc58 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 69 mpare ){. i
1bc59 66 28 20 62 52 65 76 20 29 20 69 43 6f 6d 70 61 f( bRev ) iCompa
1bc5a 72 65 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a re = -iCompare;.
1bc5b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1bc5c 20 7d 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 }. }. aPermut
1bc5d 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a e = 0;. break;.
1bc5e 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 }../* Opcode: Ju
1bc5f 6d 70 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a mp P1 P2 P3 * *.
1bc60 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 **.** Jump to th
1bc61 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 e instruction at
1bc62 20 61 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c address P1, P2,
1bc63 20 6f 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67 or P3 depending
1bc64 20 6f 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 on whether.** i
1bc65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e n the most recen
1bc66 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 t OP_Compare ins
1bc67 74 72 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20 truction the P1
1bc68 76 65 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20 vector was less
1bc69 74 68 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f than.** equal to
1bc6a 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 , or greater tha
1bc6b 6e 20 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c n the P2 vector,
1bc6c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a respectively..*
1bc6d 2f 0a 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 /.case OP_Jump:
1bc6e 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1bc6f 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 jump */. if( i
1bc70 43 6f 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 Compare<0 ){.
1bc71 20 70 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 pc = pOp->p1 -
1bc72 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 1;. }else if( i
1bc73 43 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 Compare==0 ){.
1bc74 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1bc75 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
1bc76 20 70 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 pc = pOp->p3 -
1bc77 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1bc78 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e }../* Opcode: An
1bc79 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1bc7a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f *.** Take the lo
1bc7b 67 69 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 gical AND of the
1bc7c 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 values in regis
1bc7d 74 65 72 73 20 50 31 20 61 6e 64 20 50 32 20 61 ters P1 and P2 a
1bc7e 6e 64 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 nd.** write the
1bc7f 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 result into regi
1bc80 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 ster P3..**.** I
1bc81 66 20 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 f either P1 or P
1bc82 32 20 69 73 20 30 20 28 66 61 6c 73 65 29 20 74 2 is 0 (false) t
1bc83 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 hen the result i
1bc84 73 20 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 s 0 even if.** t
1bc85 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69 he other input i
1bc86 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 s NULL. A NULL
1bc87 61 6e 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20 and true or two
1bc88 4e 55 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 NULLs give.** a
1bc89 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a NULL output..*/.
1bc8a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 /* Opcode: Or P1
1bc8b 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1bc8c 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 Take the logica
1bc8d 6c 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 l OR of the valu
1bc8e 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 es in register P
1bc8f 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 1 and P2 and.**
1bc90 73 74 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 store the answer
1bc91 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1bc92 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 .**.** If either
1bc93 20 50 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e P1 or P2 is non
1bc94 7a 65 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e zero (true) then
1bc95 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 31 the result is 1
1bc96 20 28 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 (true).** even
1bc97 69 66 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 if the other inp
1bc98 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e ut is NULL. A N
1bc99 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 ULL and false or
1bc9a 20 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 two NULLs.** gi
1bc9b 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 ve a NULL output
1bc9c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 ..*/.case OP_And
1bc9d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f : /
1bc9e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 * same as TK_AND
1bc9f 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
1bca0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 */.case OP_Or:
1bca1 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1bca2 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 same as TK_OR,
1bca3 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1bca4 2f 0a 20 20 69 6e 74 20 76 31 2c 20 76 32 3b 20 /. int v1, v2;
1bca5 20 20 20 2f 2a 20 30 3d 3d 46 41 4c 53 45 2c 20 /* 0==FALSE,
1bca6 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 1==TRUE, 2==UNKN
1bca7 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 0a OWN or NULL */..
1bca8 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
1bca9 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1bcaa 20 20 20 20 76 31 20 3d 20 32 3b 0a 20 20 7d 65 v1 = 2;. }e
1bcab 6c 73 65 7b 0a 20 20 20 20 76 31 20 3d 20 73 71 lse{. v1 = sq
1bcac 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1bcad 65 28 70 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a e(pIn1)!=0;. }.
1bcae 20 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 if( pIn2->flag
1bcaf 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1bcb0 20 20 20 20 76 32 20 3d 20 32 3b 0a 20 20 7d 65 v2 = 2;. }e
1bcb1 6c 73 65 7b 0a 20 20 20 20 76 32 20 3d 20 73 71 lse{. v2 = sq
1bcb2 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1bcb3 65 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a e(pIn2)!=0;. }.
1bcb4 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
1bcb5 65 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 e==OP_And ){.
1bcb6 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e static const un
1bcb7 73 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f signed char and_
1bcb8 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 logic[] = { 0, 0
1bcb9 2c 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c , 0, 0, 1, 2, 0,
1bcba 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 2, 2 };. v1
1bcbb 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b 76 31 2a 33 = and_logic[v1*3
1bcbc 2b 76 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 +v2];. }else{.
1bcbd 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
1bcbe 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 unsigned char or
1bcbf 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 _logic[] = { 0,
1bcc0 31 2c 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 1, 2, 1, 1, 1, 2
1bcc1 2c 20 31 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 , 1, 2 };. v1
1bcc2 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b 76 31 2a 33 = or_logic[v1*3
1bcc3 2b 76 32 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 +v2];. }. if(
1bcc4 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d 65 6d v1==2 ){. Mem
1bcc5 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1bcc6 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d , MEM_Null);. }
1bcc7 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74 2d 3e else{. pOut->
1bcc8 75 2e 69 20 3d 20 76 31 3b 0a 20 20 20 20 4d 65 u.i = v1;. Me
1bcc9 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1bcca 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d t, MEM_Int);. }
1bccb 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1bccc 4f 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 Opcode: Not P1 P
1bccd 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 2 * * *.**.** In
1bcce 74 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 terpret the valu
1bccf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1bcd0 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 as a boolean va
1bcd1 6c 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a lue. Store the.
1bcd2 2a 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c ** boolean compl
1bcd3 65 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65 ement in registe
1bcd4 72 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61 r P2. If the va
1bcd5 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1bcd6 50 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 P1 is .** NULL,
1bcd7 74 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73 then a NULL is s
1bcd8 74 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a tored in P2..*/.
1bcd9 63 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 case OP_Not: {
1bcda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bcdb 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c same as TK_NOT,
1bcdc 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d in1 */. pOut =
1bcdd 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1bcde 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 2];. if( pIn1->
1bcdf 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1bce0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1bce1 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f dbeMemSetNull(pO
1bce2 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ut);. }else{.
1bce3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1bce4 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21 SetInt64(pOut, !
1bce5 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
1bce6 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a lue(pIn1));. }.
1bce7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1bce8 70 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 pcode: BitNot P1
1bce9 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1bcea 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f Interpret the co
1bceb 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 ntent of registe
1bcec 72 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67 r P1 as an integ
1bced 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a er. Store the.*
1bcee 2a 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e * ones-complemen
1bcef 74 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75 t of the P1 valu
1bcf0 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
1bcf1 50 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73 P2. If P1 holds
1bcf2 0a 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 .** a NULL then
1bcf3 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 store a NULL in
1bcf4 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 P2..*/.case OP_B
1bcf5 69 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 itNot: {
1bcf6 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1bcf7 54 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a TK_BITNOT, in1 *
1bcf8 2f 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 /. pOut = &p->a
1bcf9 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1bcfa 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 if( pIn1->flags
1bcfb 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1bcfc 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1bcfd 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 SetNull(pOut);.
1bcfe 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
1bcff 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
1bd00 36 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 64(pOut, ~sqlite
1bd01 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
1bd02 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 n1));. }. brea
1bd03 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1bd04 20 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a If P1 P2 P3 * *
1bd05 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 .**.** Jump to P
1bd06 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 2 if the value i
1bd07 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 n register P1 is
1bd08 20 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 true. The valu
1bd09 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 e is.** is consi
1bd0a 64 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 dered true if it
1bd0b 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 is numeric and
1bd0c 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 non-zero. If th
1bd0d 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 e value.** in P1
1bd0e 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 is NULL then ta
1bd0f 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 ke the jump if P
1bd10 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 3 is true..*/./*
1bd11 20 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 Opcode: IfNot P
1bd12 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1bd13 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 * Jump to P2 if
1bd14 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1bd15 69 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 ister P1 is Fals
1bd16 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 e. The value is
1bd17 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 .** is considere
1bd18 64 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 d true if it has
1bd19 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 a numeric value
1bd1a 20 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 of zero. If th
1bd1b 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 e value.** in P1
1bd1c 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 is NULL then ta
1bd1d 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 ke the jump if P
1bd1e 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 3 is true..*/.ca
1bd1f 73 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 se OP_If:
1bd20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d /* jum
1bd21 70 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f p, in1 */.case O
1bd22 50 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 P_IfNot: {
1bd23 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
1bd24 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 20 n1 */. int c;.
1bd25 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 if( pIn1->flags
1bd26 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1bd27 20 20 20 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a c = pOp->p3;.
1bd28 20 20 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 }else{.#ifdef
1bd29 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
1bd2a 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63 TING_POINT. c
1bd2b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
1bd2c 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b tValue(pIn1)!=0;
1bd2d 0a 23 65 6c 73 65 0a 20 20 20 20 63 20 3d 20 73 .#else. c = s
1bd2e 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
1bd2f 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b 0a lue(pIn1)!=0.0;.
1bd30 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70 #endif. if( p
1bd31 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 Op->opcode==OP_I
1bd32 66 4e 6f 74 20 29 20 63 20 3d 20 21 63 3b 0a 20 fNot ) c = !c;.
1bd33 20 7d 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 }. if( c ){.
1bd34 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 pc = pOp->p2-1
1bd35 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1bd36 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e ../* Opcode: IsN
1bd37 75 6c 6c 20 50 31 20 50 32 20 50 33 20 2a 20 2a ull P1 P2 P3 * *
1bd38 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 .**.** Jump to P
1bd39 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 2 if the value i
1bd3a 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 n register P1 is
1bd3b 20 4e 55 4c 4c 2e 20 20 49 66 20 50 33 20 69 73 NULL. If P3 is
1bd3c 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e greater.** than
1bd3d 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68 65 63 zero, then chec
1bd3e 6b 20 61 6c 6c 20 76 61 6c 75 65 73 20 72 65 67 k all values reg
1bd3f 28 50 31 29 2c 20 72 65 67 28 50 31 2b 31 29 2c (P1), reg(P1+1),
1bd40 20 0a 2a 2a 20 72 65 67 28 50 31 2b 32 29 2c 20 .** reg(P1+2),
1bd41 2e 2e 2e 2c 20 72 65 67 28 50 31 2b 50 33 2d 31 ..., reg(P1+P3-1
1bd42 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73 )..*/.case OP_Is
1bd43 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 Null: {
1bd44 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1bd45 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 _ISNULL, jump, i
1bd46 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 6e 20 3d 20 n1 */. int n =
1bd47 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 pOp->p3;. asser
1bd48 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 7c 7c t( pOp->p3==0 ||
1bd49 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 pOp->p1>0 );.
1bd4a 64 6f 7b 0a 20 20 20 20 69 66 28 20 28 70 49 6e do{. if( (pIn
1bd4b 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 1->flags & MEM_N
1bd4c 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ull)!=0 ){.
1bd4d 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1bd4e 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 1;. break;.
1bd4f 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 31 2b 2b }. pIn1++
1bd50 3b 0a 20 20 7d 77 68 69 6c 65 28 20 2d 2d 6e 20 ;. }while( --n
1bd51 3e 20 30 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a > 0 );. break;.
1bd52 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f }../* Opcode: No
1bd53 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 tNull P1 P2 * *
1bd54 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 *.**.** Jump to
1bd55 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 P2 if the value
1bd56 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 in register P1 i
1bd57 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f s not NULL. .*/
1bd58 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c .case OP_NotNull
1bd59 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1bd5a 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 * same as TK_NOT
1bd5b 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 NULL, jump, in1
1bd5c 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e */. if( (pIn1->
1bd5d 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1bd5e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d )==0 ){. pc =
1bd5f 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1bd60 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1bd61 20 4f 70 63 6f 64 65 3a 20 53 65 74 4e 75 6d 43 Opcode: SetNumC
1bd62 6f 6c 75 6d 6e 73 20 2a 20 50 32 20 2a 20 2a 20 olumns * P2 * *
1bd63 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 *.**.** This opc
1bd64 6f 64 65 20 73 65 74 73 20 74 68 65 20 6e 75 6d ode sets the num
1bd65 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 ber of columns f
1bd66 6f 72 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 or the cursor op
1bd67 65 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 66 ened by the.** f
1bd68 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 ollowing instruc
1bd69 74 69 6f 6e 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a tion to P2..**.*
1bd6a 2a 20 41 6e 20 4f 50 5f 53 65 74 4e 75 6d 43 6f * An OP_SetNumCo
1bd6b 6c 75 6d 6e 73 20 69 73 20 6f 6e 6c 79 20 75 73 lumns is only us
1bd6c 65 66 75 6c 20 69 66 20 69 74 20 6f 63 63 75 72 eful if it occur
1bd6d 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 s immediately be
1bd6e 66 6f 72 65 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 fore .** one of
1bd6f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 the following op
1bd70 63 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 codes:.**.**
1bd71 20 4f 70 65 6e 52 65 61 64 0a 2a 2a 20 20 20 20 OpenRead.**
1bd72 20 4f 70 65 6e 57 72 69 74 65 0a 2a 2a 20 20 20 OpenWrite.**
1bd73 20 20 4f 70 65 6e 50 73 65 75 64 6f 0a 2a 2a 0a OpenPseudo.**.
1bd74 2a 2a 20 49 66 20 74 68 65 20 4f 50 5f 43 6f 6c ** If the OP_Col
1bd75 75 6d 6e 20 6f 70 63 6f 64 65 20 69 73 20 74 6f umn opcode is to
1bd76 20 62 65 20 65 78 65 63 75 74 65 64 20 6f 6e 20 be executed on
1bd77 61 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 0a 2a a cursor, then.*
1bd78 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 20 6d 75 * this opcode mu
1bd79 73 74 20 62 65 20 70 72 65 73 65 6e 74 20 69 6d st be present im
1bd7a 6d 65 64 69 61 74 65 6c 79 20 62 65 66 6f 72 65 mediately before
1bd7b 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74 the opcode that
1bd7c 0a 2a 2a 20 6f 70 65 6e 73 20 74 68 65 20 63 75 .** opens the cu
1bd7d 72 73 6f 72 2e 0a 2a 2f 0a 23 69 66 20 30 0a 63 rsor..*/.#if 0.c
1bd7e 61 73 65 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c ase OP_SetNumCol
1bd7f 75 6d 6e 73 3a 20 7b 0a 20 20 62 72 65 61 6b 3b umns: {. break;
1bd80 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 .}.#endif../* Op
1bd81 63 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20 code: Column P1
1bd82 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 P3 P4 *.**.**
1bd83 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 64 Interpret the d
1bd84 61 74 61 20 74 68 61 74 20 63 75 72 73 6f 72 20 ata that cursor
1bd85 50 31 20 70 6f 69 6e 74 73 20 74 6f 20 61 73 20 P1 points to as
1bd86 61 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c a structure buil
1bd87 74 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 4d t using.** the M
1bd88 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 akeRecord instru
1bd89 63 74 69 6f 6e 2e 20 20 28 53 65 65 20 74 68 65 ction. (See the
1bd8a 20 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f MakeRecord opco
1bd8b 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 de for additiona
1bd8c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e l.** information
1bd8d 20 61 62 6f 75 74 20 74 68 65 20 66 6f 72 6d 61 about the forma
1bd8e 74 20 6f 66 20 74 68 65 20 64 61 74 61 2e 29 20 t of the data.)
1bd8f 20 45 78 74 72 61 63 74 20 74 68 65 20 50 32 2d Extract the P2-
1bd90 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f th column.** fro
1bd91 6d 20 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20 m this record.
1bd92 49 66 20 74 68 65 72 65 20 61 72 65 20 6c 65 73 If there are les
1bd93 73 20 74 68 61 74 20 28 50 32 2b 31 29 20 0a 2a s that (P2+1) .*
1bd94 2a 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 * values in the
1bd95 72 65 63 6f 72 64 2c 20 65 78 74 72 61 63 74 20 record, extract
1bd96 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 a NULL..**.** Th
1bd97 65 20 76 61 6c 75 65 20 65 78 74 72 61 63 74 65 e value extracte
1bd98 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 d is stored in r
1bd99 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a egister P3..**.*
1bd9a 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 * If the column
1bd9b 63 6f 6e 74 61 69 6e 73 20 66 65 77 65 72 20 74 contains fewer t
1bd9c 68 61 6e 20 50 32 20 66 69 65 6c 64 73 2c 20 74 han P2 fields, t
1bd9d 68 65 6e 20 65 78 74 72 61 63 74 20 61 20 4e 55 hen extract a NU
1bd9e 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 LL. Or,.** if t
1bd9f 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 he P4 argument i
1bda0 73 20 61 20 50 34 5f 4d 45 4d 20 75 73 65 20 74 s a P4_MEM use t
1bda1 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1bda2 50 34 20 61 72 67 75 6d 65 6e 74 20 61 73 0a 2a P4 argument as.*
1bda3 2a 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f * the result..*/
1bda4 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a .case OP_Column:
1bda5 20 7b 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 {. int payload
1bda6 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 Size; /* Numbe
1bda7 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
1bda8 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e e record */. in
1bda9 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 t p1 = pOp->p1;
1bdaa 20 2f 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 20 /* P1 value of
1bdab 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 the opcode */.
1bdac 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 int p2 = pOp->p2
1bdad 3b 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d ; /* column num
1bdae 62 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 ber to retrieve
1bdaf 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
1bdb0 2a 70 43 20 3d 20 30 3b 2f 2a 20 54 68 65 20 56 *pC = 0;/* The V
1bdb1 44 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 DBE cursor */.
1bdb2 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 char *zRec;
1bdb3 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1bdb4 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 complete record
1bdb5 2d 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 -data */. BtCur
1bdb6 73 6f 72 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a sor *pCrsr; /*
1bdb7 20 54 68 65 20 42 54 72 65 65 20 63 75 72 73 6f The BTree curso
1bdb8 72 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 r */. u32 *aTyp
1bdb9 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 e; /* aTy
1bdba 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 pe[i] holds the
1bdbb 6e 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 numeric type of
1bdbc 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 the i-th column
1bdbd 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 */. u32 *aOffse
1bdbe 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 t; /* aOffs
1bdbf 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 et[i] is offset
1bdc0 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 to start of data
1bdc1 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e for i-th column
1bdc2 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 */. int nField
1bdc3 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 ; /* numb
1bdc4 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 er of fields in
1bdc5 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1bdc6 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 int len;
1bdc7 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 /* The length
1bdc8 20 6f 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a of the serializ
1bdc9 65 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 ed data for the
1bdca 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 column */. int
1bdcb 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f i; /
1bdcc 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1bdcd 2f 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b /. char *zData;
1bdce 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f /* Part o
1bdcf 66 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 f the record bei
1bdd0 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 ng decoded */.
1bdd1 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20 Mem *pDest;
1bdd2 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 /* Where to w
1bdd3 72 69 74 65 20 74 68 65 20 65 78 74 72 61 63 74 rite the extract
1bdd4 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 4d 65 ed value */. Me
1bdd5 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 m sMem;
1bdd6 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 /* For storing
1bdd7 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 the record being
1bdd8 20 64 65 63 6f 64 65 64 20 2a 2f 0a 0a 20 20 6d decoded */.. m
1bdd9 65 6d 73 65 74 28 26 73 4d 65 6d 2c 20 30 2c 20 emset(&sMem, 0,
1bdda 73 69 7a 65 6f 66 28 73 4d 65 6d 29 29 3b 0a 20 sizeof(sMem));.
1bddb 20 61 73 73 65 72 74 28 20 70 31 3c 70 2d 3e 6e assert( p1<p->n
1bddc 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 Cursor );. asse
1bddd 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 rt( pOp->p3>0 &&
1bdde 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 pOp->p3<=p->nMe
1bddf 6d 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20 26 m );. pDest = &
1bde0 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1bde1 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
1bde2 61 67 28 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 ag(pDest, MEM_Nu
1bde3 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 ll);.. /* This
1bde4 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 block sets the v
1bde5 61 72 69 61 62 6c 65 20 70 61 79 6c 6f 61 64 53 ariable payloadS
1bde6 69 7a 65 20 74 6f 20 62 65 20 74 68 65 20 74 6f ize to be the to
1bde7 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 tal number of.
1bde8 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 ** bytes in the
1bde9 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a 20 20 2a record.. **. *
1bdea 2a 20 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f * zRec is set to
1bdeb 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 be the complete
1bdec 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63 text of the rec
1bded 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61 ord if it is ava
1bdee 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65 ilable.. ** The
1bdef 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 complete record
1bdf0 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20 text is always
1bdf1 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73 available for ps
1bdf2 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a eudo-tables. **
1bdf3 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 If the record i
1bdf4 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75 s stored in a cu
1bdf5 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65 rsor, the comple
1bdf6 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20 te record text.
1bdf7 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61 ** might be ava
1bdf8 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 70 ilable in the p
1bdf9 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 C->aRow cache.
1bdfa 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 Or it might not
1bdfb 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 be.. ** If the
1bdfc 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61 data is unavaila
1bdfd 62 6c 65 2c 20 20 7a 52 65 63 20 69 73 20 73 65 ble, zRec is se
1bdfe 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2a 0a t to NULL.. **.
1bdff 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 6f 6d ** We also com
1be00 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 pute the number
1be01 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
1be02 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 20 63 e record. For c
1be03 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 68 65 ursors,. ** the
1be04 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1be05 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ns is stored in
1be06 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 6e the VdbeCursor.n
1be07 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 0a 20 Field element..
1be08 20 2a 2f 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 */. pC = p->ap
1be09 43 73 72 5b 70 31 5d 3b 0a 20 20 61 73 73 65 72 Csr[p1];. asser
1be0a 74 28 20 70 43 21 3d 30 20 29 3b 0a 23 69 66 6e t( pC!=0 );.#ifn
1be0b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1be0c 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 61 VIRTUALTABLE. a
1be0d 73 73 65 72 74 28 20 70 43 2d 3e 70 56 74 61 62 ssert( pC->pVtab
1be0e 43 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e Cursor==0 );.#en
1be0f 64 69 66 0a 20 20 69 66 28 20 70 43 2d 3e 70 43 dif. if( pC->pC
1be10 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 ursor!=0 ){.
1be11 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 /* The record is
1be12 20 73 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 stored in a B-T
1be13 72 65 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 ree */. rc =
1be14 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1be15 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20 rMoveto(pC);.
1be16 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 if( rc ) goto a
1be17 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1be18 72 3b 0a 20 20 20 20 7a 52 65 63 20 3d 20 30 3b r;. zRec = 0;
1be19 0a 20 20 20 20 70 43 72 73 72 20 3d 20 70 43 2d . pCrsr = pC-
1be1a 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 >pCursor;. if
1be1b 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b ( pC->nullRow ){
1be1c 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69 . payloadSi
1be1d 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ze = 0;. }els
1be1e 65 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 53 e if( pC->cacheS
1be1f 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 tatus==p->cacheC
1be20 74 72 20 29 7b 0a 20 20 20 20 20 20 70 61 79 6c tr ){. payl
1be21 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e 70 61 oadSize = pC->pa
1be22 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 yloadSize;.
1be23 20 7a 52 65 63 20 3d 20 28 63 68 61 72 2a 29 70 zRec = (char*)p
1be24 43 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c C->aRow;. }el
1be25 73 65 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 se if( pC->isInd
1be26 65 78 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 ex ){. i64
1be27 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 payloadSize64;.
1be28 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1be29 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c 20 eKeySize(pCrsr,
1be2a 26 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29 3b &payloadSize64);
1be2b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69 . payloadSi
1be2c 7a 65 20 3d 20 28 69 6e 74 29 70 61 79 6c 6f 61 ze = (int)payloa
1be2d 64 53 69 7a 65 36 34 3b 0a 20 20 20 20 7d 65 6c dSize64;. }el
1be2e 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
1be2f 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 70 3BtreeDataSize(p
1be30 43 72 73 72 2c 20 28 75 33 32 20 2a 29 26 70 61 Crsr, (u32 *)&pa
1be31 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 20 20 yloadSize);.
1be32 7d 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 }. nField = p
1be33 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 7d 65 6c C->nField;. }el
1be34 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
1be35 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 pC->pseudoTable
1be36 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 );. /* The re
1be37 63 6f 72 64 20 69 73 20 74 68 65 20 73 6f 6c 65 cord is the sole
1be38 20 65 6e 74 72 79 20 6f 66 20 61 20 70 73 65 75 entry of a pseu
1be39 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 do-table */.
1be3a 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43 payloadSize = pC
1be3b 2d 3e 6e 44 61 74 61 3b 0a 20 20 20 20 7a 52 65 ->nData;. zRe
1be3c 63 20 3d 20 70 43 2d 3e 70 44 61 74 61 3b 0a 20 c = pC->pData;.
1be3d 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 pC->cacheStat
1be3e 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1be3f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 ;. assert( pa
1be40 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 7c 7c 20 yloadSize==0 ||
1be41 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20 20 20 6e zRec!=0 );. n
1be42 46 69 65 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 Field = pC->nFie
1be43 6c 64 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20 ld;. pCrsr =
1be44 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 0;. }.. /* If
1be45 70 61 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 payloadSize is 0
1be46 2c 20 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 , then just stor
1be47 65 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 e a NULL */. if
1be48 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 ( payloadSize==0
1be49 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1be4a 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d pDest->flags&MEM
1be4b 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74 _Null );. got
1be4c 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b o op_column_out;
1be4d 0a 20 20 7d 0a 20 20 69 66 28 20 70 61 79 6c 6f . }. if( paylo
1be4e 61 64 53 69 7a 65 3e 64 62 2d 3e 61 4c 69 6d 69 adSize>db->aLimi
1be4f 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1be50 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f ENGTH] ){. go
1be51 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1be52 0a 20 20 61 73 73 65 72 74 28 20 70 32 3c 6e 46 . assert( p2<nF
1be53 69 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 ield );.. /* Re
1be54 61 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 ad and parse the
1be55 20 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 table header.
1be56 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 Store the result
1be57 73 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 s of the parse.
1be58 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 ** into the rec
1be59 6f 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 ord header cache
1be5a 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 fields of the c
1be5b 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 61 54 ursor.. */. aT
1be5c 79 70 65 20 3d 20 70 43 2d 3e 61 54 79 70 65 3b ype = pC->aType;
1be5d 0a 20 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 . if( pC->cache
1be5e 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 Status==p->cache
1be5f 43 74 72 20 29 7b 0a 20 20 20 20 61 4f 66 66 73 Ctr ){. aOffs
1be60 65 74 20 3d 20 70 43 2d 3e 61 4f 66 66 73 65 74 et = pC->aOffset
1be61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
1be62 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20 8 *zIdx;
1be63 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 /* Index into he
1be64 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a ader */. u8 *
1be65 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 2f 2a 20 zEndHdr; /*
1be66 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 Pointer to first
1be67 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 byte after the
1be68 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e header */. in
1be69 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 2f t offset; /
1be6a 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 * Offset into th
1be6b 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 6e e data */. in
1be6c 74 20 73 7a 48 64 72 53 7a 3b 20 20 20 20 20 2f t szHdrSz; /
1be6d 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 * Size of the he
1be6e 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 ader size field
1be6f 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f at start of reco
1be70 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 76 rd */. int av
1be71 61 69 6c 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 ail = 0; /* Nu
1be72 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1be73 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 available data
1be74 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 61 */.. assert(a
1be75 54 79 70 65 29 3b 0a 20 20 20 20 70 43 2d 3e 61 Type);. pC->a
1be76 4f 66 66 73 65 74 20 3d 20 61 4f 66 66 73 65 74 Offset = aOffset
1be77 20 3d 20 26 61 54 79 70 65 5b 6e 46 69 65 6c 64 = &aType[nField
1be78 5d 3b 0a 20 20 20 20 70 43 2d 3e 70 61 79 6c 6f ];. pC->paylo
1be79 61 64 53 69 7a 65 20 3d 20 70 61 79 6c 6f 61 64 adSize = payload
1be7a 53 69 7a 65 3b 0a 20 20 20 20 70 43 2d 3e 63 61 Size;. pC->ca
1be7b 63 68 65 53 74 61 74 75 73 20 3d 20 70 2d 3e 63 cheStatus = p->c
1be7c 61 63 68 65 43 74 72 3b 0a 0a 20 20 20 20 2f 2a acheCtr;.. /*
1be7d 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 Figure out how
1be7e 6d 61 6e 79 20 62 79 74 65 73 20 61 72 65 20 69 many bytes are i
1be7f 6e 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a n the header */.
1be80 20 20 20 20 69 66 28 20 7a 52 65 63 20 29 7b 0a if( zRec ){.
1be81 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 7a 52 zData = zR
1be82 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ec;. }else{.
1be83 20 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 49 if( pC->isI
1be84 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 ndex ){.
1be85 7a 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 zData = (char*)s
1be86 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 qlite3BtreeKeyFe
1be87 74 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 tch(pCrsr, &avai
1be88 6c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b l);. }else{
1be89 0a 20 20 20 20 20 20 20 20 7a 44 61 74 61 20 3d . zData =
1be8a 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42 (char*)sqlite3B
1be8b 74 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43 treeDataFetch(pC
1be8c 72 73 72 2c 20 26 61 76 61 69 6c 29 3b 0a 20 20 rsr, &avail);.
1be8d 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 }. /* I
1be8e 66 20 4b 65 79 46 65 74 63 68 28 29 2f 44 61 74 f KeyFetch()/Dat
1be8f 61 46 65 74 63 68 28 29 20 6d 61 6e 61 67 65 64 aFetch() managed
1be90 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 74 69 to get the enti
1be91 72 65 20 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 re payload,.
1be92 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 70 61 ** save the pa
1be93 79 6c 6f 61 64 20 69 6e 20 74 68 65 20 70 43 2d yload in the pC-
1be94 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 54 68 >aRow cache. Th
1be95 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75 73 20 at will save us
1be96 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 68 61 from. ** ha
1be97 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 64 64 ving to make add
1be98 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 74 6f itional calls to
1be99 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 fetch the conte
1be9a 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a 20 20 nt portion of.
1be9b 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 ** the recor
1be9c 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
1be9d 20 20 69 66 28 20 61 76 61 69 6c 3e 3d 70 61 79 if( avail>=pay
1be9e 6c 6f 61 64 53 69 7a 65 20 29 7b 0a 20 20 20 20 loadSize ){.
1be9f 20 20 20 20 7a 52 65 63 20 3d 20 7a 44 61 74 61 zRec = zData
1bea0 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 52 ;. pC->aR
1bea1 6f 77 20 3d 20 28 75 38 2a 29 7a 44 61 74 61 3b ow = (u8*)zData;
1bea2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1bea3 20 20 20 20 20 20 70 43 2d 3e 61 52 6f 77 20 3d pC->aRow =
1bea4 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
1bea5 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c }. /* The fol
1bea6 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 69 73 lowing assert is
1bea7 20 74 72 75 65 20 69 6e 20 61 6c 6c 20 63 61 73 true in all cas
1bea8 65 73 20 61 63 63 65 70 74 20 77 68 65 6e 0a 20 es accept when.
1bea9 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ** the databa
1beaa 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e se file has been
1beab 20 63 6f 72 72 75 70 74 65 64 20 65 78 74 65 72 corrupted exter
1beac 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a 20 20 20 nally.. **
1bead 20 61 73 73 65 72 74 28 20 7a 52 65 63 21 3d 30 assert( zRec!=0
1beae 20 7c 7c 20 61 76 61 69 6c 3e 3d 70 61 79 6c 6f || avail>=paylo
1beaf 61 64 53 69 7a 65 20 7c 7c 20 61 76 61 69 6c 3e adSize || avail>
1beb0 3d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 73 7a 48 =9 ); */. szH
1beb1 64 72 53 7a 20 3d 20 67 65 74 56 61 72 69 6e 74 drSz = getVarint
1beb2 33 32 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 6f 32((u8*)zData, o
1beb3 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 ffset);.. /*
1beb4 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 6f The KeyFetch() o
1beb5 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61 62 r DataFetch() ab
1beb6 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e 64 ove are fast and
1beb7 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 6e will get the en
1beb8 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f tire. ** reco
1beb9 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f 73 rd header in mos
1beba 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 68 t cases. But th
1bebb 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f 20 ey will fail to
1bebc 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 get the complete
1bebd 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 . ** record h
1bebe 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65 63 eader if the rec
1bebf 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 20 ord header does
1bec0 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 6e not fit on a sin
1bec1 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 gle page. **
1bec2 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 20 in the B-Tree.
1bec3 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e When that happen
1bec4 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 64 s, use sqlite3Vd
1bec5 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 beMemFromBtree()
1bec6 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 69 to. ** acqui
1bec7 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 re the complete
1bec8 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 20 header text..
1bec9 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 7a 52 65 */. if( !zRe
1beca 63 20 26 26 20 61 76 61 69 6c 3c 6f 66 66 73 65 c && avail<offse
1becb 74 20 29 7b 0a 20 20 20 20 20 20 73 4d 65 6d 2e t ){. sMem.
1becc 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 flags = 0;.
1becd 20 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 sMem.db = 0;.
1bece 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1becf 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 VdbeMemFromBtree
1bed0 28 70 43 72 73 72 2c 20 30 2c 20 6f 66 66 73 65 (pCrsr, 0, offse
1bed1 74 2c 20 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 t, pC->isIndex,
1bed2 26 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 &sMem);. if
1bed3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1bed4 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
1bed5 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 op_column_out;.
1bed6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 44 61 }. zDa
1bed7 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20 ta = sMem.z;.
1bed8 20 7d 0a 20 20 20 20 7a 45 6e 64 48 64 72 20 3d }. zEndHdr =
1bed9 20 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 6f 66 (u8 *)&zData[of
1beda 66 73 65 74 5d 3b 0a 20 20 20 20 7a 49 64 78 20 fset];. zIdx
1bedb 3d 20 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 73 = (u8 *)&zData[s
1bedc 7a 48 64 72 53 7a 5d 3b 0a 0a 20 20 20 20 2f 2a zHdrSz];.. /*
1bedd 20 53 63 61 6e 20 74 68 65 20 68 65 61 64 65 72 Scan the header
1bede 20 61 6e 64 20 75 73 65 20 69 74 20 74 6f 20 66 and use it to f
1bedf 69 6c 6c 20 69 6e 20 74 68 65 20 61 54 79 70 65 ill in the aType
1bee0 5b 5d 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 5d [] and aOffset[]
1bee1 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 73 2e 20 . ** arrays.
1bee2 20 61 54 79 70 65 5b 69 5d 20 77 69 6c 6c 20 63 aType[i] will c
1bee3 6f 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 ontain the type
1bee4 69 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 integer for the
1bee5 69 2d 74 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 i-th. ** colu
1bee6 6d 6e 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 69 mn and aOffset[i
1bee7 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 ] will contain t
1bee8 68 65 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 he offset from t
1bee9 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 he beginning.
1beea 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 ** of the recor
1beeb 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f d to the start o
1beec 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 f the data for t
1beed 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20 he i-th column.
1beee 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d */. for(i=
1beef 30 3b 20 69 3c 6e 46 69 65 6c 64 3b 20 69 2b 2b 0; i<nField; i++
1bef0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 49 64 ){. if( zId
1bef1 78 3c 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20 x<zEndHdr ){.
1bef2 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69 5d 20 aOffset[i]
1bef3 3d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 = offset;.
1bef4 20 20 7a 49 64 78 20 2b 3d 20 67 65 74 56 61 72 zIdx += getVar
1bef5 69 6e 74 33 32 28 7a 49 64 78 2c 20 61 54 79 70 int32(zIdx, aTyp
1bef6 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 6f e[i]);. o
1bef7 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 ffset += sqlite3
1bef8 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1bef9 6e 28 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 n(aType[i]);.
1befa 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1befb 20 20 2f 2a 20 49 66 20 69 20 69 73 20 6c 65 73 /* If i is les
1befc 73 20 74 68 61 74 20 6e 46 69 65 6c 64 2c 20 74 s that nField, t
1befd 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6c 65 hen there are le
1befe 73 73 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 ss fields in thi
1beff 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 63 s. ** rec
1bf00 6f 72 64 20 74 68 61 6e 20 53 65 74 4e 75 6d 43 ord than SetNumC
1bf01 6f 6c 75 6d 6e 73 20 69 6e 64 69 63 61 74 65 64 olumns indicated
1bf02 20 74 68 65 72 65 20 61 72 65 20 63 6f 6c 75 6d there are colum
1bf03 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 ns in the.
1bf04 20 20 2a 2a 20 74 61 62 6c 65 2e 20 53 65 74 20 ** table. Set
1bf05 74 68 65 20 6f 66 66 73 65 74 20 66 6f 72 20 61 the offset for a
1bf06 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73 ny extra columns
1bf07 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 0a not present in.
1bf08 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 ** the r
1bf09 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 73 ecord to 0. This
1bf0a 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c 6f tells code belo
1bf0b 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 4c w to store a NUL
1bf0c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 L. ** ins
1bf0d 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 6c tead of deserial
1bf0e 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 72 izing a value fr
1bf0f 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 om the record..
1bf10 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1bf11 20 20 61 4f 66 66 73 65 74 5b 69 5d 20 3d 20 30 aOffset[i] = 0
1bf12 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1bf13 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1bf14 65 6d 52 65 6c 65 61 73 65 28 26 73 4d 65 6d 29 emRelease(&sMem)
1bf15 3b 0a 20 20 20 20 73 4d 65 6d 2e 66 6c 61 67 73 ;. sMem.flags
1bf16 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 = MEM_Null;..
1bf17 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 /* If we have
1bf18 72 65 61 64 20 6d 6f 72 65 20 68 65 61 64 65 72 read more header
1bf19 20 64 61 74 61 20 74 68 61 6e 20 77 61 73 20 63 data than was c
1bf1a 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 ontained in the
1bf1b 68 65 61 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f header,. ** o
1bf1c 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 r if the end of
1bf1d 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 the last field a
1bf1e 70 70 65 61 72 73 20 74 6f 20 62 65 20 70 61 73 ppears to be pas
1bf1f 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1bf20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 . ** record,
1bf21 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 or if the end of
1bf22 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 the last field
1bf23 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 62 65 appears to be be
1bf24 66 6f 72 65 20 74 68 65 20 65 6e 64 0a 20 20 20 fore the end.
1bf25 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 ** of the recor
1bf26 64 20 28 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c d (when all fiel
1bf27 64 73 20 70 72 65 73 65 6e 74 29 2c 20 74 68 65 ds present), the
1bf28 6e 20 77 65 20 6d 75 73 74 20 62 65 20 64 65 61 n we must be dea
1bf29 6c 69 6e 67 20 0a 20 20 20 20 2a 2a 20 77 69 74 ling . ** wit
1bf2a 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 h a corrupt data
1bf2b 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 base.. */.
1bf2c 20 69 66 28 20 7a 49 64 78 3e 7a 45 6e 64 48 64 if( zIdx>zEndHd
1bf2d 72 20 7c 7c 20 6f 66 66 73 65 74 3e 70 61 79 6c r || offset>payl
1bf2e 6f 61 64 53 69 7a 65 20 0a 20 20 20 20 20 7c 7c oadSize . ||
1bf2f 20 28 7a 49 64 78 3d 3d 7a 45 6e 64 48 64 72 20 (zIdx==zEndHdr
1bf30 26 26 20 6f 66 66 73 65 74 21 3d 70 61 79 6c 6f && offset!=paylo
1bf31 61 64 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 20 adSize) ){.
1bf32 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
1bf33 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
1bf34 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f goto op_column_
1bf35 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a out;. }. }..
1bf36 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c /* Get the col
1bf37 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e umn information.
1bf38 20 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 If aOffset[p2]
1bf39 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1bf3a 6e 20 0a 20 20 2a 2a 20 64 65 73 65 72 69 61 6c n . ** deserial
1bf3b 69 7a 65 20 74 68 65 20 76 61 6c 75 65 20 66 72 ize the value fr
1bf3c 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 20 49 om the record. I
1bf3d 66 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 69 73 f aOffset[p2] is
1bf3e 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e zero,. ** then
1bf3f 20 74 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 there are not e
1bf40 6e 6f 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 nough fields in
1bf41 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 the record to sa
1bf42 74 69 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 tisfy the. ** r
1bf43 65 71 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 equest. In this
1bf44 20 63 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 case, set the v
1bf45 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 alue NULL or to
1bf46 50 34 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a P4 if P4 is. **
1bf47 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1bf48 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f Mem object.. */
1bf49 0a 20 20 69 66 28 20 61 4f 66 66 73 65 74 5b 70 . if( aOffset[p
1bf4a 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 2] ){. assert
1bf4b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1bf4c 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 65 63 20 );. if( zRec
1bf4d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1bf4e 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 VdbeMemReleaseEx
1bf4f 74 65 72 6e 61 6c 28 70 44 65 73 74 29 3b 0a 20 ternal(pDest);.
1bf50 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1bf51 53 65 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29 SerialGet((u8 *)
1bf52 26 7a 52 65 63 5b 61 4f 66 66 73 65 74 5b 70 32 &zRec[aOffset[p2
1bf53 5d 5d 2c 20 61 54 79 70 65 5b 70 32 5d 2c 20 70 ]], aType[p2], p
1bf54 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Dest);. }else
1bf55 7b 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 {. len = sq
1bf56 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1bf57 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 70 32 5d ypeLen(aType[p2]
1bf58 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1bf59 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 4d 65 VdbeMemMove(&sMe
1bf5a 6d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 20 m, pDest);.
1bf5b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1bf5c 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 eMemFromBtree(pC
1bf5d 72 73 72 2c 20 61 4f 66 66 73 65 74 5b 70 32 5d rsr, aOffset[p2]
1bf5e 2c 20 6c 65 6e 2c 20 70 43 2d 3e 69 73 49 6e 64 , len, pC->isInd
1bf5f 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 20 ex, &sMem);.
1bf60 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1bf61 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
1bf62 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 oto op_column_ou
1bf63 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
1bf64 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b zData = sMem.z;
1bf65 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1bf66 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a beSerialGet((u8*
1bf67 29 7a 44 61 74 61 2c 20 61 54 79 70 65 5b 70 32 )zData, aType[p2
1bf68 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 7d ], pDest);. }
1bf69 0a 20 20 20 20 70 44 65 73 74 2d 3e 65 6e 63 20 . pDest->enc
1bf6a 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 7d 65 = encoding;. }e
1bf6b 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 lse{. if( pOp
1bf6c 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d 45 4d ->p4type==P4_MEM
1bf6d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1bf6e 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 3VdbeMemShallowC
1bf6f 6f 70 79 28 70 44 65 73 74 2c 20 70 4f 70 2d 3e opy(pDest, pOp->
1bf70 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 p4.pMem, MEM_Sta
1bf71 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b tic);. }else{
1bf72 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1bf73 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f Dest->flags&MEM_
1bf74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 20 Null );. }.
1bf75 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 64 79 }.. /* If we dy
1bf76 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
1bf77 74 65 64 20 73 70 61 63 65 20 74 6f 20 68 6f 6c ted space to hol
1bf78 64 20 74 68 65 20 64 61 74 61 20 28 69 6e 20 74 d the data (in t
1bf79 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 he. ** sqlite3V
1bf7a 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1bf7b 29 20 63 61 6c 6c 20 61 62 6f 76 65 29 20 74 68 ) call above) th
1bf7c 65 6e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e 74 en transfer cont
1bf7d 72 6f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a 2a rol of that. **
1bf7e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c dynamically all
1bf7f 6f 63 61 74 65 64 20 73 70 61 63 65 20 6f 76 65 ocated space ove
1bf80 72 20 74 6f 20 74 68 65 20 70 44 65 73 74 20 73 r to the pDest s
1bf81 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 tructure.. ** T
1bf82 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 6d his prevents a m
1bf83 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a 2f emory copy.. */
1bf84 0a 20 20 69 66 28 20 73 4d 65 6d 2e 7a 4d 61 6c . if( sMem.zMal
1bf85 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 loc ){. asser
1bf86 74 28 20 73 4d 65 6d 2e 7a 3d 3d 73 4d 65 6d 2e t( sMem.z==sMem.
1bf87 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61 zMalloc );. a
1bf88 73 73 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e ssert( !(pDest->
1bf89 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 flags & MEM_Dyn)
1bf8a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1bf8b 21 28 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 !(pDest->flags &
1bf8c 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 (MEM_Blob|MEM_S
1bf8d 74 72 29 29 20 7c 7c 20 70 44 65 73 74 2d 3e 7a tr)) || pDest->z
1bf8e 3d 3d 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 ==sMem.z );.
1bf8f 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d 20 pDest->flags &=
1bf90 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f ~(MEM_Ephem|MEM_
1bf91 53 74 61 74 69 63 29 3b 0a 20 20 20 20 70 44 65 Static);. pDe
1bf92 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d st->flags |= MEM
1bf93 5f 54 65 72 6d 3b 0a 20 20 20 20 70 44 65 73 74 _Term;. pDest
1bf94 2d 3e 7a 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20 ->z = sMem.z;.
1bf95 20 20 70 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63 pDest->zMalloc
1bf96 20 3d 20 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 3b = sMem.zMalloc;
1bf97 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c . }.. rc = sql
1bf98 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 ite3VdbeMemMakeW
1bf99 72 69 74 65 61 62 6c 65 28 70 44 65 73 74 29 3b riteable(pDest);
1bf9a 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a ..op_column_out:
1bf9b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
1bf9c 4f 42 53 49 5a 45 28 70 44 65 73 74 29 3b 0a 20 OBSIZE(pDest);.
1bf9d 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1bf9e 70 4f 70 2d 3e 70 33 2c 20 70 44 65 73 74 29 3b pOp->p3, pDest);
1bf9f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1bfa0 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74 79 Opcode: Affinity
1bfa1 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a P1 P2 * P4 *.**
1bfa2 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e 69 .** Apply affini
1bfa3 74 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65 20 ties to a range
1bfa4 6f 66 20 50 32 20 72 65 67 69 73 74 65 72 73 20 of P2 registers
1bfa5 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 50 31 starting with P1
1bfa6 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 ..**.** P4 is a
1bfa7 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50 string that is P
1bfa8 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 2 characters lon
1bfa9 67 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72 61 g. The nth chara
1bfaa 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 cter of the.** s
1bfab 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20 tring indicates
1bfac 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e the column affin
1bfad 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ity that should
1bfae 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 be used for the
1bfaf 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 nth.** memory ce
1bfb0 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e ll in the range.
1bfb1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66 69 .*/.case OP_Affi
1bfb2 6e 69 74 79 3a 20 7b 0a 20 20 63 68 61 72 20 2a nity: {. char *
1bfb3 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d zAffinity = pOp-
1bfb4 3e 70 34 2e 7a 3b 0a 20 20 4d 65 6d 20 2a 70 44 >p4.z;. Mem *pD
1bfb5 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b ata0 = &p->aMem[
1bfb6 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d 65 6d 20 pOp->p1];. Mem
1bfb7 2a 70 4c 61 73 74 20 3d 20 26 70 44 61 74 61 30 *pLast = &pData0
1bfb8 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a 20 20 4d [pOp->p2-1];. M
1bfb9 65 6d 20 2a 70 52 65 63 3b 0a 0a 20 20 66 6f 72 em *pRec;.. for
1bfba 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 (pRec=pData0; pR
1bfbb 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b ec<=pLast; pRec+
1bfbc 2b 29 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c +){. ExpandBl
1bfbd 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 61 70 ob(pRec);. ap
1bfbe 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 63 plyAffinity(pRec
1bfbf 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63 , zAffinity[pRec
1bfc0 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 -pData0], encodi
1bfc1 6e 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b ng);. }. break
1bfc2 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1bfc3 4d 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32 MakeRecord P1 P2
1bfc4 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 P3 P4 *.**.** C
1bfc5 6f 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74 onvert P2 regist
1bfc6 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 ers beginning wi
1bfc7 74 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e th P1 into a sin
1bfc8 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69 gle entry.** sui
1bfc9 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 table for use as
1bfca 20 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69 a data record i
1bfcb 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 n a database tab
1bfcc 6c 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a le or as a key.*
1bfcd 2a 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 * in an index.
1bfce 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 The details of t
1bfcf 68 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72 he format are ir
1bfd0 72 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67 relevant as long
1bfd1 20 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f as.** the OP_Co
1bfd2 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 lumn opcode can
1bfd3 64 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 decode the recor
1bfd4 64 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65 d later..** Refe
1bfd5 72 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65 r to source code
1bfd6 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68 comments for th
1bfd7 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 e details of the
1bfd8 20 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 record.** forma
1bfd9 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 t..**.** P4 may
1bfda 62 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 be a string that
1bfdb 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 is P2 character
1bfdc 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 s long. The nth
1bfdd 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 character of th
1bfde 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 e.** string indi
1bfdf 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e cates the column
1bfe0 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 affinity that s
1bfe1 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f hould be used fo
1bfe2 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 r the nth.** fie
1bfe3 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 ld of the index
1bfe4 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d key..**.** The m
1bfe5 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72 apping from char
1bfe6 61 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74 acter to affinit
1bfe7 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 y is given by th
1bfe8 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a e SQLITE_AFF_.**
1bfe9 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 macros defined
1bfea 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a in sqliteInt.h..
1bfeb 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e **.** If P4 is N
1bfec 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64 ULL then all ind
1bfed 65 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74 ex fields have t
1bfee 68 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45 he affinity NONE
1bfef 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b ..*/.case OP_Mak
1bff0 65 52 65 63 6f 72 64 3a 20 7b 0a 20 20 2f 2a 20 eRecord: {. /*
1bff1 41 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65 63 Assuming the rec
1bff2 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66 ord contains N f
1bff3 69 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f 72 ields, the recor
1bff4 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 d format looks.
1bff5 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 ** like this:.
1bff6 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d **. ** -------
1bff7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1bff8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1bff9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1bffa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1bffb 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a -. ** | hdr-siz
1bffc 65 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79 70 e | type 0 | typ
1bffd 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 e 1 | ... | type
1bffe 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20 2e N-1 | data0 | .
1bfff 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c 20 .. | data N-1 |
1c000 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d . ** ----------
1c001 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c002 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c003 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1c004 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 --------------.
1c005 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29 **. ** Data(0)
1c006 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 is taken from r
1c007 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74 egister P1. Dat
1c008 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20 a(1) comes from
1c009 72 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20 register P1+1.
1c00a 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e ** and so froth.
1c00b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 . **. ** Each
1c00c 74 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20 type field is a
1c00d 76 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74 varint represent
1c00e 69 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74 ing the serial t
1c00f 79 70 65 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a ype of the . **
1c010 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 corresponding d
1c011 61 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65 ata element (see
1c012 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1c013 61 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20 alType()). The.
1c014 20 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65 ** hdr-size fie
1c015 6c 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72 ld is also a var
1c016 69 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 int which is the
1c017 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 offset from the
1c018 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 beginning. **
1c019 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f of the record to
1c01a 20 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75 data0.. */. u
1c01b 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b 20 20 8 *zNewRecord;
1c01c 20 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66 65 /* A buffe
1c01d 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 r to hold the da
1c01e 74 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 ta for the new r
1c01f 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a ecord */. Mem *
1c020 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 pRec;
1c021 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63 /* The new rec
1c022 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e 44 61 ord */. u64 nDa
1c023 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ta = 0;
1c024 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1c025 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 es of data space
1c026 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 20 3d */. int nHdr =
1c027 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0; /*
1c028 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1c029 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 of header space
1c02a 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 20 3d */. i64 nByte =
1c02b 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 0; /* D
1c02c 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72 ata space requir
1c02d 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f ed for this reco
1c02e 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 rd */. int nZer
1c02f 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f o = 0; /
1c030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f * Number of zero
1c031 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e bytes at the en
1c032 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 d of the record
1c033 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 */. int nVarint
1c034 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1c035 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1c036 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 n a varint */.
1c037 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b u32 serial_type;
1c038 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 66 /* Type f
1c039 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 ield */. Mem *p
1c03a 44 61 74 61 30 3b 20 20 20 20 20 20 20 20 20 20 Data0;
1c03b 20 2f 2a 20 46 69 72 73 74 20 66 69 65 6c 64 20 /* First field
1c03c 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 20 69 to be combined i
1c03d 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 2a nto the record *
1c03e 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 /. Mem *pLast;
1c03f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
1c040 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 st field of the
1c041 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 record */. int
1c042 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 nField;
1c043 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c044 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 fields in the re
1c045 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a cord */. char *
1c046 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 zAffinity;
1c047 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
1c048 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 string for the
1c049 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 record */. int
1c04a 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 file_format;
1c04b 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61 /* File forma
1c04c 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e 63 t to use for enc
1c04d 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 oding */. int i
1c04e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1c04f 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20 /* Space used
1c050 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20 in zNewRecord[]
1c051 2a 2f 0a 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 */.. nField = p
1c052 4f 70 2d 3e 70 31 3b 0a 20 20 7a 41 66 66 69 6e Op->p1;. zAffin
1c053 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b ity = pOp->p4.z;
1c054 0a 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c . assert( nFiel
1c055 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 d>0 && pOp->p2>0
1c056 20 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 46 69 65 && pOp->p2+nFie
1c057 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b ld<=p->nMem+1 );
1c058 0a 20 20 70 44 61 74 61 30 20 3d 20 26 70 2d 3e . pData0 = &p->
1c059 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 aMem[nField];.
1c05a 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32 nField = pOp->p2
1c05b 3b 0a 20 20 70 4c 61 73 74 20 3d 20 26 70 44 61 ;. pLast = &pDa
1c05c 74 61 30 5b 6e 46 69 65 6c 64 2d 31 5d 3b 0a 20 ta0[nField-1];.
1c05d 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 file_format = p
1c05e 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f ->minWriteFileFo
1c05f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 rmat;.. /* Loop
1c060 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c 65 through the ele
1c061 6d 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c 20 ments that will
1c062 6d 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 6f make up the reco
1c063 72 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 2a rd to figure. *
1c064 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 * out how much s
1c065 70 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 pace is required
1c066 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 for the new rec
1c067 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 ord.. */. for(
1c068 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65 pRec=pData0; pRe
1c069 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b c<=pLast; pRec++
1c06a 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a ){. int len;.
1c06b 20 20 20 20 69 66 28 20 7a 41 66 66 69 6e 69 74 if( zAffinit
1c06c 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 y ){. apply
1c06d 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c 20 7a Affinity(pRec, z
1c06e 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d 70 44 Affinity[pRec-pD
1c06f 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 ata0], encoding)
1c070 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1c071 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pRec->flags&MEM_
1c072 5a 65 72 6f 20 26 26 20 70 52 65 63 2d 3e 6e 3e Zero && pRec->n>
1c073 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1c074 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
1c075 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 7d lob(pRec);. }
1c076 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 79 70 65 . serial_type
1c077 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1c078 72 69 61 6c 54 79 70 65 28 70 52 65 63 2c 20 66 rialType(pRec, f
1c079 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 ile_format);.
1c07a 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 len = sqlite3Vd
1c07b 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1c07c 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 serial_type);.
1c07d 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65 6e 3b 0a nData += len;.
1c07e 20 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69 nHdr += sqli
1c07f 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65 72 te3VarintLen(ser
1c080 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 ial_type);. i
1c081 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 f( pRec->flags &
1c082 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 MEM_Zero ){.
1c083 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 /* Only pure
1c084 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 zero-filled BLOB
1c085 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74 s can be input t
1c086 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 o this Opcode..
1c087 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f ** We do no
1c088 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 t allow blobs wi
1c089 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64 20 th a prefix and
1c08a 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 a zero-filled ta
1c08b 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 5a 65 il. */. nZe
1c08c 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 2e 6e 5a ro += pRec->u.nZ
1c08d 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 ero;. }else i
1c08e 66 28 20 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 f( len ){.
1c08f 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d nZero = 0;. }
1c090 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 . }.. /* Add t
1c091 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 65 he initial heade
1c092 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 r varint and tot
1c093 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 al the size */.
1c094 20 6e 48 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74 nHdr += nVarint
1c095 20 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 = sqlite3Varint
1c096 4c 65 6e 28 6e 48 64 72 29 3b 0a 20 20 69 66 28 Len(nHdr);. if(
1c097 20 6e 56 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 nVarint<sqlite3
1c098 56 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20 VarintLen(nHdr)
1c099 29 7b 0a 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20 ){. nHdr++;.
1c09a 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 6e 48 64 }. nByte = nHd
1c09b 72 2b 6e 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20 r+nData-nZero;.
1c09c 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 if( nByte>db->a
1c09d 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1c09e 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1c09f 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1c0a0 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
1c0a1 75 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72 ure the output r
1c0a2 65 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75 egister has a bu
1c0a3 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 ffer large enoug
1c0a4 68 20 74 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a h to store . **
1c0a5 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e the new record.
1c0a6 20 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 The output regi
1c0a7 73 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 ster (pOp->p3) i
1c0a8 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f s not allowed to
1c0a9 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 . ** be one of
1c0aa 74 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 the input regist
1c0ab 65 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 ers (because the
1c0ac 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 following call
1c0ad 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 to. ** sqlite3V
1c0ae 64 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 dbeMemGrow() cou
1c0af 6c 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76 ld clobber the v
1c0b0 61 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69 alue before it i
1c0b1 73 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 s used).. */.
1c0b2 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c assert( pOp->p3<
1c0b3 70 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e pOp->p1 || pOp->
1c0b4 70 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d p3>=pOp->p1+pOp-
1c0b5 3e 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 >p2 );. pOut =
1c0b6 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1c0b7 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ];. if( sqlite3
1c0b8 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 VdbeMemGrow(pOut
1c0b9 2c 20 28 69 6e 74 29 6e 42 79 74 65 2c 20 30 29 , (int)nByte, 0)
1c0ba 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f ){. goto no_
1c0bb 6d 65 6d 3b 0a 20 20 7d 0a 20 20 7a 4e 65 77 52 mem;. }. zNewR
1c0bc 65 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 4f ecord = (u8 *)pO
1c0bd 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 ut->z;.. /* Wri
1c0be 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f te the record */
1c0bf 0a 20 20 69 20 3d 20 70 75 74 56 61 72 69 6e 74 . i = putVarint
1c0c0 33 32 28 7a 4e 65 77 52 65 63 6f 72 64 2c 20 6e 32(zNewRecord, n
1c0c1 48 64 72 29 3b 0a 20 20 66 6f 72 28 70 52 65 63 Hdr);. for(pRec
1c0c2 3d 70 44 61 74 61 30 3b 20 70 52 65 63 3c 3d 70 =pData0; pRec<=p
1c0c3 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29 7b 0a 20 Last; pRec++){.
1c0c4 20 20 20 73 65 72 69 61 6c 5f 74 79 70 65 20 3d serial_type =
1c0c5 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1c0c6 61 6c 54 79 70 65 28 70 52 65 63 2c 20 66 69 6c alType(pRec, fil
1c0c7 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 69 e_format);. i
1c0c8 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28 += putVarint32(
1c0c9 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20 &zNewRecord[i],
1c0ca 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 20 20 20 serial_type);
1c0cb 20 20 20 2f 2a 20 73 65 72 69 61 6c 20 74 79 70 /* serial typ
1c0cc 65 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 70 e */. }. for(p
1c0cd 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65 63 Rec=pData0; pRec
1c0ce 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29 <=pLast; pRec++)
1c0cf 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 { /* serial dat
1c0d0 61 20 2a 2f 0a 20 20 20 20 69 20 2b 3d 20 73 71 a */. i += sq
1c0d1 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 lite3VdbeSerialP
1c0d2 75 74 28 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 ut(&zNewRecord[i
1c0d3 5d 2c 20 28 69 6e 74 29 28 6e 42 79 74 65 2d 69 ], (int)(nByte-i
1c0d4 29 2c 20 70 52 65 63 2c 66 69 6c 65 5f 66 6f 72 ), pRec,file_for
1c0d5 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 mat);. }. asse
1c0d6 72 74 28 20 69 3d 3d 6e 42 79 74 65 20 29 3b 0a rt( i==nByte );.
1c0d7 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1c0d8 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1c0d9 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f =p->nMem );. pO
1c0da 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 6e 42 79 ut->n = (int)nBy
1c0db 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 te;. pOut->flag
1c0dc 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d s = MEM_Blob | M
1c0dd 45 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e EM_Dyn;. pOut->
1c0de 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 xDel = 0;. if(
1c0df 6e 5a 65 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 nZero ){. pOu
1c0e0 74 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 5a 65 t->u.nZero = nZe
1c0e1 72 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c ro;. pOut->fl
1c0e2 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b ags |= MEM_Zero;
1c0e3 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 . }. pOut->enc
1c0e4 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 = SQLITE_UTF8;
1c0e5 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 /* In case the
1c0e6 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e blob is ever con
1c0e7 76 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a verted to text *
1c0e8 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 /. REGISTER_TRA
1c0e9 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 CE(pOp->p3, pOut
1c0ea 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f );. UPDATE_MAX_
1c0eb 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1c0ec 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c0ed 70 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 20 pcode: Count P1
1c0ee 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 P2 * * *.**.** S
1c0ef 74 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 tore the number
1c0f0 6f 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 69 of entries (an i
1c0f1 6e 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 6e nteger value) in
1c0f2 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1c0f3 64 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 dex .** opened b
1c0f4 79 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 72 y cursor P1 in r
1c0f5 65 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 69 egister P2.*/.#i
1c0f6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1c0f7 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 73 T_BTREECOUNT.cas
1c0f8 65 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 e OP_Count: {
1c0f9 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1c0fa 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 36 erelease */. i6
1c0fb 34 20 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43 75 4 nEntry;. BtCu
1c0fc 72 73 6f 72 20 2a 70 43 72 73 72 20 3d 20 70 2d rsor *pCrsr = p-
1c0fd 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 2d >apCsr[pOp->p1]-
1c0fe 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 >pCursor;. if(
1c0ff 70 43 72 73 72 20 29 7b 0a 20 20 20 20 72 63 20 pCrsr ){. rc
1c100 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
1c101 75 6e 74 28 70 43 72 73 72 2c 20 26 6e 45 6e 74 unt(pCrsr, &nEnt
1c102 72 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ry);. }else{.
1c103 20 20 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 nEntry = 0;.
1c104 7d 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 }. pOut->flags
1c105 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 = MEM_Int;. pOu
1c106 74 2d 3e 75 2e 69 20 3d 20 6e 45 6e 74 72 79 3b t->u.i = nEntry;
1c107 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1c108 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 if../* Opcode: S
1c109 74 61 74 65 6d 65 6e 74 20 50 31 20 2a 20 2a 20 tatement P1 * *
1c10a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 * *.**.** Begin
1c10b 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 73 74 an individual st
1c10c 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1c10d 69 6f 6e 20 77 68 69 63 68 20 69 73 20 70 61 72 ion which is par
1c10e 74 20 6f 66 20 61 20 6c 61 72 67 65 72 0a 2a 2a t of a larger.**
1c10f 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 transaction. T
1c110 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f his is needed so
1c111 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d that the statem
1c112 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f ent.** can be ro
1c113 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 20 lled back after
1c114 61 6e 20 65 72 72 6f 72 20 77 69 74 68 6f 75 74 an error without
1c115 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 having to roll
1c116 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65 6e 74 69 back the.** enti
1c117 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 re transaction.
1c118 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 The statement t
1c119 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 ransaction will
1c11a 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a automatically.**
1c11b 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65 commit when the
1c11c 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a VDBE halts..**.
1c11d 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 ** If the databa
1c11e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
1c11f 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 75 currently in au
1c120 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 28 74 tocommit mode (t
1c121 68 61 74 20 0a 2a 2a 20 69 73 20 74 6f 20 73 61 hat .** is to sa
1c122 79 2c 20 69 66 20 69 74 20 69 73 20 69 6e 20 62 y, if it is in b
1c123 65 74 77 65 65 6e 20 42 45 47 49 4e 20 61 6e 64 etween BEGIN and
1c124 20 43 4f 4d 4d 49 54 29 0a 2a 2a 20 61 6e 64 20 COMMIT).** and
1c125 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 if there are no
1c126 6f 74 68 65 72 20 61 63 74 69 76 65 20 73 74 61 other active sta
1c127 74 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 73 tements on the s
1c128 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ame database.**
1c129 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 6e connection, then
1c12a 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
1c12b 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 4e 6f 20 is a no-op. No
1c12c 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1c12d 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 65 65 64 ction.** is need
1c12e 65 64 20 73 69 6e 63 65 20 61 6e 79 20 65 72 72 ed since any err
1c12f 6f 72 20 63 61 6e 20 75 73 65 20 74 68 65 20 6e or can use the n
1c130 6f 72 6d 61 6c 20 52 4f 4c 4c 42 41 43 4b 20 70 ormal ROLLBACK p
1c131 72 6f 63 65 73 73 20 74 6f 0a 2a 2a 20 75 6e 64 rocess to.** und
1c132 6f 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a o changes..**.**
1c133 20 49 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 If a statement
1c134 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
1c135 74 61 72 74 65 64 2c 20 74 68 65 6e 20 61 20 73 tarted, then a s
1c136 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
1c137 20 66 69 6c 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 file.** will be
1c138 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 allocated and i
1c139 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a nitialized..**.*
1c13a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 * The statement
1c13b 69 73 20 62 65 67 75 6e 20 6f 6e 20 74 68 65 20 is begun on the
1c13c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 database file wi
1c13d 74 68 20 69 6e 64 65 78 20 50 31 2e 20 20 54 68 th index P1. Th
1c13e 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 e main.** databa
1c13f 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 69 se file has an i
1c140 6e 64 65 78 20 6f 66 20 30 20 61 6e 64 20 74 68 ndex of 0 and th
1c141 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 e file used for
1c142 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1c143 0a 2a 2a 20 68 61 73 20 61 6e 20 69 6e 64 65 78 .** has an index
1c144 20 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f of 1..*/.case O
1c145 50 5f 53 74 61 74 65 6d 65 6e 74 3a 20 7b 0a 20 P_Statement: {.
1c146 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d if( db->autoCom
1c147 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 63 mit==0 || db->ac
1c148 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b tiveVdbeCnt>1 ){
1c149 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 . int i = pOp
1c14a 2d 3e 70 31 3b 0a 20 20 20 20 42 74 72 65 65 20 ->p1;. Btree
1c14b 2a 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 *pBt;. assert
1c14c 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e ( i>=0 && i<db->
1c14d 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 nDb );. asser
1c14e 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 t( db->aDb[i].pB
1c14f 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 20 t!=0 );. pBt
1c150 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1c151 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 ;. assert( sq
1c152 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 lite3BtreeIsInTr
1c153 61 6e 73 28 70 42 74 29 20 29 3b 0a 20 20 20 20 ans(pBt) );.
1c154 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
1c155 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 21 eMask & (1<<i))!
1c156 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d =0 );. if( p-
1c157 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 >iStatement==0 )
1c158 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1c159 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 3d db->nStatement>=
1c15a 30 20 26 26 20 64 62 2d 3e 6e 53 61 76 65 70 6f 0 && db->nSavepo
1c15b 69 6e 74 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 int>=0 );.
1c15c 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 2b db->nStatement++
1c15d 3b 20 0a 20 20 20 20 20 20 70 2d 3e 69 53 74 61 ; . p->iSta
1c15e 74 65 6d 65 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 tement = db->nSa
1c15f 76 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 vepoint + db->nS
1c160 74 61 74 65 6d 65 6e 74 3b 0a 20 20 20 20 7d 0a tatement;. }.
1c161 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1c162 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 70 BtreeBeginStmt(p
1c163 42 74 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e Bt, p->iStatemen
1c164 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b t);. }. break;
1c165 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1c166 61 76 65 70 6f 69 6e 74 20 50 31 20 2a 20 2a 20 avepoint P1 * *
1c167 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 2c P4 *.**.** Open,
1c168 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c release or roll
1c169 62 61 63 6b 20 74 68 65 20 73 61 76 65 70 6f 69 back the savepoi
1c16a 6e 74 20 6e 61 6d 65 64 20 62 79 20 70 61 72 61 nt named by para
1c16b 6d 65 74 65 72 20 50 34 2c 20 64 65 70 65 6e 64 meter P4, depend
1c16c 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 76 61 ing.** on the va
1c16d 6c 75 65 20 6f 66 20 50 31 2e 20 54 6f 20 6f 70 lue of P1. To op
1c16e 65 6e 20 61 20 6e 65 77 20 73 61 76 65 70 6f 69 en a new savepoi
1c16f 6e 74 2c 20 50 31 3d 3d 30 2e 20 54 6f 20 72 65 nt, P1==0. To re
1c170 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 lease (commit) a
1c171 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 73 61 n.** existing sa
1c172 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 31 2c 20 vepoint, P1==1,
1c173 6f 72 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 or to rollback a
1c174 6e 20 65 78 69 73 74 69 6e 67 20 73 61 76 65 70 n existing savep
1c175 6f 69 6e 74 20 50 31 3d 3d 32 2e 0a 2a 2f 0a 63 oint P1==2..*/.c
1c176 61 73 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 ase OP_Savepoint
1c177 3a 20 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 : {. int p1 = p
1c178 4f 70 2d 3e 70 31 3b 0a 20 20 63 68 61 72 20 2a Op->p1;. char *
1c179 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 2e zName = pOp->p4.
1c17a 7a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 z; /* Na
1c17b 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 me of savepoint
1c17c 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 */.. /* Assert
1c17d 74 68 61 74 20 74 68 65 20 70 31 20 70 61 72 61 that the p1 para
1c17e 6d 65 74 65 72 20 69 73 20 76 61 6c 69 64 2e 20 meter is valid.
1c17f 41 6c 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 Also that if the
1c180 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 0a 20 20 re is no open.
1c181 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 ** transaction,
1c182 74 68 65 6e 20 74 68 65 72 65 20 63 61 6e 6e 6f then there canno
1c183 74 20 62 65 20 61 6e 79 20 73 61 76 65 70 6f 69 t be any savepoi
1c184 6e 74 73 2e 20 0a 20 20 2a 2f 0a 20 20 61 73 73 nts. . */. ass
1c185 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65 70 6f ert( db->pSavepo
1c186 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75 int==0 || db->au
1c187 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 toCommit==0 );.
1c188 20 61 73 73 65 72 74 28 20 70 31 3d 3d 53 41 56 assert( p1==SAV
1c189 45 50 4f 49 4e 54 5f 42 45 47 49 4e 7c 7c 70 31 EPOINT_BEGIN||p1
1c18a 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 ==SAVEPOINT_RELE
1c18b 41 53 45 7c 7c 70 31 3d 3d 53 41 56 45 50 4f 49 ASE||p1==SAVEPOI
1c18c 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 NT_ROLLBACK );.
1c18d 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 assert( db->pSa
1c18e 76 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e 69 vepoint || db->i
1c18f 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 sTransactionSave
1c190 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 point==0 );. as
1c191 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 sert( checkSavep
1c192 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b ointCount(db) );
1c193 0a 0a 20 20 69 66 28 20 70 31 3d 3d 53 41 56 45 .. if( p1==SAVE
1c194 50 4f 49 4e 54 5f 42 45 47 49 4e 20 29 7b 0a 20 POINT_BEGIN ){.
1c195 20 20 20 69 66 28 20 64 62 2d 3e 77 72 69 74 65 if( db->write
1c196 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20 VdbeCnt>0 ){.
1c197 20 20 20 2f 2a 20 41 20 6e 65 77 20 73 61 76 65 /* A new save
1c198 70 6f 69 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 point cannot be
1c199 63 72 65 61 74 65 64 20 69 66 20 74 68 65 72 65 created if there
1c19a 20 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74 are active writ
1c19b 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 e . ** stat
1c19c 65 6d 65 6e 74 73 20 28 69 2e 65 2e 20 6f 70 65 ements (i.e. ope
1c19d 6e 20 72 65 61 64 2f 77 72 69 74 65 20 69 6e 63 n read/write inc
1c19e 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 68 61 remental blob ha
1c19f 6e 64 6c 65 73 29 2e 0a 20 20 20 20 20 20 2a 2f ndles).. */
1c1a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 . sqlite3Se
1c1a1 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1c1a2 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 Msg, db, "cannot
1c1a3 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 open savepoint
1c1a4 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c - ". "SQL
1c1a5 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 statements in p
1c1a6 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20 20 20 rogress");.
1c1a7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1c1a8 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 Y;. }else{.
1c1a9 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 int nName =
1c1aa 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1c1ab 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 53 61 zName);. Sa
1c1ac 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 0a vepoint *pNew;..
1c1ad 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 /* Create
1c1ae 61 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 a new savepoint
1c1af 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 structure. */.
1c1b0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
1c1b1 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
1c1b2 2c 20 73 69 7a 65 6f 66 28 53 61 76 65 70 6f 69 , sizeof(Savepoi
1c1b3 6e 74 29 2b 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 nt)+nName+1);.
1c1b4 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a if( pNew ){.
1c1b5 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 7a 4e pNew->zN
1c1b6 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 ame = (char *)&p
1c1b7 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 New[1];.
1c1b8 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 4e 61 memcpy(pNew->zNa
1c1b9 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 me, zName, nName
1c1ba 2b 31 29 3b 0a 20 20 20 20 0a 20 20 20 20 20 20 +1);. .
1c1bb 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 /* If there is
1c1bc 20 6e 6f 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 no open transac
1c1bd 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 61 72 6b 20 tion, then mark
1c1be 74 68 69 73 20 61 73 20 61 20 73 70 65 63 69 61 this as a specia
1c1bf 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 74 72 l. ** "tr
1c1c0 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f ansaction savepo
1c1c1 69 6e 74 22 2e 20 2a 2f 0a 20 20 20 20 20 20 20 int". */.
1c1c2 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d if( db->autoCom
1c1c3 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 mit ){.
1c1c4 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1c1c5 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 = 0;. d
1c1c6 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e b->isTransaction
1c1c7 53 61 76 65 70 6f 69 6e 74 20 3d 20 31 3b 0a 20 Savepoint = 1;.
1c1c8 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1c1c9 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 db->nSav
1c1ca 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 epoint++;.
1c1cb 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 20 }. .
1c1cc 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77 /* Link the new
1c1cd 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 6f 20 savepoint into
1c1ce 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e the database han
1c1cf 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f 0a 20 dle's list. */.
1c1d0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 pNew->pNe
1c1d1 78 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f xt = db->pSavepo
1c1d2 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d int;. db-
1c1d3 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 4e >pSavepoint = pN
1c1d4 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ew;. }.
1c1d5 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 53 }. }else{. S
1c1d6 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 avepoint *pSavep
1c1d7 6f 69 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 53 oint;. int iS
1c1d8 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 avepoint = 0;..
1c1d9 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e /* Find the n
1c1da 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 amed savepoint.
1c1db 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 If there is no s
1c1dc 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 uch savepoint, t
1c1dd 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e hen an. ** an
1c1de 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
1c1df 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
1c1e0 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 */. for(.
1c1e1 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 64 62 pSavepoint=db
1c1e2 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 0a 20 ->pSavepoint; .
1c1e3 20 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 20 pSavepoint
1c1e4 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
1c1e5 70 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e p(pSavepoint->zN
1c1e6 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 ame, zName);.
1c1e7 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 70 53 pSavepoint=pS
1c1e8 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a avepoint->pNext.
1c1e9 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 53 61 ){. iSa
1c1ea 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d vepoint++;. }
1c1eb 0a 20 20 20 20 69 66 28 20 21 70 53 61 76 65 70 . if( !pSavep
1c1ec 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 oint ){. sq
1c1ed 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1c1ee 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1c1ef 22 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f 69 "no such savepoi
1c1f0 6e 74 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b nt: %s", zName);
1c1f1 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1c1f2 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 TE_ERROR;. }e
1c1f3 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20 20 20 lse if( .
1c1f4 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
1c1f5 74 3e 30 20 7c 7c 20 28 70 31 3d 3d 53 41 56 45 t>0 || (p1==SAVE
1c1f6 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 POINT_ROLLBACK &
1c1f7 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 & db->activeVdbe
1c1f8 43 6e 74 3e 31 29 20 0a 20 20 20 20 29 7b 0a 20 Cnt>1) . ){.
1c1f9 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f /* It is no
1c1fa 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 t possible to re
1c1fb 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 lease (commit) a
1c1fc 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68 savepoint if th
1c1fd 65 72 65 20 61 72 65 20 0a 20 20 20 20 20 20 2a ere are . *
1c1fe 2a 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73 * active write s
1c1ff 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 tatements. It is
1c200 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
1c201 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 rollback a save
1c202 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 point. ** i
1c203 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 f there are any
1c204 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 active statement
1c205 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 s at all..
1c206 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1c207 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1c208 72 72 4d 73 67 2c 20 64 62 2c 20 0a 20 20 20 20 rrMsg, db, .
1c209 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73 "cannot %s s
1c20a 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 avepoint - SQL s
1c20b 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f tatements in pro
1c20c 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20 gress",.
1c20d 28 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 (p1==SAVEPOINT_R
1c20e 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62 OLLBACK ? "rollb
1c20f 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29 ack": "release")
1c210 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 . );.
1c211 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
1c212 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 ;. }else{..
1c213 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 /* Determine
1c214 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1c215 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61 this is a transa
1c216 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e ction savepoint.
1c217 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a If so,. **
1c218 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52 and this is a R
1c219 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20 ELEASE command,
1c21a 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 then the current
1c21b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 transaction .
1c21c 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 ** is commit
1c21d 74 65 64 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 ted. . */.
1c21e 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 int isTrans
1c21f 61 63 74 69 6f 6e 20 3d 20 70 53 61 76 65 70 6f action = pSavepo
1c220 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 int->pNext==0 &&
1c221 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 db->isTransacti
1c222 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 onSavepoint;.
1c223 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61 63 if( isTransac
1c224 74 69 6f 6e 20 26 26 20 70 31 3d 3d 53 41 56 45 tion && p1==SAVE
1c225 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b POINT_RELEASE ){
1c226 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 . db->aut
1c227 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 oCommit = 1;.
1c228 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1c229 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c VdbeHalt(p)==SQL
1c22a 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
1c22b 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
1c22c 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e ;. db->
1c22d 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a autoCommit = 0;.
1c22e 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 p->rc
1c22f 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 = rc = SQLITE_BU
1c230 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f SY;. go
1c231 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1c232 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1c233 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 db->isTransact
1c234 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 ionSavepoint = 0
1c235 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 ;. rc = p
1c236 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ->rc;. }els
1c237 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 e{. int i
1c238 69 3b 0a 20 20 20 20 20 20 20 20 69 53 61 76 65 i;. iSave
1c239 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 point = db->nSav
1c23a 65 70 6f 69 6e 74 20 2d 20 69 53 61 76 65 70 6f epoint - iSavepo
1c23b 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 int - 1;.
1c23c 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 64 62 for(ii=0; ii<db
1c23d 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 ->nDb; ii++){.
1c23e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1c23f 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
1c240 6e 74 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 nt(db->aDb[ii].p
1c241 42 74 2c 20 70 31 2c 20 69 53 61 76 65 70 6f 69 Bt, p1, iSavepoi
1c242 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 nt);. i
1c243 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1c244 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1c245 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1c246 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1c247 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1c248 20 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53 if( p1==S
1c249 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1c24a 4b 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 26 K && (db->flags&
1c24b 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
1c24c 6e 67 65 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 nges)!=0 ){.
1c24d 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1c24e 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
1c24f 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20 ments(db);.
1c250 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 sqlite3Rese
1c251 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 tInternalSchema(
1c252 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 db, 0);.
1c253 7d 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
1c254 20 20 20 2f 2a 20 52 65 67 61 72 64 6c 65 73 73 /* Regardless
1c255 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 69 73 of whether this
1c256 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 6f 72 is a RELEASE or
1c257 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 73 74 72 ROLLBACK, destr
1c258 6f 79 20 61 6c 6c 20 0a 20 20 20 20 20 20 2a 2a oy all . **
1c259 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 73 74 savepoints nest
1c25a 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 ed inside of the
1c25b 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 savepoint being
1c25c 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 2a 2f operated on. */
1c25d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 64 62 . while( db
1c25e 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d 70 53 ->pSavepoint!=pS
1c25f 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 avepoint ){.
1c260 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 Savepoint *p
1c261 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 Tmp = db->pSavep
1c262 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 oint;. db
1c263 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 ->pSavepoint = p
1c264 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Tmp->pNext;.
1c265 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1c266 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 20 e(db, pTmp);.
1c267 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f db->nSavepo
1c268 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a int--;. }..
1c269 20 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 /* If it i
1c26a 73 20 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65 s a RELEASE, the
1c26b 6e 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 n destroy the sa
1c26c 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 vepoint being op
1c26d 65 72 61 74 65 64 20 6f 6e 20 74 6f 6f 20 2a 2f erated on too */
1c26e 0a 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53 . if( p1==S
1c26f 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
1c270 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1c271 72 74 28 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d rt( pSavepoint==
1c272 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 db->pSavepoint )
1c273 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 ;. db->pS
1c274 61 76 65 70 6f 69 6e 74 20 3d 20 70 53 61 76 65 avepoint = pSave
1c275 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 point->pNext;.
1c276 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1c277 72 65 65 28 64 62 2c 20 70 53 61 76 65 70 6f 69 ree(db, pSavepoi
1c278 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 nt);. if(
1c279 20 21 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 !isTransaction
1c27a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d ){. db-
1c27b 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 >nSavepoint--;.
1c27c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1c27d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 . }. }.. br
1c27e 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c27f 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 e: AutoCommit P1
1c280 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1c281 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 Set the database
1c282 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 auto-commit fla
1c283 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 g to P1 (1 or 0)
1c284 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c . If P2 is true,
1c285 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e roll.** back an
1c286 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 y currently acti
1c287 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 ve btree transac
1c288 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 tions. If there
1c289 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a are any active.*
1c28a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f * VMs (apart fro
1c28b 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 m this one), the
1c28c 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 n a ROLLBACK fai
1c28d 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 ls. A COMMIT fa
1c28e 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 ils if.** there
1c28f 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69 are active writi
1c290 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65 ng VMs or active
1c291 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68 VMs that use sh
1c292 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a ared cache..**.*
1c293 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
1c294 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d on causes the VM
1c295 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 to halt..*/.cas
1c296 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a e OP_AutoCommit:
1c297 20 7b 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 {. int desired
1c298 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70 AutoCommit = pOp
1c299 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c ->p1;. int roll
1c29a 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a back = pOp->p2;.
1c29b 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d int turnOnAC =
1c29c 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d desiredAutoComm
1c29d 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 it && !db->autoC
1c29e 6f 6d 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74 ommit;.. assert
1c29f 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d ( desiredAutoCom
1c2a0 6d 69 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65 mit==1 || desire
1c2a1 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 dAutoCommit==0 )
1c2a2 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69 ;. assert( desi
1c2a3 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 redAutoCommit==1
1c2a4 20 7c 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20 || rollback==0
1c2a5 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 );.. assert( db
1c2a6 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1c2a7 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 0 ); /* At leas
1c2a8 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 t this one VM is
1c2a9 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 active */.. if
1c2aa 28 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f ( turnOnAC && ro
1c2ab 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 llback && db->ac
1c2ac 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b tiveVdbeCnt>1 ){
1c2ad 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
1c2ae 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c instruction impl
1c2af 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 ements a ROLLBAC
1c2b0 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 K and other VMs
1c2b1 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c are. ** still
1c2b2 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 running, and a
1c2b3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
1c2b4 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e ctive, return an
1c2b5 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e error indicatin
1c2b6 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 g. ** that th
1c2b7 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 e other VMs must
1c2b8 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e complete first.
1c2b9 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c . */. sql
1c2ba 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 ite3SetString(&p
1c2bb 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 ->zErrMsg, db, "
1c2bc 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 cannot rollback
1c2bd 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a transaction - ".
1c2be 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 "SQL sta
1c2bf 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 tements in progr
1c2c0 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 ess");. rc =
1c2c1 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
1c2c2 65 6c 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41 else if( turnOnA
1c2c3 43 20 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26 C && !rollback &
1c2c4 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 & db->writeVdbeC
1c2c5 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 nt>1 ){. /* I
1c2c6 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 f this instructi
1c2c7 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 on implements a
1c2c8 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72 COMMIT and other
1c2c9 20 56 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67 VMs are writing
1c2ca 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 . ** return a
1c2cb 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 n error indicati
1c2cc 6e 67 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 ng that the othe
1c2cd 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c r VMs must compl
1c2ce 65 74 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20 ete first. .
1c2cf 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 */. sqlite3Se
1c2d0 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1c2d1 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 Msg, db, "cannot
1c2d2 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74 commit transact
1c2d3 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 ion - ".
1c2d4 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 "SQL statements
1c2d5 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 in progress");.
1c2d6 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
1c2d7 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 USY;. }else if(
1c2d8 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d desiredAutoComm
1c2d9 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d it!=db->autoComm
1c2da 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 72 6f it ){. if( ro
1c2db 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 llback ){.
1c2dc 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41 assert( desiredA
1c2dd 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a utoCommit==1 );.
1c2de 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c sqlite3Rol
1c2df 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 lbackAll(db);.
1c2e0 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1c2e1 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 it = 1;. }els
1c2e2 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 e{. db->aut
1c2e3 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 64 65 oCommit = (u8)de
1c2e4 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b siredAutoCommit;
1c2e5 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1c2e6 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 e3VdbeHalt(p)==S
1c2e7 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
1c2e8 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
1c2e9 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 ;. db->au
1c2ea 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 toCommit = (u8)(
1c2eb 31 2d 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 1-desiredAutoCom
1c2ec 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d mit);. p-
1c2ed 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 >rc = rc = SQLIT
1c2ee 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 E_BUSY;.
1c2ef 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1c2f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1c2f1 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1c2f2 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b nStatement==0 );
1c2f3 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 . sqlite3Clos
1c2f4 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b eSavepoints(db);
1c2f5 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d . if( p->rc==
1c2f6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1c2f7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
1c2f8 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ONE;. }else{.
1c2f9 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1c2fa 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 E_ERROR;. }.
1c2fb 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
1c2fc 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
1c2fd 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1c2fe 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1c2ff 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 64 65 db,. (!de
1c300 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29 siredAutoCommit)
1c301 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74 20 61 ?"cannot start a
1c302 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 74 transaction wit
1c303 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f hin a transactio
1c304 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28 72 6f n":(. (ro
1c305 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 llback)?"cannot
1c306 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 rollback - no tr
1c307 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1c308 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20 ive":.
1c309 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 "cannot
1c30a 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 commit - no tra
1c30b 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
1c30c 76 65 22 29 29 3b 0a 20 20 20 20 20 20 20 20 20 ve"));.
1c30d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1c30e 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 62 72 _ERROR;. }. br
1c30f 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1c310 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 50 e: Transaction P
1c311 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1c312 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 Begin a transac
1c313 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 6e 73 tion. The trans
1c314 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 65 6e action ends when
1c315 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 6f 6c a Commit or Rol
1c316 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 65 20 lback.** opcode
1c317 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 is encountered.
1c318 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 Depending on th
1c319 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 65 e ON CONFLICT se
1c31a 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 74 72 tting, the.** tr
1c31b 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 ansaction might
1c31c 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 also be rolled b
1c31d 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ack if an error
1c31e 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a is encountered..
1c31f 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1c320 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
1c321 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 77 68 abase file on wh
1c322 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 ich the transact
1c323 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 74 65 ion is.** starte
1c324 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 20 74 d. Index 0 is t
1c325 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1c326 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 file and index
1c327 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 1 is the.** file
1c328 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 used for tempor
1c329 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 6e 64 ary tables. Ind
1c32a 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d 6f 72 ices of 2 or mor
1c32b 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 0a 2a e are used for.*
1c32c 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 * attached datab
1c32d 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 ases..**.** If P
1c32e 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 2 is non-zero, t
1c32f 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e hen a write-tran
1c330 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
1c331 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 44 20 ed. A RESERVED
1c332 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 lock is.** obtai
1c333 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ned on the datab
1c334 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 61 20 ase file when a
1c335 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1c336 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 4e n is started. N
1c337 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 o.** other proce
1c338 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 6e 6f ss can start ano
1c339 74 68 65 72 20 77 72 69 74 65 20 74 72 61 6e 73 ther write trans
1c33a 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 action while thi
1c33b 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 s transaction is
1c33c 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 20 53 .** underway. S
1c33d 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 65 20 tarting a write
1c33e 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f transaction also
1c33f 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c 6c 62 creates a rollb
1c340 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a ack journal. A.*
1c341 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 * write transact
1c342 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 ion must be star
1c343 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 ted before any c
1c344 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61 hanges can be ma
1c345 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 de to the.** dat
1c346 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 69 73 abase. If P2 is
1c347 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 2 or greater th
1c348 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 en an EXCLUSIVE
1c349 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74 lock is also obt
1c34a 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 ained.** on the
1c34b 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 file..**.** If P
1c34c 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 2 is zero, then
1c34d 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f a read-lock is o
1c34e 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 btained on the d
1c34f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f atabase file..*/
1c350 0a 63 61 73 65 20 4f 50 5f 54 72 61 6e 73 61 63 .case OP_Transac
1c351 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 20 69 20 tion: {. int i
1c352 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 72 = pOp->p1;. Btr
1c353 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73 73 65 ee *pBt;.. asse
1c354 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 rt( i>=0 && i<db
1c355 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1c356 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1c357 20 26 20 28 31 3c 3c 69 29 29 21 3d 30 20 29 3b & (1<<i))!=0 );
1c358 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 . pBt = db->aDb
1c359 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 [i].pBt;.. if(
1c35a 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 pBt ){. rc =
1c35b 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
1c35c 6e 54 72 61 6e 73 28 70 42 74 2c 20 70 4f 70 2d nTrans(pBt, pOp-
1c35d 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63 >p2);. if( rc
1c35e 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b ==SQLITE_BUSY ){
1c35f 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 . p->pc = p
1c360 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d c;. p->rc =
1c361 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1c362 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64 Y;. goto vd
1c363 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d be_return;. }
1c364 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1c365 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 ITE_OK && rc!=SQ
1c366 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 2f 2a LITE_READONLY /*
1c367 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 && rc!=SQLITE_B
1c368 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20 20 20 20 USY */ ){.
1c369 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1c36a 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 o_error;. }.
1c36b 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1c36c 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f * Opcode: ReadCo
1c36d 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 okie P1 P2 P3 *
1c36e 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f *.**.** Read coo
1c36f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72 kie number P3 fr
1c370 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61 om database P1 a
1c371 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f nd write it into
1c372 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a register P2..**
1c373 20 50 33 3d 3d 30 20 69 73 20 74 68 65 20 73 63 P3==0 is the sc
1c374 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 hema version. P
1c375 33 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61 3==1 is the data
1c376 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 base format..**
1c377 50 33 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63 P3==2 is the rec
1c378 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 ommended pager c
1c379 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 ache size, and s
1c37a 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 o forth. P1==0
1c37b 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 is.** the main d
1c37c 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1c37d 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 P1==1 is the da
1c37e 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 tabase file used
1c37f 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d to store.** tem
1c380 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a porary tables..*
1c381 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 65 *.** If P1 is ne
1c382 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 69 gative, then thi
1c383 73 20 69 73 20 61 20 72 65 71 75 65 73 74 20 74 s is a request t
1c384 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a 65 20 o read the size
1c385 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 of a.** database
1c386 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50 33 20 s free-list. P3
1c387 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31 must be set to 1
1c388 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 54 in this case. T
1c389 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64 61 74 he actual.** dat
1c38a 61 62 61 73 65 20 61 63 63 65 73 73 65 64 20 69 abase accessed i
1c38b 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e 20 46 s ((P1+1)*-1). F
1c38c 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20 50 31 or example, a P1
1c38d 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 2d 31 parameter of -1
1c38e 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 .** corresponds
1c38f 74 6f 20 64 61 74 61 62 61 73 65 20 30 20 28 22 to database 0 ("
1c390 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f 66 20 main"), a P1 of
1c391 2d 32 20 69 73 20 64 61 74 61 62 61 73 65 20 31 -2 is database 1
1c392 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a 2a 2a ("temp")..**.**
1c393 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 There must be a
1c394 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 read-lock on th
1c395 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74 68 e database (eith
1c396 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e er a transaction
1c397 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61 72 .** must be star
1c398 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75 73 ted or there mus
1c399 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 t be an open cur
1c39a 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65 sor) before.** e
1c39b 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e xecuting this in
1c39c 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 struction..*/.ca
1c39d 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 se OP_ReadCookie
1c39e 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1c39f 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1c3a0 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d ease */. int iM
1c3a1 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d eta;. int iDb =
1c3a2 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 pOp->p1;. int
1c3a3 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 iCookie = pOp->p
1c3a4 33 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 3;.. assert( pO
1c3a5 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 p->p3<SQLITE_N_B
1c3a6 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 69 TREE_META );. i
1c3a7 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 f( iDb<0 ){.
1c3a8 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62 2b 31 iDb = (-1*(iDb+1
1c3a9 29 29 3b 0a 20 20 20 20 69 43 6f 6f 6b 69 65 20 ));. iCookie
1c3aa 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 61 73 73 *= -1;. }. ass
1c3ab 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 ert( iDb>=0 && i
1c3ac 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 Db<db->nDb );.
1c3ad 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b assert( db->aDb[
1c3ae 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 iDb].pBt!=0 );.
1c3af 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
1c3b0 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 eeMask & (1<<iDb
1c3b1 29 29 21 3d 30 20 29 3b 0a 20 20 2f 2a 20 54 68 ))!=0 );. /* Th
1c3b2 65 20 69 6e 64 65 78 69 6e 67 20 6f 66 20 6d 65 e indexing of me
1c3b3 74 61 20 76 61 6c 75 65 73 20 61 74 20 74 68 65 ta values at the
1c3b4 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 69 73 schema layer is
1c3b5 20 6f 66 66 20 62 79 20 6f 6e 65 20 66 72 6f 6d off by one from
1c3b6 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 69 . ** the indexi
1c3b7 6e 67 20 69 6e 20 74 68 65 20 62 74 72 65 65 20 ng in the btree
1c3b8 6c 61 79 65 72 2e 20 20 54 68 65 20 62 74 72 65 layer. The btre
1c3b9 65 20 63 6f 6e 73 69 64 65 72 73 20 6d 65 74 61 e considers meta
1c3ba 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 62 65 20 74 [0] to. ** be t
1c3bb 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 he number of fre
1c3bc 65 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 e pages in the d
1c3bd 61 74 61 62 61 73 65 20 28 61 20 72 65 61 64 2d atabase (a read-
1c3be 6f 6e 6c 79 20 76 61 6c 75 65 29 0a 20 20 2a 2a only value). **
1c3bf 20 61 6e 64 20 6d 65 74 61 5b 31 5d 20 74 6f 20 and meta[1] to
1c3c0 62 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f be the schema co
1c3c1 6f 6b 69 65 2e 20 20 54 68 65 20 73 63 68 65 6d okie. The schem
1c3c2 61 20 6c 61 79 65 72 20 63 6f 6e 73 69 64 65 72 a layer consider
1c3c3 73 0a 20 20 2a 2a 20 6d 65 74 61 5b 31 5d 20 74 s. ** meta[1] t
1c3c4 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d 61 20 o be the schema
1c3c5 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 77 65 20 68 cookie. So we h
1c3c6 61 76 65 20 74 6f 20 73 68 69 66 74 20 74 68 65 ave to shift the
1c3c7 20 69 6e 64 65 78 0a 20 20 2a 2a 20 62 79 20 6f index. ** by o
1c3c8 6e 65 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 ne in the follow
1c3c9 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 ing statement..
1c3ca 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
1c3cb 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 64 e3BtreeGetMeta(d
1c3cc 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c b->aDb[iDb].pBt,
1c3cd 20 31 20 2b 20 69 43 6f 6f 6b 69 65 2c 20 28 75 1 + iCookie, (u
1c3ce 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20 32 *)&iMeta);.
1c3cf 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d 65 74 pOut->u.i = iMet
1c3d0 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 a;. MemSetTypeF
1c3d1 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1c3d2 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1c3d3 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f /* Opcode: SetCo
1c3d4 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 okie P1 P2 P3 *
1c3d5 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 *.**.** Write th
1c3d6 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 e content of reg
1c3d7 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 70 ister P3 (interp
1c3d8 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65 reted as an inte
1c3d9 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f ger).** into coo
1c3da 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66 kie number P2 of
1c3db 20 64 61 74 61 62 61 73 65 20 50 31 2e 0a 2a 2a database P1..**
1c3dc 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20 73 63 P2==0 is the sc
1c3dd 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 hema version. P
1c3de 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61 2==1 is the data
1c3df 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 base format..**
1c3e0 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63 P2==2 is the rec
1c3e1 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 ommended pager c
1c3e2 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 ache size, and s
1c3e3 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 o forth. P1==0
1c3e4 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 is.** the main d
1c3e5 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1c3e6 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 P1==1 is the da
1c3e7 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 tabase file used
1c3e8 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d to store.** tem
1c3e9 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a porary tables..*
1c3ea 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 *.** A transacti
1c3eb 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 on must be start
1c3ec 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 ed before execut
1c3ed 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e ing this opcode.
1c3ee 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43 .*/.case OP_SetC
1c3ef 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f ookie: { /
1c3f0 2a 20 69 6e 33 20 2a 2f 0a 20 20 44 62 20 2a 70 * in3 */. Db *p
1c3f1 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f Db;. assert( pO
1c3f2 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 p->p2<SQLITE_N_B
1c3f3 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 TREE_META );. a
1c3f4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1c3f5 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 0 && pOp->p1<db-
1c3f6 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1c3f7 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1c3f8 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 & (1<<pOp->p1))!
1c3f9 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 =0 );. pDb = &d
1c3fa 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b b->aDb[pOp->p1];
1c3fb 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e . assert( pDb->
1c3fc 70 42 74 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 pBt!=0 );. sqli
1c3fd 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
1c3fe 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a rify(pIn3);. /*
1c3ff 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 See note about
1c400 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f index shifting o
1c401 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 n OP_ReadCookie
1c402 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
1c403 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
1c404 28 70 44 62 2d 3e 70 42 74 2c 20 31 2b 70 4f 70 (pDb->pBt, 1+pOp
1c405 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d ->p2, (int)pIn3-
1c406 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70 >u.i);. if( pOp
1c407 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 2f ->p2==0 ){. /
1c408 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68 65 6d * When the schem
1c409 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 a cookie changes
1c40a 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77 , record the new
1c40b 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e 61 6c cookie internal
1c40c 6c 79 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 ly */. pDb->p
1c40d 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 Schema->schema_c
1c40e 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70 49 6e ookie = (int)pIn
1c40f 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 3->u.i;. db->
1c410 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f flags |= SQLITE_
1c411 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 InternChanges;.
1c412 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e }else if( pOp->
1c413 70 32 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 p2==1 ){. /*
1c414 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69 Record changes i
1c415 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 n the file forma
1c416 74 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53 t */. pDb->pS
1c417 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d chema->file_form
1c418 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 at = (u8)pIn3->u
1c419 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f .i;. }. if( pO
1c41a 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 p->p1==1 ){.
1c41b 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c /* Invalidate al
1c41c 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 l prepared state
1c41d 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74 ments whenever t
1c41e 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
1c41f 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 . ** schema i
1c420 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b s changed. Tick
1c421 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 et #1644 */.
1c422 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
1c423 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
1c424 64 62 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b db);. }. break
1c425 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1c426 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20 VerifyCookie P1
1c427 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b P2 *.**.** Check
1c428 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c the value of gl
1c429 6f 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 obal database pa
1c42a 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30 rameter number 0
1c42b 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 (the.** schema
1c42c 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b version) and mak
1c42d 65 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75 e sure it is equ
1c42e 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50 al to P2. .** P
1c42f 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 1 is the databas
1c430 65 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 e number which i
1c431 73 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e s 0 for the main
1c432 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
1c433 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20 * and 1 for the
1c434 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d file holding tem
1c435 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e porary tables an
1c436 64 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 d some higher nu
1c437 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 mber.** for auxi
1c438 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e liary databases.
1c439 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 .**.** The cooki
1c43a 65 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61 e changes its va
1c43b 6c 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 lue whenever the
1c43c 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
1c43d 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 changes..** Thi
1c43e 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 s operation is u
1c43f 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68 sed to detect wh
1c440 65 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b en that the cook
1c441 69 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a ie has changed.*
1c442 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 * and that the c
1c443 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e urrent process n
1c444 65 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74 eeds to reread t
1c445 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a he schema..**.**
1c446 20 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 Either a transa
1c447 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 ction needs to h
1c448 61 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64 ave been started
1c449 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e or an OP_Open n
1c44a 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 eeds.** to be ex
1c44b 65 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62 ecuted (to estab
1c44c 6c 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b lish a read lock
1c44d 29 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70 ) before this op
1c44e 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b code is.** invok
1c44f 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 ed..*/.case OP_V
1c450 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 20 erifyCookie: {.
1c451 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74 int iMeta;. Bt
1c452 72 65 65 20 2a 70 42 74 3b 0a 20 20 61 73 73 65 ree *pBt;. asse
1c453 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1c454 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1c455 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1c456 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1c457 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 1<<pOp->p1))!=0
1c458 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 );. pBt = db->a
1c459 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b Db[pOp->p1].pBt;
1c45a 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 . if( pBt ){.
1c45b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1c45c 72 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c 20 reeGetMeta(pBt,
1c45d 31 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74 61 1, (u32 *)&iMeta
1c45e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1c45f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1c460 20 20 20 20 69 4d 65 74 61 20 3d 20 30 3b 0a 20 iMeta = 0;.
1c461 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
1c462 49 54 45 5f 4f 4b 20 26 26 20 69 4d 65 74 61 21 ITE_OK && iMeta!
1c463 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 =pOp->p2 ){.
1c464 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1c465 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1c466 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1c467 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1c468 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63 db, "database sc
1c469 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 hema has changed
1c46a 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ");. /* If th
1c46b 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 e schema-cookie
1c46c 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1c46d 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74 e file matches t
1c46e 68 65 20 63 6f 6f 6b 69 65 20 0a 20 20 20 20 2a he cookie . *
1c46f 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68 * stored with th
1c470 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 e in-memory repr
1c471 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
1c472 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 e schema, do.
1c473 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 ** not reload t
1c474 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 he schema from t
1c475 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1c476 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1c477 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 If virtual-table
1c478 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 s are in use, th
1c479 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 is is not just a
1c47a 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a n optimization..
1c47b 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d ** Often, v-
1c47c 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65 tables store the
1c47d 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72 ir data in other
1c47e 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 SQLite tables,
1c47f 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 which. ** are
1c480 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69 queried from wi
1c481 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 thin xNext() and
1c482 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d other v-table m
1c483 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 ethods using.
1c484 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65 ** prepared que
1c485 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20 ries. If such a
1c486 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d query is out-of-
1c487 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 date, we do not
1c488 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 want to. ** d
1c489 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62 iscard the datab
1c48a 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74 ase schema, as t
1c48b 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70 he user code imp
1c48c 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 lementing the.
1c48d 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 ** v-table wou
1c48e 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65 ld have to be re
1c48f 61 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69 ady for the sqli
1c490 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 te3_vtab structu
1c491 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a re itself. **
1c492 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 to be invalidat
1c493 65 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69 ed whenever sqli
1c494 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 te3_step() is ca
1c495 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
1c496 20 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 . ** a v-tab
1c497 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a le method.. *
1c498 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 /. if( db->aD
1c499 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 b[pOp->p1].pSche
1c49a 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 ma->schema_cooki
1c49b 65 21 3d 69 4d 65 74 61 20 29 7b 0a 20 20 20 20 e!=iMeta ){.
1c49c 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
1c49d 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
1c49e 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d pOp->p1);. }
1c49f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 .. sqlite3Exp
1c4a0 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
1c4a1 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 72 ments(db);. r
1c4a2 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d c = SQLITE_SCHEM
1c4a3 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a A;. }. break;.
1c4a4 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 }../* Opcode: Op
1c4a5 65 6e 52 65 61 64 20 50 31 20 50 32 20 50 33 20 enRead P1 P2 P3
1c4a6 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e P4 P5.**.** Open
1c4a7 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 a read-only cur
1c4a8 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 sor for the data
1c4a9 62 61 73 65 20 74 61 62 6c 65 20 77 68 6f 73 65 base table whose
1c4aa 20 72 6f 6f 74 20 70 61 67 65 20 69 73 0a 2a 2a root page is.**
1c4ab 20 50 32 20 69 6e 20 61 20 64 61 74 61 62 61 73 P2 in a databas
1c4ac 65 20 66 69 6c 65 2e 20 20 54 68 65 20 64 61 74 e file. The dat
1c4ad 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 64 65 abase file is de
1c4ae 74 65 72 6d 69 6e 65 64 20 62 79 20 50 33 2e 20 termined by P3.
1c4af 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61 6e 73 20 .** P3==0 means
1c4b0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1c4b1 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e 73 20 74 e, P3==1 means t
1c4b2 68 65 20 64 61 74 61 62 61 73 65 20 75 73 65 64 he database used
1c4b3 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 for .** tempora
1c4b4 72 79 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 50 ry tables, and P
1c4b5 33 3e 31 20 6d 65 61 6e 73 20 75 73 65 64 20 74 3>1 means used t
1c4b6 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
1c4b7 20 61 74 74 61 63 68 65 64 0a 2a 2a 20 64 61 74 attached.** dat
1c4b8 61 62 61 73 65 2e 20 20 47 69 76 65 20 74 68 65 abase. Give the
1c4b9 20 6e 65 77 20 63 75 72 73 6f 72 20 61 6e 20 69 new cursor an i
1c4ba 64 65 6e 74 69 66 69 65 72 20 6f 66 20 50 31 2e dentifier of P1.
1c4bb 20 20 54 68 65 20 50 31 0a 2a 2a 20 76 61 6c 75 The P1.** valu
1c4bc 65 73 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 63 es need not be c
1c4bd 6f 6e 74 69 67 75 6f 75 73 20 62 75 74 20 61 6c ontiguous but al
1c4be 6c 20 50 31 20 76 61 6c 75 65 73 20 73 68 6f 75 l P1 values shou
1c4bf 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65 ld be small inte
1c4c0 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69 73 20 61 gers..** It is a
1c4c1 6e 20 65 72 72 6f 72 20 66 6f 72 20 50 31 20 74 n error for P1 t
1c4c2 6f 20 62 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a o be negative..*
1c4c3 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30 20 74 68 *.** If P5!=0 th
1c4c4 65 6e 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 en use the conte
1c4c5 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 nt of register P
1c4c6 32 20 61 73 20 74 68 65 20 72 6f 6f 74 20 70 61 2 as the root pa
1c4c7 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 76 ge, not.** the v
1c4c8 61 6c 75 65 20 6f 66 20 50 32 20 69 74 73 65 6c alue of P2 itsel
1c4c9 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 77 f..**.** There w
1c4ca 69 6c 6c 20 62 65 20 61 20 72 65 61 64 20 6c 6f ill be a read lo
1c4cb 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
1c4cc 73 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 72 se whenever ther
1c4cd 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 e is an.** open
1c4ce 63 75 72 73 6f 72 2e 20 20 49 66 20 74 68 65 20 cursor. If the
1c4cf 64 61 74 61 62 61 73 65 20 77 61 73 20 75 6e 6c database was unl
1c4d0 6f 63 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 74 ocked prior to t
1c4d1 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a his instruction.
1c4d2 2a 2a 20 74 68 65 6e 20 61 20 72 65 61 64 20 6c ** then a read l
1c4d3 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 20 ock is acquired
1c4d4 61 73 20 70 61 72 74 20 6f 66 20 74 68 69 73 20 as part of this
1c4d5 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 41 20 instruction. A
1c4d6 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c read.** lock all
1c4d7 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ows other proces
1c4d8 73 65 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 ses to read the
1c4d9 64 61 74 61 62 61 73 65 20 62 75 74 20 70 72 6f database but pro
1c4da 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79 20 6f 74 hibits.** any ot
1c4db 68 65 72 20 70 72 6f 63 65 73 73 20 66 72 6f 6d her process from
1c4dc 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64 modifying the d
1c4dd 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 72 65 atabase. The re
1c4de 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65 ad lock is.** re
1c4df 6c 65 61 73 65 64 20 77 68 65 6e 20 61 6c 6c 20 leased when all
1c4e0 63 75 72 73 6f 72 73 20 61 72 65 20 63 6c 6f 73 cursors are clos
1c4e1 65 64 2e 20 20 49 66 20 74 68 69 73 20 69 6e 73 ed. If this ins
1c4e2 74 72 75 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 truction attempt
1c4e3 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61 20 72 65 s.** to get a re
1c4e4 61 64 20 6c 6f 63 6b 20 62 75 74 20 66 61 69 6c ad lock but fail
1c4e5 73 2c 20 74 68 65 20 73 63 72 69 70 74 20 74 65 s, the script te
1c4e6 72 6d 69 6e 61 74 65 73 20 77 69 74 68 20 61 6e rminates with an
1c4e7 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 .** SQLITE_BUSY
1c4e8 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a error code..**.*
1c4e9 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d * The P4 value m
1c4ea 61 79 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 ay be either an
1c4eb 69 6e 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33 integer (P4_INT3
1c4ec 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 2) or a pointer
1c4ed 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20 to.** a KeyInfo
1c4ee 73 74 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45 structure (P4_KE
1c4ef 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73 YINFO). If it is
1c4f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1c4f1 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 KeyInfo .** stru
1c4f2 63 74 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64 cture, then said
1c4f3 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 6e structure defin
1c4f4 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 es the content a
1c4f5 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a nd collating .**
1c4f6 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 sequence of the
1c4f7 20 69 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 index being ope
1c4f8 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ned. Otherwise,
1c4f9 69 66 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65 if P4 is an inte
1c4fa 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 ger .** value, i
1c4fb 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 t is set to the
1c4fc 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
1c4fd 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a s in the table..
1c4fe 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f **.** See also O
1c4ff 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 penWrite..*/./*
1c500 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57 72 69 74 Opcode: OpenWrit
1c501 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 e P1 P2 P3 P4 P5
1c502 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 .**.** Open a re
1c503 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 ad/write cursor
1c504 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20 named P1 on the
1c505 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 table or index w
1c506 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 hose root.** pag
1c507 65 20 69 73 20 50 32 2e 20 20 4f 72 20 69 66 20 e is P2. Or if
1c508 50 35 21 3d 30 20 75 73 65 20 74 68 65 20 63 6f P5!=0 use the co
1c509 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 ntent of registe
1c50a 72 20 50 32 20 74 6f 20 66 69 6e 64 20 74 68 65 r P2 to find the
1c50b 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 0a 2a .** root page..*
1c50c 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 *.** The P4 valu
1c50d 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 e may be either
1c50e 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49 an integer (P4_I
1c50f 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 NT32) or a point
1c510 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e er to.** a KeyIn
1c511 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34 fo structure (P4
1c512 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 _KEYINFO). If it
1c513 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1c514 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 a KeyInfo .** s
1c515 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73 tructure, then s
1c516 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65 aid structure de
1c517 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e fines the conten
1c518 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 t and collating
1c519 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20 .** sequence of
1c51a 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 the index being
1c51b 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 opened. Otherwis
1c51c 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69 e, if P4 is an i
1c51d 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 nteger .** value
1c51e 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 , it is set to t
1c51f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1c520 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
1c521 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e e..**.** This in
1c522 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20 struction works
1c523 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65 just like OpenRe
1c524 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ad except that i
1c525 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73 t opens the curs
1c526 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72 or.** in read/wr
1c527 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61 ite mode. For a
1c528 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68 given table, th
1c529 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f ere can be one o
1c52a 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79 r more read-only
1c52b 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61 .** cursors or a
1c52c 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69 single read/wri
1c52d 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f te cursor but no
1c52e 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 t both..**.** Se
1c52f 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e e also OpenRead.
1c530 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e .*/.case OP_Open
1c531 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70 Read:.case OP_Op
1c532 65 6e 57 72 69 74 65 3a 20 7b 0a 20 20 69 6e 74 enWrite: {. int
1c533 20 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 4b nField = 0;. K
1c534 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
1c535 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 20 3d 20 = 0;. int i =
1c536 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70 pOp->p1;. int p
1c537 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 2 = pOp->p2;. i
1c538 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 nt iDb = pOp->p3
1c539 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a ;. int wrFlag;.
1c53a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 Btree *pX;. V
1c53b 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b dbeCursor *pCur;
1c53c 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 0a 20 . Db *pDb;. .
1c53d 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
1c53e 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
1c53f 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e ;. assert( (p->
1c540 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c btreeMask & (1<<
1c541 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 iDb))!=0 );. pD
1c542 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 b = &db->aDb[iDb
1c543 5d 3b 0a 20 20 70 58 20 3d 20 70 44 62 2d 3e 70 ];. pX = pDb->p
1c544 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 58 Bt;. assert( pX
1c545 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 !=0 );. if( pOp
1c546 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 ->opcode==OP_Ope
1c547 6e 57 72 69 74 65 20 29 7b 0a 20 20 20 20 77 72 nWrite ){. wr
1c548 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 Flag = 1;. if
1c549 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e ( pDb->pSchema->
1c54a 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d file_format < p-
1c54b 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 >minWriteFileFor
1c54c 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e mat ){. p->
1c54d 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d minWriteFileForm
1c54e 61 74 20 3d 20 70 44 62 2d 3e 70 53 63 68 65 6d at = pDb->pSchem
1c54f 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a a->file_format;.
1c550 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1c551 20 20 20 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 wrFlag = 0;.
1c552 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 }. if( pOp->p5
1c553 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1c554 70 32 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 p2>0 );. asse
1c555 72 74 28 20 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 rt( p2<=p->nMem
1c556 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 70 );. pIn2 = &p
1c557 2d 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 20 20 ->aMem[p2];.
1c558 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
1c559 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a tegerify(pIn2);.
1c55a 20 20 20 20 70 32 20 3d 20 28 69 6e 74 29 70 49 p2 = (int)pI
1c55b 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 69 66 28 n2->u.i;. if(
1c55c 20 70 32 3c 32 20 29 20 7b 0a 20 20 20 20 20 20 p2<2 ) {.
1c55d 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
1c55e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
1c55f 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1c560 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 o_error;. }.
1c561 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d }. assert( i>=
1c562 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 0 );. if( pOp->
1c563 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e p4type==P4_KEYIN
1c564 46 4f 20 29 7b 0a 20 20 20 20 70 4b 65 79 49 6e FO ){. pKeyIn
1c565 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 fo = pOp->p4.pKe
1c566 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79 49 yInfo;. pKeyI
1c567 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 nfo->enc = ENC(p
1c568 2d 3e 64 62 29 3b 0a 20 20 20 20 6e 46 69 65 6c ->db);. nFiel
1c569 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 d = pKeyInfo->nF
1c56a 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 ield+1;. }else
1c56b 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d if( pOp->p4type=
1c56c 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 20 =P4_INT32 ){.
1c56d 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 nField = pOp->p
1c56e 34 2e 69 3b 0a 20 20 7d 0a 20 20 70 43 75 72 20 4.i;. }. pCur
1c56f 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 = allocateCursor
1c570 28 70 2c 20 69 2c 20 6e 46 69 65 6c 64 2c 20 69 (p, i, nField, i
1c571 44 62 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 43 Db, 1);. if( pC
1c572 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f ur==0 ) goto no_
1c573 6d 65 6d 3b 0a 20 20 70 43 75 72 2d 3e 6e 75 6c mem;. pCur->nul
1c574 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d lRow = 1;. rc =
1c575 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1c576 73 6f 72 28 70 58 2c 20 70 32 2c 20 77 72 46 6c sor(pX, p2, wrFl
1c577 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 ag, pKeyInfo, pC
1c578 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 ur->pCursor);.
1c579 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d pCur->pKeyInfo =
1c57a 20 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 73 77 pKeyInfo;.. sw
1c57b 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20 itch( rc ){.
1c57c 63 61 73 65 20 53 51 4c 49 54 45 5f 42 55 53 59 case SQLITE_BUSY
1c57d 3a 20 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 : {. p->pc
1c57e 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 = pc;. p->r
1c57f 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f c = rc = SQLITE_
1c580 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f BUSY;. goto
1c581 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 vdbe_return;.
1c582 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
1c583 49 54 45 5f 4f 4b 3a 20 7b 0a 20 20 20 20 20 20 ITE_OK: {.
1c584 69 6e 74 20 66 6c 61 67 73 20 3d 20 73 71 6c 69 int flags = sqli
1c585 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 te3BtreeFlags(pC
1c586 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 ur->pCursor);.
1c587 20 20 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 /* Sanity ch
1c588 65 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 74 68 ecking. Only th
1c589 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74 e lower four bit
1c58a 73 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20 62 s of the flags b
1c58b 79 74 65 20 73 68 6f 75 6c 64 0a 20 20 20 20 20 yte should.
1c58c 20 2a 2a 20 62 65 20 75 73 65 64 2e 20 20 42 69 ** be used. Bi
1c58d 74 20 33 20 28 6d 61 73 6b 20 30 78 30 38 29 20 t 3 (mask 0x08)
1c58e 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 is unpredictable
1c58f 2e 20 20 54 68 65 20 6c 6f 77 65 72 20 33 20 62 . The lower 3 b
1c590 69 74 73 0a 20 20 20 20 20 20 2a 2a 20 28 6d 61 its. ** (ma
1c591 73 6b 20 30 78 30 37 29 20 73 68 6f 75 6c 64 20 sk 0x07) should
1c592 62 65 20 65 69 74 68 65 72 20 35 20 28 69 6e 74 be either 5 (int
1c593 6b 65 79 2b 6c 65 61 66 64 61 74 61 20 66 6f 72 key+leafdata for
1c594 20 74 61 62 6c 65 73 29 20 6f 72 0a 20 20 20 20 tables) or.
1c595 20 20 2a 2a 20 32 20 28 7a 65 72 6f 64 61 74 61 ** 2 (zerodata
1c596 20 66 6f 72 20 69 6e 64 69 63 65 73 29 2e 20 20 for indices).
1c597 49 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 If these conditi
1c598 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6d 65 74 20 ons are not met
1c599 69 74 20 63 61 6e 0a 20 20 20 20 20 20 2a 2a 20 it can. **
1c59a 6f 6e 6c 79 20 6d 65 61 6e 20 74 68 61 74 20 77 only mean that w
1c59b 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 e are dealing wi
1c59c 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 th a corrupt dat
1c59d 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 abase file.
1c59e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 66 */. if( (f
1c59f 6c 61 67 73 20 26 20 30 78 66 30 29 21 3d 30 20 lags & 0xf0)!=0
1c5a0 7c 7c 20 28 28 66 6c 61 67 73 20 26 20 30 78 30 || ((flags & 0x0
1c5a1 37 29 21 3d 35 20 26 26 20 28 66 6c 61 67 73 20 7)!=5 && (flags
1c5a2 26 20 30 78 30 37 29 21 3d 32 29 20 29 7b 0a 20 & 0x07)!=2) ){.
1c5a3 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1c5a4 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1c5a5 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 . goto ab
1c5a6 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1c5a7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1c5a8 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 pCur->isTable =
1c5a9 28 66 6c 61 67 73 20 26 20 42 54 52 45 45 5f 49 (flags & BTREE_I
1c5aa 4e 54 4b 45 59 29 21 3d 30 20 3f 31 3a 30 3b 0a NTKEY)!=0 ?1:0;.
1c5ab 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e pCur->isIn
1c5ac 64 65 78 20 3d 20 28 66 6c 61 67 73 20 26 20 42 dex = (flags & B
1c5ad 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d TREE_ZERODATA)!=
1c5ae 30 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 2f 2a 0 ?1:0;. /*
1c5af 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d 65 61 If P4==0 it mea
1c5b0 6e 73 20 77 65 20 61 72 65 20 65 78 70 65 63 74 ns we are expect
1c5b1 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74 61 62 ed to open a tab
1c5b2 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20 74 68 le. If P4!=0 th
1c5b3 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 65 en. ** we e
1c5b4 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70 65 6e xpect to be open
1c5b5 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 ing an index. I
1c5b6 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 77 68 f this is not wh
1c5b7 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20 20 20 at happened,.
1c5b8 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 ** then the d
1c5b9 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 atabase is corru
1c5ba 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 pt. */.
1c5bb 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69 73 54 if( (pCur->isT
1c5bc 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34 74 able && pOp->p4t
1c5bd 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 29 ype==P4_KEYINFO)
1c5be 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43 75 72 . || (pCur
1c5bf 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70 4f 70 ->isIndex && pOp
1c5c0 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 ->p4type!=P4_KEY
1c5c1 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20 20 20 INFO) ){.
1c5c2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
1c5c3 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
1c5c4 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1c5c5 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1c5c6 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
1c5c7 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1c5c8 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b 0a SQLITE_EMPTY: {.
1c5c9 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61 pCur->isTa
1c5ca 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 ble = pOp->p4typ
1c5cb 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 e!=P4_KEYINFO;.
1c5cc 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 pCur->isInd
1c5cd 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73 54 61 ex = !pCur->isTa
1c5ce 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d ble;. pCur-
1c5cf 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 >pCursor = 0;.
1c5d0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1c5d1 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
1c5d2 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
1c5d3 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f lt: {. goto
1c5d4 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1c5d5 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ror;. }. }.
1c5d6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1c5d7 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 code: OpenEpheme
1c5d8 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a ral P1 P2 * P4 *
1c5d9 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 .**.** Open a ne
1c5da 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 w cursor P1 to a
1c5db 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 transient table
1c5dc 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 ..** The cursor
1c5dd 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 is always opened
1c5de 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e read/write even
1c5df 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e if .** the main
1c5e0 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 61 database is rea
1c5e1 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 d-only. The tra
1c5e2 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61 nsient or virtua
1c5e3 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65 l.** table is de
1c5e4 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 leted automatica
1c5e5 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 72 lly when the cur
1c5e6 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a sor is closed..*
1c5e7 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e *.** P2 is the n
1c5e8 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1c5e9 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 in the virtual
1c5ea 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 table..** The cu
1c5eb 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 rsor points to a
1c5ec 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66 20 BTree table if
1c5ed 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 P4==0 and to a B
1c5ee 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 Tree index.** if
1c5ef 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 P4 is not 0. I
1c5f0 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c f P4 is not NULL
1c5f1 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 , it points to a
1c5f2 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
1c5f3 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e re.** that defin
1c5f4 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 es the format of
1c5f5 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64 keys in the ind
1c5f6 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f ex..**.** This o
1c5f7 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63 pcode was once c
1c5f8 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 alled OpenTemp.
1c5f9 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74 65 But that create
1c5fa 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 d.** confusion b
1c5fb 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d 20 ecause the term
1c5fc 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 "temp table", mi
1c5fd 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65 72 ght refer either
1c5fe 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61 .** to a TEMP ta
1c5ff 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c ble at the SQL l
1c600 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 evel, or to a ta
1c601 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a ble opened by.**
1c602 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 this opcode. T
1c603 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 hen this opcode
1c604 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 was call OpenVir
1c605 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 tual. But.** th
1c606 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75 at created confu
1c607 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68 sion with the wh
1c608 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c ole virtual-tabl
1c609 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 e idea..*/.case
1c60a 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
1c60b 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f : {. int i = pO
1c60c 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
1c60d 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74 61 74 sor *pCx;. stat
1c60e 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65 ic const int ope
1c60f 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20 nFlags = .
1c610 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
1c611 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 WRITE |. SQ
1c612 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
1c613 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f |. SQLITE_
1c614 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c OPEN_EXCLUSIVE |
1c615 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 . SQLITE_OP
1c616 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
1c617 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f |. SQLITE_
1c618 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 OPEN_TRANSIENT_D
1c619 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e B;.. assert( i>
1c61a 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c =0 );. pCx = al
1c61b 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 locateCursor(p,
1c61c 69 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20 i, pOp->p2, -1,
1c61d 31 29 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 1);. if( pCx==0
1c61e 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1c61f 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d pCx->nullRow =
1c620 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 1;. rc = sqlit
1c621 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 e3BtreeFactory(d
1c622 62 2c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f b, 0, 1, SQLITE_
1c623 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 DEFAULT_TEMP_CAC
1c624 48 45 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 HE_SIZE, openFla
1c625 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 gs,.
1c626 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
1c627 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 pCx->pBt);. if(
1c628 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1c629 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1c62a 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e e3BtreeBeginTran
1c62b 73 28 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a s(pCx->pBt, 1);.
1c62c 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
1c62d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
1c62e 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 * If a transient
1c62f 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72 index is requir
1c630 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79 ed, create it by
1c631 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 calling. **
1c632 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 sqlite3BtreeCrea
1c633 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74 teTable() with t
1c634 68 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 he BTREE_ZERODAT
1c635 41 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 A flag before.
1c636 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e ** opening it.
1c637 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 If a transient
1c638 74 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 table is require
1c639 64 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a d, just use the.
1c63a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 ** automatic
1c63b 61 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 ally created tab
1c63c 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 le with root-pag
1c63d 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 e 1 (an INTKEY t
1c63e 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 able).. */.
1c63f 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b if( pOp->p4.pK
1c640 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 eyInfo ){.
1c641 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 int pgno;.
1c642 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1c643 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 ype==P4_KEYINFO
1c644 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 );. rc = sq
1c645 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 lite3BtreeCreate
1c646 54 61 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20 Table(pCx->pBt,
1c647 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 &pgno, BTREE_ZER
1c648 4f 44 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69 ODATA); . i
1c649 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1c64a 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1c64b 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 rt( pgno==MASTER
1c64c 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 _ROOT+1 );.
1c64d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1c64e 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e treeCursor(pCx->
1c64f 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20 pBt, pgno, 1, .
1c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c651 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1c652 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 KeyInfo*)pOp->p4
1c653 2e 7a 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 .z, pCx->pCursor
1c654 29 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e );. pCx->
1c655 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e pKeyInfo = pOp->
1c656 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 p4.pKeyInfo;.
1c657 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e pCx->pKeyIn
1c658 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d fo->enc = ENC(p-
1c659 3e 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 >db);. }.
1c65a 20 20 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 pCx->isTable
1c65b 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
1c65c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1c65d 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 te3BtreeCursor(p
1c65e 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f Cx->pBt, MASTER_
1c65f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d ROOT, 1, 0, pCx-
1c660 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 >pCursor);.
1c661 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 pCx->isTable =
1c662 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 1;. }. }. p
1c663 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 Cx->isIndex = !p
1c664 43 78 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 Cx->isTable;. b
1c665 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c666 64 65 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 de: OpenPseudo P
1c667 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1c668 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72 * Open a new cur
1c669 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 sor that points
1c66a 74 6f 20 61 20 66 61 6b 65 20 74 61 62 6c 65 20 to a fake table
1c66b 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 that contains a
1c66c 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 single.** row of
1c66d 20 64 61 74 61 2e 20 20 41 6e 79 20 61 74 74 65 data. Any atte
1c66e 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 73 mpt to write a s
1c66f 65 63 6f 6e 64 20 72 6f 77 20 6f 66 20 64 61 74 econd row of dat
1c670 61 20 63 61 75 73 65 73 20 74 68 65 0a 2a 2a 20 a causes the.**
1c671 66 69 72 73 74 20 72 6f 77 20 74 6f 20 62 65 20 first row to be
1c672 64 65 6c 65 74 65 64 2e 20 20 41 6c 6c 20 64 61 deleted. All da
1c673 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 77 68 ta is deleted wh
1c674 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 en the cursor is
1c675 0a 2a 2a 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a .** closed..**.*
1c676 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 * A pseudo-table
1c677 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73 created by this
1c678 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 66 75 opcode is usefu
1c679 6c 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 l for holding th
1c67a 65 0a 2a 2a 20 4e 45 57 20 6f 72 20 4f 4c 44 20 e.** NEW or OLD
1c67b 74 61 62 6c 65 73 20 69 6e 20 61 20 74 72 69 67 tables in a trig
1c67c 67 65 72 2e 20 20 41 6c 73 6f 20 75 73 65 64 20 ger. Also used
1c67d 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69 to hold the a si
1c67e 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 ngle.** row outp
1c67f 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 ut from the sort
1c680 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 er so that the r
1c681 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 ow can be decomp
1c682 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 osed into.** ind
1c683 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 ividual columns
1c684 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c using the OP_Col
1c685 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a umn opcode..**.*
1c686 2a 20 57 68 65 6e 20 4f 50 5f 49 6e 73 65 72 74 * When OP_Insert
1c687 20 69 73 20 65 78 65 63 75 74 65 64 20 74 6f 20 is executed to
1c688 69 6e 73 65 72 74 20 61 20 72 6f 77 20 69 6e 20 insert a row in
1c689 74 6f 20 74 68 65 20 70 73 65 75 64 6f 20 74 61 to the pseudo ta
1c68a 62 6c 65 2c 0a 2a 2a 20 74 68 65 20 70 73 65 75 ble,.** the pseu
1c68b 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f 72 20 do-table cursor
1c68c 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 6d may or may not m
1c68d 61 6b 65 20 69 74 27 73 20 6f 77 6e 20 63 6f 70 ake it's own cop
1c68e 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 y of the.** orig
1c68f 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e 20 49 inal row data. I
1c690 66 20 50 32 20 69 73 20 30 2c 20 74 68 65 6e 20 f P2 is 0, then
1c691 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 the pseudo-table
1c692 20 77 69 6c 6c 20 63 6f 70 79 20 74 68 65 0a 2a will copy the.*
1c693 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 20 64 * original row d
1c694 61 74 61 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ata. Otherwise,
1c695 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1c696 20 6f 72 69 67 69 6e 61 6c 20 6d 65 6d 6f 72 79 original memory
1c697 20 63 65 6c 6c 0a 2a 2a 20 69 73 20 73 74 6f 72 cell.** is stor
1c698 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
1c699 2c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 , the vdbe progr
1c69a 61 6d 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 am must ensure t
1c69b 68 61 74 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f hat the .** memo
1c69c 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 ry cell containi
1c69d 6e 67 20 74 68 65 20 72 6f 77 20 64 61 74 61 20 ng the row data
1c69e 69 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 74 is not overwritt
1c69f 65 6e 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 en until the.**
1c6a0 70 73 65 75 64 6f 20 74 61 62 6c 65 20 69 73 20 pseudo table is
1c6a1 63 6c 6f 73 65 64 20 28 6f 72 20 61 20 6e 65 77 closed (or a new
1c6a2 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 row is inserted
1c6a3 20 69 6e 74 6f 20 69 74 29 2e 0a 2a 2a 0a 2a 2a into it)..**.**
1c6a4 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65 P3 is the numbe
1c6a5 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 r of fields in t
1c6a6 68 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 he records that
1c6a7 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62 will be stored b
1c6a8 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d y.** the pseudo-
1c6a9 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1c6aa 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a P_OpenPseudo: {.
1c6ab 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1c6ac 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1c6ad 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 20 *pCx;. assert(
1c6ae 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 i>=0 );. pCx =
1c6af 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 allocateCursor(p
1c6b0 2c 20 69 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 , i, pOp->p3, -1
1c6b1 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 78 3d , 0);. if( pCx=
1c6b2 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d =0 ) goto no_mem
1c6b3 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 ;. pCx->nullRow
1c6b4 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73 65 = 1;. pCx->pse
1c6b5 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 udoTable = 1;.
1c6b6 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f pCx->ephemPseudo
1c6b7 54 61 62 6c 65 20 3d 20 28 75 38 29 70 4f 70 2d Table = (u8)pOp-
1c6b8 3e 70 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 >p2;. pCx->isTa
1c6b9 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e ble = 1;. pCx->
1c6ba 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 isIndex = 0;. b
1c6bb 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c6bc 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a de: Close P1 * *
1c6bd 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 * *.**.** Close
1c6be 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f a cursor previo
1c6bf 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 usly opened as P
1c6c0 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 1. If P1 is not
1c6c1 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 .** currently op
1c6c2 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 en, this instruc
1c6c3 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
1c6c4 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 .*/.case OP_Clos
1c6c5 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 e: {. int i = p
1c6c6 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 Op->p1;. assert
1c6c7 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e ( i>=0 && i<p->n
1c6c8 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 Cursor );. sqli
1c6c9 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f te3VdbeFreeCurso
1c6ca 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d r(p, p->apCsr[i]
1c6cb 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d );. p->apCsr[i]
1c6cc 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1c6cd 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 ../* Opcode: See
1c6ce 6b 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20 kGe P1 P2 P3 P4
1c6cf 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f *.**.** If curso
1c6d0 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 r P1 refers to a
1c6d1 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 n SQL table (B-T
1c6d2 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e ree that uses in
1c6d3 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a teger keys), .**
1c6d4 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 use the value i
1c6d5 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 n register P3 as
1c6d6 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 the key. If cu
1c6d7 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a rsor P1 refers .
1c6d8 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 ** to an SQL ind
1c6d9 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 ex, then P3 is t
1c6da 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 he first in an a
1c6db 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 rray of P4 regis
1c6dc 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 ters .** that ar
1c6dd 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 e used as an unp
1c6de 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e acked index key.
1c6df 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 .**.** Repositi
1c6e0 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 on cursor P1 so
1c6e1 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1c6e2 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 to the smallest
1c6e3 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 entry that .** i
1c6e4 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
1c6e5 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b r equal to the k
1c6e6 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 ey value. If the
1c6e7 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 re are no record
1c6e8 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 s .** greater th
1c6e9 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
1c6ea 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 he key and P2 is
1c6eb 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 not zero, then
1c6ec 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a jump to P2..**.*
1c6ed 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1c6ee 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 d, NotFound, Dis
1c6ef 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 tinct, SeekLt, S
1c6f0 65 65 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f eekGt, SeekLe.*/
1c6f1 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b ./* Opcode: Seek
1c6f2 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a Gt P1 P2 P3 P4 *
1c6f3 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 .**.** If cursor
1c6f4 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e P1 refers to an
1c6f5 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 SQL table (B-Tr
1c6f6 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 ee that uses int
1c6f7 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 eger keys), .**
1c6f8 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e use the value in
1c6f9 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1c6fa 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 a key. If cursor
1c6fb 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1c6fc 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1c6fd 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1c6fe 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1c6ff 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1c700 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1c701 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1c702 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1c703 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1c704 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1c705 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 it points to t
1c706 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 he smallest entr
1c707 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 y that .** is gr
1c708 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b eater than the k
1c709 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 ey value. If the
1c70a 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 re are no record
1c70b 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a s greater than .
1c70c 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 ** the key and P
1c70d 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 2 is not zero, t
1c70e 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a hen jump to P2..
1c70f 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1c710 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c Found, NotFound,
1c711 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c Distinct, SeekL
1c712 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c t, SeekGe, SeekL
1c713 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 e.*/./* Opcode:
1c714 53 65 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 SeekLt P1 P2 P3
1c715 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 P4 * .**.** If c
1c716 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1c717 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 to an SQL table
1c718 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 (B-Tree that use
1c719 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c s integer keys),
1c71a 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c .** use the val
1c71b 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1c71c 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 3 as a key. If c
1c71d 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1c71e 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e .** to an SQL in
1c71f 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 dex, then P3 is
1c720 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 the first in an
1c721 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 array of P4 regi
1c722 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 sters .** that a
1c723 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e re used as an un
1c724 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
1c725 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 . .**.** Reposit
1c726 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f ion cursor P1 so
1c727 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 that it points
1c728 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 to the largest
1c729 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 entry that .** i
1c72a 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 s less than the
1c72b 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 key value. If th
1c72c 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 ere are no recor
1c72d 64 73 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a ds less than .**
1c72e 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 the key and P2
1c72f 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
1c730 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1c731 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1c732 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 und, NotFound, D
1c733 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c istinct, SeekGt,
1c734 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a SeekGe, SeekLe.
1c735 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 */./* Opcode: Se
1c736 65 6b 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 ekLe P1 P2 P3 P4
1c737 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 *.**.** If curs
1c738 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 or P1 refers to
1c739 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d an SQL table (B-
1c73a 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 Tree that uses i
1c73b 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a nteger keys), .*
1c73c 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 * use the value
1c73d 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 in register P3 a
1c73e 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 s a key. If curs
1c73f 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a or P1 refers .**
1c740 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 to an SQL index
1c741 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 , then P3 is the
1c742 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 first in an arr
1c743 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 ay of P4 registe
1c744 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 rs .** that are
1c745 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 used as an unpac
1c746 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a ked index key. .
1c747 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e **.** Reposition
1c748 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 cursor P1 so th
1c749 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 at it points to
1c74a 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 the largest entr
1c74b 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 y that .** is le
1c74c 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
1c74d 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 to the key valu
1c74e 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 e. If there are
1c74f 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c no records .** l
1c750 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
1c751 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 l to the key and
1c752 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c P2 is not zero,
1c753 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 then jump to P2
1c754 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1c755 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e : Found, NotFoun
1c756 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 d, Distinct, See
1c757 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 kGt, SeekGe, See
1c758 6b 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 kLt.*/.case OP_S
1c759 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f eekLt: /
1c75a 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 * jump, in3 */.c
1c75b 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 ase OP_SeekLe:
1c75c 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1c75d 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 in3 */.case OP_S
1c75e 65 65 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f eekGe: /
1c75f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 * jump, in3 */.c
1c760 61 73 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b ase OP_SeekGt: {
1c761 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1c762 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d in3 */. int i =
1c763 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 pOp->p1;. Vdbe
1c764 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 Cursor *pC;.. a
1c765 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1c766 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1c767 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1c768 21 3d 30 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d !=0 );. pC = p-
1c769 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 >apCsr[i];. ass
1c76a 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 ert( pC!=0 );.
1c76b 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 if( pC->pCursor!
1c76c 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 =0 ){. int re
1c76d 73 2c 20 6f 63 3b 0a 20 20 20 20 6f 63 20 3d 20 s, oc;. oc =
1c76e 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 pOp->opcode;.
1c76f 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 pC->nullRow = 0
1c770 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 ;. if( pC->is
1c771 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 Table ){. i
1c772 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 64 iKey; /*
1c773 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 The rowid we ar
1c774 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a e to seek to */.
1c775 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e . /* The in
1c776 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20 put value in P3
1c777 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20 might be of any
1c778 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 type: integer, r
1c779 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 eal, string,.
1c77a 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e ** blob, or N
1c77b 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65 ULL. But it nee
1c77c 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 ds to be an inte
1c77d 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61 ger before we ca
1c77e 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 n do. ** th
1c77f 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 e seek, so cover
1c780 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 61 t it. */. a
1c781 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e pplyNumericAffin
1c782 69 74 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 ity(pIn3);.
1c783 20 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 iKey = sqlite3V
1c784 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 dbeIntValue(pIn3
1c785 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 );. pC->row
1c786 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a idIsValid = 0;..
1c787 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
1c788 50 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e P3 value could n
1c789 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 ot be converted
1c78a 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 into an integer
1c78b 77 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a without. **
1c78c 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 loss of informa
1c78d 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69 tion, then speci
1c78e 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 al processing is
1c78f 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a required... */.
1c790 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d if( (pIn3-
1c791 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1c792 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1c793 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 if( (pIn3->flags
1c794 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 & MEM_Real)==0
1c795 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1c796 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 If the P3 value
1c797 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 cannot be conver
1c798 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e ted into any kin
1c799 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20 d of a number,.
1c79a 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e ** then
1c79b 20 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74 the seek is not
1c79c 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75 possible, so ju
1c79d 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 mp to P2 */.
1c79e 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1c79f 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 p2 - 1;.
1c7a0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1c7a1 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 }. /* If
1c7a2 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 we reach this p
1c7a3 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50 oint, then the P
1c7a4 33 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 3 value must be
1c7a5 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20 a floating.
1c7a6 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62 ** point numb
1c7a7 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 er. */. a
1c7a8 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c ssert( (pIn3->fl
1c7a9 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21 ags & MEM_Real)!
1c7aa 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 =0 );.. i
1c7ab 66 28 20 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 f( iKey==SMALLES
1c7ac 54 5f 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33 T_INT64 && (pIn3
1c7ad 2d 3e 72 3c 28 64 6f 75 62 6c 65 29 69 4b 65 79 ->r<(double)iKey
1c7ae 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 || pIn3->r>0) )
1c7af 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
1c7b0 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 74 he P3 value is t
1c7b1 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 o large in magni
1c7b2 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 tude to be expre
1c7b3 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 ssed as an.
1c7b4 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e ** integer.
1c7b5 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 */. re
1c7b6 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 s = 1;.
1c7b7 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29 if( pIn3->r<0 )
1c7b8 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 {. if
1c7b9 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 ( oc==OP_SeekGt
1c7ba 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 || oc==OP_SeekGe
1c7bb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1c7bc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1c7bd 72 65 65 46 69 72 73 74 28 70 43 2d 3e 70 43 75 reeFirst(pC->pCu
1c7be 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 rsor, &res);.
1c7bf 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
1c7c0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 c!=SQLITE_OK ) g
1c7c1 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1c7c2 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 _error;.
1c7c3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1c7c4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1c7c5 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 if( oc==OP_Se
1c7c6 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 ekLt || oc==OP_S
1c7c7 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 eekLe ){.
1c7c8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1c7c9 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d te3BtreeLast(pC-
1c7ca 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b >pCursor, &res);
1c7cb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 . i
1c7cc 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1c7cd 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1c7ce 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1c7cf 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1c7d0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1c7d1 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 if( res ){.
1c7d2 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d pc = pOp-
1c7d3 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 >p2 - 1;.
1c7d4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 }. b
1c7d5 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 reak;. }e
1c7d6 6c 73 65 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 lse if( oc==OP_S
1c7d7 65 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f eekLt || oc==OP_
1c7d8 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 SeekGe ){.
1c7d9 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 63 /* Use the c
1c7da 65 69 6c 69 6e 67 28 29 20 66 75 6e 63 74 69 6f eiling() functio
1c7db 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 n to convert rea
1c7dc 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 l->int */.
1c7dd 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 if( pIn3->r
1c7de 3e 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 > (double)iKey )
1c7df 20 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20 iKey++;.
1c7e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1c7e1 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f /* Use the flo
1c7e2 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f or() function to
1c7e3 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 convert real->i
1c7e4 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 nt */.
1c7e5 61 73 73 65 72 74 28 20 6f 63 3d 3d 4f 50 5f 53 assert( oc==OP_S
1c7e6 65 65 6b 4c 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f eekLe || oc==OP_
1c7e7 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 SeekGt );.
1c7e8 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 if( pIn3->r
1c7e9 3c 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29 < (double)iKey )
1c7ea 20 69 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20 iKey--;.
1c7eb 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 }. } .
1c7ec 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1c7ed 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1c7ee 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 d(pC->pCursor, 0
1c7ef 2c 20 28 75 36 34 29 69 4b 65 79 2c 20 30 2c 20 , (u64)iKey, 0,
1c7f0 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 &res);. if(
1c7f1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1c7f2 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 {. goto a
1c7f3 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1c7f4 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 r;. }.
1c7f5 20 69 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 if( res==0 ){.
1c7f6 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 pC->rowid
1c7f7 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 IsValid = 1;.
1c7f8 20 20 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77 pC->lastRow
1c7f9 69 64 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 20 id = iKey;.
1c7fa 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1c7fb 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f UnpackedReco
1c7fc 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 rd r;. int
1c7fd 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 nField = pOp->p4
1c7fe 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 .i;. assert
1c7ff 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1c800 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 4_INT32 );.
1c801 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64 3e assert( nField>
1c802 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b 65 0 );. r.pKe
1c803 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 yInfo = pC->pKey
1c804 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e 46 Info;. r.nF
1c805 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 46 69 65 ield = (u16)nFie
1c806 6c 64 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 63 ld;. if( oc
1c807 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 6f ==OP_SeekGt || o
1c808 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a c==OP_SeekLe ){.
1c809 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 r.flags
1c80a 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b = UNPACKED_INCRK
1c80b 45 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b EY;. }else{
1c80c 0a 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 . r.flags
1c80d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
1c80e 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d r.aMem = &p-
1c80f 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1c810 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1c811 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1c812 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f acked(pC->pCurso
1c813 72 2c 20 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 r, &r, 0, 0, &re
1c814 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 s);. if( rc
1c815 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1c816 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
1c817 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1c818 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 }. pC
1c819 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1c81a 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 0;. }. pC
1c81b 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1c81c 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 = 0;. pC->ca
1c81d 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1c81e 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 E_STALE;.#ifdef
1c81f 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 SQLITE_TEST.
1c820 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 sqlite3_search_c
1c821 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
1c822 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65 if( oc==OP_Se
1c823 65 6b 47 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 ekGe || oc==OP_S
1c824 65 65 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69 eekGt ){. i
1c825 66 28 20 72 65 73 3c 30 20 7c 7c 20 28 72 65 73 f( res<0 || (res
1c826 3d 3d 30 20 26 26 20 6f 63 3d 3d 4f 50 5f 53 65 ==0 && oc==OP_Se
1c827 65 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20 ekGt) ){.
1c828 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1c829 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73 eeNext(pC->pCurs
1c82a 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 or, &res);.
1c82b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1c82c 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 E_OK ) goto abor
1c82d 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1c82e 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 pC->rowi
1c82f 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1c830 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1c831 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 res = 0;.
1c832 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1c833 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d assert( oc=
1c834 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63 =OP_SeekLt || oc
1c835 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 ==OP_SeekLe );.
1c836 20 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 7c if( res>0 |
1c837 7c 20 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d | (res==0 && oc=
1c838 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20 =OP_SeekLt) ){.
1c839 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1c83a 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
1c83b 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 (pC->pCursor, &r
1c83c 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 es);. if(
1c83d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1c83e 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1c83f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1c840 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c pC->rowidIsVal
1c841 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 id = 0;. }e
1c842 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
1c843 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 res might be neg
1c844 61 74 69 76 65 20 62 65 63 61 75 73 65 20 74 68 ative because th
1c845 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 e table is empty
1c846 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 . Check to.
1c847 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 ** see if th
1c848 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a is is the case..
1c849 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1c84a 20 20 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33 res = sqlite3
1c84b 42 74 72 65 65 45 6f 66 28 70 43 2d 3e 70 43 75 BtreeEof(pC->pCu
1c84c 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 rsor);. }.
1c84d 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1c84e 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 pOp->p2>0 );.
1c84f 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
1c850 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1c851 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c - 1;. }. }el
1c852 73 65 20 69 66 28 20 21 70 43 2d 3e 70 73 65 75 se if( !pC->pseu
1c853 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f doTable ){. /
1c854 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 * This happens w
1c855 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 hen attempting t
1c856 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 o open the sqlit
1c857 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a e3_master table.
1c858 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 ** for read
1c859 61 63 63 65 73 73 20 72 65 74 75 72 6e 73 20 53 access returns S
1c85a 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 QLITE_EMPTY. In
1c85b 74 68 69 73 20 63 61 73 65 20 61 6c 77 61 79 73 this case always
1c85c 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 . ** take the
1c85d 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 74 68 65 jump (since the
1c85e 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 re are no record
1c85f 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 29 2e s in the table).
1c860 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d . */. pc =
1c861 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1c862 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1c863 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 Opcode: Seek P1
1c864 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1c865 50 31 20 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 P1 is an open ta
1c866 62 6c 65 20 63 75 72 73 6f 72 20 61 6e 64 20 50 ble cursor and P
1c867 32 20 69 73 20 61 20 72 6f 77 69 64 20 69 6e 74 2 is a rowid int
1c868 65 67 65 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a eger. Arrange.*
1c869 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 * for P1 to move
1c86a 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e so that it poin
1c86b 74 73 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 ts to the rowid
1c86c 67 69 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a given by P2..**.
1c86d 2a 2a 20 54 68 69 73 20 69 73 20 61 63 74 75 61 ** This is actua
1c86e 6c 6c 79 20 61 20 64 65 66 65 72 72 65 64 20 73 lly a deferred s
1c86f 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 eek. Nothing ac
1c870 74 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 tually happens u
1c871 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 ntil.** the curs
1c872 6f 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 or is used to re
1c873 61 64 20 61 20 72 65 63 6f 72 64 2e 20 20 54 68 ad a record. Th
1c874 61 74 20 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 at way, if no re
1c875 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f ads.** occur, no
1c876 20 75 6e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f unnecessary I/O
1c877 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 happens..*/.cas
1c878 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 e OP_Seek: {
1c879 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20 /* in2 */. int
1c87a 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
1c87b 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a dbeCursor *pC;..
1c87c 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1c87d 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1c87e 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 ;. pC = p->apCs
1c87f 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 r[i];. assert(
1c880 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 pC!=0 );. if( p
1c881 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b C->pCursor!=0 ){
1c882 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1c883 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1c884 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b pC->nullRow = 0;
1c885 0a 20 20 20 20 70 43 2d 3e 6d 6f 76 65 74 6f 54 . pC->movetoT
1c886 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56 arget = sqlite3V
1c887 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 dbeIntValue(pIn2
1c888 29 3b 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 );. pC->rowid
1c889 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 IsValid = 0;.
1c88a 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pC->deferredMov
1c88b 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 eto = 1;. }. b
1c88c 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f reak;.}. ../* O
1c88d 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 pcode: Found P1
1c88e 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1c88f 52 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 Register P3 hold
1c890 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 s a blob constru
1c891 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f cted by MakeReco
1c892 72 64 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e rd. P1 is an in
1c893 64 65 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e dex..** If an en
1c894 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 73 try that matches
1c895 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1c896 67 69 73 74 65 72 20 70 33 20 65 78 69 73 74 73 gister p3 exists
1c897 20 69 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a in P1 then.** j
1c898 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74 ump to P2. If t
1c899 68 65 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73 he P3 value does
1c89a 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65 not match any e
1c89b 6e 74 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68 ntry in P1.** th
1c89c 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 en fall thru. T
1c89d 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 he P1 cursor is
1c89e 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
1c89f 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e the matching en
1c8a0 74 72 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69 try.** if it exi
1c8a1 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 sts..**.** This
1c8a2 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75 instruction is u
1c8a3 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
1c8a4 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 the IN operator
1c8a5 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65 where the.** le
1c8a6 66 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 ft-hand side is
1c8a7 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
1c8a8 6e 74 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61 nt. P1 may be a
1c8a9 20 74 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20 true index, or
1c8aa 69 74 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74 it.** may be a t
1c8ab 65 6d 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74 emporary index t
1c8ac 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65 hat holds the re
1c8ad 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c sults of the SEL
1c8ae 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ECT.** statement
1c8af 2e 20 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 . This instruc
1c8b0 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 tion is also use
1c8b1 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 d to implement t
1c8b2 68 65 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b he.** DISTINCT k
1c8b3 65 79 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54 eyword in SELECT
1c8b4 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
1c8b5 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 ** This instruct
1c8b6 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e ion checks if in
1c8b7 64 65 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 dex P1 contains
1c8b8 61 20 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69 a record for whi
1c8b9 63 68 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 ch .** the first
1c8ba 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 N serialized va
1c8bb 6c 75 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74 lues exactly mat
1c8bc 63 68 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69 ch the N seriali
1c8bd 7a 65 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e zed values.** in
1c8be 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72 the record in r
1c8bf 65 67 69 73 74 65 72 20 50 33 2c 20 77 68 65 72 egister P3, wher
1c8c0 65 20 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c e N is the total
1c8c1 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 number of value
1c8c2 73 20 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72 s in.** the P3 r
1c8c3 65 63 6f 72 64 20 28 74 68 65 20 50 33 20 72 65 ecord (the P3 re
1c8c4 63 6f 72 64 20 69 73 20 61 20 70 72 65 66 69 78 cord is a prefix
1c8c5 20 6f 66 20 74 68 65 20 50 31 20 72 65 63 6f 72 of the P1 recor
1c8c6 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 d). .**.** See a
1c8c7 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49 lso: NotFound, I
1c8c8 73 55 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73 sUnique, NotExis
1c8c9 74 73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a ts.*/./* Opcode:
1c8ca 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20 NotFound P1 P2
1c8cb 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 P3 * *.**.** Reg
1c8cc 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 ister P3 holds a
1c8cd 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 blob constructe
1c8ce 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e d by MakeRecord.
1c8cf 20 20 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e P1 is.** an in
1c8d0 64 65 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72 dex. If no entr
1c8d1 79 20 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 y exists in P1 t
1c8d2 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 hat matches the
1c8d3 62 6c 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a blob then jump.*
1c8d4 2a 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20 * to P2. If an
1c8d5 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 73 74 entry does exist
1c8d6 69 6e 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 ing, fall throug
1c8d7 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 h. The cursor i
1c8d8 73 20 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 s left.** pointi
1c8d9 6e 67 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20 ng to the entry
1c8da 74 68 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a that matches..**
1c8db 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1c8dc 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 und, NotExists,
1c8dd 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 IsUnique.*/.case
1c8de 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 OP_NotFound:
1c8df 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 /* jump, in3
1c8e0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e */.case OP_Foun
1c8e1 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a d: { /* j
1c8e2 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e ump, in3 */. in
1c8e3 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 t i = pOp->p1;.
1c8e4 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73 int alreadyExis
1c8e5 74 73 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75 ts = 0;. VdbeCu
1c8e6 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1c8e7 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1c8e8 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 >nCursor );. as
1c8e9 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 sert( p->apCsr[i
1c8ea 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 ]!=0 );. if( (p
1c8eb 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 C = p->apCsr[i])
1c8ec 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a ->pCursor!=0 ){.
1c8ed 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1c8ee 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1c8ef 2a 70 49 64 78 4b 65 79 3b 0a 0a 20 20 20 20 61 *pIdxKey;.. a
1c8f0 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 ssert( pC->isTab
1c8f1 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 le==0 );. ass
1c8f2 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 ert( pIn3->flags
1c8f3 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 & MEM_Blob );.
1c8f4 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c pIdxKey = sql
1c8f5 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e ite3VdbeRecordUn
1c8f6 70 61 63 6b 28 70 43 2d 3e 70 4b 65 79 49 6e 66 pack(pC->pKeyInf
1c8f7 6f 2c 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 o, pIn3->n, pIn3
1c8f8 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->z,.
1c8f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c8fa 20 20 20 20 20 20 20 20 20 20 20 61 54 65 6d 70 aTemp
1c8fb 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54 65 6d Rec, sizeof(aTem
1c8fc 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20 pRec));. if(
1c8fd 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 pIdxKey==0 ){.
1c8fe 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1c8ff 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1c900 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 Op->opcode==OP_F
1c901 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 49 ound ){. pI
1c902 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 dxKey->flags |=
1c903 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f UNPACKED_PREFIX_
1c904 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20 20 MATCH;. }.
1c905 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1c906 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
1c907 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 70 49 (pC->pCursor, pI
1c908 64 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 dxKey, 0, 0, &re
1c909 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 s);. sqlite3V
1c90a 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 dbeDeleteUnpacke
1c90b 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 dRecord(pIdxKey)
1c90c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1c90d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1c90e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1c90f 20 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 alreadyExists
1c910 3d 20 28 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20 = (res==0);.
1c911 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1c912 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e to = 0;. pC->
1c913 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1c914 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 CHE_STALE;. }.
1c915 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1c916 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 ==OP_Found ){.
1c917 20 20 69 66 28 20 61 6c 72 65 61 64 79 45 78 69 if( alreadyExi
1c918 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e sts ) pc = pOp->
1c919 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b p2 - 1;. }else{
1c91a 0a 20 20 20 20 69 66 28 20 21 61 6c 72 65 61 64 . if( !alread
1c91b 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 yExists ) pc = p
1c91c 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a Op->p2 - 1;. }.
1c91d 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c91e 70 63 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 pcode: IsUnique
1c91f 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1c920 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 .** Cursor P1 is
1c921 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 open on an inde
1c922 78 2e 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f x. So it has no
1c923 20 64 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65 data and its ke
1c924 79 20 63 6f 6e 73 69 73 74 73 20 0a 2a 2a 20 6f y consists .** o
1c925 66 20 61 20 72 65 63 6f 72 64 20 67 65 6e 65 72 f a record gener
1c926 61 74 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 ated by OP_MakeR
1c927 65 63 6f 72 64 20 77 68 65 72 65 20 74 68 65 20 ecord where the
1c928 6c 61 73 74 20 66 69 65 6c 64 20 69 73 20 74 68 last field is th
1c929 65 20 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 e .** rowid of t
1c92a 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 he entry that th
1c92b 65 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 e index refers t
1c92c 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20 o..**.** The P3
1c92d 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e register contain
1c92e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63 s an integer rec
1c92f 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43 61 6c 6c ord number. Call
1c930 20 74 68 69 73 20 72 65 63 6f 72 64 20 0a 2a 2a this record .**
1c931 20 6e 75 6d 62 65 72 20 52 2e 20 52 65 67 69 73 number R. Regis
1c932 74 65 72 20 50 34 20 69 73 20 74 68 65 20 66 69 ter P4 is the fi
1c933 72 73 74 20 69 6e 20 61 20 73 65 74 20 6f 66 20 rst in a set of
1c934 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20 72 65 67 N contiguous reg
1c935 69 73 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 6d isters.** that m
1c936 61 6b 65 20 75 70 20 61 6e 20 75 6e 70 61 63 6b ake up an unpack
1c937 65 64 20 69 6e 64 65 78 20 6b 65 79 20 74 68 61 ed index key tha
1c938 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 t can be used wi
1c939 74 68 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a th cursor P1..**
1c93a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 20 The value of N
1c93b 63 61 6e 20 62 65 20 69 6e 66 65 72 72 65 64 20 can be inferred
1c93c 66 72 6f 6d 20 74 68 65 20 63 75 72 73 6f 72 2e from the cursor.
1c93d 20 4e 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 N includes the
1c93e 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75 65 20 61 rowid.** value a
1c93f 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 ppended to the e
1c940 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 nd of the index
1c941 72 65 63 6f 72 64 2e 20 54 68 69 73 20 72 6f 77 record. This row
1c942 69 64 20 76 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 id value may.**
1c943 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 or may not be th
1c944 65 20 73 61 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a e same as R..**.
1c945 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 ** If any of the
1c946 20 4e 20 72 65 67 69 73 74 65 72 73 20 62 65 67 N registers beg
1c947 69 6e 6e 69 6e 67 20 77 69 74 68 20 72 65 67 69 inning with regi
1c948 73 74 65 72 20 50 34 20 63 6f 6e 74 61 69 6e 73 ster P4 contains
1c949 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 a NULL.** value
1c94a 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 , jump immediate
1c94b 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ly to P2..**.**
1c94c 4f 74 68 65 72 77 69 73 65 2c 20 74 68 69 73 20 Otherwise, this
1c94d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 instruction chec
1c94e 6b 73 20 69 66 20 63 75 72 73 6f 72 20 50 31 20 ks if cursor P1
1c94f 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 contains an entr
1c950 79 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 66 y.** where the f
1c951 69 72 73 74 20 28 4e 2d 31 29 20 66 69 65 6c 64 irst (N-1) field
1c952 73 20 6d 61 74 63 68 20 62 75 74 20 74 68 65 20 s match but the
1c953 72 6f 77 69 64 20 76 61 6c 75 65 20 61 74 20 74 rowid value at t
1c954 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 he end.** of the
1c955 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 index entry is
1c956 6e 6f 74 20 52 2e 20 49 66 20 74 68 65 72 65 20 not R. If there
1c957 69 73 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 is no such entry
1c958 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a , control jumps.
1c959 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f ** to instructio
1c95a 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c n P2. Otherwise,
1c95b 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 the rowid of th
1c95c 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 69 6e e conflicting in
1c95d 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20 dex.** entry is
1c95e 63 6f 70 69 65 64 20 74 6f 20 72 65 67 69 73 74 copied to regist
1c95f 65 72 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f er P3 and contro
1c960 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20 l falls through
1c961 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 to the next.** i
1c962 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1c963 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 * See also: NotF
1c964 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c ound, NotExists,
1c965 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f Found.*/.case O
1c966 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 P_IsUnique: {
1c967 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1c968 33 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20 3 */. u16 ii;.
1c969 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1c96a 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1c96b 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c rsr;. u16 nFiel
1c96c 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d d;. Mem *aMem =
1c96d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1c96e 34 2e 69 5d 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 4.i];.. /* Asse
1c96f 72 74 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 rt that the valu
1c970 65 73 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 es of parameters
1c971 20 50 31 20 61 6e 64 20 50 34 20 61 72 65 20 69 P1 and P4 are i
1c972 6e 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 n range. */. as
1c973 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 sert( pOp->p4typ
1c974 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 e==P4_INT32 );.
1c975 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1c976 2e 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e .i>0 && pOp->p4.
1c977 69 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 i<=p->nMem );.
1c978 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1c979 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d =0 && pOp->p1<p-
1c97a 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f >nCursor );.. /
1c97b 2a 20 46 69 6e 64 20 74 68 65 20 69 6e 64 65 78 * Find the index
1c97c 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 70 43 cursor. */. pC
1c97d 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 x = p->apCsr[pOp
1c97e 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1c97f 20 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f pCx->deferredMo
1c980 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 70 43 78 veto==0 );. pCx
1c981 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 30 ->seekResult = 0
1c982 3b 0a 20 20 70 43 78 2d 3e 63 61 63 68 65 53 74 ;. pCx->cacheSt
1c983 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1c984 4c 45 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43 LE;. pCrsr = pC
1c985 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a 20 20 2f x->pCursor;.. /
1c986 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 * If any of the
1c987 76 61 6c 75 65 73 20 61 72 65 20 4e 55 4c 4c 2c values are NULL,
1c988 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 take the jump.
1c989 2a 2f 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 */. nField = pC
1c98a 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 x->pKeyInfo->nFi
1c98b 65 6c 64 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b eld;. for(ii=0;
1c98c 20 69 69 3c 6e 46 69 65 6c 64 3b 20 69 69 2b 2b ii<nField; ii++
1c98d 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 65 6d 5b ){. if( aMem[
1c98e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ii].flags & MEM_
1c98f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 63 Null ){. pc
1c990 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1c991 20 20 20 20 20 20 70 43 72 73 72 20 3d 20 30 3b pCrsr = 0;
1c992 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1c993 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
1c994 28 20 28 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 2e ( (aMem[nField].
1c995 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1c996 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 )==0 );.. if( p
1c997 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 55 Crsr!=0 ){. U
1c998 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b npackedRecord r;
1c999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c99a 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 /* B-Tree inde
1c99b 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a x search key */.
1c99c 20 20 20 20 69 36 34 20 52 3b 20 20 20 20 20 20 i64 R;
1c99d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c99e 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 /* Rowid
1c99f 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1c9a0 65 72 20 50 33 20 2a 2f 0a 0a 20 20 20 20 2f 2a er P3 */.. /*
1c9a1 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e Populate the in
1c9a2 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20 dex search key.
1c9a3 2a 2f 0a 20 20 20 20 72 2e 70 4b 65 79 49 6e 66 */. r.pKeyInf
1c9a4 6f 20 3d 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 o = pCx->pKeyInf
1c9a5 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 20 o;. r.nField
1c9a6 3d 20 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20 = nField + 1;.
1c9a7 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 r.flags = UNPA
1c9a8 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 CKED_PREFIX_SEAR
1c9a9 43 48 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d CH;. r.aMem =
1c9aa 20 61 4d 65 6d 3b 0a 0a 20 20 20 20 2f 2a 20 45 aMem;.. /* E
1c9ab 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 xtract the value
1c9ac 20 6f 66 20 52 20 66 72 6f 6d 20 72 65 67 69 73 of R from regis
1c9ad 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20 20 73 ter P3. */. s
1c9ae 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1c9af 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 egerify(pIn3);.
1c9b0 20 20 20 52 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 R = pIn3->u.i
1c9b1 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 ;.. /* Search
1c9b2 20 74 68 65 20 42 2d 54 72 65 65 20 69 6e 64 65 the B-Tree inde
1c9b3 78 2e 20 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 x. If no conflic
1c9b4 74 69 6e 67 20 72 65 63 6f 72 64 20 69 73 20 66 ting record is f
1c9b5 6f 75 6e 64 2c 20 6a 75 6d 70 0a 20 20 20 20 2a ound, jump. *
1c9b6 2a 20 74 6f 20 50 32 2e 20 4f 74 68 65 72 77 69 * to P2. Otherwi
1c9b7 73 65 2c 20 63 6f 70 79 20 74 68 65 20 72 6f 77 se, copy the row
1c9b8 69 64 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 id of the confli
1c9b9 63 74 69 6e 67 20 72 65 63 6f 72 64 20 74 6f 0a cting record to.
1c9ba 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 ** register
1c9bb 50 33 20 61 6e 64 20 66 61 6c 6c 20 74 68 72 6f P3 and fall thro
1c9bc 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1c9bd 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 2a 2f instruction. */
1c9be 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1c9bf 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
1c9c0 63 6b 65 64 28 70 43 72 73 72 2c 20 26 72 2c 20 cked(pCrsr, &r,
1c9c1 30 2c 20 30 2c 20 26 70 43 78 2d 3e 73 65 65 6b 0, 0, &pCx->seek
1c9c2 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 69 66 28 Result);. if(
1c9c3 20 28 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 41 (r.flags & UNPA
1c9c4 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 CKED_PREFIX_SEAR
1c9c5 43 48 29 20 7c 7c 20 72 2e 72 6f 77 69 64 3d 3d CH) || r.rowid==
1c9c6 52 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 R ){. pc =
1c9c7 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1c9c8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 }else{. pI
1c9c9 6e 33 2d 3e 75 2e 69 20 3d 20 72 2e 72 6f 77 69 n3->u.i = r.rowi
1c9ca 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 d;. }. }. b
1c9cb 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c9cc 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31 de: NotExists P1
1c9cd 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1c9ce 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 Use the content
1c9cf 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 of register P3
1c9d0 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 as a integer key
1c9d1 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a . If a record .
1c9d2 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 ** with that key
1c9d3 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
1c9d4 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 in table of P1,
1c9d5 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1c9d6 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f .** If the reco
1c9d7 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 rd does exist, t
1c9d8 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 hen fall thru.
1c9d9 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
1c9da 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 ft .** pointing
1c9db 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 to the record if
1c9dc 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a it exists..**.*
1c9dd 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 * The difference
1c9de 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70 between this op
1c9df 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 eration and NotF
1c9e0 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69 ound is that thi
1c9e1 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 s.** operation a
1c9e2 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69 ssumes the key i
1c9e3 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 s an integer and
1c9e4 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61 that P1 is a ta
1c9e5 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e ble whereas.** N
1c9e6 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 otFound assumes
1c9e7 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f key is a blob co
1c9e8 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d nstructed from M
1c9e9 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a akeRecord and.**
1c9ea 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e P1 is an index.
1c9eb 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1c9ec 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 Found, NotFound
1c9ed 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 , IsUnique.*/.ca
1c9ee 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a se OP_NotExists:
1c9ef 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1c9f0 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 p, in3 */. int
1c9f1 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
1c9f2 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1c9f3 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1c9f4 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 ;. assert( i>=0
1c9f5 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 && i<p->nCursor
1c9f6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1c9f7 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a >apCsr[i]!=0 );.
1c9f8 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 if( (pCrsr = (
1c9f9 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1c9fa 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 )->pCursor)!=0 )
1c9fb 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20 {. int res =
1c9fc 30 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 79 3b 0;. u64 iKey;
1c9fd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e . assert( pIn
1c9fe 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 3->flags & MEM_I
1c9ff 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 nt );. assert
1ca00 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e 69 ( p->apCsr[i]->i
1ca01 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 69 4b sTable );. iK
1ca02 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 49 ey = intToKey(pI
1ca03 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 72 63 n3->u.i);. rc
1ca04 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
1ca05 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
1ca06 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20 30 2c rsr, 0, iKey, 0,
1ca07 20 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e &res);. pC->
1ca08 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 lastRowid = pIn3
1ca09 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e 72 ->u.i;. pC->r
1ca0a 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 owidIsValid = re
1ca0b 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70 s==0 ?1:0;. p
1ca0c 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a C->nullRow = 0;.
1ca0d 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 pC->cacheSta
1ca0e 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1ca0f 45 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 E;. pC->defer
1ca10 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 redMoveto = 0;.
1ca11 20 20 20 69 66 28 20 72 65 73 21 3d 30 20 29 7b if( res!=0 ){
1ca12 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1ca13 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 61 >p2 - 1;. a
1ca14 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77 69 64 ssert( pC->rowid
1ca15 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 IsValid==0 );.
1ca16 20 20 7d 0a 20 20 20 20 70 43 2d 3e 73 65 65 6b }. pC->seek
1ca17 52 65 73 75 6c 74 20 3d 20 72 65 73 3b 0a 20 20 Result = res;.
1ca18 7d 65 6c 73 65 20 69 66 28 20 21 70 43 2d 3e 70 }else if( !pC->p
1ca19 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 seudoTable ){.
1ca1a 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
1ca1b 73 20 77 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 s when an attemp
1ca1c 74 20 74 6f 20 6f 70 65 6e 20 61 20 72 65 61 64 t to open a read
1ca1d 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 0a cursor on the .
1ca1e 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 ** sqlite_ma
1ca1f 73 74 65 72 20 74 61 62 6c 65 20 72 65 74 75 72 ster table retur
1ca20 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e ns SQLITE_EMPTY.
1ca21 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
1ca22 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 rt( pC->isTable
1ca23 29 3b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d );. pc = pOp-
1ca24 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 >p2 - 1;. ass
1ca25 65 72 74 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 ert( pC->rowidIs
1ca26 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20 Valid==0 );.
1ca27 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d pC->seekResult =
1ca28 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0;. }. break;
1ca29 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1ca2a 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20 equence P1 P2 *
1ca2b 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74 * *.**.** Find t
1ca2c 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c he next availabl
1ca2d 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 e sequence numbe
1ca2e 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e r for cursor P1.
1ca2f 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65 .** Write the se
1ca30 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e quence number in
1ca31 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1ca32 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20 ** The sequence
1ca33 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75 number on the cu
1ca34 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e rsor is incremen
1ca35 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a ted after this.*
1ca36 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 * instruction.
1ca37 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75 .*/.case OP_Sequ
1ca38 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20 ence: {
1ca39 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1ca3a 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 ease */. int i
1ca3b 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 = pOp->p1;. ass
1ca3c 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
1ca3d 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1ca3e 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b ssert( p->apCsr[
1ca3f 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d i]!=0 );. pOut-
1ca40 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b >u.i = p->apCsr[
1ca41 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a i]->seqCount++;.
1ca42 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1ca43 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1ca44 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a . break;.}.../*
1ca45 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 Opcode: NewRowi
1ca46 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1ca47 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 *.** Get a new i
1ca48 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 nteger record nu
1ca49 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 mber (a.k.a "row
1ca4a 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 id") used as the
1ca4b 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e key to a table.
1ca4c 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e .** The record n
1ca4d 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 umber is not pre
1ca4e 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 viously used as
1ca4f 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 a key in the dat
1ca50 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 abase.** table t
1ca51 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f hat cursor P1 po
1ca52 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 ints to. The ne
1ca53 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 w record number
1ca54 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 is written.** wr
1ca55 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 itten to registe
1ca56 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 r P2..**.** If P
1ca57 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 3>0 then P3 is a
1ca58 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68 register that h
1ca59 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 olds the largest
1ca5a 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 67 previously.** g
1ca5b 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 enerated record
1ca5c 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77 20 number. No new
1ca5d 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 record numbers a
1ca5e 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 re allowed to be
1ca5f 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 less.** than th
1ca60 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e 20 is value. When
1ca61 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68 this value reach
1ca62 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 es its maximum,
1ca63 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a a SQLITE_FULL.**
1ca64 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 error is genera
1ca65 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65 67 ted. The P3 reg
1ca66 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64 ister is updated
1ca67 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72 61 with the genera
1ca68 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75 ted.** record nu
1ca69 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20 6d mber. This P3 m
1ca6a 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 echanism is used
1ca6b 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 to help impleme
1ca6c 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e nt the.** AUTOIN
1ca6d 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e CREMENT feature.
1ca6e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 .*/.case OP_NewR
1ca6f 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 owid: {
1ca70 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1ca71 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 ease */. int i
1ca72 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34 = pOp->p1;. i64
1ca73 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75 v = 0;. VdbeCu
1ca74 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1ca75 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1ca76 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 >nCursor );. as
1ca77 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 sert( p->apCsr[i
1ca78 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 ]!=0 );. if( (p
1ca79 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 C = p->apCsr[i])
1ca7a 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b 0a ->pCursor==0 ){.
1ca7b 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20 /* The zero
1ca7c 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 initialization a
1ca7d 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74 bove is all that
1ca7e 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 is needed */.
1ca7f 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
1ca80 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20 e next rowid or
1ca81 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64 record number (d
1ca82 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66 ifferent terms f
1ca83 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 or the same.
1ca84 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74 ** thing) is obt
1ca85 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73 ained in a two-s
1ca86 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 tep algorithm..
1ca87 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72 **. ** Fir
1ca88 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f st we attempt to
1ca89 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 find the larges
1ca8a 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 t existing rowid
1ca8b 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20 and add one.
1ca8c 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75 ** to that. Bu
1ca8d 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 74 t if the largest
1ca8e 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 existing rowid
1ca8f 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d is already the m
1ca90 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f aximum. ** po
1ca91 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20 sitive integer,
1ca92 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20 we have to fall
1ca93 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 through to the s
1ca94 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f econd. ** pro
1ca95 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72 babilistic algor
1ca96 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ithm. **.
1ca97 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c ** The second al
1ca98 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65 gorithm is to se
1ca99 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 20 lect a rowid at
1ca9a 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69 random and see i
1ca9b 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65 f. ** it alre
1ca9c 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 68 ady exists in th
1ca9d 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 e table. If it
1ca9e 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 does not exist,
1ca9f 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73 we have. ** s
1caa0 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 68 ucceeded. If th
1caa1 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64 e random rowid d
1caa2 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 65 oes exist, we se
1caa3 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20 lect a new one.
1caa4 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67 ** and try ag
1caa5 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30 20 ain, up to 1000
1caa6 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 times.. **.
1caa7 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 ** For a table
1caa8 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e 20 with less than
1caa9 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69 65 2 billion entrie
1caaa 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69 s, the probabili
1caab 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f 74 ty. ** of not
1caac 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73 65 finding a unuse
1caad 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75 74 d rowid is about
1caae 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69 73 1.0e-300. This
1caaf 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e 6f is a . ** no
1cab0 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 n-zero probabili
1cab1 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73 74 ty, but it is st
1cab2 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79 20 ill vanishingly
1cab3 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c 64 small and should
1cab4 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63 61 . ** never ca
1cab5 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20 20 use a problem.
1cab6 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d 75 You are much, mu
1cab7 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74 ch more likely t
1cab8 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a 20 o have a. **
1cab9 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72 65 hardware failure
1caba 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20 61 than for this a
1cabb 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69 6c lgorithm to fail
1cabc 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1cabd 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e 20 The analysis in
1cabe 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72 the previous par
1cabf 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20 74 agraph assumes t
1cac0 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20 67 hat you have a g
1cac1 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 ood. ** sourc
1cac2 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 e of random numb
1cac3 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72 61 ers. Is a libra
1cac4 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 ry function like
1cac5 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20 2a lrand48(). *
1cac6 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20 20 * good enough?
1cac7 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f 74 Maybe. Maybe not
1cac8 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20 6b . It's hard to k
1cac9 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65 72 now whether ther
1caca 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62 e. ** might b
1cacb 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69 73 e subtle bugs is
1cacc 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 some implementa
1cacd 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34 38 tions of lrand48
1cace 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 63 () that. ** c
1cacf 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c ould cause probl
1cad0 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75 6e ems. To avoid un
1cad1 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69 74 certainty, SQLit
1cad2 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20 0a e uses its own .
1cad3 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e 75 ** random nu
1cad4 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62 mber generator b
1cad5 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34 20 ased on the RC4
1cad6 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a algorithm.. *
1cad7 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f 6d *. ** To prom
1cad8 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66 20 ote locality of
1cad9 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72 65 reference for re
1cada 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74 73 petitive inserts
1cadb 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 , the. ** fir
1cadc 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73 20 st few attempts
1cadd 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72 61 at choosing a ra
1cade 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b 20 ndom rowid pick
1cadf 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c 69 values just a li
1cae0 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 ttle. ** larg
1cae1 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 76 er than the prev
1cae2 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 69 ious rowid. Thi
1cae3 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 6e s has been shown
1cae4 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 0a experimentally.
1cae5 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c 65 ** to double
1cae6 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 68 the speed of th
1cae7 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f 6e e COPY operation
1cae8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
1cae9 20 72 65 73 3d 30 2c 20 72 78 3d 53 51 4c 49 54 res=0, rx=SQLIT
1caea 45 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 E_OK, cnt;. i
1caeb 36 34 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 64 x;. cnt =
1caec 30 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 0;. if( (sqli
1caed 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 te3BtreeFlags(pC
1caee 2d 3e 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 ->pCursor)&(BTRE
1caef 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a E_INTKEY|BTREE_Z
1caf0 45 52 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 ERODATA)) !=.
1caf1 20 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 BTREE_INT
1caf2 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 KEY ){. rc
1caf3 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1caf4 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 _BKPT;. got
1caf5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1caf6 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rror;. }.
1caf7 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 assert( (sqlite3
1caf8 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 BtreeFlags(pC->p
1caf9 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f Cursor) & BTREE_
1cafa 49 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 INTKEY)!=0 );.
1cafb 20 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 assert( (sqlit
1cafc 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d e3BtreeFlags(pC-
1cafd 3e 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 >pCursor) & BTRE
1cafe 45 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 E_ZERODATA)==0 )
1caff 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1cb00 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 _32BIT_ROWID.#
1cb01 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 define MAX_ROWI
1cb02 44 20 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c D 0x7fffffff.#el
1cb03 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 se. /* Some c
1cb04 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 ompilers complai
1cb05 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 n about constant
1cb06 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 s of the form 0x
1cb07 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 7fffffffffffffff
1cb08 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 .. ** Others
1cb09 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 complain about 0
1cb0a 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 x7ffffffffffffff
1cb0b 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f ffLL. The follo
1cb0c 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 wing macro seems
1cb0d 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 . ** to provi
1cb0e 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 de the constant
1cb0f 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c while making all
1cb10 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 compilers happy
1cb11 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 .. */.# def
1cb12 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 ine MAX_ROWID (
1cb13 69 36 34 29 28 20 28 28 28 75 36 34 29 30 78 37 i64)( (((u64)0x7
1cb14 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 fffffff)<<32) |
1cb15 28 75 36 34 29 30 78 66 66 66 66 66 66 66 66 20 (u64)0xffffffff
1cb16 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 ).#endif.. if
1cb17 28 20 21 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d ( !pC->useRandom
1cb18 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 76 Rowid ){. v
1cb19 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
1cb1a 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 70 43 etCachedRowid(pC
1cb1b 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 ->pCursor);.
1cb1c 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 if( v==0 ){.
1cb1d 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1cb1e 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d 3e e3BtreeLast(pC->
1cb1f 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a pCursor, &res);.
1cb20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1cb21 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1cb22 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
1cb23 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1cb24 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1cb25 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
1cb26 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a 20 20 v = 1;.
1cb27 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1cb28 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 sqlite3Bt
1cb29 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70 reeKeySize(pC->p
1cb2a 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20 Cursor, &v);.
1cb2b 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54 6f v = keyTo
1cb2c 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 20 20 Int(v);.
1cb2d 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 4f 57 if( v==MAX_ROW
1cb2e 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ID ){.
1cb2f 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 pC->useRandomR
1cb30 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 owid = 1;.
1cb31 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1cb32 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 v++;.
1cb33 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1cb34 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 }. }..#ifnd
1cb35 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1cb36 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 UTOINCREMENT.
1cb37 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 if( pOp->p3 )
1cb38 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 2a 70 {. Mem *p
1cb39 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 Mem;. ass
1cb3a 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 ert( pOp->p3>0 &
1cb3b 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d & pOp->p3<=p->nM
1cb3c 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73 20 61 em ); /* P3 is a
1cb3d 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 valid memory ce
1cb3e 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d ll */. pM
1cb3f 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f em = &p->aMem[pO
1cb40 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53 54 45 p->p3];..REGISTE
1cb41 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1cb42 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 pMem);.
1cb43 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
1cb44 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a tegerify(pMem);.
1cb45 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1cb46 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d (pMem->flags & M
1cb47 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20 2f EM_Int)!=0 ); /
1cb48 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73 20 * mem(P3) holds
1cb49 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 an integer */.
1cb4a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e if( pMem->
1cb4b 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c u.i==MAX_ROWID |
1cb4c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 | pC->useRandomR
1cb4d 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 owid ){.
1cb4e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 rc = SQLITE_FU
1cb4f 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f LL;. go
1cb50 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1cb51 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d error;. }
1cb52 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 70 . if( v<p
1cb53 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 Mem->u.i+1 ){.
1cb54 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d 65 6d v = pMem
1cb55 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 ->u.i + 1;.
1cb56 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 65 }. pMe
1cb57 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 m->u.i = v;.
1cb58 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
1cb59 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 sqlite3BtreeSe
1cb5a 74 43 61 63 68 65 64 52 6f 77 69 64 28 70 43 2d tCachedRowid(pC-
1cb5b 3e 70 43 75 72 73 6f 72 2c 20 76 3c 4d 41 58 5f >pCursor, v<MAX_
1cb5c 52 4f 57 49 44 20 3f 20 76 2b 31 20 3a 20 30 29 ROWID ? v+1 : 0)
1cb5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1cb5e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 pC->useRandomRow
1cb5f 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 id ){. asse
1cb60 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 rt( pOp->p3==0 )
1cb61 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 55 4c ; /* SQLITE_FUL
1cb62 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63 63 75 L must have occu
1cb63 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 rred prior to th
1cb64 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20 3d 20 is */. v =
1cb65 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 db->priorNewRowi
1cb66 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d 20 30 d;. cnt = 0
1cb67 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 ;. do{.
1cb68 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 if( cnt==0 &
1cb69 26 20 28 76 26 30 78 66 66 66 66 66 66 29 3d 3d & (v&0xffffff)==
1cb6a 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 76 v ){. v
1cb6b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ++;. }els
1cb6c 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c e{. sql
1cb6d 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
1cb6e 73 69 7a 65 6f 66 28 76 29 2c 20 26 76 29 3b 0a sizeof(v), &v);.
1cb6f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6e if( cn
1cb70 74 3c 35 20 29 20 76 20 26 3d 20 30 78 66 66 66 t<5 ) v &= 0xfff
1cb71 66 66 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 fff;. }.
1cb72 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d 30 20 if( v==0
1cb73 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1cb74 20 20 20 20 78 20 3d 20 69 6e 74 54 6f 4b 65 79 x = intToKey
1cb75 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72 78 20 (v);. rx
1cb76 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
1cb77 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 2d vetoUnpacked(pC-
1cb78 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 >pCursor, 0, (u6
1cb79 34 29 78 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 4)x, 0, &res);.
1cb7a 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 cnt++;.
1cb7b 20 20 20 20 7d 77 68 69 6c 65 28 20 63 6e 74 3c }while( cnt<
1cb7c 31 30 30 20 26 26 20 72 78 3d 3d 53 51 4c 49 54 100 && rx==SQLIT
1cb7d 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 29 E_OK && res==0 )
1cb7e 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 72 69 6f ;. db->prio
1cb7f 72 4e 65 77 52 6f 77 69 64 20 3d 20 76 3b 0a 20 rNewRowid = v;.
1cb80 20 20 20 20 20 69 66 28 20 72 78 3d 3d 53 51 4c if( rx==SQL
1cb81 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 ITE_OK && res==0
1cb82 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1cb83 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 SQLITE_FULL;.
1cb84 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 goto abort
1cb85 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1cb86 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1cb87 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 pC->rowidIsVali
1cb88 64 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 64 d = 0;. pC->d
1cb89 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1cb8a 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 0;. pC->cache
1cb8b 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1cb8c 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 TALE;. }. MemS
1cb8d 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1cb8e 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 MEM_Int);. pOu
1cb8f 74 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 62 72 t->u.i = v;. br
1cb90 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cb91 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 32 20 e: Insert P1 P2
1cb92 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57 P3 P4 P5.**.** W
1cb93 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
1cb94 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 to the table of
1cb95 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65 cursor P1. A ne
1cb96 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72 w entry is.** cr
1cb97 65 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73 eated if it does
1cb98 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 n't already exis
1cb99 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f t or the data fo
1cb9a 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a r an existing.**
1cb9b 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72 entry is overwr
1cb9c 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61 itten. The data
1cb9d 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 73 74 is the value st
1cb9e 6f 72 65 64 20 72 65 67 69 73 74 65 72 0a 2a 2a ored register.**
1cb9f 20 6e 75 6d 62 65 72 20 50 32 2e 20 54 68 65 20 number P2. The
1cba0 6b 65 79 20 69 73 20 73 74 6f 72 65 64 20 69 6e key is stored in
1cba1 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 54 68 register P3. Th
1cba2 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65 e key must.** be
1cba3 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a an integer..**.
1cba4 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 ** If the OPFLAG
1cba5 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 _NCHANGE flag of
1cba6 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e P5 is set, then
1cba7 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 the row change
1cba8 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 count is.** incr
1cba9 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 77 69 emented (otherwi
1cbaa 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74 68 65 se not). If the
1cbab 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 OPFLAG_LASTROWI
1cbac 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69 73 20 D flag of P5 is
1cbad 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77 set,.** then row
1cbae 69 64 20 69 73 20 73 74 6f 72 65 64 20 66 6f 72 id is stored for
1cbaf 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 74 75 subsequent retu
1cbb0 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c rn by the.** sql
1cbb1 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 ite3_last_insert
1cbb2 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74 69 6f _rowid() functio
1cbb3 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69 74 20 n (otherwise it
1cbb4 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29 2e 0a is unmodified)..
1cbb5 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 **.** Parameter
1cbb6 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 P4 may point to
1cbb7 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e a string contain
1cbb8 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 ing the table-na
1cbb9 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 me, or.** may be
1cbba 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 NULL. If it is
1cbbb 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 not NULL, then t
1cbbc 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 0a he update-hook .
1cbbd 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55 70 64 ** (sqlite3.xUpd
1cbbe 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69 73 20 ateCallback) is
1cbbf 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e invoked followin
1cbc0 67 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 69 g a successful i
1cbc1 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 nsert..**.** (WA
1cbc2 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50 RNING/TODO: If P
1cbc3 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d 63 75 1 is a pseudo-cu
1cbc4 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20 64 rsor and P2 is d
1cbc5 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c ynamically.** al
1cbc6 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 6f 77 located, then ow
1cbc7 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20 69 73 nership of P2 is
1cbc8 20 74 72 61 6e 73 66 65 72 72 65 64 20 74 6f 20 transferred to
1cbc9 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72 73 6f the pseudo-curso
1cbca 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73 74 65 r.** and registe
1cbcb 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65 70 68 r P2 becomes eph
1cbcc 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68 65 20 emeral. If the
1cbcd 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e 67 65 cursor is change
1cbce 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 d, the.** value
1cbcf 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 77 of register P2 w
1cbd0 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67 65 2e ill then change.
1cbd1 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 Make sure this
1cbd2 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 does not.** cau
1cbd3 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e se any problems.
1cbd4 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 ).**.** This ins
1cbd5 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f truction only wo
1cbd6 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20 rks on tables.
1cbd7 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 69 The equivalent i
1cbd8 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f nstruction.** fo
1cbd9 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f 50 5f r indices is OP_
1cbda 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 IdxInsert..*/.ca
1cbdb 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 7b 0a se OP_Insert: {.
1cbdc 20 20 4d 65 6d 20 2a 70 44 61 74 61 20 3d 20 26 Mem *pData = &
1cbdd 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d p->aMem[pOp->p2]
1cbde 3b 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 20 3d 20 ;. Mem *pKey =
1cbdf 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1cbe0 5d 3b 0a 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 ];.. i64 iKey;
1cbe1 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 /* The integer
1cbe2 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f ROWID or key fo
1cbe3 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 r the record to
1cbe4 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 be inserted */.
1cbe5 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 int i = pOp->p1
1cbe6 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1cbe7 70 43 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e pC;. assert( i>
1cbe8 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
1cbe9 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e or );. pC = p->
1cbea 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 apCsr[i];. asse
1cbeb 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 rt( pC!=0 );. a
1cbec 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73 ssert( pC->pCurs
1cbed 6f 72 21 3d 30 20 7c 7c 20 70 43 2d 3e 70 73 65 or!=0 || pC->pse
1cbee 75 64 6f 54 61 62 6c 65 20 29 3b 0a 20 20 61 73 udoTable );. as
1cbef 73 65 72 74 28 20 70 4b 65 79 2d 3e 66 6c 61 67 sert( pKey->flag
1cbf0 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 s & MEM_Int );.
1cbf1 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 assert( pC->isT
1cbf2 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 53 54 able );. REGIST
1cbf3 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 ER_TRACE(pOp->p2
1cbf4 2c 20 70 44 61 74 61 29 3b 0a 20 20 52 45 47 49 , pData);. REGI
1cbf5 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1cbf6 70 33 2c 20 70 4b 65 79 29 3b 0a 0a 20 20 69 4b p3, pKey);.. iK
1cbf7 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 4b ey = intToKey(pK
1cbf8 65 79 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 ey->u.i);. if(
1cbf9 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 pOp->p5 & OPFLAG
1cbfa 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 _NCHANGE ) p->nC
1cbfb 68 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 20 70 hange++;. if( p
1cbfc 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1cbfd 4c 41 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e LASTROWID ) db->
1cbfe 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 4b 65 79 lastRowid = pKey
1cbff 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70 44 61 ->u.i;. if( pDa
1cc00 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ta->flags & MEM_
1cc01 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 44 61 74 Null ){. pDat
1cc02 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 70 44 a->z = 0;. pD
1cc03 61 74 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 ata->n = 0;. }e
1cc04 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
1cc05 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 pData->flags &
1cc06 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 (MEM_Blob|MEM_St
1cc07 72 29 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 r) );. }. if(
1cc08 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 pC->pseudoTable
1cc09 29 7b 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e ){. if( !pC->
1cc0a 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 ephemPseudoTable
1cc0b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1cc0c 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 2d 3e 3DbFree(db, pC->
1cc0d 70 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 pData);. }.
1cc0e 20 20 70 43 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 pC->iKey = iKe
1cc0f 79 3b 0a 20 20 20 20 70 43 2d 3e 6e 44 61 74 61 y;. pC->nData
1cc10 20 3d 20 70 44 61 74 61 2d 3e 6e 3b 0a 20 20 20 = pData->n;.
1cc11 20 69 66 28 20 70 44 61 74 61 2d 3e 7a 3d 3d 70 if( pData->z==p
1cc12 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 7c 7c Data->zMalloc ||
1cc13 20 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f pC->ephemPseudo
1cc14 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 Table ){. p
1cc15 43 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 C->pData = pData
1cc16 2d 3e 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ->z;. if( !
1cc17 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 pC->ephemPseudoT
1cc18 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 able ){.
1cc19 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 3d 20 pData->flags &=
1cc1a 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 20 20 ~MEM_Dyn;.
1cc1b 20 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 7c pData->flags |
1cc1c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 = MEM_Ephem;.
1cc1d 20 20 20 20 20 70 44 61 74 61 2d 3e 7a 4d 61 6c pData->zMal
1cc1e 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d loc = 0;. }
1cc1f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1cc20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d 20 73 71 pC->pData = sq
1cc21 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 2d lite3Malloc( pC-
1cc22 3e 6e 44 61 74 61 2b 32 20 29 3b 0a 20 20 20 20 >nData+2 );.
1cc23 20 20 69 66 28 20 21 70 43 2d 3e 70 44 61 74 61 if( !pC->pData
1cc24 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1cc25 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43 2d memcpy(pC-
1cc26 3e 70 44 61 74 61 2c 20 70 44 61 74 61 2d 3e 7a >pData, pData->z
1cc27 2c 20 70 43 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 , pC->nData);.
1cc28 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 pC->pData[pC
1cc29 2d 3e 6e 44 61 74 61 5d 20 3d 20 30 3b 0a 20 20 ->nData] = 0;.
1cc2a 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 pC->pData[pC
1cc2b 2d 3e 6e 44 61 74 61 2b 31 5d 20 3d 20 30 3b 0a ->nData+1] = 0;.
1cc2c 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e 6e 75 }. pC->nu
1cc2d 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c llRow = 0;. }el
1cc2e 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 se{. int nZer
1cc2f 6f 3b 0a 20 20 20 20 69 6e 74 20 73 65 65 6b 52 o;. int seekR
1cc30 65 73 75 6c 74 20 3d 20 28 28 70 4f 70 2d 3e 70 esult = ((pOp->p
1cc31 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 5 & OPFLAG_USESE
1cc32 45 4b 52 45 53 55 4c 54 29 20 3f 20 70 43 2d 3e EKRESULT) ? pC->
1cc33 73 65 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 3b seekResult : 0);
1cc34 0a 20 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e . if( pData->
1cc35 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
1cc36 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 ){. nZero
1cc37 3d 20 70 44 61 74 61 2d 3e 75 2e 6e 5a 65 72 6f = pData->u.nZero
1cc38 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1cc39 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 nZero = 0;.
1cc3a 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 }. sqlite3B
1cc3b 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 treeSetCachedRow
1cc3c 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 id(pC->pCursor,
1cc3d 30 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 0);. rc = sql
1cc3e 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 ite3BtreeInsert(
1cc3f 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 pC->pCursor, 0,
1cc40 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 iKey,.
1cc41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc42 20 20 70 44 61 74 61 2d 3e 7a 2c 20 70 44 61 74 pData->z, pDat
1cc43 61 2d 3e 6e 2c 20 6e 5a 65 72 6f 2c 0a 20 20 20 a->n, nZero,.
1cc44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cc45 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 35 pOp->p5
1cc46 20 26 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 & OPFLAG_APPEND
1cc47 2c 20 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 20 , seekResult.
1cc48 20 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 43 2d );. }. . pC-
1cc49 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 >rowidIsValid =
1cc4a 30 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65 0;. pC->deferre
1cc4b 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70 dMoveto = 0;. p
1cc4c 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1cc4d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 CACHE_STALE;..
1cc4e 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 /* Invoke the u
1cc4f 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 pdate-hook if re
1cc50 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 quired. */. if(
1cc51 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1cc52 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c & db->xUpdateCal
1cc53 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 lback && pOp->p4
1cc54 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 .z ){. const
1cc55 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e char *zDb = db->
1cc56 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 aDb[pC->iDb].zNa
1cc57 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 me;. const ch
1cc58 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e ar *zTbl = pOp->
1cc59 70 34 2e 7a 3b 0a 20 20 20 20 69 6e 74 20 6f 70 p4.z;. int op
1cc5a 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f = ((pOp->p5 & O
1cc5b 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20 PFLAG_ISUPDATE)
1cc5c 3f 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 ? SQLITE_UPDATE
1cc5d 3a 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 : SQLITE_INSERT)
1cc5e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
1cc5f 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 ->isTable );.
1cc60 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c db->xUpdateCall
1cc61 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 back(db->pUpdate
1cc62 41 72 67 2c 20 6f 70 2c 20 7a 44 62 2c 20 7a 54 Arg, op, zDb, zT
1cc63 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 20 20 61 bl, iKey);. a
1cc64 73 73 65 72 74 28 20 70 43 2d 3e 69 44 62 3e 3d ssert( pC->iDb>=
1cc65 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 0 );. }. break
1cc66 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cc67 44 65 6c 65 74 65 20 50 31 20 50 32 20 2a 20 50 Delete P1 P2 * P
1cc68 34 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 4 *.**.** Delete
1cc69 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 20 77 the record at w
1cc6a 68 69 63 68 20 74 68 65 20 50 31 20 63 75 72 73 hich the P1 curs
1cc6b 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 or is currently
1cc6c 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 pointing..**.**
1cc6d 54 68 65 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 The cursor will
1cc6e 62 65 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 be left pointing
1cc6f 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 6e at either the n
1cc70 65 78 74 20 6f 72 20 74 68 65 20 70 72 65 76 69 ext or the previ
1cc71 6f 75 73 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e ous.** record in
1cc72 20 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 69 the table. If i
1cc73 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 t is left pointi
1cc74 6e 67 20 61 74 20 74 68 65 20 6e 65 78 74 20 72 ng at the next r
1cc75 65 63 6f 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 ecord, then.** t
1cc76 68 65 20 6e 65 78 74 20 4e 65 78 74 20 69 6e 73 he next Next ins
1cc77 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 truction will be
1cc78 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 a no-op. Hence
1cc79 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c it is OK to del
1cc7a 65 74 65 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 ete.** a record
1cc7b 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e from within an N
1cc7c 65 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 ext loop..**.**
1cc7d 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 If the OPFLAG_NC
1cc7e 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 32 HANGE flag of P2
1cc7f 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 is set, then th
1cc80 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 e row change cou
1cc81 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 nt is.** increme
1cc82 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 nted (otherwise
1cc83 6e 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d not)..**.** P1 m
1cc84 75 73 74 20 6e 6f 74 20 62 65 20 70 73 65 75 64 ust not be pseud
1cc85 6f 2d 74 61 62 6c 65 2e 20 20 49 74 20 68 61 73 o-table. It has
1cc86 20 74 6f 20 62 65 20 61 20 72 65 61 6c 20 74 61 to be a real ta
1cc87 62 6c 65 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 ble with.** mult
1cc88 69 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a iple rows..**.**
1cc89 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 If P4 is not NU
1cc8a 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 LL, then it is t
1cc8b 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
1cc8c 61 62 6c 65 20 74 68 61 74 20 50 31 20 69 73 0a able that P1 is.
1cc8d 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 ** pointing to.
1cc8e 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b The update hook
1cc8f 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 will be invoked
1cc90 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a , if it exists..
1cc91 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 ** If P4 is not
1cc92 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 50 31 NULL then the P1
1cc93 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 cursor must hav
1cc94 65 20 62 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65 e been positione
1cc95 64 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f d.** using OP_No
1cc96 74 46 6f 75 6e 64 20 70 72 69 6f 72 20 74 6f 20 tFound prior to
1cc97 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70 invoking this op
1cc98 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 code..*/.case OP
1cc99 5f 44 65 6c 65 74 65 3a 20 7b 0a 20 20 69 6e 74 _Delete: {. int
1cc9a 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 i = pOp->p1;.
1cc9b 69 36 34 20 69 4b 65 79 20 3d 20 30 3b 0a 20 20 i64 iKey = 0;.
1cc9c 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1cc9d 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
1cc9e 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 && i<p->nCursor
1cc9f 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 );. pC = p->apC
1cca0 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 sr[i];. assert(
1cca1 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 pC!=0 );. asse
1cca2 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 rt( pC->pCursor!
1cca3 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76 =0 ); /* Only v
1cca4 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 61 alid for real ta
1cca5 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74 bles, no pseudot
1cca6 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 ables */.. /* I
1cca7 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f f the update-hoo
1cca8 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 k will be invoke
1cca9 64 2c 20 73 65 74 20 69 4b 65 79 20 74 6f 20 74 d, set iKey to t
1ccaa 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 0a he rowid of the.
1ccab 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67 20 64 ** row being d
1ccac 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 eleted.. */. i
1ccad 66 28 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 f( db->xUpdateCa
1ccae 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 llback && pOp->p
1ccaf 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 73 65 72 4.z ){. asser
1ccb0 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 t( pC->isTable )
1ccb1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
1ccb2 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 ->rowidIsValid )
1ccb3 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20 ; /* lastRowid
1ccb4 73 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20 set by previous
1ccb5 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 OP_NotFound */.
1ccb6 20 20 20 69 4b 65 79 20 3d 20 70 43 2d 3e 6c 61 iKey = pC->la
1ccb7 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a 0a 20 20 stRowid;. }..
1ccb8 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1ccb9 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 CursorMoveto(pC)
1ccba 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 ;. if( rc ) got
1ccbb 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1ccbc 72 72 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 42 rror;. sqlite3B
1ccbd 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 treeSetCachedRow
1ccbe 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 id(pC->pCursor,
1ccbf 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 0);. rc = sqlit
1ccc0 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 70 43 e3BtreeDelete(pC
1ccc1 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 70 43 ->pCursor);. pC
1ccc2 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1ccc3 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 CACHE_STALE;..
1ccc4 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 /* Invoke the up
1ccc5 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 date-hook if req
1ccc6 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 uired. */. if(
1ccc7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1ccc8 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c db->xUpdateCall
1ccc9 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e back && pOp->p4.
1ccca 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 z ){. const c
1cccb 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 har *zDb = db->a
1cccc 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d Db[pC->iDb].zNam
1cccd 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 e;. const cha
1ccce 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 r *zTbl = pOp->p
1cccf 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 4.z;. db->xUp
1ccd0 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d dateCallback(db-
1ccd1 3e 70 55 70 64 61 74 65 41 72 67 2c 20 53 51 4c >pUpdateArg, SQL
1ccd2 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c ITE_DELETE, zDb,
1ccd3 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 zTbl, iKey);.
1ccd4 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 44 assert( pC->iD
1ccd5 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 b>=0 );. }. if
1ccd6 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c ( pOp->p2 & OPFL
1ccd7 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e AG_NCHANGE ) p->
1ccd8 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 nChange++;. bre
1ccd9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1ccda 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 50 31 20 : ResetCount P1
1ccdb 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f * *.**.** This o
1ccdc 70 63 6f 64 65 20 72 65 73 65 74 73 20 74 68 65 pcode resets the
1ccdd 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 VMs internal ch
1ccde 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 ange counter to
1ccdf 30 2e 20 49 66 20 50 31 20 69 73 20 74 72 75 65 0. If P1 is true
1cce0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 ,.** then the va
1cce1 6c 75 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67 lue of the chang
1cce2 65 20 63 6f 75 6e 74 65 72 20 69 73 20 63 6f 70 e counter is cop
1cce3 69 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 ied to the datab
1cce4 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68 ase handle.** ch
1cce5 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 28 72 65 ange counter (re
1cce6 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 turned by subseq
1cce7 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 uent calls to sq
1cce8 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 29 lite3_changes())
1cce9 0a 2a 2a 20 62 65 66 6f 72 65 20 69 74 20 69 73 .** before it is
1ccea 20 72 65 73 65 74 2e 20 54 68 69 73 20 69 73 20 reset. This is
1cceb 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20 used by trigger
1ccec 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 programs..*/.cas
1cced 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a e OP_ResetCount:
1ccee 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 {. if( pOp->p1
1ccef 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1ccf0 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 dbeSetChanges(db
1ccf1 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 , p->nChange);.
1ccf2 20 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 }. p->nChange
1ccf3 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a = 0;. break;.}.
1ccf4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 ./* Opcode: RowD
1ccf5 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ata P1 P2 * * *.
1ccf6 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f **.** Write into
1ccf7 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65 register P2 the
1ccf8 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 complete row da
1ccf9 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 ta for cursor P1
1ccfa 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f ..** There is no
1ccfb 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 interpretation
1ccfc 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a of the data. .*
1ccfd 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70 * It is just cop
1ccfe 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 ied onto the P2
1ccff 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 register exactly
1cd00 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f as .** it is fo
1cd01 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 und in the datab
1cd02 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
1cd03 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 If the P1 cursor
1cd04 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e must be pointin
1cd05 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 g to a valid row
1cd06 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 (not a NULL row
1cd07 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 ).** of a real t
1cd08 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 able, not a pseu
1cd09 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 do-table..*/./*
1cd0a 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 Opcode: RowKey P
1cd0b 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1cd0c 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 Write into regi
1cd0d 73 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 ster P2 the comp
1cd0e 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 lete row key for
1cd0f 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 cursor P1..** T
1cd10 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 here is no inter
1cd11 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 pretation of the
1cd12 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 data. .** The
1cd13 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e key is copied on
1cd14 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74 to the P3 regist
1cd15 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a er exactly as .*
1cd16 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e * it is found in
1cd17 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1cd18 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 le..**.** If the
1cd19 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 P1 cursor must
1cd1a 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 be pointing to a
1cd1b 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 valid row (not
1cd1c 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f a NULL row).** o
1cd1d 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 f a real table,
1cd1e 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 not a pseudo-tab
1cd1f 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 le..*/.case OP_R
1cd20 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 owKey:.case OP_R
1cd21 6f 77 44 61 74 61 3a 20 7b 0a 20 20 69 6e 74 20 owData: {. int
1cd22 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
1cd23 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1cd24 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1cd25 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a 20 20 70 4f ;. u32 n;.. pO
1cd26 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1cd27 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f p->p2];.. /* No
1cd28 74 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61 te that RowKey a
1cd29 6e 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72 nd RowData are r
1cd2a 65 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68 eally exactly th
1cd2b 65 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69 e same instructi
1cd2c 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 on */. assert(
1cd2d 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1cd2e 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 rsor );. pC = p
1cd2f 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 ->apCsr[i];. as
1cd30 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c sert( pC->isTabl
1cd31 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 e || pOp->opcode
1cd32 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 ==OP_RowKey );.
1cd33 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 49 assert( pC->isI
1cd34 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 ndex || pOp->opc
1cd35 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20 ode==OP_RowData
1cd36 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 );. assert( pC!
1cd37 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1cd38 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 pC->nullRow==0 )
1cd39 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e ;. assert( pC->
1cd3a 70 73 65 75 64 6f 54 61 62 6c 65 3d 3d 30 20 29 pseudoTable==0 )
1cd3b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e ;. assert( pC->
1cd3c 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 pCursor!=0 );.
1cd3d 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 pCrsr = pC->pCur
1cd3e 73 6f 72 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 sor;. rc = sqli
1cd3f 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
1cd40 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 eto(pC);. if( r
1cd41 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 c ) goto abort_d
1cd42 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 69 ue_to_error;. i
1cd43 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 f( pC->isIndex )
1cd44 7b 0a 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 {. i64 n64;.
1cd45 20 20 20 61 73 73 65 72 74 28 20 21 70 43 2d 3e assert( !pC->
1cd46 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 73 isTable );. s
1cd47 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 qlite3BtreeKeySi
1cd48 7a 65 28 70 43 72 73 72 2c 20 26 6e 36 34 29 3b ze(pCrsr, &n64);
1cd49 0a 20 20 20 20 69 66 28 20 6e 36 34 3e 64 62 2d . if( n64>db-
1cd4a 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1cd4b 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
1cd4c 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 goto too_b
1cd4d 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 ig;. }. n
1cd4e 3d 20 28 69 6e 74 29 6e 36 34 3b 0a 20 20 7d 65 = (int)n64;. }e
1cd4f 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
1cd50 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 70 43 BtreeDataSize(pC
1cd51 72 73 72 2c 20 26 6e 29 3b 0a 20 20 20 20 69 66 rsr, &n);. if
1cd52 28 20 28 69 6e 74 29 6e 3e 64 62 2d 3e 61 4c 69 ( (int)n>db->aLi
1cd53 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
1cd54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 _LENGTH] ){.
1cd55 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1cd56 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
1cd57 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1cd58 6f 77 28 70 4f 75 74 2c 20 6e 2c 20 30 29 20 29 ow(pOut, n, 0) )
1cd59 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 {. goto no_me
1cd5a 6d 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e m;. }. pOut->n
1cd5b 20 3d 20 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 = n;. MemSetTy
1cd5c 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1cd5d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 70 43 _Blob);. if( pC
1cd5e 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 ->isIndex ){.
1cd5f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1cd60 65 65 4b 65 79 28 70 43 72 73 72 2c 20 30 2c 20 eeKey(pCrsr, 0,
1cd61 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d n, pOut->z);. }
1cd62 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 else{. rc = s
1cd63 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
1cd64 70 43 72 73 72 2c 20 30 2c 20 6e 2c 20 70 4f 75 pCrsr, 0, n, pOu
1cd65 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 t->z);. }. pOu
1cd66 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f t->enc = SQLITE_
1cd67 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 UTF8; /* In cas
1cd68 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 e the blob is ev
1cd69 65 72 20 63 61 73 74 20 74 6f 20 74 65 78 74 20 er cast to text
1cd6a 2a 2f 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f */. UPDATE_MAX_
1cd6b 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1cd6c 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cd6d 70 63 6f 64 65 3a 20 52 6f 77 69 64 20 50 31 20 pcode: Rowid P1
1cd6e 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 P2 * * *.**.** S
1cd6f 74 6f 72 65 20 69 6e 20 72 65 67 69 73 74 65 72 tore in register
1cd70 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 P2 an integer w
1cd71 68 69 63 68 20 69 73 20 74 68 65 20 6b 65 79 20 hich is the key
1cd72 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 of the table ent
1cd73 72 79 20 74 68 61 74 0a 2a 2a 20 50 31 20 69 73 ry that.** P1 is
1cd74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1cd75 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 63 61 to..**.** P1 ca
1cd76 6e 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 6f n be either an o
1cd77 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f 72 rdinary table or
1cd78 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1cd79 2e 20 20 54 68 65 72 65 20 75 73 65 64 20 74 6f . There used to
1cd7a 0a 2a 2a 20 62 65 20 61 20 73 65 70 61 72 61 74 .** be a separat
1cd7b 65 20 4f 50 5f 56 52 6f 77 69 64 20 6f 70 63 6f e OP_VRowid opco
1cd7c 64 65 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 de for use with
1cd7d 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2c 20 virtual tables,
1cd7e 62 75 74 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 but this.** one
1cd7f 6f 70 63 6f 64 65 20 6e 6f 77 20 77 6f 72 6b 73 opcode now works
1cd80 20 66 6f 72 20 62 6f 74 68 20 74 61 62 6c 65 20 for both table
1cd81 74 79 70 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f types..*/.case O
1cd82 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 P_Rowid: {
1cd83 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 /* ou
1cd84 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
1cd85 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
1cd86 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
1cd87 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a 0a *pC;. i64 v;..
1cd88 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1cd89 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1cd8a 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 ;. pC = p->apCs
1cd8b 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 r[i];. assert(
1cd8c 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 pC!=0 );. if( p
1cd8d 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 C->nullRow ){.
1cd8e 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 /* Do nothing
1cd8f 73 6f 20 74 68 61 74 20 72 65 67 5b 50 32 5d 20 so that reg[P2]
1cd90 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 20 2a 2f 0a remains NULL */.
1cd91 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 65 6c break;. }el
1cd92 73 65 20 69 66 28 20 70 43 2d 3e 64 65 66 65 72 se if( pC->defer
1cd93 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 20 redMoveto ){.
1cd94 20 76 20 3d 20 70 43 2d 3e 6d 6f 76 65 74 6f 54 v = pC->movetoT
1cd95 61 72 67 65 74 3b 0a 20 20 7d 65 6c 73 65 20 69 arget;. }else i
1cd96 66 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 f( pC->pseudoTab
1cd97 6c 65 20 29 7b 0a 20 20 20 20 76 20 3d 20 6b 65 le ){. v = ke
1cd98 79 54 6f 49 6e 74 28 70 43 2d 3e 69 4b 65 79 29 yToInt(pC->iKey)
1cd99 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1cd9a 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1cd9b 4c 45 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 LE. }else if( p
1cd9c 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 C->pVtabCursor )
1cd9d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 {. sqlite3_vt
1cd9e 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 63 ab *pVtab;. c
1cd9f 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
1cda0 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 ule *pModule;.
1cda1 20 20 70 56 74 61 62 20 3d 20 70 43 2d 3e 70 56 pVtab = pC->pV
1cda2 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1cda3 3b 0a 20 20 20 20 70 4d 6f 64 75 6c 65 20 3d 20 ;. pModule =
1cda4 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a pVtab->pModule;.
1cda5 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 6f 64 assert( pMod
1cda6 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a 20 ule->xRowid );.
1cda7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1cda8 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1cda9 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1cdaa 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d misuse;. rc =
1cdab 20 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 pModule->xRowid
1cdac 28 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 (pC->pVtabCursor
1cdad 2c 20 26 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 , &v);. sqlit
1cdae 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1cdaf 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d zErrMsg);. p-
1cdb0 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 >zErrMsg = pVtab
1cdb1 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 ->zErrMsg;. p
1cdb2 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1cdb3 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 0;. if( sqlit
1cdb4 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1cdb5 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1cdb6 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65 6e 64 69 to_misuse;.#endi
1cdb7 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1cdb8 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1cdb9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
1cdba 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
1cdbb 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a rsorMoveto(pC);.
1cdbc 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
1cdbd 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1cdbe 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 43 rror;. if( pC
1cdbf 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 ->rowidIsValid )
1cdc0 7b 0a 20 20 20 20 20 20 76 20 3d 20 70 43 2d 3e {. v = pC->
1cdc1 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 7d lastRowid;. }
1cdc2 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
1cdc3 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 rt( pC->pCursor!
1cdc4 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =0 );. sqli
1cdc5 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
1cdc6 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 76 29 pC->pCursor, &v)
1cdc7 3b 0a 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54 ;. v = keyT
1cdc8 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 7d 0a 20 oInt(v);. }.
1cdc9 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d }. pOut->u.i =
1cdca 20 76 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 v;. MemSetType
1cdcb 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1cdcc 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a nt);. break;.}.
1cdcd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c ./* Opcode: Null
1cdce 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a Row P1 * * * *.*
1cdcf 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 *.** Move the cu
1cdd0 72 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c rsor P1 to a nul
1cdd1 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 l row. Any OP_C
1cdd2 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 olumn operations
1cdd3 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77 .** that occur w
1cdd4 68 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20 hile the cursor
1cdd5 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 is on the null r
1cdd6 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a ow will always.*
1cdd7 2a 20 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a * write a NULL..
1cdd8 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 */.case OP_NullR
1cdd9 6f 77 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 ow: {. int i =
1cdda 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 pOp->p1;. VdbeC
1cddb 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 ursor *pC;.. as
1cddc 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1cddd 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1cdde 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1cddf 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d ;. assert( pC!=
1cde0 30 20 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 0 );. pC->nullR
1cde1 6f 77 20 3d 20 31 3b 0a 20 20 70 43 2d 3e 72 6f ow = 1;. pC->ro
1cde2 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1cde3 20 20 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f if( pC->pCurso
1cde4 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
1cde5 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 BtreeClearCursor
1cde6 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 (pC->pCursor);.
1cde7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1cde8 2a 20 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 * Opcode: Last P
1cde9 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1cdea 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 The next use of
1cdeb 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f the Rowid or Co
1cdec 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 lumn or Next ins
1cded 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 truction for P1
1cdee 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 .** will refer t
1cdef 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
1cdf0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1cdf1 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e table or index.
1cdf2 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 .** If the table
1cdf3 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 or index is emp
1cdf4 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 ty and P2>0, the
1cdf5 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 n jump immediate
1cdf6 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 ly to P2..** If
1cdf7 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 P2 is 0 or if th
1cdf8 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1cdf9 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 is not empty, f
1cdfa 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 all through.** t
1cdfb 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
1cdfc 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
1cdfd 63 61 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 case OP_Last: {
1cdfe 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a /* jump *
1cdff 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d /. int i = pOp-
1ce00 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f >p1;. VdbeCurso
1ce01 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f r *pC;. BtCurso
1ce02 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 r *pCrsr;. int
1ce03 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 res;.. assert(
1ce04 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1ce05 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 rsor );. pC = p
1ce06 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 ->apCsr[i];. as
1ce07 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 sert( pC!=0 );.
1ce08 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 pCrsr = pC->pCu
1ce09 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 rsor;. assert(
1ce0a 70 43 72 73 72 21 3d 30 20 29 3b 0a 20 20 72 63 pCrsr!=0 );. rc
1ce0b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c = sqlite3BtreeL
1ce0c 61 73 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 ast(pCrsr, &res)
1ce0d 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 ;. pC->nullRow
1ce0e 3d 20 28 75 38 29 72 65 73 3b 0a 20 20 70 43 2d = (u8)res;. pC-
1ce0f 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1ce10 3d 20 30 3b 0a 20 20 70 43 2d 3e 72 6f 77 69 64 = 0;. pC->rowid
1ce11 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70 IsValid = 0;. p
1ce12 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1ce13 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1ce14 69 66 28 20 72 65 73 20 26 26 20 70 4f 70 2d 3e if( res && pOp->
1ce15 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 63 20 3d p2>0 ){. pc =
1ce16 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1ce17 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f }. break;.}.../
1ce18 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 20 50 * Opcode: Sort P
1ce19 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1ce1a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 This opcode doe
1ce1b 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 s exactly the sa
1ce1c 6d 65 20 74 68 69 6e 67 20 61 73 20 4f 50 5f 52 me thing as OP_R
1ce1d 65 77 69 6e 64 20 65 78 63 65 70 74 20 74 68 61 ewind except tha
1ce1e 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e t.** it incremen
1ce1f 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 ts an undocument
1ce20 65 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 ed global variab
1ce21 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 le used for test
1ce22 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69 ing..**.** Sorti
1ce23 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 ng is accomplish
1ce24 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 72 65 ed by writing re
1ce25 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 6f 72 cords into a sor
1ce26 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 ting index,.** t
1ce27 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 20 74 68 hen rewinding th
1ce28 61 74 20 69 6e 64 65 78 20 61 6e 64 20 70 6c 61 at index and pla
1ce29 79 69 6e 67 20 69 74 20 62 61 63 6b 20 66 72 6f ying it back fro
1ce2a 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a m beginning to.*
1ce2b 2a 20 65 6e 64 2e 20 20 57 65 20 75 73 65 20 74 * end. We use t
1ce2c 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 he OP_Sort opcod
1ce2d 65 20 69 6e 73 74 65 61 64 20 6f 66 20 4f 50 5f e instead of OP_
1ce2e 52 65 77 69 6e 64 20 74 6f 20 64 6f 20 74 68 65 Rewind to do the
1ce2f 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20 73 6f .** rewinding so
1ce30 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c that the global
1ce31 20 76 61 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 variable will b
1ce32 65 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 6e e incremented an
1ce33 64 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f 6e 20 d.** regression
1ce34 74 65 73 74 73 20 63 61 6e 20 64 65 74 65 72 6d tests can determ
1ce35 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
1ce36 6f 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 ot the optimizer
1ce37 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 is.** correctly
1ce38 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75 74 20 optimizing out
1ce39 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f sorts..*/.case O
1ce3a 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20 20 20 P_Sort: {
1ce3b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 64 /* jump */.#ifd
1ce3c 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
1ce3d 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f sqlite3_sort_co
1ce3e 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 unt++;. sqlite3
1ce3f 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b _search_count--;
1ce40 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 61 43 6f .#endif. p->aCo
1ce41 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53 54 4d unter[SQLITE_STM
1ce42 54 53 54 41 54 55 53 5f 53 4f 52 54 2d 31 5d 2b TSTATUS_SORT-1]+
1ce43 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 +;. /* Fall thr
1ce44 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52 65 77 ough into OP_Rew
1ce45 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f ind */.}./* Opco
1ce46 64 65 3a 20 52 65 77 69 6e 64 20 50 31 20 50 32 de: Rewind P1 P2
1ce47 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
1ce48 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 65 next use of the
1ce49 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e Rowid or Column
1ce4a 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 63 or Next instruc
1ce4b 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20 tion for P1 .**
1ce4c 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 68 will refer to th
1ce4d 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e e first entry in
1ce4e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 the database ta
1ce4f 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a ble or index..**
1ce50 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 If the table or
1ce51 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 index is empty
1ce52 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a and P2>0, then j
1ce53 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ump immediately
1ce54 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 to P2..** If P2
1ce55 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74 is 0 or if the t
1ce56 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 able or index is
1ce57 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c not empty, fall
1ce58 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 through.** to t
1ce59 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 he following ins
1ce5a 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 truction..*/.cas
1ce5b 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b 20 20 e OP_Rewind: {
1ce5c 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1ce5d 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
1ce5e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 p1;. VdbeCursor
1ce5f 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pC;. BtCursor
1ce60 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 *pCrsr;. int r
1ce61 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 es;.. assert( i
1ce62 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 >=0 && i<p->nCur
1ce63 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d sor );. pC = p-
1ce64 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 >apCsr[i];. ass
1ce65 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 ert( pC!=0 );.
1ce66 69 66 28 20 28 70 43 72 73 72 20 3d 20 70 43 2d if( (pCrsr = pC-
1ce67 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a >pCursor)!=0 ){.
1ce68 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ce69 42 74 72 65 65 46 69 72 73 74 28 70 43 72 73 72 BtreeFirst(pCrsr
1ce6a 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d , &res);. pC-
1ce6b 3e 61 74 46 69 72 73 74 20 3d 20 72 65 73 3d 3d >atFirst = res==
1ce6c 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70 43 2d 3e 0 ?1:0;. pC->
1ce6d 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d deferredMoveto =
1ce6e 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 0;. pC->cach
1ce6f 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1ce70 53 54 41 4c 45 3b 0a 20 20 20 20 70 43 2d 3e 72 STALE;. pC->r
1ce71 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
1ce72 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
1ce73 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 43 2d s = 1;. }. pC-
1ce74 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 >nullRow = (u8)r
1ce75 65 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f es;. assert( pO
1ce76 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e p->p2>0 && pOp->
1ce77 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 p2<p->nOp );. i
1ce78 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 f( res ){. pc
1ce79 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1ce7a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1ce7b 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20 /* Opcode: Next
1ce7c 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1ce7d 2a 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f 72 * Advance cursor
1ce7e 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 P1 so that it p
1ce7f 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 78 oints to the nex
1ce80 74 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 t key/data pair
1ce81 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 in its.** table
1ce82 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 or index. If th
1ce83 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 ere are no more
1ce84 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 key/value pairs
1ce85 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 then fall throug
1ce86 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c h.** to the foll
1ce87 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f owing instructio
1ce88 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 n. But if the c
1ce89 75 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 61 ursor advance wa
1ce8a 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a s successful,.**
1ce8b 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c jump immediatel
1ce8c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 y to P2..**.** T
1ce8d 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 he P1 cursor mus
1ce8e 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 t be for a real
1ce8f 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 table, not a pse
1ce90 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a udo-table..**.**
1ce91 20 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a See also: Prev.
1ce92 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 */./* Opcode: Pr
1ce93 65 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ev P1 P2 * * *.*
1ce94 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 72 *.** Back up cur
1ce95 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 sor P1 so that i
1ce96 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
1ce97 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 previous key/dat
1ce98 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a a pair in its.**
1ce99 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e table or index.
1ce9a 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f If there is no
1ce9b 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 previous key/va
1ce9c 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 lue pairs then f
1ce9d 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 all through.** t
1ce9e 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
1ce9f 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 instruction. Bu
1cea0 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 t if the cursor
1cea1 62 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 65 backup was succe
1cea2 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 ssful,.** jump i
1cea3 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 mmediately to P2
1cea4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 ..**.** The P1 c
1cea5 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f ursor must be fo
1cea6 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 r a real table,
1cea7 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 not a pseudo-tab
1cea8 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 le..*/.case OP_P
1cea9 72 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f 2a rev: /*
1ceaa 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 jump */.case OP
1ceab 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20 _Next: {
1ceac 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 56 64 62 /* jump */. Vdb
1cead 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1ceae 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1ceaf 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 43 48 int res;.. CH
1ceb0 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 ECK_FOR_INTERRUP
1ceb1 54 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 T;. assert( pOp
1ceb2 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1ceb3 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1ceb4 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 . pC = p->apCsr
1ceb5 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 [pOp->p1];. if(
1ceb6 20 70 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 pC==0 ){. br
1ceb7 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 eak; /* See tic
1ceb8 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d ket #2273 */. }
1ceb9 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 . pCrsr = pC->p
1ceba 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 Cursor;. assert
1cebb 28 20 70 43 72 73 72 20 29 3b 0a 20 20 72 65 73 ( pCrsr );. res
1cebc 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 = 1;. assert(
1cebd 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1cebe 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 to==0 );. rc =
1cebf 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1cec0 4e 65 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 Next ? sqlite3Bt
1cec1 72 65 65 4e 65 78 74 28 70 43 72 73 72 2c 20 26 reeNext(pCrsr, &
1cec2 72 65 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20 res) :.
1cec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cec4 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1cec5 65 50 72 65 76 69 6f 75 73 28 70 43 72 73 72 2c ePrevious(pCrsr,
1cec6 20 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 &res);. pC->nu
1cec7 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 65 73 3b llRow = (u8)res;
1cec8 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 . pC->cacheStat
1cec9 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1ceca 3b 0a 20 20 69 66 28 20 72 65 73 3d 3d 30 20 29 ;. if( res==0 )
1cecb 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e {. pc = pOp->
1cecc 70 32 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 p2 - 1;. if(
1cecd 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e 61 43 6f pOp->p5 ) p->aCo
1cece 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d unter[pOp->p5-1]
1cecf 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ++;.#ifdef SQLIT
1ced0 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 E_TEST. sqlit
1ced1 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b e3_search_count+
1ced2 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 +;.#endif. }.
1ced3 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1ced4 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1ced5 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 ../* Opcode: Idx
1ced6 49 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20 Insert P1 P2 P3
1ced7 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 * P5.**.** Regis
1ced8 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20 53 ter P2 holds a S
1ced9 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61 64 QL index key mad
1ceda 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 4d e using the.** M
1cedb 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 akeRecord instru
1cedc 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f 70 ctions. This op
1cedd 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 61 74 code writes that
1cede 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 key.** into the
1cedf 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74 61 index P1. Data
1cee0 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 69 for the entry i
1cee1 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 s nil..**.** P3
1cee2 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 20 70 is a flag that p
1cee3 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20 74 rovides a hint t
1cee4 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 o the b-tree lay
1cee5 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 er that this.**
1cee6 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 insert is likely
1cee7 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 to be an append
1cee8 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 ..**.** This ins
1cee9 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f truction only wo
1ceea 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73 2e rks for indices.
1ceeb 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 The equivalent
1ceec 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 instruction.**
1ceed 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f 50 for tables is OP
1ceee 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 _Insert..*/.case
1ceef 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20 7b OP_IdxInsert: {
1cef0 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a /* in2 *
1cef1 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d /. int i = pOp-
1cef2 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f >p1;. VdbeCurso
1cef3 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f r *pC;. BtCurso
1cef4 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 r *pCrsr;. asse
1cef5 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1cef6 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 >nCursor );. as
1cef7 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 sert( p->apCsr[i
1cef8 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 ]!=0 );. assert
1cef9 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 ( pIn2->flags &
1cefa 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 69 66 MEM_Blob );. if
1cefb 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d ( (pCrsr = (pC =
1cefc 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 p->apCsr[i])->p
1cefd 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 Cursor)!=0 ){.
1cefe 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 assert( pC->is
1ceff 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 Table==0 );.
1cf00 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 rc = ExpandBlob(
1cf01 70 49 6e 32 29 3b 0a 20 20 20 20 69 66 28 20 72 pIn2);. if( r
1cf02 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1cf03 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d int nKey =
1cf04 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 pIn2->n;.
1cf05 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 const char *zKey
1cf06 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20 20 = pIn2->z;.
1cf07 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1cf08 72 65 65 49 6e 73 65 72 74 28 70 43 72 73 72 2c reeInsert(pCrsr,
1cf09 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22 22 2c zKey, nKey, "",
1cf0a 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 2c 20 0, 0, pOp->p3,
1cf0b 0a 20 20 20 20 20 20 20 20 20 20 28 28 70 4f 70 . ((pOp
1cf0c 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 ->p5 & OPFLAG_US
1cf0d 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 70 ESEEKRESULT) ? p
1cf0e 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3a 20 C->seekResult :
1cf0f 30 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 0). );.
1cf10 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 assert( pC->de
1cf11 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 ferredMoveto==0
1cf12 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 63 61 63 );. pC->cac
1cf13 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1cf14 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 _STALE;. }.
1cf15 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1cf16 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 Opcode: IdxDele
1cf17 74 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a te P1 P2 P3 * *.
1cf18 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e **.** The conten
1cf19 74 20 6f 66 20 50 33 20 72 65 67 69 73 74 65 72 t of P3 register
1cf1a 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 s starting at re
1cf1b 67 69 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a gister P2 form.*
1cf1c 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e * an unpacked in
1cf1d 64 65 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70 dex key. This op
1cf1e 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 code removes tha
1cf1f 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 t entry from the
1cf20 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65 .** index opene
1cf21 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a d by cursor P1..
1cf22 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65 */.case OP_IdxDe
1cf23 6c 65 74 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 lete: {. int i
1cf24 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 = pOp->p1;. Vdb
1cf25 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1cf26 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1cf27 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1cf28 33 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 3>0 );. assert(
1cf29 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f pOp->p2>0 && pO
1cf2a 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3c 3d 70 p->p2+pOp->p3<=p
1cf2b 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 61 73 ->nMem+1 );. as
1cf2c 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1cf2d 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1cf2e 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 assert( p->apCsr
1cf2f 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 [i]!=0 );. if(
1cf30 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 70 (pCrsr = (pC = p
1cf31 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 ->apCsr[i])->pCu
1cf32 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 rsor)!=0 ){.
1cf33 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 55 6e 70 int res;. Unp
1cf34 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 ackedRecord r;.
1cf35 20 20 20 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 r.pKeyInfo =
1cf36 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 pC->pKeyInfo;.
1cf37 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 r.nField = (u1
1cf38 36 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 72 6)pOp->p3;. r
1cf39 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 .flags = 0;.
1cf3a 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 r.aMem = &p->aMe
1cf3b 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1cf3c 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1cf3d 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1cf3e 70 43 72 73 72 2c 20 26 72 2c 20 30 2c 20 30 2c pCrsr, &r, 0, 0,
1cf3f 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 &res);. if(
1cf40 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1cf41 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 res==0 ){.
1cf42 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1cf43 65 65 44 65 6c 65 74 65 28 70 43 72 73 72 29 3b eeDelete(pCrsr);
1cf44 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
1cf45 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d t( pC->deferredM
1cf46 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 oveto==0 );.
1cf47 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1cf48 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1cf49 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1cf4a 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77 * Opcode: IdxRow
1cf4b 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a id P1 P2 * * *.*
1cf4c 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 *.** Write into
1cf4d 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 register P2 an i
1cf4e 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
1cf4f 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
1cf50 6e 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a n the record at.
1cf51 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 ** the end of th
1cf52 65 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e e index key poin
1cf53 74 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72 ted to by cursor
1cf54 20 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67 P1. This integ
1cf55 65 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 er should be.**
1cf56 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 the rowid of the
1cf57 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 table entry to
1cf58 77 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78 which this index
1cf59 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a entry points..*
1cf5a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 *.** See also: R
1cf5b 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63 6f 72 64 owid, MakeRecord
1cf5c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 ..*/.case OP_Idx
1cf5d 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 Rowid: {
1cf5e 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1cf5f 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e erelease */. in
1cf60 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 t i = pOp->p1;.
1cf61 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1cf62 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1cf63 70 43 3b 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 pC;... assert(
1cf64 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 i>=0 && i<p->nCu
1cf65 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 rsor );. assert
1cf66 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 ( p->apCsr[i]!=0
1cf67 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 );. if( (pCrsr
1cf68 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 = (pC = p->apCs
1cf69 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 r[i])->pCursor)!
1cf6a 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 20 72 6f =0 ){. i64 ro
1cf6b 77 69 64 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 wid;. rc = sq
1cf6c 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d lite3VdbeCursorM
1cf6d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20 20 69 oveto(pC);. i
1cf6e 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f f( rc ) goto abo
1cf6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1cf70 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1cf71 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d >deferredMoveto=
1cf72 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
1cf73 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 ( pC->isTable==0
1cf74 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 2d );. if( !pC-
1cf75 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 >nullRow ){.
1cf76 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1cf77 62 65 49 64 78 52 6f 77 69 64 28 70 43 72 73 72 beIdxRowid(pCrsr
1cf78 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20 20 20 20 , &rowid);.
1cf79 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1cf7a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
1cf7b 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1cf7c 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 error;. }.
1cf7d 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 MemSetTypeF
1cf7e 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1cf7f 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e t);. pOut->
1cf80 75 2e 69 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20 u.i = rowid;.
1cf81 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1cf82 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 }../* Opcode: Id
1cf83 78 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20 xGE P1 P2 P3 P4
1cf84 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 P5.**.** The P4
1cf85 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 register values
1cf86 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 beginning with P
1cf87 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 3 form an unpack
1cf88 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 ed index .** key
1cf89 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 that omits the
1cf8a 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 ROWID. Compare
1cf8b 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 this key value a
1cf8c 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 gainst the index
1cf8d 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 .** that P1 is
1cf8e 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
1cf8f 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 ng to, ignoring
1cf90 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 the ROWID on the
1cf91 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a P1 index..**.**
1cf92 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 If the P1 index
1cf93 20 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65 entry is greate
1cf94 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
1cf95 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 to the key value
1cf96 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f .** then jump to
1cf97 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 P2. Otherwise
1cf98 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 fall through to
1cf99 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1cf9a 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 tion..**.** If P
1cf9b 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 5 is non-zero th
1cf9c 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 en the key value
1cf9d 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 is increased by
1cf9e 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 an epsilon .**
1cf9f 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d prior to the com
1cfa0 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d parison. This m
1cfa1 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77 ake the opcode w
1cfa2 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65 ork like IdxGT e
1cfa3 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 xcept.** that if
1cfa4 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 the key from re
1cfa5 67 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70 gister P3 is a p
1cfa6 72 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79 refix of the key
1cfa7 20 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a in the cursor,.
1cfa8 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 ** the result is
1cfa9 20 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69 false whereas i
1cfaa 74 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20 t would be true
1cfab 77 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f with IdxGT..*/./
1cfac 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 * Opcode: IdxLT
1cfad 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a P1 P2 P3 * P5.**
1cfae 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 .** The P4 regis
1cfaf 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e ter values begin
1cfb0 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 ning with P3 for
1cfb1 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e m an unpacked in
1cfb2 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 dex .** key that
1cfb3 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 omits the ROWID
1cfb4 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 . Compare this
1cfb5 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 key value agains
1cfb6 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 t the index .**
1cfb7 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 that P1 is curre
1cfb8 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
1cfb9 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 , ignoring the R
1cfba 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 OWID on the P1 i
1cfbb 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ndex..**.** If t
1cfbc 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 he P1 index entr
1cfbd 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 y is less than t
1cfbe 68 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65 he key value the
1cfbf 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1cfc0 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 Otherwise fall
1cfc1 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1cfc2 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ext instruction.
1cfc3 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 .**.** If P5 is
1cfc4 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 non-zero then th
1cfc5 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 e key value is i
1cfc6 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 ncreased by an e
1cfc7 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a psilon prior .**
1cfc8 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 to the comparis
1cfc9 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 on. This makes
1cfca 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 the opcode work
1cfcb 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 like IdxLE..*/.c
1cfcc 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 ase OP_IdxLT:
1cfcd 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1cfce 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 in3 */.case OP_I
1cfcf 64 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f dxGE: { /
1cfd0 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 * jump, in3 */.
1cfd1 20 69 6e 74 20 69 3d 20 70 4f 70 2d 3e 70 31 3b int i= pOp->p1;
1cfd2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1cfd3 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e C;.. assert( i>
1cfd4 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
1cfd5 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 or );. assert(
1cfd6 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 p->apCsr[i]!=0 )
1cfd7 3b 0a 20 20 69 66 28 20 28 70 43 20 3d 20 70 2d ;. if( (pC = p-
1cfd8 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 >apCsr[i])->pCur
1cfd9 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e sor!=0 ){. in
1cfda 74 20 72 65 73 3b 0a 20 20 20 20 55 6e 70 61 63 t res;. Unpac
1cfdb 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 kedRecord r;.
1cfdc 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 assert( pC->def
1cfdd 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1cfde 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f ;. assert( pO
1cfdf 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d p->p5==0 || pOp-
1cfe0 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 >p5==1 );. as
1cfe1 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 sert( pOp->p4typ
1cfe2 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 e==P4_INT32 );.
1cfe3 20 20 20 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 r.pKeyInfo =
1cfe4 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 pC->pKeyInfo;.
1cfe5 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 r.nField = (u1
1cfe6 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 6)pOp->p4.i;.
1cfe7 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a if( pOp->p5 ){.
1cfe8 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 r.flags =
1cfe9 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
1cfea 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f | UNPACKED_IGNO
1cfeb 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 RE_ROWID;. }e
1cfec 6c 73 65 7b 0a 20 20 20 20 20 20 72 2e 66 6c 61 lse{. r.fla
1cfed 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47 gs = UNPACKED_IG
1cfee 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 NORE_ROWID;.
1cfef 7d 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 }. r.aMem = &
1cff0 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1cff1 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1cff2 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 e3VdbeIdxKeyComp
1cff3 61 72 65 28 70 43 2c 20 26 72 2c 20 26 72 65 73 are(pC, &r, &res
1cff4 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e );. if( pOp->
1cff5 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 opcode==OP_IdxLT
1cff6 20 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 ){. res =
1cff7 2d 72 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b -res;. }else{
1cff8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1cff9 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 Op->opcode==OP_I
1cffa 64 78 47 45 20 29 3b 0a 20 20 20 20 20 20 72 65 dxGE );. re
1cffb 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 s++;. }. i
1cffc 66 28 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 f( res>0 ){.
1cffd 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1cffe 20 31 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 1 ;. }. }.
1cfff 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d000 63 6f 64 65 3a 20 44 65 73 74 72 6f 79 20 50 31 code: Destroy P1
1d001 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1d002 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 Delete an entir
1d003 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 e database table
1d004 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 or index whose
1d005 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 root page in the
1d006 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
1d007 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 e is given by P1
1d008 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c ..**.** The tabl
1d009 65 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65 e being destroye
1d00a 64 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e d is in the main
1d00b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1d00c 66 20 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 f P3==0. If.**
1d00d 50 33 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 P3==1 then the t
1d00e 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 able to be clear
1d00f 20 69 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c is in the auxil
1d010 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 iary database fi
1d011 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 le.** that is us
1d012 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c ed to store tabl
1d013 65 73 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 es create using
1d014 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 CREATE TEMPORARY
1d015 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 TABLE..**.** If
1d016 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 AUTOVACUUM is e
1d017 6e 61 62 6c 65 64 20 74 68 65 6e 20 69 74 20 69 nabled then it i
1d018 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 s possible that
1d019 61 6e 6f 74 68 65 72 20 72 6f 6f 74 20 70 61 67 another root pag
1d01a 65 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f e.** might be mo
1d01b 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 ved into the new
1d01c 6c 79 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 ly deleted root
1d01d 70 61 67 65 20 69 6e 20 6f 72 64 65 72 20 74 6f page in order to
1d01e 20 6b 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f keep all.** roo
1d01f 74 20 70 61 67 65 73 20 63 6f 6e 74 69 67 75 6f t pages contiguo
1d020 75 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e us at the beginn
1d021 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 ing of the datab
1d022 61 73 65 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 ase. The former
1d023 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 .** value of the
1d024 20 72 6f 6f 74 20 70 61 67 65 20 74 68 61 74 20 root page that
1d025 6d 6f 76 65 64 20 2d 20 69 74 73 20 76 61 6c 75 moved - its valu
1d026 65 20 62 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 e before the mov
1d027 65 20 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 e occurred -.**
1d028 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 is stored in reg
1d029 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 6e 6f ister P2. If no
1d02a 20 70 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 page .** moveme
1d02b 6e 74 20 77 61 73 20 72 65 71 75 69 72 65 64 20 nt was required
1d02c 28 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62 (because the tab
1d02d 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 le being dropped
1d02e 20 77 61 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a was already .**
1d02f 20 74 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e the last one in
1d030 20 74 68 65 20 64 61 74 61 62 61 73 65 29 20 74 the database) t
1d031 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 hen a zero is st
1d032 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1d033 20 50 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 P2..** If AUTOV
1d034 41 43 55 55 4d 20 69 73 20 64 69 73 61 62 6c 65 ACUUM is disable
1d035 64 20 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 d then a zero is
1d036 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1d037 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 ter P2..**.** Se
1d038 65 20 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f e also: Clear.*/
1d039 0a 63 61 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 .case OP_Destroy
1d03a 3a 20 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d : { /* out2-
1d03b 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1d03c 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e int iMoved;. in
1d03d 74 20 69 43 6e 74 3b 0a 23 69 66 6e 64 65 66 20 t iCnt;.#ifndef
1d03e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1d03f 55 41 4c 54 41 42 4c 45 0a 20 20 56 64 62 65 20 UALTABLE. Vdbe
1d040 2a 70 56 64 62 65 3b 0a 20 20 69 43 6e 74 20 3d *pVdbe;. iCnt =
1d041 20 30 3b 0a 20 20 66 6f 72 28 70 56 64 62 65 3d 0;. for(pVdbe=
1d042 64 62 2d 3e 70 56 64 62 65 3b 20 70 56 64 62 65 db->pVdbe; pVdbe
1d043 3b 20 70 56 64 62 65 3d 70 56 64 62 65 2d 3e 70 ; pVdbe=pVdbe->p
1d044 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
1d045 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 Vdbe->magic==VDB
1d046 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 E_MAGIC_RUN && p
1d047 56 64 62 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68 Vdbe->inVtabMeth
1d048 6f 64 3c 32 20 26 26 20 70 56 64 62 65 2d 3e 70 od<2 && pVdbe->p
1d049 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 43 c>=0 ){. iC
1d04a 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a nt++;. }. }.
1d04b 23 65 6c 73 65 0a 20 20 69 43 6e 74 20 3d 20 64 #else. iCnt = d
1d04c 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
1d04d 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69 ;.#endif. if( i
1d04e 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 72 63 20 Cnt>1 ){. rc
1d04f 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b = SQLITE_LOCKED;
1d050 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 . p->errorAct
1d051 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a ion = OE_Abort;.
1d052 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
1d053 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a iDb = pOp->p3;.
1d054 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6e 74 assert( iCnt
1d055 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==1 );. asser
1d056 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1d057 20 26 20 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 & (1<<iDb))!=0
1d058 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1d059 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c te3BtreeDropTabl
1d05a 65 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 e(db->aDb[iDb].p
1d05b 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 69 4d Bt, pOp->p1, &iM
1d05c 6f 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 oved);. MemSe
1d05d 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1d05e 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f MEM_Int);. pO
1d05f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64 ut->u.i = iMoved
1d060 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1d061 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1d062 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1d063 49 54 45 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64 ITE_OK && iMoved
1d064 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c !=0 ){. sql
1d065 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 ite3RootPageMove
1d066 64 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2c d(&db->aDb[iDb],
1d067 20 69 4d 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31 iMoved, pOp->p1
1d068 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
1d069 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d06a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72 /* Opcode: Clear
1d06b 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 P1 P2 P3.**.**
1d06c 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65 Delete all conte
1d06d 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 nts of the datab
1d06e 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 ase table or ind
1d06f 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 ex whose root pa
1d070 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 ge.** in the dat
1d071 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 67 69 abase file is gi
1d072 76 65 6e 20 62 79 20 50 31 2e 20 20 42 75 74 2c ven by P1. But,
1d073 20 75 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c unlike Destroy,
1d074 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76 do not.** remov
1d075 65 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 e the table or i
1d076 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 64 61 ndex from the da
1d077 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1d078 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 ** The table bei
1d079 6e 67 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 ng clear is in t
1d07a 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1d07b 20 66 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 file if P2==0.
1d07c 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 If.** P2==1 the
1d07d 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 n the table to b
1d07e 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 e clear is in th
1d07f 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 e auxiliary data
1d080 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 base file.** tha
1d081 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f t is used to sto
1d082 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 re tables create
1d083 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 using CREATE TE
1d084 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a MPORARY TABLE..*
1d085 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 33 20 76 *.** If the P3 v
1d086 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f alue is non-zero
1d087 2c 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 , then the table
1d088 20 72 65 66 65 72 72 65 64 20 74 6f 20 6d 75 73 referred to mus
1d089 74 20 62 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 t be an.** intke
1d08a 79 20 74 61 62 6c 65 20 28 61 6e 20 53 51 4c 20 y table (an SQL
1d08b 74 61 62 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 6e table, not an in
1d08c 64 65 78 29 2e 20 49 6e 20 74 68 69 73 20 63 61 dex). In this ca
1d08d 73 65 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 se the row chang
1d08e 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 69 73 20 69 e .** count is i
1d08f 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 ncremented by th
1d090 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 e number of rows
1d091 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 in the table be
1d092 69 6e 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2a ing cleared. .**
1d093 20 49 66 20 50 33 20 69 73 20 67 72 65 61 74 65 If P3 is greate
1d094 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 r than zero, the
1d095 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 n the value stor
1d096 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1d097 33 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63 3 is.** also inc
1d098 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 remented by the
1d099 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 number of rows i
1d09a 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e n the table bein
1d09b 67 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a g cleared..**.**
1d09c 20 53 65 65 20 61 6c 73 6f 3a 20 44 65 73 74 72 See also: Destr
1d09d 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c oy.*/.case OP_Cl
1d09e 65 61 72 3a 20 7b 0a 20 20 69 6e 74 20 6e 43 68 ear: {. int nCh
1d09f 61 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 ange = 0;. asse
1d0a0 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
1d0a1 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 k & (1<<pOp->p2)
1d0a2 29 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 )!=0 );. rc = s
1d0a3 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
1d0a4 54 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d Table(. db-
1d0a5 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 >aDb[pOp->p2].pB
1d0a6 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 t, pOp->p1, (pOp
1d0a7 2d 3e 70 33 20 3f 20 26 6e 43 68 61 6e 67 65 20 ->p3 ? &nChange
1d0a8 3a 20 30 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 : 0). );. if(
1d0a9 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 70 pOp->p3 ){. p
1d0aa 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68 ->nChange += nCh
1d0ab 61 6e 67 65 3b 0a 20 20 20 20 69 66 28 20 70 4f ange;. if( pO
1d0ac 70 2d 3e 70 33 3e 30 20 29 7b 0a 20 20 20 20 20 p->p3>0 ){.
1d0ad 20 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 p->aMem[pOp->p3
1d0ae 5d 2e 75 2e 69 20 2b 3d 20 6e 43 68 61 6e 67 65 ].u.i += nChange
1d0af 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1d0b0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d0b1 65 3a 20 43 72 65 61 74 65 54 61 62 6c 65 20 50 e: CreateTable P
1d0b2 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1d0b3 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
1d0b4 74 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 table in the mai
1d0b5 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
1d0b6 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 if P1==0 or in t
1d0b7 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 he.** auxiliary
1d0b8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 database file if
1d0b9 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 P1==1 or in an
1d0ba 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1d0bb 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 e if.** P1>1. W
1d0bc 72 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 rite the root pa
1d0bd 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
1d0be 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a new table into.
1d0bf 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a ** register P2.*
1d0c0 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 *.** The differe
1d0c1 6e 63 65 20 62 65 74 77 65 65 6e 20 61 20 74 61 nce between a ta
1d0c2 62 6c 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 ble and an index
1d0c3 20 69 73 20 74 68 69 73 3a 20 20 41 20 74 61 62 is this: A tab
1d0c4 6c 65 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 le must.** have
1d0c5 61 20 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72 a 4-byte integer
1d0c6 20 6b 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76 key and can hav
1d0c7 65 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61 e arbitrary data
1d0c8 2e 20 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 . An index.** h
1d0c9 61 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 as an arbitrary
1d0ca 6b 65 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e key but no data.
1d0cb 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1d0cc 20 43 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a CreateIndex.*/.
1d0cd 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 /* Opcode: Creat
1d0ce 65 49 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a eIndex P1 P2 * *
1d0cf 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 *.**.** Allocat
1d0d0 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e e a new index in
1d0d1 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
1d0d2 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 se file if P1==0
1d0d3 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 or in the.** au
1d0d4 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 xiliary database
1d0d5 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f file if P1==1 o
1d0d6 72 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 r in an attached
1d0d7 20 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 database if.**
1d0d8 50 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 P1>1. Write the
1d0d9 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 root page numbe
1d0da 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 r of the new tab
1d0db 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 le into.** regis
1d0dc 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 ter P2..**.** Se
1d0dd 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 e documentation
1d0de 6f 6e 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c on OP_CreateTabl
1d0df 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c e for additional
1d0e0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
1d0e1 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49 .case OP_CreateI
1d0e2 6e 64 65 78 3a 20 20 20 20 20 20 20 20 20 20 20 ndex:
1d0e3 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1d0e4 61 73 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 ase */.case OP_C
1d0e5 72 65 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 reateTable: {
1d0e6 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1d0e7 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 rerelease */. i
1d0e8 6e 74 20 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 69 nt pgno = 0;. i
1d0e9 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 20 2a nt flags;. Db *
1d0ea 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 pDb;. assert( p
1d0eb 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1d0ec 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a ->p1<db->nDb );.
1d0ed 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1d0ee 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1d0ef 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 p->p1))!=0 );.
1d0f0 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 pDb = &db->aDb[p
1d0f1 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1d0f2 74 28 20 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 t( pDb->pBt!=0 )
1d0f3 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 ;. if( pOp->opc
1d0f4 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61 ode==OP_CreateTa
1d0f5 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 66 6c ble ){. /* fl
1d0f6 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b ags = BTREE_INTK
1d0f7 45 59 3b 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 EY; */. flags
1d0f8 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 = BTREE_LEAFDAT
1d0f9 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 0a A|BTREE_INTKEY;.
1d0fa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6c 61 }else{. fla
1d0fb 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44 gs = BTREE_ZEROD
1d0fc 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 ATA;. }. rc =
1d0fd 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 sqlite3BtreeCrea
1d0fe 74 65 54 61 62 6c 65 28 70 44 62 2d 3e 70 42 74 teTable(pDb->pBt
1d0ff 2c 20 26 70 67 6e 6f 2c 20 66 6c 61 67 73 29 3b , &pgno, flags);
1d100 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 . pOut->u.i = p
1d101 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 gno;. MemSetTyp
1d102 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1d103 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Int);. break;.}
1d104 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 ../* Opcode: Par
1d105 73 65 53 63 68 65 6d 61 20 50 31 20 50 32 20 2a seSchema P1 P2 *
1d106 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 P4 *.**.** Read
1d107 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 65 and parse all e
1d108 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20 ntries from the
1d109 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 SQLITE_MASTER ta
1d10a 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 ble of database
1d10b 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 P1.** that match
1d10c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1d10d 65 20 50 34 2e 20 20 50 32 20 69 73 20 74 68 65 e P4. P2 is the
1d10e 20 22 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 20 "force" flag.
1d10f 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 68 Always do.** th
1d110 65 20 70 61 72 73 69 6e 67 20 69 66 20 50 32 20 e parsing if P2
1d111 69 73 20 74 72 75 65 2e 20 20 49 66 20 50 32 20 is true. If P2
1d112 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 is false, then t
1d113 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
1d114 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 .** no-op if the
1d115 20 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 63 schema is not c
1d116 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 2e urrently loaded.
1d117 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1d118 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 61 , if P2.** is fa
1d119 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 54 45 5f lse, the SQLITE_
1d11a 4d 41 53 54 45 52 20 74 61 62 6c 65 20 69 73 20 MASTER table is
1d11b 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 66 20 74 only parsed if t
1d11c 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 0a 2a he rest of the.*
1d11d 2a 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65 * schema is alre
1d11e 61 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 ady loaded into
1d11f 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 the symbol table
1d120 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ..**.** This opc
1d121 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 ode invokes the
1d122 70 61 72 73 65 72 20 74 6f 20 63 72 65 61 74 65 parser to create
1d123 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d a new virtual m
1d124 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 achine,.** then
1d125 72 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72 runs the new vir
1d126 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 tual machine. I
1d127 74 20 69 73 20 74 68 75 73 20 61 20 72 65 2d 65 t is thus a re-e
1d128 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a ntrant opcode..*
1d129 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53 /.case OP_ParseS
1d12a 63 68 65 6d 61 3a 20 7b 0a 20 20 69 6e 74 20 69 chema: {. int i
1d12b 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 Db = pOp->p1;.
1d12c 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 assert( iDb>=0 &
1d12d 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b & iDb<db->nDb );
1d12e 0a 0a 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 .. /* If pOp->p
1d12f 32 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 2 is 0, then thi
1d130 73 20 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e s opcode is bein
1d131 67 20 65 78 65 63 75 74 65 64 20 74 6f 20 72 65 g executed to re
1d132 61 64 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 ad a. ** single
1d133 20 72 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c row, for exampl
1d134 65 20 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73 e the row corres
1d135 70 6f 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 ponding to a new
1d136 20 69 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 index. ** crea
1d137 74 65 64 20 62 79 20 74 68 69 73 20 56 44 42 45 ted by this VDBE
1d138 2c 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 , from the sqlit
1d139 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 e_master table.
1d13a 49 74 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 It only. ** doe
1d13b 73 20 74 68 69 73 20 69 66 20 74 68 65 20 63 6f s this if the co
1d13c 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d rresponding in-m
1d13d 65 6d 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20 emory schema is
1d13e 63 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c currently. ** l
1d13f 6f 61 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65 oaded. Otherwise
1d140 2c 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 , the new index
1d141 64 65 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 definition can b
1d142 65 20 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 e loaded along.
1d143 20 2a 2a 20 77 69 74 68 20 74 68 65 20 72 65 73 ** with the res
1d144 74 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 t of the schema
1d145 77 68 65 6e 20 69 74 20 69 73 20 72 65 71 75 69 when it is requi
1d146 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 red.. **. ** A
1d147 6c 74 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65 lthough the mute
1d148 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 x on the BtShare
1d149 64 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f d object that co
1d14a 72 72 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a rresponds to. *
1d14b 2a 20 64 61 74 61 62 61 73 65 20 69 44 62 20 28 * database iDb (
1d14c 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
1d14d 74 61 69 6e 69 6e 67 20 74 68 65 20 73 71 6c 69 taining the sqli
1d14e 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a te_master table.
1d14f 20 20 2a 2a 20 72 65 61 64 20 62 79 20 74 68 69 ** read by thi
1d150 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 29 20 69 s instruction) i
1d151 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 s currently held
1d152 2c 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 , it is necessar
1d153 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e y to. ** obtain
1d154 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 the mutexes on
1d155 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 all attached dat
1d156 61 62 61 73 65 73 20 62 65 66 6f 72 65 20 63 68 abases before ch
1d157 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a 2a 20 74 ecking if. ** t
1d158 68 65 20 73 63 68 65 6d 61 20 6f 66 20 69 44 62 he schema of iDb
1d159 20 69 73 20 6c 6f 61 64 65 64 2e 20 54 68 69 73 is loaded. This
1d15a 20 69 73 20 62 65 63 61 75 73 65 2c 20 61 74 20 is because, at
1d15b 74 68 65 20 73 74 61 72 74 20 6f 66 0a 20 20 2a the start of. *
1d15c 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 * the sqlite3_ex
1d15d 65 63 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 2c ec() call below,
1d15e 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 SQLite will inv
1d15f 6f 6b 65 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 oke . ** sqlite
1d160 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 3BtreeEnterAll()
1d161 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65 78 65 73 . If all mutexes
1d162 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 are not already
1d163 20 68 65 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20 held, the. **
1d164 69 44 62 20 6d 75 74 65 78 20 6d 61 79 20 62 65 iDb mutex may be
1d165 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 72 65 6c temporarily rel
1d166 65 61 73 65 64 20 74 6f 20 61 76 6f 69 64 20 64 eased to avoid d
1d167 65 61 64 6c 6f 63 6b 2e 20 49 66 20 0a 20 20 2a eadlock. If . *
1d168 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 * this happens,
1d169 74 68 65 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 then some other
1d16a 74 68 72 65 61 64 20 6d 61 79 20 64 65 6c 65 74 thread may delet
1d16b 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 e the in-memory
1d16c 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6f 66 20 . ** schema of
1d16d 64 61 74 61 62 61 73 65 20 69 44 62 20 62 65 66 database iDb bef
1d16e 6f 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 ore the SQL stat
1d16f 65 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 ement runs. The
1d170 73 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c schema. ** will
1d171 20 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 not be reloaded
1d172 20 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d becuase the db-
1d173 3e 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 >init.busy flag
1d174 69 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a is set. This. *
1d175 2a 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 * can result in
1d176 61 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 a "no such table
1d177 3a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 : sqlite_master"
1d178 20 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 or "malformed.
1d179 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 ** database sch
1d17a 65 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 ema" error being
1d17b 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
1d17c 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 user.. */. as
1d17d 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
1d17e 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d eeHoldsMutex(db-
1d17f 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29 >aDb[iDb].pBt) )
1d180 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1d181 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 EnterAll(db);.
1d182 69 66 28 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44 if( pOp->p2 || D
1d183 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c bHasProperty(db,
1d184 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c iDb, DB_SchemaL
1d185 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 63 6f oaded) ){. co
1d186 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nst char *zMaste
1d187 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 r = SCHEMA_TABLE
1d188 28 69 44 62 29 3b 0a 20 20 20 20 63 68 61 72 20 (iDb);. char
1d189 2a 7a 53 71 6c 3b 0a 20 20 20 20 49 6e 69 74 44 *zSql;. InitD
1d18a 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20 ata initData;.
1d18b 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 initData.db =
1d18c 64 62 3b 0a 20 20 20 20 69 6e 69 74 44 61 74 61 db;. initData
1d18d 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a .iDb = pOp->p1;.
1d18e 20 20 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45 initData.pzE
1d18f 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 rrMsg = &p->zErr
1d190 4d 73 67 3b 0a 20 20 20 20 7a 53 71 6c 20 3d 20 Msg;. zSql =
1d191 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1d192 62 2c 0a 20 20 20 20 20 20 20 22 53 45 4c 45 43 b,. "SELEC
1d193 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 T name, rootpage
1d194 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e , sql FROM '%q'.
1d195 25 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 20 %s WHERE %s",.
1d196 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 db->aDb[iDb
1d197 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 ].zName, zMaster
1d198 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 , pOp->p4.z);.
1d199 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b if( zSql==0 ){
1d19a 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1d19b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 TE_NOMEM;. }e
1d19c 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 lse{. (void
1d19d 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 )sqlite3SafetyOf
1d19e 66 28 64 62 29 3b 0a 20 20 20 20 20 20 61 73 73 f(db);. ass
1d19f 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 ert( db->init.bu
1d1a0 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 sy==0 );. d
1d1a1 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 b->init.busy = 1
1d1a2 3b 0a 20 20 20 20 20 20 69 6e 69 74 44 61 74 61 ;. initData
1d1a3 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b .rc = SQLITE_OK;
1d1a4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
1d1a5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1d1a6 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 );. rc = s
1d1a7 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 qlite3_exec(db,
1d1a8 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 zSql, sqlite3Ini
1d1a9 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74 tCallback, &init
1d1aa 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 20 20 Data, 0);.
1d1ab 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1d1ac 4b 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74 K ) rc = initDat
1d1ad 61 2e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 a.rc;. sqli
1d1ae 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 te3DbFree(db, zS
1d1af 71 6c 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 ql);. db->i
1d1b0 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 nit.busy = 0;.
1d1b1 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1d1b2 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 3SafetyOn(db);.
1d1b3 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
1d1b4 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
1d1b5 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 db);. if( rc==S
1d1b6 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 QLITE_NOMEM ){.
1d1b7 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
1d1b8 20 20 7d 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d }. break; .}
1d1b9 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
1d1ba 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 QLITE_OMIT_ANALY
1d1bb 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 ZE) && !defined(
1d1bc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 SQLITE_OMIT_PARS
1d1bd 45 52 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c ER)./* Opcode: L
1d1be 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 31 20 2a oadAnalysis P1 *
1d1bf 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 * * *.**.** Rea
1d1c0 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 d the sqlite_sta
1d1c1 74 31 20 74 61 62 6c 65 20 66 6f 72 20 64 61 74 t1 table for dat
1d1c2 61 62 61 73 65 20 50 31 20 61 6e 64 20 6c 6f 61 abase P1 and loa
1d1c3 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a d the content.**
1d1c4 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 69 of that table i
1d1c5 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c nto the internal
1d1c6 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c index hash tabl
1d1c7 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 e. This will ca
1d1c8 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c 79 use.** the analy
1d1c9 73 69 73 20 74 6f 20 62 65 20 75 73 65 64 20 77 sis to be used w
1d1ca 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20 61 6c hen preparing al
1d1cb 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71 75 65 l subsequent que
1d1cc 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 ries..*/.case OP
1d1cd 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20 7b _LoadAnalysis: {
1d1ce 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 . int iDb = pOp
1d1cf 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p1;. assert(
1d1d0 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 iDb>=0 && iDb<db
1d1d1 2d 3e 6e 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 ->nDb );. rc =
1d1d2 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c sqlite3AnalysisL
1d1d3 6f 61 64 28 64 62 2c 20 69 44 62 29 3b 0a 20 20 oad(db, iDb);.
1d1d4 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 break; .}.#endi
1d1d5 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
1d1d6 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a LITE_OMIT_ANALYZ
1d1d7 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 E) && !defined(S
1d1d8 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 QLITE_OMIT_PARSE
1d1d9 52 29 20 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 R) */../* Opcod
1d1da 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20 e: DropTable P1
1d1db 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 * * P4 *.**.** R
1d1dc 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e emove the intern
1d1dd 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 al (in-memory) d
1d1de 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 ata structures t
1d1df 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 hat describe.**
1d1e0 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 the table named
1d1e1 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 P4 in database P
1d1e2 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 1. This is call
1d1e3 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 ed after a table
1d1e4 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 .** is dropped i
1d1e5 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1d1e6 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
1d1e7 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1d1e8 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e he.** schema con
1d1e9 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 sistent with wha
1d1ea 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f t is on disk..*/
1d1eb 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62 .case OP_DropTab
1d1ec 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 le: {. sqlite3U
1d1ed 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
1d1ee 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c ble(db, pOp->p1,
1d1ef 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 pOp->p4.z);. b
1d1f0 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d1f1 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 de: DropIndex P1
1d1f2 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1d1f3 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 Remove the inter
1d1f4 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 nal (in-memory)
1d1f5 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 data structures
1d1f6 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a that describe.**
1d1f7 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 the index named
1d1f8 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 P4 in database
1d1f9 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c P1. This is cal
1d1fa 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 led after an ind
1d1fb 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 ex.** is dropped
1d1fc 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 in order to kee
1d1fd 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 p the internal r
1d1fe 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
1d1ff 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 the.** schema c
1d200 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 onsistent with w
1d201 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a hat is on disk..
1d202 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 */.case OP_DropI
1d203 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65 ndex: {. sqlite
1d204 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
1d205 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70 Index(db, pOp->p
1d206 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 1, pOp->p4.z);.
1d207 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d208 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 code: DropTrigge
1d209 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a r P1 * * P4 *.**
1d20a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 .** Remove the i
1d20b 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f nternal (in-memo
1d20c 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75 ry) data structu
1d20d 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62 res that describ
1d20e 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 e.** the trigger
1d20f 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 named P4 in dat
1d210 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 abase P1. This
1d211 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
1d212 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 a trigger.** is
1d213 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 dropped in order
1d214 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 to keep the int
1d215 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
1d216 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 tion of the.** s
1d217 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 chema consistent
1d218 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e with what is on
1d219 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f disk..*/.case O
1d21a 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b P_DropTrigger: {
1d21b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b . sqlite3Unlink
1d21c 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 AndDeleteTrigger
1d21d 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f (db, pOp->p1, pO
1d21e 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 p->p4.z);. brea
1d21f 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 k;.}...#ifndef S
1d220 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
1d221 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 RITY_CHECK./* Op
1d222 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43 code: IntegrityC
1d223 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a k P1 P2 P3 * P5.
1d224 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c **.** Do an anal
1d225 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72 ysis of the curr
1d226 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62 ently open datab
1d227 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a ase. Store in.*
1d228 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68 * register P1 th
1d229 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 e text of an err
1d22a 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72 or message descr
1d22b 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65 ibing any proble
1d22c 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f ms..** If no pro
1d22d 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c blems are found,
1d22e 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e store a NULL in
1d22f 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1d230 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72 .** The register
1d231 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 P3 contains the
1d232 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1d233 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72 of allowed error
1d234 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 s..** At most re
1d235 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c g(P3) errors wil
1d236 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a l be reported..*
1d237 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
1d238 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 , the analysis s
1d239 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 tops as soon as
1d23a 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 reg(P1) errors a
1d23b 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 re .** seen. Re
1d23c 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64 g(P1) is updated
1d23d 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 with the number
1d23e 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 of errors remai
1d23f 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ning..**.** The
1d240 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
1d241 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 s of all tables
1d242 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1d243 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73 are integer.** s
1d244 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29 tored in reg(P1)
1d245 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67 , reg(P1+1), reg
1d246 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 (P1+2), .... Th
1d247 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65 ere are P2 table
1d248 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a s.** total..**.*
1d249 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a * If P5 is not z
1d24a 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69 ero, the check i
1d24b 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75 s done on the au
1d24c 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 xiliary database
1d24d 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68 .** file, not th
1d24e 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1d24f 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 file..**.** This
1d250 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 opcode is used
1d251 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1d252 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b integrity_check
1d253 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 pragma..*/.case
1d254 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a OP_IntegrityCk:
1d255 20 7b 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 {. int nRoot;
1d256 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d257 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63 f tables to chec
1d258 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 k. (Number of r
1d259 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 oot pages.) */.
1d25a 20 69 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 int *aRoot;
1d25b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f /* Array of roo
1d25c 74 70 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f tpage numbers fo
1d25d 72 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63 r tables to be c
1d25e 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 hecked */. int
1d25f 6a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c j; /* L
1d260 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1d261 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 int nErr;
1d262 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 /* Number of er
1d263 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f rors reported */
1d264 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 . char *z;
1d265 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 /* Text of th
1d266 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a e error report *
1d267 2f 0a 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 /. Mem *pnErr;
1d268 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
1d269 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 keeping track of
1d26a 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e errors remainin
1d26b 67 20 2a 2f 0a 20 20 0a 20 20 6e 52 6f 6f 74 20 g */. . nRoot
1d26c 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 = pOp->p2;. ass
1d26d 65 72 74 28 20 6e 52 6f 6f 74 3e 30 20 29 3b 0a ert( nRoot>0 );.
1d26e 20 20 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 aRoot = sqlite
1d26f 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1d270 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 52 sizeof(int)*(nR
1d271 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 oot+1) );. if(
1d272 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20 aRoot==0 ) goto
1d273 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74 no_mem;. assert
1d274 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 ( pOp->p3>0 && p
1d275 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p3<=p->nMem
1d276 29 3b 0a 20 20 70 6e 45 72 72 20 3d 20 26 70 2d );. pnErr = &p-
1d277 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1d278 20 20 61 73 73 65 72 74 28 20 28 70 6e 45 72 72 assert( (pnErr
1d279 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1d27a 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 t)!=0 );. asser
1d27b 74 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 t( (pnErr->flags
1d27c 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f & (MEM_Str|MEM_
1d27d 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 Blob))==0 );. p
1d27e 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In1 = &p->aMem[p
1d27f 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 6a Op->p1];. for(j
1d280 3d 30 3b 20 6a 3c 6e 52 6f 6f 74 3b 20 6a 2b 2b =0; j<nRoot; j++
1d281 29 7b 0a 20 20 20 20 61 52 6f 6f 74 5b 6a 5d 20 ){. aRoot[j]
1d282 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 = (int)sqlite3Vd
1d283 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 beIntValue(&pIn1
1d284 5b 6a 5d 29 3b 0a 20 20 7d 0a 20 20 61 52 6f 6f [j]);. }. aRoo
1d285 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65 t[j] = 0;. asse
1d286 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e rt( pOp->p5<db->
1d287 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nDb );. assert(
1d288 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1d289 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d (1<<pOp->p5))!=
1d28a 30 20 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 0 );. z = sqlit
1d28b 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 e3BtreeIntegrity
1d28c 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b 70 4f Check(db->aDb[pO
1d28d 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 61 52 6f 6f p->p5].pBt, aRoo
1d28e 74 2c 20 6e 52 6f 6f 74 2c 0a 20 20 20 20 20 20 t, nRoot,.
1d28f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d290 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 (int)
1d291 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 6e 45 72 pnErr->u.i, &nEr
1d292 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
1d293 72 65 65 28 64 62 2c 20 61 52 6f 6f 74 29 3b 0a ree(db, aRoot);.
1d294 20 20 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20 pnErr->u.i -=
1d295 6e 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 33 56 nErr;. sqlite3V
1d296 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49 dbeMemSetNull(pI
1d297 6e 31 29 3b 0a 20 20 69 66 28 20 6e 45 72 72 3d n1);. if( nErr=
1d298 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
1d299 28 20 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 ( z==0 );. }els
1d29a 65 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 e if( z==0 ){.
1d29b 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1d29c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
1d29d 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
1d29e 28 70 49 6e 31 2c 20 7a 2c 20 2d 31 2c 20 53 51 (pIn1, z, -1, SQ
1d29f 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 LITE_UTF8, sqlit
1d2a0 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20 e3_free);. }.
1d2a1 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1d2a2 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c IZE(pIn1);. sql
1d2a3 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
1d2a4 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63 coding(pIn1, enc
1d2a5 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b oding);. break;
1d2a6 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1d2a7 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
1d2a8 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 TY_CHECK */../*
1d2a9 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 41 64 Opcode: RowSetAd
1d2aa 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a d P1 P2 * * *.**
1d2ab 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 69 .** Insert the i
1d2ac 6e 74 65 67 65 72 20 76 61 6c 75 65 20 68 65 6c nteger value hel
1d2ad 64 20 62 79 20 72 65 67 69 73 74 65 72 20 50 32 d by register P2
1d2ae 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61 6e 20 into a boolean
1d2af 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20 69 6e index.** held in
1d2b0 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1d2b1 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74 69 6f 6e .** An assertion
1d2b2 20 66 61 69 6c 73 20 69 66 20 50 32 20 69 73 20 fails if P2 is
1d2b3 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a not an integer..
1d2b4 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 */.case OP_RowSe
1d2b5 74 41 64 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a tAdd: { /*
1d2b6 20 69 6e 32 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 in2 */. Mem *p
1d2b7 49 64 78 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 6c Idx;. Mem *pVal
1d2b8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1d2b9 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 >p1>0 && pOp->p1
1d2ba 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
1d2bb 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Idx = &p->aMem[p
1d2bc 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1d2bd 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 t( pOp->p2>0 &&
1d2be 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p2<=p->nMem
1d2bf 20 29 3b 0a 20 20 70 56 61 6c 20 3d 20 26 70 2d );. pVal = &p-
1d2c0 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1d2c1 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d assert( (pVal-
1d2c2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1d2c3 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 )!=0 );. if( (p
1d2c4 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Idx->flags & MEM
1d2c5 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 _RowSet)==0 ){.
1d2c6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1d2c7 6d 53 65 74 52 6f 77 53 65 74 28 70 49 64 78 29 mSetRowSet(pIdx)
1d2c8 3b 0a 20 20 20 20 69 66 28 20 28 70 49 64 78 2d ;. if( (pIdx-
1d2c9 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
1d2ca 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e Set)==0 ) goto n
1d2cb 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c o_mem;. }. sql
1d2cc 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 ite3RowSetInsert
1d2cd 28 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74 (pIdx->u.pRowSet
1d2ce 2c 20 70 56 61 6c 2d 3e 75 2e 69 29 3b 0a 20 20 , pVal->u.i);.
1d2cf 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d2d0 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61 64 20 ode: RowSetRead
1d2d1 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1d2d2 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 73 ** Extract the s
1d2d3 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20 66 72 mallest value fr
1d2d4 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 om boolean index
1d2d5 20 50 31 20 61 6e 64 20 70 75 74 20 74 68 61 74 P1 and put that
1d2d6 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a 20 72 value into.** r
1d2d7 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f 72 2c egister P3. Or,
1d2d8 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 if boolean inde
1d2d9 78 20 50 31 20 69 73 20 69 6e 69 74 69 61 6c 6c x P1 is initiall
1d2da 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65 20 50 y empty, leave P
1d2db 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 61 3.** unchanged a
1d2dc 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 nd jump to instr
1d2dd 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a 63 61 uction P2..*/.ca
1d2de 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 se OP_RowSetRead
1d2df 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d : { /* jum
1d2e0 70 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 4d 65 6d p, out3 */. Mem
1d2e1 20 2a 70 49 64 78 3b 0a 20 20 69 36 34 20 76 61 *pIdx;. i64 va
1d2e2 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 l;. assert( pOp
1d2e3 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p1>0 && pOp->p
1d2e4 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 1<=p->nMem );.
1d2e5 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
1d2e6 55 50 54 3b 0a 20 20 70 49 64 78 20 3d 20 26 70 UPT;. pIdx = &p
1d2e7 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b ->aMem[pOp->p1];
1d2e8 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1d2e9 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 em[pOp->p3];. i
1d2ea 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67 73 20 f( (pIdx->flags
1d2eb 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
1d2ec 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 52 . || sqlite3R
1d2ed 6f 77 53 65 74 4e 65 78 74 28 70 49 64 78 2d 3e owSetNext(pIdx->
1d2ee 75 2e 70 52 6f 77 53 65 74 2c 20 26 76 61 6c 29 u.pRowSet, &val)
1d2ef 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 ==0. ){. /*
1d2f0 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 The boolean inde
1d2f1 78 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20 x is empty */.
1d2f2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d2f3 53 65 74 4e 75 6c 6c 28 70 49 64 78 29 3b 0a 20 SetNull(pIdx);.
1d2f4 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1d2f5 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 - 1;. }else{.
1d2f6 20 20 2f 2a 20 41 20 76 61 6c 75 65 20 77 61 73 /* A value was
1d2f7 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 pulled from the
1d2f8 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 61 73 index */. as
1d2f9 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 sert( pOp->p3>0
1d2fa 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e && pOp->p3<=p->n
1d2fb 4d 65 6d 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 Mem );. sqlit
1d2fc 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1d2fd 34 28 70 4f 75 74 2c 20 76 61 6c 29 3b 0a 20 20 4(pOut, val);.
1d2fe 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1d2ff 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 54 Opcode: RowSetT
1d300 65 73 74 20 50 31 20 50 32 20 50 33 20 50 34 0a est P1 P2 P3 P4.
1d301 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 **.** Register P
1d302 33 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 3 is assumed to
1d303 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20 69 6e hold a 64-bit in
1d304 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49 66 20 teger value. If
1d305 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 63 register P1.** c
1d306 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 65 74 ontains a RowSet
1d307 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 74 object and that
1d308 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 63 RowSet object c
1d309 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 76 ontains.** the v
1d30a 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 33 2c alue held in P3,
1d30b 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 74 65 jump to registe
1d30c 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c r P2. Otherwise,
1d30d 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a 20 69 insert the.** i
1d30e 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 6e 74 nteger in P3 int
1d30f 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 6e 64 o the RowSet and
1d310 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f 20 continue on to
1d311 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70 63 6f the.** next opco
1d312 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f de..**.** The Ro
1d313 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20 6f wSet object is o
1d314 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74 68 65 ptimized for the
1d315 20 63 61 73 65 20 77 68 65 72 65 20 73 75 63 63 case where succ
1d316 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a 20 6f essive sets.** o
1d317 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68 65 72 f integers, wher
1d318 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e 74 61 e each set conta
1d319 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61 74 65 ins no duplicate
1d31a 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a 20 6f s. Each set.** o
1d31b 66 20 76 61 6c 75 65 73 20 69 73 20 69 64 65 6e f values is iden
1d31c 74 69 66 69 65 64 20 62 79 20 61 20 75 6e 69 71 tified by a uniq
1d31d 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54 68 65 ue P4 value. The
1d31e 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20 6d 75 first set.** mu
1d31f 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c 20 74 st have P4==0, t
1d320 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50 34 3d he final set P4=
1d321 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62 65 20 -1. P4 must be
1d322 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a 2a 20 either -1 or.**
1d323 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 46 non-negative. F
1d324 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 or non-negative
1d325 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f 6e 6c values of P4 onl
1d326 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a 2a 2a y the lower 4.**
1d327 20 62 69 74 73 20 61 72 65 20 73 69 67 6e 69 66 bits are signif
1d328 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 icant..**.** Thi
1d329 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d 69 7a s allows optimiz
1d32a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68 65 6e ations: (a) when
1d32b 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69 73 20 P4==0 there is
1d32c 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 0a no need to test.
1d32d 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20 6f 62 ** the rowset ob
1d32e 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61 73 20 ject for P3, as
1d32f 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 it is guaranteed
1d330 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 not to contain
1d331 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65 6e 20 it,.** (b) when
1d332 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69 73 20 P4==-1 there is
1d333 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73 65 72 no need to inser
1d334 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61 73 20 t the value, as
1d335 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 it will.** never
1d336 20 62 65 20 74 65 73 74 65 64 20 66 6f 72 2c 20 be tested for,
1d337 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61 20 76 and (c) when a v
1d338 61 6c 75 65 20 74 68 61 74 20 69 73 20 70 61 72 alue that is par
1d339 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a 2a 2a t of set X is.**
1d33a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65 72 65 inserted, there
1d33b 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 73 is no need to s
1d33c 65 61 72 63 68 20 74 6f 20 73 65 65 20 69 66 20 earch to see if
1d33d 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77 the same value w
1d33e 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 as.** previously
1d33f 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 72 inserted as par
1d340 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e 6c 79 t of set X (only
1d341 20 69 66 20 69 74 20 77 61 73 20 70 72 65 76 69 if it was previ
1d342 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65 ously.** inserte
1d343 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 6f 6d d as part of som
1d344 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a 2a 2f e other set)..*/
1d345 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 54 .case OP_RowSetT
1d346 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 est: {
1d347 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1d348 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
1d349 20 20 69 6e 74 20 69 53 65 74 20 3d 20 70 4f 70 int iSet = pOp
1d34a 2d 3e 70 34 2e 69 3b 0a 20 20 61 73 73 65 72 74 ->p4.i;. assert
1d34b 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d 45 ( pIn3->flags&ME
1d34c 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 M_Int );.. /* I
1d34d 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 74 68 f there is anyth
1d34e 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 ing other than a
1d34f 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 69 rowset object i
1d350 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 31 n memory cell P1
1d351 2c 0a 20 20 2a 2a 20 64 65 6c 65 74 65 20 69 74 ,. ** delete it
1d352 20 6e 6f 77 20 61 6e 64 20 69 6e 69 74 69 61 6c now and initial
1d353 69 7a 65 20 50 31 20 77 69 74 68 20 61 6e 20 65 ize P1 with an e
1d354 6d 70 74 79 20 72 6f 77 73 65 74 0a 20 20 2a 2f mpty rowset. */
1d355 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1d356 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
1d357 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )==0 ){. sqli
1d358 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 te3VdbeMemSetRow
1d359 53 65 74 28 70 49 6e 31 29 3b 0a 20 20 20 20 69 Set(pIn1);. i
1d35a 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1d35b 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
1d35c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1d35d 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
1d35e 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 Op->p4type==P4_I
1d35f 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 NT32 );. assert
1d360 28 20 69 53 65 74 3d 3d 2d 31 20 7c 7c 20 69 53 ( iSet==-1 || iS
1d361 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 et>=0 );. if( i
1d362 53 65 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 Set ){. int e
1d363 78 69 73 74 73 3b 0a 20 20 20 20 65 78 69 73 74 xists;. exist
1d364 73 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 s = sqlite3RowSe
1d365 74 54 65 73 74 28 70 49 6e 31 2d 3e 75 2e 70 52 tTest(pIn1->u.pR
1d366 6f 77 53 65 74 2c 20 0a 20 20 20 20 20 20 20 20 owSet, .
1d367 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d368 20 20 20 20 20 20 20 28 75 38 29 28 69 53 65 74 (u8)(iSet
1d369 3e 3d 30 20 3f 20 69 53 65 74 20 26 20 30 78 66 >=0 ? iSet & 0xf
1d36a 20 3a 20 30 78 66 66 29 2c 0a 20 20 20 20 20 20 : 0xff),.
1d36b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d36c 20 20 20 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 pIn3->u
1d36d 2e 69 29 3b 0a 20 20 20 20 69 66 28 20 65 78 69 .i);. if( exi
1d36e 73 74 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 sts ){. pc
1d36f 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1d370 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1d371 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 53 65 74 }. }. if( iSet
1d372 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >=0 ){. sqlit
1d373 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 e3RowSetInsert(p
1d374 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 In1->u.pRowSet,
1d375 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a pIn3->u.i);. }.
1d376 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 break;.}...#if
1d377 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d378 5f 54 52 49 47 47 45 52 0a 2f 2a 20 4f 70 63 6f _TRIGGER./* Opco
1d379 64 65 3a 20 43 6f 6e 74 65 78 74 50 75 73 68 20 de: ContextPush
1d37a 2a 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 53 61 76 * * * .**.** Sav
1d37b 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 e the current Vd
1d37c 62 65 20 63 6f 6e 74 65 78 74 20 73 75 63 68 20 be context such
1d37d 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 that it can be r
1d37e 65 73 74 6f 72 65 64 20 62 79 20 61 20 43 6f 6e estored by a Con
1d37f 74 65 78 74 50 6f 70 0a 2a 2a 20 6f 70 63 6f 64 textPop.** opcod
1d380 65 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 e. The context s
1d381 74 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 tores the last i
1d382 6e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 nsert row id, th
1d383 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 e last statement
1d384 20 63 68 61 6e 67 65 0a 2a 2a 20 63 6f 75 6e 74 change.** count
1d385 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e , and the curren
1d386 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e t statement chan
1d387 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 ge count..*/.cas
1d388 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 e OP_ContextPush
1d389 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 2d : {. int i = p-
1d38a 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 >contextStackTop
1d38b 2b 2b 3b 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 ++;. Context *p
1d38c 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 Context;.. asse
1d38d 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 2f 2a rt( i>=0 );. /*
1d38e 20 46 49 58 20 4d 45 3a 20 54 68 69 73 20 73 68 FIX ME: This sh
1d38f 6f 75 6c 64 20 62 65 20 61 6c 6c 6f 63 61 74 65 ould be allocate
1d390 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 d as part of the
1d391 20 76 64 62 65 20 61 74 20 63 6f 6d 70 69 6c 65 vdbe at compile
1d392 2d 74 69 6d 65 20 2a 2f 0a 20 20 69 66 28 20 69 -time */. if( i
1d393 3e 3d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 >=p->contextStac
1d394 6b 44 65 70 74 68 20 29 7b 0a 20 20 20 20 70 2d kDepth ){. p-
1d395 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 >contextStackDep
1d396 74 68 20 3d 20 69 2b 31 3b 0a 20 20 20 20 70 2d th = i+1;. p-
1d397 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 >contextStack =
1d398 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
1d399 4f 72 46 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f OrFree(db, p->co
1d39a 6e 74 65 78 74 53 74 61 63 6b 2c 0a 20 20 20 20 ntextStack,.
1d39b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d39c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d39d 20 20 20 20 20 20 73 69 7a 65 6f 66 28 43 6f 6e sizeof(Con
1d39e 74 65 78 74 29 2a 28 69 2b 31 29 29 3b 0a 20 20 text)*(i+1));.
1d39f 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 if( p->context
1d3a0 53 74 61 63 6b 3d 3d 30 20 29 20 67 6f 74 6f 20 Stack==0 ) goto
1d3a1 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 43 no_mem;. }. pC
1d3a2 6f 6e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e ontext = &p->con
1d3a3 74 65 78 74 53 74 61 63 6b 5b 69 5d 3b 0a 20 20 textStack[i];.
1d3a4 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f pContext->lastRo
1d3a5 77 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f wid = db->lastRo
1d3a6 77 69 64 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d wid;. pContext-
1d3a7 3e 6e 43 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 >nChange = p->nC
1d3a8 68 61 6e 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a hange;. break;.
1d3a9 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f }../* Opcode: Co
1d3aa 6e 74 65 78 74 50 6f 70 20 2a 20 2a 20 2a 20 0a ntextPop * * * .
1d3ab 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 **.** Restore th
1d3ac 65 20 56 64 62 65 20 63 6f 6e 74 65 78 74 20 74 e Vdbe context t
1d3ad 6f 20 74 68 65 20 73 74 61 74 65 20 69 74 20 77 o the state it w
1d3ae 61 73 20 69 6e 20 77 68 65 6e 20 63 6f 6e 74 65 as in when conte
1d3af 78 74 50 75 73 68 20 77 61 73 20 6c 61 73 74 0a xtPush was last.
1d3b0 2a 2a 20 65 78 65 63 75 74 65 64 2e 20 54 68 65 ** executed. The
1d3b1 20 63 6f 6e 74 65 78 74 20 73 74 6f 72 65 73 20 context stores
1d3b2 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 the last insert
1d3b3 72 6f 77 20 69 64 2c 20 74 68 65 20 6c 61 73 74 row id, the last
1d3b4 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 68 statement.** ch
1d3b5 61 6e 67 65 20 63 6f 75 6e 74 2c 20 61 6e 64 20 ange count, and
1d3b6 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 the current stat
1d3b7 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 ement change cou
1d3b8 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 nt..*/.case OP_C
1d3b9 6f 6e 74 65 78 74 50 6f 70 3a 20 7b 0a 20 20 43 ontextPop: {. C
1d3ba 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 ontext *pContext
1d3bb 20 3d 20 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 = &p->contextSt
1d3bc 61 63 6b 5b 2d 2d 70 2d 3e 63 6f 6e 74 65 78 74 ack[--p->context
1d3bd 53 74 61 63 6b 54 6f 70 5d 3b 0a 20 20 61 73 73 StackTop];. ass
1d3be 65 72 74 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 ert( p->contextS
1d3bf 74 61 63 6b 54 6f 70 3e 3d 30 20 29 3b 0a 20 20 tackTop>=0 );.
1d3c0 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 db->lastRowid =
1d3c1 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f pContext->lastRo
1d3c2 77 69 64 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 wid;. p->nChang
1d3c3 65 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 6e 43 e = pContext->nC
1d3c4 68 61 6e 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a hange;. break;.
1d3c5 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e }.#endif /* #ifn
1d3c6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1d3c7 54 52 49 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e TRIGGER */..#ifn
1d3c8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1d3c9 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a AUTOINCREMENT./*
1d3ca 20 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 Opcode: MemMax
1d3cb 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1d3cc 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 * Set the value
1d3cd 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 74 of register P1 t
1d3ce 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 o the maximum of
1d3cf 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 6c its current val
1d3d0 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 ue.** and the va
1d3d1 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1d3d2 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 P2..**.** This i
1d3d3 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 nstruction throw
1d3d4 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 s an error if th
1d3d5 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 e memory cell is
1d3d6 20 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a not initially.*
1d3d7 2a 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f * an integer..*/
1d3d8 0a 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a .case OP_MemMax:
1d3d9 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 { /* in1
1d3da 2c 20 69 6e 32 20 2a 2f 0a 20 20 73 71 6c 69 74 , in2 */. sqlit
1d3db 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1d3dc 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c ify(pIn1);. sql
1d3dd 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
1d3de 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 erify(pIn2);. i
1d3df 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e f( pIn1->u.i<pIn
1d3e0 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 70 49 6e 2->u.i){. pIn
1d3e1 31 2d 3e 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 1->u.i = pIn2->u
1d3e2 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b .i;. }. break;
1d3e3 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1d3e4 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
1d3e5 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 REMENT */../* Op
1d3e6 63 6f 64 65 3a 20 49 66 50 6f 73 20 50 31 20 50 code: IfPos P1 P
1d3e7 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 2 * * *.**.** If
1d3e8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 the value of re
1d3e9 67 69 73 74 65 72 20 50 31 20 69 73 20 31 20 6f gister P1 is 1 o
1d3ea 72 20 67 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 r greater, jump
1d3eb 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 to P2..**.** It
1d3ec 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 is illegal to us
1d3ed 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 e this instructi
1d3ee 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 on on a register
1d3ef 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f that does.** no
1d3f0 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 t contain an int
1d3f1 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 eger. An assert
1d3f2 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 ion fault will r
1d3f3 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 esult if you try
1d3f4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 ..*/.case OP_IfP
1d3f5 6f 73 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 os: { /*
1d3f6 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 jump, in1 */. a
1d3f7 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 ssert( pIn1->fla
1d3f8 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 gs&MEM_Int );.
1d3f9 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 if( pIn1->u.i>0
1d3fa 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1d3fb 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 ->p2 - 1;. }.
1d3fc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d3fd 6f 64 65 3a 20 49 66 4e 65 67 20 50 31 20 50 32 ode: IfNeg P1 P2
1d3fe 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 * * *.**.** If
1d3ff 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 the value of reg
1d400 69 73 74 65 72 20 50 31 20 69 73 20 6c 65 73 73 ister P1 is less
1d401 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 than zero, jump
1d402 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 to P2. .**.** I
1d403 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 t is illegal to
1d404 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 use this instruc
1d405 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 tion on a regist
1d406 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 er that does.**
1d407 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 not contain an i
1d408 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 nteger. An asse
1d409 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c rtion fault will
1d40a 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 result if you t
1d40b 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 ry..*/.case OP_I
1d40c 66 4e 65 67 3a 20 7b 20 20 20 20 20 20 20 20 2f fNeg: { /
1d40d 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 * jump, in1 */.
1d40e 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 assert( pIn1->f
1d40f 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a lags&MEM_Int );.
1d410 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c if( pIn1->u.i<
1d411 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 0 ){. pc = p
1d412 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a Op->p2 - 1;. }.
1d413 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d414 70 63 6f 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 pcode: IfZero P1
1d415 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1d416 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 If the value of
1d417 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 65 register P1 is e
1d418 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 xactly 0, jump t
1d419 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 o P2. .**.** It
1d41a 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 is illegal to us
1d41b 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 e this instructi
1d41c 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 on on a register
1d41d 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f that does.** no
1d41e 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 t contain an int
1d41f 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 eger. An assert
1d420 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 ion fault will r
1d421 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 esult if you try
1d422 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a ..*/.case OP_IfZ
1d423 65 72 6f 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a ero: { /*
1d424 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 jump, in1 */.
1d425 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c assert( pIn1->fl
1d426 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 ags&MEM_Int );.
1d427 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d if( pIn1->u.i==
1d428 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 0 ){. pc = p
1d429 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a Op->p2 - 1;. }.
1d42a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d42b 70 63 6f 64 65 3a 20 41 67 67 53 74 65 70 20 2a pcode: AggStep *
1d42c 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1d42d 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 ** Execute the s
1d42e 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 tep function for
1d42f 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e 20 20 an aggregate.
1d430 54 68 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 The.** function
1d431 68 61 73 20 50 35 20 61 72 67 75 6d 65 6e 74 73 has P5 arguments
1d432 2e 20 20 20 50 34 20 69 73 20 61 20 70 6f 69 6e . P4 is a poin
1d433 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 ter to the FuncD
1d434 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 ef.** structure
1d435 74 68 61 74 20 73 70 65 63 69 66 69 65 73 20 74 that specifies t
1d436 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 he function. Us
1d437 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 e register.** P3
1d438 20 61 73 20 74 68 65 20 61 63 63 75 6d 75 6c 61 as the accumula
1d439 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 tor..**.** The P
1d43a 35 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 5 arguments are
1d43b 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 taken from regis
1d43c 74 65 72 20 50 32 20 61 6e 64 20 69 74 73 0a 2a ter P2 and its.*
1d43d 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f * successors..*/
1d43e 0a 63 61 73 65 20 4f 50 5f 41 67 67 53 74 65 70 .case OP_AggStep
1d43f 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f : {. int n = pO
1d440 70 2d 3e 70 35 3b 0a 20 20 69 6e 74 20 69 3b 0a p->p5;. int i;.
1d441 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 2a 70 52 Mem *pMem, *pR
1d442 65 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f ec;. sqlite3_co
1d443 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c ntext ctx;. sql
1d444 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 ite3_value **apV
1d445 61 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e al;.. assert( n
1d446 3e 3d 30 20 29 3b 0a 20 20 70 52 65 63 20 3d 20 >=0 );. pRec =
1d447 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1d448 5d 3b 0a 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e ];. apVal = p->
1d449 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 apArg;. assert(
1d44a 20 61 70 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 apVal || n==0 )
1d44b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e ;. for(i=0; i<n
1d44c 3b 20 69 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b 0a ; i++, pRec++){.
1d44d 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 apVal[i] = p
1d44e 52 65 63 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 Rec;. storeTy
1d44f 70 65 49 6e 66 6f 28 70 52 65 63 2c 20 65 6e 63 peInfo(pRec, enc
1d450 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 63 74 oding);. }. ct
1d451 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 x.pFunc = pOp->p
1d452 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 4.pFunc;. asser
1d453 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 t( pOp->p3>0 &&
1d454 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1d455 20 29 3b 0a 20 20 63 74 78 2e 70 4d 65 6d 20 3d );. ctx.pMem =
1d456 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d pMem = &p->aMem
1d457 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 70 4d 65 [pOp->p3];. pMe
1d458 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63 74 78 2e 73 2e m->n++;. ctx.s.
1d459 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1d45a 3b 0a 20 20 63 74 78 2e 73 2e 7a 20 3d 20 30 3b ;. ctx.s.z = 0;
1d45b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 . ctx.s.zMalloc
1d45c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44 = 0;. ctx.s.xD
1d45d 65 6c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e el = 0;. ctx.s.
1d45e 64 62 20 3d 20 64 62 3b 0a 20 20 63 74 78 2e 69 db = db;. ctx.i
1d45f 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74 sError = 0;. ct
1d460 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 x.pColl = 0;. i
1d461 66 28 20 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c f( ctx.pFunc->fl
1d462 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e ags & SQLITE_FUN
1d463 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 C_NEEDCOLL ){.
1d464 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d assert( pOp>p-
1d465 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65 >aOp );. asse
1d466 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 rt( pOp[-1].p4ty
1d467 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 pe==P4_COLLSEQ )
1d468 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f ;. assert( pO
1d469 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 p[-1].opcode==OP
1d46a 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 _CollSeq );.
1d46b 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b ctx.pColl = pOp[
1d46c 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 -1].p4.pColl;.
1d46d 7d 0a 20 20 28 63 74 78 2e 70 46 75 6e 63 2d 3e }. (ctx.pFunc->
1d46e 78 53 74 65 70 29 28 26 63 74 78 2c 20 6e 2c 20 xStep)(&ctx, n,
1d46f 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 63 74 apVal);. if( ct
1d470 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 x.isError ){.
1d471 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1d472 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1d473 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 b, "%s", sqlite3
1d474 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 63 74 78 _value_text(&ctx
1d475 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 .s));. rc = c
1d476 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a tx.isError;. }.
1d477 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d478 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 3b Release(&ctx.s);
1d479 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d47a 4f 70 63 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c Opcode: AggFinal
1d47b 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a P1 P2 * P4 *.**
1d47c 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 .** Execute the
1d47d 66 69 6e 61 6c 69 7a 65 72 20 66 75 6e 63 74 69 finalizer functi
1d47e 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 on for an aggreg
1d47f 61 74 65 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74 ate. P1 is.** t
1d480 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 he memory locati
1d481 6f 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 61 on that is the a
1d482 63 63 75 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74 ccumulator for t
1d483 68 65 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a he aggregate..**
1d484 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 .** P2 is the nu
1d485 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
1d486 73 20 74 68 61 74 20 74 68 65 20 73 74 65 70 20 s that the step
1d487 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 function takes a
1d488 6e 64 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f nd.** P4 is a po
1d489 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e inter to the Fun
1d48a 63 44 65 66 20 66 6f 72 20 74 68 69 73 20 66 75 cDef for this fu
1d48b 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a nction. The P2.
1d48c 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e ** argument is n
1d48d 6f 74 20 75 73 65 64 20 62 79 20 74 68 69 73 20 ot used by this
1d48e 6f 70 63 6f 64 65 2e 20 20 49 74 20 69 73 20 6f opcode. It is o
1d48f 6e 6c 79 20 74 68 65 72 65 20 74 6f 20 64 69 73 nly there to dis
1d490 61 6d 62 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e ambiguate.** fun
1d491 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 ctions that can
1d492 74 61 6b 65 20 76 61 72 79 69 6e 67 20 6e 75 6d take varying num
1d493 62 65 72 73 20 6f 66 20 61 72 67 75 6d 65 6e 74 bers of argument
1d494 73 2e 20 20 54 68 65 0a 2a 2a 20 50 34 20 61 72 s. The.** P4 ar
1d495 67 75 6d 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e gument is only n
1d496 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64 65 eeded for the de
1d497 67 65 6e 65 72 61 74 65 20 63 61 73 65 20 77 68 generate case wh
1d498 65 72 65 0a 2a 2a 20 74 68 65 20 73 74 65 70 20 ere.** the step
1d499 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 function was not
1d49a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c previously call
1d49b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 ed..*/.case OP_A
1d49c 67 67 46 69 6e 61 6c 3a 20 7b 0a 20 20 4d 65 6d ggFinal: {. Mem
1d49d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 *pMem;. assert
1d49e 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 ( pOp->p1>0 && p
1d49f 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1d4a0 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e );. pMem = &p->
1d4a1 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1d4a2 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
1d4a3 66 6c 61 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 flags & ~(MEM_Nu
1d4a4 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 ll|MEM_Agg))==0
1d4a5 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1d4a6 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 3VdbeMemFinalize
1d4a7 28 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 (pMem, pOp->p4.p
1d4a8 46 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 3d Func);. if( rc=
1d4a9 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b =SQLITE_ERROR ){
1d4aa 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1d4ab 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1d4ac 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
1d4ad 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1d4ae 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 pMem));. }. sq
1d4af 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
1d4b0 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e ncoding(pMem, en
1d4b1 63 6f 64 69 6e 67 29 3b 0a 20 20 55 50 44 41 54 coding);. UPDAT
1d4b2 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1d4b3 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Mem);. if( sqli
1d4b4 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1d4b5 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f (pMem) ){. go
1d4b6 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1d4b7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 break;.}...#if
1d4b8 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1d4b9 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 26 26 _OMIT_VACUUM) &&
1d4ba 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1d4bb 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a _OMIT_ATTACH)./*
1d4bc 20 4f 70 63 6f 64 65 3a 20 56 61 63 75 75 6d 20 Opcode: Vacuum
1d4bd 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 * * * * *.**.**
1d4be 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74 69 72 Vacuum the entir
1d4bf 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 e database. Thi
1d4c0 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 63 61 s opcode will ca
1d4c1 75 73 65 20 6f 74 68 65 72 20 76 69 72 74 75 61 use other virtua
1d4c2 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f l.** machines to
1d4c3 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64 20 be created and
1d4c4 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74 run. It may not
1d4c5 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
1d4c6 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e within.** a tran
1d4c7 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 saction..*/.case
1d4c8 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20 OP_Vacuum: {.
1d4c9 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1d4ca 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 yOff(db) ) goto
1d4cb 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1d4cc 75 73 65 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c use; . rc = sql
1d4cd 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 26 70 ite3RunVacuum(&p
1d4ce 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a ->zErrMsg, db);.
1d4cf 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1d4d0 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f etyOn(db) ) goto
1d4d1 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1d4d2 73 75 73 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d suse;. break;.}
1d4d3 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 .#endif..#if !de
1d4d4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1d4d5 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a T_AUTOVACUUM)./*
1d4d6 20 4f 70 63 6f 64 65 3a 20 49 6e 63 72 56 61 63 Opcode: IncrVac
1d4d7 75 75 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a uum P1 P2 * * *.
1d4d8 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 **.** Perform a
1d4d9 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 74 single step of t
1d4da 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 he incremental v
1d4db 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72 65 20 acuum procedure
1d4dc 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64 61 74 on.** the P1 dat
1d4dd 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 76 61 abase. If the va
1d4de 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73 68 65 cuum has finishe
1d4df 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 d, jump to instr
1d4e0 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74 uction.** P2. Ot
1d4e1 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 herwise, fall th
1d4e2 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1d4e3 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
1d4e4 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72 56 61 /.case OP_IncrVa
1d4e5 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f cuum: { /
1d4e6 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 42 74 72 65 * jump */. Btre
1d4e7 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 e *pBt;.. asser
1d4e8 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1d4e9 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 pOp->p1<db->nDb
1d4ea 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
1d4eb 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1d4ec 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 <<pOp->p1))!=0 )
1d4ed 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 ;. pBt = db->aD
1d4ee 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a b[pOp->p1].pBt;.
1d4ef 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1d4f0 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 70 42 reeIncrVacuum(pB
1d4f1 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
1d4f2 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
1d4f3 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1d4f4 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 1;. rc = SQLI
1d4f5 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 TE_OK;. }. bre
1d4f6 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a ak;.}.#endif../*
1d4f7 20 4f 70 63 6f 64 65 3a 20 45 78 70 69 72 65 20 Opcode: Expire
1d4f8 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1d4f9 20 43 61 75 73 65 20 70 72 65 63 6f 6d 70 69 6c Cause precompil
1d4fa 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f ed statements to
1d4fb 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e become expired.
1d4fc 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74 An expired stat
1d4fd 65 6d 65 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 ement.** fails w
1d4fe 69 74 68 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 ith an error cod
1d4ff 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 43 48 45 e of SQLITE_SCHE
1d500 4d 41 20 69 66 20 69 74 20 69 73 20 65 76 65 72 MA if it is ever
1d501 20 65 78 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 executed .** (v
1d502 69 61 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 ia sqlite3_step(
1d503 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 ))..** .** If P1
1d504 20 69 73 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 is 0, then all
1d505 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 SQL statements b
1d506 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 49 ecome expired. I
1d507 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f f P1 is non-zero
1d508 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 ,.** then only t
1d509 68 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 he currently exe
1d50a 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 cuting statement
1d50b 20 69 73 20 61 66 66 65 63 74 65 64 2e 20 0a 2a is affected. .*
1d50c 2f 0a 63 61 73 65 20 4f 50 5f 45 78 70 69 72 65 /.case OP_Expire
1d50d 3a 20 7b 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e : {. if( !pOp->
1d50e 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 p1 ){. sqlite
1d50f 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 3ExpirePreparedS
1d510 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 tatements(db);.
1d511 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 }else{. p->e
1d512 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a xpired = 1;. }.
1d513 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e break;.}..#ifn
1d514 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1d515 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 SHARED_CACHE./*
1d516 4f 70 63 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 Opcode: TableLoc
1d517 6b 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a k P1 P2 P3 P4 *.
1d518 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c **.** Obtain a l
1d519 6f 63 6b 20 6f 6e 20 61 20 70 61 72 74 69 63 75 ock on a particu
1d51a 6c 61 72 20 74 61 62 6c 65 2e 20 54 68 69 73 20 lar table. This
1d51b 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f instruction is o
1d51c 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a nly used when.**
1d51d 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 the shared-cach
1d51e 65 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 e feature is ena
1d51f 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 bled. .**.** If
1d520 50 31 20 69 73 20 20 74 68 65 20 69 6e 64 65 78 P1 is the index
1d521 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1d522 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b in sqlite3.aDb[
1d523 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 ] of the databas
1d524 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 e.** on which th
1d525 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72 e lock is acquir
1d526 65 64 2e 20 20 41 20 72 65 61 64 6c 6f 63 6b 20 ed. A readlock
1d527 69 73 20 6f 62 74 61 69 6e 65 64 20 69 66 20 50 is obtained if P
1d528 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69 3==0 or.** a wri
1d529 74 65 20 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31 te lock if P3==1
1d52a 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61 ..**.** P2 conta
1d52b 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 ins the root-pag
1d52c 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
1d52d 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 o lock..**.** P4
1d52e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e contains a poin
1d52f 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 ter to the name
1d530 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 of the table bei
1d531 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20 ng locked. This
1d532 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20 is only.** used
1d533 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 to generate an e
1d534 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20 rror message if
1d535 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 the lock cannot
1d536 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a be obtained..*/.
1d537 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 case OP_TableLoc
1d538 6b 3a 20 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20 k: {. int p1 =
1d539 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 75 38 20 69 pOp->p1; . u8 i
1d53a 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 sWriteLock = (u8
1d53b 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 )pOp->p3;. asse
1d53c 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c rt( p1>=0 && p1<
1d53d 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
1d53e 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1d53f 73 6b 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 sk & (1<<p1))!=0
1d540 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 );. assert( is
1d541 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 WriteLock==0 ||
1d542 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 isWriteLock==1 )
1d543 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1d544 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 BtreeLockTable(d
1d545 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 b->aDb[p1].pBt,
1d546 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 pOp->p2, isWrite
1d547 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20 28 72 63 Lock);. if( (rc
1d548 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c &0xFF)==SQLITE_L
1d549 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 63 6f 6e OCKED ){. con
1d54a 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 st char *z = pOp
1d54b 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 73 71 6c 69 ->p4.z;. sqli
1d54c 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1d54d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 >zErrMsg, db, "d
1d54e 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 atabase table is
1d54f 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 locked: %s", z)
1d550 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d551 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1d552 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
1d553 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 CHE */..#ifndef
1d554 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1d555 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f UALTABLE./* Opco
1d556 64 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a de: VBegin * * *
1d557 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d P4 *.**.** P4 m
1d558 61 79 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 ay be a pointer
1d559 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 to an sqlite3_vt
1d55a 61 62 20 73 74 72 75 63 74 75 72 65 2e 20 49 66 ab structure. If
1d55b 20 73 6f 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a so, call the .*
1d55c 2a 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 * xBegin method
1d55d 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a for that table..
1d55e 2a 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 **.** Also, whet
1d55f 68 65 72 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 her or not P4 is
1d560 20 73 65 74 2c 20 63 68 65 63 6b 20 74 68 61 74 set, check that
1d561 20 74 68 69 73 20 69 73 20 6e 6f 74 20 62 65 69 this is not bei
1d562 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a ng called from.*
1d563 2a 20 77 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 * within a callb
1d564 61 63 6b 20 74 6f 20 61 20 76 69 72 74 75 61 6c ack to a virtual
1d565 20 74 61 62 6c 65 20 78 53 79 6e 63 28 29 20 6d table xSync() m
1d566 65 74 68 6f 64 2e 20 49 66 20 69 74 20 69 73 2c ethod. If it is,
1d567 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f the error.** co
1d568 64 65 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 de will be set t
1d569 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e o SQLITE_LOCKED.
1d56a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 .*/.case OP_VBeg
1d56b 69 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f in: {. sqlite3_
1d56c 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f vtab *pVtab = pO
1d56d 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 72 p->p4.pVtab;. r
1d56e 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 42 c = sqlite3VtabB
1d56f 65 67 69 6e 28 64 62 2c 20 70 56 74 61 62 29 3b egin(db, pVtab);
1d570 0a 20 20 69 66 28 20 70 56 74 61 62 20 29 7b 0a . if( pVtab ){.
1d571 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1d572 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1d573 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 );. p->zErrMs
1d574 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d g = pVtab->zErrM
1d575 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a sg;. pVtab->z
1d576 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a ErrMsg = 0;. }.
1d577 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1d578 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1d579 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1d57a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1d57b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1d57c 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 LE./* Opcode: VC
1d57d 72 65 61 74 65 20 50 31 20 2a 20 2a 20 50 34 20 reate P1 * * P4
1d57e 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 *.**.** P4 is th
1d57f 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74 e name of a virt
1d580 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74 ual table in dat
1d581 61 62 61 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 abase P1. Call t
1d582 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f he xCreate metho
1d583 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20 74 61 d.** for that ta
1d584 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ble..*/.case OP_
1d585 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 VCreate: {. rc
1d586 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c = sqlite3VtabCal
1d587 6c 43 72 65 61 74 65 28 64 62 2c 20 70 4f 70 2d lCreate(db, pOp-
1d588 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 >p1, pOp->p4.z,
1d589 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 &p->zErrMsg);.
1d58a 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1d58b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1d58c 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1d58d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1d58e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1d58f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 ./* Opcode: VDes
1d590 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20 2a troy P1 * * P4 *
1d591 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 .**.** P4 is the
1d592 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 name of a virtu
1d593 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 al table in data
1d594 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 base P1. Call t
1d595 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 he xDestroy meth
1d596 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 od.** of that ta
1d597 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ble..*/.case OP_
1d598 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d VDestroy: {. p-
1d599 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 >inVtabMethod =
1d59a 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 2;. rc = sqlite
1d59b 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 3VtabCallDestroy
1d59c 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f (db, pOp->p1, pO
1d59d 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 p->p4.z);. p->i
1d59e 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b nVtabMethod = 0;
1d59f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1d5a0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1d5a1 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1d5a2 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1d5a3 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1d5a4 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1d5a5 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a Open P1 * * P4 *
1d5a6 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 .**.** P4 is a p
1d5a7 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 ointer to a virt
1d5a8 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 ual table object
1d5a9 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 , an sqlite3_vta
1d5aa 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 b structure..**
1d5ab 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 6e P1 is a cursor n
1d5ac 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70 63 umber. This opc
1d5ad 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72 73 ode opens a curs
1d5ae 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 or to the virtua
1d5af 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 l.** table and s
1d5b0 74 6f 72 65 73 20 74 68 61 74 20 63 75 72 73 6f tores that curso
1d5b1 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 r in P1..*/.case
1d5b2 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a 20 20 56 OP_VOpen: {. V
1d5b3 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 dbeCursor *pCur
1d5b4 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 = 0;. sqlite3_v
1d5b5 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 tab_cursor *pVta
1d5b6 62 43 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 bCursor = 0;..
1d5b7 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1d5b8 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 tab = pOp->p4.pV
1d5b9 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d tab;. sqlite3_m
1d5ba 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d odule *pModule =
1d5bb 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 (sqlite3_module
1d5bc 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c *)pVtab->pModul
1d5bd 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 56 74 e;.. assert(pVt
1d5be 61 62 20 26 26 20 70 4d 6f 64 75 6c 65 29 3b 0a ab && pModule);.
1d5bf 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1d5c0 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 etyOff(db) ) got
1d5c1 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1d5c2 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70 4d isuse;. rc = pM
1d5c3 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 56 74 odule->xOpen(pVt
1d5c4 61 62 2c 20 26 70 56 74 61 62 43 75 72 73 6f 72 ab, &pVtabCursor
1d5c5 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1d5c6 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1d5c7 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 g);. p->zErrMsg
1d5c8 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 = pVtab->zErrMs
1d5c9 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 g;. pVtab->zErr
1d5ca 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 Msg = 0;. if( s
1d5cb 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1d5cc 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1d5cd 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1d5ce 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
1d5cf 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 rc ){. /* Ini
1d5d0 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33 5f tialize sqlite3_
1d5d1 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 vtab_cursor base
1d5d2 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 70 56 class */. pV
1d5d3 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1d5d4 20 3d 20 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f = pVtab;.. /
1d5d5 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 * Initialise vdb
1d5d6 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 e cursor object
1d5d7 2a 2f 0a 20 20 20 20 70 43 75 72 20 3d 20 61 6c */. pCur = al
1d5d8 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 locateCursor(p,
1d5d9 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20 pOp->p1, 0, -1,
1d5da 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 0);. if( pCur
1d5db 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e ){. pCur->
1d5dc 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 56 pVtabCursor = pV
1d5dd 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 tabCursor;.
1d5de 20 70 43 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d pCur->pModule =
1d5df 20 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 pVtabCursor->pV
1d5e0 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 tab->pModule;.
1d5e1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 }else{. d
1d5e2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1d5e3 3d 20 31 3b 0a 20 20 20 20 20 20 70 4d 6f 64 75 = 1;. pModu
1d5e4 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 le->xClose(pVtab
1d5e5 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 Cursor);. }.
1d5e6 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1d5e7 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1d5e8 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1d5e9 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1d5ea 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1d5eb 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1d5ec 20 56 46 69 6c 74 65 72 20 50 31 20 50 32 20 50 VFilter P1 P2 P
1d5ed 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 3 P4 *.**.** P1
1d5ee 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e is a cursor open
1d5ef 65 64 20 75 73 69 6e 67 20 56 4f 70 65 6e 2e 20 ed using VOpen.
1d5f0 20 50 32 20 69 73 20 61 6e 20 61 64 64 72 65 73 P2 is an addres
1d5f1 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 0a s to jump to if.
1d5f2 2a 2a 20 74 68 65 20 66 69 6c 74 65 72 65 64 20 ** the filtered
1d5f3 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d result set is em
1d5f4 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 pty..**.** P4 is
1d5f5 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 either NULL or
1d5f6 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 77 61 a string that wa
1d5f7 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 s generated by t
1d5f8 68 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a he xBestIndex.**
1d5f9 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d method of the m
1d5fa 6f 64 75 6c 65 2e 20 20 54 68 65 20 69 6e 74 65 odule. The inte
1d5fb 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 rpretation of th
1d5fc 65 20 50 34 20 73 74 72 69 6e 67 20 69 73 20 6c e P4 string is l
1d5fd 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6d 6f eft.** to the mo
1d5fe 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 dule implementat
1d5ff 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion..**.** This
1d600 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 opcode invokes t
1d601 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f he xFilter metho
1d602 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c d on the virtual
1d603 20 74 61 62 6c 65 20 73 70 65 63 69 66 69 65 64 table specified
1d604 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54 68 65 20 .** by P1. The
1d605 69 6e 74 65 67 65 72 20 71 75 65 72 79 20 70 6c integer query pl
1d606 61 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 an parameter to
1d607 78 46 69 6c 74 65 72 20 69 73 20 73 74 6f 72 65 xFilter is store
1d608 64 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a d in register.**
1d609 20 50 33 2e 20 52 65 67 69 73 74 65 72 20 50 33 P3. Register P3
1d60a 2b 31 20 73 74 6f 72 65 73 20 74 68 65 20 61 72 +1 stores the ar
1d60b 67 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 gc parameter to
1d60c 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 be passed to the
1d60d 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d 65 74 68 .** xFilter meth
1d60e 6f 64 2e 20 52 65 67 69 73 74 65 72 73 20 50 33 od. Registers P3
1d60f 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 20 61 72 +2..P3+1+argc ar
1d610 65 20 74 68 65 20 61 72 67 63 0a 2a 2a 20 61 64 e the argc.** ad
1d611 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74 ditional paramet
1d612 65 72 73 20 77 68 69 63 68 20 61 72 65 20 70 61 ers which are pa
1d613 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 69 6c 74 ssed to.** xFilt
1d614 65 72 20 61 73 20 61 72 67 76 2e 20 52 65 67 69 er as argv. Regi
1d615 73 74 65 72 20 50 33 2b 32 20 62 65 63 6f 6d 65 ster P3+2 become
1d616 73 20 61 72 67 76 5b 30 5d 20 77 68 65 6e 20 70 s argv[0] when p
1d617 61 73 73 65 64 20 74 6f 20 78 46 69 6c 74 65 72 assed to xFilter
1d618 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 69 ..**.** A jump i
1d619 73 20 6d 61 64 65 20 74 6f 20 50 32 20 69 66 20 s made to P2 if
1d61a 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 61 the result set a
1d61b 66 74 65 72 20 66 69 6c 74 65 72 69 6e 67 20 77 fter filtering w
1d61c 6f 75 6c 64 20 62 65 20 65 6d 70 74 79 2e 0a 2a ould be empty..*
1d61d 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69 6c 74 65 /.case OP_VFilte
1d61e 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a r: { /* jump *
1d61f 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 /. int nArg;.
1d620 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 63 6f int iQuery;. co
1d621 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
1d622 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d le *pModule;. M
1d623 65 6d 20 2a 70 51 75 65 72 79 20 3d 20 26 70 2d em *pQuery = &p-
1d624 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1d625 20 20 4d 65 6d 20 2a 70 41 72 67 63 20 3d 20 26 Mem *pArgc = &
1d626 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 73 71 6c pQuery[1];. sql
1d627 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
1d628 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 *pVtabCursor;.
1d629 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1d62a 56 74 61 62 3b 0a 0a 20 20 56 64 62 65 43 75 72 Vtab;.. VdbeCur
1d62b 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 sor *pCur = p->a
1d62c 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a pCsr[pOp->p1];..
1d62d 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1d62e 28 70 4f 70 2d 3e 70 33 2c 20 70 51 75 65 72 79 (pOp->p3, pQuery
1d62f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
1d630 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 r->pVtabCursor )
1d631 3b 0a 20 20 70 56 74 61 62 43 75 72 73 6f 72 20 ;. pVtabCursor
1d632 3d 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 = pCur->pVtabCur
1d633 73 6f 72 3b 0a 20 20 70 56 74 61 62 20 3d 20 70 sor;. pVtab = p
1d634 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 VtabCursor->pVta
1d635 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 b;. pModule = p
1d636 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a Vtab->pModule;..
1d637 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20 69 6e /* Grab the in
1d638 64 65 78 20 6e 75 6d 62 65 72 20 61 6e 64 20 61 dex number and a
1d639 72 67 63 20 70 61 72 61 6d 65 74 65 72 73 20 2a rgc parameters *
1d63a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 51 75 /. assert( (pQu
1d63b 65 72 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 ery->flags&MEM_I
1d63c 6e 74 29 21 3d 30 20 26 26 20 70 41 72 67 63 2d nt)!=0 && pArgc-
1d63d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 >flags==MEM_Int
1d63e 29 3b 0a 20 20 6e 41 72 67 20 3d 20 28 69 6e 74 );. nArg = (int
1d63f 29 70 41 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 69 )pArgc->u.i;. i
1d640 51 75 65 72 79 20 3d 20 28 69 6e 74 29 70 51 75 Query = (int)pQu
1d641 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 ery->u.i;.. /*
1d642 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 Invoke the xFilt
1d643 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b er method */. {
1d644 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20 30 . int res = 0
1d645 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 ;. int i;.
1d646 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 Mem **apArg = p
1d647 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 ->apArg;. for
1d648 28 69 20 3d 20 30 3b 20 69 3c 6e 41 72 67 3b 20 (i = 0; i<nArg;
1d649 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 70 41 72 i++){. apAr
1d64a 67 5b 69 5d 20 3d 20 26 70 41 72 67 63 5b 69 2b g[i] = &pArgc[i+
1d64b 31 5d 3b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 1];. storeT
1d64c 79 70 65 49 6e 66 6f 28 61 70 41 72 67 5b 69 5d ypeInfo(apArg[i]
1d64d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 , 0);. }..
1d64e 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1d64f 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1d650 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1d651 73 75 73 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 suse;. sqlite
1d652 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3VtabLock(pVtab)
1d653 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d ;. p->inVtabM
1d654 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 72 ethod = 1;. r
1d655 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 c = pModule->xFi
1d656 6c 74 65 72 28 70 56 74 61 62 43 75 72 73 6f 72 lter(pVtabCursor
1d657 2c 20 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70 , iQuery, pOp->p
1d658 34 2e 7a 2c 20 6e 41 72 67 2c 20 61 70 41 72 67 4.z, nArg, apArg
1d659 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 );. p->inVtab
1d65a 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 Method = 0;.
1d65b 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1d65c 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1d65d 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1d65e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a pVtab->zErrMsg;.
1d65f 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d pVtab->zErrM
1d660 73 67 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 sg = 0;. sqli
1d661 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 te3VtabUnlock(db
1d662 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 , pVtab);. if
1d663 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1d664 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 70 ){. res = p
1d665 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70 56 74 Module->xEof(pVt
1d666 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d abCursor);. }
1d667 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1d668 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1d669 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1d66a 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20 69 66 _misuse;.. if
1d66b 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 ( res ){. p
1d66c 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1d66d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 . }. }. pCu
1d66e 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a r->nullRow = 0;.
1d66f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1d670 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1d671 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1d672 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1d673 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1d674 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1d675 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 Column P1 P2 P3
1d676 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 * *.**.** Store
1d677 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1d678 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 P2-th column of
1d679 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74 .** the row of t
1d67a 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
1d67b 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31 that the .** P1
1d67c 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 cursor is point
1d67d 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69 ing to into regi
1d67e 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 ster P3..*/.case
1d67f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 OP_VColumn: {.
1d680 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1d681 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 Vtab;. const sq
1d682 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1d683 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44 odule;. Mem *pD
1d684 65 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 est;. sqlite3_c
1d685 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b ontext sContext;
1d686 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a .. VdbeCursor *
1d687 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b pCur = p->apCsr[
1d688 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1d689 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 rt( pCur->pVtabC
1d68a 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 ursor );. asser
1d68b 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 t( pOp->p3>0 &&
1d68c 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1d68d 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20 26 70 );. pDest = &p
1d68e 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1d68f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c . if( pCur->nul
1d690 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 lRow ){. sqli
1d691 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1d692 6c 28 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72 l(pDest);. br
1d693 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 56 74 61 62 eak;. }. pVtab
1d694 20 3d 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 = pCur->pVtabCu
1d695 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 70 rsor->pVtab;. p
1d696 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e Module = pVtab->
1d697 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 pModule;. asser
1d698 74 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c t( pModule->xCol
1d699 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 umn );. memset(
1d69a 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 &sContext, 0, si
1d69b 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74 29 29 3b zeof(sContext));
1d69c 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 .. /* The outpu
1d69d 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 t cell may alrea
1d69e 64 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72 dy have a buffer
1d69f 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 allocated. Move
1d6a0 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e . ** the curren
1d6a1 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 73 43 t contents to sC
1d6a2 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e 20 63 ontext.s so in c
1d6a3 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75 6e ase the user-fun
1d6a4 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 63 61 6e 20 ction . ** can
1d6a5 75 73 65 20 74 68 65 20 61 6c 72 65 61 64 79 20 use the already
1d6a6 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
1d6a7 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f instead of allo
1d6a8 63 61 74 69 6e 67 20 61 20 0a 20 20 2a 2a 20 6e cating a . ** n
1d6a9 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73 ew one.. */. s
1d6aa 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1d6ab 65 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 70 e(&sContext.s, p
1d6ac 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 Dest);. MemSetT
1d6ad 79 70 65 46 6c 61 67 28 26 73 43 6f 6e 74 65 78 ypeFlag(&sContex
1d6ae 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a t.s, MEM_Null);.
1d6af 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1d6b0 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1d6b1 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1d6b2 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70 misuse;. rc = p
1d6b3 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 28 Module->xColumn(
1d6b4 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1d6b5 72 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 4f r, &sContext, pO
1d6b6 70 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74 65 p->p2);. sqlite
1d6b7 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1d6b8 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1d6b9 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a rrMsg = pVtab->z
1d6ba 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d ErrMsg;. pVtab-
1d6bb 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 >zErrMsg = 0;..
1d6bc 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 /* Copy the res
1d6bd 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ult of the funct
1d6be 69 6f 6e 20 74 6f 20 74 68 65 20 50 33 20 72 65 ion to the P3 re
1d6bf 67 69 73 74 65 72 2e 20 57 65 0a 20 20 2a 2a 20 gister. We. **
1d6c0 64 6f 20 74 68 69 73 20 72 65 67 61 72 64 6c 65 do this regardle
1d6c1 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 ss of whether or
1d6c2 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 not an error oc
1d6c3 63 75 72 72 65 64 20 74 6f 20 65 6e 73 75 72 65 curred to ensure
1d6c4 20 61 6e 79 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 any. ** dynami
1d6c5 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 c allocation in
1d6c6 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 20 4d 65 sContext.s (a Me
1d6c7 6d 20 73 74 72 75 63 74 29 20 69 73 20 20 72 65 m struct) is re
1d6c8 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 leased.. */. s
1d6c9 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1d6ca 45 6e 63 6f 64 69 6e 67 28 26 73 43 6f 6e 74 65 Encoding(&sConte
1d6cb 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b xt.s, encoding);
1d6cc 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1d6cd 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44 65 73 74 E(pOp->p3, pDest
1d6ce 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1d6cf 4d 65 6d 4d 6f 76 65 28 70 44 65 73 74 2c 20 26 MemMove(pDest, &
1d6d0 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20 20 55 sContext.s);. U
1d6d1 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1d6d2 5a 45 28 70 44 65 73 74 29 3b 0a 0a 20 20 69 66 ZE(pDest);.. if
1d6d3 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1d6d4 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 n(db) ){. got
1d6d5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1d6d6 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 isuse;. }. if(
1d6d7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 sqlite3VdbeMemT
1d6d8 6f 6f 42 69 67 28 70 44 65 73 74 29 20 29 7b 0a ooBig(pDest) ){.
1d6d9 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1d6da 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d6db 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1d6dc 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1d6dd 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 BLE */..#ifndef
1d6de 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1d6df 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f UALTABLE./* Opco
1d6e0 64 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32 20 de: VNext P1 P2
1d6e1 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 * * *.**.** Adva
1d6e2 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c nce virtual tabl
1d6e3 65 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78 74 e P1 to the next
1d6e4 20 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73 75 row in its resu
1d6e5 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a 75 lt set and.** ju
1d6e6 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f mp to instructio
1d6e7 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 68 n P2. Or, if th
1d6e8 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
1d6e9 68 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 74 has reached.** t
1d6ea 68 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72 65 he end of its re
1d6eb 73 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 66 sult set, then f
1d6ec 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 all through to t
1d6ed 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
1d6ee 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ion..*/.case OP_
1d6ef 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a 75 VNext: { /* ju
1d6f0 6d 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f mp */. sqlite3_
1d6f1 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 vtab *pVtab;. c
1d6f2 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
1d6f3 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 ule *pModule;.
1d6f4 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 0a 20 20 int res = 0;..
1d6f5 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 VdbeCursor *pCur
1d6f6 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1d6f7 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1d6f8 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1d6f9 72 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d r );. if( pCur-
1d6fa 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 >nullRow ){.
1d6fb 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 56 74 break;. }. pVt
1d6fc 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74 61 62 ab = pCur->pVtab
1d6fd 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 Cursor->pVtab;.
1d6fe 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 pModule = pVtab
1d6ff 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 ->pModule;. ass
1d700 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e ert( pModule->xN
1d701 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 ext );.. /* Inv
1d702 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 oke the xNext()
1d703 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f method of the mo
1d704 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e dule. There is n
1d705 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 o way for the.
1d706 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d ** underlying im
1d707 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 plementation to
1d708 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1d709 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75 if one occurs du
1d70a 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28 ring. ** xNext(
1d70b 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61 ). Instead, if a
1d70c 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
1d70d 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 true is returned
1d70e 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 (indicating tha
1d70f 74 20 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20 t . ** data is
1d710 61 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 available) and t
1d711 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 he error code re
1d712 74 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c turned when xCol
1d713 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 umn or. ** some
1d714 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73 other method is
1d715 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e next invoked on
1d716 20 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61 the save virtua
1d717 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a l table cursor..
1d718 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
1d719 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1d71a 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1d71b 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 73 71 _to_misuse;. sq
1d71c 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 lite3VtabLock(pV
1d71d 74 61 62 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 tab);. p->inVta
1d71e 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 72 bMethod = 1;. r
1d71f 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 c = pModule->xNe
1d720 78 74 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 xt(pCur->pVtabCu
1d721 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 rsor);. p->inVt
1d722 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1d723 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1d724 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1d725 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 p->zErrMsg = pV
1d726 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 tab->zErrMsg;.
1d727 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1d728 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 0;. sqlite3Vta
1d729 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 bUnlock(db, pVta
1d72a 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 b);. if( rc==SQ
1d72b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
1d72c 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 45 es = pModule->xE
1d72d 6f 66 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 of(pCur->pVtabCu
1d72e 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 rsor);. }. if(
1d72f 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1d730 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1d731 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1d732 0a 0a 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a .. if( !res ){.
1d733 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
1d734 69 73 20 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f is data, jump to
1d735 20 50 32 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 P2 */. pc =
1d736 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1d737 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1d738 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1d739 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1d73a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1d73b 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1d73c 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1d73d 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 Rename P1 * * P4
1d73e 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 *.**.** P4 is a
1d73f 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 pointer to a vi
1d740 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 rtual table obje
1d741 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 ct, an sqlite3_v
1d742 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a tab structure..*
1d743 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e * This opcode in
1d744 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 vokes the corres
1d745 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 ponding xRename
1d746 6d 65 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 method. The valu
1d747 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 e.** in register
1d748 20 50 31 20 69 73 20 70 61 73 73 65 64 20 61 73 P1 is passed as
1d749 20 74 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d the zName argum
1d74a 65 6e 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61 ent to the xRena
1d74b 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 me method..*/.ca
1d74c 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b se OP_VRename: {
1d74d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 . sqlite3_vtab
1d74e 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 *pVtab = pOp->p4
1d74f 2e 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a 70 .pVtab;. Mem *p
1d750 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b Name = &p->aMem[
1d751 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1d752 72 74 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 rt( pVtab->pModu
1d753 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20 le->xRename );.
1d754 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1d755 70 4f 70 2d 3e 70 31 2c 20 70 4e 61 6d 65 29 3b pOp->p1, pName);
1d756 0a 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 4e .. Stringify(pN
1d757 61 6d 65 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a ame, encoding);.
1d758 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1d759 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1d75a 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1d75b 6d 69 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65 misuse;. sqlite
1d75c 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3VtabLock(pVtab)
1d75d 3b 0a 20 20 72 63 20 3d 20 70 56 74 61 62 2d 3e ;. rc = pVtab->
1d75e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 pModule->xRename
1d75f 28 70 56 74 61 62 2c 20 70 4e 61 6d 65 2d 3e 7a (pVtab, pName->z
1d760 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1d761 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1d762 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 g);. p->zErrMsg
1d763 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 = pVtab->zErrMs
1d764 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 g;. pVtab->zErr
1d765 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 Msg = 0;. sqlit
1d766 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c e3VtabUnlock(db,
1d767 20 70 56 74 61 62 29 3b 0a 20 20 69 66 28 20 73 pVtab);. if( s
1d768 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1d769 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1d76a 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a due_to_misuse;..
1d76b 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1d76c 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
1d76d 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1d76e 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1d76f 55 70 64 61 74 65 20 50 31 20 50 32 20 50 33 20 Update P1 P2 P3
1d770 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 P4 *.**.** P4 is
1d771 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1d772 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 virtual table ob
1d773 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 ject, an sqlite3
1d774 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e _vtab structure.
1d775 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1d776 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 invokes the corr
1d777 65 73 70 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 esponding xUpdat
1d778 65 20 6d 65 74 68 6f 64 2e 20 50 32 20 76 61 6c e method. P2 val
1d779 75 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 ues.** are conti
1d77a 67 75 6f 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c guous memory cel
1d77b 6c 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 50 ls starting at P
1d77c 33 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 3 to pass to the
1d77d 20 78 55 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76 xUpdate .** inv
1d77e 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c ocation. The val
1d77f 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 28 ue in register (
1d780 50 33 2b 50 32 2d 31 29 20 63 6f 72 72 65 73 70 P3+P2-1) corresp
1d781 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 onds to the .**
1d782 70 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 p2th element of
1d783 74 68 65 20 61 72 67 76 20 61 72 72 61 79 20 70 the argv array p
1d784 61 73 73 65 64 20 74 6f 20 78 55 70 64 61 74 65 assed to xUpdate
1d785 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 ..**.** The xUpd
1d786 61 74 65 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 ate method will
1d787 64 6f 20 61 20 44 45 4c 45 54 45 20 6f 72 20 61 do a DELETE or a
1d788 6e 20 49 4e 53 45 52 54 20 6f 72 20 62 6f 74 68 n INSERT or both
1d789 2e 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 30 5d ..** The argv[0]
1d78a 20 65 6c 65 6d 65 6e 74 20 28 77 68 69 63 68 20 element (which
1d78b 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d corresponds to m
1d78c 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a emory cell P3).*
1d78d 2a 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f * is the rowid o
1d78e 66 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 f a row to delet
1d78f 65 2e 20 20 49 66 20 61 72 67 76 5b 30 5d 20 69 e. If argv[0] i
1d790 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a s NULL then no .
1d791 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75 ** deletion occu
1d792 72 73 2e 20 20 54 68 65 20 61 72 67 76 5b 31 5d rs. The argv[1]
1d793 20 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20 element is the
1d794 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 rowid of the new
1d795 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 69 73 20 .** row. This
1d796 63 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 can be NULL to h
1d797 61 76 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 ave the virtual
1d798 74 61 62 6c 65 20 73 65 6c 65 63 74 20 74 68 65 table select the
1d799 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66 new .** rowid f
1d79a 6f 72 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 or itself. The
1d79b 73 75 62 73 65 71 75 65 6e 74 20 65 6c 65 6d 65 subsequent eleme
1d79c 6e 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 nts in the array
1d79d 20 61 72 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c are .** the val
1d79e 75 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ues of columns i
1d79f 6e 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a n the new row..*
1d7a0 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 20 74 68 *.** If P2==1 th
1d7a1 65 6e 20 6e 6f 20 69 6e 73 65 72 74 20 69 73 20 en no insert is
1d7a2 70 65 72 66 6f 72 6d 65 64 2e 20 20 61 72 67 76 performed. argv
1d7a3 5b 30 5d 20 69 73 20 74 68 65 20 72 6f 77 69 64 [0] is the rowid
1d7a4 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20 of.** a row to
1d7a5 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 delete..**.** P1
1d7a6 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c is a boolean fl
1d7a7 61 67 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 ag. If it is set
1d7a8 20 74 6f 20 74 72 75 65 20 61 6e 64 20 74 68 65 to true and the
1d7a9 20 78 55 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a xUpdate call.**
1d7aa 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
1d7ab 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 then the value r
1d7ac 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
1d7ad 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 e3_last_insert_r
1d7ae 6f 77 69 64 28 29 20 0a 2a 2a 20 69 73 20 73 65 owid() .** is se
1d7af 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f t to the value o
1d7b0 66 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 f the rowid for
1d7b1 74 68 65 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 the row just ins
1d7b2 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f erted..*/.case O
1d7b3 50 5f 56 55 70 64 61 74 65 3a 20 7b 0a 20 20 73 P_VUpdate: {. s
1d7b4 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
1d7b5 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 ab = pOp->p4.pVt
1d7b6 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f ab;. sqlite3_mo
1d7b7 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 dule *pModule =
1d7b8 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 (sqlite3_module
1d7b9 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 *)pVtab->pModule
1d7ba 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70 ;. int nArg = p
1d7bb 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 Op->p2;. assert
1d7bc 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1d7bd 34 5f 56 54 41 42 20 29 3b 0a 20 20 69 66 28 20 4_VTAB );. if(
1d7be 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 pModule->xUpdate
1d7bf 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1d7c0 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1d7c1 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 72 65 zErrMsg, db, "re
1d7c2 61 64 2d 6f 6e 6c 79 20 74 61 62 6c 65 22 29 3b ad-only table");
1d7c3 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1d7c4 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b _ERROR;. }else{
1d7c5 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1d7c6 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 sqlite_int64 row
1d7c7 69 64 3b 0a 20 20 20 20 4d 65 6d 20 2a 2a 61 70 id;. Mem **ap
1d7c8 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a Arg = p->apArg;.
1d7c9 20 20 20 20 4d 65 6d 20 2a 70 58 20 3d 20 26 70 Mem *pX = &p
1d7ca 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1d7cb 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1d7cc 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nArg; i++){.
1d7cd 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 storeTypeInfo(
1d7ce 70 58 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 70 pX, 0);. ap
1d7cf 41 72 67 5b 69 5d 20 3d 20 70 58 3b 0a 20 20 20 Arg[i] = pX;.
1d7d0 20 20 20 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20 pX++;. }.
1d7d1 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1d7d2 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1d7d3 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1d7d4 6d 69 73 75 73 65 3b 0a 20 20 20 20 73 71 6c 69 misuse;. sqli
1d7d5 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 te3VtabLock(pVta
1d7d6 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f b);. rc = pMo
1d7d7 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 28 70 56 dule->xUpdate(pV
1d7d8 74 61 62 2c 20 6e 41 72 67 2c 20 61 70 41 72 67 tab, nArg, apArg
1d7d9 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 , &rowid);. s
1d7da 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1d7db 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1d7dc 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 p->zErrMsg = p
1d7dd 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 Vtab->zErrMsg;.
1d7de 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 pVtab->zErrMs
1d7df 67 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 g = 0;. sqlit
1d7e0 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c e3VtabUnlock(db,
1d7e1 20 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 pVtab);. if(
1d7e2 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1d7e3 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1d7e4 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1d7e5 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 . if( pOp->p1
1d7e6 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f && rc==SQLITE_O
1d7e7 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 K ){. asser
1d7e8 74 28 20 6e 41 72 67 3e 31 20 26 26 20 61 70 41 t( nArg>1 && apA
1d7e9 72 67 5b 30 5d 20 26 26 20 28 61 70 41 72 67 5b rg[0] && (apArg[
1d7ea 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 0]->flags&MEM_Nu
1d7eb 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20 64 62 2d ll) );. db-
1d7ec 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 72 6f 77 >lastRowid = row
1d7ed 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d id;. }. p-
1d7ee 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a >nChange++;. }.
1d7ef 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1d7f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1d7f1 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1d7f2 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54 ..#ifndef SQLIT
1d7f3 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 E_OMIT_PAGER_PRA
1d7f4 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 GMAS./* Opcode:
1d7f5 50 61 67 65 63 6f 75 6e 74 20 50 31 20 50 32 20 Pagecount P1 P2
1d7f6 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1d7f7 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75 e the current nu
1d7f8 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1d7f9 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f 20 database P1 to
1d7fa 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0a memory cell P2..
1d7fb 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 67 65 63 */.case OP_Pagec
1d7fc 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 ount: {
1d7fd 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1d7fe 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 70 lease */. int p
1d7ff 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 1 = pOp->p1; .
1d800 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 50 61 67 int nPage;. Pag
1d801 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c er *pPager = sql
1d802 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64 ite3BtreePager(d
1d803 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 29 3b b->aDb[p1].pBt);
1d804 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 .. rc = sqlite3
1d805 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
1d806 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a Pager, &nPage);.
1d807 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1d808 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d _OK ){. pOut-
1d809 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1d80a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 ;. pOut->u.i
1d80b 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62 = nPage;. }. b
1d80c 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a reak;.}.#endif..
1d80d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1d80e 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63 MIT_TRACE./* Opc
1d80f 6f 64 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a ode: Trace * * *
1d810 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 P4 *.**.** If t
1d811 72 61 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 racing is enable
1d812 64 20 28 62 79 20 74 68 65 20 73 71 6c 69 74 65 d (by the sqlite
1d813 33 5f 74 72 61 63 65 28 29 29 20 69 6e 74 65 72 3_trace()) inter
1d814 66 61 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 face, then.** th
1d815 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 e UTF-8 string c
1d816 6f 6e 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69 ontained in P4 i
1d817 73 20 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65 s emitted on the
1d818 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e trace callback.
1d819 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63 .*/.case OP_Trac
1d81a 65 3a 20 7b 0a 20 20 63 68 61 72 20 2a 7a 54 72 e: {. char *zTr
1d81b 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e 7a ace = (pOp->p4.z
1d81c 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20 70 ? pOp->p4.z : p
1d81d 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 7a ->zSql);. if( z
1d81e 54 72 61 63 65 20 29 7b 0a 20 20 20 20 69 66 28 Trace ){. if(
1d81f 20 64 62 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 db->xTrace ){.
1d820 20 20 20 20 20 64 62 2d 3e 78 54 72 61 63 65 28 db->xTrace(
1d821 64 62 2d 3e 70 54 72 61 63 65 41 72 67 2c 20 7a db->pTraceArg, z
1d822 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 69 Trace);. }.#i
1d823 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1d824 47 0a 20 20 20 20 69 66 28 20 28 64 62 2d 3e 66 G. if( (db->f
1d825 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 71 lags & SQLITE_Sq
1d826 6c 54 72 61 63 65 29 21 3d 30 20 29 7b 0a 20 20 lTrace)!=0 ){.
1d827 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 sqlite3Debug
1d828 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 61 63 Printf("SQL-trac
1d829 65 3a 20 25 73 5c 6e 22 2c 20 7a 54 72 61 63 65 e: %s\n", zTrace
1d82a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 );. }.#endif
1d82b 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
1d82c 2a 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a */. }. break;.
1d82d 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70 }.#endif.../* Op
1d82e 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a code: Noop * * *
1d82f 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f * *.**.** Do no
1d830 74 68 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 thing. This ins
1d831 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 truction is ofte
1d832 6e 20 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75 n useful as a ju
1d833 6d 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f mp.** destinatio
1d834 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 n..*/./*.** The
1d835 6d 61 67 69 63 20 45 78 70 6c 61 69 6e 20 6f 70 magic Explain op
1d836 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e code are only in
1d837 73 65 72 74 65 64 20 77 68 65 6e 20 65 78 70 6c serted when expl
1d838 61 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a ain==2 (which.**
1d839 20 69 73 20 74 6f 20 73 61 79 20 77 68 65 6e 20 is to say when
1d83a 74 68 65 20 45 58 50 4c 41 49 4e 20 51 55 45 52 the EXPLAIN QUER
1d83b 59 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 69 73 Y PLAN syntax is
1d83c 20 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20 used.).** This
1d83d 6f 70 63 6f 64 65 20 72 65 63 6f 72 64 73 20 69 opcode records i
1d83e 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
1d83f 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 the optimizer.
1d840 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 65 It is the.** the
1d841 20 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70 same as a no-op
1d842 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 73 6e . This opcodesn
1d843 65 76 65 72 20 61 70 70 65 61 72 73 20 69 6e 20 ever appears in
1d844 61 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 72 61 a real VM progra
1d845 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b m..*/.default: {
1d846 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 /* Thi
1d847 73 20 69 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e s is really OP_N
1d848 6f 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61 oop and OP_Expla
1d849 69 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d in */. break;.}
1d84a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1d84b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d84c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d84d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d84e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d84f 0a 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f 66 .** The cases of
1d850 20 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 the switch stat
1d851 65 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69 73 ement above this
1d852 20 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c line should all
1d853 20 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 be indented.**
1d854 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 by 6 spaces. Bu
1d855 74 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 t the left-most
1d856 36 20 73 70 61 63 65 73 20 68 61 76 65 20 62 65 6 spaces have be
1d857 65 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d en removed to im
1d858 70 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 prove the.** rea
1d859 64 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 dability. From
1d85a 74 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f this point on do
1d85b 77 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 wn, the normal i
1d85c 6e 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 ndentation rules
1d85d 20 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 are.** restored
1d85e 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**************
1d85f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d862 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1d863 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 . }..#ifdef V
1d864 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 DBE_PROFILE.
1d865 7b 0a 20 20 20 20 20 20 75 36 34 20 65 6c 61 70 {. u64 elap
1d866 73 65 64 20 3d 20 73 71 6c 69 74 65 33 48 77 74 sed = sqlite3Hwt
1d867 69 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 ime() - start;.
1d868 20 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 pOp->cycles
1d869 20 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 += elapsed;.
1d86a 20 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 pOp->cnt++;.#
1d86b 69 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72 if 0. fpr
1d86c 69 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31 intf(stdout, "%1
1d86d 30 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 64 29 0llu ", elapsed)
1d86e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1d86f 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 3VdbePrintOp(std
1d870 6f 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d out, origPc, &p-
1d871 3e 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 >aOp[origPc]);.#
1d872 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 endif. }.#end
1d873 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 if.. /* The f
1d874 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 ollowing code ad
1d875 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 ds nothing to th
1d876 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f e actual functio
1d877 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 nality. ** of
1d878 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 the program. I
1d879 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 t is only here f
1d87a 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 or testing and d
1d87b 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a ebugging.. **
1d87c 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 On the other ha
1d87d 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e nd, it does burn
1d87e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72 CPU cycles ever
1d87f 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 y time through.
1d880 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 ** the evalua
1d881 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 tor loop. So we
1d882 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 can leave it ou
1d883 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73 t when NDEBUG is
1d884 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f defined.. */
1d885 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
1d886 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d assert( pc>=
1d887 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 -1 && pc<p->nOp
1d888 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
1d889 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 E_DEBUG. if(
1d88a 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 p->trace ){.
1d88b 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 70 if( rc!=0 ) fp
1d88c 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 rintf(p->trace,"
1d88d 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 rc=%d\n",rc);.
1d88e 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 if( opProper
1d88f 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f ty & OPFLG_OUT2_
1d890 50 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 PRERELEASE ){.
1d891 20 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 registerTr
1d892 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f ace(p->trace, pO
1d893 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p2, pOut);.
1d894 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1d895 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1d896 4c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 LG_OUT3 ){.
1d897 20 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 registerTrace
1d898 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e (p->trace, pOp->
1d899 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 p3, pOut);.
1d89a 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 }. }.#endif
1d89b 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 /* SQLITE_DEBUG
1d89c 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e */.#endif /* N
1d89d 44 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a DEBUG */. } /*
1d89e 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 The end of the
1d89f 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 for(;;) loop the
1d8a0 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f loops through o
1d8a1 70 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 pcodes */.. /*
1d8a2 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 If we reach this
1d8a3 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 point, it means
1d8a4 20 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20 that execution
1d8a5 69 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 is finished with
1d8a6 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f . ** an error o
1d8a7 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a f some kind.. *
1d8a8 2f 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c /.vdbe_error_hal
1d8a9 74 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 20 t:. assert( rc
1d8aa 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b );. p->rc = rc;
1d8ab 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 . sqlite3VdbeHa
1d8ac 6c 74 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d lt(p);. if( rc=
1d8ad 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f =SQLITE_IOERR_NO
1d8ae 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 MEM ) db->malloc
1d8af 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 Failed = 1;. rc
1d8b0 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
1d8b1 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 .. /* This is t
1d8b2 68 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 he only way out
1d8b3 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 of this procedur
1d8b4 65 2e 20 20 57 65 20 68 61 76 65 20 74 6f 0a 20 e. We have to.
1d8b5 20 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 ** release the
1d8b6 6d 75 74 65 78 65 73 20 6f 6e 20 62 74 72 65 65 mutexes on btree
1d8b7 73 20 74 68 61 74 20 77 65 72 65 20 61 63 71 75 s that were acqu
1d8b8 69 72 65 64 20 61 74 20 74 68 65 0a 20 20 2a 2a ired at the. **
1d8b9 20 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65 top. */.vdbe_re
1d8ba 74 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42 turn:. sqlite3B
1d8bb 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 treeMutexArrayLe
1d8bc 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b ave(&p->aMutex);
1d8bd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 . return rc;..
1d8be 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 /* Jump to here
1d8bf 20 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20 if a string or
1d8c0 62 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e blob larger than
1d8c1 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 SQLITE_MAX_LENG
1d8c2 54 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 TH. ** is encou
1d8c3 6e 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f ntered.. */.too
1d8c4 5f 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 _big:. sqlite3S
1d8c5 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1d8c6 72 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 69 6e rMsg, db, "strin
1d8c7 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 g or blob too bi
1d8c8 67 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 g");. rc = SQLI
1d8c9 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 TE_TOOBIG;. got
1d8ca 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c o vdbe_error_hal
1d8cb 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f t;.. /* Jump to
1d8cc 20 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f here if a mallo
1d8cd 63 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a c() fails.. */.
1d8ce 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 no_mem:. db->ma
1d8cf 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
1d8d0 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d8d1 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d8d2 64 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f db, "out of memo
1d8d3 72 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c ry");. rc = SQL
1d8d4 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 ITE_NOMEM;. got
1d8d5 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c o vdbe_error_hal
1d8d6 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f t;.. /* Jump to
1d8d7 20 68 65 72 65 20 66 6f 72 20 61 6e 20 53 51 4c here for an SQL
1d8d8 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 ITE_MISUSE error
1d8d9 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 .. */.abort_due
1d8da 5f 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63 _to_misuse:. rc
1d8db 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 = SQLITE_MISUSE
1d8dc 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 ;. /* Fall thru
1d8dd 20 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f into abort_due_
1d8de 74 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f to_error */.. /
1d8df 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 * Jump to here f
1d8e0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e or any other kin
1d8e1 64 20 6f 66 20 66 61 74 61 6c 20 65 72 72 6f 72 d of fatal error
1d8e2 2e 20 20 54 68 65 20 22 72 63 22 20 76 61 72 69 . The "rc" vari
1d8e3 61 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 able. ** should
1d8e4 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 hold the error
1d8e5 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f number.. */.abo
1d8e6 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a rt_due_to_error:
1d8e7 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 45 . assert( p->zE
1d8e8 72 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66 rrMsg==0 );. if
1d8e9 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1d8ea 65 64 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 ed ) rc = SQLITE
1d8eb 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 _NOMEM;. if( rc
1d8ec 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e !=SQLITE_IOERR_N
1d8ed 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 OMEM ){. sqli
1d8ee 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1d8ef 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
1d8f0 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 s", sqlite3ErrSt
1d8f1 72 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f r(rc));. }. go
1d8f2 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 to vdbe_error_ha
1d8f3 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 lt;.. /* Jump t
1d8f4 6f 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71 o here if the sq
1d8f5 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 lite3_interrupt(
1d8f6 29 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69 ) API sets the i
1d8f7 6e 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c nterrupt. ** fl
1d8f8 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 ag.. */.abort_d
1d8f9 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a ue_to_interrupt:
1d8fa 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75 . assert( db->u
1d8fb 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 1.isInterrupted
1d8fc 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 );. rc = SQLITE
1d8fd 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 2d _INTERRUPT;. p-
1d8fe 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 >rc = rc;. sqli
1d8ff 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1d900 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
1d901 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 s", sqlite3ErrSt
1d902 72 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76 r(rc));. goto v
1d903 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a dbe_error_halt;.
1d904 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
1d905 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 ** End of vdbe.c
1d906 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1d907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d908 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d909 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1d90a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
1d90b 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a beblob.c *******
1d90c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d90d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d90e 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 */./*.** 2007 Ma
1d90f 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 y 1.**.** The au
1d910 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1d911 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1d912 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1d913 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
1d914 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1d915 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
1d916 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
1d917 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
1d918 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
1d919 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
1d91a 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
1d91b 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
1d91c 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
1d91d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
1d91e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
1d91f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1d920 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1d921 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d924 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1d925 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
1d926 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
1d927 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
1d928 6e 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 nt incremental B
1d929 4c 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 LOB I/O..**.** $
1d92a 49 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76 Id: vdbeblob.c,v
1d92b 20 31 2e 33 31 20 32 30 30 39 2f 30 33 2f 32 34 1.31 2009/03/24
1d92c 20 31 35 3a 30 38 3a 31 30 20 64 72 68 20 45 78 15:08:10 drh Ex
1d92d 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 p $.*/...#ifndef
1d92e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
1d92f 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c RBLOB../*.** Val
1d930 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a id sqlite3_blob*
1d931 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74 handles point t
1d932 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63 o Incrblob struc
1d933 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 tures..*/.typede
1d934 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f f struct Incrblo
1d935 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75 b Incrblob;.stru
1d936 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20 ct Incrblob {.
1d937 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
1d938 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 /* Copy
1d939 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65 of "flags" passe
1d93a 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f d to sqlite3_blo
1d93b 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e b_open() */. in
1d93c 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1d93d 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1d93e 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 open blob, in b
1d93f 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f ytes */. int iO
1d940 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
1d941 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 /* Byte offset
1d942 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 of blob in curs
1d943 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 or data */. BtC
1d944 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 ursor *pCsr;
1d945 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 /* Cursor p
1d946 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 ointing at blob
1d947 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 row */. sqlite3
1d948 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 _stmt *pStmt;
1d949 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f /* Statement ho
1d94a 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 lding cursor ope
1d94b 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a n */. sqlite3 *
1d94c 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f db; /
1d94d 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 * The associated
1d94e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a database */.};.
1d94f 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c ./*.** Open a bl
1d950 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 ob handle..*/.SQ
1d951 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1d952 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a ite3_blob_open(.
1d953 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 sqlite3* db,
1d954 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1d955 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1d956 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 tion */. const
1d957 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 char *zDb,
1d958 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65 /* The attache
1d959 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 d database conta
1d95a 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a ining the blob *
1d95b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1d95c 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 zTable, /* T
1d95d 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e he table contain
1d95e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a ing the blob */.
1d95f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
1d960 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65 olumn, /* The
1d961 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 column containi
1d962 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 ng the blob */.
1d963 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 sqlite_int64 iR
1d964 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ow, /* The
1d965 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 row containing t
1d966 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74 he glob */. int
1d967 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
1d968 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20 /* True ->
1d969 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 read/write acces
1d96a 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64 s, false -> read
1d96b 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74 -only */. sqlit
1d96c 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 e3_blob **ppBlob
1d96d 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
1d96e 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62 accessing the b
1d96f 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72 lob returned her
1d970 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41 e */.){. int nA
1d971 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e ttempt = 0;. in
1d972 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 t iCol;
1d973 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1d974 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77 f zColumn in row
1d975 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a -record */.. /*
1d976 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72 This VDBE progr
1d977 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65 am seeks a btree
1d978 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 cursor to the i
1d979 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20 dentified . **
1d97a 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74 db/table/row ent
1d97b 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66 ry. The reason f
1d97c 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20 or using a vdbe
1d97d 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a program instead.
1d97e 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20 ** of writing
1d97f 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20 code to use the
1d980 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72 b-tree layer dir
1d981 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68 ectly is that th
1d982 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 e. ** vdbe prog
1d983 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64 ram will take ad
1d984 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76 vantage of the v
1d985 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69 arious transacti
1d986 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 on,. ** locking
1d987 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c and error handl
1d988 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75 ing infrastructu
1d989 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 re built into th
1d98a 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a e vdbe.. **. *
1d98b 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20 * After seeking
1d98c 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20 the cursor, the
1d98d 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e vdbe executes an
1d98e 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20 OP_ResultRow..
1d98f 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61 ** Code externa
1d990 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 l to the Vdbe th
1d991 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65 en "borrows" the
1d992 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 b-tree cursor a
1d993 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20 nd. ** uses it
1d994 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1d995 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c blob_read(), bl
1d996 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a ob_write() and .
1d997 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28 ** blob_bytes(
1d998 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a ) functions.. *
1d999 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 *. ** The sqlit
1d99a 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20 e3_blob_close()
1d99b 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a function finaliz
1d99c 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 es the vdbe prog
1d99d 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20 ram,. ** which
1d99e 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65 closes the b-tre
1d99f 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f e cursor and (po
1d9a0 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20 ssibly) commits
1d9a1 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 the . ** transa
1d9a2 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 ction.. */. st
1d9a3 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f atic const VdbeO
1d9a4 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d pList openBlob[]
1d9a5 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61 = {. {OP_Tra
1d9a6 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 nsaction, 0, 0,
1d9a7 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74 0}, /* 0: St
1d9a8 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f art a transactio
1d9a9 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72 n */. {OP_Ver
1d9aa 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c ifyCookie, 0, 0,
1d9ab 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68 0}, /* 1: Ch
1d9ac 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63 eck the schema c
1d9ad 6f 6f 6b 69 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a ookie */.. /*
1d9ae 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c One of the foll
1d9af 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74 72 75 owing two instru
1d9b0 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c 61 63 ctions is replac
1d9b1 65 64 20 62 79 20 61 6e 0a 20 20 20 20 2a 2a 20 ed by an. **
1d9b2 4f 50 5f 4e 6f 6f 70 20 62 65 66 6f 72 65 20 65 OP_Noop before e
1d9b3 78 65 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a xection.. */.
1d9b4 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 {OP_OpenRead
1d9b5 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 , 0, 0, 0},
1d9b6 20 20 20 2f 2a 20 32 3a 20 4f 70 65 6e 20 63 75 /* 2: Open cu
1d9b7 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69 rsor 0 for readi
1d9b8 6e 67 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 ng */. {OP_Op
1d9b9 65 6e 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 enWrite, 0, 0, 0
1d9ba 7d 2c 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 4f }, /* 3: O
1d9bb 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 pen cursor 0 for
1d9bc 20 72 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a read/write */..
1d9bd 20 20 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 {OP_Variable
1d9be 2c 20 31 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 , 1, 1, 1},
1d9bf 20 20 20 2f 2a 20 34 3a 20 50 75 73 68 20 74 68 /* 4: Push th
1d9c0 65 20 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73 e rowid to the s
1d9c1 74 61 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f tack */. {OP_
1d9c2 4e 6f 74 45 78 69 73 74 73 2c 20 30 2c 20 38 2c NotExists, 0, 8,
1d9c3 20 31 7d 2c 20 20 20 20 20 20 20 2f 2a 20 35 3a 1}, /* 5:
1d9c4 20 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 Seek the cursor
1d9c5 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 */. {OP_Colu
1d9c6 6d 6e 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 mn, 0, 0, 1},
1d9c7 20 20 20 20 20 20 20 2f 2a 20 36 20 20 2a 2f 0a /* 6 */.
1d9c8 20 20 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f {OP_ResultRo
1d9c9 77 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 w, 1, 0, 0},
1d9ca 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20 20 20 /* 7 */.
1d9cb 7b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c {OP_Close, 0, 0,
1d9cc 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 0}, /
1d9cd 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f * 8 */. {OP_
1d9ce 48 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 Halt, 0, 0, 0},
1d9cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 39 20 /* 9
1d9d0 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20 */. };.. Vdbe
1d9d1 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 *v = 0;. int rc
1d9d2 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1d9d3 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a char zErr[128];.
1d9d4 0a 20 20 7a 45 72 72 5b 30 5d 20 3d 20 30 3b 0a . zErr[0] = 0;.
1d9d5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1d9d6 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 enter(db->mutex)
1d9d7 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 50 61 72 ;. do {. Par
1d9d8 73 65 20 73 50 61 72 73 65 3b 0a 20 20 20 20 54 se sParse;. T
1d9d9 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20 20 20 able *pTab;..
1d9da 20 6d 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c memset(&sParse,
1d9db 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 0, sizeof(Parse
1d9dc 29 29 3b 0a 20 20 20 20 73 50 61 72 73 65 2e 64 ));. sParse.d
1d9dd 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 28 b = db;.. if(
1d9de 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1d9df 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 (db) ){. sq
1d9e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1d9e1 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 e(db->mutex);.
1d9e2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1d9e3 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 7d 0a E_MISUSE;. }.
1d9e4 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1d9e5 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 eEnterAll(db);.
1d9e6 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 pTab = sqlite
1d9e7 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 26 73 50 3LocateTable(&sP
1d9e8 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c 65 2c arse, 0, zTable,
1d9e9 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 zDb);. if( p
1d9ea 54 61 62 20 26 26 20 49 73 56 69 72 74 75 61 6c Tab && IsVirtual
1d9eb 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 (pTab) ){.
1d9ec 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 pTab = 0;.
1d9ed 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1d9ee 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 &sParse, "cannot
1d9ef 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 74 61 open virtual ta
1d9f0 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 ble: %s", zTable
1d9f1 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 );. }.#ifndef
1d9f2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 SQLITE_OMIT_VIE
1d9f3 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26 W. if( pTab &
1d9f4 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 & pTab->pSelect
1d9f5 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 ){. pTab =
1d9f6 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 0;. sqlite3
1d9f7 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72 73 65 ErrorMsg(&sParse
1d9f8 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 , "cannot open v
1d9f9 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 iew: %s", zTable
1d9fa 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
1d9fb 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 7b if( !pTab ){
1d9fc 0a 20 20 20 20 20 20 69 66 28 20 73 50 61 72 73 . if( sPars
1d9fd 65 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 e.zErrMsg ){.
1d9fe 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1d9ff 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 rintf(sizeof(zEr
1da00 72 29 2c 20 7a 45 72 72 2c 20 22 25 73 22 2c 20 r), zErr, "%s",
1da01 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 29 3b sParse.zErrMsg);
1da02 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
1da03 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1da04 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 29 sParse.zErrMsg)
1da05 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
1da06 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 ITE_ERROR;.
1da07 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1da08 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 fetyOff(db);.
1da09 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
1da0a 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 eaveAll(db);.
1da0b 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 goto blob_ope
1da0c 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 n_out;. }..
1da0d 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63 68 20 /* Now search
1da0e 70 54 61 62 20 66 6f 72 20 74 68 65 20 65 78 61 pTab for the exa
1da0f 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 ct column. */.
1da10 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 for(iCol=0; iC
1da11 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b ol < pTab->nCol;
1da12 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 20 20 iCol++) {.
1da13 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
1da14 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 Cmp(pTab->aCol[i
1da15 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c Col].zName, zCol
1da16 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 umn)==0 ){.
1da17 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1da18 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
1da19 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c iCol==pTab->nCol
1da1a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1da1b 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
1da1c 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 f(zErr), zErr, "
1da1d 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 no such column:
1da1e 5c 22 25 73 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e \"%s\"", zColumn
1da1f 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 );. rc = SQ
1da20 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
1da21 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1da22 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 afetyOff(db);.
1da23 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1da24 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 LeaveAll(db);.
1da25 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1da26 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 en_out;. }..
1da27 20 20 20 2f 2a 20 49 66 20 74 68 65 20 76 61 6c /* If the val
1da28 75 65 20 69 73 20 62 65 69 6e 67 20 6f 70 65 6e ue is being open
1da29 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 20 ed for writing,
1da2a 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 check that the.
1da2b 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 ** column is
1da2c 6e 6f 74 20 69 6e 64 65 78 65 64 2e 20 49 74 20 not indexed. It
1da2d 69 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 72 is against the r
1da2e 75 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61 6e 0a ules to open an.
1da2f 20 20 20 20 2a 2a 20 69 6e 64 65 78 65 64 20 63 ** indexed c
1da30 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e olumn for writin
1da31 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 g.. */. if
1da32 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 ( flags ){.
1da33 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 Index *pIdx;.
1da34 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 for(pIdx=pTa
1da35 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b b->pIndex; pIdx;
1da36 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 pIdx=pIdx->pNex
1da37 74 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 t){. int
1da38 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a j;. for(j
1da39 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c =0; j<pIdx->nCol
1da3a 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 umn; j++){.
1da3b 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 if( pIdx->a
1da3c 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43 6f 6c iColumn[j]==iCol
1da3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1da3e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1da3f 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1da40 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Err,.
1da41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da42 20 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 69 "cannot open i
1da43 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f ndexed column fo
1da44 72 20 77 72 69 74 69 6e 67 22 29 3b 0a 20 20 20 r writing");.
1da45 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
1da46 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
1da47 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 (void)sq
1da48 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1da49 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 b);.
1da4a 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1da4b 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 eAll(db);.
1da4c 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f goto blob_
1da4d 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 open_out;.
1da4e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
1da4f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
1da50 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 56 64 v = sqlite3Vd
1da51 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 beCreate(db);.
1da52 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 if( v ){.
1da53 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 int iDb = sqlit
1da54 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1da55 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d db, pTab->pSchem
1da56 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 a);. sqlite
1da57 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 3VdbeAddOpList(v
1da58 2c 20 73 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f , sizeof(openBlo
1da59 62 29 2f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 b)/sizeof(VdbeOp
1da5a 4c 69 73 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 List), openBlob)
1da5b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 ;.. /* Conf
1da5c 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61 igure the OP_Tra
1da5d 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 nsaction */.
1da5e 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1da5f 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29 ngeP1(v, 0, iDb)
1da60 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1da61 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30 dbeChangeP2(v, 0
1da62 2c 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 , (flags ? 1 : 0
1da63 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f ));.. /* Co
1da64 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 56 nfigure the OP_V
1da65 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f 0a 20 erifyCookie */.
1da66 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1da67 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c 20 69 ChangeP1(v, 1, i
1da68 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 Db);. sqlit
1da69 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 e3VdbeChangeP2(v
1da6a 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 , 1, pTab->pSche
1da6b 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 ma->schema_cooki
1da6c 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 e);.. /* Ma
1da6d 6b 65 20 73 75 72 65 20 61 20 6d 75 74 65 78 20 ke sure a mutex
1da6e 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 74 is held on the t
1da6f 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73 able to be acces
1da70 73 65 64 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c sed */. sql
1da71 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
1da72 65 28 76 2c 20 69 44 62 29 3b 20 0a 0a 20 20 20 e(v, iDb); ..
1da73 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 /* Remove eit
1da74 68 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 her the OP_OpenW
1da75 72 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 rite or OpenRead
1da76 2e 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 . Set the P2 .
1da77 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 ** parameter
1da78 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f of the other to
1da79 20 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 0a 20 20 pTab->tnum. .
1da7a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
1da7b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f ite3VdbeChangeTo
1da7c 4e 6f 6f 70 28 76 2c 20 28 66 6c 61 67 73 20 3f Noop(v, (flags ?
1da7d 20 32 20 3a 20 33 29 2c 20 31 29 3b 0a 20 20 20 2 : 3), 1);.
1da7e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1da7f 61 6e 67 65 50 32 28 76 2c 20 28 66 6c 61 67 73 angeP2(v, (flags
1da80 20 3f 20 33 20 3a 20 32 29 2c 20 70 54 61 62 2d ? 3 : 2), pTab-
1da81 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 >tnum);. sq
1da82 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1da83 33 28 76 2c 20 28 66 6c 61 67 73 20 3f 20 33 20 3(v, (flags ? 3
1da84 3a 20 32 29 2c 20 69 44 62 29 3b 0a 0a 20 20 20 : 2), iDb);..
1da85 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 /* Configure
1da86 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
1da87 6c 75 6d 6e 73 2e 20 43 6f 6e 66 69 67 75 72 65 lumns. Configure
1da88 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 0a 20 the cursor to.
1da89 20 20 20 20 20 2a 2a 20 74 68 69 6e 6b 20 74 68 ** think th
1da8a 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 at the table has
1da8b 20 6f 6e 65 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e one more column
1da8c 20 74 68 61 6e 20 69 74 20 72 65 61 6c 6c 79 0a than it really.
1da8d 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2e 20 41 ** does. A
1da8e 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 n OP_Column to r
1da8f 65 74 72 69 65 76 65 20 74 68 69 73 20 69 6d 61 etrieve this ima
1da90 67 69 6e 61 72 79 20 63 6f 6c 75 6d 6e 20 77 69 ginary column wi
1da91 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 ll. ** alwa
1da92 79 73 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c ys return an SQL
1da93 20 4e 55 4c 4c 2e 20 54 68 69 73 20 69 73 20 75 NULL. This is u
1da94 73 65 66 75 6c 20 62 65 63 61 75 73 65 20 69 74 seful because it
1da95 20 6d 65 61 6e 73 0a 20 20 20 20 20 20 2a 2a 20 means. **
1da96 77 65 20 63 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 we can invoke OP
1da97 5f 43 6f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 _Column to fill
1da98 69 6e 20 74 68 65 20 76 64 62 65 20 63 75 72 73 in the vdbe curs
1da99 6f 72 73 20 74 79 70 65 20 0a 20 20 20 20 20 20 ors type .
1da9a 2a 2a 20 61 6e 64 20 6f 66 66 73 65 74 20 63 61 ** and offset ca
1da9b 63 68 65 20 77 69 74 68 6f 75 74 20 63 61 75 73 che without caus
1da9c 69 6e 67 20 61 6e 79 20 49 4f 2e 0a 20 20 20 20 ing any IO..
1da9d 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
1da9e 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 e3VdbeChangeP4(v
1da9f 2c 20 66 6c 61 67 73 20 3f 20 33 20 3a 20 32 2c , flags ? 3 : 2,
1daa0 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 SQLITE_INT_TO_P
1daa1 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 TR(pTab->nCol+1)
1daa2 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 , P4_INT32);.
1daa3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1daa4 61 6e 67 65 50 32 28 76 2c 20 36 2c 20 70 54 61 angeP2(v, 6, pTa
1daa5 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 b->nCol);.
1daa6 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
1daa7 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
1daa8 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1daa9 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31 Ready(v, 1, 1, 1
1daaa 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
1daab 20 20 7d 0a 20 20 20 0a 20 20 20 20 73 71 6c 69 }. . sqli
1daac 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c te3BtreeLeaveAll
1daad 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 (db);. rc = s
1daae 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1daaf 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 db);. if( rc!
1dab0 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 =SQLITE_OK || db
1dab1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1dab2 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f {. goto blo
1dab3 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 b_open_out;.
1dab4 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 }.. sqlite3_b
1dab5 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 ind_int64((sqlit
1dab6 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 e3_stmt *)v, 1,
1dab7 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 iRow);. rc =
1dab8 73 71 6c 69 74 65 33 5f 73 74 65 70 28 28 73 71 sqlite3_step((sq
1dab9 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b lite3_stmt *)v);
1daba 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1dabb 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 ITE_ROW ){.
1dabc 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 nAttempt++;.
1dabd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f rc = sqlite3_
1dabe 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 finalize((sqlite
1dabf 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 3_stmt *)v);.
1dac0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1dac1 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 ntf(sizeof(zErr)
1dac2 2c 20 7a 45 72 72 2c 20 73 71 6c 69 74 65 33 5f , zErr, sqlite3_
1dac3 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 errmsg(db));.
1dac4 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d 0a v = 0;. }.
1dac5 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74 65 } while( nAtte
1dac6 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51 4c mpt<5 && rc==SQL
1dac7 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a 20 ITE_SCHEMA );..
1dac8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1dac9 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ROW ){. /* Th
1daca 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61 73 e row-record has
1dacb 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75 63 been opened suc
1dacc 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63 6b cessfully. Check
1dacd 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a that the. **
1dace 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73 74 column in quest
1dacf 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65 78 ion contains tex
1dad0 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66 20 t or a blob. If
1dad1 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 it contains.
1dad2 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20 75 ** text, it is u
1dad3 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 p to the caller
1dad4 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f 64 to get the encod
1dad5 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20 2a ing right.. *
1dad6 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20 2a /. Incrblob *
1dad7 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20 74 pBlob;. u32 t
1dad8 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30 ype = v->apCsr[0
1dad9 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b 0a ]->aType[iCol];.
1dada 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31 32 . if( type<12
1dadb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1dadc 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
1dadd 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 f(zErr), zErr, "
1dade 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 61 6c 75 cannot open valu
1dadf 65 20 6f 66 20 74 79 70 65 20 25 73 22 2c 0a 20 e of type %s",.
1dae0 20 20 20 20 20 20 20 20 20 74 79 70 65 3d 3d 30 type==0
1dae1 3f 22 6e 75 6c 6c 22 3a 20 74 79 70 65 3d 3d 37 ?"null": type==7
1dae2 3f 22 72 65 61 6c 22 3a 20 22 69 6e 74 65 67 65 ?"real": "intege
1dae3 72 22 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 r". );.
1dae4 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1dae5 52 4f 52 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ROR;. goto
1dae6 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 blob_open_out;.
1dae7 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f 62 20 3d }. pBlob =
1dae8 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 73 71 6c (Incrblob *)sql
1dae9 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
1daea 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 63 72 (db, sizeof(Incr
1daeb 62 6c 6f 62 29 29 3b 0a 20 20 20 20 69 66 28 20 blob));. if(
1daec 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1daed 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1daee 33 44 62 46 72 65 65 28 64 62 2c 20 70 42 6c 6f 3DbFree(db, pBlo
1daef 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 b);. goto b
1daf0 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 lob_open_out;.
1daf1 20 20 7d 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 66 }. pBlob->f
1daf2 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 lags = flags;.
1daf3 20 20 70 42 6c 6f 62 2d 3e 70 43 73 72 20 3d 20 pBlob->pCsr =
1daf4 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 70 43 v->apCsr[0]->pC
1daf5 75 72 73 6f 72 3b 0a 20 20 20 20 73 71 6c 69 74 ursor;. sqlit
1daf6 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 e3BtreeEnterCurs
1daf7 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b or(pBlob->pCsr);
1daf8 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1daf9 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 70 eCacheOverflow(p
1dafa 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 Blob->pCsr);.
1dafb 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1dafc 76 65 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e veCursor(pBlob->
1dafd 70 43 73 72 29 3b 0a 20 20 20 20 70 42 6c 6f 62 pCsr);. pBlob
1dafe 2d 3e 70 53 74 6d 74 20 3d 20 28 73 71 6c 69 74 ->pStmt = (sqlit
1daff 65 33 5f 73 74 6d 74 20 2a 29 76 3b 0a 20 20 20 e3_stmt *)v;.
1db00 20 70 42 6c 6f 62 2d 3e 69 4f 66 66 73 65 74 20 pBlob->iOffset
1db01 3d 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 = v->apCsr[0]->a
1db02 4f 66 66 73 65 74 5b 69 43 6f 6c 5d 3b 0a 20 20 Offset[iCol];.
1db03 20 20 70 42 6c 6f 62 2d 3e 6e 42 79 74 65 20 3d pBlob->nByte =
1db04 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1db05 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65 29 3b alTypeLen(type);
1db06 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 64 62 20 3d . pBlob->db =
1db07 20 64 62 3b 0a 20 20 20 20 2a 70 70 42 6c 6f 62 db;. *ppBlob
1db08 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 = (sqlite3_blob
1db09 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 20 20 72 63 *)pBlob;. rc
1db0a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1db0b 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 }else if( rc==SQ
1db0c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 LITE_OK ){. s
1db0d 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1db0e 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 sizeof(zErr), zE
1db0f 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 72 6f 77 rr, "no such row
1db10 69 64 3a 20 25 6c 6c 64 22 2c 20 69 52 6f 77 29 id: %lld", iRow)
1db11 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1db12 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c E_ERROR;. }..bl
1db13 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 7a ob_open_out:. z
1db14 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72 29 Err[sizeof(zErr)
1db15 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 -1] = '\0';. if
1db16 28 20 76 20 26 26 20 28 72 63 21 3d 53 51 4c 49 ( v && (rc!=SQLI
1db17 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c TE_OK || db->mal
1db18 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 locFailed) ){.
1db19 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e sqlite3VdbeFin
1db1a 61 6c 69 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20 alize(v);. }.
1db1b 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c sqlite3Error(db,
1db1c 20 72 63 2c 20 28 72 63 3d 3d 53 51 4c 49 54 45 rc, (rc==SQLITE
1db1d 5f 4f 4b 3f 30 3a 7a 45 72 72 29 29 3b 0a 20 20 _OK?0:zErr));.
1db1e 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 rc = sqlite3ApiE
1db1f 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 xit(db, rc);. s
1db20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1db21 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 ve(db->mutex);.
1db22 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1db23 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f *.** Close a blo
1db24 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 b handle that wa
1db25 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65 s previously cre
1db26 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 ated using.** sq
1db27 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 lite3_blob_open(
1db28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 )..*/.SQLITE_API
1db29 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f int sqlite3_blo
1db2a 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f b_close(sqlite3_
1db2b 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 blob *pBlob){.
1db2c 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 Incrblob *p = (I
1db2d 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b ncrblob *)pBlob;
1db2e 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c . int rc;. sql
1db2f 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 64 62 20 ite3 *db;.. db
1db30 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 = p->db;. sqlit
1db31 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1db32 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 b->mutex);. rc
1db33 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 = sqlite3_finali
1db34 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 ze(p->pStmt);.
1db35 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1db36 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f , p);. sqlite3_
1db37 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e mutex_leave(db->
1db38 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
1db39 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 rc;.}../*.** Pe
1db3a 72 66 6f 72 6d 20 61 20 72 65 61 64 20 6f 72 20 rform a read or
1db3b 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 write operation
1db3c 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 on a blob.*/.sta
1db3d 74 69 63 20 69 6e 74 20 62 6c 6f 62 52 65 61 64 tic int blobRead
1db3e 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 Write(. sqlite3
1db3f 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 _blob *pBlob, .
1db40 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 void *z, . int
1db41 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f 66 66 73 n, . int iOffs
1db42 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a 78 43 61 et, . int (*xCa
1db43 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 ll)(BtCursor*, u
1db44 33 32 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 0a 32, u32, void*).
1db45 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 49 ){. int rc;. I
1db46 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e ncrblob *p = (In
1db47 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a crblob *)pBlob;.
1db48 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c Vdbe *v;. sql
1db49 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
1db4a 3b 20 20 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d ; .. sqlite3_m
1db4b 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d utex_enter(db->m
1db4c 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 utex);. v = (Vd
1db4d 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 be*)p->pStmt;..
1db4e 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 if( n<0 || iOff
1db4f 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 set<0 || (iOffse
1db50 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b t+n)>p->nByte ){
1db51 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 . /* Request
1db52 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e is out of range.
1db53 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69 Return a transi
1db54 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 ent error. */.
1db55 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1db56 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ROR;. sqlite3
1db57 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 Error(db, SQLITE
1db58 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 _ERROR, 0);. }
1db59 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b else if( v==0 ){
1db5a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . /* If there
1db5b 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 is no statement
1db5c 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 handle, then th
1db5d 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 e blob-handle ha
1db5e 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 s. ** already
1db5f 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 been invalidate
1db60 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 d. Return SQLITE
1db61 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 _ABORT in this c
1db62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ase.. */.
1db63 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 rc = SQLITE_ABOR
1db64 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 T;. }else{.
1db65 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 /* Call either B
1db66 74 72 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 treeData() or Bt
1db67 72 65 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 reePutData(). If
1db68 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 SQLITE_ABORT is
1db69 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 . ** returned
1db6a 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 , clean-up the s
1db6b 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e tatement handle.
1db6c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
1db6d 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 rt( db == v->db
1db6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
1db6f 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 reeEnterCursor(p
1db70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 ->pCsr);. rc
1db71 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c = xCall(p->pCsr,
1db72 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 iOffset+p->iOff
1db73 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 set, n, z);.
1db74 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1db75 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 eCursor(p->pCsr)
1db76 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1db77 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 LITE_ABORT ){.
1db78 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 sqlite3VdbeF
1db79 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 inalize(v);.
1db7a 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a p->pStmt = 0;.
1db7b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1db7c 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 db->errCode = r
1db7d 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d c;. v->rc =
1db7e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rc;. }. }.
1db7f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1db80 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 Exit(db, rc);.
1db81 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1db82 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ave(db->mutex);.
1db83 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1db84 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 /*.** Read data
1db85 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 from a blob hand
1db86 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
1db87 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1db88 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f ob_read(sqlite3_
1db89 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 blob *pBlob, voi
1db8a 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 d *z, int n, int
1db8b 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 iOffset){. ret
1db8c 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 urn blobReadWrit
1db8d 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 e(pBlob, z, n, i
1db8e 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 Offset, sqlite3B
1db8f 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a treeData);.}../*
1db90 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 .** Write data t
1db91 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e o a blob handle.
1db92 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1db93 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
1db94 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c write(sqlite3_bl
1db95 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 ob *pBlob, const
1db96 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c void *z, int n,
1db97 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 int iOffset){.
1db98 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 return blobRead
1db99 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f Write(pBlob, (vo
1db9a 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 id *)z, n, iOffs
1db9b 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 et, sqlite3Btree
1db9c 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a PutData);.}../*.
1db9d 2a 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 ** Query a blob
1db9e 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 handle for the s
1db9f 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e ize of the data.
1dba0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 .**.** The Incrb
1dba1 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 lob.nByte field
1dba2 69 73 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 is fixed for the
1dba3 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 lifetime of the
1dba4 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 Incrblob.** so
1dba5 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 no mutex is requ
1dba6 69 72 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e ired for access.
1dba7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1dba8 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
1dba9 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c bytes(sqlite3_bl
1dbaa 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e ob *pBlob){. In
1dbab 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 crblob *p = (Inc
1dbac 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 rblob *)pBlob;.
1dbad 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42 79 74 65 return p->nByte
1dbae 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 23 ;.}..#endif /* #
1dbaf 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1dbb0 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a IT_INCRBLOB */..
1dbb1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1dbb2 45 6e 64 20 6f 66 20 76 64 62 65 62 6c 6f 62 2e End of vdbeblob.
1dbb3 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1dbb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1dbb6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1dbb7 42 65 67 69 6e 20 66 69 6c 65 20 6a 6f 75 72 6e Begin file journ
1dbb8 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a al.c ***********
1dbb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1dbbb 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
1dbbc 74 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 22.**.** The a
1dbbd 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
1dbbe 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
1dbbf 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
1dbc0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
1dbc1 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
1dbc2 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
1dbc3 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
1dbc4 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
1dbc5 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
1dbc6 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
1dbc7 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
1dbc8 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
1dbc9 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
1dbca 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
1dbcb 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
1dbcc 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
1dbcd 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
1dbce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbcf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dbd2 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 ***.**.** @(#) $
1dbd3 49 64 3a 20 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 Id: journal.c,v
1dbd4 31 2e 39 20 32 30 30 39 2f 30 31 2f 32 30 20 31 1.9 2009/01/20 1
1dbd5 37 3a 30 36 3a 32 37 20 64 61 6e 69 65 6c 6b 31 7:06:27 danielk1
1dbd6 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 977 Exp $.*/..#i
1dbd7 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
1dbd8 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a LE_ATOMIC_WRITE.
1dbd9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ./*.** This file
1dbda 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 73 70 implements a sp
1dbdb 65 63 69 61 6c 20 6b 69 6e 64 20 6f 66 20 73 71 ecial kind of sq
1dbdc 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 lite3_file objec
1dbdd 74 20 75 73 65 64 0a 2a 2a 20 62 79 20 53 51 4c t used.** by SQL
1dbde 69 74 65 20 74 6f 20 63 72 65 61 74 65 20 6a 6f ite to create jo
1dbdf 75 72 6e 61 6c 20 66 69 6c 65 73 20 69 66 20 74 urnal files if t
1dbe0 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 he atomic-write
1dbe1 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 optimization.**
1dbe2 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a is enabled..**.*
1dbe3 2a 20 54 68 65 20 64 69 73 74 69 6e 63 74 69 76 * The distinctiv
1dbe4 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
1dbe5 20 6f 66 20 74 68 69 73 20 73 71 6c 69 74 65 33 of this sqlite3
1dbe6 5f 66 69 6c 65 20 69 73 20 74 68 61 74 20 74 68 _file is that th
1dbe7 65 0a 2a 2a 20 61 63 74 75 61 6c 20 6f 6e 20 64 e.** actual on d
1dbe8 69 73 6b 20 66 69 6c 65 20 69 73 20 63 72 65 61 isk file is crea
1dbe9 74 65 64 20 6c 61 7a 69 6c 79 2e 20 57 68 65 6e ted lazily. When
1dbea 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 72 65 the file is cre
1dbeb 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 63 61 6c ated,.** the cal
1dbec 6c 65 72 20 73 70 65 63 69 66 69 65 73 20 61 20 ler specifies a
1dbed 62 75 66 66 65 72 20 73 69 7a 65 20 66 6f 72 20 buffer size for
1dbee 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 an in-memory buf
1dbef 66 65 72 20 74 6f 0a 2a 2a 20 62 65 20 75 73 65 fer to.** be use
1dbf0 64 20 74 6f 20 73 65 72 76 69 63 65 20 72 65 61 d to service rea
1dbf1 64 28 29 20 61 6e 64 20 77 72 69 74 65 28 29 20 d() and write()
1dbf2 72 65 71 75 65 73 74 73 2e 20 54 68 65 20 61 63 requests. The ac
1dbf3 74 75 61 6c 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 tual file.** on
1dbf4 64 69 73 6b 20 69 73 20 6e 6f 74 20 63 72 65 61 disk is not crea
1dbf5 74 65 64 20 6f 72 20 70 6f 70 75 6c 61 74 65 64 ted or populated
1dbf6 20 75 6e 74 69 6c 20 65 69 74 68 65 72 3a 0a 2a until either:.*
1dbf7 2a 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 69 6e *.** 1) The in
1dbf8 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e -memory represen
1dbf9 74 61 74 69 6f 6e 20 67 72 6f 77 73 20 74 6f 6f tation grows too
1dbfa 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 61 large for the a
1dbfb 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 20 20 20 llocated .**
1dbfc 20 20 62 75 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 buffer, or.**
1dbfd 20 20 32 29 20 54 68 65 20 73 71 6c 69 74 65 33 2) The sqlite3
1dbfe 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 20 JournalCreate()
1dbff 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1dc00 65 64 2e 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 ed..*/..../*.**
1dc01 41 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 6f 62 A JournalFile ob
1dc02 6a 65 63 74 20 69 73 20 61 20 73 75 62 63 6c 61 ject is a subcla
1dc03 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 ss of sqlite3_fi
1dc04 6c 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 73 le used by.** as
1dc05 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 an open file ha
1dc06 6e 64 6c 65 20 66 6f 72 20 6a 6f 75 72 6e 61 6c ndle for journal
1dc07 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 files..*/.struc
1dc08 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 7b 0a t JournalFile {.
1dc09 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 sqlite3_io_met
1dc0a 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 hods *pMethod;
1dc0b 20 20 2f 2a 20 49 2f 4f 20 6d 65 74 68 6f 64 73 /* I/O methods
1dc0c 20 6f 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 on journal file
1dc0d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 3b s */. int nBuf;
1dc0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dc0f 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1dc10 66 20 7a 42 75 66 5b 5d 20 69 6e 20 62 79 74 65 f zBuf[] in byte
1dc11 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 75 s */. char *zBu
1dc12 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
1dc13 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 /* Space
1dc14 74 6f 20 62 75 66 66 65 72 20 6a 6f 75 72 6e 61 to buffer journa
1dc15 6c 20 77 72 69 74 65 73 20 2a 2f 0a 20 20 69 6e l writes */. in
1dc16 74 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 t iSize;
1dc17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1dc18 20 41 6d 6f 75 6e 74 20 6f 66 20 7a 42 75 66 5b Amount of zBuf[
1dc19 5d 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 ] currently used
1dc1a 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b */. int flags;
1dc1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dc1c 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 66 /* xOpen f
1dc1d 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 lags */. sqlite
1dc1e 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 3_vfs *pVfs;
1dc1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1dc20 20 22 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 "real" underlyi
1dc21 6e 67 20 56 46 53 20 2a 2f 0a 20 20 73 71 6c 69 ng VFS */. sqli
1dc22 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c 3b te3_file *pReal;
1dc23 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1dc24 68 65 20 22 72 65 61 6c 22 20 75 6e 64 65 72 6c he "real" underl
1dc25 79 69 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69 ying file descri
1dc26 70 74 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ptor */. const
1dc27 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 char *zJournal;
1dc28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
1dc29 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
1dc2a 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 file */.};.type
1dc2b 64 65 66 20 73 74 72 75 63 74 20 4a 6f 75 72 6e def struct Journ
1dc2c 61 6c 46 69 6c 65 20 4a 6f 75 72 6e 61 6c 46 69 alFile JournalFi
1dc2d 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74 le;../*.** If it
1dc2e 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 does not alread
1dc2f 79 20 65 78 69 73 74 73 2c 20 63 72 65 61 74 65 y exists, create
1dc30 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 74 68 and populate th
1dc31 65 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 20 0a e on-disk file .
1dc32 2a 2a 20 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 ** for JournalFi
1dc33 6c 65 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 le p..*/.static
1dc34 69 6e 74 20 63 72 65 61 74 65 46 69 6c 65 28 4a int createFile(J
1dc35 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 29 7b 0a ournalFile *p){.
1dc36 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1dc37 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 2d 3e E_OK;. if( !p->
1dc38 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c pReal ){. sql
1dc39 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c ite3_file *pReal
1dc3a 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 = (sqlite3_file
1dc3b 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 72 63 *)&p[1];. rc
1dc3c 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
1dc3d 28 70 2d 3e 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f (p->pVfs, p->zJo
1dc3e 75 72 6e 61 6c 2c 20 70 52 65 61 6c 2c 20 70 2d urnal, pReal, p-
1dc3f 3e 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 >flags, 0);.
1dc40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1dc41 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 52 K ){. p->pR
1dc42 65 61 6c 20 3d 20 70 52 65 61 6c 3b 0a 20 20 20 eal = pReal;.
1dc43 20 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3e if( p->iSize>
1dc44 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 0 ){. ass
1dc45 65 72 74 28 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d ert(p->iSize<=p-
1dc46 3e 6e 42 75 66 29 3b 0a 20 20 20 20 20 20 20 20 >nBuf);.
1dc47 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
1dc48 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 70 2d ite(p->pReal, p-
1dc49 3e 7a 42 75 66 2c 20 70 2d 3e 69 53 69 7a 65 2c >zBuf, p->iSize,
1dc4a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
1dc4b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1dc4c 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f rc;.}../*.** Clo
1dc4d 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a se the file..*/.
1dc4e 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 43 static int jrnlC
1dc4f 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
1dc50 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4a 6f 75 72 e *pJfd){. Jour
1dc51 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f nalFile *p = (Jo
1dc52 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 urnalFile *)pJfd
1dc53 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c ;. if( p->pReal
1dc54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f ){. sqlite3O
1dc55 73 43 6c 6f 73 65 28 70 2d 3e 70 52 65 61 6c 29 sClose(p->pReal)
1dc56 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
1dc57 66 72 65 65 28 70 2d 3e 7a 42 75 66 29 3b 0a 20 free(p->zBuf);.
1dc58 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1dc59 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 K;.}../*.** Read
1dc5a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 66 data from the f
1dc5b 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
1dc5c 6e 74 20 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 nt jrnlRead(. s
1dc5d 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1dc5e 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 d, /* The jou
1dc5f 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 rnal file from w
1dc60 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a hich to read */.
1dc61 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 void *zBuf,
1dc62 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 /* Put
1dc63 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 the results here
1dc64 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 */. int iAmt,
1dc65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1dc66 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1dc67 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c to read */. sql
1dc68 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 ite_int64 iOfst
1dc69 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 /* Begin rea
1dc6a 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 ding at this off
1dc6b 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 set */.){. int
1dc6c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1dc6d 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1dc6e 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1dc6f 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d *)pJfd;. if( p-
1dc70 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 >pReal ){. rc
1dc71 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
1dc72 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c (p->pReal, zBuf,
1dc73 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 iAmt, iOfst);.
1dc74 20 7d 65 6c 73 65 20 69 66 28 20 28 69 41 6d 74 }else if( (iAmt
1dc75 2b 69 4f 66 73 74 29 3e 70 2d 3e 69 53 69 7a 65 +iOfst)>p->iSize
1dc76 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
1dc77 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
1dc78 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 READ;. }else{.
1dc79 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 memcpy(zBuf,
1dc7a 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c &p->zBuf[iOfst],
1dc7b 20 69 41 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 iAmt);. }. re
1dc7c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1dc7d 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 * Write data to
1dc7e 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1dc7f 74 69 63 20 69 6e 74 20 6a 72 6e 6c 57 72 69 74 tic int jrnlWrit
1dc80 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
1dc81 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1dc82 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1dc83 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 into which to wr
1dc84 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ite */. const v
1dc85 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 oid *zBuf,
1dc86 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 /* Take data to
1dc87 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 be written from
1dc88 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 here */. int iA
1dc89 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1dc8a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1dc8b 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
1dc8c 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1dc8d 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 Ofst /* Begi
1dc8e 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 n writing at thi
1dc8f 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 s offset into th
1dc90 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 e file */.){. i
1dc91 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1dc92 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 K;. JournalFile
1dc93 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 *p = (JournalFi
1dc94 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 le *)pJfd;. if(
1dc95 20 21 70 2d 3e 70 52 65 61 6c 20 26 26 20 28 69 !p->pReal && (i
1dc96 4f 66 73 74 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 Ofst+iAmt)>p->nB
1dc97 75 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 uf ){. rc = c
1dc98 72 65 61 74 65 46 69 6c 65 28 70 29 3b 0a 20 20 reateFile(p);.
1dc99 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
1dc9a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
1dc9b 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 p->pReal ){.
1dc9c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1dc9d 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c sWrite(p->pReal,
1dc9e 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 zBuf, iAmt, iOf
1dc9f 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a st);. }else{.
1dca0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d memcpy(&p-
1dca1 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 >zBuf[iOfst], zB
1dca2 75 66 2c 20 69 41 6d 74 29 3b 0a 20 20 20 20 20 uf, iAmt);.
1dca3 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 28 69 if( p->iSize<(i
1dca4 4f 66 73 74 2b 69 41 6d 74 29 20 29 7b 0a 20 20 Ofst+iAmt) ){.
1dca5 20 20 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d p->iSize =
1dca6 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3b 0a 20 (iOfst+iAmt);.
1dca7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1dca8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1dca9 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
1dcaa 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1dcab 74 69 63 20 69 6e 74 20 6a 72 6e 6c 54 72 75 6e tic int jrnlTrun
1dcac 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c cate(sqlite3_fil
1dcad 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f e *pJfd, sqlite_
1dcae 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 69 int64 size){. i
1dcaf 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1dcb0 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 K;. JournalFile
1dcb1 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 *p = (JournalFi
1dcb2 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 le *)pJfd;. if(
1dcb3 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 p->pReal ){.
1dcb4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
1dcb5 72 75 6e 63 61 74 65 28 70 2d 3e 70 52 65 61 6c runcate(p->pReal
1dcb6 2c 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 , size);. }else
1dcb7 20 69 66 28 20 73 69 7a 65 3c 70 2d 3e 69 53 69 if( size<p->iSi
1dcb8 7a 65 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 ze ){. p->iSi
1dcb9 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 ze = size;. }.
1dcba 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1dcbb 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 *.** Sync the fi
1dcbc 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1dcbd 74 20 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 t jrnlSync(sqlit
1dcbe 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 69 e3_file *pJfd, i
1dcbf 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 nt flags){. int
1dcc0 20 72 63 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 rc;. JournalFi
1dcc1 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c le *p = (Journal
1dcc2 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 File *)pJfd;. i
1dcc3 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 f( p->pReal ){.
1dcc4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1dcc5 73 53 79 6e 63 28 70 2d 3e 70 52 65 61 6c 2c 20 sSync(p->pReal,
1dcc6 66 6c 61 67 73 29 3b 0a 20 20 7d 65 6c 73 65 7b flags);. }else{
1dcc7 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1dcc8 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 _OK;. }. retur
1dcc9 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 n rc;.}../*.** Q
1dcca 75 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 uery the size of
1dccb 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 the file in byt
1dccc 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
1dccd 74 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 t jrnlFileSize(s
1dcce 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1dccf 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 d, sqlite_int64
1dcd0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 *pSize){. int r
1dcd1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1dcd2 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1dcd3 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1dcd4 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e )pJfd;. if( p->
1dcd5 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 pReal ){. rc
1dcd6 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
1dcd7 69 7a 65 28 70 2d 3e 70 52 65 61 6c 2c 20 70 53 ize(p->pReal, pS
1dcd8 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ize);. }else{.
1dcd9 20 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c *pSize = (sql
1dcda 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 69 53 ite_int64) p->iS
1dcdb 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ize;. }. retur
1dcdc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
1dcdd 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 able of methods
1dcde 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 for JournalFile
1dcdf 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a sqlite3_file obj
1dce0 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ect..*/.static s
1dce1 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f truct sqlite3_io
1dce2 5f 6d 65 74 68 6f 64 73 20 4a 6f 75 72 6e 61 6c _methods Journal
1dce3 46 69 6c 65 4d 65 74 68 6f 64 73 20 3d 20 7b 0a FileMethods = {.
1dce4 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
1dce5 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
1dce6 20 20 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 jrnlClose,
1dce7 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 /* xClose */.
1dce8 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 2f jrnlRead, /
1dce9 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6a 72 6e * xRead */. jrn
1dcea 6c 57 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 lWrite, /* x
1dceb 57 72 69 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 Write */. jrnlT
1dcec 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72 runcate, /* xTr
1dced 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c uncate */. jrnl
1dcee 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 Sync, /* xS
1dcef 79 6e 63 20 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c ync */. jrnlFil
1dcf0 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 eSize, /* xFile
1dcf1 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Size */. 0,
1dcf2 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
1dcf3 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 k */. 0,
1dcf4 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
1dcf5 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1dcf6 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
1dcf7 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 servedLock */.
1dcf8 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1dcf9 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a * xFileControl *
1dcfa 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 /. 0,
1dcfb 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a /* xSectorSiz
1dcfc 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 e */. 0
1dcfd 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 /* xDevice
1dcfe 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 Characteristics
1dcff 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 */.};../* .** Op
1dd00 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c en a journal fil
1dd01 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1dd02 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1dd03 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 JournalOpen(. s
1dd04 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1dd05 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 , /* The
1dd06 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 VFS to use for
1dd07 61 63 74 75 61 6c 20 66 69 6c 65 20 49 2f 4f 20 actual file I/O
1dd08 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1dd09 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 *zName,
1dd0a 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a /* Name of the j
1dd0b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
1dd0c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
1dd0d 4a 66 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 Jfd, /* P
1dd0e 72 65 61 6c 6c 6f 63 61 74 65 64 2c 20 62 6c 61 reallocated, bla
1dd0f 6e 6b 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a nk file handle *
1dd10 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
1dd11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1dd12 2a 20 4f 70 65 6e 69 6e 67 20 66 6c 61 67 73 20 * Opening flags
1dd13 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 20 20 20 */. int nBuf
1dd14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd15 2f 2a 20 42 79 74 65 73 20 62 75 66 66 65 72 65 /* Bytes buffere
1dd16 64 20 62 65 66 6f 72 65 20 6f 70 65 6e 69 6e 67 d before opening
1dd17 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a the file */.){.
1dd18 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1dd19 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1dd1a 2a 29 70 4a 66 64 3b 0a 20 20 6d 65 6d 73 65 74 *)pJfd;. memset
1dd1b 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4a 6f (p, 0, sqlite3Jo
1dd1c 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 29 urnalSize(pVfs))
1dd1d 3b 0a 20 20 69 66 28 20 6e 42 75 66 3e 30 20 29 ;. if( nBuf>0 )
1dd1e 7b 0a 20 20 20 20 70 2d 3e 7a 42 75 66 20 3d 20 {. p->zBuf =
1dd1f 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
1dd20 6f 28 6e 42 75 66 29 3b 0a 20 20 20 20 69 66 28 o(nBuf);. if(
1dd21 20 21 70 2d 3e 7a 42 75 66 20 29 7b 0a 20 20 20 !p->zBuf ){.
1dd22 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1dd23 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
1dd24 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
1dd25 6e 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 n sqlite3OsOpen(
1dd26 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 pVfs, zName, pJf
1dd27 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 d, flags, 0);.
1dd28 7d 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d }. p->pMethod =
1dd29 20 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 &JournalFileMet
1dd2a 68 6f 64 73 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 hods;. p->nBuf
1dd2b 3d 20 6e 42 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 = nBuf;. p->fla
1dd2c 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 2d gs = flags;. p-
1dd2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d >zJournal = zNam
1dd2e 65 3b 0a 20 20 70 2d 3e 70 56 66 73 20 3d 20 70 e;. p->pVfs = p
1dd2f 56 66 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 Vfs;. return SQ
1dd30 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1dd31 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e * If the argumen
1dd32 74 20 70 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 t p points to a
1dd33 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 74 72 75 JournalFile stru
1dd34 63 74 75 72 65 2c 20 61 6e 64 20 74 68 65 20 75 cture, and the u
1dd35 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c nderlying.** fil
1dd36 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 e has not yet be
1dd37 65 6e 20 63 72 65 61 74 65 64 2c 20 63 72 65 61 en created, crea
1dd38 74 65 20 69 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 te it now..*/.SQ
1dd39 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1dd3a 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 sqlite3JournalC
1dd3b 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 reate(sqlite3_fi
1dd3c 6c 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d le *p){. if( p-
1dd3d 3e 70 4d 65 74 68 6f 64 73 21 3d 26 4a 6f 75 72 >pMethods!=&Jour
1dd3e 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 20 29 nalFileMethods )
1dd3f 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1dd40 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 ITE_OK;. }. re
1dd41 74 75 72 6e 20 63 72 65 61 74 65 46 69 6c 65 28 turn createFile(
1dd42 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1dd43 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 );.}../* .** Ret
1dd44 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1dd45 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
1dd46 20 74 6f 20 73 74 6f 72 65 20 61 20 4a 6f 75 72 to store a Jour
1dd47 6e 61 6c 46 69 6c 65 20 74 68 61 74 20 75 73 65 nalFile that use
1dd48 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f s vfs.** pVfs to
1dd49 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 create the unde
1dd4a 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 rlying on-disk f
1dd4b 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f iles..*/.SQLITE_
1dd4c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1dd4d 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 te3JournalSize(s
1dd4e 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1dd4f 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 56 66 ){. return (pVf
1dd50 73 2d 3e 73 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 s->szOsFile+size
1dd51 6f 66 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 29 29 of(JournalFile))
1dd52 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a ;.}.#endif../***
1dd53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1dd54 6f 66 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a of journal.c ***
1dd55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1dd58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1dd59 6e 20 66 69 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 n file memjourna
1dd5a 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
1dd5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1dd5d 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 37 * 2008 October 7
1dd5e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1dd5f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1dd60 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1dd61 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1dd62 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1dd63 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1dd64 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1dd65 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1dd66 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1dd67 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1dd68 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1dd69 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1dd6a 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1dd6b 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1dd6c 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1dd6d 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1dd6e 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1dd6f 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1dd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1dd73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1dd74 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
1dd75 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 contains code us
1dd76 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 e to implement a
1dd77 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c n in-memory roll
1dd78 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a back journal..**
1dd79 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 The in-memory r
1dd7a 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
1dd7b 69 73 20 75 73 65 64 20 74 6f 20 6a 6f 75 72 6e is used to journ
1dd7c 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 al transactions
1dd7d 66 6f 72 0a 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a for.** ":memory:
1dd7e 22 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 " databases and
1dd7f 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c when the journal
1dd80 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 59 20 70 72 61 _mode=MEMORY pra
1dd81 67 6d 61 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a gma is used..**.
1dd82 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6d 65 6d ** @(#) $Id: mem
1dd83 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 31 32 journal.c,v 1.12
1dd84 20 32 30 30 39 2f 30 35 2f 30 34 20 31 31 3a 34 2009/05/04 11:4
1dd85 32 3a 33 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 2:30 danielk1977
1dd86 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f Exp $.*/../* Fo
1dd87 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 rward references
1dd88 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 to internal str
1dd89 75 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 uctures */.typed
1dd8a 65 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 ef struct MemJou
1dd8b 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b rnal MemJournal;
1dd8c 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1dd8d 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f FilePoint FilePo
1dd8e 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 int;.typedef str
1dd8f 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 uct FileChunk Fi
1dd90 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 leChunk;../* Spa
1dd91 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
1dd92 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
1dd93 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 is allocated in
1dd94 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a increments of.**
1dd95 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 this many bytes
1dd96 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 ..**.** The size
1dd97 20 63 68 6f 73 65 6e 20 69 73 20 61 20 6c 69 74 chosen is a lit
1dd98 74 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 61 20 tle less than a
1dd99 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 54 power of two. T
1dd9a 68 61 74 20 77 61 79 2c 0a 2a 2a 20 74 68 65 20 hat way,.** the
1dd9b 46 69 6c 65 43 68 75 6e 6b 20 6f 62 6a 65 63 74 FileChunk object
1dd9c 20 77 69 6c 6c 20 68 61 76 65 20 61 20 73 69 7a will have a siz
1dd9d 65 20 74 68 61 74 20 61 6c 6d 6f 73 74 20 65 78 e that almost ex
1dd9e 61 63 74 6c 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 actly fills.** a
1dd9f 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c power-of-two al
1dda0 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 location. This
1dda1 6d 69 6d 69 6d 69 7a 65 73 20 77 61 73 74 65 64 mimimizes wasted
1dda2 20 73 70 61 63 65 20 69 6e 20 70 6f 77 65 72 2d space in power-
1dda3 6f 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 of-two.** memory
1dda4 20 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a allocators..*/.
1dda5 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f #define JOURNAL_
1dda6 43 48 55 4e 4b 53 49 5a 45 20 28 28 69 6e 74 29 CHUNKSIZE ((int)
1dda7 28 31 30 32 34 2d 73 69 7a 65 6f 66 28 46 69 6c (1024-sizeof(Fil
1dda8 65 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d eChunk*)))../* M
1dda9 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 acro to find the
1ddaa 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 minimum of two
1ddab 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a numeric values..
1ddac 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 */.#ifndef MIN.#
1ddad 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 define MIN(x,y)
1ddae 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 ((x)<(y)?(x):(y
1ddaf 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a )).#endif../*.**
1ddb0 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f The rollback jo
1ddb1 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70 6f 73 65 urnal is compose
1ddb2 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d of a linked li
1ddb3 73 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 st of these stru
1ddb4 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 ctures..*/.struc
1ddb5 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 t FileChunk {.
1ddb6 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 78 74 FileChunk *pNext
1ddb7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1ddb8 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 69 6e /* Next chunk in
1ddb9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a the journal */.
1ddba 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 u8 zChunk[JOUR
1ddbb 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 NAL_CHUNKSIZE];
1ddbc 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 /* Content of
1ddbd 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b this chunk */.};
1ddbe 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
1ddbf 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
1ddc0 63 74 20 73 65 72 76 65 73 20 61 73 20 61 20 63 ct serves as a c
1ddc1 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 72 ursor into the r
1ddc2 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
1ddc3 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 .** The cursor c
1ddc4 61 6e 20 62 65 20 65 69 74 68 65 72 20 66 6f 72 an be either for
1ddc5 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 reading or writ
1ddc6 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 ing..*/.struct F
1ddc7 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 73 71 6c ilePoint {. sql
1ddc8 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73 ite3_int64 iOffs
1ddc9 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 et; /*
1ddca 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 Offset from the
1ddcb 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
1ddcc 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 43 file */. FileC
1ddcd 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 hunk *pChunk;
1ddce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
1ddcf 65 63 69 66 69 63 20 63 68 75 6e 6b 20 69 6e 74 ecific chunk int
1ddd0 6f 20 77 68 69 63 68 20 63 75 72 73 6f 72 20 70 o which cursor p
1ddd1 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a oints */.};../*.
1ddd2 2a 2a 20 54 68 69 73 20 73 75 62 63 6c 61 73 73 ** This subclass
1ddd3 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f is a subclass o
1ddd4 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 20 f sqlite3_file.
1ddd5 20 45 61 63 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 Each open memor
1ddd6 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 y-journal.** is
1ddd7 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
1ddd8 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 his class..*/.st
1ddd9 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ruct MemJournal
1ddda 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d {. sqlite3_io_m
1dddb 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b ethods *pMethod;
1dddc 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 63 6c /* Parent cl
1dddd 61 73 73 2e 20 4d 55 53 54 20 42 45 20 46 49 52 ass. MUST BE FIR
1ddde 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e ST */. FileChun
1dddf 6b 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20 k *pFirst;
1dde0 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 64 20 /* Head
1dde1 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 of in-memory chu
1dde2 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 46 69 6c nk-list */. Fil
1dde3 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69 6e 74 3b ePoint endpoint;
1dde4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1dde5 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 Pointer to the e
1dde6 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a nd of the file *
1dde7 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 72 65 /. FilePoint re
1dde8 61 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 adpoint;
1dde9 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1ddea 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
1ddeb 20 6c 61 73 74 20 78 52 65 61 64 28 29 20 2a 2f last xRead() */
1ddec 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 .};../*.** Read
1dded 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 69 6e data from the in
1ddee 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 -memory journal
1ddef 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 74 file. This is t
1ddf0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
1ddf1 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 n.** of the sqli
1ddf2 74 65 33 5f 76 66 73 2e 78 52 65 61 64 20 6d 65 te3_vfs.xRead me
1ddf3 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 thod..*/.static
1ddf4 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 int memjrnlRead(
1ddf5 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
1ddf6 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 *pJfd, /* The
1ddf7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 journal file fr
1ddf8 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 om which to read
1ddf9 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 */. void *zBuf
1ddfa 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
1ddfb 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20 Put the results
1ddfc 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 here */. int iA
1ddfd 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1ddfe 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1ddff 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 tes to read */.
1de00 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f sqlite_int64 iO
1de01 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e fst /* Begin
1de02 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 reading at this
1de03 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 offset */.){.
1de04 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1de05 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1de06 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20 3d fd;. u8 *zOut =
1de07 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52 65 zBuf;. int nRe
1de08 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e 74 ad = iAmt;. int
1de09 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 iChunkOffset;.
1de0a 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 FileChunk *pChu
1de0b 6e 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c 69 74 65 nk;.. /* SQLite
1de0c 20 6e 65 76 65 72 20 74 72 69 65 73 20 74 6f 20 never tries to
1de0d 72 65 61 64 20 70 61 73 74 20 74 68 65 20 65 6e read past the en
1de0e 64 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 d of a rollback
1de0f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
1de10 20 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 2b assert( iOfst+
1de11 69 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e iAmt<=p->endpoin
1de12 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20 t.iOffset );..
1de13 69 66 28 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 if( p->readpoint
1de14 2e 69 4f 66 66 73 65 74 21 3d 69 4f 66 73 74 20 .iOffset!=iOfst
1de15 7c 7c 20 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 || iOfst==0 ){.
1de16 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 sqlite3_int64
1de17 20 69 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 66 iOff = 0;. f
1de18 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 or(pChunk=p->pFi
1de19 72 73 74 3b 20 0a 20 20 20 20 20 20 20 20 41 4c rst; . AL
1de1a 57 41 59 53 28 70 43 68 75 6e 6b 29 20 26 26 20 WAYS(pChunk) &&
1de1b 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 (iOff+JOURNAL_CH
1de1c 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b UNKSIZE)<=iOfst;
1de1d 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d . pChunk=
1de1e 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 pChunk->pNext.
1de1f 20 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20 ){. iOff
1de20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b += JOURNAL_CHUNK
1de21 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 SIZE;. }. }e
1de22 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20 lse{. pChunk
1de23 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 = p->readpoint.p
1de24 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 Chunk;. }.. iC
1de25 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e hunkOffset = (in
1de26 74 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c t)(iOfst%JOURNAL
1de27 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 _CHUNKSIZE);. d
1de28 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 o {. int iSpa
1de29 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 ce = JOURNAL_CHU
1de2a 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f NKSIZE - iChunkO
1de2b 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e ffset;. int n
1de2c 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64 Copy = MIN(nRead
1de2d 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b , (JOURNAL_CHUNK
1de2e 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 SIZE - iChunkOff
1de2f 73 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 set));. memcp
1de30 79 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d y(zOut, &pChunk-
1de31 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 >zChunk[iChunkOf
1de32 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 fset], nCopy);.
1de33 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 zOut += nCopy
1de34 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69 ;. nRead -= i
1de35 53 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e Space;. iChun
1de36 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d kOffset = 0;. }
1de37 20 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 while( nRead>=0
1de38 20 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 && (pChunk=pChu
1de39 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26 nk->pNext)!=0 &&
1de3a 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d nRead>0 );. p-
1de3b 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 >readpoint.iOffs
1de3c 65 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b et = iOfst+iAmt;
1de3d 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e . p->readpoint.
1de3e 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b pChunk = pChunk;
1de3f 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
1de40 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 E_OK;.}../*.** W
1de41 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 rite data to the
1de42 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1de43 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 int memjrnlWrit
1de44 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
1de45 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1de46 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1de47 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 into which to wr
1de48 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ite */. const v
1de49 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 oid *zBuf,
1de4a 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 /* Take data to
1de4b 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 be written from
1de4c 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 here */. int iA
1de4d 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1de4e 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1de4f 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
1de50 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1de51 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 Ofst /* Begi
1de52 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 n writing at thi
1de53 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 s offset into th
1de54 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d e file */.){. M
1de55 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 emJournal *p = (
1de56 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 MemJournal *)pJf
1de57 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 d;. int nWrite
1de58 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 = iAmt;. u8 *zW
1de59 72 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75 rite = (u8 *)zBu
1de5a 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d f;.. /* An in-m
1de5b 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 emory journal fi
1de5c 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 le should only e
1de5d 76 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20 ver be appended
1de5e 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 to. Random. **
1de5f 61 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72 access writes ar
1de60 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62 e not required b
1de61 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 y sqlite.. */.
1de62 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 3d 3d assert( iOfst==
1de63 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 p->endpoint.iOff
1de64 73 65 74 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f set );. UNUSED_
1de65 50 41 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29 PARAMETER(iOfst)
1de66 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69 ;.. while( nWri
1de67 74 65 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65 te>0 ){. File
1de68 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 Chunk *pChunk =
1de69 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 p->endpoint.pChu
1de6a 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75 nk;. int iChu
1de6b 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 nkOffset = (int)
1de6c 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 (p->endpoint.iOf
1de6d 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 fset%JOURNAL_CHU
1de6e 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74 NKSIZE);. int
1de6f 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57 iSpace = MIN(nW
1de70 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 rite, JOURNAL_CH
1de71 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b UNKSIZE - iChunk
1de72 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66 Offset);.. if
1de73 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d ( iChunkOffset==
1de74 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65 0 ){. /* Ne
1de75 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69 w chunk is requi
1de76 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68 red to extend th
1de77 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 e file. */.
1de78 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77 FileChunk *pNew
1de79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
1de7a 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 c(sizeof(FileChu
1de7b 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nk));. if(
1de7c 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 !pNew ){.
1de7d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
1de7e 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 OERR_NOMEM;.
1de7f 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e }. pNew->
1de80 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 pNext = 0;.
1de81 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 if( pChunk ){.
1de82 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1de83 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 ->pFirst );.
1de84 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 pChunk->pNex
1de85 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 t = pNew;.
1de86 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 }else{. a
1de87 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73 ssert( !p->pFirs
1de88 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e t );. p->
1de89 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 pFirst = pNew;.
1de8a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e }. p->
1de8b 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 endpoint.pChunk
1de8c 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 = pNew;. }..
1de8d 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e memcpy(&p->en
1de8e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a dpoint.pChunk->z
1de8f 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 Chunk[iChunkOffs
1de90 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70 et], zWrite, iSp
1de91 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65 ace);. zWrite
1de92 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 += iSpace;.
1de93 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65 nWrite -= iSpace
1de94 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e ;. p->endpoin
1de95 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70 t.iOffset += iSp
1de96 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ace;. }.. retu
1de97 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1de98 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
1de99 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1de9a 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 tic int memjrnlT
1de9b 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f runcate(sqlite3_
1de9c 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 file *pJfd, sqli
1de9d 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a te_int64 size){.
1de9e 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 MemJournal *p
1de9f 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 = (MemJournal *)
1dea0 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e pJfd;. FileChun
1dea1 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73 k *pChunk;. ass
1dea2 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 ert(size==0);.
1dea3 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1dea4 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b (size);. pChunk
1dea5 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20 = p->pFirst;.
1dea6 77 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b while( pChunk ){
1dea7 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a . FileChunk *
1dea8 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 pTmp = pChunk;.
1dea9 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 pChunk = pChu
1deaa 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 nk->pNext;. s
1deab 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 qlite3_free(pTmp
1deac 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1dead 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 MemJournalOpen(p
1deae 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 Jfd);. return S
1deaf 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1deb0 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c ** Close the fil
1deb1 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1deb2 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 memjrnlClose(sq
1deb3 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1deb4 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e ){. memjrnlTrun
1deb5 63 61 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20 cate(pJfd, 0);.
1deb6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1deb7 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e K;.}.../*.** Syn
1deb8 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a c the file..**.*
1deb9 2a 20 53 79 6e 63 69 6e 67 20 61 6e 20 69 6e 2d * Syncing an in-
1deba 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69 memory journal i
1debb 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c s a no-op. And,
1debc 20 69 6e 20 66 61 63 74 2c 20 74 68 69 73 20 72 in fact, this r
1debd 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 outine.** is nev
1debe 65 72 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 77 er called in a w
1debf 6f 72 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 orking implement
1dec0 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6d 70 ation. This imp
1dec1 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 lementation.** e
1dec2 78 69 73 74 73 20 70 75 72 65 6c 79 20 61 73 20 xists purely as
1dec3 61 20 63 6f 6e 74 69 6e 67 65 6e 63 79 2c 20 69 a contingency, i
1dec4 6e 20 63 61 73 65 20 73 6f 6d 65 20 6d 61 6c 66 n case some malf
1dec5 75 6e 63 74 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 unction in some
1dec6 6f 74 68 65 72 0a 2a 2a 20 70 61 72 74 20 6f 66 other.** part of
1dec7 20 53 51 4c 69 74 65 20 63 61 75 73 65 73 20 53 SQLite causes S
1dec8 79 6e 63 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 ync to be called
1dec9 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a by mistake..*/.
1deca 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1decb 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 nlSync(sqlite3_f
1decc 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e ile *NotUsed, in
1decd 74 20 4e 6f 74 55 73 65 64 32 29 7b 20 20 20 2f t NotUsed2){ /
1dece 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 55 4e 55 *NO_TEST*/. UNU
1decf 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
1ded0 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
1ded1 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
1ded2 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 /*NO_T
1ded3 45 53 54 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 EST*/. assert(
1ded4 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 0 );
1ded5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ded6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ded7 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f /*NO_TEST*/
1ded8 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1ded9 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
1deda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dedb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dedc 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 /*NO_TEST*/.}
1dedd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dedf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dee0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f /*NO_
1dee1 54 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 TEST*/../*.** Qu
1dee2 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ery the size of
1dee3 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65 the file in byte
1dee4 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
1dee5 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 memjrnlFileSize
1dee6 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1dee7 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 Jfd, sqlite_int6
1dee8 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4 *pSize){. Mem
1dee9 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 Journal *p = (Me
1deea 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b mJournal *)pJfd;
1deeb 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c . *pSize = (sql
1deec 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e ite_int64) p->en
1deed 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a dpoint.iOffset;.
1deee 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1deef 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 OK;.}../*.** Tab
1def0 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f le of methods fo
1def1 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c r MemJournal sql
1def2 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 ite3_file object
1def3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
1def4 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ct sqlite3_io_me
1def5 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c thods MemJournal
1def6 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c Methods = {. 1,
1def7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1def8 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 /* iVersion */.
1def9 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 memjrnlClose,
1defa 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a /* xClose */.
1defb 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 memjrnlRead,
1defc 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a /* xRead */.
1defd 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 memjrnlWrite,
1defe 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f /* xWrite */
1deff 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 . memjrnlTrunca
1df00 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 te, /* xTruncat
1df01 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 e */. memjrnlSy
1df02 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e nc, /* xSyn
1df03 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 c */. memjrnlFi
1df04 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c leSize, /* xFil
1df05 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 eSize */. 0,
1df06 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1df07 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 xLock */. 0,
1df08 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1df09 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 xUnlock */. 0,
1df0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1df0b 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
1df0c 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Lock */. 0,
1df0d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1df0e 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 FileControl */.
1df0f 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1df10 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a /* xSectorSiz
1df11 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 e */. 0
1df12 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76 /* xDev
1df13 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
1df14 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a cs */.};../* .**
1df15 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 Open a journal
1df16 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f file..*/.SQLITE_
1df17 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1df18 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 ite3MemJournalOp
1df19 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 en(sqlite3_file
1df1a 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 *pJfd){. MemJou
1df1b 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f rnal *p = (MemJo
1df1c 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 urnal *)pJfd;.
1df1d 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
1df1e 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20 TE_ALIGNMENT(p)
1df1f 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 );. memset(p, 0
1df20 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 , sqlite3MemJour
1df21 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d nalSize());. p-
1df22 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a >pMethod = &MemJ
1df23 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d ournalMethods;.}
1df24 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1df25 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d rue if the file-
1df26 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73 handle passed as
1df27 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 an argument is
1df28 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 .** an in-memory
1df29 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c journal .*/.SQL
1df2a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1df2b 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
1df2c 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 nal(sqlite3_file
1df2d 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72 *pJfd){. retur
1df2e 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 n pJfd->pMethods
1df2f 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 ==&MemJournalMet
1df30 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 hods;.}../* .**
1df31 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1df32 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 r of bytes requi
1df33 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d red to store a M
1df34 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75 emJournal that u
1df35 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 ses vfs.** pVfs
1df36 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e to create the un
1df37 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b derlying on-disk
1df38 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 files..*/.SQLIT
1df39 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1df3a 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
1df3b 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 ize(void){. ret
1df3c 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f urn sizeof(MemJo
1df3d 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a urnal);.}../****
1df3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1df3f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a f memjournal.c *
1df40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1df43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1df44 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20 2a file walker.c *
1df45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1df48 20 32 30 30 38 20 41 75 67 75 73 74 20 31 36 0a 2008 August 16.
1df49 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
1df4a 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
1df4b 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
1df4c 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1df4d 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
1df4e 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
1df4f 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
1df50 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
1df51 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
1df52 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
1df53 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
1df54 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
1df55 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
1df56 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
1df57 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
1df58 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
1df59 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
1df5a 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1df5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1df5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1df5f 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1df60 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 ains routines us
1df61 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74 ed for walking t
1df62 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 66 he parser tree f
1df63 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 61 or.** an SQL sta
1df64 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 tement..**.** $I
1df65 64 3a 20 77 61 6c 6b 65 72 2e 63 2c 76 20 31 2e d: walker.c,v 1.
1df66 34 20 32 30 30 39 2f 30 34 2f 30 38 20 31 33 3a 4 2009/04/08 13:
1df67 35 31 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a 51:52 drh Exp $.
1df68 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 */.../*.** Walk
1df69 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 an expression tr
1df6a 65 65 2e 20 20 49 6e 76 6f 6b 65 20 74 68 65 20 ee. Invoke the
1df6b 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f callback once fo
1df6c 72 20 65 61 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f r each node.** o
1df6d 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1df6e 2c 20 77 68 69 6c 65 20 64 65 63 65 6e 64 69 6e , while decendin
1df6f 67 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f g. (In other wo
1df70 72 64 73 2c 20 74 68 65 20 63 61 6c 6c 62 61 63 rds, the callbac
1df71 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 k.** is invoked
1df72 62 65 66 6f 72 65 20 76 69 73 69 74 69 6e 67 20 before visiting
1df73 63 68 69 6c 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a children.).**.**
1df74 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
1df75 65 20 66 72 6f 6d 20 74 68 65 20 63 61 6c 6c 62 e from the callb
1df76 61 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e ack should be on
1df77 65 20 6f 66 20 74 68 65 20 57 52 43 5f 2a 0a 2a e of the WRC_*.*
1df78 2a 20 63 6f 6e 73 74 61 6e 74 73 20 74 6f 20 73 * constants to s
1df79 70 65 63 69 66 79 20 68 6f 77 20 74 6f 20 70 72 pecify how to pr
1df7a 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20 77 oceed with the w
1df7b 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 alk..**.** WR
1df7c 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 20 20 C_Continue
1df7d 43 6f 6e 74 69 6e 75 65 20 64 65 73 63 65 6e 64 Continue descend
1df7e 69 6e 67 20 64 6f 77 6e 20 74 68 65 20 74 72 65 ing down the tre
1df7f 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f e..**.** WRC_
1df80 50 72 75 6e 65 20 20 20 20 20 20 20 20 20 44 6f Prune Do
1df81 20 6e 6f 74 20 64 65 73 63 65 6e 64 20 69 6e 74 not descend int
1df82 6f 20 63 68 69 6c 64 20 6e 6f 64 65 73 2e 20 20 o child nodes.
1df83 42 75 74 20 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 But allow.**
1df84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1df85 20 20 74 68 65 20 77 61 6c 6b 20 74 6f 20 63 6f the walk to co
1df86 6e 74 69 6e 75 65 20 77 69 74 68 20 73 69 62 6c ntinue with sibl
1df87 69 6e 67 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a ing nodes..**.**
1df88 20 20 20 20 57 52 43 5f 41 62 6f 72 74 20 20 20 WRC_Abort
1df89 20 20 20 20 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 Do no more
1df8a 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20 55 6e 77 callbacks. Unw
1df8b 69 6e 64 20 74 68 65 20 73 74 61 63 6b 20 61 6e ind the stack an
1df8c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 d.**
1df8d 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
1df8e 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 77 the top-level w
1df8f 61 6c 6b 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 alk call..**.**
1df90 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
1df91 20 66 72 6f 6d 20 74 68 69 73 20 72 6f 75 74 69 from this routi
1df92 6e 65 20 69 73 20 57 52 43 5f 41 62 6f 72 74 20 ne is WRC_Abort
1df93 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 74 to abandon the t
1df94 72 65 65 20 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 ree walk.** and
1df95 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 74 6f 20 WRC_Continue to
1df96 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c continue..*/.SQL
1df97 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1df98 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1df99 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
1df9a 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
1df9b 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 int rc;. if( p
1df9c 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e Expr==0 ) return
1df9d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 WRC_Continue;.
1df9e 20 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48 testcase( ExprH
1df9f 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
1dfa0 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 , EP_TokenOnly)
1dfa1 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 45 );. testcase( E
1dfa2 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
1dfa3 45 78 70 72 2c 20 45 50 5f 53 70 61 6e 54 6f 6b Expr, EP_SpanTok
1dfa4 65 6e 29 20 29 3b 0a 20 20 74 65 73 74 63 61 73 en) );. testcas
1dfa5 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 e( ExprHasProper
1dfa6 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 64 ty(pExpr, EP_Red
1dfa7 75 63 65 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 uced) );. rc =
1dfa8 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 43 61 pWalker->xExprCa
1dfa9 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 llback(pWalker,
1dfaa 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 72 63 pExpr);. if( rc
1dfab 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 0a 20 ==WRC_Continue.
1dfac 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
1dfad 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 !ExprHasAnyPrope
1dfae 72 74 79 28 70 45 78 70 72 2c 45 50 5f 54 6f 6b rty(pExpr,EP_Tok
1dfaf 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f enOnly|EP_SpanTo
1dfb0 6b 65 6e 29 20 29 7b 0a 20 20 20 20 69 66 28 20 ken) ){. if(
1dfb1 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1dfb2 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e pWalker, pExpr->
1dfb3 70 4c 65 66 74 29 20 29 20 72 65 74 75 72 6e 20 pLeft) ) return
1dfb4 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 WRC_Abort;. i
1dfb5 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 f( sqlite3WalkEx
1dfb6 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 pr(pWalker, pExp
1dfb7 72 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74 r->pRight) ) ret
1dfb8 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1dfb9 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
1dfba 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1dfbb 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
1dfbc 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1dfbd 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b WalkSelect(pWalk
1dfbe 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 er, pExpr->x.pSe
1dfbf 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 57 lect) ) return W
1dfc0 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 65 RC_Abort;. }e
1dfc1 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 73 lse{. if( s
1dfc2 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 qlite3WalkExprLi
1dfc3 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 st(pWalker, pExp
1dfc4 72 2d 3e 78 2e 70 4c 69 73 74 29 20 29 20 72 65 r->x.pList) ) re
1dfc5 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1dfc6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1dfc7 72 6e 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 rn rc & WRC_Abor
1dfc8 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c t;.}../*.** Call
1dfc9 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1dfca 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 () for every exp
1dfcb 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69 73 74 20 ression in list
1dfcc 70 20 6f 72 20 75 6e 74 69 6c 0a 2a 2a 20 61 6e p or until.** an
1dfcd 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 20 69 abort request i
1dfce 73 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 s seen..*/.SQLIT
1dfcf 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1dfd0 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1dfd1 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 t(Walker *pWalke
1dfd2 72 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b r, ExprList *p){
1dfd3 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 57 . int i, rc = W
1dfd4 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 73 RC_Continue;. s
1dfd5 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
1dfd6 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 tem *pItem;. if
1dfd7 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 ( p ){. for(i
1dfd8 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d =p->nExpr, pItem
1dfd9 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c =p->a; i>0; i--,
1dfda 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1dfdb 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1dfdc 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 49 Expr(pWalker, pI
1dfdd 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 72 65 tem->pExpr) ) re
1dfde 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1dfdf 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1dfe0 72 6e 20 72 63 20 26 20 57 52 43 5f 43 6f 6e 74 rn rc & WRC_Cont
1dfe1 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 inue;.}../*.** W
1dfe2 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 alk all expressi
1dfe3 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ons associated w
1dfe4 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 ith SELECT state
1dfe5 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e ment p. Do.** n
1dfe6 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 ot invoke the SE
1dfe7 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e LECT callback on
1dfe8 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 p, but do (of c
1dfe9 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a ourse) invoke.**
1dfea 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 any expr callba
1dfeb 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63 cks and SELECT c
1dfec 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f allbacks that co
1dfed 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69 me from subqueri
1dfee 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 es..** Return WR
1dfef 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 C_Abort or WRC_C
1dff0 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 ontinue..*/.SQLI
1dff1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1dff2 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1dff3 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 Expr(Walker *pWa
1dff4 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
1dff5 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 {. if( sqlite3W
1dff6 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1dff7 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 ker, p->pEList)
1dff8 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1dff9 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1dffa 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1dffb 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 r, p->pWhere) )
1dffc 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1dffd 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 ;. if( sqlite3W
1dffe 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1dfff 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 ker, p->pGroupBy
1e000 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1e001 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1e002 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1e003 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 ker, p->pHaving)
1e004 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1e005 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ort;. if( sqlit
1e006 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1e007 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 Walker, p->pOrde
1e008 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 rBy) ) return WR
1e009 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 C_Abort;. if( s
1e00a 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1e00b 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 Walker, p->pLimi
1e00c 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1e00d 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1e00e 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1e00f 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 lker, p->pOffset
1e010 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1e011 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 bort;. return W
1e012 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a RC_Continue;.}..
1e013 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 /*.** Walk the p
1e014 61 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63 arse trees assoc
1e015 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73 iated with all s
1e016 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 ubqueries in the
1e017 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 .** FROM clause
1e018 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d of SELECT statem
1e019 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 ent p. Do not i
1e01a 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74 nvoke the select
1e01b 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 .** callback on
1e01c 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 p, but do invoke
1e01d 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d it on each FROM
1e01e 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 clause subquery
1e01f 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 .** and on any s
1e020 75 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65 ubqueries furthe
1e021 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 r down in the tr
1e022 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 ee. Return .**
1e023 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 WRC_Abort or WRC
1e024 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 _Continue;.*/.SQ
1e025 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1e026 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1e027 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 ctFrom(Walker *p
1e028 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a Walker, Select *
1e029 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 p){. SrcList *p
1e02a 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 Src;. int i;.
1e02b 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
1e02c 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 tem *pItem;.. p
1e02d 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 Src = p->pSrc;.
1e02e 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20 20 if( pSrc ){.
1e02f 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 for(i=pSrc->nSr
1e030 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 c, pItem=pSrc->a
1e031 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 ; i>0; i--, pIte
1e032 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 m++){. if(
1e033 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1e034 74 28 70 57 61 6c 6b 65 72 2c 20 70 49 74 65 6d t(pWalker, pItem
1e035 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 ->pSelect) ){.
1e036 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1e037 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a _Abort;. }.
1e038 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1e039 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b rn WRC_Continue;
1e03a 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 .} ../*.** Call
1e03b 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1e03c 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 ) for every expr
1e03d 65 73 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74 ession in Select
1e03e 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 2a 2a statement p..**
1e03f 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57 Invoke sqlite3W
1e040 61 6c 6b 53 65 6c 65 63 74 28 29 20 66 6f 72 20 alkSelect() for
1e041 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 subqueries in th
1e042 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e e FROM clause an
1e043 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 d.** on the comp
1e044 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 68 61 69 ound select chai
1e045 6e 2c 20 70 2d 3e 70 50 72 69 6f 72 2e 0a 2a 2a n, p->pPrior..**
1e046 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 43 .** Return WRC_C
1e047 6f 6e 74 69 6e 75 65 20 75 6e 64 65 72 20 6e 6f ontinue under no
1e048 72 6d 61 6c 20 63 6f 6e 64 69 74 69 6f 6e 73 2e rmal conditions.
1e049 20 20 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f Return WRC_Abo
1e04a 72 74 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69 rt if.** there i
1e04b 73 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 65 s an abort reque
1e04c 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 st..**.** If the
1e04d 20 57 61 6c 6b 65 72 20 64 6f 65 73 20 6e 6f 74 Walker does not
1e04e 20 68 61 76 65 20 61 6e 20 78 53 65 6c 65 63 74 have an xSelect
1e04f 43 61 6c 6c 62 61 63 6b 28 29 20 74 68 65 6e 20 Callback() then
1e050 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1e051 69 73 20 61 20 6e 6f 2d 6f 70 20 72 65 74 75 72 is a no-op retur
1e052 6e 69 6e 67 20 57 52 43 5f 43 6f 6e 74 69 6e 75 ning WRC_Continu
1e053 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1e054 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1e055 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 WalkSelect(Walke
1e056 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
1e057 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 ct *p){. int rc
1e058 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 ;. if( p==0 ||
1e059 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74 pWalker->xSelect
1e05a 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65 Callback==0 ) re
1e05b 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1e05c 65 3b 0a 20 20 72 63 20 3d 20 57 52 43 5f 43 6f e;. rc = WRC_Co
1e05d 6e 74 69 6e 75 65 3b 0a 20 20 77 68 69 6c 65 28 ntinue;. while(
1e05e 20 70 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 p ){. rc =
1e05f 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74 pWalker->xSelect
1e060 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 Callback(pWalker
1e061 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , p);. if( rc
1e062 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 ) break;. if
1e063 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c ( sqlite3WalkSel
1e064 65 63 74 45 78 70 72 28 70 57 61 6c 6b 65 72 2c ectExpr(pWalker,
1e065 20 70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 p) ) return WRC
1e066 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 _Abort;. if(
1e067 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1e068 74 46 72 6f 6d 28 70 57 61 6c 6b 65 72 2c 20 70 tFrom(pWalker, p
1e069 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1e06a 62 6f 72 74 3b 0a 20 20 20 20 70 20 3d 20 70 2d bort;. p = p-
1e06b 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 >pPrior;. }. r
1e06c 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 5f 41 eturn rc & WRC_A
1e06d 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a bort;.}../******
1e06e 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1e06f 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a walker.c *******
1e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e071 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e072 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1e073 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1e074 69 6c 65 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a ile resolve.c **
1e075 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e077 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1e078 30 30 38 20 41 75 67 75 73 74 20 31 38 0a 2a 2a 008 August 18.**
1e079 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1e07a 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1e07b 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1e07c 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1e07d 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1e07e 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1e07f 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1e080 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1e081 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1e082 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1e083 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1e084 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1e085 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1e086 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1e087 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1e088 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1e089 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1e08a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1e08b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e08c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e08d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e08e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1e08f 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1e090 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 tains routines u
1e091 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 sed for walking
1e092 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 the parser tree
1e093 61 6e 64 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 61 and.** resolve a
1e094 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 ll identifiers b
1e095 79 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68 y associating th
1e096 65 6d 20 77 69 74 68 20 61 20 70 61 72 74 69 63 em with a partic
1e097 75 6c 61 72 0a 2a 2a 20 74 61 62 6c 65 20 61 6e ular.** table an
1e098 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 d column..**.**
1e099 24 49 64 3a 20 72 65 73 6f 6c 76 65 2e 63 2c 76 $Id: resolve.c,v
1e09a 20 31 2e 32 32 20 32 30 30 39 2f 30 35 2f 30 35 1.22 2009/05/05
1e09b 20 31 35 3a 34 36 3a 34 33 20 64 72 68 20 45 78 15:46:43 drh Ex
1e09c 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 p $.*/../*.** Tu
1e09d 72 6e 20 74 68 65 20 70 45 78 70 72 20 65 78 70 rn the pExpr exp
1e09e 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 ression into an
1e09f 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 69 43 alias for the iC
1e0a0 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 ol-th column of
1e0a1 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 the.** result se
1e0a2 74 20 69 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a t in pEList..**.
1e0a3 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 ** If the result
1e0a4 20 73 65 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 set column is a
1e0a5 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 simple column r
1e0a6 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 74 eference, then t
1e0a7 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d his routine.** m
1e0a8 61 6b 65 73 20 61 6e 20 65 78 61 63 74 20 63 6f akes an exact co
1e0a9 70 79 2e 20 20 42 75 74 20 66 6f 72 20 61 6e 79 py. But for any
1e0aa 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 65 other kind of e
1e0ab 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a xpression, this.
1e0ac 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 ** routine make
1e0ad 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 a copy of the re
1e0ae 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20 sult set column
1e0af 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 as the argument
1e0b0 74 6f 20 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20 to the.** TK_AS
1e0b1 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 54 operator. The T
1e0b2 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 63 61 K_AS operator ca
1e0b3 75 73 65 73 20 74 68 65 20 65 78 70 72 65 73 73 uses the express
1e0b4 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61 ion to be.** eva
1e0b5 6c 75 61 74 65 64 20 6a 75 73 74 20 6f 6e 63 65 luated just once
1e0b6 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64 and then reused
1e0b7 20 66 6f 72 20 65 61 63 68 20 61 6c 69 61 73 2e for each alias.
1e0b8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f .**.** The reaso
1e0b9 6e 20 66 6f 72 20 73 75 70 70 72 65 73 73 69 6e n for suppressin
1e0ba 67 20 74 68 65 20 54 4b 5f 41 53 20 74 65 72 6d g the TK_AS term
1e0bb 20 77 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 when the expres
1e0bc 73 69 6f 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 sion is a simple
1e0bd 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 .** column refer
1e0be 65 6e 63 65 20 69 73 20 73 6f 20 74 68 61 74 20 ence is so that
1e0bf 74 68 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 the column refer
1e0c0 65 6e 63 65 20 77 69 6c 6c 20 62 65 20 72 65 63 ence will be rec
1e0c1 6f 67 6e 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73 ognized as.** us
1e0c2 61 62 6c 65 20 62 79 20 69 6e 64 69 63 65 73 20 able by indices
1e0c3 77 69 74 68 69 6e 20 74 68 65 20 57 48 45 52 45 within the WHERE
1e0c4 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 clause processi
1e0c5 6e 67 20 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a ng logic. .**.**
1e0c6 20 48 61 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41 Hack: The TK_A
1e0c7 53 20 6f 70 65 72 61 74 6f 72 20 69 73 20 69 6e S operator is in
1e0c8 68 69 62 69 74 65 64 20 69 66 20 7a 54 79 70 65 hibited if zType
1e0c9 5b 30 5d 3d 3d 27 47 27 2e 20 20 54 68 69 73 20 [0]=='G'. This
1e0ca 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e means.** that in
1e0cb 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 a GROUP BY clau
1e0cc 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 se, the expressi
1e0cd 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65 64 20 on is evaluated
1e0ce 74 77 69 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a twice. Hence:.*
1e0cf 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 *.** SELECT
1e0d0 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c random()%5 AS x,
1e0d1 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 count(*) FROM t
1e0d2 61 62 20 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a ab GROUP BY x.**
1e0d3 0a 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e .** Is equivalen
1e0d4 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 t to:.**.**
1e0d5 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 SELECT random()%
1e0d6 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 5 AS x, count(*)
1e0d7 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 FROM tab GROUP
1e0d8 42 59 20 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a BY random()%5.**
1e0d9 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f .** The result o
1e0da 66 20 72 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20 f random()%5 in
1e0db 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 the GROUP BY cla
1e0dc 75 73 65 20 69 73 20 70 72 6f 62 61 62 6c 79 20 use is probably
1e0dd 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f different.** fro
1e0de 6d 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 m the result in
1e0df 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2e 20 the result-set.
1e0e0 20 57 65 20 6d 69 67 68 74 20 66 69 78 20 74 68 We might fix th
1e0e1 69 73 20 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a is someday. Or.
1e0e2 2a 2a 20 74 68 65 6e 20 61 67 61 69 6e 2c 20 77 ** then again, w
1e0e3 65 20 6d 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a e might not....*
1e0e4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
1e0e5 73 6f 6c 76 65 41 6c 69 61 73 28 0a 20 20 50 61 solveAlias(. Pa
1e0e6 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1e0e7 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
1e0e8 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
1e0e9 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 rList *pEList,
1e0ea 20 20 20 20 2f 2a 20 41 20 72 65 73 75 6c 74 20 /* A result
1e0eb 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f set */. int iCo
1e0ec 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
1e0ed 2f 2a 20 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 /* A column in t
1e0ee 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 he result set.
1e0ef 30 2e 2e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 0..pEList->nExpr
1e0f0 2d 31 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 -1 */. Expr *pE
1e0f1 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f xpr, /
1e0f2 2a 20 54 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 * Transform this
1e0f3 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 74 into an alias t
1e0f4 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 o the result set
1e0f5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1e0f6 20 2a 7a 54 79 70 65 20 20 20 20 20 20 2f 2a 20 *zType /*
1e0f7 22 47 52 4f 55 50 22 20 6f 72 20 22 4f 52 44 45 "GROUP" or "ORDE
1e0f8 52 22 20 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20 R" or "" */.){.
1e0f9 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 20 20 20 Expr *pOrig;
1e0fa 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 /* The i
1e0fb 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 Col-th column of
1e0fc 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1e0fd 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 75 70 3b */. Expr *pDup;
1e0fe 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1e0ff 6f 70 79 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a opy of pOrig */.
1e100 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
1e101 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1e102 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1e103 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ion */.. assert
1e104 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f ( iCol>=0 && iCo
1e105 6c 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 l<pEList->nExpr
1e106 29 3b 0a 20 20 70 4f 72 69 67 20 3d 20 70 45 4c );. pOrig = pEL
1e107 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 ist->a[iCol].pEx
1e108 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f pr;. assert( pO
1e109 72 69 67 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 rig!=0 );. asse
1e10a 72 74 28 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73 rt( pOrig->flags
1e10b 20 26 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 29 & EP_Resolved )
1e10c 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
1e10d 3e 64 62 3b 0a 20 20 70 44 75 70 20 3d 20 73 71 >db;. pDup = sq
1e10e 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1e10f 20 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 69 66 pOrig, 0);. if
1e110 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 75 ( pDup==0 ) retu
1e111 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 54 6f 6b rn;. sqlite3Tok
1e112 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 44 75 70 enCopy(db, &pDup
1e113 2d 3e 74 6f 6b 65 6e 2c 20 26 70 4f 72 69 67 2d ->token, &pOrig-
1e114 3e 74 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 >token);. if( p
1e115 44 75 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 Dup->op!=TK_COLU
1e116 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d MN && zType[0]!=
1e117 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 70 20 'G' ){. pDup
1e118 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 = sqlite3PExpr(p
1e119 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44 Parse, TK_AS, pD
1e11a 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 up, 0, 0);. i
1e11b 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 f( pDup==0 ) ret
1e11c 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c urn;. if( pEL
1e11d 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c ist->a[iCol].iAl
1e11e 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ias==0 ){.
1e11f 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e pEList->a[iCol].
1e120 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28 2b iAlias = (u16)(+
1e121 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 29 +pParse->nAlias)
1e122 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75 70 ;. }. pDup
1e123 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69 73 ->iTable = pELis
1e124 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 t->a[iCol].iAlia
1e125 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 78 s;. }. if( pEx
1e126 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 pr->flags & EP_E
1e127 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 xpCollate ){.
1e128 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d 20 70 pDup->pColl = p
1e129 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 Expr->pColl;.
1e12a 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 pDup->flags |=
1e12b 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 EP_ExpCollate;.
1e12c 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 }. sqlite3Expr
1e12d 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70 72 29 Clear(db, pExpr)
1e12e 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78 70 72 ;. memcpy(pExpr
1e12f 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66 28 2a , pDup, sizeof(*
1e130 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c 69 74 pExpr));. sqlit
1e131 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 75 e3DbFree(db, pDu
1e132 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 p);.}../*.** Giv
1e133 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 en the name of a
1e134 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 column of the f
1e135 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a orm X.Y.Z or Y.Z
1e136 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b or just Z, look
1e137 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61 6d 65 up.** that name
1e138 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 20 73 in the set of s
1e139 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 6e 20 ource tables in
1e13a 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d 61 6b pSrcList and mak
1e13b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a 2a 20 e the pExpr .**
1e13c 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 expression node
1e13d 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20 74 68 refer back to th
1e13e 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75 6d 6e at source column
1e13f 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
1e140 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20 changes.** are
1e141 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a 0a 2a made to pExpr:.*
1e142 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 *.** pExpr->i
1e143 44 62 20 20 20 20 20 20 20 20 20 20 20 53 65 74 Db Set
1e144 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 64 62 the index in db
1e145 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 ->aDb[] of the d
1e146 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20 20 20 atabase X.**
1e147 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e148 20 20 20 20 20 28 65 76 65 6e 20 69 66 20 58 20 (even if X
1e149 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a 2a 20 is implied)..**
1e14a 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1e14b 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 74 Set to t
1e14c 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 he cursor number
1e14d 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6f for the table o
1e14e 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 btained.**
1e14f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e150 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69 73 74 from pSrcList
1e151 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 ..** pExpr->p
1e152 54 61 62 20 20 20 20 20 20 20 20 20 20 50 6f 69 Tab Poi
1e153 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65 nts to the Table
1e154 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 58 2e structure of X.
1e155 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20 20 20 Y (even if.**
1e156 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e157 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72 20 59 X and/or Y
1e158 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29 0a 2a are implied.).*
1e159 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c * pExpr->iCol
1e15a 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20 74 6f umn Set to
1e15b 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 the column numb
1e15c 65 72 20 77 69 74 68 69 6e 20 74 68 65 20 74 61 er within the ta
1e15d 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 ble..** pExpr
1e15e 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 ->op
1e15f 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e Set to TK_COLUMN
1e160 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 ..** pExpr->p
1e161 4c 65 66 74 20 20 20 20 20 20 20 20 20 41 6e 79 Left Any
1e162 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73 expression this
1e163 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65 points to is de
1e164 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45 78 70 leted.** pExp
1e165 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20 20 20 r->pRight
1e166 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 Any expression
1e167 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 this points to i
1e168 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a s deleted..**.**
1e169 20 54 68 65 20 70 44 62 54 6f 6b 65 6e 20 69 73 The pDbToken is
1e16a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1e16b 20 64 61 74 61 62 61 73 65 20 28 74 68 65 20 22 database (the "
1e16c 58 22 29 2e 20 20 54 68 69 73 20 76 61 6c 75 65 X"). This value
1e16d 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20 may be.** NULL
1e16e 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e 61 6d meaning that nam
1e16f 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d e is of the form
1e170 20 59 2e 5a 20 6f 72 20 5a 2e 20 20 41 6e 79 20 Y.Z or Z. Any
1e171 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 62 61 available databa
1e172 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 se.** can be use
1e173 64 2e 20 20 54 68 65 20 70 54 61 62 6c 65 54 6f d. The pTableTo
1e174 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ken is the name
1e175 6f 66 20 74 68 65 20 74 61 62 6c 65 20 28 74 68 of the table (th
1e176 65 20 22 59 22 29 2e 20 20 54 68 69 73 0a 2a 2a e "Y"). This.**
1e177 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 4e 55 value can be NU
1e178 4c 4c 20 69 66 20 70 44 62 54 6f 6b 65 6e 20 69 LL if pDbToken i
1e179 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 20 49 66 s also NULL. If
1e17a 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20 pTableToken is
1e17b 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 NULL it.** means
1e17c 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f that the form o
1e17d 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20 f the name is Z
1e17e 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 and that columns
1e17f 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a from any table.
1e180 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a ** can be used..
1e181 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d **.** If the nam
1e182 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f e cannot be reso
1e183 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73 lved unambiguous
1e184 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 ly, leave an err
1e185 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e or message.** in
1e186 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 pParse and retu
1e187 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 rn non-zero. Re
1e188 74 75 72 6e 20 7a 65 72 6f 20 6f 6e 20 73 75 63 turn zero on suc
1e189 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cess..*/.static
1e18a 69 6e 74 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a int lookupName(.
1e18b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1e18c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
1e18d 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1e18e 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 62 54 6f 6b . Token *pDbTok
1e18f 65 6e 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 en, /* Name
1e190 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1e191 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 containing table
1e192 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 , or NULL */. T
1e193 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 54 6f 6b 65 oken *pTableToke
1e194 6e 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 n, /* Name of t
1e195 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 able containing
1e196 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20 column, or NULL
1e197 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c */. Token *pCol
1e198 75 6d 6e 54 6f 6b 65 6e 2c 20 2f 2a 20 4e 61 6d umnToken, /* Nam
1e199 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e e of the column.
1e19a 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 */. NameContex
1e19b 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a 20 54 68 t *pNC, /* Th
1e19c 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 75 e name context u
1e19d 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 sed to resolve t
1e19e 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70 he name */. Exp
1e19f 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 r *pExpr
1e1a0 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 45 /* Make this E
1e1a1 58 50 52 20 6e 6f 64 65 20 70 6f 69 6e 74 20 74 XPR node point t
1e1a2 6f 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 63 o the selected c
1e1a3 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b 0a 20 20 63 68 olumn */.){. ch
1e1a4 61 72 20 2a 7a 44 62 20 3d 20 30 3b 20 20 20 20 ar *zDb = 0;
1e1a5 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
1e1a6 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
1e1a7 20 22 58 22 20 69 6e 20 58 2e 59 2e 5a 20 2a 2f "X" in X.Y.Z */
1e1a8 0a 20 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 . char *zTab =
1e1a9 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 0; /* Name
1e1aa 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 of the table. T
1e1ab 68 65 20 22 59 22 20 69 6e 20 58 2e 59 2e 5a 20 he "Y" in X.Y.Z
1e1ac 6f 72 20 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 or Y.Z */. char
1e1ad 20 2a 7a 43 6f 6c 20 3d 20 30 3b 20 20 20 20 20 *zCol = 0;
1e1ae 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1e1af 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 22 5a 22 column. The "Z"
1e1b0 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 */. int i, j;
1e1b1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
1e1b2 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 op counters */.
1e1b3 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 int cnt = 0;
1e1b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e1b5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1e1b6 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 matching column
1e1b7 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 names */. int c
1e1b8 6e 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 ntTab = 0;
1e1b9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e1ba 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 Number of matchi
1e1bb 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a ng table names *
1e1bc 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 /. sqlite3 *db
1e1bd 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 = pParse->db;
1e1be 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
1e1bf 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1e1c0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 */. struct Src
1e1c1 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1e1c2 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 ; /* Use f
1e1c3 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 or looping over
1e1c4 70 53 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a pSrcList items *
1e1c5 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 /. struct SrcLi
1e1c6 73 74 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 st_item *pMatch
1e1c7 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 = 0; /* The mat
1e1c8 63 68 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 ching pSrcList i
1e1c9 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e tem */. NameCon
1e1ca 74 65 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 text *pTopNC = p
1e1cb 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 NC; /* Fi
1e1cc 72 73 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 rst namecontext
1e1cd 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 in the list */.
1e1ce 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
1e1cf 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1e1d0 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 /* Schema of
1e1d1 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a the expression *
1e1d2 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 43 /.. assert( pNC
1e1d3 20 29 3b 20 2f 2a 20 74 68 65 20 6e 61 6d 65 20 ); /* the name
1e1d4 63 6f 6e 74 65 78 74 20 63 61 6e 6e 6f 74 20 62 context cannot b
1e1d5 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 e NULL. */. ass
1e1d6 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 ert( pColumnToke
1e1d7 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 n && pColumnToke
1e1d8 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20 5a n->z ); /* The Z
1e1d9 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 in X.Y.Z cannot
1e1da 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 2f be NULL */.. /
1e1db 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a 65 * Dequote and ze
1e1dc 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68 65 ro-terminate the
1e1dd 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62 20 names */. zDb
1e1de 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1e1df 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54 6f mToken(db, pDbTo
1e1e0 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20 73 ken);. zTab = s
1e1e1 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1e1e2 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54 6f ken(db, pTableTo
1e1e3 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20 73 ken);. zCol = s
1e1e4 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1e1e5 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 54 ken(db, pColumnT
1e1e6 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62 2d oken);. if( db-
1e1e7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1e1e8 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 . goto lookup
1e1e9 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 name_end;. }..
1e1ea 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
1e1eb 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 he node to no-ma
1e1ec 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e tch */. pExpr->
1e1ed 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 iTable = -1;. p
1e1ee 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a Expr->pTab = 0;.
1e1ef 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74 . /* Start at t
1e1f0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f he inner-most co
1e1f1 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f ntext and move o
1e1f2 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d utward until a m
1e1f3 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f atch is found */
1e1f4 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 . while( pNC &&
1e1f5 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45 cnt==0 ){. E
1e1f6 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
1e1f7 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 . SrcList *pS
1e1f8 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 rcList = pNC->pS
1e1f9 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28 rcList;.. if(
1e1fa 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20 pSrcList ){.
1e1fb 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 for(i=0, pIte
1e1fc 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69 m=pSrcList->a; i
1e1fd 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b <pSrcList->nSrc;
1e1fe 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
1e1ff 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 Table *p
1e200 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 Tab;. int
1e201 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f iDb;. Co
1e202 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20 lumn *pCol;. .
1e203 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 49 pTab = pI
1e204 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 tem->pTab;.
1e205 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 assert( pTab!
1e206 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 6d =0 && pTab->zNam
1e207 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 e!=0 );.
1e208 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
1e209 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 emaToIndex(db, p
1e20a 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
1e20b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1e20c 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 Tab->nCol>0 );.
1e20d 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 20 if( zTab
1e20e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
1e20f 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 pItem->zAlias )
1e210 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 {. ch
1e211 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 ar *zTabName = p
1e212 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 Item->zAlias;.
1e213 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
1e214 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 lite3StrICmp(zTa
1e215 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 bName, zTab)!=0
1e216 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1e217 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1e218 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a char *z
1e219 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e TabName = pTab->
1e21a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 zName;.
1e21b 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 3d if( zTabName=
1e21c 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 =0 || sqlite3Str
1e21d 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a ICmp(zTabName, z
1e21e 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e Tab)!=0 ) contin
1e21f 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ue;.
1e220 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 if( zDb!=0 && sq
1e221 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d lite3StrICmp(db-
1e222 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c >aDb[iDb].zName,
1e223 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 zDb)!=0 ){.
1e224 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e contin
1e225 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ue;.
1e226 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1e227 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e228 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b if( 0==(cntTab++
1e229 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
1e22a 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1e22b 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 Item->iCursor;.
1e22c 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1e22d 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 pTab = pTab;.
1e22e 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d pSchema =
1e22f 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a pTab->pSchema;.
1e230 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 pMatch
1e231 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 = pItem;.
1e232 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 }. for(
1e233 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e j=0, pCol=pTab->
1e234 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 aCol; j<pTab->nC
1e235 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 ol; j++, pCol++)
1e236 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1e237 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 sqlite3StrICmp(p
1e238 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c Col->zName, zCol
1e239 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1e23a 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 IdList *pUsi
1e23b 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ng;.
1e23c 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 cnt++;.
1e23d 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1e23e 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f = pItem->iCurso
1e23f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 r;. p
1e240 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 Expr->pTab = pTa
1e241 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 b;. p
1e242 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 Match = pItem;.
1e243 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 pSche
1e244 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 ma = pTab->pSche
1e245 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ma;.
1e246 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68 /* Substitute th
1e247 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 e rowid (column
1e248 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 -1) for the INTE
1e249 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 GER PRIMARY KEY
1e24a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 */. p
1e24b 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 Expr->iColumn =
1e24c 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f j==pTab->iPKey ?
1e24d 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20 20 -1 : j;.
1e24e 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c if( i<pSrcL
1e24f 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 ist->nSrc-1 ){.
1e250 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
1e251 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 pItem[1].jointy
1e252 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 pe & JT_NATURAL
1e253 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1e254 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61 /* If this ma
1e255 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20 tch occurred in
1e256 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f the left table o
1e257 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e f a natural join
1e258 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1e259 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74 ** then skip t
1e25a 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 74 he right table t
1e25b 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 o avoid a duplic
1e25c 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 ate match */.
1e25d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 pIt
1e25e 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 em++;.
1e25f 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 i++;.
1e260 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }else i
1e261 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74 f( (pUsing = pIt
1e262 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30 em[1].pUsing)!=0
1e263 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1e264 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d /* If this m
1e265 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61 atch occurs on a
1e266 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 column that is
1e267 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 in the USING cla
1e268 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 use.
1e269 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e ** of a join
1e26a 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72 63 , skip the searc
1e26b 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 h of the right t
1e26c 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e able of the join
1e26d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1e26e 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 ** to avoid a d
1e26f 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74 uplicate match t
1e270 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 here. */.
1e271 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a int k;.
1e272 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e273 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e for(k=0; k<pUsin
1e274 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20 g->nId; k++){.
1e275 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e276 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1e277 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e mp(pUsing->a[k].
1e278 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 zName, zCol)==0
1e279 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1e27a 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a pItem++;.
1e27b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e27c 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 i++;.
1e27d 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 bre
1e27e 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ak;.
1e27f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e280 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e281 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e282 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e283 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1e284 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1e285 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
1e286 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1e287 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 MIT_TRIGGER.
1e288 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f /* If we have no
1e289 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76 t already resolv
1e28a 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65 ed the name, the
1e28b 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20 n maybe . **
1e28c 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72 it is a new.* or
1e28d 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61 old.* trigger a
1e28e 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 rgument referenc
1e28f 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 e. */. if(
1e290 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21 zDb==0 && zTab!
1e291 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20 =0 && cnt==0 &&
1e292 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 pParse->trigStac
1e293 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54 72 k!=0 ){. Tr
1e294 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72 69 iggerStack *pTri
1e295 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61 72 ggerStack = pPar
1e296 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a 20 se->trigStack;.
1e297 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 Table *pTab
1e298 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32 20 = 0;. u32
1e299 2a 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a *piColMask = 0;.
1e29a 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67 67 if( pTrigg
1e29b 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 erStack->newIdx
1e29c 21 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33 != -1 && sqlite3
1e29d 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a StrICmp("new", z
1e29e 54 61 62 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 Tab) == 0 ){.
1e29f 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1e2a0 6c 65 20 3d 20 70 54 72 69 67 67 65 72 53 74 61 le = pTriggerSta
1e2a1 63 6b 2d 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20 ck->newIdx;.
1e2a2 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69 assert( pTri
1e2a3 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 ggerStack->pTab
1e2a4 29 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 );. pTab
1e2a5 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d = pTriggerStack-
1e2a6 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 70 >pTab;. p
1e2a7 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 iColMask = &(pTr
1e2a8 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43 iggerStack->newC
1e2a9 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d olMask);. }
1e2aa 65 6c 73 65 20 69 66 28 20 70 54 72 69 67 67 65 else if( pTrigge
1e2ab 72 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21 rStack->oldIdx !
1e2ac 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53 = -1 && sqlite3S
1e2ad 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54 trICmp("old", zT
1e2ae 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ab)==0 ){.
1e2af 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1e2b0 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d = pTriggerStack-
1e2b1 3e 6f 6c 64 49 64 78 3b 0a 20 20 20 20 20 20 20 >oldIdx;.
1e2b2 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65 assert( pTrigge
1e2b3 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a rStack->pTab );.
1e2b4 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
1e2b5 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 TriggerStack->pT
1e2b6 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f ab;. piCo
1e2b7 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67 lMask = &(pTrigg
1e2b8 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d erStack->oldColM
1e2b9 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 ask);. }..
1e2ba 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b if( pTab ){
1e2bb 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 . int iC
1e2bc 6f 6c 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 ol;. Colu
1e2bd 6d 6e 20 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d mn *pCol = pTab-
1e2be 3e 61 43 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20 >aCol;..
1e2bf 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e pSchema = pTab->
1e2c0 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 pSchema;.
1e2c1 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 cntTab++;.
1e2c2 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 for(iCol=0; i
1e2c3 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c Col < pTab->nCol
1e2c4 3b 20 69 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b ; iCol++, pCol++
1e2c5 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ) {. if
1e2c6 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
1e2c7 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 (pCol->zName, zC
1e2c8 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ol)==0 ){.
1e2c9 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 cnt++;.
1e2ca 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1e2cb 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d iColumn = iCol==
1e2cc 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 pTab->iPKey ? -1
1e2cd 20 3a 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 : iCol;.
1e2ce 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 pExpr->pTab
1e2cf 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 = pTab;.
1e2d0 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 if( iCol>=0
1e2d1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1e2d2 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c testcase( iCol
1e2d3 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20 ==31 );.
1e2d4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1e2d5 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 iCol==32 );.
1e2d6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43 if( iC
1e2d7 6f 6c 3e 3d 33 32 20 29 7b 0a 20 20 20 20 20 20 ol>=32 ){.
1e2d8 20 20 20 20 20 20 20 20 20 20 2a 70 69 43 6f 6c *piCol
1e2d9 4d 61 73 6b 20 3d 20 30 78 66 66 66 66 66 66 66 Mask = 0xfffffff
1e2da 66 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 f;.
1e2db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1e2dc 20 20 20 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61 *piColMa
1e2dd 73 6b 20 7c 3d 20 28 28 75 33 32 29 31 29 3c 3c sk |= ((u32)1)<<
1e2de 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 iCol;.
1e2df 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1e2e0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1e2e1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 break;.
1e2e2 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1e2e3 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
1e2e4 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
1e2e5 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
1e2e6 52 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 R) */.. /*.
1e2e7 20 20 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65 ** Perhaps the
1e2e8 20 6e 61 6d 65 20 69 73 20 61 20 72 65 66 65 72 name is a refer
1e2e9 65 6e 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49 ence to the ROWI
1e2ea 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 D. */. if(
1e2eb 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61 cnt==0 && cntTa
1e2ec 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49 b==1 && sqlite3I
1e2ed 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a sRowid(zCol) ){.
1e2ee 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 cnt = 1;.
1e2ef 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
1e2f0 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 umn = -1;.
1e2f1 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 pExpr->affinity
1e2f2 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 = SQLITE_AFF_INT
1e2f3 45 47 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 EGER;. }..
1e2f4 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 /*. ** If th
1e2f5 65 20 69 6e 70 75 74 20 69 73 20 6f 66 20 74 68 e input is of th
1e2f6 65 20 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e e form Z (not Y.
1e2f7 5a 20 6f 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e Z or X.Y.Z) then
1e2f8 20 74 68 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20 the name Z.
1e2f9 2a 2a 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 ** might refer t
1e2fa 6f 20 61 6e 20 72 65 73 75 6c 74 2d 73 65 74 20 o an result-set
1e2fb 61 6c 69 61 73 2e 20 20 54 68 69 73 20 68 61 70 alias. This hap
1e2fc 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c pens, for exampl
1e2fd 65 2c 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77 e, when. ** w
1e2fe 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 e are resolving
1e2ff 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45 names in the WHE
1e300 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 RE clause of the
1e301 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 following comma
1e302 6e 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a nd:. **. *
1e303 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 * SELECT a+b
1e304 20 41 53 20 78 20 46 52 4f 4d 20 74 61 62 6c 65 AS x FROM table
1e305 20 57 48 45 52 45 20 78 3c 31 30 3b 0a 20 20 20 WHERE x<10;.
1e306 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61 **. ** In ca
1e307 73 65 73 20 6c 69 6b 65 20 74 68 69 73 2c 20 72 ses like this, r
1e308 65 70 6c 61 63 65 20 70 45 78 70 72 20 77 69 74 eplace pExpr wit
1e309 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 h a copy of the
1e30a 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 0a expression that.
1e30b 20 20 20 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65 ** forms the
1e30c 20 72 65 73 75 6c 74 20 73 65 74 20 65 6e 74 72 result set entr
1e30d 79 20 28 22 61 2b 62 22 20 69 6e 20 74 68 65 20 y ("a+b" in the
1e30e 65 78 61 6d 70 6c 65 29 20 61 6e 64 20 72 65 74 example) and ret
1e30f 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e urn immediately.
1e310 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 . ** Note tha
1e311 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e t the expression
1e312 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1e313 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 et should have a
1e314 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20 lready been.
1e315 2a 2a 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74 ** resolved by t
1e316 68 65 20 74 69 6d 65 20 74 68 65 20 57 48 45 52 he time the WHER
1e317 45 20 63 6c 61 75 73 65 20 69 73 20 72 65 73 6f E clause is reso
1e318 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 lved.. */.
1e319 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 if( cnt==0 && (
1e31a 70 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 pEList = pNC->pE
1e31b 4c 69 73 74 29 21 3d 30 20 26 26 20 7a 54 61 62 List)!=0 && zTab
1e31c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 ==0 ){. for
1e31d 28 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e (j=0; j<pEList->
1e31e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 nExpr; j++){.
1e31f 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20 3d char *zAs =
1e320 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e pEList->a[j].zN
1e321 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ame;. if(
1e322 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 zAs!=0 && sqlit
1e323 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a e3StrICmp(zAs, z
1e324 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Col)==0 ){.
1e325 20 20 20 20 20 45 78 70 72 20 2a 70 4f 72 69 67 Expr *pOrig
1e326 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 ;. asse
1e327 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 rt( pExpr->pLeft
1e328 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 70 52 ==0 && pExpr->pR
1e329 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ight==0 );.
1e32a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
1e32b 70 72 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 29 pr->x.pList==0 )
1e32c 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 ;. asse
1e32d 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 rt( pExpr->x.pSe
1e32e 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 lect==0 );.
1e32f 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70 45 4c pOrig = pEL
1e330 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 3b ist->a[j].pExpr;
1e331 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 . if( !
1e332 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 26 26 pNC->allowAgg &&
1e333 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1e334 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67 29 20 (pOrig, EP_Agg)
1e335 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
1e336 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1e337 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f Parse, "misuse o
1e338 66 20 61 6c 69 61 73 65 64 20 61 67 67 72 65 67 f aliased aggreg
1e339 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b 0a 20 ate %s", zAs);.
1e33a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1e33b 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
1e33c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 l);.
1e33d 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 return 2;.
1e33e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1e33f 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 resolveAlias(pPa
1e340 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c 20 rse, pEList, j,
1e341 70 45 78 70 72 2c 20 22 22 29 3b 0a 20 20 20 20 pExpr, "");.
1e342 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 cnt = 1;.
1e343 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 pMatch
1e344 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 = 0;. a
1e345 73 73 65 72 74 28 20 7a 54 61 62 3d 3d 30 20 26 ssert( zTab==0 &
1e346 26 20 7a 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 & zDb==0 );.
1e347 20 20 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 goto looku
1e348 70 6e 61 6d 65 5f 65 6e 64 5f 32 3b 0a 20 20 20 pname_end_2;.
1e349 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a }. } .
1e34a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 }.. /* Ad
1e34b 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 vance to the nex
1e34c 74 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 t name context.
1e34d 20 54 68 65 20 6c 6f 6f 70 20 77 69 6c 6c 20 65 The loop will e
1e34e 78 69 74 20 77 68 65 6e 20 65 69 74 68 65 72 0a xit when either.
1e34f 20 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20 61 ** we have a
1e350 20 6d 61 74 63 68 20 28 63 6e 74 3e 30 29 20 6f match (cnt>0) o
1e351 72 20 77 68 65 6e 20 77 65 20 72 75 6e 20 6f 75 r when we run ou
1e352 74 20 6f 66 20 6e 61 6d 65 20 63 6f 6e 74 65 78 t of name contex
1e353 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ts.. */. i
1e354 66 28 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 f( cnt==0 ){.
1e355 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e pNC = pNC->pN
1e356 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ext;. }. }..
1e357 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 58 20 61 /*. ** If X a
1e358 6e 64 20 59 20 61 72 65 20 4e 55 4c 4c 20 28 69 nd Y are NULL (i
1e359 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 n other words if
1e35a 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e only the column
1e35b 20 6e 61 6d 65 20 5a 20 69 73 0a 20 20 2a 2a 20 name Z is. **
1e35c 73 75 70 70 6c 69 65 64 29 20 61 6e 64 20 74 68 supplied) and th
1e35d 65 20 76 61 6c 75 65 20 6f 66 20 5a 20 69 73 20 e value of Z is
1e35e 65 6e 63 6c 6f 73 65 64 20 69 6e 20 64 6f 75 62 enclosed in doub
1e35f 6c 65 2d 71 75 6f 74 65 73 2c 20 74 68 65 6e 0a le-quotes, then.
1e360 20 20 2a 2a 20 5a 20 69 73 20 61 20 73 74 72 69 ** Z is a stri
1e361 6e 67 20 6c 69 74 65 72 61 6c 20 69 66 20 69 74 ng literal if it
1e362 20 64 6f 65 73 6e 27 74 20 6d 61 74 63 68 20 61 doesn't match a
1e363 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e ny column names.
1e364 20 20 49 6e 20 74 68 61 74 0a 20 20 2a 2a 20 63 In that. ** c
1e365 61 73 65 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 ase, we need to
1e366 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 return right awa
1e367 79 20 61 6e 64 20 6e 6f 74 20 6d 61 6b 65 20 61 y and not make a
1e368 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 20 ny changes to.
1e369 2a 2a 20 70 45 78 70 72 2e 0a 20 20 2a 2a 0a 20 ** pExpr.. **.
1e36a 20 2a 2a 20 42 65 63 61 75 73 65 20 6e 6f 20 72 ** Because no r
1e36b 65 66 65 72 65 6e 63 65 20 77 61 73 20 6d 61 64 eference was mad
1e36c 65 20 74 6f 20 6f 75 74 65 72 20 63 6f 6e 74 65 e to outer conte
1e36d 78 74 73 2c 20 74 68 65 20 70 4e 43 2d 3e 6e 52 xts, the pNC->nR
1e36e 65 66 0a 20 20 2a 2a 20 66 69 65 6c 64 73 20 61 ef. ** fields a
1e36f 72 65 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 69 re not changed i
1e370 6e 20 61 6e 79 20 63 6f 6e 74 65 78 74 2e 0a 20 n any context..
1e371 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 3d 3d 30 */. if( cnt==0
1e372 20 26 26 20 7a 54 61 62 3d 3d 30 20 26 26 20 45 && zTab==0 && E
1e373 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
1e374 45 78 70 72 2c 45 50 5f 44 62 6c 51 75 6f 74 65 Expr,EP_DblQuote
1e375 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 d) ){. sqlite
1e376 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 3DbFree(db, zCol
1e377 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 );. pExpr->op
1e378 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 = TK_STRING;.
1e379 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 pExpr->pTab =
1e37a 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0;. return 0;
1e37b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 . }.. /*. **
1e37c 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 cnt==0 means the
1e37d 72 65 20 77 61 73 20 6e 6f 74 20 6d 61 74 63 68 re was not match
1e37e 2e 20 20 63 6e 74 3e 31 20 6d 65 61 6e 73 20 74 . cnt>1 means t
1e37f 68 65 72 65 20 77 65 72 65 20 74 77 6f 20 6f 72 here were two or
1e380 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d 61 74 63 68 . ** more match
1e381 65 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c es. Either way,
1e382 20 77 65 20 68 61 76 65 20 61 6e 20 65 72 72 6f we have an erro
1e383 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e r.. */. if( cn
1e384 74 21 3d 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73 t!=1 ){. cons
1e385 74 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 t char *zErr;.
1e386 20 20 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20 zErr = cnt==0
1e387 3f 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d ? "no such colum
1e388 6e 22 20 3a 20 22 61 6d 62 69 67 75 6f 75 73 20 n" : "ambiguous
1e389 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20 column name";.
1e38a 20 20 69 66 28 20 7a 44 62 20 29 7b 0a 20 20 20 if( zDb ){.
1e38b 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1e38c 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 sg(pParse, "%s:
1e38d 25 73 2e 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c %s.%s.%s", zErr,
1e38e 20 7a 44 62 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c zDb, zTab, zCol
1e38f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
1e390 20 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 73 zTab ){. s
1e391 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1e392 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 Parse, "%s: %s.%
1e393 73 22 2c 20 7a 45 72 72 2c 20 7a 54 61 62 2c 20 s", zErr, zTab,
1e394 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 zCol);. }else
1e395 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1e396 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1e397 22 25 73 3a 20 25 73 22 2c 20 7a 45 72 72 2c 20 "%s: %s", zErr,
1e398 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 zCol);. }.
1e399 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b pTopNC->nErr++;
1e39a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 . }.. /* If a
1e39b 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61 column from a ta
1e39c 62 6c 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20 ble in pSrcList
1e39d 69 73 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74 is referenced, t
1e39e 68 65 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 hen record. **
1e39f 74 68 69 73 20 66 61 63 74 20 69 6e 20 74 68 65 this fact in the
1e3a0 20 70 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f pSrcList.a[].co
1e3a1 6c 55 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20 lUsed bitmask.
1e3a2 43 6f 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a Column 0 causes.
1e3a3 20 20 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65 ** bit 0 to be
1e3a4 20 73 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20 set. Column 1
1e3a5 73 65 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64 sets bit 1. And
1e3a6 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 so forth. If t
1e3a7 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e he. ** column n
1e3a8 75 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72 umber is greater
1e3a9 20 74 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72 than the number
1e3aa 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 of bits in the
1e3ab 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65 bitmask. ** the
1e3ac 6e 20 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f n set the high-o
1e3ad 72 64 65 72 20 62 69 74 20 6f 66 20 74 68 65 20 rder bit of the
1e3ae 62 69 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20 bitmask.. */.
1e3af 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 if( pExpr->iColu
1e3b0 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21 mn>=0 && pMatch!
1e3b1 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 =0 ){. int n
1e3b2 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e = pExpr->iColumn
1e3b3 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
1e3b4 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 n==BMS-1 );.
1e3b5 69 66 28 20 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20 if( n>=BMS ){.
1e3b6 20 20 20 20 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20 n = BMS-1;.
1e3b7 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1e3b8 20 70 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f 72 pMatch->iCursor
1e3b9 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 ==pExpr->iTable
1e3ba 29 3b 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e 63 );. pMatch->c
1e3bb 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d olUsed |= ((Bitm
1e3bc 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a ask)1)<<n;. }..
1e3bd 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a lookupname_end:.
1e3be 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e /* Clean up an
1e3bf 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 d return. */.
1e3c0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1e3c1 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 , zDb);. sqlite
1e3c2 33 44 62 46 72 65 65 28 64 62 2c 20 7a 54 61 62 3DbFree(db, zTab
1e3c3 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
1e3c4 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 Delete(db, pExpr
1e3c5 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 ->pLeft);. pExp
1e3c6 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 r->pLeft = 0;.
1e3c7 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1e3c8 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 e(db, pExpr->pRi
1e3c9 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 ght);. pExpr->p
1e3ca 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 Right = 0;. pEx
1e3cb 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 pr->op = TK_COLU
1e3cc 4d 4e 3b 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 MN;.lookupname_e
1e3cd 6e 64 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 44 nd_2:. sqlite3D
1e3ce 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b bFree(db, zCol);
1e3cf 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b . if( cnt==1 ){
1e3d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 43 . assert( pNC
1e3d1 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 !=0 );. sqlit
1e3d2 65 33 41 75 74 68 52 65 61 64 28 70 50 61 72 73 e3AuthRead(pPars
1e3d3 65 2c 20 70 45 78 70 72 2c 20 70 53 63 68 65 6d e, pExpr, pSchem
1e3d4 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 a, pNC->pSrcList
1e3d5 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d );. /* Increm
1e3d6 65 6e 74 20 74 68 65 20 6e 52 65 66 20 76 61 6c ent the nRef val
1e3d7 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 ue on all name c
1e3d8 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 ontexts from Top
1e3d9 4e 43 20 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 NC up to. **
1e3da 74 68 65 20 70 6f 69 6e 74 20 77 68 65 72 65 20 the point where
1e3db 74 68 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 the name matched
1e3dc 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 . */. for(;;)
1e3dd 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1e3de 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 pTopNC!=0 );.
1e3df 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b pTopNC->nRef+
1e3e0 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f +;. if( pTo
1e3e1 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b pNC==pNC ) break
1e3e2 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 20 3d ;. pTopNC =
1e3e3 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a pTopNC->pNext;.
1e3e4 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
1e3e5 20 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 0;. } else {.
1e3e6 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
1e3e7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
1e3e8 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 62 61 outine is callba
1e3e9 63 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 57 61 ck for sqlite3Wa
1e3ea 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 lkExpr()..**.**
1e3eb 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 Resolve symbolic
1e3ec 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 names into TK_C
1e3ed 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 OLUMN operators
1e3ee 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 0a for the current.
1e3ef 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 ** node in the e
1e3f0 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
1e3f1 20 52 65 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e Return 0 to con
1e3f2 74 69 6e 75 65 20 74 68 65 20 73 65 61 72 63 68 tinue the search
1e3f3 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 down.** the tre
1e3f4 65 20 6f 72 20 32 20 74 6f 20 61 62 6f 72 74 20 e or 2 to abort
1e3f5 74 68 65 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a the tree walk..*
1e3f6 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1e3f7 65 20 61 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f e also does erro
1e3f8 72 20 63 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e r checking and n
1e3f9 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 ame resolution f
1e3fa 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e or.** function n
1e3fb 61 6d 65 73 2e 20 20 54 68 65 20 6f 70 65 72 61 ames. The opera
1e3fc 74 6f 72 20 66 6f 72 20 61 67 67 72 65 67 61 74 tor for aggregat
1e3fd 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 e functions is c
1e3fe 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f hanged.** to TK_
1e3ff 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f AGG_FUNCTION..*/
1e400 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
1e401 6c 76 65 45 78 70 72 53 74 65 70 28 57 61 6c 6b lveExprStep(Walk
1e402 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1e403 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d r *pExpr){. Nam
1e404 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20 eContext *pNC;.
1e405 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a Parse *pParse;.
1e406 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 . pNC = pWalker
1e407 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72 ->u.pNC;. asser
1e408 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70 t( pNC!=0 );. p
1e409 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 Parse = pNC->pPa
1e40a 72 73 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 rse;. assert( p
1e40b 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e Parse==pWalker->
1e40c 70 50 61 72 73 65 20 29 3b 0a 0a 20 20 69 66 28 pParse );.. if(
1e40d 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 ExprHasAnyPrope
1e40e 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 rty(pExpr, EP_Re
1e40f 73 6f 6c 76 65 64 29 20 29 20 72 65 74 75 72 6e solved) ) return
1e410 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78 WRC_Prune;. Ex
1e411 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 prSetProperty(pE
1e412 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 xpr, EP_Resolved
1e413 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 );.#ifndef NDEBU
1e414 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 G. if( pNC->pSr
1e415 63 4c 69 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 cList && pNC->pS
1e416 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 rcList->nAlloc>0
1e417 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 ){. SrcList
1e418 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d *pSrcList = pNC-
1e419 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 >pSrcList;. i
1e41a 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
1e41b 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 0; i<pNC->pSrcLi
1e41c 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a st->nSrc; i++){.
1e41d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 assert( pS
1e41e 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 rcList->a[i].iCu
1e41f 72 73 6f 72 3e 3d 30 20 26 26 20 70 53 72 63 4c rsor>=0 && pSrcL
1e420 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f ist->a[i].iCurso
1e421 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b r<pParse->nTab);
1e422 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
1e423 66 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 f. switch( pExp
1e424 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65 r->op ){..#if de
1e425 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
1e426 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 BLE_UPDATE_DELET
1e427 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 E_LIMIT) && !def
1e428 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1e429 5f 53 55 42 51 55 45 52 59 29 0a 20 20 20 20 2f _SUBQUERY). /
1e42a 2a 20 54 68 65 20 73 70 65 63 69 61 6c 20 6f 70 * The special op
1e42b 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 erator TK_ROW me
1e42c 61 6e 73 20 75 73 65 20 74 68 65 20 72 6f 77 69 ans use the rowi
1e42d 64 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 0a d for the first.
1e42e 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e ** column in
1e42f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
1e430 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 . This is used
1e431 62 79 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 by the LIMIT and
1e432 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a 2a ORDER BY. **
1e433 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 clause processi
1e434 6e 67 20 6f 6e 20 55 50 44 41 54 45 20 61 6e 64 ng on UPDATE and
1e435 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e DELETE statemen
1e436 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 ts.. */. c
1e437 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 ase TK_ROW: {.
1e438 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 SrcList *pSr
1e439 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 cList = pNC->pSr
1e43a 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 73 74 72 cList;. str
1e43b 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1e43c 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 61 *pItem;. a
1e43d 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 74 20 ssert( pSrcList
1e43e 26 26 20 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 && pSrcList->nSr
1e43f 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 49 c==1 );. pI
1e440 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e tem = pSrcList->
1e441 61 3b 20 0a 20 20 20 20 20 20 70 45 78 70 72 2d a; . pExpr-
1e442 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b >op = TK_COLUMN;
1e443 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 . pExpr->pT
1e444 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 ab = pItem->pTab
1e445 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 ;. pExpr->i
1e446 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 Table = pItem->i
1e447 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 45 Cursor;. pE
1e448 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d xpr->iColumn = -
1e449 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 1;. pExpr->
1e44a 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 affinity = SQLIT
1e44b 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 E_AFF_INTEGER;.
1e44c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1e44d 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 }.#endif /* defi
1e44e 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
1e44f 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f E_UPDATE_DELETE_
1e450 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e LIMIT) && !defin
1e451 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
1e452 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20 UBQUERY) */..
1e453 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 /* A lone ident
1e454 69 66 69 65 72 20 69 73 20 74 68 65 20 6e 61 6d ifier is the nam
1e455 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 e of a column..
1e456 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 */. case T
1e457 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20 20 6c 6f K_ID: {. lo
1e458 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c okupName(pParse,
1e459 20 30 2c 20 30 2c 20 26 70 45 78 70 72 2d 3e 74 0, 0, &pExpr->t
1e45a 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 oken, pNC, pExpr
1e45b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1e45c 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d WRC_Prune;. }
1e45d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 . . /* A tab
1e45e 6c 65 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 le name and colu
1e45f 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e mn name: ID.
1e460 49 44 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 ID. ** Or a d
1e461 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 atabase, table a
1e462 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 nd column: ID.I
1e463 44 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 D.ID. */.
1e464 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 case TK_DOT: {.
1e465 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c Token *pCol
1e466 75 6d 6e 3b 0a 20 20 20 20 20 20 54 6f 6b 65 6e umn;. Token
1e467 20 2a 70 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 *pTable;.
1e468 54 6f 6b 65 6e 20 2a 70 44 62 3b 0a 20 20 20 20 Token *pDb;.
1e469 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 0a Expr *pRight;.
1e46a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70 53 . /* if( pS
1e46b 72 63 4c 69 73 74 3d 3d 30 20 29 20 62 72 65 61 rcList==0 ) brea
1e46c 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70 52 69 67 k; */. pRig
1e46d 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 ht = pExpr->pRig
1e46e 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 ht;. if( pR
1e46f 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 ight->op==TK_ID
1e470 29 7b 0a 20 20 20 20 20 20 20 20 70 44 62 20 3d ){. pDb =
1e471 20 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 0;. pTab
1e472 6c 65 20 3d 20 26 70 45 78 70 72 2d 3e 70 4c 65 le = &pExpr->pLe
1e473 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 ft->token;.
1e474 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 pColumn = &pR
1e475 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 ight->token;.
1e476 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1e477 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74 assert( pRight
1e478 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a ->op==TK_DOT );.
1e479 20 20 20 20 20 20 20 20 70 44 62 20 3d 20 26 70 pDb = &p
1e47a 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b Expr->pLeft->tok
1e47b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 en;. pTab
1e47c 6c 65 20 3d 20 26 70 52 69 67 68 74 2d 3e 70 4c le = &pRight->pL
1e47d 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 eft->token;.
1e47e 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20 26 70 pColumn = &p
1e47f 52 69 67 68 74 2d 3e 70 52 69 67 68 74 2d 3e 74 Right->pRight->t
1e480 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 oken;. }.
1e481 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 lookupName(p
1e482 50 61 72 73 65 2c 20 70 44 62 2c 20 70 54 61 62 Parse, pDb, pTab
1e483 6c 65 2c 20 70 43 6f 6c 75 6d 6e 2c 20 70 4e 43 le, pColumn, pNC
1e484 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 , pExpr);.
1e485 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 return WRC_Prune
1e486 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1e487 52 65 73 6f 6c 76 65 20 66 75 6e 63 74 69 6f 6e Resolve function
1e488 20 6e 61 6d 65 73 0a 20 20 20 20 2a 2f 0a 20 20 names. */.
1e489 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f case TK_CONST_
1e48a 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 FUNC:. case T
1e48b 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 K_FUNCTION: {.
1e48c 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c ExprList *pL
1e48d 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 ist = pExpr->x.p
1e48e 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 List; /* The
1e48f 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a 2f argument list */
1e490 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 . int n = p
1e491 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 List ? pList->nE
1e492 78 70 72 20 3a 20 30 3b 20 20 20 20 2f 2a 20 4e xpr : 0; /* N
1e493 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
1e494 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ts */. int
1e495 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30 no_such_func = 0
1e496 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
1e497 69 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 if no such funct
1e498 69 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 ion exists */.
1e499 20 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 int wrong_nu
1e49a 6d 5f 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20 m_args = 0;
1e49b 2f 2a 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67 /* True if wrong
1e49c 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d number of argum
1e49d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e ents */. in
1e49e 74 20 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20 t is_agg = 0;
1e49f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1e4a0 65 20 69 66 20 69 73 20 61 6e 20 61 67 67 72 65 e if is an aggre
1e4a1 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f gate function */
1e4a2 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b . int auth;
1e4a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e4a4 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 /* Authorizat
1e4a5 69 6f 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66 ion to use the f
1e4a6 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 unction */.
1e4a7 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 int nId;
1e4a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e4a9 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 Number of charac
1e4aa 74 65 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e ters in function
1e4ab 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 name */. c
1e4ac 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 onst char *zId;
1e4ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1e4ae 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e e function name.
1e4af 20 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 */. FuncDe
1e4b0 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 f *pDef;
1e4b1 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
1e4b2 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 tion about the f
1e4b3 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 unction */.
1e4b4 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 u8 enc = ENC(pP
1e4b5 61 72 73 65 2d 3e 64 62 29 3b 20 20 20 2f 2a 20 arse->db); /*
1e4b6 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 The database enc
1e4b7 6f 64 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20 oding */..
1e4b8 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
1e4b9 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1e4ba 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
1e4bb 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63 68 . zId = (ch
1e4bc 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e ar*)pExpr->token
1e4bd 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 .z;. nId =
1e4be 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a pExpr->token.n;.
1e4bf 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c pDef = sql
1e4c0 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e ite3FindFunction
1e4c1 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 (pParse->db, zId
1e4c2 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 30 , nId, n, enc, 0
1e4c3 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 );. if( pDe
1e4c4 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 f==0 ){.
1e4c5 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 pDef = sqlite3Fi
1e4c6 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 ndFunction(pPars
1e4c7 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c e->db, zId, nId,
1e4c8 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 -1, enc, 0);.
1e4c9 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d if( pDef==
1e4ca 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 0 ){. n
1e4cb 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 3b o_such_func = 1;
1e4cc 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1e4cd 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67 5f wrong_
1e4ce 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 20 num_args = 1;.
1e4cf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
1e4d0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73 5f lse{. is_
1e4d1 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 6e agg = pDef->xFun
1e4d2 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 c==0;. }.#i
1e4d3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1e4d4 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a T_AUTHORIZATION.
1e4d5 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20 29 if( pDef )
1e4d6 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20 3d {. auth =
1e4d7 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
1e4d8 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 k(pParse, SQLITE
1e4d9 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 44 _FUNCTION, 0, pD
1e4da 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 ef->zName, 0);.
1e4db 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68 21 if( auth!
1e4dc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1e4dd 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68 if( auth
1e4de 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b ==SQLITE_DENY ){
1e4df 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1e4e0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1e4e1 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 rse, "not author
1e4e2 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e 63 ized to use func
1e4e3 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 20 tion: %s",.
1e4e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e4e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1e4e6 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 Def->zName);.
1e4e7 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 pNC->nE
1e4e8 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 rr++;.
1e4e9 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 }. pExp
1e4ea 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b r->op = TK_NULL;
1e4eb 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
1e4ec 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 n WRC_Prune;.
1e4ed 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 }. }.#
1e4ee 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 endif. if(
1e4ef 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 2d 3e is_agg && !pNC->
1e4f0 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20 20 allowAgg ){.
1e4f1 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1e4f2 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 Msg(pParse, "mis
1e4f3 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 use of aggregate
1e4f4 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 function %.*s()
1e4f5 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20 20 ", nId,zId);.
1e4f6 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b pNC->nErr++
1e4f7 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 ;. is_agg
1e4f8 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 = 0;. }els
1e4f9 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66 75 e if( no_such_fu
1e4fa 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 nc ){. sq
1e4fb 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1e4fc 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 66 arse, "no such f
1e4fd 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c 20 unction: %.*s",
1e4fe 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 nId, zId);.
1e4ff 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a pNC->nErr++;.
1e500 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1e501 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 29 wrong_num_args )
1e502 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1e503 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1e504 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f ,"wrong number o
1e505 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 66 f arguments to f
1e506 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c unction %.*s()",
1e507 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 49 . nI
1e508 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 d, zId);.
1e509 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 pNC->nErr++;.
1e50a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1e50b 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 20 20 is_agg ){.
1e50c 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
1e50d 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 _AGG_FUNCTION;.
1e50e 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 pNC->hasA
1e50f 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a gg = 1;. }.
1e510 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 if( is_agg
1e511 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 ) pNC->allowAgg
1e512 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 = 0;. sqli
1e513 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 te3WalkExprList(
1e514 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29 3b pWalker, pList);
1e515 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 . if( is_ag
1e516 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 g ) pNC->allowAg
1e517 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20 g = 1;. /*
1e518 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74 65 FIX ME: Compute
1e519 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
1e51a 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 65 78 based on the ex
1e51b 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a 20 20 pected return.
1e51c 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 20 74 ** type of t
1e51d 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 20 he function .
1e51e 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 */. retu
1e51f 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
1e520 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
1e521 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
1e522 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c . case TK_SEL
1e523 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b ECT:. case TK
1e524 5f 45 58 49 53 54 53 3a 0a 23 65 6e 64 69 66 0a _EXISTS:.#endif.
1e525 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 case TK_IN:
1e526 7b 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72 {. if( Expr
1e527 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
1e528 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
1e529 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
1e52a 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 66 nRef = pNC->nRef
1e52b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
1e52c 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 _OMIT_CHECK.
1e52d 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 if( pNC->isC
1e52e 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 heck ){.
1e52f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1e530 67 28 70 50 61 72 73 65 2c 22 73 75 62 71 75 65 g(pParse,"subque
1e531 72 69 65 73 20 70 72 6f 68 69 62 69 74 65 64 20 ries prohibited
1e532 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 in CHECK constra
1e533 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20 20 ints");.
1e534 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 }.#endif.
1e535 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1e536 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 ct(pWalker, pExp
1e537 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 r->x.pSelect);.
1e538 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1e539 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29 NC->nRef>=nRef )
1e53a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52 ;. if( nR
1e53b 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b ef!=pNC->nRef ){
1e53c 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53 . ExprS
1e53d 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 etProperty(pExpr
1e53e 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b , EP_VarSelect);
1e53f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1e540 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1e541 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1e542 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a LITE_OMIT_CHECK.
1e543 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 case TK_VARI
1e544 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 ABLE: {. if
1e545 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 ( pNC->isCheck )
1e546 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1e547 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1e548 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 6f ,"parameters pro
1e549 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b hibited in CHECK
1e54a 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a constraints");.
1e54b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1e54c 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
1e54d 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 f. }. return (
1e54e 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 pParse->nErr ||
1e54f 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
1e550 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f ocFailed) ? WRC_
1e551 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74 Abort : WRC_Cont
1e552 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 inue;.}../*.** p
1e553 45 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 EList is a list
1e554 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77 of expressions w
1e555 68 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79 20 hich are really
1e556 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f the result set o
1e557 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43 f the.** a SELEC
1e558 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 45 T statement. pE
1e559 20 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61 6e is a term in an
1e55a 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
1e55b 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a UP BY clause..**
1e55c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
1e55d 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70 ecks to see if p
1e55e 45 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 64 E is a simple id
1e55f 65 6e 74 69 66 69 65 72 20 77 68 69 63 68 20 63 entifier which c
1e560 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f orresponds.** to
1e561 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20 the AS-name of
1e562 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d 73 one of the terms
1e563 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
1e564 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74 20 on list. If it
1e565 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 is,.** this rout
1e566 69 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69 6e ine return an in
1e567 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 teger between 1
1e568 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 and N where N is
1e569 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
1e56a 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45 * elements in pE
1e56b 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64 List, correspond
1e56c 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63 68 ing to the match
1e56d 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20 74 ing entry. If t
1e56e 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61 here is.** no ma
1e56f 74 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69 73 tch, or if pE is
1e570 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69 64 not a simple id
1e571 65 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20 74 entifier, then t
1e572 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 his routine.** r
1e573 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 eturn 0..**.** p
1e574 45 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20 72 EList has been r
1e575 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61 73 esolved. pE has
1e576 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 not..*/.static
1e577 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d int resolveAsNam
1e578 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
1e579 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 se, /* Parsi
1e57a 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 ng context for e
1e57b 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f rror messages */
1e57c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
1e57d 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ist, /* List of
1e57e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 expressions to
1e57f 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a scan */. Expr *
1e580 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pE /*
1e581 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61 72 Expression we ar
1e582 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74 63 e trying to matc
1e583 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b h */.){. int i;
1e584 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e585 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
1e586 0a 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 . if( pE->op==T
1e587 4b 5f 49 44 20 7c 7c 20 28 70 45 2d 3e 6f 70 3d K_ID || (pE->op=
1e588 3d 54 4b 5f 53 54 52 49 4e 47 20 26 26 20 70 45 =TK_STRING && pE
1e589 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 27 5c ->token.z[0]!='\
1e58a 27 27 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 '') ){. sqlit
1e58b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
1e58c 3e 64 62 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a >db;. char *z
1e58d 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d Col = sqlite3Nam
1e58e 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 eFromToken(db, &
1e58f 70 45 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 pE->token);.
1e590 69 66 28 20 7a 43 6f 6c 3d 3d 30 20 29 7b 0a 20 if( zCol==0 ){.
1e591 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a return -1;.
1e592 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d }. for(i=
1e593 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 0; i<pEList->nEx
1e594 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
1e595 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 char *zAs = pELi
1e596 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a st->a[i].zName;.
1e597 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 if( zAs!=0
1e598 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1e599 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 mp(zAs, zCol)==0
1e59a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1e59b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 te3DbFree(db, zC
1e59c 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 ol);. ret
1e59d 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d urn i+1;. }
1e59e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1e59f 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
1e5a0 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e l);. }. return
1e5a1 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 0;.}../*.** pE
1e5a2 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1e5a3 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 an expression wh
1e5a4 69 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65 20 ich is a single
1e5a5 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f term in the.** O
1e5a6 52 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f 6d RDER BY of a com
1e5a7 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20 54 pound SELECT. T
1e5a8 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 he expression ha
1e5a9 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61 s not been.** na
1e5aa 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a me resolved..**.
1e5ab 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74 20 ** At the point
1e5ac 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
1e5ad 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65 61 called, we alrea
1e5ae 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 dy know that the
1e5af 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 .** ORDER BY ter
1e5b0 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 m is not an inte
1e5b1 67 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 ger index into t
1e5b2 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 he result set.
1e5b3 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73 20 That.** case is
1e5b4 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 63 handled by the c
1e5b5 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a alling routine..
1e5b6 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f **.** Attempt to
1e5b7 20 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e 73 match pE agains
1e5b8 74 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c t result set col
1e5b9 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66 74 umns in the left
1e5ba 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20 -most.** SELECT
1e5bb 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 statement. Retu
1e5bc 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20 6f rn the index i o
1e5bd 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 63 f the matching c
1e5be 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20 olumn,.** as an
1e5bf 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 68 indication to th
1e5c0 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 74 e caller that it
1e5c1 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 20 should sort by
1e5c2 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e the i-th column.
1e5c3 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 .** The left-mos
1e5c4 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20 t column is 1.
1e5c5 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1e5c6 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
1e5c7 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d ed is the.** sam
1e5c8 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 e integer value
1e5c9 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75 73 that would be us
1e5ca 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 ed in the SQL st
1e5cb 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63 atement to indic
1e5cc 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d ate.** the colum
1e5cd 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 n..**.** If ther
1e5ce 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72 e is no match, r
1e5cf 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e eturn 0. Return
1e5d0 20 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72 20 -1 if an error
1e5d1 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 occurs..*/.stati
1e5d2 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 c int resolveOrd
1e5d3 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 erByTermToExprLi
1e5d4 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 st(. Parse *pPa
1e5d5 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 rse, /* Pars
1e5d6 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ing context for
1e5d7 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a error messages *
1e5d8 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c /. Select *pSel
1e5d9 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53 45 ect, /* The SE
1e5da 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 77 LECT statement w
1e5db 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 ith the ORDER BY
1e5dc 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 clause */. Exp
1e5dd 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 r *pE
1e5de 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20 /* The specific
1e5df 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a 2f ORDER BY term */
1e5e0 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 .){. int i;
1e5e1 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1e5e2 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 counter */. Ex
1e5e3 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
1e5e4 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 /* The columns
1e5e5 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
1e5e6 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 t */. NameConte
1e5e7 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d xt nc; /* Nam
1e5e8 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72 65 e context for re
1e5e9 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20 solving pE */..
1e5ea 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1e5eb 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 ExprIsInteger(pE
1e5ec 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45 , &i)==0 );. pE
1e5ed 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e List = pSelect->
1e5ee 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 pEList;.. /* Re
1e5ef 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 solve all names
1e5f0 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 in the ORDER BY
1e5f1 74 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a term expression.
1e5f2 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e */. memset(&n
1e5f3 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29 c, 0, sizeof(nc)
1e5f4 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20 3d );. nc.pParse =
1e5f5 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53 pParse;. nc.pS
1e5f6 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 rcList = pSelect
1e5f7 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c ->pSrc;. nc.pEL
1e5f8 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 ist = pEList;.
1e5f9 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b nc.allowAgg = 1;
1e5fa 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a . nc.nErr = 0;.
1e5fb 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 if( sqlite3Res
1e5fc 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 6e olveExprNames(&n
1e5fd 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73 71 c, pE) ){. sq
1e5fe 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 lite3ErrorClear(
1e5ff 70 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 74 pParse);. ret
1e600 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
1e601 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 Try to match th
1e602 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 e ORDER BY expre
1e603 73 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e ssion against an
1e604 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a expression. **
1e605 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1e606 65 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 31 et. Return an 1
1e607 2d 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 20 -based index of
1e608 74 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a the matching. *
1e609 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e 74 * result-set ent
1e60a 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 ry.. */. for(i
1e60b 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 =0; i<pEList->nE
1e60c 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 xpr; i++){. i
1e60d 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f f( sqlite3ExprCo
1e60e 6d 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b mpare(pEList->a[
1e60f 69 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29 7b i].pExpr, pE) ){
1e610 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b . return i+
1e611 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 1;. }. }..
1e612 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20 /* If no match,
1e613 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72 return 0. */. r
1e614 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
1e615 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f 52 * Generate an OR
1e616 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
1e617 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72 BY term out-of-r
1e618 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 ange error..*/.s
1e619 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c tatic void resol
1e61a 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f veOutOfRangeErro
1e61b 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 r(. Parse *pPar
1e61c 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 se, /* T
1e61d 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78 74 he error context
1e61e 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 into which to w
1e61f 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 20 2a rite the error *
1e620 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1e621 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f zType, /* "O
1e622 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 RDER" or "GROUP"
1e623 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 */. int i,
1e624 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e625 54 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61 73 The index (1-bas
1e626 65 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d 20 ed) of the term
1e627 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a out of range */.
1e628 20 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 20 int mx
1e629 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 /* Larg
1e62a 65 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 20 est permissible
1e62b 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b value of i */.){
1e62c 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d . sqlite3ErrorM
1e62d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 sg(pParse, .
1e62e 22 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 6f "%r %s BY term o
1e62f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 ut of range - sh
1e630 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22 62 ould be ". "b
1e631 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22 etween 1 and %d"
1e632 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b , i, zType, mx);
1e633 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a .}../*.** Analyz
1e634 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 e the ORDER BY c
1e635 6c 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f lause in a compo
1e636 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 und SELECT state
1e637 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a ment. Modify.*
1e638 2a 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 * each term of t
1e639 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
1e63a 73 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 se is a constant
1e63b 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e integer between
1e63c 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72 1.** and N wher
1e63d 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e N is the numbe
1e63e 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1e63f 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c the compound SEL
1e640 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52 ECT..**.** ORDER
1e641 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 BY terms that a
1e642 72 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e re already an in
1e643 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 teger between 1
1e644 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d and N are.** unm
1e645 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52 20 odified. ORDER
1e646 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 BY terms that ar
1e647 65 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73 69 e integers outsi
1e648 64 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66 0a de the range of.
1e649 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20 67 ** 1 through N g
1e64a 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 enerate an error
1e64b 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d . ORDER BY term
1e64c 73 20 74 68 61 74 20 61 72 65 20 65 78 70 72 65 s that are expre
1e64d 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 ssions.** are ma
1e64e 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 72 65 tched against re
1e64f 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 sult set express
1e650 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 ions of compound
1e651 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e SELECT.** begin
1e652 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c 65 ning with the le
1e653 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 61 ft-most SELECT a
1e654 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72 nd working towar
1e655 64 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20 d the right..**
1e656 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 At the first mat
1e657 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 59 ch, the ORDER BY
1e658 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 expression is t
1e659 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a ransformed into.
1e65a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 ** the integer c
1e65b 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a olumn number..**
1e65c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1e65d 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
1e65e 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 seen..*/.static
1e65f 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f int resolveCompo
1e660 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50 61 undOrderBy(. Pa
1e661 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1e662 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
1e663 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65 ontext. Leave e
1e664 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 rror messages he
1e665 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a re */. Select *
1e666 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a pSelect /*
1e667 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 The SELECT stat
1e668 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 ement containing
1e669 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a 2f the ORDER BY */
1e66a 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 .){. int i;. E
1e66b 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 xprList *pOrderB
1e66c 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 y;. ExprList *p
1e66d 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 EList;. sqlite3
1e66e 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65 *db;. int more
1e66f 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72 ToDo = 1;.. pOr
1e670 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 2d derBy = pSelect-
1e671 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28 >pOrderBy;. if(
1e672 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 pOrderBy==0 ) r
1e673 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 eturn 0;. db =
1e674 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20 pParse->db;.#if
1e675 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d SQLITE_MAX_COLUM
1e676 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 N. if( pOrderBy
1e677 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d ->nExpr>db->aLim
1e678 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1e679 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 COLUMN] ){. s
1e67a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1e67b 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 Parse, "too many
1e67c 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52 20 terms in ORDER
1e67d 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 BY clause");.
1e67e 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 return 1;. }.#
1e67f 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b endif. for(i=0;
1e680 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 i<pOrderBy->nEx
1e681 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f pr; i++){. pO
1e682 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e rderBy->a[i].don
1e683 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65 e = 0;. }. pSe
1e684 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b lect->pNext = 0;
1e685 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 . while( pSelec
1e686 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 t->pPrior ){.
1e687 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 pSelect->pPrior
1e688 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65 63 ->pNext = pSelec
1e689 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d t;. pSelect =
1e68a 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 pSelect->pPrior
1e68b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 ;. }. while( p
1e68c 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 6f Select && moreTo
1e68d 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 Do ){. struct
1e68e 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1e68f 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54 pItem;. moreT
1e690 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c oDo = 0;. pEL
1e691 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 ist = pSelect->p
1e692 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 EList;. asser
1e693 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a t( pEList!=0 );.
1e694 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 for(i=0, pIt
1e695 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 em=pOrderBy->a;
1e696 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 i<pOrderBy->nExp
1e697 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 r; i++, pItem++)
1e698 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c {. int iCol
1e699 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78 70 = -1;. Exp
1e69a 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20 r *pE, *pDup;.
1e69b 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 64 if( pItem->d
1e69c 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a one ) continue;.
1e69d 20 20 20 20 20 20 70 45 20 3d 20 70 49 74 65 6d pE = pItem
1e69e 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 ->pExpr;. i
1e69f 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 f( sqlite3ExprIs
1e6a0 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f Integer(pE, &iCo
1e6a1 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 l) ){. if
1e6a2 28 20 69 43 6f 6c 3c 30 20 7c 7c 20 69 43 6f 6c ( iCol<0 || iCol
1e6a3 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 >pEList->nExpr )
1e6a4 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f {. reso
1e6a5 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 lveOutOfRangeErr
1e6a6 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45 or(pParse, "ORDE
1e6a7 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d R", i+1, pEList-
1e6a8 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 >nExpr);.
1e6a9 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1e6aa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1e6ab 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c se{. iCol
1e6ac 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 = resolveAsName
1e6ad 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c (pParse, pEList,
1e6ae 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 pE);. if
1e6af 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 ( iCol==0 ){.
1e6b0 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 pDup = sq
1e6b1 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
1e6b2 20 70 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 pE, 0);.
1e6b3 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c if( !db->mall
1e6b4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
1e6b5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 assert(p
1e6b6 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Dup);.
1e6b7 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 iCol = resolve
1e6b8 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 OrderByTermToExp
1e6b9 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 rList(pParse, pS
1e6ba 65 6c 65 63 74 2c 20 70 44 75 70 29 3b 0a 20 20 elect, pDup);.
1e6bb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e6bc 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
1e6bd 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b elete(db, pDup);
1e6be 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1e6bf 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b if( iCol<0 ){
1e6c0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
1e6c1 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 n 1;. }.
1e6c2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1e6c3 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 iCol>0 ){.
1e6c4 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c CollSeq *pCol
1e6c5 6c 20 3d 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 l = pE->pColl;.
1e6c6 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 int flags
1e6c7 20 3d 20 70 45 2d 3e 66 6c 61 67 73 20 26 20 45 = pE->flags & E
1e6c8 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 P_ExpCollate;.
1e6c9 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1e6ca 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 29 3b rDelete(db, pE);
1e6cb 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e . pItem->
1e6cc 70 45 78 70 72 20 3d 20 70 45 20 3d 20 73 71 6c pExpr = pE = sql
1e6cd 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f ite3Expr(db, TK_
1e6ce 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 30 INTEGER, 0, 0, 0
1e6cf 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
1e6d0 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b E==0 ) return 1;
1e6d1 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f . pE->pCo
1e6d2 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 ll = pColl;.
1e6d3 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d pE->flags |=
1e6d4 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66 EP_IntValue | f
1e6d5 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45 lags;. pE
1e6d6 2d 3e 69 54 61 62 6c 65 20 3d 20 69 43 6f 6c 3b ->iTable = iCol;
1e6d7 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e . pItem->
1e6d8 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c iCol = (u16)iCol
1e6d9 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d ;. pItem-
1e6da 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 >done = 1;.
1e6db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1e6dc 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 20 20 moreToDo = 1;.
1e6dd 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1e6de 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 pSelect = pSelec
1e6df 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 t->pNext;. }.
1e6e0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 for(i=0; i<pOrde
1e6e1 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 rBy->nExpr; i++)
1e6e2 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 {. if( pOrder
1e6e3 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 By->a[i].done==0
1e6e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1e6e5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1e6e6 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59 20 74 , "%r ORDER BY t
1e6e7 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 erm does not mat
1e6e8 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20 20 20 ch any ".
1e6e9 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e 20 "column in
1e6ea 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 22 2c the result set",
1e6eb 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72 65 74 i+1);. ret
1e6ec 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 1;. }. }
1e6ed 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1e6ee 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76 65 72 /*.** Check ever
1e6ef 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f 52 y term in the OR
1e6f0 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
1e6f1 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 BY clause pOrder
1e6f2 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53 45 4c By of.** the SEL
1e6f3 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53 ECT statement pS
1e6f4 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79 20 74 elect. If any t
1e6f5 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e 63 65 erm is reference
1e6f6 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c 74 20 to a.** result
1e6f7 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 28 set expression (
1e6f8 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 as determined by
1e6f9 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61 2e the ExprList.a.
1e6fa 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a 20 74 iCol field).** t
1e6fb 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 61 74 hen convert that
1e6fc 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63 6f 70 term into a cop
1e6fd 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 y of the corresp
1e6fe 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65 onding result se
1e6ff 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a t.** column..**.
1e700 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73 ** If any errors
1e701 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20 61 are detected, a
1e702 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 dd an error mess
1e703 61 67 65 20 74 6f 20 70 50 61 72 73 65 20 61 6e age to pParse an
1e704 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d d.** return non-
1e705 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 zero. Return ze
1e706 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72 73 20 ro if no errors
1e707 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c are seen..*/.SQL
1e708 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1e709 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 sqlite3ResolveOr
1e70a 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 50 61 derGroupBy(. Pa
1e70b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1e70c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
1e70d 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65 ontext. Leave e
1e70e 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 rror messages he
1e70f 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a re */. Select *
1e710 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a pSelect, /*
1e711 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 The SELECT stat
1e712 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 ement containing
1e713 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 20 the clause */.
1e714 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 ExprList *pOrde
1e715 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20 4f 52 rBy, /* The OR
1e716 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
1e717 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 BY clause to be
1e718 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 63 processed */. c
1e719 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 onst char *zType
1e71a 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 /* "ORDER"
1e71b 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 29 7b or "GROUP" */.){
1e71c 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 . int i;. sqli
1e71d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1e71e 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69 73 74 ->db;. ExprList
1e71f 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75 *pEList;. stru
1e720 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
1e721 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 *pItem;.. if(
1e722 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 70 pOrderBy==0 || p
1e723 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
1e724 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e cFailed ) return
1e725 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 0;.#if SQLITE_M
1e726 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 AX_COLUMN. if(
1e727 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e pOrderBy->nExpr>
1e728 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1e729 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 E_LIMIT_COLUMN]
1e72a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1e72b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1e72c 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 too many terms i
1e72d 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65 22 2c n %s BY clause",
1e72e 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72 65 74 zType);. ret
1e72f 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 urn 1;. }.#endi
1e730 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 f. pEList = pSe
1e731 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 lect->pEList;.
1e732 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d assert( pEList!=
1e733 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 0 ); /* sqlite3
1e734 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75 61 72 SelectNew() guar
1e735 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f 0a 20 antees this */.
1e736 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d for(i=0, pItem=
1e737 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 pOrderBy->a; i<p
1e738 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 OrderBy->nExpr;
1e739 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
1e73a 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 if( pItem->iC
1e73b 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ol ){. if(
1e73c 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 4c 69 pItem->iCol>pELi
1e73d 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 st->nExpr ){.
1e73e 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f resolveOutO
1e73f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 fRangeError(pPar
1e740 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 se, zType, i+1,
1e741 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a pEList->nExpr);.
1e742 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 return 1
1e743 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1e744 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 resolveAlias(pPa
1e745 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 49 74 rse, pEList, pIt
1e746 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 74 65 em->iCol-1, pIte
1e747 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70 65 29 m->pExpr, zType)
1e748 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1e749 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
1e74a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20 pOrderBy is an
1e74b 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
1e74c 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20 53 P BY clause in S
1e74d 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
1e74e 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 65 20 pSelect..** The
1e74f 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 Name context of
1e750 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 the SELECT state
1e751 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20 7a 54 ment is pNC. zT
1e752 79 70 65 20 69 73 20 65 69 74 68 65 72 0a 2a 2a ype is either.**
1e753 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f "ORDER" or "GRO
1e754 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e UP" depending on
1e755 20 77 68 69 63 68 20 74 79 70 65 20 6f 66 20 63 which type of c
1e756 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 20 69 lause pOrderBy i
1e757 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
1e758 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73 20 65 utine resolves e
1e759 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 ach term of the
1e75a 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e 20 65 clause into an e
1e75b 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 49 66 xpression..** If
1e75c 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65 the order-by te
1e75d 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 rm is an integer
1e75e 20 49 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 I between 1 and
1e75f 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73 20 74 N (where N is t
1e760 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
1e761 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 columns in the r
1e762 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 esult set of the
1e763 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20 74 68 SELECT) then th
1e764 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 e expression.**
1e765 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74 69 6f in the resolutio
1e766 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 n is a copy of t
1e767 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74 2d 73 he I-th result-s
1e768 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 et expression.
1e769 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65 72 2d If.** the order-
1e76a 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69 64 by term is an id
1e76b 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f 72 72 entify that corr
1e76c 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 41 esponds to the A
1e76d 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 20 72 S-name of.** a r
1e76e 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 esult-set expres
1e76f 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 sion, then the t
1e770 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 erm resolves to
1e771 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a a copy of the.**
1e772 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 result-set expr
1e773 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 ession. Otherwi
1e774 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 se, the expressi
1e775 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64 20 69 on is resolved i
1e776 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c 20 77 n.** the usual w
1e777 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c 69 74 ay - using sqlit
1e778 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
1e779 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 es()..**.** This
1e77a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
1e77b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1e77c 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72 6f 72 rrors. If error
1e77d 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a 2a 2a s occur, then.**
1e77e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
1e77f 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6d 69 error message mi
1e780 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e 20 70 ght be left in p
1e781 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65 72 72 Parse. (OOM err
1e782 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65 64 2e ors.** excepted.
1e783 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ).*/.static int
1e784 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 resolveOrderGrou
1e785 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 pBy(. NameConte
1e786 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f 2a 20 xt *pNC, /*
1e787 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 The name context
1e788 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 of the SELECT s
1e789 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 tatement */. Se
1e78a 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 lect *pSelect,
1e78b 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 /* The SELEC
1e78c 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f 6c 64 T statement hold
1e78d 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a 2f 0a ing pOrderBy */.
1e78e 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 ExprList *pOrd
1e78f 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20 4f 52 erBy, /* An OR
1e790 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
1e791 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72 65 73 BY clause to res
1e792 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 olve */. const
1e793 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20 char *zType
1e794 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44 45 52 /* Either "ORDER
1e795 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20 61 73 " or "GROUP", as
1e796 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a appropriate */.
1e797 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
1e798 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e799 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1e79a 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f ter */. int iCo
1e79b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
1e79c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d /* Colum
1e79d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 74 n number */. st
1e79e 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
1e79f 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f 2a 20 em *pItem; /*
1e7a0 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 A term of the OR
1e7a1 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
1e7a2 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1e7a3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1e7a4 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
1e7a5 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 text */. int nR
1e7a6 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 esult;
1e7a7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1e7a8 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 er of terms in t
1e7a9 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f he result set */
1e7aa 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 .. if( pOrderBy
1e7ab 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1e7ac 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53 65 6c nResult = pSel
1e7ad 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 ect->pEList->nEx
1e7ae 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 pr;. pParse = p
1e7af 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 66 6f NC->pParse;. fo
1e7b0 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 r(i=0, pItem=pOr
1e7b1 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 derBy->a; i<pOrd
1e7b2 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b erBy->nExpr; i++
1e7b3 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
1e7b4 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74 65 6d Expr *pE = pItem
1e7b5 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 43 6f ->pExpr;. iCo
1e7b6 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d l = resolveAsNam
1e7b7 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 e(pParse, pSelec
1e7b8 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a t->pEList, pE);.
1e7b9 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 if( iCol<0 )
1e7ba 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
1e7bb 3b 20 20 2f 2a 20 4f 4f 4d 20 65 72 72 6f 72 20 ; /* OOM error
1e7bc 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 */. }. if(
1e7bd 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 iCol>0 ){.
1e7be 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d /* If an AS-nam
1e7bf 65 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 e match is found
1e7c0 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45 , mark this ORDE
1e7c1 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62 R BY column as b
1e7c2 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 20 eing. ** a
1e7c3 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c copy of the iCol
1e7c4 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 63 -th result-set c
1e7c5 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 73 olumn. The subs
1e7c6 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 equent call to.
1e7c7 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 ** sqlite3R
1e7c8 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
1e7c9 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 By() will conver
1e7ca 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e t the expression
1e7cb 20 74 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 to a. ** c
1e7cc 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d opy of the iCol-
1e7cd 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 th result-set ex
1e7ce 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 pression. */.
1e7cf 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d pItem->iCol =
1e7d0 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 (u16)iCol;.
1e7d1 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
1e7d2 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1e7d3 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 3ExprIsInteger(p
1e7d4 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 E, &iCol) ){.
1e7d5 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 /* The ORDER
1e7d6 42 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e BY term is an in
1e7d7 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 20 teger constant.
1e7d8 20 41 67 61 69 6e 2c 20 73 65 74 20 74 68 65 20 Again, set the
1e7d9 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 column. **
1e7da 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 73 number so that s
1e7db 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 qlite3ResolveOrd
1e7dc 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c erGroupBy() will
1e7dd 20 63 6f 6e 76 65 72 74 20 74 68 65 0a 20 20 20 convert the.
1e7de 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62 79 20 74 ** order-by t
1e7df 65 72 6d 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 erm to a copy of
1e7e0 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 the result-set
1e7e1 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 expression */.
1e7e2 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 31 20 29 if( iCol<1 )
1e7e3 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 {. resolv
1e7e4 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 eOutOfRangeError
1e7e5 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 (pParse, zType,
1e7e6 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29 3b 0a 20 i+1, nResult);.
1e7e7 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b return 1;
1e7e8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1e7e9 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 Item->iCol = (u1
1e7ea 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6)iCol;. co
1e7eb 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 ntinue;. }..
1e7ec 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c /* Otherwise,
1e7ed 20 74 72 65 61 74 20 74 68 65 20 4f 52 44 45 52 treat the ORDER
1e7ee 20 42 59 20 74 65 72 6d 20 61 73 20 61 6e 20 6f BY term as an o
1e7ef 72 64 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 rdinary expressi
1e7f0 6f 6e 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d on */. pItem-
1e7f1 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69 >iCol = 0;. i
1e7f2 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 f( sqlite3Resolv
1e7f3 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20 eExprNames(pNC,
1e7f4 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 pE) ){. ret
1e7f5 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 1;. }. }
1e7f6 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1e7f7 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 3ResolveOrderGro
1e7f8 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 53 65 upBy(pParse, pSe
1e7f9 6c 65 63 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 lect, pOrderBy,
1e7fa 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a zType);.}../*.**
1e7fb 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 Resolve names i
1e7fc 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 n the SELECT sta
1e7fd 74 65 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c tement p and all
1e7fe 20 6f 66 20 69 74 73 20 64 65 73 63 65 6e 64 65 of its descende
1e7ff 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nts..*/.static i
1e800 6e 74 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 nt resolveSelect
1e801 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 Step(Walker *pWa
1e802 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
1e803 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 {. NameContext
1e804 2a 70 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43 *pOuterNC; /* C
1e805 6f 6e 74 65 78 74 20 74 68 61 74 20 63 6f 6e 74 ontext that cont
1e806 61 69 6e 73 20 74 68 69 73 20 53 45 4c 45 43 54 ains this SELECT
1e807 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 */. NameContex
1e808 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a t sNC; /*
1e809 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 Name context of
1e80a 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a this SELECT */.
1e80b 20 20 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 int isCompound
1e80c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
1e80d 65 20 69 66 20 70 20 69 73 20 61 20 63 6f 6d 70 e if p is a comp
1e80e 6f 75 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20 ound select */.
1e80f 20 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20 int nCompound;
1e810 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1e811 65 72 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74 er of compound t
1e812 65 72 6d 73 20 70 72 6f 63 65 73 73 65 64 20 73 erms processed s
1e813 6f 20 66 61 72 20 2a 2f 0a 20 20 50 61 72 73 65 o far */. Parse
1e814 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 *pParse;
1e815 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
1e816 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c ntext */. ExprL
1e817 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 ist *pEList;
1e818 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 74 /* Result set
1e819 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
1e81a 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 */. int i;
1e81b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e81c 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
1e81d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 . ExprList *pGr
1e81e 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a 20 54 68 oupBy; /* Th
1e81f 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 e GROUP BY claus
1e820 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 e */. Select *p
1e821 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f Leftmost; /
1e822 2a 20 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 * Left-most of S
1e823 45 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f ELECT of a compo
1e824 75 6e 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 und */. sqlite3
1e825 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 *db;
1e826 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e /* Database con
1e827 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 nection */. ..
1e828 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
1e829 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 . if( p->selFla
1e82a 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 gs & SF_Resolved
1e82b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 ){. return W
1e82c 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 RC_Prune;. }.
1e82d 70 4f 75 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b pOuterNC = pWalk
1e82e 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61 er->u.pNC;. pPa
1e82f 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 rse = pWalker->p
1e830 50 61 72 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 Parse;. db = pP
1e831 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 arse->db;.. /*
1e832 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33 Normally sqlite3
1e833 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 77 SelectExpand() w
1e834 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 66 69 ill be called fi
1e835 72 73 74 20 61 6e 64 20 77 69 6c 6c 20 68 61 76 rst and will hav
1e836 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 e. ** already e
1e837 78 70 61 6e 64 65 64 20 74 68 69 73 20 53 45 4c xpanded this SEL
1e838 45 43 54 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 ECT. However, i
1e839 66 20 74 68 69 73 20 69 73 20 61 20 73 75 62 71 f this is a subq
1e83a 75 65 72 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a uery within. **
1e83b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 an expression,
1e83c 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
1e83d 70 72 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20 62 prNames() will b
1e83e 65 20 63 61 6c 6c 65 64 20 77 69 74 68 6f 75 74 e called without
1e83f 20 61 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63 61 a. ** prior ca
1e840 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 53 65 6c ll to sqlite3Sel
1e841 65 63 74 45 78 70 61 6e 64 28 29 2e 20 20 57 68 ectExpand(). Wh
1e842 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c en that happens,
1e843 20 6c 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 let. ** sqlite
1e844 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 64 6f 3SelectPrep() do
1e845 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 all of the proc
1e846 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20 essing for this
1e847 53 45 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c SELECT.. ** sql
1e848 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 ite3SelectPrep()
1e849 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 will invoke bot
1e84a 68 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 h sqlite3SelectE
1e84b 78 70 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a xpand() and. **
1e84c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e this routine in
1e84d 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 the correct ord
1e84e 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 er.. */. if( (
1e84f 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 p->selFlags & SF
1e850 5f 45 78 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b _Expanded)==0 ){
1e851 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 . sqlite3Sele
1e852 63 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 ctPrep(pParse, p
1e853 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 , pOuterNC);.
1e854 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d return (pParse-
1e855 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c >nErr || db->mal
1e856 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 locFailed) ? WRC
1e857 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 50 72 75 _Abort : WRC_Pru
1e858 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d ne;. }.. isCom
1e859 70 6f 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f pound = p->pPrio
1e85a 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e r!=0;. nCompoun
1e85b 64 20 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f d = 0;. pLeftmo
1e85c 73 74 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28 st = p;. while(
1e85d 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 p ){. assert
1e85e 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 ( (p->selFlags &
1e85f 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 SF_Expanded)!=0
1e860 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1e861 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 (p->selFlags & S
1e862 46 5f 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29 F_Resolved)==0 )
1e863 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 ;. p->selFlag
1e864 73 20 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64 s |= SF_Resolved
1e865 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 ;.. /* Resolv
1e866 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e e the expression
1e867 73 20 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 s in the LIMIT a
1e868 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 nd OFFSET clause
1e869 73 2e 20 54 68 65 73 65 0a 20 20 20 20 2a 2a 20 s. These. **
1e86a 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 are not allowed
1e86b 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 to refer to any
1e86c 6e 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61 names, so pass a
1e86d 6e 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 n empty NameCont
1e86e 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ext.. */.
1e86f 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 memset(&sNC, 0,
1e870 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 sizeof(sNC));.
1e871 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 sNC.pParse = p
1e872 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 Parse;. if( s
1e873 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
1e874 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e rNames(&sNC, p->
1e875 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 pLimit) ||.
1e876 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 sqlite3Resolv
1e877 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
1e878 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a p->pOffset) ){.
1e879 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1e87a 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
1e87b 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 70 20 74 . /* Set up t
1e87c 68 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f he local name-co
1e87d 6e 74 65 78 74 20 74 6f 20 70 61 73 73 20 74 6f ntext to pass to
1e87e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1e87f 78 70 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20 xprNames() to.
1e880 20 20 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 ** resolve the
1e881 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 result-set expr
1e882 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20 ession list..
1e883 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f */. sNC.allo
1e884 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e wAgg = 1;. sN
1e885 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e C.pSrcList = p->
1e886 70 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e pSrc;. sNC.pN
1e887 65 78 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a ext = pOuterNC;.
1e888 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 . /* Resolv
1e889 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72 e names in the r
1e88a 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 esult set. */.
1e88b 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 pEList = p->pE
1e88c 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 List;. assert
1e88d 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 ( pEList!=0 );.
1e88e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 for(i=0; i<pE
1e88f 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b List->nExpr; i++
1e890 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 ){. Expr *p
1e891 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d X = pEList->a[i]
1e892 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 .pExpr;. if
1e893 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 ( sqlite3Resolve
1e894 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 ExprNames(&sNC,
1e895 70 58 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 pX) ){. r
1e896 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1e897 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1e898 20 0a 20 20 20 20 2f 2a 20 52 65 63 75 72 73 69 . /* Recursi
1e899 76 65 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d vely resolve nam
1e89a 65 73 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 es in all subque
1e89b 72 69 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ries. */.
1e89c 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 for(i=0; i<p->pS
1e89d 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a rc->nSrc; i++){.
1e89e 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 struct Src
1e89f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1e8a0 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 = &p->pSrc->a[i
1e8a1 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 ];. if( pIt
1e8a2 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 em->pSelect ){.
1e8a3 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 const cha
1e8a4 72 20 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 r *zSavedContext
1e8a5 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 = pParse->zAuth
1e8a6 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 Context;.
1e8a7 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d if( pItem->zNam
1e8a8 65 20 29 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 e ) pParse->zAut
1e8a9 68 43 6f 6e 74 65 78 74 20 3d 20 70 49 74 65 6d hContext = pItem
1e8aa 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 ->zName;.
1e8ab 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 sqlite3ResolveS
1e8ac 65 6c 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 electNames(pPars
1e8ad 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 e, pItem->pSelec
1e8ae 74 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 t, pOuterNC);.
1e8af 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 pParse->zA
1e8b0 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 uthContext = zSa
1e8b1 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 vedContext;.
1e8b2 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e if( pParse->
1e8b3 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c nErr || db->mall
1e8b4 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 ocFailed ) retur
1e8b5 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1e8b6 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
1e8b7 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
1e8b8 65 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 e no aggregate f
1e8b9 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 unctions in the
1e8ba 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20 result-set, and
1e8bb 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20 20 no GROUP BY .
1e8bc 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 ** expression,
1e8bd 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67 do not allow agg
1e8be 72 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f regates in any o
1e8bf 66 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 f the other expr
1e8c0 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a essions.. */.
1e8c1 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e assert( (p->
1e8c2 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 selFlags & SF_Ag
1e8c3 67 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20 gregate)==0 );.
1e8c4 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d pGroupBy = p-
1e8c5 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 69 >pGroupBy;. i
1e8c6 66 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20 73 f( pGroupBy || s
1e8c7 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20 20 NC.hasAgg ){.
1e8c8 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c p->selFlags |
1e8c9 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a = SF_Aggregate;.
1e8ca 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e8cb 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 sNC.allowAgg =
1e8cc 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 0;. }. .
1e8cd 2f 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 63 /* If a HAVING c
1e8ce 6c 61 75 73 65 20 69 73 20 70 72 65 73 65 6e 74 lause is present
1e8cf 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 , then there mus
1e8d0 74 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 20 t be a GROUP BY
1e8d1 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 clause.. */.
1e8d2 20 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e if( p->pHavin
1e8d3 67 20 26 26 20 21 70 47 72 6f 75 70 42 79 20 29 g && !pGroupBy )
1e8d4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1e8d5 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1e8d6 22 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 "a GROUP BY clau
1e8d7 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62 se is required b
1e8d8 65 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b 0a efore HAVING");.
1e8d9 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1e8da 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
1e8db 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 . /* Add the
1e8dc 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 expression list
1e8dd 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 to the name-cont
1e8de 65 78 74 20 62 65 66 6f 72 65 20 70 61 72 73 69 ext before parsi
1e8df 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 ng the. ** ot
1e8e0 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 her expressions
1e8e1 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 in the SELECT st
1e8e2 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 atement. This is
1e8e3 20 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 so that. **
1e8e4 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 expressions in t
1e8e5 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1e8e6 28 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65 72 (etc.) can refer
1e8e7 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 to expressions
1e8e8 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 65 by. ** aliase
1e8e9 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
1e8ea 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 set.. **.
1e8eb 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20 ** Minor point:
1e8ec 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
1e8ed 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 ase, then the ex
1e8ee 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65 pression will be
1e8ef 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75 . ** re-evalu
1e8f0 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 65 ated for each re
1e8f1 66 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20 ference to it..
1e8f2 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45 */. sNC.pE
1e8f3 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 List = p->pEList
1e8f4 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1e8f5 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
1e8f6 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72 s(&sNC, p->pWher
1e8f7 65 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c e) ||. sql
1e8f8 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
1e8f9 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48 ames(&sNC, p->pH
1e8fa 61 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20 aving). ){.
1e8fb 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
1e8fc 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 bort;. }..
1e8fd 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 /* The ORDER BY
1e8fe 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 6c and GROUP BY cl
1e8ff 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 65 auses may not re
1e900 66 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a fer to terms in.
1e901 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 ** outer que
1e902 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20 ries . */.
1e903 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a sNC.pNext = 0;.
1e904 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 sNC.allowAgg
1e905 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 = 1;.. /* Pr
1e906 6f 63 65 73 73 20 74 68 65 20 4f 52 44 45 52 20 ocess the ORDER
1e907 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73 69 BY clause for si
1e908 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20 73 ngleton SELECT s
1e909 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a tatements.. *
1e90a 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 * The ORDER BY c
1e90b 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75 lause for compou
1e90c 6e 64 73 20 53 45 4c 45 43 54 20 73 74 61 74 65 nds SELECT state
1e90d 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65 64 ments is handled
1e90e 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61 . ** below, a
1e90f 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 fter all of the
1e910 72 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72 20 result-sets for
1e911 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d 65 all of the eleme
1e912 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 nts of. ** th
1e913 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 e compound have
1e914 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 been resolved..
1e915 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 */. if( !i
1e916 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 sCompound && res
1e917 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
1e918 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72 (&sNC, p, p->pOr
1e919 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 20 derBy, "ORDER")
1e91a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1e91b 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
1e91c 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
1e91d 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1e91e 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1e91f 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 ort;. }. .
1e920 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 /* Resolve the
1e921 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
1e922 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 . At the same t
1e923 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a ime, make sure .
1e924 20 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55 50 ** the GROUP
1e925 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20 BY clause does
1e926 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67 72 not contain aggr
1e927 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e egate functions.
1e928 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1e929 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 pGroupBy ){.
1e92a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
1e92b 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
1e92c 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65 . if( re
1e92d 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 solveOrderGroupB
1e92e 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75 y(&sNC, p, pGrou
1e92f 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c pBy, "GROUP") ||
1e930 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1e931 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 d ){. ret
1e932 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1e933 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 }. for
1e934 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f (i=0, pItem=pGro
1e935 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 upBy->a; i<pGrou
1e936 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c pBy->nExpr; i++,
1e937 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1e938 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
1e939 6f 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 operty(pItem->pE
1e93a 78 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a xpr, EP_Agg) ){.
1e93b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1e93c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1e93d 2c 20 22 61 67 67 72 65 67 61 74 65 20 66 75 6e , "aggregate fun
1e93e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 ctions are not a
1e93f 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20 20 llowed in ".
1e940 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20 47 "the G
1e941 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22 29 ROUP BY clause")
1e942 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
1e943 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1e944 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1e945 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 }.. /* Ad
1e946 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 vance to the nex
1e947 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6f t term of the co
1e948 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20 mpound. */.
1e949 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b p = p->pPrior;
1e94a 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b . nCompound++
1e94b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f ;. }.. /* Reso
1e94c 6c 76 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 lve the ORDER BY
1e94d 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 on a compound S
1e94e 45 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c 20 ELECT after all
1e94f 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68 terms of. ** th
1e950 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 e compound have
1e951 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 been resolved..
1e952 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 70 */. if( isComp
1e953 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 43 ound && resolveC
1e954 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 70 ompoundOrderBy(p
1e955 50 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74 Parse, pLeftmost
1e956 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
1e957 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a WRC_Abort;. }..
1e958 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
1e959 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ne;.}../*.** Thi
1e95a 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 s routine walks
1e95b 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 an expression tr
1e95c 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 20 ee and resolves
1e95d 72 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a references to.**
1e95e 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61 table columns a
1e95f 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f nd result-set co
1e960 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20 73 lumns. At the s
1e961 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72 72 ame time, do err
1e962 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f or.** checking o
1e963 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67 65 n function usage
1e964 20 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67 20 and set a flag
1e965 69 66 20 61 6e 79 20 61 67 67 72 65 67 61 74 65 if any aggregate
1e966 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 functions.** ar
1e967 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f e seen..**.** To
1e968 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 63 resolve table c
1e969 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 olumns reference
1e96a 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f s we look for no
1e96b 64 65 73 20 28 6f 72 20 73 75 62 74 72 65 65 73 des (or subtrees
1e96c 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72 ) of the .** for
1e96d 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f m X.Y.Z or Y.Z o
1e96e 72 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a 2a r just Z where.*
1e96f 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20 54 *.** X: T
1e970 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 he name of a dat
1e971 61 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d 61 abase. Ex: "ma
1e972 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f 72 in" or "temp" or
1e973 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 .** th
1e974 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 e symbolic name
1e975 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 41 assigned to an A
1e976 54 54 41 43 48 2d 65 64 20 64 61 74 61 62 61 73 TTACH-ed databas
1e977 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a e..**.** Y:
1e978 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 The name of a
1e979 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d table in a FROM
1e97a 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e 20 clause. Or in
1e97b 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20 20 a trigger.**
1e97c 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74 68 one of th
1e97d 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73 20 e special names
1e97e 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a "old" or "new"..
1e97f 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20 **.** Z:
1e980 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f The name of a co
1e981 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e lumn in table Y.
1e982 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20 .**.** The node
1e983 61 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 at the root of t
1e984 68 65 20 73 75 62 74 72 65 65 20 69 73 20 6d 6f he subtree is mo
1e985 64 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77 dified as follow
1e986 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72 s:.**.** Expr
1e987 2e 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e 67 .op Chang
1e988 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a ed to TK_COLUMN.
1e989 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62 20 ** Expr.pTab
1e98a 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 Points to t
1e98b 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 he Table object
1e98c 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78 for X.Y.** Ex
1e98d 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65 pr.iColumn The
1e98e 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e column index in
1e98f 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74 68 X.Y. -1 for th
1e990 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45 e rowid..** E
1e991 78 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54 68 xpr.iTable Th
1e992 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 e VDBE cursor nu
1e993 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a mber for X.Y.**.
1e994 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 **.** To resolve
1e995 20 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66 65 result-set refe
1e996 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72 rences, look for
1e997 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 expression node
1e998 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d s of the.** form
1e999 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61 6e Z (with no X an
1e99a 64 20 59 20 70 72 65 66 69 78 29 20 77 68 65 72 d Y prefix) wher
1e99b 65 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73 20 e the Z matches
1e99c 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a the right-hand.*
1e99d 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 20 * size of an AS
1e99e 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 65 clause in the re
1e99f 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 45 sult-set of a SE
1e9a0 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 70 LECT. The Z exp
1e9a1 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 ression.** is re
1e9a2 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 79 placed by a copy
1e9a3 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e of the left-han
1e9a4 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 d side of the re
1e9a5 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
1e9a6 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61 ion..** Table-na
1e9a7 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 me and function
1e9a8 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72 resolution occur
1e9a9 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69 74 s on the substit
1e9aa 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 0a uted expression.
1e9ab 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65 78 ** tree. For ex
1e9ac 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a ample, in:.**.**
1e9ad 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 SELECT a+b
1e9ae 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20 AS x, c+d AS y
1e9af 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59 FROM t1 ORDER BY
1e9b0 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78 x;.**.** The "x
1e9b1 22 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f 72 " term of the or
1e9b2 64 65 72 20 62 79 20 69 73 20 72 65 70 6c 61 63 der by is replac
1e9b3 65 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20 72 ed by "a+b" to r
1e9b4 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ender:.**.**
1e9b5 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 SELECT a+b AS
1e9b6 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d x, c+d AS y FROM
1e9b7 20 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b 62 t1 ORDER BY a+b
1e9b8 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ;.**.** Function
1e9b9 20 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63 6b calls are check
1e9ba 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ed to make sure
1e9bb 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f that the functio
1e9bc 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64 n is .** defined
1e9bd 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 6f and that the co
1e9be 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrect number of
1e9bf 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73 70 arguments are sp
1e9c0 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74 ecified..** If t
1e9c1 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 he function is a
1e9c2 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 n aggregate func
1e9c3 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 tion, then the p
1e9c4 4e 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a NC->hasAgg is.**
1e9c5 20 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70 63 set and the opc
1e9c6 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 66 ode is changed f
1e9c7 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 rom TK_FUNCTION
1e9c8 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 to TK_AGG_FUNCTI
1e9c9 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70 ON..** If an exp
1e9ca 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 ression contains
1e9cb 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1e9cc 69 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45 50 ions then the EP
1e9cd 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79 _Agg.** property
1e9ce 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 on the expressi
1e9cf 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a on is set..**.**
1e9d0 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 An error messag
1e9d1 65 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61 e is left in pPa
1e9d2 72 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 rse if anything
1e9d3 69 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20 6e is amiss. The n
1e9d4 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f umber.** if erro
1e9d5 72 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a rs is returned..
1e9d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1e9d7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
1e9d8 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20 0a olveExprNames( .
1e9d9 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1e9da 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d NC, /* Nam
1e9db 65 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c 76 espace to resolv
1e9dc 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e e expressions in
1e9dd 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 . */. Expr *pEx
1e9de 70 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f pr /
1e9df 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
1e9e0 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e to be analyzed.
1e9e1 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76 */.){. int sav
1e9e2 65 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b edHasAgg;. Walk
1e9e3 65 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45 78 er w;.. if( pEx
1e9e4 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 pr==0 ) return 0
1e9e5 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ;.#if SQLITE_MAX
1e9e6 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 _EXPR_DEPTH>0.
1e9e7 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 {. Parse *pPa
1e9e8 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 rse = pNC->pPars
1e9e9 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 e;. if( sqlit
1e9ea 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
1e9eb 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d t(pParse, pExpr-
1e9ec 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50 >nHeight+pNC->pP
1e9ed 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 29 arse->nHeight) )
1e9ee 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
1e9ef 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 ;. }. pPar
1e9f0 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 se->nHeight += p
1e9f1 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 Expr->nHeight;.
1e9f2 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 65 }.#endif. save
1e9f3 64 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68 dHasAgg = pNC->h
1e9f4 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 asAgg;. pNC->ha
1e9f5 73 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45 sAgg = 0;. w.xE
1e9f6 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 xprCallback = re
1e9f7 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 solveExprStep;.
1e9f8 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 w.xSelectCallba
1e9f9 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 ck = resolveSele
1e9fa 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 ctStep;. w.pPar
1e9fb 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1e9fc 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e ;. w.u.pNC = pN
1e9fd 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b C;. sqlite3Walk
1e9fe 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b Expr(&w, pExpr);
1e9ff 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
1ea00 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 70 EXPR_DEPTH>0. p
1ea01 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 NC->pParse->nHei
1ea02 67 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48 ght -= pExpr->nH
1ea03 65 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20 eight;.#endif.
1ea04 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20 if( pNC->nErr>0
1ea05 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 ){. ExprSetPr
1ea06 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1ea07 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 _Error);. }. i
1ea08 66 28 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 f( pNC->hasAgg )
1ea09 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f {. ExprSetPro
1ea0a 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1ea0b 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Agg);. }else if
1ea0c 28 20 73 61 76 65 64 48 61 73 41 67 67 20 29 7b ( savedHasAgg ){
1ea0d 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 . pNC->hasAgg
1ea0e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
1ea0f 72 6e 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 rn ExprHasProper
1ea10 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 ty(pExpr, EP_Err
1ea11 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 or);.}.../*.** R
1ea12 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 esolve all names
1ea13 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 69 in all expressi
1ea14 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 ons of a SELECT
1ea15 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 and in all.** de
1ea16 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 68 65 20 cendents of the
1ea17 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e SELECT, includin
1ea18 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 g compounds off
1ea19 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a of p->pPrior,.**
1ea1a 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 65 subqueries in e
1ea1b 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 xpressions, and
1ea1c 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64 20 subqueries used
1ea1d 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a as FROM clause.*
1ea1e 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 * terms..**.** S
1ea1f 65 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 ee sqlite3Resolv
1ea20 65 45 78 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 eExprNames() for
1ea21 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f a description o
1ea22 66 20 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a f the kinds of.*
1ea23 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e * transformation
1ea24 73 20 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a s that occur..**
1ea25 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 .** All SELECT s
1ea26 74 61 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 tatements should
1ea27 20 68 61 76 65 20 62 65 65 6e 20 65 78 70 61 6e have been expan
1ea28 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c ded using.** sql
1ea29 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 ite3SelectExpand
1ea2a 28 29 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f () prior to invo
1ea2b 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e king this routin
1ea2c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1ea2d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1ea2e 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 3ResolveSelectNa
1ea2f 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 mes(. Parse *pP
1ea30 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a arse, /*
1ea31 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 The parser cont
1ea32 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ext */. Select
1ea33 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1ea34 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
1ea35 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f atement being co
1ea36 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f ded. */. NameCo
1ea37 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 ntext *pOuterNC
1ea38 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 /* Name context
1ea39 20 66 6f 72 20 70 61 72 65 6e 74 20 53 45 4c 45 for parent SELE
1ea3a 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a CT statement */.
1ea3b 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a ){. Walker w;..
1ea3c 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 assert( p!=0 )
1ea3d 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 ;. w.xExprCallb
1ea3e 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 ack = resolveExp
1ea3f 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 rStep;. w.xSele
1ea40 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 ctCallback = res
1ea41 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a olveSelectStep;.
1ea42 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 w.pParse = pPa
1ea43 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d rse;. w.u.pNC =
1ea44 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c pOuterNC;. sql
1ea45 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 ite3WalkSelect(&
1ea46 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a w, p);.}../*****
1ea47 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1ea48 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a resolve.c *****
1ea49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea4b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1ea4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1ea4d 66 69 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a file expr.c ****
1ea4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea50 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1ea51 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
1ea52 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
1ea53 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1ea54 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1ea55 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1ea56 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1ea57 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1ea58 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1ea59 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1ea5a 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1ea5b 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1ea5c 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1ea5d 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1ea5e 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1ea5f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1ea60 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1ea61 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1ea62 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1ea63 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1ea64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ea68 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
1ea69 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 ntains routines
1ea6a 75 73 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 used for analyzi
1ea6b 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 ng expressions a
1ea6c 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 nd.** for genera
1ea6d 74 69 6e 67 20 56 44 42 45 20 63 6f 64 65 20 74 ting VDBE code t
1ea6e 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 65 78 hat evaluates ex
1ea6f 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c pressions in SQL
1ea70 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ite..**.** $Id:
1ea71 65 78 70 72 2e 63 2c 76 20 31 2e 34 33 32 20 32 expr.c,v 1.432 2
1ea72 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 35 37 3a 009/05/06 18:57:
1ea73 31 30 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 10 shane Exp $.*
1ea74 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
1ea75 74 68 65 20 27 61 66 66 69 6e 69 74 79 27 20 6f the 'affinity' o
1ea76 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1ea77 20 70 45 78 70 72 20 69 66 20 61 6e 79 2e 0a 2a pExpr if any..*
1ea78 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 *.** If pExpr is
1ea79 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20 72 65 66 a column, a ref
1ea7a 65 72 65 6e 63 65 20 74 6f 20 61 20 63 6f 6c 75 erence to a colu
1ea7b 6d 6e 20 76 69 61 20 61 6e 20 27 41 53 27 20 61 mn via an 'AS' a
1ea7c 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61 20 73 75 lias,.** or a su
1ea7d 62 2d 73 65 6c 65 63 74 20 77 69 74 68 20 61 20 b-select with a
1ea7e 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 65 column as the re
1ea7f 74 75 72 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e turn value, then
1ea80 20 74 68 65 20 0a 2a 2a 20 61 66 66 69 6e 69 74 the .** affinit
1ea81 79 20 6f 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e y of that column
1ea82 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
1ea83 68 65 72 77 69 73 65 2c 20 30 78 30 30 20 69 73 herwise, 0x00 is
1ea84 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 69 6e returned,.** in
1ea85 64 69 63 61 74 69 6e 67 20 6e 6f 20 61 66 66 69 dicating no affi
1ea86 6e 69 74 79 20 66 6f 72 20 74 68 65 20 65 78 70 nity for the exp
1ea87 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69 ression..**.** i
1ea88 2e 65 2e 20 74 68 65 20 57 48 45 52 45 20 63 6c .e. the WHERE cl
1ea89 61 75 73 65 20 65 78 70 72 65 73 73 73 69 6f 6e ause expresssion
1ea8a 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s in the followi
1ea8b 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6c ng statements al
1ea8c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20 61 66 66 l.** have an aff
1ea8d 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 inity:.**.** CRE
1ea8e 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 29 3b ATE TABLE t1(a);
1ea8f 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 52 4f .** SELECT * FRO
1ea90 4d 20 74 31 20 57 48 45 52 45 20 61 3b 0a 2a 2a M t1 WHERE a;.**
1ea91 20 53 45 4c 45 43 54 20 61 20 41 53 20 62 20 46 SELECT a AS b F
1ea92 52 4f 4d 20 74 31 20 57 48 45 52 45 20 62 3b 0a ROM t1 WHERE b;.
1ea93 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d ** SELECT * FROM
1ea94 20 74 31 20 57 48 45 52 45 20 28 73 65 6c 65 63 t1 WHERE (selec
1ea95 74 20 61 20 66 72 6f 6d 20 74 31 29 3b 0a 2a 2f t a from t1);.*/
1ea96 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ea97 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 70 72 char sqlite3Expr
1ea98 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 Affinity(Expr *p
1ea99 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 6f 70 20 Expr){. int op
1ea9a 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 = pExpr->op;. i
1ea9b 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 f( op==TK_SELECT
1ea9c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1ea9d 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f pExpr->flags&EP_
1ea9e 78 49 73 53 65 6c 65 63 74 20 29 3b 0a 20 20 20 xIsSelect );.
1ea9f 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 return sqlite3E
1eaa0 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 xprAffinity(pExp
1eaa1 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 r->x.pSelect->pE
1eaa2 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 List->a[0].pExpr
1eaa3 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 );. }.#ifndef S
1eaa4 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a QLITE_OMIT_CAST.
1eaa5 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 if( op==TK_CAS
1eaa6 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 T ){. return
1eaa7 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 sqlite3AffinityT
1eaa8 79 70 65 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 ype(&pExpr->toke
1eaa9 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 n);. }.#endif.
1eaaa 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 if( (op==TK_AGG
1eaab 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 _COLUMN || op==T
1eaac 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d K_COLUMN || op==
1eaad 54 4b 5f 52 45 47 49 53 54 45 52 29 20 0a 20 20 TK_REGISTER) .
1eaae 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 && pExpr->pTab!
1eaaf 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 6f =0. ){. /* o
1eab0 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 p==TK_REGISTER &
1eab1 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 & pExpr->pTab!=0
1eab2 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 45 happens when pE
1eab3 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c xpr was original
1eab4 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 ly. ** a TK_C
1eab5 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 72 OLUMN but was pr
1eab6 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 eviously evaluat
1eab7 65 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 6e ed and cached in
1eab8 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 a register */.
1eab9 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 78 70 72 int j = pExpr
1eaba 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 ->iColumn;. i
1eabb 66 28 20 6a 3c 30 20 29 20 72 65 74 75 72 6e 20 f( j<0 ) return
1eabc 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
1eabd 45 52 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ER;. assert(
1eabe 70 45 78 70 72 2d 3e 70 54 61 62 20 26 26 20 6a pExpr->pTab && j
1eabf 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 6e 43 <pExpr->pTab->nC
1eac0 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ol );. return
1eac1 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 pExpr->pTab->aC
1eac2 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b 0a ol[j].affinity;.
1eac3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 78 }. return pEx
1eac4 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d 0a pr->affinity;.}.
1eac5 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 ./*.** Set the c
1eac6 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1eac7 65 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e e for expression
1eac8 20 70 45 78 70 72 20 74 6f 20 62 65 20 74 68 65 pExpr to be the
1eac9 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 collating.** se
1eaca 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 79 20 quence named by
1eacb 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 72 6e pToken. Return
1eacc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1eacd 65 20 72 65 76 69 73 65 64 20 65 78 70 72 65 73 e revised expres
1eace 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c sion..** The col
1eacf 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
1ead0 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 is marked as "ex
1ead1 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20 74 68 plicit" using th
1ead2 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 0a e EP_ExpCollate.
1ead3 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78 70 ** flag. An exp
1ead4 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67 20 licit collating
1ead5 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f 76 sequence will ov
1ead6 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74 0a erride implicit.
1ead7 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 ** collating seq
1ead8 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 uences..*/.SQLIT
1ead9 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
1eada 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f sqlite3ExprSetCo
1eadb 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ll(Parse *pParse
1eadc 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 , Expr *pExpr, T
1eadd 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65 29 oken *pCollName)
1eade 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 {. char *zColl
1eadf 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1eae0 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 /* Dequoted name
1eae1 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 of collation se
1eae2 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c quence */. Coll
1eae3 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 71 Seq *pColl;. sq
1eae4 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
1eae5 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 se->db;. zColl
1eae6 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1eae7 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 6c mToken(db, pColl
1eae8 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 45 78 Name);. if( pEx
1eae9 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a 20 pr && zColl ){.
1eaea 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 pColl = sqlit
1eaeb 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 e3LocateCollSeq(
1eaec 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d pParse, zColl, -
1eaed 31 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 1);. if( pCol
1eaee 6c 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 l ){. pExpr
1eaef 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b ->pColl = pColl;
1eaf0 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c . pExpr->fl
1eaf1 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c ags |= EP_ExpCol
1eaf2 6c 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a late;. }. }.
1eaf3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1eaf4 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 72 65 db, zColl);. re
1eaf5 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 0a 2f turn pExpr;.}../
1eaf6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1eaf7 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f default collatio
1eaf8 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 n sequence for t
1eaf9 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 he expression pE
1eafa 78 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65 72 65 xpr. If.** there
1eafb 20 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 20 63 is no default c
1eafc 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c 20 72 ollation type, r
1eafd 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 eturn 0..*/.SQLI
1eafe 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
1eaff 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 eq *sqlite3ExprC
1eb00 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 ollSeq(Parse *pP
1eb01 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
1eb02 72 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 r){. CollSeq *p
1eb03 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 45 78 70 72 Coll = 0;. Expr
1eb04 20 2a 70 20 3d 20 70 45 78 70 72 3b 0a 20 20 77 *p = pExpr;. w
1eb05 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 69 hile( p ){. i
1eb06 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c nt op;. pColl
1eb07 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 = p->pColl;.
1eb08 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 62 72 65 if( pColl ) bre
1eb09 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e ak;. op = p->
1eb0a 6f 70 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 3d op;. if( (op=
1eb0b 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c =TK_AGG_COLUMN |
1eb0c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 | op==TK_COLUMN
1eb0d 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 || op==TK_REGIST
1eb0e 45 52 29 20 26 26 20 70 2d 3e 70 54 61 62 21 3d ER) && p->pTab!=
1eb0f 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70 0 ){. /* op
1eb10 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26 ==TK_REGISTER &&
1eb11 20 70 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 p->pTab!=0 happ
1eb12 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 ens when pExpr w
1eb13 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 as originally.
1eb14 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 ** a TK_COLU
1eb15 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 76 69 MN but was previ
1eb16 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20 ously evaluated
1eb17 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 61 20 and cached in a
1eb18 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 register */.
1eb19 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
1eb1a 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a oll;. int j
1eb1b 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 = p->iColumn;.
1eb1c 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b if( j>=0 ){
1eb1d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1eb1e 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
1eb1f 62 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c b;. zColl
1eb20 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c = p->pTab->aCol
1eb21 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 [j].zColl;.
1eb22 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 pColl = sqlit
1eb23 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 e3FindCollSeq(db
1eb24 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c , ENC(db), zColl
1eb25 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 , -1, 0);.
1eb26 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d pExpr->pColl =
1eb27 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a pColl;. }.
1eb28 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1eb29 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54 }. if( op!=T
1eb2a 4b 5f 43 41 53 54 20 26 26 20 6f 70 21 3d 54 4b K_CAST && op!=TK
1eb2b 5f 55 50 4c 55 53 20 29 7b 0a 20 20 20 20 20 20 _UPLUS ){.
1eb2c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1eb2d 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 p = p->pLeft;.
1eb2e 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1eb2f 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 CheckCollSeq(pPa
1eb30 72 73 65 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a rse, pColl) ){ .
1eb31 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 pColl = 0;.
1eb32 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c }. return pCol
1eb33 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 l;.}../*.** pExp
1eb34 72 20 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 r is an operand
1eb35 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 of a comparison
1eb36 6f 70 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20 operator. aff2
1eb37 69 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 is the.** type a
1eb38 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 6f ffinity of the o
1eb39 74 68 65 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 ther operand. T
1eb3a 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
1eb3b 72 6e 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 rns the.** type
1eb3c 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1eb3d 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
1eb3e 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1eb3f 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c operator..*/.SQL
1eb40 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
1eb41 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 sqlite3CompareA
1eb42 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 ffinity(Expr *pE
1eb43 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 7b xpr, char aff2){
1eb44 0a 20 20 63 68 61 72 20 61 66 66 31 20 3d 20 73 . char aff1 = s
1eb45 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 qlite3ExprAffini
1eb46 74 79 28 70 45 78 70 72 29 3b 0a 20 20 69 66 28 ty(pExpr);. if(
1eb47 20 61 66 66 31 20 26 26 20 61 66 66 32 20 29 7b aff1 && aff2 ){
1eb48 0a 20 20 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 . /* Both sid
1eb49 65 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 es of the compar
1eb4a 69 73 6f 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 ison are columns
1eb4b 2e 20 49 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d . If one has num
1eb4c 65 72 69 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 eric. ** affi
1eb4d 6e 69 74 79 2c 20 75 73 65 20 74 68 61 74 2e 20 nity, use that.
1eb4e 4f 74 68 65 72 77 69 73 65 20 75 73 65 20 6e 6f Otherwise use no
1eb4f 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a affinity.. *
1eb50 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 /. if( sqlite
1eb51 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 3IsNumericAffini
1eb52 74 79 28 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 ty(aff1) || sqli
1eb53 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 te3IsNumericAffi
1eb54 6e 69 74 79 28 61 66 66 32 29 20 29 7b 0a 20 20 nity(aff2) ){.
1eb55 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1eb56 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 E_AFF_NUMERIC;.
1eb57 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1eb58 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 return SQLITE_AF
1eb59 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 F_NONE;. }.
1eb5a 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66 31 20 }else if( !aff1
1eb5b 26 26 20 21 61 66 66 32 20 29 7b 0a 20 20 20 20 && !aff2 ){.
1eb5c 2f 2a 20 4e 65 69 74 68 65 72 20 73 69 64 65 20 /* Neither side
1eb5d 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f of the compariso
1eb5e 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 n is a column.
1eb5f 43 6f 6d 70 61 72 65 20 74 68 65 0a 20 20 20 20 Compare the.
1eb60 2a 2a 20 72 65 73 75 6c 74 73 20 64 69 72 65 63 ** results direc
1eb61 74 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 tly.. */.
1eb62 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 return SQLITE_AF
1eb63 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b F_NONE;. }else{
1eb64 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 . /* One side
1eb65 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 is a column, th
1eb66 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 e other is not.
1eb67 55 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 Use the columns
1eb68 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 affinity. */.
1eb69 20 61 73 73 65 72 74 28 20 61 66 66 31 3d 3d 30 assert( aff1==0
1eb6a 20 7c 7c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 || aff2==0 );.
1eb6b 20 20 20 72 65 74 75 72 6e 20 28 61 66 66 31 20 return (aff1
1eb6c 2b 20 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a + aff2);. }.}..
1eb6d 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 /*.** pExpr is a
1eb6e 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 comparison oper
1eb6f 61 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 74 68 ator. Return th
1eb70 65 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 e type affinity
1eb71 74 68 61 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 that should.** b
1eb72 65 20 61 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 e applied to bot
1eb73 68 20 6f 70 65 72 61 6e 64 73 20 70 72 69 6f 72 h operands prior
1eb74 20 74 6f 20 64 6f 69 6e 67 20 74 68 65 20 63 6f to doing the co
1eb75 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 mparison..*/.sta
1eb76 74 69 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69 tic char compari
1eb77 73 6f 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72 sonAffinity(Expr
1eb78 20 2a 70 45 78 70 72 29 7b 0a 20 20 63 68 61 72 *pExpr){. char
1eb79 20 61 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 aff;. assert(
1eb7a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 pExpr->op==TK_EQ
1eb7b 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 || pExpr->op==T
1eb7c 4b 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f K_IN || pExpr->o
1eb7d 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 p==TK_LT ||.
1eb7e 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d pExpr->op=
1eb7f 3d 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d =TK_GT || pExpr-
1eb80 3e 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 >op==TK_GE || pE
1eb81 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c xpr->op==TK_LE |
1eb82 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 |. pExp
1eb83 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a r->op==TK_NE );.
1eb84 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1eb85 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61 66 66 20 >pLeft );. aff
1eb86 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 = sqlite3ExprAff
1eb87 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 4c 65 inity(pExpr->pLe
1eb88 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 ft);. if( pExpr
1eb89 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 ->pRight ){.
1eb8a 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d aff = sqlite3Com
1eb8b 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 78 pareAffinity(pEx
1eb8c 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 66 66 29 pr->pRight, aff)
1eb8d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 45 78 ;. }else if( Ex
1eb8e 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
1eb8f 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
1eb90 74 29 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 t) ){. aff =
1eb91 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 sqlite3CompareAf
1eb92 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e finity(pExpr->x.
1eb93 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d pSelect->pEList-
1eb94 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 61 66 66 >a[0].pExpr, aff
1eb95 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 );. }else if( !
1eb96 61 66 66 20 29 7b 0a 20 20 20 20 61 66 66 20 3d aff ){. aff =
1eb97 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 SQLITE_AFF_NONE
1eb98 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 ;. }. return a
1eb99 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 ff;.}../*.** pEx
1eb9a 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69 73 pr is a comparis
1eb9b 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 65 on expression, e
1eb9c 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20 49 4e 28 g. '=', '<', IN(
1eb9d 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20 69 64 78 ...) etc..** idx
1eb9e 5f 61 66 66 69 6e 69 74 79 20 69 73 20 74 68 65 _affinity is the
1eb9f 20 61 66 66 69 6e 69 74 79 20 6f 66 20 61 6e 20 affinity of an
1eba0 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 2e 20 indexed column.
1eba1 52 65 74 75 72 6e 20 74 72 75 65 0a 2a 2a 20 69 Return true.** i
1eba2 66 20 74 68 65 20 69 6e 64 65 78 20 77 69 74 68 f the index with
1eba3 20 61 66 66 69 6e 69 74 79 20 69 64 78 5f 61 66 affinity idx_af
1eba4 66 69 6e 69 74 79 20 6d 61 79 20 62 65 20 75 73 finity may be us
1eba5 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a ed to implement.
1eba6 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f ** the compariso
1eba7 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a 2f 0a 53 n in pExpr..*/.S
1eba8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1eba9 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 t sqlite3IndexAf
1ebaa 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 finityOk(Expr *p
1ebab 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 Expr, char idx_a
1ebac 66 66 69 6e 69 74 79 29 7b 0a 20 20 63 68 61 72 ffinity){. char
1ebad 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f aff = compariso
1ebae 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 nAffinity(pExpr)
1ebaf 3b 0a 20 20 73 77 69 74 63 68 28 20 61 66 66 20 ;. switch( aff
1ebb0 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
1ebb1 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 20 20 20 TE_AFF_NONE:.
1ebb2 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1ebb3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 case SQLITE_AFF
1ebb4 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 72 65 74 _TEXT:. ret
1ebb5 75 72 6e 20 69 64 78 5f 61 66 66 69 6e 69 74 79 urn idx_affinity
1ebb6 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 ==SQLITE_AFF_TEX
1ebb7 54 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a T;. default:.
1ebb8 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c return sql
1ebb9 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 ite3IsNumericAff
1ebba 69 6e 69 74 79 28 69 64 78 5f 61 66 66 69 6e 69 inity(idx_affini
1ebbb 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ty);. }.}../*.*
1ebbc 2a 20 52 65 74 75 72 6e 20 74 68 65 20 50 35 20 * Return the P5
1ebbd 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f 75 6c value that shoul
1ebbe 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 61 20 d be used for a
1ebbf 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f binary compariso
1ebc0 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 4f 50 5f n.** opcode (OP_
1ebc1 45 71 2c 20 4f 50 5f 47 65 20 65 74 63 2e 29 20 Eq, OP_Ge etc.)
1ebc2 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 used to compare
1ebc3 70 45 78 70 72 31 20 61 6e 64 20 70 45 78 70 72 pExpr1 and pExpr
1ebc4 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2..*/.static u8
1ebc5 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 binaryCompareP5(
1ebc6 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20 45 78 Expr *pExpr1, Ex
1ebc7 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e 74 20 pr *pExpr2, int
1ebc8 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 75 jumpIfNull){. u
1ebc9 38 20 61 66 66 20 3d 20 28 63 68 61 72 29 73 71 8 aff = (char)sq
1ebca 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 lite3ExprAffinit
1ebcb 79 28 70 45 78 70 72 32 29 3b 0a 20 20 61 66 66 y(pExpr2);. aff
1ebcc 20 3d 20 28 75 38 29 73 71 6c 69 74 65 33 43 6f = (u8)sqlite3Co
1ebcd 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 mpareAffinity(pE
1ebce 78 70 72 31 2c 20 61 66 66 29 20 7c 20 28 75 38 xpr1, aff) | (u8
1ebcf 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20 20 72 )jumpIfNull;. r
1ebd0 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a eturn aff;.}../*
1ebd1 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
1ebd2 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c 6c nter to the coll
1ebd3 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 ation sequence t
1ebd4 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 hat should be us
1ebd5 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e 61 72 ed by.** a binar
1ebd6 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 y comparison ope
1ebd7 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e 67 20 rator comparing
1ebd8 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 pLeft and pRight
1ebd9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
1ebda 65 66 74 20 68 61 6e 64 20 65 78 70 72 65 73 73 eft hand express
1ebdb 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 ion has a collat
1ebdc 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 79 70 ing sequence typ
1ebdd 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a e, then it is.**
1ebde 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69 73 65 used. Otherwise
1ebdf 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 the collation s
1ebe0 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 equence for the
1ebe1 72 69 67 68 74 20 68 61 6e 64 20 65 78 70 72 65 right hand expre
1ebe2 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73 65 64 ssion.** is used
1ebe3 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 6c 74 , or the default
1ebe4 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e 65 69 (BINARY) if nei
1ebe5 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 ther expression
1ebe6 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 0a has a collating.
1ebe7 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 41 ** type..**.** A
1ebe8 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 20 28 rgument pRight (
1ebe9 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 20 6d but not pLeft) m
1ebea 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69 ay be a null poi
1ebeb 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61 nter. In this ca
1ebec 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 se,.** it is not
1ebed 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a 2f 0a considered..*/.
1ebee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
1ebef 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 42 ollSeq *sqlite3B
1ebf0 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c inaryCompareColl
1ebf1 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a 70 50 Seq(. Parse *pP
1ebf2 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a 70 arse, . Expr *p
1ebf3 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20 2a 70 Left, . Expr *p
1ebf4 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c 6c 53 Right.){. CollS
1ebf5 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61 73 73 eq *pColl;. ass
1ebf6 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20 ert( pLeft );.
1ebf7 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 if( pLeft->flags
1ebf8 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 & EP_ExpCollate
1ebf9 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1ebfa 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a pLeft->pColl );.
1ebfb 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 pColl = pLef
1ebfc 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c 73 t->pColl;. }els
1ebfd 65 20 69 66 28 20 70 52 69 67 68 74 20 26 26 20 e if( pRight &&
1ebfe 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 pRight->flags &
1ebff 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b EP_ExpCollate ){
1ec00 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69 . assert( pRi
1ec01 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20 ght->pColl );.
1ec02 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74 pColl = pRight
1ec03 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c 73 65 ->pColl;. }else
1ec04 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 {. pColl = sq
1ec05 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 lite3ExprCollSeq
1ec06 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29 3b (pParse, pLeft);
1ec07 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 . if( !pColl
1ec08 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d ){. pColl =
1ec09 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c sqlite3ExprColl
1ec0a 53 65 71 28 70 50 61 72 73 65 2c 20 70 52 69 67 Seq(pParse, pRig
1ec0b 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ht);. }. }.
1ec0c 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d return pColl;.}
1ec0d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1ec0e 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 66 6f the operands fo
1ec0f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f r a comparison o
1ec10 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66 6f 72 peration. Befor
1ec11 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 e.** generating
1ec12 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65 61 63 the code for eac
1ec13 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74 20 74 h operand, set t
1ec14 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a 2a 20 he EP_AnyAff.**
1ec15 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78 70 72 flag on the expr
1ec16 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 ession so that i
1ec17 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 t will be able t
1ec18 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61 63 68 o used a.** cach
1ec19 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 ed column value
1ec1a 74 68 61 74 20 68 61 73 20 70 72 65 76 69 6f 75 that has previou
1ec1b 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e sly undergone an
1ec1c 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63 68 61 .** affinity cha
1ec1d 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 nge..*/.static v
1ec1e 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f oid codeCompareO
1ec1f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72 73 65 perands(. Parse
1ec20 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 *pParse, /*
1ec21 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 Parsing and code
1ec22 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 generating cont
1ec23 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ext */. Expr *p
1ec24 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 Left, /* Th
1ec25 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a e left operand *
1ec26 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c 65 66 /. int *pRegLef
1ec27 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 t, /* Registe
1ec28 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f 70 65 r where left ope
1ec29 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64 20 2a rand is stored *
1ec2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 4c 65 /. int *pFreeLe
1ec2b 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20 74 68 ft, /* Free th
1ec2c 69 73 20 72 65 67 69 73 74 65 72 20 77 68 65 6e is register when
1ec2d 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70 72 20 done */. Expr
1ec2e 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 *pRight, /*
1ec2f 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e The right operan
1ec30 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 d */. int *pReg
1ec31 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65 67 69 Right, /* Regi
1ec32 73 74 65 72 20 77 68 65 72 65 20 72 69 67 68 74 ster where right
1ec33 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 operand is stor
1ec34 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 ed */. int *pFr
1ec35 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57 72 69 eeRight /* Wri
1ec36 74 65 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 te temp register
1ec37 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65 72 61 for right opera
1ec38 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 nd there */.){.
1ec39 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d 3e 6f while( pLeft->o
1ec3a 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 4c p==TK_UPLUS ) pL
1ec3b 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 4c 65 eft = pLeft->pLe
1ec3c 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66 6c 61 ft;. pLeft->fla
1ec3d 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66 66 3b gs |= EP_AnyAff;
1ec3e 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d 20 73 . *pRegLeft = s
1ec3f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
1ec40 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 mp(pParse, pLeft
1ec41 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a 20 20 , pFreeLeft);.
1ec42 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d 3e 6f while( pRight->o
1ec43 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 52 p==TK_UPLUS ) pR
1ec44 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d 3e 70 ight = pRight->p
1ec45 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74 2d 3e Left;. pRight->
1ec46 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 flags |= EP_AnyA
1ec47 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67 68 74 ff;. *pRegRight
1ec48 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1ec49 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
1ec4a 52 69 67 68 74 2c 20 70 46 72 65 65 52 69 67 68 Right, pFreeRigh
1ec4b 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e t);.}../*.** Gen
1ec4c 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 erate code for a
1ec4d 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 comparison oper
1ec4e 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ator..*/.static
1ec4f 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 int codeCompare(
1ec50 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1ec51 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 , /* The pars
1ec52 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20 67 65 ing (and code ge
1ec53 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74 65 78 nerating) contex
1ec54 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 t */. Expr *pLe
1ec55 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ft, /* The
1ec56 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a left operand */.
1ec57 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 Expr *pRight,
1ec58 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 /* The right
1ec59 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
1ec5a 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20 20 20 t opcode,
1ec5b 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 73 6f /* The compariso
1ec5c 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e n opcode */. in
1ec5d 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32 2c 20 t in1, int in2,
1ec5e 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
1ec5f 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a 2f 0a ing operands */.
1ec60 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20 20 20 int dest,
1ec61 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 /* Jump here
1ec62 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a 20 20 if true. */.
1ec63 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 20 int jumpIfNull
1ec64 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 6a 75 /* If true, ju
1ec65 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 mp if either ope
1ec66 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a rand is NULL */.
1ec67 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20 20 69 ){. int p5;. i
1ec68 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c 6c 53 nt addr;. CollS
1ec69 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20 3d 20 eq *p4;.. p4 =
1ec6a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d sqlite3BinaryCom
1ec6b 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 pareCollSeq(pPar
1ec6c 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 se, pLeft, pRigh
1ec6d 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e 61 72 t);. p5 = binar
1ec6e 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65 66 74 yCompareP5(pLeft
1ec6f 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70 49 66 , pRight, jumpIf
1ec70 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20 3d 20 Null);. addr =
1ec71 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1ec72 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 4(pParse->pVdbe,
1ec73 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20 64 65 opcode, in2, de
1ec74 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20 20 20 st, in1,.
1ec75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ec76 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c 20 50 (void*)p4, P
1ec77 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 73 71 4_COLLSEQ);. sq
1ec78 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1ec79 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 5(pParse->pVdbe,
1ec7a 20 28 75 38 29 70 35 29 3b 0a 20 20 69 66 28 20 (u8)p5);. if(
1ec7b 28 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 (p5 & SQLITE_AFF
1ec7c 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45 5f 41 _MASK)!=SQLITE_A
1ec7d 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 73 FF_NONE ){. s
1ec7e 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
1ec7f 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 ffinityChange(pP
1ec80 61 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b 0a 20 arse, in1, 1);.
1ec81 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
1ec82 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
1ec83 65 28 70 50 61 72 73 65 2c 20 69 6e 32 2c 20 31 e(pParse, in2, 1
1ec84 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1ec85 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c addr;.}..#if SQL
1ec86 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 ITE_MAX_EXPR_DEP
1ec87 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b TH>0./*.** Check
1ec88 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20 6e that argument n
1ec89 48 65 69 67 68 74 20 69 73 20 6c 65 73 73 20 74 Height is less t
1ec8a 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
1ec8b 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 65 the maximum.** e
1ec8c 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74 68 20 xpression depth
1ec8d 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74 20 69 allowed. If it i
1ec8e 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61 6e 20 s not, leave an
1ec8f 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
1ec90 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 .** pParse..*/.S
1ec91 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1ec92 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 t sqlite3ExprChe
1ec93 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 20 2a ckHeight(Parse *
1ec94 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 48 65 69 pParse, int nHei
1ec95 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ght){. int rc =
1ec96 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
1ec97 74 20 6d 78 48 65 69 67 68 74 20 3d 20 70 50 61 t mxHeight = pPa
1ec98 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b rse->db->aLimit[
1ec99 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 SQLITE_LIMIT_EXP
1ec9a 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66 28 20 R_DEPTH];. if(
1ec9b 6e 48 65 69 67 68 74 3e 6d 78 48 65 69 67 68 74 nHeight>mxHeight
1ec9c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1ec9d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1ec9e 0a 20 20 20 20 20 20 20 22 45 78 70 72 65 73 73 . "Express
1ec9f 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f 6f 20 ion tree is too
1eca0 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d 20 64 large (maximum d
1eca1 65 70 74 68 20 25 64 29 22 2c 20 6d 78 48 65 69 epth %d)", mxHei
1eca2 67 68 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 ght. );. r
1eca3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1eca4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1eca5 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c c;.}../* The fol
1eca6 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66 75 6e lowing three fun
1eca7 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 74 4f 66 ctions, heightOf
1eca8 45 78 70 72 28 29 2c 20 68 65 69 67 68 74 4f 66 Expr(), heightOf
1eca9 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20 61 6e ExprList().** an
1ecaa 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 d heightOfSelect
1ecab 28 29 2c 20 61 72 65 20 75 73 65 64 20 74 6f 20 (), are used to
1ecac 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 61 determine the ma
1ecad 78 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a 2a 20 ximum height.**
1ecae 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f of any expressio
1ecaf 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 n tree reference
1ecb0 64 20 62 79 20 74 68 65 20 73 74 72 75 63 74 75 d by the structu
1ecb1 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 re passed as the
1ecb2 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65 .** first argume
1ecb3 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 nt..**.** If thi
1ecb4 73 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 s maximum height
1ecb5 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
1ecb6 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c the current val
1ecb7 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f ue pointed.** to
1ecb8 20 62 79 20 70 6e 48 65 69 67 68 74 2c 20 74 68 by pnHeight, th
1ecb9 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
1ecba 65 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 6e er, then set *pn
1ecbb 48 65 69 67 68 74 20 74 6f 20 74 68 61 74 0a 2a Height to that.*
1ecbc 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 * value..*/.stat
1ecbd 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 ic void heightOf
1ecbe 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e Expr(Expr *p, in
1ecbf 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 t *pnHeight){.
1ecc0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 if( p ){. if(
1ecc1 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70 6e 48 p->nHeight>*pnH
1ecc2 65 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 2a eight ){. *
1ecc3 70 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e 6e 48 pnHeight = p->nH
1ecc4 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d eight;. }. }
1ecc5 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 .}.static void h
1ecc6 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 eightOfExprList(
1ecc7 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 ExprList *p, int
1ecc8 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 *pnHeight){. i
1ecc9 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 f( p ){. int
1ecca 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 i;. for(i=0;
1eccb 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 i<p->nExpr; i++)
1eccc 7b 0a 20 20 20 20 20 20 68 65 69 67 68 74 4f 66 {. heightOf
1eccd 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70 45 78 Expr(p->a[i].pEx
1ecce 70 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 pr, pnHeight);.
1eccf 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 }. }.}.stati
1ecd0 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 53 c void heightOfS
1ecd1 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c elect(Select *p,
1ecd2 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b int *pnHeight){
1ecd3 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
1ecd4 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e heightOfExpr(p->
1ecd5 70 57 68 65 72 65 2c 20 70 6e 48 65 69 67 68 74 pWhere, pnHeight
1ecd6 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 );. heightOfE
1ecd7 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20 xpr(p->pHaving,
1ecd8 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 pnHeight);. h
1ecd9 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
1ecda 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67 68 74 29 Limit, pnHeight)
1ecdb 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
1ecdc 70 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c 20 70 pr(p->pOffset, p
1ecdd 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
1ecde 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 ightOfExprList(p
1ecdf 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65 69 67 ->pEList, pnHeig
1ece0 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f ht);. heightO
1ece1 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 fExprList(p->pGr
1ece2 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 68 74 29 oupBy, pnHeight)
1ece3 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
1ece4 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 prList(p->pOrder
1ece5 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 By, pnHeight);.
1ece6 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 heightOfSelec
1ece7 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70 6e 48 t(p->pPrior, pnH
1ece8 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f eight);. }.}../
1ece9 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 *.** Set the Exp
1ecea 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 r.nHeight variab
1eceb 6c 65 20 69 6e 20 74 68 65 20 73 74 72 75 63 74 le in the struct
1ecec 75 72 65 20 70 61 73 73 65 64 20 61 73 20 61 6e ure passed as an
1eced 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 41 .** argument. A
1ecee 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 74 n expression wit
1ecef 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c 20 45 h no children, E
1ecf0 78 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a 2a 2a xpr.pList or .**
1ecf1 20 45 78 70 72 2e 70 53 65 6c 65 63 74 20 6d 65 Expr.pSelect me
1ecf2 6d 62 65 72 20 68 61 73 20 61 20 68 65 69 67 68 mber has a heigh
1ecf3 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74 68 65 t of 1. Any othe
1ecf4 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 r expression.**
1ecf5 68 61 73 20 61 20 68 65 69 67 68 74 20 65 71 75 has a height equ
1ecf6 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 al to the maximu
1ecf7 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e 79 20 m height of any
1ecf8 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66 65 72 65 other .** refere
1ecf9 6e 63 65 64 20 45 78 70 72 20 70 6c 75 73 20 6f nced Expr plus o
1ecfa 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ne..*/.static vo
1ecfb 69 64 20 65 78 70 72 53 65 74 48 65 69 67 68 74 id exprSetHeight
1ecfc 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 6e 74 (Expr *p){. int
1ecfd 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 nHeight = 0;.
1ecfe 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e heightOfExpr(p->
1ecff 70 4c 65 66 74 2c 20 26 6e 48 65 69 67 68 74 29 pLeft, &nHeight)
1ed00 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 ;. heightOfExpr
1ed01 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e 48 65 (p->pRight, &nHe
1ed02 69 67 68 74 29 3b 0a 20 20 69 66 28 20 45 78 70 ight);. if( Exp
1ed03 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 rHasProperty(p,
1ed04 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b EP_xIsSelect) ){
1ed05 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c . heightOfSel
1ed06 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c 65 63 74 ect(p->x.pSelect
1ed07 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d , &nHeight);. }
1ed08 65 6c 73 65 7b 0a 20 20 20 20 68 65 69 67 68 74 else{. height
1ed09 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 78 2e OfExprList(p->x.
1ed0a 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68 74 29 pList, &nHeight)
1ed0b 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48 65 69 67 ;. }. p->nHeig
1ed0c 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b 20 31 ht = nHeight + 1
1ed0d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
1ed0e 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20 he Expr.nHeight
1ed0f 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67 20 74 variable using t
1ed10 68 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74 he exprSetHeight
1ed11 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 0a () function. If.
1ed12 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 69 73 ** the height is
1ed13 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
1ed14 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 e maximum allowe
1ed15 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 d expression dep
1ed16 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 th,.** leave an
1ed17 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e error in pParse.
1ed18 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1ed19 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
1ed1a 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 xprSetHeight(Par
1ed1b 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
1ed1c 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65 74 48 *p){. exprSetH
1ed1d 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71 6c 69 eight(p);. sqli
1ed1e 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 te3ExprCheckHeig
1ed1f 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48 ht(pParse, p->nH
1ed20 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a eight);.}../*.**
1ed21 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 Return the maxi
1ed22 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e mum height of an
1ed23 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 y expression tre
1ed24 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20 e referenced.**
1ed25 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 by the select st
1ed26 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 atement passed a
1ed27 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a s an argument..*
1ed28 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ed29 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 int sqlite3Sele
1ed2a 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65 6c ctExprHeight(Sel
1ed2b 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e ect *p){. int n
1ed2c 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 Height = 0;. he
1ed2d 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20 ightOfSelect(p,
1ed2e 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74 &nHeight);. ret
1ed2f 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23 urn nHeight;.}.#
1ed30 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 65 else. #define e
1ed31 78 70 72 53 65 74 48 65 69 67 68 74 28 79 29 0a xprSetHeight(y).
1ed32 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1ed33 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e _MAX_EXPR_DEPTH>
1ed34 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 0 */../*.** Cons
1ed35 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 truct a new expr
1ed36 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 ession node and
1ed37 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
1ed38 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a to it. Memory.
1ed39 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 ** for this node
1ed3a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
1ed3b 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
1ed3c 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 (). The calling
1ed3d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 function.** is
1ed3e 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
1ed3f 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 making sure the
1ed40 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 node eventually
1ed41 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 53 gets freed..*/.S
1ed42 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
1ed43 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 pr *sqlite3Expr(
1ed44 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
1ed45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
1ed46 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 ndle for sqlite3
1ed47 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 DbMallocZero() (
1ed48 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a may be null) */.
1ed49 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 int op,
1ed4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
1ed4b 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a ression opcode *
1ed4c 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c /. Expr *pLeft,
1ed4d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1ed4e 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 eft operand */.
1ed4f 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 Expr *pRight,
1ed50 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 /* Righ
1ed51 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 t operand */. c
1ed52 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b onst Token *pTok
1ed53 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 en /* Argume
1ed54 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 nt token */.){.
1ed55 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 70 Expr *pNew;. p
1ed56 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
1ed57 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
1ed58 7a 65 6f 66 28 45 78 70 72 29 29 3b 0a 20 20 69 zeof(Expr));. i
1ed59 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 f( pNew==0 ){.
1ed5a 20 20 2f 2a 20 57 68 65 6e 20 6d 61 6c 6c 6f 63 /* When malloc
1ed5b 20 66 61 69 6c 73 2c 20 64 65 6c 65 74 65 20 70 fails, delete p
1ed5c 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e Left and pRight.
1ed5d 20 45 78 70 72 65 73 73 69 6f 6e 73 20 70 61 73 Expressions pas
1ed5e 73 65 64 20 74 6f 20 0a 20 20 20 20 2a 2a 20 74 sed to . ** t
1ed5f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 his function mus
1ed60 74 20 61 6c 77 61 79 73 20 62 65 20 61 6c 6c 6f t always be allo
1ed61 63 61 74 65 64 20 77 69 74 68 20 73 71 6c 69 74 cated with sqlit
1ed62 65 33 45 78 70 72 28 29 20 66 6f 72 20 74 68 69 e3Expr() for thi
1ed63 73 20 0a 20 20 20 20 2a 2a 20 72 65 61 73 6f 6e s . ** reason
1ed64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 . . */. sq
1ed65 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1ed66 64 62 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 db, pLeft);.
1ed67 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1ed68 65 28 64 62 2c 20 70 52 69 67 68 74 29 3b 0a 20 e(db, pRight);.
1ed69 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1ed6a 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 28 75 . pNew->op = (u
1ed6b 38 29 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 8)op;. pNew->pL
1ed6c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 70 eft = pLeft;. p
1ed6d 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 70 52 New->pRight = pR
1ed6e 69 67 68 74 3b 0a 20 20 70 4e 65 77 2d 3e 69 41 ight;. pNew->iA
1ed6f 67 67 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d gg = -1;. pNew-
1ed70 3e 73 70 61 6e 2e 7a 20 3d 20 28 75 38 2a 29 22 >span.z = (u8*)"
1ed71 22 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 ";. if( pToken
1ed72 29 7b 0a 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 ){. int c;.
1ed73 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e assert( pToken
1ed74 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 ->dyn==0 );.
1ed75 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 2a 70 54 pNew->span = *pT
1ed76 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 54 oken;. if( pT
1ed77 6f 6b 65 6e 2d 3e 6e 3e 3d 32 20 0a 20 20 20 20 oken->n>=2 .
1ed78 20 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 && ((c = pT
1ed79 6f 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 oken->z[0])=='\'
1ed7a 27 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 ' || c=='"' || c
1ed7b 3d 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 =='[' || c=='`')
1ed7c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1ed7d 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 3TokenCopy(db, &
1ed7e 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f pNew->token, pTo
1ed7f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ken);. if(
1ed80 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b pNew->token.z ){
1ed81 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 74 . pNew->t
1ed82 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 oken.n = sqlite3
1ed83 44 65 71 75 6f 74 65 28 28 63 68 61 72 2a 29 70 Dequote((char*)p
1ed84 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20 New->token.z);.
1ed85 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1ed86 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 6e 3d 3d 28 75 New->token.n==(u
1ed87 6e 73 69 67 6e 65 64 29 73 71 6c 69 74 65 33 53 nsigned)sqlite3S
1ed88 74 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29 70 trlen30((char*)p
1ed89 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 29 20 29 3b New->token.z) );
1ed8a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1ed8b 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e 65 77 f( c=='"' ) pNew
1ed8c 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44 62 ->flags |= EP_Db
1ed8d 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 7d 65 6c lQuoted;. }el
1ed8e 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e se{. pNew->
1ed8f 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b token = *pToken;
1ed90 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d . }. pNew-
1ed91 3e 74 6f 6b 65 6e 2e 71 75 6f 74 65 64 20 3d 20 >token.quoted =
1ed92 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 0;. }else if( p
1ed93 4c 65 66 74 20 29 7b 0a 20 20 20 20 69 66 28 20 Left ){. if(
1ed94 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 pRight ){.
1ed95 69 66 28 20 70 52 69 67 68 74 2d 3e 73 70 61 6e if( pRight->span
1ed96 2e 64 79 6e 3d 3d 30 20 26 26 20 70 4c 65 66 74 .dyn==0 && pLeft
1ed97 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 29 7b ->span.dyn==0 ){
1ed98 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ed99 45 78 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26 ExprSpan(pNew, &
1ed9a 70 4c 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52 pLeft->span, &pR
1ed9b 69 67 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 ight->span);.
1ed9c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
1ed9d 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 Right->flags & E
1ed9e 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a P_ExpCollate ){.
1ed9f 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c pNew->fl
1eda0 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c ags |= EP_ExpCol
1eda1 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 4e late;. pN
1eda2 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 ew->pColl = pRig
1eda3 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 ht->pColl;.
1eda4 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
1eda5 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 pLeft->flags &
1eda6 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b EP_ExpCollate ){
1eda7 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 . pNew->fla
1eda8 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c gs |= EP_ExpColl
1eda9 61 74 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d ate;. pNew-
1edaa 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e >pColl = pLeft->
1edab 70 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d pColl;. }. }
1edac 0a 0a 20 20 65 78 70 72 53 65 74 48 65 69 67 68 .. exprSetHeigh
1edad 74 28 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 t(pNew);. retur
1edae 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
1edaf 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 Works like sqli
1edb0 74 65 33 45 78 70 72 28 29 20 65 78 63 65 70 74 te3Expr() except
1edb1 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61 that it takes a
1edb2 6e 20 65 78 74 72 61 20 50 61 72 73 65 2a 0a 2a n extra Parse*.*
1edb3 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 6e * argument and n
1edb4 6f 74 69 66 69 65 73 20 74 68 65 20 61 73 73 6f otifies the asso
1edb5 63 69 61 74 65 64 20 63 6f 6e 6e 65 63 74 69 6f ciated connectio
1edb6 6e 20 6f 62 6a 65 63 74 20 69 66 20 6d 61 6c 6c n object if mall
1edb7 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c oc fails..*/.SQL
1edb8 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
1edb9 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a *sqlite3PExpr(.
1edba 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1edbb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
1edbc 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
1edbd 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 int op,
1edbe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
1edbf 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a ression opcode *
1edc0 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c /. Expr *pLeft,
1edc1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1edc2 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 eft operand */.
1edc3 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 Expr *pRight,
1edc4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 /* Righ
1edc5 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 t operand */. c
1edc6 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b onst Token *pTok
1edc7 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 en /* Argume
1edc8 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 nt token */.){.
1edc9 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 Expr *p = sqlit
1edca 65 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64 e3Expr(pParse->d
1edcb 62 2c 20 6f 70 2c 20 70 4c 65 66 74 2c 20 70 52 b, op, pLeft, pR
1edcc 69 67 68 74 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 ight, pToken);.
1edcd 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 if( p ){. sq
1edce 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 lite3ExprCheckHe
1edcf 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e ight(pParse, p->
1edd0 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 nHeight);. }.
1edd1 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
1edd2 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20 ** When doing a
1edd3 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20 79 6f nested parse, yo
1edd4 75 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65 u can include te
1edd5 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73 rms in an expres
1edd6 73 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f sion.** that loo
1edd7 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23 k like this: #
1edd8 31 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 1 #2 ... These
1edd9 74 65 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72 terms refer to r
1edda 65 67 69 73 74 65 72 73 0a 2a 2a 20 69 6e 20 74 egisters.** in t
1eddb 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
1eddc 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e ne. #N is the N
1eddd 2d 74 68 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a -th register..**
1edde 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1eddf 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
1ede0 65 20 70 61 72 73 65 72 20 74 6f 20 64 65 61 6c e parser to deal
1ede1 20 77 69 74 68 20 6f 6e 20 6f 66 20 74 68 6f 73 with on of thos
1ede2 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 20 69 e terms..** It i
1ede3 6d 6d 65 64 69 61 74 65 6c 79 20 67 65 6e 65 72 mmediately gener
1ede4 61 74 65 73 20 63 6f 64 65 20 74 6f 20 73 74 6f ates code to sto
1ede5 72 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 re the value in
1ede6 61 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f a memory locatio
1ede7 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e n..** The return
1ede8 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 s an expression
1ede9 74 68 61 74 20 77 69 6c 6c 20 63 6f 64 65 20 74 that will code t
1edea 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 76 61 o extract the va
1edeb 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 61 74 lue from.** that
1edec 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e memory location
1eded 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 as needed..*/.S
1edee 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
1edef 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73 pr *sqlite3Regis
1edf0 74 65 72 45 78 70 72 28 50 61 72 73 65 20 2a 70 terExpr(Parse *p
1edf1 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 Parse, Token *pT
1edf2 6f 6b 65 6e 29 7b 0a 20 20 56 64 62 65 20 2a 76 oken){. Vdbe *v
1edf3 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
1edf4 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 69 ;. Expr *p;. i
1edf5 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 f( pParse->neste
1edf6 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 d==0 ){. sqli
1edf7 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1edf8 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 se, "near \"%T\"
1edf9 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c : syntax error",
1edfa 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72 65 pToken);. re
1edfb 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70 turn sqlite3PExp
1edfc 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c r(pParse, TK_NUL
1edfd 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d L, 0, 0, 0);. }
1edfe 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 . if( v==0 ) re
1edff 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 73 71 turn 0;. p = sq
1ee00 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 lite3PExpr(pPars
1ee01 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 e, TK_REGISTER,
1ee02 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 0, 0, pToken);.
1ee03 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 if( p==0 ){.
1ee04 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d return 0; /* M
1ee05 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a alloc failed */.
1ee06 20 20 7d 0a 20 20 70 2d 3e 69 54 61 62 6c 65 20 }. p->iTable
1ee07 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 = atoi((char*)&p
1ee08 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 Token->z[1]);.
1ee09 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
1ee0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 ** Join two expr
1ee0b 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e essions using an
1ee0c 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 AND operator.
1ee0d 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73 If either expres
1ee0e 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c sion is.** NULL,
1ee0f 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 then just retur
1ee10 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 n the other expr
1ee11 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ession..*/.SQLIT
1ee12 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
1ee13 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 sqlite3ExprAnd(s
1ee14 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
1ee15 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 *pLeft, Expr *p
1ee16 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c Right){. if( pL
1ee17 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 eft==0 ){. re
1ee18 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d turn pRight;. }
1ee19 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d else if( pRight=
1ee1a 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1ee1b 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b pLeft;. }else{
1ee1c 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
1ee1d 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41 te3Expr(db, TK_A
1ee1e 4e 44 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 ND, pLeft, pRigh
1ee1f 74 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a t, 0);. }.}../*
1ee20 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 .** Set the Expr
1ee21 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74 .span field of t
1ee22 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 he given express
1ee23 69 6f 6e 20 74 6f 20 73 70 61 6e 20 61 6c 6c 0a ion to span all.
1ee24 2a 2a 20 74 65 78 74 20 62 65 74 77 65 65 6e 20 ** text between
1ee25 74 68 65 20 74 77 6f 20 67 69 76 65 6e 20 74 6f the two given to
1ee26 6b 65 6e 73 2e 20 20 42 6f 74 68 20 74 6f 6b 65 kens. Both toke
1ee27 6e 73 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 ns must be point
1ee28 69 6e 67 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 ing.** at the sa
1ee29 6d 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 me string..*/.SQ
1ee2a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1ee2b 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 d sqlite3ExprSpa
1ee2c 6e 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 n(Expr *pExpr, T
1ee2d 6f 6b 65 6e 20 2a 70 4c 65 66 74 2c 20 54 6f 6b oken *pLeft, Tok
1ee2e 65 6e 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 en *pRight){. a
1ee2f 73 73 65 72 74 28 20 70 52 69 67 68 74 21 3d 30 ssert( pRight!=0
1ee30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c );. assert( pL
1ee31 65 66 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 eft!=0 );. if(
1ee32 70 45 78 70 72 20 29 7b 0a 20 20 20 20 70 45 78 pExpr ){. pEx
1ee33 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4c 65 pr->span.z = pLe
1ee34 66 74 2d 3e 7a 3b 0a 20 20 20 20 2f 2a 20 54 68 ft->z;. /* Th
1ee35 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
1ee36 72 74 28 29 20 6d 61 79 20 66 61 69 6c 20 77 68 rt() may fail wh
1ee37 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 en this is calle
1ee38 64 20 0a 20 20 20 20 2a 2a 20 76 69 61 20 73 71 d . ** via sq
1ee39 6c 69 74 65 33 50 45 78 70 72 28 29 2f 73 71 6c lite3PExpr()/sql
1ee3a 69 74 65 33 45 78 70 72 28 29 20 66 72 6f 6d 20 ite3Expr() from
1ee3b 61 64 64 57 68 65 72 65 54 65 72 6d 28 29 2e 20 addWhereTerm().
1ee3c 2a 2f 0a 20 20 20 20 2f 2a 20 61 73 73 65 72 74 */. /* assert
1ee3d 28 70 52 69 67 68 74 2d 3e 7a 20 3e 3d 20 70 4c (pRight->z >= pL
1ee3e 65 66 74 2d 3e 7a 29 3b 20 2a 2f 0a 20 20 20 20 eft->z); */.
1ee3f 70 45 78 70 72 2d 3e 73 70 61 6e 2e 6e 20 3d 20 pExpr->span.n =
1ee40 70 52 69 67 68 74 2d 3e 6e 20 2b 20 28 75 6e 73 pRight->n + (uns
1ee41 69 67 6e 65 64 29 28 70 52 69 67 68 74 2d 3e 7a igned)(pRight->z
1ee42 20 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20 20 - pLeft->z);.
1ee43 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 }.}../*.** Const
1ee44 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 ruct a new expre
1ee45 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 ssion node for a
1ee46 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d function with m
1ee47 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d ultiple.** argum
1ee48 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ents..*/.SQLITE_
1ee49 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
1ee4a 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f lite3ExprFunctio
1ee4b 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c n(Parse *pParse,
1ee4c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
1ee4d 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 , Token *pToken)
1ee4e 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a {. Expr *pNew;.
1ee4f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1ee50 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 pParse->db;. as
1ee51 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a sert( pToken );.
1ee52 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
1ee53 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
1ee54 20 73 69 7a 65 6f 66 28 45 78 70 72 29 20 29 3b sizeof(Expr) );
1ee55 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 . if( pNew==0 )
1ee56 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 {. sqlite3Exp
1ee57 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 rListDelete(db,
1ee58 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 64 pList); /* Avoid
1ee59 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 68 65 memory leak whe
1ee5a 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a n malloc fails *
1ee5b 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a /. return 0;.
1ee5c 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d }. pNew->op =
1ee5d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 TK_FUNCTION;.
1ee5e 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 pNew->x.pList =
1ee5f 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 pList;. assert(
1ee60 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
1ee61 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 y(pNew, EP_xIsSe
1ee62 6c 65 63 74 29 20 29 3b 0a 20 20 61 73 73 65 72 lect) );. asser
1ee63 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d t( pToken->dyn==
1ee64 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61 0 );. pNew->spa
1ee65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 73 n = *pToken;. s
1ee66 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 qlite3TokenCopy(
1ee67 64 62 2c 20 26 70 4e 65 77 2d 3e 74 6f 6b 65 6e db, &pNew->token
1ee68 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c , pToken);. sql
1ee69 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 ite3ExprSetHeigh
1ee6a 74 28 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b t(pParse, pNew);
1ee6b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
1ee6c 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 }../*.** Assign
1ee6d 61 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 a variable numbe
1ee6e 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 r to an expressi
1ee6f 6f 6e 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20 on that encodes
1ee70 61 20 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e a wildcard.** in
1ee71 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 the original SQ
1ee72 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a L statement. .*
1ee73 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63 *.** Wildcards c
1ee74 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 onsisting of a s
1ee75 69 6e 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73 ingle "?" are as
1ee76 73 69 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20 signed the next
1ee77 73 65 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 sequential.** va
1ee78 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a riable number..*
1ee79 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f *.** Wildcards o
1ee7a 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e f the form "?nnn
1ee7b 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 " are assigned t
1ee7c 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e he number "nnn".
1ee7d 20 20 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 We make.** sur
1ee7e 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74 e "nnn" is not t
1ee7f 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61 oo be to avoid a
1ee80 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 denial of servi
1ee81 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a ce attack when.*
1ee82 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d * the SQL statem
1ee83 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 ent comes from a
1ee84 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 n external sourc
1ee85 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 e..**.** Wildcar
1ee86 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 ds of the form "
1ee87 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 20 :aaa" or "$aaa"
1ee88 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 are assigned the
1ee89 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 same number.**
1ee8a 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 as the previous
1ee8b 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
1ee8c 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 same wildcard.
1ee8d 4f 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68 Or if this is th
1ee8e 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 e first.** insta
1ee8f 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 nce of the wildc
1ee90 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 ard, the next se
1ee91 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 quenial variable
1ee92 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 number is.** as
1ee93 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 signed..*/.SQLIT
1ee94 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1ee95 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e qlite3ExprAssign
1ee96 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 VarNumber(Parse
1ee97 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1ee98 45 78 70 72 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a Expr){. Token *
1ee99 70 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 pToken;. sqlite
1ee9a 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1ee9b 64 62 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 db;.. if( pExpr
1ee9c 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1ee9d 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70 72 2d pToken = &pExpr-
1ee9e 3e 74 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 >token;. assert
1ee9f 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31 20 29 ( pToken->n>=1 )
1eea0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b ;. assert( pTok
1eea1 65 6e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 61 73 en->z!=0 );. as
1eea2 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b sert( pToken->z[
1eea3 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 0]!=0 );. if( p
1eea4 54 6f 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b 0a 20 Token->n==1 ){.
1eea5 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f /* Wildcard o
1eea6 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 f the form "?".
1eea7 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 Assign the next
1eea8 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 variable number
1eea9 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 */. pExpr->i
1eeaa 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 Table = ++pParse
1eeab 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 65 20 ->nVar;. }else
1eeac 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d if( pToken->z[0]
1eead 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20 =='?' ){. /*
1eeae 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 Wildcard of the
1eeaf 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f form "?nnn". Co
1eeb0 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61 nvert "nnn" to a
1eeb1 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20 n integer and.
1eeb2 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74 ** use it as t
1eeb3 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 he variable numb
1eeb4 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b er */. int i;
1eeb5 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 . pExpr->iTab
1eeb6 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63 le = i = atoi((c
1eeb7 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a 5b har*)&pToken->z[
1eeb8 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 1]);. testcas
1eeb9 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74 e( i==0 );. t
1eeba 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b estcase( i==1 );
1eebb 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 . testcase( i
1eebc 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ==db->aLimit[SQL
1eebd 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 ITE_LIMIT_VARIAB
1eebe 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a LE_NUMBER]-1 );.
1eebf 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d testcase( i=
1eec0 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 =db->aLimit[SQLI
1eec1 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c TE_LIMIT_VARIABL
1eec2 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20 E_NUMBER] );.
1eec3 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62 if( i<1 || i>db
1eec4 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1eec5 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
1eec6 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 20 20 UMBER] ){.
1eec7 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1eec8 70 50 61 72 73 65 2c 20 22 76 61 72 69 61 62 6c pParse, "variabl
1eec9 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 62 65 e number must be
1eeca 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e 64 20 between ?1 and
1eecb 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 ?%d",.
1eecc 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1eecd 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 E_LIMIT_VARIABLE
1eece 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d _NUMBER]);. }
1eecf 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73 . if( i>pPars
1eed0 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 20 e->nVar ){.
1eed1 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 pParse->nVar =
1eed2 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 i;. }. }else
1eed3 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 {. /* Wildcar
1eed4 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 ds of the form "
1eed5 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 2e :aaa" or "$aaa".
1eed6 20 20 52 65 75 73 65 20 74 68 65 20 73 61 6d 65 Reuse the same
1eed7 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a variable. **
1eed8 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65 20 70 number as the p
1eed9 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20 rior appearance
1eeda 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 of the same name
1eedb 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65 , or if the name
1eedc 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65 . ** has neve
1eedd 72 20 61 70 70 65 61 72 65 64 20 62 65 66 6f 72 r appeared befor
1eede 65 2c 20 72 65 75 73 65 20 74 68 65 20 73 61 6d e, reuse the sam
1eedf 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 e variable numbe
1eee0 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 r. */. int
1eee1 20 69 3b 0a 20 20 20 20 75 33 32 20 6e 3b 0a 20 i;. u32 n;.
1eee2 20 20 20 6e 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e n = pToken->n
1eee3 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1eee4 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 <pParse->nVarExp
1eee5 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 r; i++){. E
1eee6 78 70 72 20 2a 70 45 3b 0a 20 20 20 20 20 20 69 xpr *pE;. i
1eee7 66 28 20 28 70 45 20 3d 20 70 50 61 72 73 65 2d f( (pE = pParse-
1eee8 3e 61 70 56 61 72 45 78 70 72 5b 69 5d 29 21 3d >apVarExpr[i])!=
1eee9 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 70 0. && p
1eeea 45 2d 3e 74 6f 6b 65 6e 2e 6e 3d 3d 6e 0a 20 20 E->token.n==n.
1eeeb 20 20 20 20 20 20 20 20 26 26 20 6d 65 6d 63 6d && memcm
1eeec 70 28 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 p(pE->token.z, p
1eeed 54 6f 6b 65 6e 2d 3e 7a 2c 20 6e 29 3d 3d 30 20 Token->z, n)==0
1eeee 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 ){. pExpr
1eeef 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 2d 3e 69 ->iTable = pE->i
1eef0 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 62 Table;. b
1eef1 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1eef2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 70 }. if( i>=p
1eef3 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 20 Parse->nVarExpr
1eef4 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e ){. pExpr->
1eef5 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 iTable = ++pPars
1eef6 65 2d 3e 6e 56 61 72 3b 0a 20 20 20 20 20 20 69 e->nVar;. i
1eef7 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 f( pParse->nVarE
1eef8 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 xpr>=pParse->nVa
1eef9 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a rExprAlloc-1 ){.
1eefa 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
1eefb 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d nVarExprAlloc +=
1eefc 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 pParse->nVarExp
1eefd 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 rAlloc + 10;.
1eefe 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 pParse->apV
1eeff 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20 arExpr =.
1ef00 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 sqlite3DbRe
1ef01 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 allocOrFree(.
1ef02 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a 20 db,.
1ef03 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 pPa
1ef04 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a rse->apVarExpr,.
1ef05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
1ef06 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c arse->nVarExprAl
1ef07 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 loc*sizeof(pPars
1ef08 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 e->apVarExpr[0])
1ef09 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a . );.
1ef0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1ef0b 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 ( !db->mallocFai
1ef0c 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 led ){. a
1ef0d 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 ssert( pParse->a
1ef0e 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 pVarExpr!=0 );.
1ef0f 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 pParse->a
1ef10 70 56 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d pVarExpr[pParse-
1ef11 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 >nVarExpr++] = p
1ef12 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Expr;. }.
1ef13 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 }. } . if( !
1ef14 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 pParse->nErr &&
1ef15 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d pParse->nVar>db-
1ef16 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1ef17 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 IMIT_VARIABLE_NU
1ef18 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c MBER] ){. sql
1ef19 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1ef1a 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53 rse, "too many S
1ef1b 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b 0a QL variables");.
1ef1c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 }.}../*.** Cle
1ef1d 61 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e ar an expression
1ef1e 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 6f structure witho
1ef1f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 ut deleting the
1ef20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 structure itself
1ef21 2e 0a 2a 2a 20 53 75 62 73 74 72 75 63 74 75 72 ..** Substructur
1ef22 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f e is deleted..*/
1ef23 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ef24 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1ef25 43 6c 65 61 72 28 73 71 6c 69 74 65 33 20 2a 64 Clear(sqlite3 *d
1ef26 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 b, Expr *p){. i
1ef27 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 f( p->token.dyn
1ef28 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 ) sqlite3DbFree(
1ef29 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74 6f db, (char*)p->to
1ef2a 6b 65 6e 2e 7a 29 3b 0a 20 20 69 66 28 20 21 45 ken.z);. if( !E
1ef2b 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 xprHasAnyPropert
1ef2c 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c y(p, EP_TokenOnl
1ef2d 79 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 y|EP_SpanToken)
1ef2e 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 70 ){. if( p->sp
1ef2f 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 an.dyn ) sqlite3
1ef30 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 DbFree(db, (char
1ef31 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a 20 20 *)p->span.z);.
1ef32 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
1ef33 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 perty(p, EP_Redu
1ef34 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a ced) ){. /*
1ef35 20 53 75 62 74 72 65 65 73 20 61 72 65 20 70 61 Subtrees are pa
1ef36 72 74 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6d rt of the same m
1ef37 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
1ef38 20 77 68 65 6e 20 45 50 5f 52 65 64 75 63 65 64 when EP_Reduced
1ef39 20 73 65 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 set */. if
1ef3a 28 20 70 2d 3e 70 4c 65 66 74 20 29 20 73 71 6c ( p->pLeft ) sql
1ef3b 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 ite3ExprClear(db
1ef3c 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 , p->pLeft);.
1ef3d 20 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 if( p->pRight
1ef3e 20 29 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c ) sqlite3ExprCl
1ef3f 65 61 72 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 ear(db, p->pRigh
1ef40 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 t);. }else{.
1ef41 20 20 20 20 20 2f 2a 20 53 75 62 74 72 65 65 73 /* Subtrees
1ef42 20 61 72 65 20 73 65 70 61 72 61 74 65 20 61 6c are separate al
1ef43 6c 6f 63 61 74 69 6f 6e 73 20 77 68 65 6e 20 45 locations when E
1ef44 50 5f 52 65 64 75 63 65 64 20 69 73 20 63 6c 65 P_Reduced is cle
1ef45 61 72 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 ar */. sqli
1ef46 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1ef47 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 , p->pLeft);.
1ef48 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 sqlite3ExprDe
1ef49 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 67 lete(db, p->pRig
1ef4a 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f ht);. }. /
1ef4b 2a 20 78 2e 70 53 65 6c 65 63 74 20 61 6e 64 20 * x.pSelect and
1ef4c 78 2e 70 4c 69 73 74 20 61 72 65 20 61 6c 77 61 x.pList are alwa
1ef4d 79 73 20 73 65 70 61 72 61 74 65 6c 79 20 61 6c ys separately al
1ef4e 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20 20 69 located */. i
1ef4f 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 f( ExprHasProper
1ef50 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 ty(p, EP_xIsSele
1ef51 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ct) ){. sql
1ef52 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 ite3SelectDelete
1ef53 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 (db, p->x.pSelec
1ef54 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 t);. }else{.
1ef55 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1ef56 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
1ef57 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 ->x.pList);.
1ef58 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
1ef59 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 ecursively delet
1ef5a 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 e an expression
1ef5b 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tree..*/.SQLITE_
1ef5c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1ef5d 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 ite3ExprDelete(s
1ef5e 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
1ef5f 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 *p){. if( p==0
1ef60 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c ) return;. sql
1ef61 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 ite3ExprClear(db
1ef62 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 , p);. sqlite3D
1ef63 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a bFree(db, p);.}.
1ef64 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1ef65 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
1ef66 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
1ef67 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 the expression s
1ef68 74 72 75 63 74 75 72 65 20 0a 2a 2a 20 70 61 73 tructure .** pas
1ef69 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
1ef6a 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 argument. This
1ef6b 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 is always one of
1ef6c 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 0a EXPR_FULLSIZE,.
1ef6d 2a 2a 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 ** EXPR_REDUCEDS
1ef6e 49 5a 45 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45 IZE or EXPR_TOKE
1ef6f 4e 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74 NONLYSIZE..*/.st
1ef70 61 74 69 63 20 69 6e 74 20 65 78 70 72 53 74 72 atic int exprStr
1ef71 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 29 uctSize(Expr *p)
1ef72 7b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 {. if( ExprHasP
1ef73 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f roperty(p, EP_To
1ef74 6b 65 6e 4f 6e 6c 79 29 20 29 20 72 65 74 75 72 kenOnly) ) retur
1ef75 6e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 n EXPR_TOKENONLY
1ef76 53 49 5a 45 3b 0a 20 20 69 66 28 20 45 78 70 72 SIZE;. if( Expr
1ef77 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
1ef78 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29 20 72 P_SpanToken) ) r
1ef79 65 74 75 72 6e 20 45 58 50 52 5f 53 50 41 4e 54 eturn EXPR_SPANT
1ef7a 4f 4b 45 4e 53 49 5a 45 3b 0a 20 20 69 66 28 20 OKENSIZE;. if(
1ef7b 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1ef7c 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 p, EP_Reduced) )
1ef7d 20 72 65 74 75 72 6e 20 45 58 50 52 5f 52 45 44 return EXPR_RED
1ef7e 55 43 45 44 53 49 5a 45 3b 0a 20 20 72 65 74 75 UCEDSIZE;. retu
1ef7f 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 rn EXPR_FULLSIZE
1ef80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 ;.}../*.** sqlit
1ef81 65 33 45 78 70 72 44 75 70 28 29 20 68 61 73 20 e3ExprDup() has
1ef82 62 65 65 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63 been called to c
1ef83 72 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 reate a copy of
1ef84 65 78 70 72 65 73 73 69 6f 6e 20 70 20 77 69 74 expression p wit
1ef85 68 0a 2a 2a 20 74 68 65 20 45 58 50 52 44 55 50 h.** the EXPRDUP
1ef86 5f 58 58 58 20 66 6c 61 67 73 20 70 61 73 73 65 _XXX flags passe
1ef87 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
1ef88 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 66 argument. This f
1ef89 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 72 65 74 75 unction .** retu
1ef8a 72 6e 73 20 74 68 65 20 73 70 61 63 65 20 72 65 rns the space re
1ef8b 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 63 quired for the c
1ef8c 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 20 opy of the Expr
1ef8d 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 2e 0a structure only..
1ef8e 2a 2a 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 ** This is alway
1ef8f 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 s one of EXPR_FU
1ef90 4c 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 45 44 LLSIZE, EXPR_RED
1ef91 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50 52 UCEDSIZE or EXPR
1ef92 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 0a _TOKENONLYSIZE..
1ef93 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 */.static int du
1ef94 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a pedExprStructSiz
1ef95 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 e(Expr *p, int f
1ef96 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 53 69 lags){. int nSi
1ef97 7a 65 3b 0a 20 20 69 66 28 20 30 3d 3d 28 66 6c ze;. if( 0==(fl
1ef98 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 ags&EXPRDUP_REDU
1ef99 43 45 29 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 CE) ){. nSize
1ef9a 20 3d 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 = EXPR_FULLSIZE
1ef9b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d ;. }else if( p-
1ef9c 3e 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69 >pLeft || p->pRi
1ef9d 67 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 ght || p->pColl
1ef9e 7c 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b || p->x.pList ){
1ef9f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 . nSize = EXP
1efa0 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 R_REDUCEDSIZE;.
1efa1 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 }else if( flags
1efa2 26 45 58 50 52 44 55 50 5f 53 50 41 4e 20 29 7b &EXPRDUP_SPAN ){
1efa3 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 . nSize = EXP
1efa4 52 5f 53 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 3b R_SPANTOKENSIZE;
1efa5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 53 . }else{. nS
1efa6 69 7a 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e ize = EXPR_TOKEN
1efa7 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 7d 0a 20 20 ONLYSIZE;. }.
1efa8 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a 7d 0a return nSize;.}.
1efa9 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 45 78 ./*.** sqlite3Ex
1efaa 70 72 44 75 70 28 29 20 68 61 73 20 62 65 65 6e prDup() has been
1efab 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 called to creat
1efac 65 20 61 20 63 6f 70 79 20 6f 66 20 65 78 70 72 e a copy of expr
1efad 65 73 73 69 6f 6e 20 70 20 77 69 74 68 0a 2a 2a ession p with.**
1efae 20 74 68 65 20 45 58 50 52 44 55 50 5f 58 58 58 the EXPRDUP_XXX
1efaf 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
1efb0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 econd argument.
1efb1 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
1efb2 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 73 70 61 turns.** the spa
1efb3 63 65 20 69 6e 20 62 79 74 65 73 20 72 65 71 75 ce in bytes requ
1efb4 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 ired to store th
1efb5 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 e copy of the Ex
1efb6 70 72 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 pr structure.**
1efb7 61 6e 64 20 74 68 65 20 63 6f 70 69 65 73 20 6f and the copies o
1efb8 66 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e f the Expr.token
1efb9 2e 7a 20 61 6e 64 20 45 78 70 72 2e 73 70 61 6e .z and Expr.span
1efba 2e 7a 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c .z (if applicabl
1efbb 65 29 0a 2a 2a 20 73 74 72 69 6e 67 20 62 75 66 e).** string buf
1efbc 66 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 fers..*/.static
1efbd 69 6e 74 20 64 75 70 65 64 45 78 70 72 4e 6f 64 int dupedExprNod
1efbe 65 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 eSize(Expr *p, i
1efbf 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 nt flags){. int
1efc0 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64 45 78 nByte = dupedEx
1efc1 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 prStructSize(p,
1efc2 66 6c 61 67 73 29 20 2b 20 28 70 2d 3e 74 6f 6b flags) + (p->tok
1efc3 65 6e 2e 7a 20 3f 20 70 2d 3e 74 6f 6b 65 6e 2e en.z ? p->token.
1efc4 6e 20 2b 20 31 20 3a 20 30 29 3b 0a 20 20 69 66 n + 1 : 0);. if
1efc5 28 20 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 ( (flags&EXPRDUP
1efc6 5f 53 50 41 4e 29 21 3d 30 0a 20 20 20 26 26 20 _SPAN)!=0. &&
1efc7 28 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 70 2d 3e (p->token.z!=p->
1efc8 73 70 61 6e 2e 7a 20 7c 7c 20 70 2d 3e 74 6f 6b span.z || p->tok
1efc9 65 6e 2e 6e 21 3d 70 2d 3e 73 70 61 6e 2e 6e 29 en.n!=p->span.n)
1efca 0a 20 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 . ){. nByte
1efcb 2b 3d 20 70 2d 3e 73 70 61 6e 2e 6e 3b 0a 20 20 += p->span.n;.
1efcc 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 }. return ROUND
1efcd 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 8(nByte);.}../*.
1efce 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1efcf 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
1efd0 71 75 69 72 65 64 20 74 6f 20 63 72 65 61 74 65 quired to create
1efd1 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20 a duplicate of
1efd2 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 the .** expressi
1efd3 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 on passed as the
1efd4 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
1efd5 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
1efd6 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73 ment is a.** mas
1efd7 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50 k containing EXP
1efd8 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a RDUP_XXX flags..
1efd9 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
1efda 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 returned include
1efdb 73 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74 s space to creat
1efdc 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 e a copy of the
1efdd 45 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20 69 Expr struct.** i
1efde 74 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 75 tself and the bu
1efdf 66 66 65 72 20 72 65 66 65 72 72 65 64 20 74 6f ffer referred to
1efe0 20 62 79 20 45 78 70 72 2e 74 6f 6b 65 6e 2c 20 by Expr.token,
1efe1 69 66 20 61 6e 79 2e 20 49 66 20 74 68 65 20 0a if any. If the .
1efe2 2a 2a 20 45 58 50 52 44 55 50 5f 53 50 41 4e 20 ** EXPRDUP_SPAN
1efe3 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
1efe4 6e 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74 n space to creat
1efe5 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 e a copy of the
1efe6 62 75 66 66 65 72 0a 2a 2a 20 72 65 66 65 72 65 buffer.** refere
1efe7 64 20 74 6f 20 62 79 20 45 78 70 72 2e 73 70 61 d to by Expr.spa
1efe8 6e 20 69 73 20 61 6c 73 6f 20 69 6e 63 6c 75 64 n is also includ
1efe9 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
1efea 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 EXPRDUP_REDUCE
1efeb 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
1efec 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c n the return val
1efed 75 65 20 69 6e 63 6c 75 64 65 73 20 0a 2a 2a 20 ue includes .**
1efee 73 70 61 63 65 20 74 6f 20 64 75 70 6c 69 63 61 space to duplica
1efef 74 65 20 61 6c 6c 20 45 78 70 72 20 6e 6f 64 65 te all Expr node
1eff0 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 66 6f s in the tree fo
1eff1 72 6d 65 64 20 62 79 20 45 78 70 72 2e 70 4c 65 rmed by Expr.pLe
1eff2 66 74 20 0a 2a 2a 20 61 6e 64 20 45 78 70 72 2e ft .** and Expr.
1eff3 70 52 69 67 68 74 20 76 61 72 69 61 62 6c 65 73 pRight variables
1eff4 20 28 62 75 74 20 6e 6f 74 20 66 6f 72 20 61 6e (but not for an
1eff5 79 20 73 74 72 75 63 74 75 72 65 73 20 70 6f 69 y structures poi
1eff6 6e 74 65 64 20 74 6f 20 6f 72 20 0a 2a 2a 20 64 nted to or .** d
1eff7 65 73 63 65 6e 64 65 64 20 66 72 6f 6d 20 74 68 escended from th
1eff8 65 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f e Expr.x.pList o
1eff9 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 r Expr.x.pSelect
1effa 20 76 61 72 69 61 62 6c 65 73 29 2e 0a 2a 2f 0a variables)..*/.
1effb 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64 static int duped
1effc 45 78 70 72 53 69 7a 65 28 45 78 70 72 20 2a 70 ExprSize(Expr *p
1effd 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
1effe 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 int nByte = 0;.
1efff 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 42 if( p ){. nB
1f000 79 74 65 20 3d 20 64 75 70 65 64 45 78 70 72 4e yte = dupedExprN
1f001 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 odeSize(p, flags
1f002 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 );. if( flags
1f003 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 &EXPRDUP_REDUCE
1f004 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 20 3d ){. int f =
1f005 20 66 6c 61 67 73 26 28 7e 45 58 50 52 44 55 50 flags&(~EXPRDUP
1f006 5f 53 50 41 4e 29 3b 0a 20 20 20 20 20 20 6e 42 _SPAN);. nB
1f007 79 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70 72 yte += dupedExpr
1f008 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 66 Size(p->pLeft, f
1f009 29 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a ) + dupedExprSiz
1f00a 65 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 29 3b e(p->pRight, f);
1f00b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1f00c 75 72 6e 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a urn nByte;.}../*
1f00d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1f00e 6e 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 n is similar to
1f00f 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 sqlite3ExprDup()
1f010 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 69 66 , except that if
1f011 20 70 7a 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 pzBuffer .** is
1f012 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a not NULL then *
1f013 70 7a 42 75 66 66 65 72 20 69 73 20 61 73 73 75 pzBuffer is assu
1f014 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 med to point to
1f015 61 20 62 75 66 66 65 72 20 6c 61 72 67 65 20 65 a buffer large e
1f016 6e 6f 75 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f nough .** to sto
1f017 72 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 65 re the copy of e
1f018 78 70 72 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 xpression p, the
1f019 20 63 6f 70 69 65 73 20 6f 66 20 70 2d 3e 74 6f copies of p->to
1f01a 6b 65 6e 20 61 6e 64 20 70 2d 3e 73 70 61 6e 20 ken and p->span
1f01b 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62 .** (if applicab
1f01c 6c 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 le), and the cop
1f01d 69 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c ies of the p->pL
1f01e 65 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 eft and p->pRigh
1f01f 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a t expressions,.*
1f020 2a 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 * if any. Before
1f021 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 returning, *pzB
1f022 75 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20 uffer is set to
1f023 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 the first byte p
1f024 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 assed the.** por
1f025 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66 tion of the buff
1f026 65 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 er copied into b
1f027 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
1f028 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 .*/.static Expr
1f029 2a 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 *exprDup(sqlite3
1f02a 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 *db, Expr *p, i
1f02b 6e 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 nt flags, u8 **p
1f02c 7a 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72 zBuffer){. Expr
1f02d 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 *pNew = 0;
1f02e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f02f 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 /* Value to ret
1f030 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 urn */. if( p )
1f031 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 {. const int
1f032 69 73 52 65 71 75 69 72 65 53 70 61 6e 20 3d 20 isRequireSpan =
1f033 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 53 (flags&EXPRDUP_S
1f034 50 41 4e 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 PAN);. const
1f035 69 6e 74 20 69 73 52 65 64 75 63 65 64 20 3d 20 int isReduced =
1f036 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 (flags&EXPRDUP_R
1f037 45 44 55 43 45 29 3b 0a 20 20 20 20 75 38 20 2a EDUCE);. u8 *
1f038 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 61 73 73 zAlloc;.. ass
1f039 65 72 74 28 20 70 7a 42 75 66 66 65 72 3d 3d 30 ert( pzBuffer==0
1f03a 20 7c 7c 20 69 73 52 65 64 75 63 65 64 20 29 3b || isReduced );
1f03b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 .. /* Figure
1f03c 6f 75 74 20 77 68 65 72 65 20 74 6f 20 77 72 69 out where to wri
1f03d 74 65 20 74 68 65 20 6e 65 77 20 45 78 70 72 20 te the new Expr
1f03e 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 structure. */.
1f03f 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 29 if( pzBuffer )
1f040 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d {. zAlloc =
1f041 20 2a 70 7a 42 75 66 66 65 72 3b 0a 20 20 20 20 *pzBuffer;.
1f042 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 41 6c }else{. zAl
1f043 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d loc = sqlite3DbM
1f044 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64 75 70 allocRaw(db, dup
1f045 65 64 45 78 70 72 53 69 7a 65 28 70 2c 20 66 6c edExprSize(p, fl
1f046 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ags));. }.
1f047 20 70 4e 65 77 20 3d 20 28 45 78 70 72 20 2a 29 pNew = (Expr *)
1f048 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69 66 28 zAlloc;.. if(
1f049 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 2f pNew ){. /
1f04a 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 20 74 * Set nNewSize t
1f04b 6f 20 74 68 65 20 73 69 7a 65 20 61 6c 6c 6f 63 o the size alloc
1f04c 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 74 72 ated for the str
1f04d 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 ucture pointed t
1f04e 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 70 4e o. ** by pN
1f04f 65 77 2e 20 54 68 69 73 20 69 73 20 65 69 74 68 ew. This is eith
1f050 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 er EXPR_FULLSIZE
1f051 2c 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 , EXPR_REDUCEDSI
1f052 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 45 ZE or. ** E
1f053 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a XPR_TOKENONLYSIZ
1f054 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 65 74 E. nToken is set
1f055 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
1f056 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 f bytes consumed
1f057 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65 . ** by the
1f058 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 2d 3e copy of the p->
1f059 74 6f 6b 65 6e 2e 7a 20 73 74 72 69 6e 67 20 28 token.z string (
1f05a 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a if any).. *
1f05b 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e /. const in
1f05c 74 20 6e 4e 65 77 53 69 7a 65 20 3d 20 64 75 70 t nNewSize = dup
1f05d 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 edExprStructSize
1f05e 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 (p, flags);.
1f05f 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 6f 6b const int nTok
1f060 65 6e 20 3d 20 28 70 2d 3e 74 6f 6b 65 6e 2e 7a en = (p->token.z
1f061 20 3f 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 20 2b 20 ? p->token.n +
1f062 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 69 66 1 : 0);. if
1f063 28 20 69 73 52 65 64 75 63 65 64 20 29 7b 0a 20 ( isReduced ){.
1f064 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 45 assert( E
1f065 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
1f066 2c 20 45 50 5f 52 65 64 75 63 65 64 29 3d 3d 30 , EP_Reduced)==0
1f067 20 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 );. memc
1f068 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 4e py(zAlloc, p, nN
1f069 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d ewSize);. }
1f06a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e else{. in
1f06b 74 20 6e 53 69 7a 65 20 3d 20 65 78 70 72 53 74 t nSize = exprSt
1f06c 72 75 63 74 53 69 7a 65 28 70 29 3b 0a 20 20 20 ructSize(p);.
1f06d 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c memcpy(zAll
1f06e 6f 63 2c 20 70 2c 20 6e 53 69 7a 65 29 3b 0a 20 oc, p, nSize);.
1f06f 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 7a memset(&z
1f070 41 6c 6c 6f 63 5b 6e 53 69 7a 65 5d 2c 20 30 2c Alloc[nSize], 0,
1f071 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2d 6e EXPR_FULLSIZE-n
1f072 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a Size);. }..
1f073 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 /* Set the
1f074 20 45 50 5f 52 65 64 75 63 65 64 20 61 6e 64 20 EP_Reduced and
1f075 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 EP_TokenOnly fla
1f076 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 gs appropriately
1f077 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 2d . */. pNew-
1f078 3e 66 6c 61 67 73 20 26 3d 20 7e 28 45 50 5f 52 >flags &= ~(EP_R
1f079 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f educed|EP_TokenO
1f07a 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e nly|EP_SpanToken
1f07b 29 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 );. switch(
1f07c 20 6e 4e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 nNewSize ){.
1f07d 20 20 20 20 20 63 61 73 65 20 45 58 50 52 5f 52 case EXPR_R
1f07e 45 44 55 43 45 44 53 49 5a 45 3a 20 20 20 70 4e EDUCEDSIZE: pN
1f07f 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ew->flags |= EP_
1f080 52 65 64 75 63 65 64 3b 20 62 72 65 61 6b 3b 0a Reduced; break;.
1f081 20 20 20 20 20 20 20 20 63 61 73 65 20 45 58 50 case EXP
1f082 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3a R_TOKENONLYSIZE:
1f083 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 pNew->flags |=
1f084 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 3b 20 62 72 EP_TokenOnly; br
1f085 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
1f086 65 20 45 58 50 52 5f 53 50 41 4e 54 4f 4b 45 4e e EXPR_SPANTOKEN
1f087 53 49 5a 45 3a 20 70 4e 65 77 2d 3e 66 6c 61 67 SIZE: pNew->flag
1f088 73 20 7c 3d 20 45 50 5f 53 70 61 6e 54 6f 6b 65 s |= EP_SpanToke
1f089 6e 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 n; break;.
1f08a 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 }.. /* Copy
1f08b 20 74 68 65 20 70 2d 3e 74 6f 6b 65 6e 20 73 74 the p->token st
1f08c 72 69 6e 67 2c 20 69 66 20 61 6e 79 2e 20 2a 2f ring, if any. */
1f08d 0a 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 . if( nToke
1f08e 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 73 n ){. uns
1f08f 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 6f 6b igned char *zTok
1f090 65 6e 20 3d 20 26 7a 41 6c 6c 6f 63 5b 6e 4e 65 en = &zAlloc[nNe
1f091 77 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20 20 20 wSize];.
1f092 6d 65 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c 20 70 memcpy(zToken, p
1f093 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 6e 54 6f 6b 65 ->token.z, nToke
1f094 6e 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7a 54 n-1);. zT
1f095 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 2d 31 5d 20 3d oken[nToken-1] =
1f096 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 70 '\0';. p
1f097 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 3d New->token.dyn =
1f098 20 30 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 0;. pNew
1f099 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 7a 54 6f 6b ->token.z = zTok
1f09a 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 en;. }..
1f09b 20 20 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66 if( 0==((p->f
1f09c 6c 61 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73 lags|pNew->flags
1f09d 29 20 26 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 ) & EP_TokenOnly
1f09e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
1f09f 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 4e 65 77 Fill in the pNew
1f0a0 2d 3e 73 70 61 6e 20 74 6f 6b 65 6e 2c 20 69 66 ->span token, if
1f0a1 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 required. */.
1f0a2 20 20 20 20 20 20 69 66 28 20 69 73 52 65 71 75 if( isRequ
1f0a3 69 72 65 53 70 61 6e 20 29 7b 0a 20 20 20 20 20 ireSpan ){.
1f0a4 20 20 20 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65 if( p->toke
1f0a5 6e 2e 7a 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c n.z!=p->span.z |
1f0a6 7c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d | p->token.n!=p-
1f0a7 3e 73 70 61 6e 2e 6e 20 29 7b 0a 20 20 20 20 20 >span.n ){.
1f0a8 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 pNew->spa
1f0a9 6e 2e 7a 20 3d 20 26 7a 41 6c 6c 6f 63 5b 6e 4e n.z = &zAlloc[nN
1f0aa 65 77 53 69 7a 65 2b 6e 54 6f 6b 65 6e 5d 3b 0a ewSize+nToken];.
1f0ab 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 memc
1f0ac 70 79 28 28 63 68 61 72 20 2a 29 70 4e 65 77 2d py((char *)pNew-
1f0ad 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e >span.z, p->span
1f0ae 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29 3b 0a .z, p->span.n);.
1f0af 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 pNew
1f0b0 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20 30 3b 0a ->span.dyn = 0;.
1f0b1 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
1f0b2 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 . pNe
1f0b3 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4e 65 77 w->span.z = pNew
1f0b4 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 ->token.z;.
1f0b5 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 pNew->spa
1f0b6 6e 2e 6e 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65 n.n = pNew->toke
1f0b7 6e 2e 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d n.n;. }
1f0b8 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1f0b9 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e pNew->
1f0ba 73 70 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20 20 20 span.z = 0;.
1f0bb 20 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e pNew->span
1f0bc 2e 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 .n = 0;.
1f0bd 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }. }..
1f0be 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66 6c 61 if( 0==((p->fla
1f0bf 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73 29 20 gs|pNew->flags)
1f0c0 26 20 28 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c & (EP_TokenOnly|
1f0c1 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 29 20 29 EP_SpanToken)) )
1f0c2 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c {. /* Fil
1f0c3 6c 20 69 6e 20 74 68 65 20 70 4e 65 77 2d 3e 78 l in the pNew->x
1f0c4 2e 70 53 65 6c 65 63 74 20 6f 72 20 70 4e 65 77 .pSelect or pNew
1f0c5 2d 3e 78 2e 70 4c 69 73 74 20 6d 65 6d 62 65 72 ->x.pList member
1f0c6 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 . */. if(
1f0c7 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1f0c8 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 (p, EP_xIsSelect
1f0c9 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
1f0ca 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d New->x.pSelect =
1f0cb 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 sqlite3SelectDu
1f0cc 70 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 p(db, p->x.pSele
1f0cd 63 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a ct, isReduced);.
1f0ce 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1f0cf 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 pNew->x
1f0d0 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 .pList = sqlite3
1f0d1 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 ExprListDup(db,
1f0d2 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 73 52 65 p->x.pList, isRe
1f0d3 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20 duced);.
1f0d4 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }. }..
1f0d5 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 70 4e 65 77 /* Fill in pNew
1f0d6 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 4e 65 77 ->pLeft and pNew
1f0d7 2d 3e 70 52 69 67 68 74 2e 20 2a 2f 0a 20 20 20 ->pRight. */.
1f0d8 20 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e if( ExprHasAn
1f0d9 79 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 yProperty(pNew,
1f0da 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f EP_Reduced|EP_To
1f0db 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 kenOnly|EP_SpanT
1f0dc 6f 6b 65 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 oken) ){.
1f0dd 20 7a 41 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64 zAlloc += duped
1f0de 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 ExprNodeSize(p,
1f0df 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 flags);.
1f0e0 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
1f0e1 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 rty(pNew, EP_Red
1f0e2 75 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 uced) ){.
1f0e3 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d pNew->pLeft =
1f0e4 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e exprDup(db, p->
1f0e5 70 4c 65 66 74 2c 20 45 58 50 52 44 55 50 5f 52 pLeft, EXPRDUP_R
1f0e6 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b EDUCE, &zAlloc);
1f0e7 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d . pNew-
1f0e8 3e 70 52 69 67 68 74 20 3d 20 65 78 70 72 44 75 >pRight = exprDu
1f0e9 70 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c p(db, p->pRight,
1f0ea 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c EXPRDUP_REDUCE,
1f0eb 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 &zAlloc);.
1f0ec 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1f0ed 20 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20 pzBuffer ){.
1f0ee 20 20 20 20 20 20 20 2a 70 7a 42 75 66 66 65 72 *pzBuffer
1f0ef 20 3d 20 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 = zAlloc;.
1f0f0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1f0f1 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 if( !ExprHasAny
1f0f2 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 Property(p, EP_T
1f0f3 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e okenOnly|EP_Span
1f0f4 54 6f 6b 65 6e 29 20 29 7b 0a 20 20 20 20 20 20 Token) ){.
1f0f5 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 pNew->pLeft =
1f0f6 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
1f0f7 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 30 29 3b b, p->pLeft, 0);
1f0f8 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 . pNew->p
1f0f9 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 Right = sqlite3E
1f0fa 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 xprDup(db, p->pR
1f0fb 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 ight, 0);.
1f0fc 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
1f0fd 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn pNew;.}../*
1f0fe 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1f0ff 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 g group of routi
1f100 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f nes make deep co
1f101 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 pies of expressi
1f102 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 ons,.** expressi
1f103 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 on lists, ID lis
1f104 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73 ts, and select s
1f105 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 tatements. The
1f106 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 copies can.** be
1f107 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65 69 deleted (by bei
1f108 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 ng passed to the
1f109 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e ir respective ..
1f10a 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e .Delete() routin
1f10b 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 es).** without e
1f10c 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69 ffecting the ori
1f10d 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ginals..**.** Th
1f10e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 e expression lis
1f10f 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 t, ID, and sourc
1f110 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62 e lists return b
1f111 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 y sqlite3ExprLis
1f112 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 tDup(),.** sqlit
1f113 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61 e3IdListDup(), a
1f114 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 nd sqlite3SrcLis
1f115 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 tDup() can not b
1f116 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e 64 e further expand
1f117 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 ed .** by subseq
1f118 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 uent calls to sq
1f119 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 lite*ListAppend(
1f11a 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a ) routines..**.*
1f11b 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61 * Any tables tha
1f11c 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69 t the SrcList mi
1f11d 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 ght point to are
1f11e 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e not duplicated.
1f11f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 .**.** The flags
1f120 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e 74 61 parameter conta
1f121 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f ins a combinatio
1f122 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44 55 50 n of the EXPRDUP
1f123 5f 58 58 58 20 66 6c 61 67 73 2e 20 49 66 0a 2a _XXX flags. If.*
1f124 2a 20 74 68 65 20 45 58 50 52 44 55 50 5f 53 50 * the EXPRDUP_SP
1f125 41 4e 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 AN flag is set i
1f126 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 n the argument p
1f127 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 74 arameter, then t
1f128 68 65 20 0a 2a 2a 20 45 78 70 72 2e 73 70 61 6e he .** Expr.span
1f129 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e field of the in
1f12a 70 75 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 put expression i
1f12b 73 20 63 6f 70 69 65 64 2e 20 49 66 20 45 58 50 s copied. If EXP
1f12c 52 44 55 50 5f 53 50 41 4e 20 69 73 0a 2a 2a 20 RDUP_SPAN is.**
1f12d 63 6c 65 61 72 2c 20 74 68 65 6e 20 74 68 65 20 clear, then the
1f12e 45 78 70 72 2e 73 70 61 6e 20 66 69 65 6c 64 20 Expr.span field
1f12f 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 of the returned
1f130 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 expression struc
1f131 74 75 72 65 0a 2a 2a 20 69 73 20 7a 65 72 6f 65 ture.** is zeroe
1f132 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1f133 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 66 EXPRDUP_REDUCE f
1f134 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e lag is set, then
1f135 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 72 the structure r
1f136 65 74 75 72 6e 65 64 20 69 73 20 61 0a 2a 2a 20 eturned is a.**
1f137 74 72 75 6e 63 61 74 65 64 20 76 65 72 73 69 6f truncated versio
1f138 6e 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20 45 n of the usual E
1f139 78 70 72 20 73 74 72 75 63 74 75 72 65 20 74 68 xpr structure th
1f13a 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 at will be store
1f13b 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 d as.** part of
1f13c 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 the in-memory re
1f13d 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
1f13e 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
1f13f 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ema..*/.SQLITE_P
1f140 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
1f141 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c 69 ite3ExprDup(sqli
1f142 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 te3 *db, Expr *p
1f143 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
1f144 72 65 74 75 72 6e 20 65 78 70 72 44 75 70 28 64 return exprDup(d
1f145 62 2c 20 70 2c 20 66 6c 61 67 73 2c 20 30 29 3b b, p, flags, 0);
1f146 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
1f147 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f E void sqlite3To
1f148 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 20 kenCopy(sqlite3
1f149 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c *db, Token *pTo,
1f14a 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 46 const Token *pF
1f14b 72 6f 6d 29 7b 0a 20 20 69 66 28 20 70 54 6f 2d rom){. if( pTo-
1f14c 3e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62 >dyn ) sqlite3Db
1f14d 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29 Free(db, (char*)
1f14e 70 54 6f 2d 3e 7a 29 3b 0a 20 20 69 66 28 20 70 pTo->z);. if( p
1f14f 46 72 6f 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70 From->z ){. p
1f150 54 6f 2d 3e 6e 20 3d 20 70 46 72 6f 6d 2d 3e 6e To->n = pFrom->n
1f151 3b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d 20 28 ;. pTo->z = (
1f152 75 38 2a 29 73 71 6c 69 74 65 33 44 62 53 74 72 u8*)sqlite3DbStr
1f153 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 NDup(db, (char*)
1f154 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46 72 6f 6d 2d pFrom->z, pFrom-
1f155 3e 6e 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 64 79 >n);. pTo->dy
1f156 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a n = 1;. }else{.
1f157 20 20 20 20 70 54 6f 2d 3e 7a 20 3d 20 30 3b 0a pTo->z = 0;.
1f158 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 }.}.SQLITE_PRI
1f159 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 VATE ExprList *s
1f15a 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 qlite3ExprListDu
1f15b 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 p(sqlite3 *db, E
1f15c 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 xprList *p, int
1f15d 66 6c 61 67 73 29 7b 0a 20 20 45 78 70 72 4c 69 flags){. ExprLi
1f15e 73 74 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 st *pNew;. stru
1f15f 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
1f160 20 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 *pItem, *pOldIt
1f161 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 em;. int i;. i
1f162 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
1f163 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 0;. pNew = sql
1f164 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
1f165 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 db, sizeof(*pNew
1f166 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d ) );. if( pNew=
1f167 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1f168 20 70 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 pNew->iECursor
1f169 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 = 0;. pNew->nEx
1f16a 70 72 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f pr = pNew->nAllo
1f16b 63 20 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 c = p->nExpr;.
1f16c 70 4e 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 pNew->a = pItem
1f16d 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1f16e 63 52 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 cRaw(db, p->nEx
1f16f 70 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 pr*sizeof(p->a[0
1f170 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 49 74 65 ]) );. if( pIte
1f171 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 m==0 ){. sqli
1f172 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e te3DbFree(db, pN
1f173 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ew);. return
1f174 30 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74 0;. } . pOldIt
1f175 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72 em = p->a;. for
1f176 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 (i=0; i<p->nExpr
1f177 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20 ; i++, pItem++,
1f178 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 pOldItem++){.
1f179 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b Expr *pNewExpr;
1f17a 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64 45 . Expr *pOldE
1f17b 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e xpr = pOldItem->
1f17c 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d pExpr;. pItem
1f17d 2d 3e 70 45 78 70 72 20 3d 20 70 4e 65 77 45 78 ->pExpr = pNewEx
1f17e 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 pr = sqlite3Expr
1f17f 44 75 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 Dup(db, pOldExpr
1f180 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 49 , flags);. pI
1f181 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c tem->zName = sql
1f182 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
1f183 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pOldItem->zName
1f184 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f );. pItem->so
1f185 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 rtOrder = pOldIt
1f186 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 em->sortOrder;.
1f187 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d pItem->done =
1f188 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 0;. pItem->i
1f189 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e Col = pOldItem->
1f18a 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d iCol;. pItem-
1f18b 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 >iAlias = pOldIt
1f18c 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a em->iAlias;. }.
1f18d 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
1f18e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f ../*.** If curso
1f18f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69 rs, triggers, vi
1f190 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 72 69 ews and subqueri
1f191 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 es are all omitt
1f192 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 ed from.** the b
1f193 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 uild, then none
1f194 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
1f195 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 routines, excep
1f196 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 t for .** sqlite
1f197 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61 3SelectDup(), ca
1f198 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c n be called. sql
1f199 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 20 ite3SelectDup()
1f19a 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 is sometimes.**
1f19b 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 called with a NU
1f19c 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a LL argument..*/.
1f19d 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
1f19e 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c ITE_OMIT_VIEW) |
1f19f 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 | !defined(SQLIT
1f1a0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 E_OMIT_TRIGGER)
1f1a1 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 \. || !defined(S
1f1a2 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1f1a3 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 ERY).SQLITE_PRIV
1f1a4 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
1f1a5 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 ite3SrcListDup(s
1f1a6 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c qlite3 *db, SrcL
1f1a7 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 ist *p, int flag
1f1a8 73 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 s){. SrcList *p
1f1a9 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 New;. int i;.
1f1aa 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 int nByte;. if(
1f1ab 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
1f1ac 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 ;. nByte = size
1f1ad 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 of(*p) + (p->nSr
1f1ae 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e c>0 ? sizeof(p->
1f1af 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 a[0]) * (p->nSrc
1f1b0 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 -1) : 0);. pNew
1f1b1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1f1b2 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20 ocRaw(db, nByte
1f1b3 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 );. if( pNew==0
1f1b4 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
1f1b5 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 New->nSrc = pNew
1f1b6 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 ->nAlloc = p->nS
1f1b7 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 rc;. for(i=0; i
1f1b8 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a <p->nSrc; i++){.
1f1b9 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
1f1ba 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 st_item *pNewIte
1f1bb 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b m = &pNew->a[i];
1f1bc 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c . struct SrcL
1f1bd 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 ist_item *pOldIt
1f1be 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 em = &p->a[i];.
1f1bf 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a Table *pTab;.
1f1c0 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 pNewItem->zD
1f1c1 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 atabase = sqlite
1f1c2 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 3DbStrDup(db, pO
1f1c3 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 ldItem->zDatabas
1f1c4 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d e);. pNewItem
1f1c5 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 ->zName = sqlite
1f1c6 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 3DbStrDup(db, pO
1f1c7 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a ldItem->zName);.
1f1c8 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 pNewItem->zA
1f1c9 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 lias = sqlite3Db
1f1ca 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 StrDup(db, pOldI
1f1cb 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 tem->zAlias);.
1f1cc 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e pNewItem->join
1f1cd 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d type = pOldItem-
1f1ce 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 >jointype;. p
1f1cf 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 NewItem->iCursor
1f1d0 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 = pOldItem->iCu
1f1d1 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 rsor;. pNewIt
1f1d2 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 em->isPopulated
1f1d3 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f = pOldItem->isPo
1f1d4 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 pulated;. pNe
1f1d5 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 wItem->zIndex =
1f1d6 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1f1d7 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 db, pOldItem->zI
1f1d8 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 ndex);. pNewI
1f1d9 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 tem->notIndexed
1f1da 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 = pOldItem->notI
1f1db 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 ndexed;. pNew
1f1dc 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 Item->pIndex = p
1f1dd 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b OldItem->pIndex;
1f1de 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77 . pTab = pNew
1f1df 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c Item->pTab = pOl
1f1e0 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 dItem->pTab;.
1f1e1 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 if( pTab ){.
1f1e2 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b pTab->nRef++;
1f1e3 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 . }. pNewI
1f1e4 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 tem->pSelect = s
1f1e5 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 qlite3SelectDup(
1f1e6 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 db, pOldItem->pS
1f1e7 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20 elect, flags);.
1f1e8 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e pNewItem->pOn
1f1e9 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
1f1ea 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
1f1eb 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 pOn, flags);.
1f1ec 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e pNewItem->pUsin
1f1ed 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 g = sqlite3IdLis
1f1ee 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 tDup(db, pOldIte
1f1ef 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 m->pUsing);.
1f1f0 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 pNewItem->colUse
1f1f1 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f d = pOldItem->co
1f1f2 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 lUsed;. }. ret
1f1f3 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 urn pNew;.}.SQLI
1f1f4 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 TE_PRIVATE IdLis
1f1f5 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 t *sqlite3IdList
1f1f6 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c Dup(sqlite3 *db,
1f1f7 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 IdList *p){. I
1f1f8 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 dList *pNew;. i
1f1f9 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 nt i;. if( p==0
1f1fa 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
1f1fb 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
1f1fc 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a allocRaw(db, siz
1f1fd 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 eof(*pNew) );.
1f1fe 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 if( pNew==0 ) re
1f1ff 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e turn 0;. pNew->
1f200 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c nId = pNew->nAll
1f201 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 oc = p->nId;. p
1f202 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 New->a = sqlite3
1f203 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
1f204 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d p->nId*sizeof(p-
1f205 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 >a[0]) );. if(
1f206 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 pNew->a==0 ){.
1f207 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f208 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 db, pNew);. r
1f209 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 eturn 0;. }. f
1f20a 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64 or(i=0; i<p->nId
1f20b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 ; i++){. stru
1f20c 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a ct IdList_item *
1f20d 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 pNewItem = &pNew
1f20e 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 ->a[i];. stru
1f20f 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a ct IdList_item *
1f210 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 pOldItem = &p->a
1f211 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 [i];. pNewIte
1f212 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 m->zName = sqlit
1f213 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
1f214 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b OldItem->zName);
1f215 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 . pNewItem->i
1f216 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 dx = pOldItem->i
1f217 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e dx;. }. return
1f218 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f pNew;.}.SQLITE_
1f219 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a PRIVATE Select *
1f21a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 sqlite3SelectDup
1f21b 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 (sqlite3 *db, Se
1f21c 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 lect *p, int fla
1f21d 67 73 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 gs){. Select *p
1f21e 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 New;. if( p==0
1f21f 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
1f220 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
1f221 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
1f222 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 of(*p) );. if(
1f223 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e pNew==0 ) return
1f224 20 30 3b 0a 20 20 2f 2a 20 41 6c 77 61 79 73 20 0;. /* Always
1f225 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
1f226 68 65 20 73 70 61 6e 20 66 6f 72 20 74 6f 70 2d he span for top-
1f227 6c 65 76 65 6c 20 65 78 70 72 65 73 73 69 6f 6e level expression
1f228 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 65 78 s in the. ** ex
1f229 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 pression list.
1f22a 54 68 65 20 6c 6f 67 69 63 20 69 6e 20 53 45 4c The logic in SEL
1f22b 45 43 54 20 70 72 6f 63 65 73 73 69 6e 67 20 74 ECT processing t
1f22c 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 20 hat determines.
1f22d 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 ** the names of
1f22e 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
1f22f 72 65 73 75 6c 74 20 73 65 74 20 6e 65 65 64 73 result set needs
1f230 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f this informatio
1f231 6e 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 70 45 4c n */. pNew->pEL
1f232 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ist = sqlite3Exp
1f233 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e rListDup(db, p->
1f234 70 45 4c 69 73 74 2c 20 66 6c 61 67 73 7c 45 58 pEList, flags|EX
1f235 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a 20 20 70 PRDUP_SPAN);. p
1f236 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 New->pSrc = sqli
1f237 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 te3SrcListDup(db
1f238 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 , p->pSrc, flags
1f239 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 );. pNew->pWher
1f23a 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 e = sqlite3ExprD
1f23b 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 up(db, p->pWhere
1f23c 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 , flags);. pNew
1f23d 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c ->pGroupBy = sql
1f23e 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
1f23f 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c db, p->pGroupBy,
1f240 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d flags);. pNew-
1f241 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 >pHaving = sqlit
1f242 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d e3ExprDup(db, p-
1f243 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 29 >pHaving, flags)
1f244 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 ;. pNew->pOrder
1f245 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 By = sqlite3Expr
1f246 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 ListDup(db, p->p
1f247 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29 3b OrderBy, flags);
1f248 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d . pNew->op = p-
1f249 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 >op;. pNew->pPr
1f24a 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c ior = sqlite3Sel
1f24b 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 ectDup(db, p->pP
1f24c 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 rior, flags);.
1f24d 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 pNew->pLimit = s
1f24e 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
1f24f 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61 , p->pLimit, fla
1f250 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 gs);. pNew->pOf
1f251 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78 fset = sqlite3Ex
1f252 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 prDup(db, p->pOf
1f253 66 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 fset, flags);.
1f254 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 pNew->iLimit = 0
1f255 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 ;. pNew->iOffse
1f256 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 t = 0;. pNew->s
1f257 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c elFlags = p->sel
1f258 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73 Flags & ~SF_Uses
1f259 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 Ephemeral;. pNe
1f25a 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 w->pRightmost =
1f25b 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 0;. pNew->addrO
1f25c 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b penEphm[0] = -1;
1f25d 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 . pNew->addrOpe
1f25e 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 nEphm[1] = -1;.
1f25f 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 pNew->addrOpenE
1f260 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 phm[2] = -1;. r
1f261 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 eturn pNew;.}.#e
1f262 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 lse.SQLITE_PRIVA
1f263 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 TE Select *sqlit
1f264 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 e3SelectDup(sqli
1f265 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 te3 *db, Select
1f266 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a *p, int flags){.
1f267 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 assert( p==0 )
1f268 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
1f269 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 #endif.../*.** A
1f26a 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 dd a new element
1f26b 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 to the end of a
1f26c 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 n expression lis
1f26d 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a t. If pList is.
1f26e 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c ** initially NUL
1f26f 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 L, then create a
1f270 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 new expression
1f271 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f list..*/.SQLITE_
1f272 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 PRIVATE ExprList
1f273 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 *sqlite3ExprLis
1f274 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65 tAppend(. Parse
1f275 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1f276 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
1f277 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c ntext */. ExprL
1f278 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 ist *pList,
1f279 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 /* List to wh
1f27a 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d ich to append. M
1f27b 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a ight be NULL */.
1f27c 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 Expr *pExpr,
1f27d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
1f27e 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 ression to be ap
1f27f 70 65 6e 64 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 pended */. Toke
1f280 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20 n *pName
1f281 20 20 20 20 2f 2a 20 41 53 20 6b 65 79 77 6f 72 /* AS keywor
1f282 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 d for the expres
1f283 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c sion */.){. sql
1f284 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
1f285 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 e->db;. if( pLi
1f286 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 st==0 ){. pLi
1f287 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 st = sqlite3DbMa
1f288 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
1f289 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b eof(ExprList) );
1f28a 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d . if( pList==
1f28b 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
1f28c 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 no_mem;. }.
1f28d 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d assert( pList-
1f28e 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 >nAlloc==0 );.
1f28f 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e }. if( pList->n
1f290 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 Alloc<=pList->nE
1f291 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 xpr ){. struc
1f292 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
1f293 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 *a;. int n =
1f294 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 pList->nAlloc*2
1f295 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c + 4;. a = sql
1f296 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 ite3DbRealloc(db
1f297 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 , pList->a, n*si
1f298 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d zeof(pList->a[0]
1f299 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 ));. if( a==0
1f29a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e ){. goto n
1f29b 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 o_mem;. }.
1f29c 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 pList->a = a;.
1f29d 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 pList->nAlloc
1f29e 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1f29f 6f 63 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69 ocSize(db, a)/si
1f2a0 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a zeof(a[0]);. }.
1f2a1 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d assert( pList-
1f2a2 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 >a!=0 );. if( p
1f2a3 45 78 70 72 20 7c 7c 20 70 4e 61 6d 65 20 29 7b Expr || pName ){
1f2a4 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 . struct Expr
1f2a5 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1f2a6 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 = &pList->a[pLi
1f2a7 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a 20 20 st->nExpr++];.
1f2a8 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c 20 memset(pItem,
1f2a9 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 65 6d 0, sizeof(*pItem
1f2aa 29 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a ));. pItem->z
1f2ab 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 Name = sqlite3Na
1f2ac 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 meFromToken(db,
1f2ad 70 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 pName);. pIte
1f2ae 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 m->pExpr = pExpr
1f2af 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c ;. pItem->iAl
1f2b0 69 61 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ias = 0;. }. r
1f2b1 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e 6f eturn pList;..no
1f2b2 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 20 2f 2a 20 _mem: . /*
1f2b3 41 76 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65 Avoid leaking me
1f2b4 6d 6f 72 79 20 69 66 20 6d 61 6c 6c 6f 63 20 68 mory if malloc h
1f2b5 61 73 20 66 61 69 6c 65 64 2e 20 2a 2f 0a 20 20 as failed. */.
1f2b6 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1f2b7 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 e(db, pExpr);.
1f2b8 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
1f2b9 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 elete(db, pList)
1f2ba 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
1f2bb 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 ./*.** If the ex
1f2bc 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 70 45 pression list pE
1f2bd 4c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 6d 6f List contains mo
1f2be 72 65 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 65 re than iLimit e
1f2bf 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 6c 65 61 76 lements,.** leav
1f2c0 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 e an error messa
1f2c1 67 65 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f ge in pParse..*/
1f2c2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f2c3 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1f2c4 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 ListCheckLength(
1f2c5 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1f2c6 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 ,. ExprList *pE
1f2c7 4c 69 73 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 List,. const ch
1f2c8 61 72 20 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20 ar *zObject.){.
1f2c9 20 69 6e 74 20 6d 78 20 3d 20 70 50 61 72 73 65 int mx = pParse
1f2ca 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ->db->aLimit[SQL
1f2cb 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e ITE_LIMIT_COLUMN
1f2cc 5d 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 ];. testcase( p
1f2cd 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d EList && pEList-
1f2ce 3e 6e 45 78 70 72 3d 3d 6d 78 20 29 3b 0a 20 20 >nExpr==mx );.
1f2cf 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 testcase( pEList
1f2d0 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 && pEList->nExp
1f2d1 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20 20 69 66 28 r==mx+1 );. if(
1f2d2 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 pEList && pELis
1f2d3 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 29 7b 0a 20 t->nExpr>mx ){.
1f2d4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f2d5 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 sg(pParse, "too
1f2d6 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 many columns in
1f2d7 25 73 22 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a 20 %s", zObject);.
1f2d8 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 }.}../*.** Dele
1f2d9 74 65 20 61 6e 20 65 6e 74 69 72 65 20 65 78 70 te an entire exp
1f2da 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f ression list..*/
1f2db 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f2dc 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1f2dd 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 ListDelete(sqlit
1f2de 65 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 e3 *db, ExprList
1f2df 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 *pList){. int
1f2e0 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 i;. struct Expr
1f2e1 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1f2e2 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 ;. if( pList==0
1f2e3 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
1f2e4 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 ert( pList->a!=0
1f2e5 20 7c 7c 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70 || (pList->nExp
1f2e6 72 3d 3d 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e r==0 && pList->n
1f2e7 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b 0a 20 20 61 Alloc==0) );. a
1f2e8 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 ssert( pList->nE
1f2e9 78 70 72 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c xpr<=pList->nAll
1f2ea 6f 63 20 29 3b 0a 20 20 66 6f 72 28 70 49 74 65 oc );. for(pIte
1f2eb 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b m=pList->a, i=0;
1f2ec 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pList->nExpr;
1f2ed 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
1f2ee 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
1f2ef 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d elete(db, pItem-
1f2f0 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 73 71 6c >pExpr);. sql
1f2f1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1f2f2 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Item->zName);.
1f2f3 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 }. sqlite3DbFre
1f2f4 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b e(db, pList->a);
1f2f5 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1f2f6 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a (db, pList);.}..
1f2f7 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 /*.** These rout
1f2f8 69 6e 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20 ines are Walker
1f2f9 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b callbacks. Walk
1f2fa 65 72 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69 er.u.pi is a poi
1f2fb 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e nter.** to an in
1f2fc 74 65 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f teger. These ro
1f2fd 75 74 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b utines are check
1f2fe 69 6e 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f ing an expressio
1f2ff 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69 n to see.** if i
1f300 74 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e t is a constant.
1f301 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e Set *Walker.u.
1f302 70 69 20 74 6f 20 30 20 69 66 20 74 68 65 20 65 pi to 0 if the e
1f303 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 xpression is.**
1f304 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a not constant..**
1f305 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61 .** These callba
1f306 63 6b 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 ck routines are
1f307 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
1f308 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a t the following:
1f309 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 .**.** sqlit
1f30a 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
1f30b 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
1f30c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 3ExprIsConstantN
1f30d 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20 otJoin().**
1f30e 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
1f30f 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 stantOrFunction(
1f310 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ).**.*/.static i
1f311 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e nt exprNodeIsCon
1f312 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 stant(Walker *pW
1f313 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
1f314 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57 pr){.. /* If pW
1f315 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20 alker->u.i is 3
1f316 74 68 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66 then any term of
1f317 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1f318 74 68 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a that comes from.
1f319 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 ** the ON or U
1f31a 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 SING clauses of
1f31b 61 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 a join disqualif
1f31c 69 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69 ies the expressi
1f31d 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 on. ** from bei
1f31e 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f ng considered co
1f31f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 nstant. */. if(
1f320 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 pWalker->u.i==3
1f321 20 26 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 && ExprHasAnyPr
1f322 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1f323 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 _FromJoin) ){.
1f324 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d pWalker->u.i =
1f325 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 0;. return W
1f326 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 RC_Abort;. }..
1f327 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e switch( pExpr->
1f328 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e op ){. /* Con
1f329 73 69 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 sider functions
1f32a 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69 to be constant i
1f32b 66 20 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75 f all their argu
1f32c 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61 ments are consta
1f32d 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57 nt. ** and pW
1f32e 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f alker->u.i==2 */
1f32f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e . case TK_FUN
1f330 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 CTION:. if(
1f331 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 pWalker->u.i==2
1f332 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
1f333 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 /* Fall throu
1f334 67 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 gh */. case T
1f335 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54 K_ID:. case T
1f336 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 K_COLUMN:. ca
1f337 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 se TK_AGG_FUNCTI
1f338 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ON:. case TK_
1f339 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 23 69 66 6e AGG_COLUMN:.#ifn
1f33a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f33b 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 SUBQUERY. cas
1f33c 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 e TK_SELECT:.
1f33d 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a case TK_EXISTS:
1f33e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1f33f 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 pExpr->op==TK_S
1f340 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 74 ELECT );. t
1f341 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
1f342 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b op==TK_EXISTS );
1f343 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 74 65 .#endif. te
1f344 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
1f345 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 20 20 20 p==TK_ID );.
1f346 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
1f347 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e r->op==TK_COLUMN
1f348 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1f349 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
1f34a 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 K_AGG_FUNCTION )
1f34b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1f34c 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
1f34d 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 AGG_COLUMN );.
1f34e 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 pWalker->u.i
1f34f 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 = 0;. retu
1f350 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1f351 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
1f352 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 return WRC_Cont
1f353 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 inue;. }.}.stat
1f354 69 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64 ic int selectNod
1f355 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b eIsConstant(Walk
1f356 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c er *pWalker, Sel
1f357 65 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 ect *NotUsed){.
1f358 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1f359 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57 R(NotUsed);. pW
1f35a 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a alker->u.i = 0;.
1f35b 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
1f35c 72 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 rt;.}.static int
1f35d 20 65 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70 exprIsConst(Exp
1f35e 72 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c r *p, int initFl
1f35f 61 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b ag){. Walker w;
1f360 0a 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46 . w.u.i = initF
1f361 6c 61 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 lag;. w.xExprCa
1f362 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64 llback = exprNod
1f363 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 eIsConstant;. w
1f364 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b .xSelectCallback
1f365 20 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 = selectNodeIsC
1f366 6f 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74 onstant;. sqlit
1f367 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 e3WalkExpr(&w, p
1f368 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e );. return w.u.
1f369 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b i;.}../*.** Walk
1f36a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
1f36b 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 ree. Return 1 i
1f36c 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1f36d 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 is constant.**
1f36e 61 6e 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f and 0 if it invo
1f36f 6c 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f lves variables o
1f370 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 r function calls
1f371 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 ..**.** For the
1f372 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 purposes of this
1f373 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 function, a dou
1f374 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e ble-quoted strin
1f375 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a g (ex: "abc").**
1f376 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 is considered a
1f377 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 variable but a
1f378 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 single-quoted st
1f379 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 ring (ex: 'abc')
1f37a 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e is.** a constan
1f37b 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
1f37c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1f37d 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 ExprIsConstant(E
1f37e 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 xpr *p){. retur
1f37f 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c n exprIsConst(p,
1f380 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 1);.}../*.** Wa
1f381 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e lk an expression
1f382 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 tree. Return 1
1f383 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 if the expressi
1f384 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a on is constant.*
1f385 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f * that does no o
1f386 72 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 riginate from th
1f387 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c e ON or USING cl
1f388 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e auses of a join.
1f389 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 .** Return 0 if
1f38a 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 it involves vari
1f38b 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f ables or functio
1f38c 6e 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 n calls or terms
1f38d 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f from.** an ON o
1f38e 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a r USING clause..
1f38f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f390 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
1f391 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f rIsConstantNotJo
1f392 69 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 in(Expr *p){. r
1f393 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 eturn exprIsCons
1f394 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a t(p, 3);.}../*.*
1f395 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 * Walk an expres
1f396 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 sion tree. Retu
1f397 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 rn 1 if the expr
1f398 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 ession is consta
1f399 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74 nt.** or a funct
1f39a 69 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f ion call with co
1f39b 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73 nstant arguments
1f39c 2e 20 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20 . Return and 0
1f39d 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 if there.** are
1f39e 61 6e 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a any variables..*
1f39f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 *.** For the pur
1f3a0 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 poses of this fu
1f3a1 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 nction, a double
1f3a2 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 -quoted string (
1f3a3 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 ex: "abc").** is
1f3a4 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 considered a va
1f3a5 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e riable but a sin
1f3a6 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e gle-quoted strin
1f3a7 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 g (ex: 'abc') is
1f3a8 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a .** a constant..
1f3a9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f3aa 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
1f3ab 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e rIsConstantOrFun
1f3ac 63 74 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a ction(Expr *p){.
1f3ad 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 return exprIsC
1f3ae 6f 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f onst(p, 2);.}../
1f3af 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 *.** If the expr
1f3b0 65 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61 ession p codes a
1f3b1 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 constant intege
1f3b2 72 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20 r that is small
1f3b3 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74 enough.** to fit
1f3b4 20 69 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 in a 32-bit int
1f3b5 65 67 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61 eger, return 1 a
1f3b6 6e 64 20 70 75 74 20 74 68 65 20 76 61 6c 75 65 nd put the value
1f3b7 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a of the integer.
1f3b8 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20 ** in *pValue.
1f3b9 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
1f3ba 6e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 n is not an inte
1f3bb 67 65 72 20 6f 72 20 69 66 20 69 74 20 69 73 20 ger or if it is
1f3bc 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69 too big.** to fi
1f3bd 74 20 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32 t in a signed 32
1f3be 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 -bit integer, re
1f3bf 74 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65 turn 0 and leave
1f3c0 20 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67 *pValue unchang
1f3c1 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1f3c2 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1f3c3 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 3ExprIsInteger(E
1f3c4 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61 xpr *p, int *pVa
1f3c5 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d lue){. int rc =
1f3c6 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 0;. if( p->fla
1f3c7 67 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 gs & EP_IntValue
1f3c8 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 ){. *pValue
1f3c9 3d 20 70 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 = p->iTable;.
1f3ca 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
1f3cb 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 switch( p->op )
1f3cc 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e {. case TK_IN
1f3cd 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 TEGER: {. r
1f3ce 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49 6e c = sqlite3GetIn
1f3cf 74 33 32 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f t32((char*)p->to
1f3d0 6b 65 6e 2e 7a 2c 20 70 56 61 6c 75 65 29 3b 0a ken.z, pValue);.
1f3d1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1f3d2 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 }. case TK_U
1f3d3 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 72 63 PLUS: {. rc
1f3d4 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 = sqlite3ExprIs
1f3d5 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 Integer(p->pLeft
1f3d6 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 , pValue);.
1f3d7 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1f3d8 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 case TK_UMINUS
1f3d9 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 3b : {. int v;
1f3da 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1f3db 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
1f3dc 70 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29 7b p->pLeft, &v) ){
1f3dd 0a 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75 65 . *pValue
1f3de 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 72 = -v;. r
1f3df 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 1;. }.
1f3e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1f3e1 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 62 }. default: b
1f3e2 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 reak;. }. if(
1f3e3 72 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 70 20 rc ){. p->op
1f3e4 3d 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 = TK_INTEGER;.
1f3e5 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 p->flags |= EP
1f3e6 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70 _IntValue;. p
1f3e7 2d 3e 69 54 61 62 6c 65 20 3d 20 2a 70 56 61 6c ->iTable = *pVal
1f3e8 75 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ue;. }. return
1f3e9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
1f3ea 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
1f3eb 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73 given string is
1f3ec 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e a row-id column
1f3ed 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 name..*/.SQLITE
1f3ee 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1f3ef 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 ite3IsRowid(cons
1f3f0 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 t char *z){. if
1f3f1 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
1f3f2 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d (z, "_ROWID_")==
1f3f3 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 0 ) return 1;.
1f3f4 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1f3f5 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d mp(z, "ROWID")==
1f3f6 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 0 ) return 1;.
1f3f7 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1f3f8 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20 mp(z, "OID")==0
1f3f9 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 ) return 1;. re
1f3fa 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
1f3fb 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
1f3fc 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 the IN operator
1f3fd 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 optimization is
1f3fe 65 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 74 enabled and.** t
1f3ff 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
1f400 65 6e 74 20 70 20 65 78 69 73 74 73 20 61 6e 64 ent p exists and
1f401 20 69 73 20 6f 66 20 74 68 65 0a 2a 2a 20 73 69 is of the.** si
1f402 6d 70 6c 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a mple form:.**.**
1f403 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c SELECT <col
1f404 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 umn> FROM <table
1f405 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 >.**.** If this
1f406 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20 is the case, it
1f407 6d 61 79 20 62 65 20 70 6f 73 73 69 62 6c 65 20 may be possible
1f408 74 6f 20 75 73 65 20 61 6e 20 65 78 69 73 74 69 to use an existi
1f409 6e 67 20 74 61 62 6c 65 0a 2a 2a 20 6f 72 20 69 ng table.** or i
1f40a 6e 64 65 78 20 69 6e 73 74 65 61 64 20 6f 66 20 ndex instead of
1f40b 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 65 70 generating an ep
1f40c 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a 2a heremal table..*
1f40d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1f40e 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 73 _OMIT_SUBQUERY.s
1f40f 74 61 74 69 63 20 69 6e 74 20 69 73 43 61 6e 64 tatic int isCand
1f410 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 53 65 idateForInOpt(Se
1f411 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c lect *p){. SrcL
1f412 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 45 78 70 ist *pSrc;. Exp
1f413 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 rList *pEList;.
1f414 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 Table *pTab;.
1f415 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1f416 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 n 0;
1f417 20 20 20 20 20 20 20 2f 2a 20 72 69 67 68 74 2d /* right-
1f418 68 61 6e 64 20 73 69 64 65 20 6f 66 20 49 4e 20 hand side of IN
1f419 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 is SELECT */. i
1f41a 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 f( p->pPrior ) r
1f41b 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
1f41c 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 /* Not a c
1f41d 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a ompound SELECT *
1f41e 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c /. if( p->selFl
1f41f 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e ags & (SF_Distin
1f420 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 ct|SF_Aggregate)
1f421 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1f422 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 0; /* No DISTIN
1f423 43 54 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e CT keyword and n
1f424 6f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 o aggregate func
1f425 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 tions */. }. i
1f426 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 f( p->pGroupBy )
1f427 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 return 0;
1f428 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 /* Has no
1f429 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1f42a 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d */. if( p->pLim
1f42b 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 it ) return 0;
1f42c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
1f42d 61 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 as no LIMIT clau
1f42e 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 se */. if( p->p
1f42f 4f 66 66 73 65 74 20 29 20 72 65 74 75 72 6e 20 Offset ) return
1f430 30 3b 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 0;. if( p->pWhe
1f431 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 re ) return 0;
1f432 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
1f433 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 as no WHERE clau
1f434 73 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70 se */. pSrc = p
1f435 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 ->pSrc;. assert
1f436 28 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 69 ( pSrc!=0 );. i
1f437 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 f( pSrc->nSrc!=1
1f438 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1f439 20 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 20 /* Single
1f43a 74 65 72 6d 20 69 6e 20 46 52 4f 4d 20 63 6c 61 term in FROM cla
1f43b 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 use */. if( pSr
1f43c 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20 c->a[0].pSelect
1f43d 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
1f43e 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 /* FROM clause i
1f43f 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65 72 79 s not a subquery
1f440 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 70 53 72 */. pTab = pSr
1f441 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 c->a[0].pTab;.
1f442 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65 if( pTab==0 ) re
1f443 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 54 turn 0;. if( pT
1f444 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 72 65 ab->pSelect ) re
1f445 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 turn 0;
1f446 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 /* FROM clause
1f447 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f is not a view */
1f448 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c . if( IsVirtual
1f449 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 (pTab) ) return
1f44a 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 0; /* FRO
1f44b 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20 76 M clause not a v
1f44c 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a irtual table */.
1f44d 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 pEList = p->pE
1f44e 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 List;. if( pELi
1f44f 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 st->nExpr!=1 ) r
1f450 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 2f eturn 0; /
1f451 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 * One column in
1f452 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a the result set *
1f453 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e /. if( pEList->
1f454 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d a[0].pExpr->op!=
1f455 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 TK_COLUMN ) retu
1f456 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20 rn 0; /* Result
1f457 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 is a column */.
1f458 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
1f459 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1f45a 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a IT_SUBQUERY */..
1f45b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1f45c 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 ion is used by t
1f45d 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
1f45e 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e n of the IN (...
1f45f 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49 ) operator..** I
1f460 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66 69 t's job is to fi
1f461 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20 62 nd or create a b
1f462 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 -tree structure
1f463 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 that may be used
1f464 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74 65 .** either to te
1f465 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 st for membershi
1f466 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20 73 p of the (...) s
1f467 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74 65 et or to iterate
1f468 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20 through.** its
1f469 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 6e members, skippin
1f46a 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a g duplicates..**
1f46b 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 6f .** The cursor o
1f46c 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 73 74 72 pened on the str
1f46d 75 63 74 75 72 65 20 28 64 61 74 61 62 61 73 65 ucture (database
1f46e 20 74 61 62 6c 65 2c 20 64 61 74 61 62 61 73 65 table, database
1f46f 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 72 20 65 70 index .** or ep
1f470 68 65 72 6d 61 6c 20 74 61 62 6c 65 29 20 69 73 hermal table) is
1f471 20 73 74 6f 72 65 64 20 69 6e 20 70 58 2d 3e 69 stored in pX->i
1f472 54 61 62 6c 65 20 62 65 66 6f 72 65 20 74 68 69 Table before thi
1f473 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
1f474 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ns..** The retur
1f475 6e 65 64 20 76 61 6c 75 65 20 69 6e 64 69 63 61 ned value indica
1f476 74 65 73 20 74 68 65 20 73 74 72 75 63 74 75 72 tes the structur
1f477 65 20 74 79 70 65 2c 20 61 73 20 66 6f 6c 6c 6f e type, as follo
1f478 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 5f 49 ws:.**.** IN_I
1f479 4e 44 45 58 5f 52 4f 57 49 44 20 2d 20 54 68 65 NDEX_ROWID - The
1f47a 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e cursor was open
1f47b 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 ed on a database
1f47c 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 49 4e 5f table..** IN_
1f47d 49 4e 44 45 58 5f 49 4e 44 45 58 20 2d 20 54 68 INDEX_INDEX - Th
1f47e 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 e cursor was ope
1f47f 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 ned on a databas
1f480 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 20 20 49 4e e index..** IN
1f481 5f 49 4e 44 45 58 5f 45 50 48 20 2d 20 20 20 54 _INDEX_EPH - T
1f482 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 he cursor was op
1f483 65 6e 65 64 20 6f 6e 20 61 20 73 70 65 63 69 61 ened on a specia
1f484 6c 6c 79 20 63 72 65 61 74 65 64 20 61 6e 64 0a lly created and.
1f485 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1f486 20 20 20 20 20 20 70 6f 70 75 6c 61 74 65 64 20 populated
1f487 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e epheremal table.
1f488 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 69 73 74 69 .**.** An existi
1f489 6e 67 20 73 74 72 75 63 74 75 72 65 20 6d 61 79 ng structure may
1f48a 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69 66 only be used if
1f48b 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 20 6f the SELECT is o
1f48c 66 20 74 68 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 f the simple.**
1f48d 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 form:.**.**
1f48e 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 SELECT <column>
1f48f 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a FROM <table>.**.
1f490 2a 2a 20 49 66 20 70 72 4e 6f 74 46 6f 75 6e 64 ** If prNotFound
1f491 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c parameter is 0,
1f492 20 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 then the struct
1f493 75 72 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 ure will be used
1f494 20 74 6f 20 69 74 65 72 61 74 65 0a 2a 2a 20 74 to iterate.** t
1f495 68 72 6f 75 67 68 20 74 68 65 20 73 65 74 20 6d hrough the set m
1f496 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 embers, skipping
1f497 20 61 6e 79 20 64 75 70 6c 69 63 61 74 65 73 2e any duplicates.
1f498 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e In this case an
1f499 0a 2a 2a 20 65 70 68 65 72 65 6d 61 6c 20 74 61 .** epheremal ta
1f49a 62 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 ble must be used
1f49b 20 75 6e 6c 65 73 73 20 74 68 65 20 73 65 6c 65 unless the sele
1f49c 63 74 65 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 cted <column> is
1f49d 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 guaranteed.** t
1f49e 6f 20 62 65 20 75 6e 69 71 75 65 20 2d 20 65 69 o be unique - ei
1f49f 74 68 65 72 20 62 65 63 61 75 73 65 20 69 74 20 ther because it
1f4a0 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 is an INTEGER PR
1f4a1 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 69 74 0a IMARY KEY or it.
1f4a2 2a 2a 20 69 73 20 75 6e 69 71 75 65 20 62 79 20 ** is unique by
1f4a3 76 69 72 74 75 65 20 6f 66 20 61 20 63 6f 6e 73 virtue of a cons
1f4a4 74 72 61 69 6e 74 20 6f 72 20 69 6d 70 6c 69 63 traint or implic
1f4a5 69 74 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 it index..**.**
1f4a6 49 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e If the prNotFoun
1f4a7 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e d parameter is n
1f4a8 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73 ot 0, then the s
1f4a9 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65 tructure will be
1f4aa 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 66 61 used .** for fa
1f4ab 73 74 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69 st set membershi
1f4ac 70 20 74 65 73 74 73 2e 20 49 6e 20 74 68 69 73 p tests. In this
1f4ad 20 63 61 73 65 20 61 6e 20 65 70 68 65 72 65 6d case an epherem
1f4ae 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 0a 2a al table must .*
1f4af 2a 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 * be used unless
1f4b0 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 61 6e 20 <column> is an
1f4b1 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
1f4b2 4b 45 59 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 KEY or an index
1f4b3 63 61 6e 20 0a 2a 2a 20 62 65 20 66 6f 75 6e 64 can .** be found
1f4b4 20 77 69 74 68 20 3c 63 6f 6c 75 6d 6e 3e 20 61 with <column> a
1f4b5 73 20 69 74 73 20 6c 65 66 74 2d 6d 6f 73 74 20 s its left-most
1f4b6 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 column..**.** Wh
1f4b7 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 en the structure
1f4b8 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 is being used f
1f4b9 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69 or set membershi
1f4ba 70 20 74 65 73 74 73 2c 20 74 68 65 20 75 73 65 p tests, the use
1f4bb 72 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 6b 6e r.** needs to kn
1f4bc 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ow whether or no
1f4bd 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 t the structure
1f4be 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 contains an SQL
1f4bf 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c 75 65 20 69 NULL .** value i
1f4c0 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 72 72 65 n order to corre
1f4c1 63 74 6c 79 20 65 76 61 6c 75 61 74 65 20 65 78 ctly evaluate ex
1f4c2 70 72 65 73 73 69 6f 6e 73 20 6c 69 6b 65 20 22 pressions like "
1f4c3 58 20 49 4e 20 28 59 2c 20 5a 29 22 2e 0a 2a 2a X IN (Y, Z)"..**
1f4c4 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 63 If there is a c
1f4c5 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 73 hance that the s
1f4c6 74 72 75 63 74 75 72 65 20 6d 61 79 20 63 6f 6e tructure may con
1f4c7 74 61 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c 75 tain a NULL valu
1f4c8 65 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2c e at.** runtime,
1f4c9 20 74 68 65 6e 20 61 20 72 65 67 69 73 74 65 72 then a register
1f4ca 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
1f4cb 64 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e d the register n
1f4cc 75 6d 62 65 72 20 77 72 69 74 74 65 6e 0a 2a 2a umber written.**
1f4cd 20 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2e to *prNotFound.
1f4ce 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
1f4cf 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 chance that the
1f4d0 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
1f4d1 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c 20 76 61 6c ns a.** NULL val
1f4d2 75 65 2c 20 74 68 65 6e 20 2a 70 72 4e 6f 74 46 ue, then *prNotF
1f4d3 6f 75 6e 64 20 69 73 20 6c 65 66 74 20 75 6e 63 ound is left unc
1f4d4 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 hanged..**.** If
1f4d5 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 a register is a
1f4d6 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 74 73 llocated and its
1f4d7 20 6c 6f 63 61 74 69 6f 6e 20 73 74 6f 72 65 64 location stored
1f4d8 20 69 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2c in *prNotFound,
1f4d9 20 74 68 65 6e 0a 2a 2a 20 69 74 73 20 69 6e 69 then.** its ini
1f4da 74 69 61 6c 20 76 61 6c 75 65 20 69 73 20 4e 55 tial value is NU
1f4db 4c 4c 2e 20 49 66 20 74 68 65 20 73 74 72 75 63 LL. If the struc
1f4dc 74 75 72 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 ture does not re
1f4dd 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a main constant.**
1f4de 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f for the duratio
1f4df 6e 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 28 n of the query (
1f4e0 69 2e 65 2e 20 74 68 65 20 73 65 74 20 69 73 20 i.e. the set is
1f4e1 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 62 a correlated sub
1f4e2 2d 73 65 6c 65 63 74 29 2c 20 0a 2a 2a 20 74 68 -select), .** th
1f4e3 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 e value of the a
1f4e4 6c 6c 6f 63 61 74 65 64 20 72 65 67 69 73 74 65 llocated registe
1f4e5 72 20 69 73 20 72 65 73 65 74 20 74 6f 20 4e 55 r is reset to NU
1f4e6 4c 4c 20 65 61 63 68 20 74 69 6d 65 20 74 68 65 LL each time the
1f4e7 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 .** structure i
1f4e8 73 20 72 65 70 6f 70 75 6c 61 74 65 64 2e 20 54 s repopulated. T
1f4e9 68 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 his allows the c
1f4ea 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 64 62 aller to use vdb
1f4eb 65 20 63 6f 64 65 20 0a 2a 2a 20 65 71 75 69 76 e code .** equiv
1f4ec 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c alent to the fol
1f4ed 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
1f4ee 69 66 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 if( register==NU
1f4ef 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 LL ){.** has
1f4f0 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 _null = <test if
1f4f1 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 data structure
1f4f2 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a contains null>.*
1f4f3 2a 20 20 20 20 20 72 65 67 69 73 74 65 72 20 3d * register =
1f4f4 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 1.** }.**.**
1f4f5 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 in order to avoi
1f4f6 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 d running the <t
1f4f7 65 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75 est if data stru
1f4f8 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e cture contains n
1f4f9 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 ull>.** test mor
1f4fa 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20 e often than is
1f4fb 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 necessary..*/.#i
1f4fc 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f4fd 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 T_SUBQUERY.SQLIT
1f4fe 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f4ff 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 lite3FindInIndex
1f500 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1f501 45 78 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 Expr *pX, int *p
1f502 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 rNotFound){. Se
1f503 6c 65 63 74 20 2a 70 3b 0a 20 20 69 6e 74 20 65 lect *p;. int e
1f504 54 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 Type = 0;. int
1f505 69 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e iTab = pParse->n
1f506 54 61 62 2b 2b 3b 0a 20 20 69 6e 74 20 6d 75 73 Tab++;. int mus
1f507 74 42 65 55 6e 69 71 75 65 20 3d 20 21 70 72 4e tBeUnique = !prN
1f508 6f 74 46 6f 75 6e 64 3b 0a 0a 20 20 2f 2a 20 54 otFound;.. /* T
1f509 68 65 20 66 6f 6c 6c 77 69 6e 67 20 69 66 28 2e he follwing if(.
1f50a 2e 2e 29 20 65 78 70 72 65 73 73 69 6f 6e 20 69 ..) expression i
1f50b 73 20 74 72 75 65 20 69 66 20 74 68 65 20 53 45 s true if the SE
1f50c 4c 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 0a LECT is of the .
1f50d 20 20 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72 6d ** simple form
1f50e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 :. **. **
1f50f 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 SELECT <column>
1f510 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 20 20 2a FROM <table>. *
1f511 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 *. ** If this i
1f512 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20 6d s the case, it m
1f513 61 79 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 ay be possible t
1f514 6f 20 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e o use an existin
1f515 67 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 72 20 g table. ** or
1f516 69 6e 64 65 78 20 69 6e 73 74 65 61 64 20 6f 66 index instead of
1f517 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 65 generating an e
1f518 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a pheremal table..
1f519 20 20 2a 2f 0a 20 20 70 20 3d 20 28 45 78 70 72 */. p = (Expr
1f51a 48 61 73 50 72 6f 70 65 72 74 79 28 70 58 2c 20 HasProperty(pX,
1f51b 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 3f 20 EP_xIsSelect) ?
1f51c 70 58 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3a 20 pX->x.pSelect :
1f51d 30 29 3b 0a 20 20 69 66 28 20 69 73 43 61 6e 64 0);. if( isCand
1f51e 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29 idateForInOpt(p)
1f51f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 ){. sqlite3
1f520 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1f521 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1f522 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
1f523 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 ction */. Exp
1f524 72 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e 70 45 r *pExpr = p->pE
1f525 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 List->a[0].pExpr
1f526 3b 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f ; /* Expressio
1f527 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 n <column> */.
1f528 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 int iCol = pEx
1f529 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 pr->iColumn;
1f52a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
1f52b 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 3c 63 6f 6c x of column <col
1f52c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 56 64 62 65 umn> */. Vdbe
1f52d 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 *v = sqlite3Get
1f52e 56 64 62 65 28 70 50 61 72 73 65 29 3b 20 20 20 Vdbe(pParse);
1f52f 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 /* Virtual ma
1f530 63 68 69 6e 65 20 62 65 69 6e 67 20 63 6f 64 65 chine being code
1f531 64 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a d */. Table *
1f532 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e pTab = p->pSrc->
1f533 61 5b 30 5d 2e 70 54 61 62 3b 20 20 20 20 20 20 a[0].pTab;
1f534 2f 2a 20 54 61 62 6c 65 20 3c 74 61 62 6c 65 3e /* Table <table>
1f535 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 . */. int iDb
1f536 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1f537 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f538 2f 2a 20 44 61 74 61 62 61 73 65 20 69 64 78 20 /* Database idx
1f539 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 20 0a for pTab */. .
1f53a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f /* Code an O
1f53b 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 61 P_VerifyCookie a
1f53c 6e 64 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 nd OP_TableLock
1f53d 66 6f 72 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a for <table>. */.
1f53e 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 iDb = sqlite
1f53f 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 3SchemaToIndex(d
1f540 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
1f541 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f );. sqlite3Co
1f542 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 deVerifySchema(p
1f543 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 Parse, iDb);.
1f544 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
1f545 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 k(pParse, iDb, p
1f546 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 Tab->tnum, 0, pT
1f547 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 ab->zName);..
1f548 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f /* This functio
1f549 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 n is only called
1f54a 20 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 from two places
1f54b 2e 20 49 6e 20 62 6f 74 68 20 63 61 73 65 73 20 . In both cases
1f54c 74 68 65 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 the vdbe. **
1f54d 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
1f54e 20 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 allocated. So a
1f54f 73 73 75 6d 65 20 73 71 6c 69 74 65 33 47 65 74 ssume sqlite3Get
1f550 56 64 62 65 28 29 20 69 73 20 61 6c 77 61 79 73 Vdbe() is always
1f551 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66 . ** successf
1f552 75 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a ul here.. */.
1f553 20 20 20 20 61 73 73 65 72 74 28 76 29 3b 0a 20 assert(v);.
1f554 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b if( iCol<0 ){
1f555 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 . int iMem
1f556 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
1f557 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 ;. int iAdd
1f558 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 r;. sqlite3
1f559 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c VdbeUsesBtree(v,
1f55a 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 69 41 iDb);.. iA
1f55b 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
1f55c 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 eAddOp1(v, OP_If
1f55d 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 , iMem);. s
1f55e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1f55f 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1f560 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20 1, iMem);..
1f561 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c sqlite3OpenTabl
1f562 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 e(pParse, iTab,
1f563 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 iDb, pTab, OP_Op
1f564 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65 enRead);. e
1f565 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f Type = IN_INDEX_
1f566 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71 ROWID;.. sq
1f567 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
1f568 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 e(v, iAddr);.
1f569 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e }else{. In
1f56a 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 dex *pIdx;
1f56b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f56c 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 /* Iterator v
1f56d 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 ariable */..
1f56e 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 /* The collati
1f56f 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 on sequence used
1f570 20 62 79 20 74 68 65 20 63 6f 6d 70 61 72 69 73 by the comparis
1f571 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65 78 20 on. If an index
1f572 69 73 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 is to . **
1f573 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 be used in place
1f574 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c 65 of a temp-table
1f575 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72 64 , it must be ord
1f576 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a 20 ered according.
1f577 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 ** to this
1f578 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
1f579 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f ce. */. Co
1f57a 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71 llSeq *pReq = sq
1f57b 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 lite3BinaryCompa
1f57c 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 reCollSeq(pParse
1f57d 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 , pX->pLeft, pEx
1f57e 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 pr);.. /* C
1f57f 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66 heck that the af
1f580 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c finity that will
1f581 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66 be used to perf
1f582 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a orm the . *
1f583 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 * comparison is
1f584 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 the same as the
1f585 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 affinity of the
1f586 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20 column. If.
1f587 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69 ** it is not, i
1f588 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
1f589 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64 e to use any ind
1f58a 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ex.. */.
1f58b 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f char aff = co
1f58c 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 mparisonAffinity
1f58d 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 (pX);. int
1f58e 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 70 affinity_ok = (p
1f58f 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e Tab->aCol[iCol].
1f590 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c 7c 61 affinity==aff||a
1f591 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ff==SQLITE_AFF_N
1f592 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72 ONE);.. for
1f593 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 (pIdx=pTab->pInd
1f594 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 79 70 ex; pIdx && eTyp
1f595 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 74 79 e==0 && affinity
1f596 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e _ok; pIdx=pIdx->
1f597 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 pNext){.
1f598 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 6f 6c if( (pIdx->aiCol
1f599 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20 umn[0]==iCol).
1f59a 20 20 20 20 20 20 20 26 26 20 28 70 52 65 71 3d && (pReq=
1f59b 3d 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c =sqlite3FindColl
1f59c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c Seq(db, ENC(db),
1f59d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d pIdx->azColl[0]
1f59e 2c 20 2d 31 2c 20 30 29 29 0a 20 20 20 20 20 20 , -1, 0)).
1f59f 20 20 20 26 26 20 28 21 6d 75 73 74 42 65 55 6e && (!mustBeUn
1f5a0 69 71 75 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e ique || (pIdx->n
1f5a1 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 Column==1 && pId
1f5a2 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e x->onError!=OE_N
1f5a3 6f 6e 65 29 29 0a 20 20 20 20 20 20 20 20 29 7b one)). ){
1f5a4 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
1f5a5 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e Mem = ++pParse->
1f5a6 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 nMem;.
1f5a7 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 int iAddr;.
1f5a8 20 20 20 20 20 63 68 61 72 20 2a 70 4b 65 79 3b char *pKey;
1f5a9 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 4b . . pK
1f5aa 65 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c ey = (char *)sql
1f5ab 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f ite3IndexKeyinfo
1f5ac 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a (pParse, pIdx);.
1f5ad 20 20 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 iDb =
1f5ae 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
1f5af 6e 64 65 78 28 64 62 2c 20 70 49 64 78 2d 3e 70 ndex(db, pIdx->p
1f5b0 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 Schema);.
1f5b1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 sqlite3VdbeUs
1f5b2 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b esBtree(v, iDb);
1f5b3 0a 0a 20 20 20 20 20 20 20 20 20 20 69 41 64 64 .. iAdd
1f5b4 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
1f5b5 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 ddOp1(v, OP_If,
1f5b6 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 iMem);.
1f5b7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f5b8 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
1f5b9 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20 , 1, iMem);. .
1f5ba 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1f5bb 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1f5bc 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c _OpenRead, iTab,
1f5bd 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 pIdx->tnum, iDb
1f5be 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1f5bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5c0 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f pKey,P4_KEYINFO
1f5c1 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 _HANDOFF);.
1f5c2 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 VdbeComment
1f5c3 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d ((v, "%s", pIdx-
1f5c4 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 >zName));.
1f5c5 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 eType = IN_I
1f5c6 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 NDEX_INDEX;..
1f5c7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1f5c8 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 beJumpHere(v, iA
1f5c9 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ddr);.
1f5ca 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 if( prNotFound &
1f5cb 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 & !pTab->aCol[iC
1f5cc 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 ol].notNull ){.
1f5cd 20 20 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f *prNo
1f5ce 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 tFound = ++pPars
1f5cf 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 e->nMem;.
1f5d0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1f5d1 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1f5d2 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30 .. if( eType==0
1f5d3 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 ){. int rMay
1f5d4 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 HaveNull = 0;.
1f5d5 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 eType = IN_IND
1f5d6 45 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 EX_EPH;. if(
1f5d7 70 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 prNotFound ){.
1f5d8 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 *prNotFound
1f5d9 3d 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d = rMayHaveNull =
1f5da 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
1f5db 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
1f5dc 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d X->pLeft->iColum
1f5dd 6e 3c 30 20 26 26 20 21 45 78 70 72 48 61 73 41 n<0 && !ExprHasA
1f5de 6e 79 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 nyProperty(pX, E
1f5df 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a P_xIsSelect) ){.
1f5e0 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e eType = IN
1f5e1 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 _INDEX_ROWID;.
1f5e2 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 }. sqlite3C
1f5e3 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 odeSubselect(pPa
1f5e4 72 73 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 rse, pX, rMayHav
1f5e5 65 4e 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e eNull, eType==IN
1f5e6 5f 49 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 _INDEX_ROWID);.
1f5e7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e }else{. pX->
1f5e8 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 iTable = iTab;.
1f5e9 20 7d 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70 }. return eTyp
1f5ea 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a e;.}.#endif../*.
1f5eb 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1f5ec 20 66 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 for scalar subq
1f5ed 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20 61 ueries used as a
1f5ee 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 n expression.**
1f5ef 61 6e 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 and IN operators
1f5f0 2e 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a . Examples:.**.
1f5f1 2a 2a 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 ** (SELECT a
1f5f2 20 46 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 FROM b)
1f5f3 20 20 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a -- subquery.**
1f5f4 20 20 20 20 20 45 58 49 53 54 53 20 28 53 45 4c EXISTS (SEL
1f5f5 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 ECT a FROM b)
1f5f6 2d 2d 20 45 58 49 53 54 53 20 73 75 62 71 75 65 -- EXISTS subque
1f5f7 72 79 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 ry.** x IN (
1f5f8 34 2c 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 4,5,11)
1f5f9 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 -- IN opera
1f5fa 74 6f 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e tor with list on
1f5fb 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
1f5fc 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 .** x IN (SE
1f5fd 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 LECT a FROM b)
1f5fe 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f -- IN operato
1f5ff 72 20 77 69 74 68 20 73 75 62 71 75 65 72 79 20 r with subquery
1f600 6f 6e 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a on the right.**.
1f601 2a 2a 20 54 68 65 20 70 45 78 70 72 20 70 61 72 ** The pExpr par
1f602 61 6d 65 74 65 72 20 64 65 73 63 72 69 62 65 73 ameter describes
1f603 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1f604 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
1f605 65 20 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 e IN.** operator
1f606 20 6f 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a or subquery..**
1f607 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 .** If parameter
1f608 20 69 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d isRowid is non-
1f609 7a 65 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 zero, then expre
1f60a 73 73 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67 ssion pExpr is g
1f60b 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 uaranteed.** to
1f60c 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 be of the form "
1f60d 3c 72 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f <rowid> IN (?, ?
1f60e 2c 20 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f , ?)", where <ro
1f60f 77 69 64 3e 20 69 73 20 61 20 72 65 66 65 72 65 wid> is a refere
1f610 6e 63 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 nce.** to some i
1f611 6e 74 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d nteger key colum
1f612 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54 n of a table B-T
1f613 72 65 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ree. In this cas
1f614 65 2c 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 e, use an.** int
1f615 6b 65 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74 key B-Tree to st
1f616 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 49 ore the set of I
1f617 4e 28 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e N(...) values in
1f618 73 74 65 61 64 20 6f 66 20 74 68 65 20 75 73 75 stead of the usu
1f619 61 6c 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 al.** (slower) v
1f61a 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b ariable length k
1f61b 65 79 73 20 42 2d 54 72 65 65 2e 0a 2a 2f 0a 23 eys B-Tree..*/.#
1f61c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f61d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 IT_SUBQUERY.SQLI
1f61e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1f61f 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 sqlite3CodeSubse
1f620 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 lect(. Parse *p
1f621 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a Parse, . Expr *
1f622 70 45 78 70 72 2c 20 0a 20 20 69 6e 74 20 72 4d pExpr, . int rM
1f623 61 79 48 61 76 65 4e 75 6c 6c 2c 0a 20 20 69 6e ayHaveNull,. in
1f624 74 20 69 73 52 6f 77 69 64 0a 29 7b 0a 20 20 69 t isRowid.){. i
1f625 6e 74 20 74 65 73 74 41 64 64 72 20 3d 20 30 3b nt testAddr = 0;
1f626 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f627 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 2d 74 69 /* One-ti
1f628 6d 65 20 74 65 73 74 20 61 64 64 72 65 73 73 20 me test address
1f629 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 */. Vdbe *v = s
1f62a 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
1f62b 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d arse);. if( v==
1f62c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 0 ) return;. sq
1f62d 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 lite3ExprCachePu
1f62e 73 68 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f sh(pParse);.. /
1f62f 2a 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73 74 * This code must
1f630 20 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20 65 be run in its e
1f631 6e 74 69 72 65 74 79 20 65 76 65 72 79 20 74 69 ntirety every ti
1f632 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 me it is encount
1f633 65 72 65 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79 ered. ** if any
1f634 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
1f635 67 20 69 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a g is true:. **.
1f636 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 ** * The r
1f637 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 ight-hand side i
1f638 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 s a correlated s
1f639 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 20 20 20 ubquery. **
1f63a 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e * The right-han
1f63b 64 20 73 69 64 65 20 69 73 20 61 6e 20 65 78 70 d side is an exp
1f63c 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e ression list con
1f63d 74 61 69 6e 69 6e 67 20 76 61 72 69 61 62 6c 65 taining variable
1f63e 73 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57 65 20 s. ** * We
1f63f 61 72 65 20 69 6e 73 69 64 65 20 61 20 74 72 69 are inside a tri
1f640 67 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 gger. **. ** I
1f641 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f f all of the abo
1f642 76 65 20 61 72 65 20 66 61 6c 73 65 2c 20 74 68 ve are false, th
1f643 65 6e 20 77 65 20 63 61 6e 20 72 75 6e 20 74 68 en we can run th
1f644 69 73 20 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63 is code just onc
1f645 65 0a 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 e. ** save the
1f646 72 65 73 75 6c 74 73 2c 20 61 6e 64 20 72 65 75 results, and reu
1f647 73 65 20 74 68 65 20 73 61 6d 65 20 72 65 73 75 se the same resu
1f648 6c 74 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 lt on subsequent
1f649 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 invocations..
1f64a 2a 2f 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 */. if( !ExprHa
1f64b 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 sAnyProperty(pEx
1f64c 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 pr, EP_VarSelect
1f64d 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 ) && !pParse->tr
1f64e 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 69 igStack ){. i
1f64f 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 nt mem = ++pPars
1f650 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c e->nMem;. sql
1f651 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
1f652 2c 20 4f 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 , OP_If, mem);.
1f653 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 73 71 testAddr = sq
1f654 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1f655 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 v, OP_Integer, 1
1f656 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 , mem);. asse
1f657 72 74 28 20 74 65 73 74 41 64 64 72 3e 30 20 7c rt( testAddr>0 |
1f658 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 | pParse->db->ma
1f659 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
1f65a 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 }.. switch( pEx
1f65b 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 pr->op ){. ca
1f65c 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 se TK_IN: {.
1f65d 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b char affinity;
1f65e 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b . KeyInfo k
1f65f 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e eyInfo;. in
1f660 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f t addr; /
1f661 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f * Address of OP_
1f662 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e OpenEphemeral in
1f663 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 struction */.
1f664 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d Expr *pLeft =
1f665 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a pExpr->pLeft;..
1f666 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 48 61 if( rMayHa
1f667 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 veNull ){.
1f668 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1f669 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
1f66a 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 0, rMayHaveNull)
1f66b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
1f66c 20 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 affinity = sqli
1f66d 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 te3ExprAffinity(
1f66e 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f pLeft);.. /
1f66f 2a 20 57 68 65 74 68 65 72 20 74 68 69 73 20 69 * Whether this i
1f670 73 20 61 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 s an 'x IN(SELEC
1f671 54 2e 2e 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 T...)' or an 'x
1f672 49 4e 28 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a IN(<exprlist>)'.
1f673 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 ** express
1f674 69 6f 6e 20 69 74 20 69 73 20 68 61 6e 64 6c 65 ion it is handle
1f675 64 20 74 68 65 20 73 61 6d 65 20 77 61 79 2e 20 d the same way.
1f676 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 A virtual table
1f677 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c is . ** fil
1f678 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65 2d led with single-
1f679 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 field index keys
1f67a 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 representing th
1f67b 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 e results.
1f67c 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 ** from the SELE
1f67d 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72 6c CT or the <exprl
1f67e 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 ist>.. **.
1f67f 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 27 ** If the '
1f680 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 x' expression is
1f681 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c a column value,
1f682 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e or the SELECT..
1f683 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 .. ** state
1f684 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20 63 ment returns a c
1f685 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 olumn value, the
1f686 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f n the affinity o
1f687 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 f that. **
1f688 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 74 column is used t
1f689 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64 65 o build the inde
1f68a 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68 20 x keys. If both
1f68b 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20 20 'x' and the.
1f68c 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 ** SELECT... s
1f68d 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c tatement are col
1f68e 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 umns, then numer
1f68f 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 ic affinity is u
1f690 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 sed. ** if
1f691 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 either column ha
1f692 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 s NUMERIC or INT
1f693 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20 49 EGER affinity. I
1f694 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20 20 f neither.
1f695 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20 53 ** 'x' nor the S
1f696 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 ELECT... stateme
1f697 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 nt are columns,
1f698 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 then numeric aff
1f699 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 inity. ** i
1f69a 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f s used.. */
1f69b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 . pExpr->iT
1f69c 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e able = pParse->n
1f69d 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 Tab++;. add
1f69e 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
1f69f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp2(v, OP_Open
1f6a0 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 Ephemeral, pExpr
1f6a1 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 ->iTable, !isRow
1f6a2 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 id);. memse
1f6a3 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 t(&keyInfo, 0, s
1f6a4 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b izeof(keyInfo));
1f6a5 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e . keyInfo.n
1f6a6 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 Field = 1;..
1f6a7 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
1f6a8 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1f6a9 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
1f6aa 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a /* Case 1:
1f6ab 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53 45 expr IN (SE
1f6ac 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20 LECT ...).
1f6ad 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
1f6ae 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1f6af 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c write the resul
1f6b0 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 ts of the select
1f6b1 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 into the tempor
1f6b2 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ary. ** t
1f6b3 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 able allocated a
1f6b4 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e nd opened above.
1f6b5 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1f6b6 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 SelectDest d
1f6b7 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78 70 est;. Exp
1f6b8 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a rList *pEList;..
1f6b9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1f6ba 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 !isRowid );.
1f6bb 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
1f6bc 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c tDestInit(&dest,
1f6bd 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72 2d SRT_Set, pExpr-
1f6be 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 >iTable);.
1f6bf 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79 20 dest.affinity
1f6c0 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b 0a = (u8)affinity;.
1f6c1 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1f6c2 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 (pExpr->iTable&0
1f6c3 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78 70 x0000FFFF)==pExp
1f6c4 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 r->iTable );.
1f6c5 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1f6c6 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
1f6c7 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c Expr->x.pSelect,
1f6c8 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 &dest) ){.
1f6c9 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
1f6ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
1f6cb 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 EList = pExpr->x
1f6cc 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 .pSelect->pEList
1f6cd 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 ;. if( pE
1f6ce 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e List && pEList->
1f6cf 6e 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20 nExpr>0 ){ .
1f6d0 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 keyInfo.aC
1f6d1 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 oll[0] = sqlite3
1f6d2 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c BinaryCompareCol
1f6d3 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 lSeq(pParse, pEx
1f6d4 70 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 pr->pLeft,.
1f6d5 20 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d pEList-
1f6d6 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 >a[0].pExpr);.
1f6d7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
1f6d8 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 lse if( pExpr->x
1f6d9 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 .pList ){.
1f6da 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 20 /* Case 2:
1f6db 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c 69 expr IN (exprli
1f6dc 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 st). **.
1f6dd 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 ** For ea
1f6de 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 ch expression, b
1f6df 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 uild an index ke
1f6e0 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 y from the evalu
1f6e1 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 ation and.
1f6e2 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e ** store it in
1f6e3 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 the temporary t
1f6e4 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 able. If <expr>
1f6e5 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 is a column, the
1f6e6 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a n use. **
1f6e7 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 that columns af
1f6e8 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 6c finity when buil
1f6e9 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 2e ding index keys.
1f6ea 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f If <expr> is no
1f6eb 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 t. ** a c
1f6ec 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 olumn, use numer
1f6ed 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 ic affinity..
1f6ee 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1f6ef 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 45 int i;. E
1f6f0 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d xprList *pList =
1f6f1 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b pExpr->x.pList;
1f6f2 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 . struct
1f6f3 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
1f6f4 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e Item;. in
1f6f5 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20 t r1, r2, r3;..
1f6f6 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69 if( !affi
1f6f7 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20 nity ){.
1f6f8 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c affinity = SQL
1f6f9 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 ITE_AFF_NONE;.
1f6fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1f6fb 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d keyInfo.aColl[0]
1f6fc 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1f6fd 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 llSeq(pParse, pE
1f6fe 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 xpr->pLeft);..
1f6ff 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 /* Loop th
1f700 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65 rough each expre
1f701 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 ssion in <exprli
1f702 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 st>. */.
1f703 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r1 = sqlite3GetT
1f704 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
1f705 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c r2 = sql
1f706 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1f707 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
1f708 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f709 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
1f70a 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f r2);. fo
1f70b 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 r(i=pList->nExpr
1f70c 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 , pItem=pList->a
1f70d 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 ; i>0; i--, pIte
1f70e 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 m++){.
1f70f 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65 Expr *pE2 = pIte
1f710 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 m->pExpr;..
1f711 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 /* If the e
1f712 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 xpression is not
1f713 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77 constant then w
1f714 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20 e will need to.
1f715 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61 ** disa
1f716 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61 ble the test tha
1f717 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 t was generated
1f718 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73 above that makes
1f719 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20 sure.
1f71a 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c ** this code onl
1f71b 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e y executes once.
1f71c 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20 Because for a
1f71d 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20 non-constant.
1f71e 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 ** expres
1f71f 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 sion we need to
1f720 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 rerun this code
1f721 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20 each time..
1f722 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1f723 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26 if( testAddr &
1f724 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 & !sqlite3ExprIs
1f725 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b Constant(pE2) ){
1f726 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1f727 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f ite3VdbeChangeTo
1f728 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72 Noop(v, testAddr
1f729 2d 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 -1, 2);.
1f72a 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30 testAddr = 0
1f72b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 ;. }..
1f72c 20 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c /* Eval
1f72d 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 uate the express
1f72e 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69 ion and insert i
1f72f 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20 t into the temp
1f730 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 table */.
1f731 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45 r3 = sqlite3E
1f732 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
1f733 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a arse, pE2, r1);.
1f734 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 if( is
1f735 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 Rowid ){.
1f736 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f737 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 AddOp2(v, OP_Mus
1f738 74 42 65 49 6e 74 2c 20 72 33 2c 20 73 71 6c 69 tBeInt, r3, sqli
1f739 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 te3VdbeCurrentAd
1f73a 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 20 dr(v)+2);.
1f73b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f73c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e eAddOp3(v, OP_In
1f73d 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 sert, pExpr->iTa
1f73e 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20 20 ble, r2, r3);.
1f73f 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1f740 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1f741 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
1f742 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 OP_MakeRecord, r
1f743 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e 3, 1, r2, &affin
1f744 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 ity, 1);.
1f745 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1f746 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
1f747 6e 67 65 28 70 50 61 72 73 65 2c 20 72 33 2c 20 nge(pParse, r3,
1f748 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1);.
1f749 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f74a 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 2(v, OP_IdxInser
1f74b 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 t, pExpr->iTable
1f74c 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20 , r2);.
1f74d 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1f74e 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
1f74f 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
1f750 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 e, r1);.
1f751 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
1f752 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 mpReg(pParse, r2
1f753 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1f754 20 69 66 28 20 21 69 73 52 6f 77 69 64 20 29 7b if( !isRowid ){
1f755 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1f756 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 VdbeChangeP4(v,
1f757 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29 26 6b addr, (void *)&k
1f758 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e eyInfo, P4_KEYIN
1f759 46 4f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 FO);. }.
1f75a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1f75b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 . case TK_EXI
1f75c 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b STS:. case TK
1f75d 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 _SELECT: {.
1f75e 20 2f 2a 20 54 68 69 73 20 68 61 73 20 74 6f 20 /* This has to
1f75f 62 65 20 61 20 73 63 61 6c 61 72 20 53 45 4c 45 be a scalar SELE
1f760 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f CT. Generate co
1f761 64 65 20 74 6f 20 70 75 74 20 74 68 65 0a 20 20 de to put the.
1f762 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 ** value of
1f763 74 68 69 73 20 73 65 6c 65 63 74 20 69 6e 20 61 this select in a
1f764 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 memory cell and
1f765 20 72 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 record the numb
1f766 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 er. ** of t
1f767 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 he memory cell i
1f768 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 n iColumn..
1f769 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 */. static
1f76a 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e 65 const Token one
1f76b 20 3d 20 7b 20 28 75 38 2a 29 22 31 22 2c 20 30 = { (u8*)"1", 0
1f76c 2c 20 30 2c 20 31 20 7d 3b 0a 20 20 20 20 20 20 , 0, 1 };.
1f76d 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 0a 20 20 Select *pSel;.
1f76e 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 SelectDest d
1f76f 65 73 74 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 est;.. asse
1f770 72 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 rt( ExprHasPrope
1f771 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
1f772 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 sSelect) );.
1f773 20 20 70 53 65 6c 20 3d 20 70 45 78 70 72 2d 3e pSel = pExpr->
1f774 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 x.pSelect;.
1f775 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
1f776 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c stInit(&dest, 0,
1f777 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 ++pParse->nMem)
1f778 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 ;. if( pExp
1f779 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 r->op==TK_SELECT
1f77a 20 29 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 ){. dest
1f77b 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d .eDest = SRT_Mem
1f77c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1f77d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1f77e 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e P_Null, 0, dest.
1f77f 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 iParm);.
1f780 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 VdbeComment((v,
1f781 22 49 6e 69 74 20 73 75 62 71 75 65 72 79 20 72 "Init subquery r
1f782 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 esult"));.
1f783 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 }else{. d
1f784 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f est.eDest = SRT_
1f785 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 Exists;.
1f786 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f787 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
1f788 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0, dest.iParm);
1f789 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d . VdbeCom
1f78a 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45 ment((v, "Init E
1f78b 58 49 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b XISTS result"));
1f78c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
1f78d 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1f78e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 (pParse->db, pSe
1f78f 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 l->pLimit);.
1f790 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d pSel->pLimit =
1f791 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 sqlite3PExpr(pP
1f792 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 arse, TK_INTEGER
1f793 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20 , 0, 0, &one);.
1f794 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1f795 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
1f796 53 65 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 Sel, &dest) ){.
1f797 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
1f798 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 45 78 }. pEx
1f799 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 64 65 pr->iColumn = de
1f79a 73 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 st.iParm;.
1f79b 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
1f79c 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 72 .. if( testAddr
1f79d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1f79e 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 dbeJumpHere(v, t
1f79f 65 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a estAddr-1);. }.
1f7a0 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
1f7a1 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 hePop(pParse, 1)
1f7a2 3b 0a 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 ;.. return;.}.#
1f7a3 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1f7a4 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f OMIT_SUBQUERY */
1f7a5 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74 ../*.** Duplicat
1f7a6 65 20 61 6e 20 38 2d 62 79 74 65 20 76 61 6c 75 e an 8-byte valu
1f7a7 65 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 e.*/.static char
1f7a8 20 2a 64 75 70 38 62 79 74 65 73 28 56 64 62 65 *dup8bytes(Vdbe
1f7a9 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *v, const char
1f7aa 2a 69 6e 29 7b 0a 20 20 63 68 61 72 20 2a 6f 75 *in){. char *ou
1f7ab 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
1f7ac 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 56 64 locRaw(sqlite3Vd
1f7ad 62 65 44 62 28 76 29 2c 20 38 29 3b 0a 20 20 69 beDb(v), 8);. i
1f7ae 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 6d 65 f( out ){. me
1f7af 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38 29 mcpy(out, in, 8)
1f7b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f ;. }. return o
1f7b1 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e ut;.}../*.** Gen
1f7b2 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63 erate an instruc
1f7b3 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 tion that will p
1f7b4 75 74 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 ut the floating
1f7b5 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 64 point.** value d
1f7b6 65 73 63 72 69 62 65 64 20 62 79 20 7a 5b 30 2e escribed by z[0.
1f7b7 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 .n-1] into regis
1f7b8 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 ter iMem..**.**
1f7b9 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 The z[] string w
1f7ba 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 ill probably not
1f7bb 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 be zero-termina
1f7bc 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a ted. But the .*
1f7bd 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 * z[n] character
1f7be 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
1f7bf 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 o be something t
1f7c0 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f hat does not loo
1f7c1 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f k.** like the co
1f7c2 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 ntinuation of th
1f7c3 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
1f7c4 74 69 63 20 76 6f 69 64 20 63 6f 64 65 52 65 61 tic void codeRea
1f7c5 6c 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 l(Vdbe *v, const
1f7c6 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c char *z, int n,
1f7c7 20 69 6e 74 20 6e 65 67 61 74 65 46 6c 61 67 2c int negateFlag,
1f7c8 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 61 73 int iMem){. as
1f7c9 73 65 72 74 28 20 7a 20 7c 7c 20 76 3d 3d 30 20 sert( z || v==0
1f7ca 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 44 62 || sqlite3VdbeDb
1f7cb 28 76 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 (v)->mallocFaile
1f7cc 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 d );. assert( !
1f7cd 7a 20 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 64 z || !sqlite3Isd
1f7ce 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 igit(z[n]) );.
1f7cf 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1f7d0 28 6e 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b 0a (n);. if( z ){.
1f7d1 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 65 double value
1f7d2 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a ;. char *zV;.
1f7d3 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 sqlite3AtoF(
1f7d4 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 z, &value);.
1f7d5 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e if( sqlite3IsNaN
1f7d6 28 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 (value) ){.
1f7d7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f7d8 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
1f7d9 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65 6c , iMem);. }el
1f7da 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 se{. if( ne
1f7db 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 65 gateFlag ) value
1f7dc 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20 = -value;.
1f7dd 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 zV = dup8bytes(
1f7de 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 v, (char*)&value
1f7df 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f7e0 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1f7e1 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 _Real, 0, iMem,
1f7e2 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0, zV, P4_REAL);
1f7e3 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f . }. }.}.../
1f7e4 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e *.** Generate an
1f7e5 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
1f7e6 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 69 t will put the i
1f7e7 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65 20 nteger describe
1f7e8 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e 2e by.** text z[0..
1f7e9 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 74 n-1] into regist
1f7ea 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 er iMem..**.** T
1f7eb 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69 he z[] string wi
1f7ec 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 ll probably not
1f7ed 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 be zero-terminat
1f7ee 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a 2a ed. But the .**
1f7ef 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 20 z[n] character
1f7f0 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f is guaranteed to
1f7f1 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 be something th
1f7f2 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b at does not look
1f7f3 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e .** like the con
1f7f4 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65 tinuation of the
1f7f5 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
1f7f6 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74 65 ic void codeInte
1f7f7 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78 70 ger(Vdbe *v, Exp
1f7f8 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e 65 r *pExpr, int ne
1f7f9 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29 gFlag, int iMem)
1f7fa 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a {. const char *
1f7fb 7a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e z;. if( pExpr->
1f7fc 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61 flags & EP_IntVa
1f7fd 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 lue ){. int i
1f7fe 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 = pExpr->iTable
1f7ff 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 ;. if( negFla
1f800 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 20 g ) i = -i;.
1f801 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f802 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
1f803 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65 6c i, iMem);. }el
1f804 73 65 20 69 66 28 20 28 7a 20 3d 20 28 63 68 61 se if( (z = (cha
1f805 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e r*)pExpr->token.
1f806 7a 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 z)!=0 ){. int
1f807 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 i;. int n =
1f808 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a pExpr->token.n;.
1f809 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c assert( !sql
1f80a 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 6e 5d ite3Isdigit(z[n]
1f80b 29 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c ) );. if( sql
1f80c 69 74 65 33 47 65 74 49 6e 74 33 32 28 7a 2c 20 ite3GetInt32(z,
1f80d 26 69 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 &i) ){. if(
1f80e 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20 2d negFlag ) i = -
1f80f 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 i;. sqlite3
1f810 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1f811 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 _Integer, i, iMe
1f812 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 m);. }else if
1f813 28 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 ( sqlite3FitsIn6
1f814 34 42 69 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67 4Bits(z, negFlag
1f815 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 ) ){. i64 v
1f816 61 6c 75 65 3b 0a 20 20 20 20 20 20 63 68 61 72 alue;. char
1f817 20 2a 7a 56 3b 0a 20 20 20 20 20 20 73 71 6c 69 *zV;. sqli
1f818 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 te3Atoi64(z, &va
1f819 6c 75 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 lue);. if(
1f81a 6e 65 67 46 6c 61 67 20 29 20 76 61 6c 75 65 20 negFlag ) value
1f81b 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 = -value;.
1f81c 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 zV = dup8bytes(v
1f81d 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 , (char*)&value)
1f81e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1f81f 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1f820 49 6e 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 Int64, 0, iMem,
1f821 30 2c 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 0, zV, P4_INT64)
1f822 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1f823 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a codeReal(v, z
1f824 2c 20 6e 2c 20 6e 65 67 46 6c 61 67 2c 20 69 4d , n, negFlag, iM
1f825 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d em);. }. }.}
1f826 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 20 ../*.** Clear a
1f827 63 61 63 68 65 20 65 6e 74 72 79 2e 0a 2a 2f 0a cache entry..*/.
1f828 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 63 68 static void cach
1f829 65 45 6e 74 72 79 43 6c 65 61 72 28 50 61 72 73 eEntryClear(Pars
1f82a 65 20 2a 70 50 61 72 73 65 2c 20 73 74 72 75 63 e *pParse, struc
1f82b 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 29 7b t yColCache *p){
1f82c 0a 20 20 69 66 28 20 70 2d 3e 74 65 6d 70 52 65 . if( p->tempRe
1f82d 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 g ){. if( pPa
1f82e 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72 rse->nTempReg<Ar
1f82f 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e raySize(pParse->
1f830 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 20 aTempReg) ){.
1f831 20 20 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 pParse->aTemp
1f832 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d Reg[pParse->nTem
1f833 70 52 65 67 2b 2b 5d 20 3d 20 70 2d 3e 69 52 65 pReg++] = p->iRe
1f834 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e g;. }. p->
1f835 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d tempReg = 0;. }
1f836 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 .}.../*.** Recor
1f837 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 d in the column
1f838 63 61 63 68 65 20 74 68 61 74 20 61 20 70 61 72 cache that a par
1f839 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 20 66 ticular column f
1f83a 72 6f 6d 20 61 0a 2a 2a 20 70 61 72 74 69 63 75 rom a.** particu
1f83b 6c 61 72 20 74 61 62 6c 65 20 69 73 20 73 74 6f lar table is sto
1f83c 72 65 64 20 69 6e 20 61 20 70 61 72 74 69 63 75 red in a particu
1f83d 6c 61 72 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f lar register..*/
1f83e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f83f 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1f840 43 61 63 68 65 53 74 6f 72 65 28 50 61 72 73 65 CacheStore(Parse
1f841 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 54 *pParse, int iT
1f842 61 62 2c 20 69 6e 74 20 69 43 6f 6c 2c 20 69 6e ab, int iCol, in
1f843 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t iReg){. int i
1f844 3b 0a 20 20 69 6e 74 20 6d 69 6e 4c 72 75 3b 0a ;. int minLru;.
1f845 20 20 69 6e 74 20 69 64 78 4c 72 75 3b 0a 20 20 int idxLru;.
1f846 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
1f847 20 2a 70 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74 *p;.. /* First
1f848 20 72 65 70 6c 61 63 65 20 61 6e 79 20 65 78 69 replace any exi
1f849 73 74 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 sting entry */.
1f84a 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
1f84b 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
1f84c 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
1f84d 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
1f84e 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 if( p->iReg &
1f84f 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 & p->iTable==iTa
1f850 62 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d b && p->iColumn=
1f851 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 63 =iCol ){. c
1f852 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 acheEntryClear(p
1f853 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 Parse, p);.
1f854 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 p->iLevel = pPa
1f855 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c rse->iCacheLevel
1f856 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 ;. p->iReg
1f857 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d = iReg;. p-
1f858 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a >affChange = 0;.
1f859 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 p->lru = p
1f85a 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 Parse->iCacheCnt
1f85b 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ++;. return
1f85c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1f85d 28 20 69 52 65 67 3c 3d 30 20 29 20 72 65 74 75 ( iReg<=0 ) retu
1f85e 72 6e 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 61 rn;.. /* Find a
1f85f 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 61 6e 64 n empty slot and
1f860 20 72 65 70 6c 61 63 65 20 69 74 20 2a 2f 0a 20 replace it */.
1f861 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
1f862 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
1f863 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
1f864 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
1f865 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d if( p->iReg==
1f866 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 0 ){. p->iL
1f867 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 evel = pParse->i
1f868 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 CacheLevel;.
1f869 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 p->iTable = iT
1f86a 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f ab;. p->iCo
1f86b 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 lumn = iCol;.
1f86c 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 p->iReg = iRe
1f86d 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43 g;. p->affC
1f86e 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 hange = 0;.
1f86f 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b p->tempReg = 0;
1f870 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 . p->lru =
1f871 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e pParse->iCacheCn
1f872 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 t++;. retur
1f873 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 n;. }. }..
1f874 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 6c /* Replace the l
1f875 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73 65 ast recently use
1f876 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75 20 3d 20 d */. minLru =
1f877 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 69 64 0x7fffffff;. id
1f878 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20 66 6f 72 xLru = -1;. for
1f879 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
1f87a 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
1f87b 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
1f87c 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
1f87d 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e 4c 72 75 f( p->lru<minLru
1f87e 20 29 7b 0a 20 20 20 20 20 20 69 64 78 4c 72 75 ){. idxLru
1f87f 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d 69 6e 4c = i;. minL
1f880 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a 20 20 20 ru = p->lru;.
1f881 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 64 78 }. }. if( idx
1f882 4c 72 75 3e 3d 30 20 29 7b 0a 20 20 20 20 70 20 Lru>=0 ){. p
1f883 3d 20 26 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 = &pParse->aColC
1f884 61 63 68 65 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 ache[idxLru];.
1f885 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 p->iLevel = pP
1f886 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 arse->iCacheLeve
1f887 6c 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 l;. p->iTable
1f888 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e = iTab;. p->
1f889 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a iColumn = iCol;.
1f88a 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 p->iReg = iR
1f88b 65 67 3b 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 eg;. p->affCh
1f88c 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d ange = 0;. p-
1f88d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 >tempReg = 0;.
1f88e 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 p->lru = pPars
1f88f 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a e->iCacheCnt++;.
1f890 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
1f891 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 }../*.** Indicat
1f892 65 20 74 68 61 74 20 61 20 72 65 67 69 73 74 65 e that a registe
1f893 72 20 69 73 20 62 65 69 6e 67 20 6f 76 65 72 77 r is being overw
1f894 72 69 74 74 65 6e 2e 20 20 50 75 72 67 65 20 74 ritten. Purge t
1f895 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 66 he register.** f
1f896 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 rom the column c
1f897 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
1f898 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1f899 69 74 65 33 45 78 70 72 43 61 63 68 65 52 65 6d ite3ExprCacheRem
1f89a 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ove(Parse *pPars
1f89b 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 e, int iReg){.
1f89c 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
1f89d 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 yColCache *p;.
1f89e 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 for(i=0, p=pPars
1f89f 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c e->aColCache; i<
1f8a0 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
1f8a1 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 E; i++, p++){.
1f8a2 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 if( p->iReg==i
1f8a3 52 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63 Reg ){. cac
1f8a4 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 heEntryClear(pPa
1f8a5 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 rse, p);. p
1f8a6 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 ->iReg = 0;.
1f8a7 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
1f8a8 65 6d 65 6d 62 65 72 20 74 68 65 20 63 75 72 72 emember the curr
1f8a9 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 ent column cache
1f8aa 20 63 6f 6e 74 65 78 74 2e 20 20 41 6e 79 20 6e context. Any n
1f8ab 65 77 20 65 6e 74 72 69 65 73 20 61 64 64 65 64 ew entries added
1f8ac 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65 .** added to the
1f8ad 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61 66 column cache af
1f8ae 74 65 72 20 74 68 69 73 20 63 61 6c 6c 20 61 72 ter this call ar
1f8af 65 20 72 65 6d 6f 76 65 64 20 77 68 65 6e 20 74 e removed when t
1f8b0 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 he.** correspond
1f8b1 69 6e 67 20 70 6f 70 20 6f 63 63 75 72 73 2e 0a ing pop occurs..
1f8b2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f8b3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1f8b4 70 72 43 61 63 68 65 50 75 73 68 28 50 61 72 73 prCachePush(Pars
1f8b5 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 70 50 e *pParse){. pP
1f8b6 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 arse->iCacheLeve
1f8b7 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 l++;.}../*.** Re
1f8b8 6d 6f 76 65 20 66 72 6f 6d 20 74 68 65 20 63 6f move from the co
1f8b9 6c 75 6d 6e 20 63 61 63 68 65 20 61 6e 79 20 65 lumn cache any e
1f8ba 6e 74 72 69 65 73 20 74 68 61 74 20 77 65 72 65 ntries that were
1f8bb 20 61 64 64 65 64 20 73 69 6e 63 65 20 74 68 65 added since the
1f8bc 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 .** the previous
1f8bd 20 4e 20 50 75 73 68 20 6f 70 65 72 61 74 69 6f N Push operatio
1f8be 6e 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f ns. In other wo
1f8bf 72 64 73 2c 20 72 65 73 74 6f 72 65 20 74 68 65 rds, restore the
1f8c0 20 63 61 63 68 65 0a 2a 2a 20 74 6f 20 74 68 65 cache.** to the
1f8c1 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e state it was in
1f8c2 20 4e 20 50 75 73 68 65 73 20 61 67 6f 2e 0a 2a N Pushes ago..*
1f8c3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f8c4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
1f8c5 72 43 61 63 68 65 50 6f 70 28 50 61 72 73 65 20 rCachePop(Parse
1f8c6 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 4e 29 7b *pParse, int N){
1f8c7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
1f8c8 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b ct yColCache *p;
1f8c9 0a 20 20 61 73 73 65 72 74 28 20 4e 3e 30 20 29 . assert( N>0 )
1f8ca 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 ;. assert( pPar
1f8cb 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e se->iCacheLevel>
1f8cc 3d 4e 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e =N );. pParse->
1f8cd 69 43 61 63 68 65 4c 65 76 65 6c 20 2d 3d 20 4e iCacheLevel -= N
1f8ce 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 ;. for(i=0, p=p
1f8cf 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
1f8d0 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c ; i<SQLITE_N_COL
1f8d1 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 CACHE; i++, p++)
1f8d2 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 {. if( p->iRe
1f8d3 67 20 26 26 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 g && p->iLevel>p
1f8d4 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 Parse->iCacheLev
1f8d5 65 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 el ){. cach
1f8d6 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 eEntryClear(pPar
1f8d7 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d se, p);. p-
1f8d8 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d >iReg = 0;. }
1f8d9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 . }.}../*.** Ge
1f8da 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 nerate code that
1f8db 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68 will extract th
1f8dc 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c e iColumn-th col
1f8dd 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c umn from.** tabl
1f8de 65 20 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65 e pTab and store
1f8df 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 the column valu
1f8e0 65 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e e in a register.
1f8e1 20 20 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69 An effort.** i
1f8e2 73 20 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20 s made to store
1f8e3 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 the column value
1f8e4 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 in register iRe
1f8e5 67 2c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a g, but this is.*
1f8e6 2a 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 * not guaranteed
1f8e7 2e 20 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 . The location
1f8e8 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 of the column va
1f8e9 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e lue is returned.
1f8ea 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 .**.** There mus
1f8eb 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 t be an open cur
1f8ec 73 6f 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69 sor to pTab in i
1f8ed 54 61 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20 Table when this
1f8ee 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 routine.** is ca
1f8ef 6c 6c 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d lled. If iColum
1f8f0 6e 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 n<0 then code is
1f8f1 20 67 65 6e 65 72 61 74 65 64 20 74 68 61 74 20 generated that
1f8f2 65 78 74 72 61 63 74 73 20 74 68 65 20 72 6f 77 extracts the row
1f8f3 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 id..**.** This r
1f8f4 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 61 74 74 outine might att
1f8f5 65 6d 70 74 20 74 6f 20 72 65 75 73 65 20 74 68 empt to reuse th
1f8f6 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 e value of the c
1f8f7 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 olumn that.** ha
1f8f8 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c s already been l
1f8f9 6f 61 64 65 64 20 69 6e 74 6f 20 61 20 72 65 67 oaded into a reg
1f8fa 69 73 74 65 72 2e 20 20 54 68 65 20 76 61 6c 75 ister. The valu
1f8fb 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a e will always.**
1f8fc 20 62 65 20 75 73 65 64 20 69 66 20 69 74 20 68 be used if it h
1f8fd 61 73 20 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65 as not undergone
1f8fe 20 61 6e 79 20 61 66 66 69 6e 69 74 79 20 63 68 any affinity ch
1f8ff 61 6e 67 65 73 2e 20 20 42 75 74 20 69 66 0a 2a anges. But if.*
1f900 2a 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 * an affinity ch
1f901 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 ange has occurre
1f902 64 2c 20 74 68 65 6e 20 74 68 65 20 63 61 63 68 d, then the cach
1f903 65 64 20 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e ed value will on
1f904 6c 79 20 62 65 0a 2a 2a 20 75 73 65 64 20 69 66 ly be.** used if
1f905 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 69 73 allowAffChng is
1f906 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 true..*/.SQLITE
1f907 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1f908 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 ite3ExprCodeGetC
1f909 6f 6c 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a olumn(. Parse *
1f90a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 pParse, /* Par
1f90b 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 sing and code ge
1f90c 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 nerating context
1f90d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 */. Table *pTa
1f90e 62 2c 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69 b, /* Descri
1f90f 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 ption of the tab
1f910 6c 65 20 77 65 20 61 72 65 20 72 65 61 64 69 6e le we are readin
1f911 67 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 g from */. int
1f912 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 iColumn, /*
1f913 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 Index of the tab
1f914 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 le column */. i
1f915 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 nt iTable,
1f916 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f /* The cursor po
1f917 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 74 61 inting to the ta
1f918 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 ble */. int iRe
1f919 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f g, /* Sto
1f91a 72 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 re results here
1f91b 2a 2f 0a 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66 */. int allowAf
1f91c 66 43 68 6e 67 20 2f 2a 20 54 72 75 65 20 69 66 fChng /* True if
1f91d 20 70 72 69 6f 72 20 61 66 66 69 6e 69 74 79 20 prior affinity
1f91e 63 68 61 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a changes are OK *
1f91f 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d /.){. Vdbe *v =
1f920 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
1f921 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 int i;. struc
1f922 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a t yColCache *p;.
1f923 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 . for(i=0, p=pP
1f924 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b arse->aColCache;
1f925 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 i<SQLITE_N_COLC
1f926 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b ACHE; i++, p++){
1f927 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 . if( p->iReg
1f928 3e 30 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d >0 && p->iTable=
1f929 3d 69 54 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 =iTable && p->iC
1f92a 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 olumn==iColumn.
1f92b 20 20 20 20 20 20 20 20 20 20 26 26 20 28 21 70 && (!p
1f92c 2d 3e 61 66 66 43 68 61 6e 67 65 20 7c 7c 20 61 ->affChange || a
1f92d 6c 6c 6f 77 41 66 66 43 68 6e 67 29 20 29 7b 0a llowAffChng) ){.
1f92e 23 69 66 20 30 0a 20 20 20 20 20 20 73 71 6c 69 #if 0. sqli
1f92f 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c te3VdbeAddOp0(v,
1f930 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 20 20 20 OP_Noop);.
1f931 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
1f932 20 22 4f 50 54 3a 20 74 61 62 25 64 2e 63 6f 6c "OPT: tab%d.col
1f933 25 64 20 2d 3e 20 72 25 64 22 2c 20 69 54 61 62 %d -> r%d", iTab
1f934 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70 2d 3e le, iColumn, p->
1f935 69 52 65 67 29 29 3b 0a 23 65 6e 64 69 66 0a 20 iReg));.#endif.
1f936 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 p->lru = pP
1f937 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b arse->iCacheCnt+
1f938 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 +;. p->temp
1f939 52 65 67 20 3d 20 30 3b 20 20 2f 2a 20 54 68 69 Reg = 0; /* Thi
1f93a 73 20 70 69 6e 73 20 74 68 65 20 72 65 67 69 73 s pins the regis
1f93b 74 65 72 2c 20 62 75 74 20 61 6c 73 6f 20 6c 65 ter, but also le
1f93c 61 6b 73 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 aks it */.
1f93d 72 65 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a return p->iReg;.
1f93e 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 }. } . as
1f93f 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 sert( v!=0 );.
1f940 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b if( iColumn<0 ){
1f941 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1f942 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 AddOp2(v, OP_Row
1f943 69 64 2c 20 69 54 61 62 6c 65 2c 20 69 52 65 67 id, iTable, iReg
1f944 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 );. }else if( p
1f945 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 Tab==0 ){. sq
1f946 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1f947 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 v, OP_Column, iT
1f948 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 able, iColumn, i
1f949 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Reg);. }else{.
1f94a 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 int op = IsVi
1f94b 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 rtual(pTab) ? OP
1f94c 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f _VColumn : OP_Co
1f94d 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 lumn;. sqlite
1f94e 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 3VdbeAddOp3(v, o
1f94f 70 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 p, iTable, iColu
1f950 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 mn, iReg);. s
1f951 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 qlite3ColumnDefa
1f952 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f ult(v, pTab, iCo
1f953 6c 75 6d 6e 29 3b 0a 23 69 66 6e 64 65 66 20 53 lumn);.#ifndef S
1f954 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
1f955 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 69 66 ING_POINT. if
1f956 28 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f ( pTab->aCol[iCo
1f957 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d lumn].affinity==
1f958 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 SQLITE_AFF_REAL
1f959 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1f95a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
1f95b 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20 69 _RealAffinity, i
1f95c 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 Reg);. }.#end
1f95d 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 if. }. sqlite3
1f95e 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 ExprCacheStore(p
1f95f 50 61 72 73 65 2c 20 69 54 61 62 6c 65 2c 20 69 Parse, iTable, i
1f960 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 Column, iReg);.
1f961 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a return iReg;.}.
1f962 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c ./*.** Clear all
1f963 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e column cache en
1f964 74 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tries..*/.SQLITE
1f965 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1f966 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c lite3ExprCacheCl
1f967 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 ear(Parse *pPars
1f968 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 e){. int i;. s
1f969 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
1f96a 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 *p;.. for(i=0,
1f96b 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
1f96c 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
1f96d 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
1f96e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e ++){. if( p->
1f96f 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61 iReg ){. ca
1f970 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 cheEntryClear(pP
1f971 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 arse, p);.
1f972 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 p->iReg = 0;.
1f973 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
1f974 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 Record the fact
1f975 74 68 61 74 20 61 6e 20 61 66 66 69 6e 69 74 79 that an affinity
1f976 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 change has occu
1f977 72 72 65 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a rred on iCount.*
1f978 2a 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 * registers star
1f979 74 69 6e 67 20 77 69 74 68 20 69 53 74 61 72 74 ting with iStart
1f97a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f97b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f97c 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 ExprCacheAffinit
1f97d 79 43 68 61 6e 67 65 28 50 61 72 73 65 20 2a 70 yChange(Parse *p
1f97e 50 61 72 73 65 2c 20 69 6e 74 20 69 53 74 61 72 Parse, int iStar
1f97f 74 2c 20 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a t, int iCount){.
1f980 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 69 53 74 int iEnd = iSt
1f981 61 72 74 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31 art + iCount - 1
1f982 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 ;. int i;. str
1f983 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 uct yColCache *p
1f984 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 ;. for(i=0, p=p
1f985 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
1f986 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c ; i<SQLITE_N_COL
1f987 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 CACHE; i++, p++)
1f988 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d {. int r = p-
1f989 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 >iReg;. if( r
1f98a 3e 3d 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69 >=iStart && r<=i
1f98b 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e End ){. p->
1f98c 61 66 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 affChange = 1;.
1f98d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1f98e 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1f98f 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 to move content
1f990 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 from registers i
1f991 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 From...iFrom+nRe
1f992 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 g-1.** over to i
1f993 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 To..iTo+nReg-1.
1f994 4b 65 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 Keep the column
1f995 63 61 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 cache up-to-date
1f996 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f997 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f998 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 ExprCodeMove(Par
1f999 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1f99a 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 iFrom, int iTo,
1f99b 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 int nReg){. int
1f99c 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f i;. struct yCo
1f99d 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 69 66 28 lCache *p;. if(
1f99e 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 20 72 65 iFrom==iTo ) re
1f99f 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 turn;. sqlite3V
1f9a0 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 dbeAddOp3(pParse
1f9a1 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 ->pVdbe, OP_Move
1f9a2 2c 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 , iFrom, iTo, nR
1f9a3 65 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 eg);. for(i=0,
1f9a4 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
1f9a5 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
1f9a6 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
1f9a7 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d ++){. int x =
1f9a8 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 p->iReg;. if
1f9a9 28 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c ( x>=iFrom && x<
1f9aa 69 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 iFrom+nReg ){.
1f9ab 20 20 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 p->iReg += i
1f9ac 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a To-iFrom;. }.
1f9ad 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}../*.** Gen
1f9ae 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f erate code to co
1f9af 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 py content from
1f9b0 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e registers iFrom.
1f9b1 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a ..iFrom+nReg-1.*
1f9b2 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 * over to iTo..i
1f9b3 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 To+nReg-1..*/.SQ
1f9b4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1f9b5 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 d sqlite3ExprCod
1f9b6 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 eCopy(Parse *pPa
1f9b7 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 rse, int iFrom,
1f9b8 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 int iTo, int nRe
1f9b9 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 g){. int i;. i
1f9ba 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 20 f( iFrom==iTo )
1f9bb 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d return;. for(i=
1f9bc 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29 7b 0; i<nReg; i++){
1f9bd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1f9be 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 AddOp2(pParse->p
1f9bf 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 Vdbe, OP_Copy, i
1f9c0 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b 0a From+i, iTo+i);.
1f9c1 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
1f9c2 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79 20 urn true if any
1f9c3 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20 register in the
1f9c4 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54 6f range iFrom..iTo
1f9c5 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a 20 (inclusive).**
1f9c6 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74 20 is used as part
1f9c7 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 of the column ca
1f9c8 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 che..*/.static i
1f9c9 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 nt usedAsColumnC
1f9ca 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72 ache(Parse *pPar
1f9cb 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 se, int iFrom, i
1f9cc 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20 69 nt iTo){. int i
1f9cd 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
1f9ce 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 ache *p;. for(i
1f9cf 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 =0, p=pParse->aC
1f9d0 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 olCache; i<SQLIT
1f9d1 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b E_N_COLCACHE; i+
1f9d2 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 +, p++){. int
1f9d3 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 r = p->iReg;.
1f9d4 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d 20 26 if( r>=iFrom &
1f9d5 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 75 72 & r<=iTo ) retur
1f9d6 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n 1;. }. retur
1f9d7 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 n 0;.}../*.** If
1f9d8 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 the last instru
1f9d9 63 74 69 6f 6e 20 63 6f 64 65 64 20 69 73 20 61 ction coded is a
1f9da 6e 20 65 70 68 65 6d 65 72 61 6c 20 63 6f 70 79 n ephemeral copy
1f9db 20 6f 66 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 of any of.** th
1f9dc 65 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 74 e registers in t
1f9dd 68 65 20 6e 52 65 67 20 72 65 67 69 73 74 65 72 he nReg register
1f9de 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 s beginning with
1f9df 20 69 52 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 iReg, then.** c
1f9e0 6f 6e 76 65 72 74 20 74 68 65 20 6c 61 73 74 20 onvert the last
1f9e1 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d instruction from
1f9e2 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f OP_SCopy to OP_
1f9e3 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f Copy..*/.SQLITE_
1f9e4 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1f9e5 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 ite3ExprHardCopy
1f9e6 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1f9e7 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 int iReg, int nR
1f9e8 65 67 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b eg){. int addr;
1f9e9 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a . VdbeOp *pOp;.
1f9ea 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 76 20 Vdbe *v;.. v
1f9eb 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b = pParse->pVdbe;
1f9ec 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 . addr = sqlite
1f9ed 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 3VdbeCurrentAddr
1f9ee 28 76 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c (v);. pOp = sql
1f9ef 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c ite3VdbeGetOp(v,
1f9f0 20 61 64 64 72 2d 31 29 3b 0a 20 20 61 73 73 65 addr-1);. asse
1f9f1 72 74 28 20 70 4f 70 20 7c 7c 20 70 50 61 72 73 rt( pOp || pPars
1f9f2 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e->db->mallocFai
1f9f3 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 led );. if( pOp
1f9f4 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d && pOp->opcode=
1f9f5 3d 4f 50 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 =OP_SCopy && pOp
1f9f6 2d 3e 70 31 3e 3d 69 52 65 67 20 26 26 20 70 4f ->p1>=iReg && pO
1f9f7 70 2d 3e 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 p->p1<iReg+nReg
1f9f8 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f ){. pOp->opco
1f9f9 64 65 20 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 de = OP_Copy;.
1f9fa 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
1f9fb 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 ate code to stor
1f9fc 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
1f9fd 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 he iAlias-th ali
1f9fe 61 73 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a as in register.*
1f9ff 2a 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 66 * target. The f
1fa00 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 irst time this i
1fa01 73 20 63 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 s called, pExpr
1fa02 69 73 20 65 76 61 6c 75 61 74 65 64 20 74 6f 20 is evaluated to
1fa03 63 6f 6d 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 compute.** the v
1fa04 61 6c 75 65 20 6f 66 20 74 68 65 20 61 6c 69 61 alue of the alia
1fa05 73 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 s. The value is
1fa06 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 stored in an au
1fa07 78 69 6c 69 61 72 79 20 72 65 67 69 73 74 65 72 xiliary register
1fa08 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 .** and the numb
1fa09 65 72 20 6f 66 20 74 68 61 74 20 72 65 67 69 73 er of that regis
1fa0a 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
1fa0b 20 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 On subsequent
1fa0c 63 61 6c 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 calls,.** the re
1fa0d 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 69 73 gister number is
1fa0e 20 72 65 74 75 72 6e 65 64 20 77 69 74 68 6f 75 returned withou
1fa0f 74 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 t generating any
1fa10 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 code..**.** Not
1fa11 65 20 74 68 61 74 20 69 6e 20 6f 72 64 65 72 20 e that in order
1fa12 66 6f 72 20 74 68 69 73 20 74 6f 20 77 6f 72 6b for this to work
1fa13 2c 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 67 , code must be g
1fa14 65 6e 65 72 61 74 65 64 20 69 6e 20 74 68 65 0a enerated in the.
1fa15 2a 2a 20 73 61 6d 65 20 6f 72 64 65 72 20 74 68 ** same order th
1fa16 61 74 20 69 74 20 69 73 20 65 78 65 63 75 74 65 at it is execute
1fa17 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 d..**.** Aliases
1fa18 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 are numbered st
1fa19 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e 20 20 arting with 1.
1fa1a 53 6f 20 69 41 6c 69 61 73 20 69 73 20 69 6e 20 So iAlias is in
1fa1b 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 the range.** of
1fa1c 31 20 74 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 1 to pParse->nAl
1fa1d 69 61 73 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 ias inclusive.
1fa1e 0a 2a 2a 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 .**.** pParse->a
1fa1f 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 Alias[iAlias-1]
1fa20 72 65 63 6f 72 64 73 20 74 68 65 20 72 65 67 69 records the regi
1fa21 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 ster number wher
1fa22 65 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f e the value.** o
1fa23 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20 f the iAlias-th
1fa24 61 6c 69 61 73 20 69 73 20 73 74 6f 72 65 64 2e alias is stored.
1fa25 20 20 49 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 If zero, that
1fa26 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a means that the.*
1fa27 2a 20 61 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 * alias has not
1fa28 79 65 74 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 yet been compute
1fa29 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
1fa2a 20 63 6f 64 65 41 6c 69 61 73 28 50 61 72 73 65 codeAlias(Parse
1fa2b 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 41 *pParse, int iA
1fa2c 6c 69 61 73 2c 20 45 78 70 72 20 2a 70 45 78 70 lias, Expr *pExp
1fa2d 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a r, int target){.
1fa2e 23 69 66 20 30 0a 20 20 73 71 6c 69 74 65 33 20 #if 0. sqlite3
1fa2f 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1fa30 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 0a 20 20 ;. int iReg;.
1fa31 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 if( pParse->nAli
1fa32 61 73 41 6c 6c 6f 63 3c 70 50 61 72 73 65 2d 3e asAlloc<pParse->
1fa33 6e 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 70 50 nAlias ){. pP
1fa34 61 72 73 65 2d 3e 61 41 6c 69 61 73 20 3d 20 73 arse->aAlias = s
1fa35 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f qlite3DbReallocO
1fa36 72 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 rFree(db, pParse
1fa37 2d 3e 61 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 ->aAlias,.
1fa38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa39 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f sizeo
1fa3a 66 28 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 f(pParse->aAlias
1fa3b 5b 30 5d 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c [0])*pParse->nAl
1fa3c 69 61 73 20 29 3b 0a 20 20 20 20 74 65 73 74 63 ias );. testc
1fa3d 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ase( db->mallocF
1fa3e 61 69 6c 65 64 20 26 26 20 70 50 61 72 73 65 2d ailed && pParse-
1fa3f 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3e 30 20 29 >nAliasAlloc>0 )
1fa40 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 ;. if( db->ma
1fa41 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 llocFailed ) ret
1fa42 75 72 6e 20 30 3b 0a 20 20 20 20 6d 65 6d 73 65 urn 0;. memse
1fa43 74 28 26 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 t(&pParse->aAlia
1fa44 73 5b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 s[pParse->nAlias
1fa45 41 6c 6c 6f 63 5d 2c 20 30 2c 0a 20 20 20 20 20 Alloc], 0,.
1fa46 20 20 20 20 20 20 28 70 50 61 72 73 65 2d 3e 6e (pParse->n
1fa47 41 6c 69 61 73 2d 70 50 61 72 73 65 2d 3e 6e 41 Alias-pParse->nA
1fa48 6c 69 61 73 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f liasAlloc)*sizeo
1fa49 66 28 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 f(pParse->aAlias
1fa4a 5b 30 5d 29 29 3b 0a 20 20 20 20 70 50 61 72 73 [0]));. pPars
1fa4b 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 20 3d e->nAliasAlloc =
1fa4c 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 3b pParse->nAlias;
1fa4d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 . }. assert( i
1fa4e 41 6c 69 61 73 3e 30 20 26 26 20 69 41 6c 69 61 Alias>0 && iAlia
1fa4f 73 3c 3d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 s<=pParse->nAlia
1fa50 73 20 29 3b 0a 20 20 69 52 65 67 20 3d 20 70 50 s );. iReg = pP
1fa51 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c arse->aAlias[iAl
1fa52 69 61 73 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52 ias-1];. if( iR
1fa53 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 eg==0 ){. if(
1fa54 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c pParse->iCacheL
1fa55 65 76 65 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 evel>0 ){.
1fa56 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 iReg = sqlite3Ex
1fa57 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 prCodeTarget(pPa
1fa58 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 rse, pExpr, targ
1fa59 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a et);. }else{.
1fa5a 20 20 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70 iReg = ++p
1fa5b 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
1fa5c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
1fa5d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 de(pParse, pExpr
1fa5e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20 70 , iReg);. p
1fa5f 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 Parse->aAlias[iA
1fa60 6c 69 61 73 2d 31 5d 20 3d 20 69 52 65 67 3b 0a lias-1] = iReg;.
1fa61 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1fa62 72 6e 20 69 52 65 67 3b 0a 23 65 6c 73 65 0a 20 rn iReg;.#else.
1fa63 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1fa64 52 28 69 41 6c 69 61 73 29 3b 0a 20 20 72 65 74 R(iAlias);. ret
1fa65 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 43 urn sqlite3ExprC
1fa66 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 odeTarget(pParse
1fa67 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 , pExpr, target)
1fa68 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
1fa69 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1fa6a 69 6e 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 into the current
1fa6b 20 56 64 62 65 20 74 6f 20 65 76 61 6c 75 61 74 Vdbe to evaluat
1fa6c 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 e the given.** e
1fa6d 78 70 72 65 73 73 69 6f 6e 2e 20 20 41 74 74 65 xpression. Atte
1fa6e 6d 70 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 mpt to store the
1fa6f 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 results in regi
1fa70 73 74 65 72 20 22 74 61 72 67 65 74 22 2e 0a 2a ster "target"..*
1fa71 2a 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 * Return the reg
1fa72 69 73 74 65 72 20 77 68 65 72 65 20 72 65 73 75 ister where resu
1fa73 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 2e 0a lts are stored..
1fa74 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 69 73 20 **.** With this
1fa75 72 6f 75 74 69 6e 65 2c 20 74 68 65 72 65 20 69 routine, there i
1fa76 73 20 6e 6f 20 67 75 61 72 61 6e 74 65 65 20 74 s no guarantee t
1fa77 68 61 74 20 72 65 73 75 6c 74 73 20 77 69 6c 6c hat results will
1fa78 0a 2a 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e .** be stored in
1fa79 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 target. The re
1fa7a 73 75 6c 74 20 6d 69 67 68 74 20 62 65 20 73 74 sult might be st
1fa7b 6f 72 65 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 ored in some oth
1fa7c 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 69 er.** register i
1fa7d 66 20 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 f it is convenie
1fa7e 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 nt to do so. Th
1fa7f 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
1fa80 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b on.** must check
1fa81 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 the return code
1fa82 20 61 6e 64 20 6d 6f 76 65 20 74 68 65 20 72 65 and move the re
1fa83 73 75 6c 74 73 20 74 6f 20 74 68 65 20 64 65 73 sults to the des
1fa84 69 72 65 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 ired.** register
1fa85 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1fa86 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
1fa87 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 xprCodeTarget(Pa
1fa88 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
1fa89 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 r *pExpr, int ta
1fa8a 72 67 65 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 rget){. Vdbe *v
1fa8b 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
1fa8c 3b 20 20 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64 ; /* The VM und
1fa8d 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 er construction
1fa8e 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 */. int op;
1fa8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fa90 2a 20 54 68 65 20 6f 70 63 6f 64 65 20 62 65 69 * The opcode bei
1fa91 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e ng coded */. in
1fa92 74 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 t inReg = target
1fa93 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c ; /* Resul
1fa94 74 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 ts stored in reg
1fa95 69 73 74 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20 ister inReg */.
1fa96 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20 int regFree1 =
1fa97 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 0; /* If
1fa98 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 non-zero free t
1fa99 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 his temporary re
1fa9a 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 gister */. int
1fa9b 72 65 67 46 72 65 65 32 20 3d 20 30 3b 20 20 20 regFree2 = 0;
1fa9c 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d /* If non-
1fa9d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 zero free this t
1fa9e 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 emporary registe
1fa9f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72 r */. int r1, r
1faa0 32 2c 20 72 33 2c 20 72 34 3b 20 20 20 20 20 20 2, r3, r4;
1faa1 20 2f 2a 20 56 61 72 69 6f 75 73 20 72 65 67 69 /* Various regi
1faa2 73 74 65 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a ster numbers */.
1faa3 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a sqlite3 *db;..
1faa4 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
1faa5 62 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d b;. assert( v!=
1faa6 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 0 || db->mallocF
1faa7 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72 ailed );. asser
1faa8 74 28 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 t( target>0 && t
1faa9 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e arget<=pParse->n
1faaa 4d 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d Mem );. if( v==
1faab 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 0 ) return 0;..
1faac 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b if( pExpr==0 ){
1faad 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c . op = TK_NUL
1faae 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 L;. }else{.
1faaf 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a op = pExpr->op;.
1fab0 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 }. switch( op
1fab1 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
1fab2 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 AGG_COLUMN: {.
1fab3 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 AggInfo *pAg
1fab4 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 gInfo = pExpr->p
1fab5 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 AggInfo;. s
1fab6 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f truct AggInfo_co
1fab7 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 l *pCol = &pAggI
1fab8 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d nfo->aCol[pExpr-
1fab9 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66 >iAgg];. if
1faba 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 ( !pAggInfo->dir
1fabb 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 ectMode ){.
1fabc 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d assert( pCol-
1fabd 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20 >iMem>0 );.
1fabe 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d inReg = pCol-
1fabf 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62 >iMem;. b
1fac0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 reak;. }els
1fac1 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e e if( pAggInfo->
1fac2 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b useSortingIdx ){
1fac3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1fac4 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1fac5 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66 _Column, pAggInf
1fac6 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20 o->sortingIdx,.
1fac7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fac8 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f pCo
1fac9 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e l->iSorterColumn
1faca 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1facb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1facc 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 }. /* Other
1facd 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20 wise, fall thru
1face 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55 into the TK_COLU
1facf 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d MN case */. }
1fad0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c . case TK_COL
1fad1 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 UMN: {. if(
1fad2 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 pExpr->iTable<0
1fad3 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
1fad4 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 his only happens
1fad5 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 when coding che
1fad6 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a ck constraints *
1fad7 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 /. assert
1fad8 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 ( pParse->ckBase
1fad9 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e >0 );. in
1fada 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f Reg = pExpr->iCo
1fadb 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63 lumn + pParse->c
1fadc 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c kBase;. }el
1fadd 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 se{. test
1fade 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c case( (pExpr->fl
1fadf 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 ags & EP_AnyAff)
1fae0 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 !=0 );. i
1fae1 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 nReg = sqlite3Ex
1fae2 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 prCodeGetColumn(
1fae3 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1fae4 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Tab,.
1fae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fae6 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f pExpr->iCo
1fae7 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61 lumn, pExpr->iTa
1fae8 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 ble, target,.
1fae9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1faea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
1faeb 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f xpr->flags & EP_
1faec 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d AnyAff);. }
1faed 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1faee 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1faef 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 INTEGER: {.
1faf0 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 codeInteger(v,
1faf1 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74 pExpr, 0, target
1faf2 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1faf3 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
1faf4 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 K_FLOAT: {.
1faf5 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28 63 68 codeReal(v, (ch
1faf6 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e ar*)pExpr->token
1faf7 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e .z, pExpr->token
1faf8 2e 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a .n, 0, target);.
1faf9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1fafa 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 }. case TK_S
1fafb 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 73 TRING: {. s
1fafc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1fafd 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 (v, OP_String8,
1fafe 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 0a 20 20 0, target, 0,.
1faff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fb00 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 45 78 (char*)pEx
1fb01 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 pr->token.z, pEx
1fb02 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 pr->token.n);.
1fb03 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1fb04 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c . case TK_NUL
1fb05 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 L: {. sqlit
1fb06 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1fb07 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 OP_Null, 0, targ
1fb08 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b et);. break
1fb09 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
1fb0a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
1fb0b 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 _LITERAL. cas
1fb0c 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 e TK_BLOB: {.
1fb0d 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 int n;.
1fb0e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 const char *z;.
1fb0f 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 char *zBlob
1fb10 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1fb11 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d pExpr->token.n>=
1fb12 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 3 );. asser
1fb13 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e t( pExpr->token.
1fb14 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 z[0]=='x' || pEx
1fb15 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d pr->token.z[0]==
1fb16 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 'X' );. ass
1fb17 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 ert( pExpr->toke
1fb18 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a n.z[1]=='\'' );.
1fb19 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
1fb1a 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78 xpr->token.z[pEx
1fb1b 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d pr->token.n-1]==
1fb1c 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 6e 20 '\'' );. n
1fb1d 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e = pExpr->token.n
1fb1e 20 2d 20 33 3b 0a 20 20 20 20 20 20 7a 20 3d 20 - 3;. z =
1fb1f 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f (char*)pExpr->to
1fb20 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 20 ken.z + 2;.
1fb21 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 zBlob = sqlite3
1fb22 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 HexToBlob(sqlite
1fb23 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e 3VdbeDb(v), z, n
1fb24 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1fb25 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1fb26 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67 _Blob, n/2, targ
1fb27 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34 et, 0, zBlob, P4
1fb28 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 _DYNAMIC);.
1fb29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
1fb2a 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b ndif. case TK
1fb2b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 _VARIABLE: {.
1fb2c 20 20 20 69 6e 74 20 69 50 72 69 6f 72 3b 0a 20 int iPrior;.
1fb2d 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 VdbeOp *pOp
1fb2e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 ;. if( pExp
1fb2f 72 2d 3e 74 6f 6b 65 6e 2e 6e 3c 3d 31 0a 20 20 r->token.n<=1.
1fb30 20 20 20 20 20 20 20 26 26 20 28 69 50 72 69 6f && (iPrio
1fb31 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 r = sqlite3VdbeC
1fb32 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 31 29 urrentAddr(v)-1)
1fb33 3e 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 >=0. &&
1fb34 28 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 (pOp = sqlite3Vd
1fb35 62 65 47 65 74 4f 70 28 76 2c 20 69 50 72 69 6f beGetOp(v, iPrio
1fb36 72 29 29 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f r))->opcode==OP_
1fb37 56 61 72 69 61 62 6c 65 0a 20 20 20 20 20 20 20 Variable.
1fb38 20 20 26 26 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 && pOp->p1+pOp
1fb39 2d 3e 70 33 3d 3d 70 45 78 70 72 2d 3e 69 54 61 ->p3==pExpr->iTa
1fb3a 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20 ble. &&
1fb3b 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d pOp->p2+pOp->p3=
1fb3c 3d 74 61 72 67 65 74 0a 20 20 20 20 20 20 20 20 =target.
1fb3d 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 && pOp->p4.z==0
1fb3e 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 . ){.
1fb3f 20 20 2f 2a 20 49 66 20 74 68 65 20 70 72 65 76 /* If the prev
1fb40 69 6f 75 73 20 69 6e 73 74 72 75 63 74 69 6f 6e ious instruction
1fb41 20 77 61 73 20 61 20 63 6f 70 79 20 6f 66 20 74 was a copy of t
1fb42 68 65 20 70 72 65 76 69 6f 75 73 20 75 6e 6e 61 he previous unna
1fb43 6d 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 med. ** p
1fb44 61 72 61 6d 65 74 65 72 20 69 6e 74 6f 20 74 68 arameter into th
1fb45 65 20 70 72 65 76 69 6f 75 73 20 72 65 67 69 73 e previous regis
1fb46 74 65 72 2c 20 74 68 65 6e 20 73 69 6d 70 6c 79 ter, then simply
1fb47 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 0a 20 increment the.
1fb48 20 20 20 20 20 20 20 2a 2a 20 72 65 70 65 61 74 ** repeat
1fb49 20 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20 70 72 count on the pr
1fb4a 69 6f 72 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ior instruction
1fb4b 72 61 74 68 65 72 20 74 68 61 6e 20 6d 61 6b 69 rather than maki
1fb4c 6e 67 20 61 20 6e 65 77 0a 20 20 20 20 20 20 20 ng a new.
1fb4d 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ** instruction.
1fb4e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1fb4f 20 20 20 20 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 pOp->p3++;.
1fb50 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1fb51 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fb52 64 64 4f 70 33 28 76 2c 20 4f 50 5f 56 61 72 69 ddOp3(v, OP_Vari
1fb53 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 able, pExpr->iTa
1fb54 62 6c 65 2c 20 74 61 72 67 65 74 2c 20 31 29 3b ble, target, 1);
1fb55 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 . if( pEx
1fb56 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 31 20 29 7b pr->token.n>1 ){
1fb57 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1fb58 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 e3VdbeChangeP4(v
1fb59 2c 20 2d 31 2c 20 28 63 68 61 72 2a 29 70 45 78 , -1, (char*)pEx
1fb5a 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 pr->token.z, pEx
1fb5b 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 pr->token.n);.
1fb5c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1fb5d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1fb5e 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 }. case TK_R
1fb5f 45 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 EGISTER: {.
1fb60 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e inReg = pExpr->
1fb61 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 iTable;. br
1fb62 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1fb63 61 73 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 ase TK_AS: {.
1fb64 20 20 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 inReg = codeA
1fb65 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 lias(pParse, pEx
1fb66 70 72 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 pr->iTable, pExp
1fb67 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 r->pLeft, target
1fb68 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1fb69 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1fb6a 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 LITE_OMIT_CAST.
1fb6b 20 20 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a case TK_CAST:
1fb6c 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 {. /* Expr
1fb6d 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 essions of the f
1fb6e 6f 72 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 orm: CAST(pLef
1fb6f 74 20 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 t AS token) */.
1fb70 20 20 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f int aff, to
1fb71 5f 6f 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 _op;. inReg
1fb72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1fb73 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c deTarget(pParse,
1fb74 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 pExpr->pLeft, t
1fb75 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 61 66 arget);. af
1fb76 66 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e f = sqlite3Affin
1fb77 69 74 79 54 79 70 65 28 26 70 45 78 70 72 2d 3e ityType(&pExpr->
1fb78 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f token);. to
1fb79 5f 6f 70 20 3d 20 61 66 66 20 2d 20 53 51 4c 49 _op = aff - SQLI
1fb7a 54 45 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f 50 TE_AFF_TEXT + OP
1fb7b 5f 54 6f 54 65 78 74 3b 0a 20 20 20 20 20 20 61 _ToText;. a
1fb7c 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 ssert( to_op==OP
1fb7d 5f 54 6f 54 65 78 74 20 20 20 20 7c 7c 20 61 66 _ToText || af
1fb7e 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 f!=SQLITE_AFF_TE
1fb7f 58 54 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 XT );. a
1fb80 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 ssert( to_op==OP
1fb81 5f 54 6f 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66 _ToBlob || af
1fb82 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f f!=SQLITE_AFF_NO
1fb83 4e 45 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 NE );. a
1fb84 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 ssert( to_op==OP
1fb85 5f 54 6f 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66 _ToNumeric || af
1fb86 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 f!=SQLITE_AFF_NU
1fb87 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 20 20 61 MERIC );. a
1fb88 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 ssert( to_op==OP
1fb89 5f 54 6f 49 6e 74 20 20 20 20 20 7c 7c 20 61 66 _ToInt || af
1fb8a 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e f!=SQLITE_AFF_IN
1fb8b 54 45 47 45 52 20 29 3b 0a 20 20 20 20 20 20 61 TEGER );. a
1fb8c 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 ssert( to_op==OP
1fb8d 5f 54 6f 52 65 61 6c 20 20 20 20 7c 7c 20 61 66 _ToReal || af
1fb8e 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 f!=SQLITE_AFF_RE
1fb8f 41 4c 20 20 20 20 29 3b 0a 20 20 20 20 20 20 74 AL );. t
1fb90 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d estcase( to_op==
1fb91 4f 50 5f 54 6f 54 65 78 74 20 29 3b 0a 20 20 20 OP_ToText );.
1fb92 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f testcase( to_
1fb93 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b op==OP_ToBlob );
1fb94 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fb95 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d to_op==OP_ToNum
1fb96 65 72 69 63 20 29 3b 0a 20 20 20 20 20 20 74 65 eric );. te
1fb97 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f stcase( to_op==O
1fb98 50 5f 54 6f 49 6e 74 20 29 3b 0a 20 20 20 20 20 P_ToInt );.
1fb99 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 testcase( to_op
1fb9a 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 ==OP_ToReal );.
1fb9b 20 20 20 20 20 69 66 28 20 69 6e 52 65 67 21 3d if( inReg!=
1fb9c 74 61 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20 target ){.
1fb9d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1fb9e 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c Op2(v, OP_SCopy,
1fb9f 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b inReg, target);
1fba0 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d . inReg =
1fba1 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 7d target;. }
1fba2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1fba3 62 65 41 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f beAddOp1(v, to_o
1fba4 70 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 p, inReg);.
1fba5 20 74 65 73 74 63 61 73 65 28 20 75 73 65 64 41 testcase( usedA
1fba6 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 sColumnCache(pPa
1fba7 72 73 65 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 rse, inReg, inRe
1fba8 67 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 g) );. sqli
1fba9 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
1fbaa 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 nityChange(pPars
1fbab 65 2c 20 69 6e 52 65 67 2c 20 31 29 3b 0a 20 20 e, inReg, 1);.
1fbac 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1fbad 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1fbae 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 20 E_OMIT_CAST */.
1fbaf 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 case TK_LT:.
1fbb0 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 case TK_LE:.
1fbb1 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 case TK_GT:.
1fbb2 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 case TK_GE:.
1fbb3 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 case TK_NE:.
1fbb4 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b case TK_EQ: {
1fbb5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1fbb6 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 K_LT==OP_Lt );.
1fbb7 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1fbb8 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 LE==OP_Le );.
1fbb9 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 assert( TK_GT
1fbba 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 ==OP_Gt );.
1fbbb 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d assert( TK_GE==
1fbbc 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61 OP_Ge );. a
1fbbd 73 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 ssert( TK_EQ==OP
1fbbe 5f 45 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73 _Eq );. ass
1fbbf 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e ert( TK_NE==OP_N
1fbc0 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 e );. testc
1fbc1 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 ase( op==TK_LT )
1fbc2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fbc3 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 ( op==TK_LE );.
1fbc4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1fbc5 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 p==TK_GT );.
1fbc6 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1fbc7 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 TK_GE );. t
1fbc8 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1fbc9 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 EQ );. test
1fbca 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 case( op==TK_NE
1fbcb 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d );. codeCom
1fbcc 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 pareOperands(pPa
1fbcd 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1fbce 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 t, &r1, ®Free
1fbcf 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
1fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbd1 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 pExpr->pRig
1fbd2 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 ht, &r2, ®Fre
1fbd3 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 e2);. codeC
1fbd4 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 ompare(pParse, p
1fbd5 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 Expr->pLeft, pEx
1fbd6 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a pr->pRight, op,.
1fbd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbd8 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c r1, r2, inReg,
1fbd9 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 SQLITE_STOREP2)
1fbda 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fbdb 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
1fbdc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fbdd 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a regFree2==0 );.
1fbde 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1fbdf 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 }. case TK_A
1fbe0 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ND:. case TK_
1fbe1 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f OR:. case TK_
1fbe2 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 PLUS:. case T
1fbe3 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65 K_STAR:. case
1fbe4 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 TK_MINUS:. c
1fbe5 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 ase TK_REM:.
1fbe6 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a case TK_BITAND:.
1fbe7 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f case TK_BITO
1fbe8 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 R:. case TK_S
1fbe9 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54 LASH:. case T
1fbea 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61 K_LSHIFT:. ca
1fbeb 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 se TK_RSHIFT: .
1fbec 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 case TK_CONCA
1fbed 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 T: {. asser
1fbee 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e t( TK_AND==OP_An
1fbef 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 d );. asser
1fbf0 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 t( TK_OR==OP_Or
1fbf1 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1fbf2 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 TK_PLUS==OP_Add
1fbf3 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1fbf4 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 ( TK_MINUS==OP_S
1fbf5 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20 ubtract );.
1fbf6 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d assert( TK_REM=
1fbf7 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b =OP_Remainder );
1fbf8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1fbf9 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 K_BITAND==OP_Bit
1fbfa 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 And );. ass
1fbfb 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f ert( TK_BITOR==O
1fbfc 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 P_BitOr );.
1fbfd 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 assert( TK_SLAS
1fbfe 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a H==OP_Divide );.
1fbff 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1fc00 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 _LSHIFT==OP_Shif
1fc01 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61 tLeft );. a
1fc02 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 ssert( TK_RSHIFT
1fc03 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 ==OP_ShiftRight
1fc04 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1fc05 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 TK_CONCAT==OP_C
1fc06 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74 oncat );. t
1fc07 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1fc08 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 AND );. tes
1fc09 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 tcase( op==TK_OR
1fc0a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1fc0b 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 se( op==TK_PLUS
1fc0c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1fc0d 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 e( op==TK_MINUS
1fc0e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1fc0f 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b e( op==TK_REM );
1fc10 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fc11 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 op==TK_BITAND )
1fc12 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fc13 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 ( op==TK_BITOR )
1fc14 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fc15 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 ( op==TK_SLASH )
1fc16 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fc17 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 ( op==TK_LSHIFT
1fc18 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1fc19 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 e( op==TK_RSHIFT
1fc1a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1fc1b 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 se( op==TK_CONCA
1fc1c 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 T );. r1 =
1fc1d 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1fc1e 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
1fc1f 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
1fc20 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d ee1);. r2 =
1fc21 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1fc22 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
1fc23 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 pr->pRight, ®
1fc24 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71 Free2);. sq
1fc25 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1fc26 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 v, op, r2, r1, t
1fc27 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 arget);. te
1fc28 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
1fc29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ==0 );. tes
1fc2a 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
1fc2b 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
1fc2c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1fc2d 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 e TK_UMINUS: {.
1fc2e 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 Expr *pLeft
1fc2f 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b = pExpr->pLeft;
1fc30 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1fc31 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66 Left );. if
1fc32 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f ( pLeft->op==TK_
1fc33 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 FLOAT ){.
1fc34 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28 63 68 codeReal(v, (ch
1fc35 61 72 2a 29 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e ar*)pLeft->token
1fc36 2e 7a 2c 20 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e .z, pLeft->token
1fc37 2e 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a .n, 1, target);.
1fc38 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1fc39 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e pLeft->op==TK_IN
1fc3a 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 TEGER ){.
1fc3b 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 codeInteger(v,
1fc3c 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 pLeft, 1, target
1fc3d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1fc3e 20 20 20 20 20 20 20 20 72 65 67 46 72 65 65 31 regFree1
1fc3f 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 = r1 = sqlite3G
1fc40 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 etTempReg(pParse
1fc41 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1fc42 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1fc43 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 OP_Integer, 0, r
1fc44 31 29 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 1);. r2 =
1fc45 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1fc46 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
1fc47 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
1fc48 72 65 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 ree2);. s
1fc49 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1fc4a 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c (v, OP_Subtract,
1fc4b 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 r2, r1, target)
1fc4c 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
1fc4d 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 se( regFree2==0
1fc4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1fc4f 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b inReg = target;
1fc50 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1fc51 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1fc52 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 BITNOT:. case
1fc53 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 TK_NOT: {.
1fc54 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e assert( TK_BITN
1fc55 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b OT==OP_BitNot );
1fc56 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1fc57 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b K_NOT==OP_Not );
1fc58 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fc59 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 op==TK_BITNOT )
1fc5a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1fc5b 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a ( op==TK_NOT );.
1fc5c 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 r1 = sqlit
1fc5d 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
1fc5e 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1fc5f 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b eft, ®Free1);
1fc60 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1fc61 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
1fc62 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 inReg = ta
1fc63 72 67 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 rget;. sqli
1fc64 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1fc65 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b op, r1, inReg);
1fc66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1fc67 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1fc68 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 ISNULL:. case
1fc69 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 TK_NOTNULL: {.
1fc6a 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 int addr;.
1fc6b 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1fc6c 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c ISNULL==OP_IsNul
1fc6d 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 l );. asser
1fc6e 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f t( TK_NOTNULL==O
1fc6f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 P_NotNull );.
1fc70 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1fc71 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 =TK_ISNULL );.
1fc72 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1fc73 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a ==TK_NOTNULL );.
1fc74 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1fc75 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
1fc76 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 teger, 1, target
1fc77 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 );. r1 = sq
1fc78 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
1fc79 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d p(pParse, pExpr-
1fc7a 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 >pLeft, ®Free
1fc7b 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 1);. testca
1fc7c 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
1fc7d 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 );. addr =
1fc7e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1fc7f 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20 1(v, op, r1);.
1fc80 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fc81 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 ddOp2(v, OP_AddI
1fc82 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b mm, target, -1);
1fc83 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1fc84 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 beJumpHere(v, ad
1fc85 64 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b dr);. break
1fc86 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1fc87 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e TK_AGG_FUNCTION
1fc88 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 : {. AggInf
1fc89 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72 o *pInfo = pExpr
1fc8a 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 ->pAggInfo;.
1fc8b 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 if( pInfo==0 )
1fc8c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1fc8d 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1fc8e 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67 , "misuse of agg
1fc8f 72 65 67 61 74 65 3a 20 25 54 22 2c 0a 20 20 20 regate: %T",.
1fc90 20 20 20 20 20 20 20 20 20 26 70 45 78 70 72 2d &pExpr-
1fc91 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 7d 65 >span);. }e
1fc92 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 52 lse{. inR
1fc93 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75 6e eg = pInfo->aFun
1fc94 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e 69 c[pExpr->iAgg].i
1fc95 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 Mem;. }.
1fc96 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1fc97 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 case TK_CONS
1fc98 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 T_FUNC:. case
1fc99 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a TK_FUNCTION: {.
1fc9a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a ExprList *
1fc9b 70 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 pFarg; /*
1fc9c 4c 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e List of function
1fc9d 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
1fc9e 20 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 20 int nFarg;
1fc9f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1fca0 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f 6e mber of function
1fca1 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
1fca2 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 FuncDef *pDe
1fca3 66 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 f; /* Th
1fca4 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e e function defin
1fca5 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a ition object */.
1fca6 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 int nId;
1fca7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fca8 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 75 Length of the fu
1fca9 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20 62 nction name in b
1fcaa 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63 6f ytes */. co
1fcab 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 nst char *zId;
1fcac 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 /* The func
1fcad 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 tion name */.
1fcae 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73 6b int constMask
1fcaf 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61 73 = 0; /* Mas
1fcb0 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 k of function ar
1fcb1 67 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 guments that are
1fcb2 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 20 constant */.
1fcb3 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 int i;
1fcb4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1fcb5 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 p counter */.
1fcb6 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 u8 enc = ENC(
1fcb7 64 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 db); /* The
1fcb8 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 text encoding u
1fcb9 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 sed by this data
1fcba 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 6f base */. Co
1fcbb 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 llSeq *pColl = 0
1fcbc 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61 74 ; /* A collat
1fcbd 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ing sequence */.
1fcbe 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
1fcbf 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1fcc0 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c pExpr, EP_xIsSel
1fcc1 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74 65 ect) );. te
1fcc2 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 stcase( op==TK_C
1fcc3 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 ONST_FUNC );.
1fcc4 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1fcc5 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a =TK_FUNCTION );.
1fcc6 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 if( ExprHa
1fcc7 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 sAnyProperty(pEx
1fcc8 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 pr, EP_TokenOnly
1fcc9 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29 |EP_SpanToken) )
1fcca 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20 {. pFarg
1fccb 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
1fccc 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20 {. pFarg
1fccd 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 = pExpr->x.pList
1fcce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1fccf 6e 46 61 72 67 20 3d 20 70 46 61 72 67 20 3f 20 nFarg = pFarg ?
1fcd0 70 46 61 72 67 2d 3e 6e 45 78 70 72 20 3a 20 30 pFarg->nExpr : 0
1fcd1 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63 ;. zId = (c
1fcd2 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 har*)pExpr->toke
1fcd3 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d n.z;. nId =
1fcd4 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b pExpr->token.n;
1fcd5 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 . pDef = sq
1fcd6 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f lite3FindFunctio
1fcd7 6e 28 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 n(db, zId, nId,
1fcd8 6e 46 61 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a nFarg, enc, 0);.
1fcd9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 44 assert( pD
1fcda 65 66 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 ef!=0 );. i
1fcdb 66 28 20 70 46 61 72 67 20 29 7b 0a 20 20 20 20 f( pFarg ){.
1fcdc 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 r1 = sqlite3
1fcdd 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 GetTempRange(pPa
1fcde 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 rse, nFarg);.
1fcdf 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1fce0 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 CodeExprList(pPa
1fce1 72 73 65 2c 20 70 46 61 72 67 2c 20 72 31 2c 20 rse, pFarg, r1,
1fce2 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 1);. }else{
1fce3 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 30 3b . r1 = 0;
1fce4 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 . }.#ifndef
1fce5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1fce6 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 TUALTABLE.
1fce7 2f 2a 20 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 /* Possibly over
1fce8 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f load the functio
1fce9 6e 20 69 66 20 74 68 65 20 66 69 72 73 74 20 61 n if the first a
1fcea 72 67 75 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 rgument is.
1fceb 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 74 61 ** a virtual ta
1fcec 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 ble column..
1fced 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f **. ** Fo
1fcee 72 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e r infix function
1fcef 73 20 28 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 s (LIKE, GLOB, R
1fcf0 45 47 45 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 EGEXP, and MATCH
1fcf1 29 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 ) use the.
1fcf2 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 ** second argume
1fcf3 6e 74 2c 20 6e 6f 74 20 74 68 65 20 66 69 72 73 nt, not the firs
1fcf4 74 2c 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 t, as the argume
1fcf5 6e 74 20 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 nt to test to.
1fcf6 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74 ** see if it
1fcf7 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 is a column in
1fcf8 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e a virtual table.
1fcf9 20 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 This is done b
1fcfa 65 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 ecause. **
1fcfb 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 the left operand
1fcfc 20 6f 66 20 69 6e 66 69 78 20 66 75 6e 63 74 69 of infix functi
1fcfd 6f 6e 73 20 28 74 68 65 20 6f 70 65 72 61 6e 64 ons (the operand
1fcfe 20 77 65 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 we want to.
1fcff 20 20 2a 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 ** control ove
1fd00 72 6c 6f 61 64 69 6e 67 29 20 65 6e 64 73 20 75 rloading) ends u
1fd01 70 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 p as the second
1fd02 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a argument to the.
1fd03 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f ** functio
1fd04 6e 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 69 n. The expressi
1fd05 6f 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 69 73 on "A glob B" is
1fd06 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a equivalent to .
1fd07 20 20 20 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 ** "glob(B
1fd08 2c 41 29 2e 20 20 57 65 20 77 61 6e 74 20 74 6f ,A). We want to
1fd09 20 75 73 65 20 74 68 65 20 41 20 69 6e 20 22 41 use the A in "A
1fd0a 20 67 6c 6f 62 20 42 22 20 74 6f 20 74 65 73 74 glob B" to test
1fd0b 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 . ** for fu
1fd0c 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 nction overloadi
1fd0d 6e 67 2e 20 20 42 75 74 20 77 65 20 75 73 65 20 ng. But we use
1fd0e 74 68 65 20 42 20 74 65 72 6d 20 69 6e 20 22 67 the B term in "g
1fd0f 6c 6f 62 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 lob(B,A)"..
1fd10 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 46 */. if( nF
1fd11 61 72 67 3e 3d 32 20 26 26 20 28 70 45 78 70 72 arg>=2 && (pExpr
1fd12 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 ->flags & EP_Inf
1fd13 69 78 46 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 ixFunc) ){.
1fd14 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 pDef = sqlite
1fd15 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 3VtabOverloadFun
1fd16 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 ction(db, pDef,
1fd17 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b nFarg, pFarg->a[
1fd18 31 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 1].pExpr);.
1fd19 20 7d 65 6c 73 65 20 69 66 28 20 6e 46 61 72 67 }else if( nFarg
1fd1a 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 >0 ){. pD
1fd1b 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 ef = sqlite3Vtab
1fd1c 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e OverloadFunction
1fd1d 28 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 (db, pDef, nFarg
1fd1e 2c 20 70 46 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 , pFarg->a[0].pE
1fd1f 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 xpr);. }.#e
1fd20 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 69 ndif. for(i
1fd21 3d 30 3b 20 69 3c 6e 46 61 72 67 20 26 26 20 69 =0; i<nFarg && i
1fd22 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 <32; i++){.
1fd23 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 if( sqlite3Ex
1fd24 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 46 61 prIsConstant(pFa
1fd25 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 rg->a[i].pExpr)
1fd26 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e ){. con
1fd27 73 74 4d 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 stMask |= (1<<i)
1fd28 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1fd29 20 20 20 20 69 66 28 20 28 70 44 65 66 2d 3e 66 if( (pDef->f
1fd2a 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
1fd2b 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20 NC_NEEDCOLL)!=0
1fd2c 26 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 && !pColl ){.
1fd2d 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 pColl = s
1fd2e 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 qlite3ExprCollSe
1fd2f 71 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d q(pParse, pFarg-
1fd30 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 >a[i].pExpr);.
1fd31 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1fd32 20 20 20 20 20 20 69 66 28 20 70 44 65 66 2d 3e if( pDef->
1fd33 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 flags & SQLITE_F
1fd34 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a UNC_NEEDCOLL ){.
1fd35 20 20 20 20 20 20 20 20 69 66 28 20 21 70 43 6f if( !pCo
1fd36 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d ll ) pColl = db-
1fd37 3e 70 44 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 >pDfltColl; .
1fd38 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1fd39 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c AddOp4(v, OP_Col
1fd3a 6c 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 lSeq, 0, 0, 0, (
1fd3b 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 char *)pColl, P4
1fd3c 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 _COLLSEQ);.
1fd3d 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1fd3e 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
1fd3f 5f 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 _Function, const
1fd40 4d 61 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74 Mask, r1, target
1fd41 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1fd42 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a (char*
1fd43 29 70 44 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 )pDef, P4_FUNCDE
1fd44 46 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 F);. sqlite
1fd45 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 3VdbeChangeP5(v,
1fd46 20 28 75 38 29 6e 46 61 72 67 29 3b 0a 20 20 20 (u8)nFarg);.
1fd47 20 20 20 69 66 28 20 6e 46 61 72 67 20 29 7b 0a if( nFarg ){.
1fd48 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
1fd49 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 eleaseTempRange(
1fd4a 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61 72 pParse, r1, nFar
1fd4b 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 g);. }.
1fd4c 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
1fd4d 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
1fd4e 28 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61 (pParse, r1, nFa
1fd4f 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b rg);. break
1fd50 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
1fd51 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1fd52 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b UERY. case TK
1fd53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 73 _EXISTS:. cas
1fd54 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 e TK_SELECT: {.
1fd55 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1fd56 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a p==TK_EXISTS );.
1fd57 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1fd58 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b op==TK_SELECT );
1fd59 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 . if( pExpr
1fd5a 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a ->iColumn==0 ){.
1fd5b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 sqlite3C
1fd5c 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 odeSubselect(pPa
1fd5d 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 rse, pExpr, 0, 0
1fd5e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1fd5f 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e inReg = pExpr->
1fd60 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 62 iColumn;. b
1fd61 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1fd62 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 case TK_IN: {.
1fd63 20 20 20 20 69 6e 74 20 72 4e 6f 74 46 6f 75 6e int rNotFoun
1fd64 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 d = 0;. int
1fd65 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 rMayHaveNull =
1fd66 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 32 2c 0;. int j2,
1fd67 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20 20 j3, j4, j5;.
1fd68 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 char affinity
1fd69 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 54 79 70 ;. int eTyp
1fd6a 65 3b 0a 0a 20 20 20 20 20 20 56 64 62 65 4e 6f e;.. VdbeNo
1fd6b 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62 opComment((v, "b
1fd6c 65 67 69 6e 20 49 4e 20 65 78 70 72 20 72 25 64 egin IN expr r%d
1fd6d 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 20 ", target));.
1fd6e 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74 eType = sqlit
1fd6f 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70 50 e3FindInIndex(pP
1fd70 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 4d arse, pExpr, &rM
1fd71 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 ayHaveNull);.
1fd72 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e if( rMayHaveN
1fd73 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ull ){. r
1fd74 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 NotFound = ++pPa
1fd75 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 rse->nMem;.
1fd76 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 67 }.. /* Fig
1fd77 75 72 65 20 6f 75 74 20 74 68 65 20 61 66 66 69 ure out the affi
1fd78 6e 69 74 79 20 74 6f 20 75 73 65 20 74 6f 20 63 nity to use to c
1fd79 72 65 61 74 65 20 61 20 6b 65 79 20 66 72 6f 6d reate a key from
1fd7a 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 the results.
1fd7b 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 ** of the exp
1fd7c 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74 ression. affinit
1fd7d 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73 74 yStr stores a st
1fd7e 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69 74 atic string suit
1fd7f 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 20 20 2a able for. *
1fd80 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 * P4 of OP_MakeR
1fd81 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a ecord.. */.
1fd82 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 3d affinity =
1fd83 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e comparisonAffin
1fd84 69 74 79 28 70 45 78 70 72 29 3b 0a 0a 0a 20 20 ity(pExpr);...
1fd85 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 /* Code the
1fd86 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 3c 65 78 <expr> from "<ex
1fd87 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 2e 20 54 pr> IN (...)". T
1fd88 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 he temporary tab
1fd89 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70 45 78 70 le. ** pExp
1fd8a 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e 74 61 69 r->iTable contai
1fd8b 6e 73 20 74 68 65 20 76 61 6c 75 65 73 20 74 68 ns the values th
1fd8c 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 28 at make up the (
1fd8d 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20 20 20 20 ...) set..
1fd8e 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1fd8f 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 ExprCachePush(pP
1fd90 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c arse);. sql
1fd91 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
1fd92 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1fd93 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
1fd94 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 j2 = sqlite3Vd
1fd95 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 beAddOp1(v, OP_I
1fd96 73 4e 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b 0a sNull, target);.
1fd97 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d if( eType=
1fd98 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 =IN_INDEX_ROWID
1fd99 29 7b 0a 20 20 20 20 20 20 20 20 6a 33 20 3d 20 ){. j3 =
1fd9a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1fd9b 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 1(v, OP_MustBeIn
1fd9c 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
1fd9d 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 j4 = sqlite3
1fd9e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1fd9f 5f 4e 6f 74 45 78 69 73 74 73 2c 20 70 45 78 70 _NotExists, pExp
1fda0 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74 61 r->iTable, 0, ta
1fda1 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 rget);. s
1fda2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1fda3 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1fda4 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 1, target);.
1fda5 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 j5 = sqlite3
1fda6 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 VdbeAddOp0(v, OP
1fda7 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 _Goto);.
1fda8 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
1fda9 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 ere(v, j3);.
1fdaa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
1fdab 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a umpHere(v, j4);.
1fdac 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1fdad 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1fdae 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67 Integer, 0, targ
1fdaf 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 et);. }else
1fdb0 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 72 {. r2 = r
1fdb1 65 67 46 72 65 65 32 20 3d 20 73 71 6c 69 74 65 egFree2 = sqlite
1fdb2 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 3GetTempReg(pPar
1fdb3 73 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a se);.. /*
1fdb4 20 43 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 Create a record
1fdb5 20 61 6e 64 20 74 65 73 74 20 66 6f 72 20 73 65 and test for se
1fdb6 74 20 6d 65 6d 62 65 72 73 68 69 70 2e 20 49 66 t membership. If
1fdb7 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69 6e the set contain
1fdb8 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 s. ** the
1fdb9 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75 6d value, then jum
1fdba 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 p to the end of
1fdbb 74 68 65 20 74 65 73 74 20 63 6f 64 65 2e 20 54 the test code. T
1fdbc 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 20 20 he target.
1fdbd 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 73 74 ** register st
1fdbe 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ill contains the
1fdbf 20 74 72 75 65 20 28 31 29 20 76 61 6c 75 65 20 true (1) value
1fdc0 77 72 69 74 74 65 6e 20 74 6f 20 69 74 20 65 61 written to it ea
1fdc1 72 6c 69 65 72 2e 0a 20 20 20 20 20 20 20 20 2a rlier.. *
1fdc2 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 /. sqlite
1fdc3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1fdc4 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74 61 P_MakeRecord, ta
1fdc5 72 67 65 74 2c 20 31 2c 20 72 32 2c 20 26 61 66 rget, 1, r2, &af
1fdc6 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 finity, 1);.
1fdc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1fdc8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
1fdc9 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b ger, 1, target);
1fdca 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73 71 . j5 = sq
1fdcb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1fdcc 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 v, OP_Found, pEx
1fdcd 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 pr->iTable, 0, r
1fdce 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 2);.. /*
1fdcf 49 66 20 74 68 65 20 73 65 74 20 6d 65 6d 62 65 If the set membe
1fdd0 72 73 68 69 70 20 74 65 73 74 20 66 61 69 6c 73 rship test fails
1fdd1 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c , then the resul
1fdd2 74 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20 t of the .
1fdd3 20 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e 29 ** "x IN (...)
1fdd4 22 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 " expression mus
1fdd5 74 20 62 65 20 65 69 74 68 65 72 20 30 20 6f 72 t be either 0 or
1fdd6 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 65 NULL. If the se
1fdd7 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e t. ** con
1fdd8 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 tains no NULL va
1fdd9 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 lues, then the r
1fdda 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20 74 esult is 0. If t
1fddb 68 65 20 73 65 74 20 0a 20 20 20 20 20 20 20 20 he set .
1fddc 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 ** contains one
1fddd 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c or more NULL val
1fdde 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 ues, then the re
1fddf 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 sult of the.
1fde0 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f ** expressio
1fde1 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a n is also NULL..
1fde2 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1fde3 20 20 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e 64 if( rNotFound
1fde4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1fde5 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 /* This branch
1fde6 72 75 6e 73 20 69 66 20 69 74 20 69 73 20 6b 6e runs if it is kn
1fde7 6f 77 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 own at compile t
1fde8 69 6d 65 20 28 6e 6f 77 29 20 74 68 61 74 20 0a ime (now) that .
1fde9 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 ** the
1fdea 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f set contains no
1fdeb 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54 68 NULL values. Th
1fdec 69 73 20 68 61 70 70 65 6e 73 20 61 73 20 74 68 is happens as th
1fded 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 20 e result.
1fdee 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20 ** of a "NOT
1fdef 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 NULL" constraint
1fdf0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1fdf1 20 73 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 64 schema. No need
1fdf2 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f . ** to
1fdf3 20 74 65 73 74 20 74 68 65 20 64 61 74 61 20 73 test the data s
1fdf4 74 72 75 63 74 75 72 65 20 61 74 20 72 75 6e 74 tructure at runt
1fdf5 69 6d 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 ime in this case
1fdf6 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
1fdf7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1fdf8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1fdf9 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72 _Integer, 0, tar
1fdfa 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 get);. }e
1fdfb 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
1fdfc 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f 70 * This block pop
1fdfd 75 6c 61 74 65 73 20 74 68 65 20 72 4e 6f 74 46 ulates the rNotF
1fdfe 6f 75 6e 64 20 72 65 67 69 73 74 65 72 20 77 69 ound register wi
1fdff 74 68 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a 20 th either NULL.
1fe00 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 30 ** or 0
1fe01 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c (an integer val
1fe02 75 65 29 2e 20 49 66 20 74 68 65 20 64 61 74 61 ue). If the data
1fe03 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
1fe04 69 6e 73 20 6f 6e 65 0a 20 20 20 20 20 20 20 20 ins one.
1fe05 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 4c ** or more NUL
1fe06 4c 73 2c 20 74 68 65 6e 20 73 65 74 20 72 4e 6f Ls, then set rNo
1fe07 74 46 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e 20 tFound to NULL.
1fe08 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74 20 69 Otherwise, set i
1fe09 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 t. ** t
1fe0a 6f 20 30 2e 20 49 66 20 72 65 67 69 73 74 65 72 o 0. If register
1fe0b 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 rMayHaveNull is
1fe0c 20 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f 20 already set to
1fe0d 73 6f 6d 65 20 76 61 6c 75 65 0a 20 20 20 20 20 some value.
1fe0e 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 ** other th
1fe0f 61 6e 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 an NULL, then th
1fe10 65 20 74 65 73 74 20 68 61 73 20 61 6c 72 65 61 e test has alrea
1fe11 64 79 20 62 65 65 6e 20 72 75 6e 20 61 6e 64 20 dy been run and
1fe12 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 4e . ** rN
1fe13 6f 74 46 6f 75 6e 64 20 69 73 20 61 6c 72 65 61 otFound is alrea
1fe14 64 79 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20 20 dy populated..
1fe15 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1fe16 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 static cons
1fe17 74 20 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f 72 t char nullRecor
1fe18 64 5b 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30 78 d[] = { 0x02, 0x
1fe19 30 30 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 00 };.
1fe1a 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j3 = sqlite3Vdbe
1fe1b 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 AddOp1(v, OP_Not
1fe1c 4e 75 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e 75 Null, rMayHaveNu
1fe1d 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 ll);. s
1fe1e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1fe1f 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
1fe20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20 rNotFound);.
1fe21 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1fe22 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c eAddOp4(v, OP_Bl
1fe23 6f 62 2c 20 32 2c 20 72 4d 61 79 48 61 76 65 4e ob, 2, rMayHaveN
1fe24 75 6c 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 ull, 0, .
1fe25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe26 20 20 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72 64 nullRecord
1fe27 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 , P4_STATIC);.
1fe28 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71 6c j4 = sql
1fe29 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1fe2a 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 , OP_Found, pExp
1fe2b 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 4d r->iTable, 0, rM
1fe2c 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 ayHaveNull);.
1fe2d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1fe2e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
1fe2f 6e 74 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74 46 nteger, 0, rNotF
1fe30 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 ound);.
1fe31 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
1fe32 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 Here(v, j4);.
1fe33 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1fe34 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 beJumpHere(v, j3
1fe35 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a );.. /*
1fe36 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 20 Copy the value
1fe37 6f 66 20 72 65 67 69 73 74 65 72 20 72 4e 6f 74 of register rNot
1fe38 46 6f 75 6e 64 20 28 77 68 69 63 68 20 69 73 20 Found (which is
1fe39 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 30 either NULL or 0
1fe3a 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 ). ** i
1fe3b 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 72 nto the target r
1fe3c 65 67 69 73 74 65 72 2e 20 54 68 69 73 20 77 69 egister. This wi
1fe3d 6c 6c 20 62 65 20 74 68 65 20 72 65 73 75 6c 74 ll be the result
1fe3e 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 of the.
1fe3f 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e ** expression.
1fe40 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 . */.
1fe41 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1fe42 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1fe43 43 6f 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 2c Copy, rNotFound,
1fe44 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
1fe45 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1fe46 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1fe47 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 pHere(v, j2);.
1fe48 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
1fe49 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a umpHere(v, j5);.
1fe4a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1fe4b 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 rCachePop(pParse
1fe4c 2c 20 31 29 3b 0a 20 20 20 20 20 20 56 64 62 65 , 1);. Vdbe
1fe4d 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 Comment((v, "end
1fe4e 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 74 IN expr r%d", t
1fe4f 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 62 arget));. b
1fe50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 reak;. }.#end
1fe51 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a if. /*. **
1fe52 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 20 x BETWEEN y
1fe53 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 AND z. **.
1fe54 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75 69 ** This is equi
1fe55 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a 2a valent to. **
1fe56 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 . ** x>=y
1fe57 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a 0a AND x<=z. **.
1fe58 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f 72 ** X is stor
1fe59 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 ed in pExpr->pLe
1fe5a 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 ft.. ** Y is
1fe5b 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d stored in pExpr-
1fe5c 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 >pList->a[0].pEx
1fe5d 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20 pr.. ** Z is
1fe5e 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d stored in pExpr-
1fe5f 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 >pList->a[1].pEx
1fe60 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 pr.. */. c
1fe61 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 ase TK_BETWEEN:
1fe62 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c {. Expr *pL
1fe63 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 eft = pExpr->pLe
1fe64 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 ft;. struct
1fe65 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1fe66 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d 3e pLItem = pExpr->
1fe67 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 x.pList->a;.
1fe68 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d Expr *pRight =
1fe69 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a pLItem->pExpr;.
1fe6a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 . codeCompa
1fe6b 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 reOperands(pPars
1fe6c 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 e, pLeft, &r1, &
1fe6d 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 regFree1,.
1fe6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe6f 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 pRig
1fe70 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 ht, &r2, ®Fre
1fe71 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 e2);. testc
1fe72 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
1fe73 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1fe74 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 se( regFree2==0
1fe75 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73 71 );. r3 = sq
1fe76 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
1fe77 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72 pParse);. r
1fe78 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 4 = sqlite3GetTe
1fe79 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 mpReg(pParse);.
1fe7a 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 codeCompare
1fe7b 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 (pParse, pLeft,
1fe7c 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20 pRight, OP_Ge,.
1fe7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe7e 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c r1, r2, r3, SQL
1fe7f 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 ITE_STOREP2);.
1fe80 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 pLItem++;.
1fe81 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c 49 pRight = pLI
1fe82 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 tem->pExpr;.
1fe83 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
1fe84 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
1fe85 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
1fe86 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r2 = sqlite3Exp
1fe87 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
1fe88 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 , pRight, ®Fr
1fe89 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ee2);. test
1fe8a 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d case( regFree2==
1fe8b 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 0 );. codeC
1fe8c 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 ompare(pParse, p
1fe8d 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 Left, pRight, OP
1fe8e 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c _Le, r1, r2, r4,
1fe8f 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 SQLITE_STOREP2)
1fe90 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1fe91 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1fe92 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 72 And, r3, r4, tar
1fe93 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 get);. sqli
1fe94 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
1fe95 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a 20 g(pParse, r3);.
1fe96 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
1fe97 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
1fe98 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62 72 e, r4);. br
1fe99 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1fe9a 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a ase TK_UPLUS: {.
1fe9b 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 inReg = sq
1fe9c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 lite3ExprCodeTar
1fe9d 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 get(pParse, pExp
1fe9e 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 r->pLeft, target
1fe9f 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1fea0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 }.. /*.
1fea1 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20 ** Form A:.
1fea2 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48 45 ** CASE x WHE
1fea3 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 N e1 THEN r1 WHE
1fea4 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e N e2 THEN r2 ...
1fea5 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e WHEN eN THEN rN
1fea6 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 ELSE y END.
1fea7 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42 **. ** Form B
1fea8 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 :. ** CASE
1fea9 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 WHEN e1 THEN r1
1feaa 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 WHEN e2 THEN r2
1feab 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e ... WHEN eN THEN
1feac 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 rN ELSE y END.
1fead 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 **. ** For
1feae 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 72 m A is can be tr
1feaf 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74 ansformed into t
1feb0 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f he equivalent fo
1feb1 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a rm B as follows:
1feb2 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 . ** CASE W
1feb3 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31 HEN x=e1 THEN r1
1feb4 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20 WHEN x=e2 THEN
1feb5 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 r2 .... **
1feb6 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54 WHEN x=eN T
1feb7 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e HEN rN ELSE y EN
1feb8 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 D. **. **
1feb9 58 20 28 69 66 20 69 74 20 65 78 69 73 74 73 29 X (if it exists)
1feba 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c is in pExpr->pL
1febb 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 eft.. ** Y is
1febc 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 in pExpr->pRigh
1febd 74 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c 73 t. The Y is als
1febe 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 o optional. If
1febf 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 there is no.
1fec0 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61 ** ELSE clause a
1fec1 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d nd no other term
1fec2 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74 matches, then t
1fec3 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1fec4 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f . ** exprssio
1fec5 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a n is NULL.. *
1fec6 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78 70 72 * Ei is in pExpr
1fec7 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20 ->pList->a[i*2]
1fec8 61 6e 64 20 52 69 20 69 73 20 70 45 78 70 72 2d and Ri is pExpr-
1fec9 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d >pList->a[i*2+1]
1feca 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1fecb 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 The result of th
1fecc 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
1fecd 74 68 65 20 52 69 20 66 6f 72 20 74 68 65 20 66 the Ri for the f
1fece 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69 irst matching Ei
1fecf 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 ,. ** or if t
1fed0 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 here is no match
1fed1 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45 ing Ei, the ELSE
1fed2 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74 term Y, or if t
1fed3 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e here is. ** n
1fed4 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c o ELSE term, NUL
1fed5 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 L.. */. ca
1fed6 73 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a 20 20 se TK_CASE: {.
1fed7 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c int endLabel
1fed8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1fed9 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 /* GOTO la
1feda 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 43 bel for end of C
1fedb 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20 ASE stmt */.
1fedc 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b 20 int nextCase;
1fedd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fede 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 /* GOTO labe
1fedf 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e 20 l for next WHEN
1fee0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 clause */.
1fee1 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20 int nExpr;
1fee2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fee3 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f /* 2x number o
1fee4 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a f WHEN terms */.
1fee5 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 int i;
1fee6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fee7 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1fee8 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 counter */.
1fee9 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 ExprList *pELis
1feea 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
1feeb 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48 /* List of WH
1feec 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 EN terms */.
1feed 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
1feee 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65 t_item *aListele
1feef 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 m; /* Array of
1fef0 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 WHEN terms */.
1fef1 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70 61 Expr opCompa
1fef2 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 re;
1fef3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d /* The X==
1fef4 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f Ei expression */
1fef5 0a 20 20 20 20 20 20 45 78 70 72 20 63 61 63 68 . Expr cach
1fef6 65 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 eX;
1fef7 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 /* Cach
1fef8 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58 20 ed expression X
1fef9 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 */. Expr *p
1fefa 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 X;
1fefb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1fefc 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20 2a e X expression *
1fefd 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 /. Expr *pT
1fefe 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 est = 0;
1feff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d /* X==
1ff00 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a Ei (form A) or j
1ff01 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29 20 ust Ei (form B)
1ff02 2a 2f 0a 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c */. VVA_ONL
1ff03 59 28 20 69 6e 74 20 69 43 61 63 68 65 4c 65 76 Y( int iCacheLev
1ff04 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 el = pParse->iCa
1ff05 63 68 65 4c 65 76 65 6c 3b 20 29 0a 0a 20 20 20 cheLevel; )..
1ff06 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
1ff07 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
1ff08 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
1ff09 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 && pExpr->x.pLi
1ff0a 73 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 st );. asse
1ff0b 72 74 28 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 rt((pExpr->x.pLi
1ff0c 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 3d st->nExpr % 2) =
1ff0d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 = 0);. asse
1ff0e 72 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 rt(pExpr->x.pLis
1ff0f 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 t->nExpr > 0);.
1ff10 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 pEList = pE
1ff11 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 xpr->x.pList;.
1ff12 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 aListelem =
1ff13 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 pEList->a;.
1ff14 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d nExpr = pEList-
1ff15 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 65 6e >nExpr;. en
1ff16 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 dLabel = sqlite3
1ff17 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 VdbeMakeLabel(v)
1ff18 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 58 20 ;. if( (pX
1ff19 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 = pExpr->pLeft)!
1ff1a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 =0 ){. ca
1ff1b 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 cheX = *pX;.
1ff1c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 58 testcase( pX
1ff1d 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 ->op==TK_COLUMN
1ff1e 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 || pX->op==TK_RE
1ff1f 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20 GISTER );.
1ff20 20 20 63 61 63 68 65 58 2e 69 54 61 62 6c 65 20 cacheX.iTable
1ff21 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
1ff22 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58 eTemp(pParse, pX
1ff23 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 , ®Free1);.
1ff24 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ff25 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1ff26 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 6f 70 cacheX.op
1ff27 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a = TK_REGISTER;.
1ff28 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 opCompar
1ff29 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 e.op = TK_EQ;.
1ff2a 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e opCompare.
1ff2b 70 4c 65 66 74 20 3d 20 26 63 61 63 68 65 58 3b pLeft = &cacheX;
1ff2c 0a 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d . pTest =
1ff2d 20 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 &opCompare;.
1ff2e 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 }. for(i
1ff2f 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 =0; i<nExpr; i=i
1ff30 2b 32 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c +2){. sql
1ff31 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 ite3ExprCachePus
1ff32 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 h(pParse);.
1ff33 20 20 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 if( pX ){.
1ff34 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1ff35 54 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 Test!=0 );.
1ff36 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 opCompare.p
1ff37 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 Right = aListele
1ff38 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 m[i].pExpr;.
1ff39 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ff3a 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69 pTest = aLi
1ff3b 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b stelem[i].pExpr;
1ff3c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1ff3d 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71 nextCase = sq
1ff3e 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1ff3f 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74 el(v);. t
1ff40 65 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e estcase( pTest->
1ff41 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c op==TK_COLUMN ||
1ff42 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 52 pTest->op==TK_R
1ff43 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 EGISTER );.
1ff44 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 sqlite3ExprIf
1ff45 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 False(pParse, pT
1ff46 65 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 est, nextCase, S
1ff47 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1ff48 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
1ff49 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 ase( aListelem[i
1ff4a 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 +1].pExpr->op==T
1ff4b 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 K_COLUMN );.
1ff4c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c testcase( aL
1ff4d 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 istelem[i+1].pEx
1ff4e 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 pr->op==TK_REGIS
1ff4f 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 TER );. s
1ff50 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1ff51 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d Parse, aListelem
1ff52 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 [i+1].pExpr, tar
1ff53 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 get);. sq
1ff54 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1ff55 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 v, OP_Goto, 0, e
1ff56 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 ndLabel);.
1ff57 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
1ff58 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 hePop(pParse, 1)
1ff59 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1ff5a 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
1ff5b 6c 28 76 2c 20 6e 65 78 74 43 61 73 65 29 3b 0a l(v, nextCase);.
1ff5c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1ff5d 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 ( pExpr->pRight
1ff5e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1ff5f 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 e3ExprCachePush(
1ff60 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 pParse);.
1ff61 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1ff62 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
1ff63 70 52 69 67 68 74 2c 20 74 61 72 67 65 74 29 3b pRight, target);
1ff64 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ff65 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 ExprCachePop(pPa
1ff66 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d rse, 1);. }
1ff67 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
1ff68 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1ff69 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 v, OP_Null, 0, t
1ff6a 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a arget);. }.
1ff6b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 assert( db
1ff6c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c ->mallocFailed |
1ff6d 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 | pParse->nErr>0
1ff6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
1ff6f 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 pParse->iCacheLe
1ff70 76 65 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c vel==iCacheLevel
1ff71 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
1ff72 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
1ff73 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a l(v, endLabel);.
1ff74 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1ff75 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
1ff76 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 E_OMIT_TRIGGER.
1ff77 20 20 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45 case TK_RAISE
1ff78 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70 : {. if( !p
1ff79 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b Parse->trigStack
1ff7a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1ff7b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1ff7c 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 se,.
1ff7d 20 20 20 20 20 20 20 20 20 20 20 22 52 41 49 53 "RAIS
1ff7e 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 E() may only be
1ff7f 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 74 72 used within a tr
1ff80 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b igger-program");
1ff81 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1ff82 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
1ff83 20 69 66 28 20 70 45 78 70 72 2d 3e 61 66 66 69 if( pExpr->affi
1ff84 6e 69 74 79 21 3d 4f 45 5f 49 67 6e 6f 72 65 20 nity!=OE_Ignore
1ff85 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1ff86 72 74 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e rt( pExpr->affin
1ff87 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b ity==OE_Rollback
1ff88 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ||.
1ff89 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 pExpr->affi
1ff8a 6e 69 74 79 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 nity == OE_Abort
1ff8b 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ||.
1ff8c 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 pExpr->affi
1ff8d 6e 69 74 79 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 nity == OE_Fail
1ff8e 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
1ff8f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1ff90 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 OP_Halt, SQLITE
1ff91 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78 _CONSTRAINT, pEx
1ff92 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c 20 30 2c pr->affinity, 0,
1ff93 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1ff94 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 (char*)
1ff95 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 pExpr->token.z,
1ff96 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b pExpr->token.n);
1ff97 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a . } else {.
1ff98 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
1ff99 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
1ff9a 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72 65 20 29 3b == OE_Ignore );
1ff9b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 . sqlite
1ff9c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1ff9d 50 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c P_ContextPop, 0,
1ff9e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 0);. sq
1ff9f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1ffa0 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 v, OP_Goto, 0, p
1ffa1 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b Parse->trigStack
1ffa2 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20 ->ignoreJump);.
1ffa3 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d VdbeComm
1ffa4 65 6e 74 28 28 76 2c 20 22 72 61 69 73 65 28 49 ent((v, "raise(I
1ffa5 47 4e 4f 52 45 29 22 29 29 3b 0a 20 20 20 20 20 GNORE)"));.
1ffa6 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1ffa7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
1ffa8 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 . sqlite3Releas
1ffa9 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
1ffaa 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 regFree1);. sq
1ffab 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
1ffac 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 Reg(pParse, regF
1ffad 72 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 ree2);. return
1ffae 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 inReg;.}../*.**
1ffaf 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1ffb0 20 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70 evaluate an exp
1ffb1 72 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72 ression and stor
1ffb2 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a e the results.**
1ffb3 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 into a register
1ffb4 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 . Return the re
1ffb5 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 gister number wh
1ffb6 65 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a ere the results.
1ffb7 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a ** are stored..*
1ffb8 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69 *.** If the regi
1ffb9 73 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72 ster is a tempor
1ffba 61 72 79 20 72 65 67 69 73 74 65 72 20 74 68 61 ary register tha
1ffbb 74 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 t can be dealloc
1ffbc 61 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 ated,.** then wr
1ffbd 69 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69 ite its number i
1ffbe 6e 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74 nto *pReg. If t
1ffbf 68 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 he result regist
1ffc0 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 er is not.** a t
1ffc1 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73 emporary, then s
1ffc2 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f et *pReg to zero
1ffc3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1ffc4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
1ffc5 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73 xprCodeTemp(Pars
1ffc6 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
1ffc7 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65 *pExpr, int *pRe
1ffc8 67 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 g){. int r1 = s
1ffc9 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
1ffca 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 (pParse);. int
1ffcb 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r2 = sqlite3Expr
1ffcc 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
1ffcd 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 e, pExpr, r1);.
1ffce 20 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20 if( r2==r1 ){.
1ffcf 20 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20 *pReg = r1;.
1ffd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
1ffd1 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
1ffd2 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 g(pParse, r1);.
1ffd3 20 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20 *pReg = 0;.
1ffd4 7d 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d }. return r2;.}
1ffd5 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1ffd6 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 code that will
1ffd7 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 evaluate express
1ffd8 69 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 ion pExpr and st
1ffd9 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c ore the.** resul
1ffda 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 ts in register t
1ffdb 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 arget. The resu
1ffdc 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 lts are guarante
1ffdd 65 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20 ed to appear.**
1ffde 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 in register targ
1ffdf 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 et..*/.SQLITE_PR
1ffe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1ffe1 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 20 3ExprCode(Parse
1ffe2 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1ffe3 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 Expr, int target
1ffe4 29 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a ){. int inReg;.
1ffe5 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 . assert( targe
1ffe6 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 t>0 && target<=p
1ffe7 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 Parse->nMem );.
1ffe8 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 inReg = sqlite3
1ffe9 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
1ffea 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 Parse, pExpr, ta
1ffeb 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 rget);. assert(
1ffec 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c pParse->pVdbe |
1ffed 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 | pParse->db->ma
1ffee 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
1ffef 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 if( inReg!=targe
1fff0 74 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64 t && pParse->pVd
1fff1 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 be ){. sqlite
1fff2 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 3VdbeAddOp2(pPar
1fff3 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 se->pVdbe, OP_SC
1fff4 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 opy, inReg, targ
1fff5 65 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 et);. }. retur
1fff6 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a n target;.}../*.
1fff7 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1fff8 20 74 68 61 74 20 65 76 61 6c 75 74 65 73 20 74 that evalutes t
1fff9 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 he given express
1fffa 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65 ion and puts the
1fffb 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 result.** in re
1fffc 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a gister target..*
1fffd 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61 *.** Also make a
1fffe 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 copy of the exp
1ffff 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20 ression results
20000 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61 into another "ca
20001 63 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a che" register.**
20002 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 and modify the
20003 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 expression so th
20004 61 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 at the next time
20005 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 it is evaluated
20006 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 ,.** the result
20007 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 is a copy of the
20008 20 63 61 63 68 65 20 72 65 67 69 73 74 65 72 2e cache register.
20009 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
2000a 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 ine is used for
2000b 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 expressions that
2000c 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70 are used multip
2000d 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54 le .** times. T
2000e 68 65 79 20 61 72 65 20 65 76 61 6c 75 61 74 65 hey are evaluate
2000f 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72 d once and the r
20010 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 esults of the ex
20011 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 pression.** are
20012 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 reused..*/.SQLIT
20013 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20014 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 lite3ExprCodeAnd
20015 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 Cache(Parse *pPa
20016 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
20017 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 , int target){.
20018 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 Vdbe *v = pPars
20019 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 e->pVdbe;. int
2001a 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d inReg;. inReg =
2001b 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
2001c 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
2001d 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 target);. asser
2001e 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 t( target>0 );.
2001f 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d if( pExpr->op!=
20020 54 4b 5f 52 45 47 49 53 54 45 52 20 29 7b 20 20 TK_REGISTER ){
20021 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 . int iMem;.
20022 20 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 iMem = ++pPar
20023 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 se->nMem;. sq
20024 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
20025 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65 v, OP_Copy, inRe
20026 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45 g, iMem);. pE
20027 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d xpr->iTable = iM
20028 65 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f em;. pExpr->o
20029 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b p = TK_REGISTER;
2002a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e . }. return in
2002b 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 Reg;.}../*.** Re
2002c 74 75 72 6e 20 54 52 55 45 20 69 66 20 70 45 78 turn TRUE if pEx
2002d 70 72 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e pr is an constan
2002e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 t expression tha
2002f 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 t is appropriate
20030 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e .** for factorin
20031 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e g out of a loop.
20032 20 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 78 Appropriate ex
20033 70 72 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a pressions are:.*
20034 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 *.** * Any e
20035 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 xpression that e
20036 76 61 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20 valuates to two
20037 6f 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e or more opcodes.
20038 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 .**.** * Any
20039 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f OP_Integer, OP_
2003a 52 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c Real, OP_String,
2003b 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c OP_Blob, OP_Nul
2003c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 l, .** or
2003d 4f 50 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74 OP_Variable that
2003e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
2003f 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 o be placed in a
20040 20 0a 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69 .** speci
20041 66 69 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a fic register..**
20042 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 .** There is no
20043 70 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 point in factori
20044 6e 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e ng out single-in
20045 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 struction consta
20046 6e 74 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e nt.** expression
20047 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 s that need to b
20048 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61 e placed in a pa
20049 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 74 65 rticular registe
2004a 72 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 r. .** We could
2004b 20 66 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 factor them out
2004c 2c 20 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f , but then we wo
2004d 75 6c 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e uld end up addin
2004e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 g an.** OP_SCopy
2004f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 instruction to
20050 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 69 move the value i
20051 6e 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 nto the correct
20052 72 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 register.** late
20053 72 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 r. We might as
20054 77 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68 well just use th
20055 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 e original instr
20056 75 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 uction and.** av
20057 6f 69 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 oid the OP_SCopy
20058 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
20059 69 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 isAppropriateFor
2005a 46 61 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a Factoring(Expr *
2005b 70 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 p){. if( !sqlit
2005c 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
2005d 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 NotJoin(p) ){.
2005e 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 return 0; /*
2005f 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 Only constant ex
20060 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70 pressions are ap
20061 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61 propriate for fa
20062 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 ctoring */. }.
20063 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 if( (p->flags &
20064 20 45 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d EP_FixedDest)==
20065 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
20066 31 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 1; /* Any const
20067 61 6e 74 20 77 69 74 68 6f 75 74 20 61 20 66 69 ant without a fi
20068 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 xed destination
20069 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a is appropriate *
2006a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 /. }. while( p
2006b 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 ->op==TK_UPLUS )
2006c 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 p = p->pLeft;.
2006d 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 switch( p->op )
2006e 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
2006f 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
20070 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 AL. case TK_B
20071 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 LOB:.#endif.
20072 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 case TK_VARIABLE
20073 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e :. case TK_IN
20074 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 TEGER:. case
20075 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 TK_FLOAT:. ca
20076 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 se TK_NULL:.
20077 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 case TK_STRING:
20078 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
20079 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 ( p->op==TK_BLOB
2007a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
2007b 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 se( p->op==TK_VA
2007c 52 49 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 RIABLE );.
2007d 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d testcase( p->op=
2007e 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 =TK_INTEGER );.
2007f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
20080 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 ->op==TK_FLOAT )
20081 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
20082 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c ( p->op==TK_NULL
20083 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
20084 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 se( p->op==TK_ST
20085 52 49 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a RING );. /*
20086 20 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 Single-instruct
20087 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 ion constants wi
20088 74 68 20 61 20 66 69 78 65 64 20 64 65 73 74 69 th a fixed desti
20089 6e 61 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 nation are.
2008a 20 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20 ** better done
2008b 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 in-line. If we
2008c 66 61 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 factor them, the
2008d 79 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a y will just end.
2008e 20 20 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 ** up gene
2008f 72 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f rating an OP_SCo
20090 70 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 py to move the v
20091 61 6c 75 65 20 74 6f 20 74 68 65 20 64 65 73 74 alue to the dest
20092 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a ination. **
20093 20 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 register. */.
20094 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
20095 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
20096 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 UMINUS: {.
20097 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f if( p->pLeft->o
20098 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 p==TK_FLOAT || p
20099 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f ->pLeft->op==TK_
2009a 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 INTEGER ){.
2009b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
2009c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 62 72 }. br
2009d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
2009e 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
2009f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
200a0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
200a1 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 /*.** If pExpr i
200a2 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 s a constant exp
200a3 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 ression that is
200a4 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a appropriate for.
200a5 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 ** factoring out
200a6 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e of a loop, then
200a7 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 evaluate the ex
200a8 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f pression.** into
200a9 20 61 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 a register and
200aa 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 convert the expr
200ab 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b ession into a TK
200ac 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 _REGISTER.** exp
200ad 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 ression..*/.stat
200ae 69 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 ic int evalConst
200af 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 Expr(Walker *pWa
200b0 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 lker, Expr *pExp
200b1 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 r){. Parse *pPa
200b2 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 rse = pWalker->p
200b3 50 61 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 Parse;. switch(
200b4 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
200b5 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 case TK_REGIST
200b6 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 ER: {. retu
200b7 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
200b8 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
200b9 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 :. case TK_AG
200ba 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 G_FUNCTION:.
200bb 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 case TK_CONST_FU
200bc 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 NC: {. /* T
200bd 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 he arguments to
200be 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20 a function have
200bf 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 a fixed destinat
200c0 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61 ion.. ** Ma
200c1 72 6b 20 74 68 65 6d 20 74 68 69 73 20 77 61 79 rk them this way
200c2 20 74 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61 to avoid genera
200c3 74 65 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f ted unneeded OP_
200c4 53 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69 SCopy. ** i
200c5 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20 nstructions. .
200c6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 */. Exp
200c7 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 rList *pList = p
200c8 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 Expr->x.pList;.
200c9 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
200ca 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
200cb 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
200cc 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 t) );. if(
200cd 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 pList ){.
200ce 20 69 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d 3e int i = pList->
200cf 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 73 nExpr;. s
200d0 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
200d1 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c 69 tem *pItem = pLi
200d2 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 st->a;. f
200d3 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 or(; i>0; i--, p
200d4 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 Item++){.
200d5 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 45 if( pItem->pE
200d6 78 70 72 20 29 20 70 49 74 65 6d 2d 3e 70 45 78 xpr ) pItem->pEx
200d7 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f pr->flags |= EP_
200d8 46 69 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20 FixedDest;.
200d9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
200da 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
200db 20 20 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72 }. if( isAppr
200dc 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72 opriateForFactor
200dd 69 6e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20 ing(pExpr) ){.
200de 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61 int r1 = ++pPa
200df 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 rse->nMem;. i
200e0 6e 74 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20 nt r2;. r2 =
200e1 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
200e2 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
200e3 78 70 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66 xpr, r1);. if
200e4 28 20 72 31 21 3d 72 32 20 29 20 73 71 6c 69 74 ( r1!=r2 ) sqlit
200e5 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
200e6 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 (pParse, r1);.
200e7 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
200e8 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 70 _REGISTER;. p
200e9 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 72 Expr->iTable = r
200ea 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 2;. return WR
200eb 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 72 C_Prune;. }. r
200ec 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
200ed 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 ue;.}../*.** Pre
200ee 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 74 61 6e evaluate constan
200ef 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 t subexpressions
200f0 20 77 69 74 68 69 6e 20 70 45 78 70 72 20 61 6e within pExpr an
200f1 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 d store the.** r
200f2 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 esults in regist
200f3 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 70 45 78 ers. Modify pEx
200f4 70 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 pr so that the c
200f5 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 onstant subexpre
200f6 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b 5f sions.** are TK_
200f7 52 45 47 49 53 54 45 52 20 6f 70 63 6f 64 65 73 REGISTER opcodes
200f8 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 that refer to t
200f9 68 65 20 70 72 65 63 6f 6d 70 75 74 65 64 20 76 he precomputed v
200fa 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 alues..*/.SQLITE
200fb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
200fc 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e lite3ExprCodeCon
200fd 73 74 61 6e 74 73 28 50 61 72 73 65 20 2a 70 50 stants(Parse *pP
200fe 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
200ff 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a r){. Walker w;.
20100 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 w.xExprCallbac
20101 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 k = evalConstExp
20102 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 r;. w.xSelectCa
20103 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 2e llback = 0;. w.
20104 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
20105 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 . sqlite3WalkEx
20106 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d pr(&w, pExpr);.}
20107 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 .../*.** Generat
20108 65 20 63 6f 64 65 20 74 68 61 74 20 70 75 73 68 e code that push
20109 65 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 es the value of
2010a 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f 66 every element of
2010b 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 the given.** ex
2010c 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 6e pression list in
2010d 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 to a sequence of
2010e 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e registers begin
2010f 6e 69 6e 67 20 61 74 20 74 61 72 67 65 74 2e 0a ning at target..
20110 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 **.** Return the
20111 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 number of eleme
20112 6e 74 73 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a nts evaluated..*
20113 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20114 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
20115 43 6f 64 65 45 78 70 72 4c 69 73 74 28 0a 20 20 CodeExprList(.
20116 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
20117 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
20118 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c ntext */. ExprL
20119 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a ist *pList, /*
2011a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
2011b 6c 69 73 74 20 74 6f 20 62 65 20 63 6f 64 65 64 list to be coded
2011c 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 74 */. int target
2011d 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 , /* Wher
2011e 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c e to write resul
2011f 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 61 ts */. int doHa
20120 72 64 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d 61 rdCopy /* Ma
20121 6b 65 20 61 20 68 61 72 64 20 63 6f 70 79 20 6f ke a hard copy o
20122 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 f every element
20123 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45 */.){. struct E
20124 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
20125 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b tem;. int i, n;
20126 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 . assert( pList
20127 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
20128 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 6e target>0 );. n
20129 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b = pList->nExpr;
2012a 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 . for(pItem=pLi
2012b 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e 3b st->a, i=0; i<n;
2012c 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
2012d 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 if( pItem->i
2012e 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 69 Alias ){. i
2012f 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 65 41 6c nt iReg = codeAl
20130 69 61 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 ias(pParse, pIte
20131 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 74 65 6d m->iAlias, pItem
20132 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b ->pExpr, target+
20133 69 29 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a i);. Vdbe *
20134 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
20135 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 be(pParse);.
20136 20 20 69 66 28 20 69 52 65 67 21 3d 74 61 72 67 if( iReg!=targ
20137 65 74 2b 69 20 29 7b 0a 20 20 20 20 20 20 20 20 et+i ){.
20138 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20139 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 2(v, OP_SCopy, i
2013a 52 65 67 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a Reg, target+i);.
2013b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
2013c 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
2013d 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
2013e 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 pItem->pExpr, t
2013f 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 7d 0a arget+i);. }.
20140 20 20 20 20 69 66 28 20 64 6f 48 61 72 64 43 6f if( doHardCo
20141 70 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 py ){. sqli
20142 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 te3ExprHardCopy(
20143 70 50 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20 pParse, target,
20144 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 n);. }. }.
20145 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a return n;.}../*.
20146 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
20147 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 for a boolean e
20148 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 xpression such t
20149 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 hat a jump is ma
2014a 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 de.** to the lab
2014b 65 6c 20 22 64 65 73 74 22 20 69 66 20 74 68 65 el "dest" if the
2014c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 expression is t
2014d 72 75 65 20 62 75 74 20 65 78 65 63 75 74 69 6f rue but executio
2014e 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 n.** continues s
2014f 74 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20 traight thru if
20150 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
20151 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 s false..**.** I
20152 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
20153 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 evaluates to NU
20154 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 LL (neither true
20155 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65 nor false), the
20156 6e 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 n.** take the ju
20157 6d 70 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66 mp if the jumpIf
20158 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c Null flag is SQL
20159 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a ITE_JUMPIFNULL..
2015a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 **.** This code
2015b 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66 depends on the f
2015c 61 63 74 20 74 68 61 74 20 63 65 72 74 61 69 6e act that certain
2015d 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65 token values (e
2015e 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65 x: TK_EQ).** are
2015f 20 74 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63 the same as opc
20160 6f 64 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20 ode values (ex:
20161 4f 50 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c OP_Eq) that impl
20162 65 6d 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73 ement the corres
20163 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 ponding.** opera
20164 74 69 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63 tion. Special c
20165 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e omments in vdbe.
20166 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f c and the mkopco
20167 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69 deh.awk script i
20168 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72 n.** the make pr
20169 6f 63 65 73 73 20 63 61 75 73 65 20 74 68 65 73 ocess cause thes
2016a 65 20 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67 e values to alig
2016b 6e 2e 20 20 41 73 73 65 72 74 28 29 73 20 69 6e n. Assert()s in
2016c 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c the code.** bel
2016d 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 74 ow verify that t
2016e 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 he numbers are a
2016f 6c 69 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79 ligned correctly
20170 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20171 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20172 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65 ExprIfTrue(Parse
20173 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
20174 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c pExpr, int dest,
20175 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 int jumpIfNull)
20176 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 {. Vdbe *v = pP
20177 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 arse->pVdbe;. i
20178 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 nt op = 0;. int
20179 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 regFree1 = 0;.
2017a 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 int regFree2 =
2017b 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0;. int r1, r2;
2017c 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 .. assert( jump
2017d 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a IfNull==SQLITE_J
2017e 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d UMPIFNULL || jum
2017f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
20180 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78 70 if( v==0 || pExp
20181 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 r==0 ) return;.
20182 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b op = pExpr->op;
20183 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
20184 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 . case TK_AND
20185 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 : {. int d2
20186 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
20187 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 keLabel(v);.
20188 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 testcase( jump
20189 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 IfNull==0 );.
2018a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
2018b 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b chePush(pParse);
2018c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
2018d 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 prIfFalse(pParse
2018e 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
2018f 64 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 d2,jumpIfNull^SQ
20190 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 LITE_JUMPIFNULL)
20191 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
20192 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 xprIfTrue(pParse
20193 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c , pExpr->pRight,
20194 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c dest, jumpIfNul
20195 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 l);. sqlite
20196 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
20197 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20 l(v, d2);.
20198 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
20199 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a Pop(pParse, 1);.
2019a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2019b 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f }. case TK_O
2019c 52 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 R: {. testc
2019d 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d ase( jumpIfNull=
2019e 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =0 );. sqli
2019f 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 te3ExprIfTrue(pP
201a0 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
201a1 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 ft, dest, jumpIf
201a2 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c Null);. sql
201a3 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 ite3ExprIfTrue(p
201a4 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 Parse, pExpr->pR
201a5 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 ight, dest, jump
201a6 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 IfNull);. b
201a7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
201a8 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 case TK_NOT: {.
201a9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a testcase( j
201aa 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a umpIfNull==0 );.
201ab 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
201ac 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c rIfFalse(pParse,
201ad 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 pExpr->pLeft, d
201ae 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
201af 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
201b0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
201b1 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LT:. case TK
201b2 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LE:. case TK
201b3 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GT:. case TK
201b4 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GE:. case TK
201b5 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _NE:. case TK
201b6 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 _EQ: {. ass
201b7 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c ert( TK_LT==OP_L
201b8 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t );. asser
201b9 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 t( TK_LE==OP_Le
201ba 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
201bb 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b TK_GT==OP_Gt );
201bc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
201bd 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 K_GE==OP_Ge );.
201be 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
201bf 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 EQ==OP_Eq );.
201c0 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 assert( TK_NE
201c1 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 ==OP_Ne );.
201c2 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
201c3 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 K_LT );. te
201c4 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c stcase( op==TK_L
201c5 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 E );. testc
201c6 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 ase( op==TK_GT )
201c7 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
201c8 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 ( op==TK_GE );.
201c9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
201ca 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 p==TK_EQ );.
201cb 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
201cc 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 74 TK_NE );. t
201cd 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e estcase( jumpIfN
201ce 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ull==0 );.
201cf 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 codeCompareOpera
201d0 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 nds(pParse, pExp
201d1 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 r->pLeft, &r1, &
201d2 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 regFree1,.
201d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
201d4 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
201d5 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 r->pRight, &r2,
201d6 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 ®Free2);.
201d7 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 codeCompare(pP
201d8 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
201d9 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 ft, pExpr->pRigh
201da 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 t, op,.
201db 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c r1, r2,
201dc 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c dest, jumpIfNul
201dd 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 l);. testca
201de 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
201df 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
201e0 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 e( regFree2==0 )
201e1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
201e2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
201e3 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 _ISNULL:. cas
201e4 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a e TK_NOTNULL: {.
201e5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
201e6 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 _ISNULL==OP_IsNu
201e7 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ll );. asse
201e8 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d rt( TK_NOTNULL==
201e9 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 OP_NotNull );.
201ea 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
201eb 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 ==TK_ISNULL );.
201ec 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
201ed 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b p==TK_NOTNULL );
201ee 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 . r1 = sqli
201ef 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
201f0 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
201f1 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 Left, ®Free1)
201f2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
201f3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c dbeAddOp2(v, op,
201f4 20 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 r1, dest);.
201f5 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
201f6 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree1==0 );.
201f7 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
201f8 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 case TK_BETWEE
201f9 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 N: {. /*
201fa 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 x BETWEEN y AND
201fb 20 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 z. **.
201fc 20 20 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 ** Is equivale
201fd 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a nt to . **.
201fe 20 20 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 ** x>=y
201ff 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20 AND x<=z.
20200 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65 **. ** Code
20201 20 69 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b it as such, tak
20202 69 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74 ing care to do t
20203 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 he common subexp
20204 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a ression. **
20205 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 elementation of
20206 20 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 x.. */.
20207 20 20 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b Expr exprAnd;
20208 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 . Expr comp
20209 4c 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 Left;. Expr
2020a 20 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 compRight;.
2020b 20 20 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20 Expr exprX;..
2020c 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
2020d 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
2020e 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
2020f 74 29 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 t) );. expr
20210 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 X = *pExpr->pLef
20211 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 t;. exprAnd
20212 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 .op = TK_AND;.
20213 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 exprAnd.pLef
20214 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 t = &compLeft;.
20215 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69 exprAnd.pRi
20216 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 ght = &compRight
20217 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 ;. compLeft
20218 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 .op = TK_GE;.
20219 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 compLeft.pLef
2021a 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 t = &exprX;.
2021b 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 compLeft.pRigh
2021c 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 t = pExpr->x.pLi
2021d 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a st->a[0].pExpr;.
2021e 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e compRight.
2021f 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 op = TK_LE;.
20220 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 compRight.pLef
20221 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 t = &exprX;.
20222 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 compRight.pRig
20223 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c ht = pExpr->x.pL
20224 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b ist->a[1].pExpr;
20225 0a 20 20 20 20 20 20 65 78 70 72 58 2e 69 54 61 . exprX.iTa
20226 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ble = sqlite3Exp
20227 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
20228 2c 20 26 65 78 70 72 58 2c 20 26 72 65 67 46 72 , &exprX, ®Fr
20229 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ee1);. test
2022a 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
2022b 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58 0 );. exprX
2022c 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 .op = TK_REGISTE
2022d 52 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 R;. testcas
2022e 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 e( jumpIfNull==0
2022f 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
20230 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 3ExprIfTrue(pPar
20231 73 65 2c 20 26 65 78 70 72 41 6e 64 2c 20 64 65 se, &exprAnd, de
20232 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b st, jumpIfNull);
20233 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
20234 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
20235 20 7b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 {. r1 = sq
20236 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
20237 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c p(pParse, pExpr,
20238 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 ®Free1);.
20239 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2023a 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 2c 20 72 dOp3(v, OP_If, r
2023b 31 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 1, dest, jumpIfN
2023c 75 6c 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 ull!=0);. t
2023d 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
2023e 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 1==0 );. te
2023f 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 stcase( jumpIfNu
20240 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 ll==0 );. b
20241 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
20242 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
20243 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
20244 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c regFree1);. sql
20245 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
20246 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 eg(pParse, regFr
20247 65 65 32 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a ee2); .}../*.**
20248 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 Generate code f
20249 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 or a boolean exp
2024a 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61 ression such tha
2024b 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 t a jump is made
2024c 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c .** to the label
2024d 20 22 64 65 73 74 22 20 69 66 20 74 68 65 20 65 "dest" if the e
2024e 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c xpression is fal
2024f 73 65 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e se but execution
20250 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 .** continues st
20251 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20 74 raight thru if t
20252 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
20253 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 true..**.** If
20254 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65 the expression e
20255 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c valuates to NULL
20256 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e (neither true n
20257 6f 72 20 66 61 6c 73 65 29 20 74 68 65 6e 0a 2a or false) then.*
20258 2a 20 6a 75 6d 70 20 69 66 20 6a 75 6d 70 49 66 * jump if jumpIf
20259 4e 75 6c 6c 20 69 73 20 53 51 4c 49 54 45 5f 4a Null is SQLITE_J
2025a 55 4d 50 49 46 4e 55 4c 4c 20 6f 72 20 66 61 6c UMPIFNULL or fal
2025b 6c 20 74 68 72 6f 75 67 68 20 69 66 20 6a 75 6d l through if jum
2025c 70 49 66 4e 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e pIfNull.** is 0.
2025d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
2025e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
2025f 78 70 72 49 66 46 61 6c 73 65 28 50 61 72 73 65 xprIfFalse(Parse
20260 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
20261 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c pExpr, int dest,
20262 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 int jumpIfNull)
20263 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 {. Vdbe *v = pP
20264 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 arse->pVdbe;. i
20265 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 nt op = 0;. int
20266 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 regFree1 = 0;.
20267 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 int regFree2 =
20268 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0;. int r1, r2;
20269 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 .. assert( jump
2026a 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a IfNull==SQLITE_J
2026b 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d UMPIFNULL || jum
2026c 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
2026d 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78 70 if( v==0 || pExp
2026e 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a r==0 ) return;..
2026f 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f /* The value o
20270 66 20 70 45 78 70 72 2d 3e 6f 70 20 61 6e 64 20 f pExpr->op and
20271 6f 70 20 61 72 65 20 72 65 6c 61 74 65 64 20 61 op are related a
20272 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a s follows:. **.
20273 20 20 2a 2a 20 20 20 20 20 20 20 70 45 78 70 72 ** pExpr
20274 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 ->op
20275 6f 70 0a 20 20 2a 2a 20 20 20 20 20 20 20 2d 2d op. ** --
20276 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 20 20 20 20 -------
20277 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a ----------. **
20278 20 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c TK_ISNULL
20279 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 6f 74 OP_Not
2027a 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 Null. **
2027b 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 TK_NOTNULL
2027c 20 20 20 4f 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a OP_IsNull. *
2027d 2a 20 20 20 20 20 20 20 54 4b 5f 4e 45 20 20 20 * TK_NE
2027e 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 45 71 OP_Eq
2027f 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 45 . ** TK_E
20280 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f Q O
20281 50 5f 4e 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 P_Ne. **
20282 54 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 TK_GT
20283 20 20 20 4f 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 OP_Le. **
20284 20 20 20 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 TK_LE
20285 20 20 20 20 20 20 20 4f 50 5f 47 74 0a 20 20 2a OP_Gt. *
20286 2a 20 20 20 20 20 20 20 54 4b 5f 47 45 20 20 20 * TK_GE
20287 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 74 OP_Lt
20288 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c . ** TK_L
20289 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f T O
2028a 50 5f 47 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 P_Ge. **. ** F
2028b 6f 72 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 or other values
2028c 6f 66 20 70 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 of pExpr->op, op
2028d 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e is undefined an
2028e 64 20 75 6e 75 73 65 64 2e 0a 20 20 2a 2a 20 54 d unused.. ** T
2028f 68 65 20 76 61 6c 75 65 20 6f 66 20 54 4b 5f 20 he value of TK_
20290 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 and OP_ constant
20291 73 20 61 72 65 20 61 72 72 61 6e 67 65 64 20 73 s are arranged s
20292 75 63 68 20 74 68 61 74 20 77 65 0a 20 20 2a 2a uch that we. **
20293 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 can compute the
20294 20 6d 61 70 70 69 6e 67 20 61 62 6f 76 65 20 75 mapping above u
20295 73 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 sing the followi
20296 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 ng expression..
20297 20 2a 2a 20 41 73 73 65 72 74 28 29 73 20 76 65 ** Assert()s ve
20298 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f rify that the co
20299 6d 70 75 74 61 74 69 6f 6e 20 69 73 20 63 6f 72 mputation is cor
2029a 72 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 rect.. */. op
2029b 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70 2b 28 54 = ((pExpr->op+(T
2029c 4b 5f 49 53 4e 55 4c 4c 26 31 29 29 5e 31 29 2d K_ISNULL&1))^1)-
2029d 28 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a (TK_ISNULL&1);..
2029e 20 20 2f 2a 20 56 65 72 69 66 79 20 63 6f 72 72 /* Verify corr
2029f 65 63 74 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 ect alignment of
202a0 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e TK_ and OP_ con
202a1 73 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 stants. */. as
202a2 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 sert( pExpr->op!
202a3 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 =TK_ISNULL || op
202a4 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a ==OP_NotNull );.
202a5 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
202a6 3e 6f 70 21 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 >op!=TK_NOTNULL
202a7 7c 7c 20 6f 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c || op==OP_IsNull
202a8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
202a9 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c xpr->op!=TK_NE |
202aa 7c 20 6f 70 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 | op==OP_Eq );.
202ab 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
202ac 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d op!=TK_EQ || op=
202ad 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 61 73 73 65 =OP_Ne );. asse
202ae 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 rt( pExpr->op!=T
202af 4b 5f 4c 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 K_LT || op==OP_G
202b0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e );. assert( p
202b1 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 Expr->op!=TK_LE
202b2 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a || op==OP_Gt );.
202b3 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
202b4 3e 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 >op!=TK_GT || op
202b5 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 61 73 73 ==OP_Le );. ass
202b6 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d ert( pExpr->op!=
202b7 54 4b 5f 47 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f TK_GE || op==OP_
202b8 4c 74 20 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 Lt );.. switch(
202b9 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
202ba 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b case TK_AND: {
202bb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
202bc 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
202bd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
202be 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 xprIfFalse(pPars
202bf 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
202c0 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c dest, jumpIfNul
202c1 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 l);. sqlite
202c2 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 3ExprIfFalse(pPa
202c3 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 rse, pExpr->pRig
202c4 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 ht, dest, jumpIf
202c5 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 Null);. bre
202c6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
202c7 73 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 se TK_OR: {.
202c8 20 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 int d2 = sqlit
202c9 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
202ca 76 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 v);. testca
202cb 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d se( jumpIfNull==
202cc 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 );. sqlit
202cd 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 e3ExprCachePush(
202ce 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 pParse);. s
202cf 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 qlite3ExprIfTrue
202d0 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
202d1 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 49 pLeft, d2, jumpI
202d2 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d fNull^SQLITE_JUM
202d3 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 PIFNULL);.
202d4 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c sqlite3ExprIfFal
202d5 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 se(pParse, pExpr
202d6 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 ->pRight, dest,
202d7 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 jumpIfNull);.
202d8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 sqlite3VdbeRe
202d9 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 solveLabel(v, d2
202da 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
202db 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 ExprCachePop(pPa
202dc 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 rse, 1);. b
202dd 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
202de 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 case TK_NOT: {.
202df 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
202e0 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 IfTrue(pParse, p
202e1 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 Expr->pLeft, des
202e2 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a t, jumpIfNull);.
202e3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
202e4 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c }. case TK_L
202e5 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c T:. case TK_L
202e6 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 E:. case TK_G
202e7 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 T:. case TK_G
202e8 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e E:. case TK_N
202e9 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 E:. case TK_E
202ea 51 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 Q: {. testc
202eb 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 ase( op==TK_LT )
202ec 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
202ed 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 ( op==TK_LE );.
202ee 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
202ef 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 p==TK_GT );.
202f0 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
202f1 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 TK_GE );. t
202f2 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
202f3 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 EQ );. test
202f4 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 case( op==TK_NE
202f5 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
202f6 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 e( jumpIfNull==0
202f7 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f );. codeCo
202f8 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 mpareOperands(pP
202f9 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
202fa 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 ft, &r1, ®Fre
202fb 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 e1,.
202fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
202fd 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 pExpr->pRi
202fe 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 ght, &r2, ®Fr
202ff 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 ee2);. code
20300 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 Compare(pParse,
20301 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 pExpr->pLeft, pE
20302 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c xpr->pRight, op,
20303 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
20304 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c r1, r2, dest,
20305 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
20306 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
20307 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 gFree1==0 );.
20308 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
20309 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 Free2==0 );.
2030a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
2030b 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c case TK_ISNUL
2030c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e L:. case TK_N
2030d 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 OTNULL: {.
2030e 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
2030f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 _ISNULL );.
20310 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
20311 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 K_NOTNULL );.
20312 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 r1 = sqlite3E
20313 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
20314 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
20315 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 , ®Free1);.
20316 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20317 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c ddOp2(v, op, r1,
20318 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 dest);. te
20319 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
2031a 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
2031b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
2031c 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b se TK_BETWEEN: {
2031d 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42 . /* x B
2031e 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 ETWEEN y AND z.
2031f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
20320 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 Is equivalent t
20321 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 o . **.
20322 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 ** x>=y AND
20323 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 x<=z. **.
20324 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20 ** Code it
20325 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 as such, taking
20326 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 care to do the c
20327 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 ommon subexpress
20328 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 ion. ** ele
20329 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a mentation of x..
2032a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 */. E
2032b 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20 xpr exprAnd;.
2032c 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 Expr compLeft
2032d 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d ;. Expr com
2032e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78 pRight;. Ex
2032f 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20 pr exprX;..
20330 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
20331 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
20332 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
20333 3b 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 ;. exprX =
20334 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 *pExpr->pLeft;.
20335 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 exprAnd.op
20336 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 = TK_AND;.
20337 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 exprAnd.pLeft =
20338 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 &compLeft;.
20339 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 exprAnd.pRight
2033a 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 = &compRight;.
2033b 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 compLeft.op
2033c 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 = TK_GE;. c
2033d 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 ompLeft.pLeft =
2033e 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f &exprX;. co
2033f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 mpLeft.pRight =
20340 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e pExpr->x.pList->
20341 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 a[0].pExpr;.
20342 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d compRight.op =
20343 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f TK_LE;. co
20344 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 mpRight.pLeft =
20345 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f &exprX;. co
20346 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d mpRight.pRight =
20347 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d pExpr->x.pList-
20348 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 >a[1].pExpr;.
20349 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 exprX.iTable
2034a 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
2034b 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 eTemp(pParse, &e
2034c 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 xprX, ®Free1)
2034d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
2034e 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
2034f 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 . exprX.op
20350 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 = TK_REGISTER;.
20351 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a testcase( j
20352 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a umpIfNull==0 );.
20353 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
20354 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c rIfFalse(pParse,
20355 20 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c &exprAnd, dest,
20356 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
20357 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
20358 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
20359 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 r1 = sqlit
2035a 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
2035b 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 Parse, pExpr, &r
2035c 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 egFree1);.
2035d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2035e 33 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 3(v, OP_IfNot, r
2035f 31 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 1, dest, jumpIfN
20360 75 6c 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 ull!=0);. t
20361 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
20362 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 1==0 );. te
20363 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 stcase( jumpIfNu
20364 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 ll==0 );. b
20365 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
20366 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
20367 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
20368 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c regFree1);. sql
20369 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
2036a 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 eg(pParse, regFr
2036b 65 65 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ee2);.}../*.** D
2036c 6f 20 61 20 64 65 65 70 20 63 6f 6d 70 61 72 69 o a deep compari
2036d 73 6f 6e 20 6f 66 20 74 77 6f 20 65 78 70 72 65 son of two expre
2036e 73 73 69 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 ssion trees. Re
2036f 74 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a turn TRUE (non-z
20370 65 72 6f 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 ero).** if they
20371 61 72 65 20 69 64 65 6e 74 69 63 61 6c 20 61 6e are identical an
20372 64 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 69 d return FALSE i
20373 66 20 74 68 65 79 20 64 69 66 66 65 72 20 69 6e f they differ in
20374 20 61 6e 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 any way..**.**
20375 53 6f 6d 65 74 69 6d 65 73 20 74 68 69 73 20 72 Sometimes this r
20376 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 outine will retu
20377 72 6e 20 46 41 4c 53 45 20 65 76 65 6e 20 69 66 rn FALSE even if
20378 20 74 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 the two express
20379 69 6f 6e 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 ions.** really a
2037a 72 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 re equivalent.
2037b 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f If we cannot pro
2037c 76 65 20 74 68 61 74 20 74 68 65 20 65 78 70 72 ve that the expr
2037d 65 73 73 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 essions are.** i
2037e 64 65 6e 74 69 63 61 6c 2c 20 77 65 20 72 65 74 dentical, we ret
2037f 75 72 6e 20 46 41 4c 53 45 20 6a 75 73 74 20 74 urn FALSE just t
20380 6f 20 62 65 20 73 61 66 65 2e 20 20 53 6f 20 69 o be safe. So i
20381 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a f this routine.*
20382 2a 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 2c * returns false,
20383 20 74 68 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 then you do not
20384 20 72 65 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 really know for
20385 20 63 65 72 74 61 69 6e 20 69 66 20 74 68 65 20 certain if the
20386 74 77 6f 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f two.** expressio
20387 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e ns are the same.
20388 20 20 42 75 74 20 69 66 20 79 6f 75 20 67 65 74 But if you get
20389 20 61 20 54 52 55 45 20 72 65 74 75 72 6e 2c 20 a TRUE return,
2038a 74 68 65 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 then you.** can
2038b 62 65 20 73 75 72 65 20 74 68 65 20 65 78 70 72 be sure the expr
2038c 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20 essions are the
2038d 73 61 6d 65 2e 20 20 49 6e 20 74 68 65 20 70 6c same. In the pl
2038e 61 63 65 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 aces where.** th
2038f 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
20390 65 64 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 ed, it does not
20391 68 75 72 74 20 74 6f 20 67 65 74 20 61 6e 20 65 hurt to get an e
20392 78 74 72 61 20 46 41 4c 53 45 20 2d 20 74 68 61 xtra FALSE - tha
20393 74 0a 2a 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 t.** just might
20394 72 65 73 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 result in some s
20395 6c 69 67 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 lightly slower c
20396 6f 64 65 2e 20 20 42 75 74 20 72 65 74 75 72 6e ode. But return
20397 69 6e 67 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 ing.** an incorr
20398 65 63 74 20 54 52 55 45 20 63 6f 75 6c 64 20 6c ect TRUE could l
20399 65 61 64 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 ead to a malfunc
2039a 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
2039b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
2039c 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 te3ExprCompare(E
2039d 78 70 72 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 xpr *pA, Expr *p
2039e 42 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 B){. int i;. i
2039f 66 28 20 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 f( pA==0||pB==0
203a0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 ){. return pB
203a1 3d 3d 70 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ==pA;. }. if(
203a2 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
203a3 70 41 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pA, EP_xIsSelect
203a4 29 20 7c 7c 20 45 78 70 72 48 61 73 50 72 6f 70 ) || ExprHasProp
203a5 65 72 74 79 28 70 42 2c 20 45 50 5f 78 49 73 53 erty(pB, EP_xIsS
203a6 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 72 65 elect) ){. re
203a7 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 turn 0;. }. if
203a8 28 20 28 70 41 2d 3e 66 6c 61 67 73 20 26 20 45 ( (pA->flags & E
203a9 50 5f 44 69 73 74 69 6e 63 74 29 21 3d 28 70 42 P_Distinct)!=(pB
203aa 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 ->flags & EP_Dis
203ab 74 69 6e 63 74 29 20 29 20 72 65 74 75 72 6e 20 tinct) ) return
203ac 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70 21 0;. if( pA->op!
203ad 3d 70 42 2d 3e 6f 70 20 29 20 72 65 74 75 72 6e =pB->op ) return
203ae 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 0;. if( !sqlit
203af 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 41 e3ExprCompare(pA
203b0 2d 3e 70 4c 65 66 74 2c 20 70 42 2d 3e 70 4c 65 ->pLeft, pB->pLe
203b1 66 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ft) ) return 0;.
203b2 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 if( !sqlite3Ex
203b3 70 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 52 prCompare(pA->pR
203b4 69 67 68 74 2c 20 70 42 2d 3e 70 52 69 67 68 74 ight, pB->pRight
203b5 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 ) ) return 0;..
203b6 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 if( pA->x.pList
203b7 20 26 26 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20 && pB->x.pList
203b8 29 7b 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 78 ){. if( pA->x
203b9 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70 .pList->nExpr!=p
203ba 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 B->x.pList->nExp
203bb 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 r ) return 0;.
203bc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 2d for(i=0; i<pA-
203bd 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b >x.pList->nExpr;
203be 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 i++){. Exp
203bf 72 20 2a 70 45 78 70 72 41 20 3d 20 70 41 2d 3e r *pExprA = pA->
203c0 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 x.pList->a[i].pE
203c1 78 70 72 3b 0a 20 20 20 20 20 20 45 78 70 72 20 xpr;. Expr
203c2 2a 70 45 78 70 72 42 20 3d 20 70 42 2d 3e 78 2e *pExprB = pB->x.
203c3 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 pList->a[i].pExp
203c4 72 3b 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 r;. if( !sq
203c5 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 lite3ExprCompare
203c6 28 70 45 78 70 72 41 2c 20 70 45 78 70 72 42 29 (pExprA, pExprB)
203c7 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
203c8 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }. }else if( p
203c9 41 2d 3e 78 2e 70 4c 69 73 74 20 7c 7c 20 70 42 A->x.pList || pB
203ca 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 ->x.pList ){.
203cb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a return 0;. }..
203cc 20 20 69 66 28 20 70 41 2d 3e 69 54 61 62 6c 65 if( pA->iTable
203cd 21 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c 7c 20 !=pB->iTable ||
203ce 70 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 42 2d pA->iColumn!=pB-
203cf 3e 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74 75 72 >iColumn ) retur
203d0 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e 6f n 0;. if( pA->o
203d1 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 p!=TK_COLUMN &&
203d2 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b 0a 20 pA->token.z ){.
203d3 20 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b 65 6e if( pB->token
203d4 2e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 .z==0 ) return 0
203d5 3b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 74 6f ;. if( pB->to
203d6 6b 65 6e 2e 6e 21 3d 70 41 2d 3e 74 6f 6b 65 6e ken.n!=pA->token
203d7 2e 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 .n ) return 0;.
203d8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 if( sqlite3St
203d9 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70 41 rNICmp((char*)pA
203da 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28 63 68 61 72 2a ->token.z,(char*
203db 29 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 2c 70 42 2d )pB->token.z,pB-
203dc 3e 74 6f 6b 65 6e 2e 6e 29 21 3d 30 20 29 7b 0a >token.n)!=0 ){.
203dd 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
203de 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
203df 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn 1;.}.../*.**
203e0 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e Add a new elemen
203e1 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66 t to the pAggInf
203e2 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e o->aCol[] array.
203e3 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 Return the ind
203e4 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 ex of.** the new
203e5 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 element. Retur
203e6 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d n a negative num
203e7 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 ber if malloc fa
203e8 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ils..*/.static i
203e9 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c nt addAggInfoCol
203ea 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c umn(sqlite3 *db,
203eb 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 AggInfo *pInfo)
203ec 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e {. int i;. pIn
203ed 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 fo->aCol = sqlit
203ee 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 e3ArrayAllocate(
203ef 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20 . db,.
203f0 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a pInfo->aCol,.
203f1 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49 sizeof(pI
203f2 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 nfo->aCol[0]),.
203f3 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20 3,.
203f4 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c &pInfo->nColumn,
203f5 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e . &pInfo->
203f6 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 nColumnAlloc,.
203f7 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 &i. );. r
203f8 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a eturn i;.} ..
203f9 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 /*.** Add a new
203fa 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 element to the p
203fb 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d AggInfo->aFunc[]
203fc 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20 array. Return
203fd 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 the index of.**
203fe 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e the new element.
203ff 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74 Return a negat
20400 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 ive number if ma
20401 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 lloc fails..*/.s
20402 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67 67 tatic int addAgg
20403 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74 65 33 InfoFunc(sqlite3
20404 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 *db, AggInfo *p
20405 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a Info){. int i;.
20406 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d pInfo->aFunc =
20407 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c sqlite3ArrayAll
20408 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20 64 62 ocate(. db
20409 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66 6f 2d , . pInfo-
2040a 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 73 >aFunc,. s
2040b 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75 izeof(pInfo->aFu
2040c 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 nc[0]),. 3
2040d 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d ,. &pInfo-
2040e 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 26 >nFunc,. &
2040f 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f pInfo->nFuncAllo
20410 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29 c,. &i. )
20411 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 ;. return i;.}
20412 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ../*.** This
20413 69 73 20 74 68 65 20 78 45 78 70 72 43 61 6c 6c is the xExprCall
20414 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65 65 20 back for a tree
20415 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73 20 75 walker. It is u
20416 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d sed to.** implem
20417 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 41 ent sqlite3ExprA
20418 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 nalyzeAggregates
20419 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 (). See sqlite3
2041a 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 ExprAnalyzeAggre
2041b 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64 gates.** for add
2041c 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
2041d 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
2041e 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 nt analyzeAggreg
2041f 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c ate(Walker *pWal
20420 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 ker, Expr *pExpr
20421 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 ){. int i;. Na
20422 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d meContext *pNC =
20423 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b pWalker->u.pNC;
20424 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20425 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a = pNC->pParse;.
20426 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c SrcList *pSrcL
20427 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c ist = pNC->pSrcL
20428 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a ist;. AggInfo *
20429 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e pAggInfo = pNC->
2042a 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 pAggInfo;.. swi
2042b 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 tch( pExpr->op )
2042c 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 {. case TK_AG
2042d 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 G_COLUMN:. ca
2042e 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a se TK_COLUMN: {.
2042f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
20430 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 pExpr->op==TK_AG
20431 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 G_COLUMN );.
20432 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
20433 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e r->op==TK_COLUMN
20434 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 );. /* Che
20435 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ck to see if the
20436 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e column is in on
20437 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20 e of the tables
20438 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 20 20 in the FROM.
20439 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66 20 74 ** clause of t
2043a 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65 he aggregate que
2043b 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 ry */. if(
2043c 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20 20 pSrcList ){.
2043d 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
2043e 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d st_item *pItem =
2043f 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 pSrcList->a;.
20440 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
20441 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b <pSrcList->nSrc;
20442 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
20443 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 struct
20444 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 AggInfo_col *pC
20445 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ol;. if
20446 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d ( pExpr->iTable=
20447 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 =pItem->iCursor
20448 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f ){. /
20449 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 * If we reach th
2044a 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 is point, it mea
2044b 6e 73 20 74 68 61 74 20 70 45 78 70 72 20 72 65 ns that pExpr re
2044c 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 0a fers to a table.
2044d 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
2044e 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 46 52 hat is in the FR
2044f 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 OM clause of the
20450 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 aggregate query
20451 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 . .
20452 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a **. *
20453 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20 * Make an entry
20454 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 for the column i
20455 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c n pAggInfo->aCol
20456 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20 20 20 [] if there.
20457 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f ** is no
20458 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65 72 65 t an entry there
20459 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 20 20 already..
2045a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
2045b 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 int k;.
2045c 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 70 pCol = p
2045d 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a 20 AggInfo->aCol;.
2045e 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b for(k
2045f 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f 2d 3e =0; k<pAggInfo->
20460 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 70 43 nColumn; k++, pC
20461 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 ol++){.
20462 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 if( pCol->i
20463 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 Table==pExpr->iT
20464 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 able &&.
20465 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e pCol->
20466 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e iColumn==pExpr->
20467 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 iColumn ){.
20468 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
20469 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
2046a 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
2046b 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
2046c 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 (k>=pAggInfo->nC
2046d 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 20 20 olumn).
2046e 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64 64 41 && (k = addA
2046f 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 50 61 ggInfoColumn(pPa
20470 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e 66 rse->db, pAggInf
20471 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 20 20 o))>=0 .
20472 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ){.
20473 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 41 67 pCol = &pAg
20474 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b 0a gInfo->aCol[k];.
20475 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 pC
20476 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 70 72 ol->pTab = pExpr
20477 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 ->pTab;.
20478 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 61 62 pCol->iTab
20479 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 le = pExpr->iTab
2047a 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 le;.
2047b 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 20 pCol->iColumn
2047c 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e = pExpr->iColumn
2047d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
2047e 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 pCol->iMem = ++p
2047f 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
20480 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d pCol-
20481 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d >iSorterColumn =
20482 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 -1;.
20483 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 20 3d pCol->pExpr =
20484 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 pExpr;.
20485 20 20 20 20 20 20 69 66 28 20 70 41 67 67 49 6e if( pAggIn
20486 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a fo->pGroupBy ){.
20487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20488 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 20 20 int j, n;.
20489 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 ExprLi
2048a 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67 49 6e st *pGB = pAggIn
2048b 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 fo->pGroupBy;.
2048c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 st
2048d 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
2048e 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 42 2d em *pTerm = pGB-
2048f 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 >a;.
20490 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e 45 78 n = pGB->nEx
20491 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 pr;.
20492 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e for(j=0; j<n
20493 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b ; j++, pTerm++){
20494 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
20495 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 54 Expr *pE = pT
20496 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 erm->pExpr;.
20497 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
20498 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c ( pE->op==TK_COL
20499 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 62 6c UMN && pE->iTabl
2049a 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 e==pExpr->iTable
2049b 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 &&.
2049c 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e 69 43 pE->iC
2049d 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69 43 olumn==pExpr->iC
2049e 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 olumn ){.
2049f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f pCo
204a0 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e l->iSorterColumn
204a1 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 = j;.
204a2 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
204a3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
204a4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
204a5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
204a6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
204a7 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 if( pCol->i
204a8 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 20 29 SorterColumn<0 )
204a9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
204aa 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 pCol->iSorterC
204ab 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e 66 6f olumn = pAggInfo
204ac 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e ->nSortingColumn
204ad 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ++;.
204ae 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
204af 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a }. /*
204b0 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20 61 6e There is now an
204b1 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78 70 72 entry for pExpr
204b2 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 in pAggInfo->aC
204b3 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 20 20 ol[] (either.
204b4 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63 61 ** beca
204b5 75 73 65 20 69 74 20 77 61 73 20 74 68 65 72 65 use it was there
204b6 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63 61 75 before or becau
204b7 73 65 20 77 65 20 6a 75 73 74 20 63 72 65 61 74 se we just creat
204b8 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20 20 20 ed it)..
204b9 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 20 74 ** Convert t
204ba 68 65 20 70 45 78 70 72 20 74 6f 20 62 65 20 61 he pExpr to be a
204bb 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 72 TK_AGG_COLUMN r
204bc 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68 61 74 eferring to that
204bd 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
204be 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d pAggInfo->aCol[]
204bf 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 20 20 entry..
204c0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
204c1 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e pExpr->pAggIn
204c2 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 fo = pAggInfo;.
204c3 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
204c4 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 43 4f ->op = TK_AGG_CO
204c5 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 LUMN;.
204c6 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 pExpr->iAgg =
204c7 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 k;. b
204c8 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
204c9 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78 70 72 } /* endif pExpr
204ca 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d ->iTable==pItem-
204cb 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 >iCursor */.
204cc 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f } /* end loo
204cd 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 20 p over pSrcList
204ce 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 */. }.
204cf 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
204d0 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 e;. }. cas
204d1 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f e TK_AGG_FUNCTIO
204d2 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 N: {. /* Th
204d3 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 e pNC->nDepth==0
204d4 20 74 65 73 74 20 63 61 75 73 65 73 20 61 67 67 test causes agg
204d5 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
204d6 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 0a 20 in subqueries.
204d7 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 67 ** to be ig
204d8 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 nored */. i
204d9 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d f( pNC->nDepth==
204da 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
204db 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
204dc 70 45 78 70 72 20 69 73 20 61 20 64 75 70 6c 69 pExpr is a dupli
204dd 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 cate of another
204de 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 20 20 aggregate .
204df 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 ** function t
204e0 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 hat is already i
204e1 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f 20 73 n the pAggInfo s
204e2 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 20 20 tructure.
204e3 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 72 75 */. stru
204e4 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 ct AggInfo_func
204e5 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 6e 66 *pItem = pAggInf
204e6 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 20 20 o->aFunc;.
204e7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 for(i=0; i<pAg
204e8 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b gInfo->nFunc; i+
204e9 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
204ea 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
204eb 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 49 e3ExprCompare(pI
204ec 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 78 70 tem->pExpr, pExp
204ed 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 r) ){.
204ee 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
204ef 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
204f0 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 70 41 if( i>=pA
204f1 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b ggInfo->nFunc ){
204f2 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 45 . /* pE
204f3 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e xpr is original.
204f4 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e 74 Make a new ent
204f5 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e ry in pAggInfo->
204f6 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20 20 20 aFunc[].
204f7 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 */. u
204f8 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 8 enc = ENC(pPar
204f9 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 20 se->db);.
204fa 20 20 20 69 20 3d 20 61 64 64 41 67 67 49 6e 66 i = addAggInf
204fb 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e 64 62 oFunc(pParse->db
204fc 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 , pAggInfo);.
204fd 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 30 20 if( i>=0
204fe 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 ){. a
204ff 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
20500 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
20501 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a P_xIsSelect) );.
20502 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 pIte
20503 6d 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 m = &pAggInfo->a
20504 46 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 Func[i];.
20505 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 pItem->pExp
20506 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 r = pExpr;.
20507 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d pItem->iM
20508 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e em = ++pParse->n
20509 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 Mem;.
2050a 20 70 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 pItem->pFunc =
2050b 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 sqlite3FindFunct
2050c 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a ion(pParse->db,.
2050d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2050e 20 20 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d (char*)pExpr-
2050f 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d >token.z, pExpr-
20510 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20 20 20 20 20 20 >token.n,.
20511 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 pEx
20512 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3f 20 70 45 pr->x.pList ? pE
20513 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 xpr->x.pList->nE
20514 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c 20 30 29 xpr : 0, enc, 0)
20515 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
20516 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 ( pExpr->flags &
20517 20 45 50 5f 44 69 73 74 69 6e 63 74 20 29 7b 0a EP_Distinct ){.
20518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 pI
20519 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d tem->iDistinct =
2051a 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b pParse->nTab++;
2051b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
2051c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
2051d 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e pItem->iDistin
2051e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 ct = -1;.
2051f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
20520 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
20521 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 70 45 78 /* Make pEx
20522 70 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 pr point to the
20523 61 70 70 72 6f 70 72 69 61 74 65 20 70 41 67 67 appropriate pAgg
20524 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 65 6e Info->aFunc[] en
20525 74 72 79 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 try. */.
20526 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41 pExpr->iA
20527 67 67 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 gg = i;.
20528 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20 pExpr->pAggInfo
20529 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 = pAggInfo;.
2052a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 return WRC_P
2052b 72 75 6e 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rune;. }.
2052c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
2052d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
2052e 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 6c .static int anal
2052f 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e 53 yzeAggregatesInS
20530 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 elect(Walker *pW
20531 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 alker, Select *p
20532 53 65 6c 65 63 74 29 7b 0a 20 20 4e 61 6d 65 43 Select){. NameC
20533 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57 ontext *pNC = pW
20534 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 alker->u.pNC;.
20535 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d if( pNC->nDepth=
20536 3d 30 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 6e =0 ){. pNC->n
20537 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 73 71 6c Depth++;. sql
20538 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 ite3WalkSelect(p
20539 57 61 6c 6b 65 72 2c 20 70 53 65 6c 65 63 74 29 Walker, pSelect)
2053a 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74 ;. pNC->nDept
2053b 68 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 h--;. return
2053c 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 65 6c WRC_Prune;. }el
2053d 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 se{. return W
2053e 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d RC_Continue;. }
2053f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a .}../*.** Analyz
20540 65 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 e the given expr
20541 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20 66 ession looking f
20542 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 6e or aggregate fun
20543 63 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f ctions and.** fo
20544 72 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 74 r variables that
20545 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64 65 need to be adde
20546 64 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 2d d to the pParse-
20547 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 2e 0a 2a >aAgg[] array..*
20548 2a 20 4d 61 6b 65 20 61 64 64 69 74 69 6f 6e 61 * Make additiona
20549 6c 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 l entries to the
2054a 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20 pParse->aAgg[]
2054b 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73 61 array as necessa
2054c 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ry..**.** This r
2054d 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e outine should on
2054e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 ly be called aft
2054f 65 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f er the expressio
20550 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 6e n has been.** an
20551 61 6c 79 7a 65 64 20 62 79 20 73 71 6c 69 74 65 alyzed by sqlite
20552 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
20553 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 s()..*/.SQLITE_P
20554 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20555 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 te3ExprAnalyzeAg
20556 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f 6e gregates(NameCon
20557 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 20 text *pNC, Expr
20558 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 *pExpr){. Walke
20559 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 r w;. w.xExprCa
2055a 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 llback = analyze
2055b 41 67 67 72 65 67 61 74 65 3b 0a 20 20 77 2e 78 Aggregate;. w.x
2055c 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d SelectCallback =
2055d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 analyzeAggregat
2055e 65 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20 77 2e esInSelect;. w.
2055f 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 u.pNC = pNC;. s
20560 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 qlite3WalkExpr(&
20561 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a w, pExpr);.}../*
20562 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 .** Call sqlite3
20563 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 ExprAnalyzeAggre
20564 67 61 74 65 73 28 29 20 66 6f 72 20 65 76 65 72 gates() for ever
20565 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 y expression in
20566 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e an.** expression
20567 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 20 74 list. Return t
20568 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 he number of err
20569 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ors..**.** If an
2056a 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e 64 2c error is found,
2056b 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 69 73 the analysis is
2056c 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a 53 cut short..*/.S
2056d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
2056e 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e id sqlite3ExprAn
2056f 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d alyzeAggList(Nam
20570 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 eContext *pNC, E
20571 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b xprList *pList){
20572 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 . struct ExprLi
20573 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
20574 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 int i;. if( p
20575 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 List ){. for(
20576 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 pItem=pList->a,
20577 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 i=0; i<pList->nE
20578 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
20579 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
2057a 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 3ExprAnalyzeAggr
2057b 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49 74 65 egates(pNC, pIte
2057c 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d m->pExpr);. }
2057d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c . }.}../*.** Al
2057e 6c 6f 63 61 74 65 20 61 20 73 69 6e 67 6c 65 20 locate a single
2057f 6e 65 77 20 72 65 67 69 73 74 65 72 20 66 6f 72 new register for
20580 20 75 73 65 20 74 6f 20 68 6f 6c 64 20 73 6f 6d use to hold som
20581 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72 e intermediate r
20582 65 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 esult..*/.SQLITE
20583 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
20584 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 ite3GetTempReg(P
20585 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 arse *pParse){.
20586 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 if( pParse->nTe
20587 6d 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 mpReg==0 ){.
20588 72 65 74 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d return ++pParse-
20589 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 >nMem;. }. ret
2058a 75 72 6e 20 70 50 61 72 73 65 2d 3e 61 54 65 6d urn pParse->aTem
2058b 70 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e pReg[--pParse->n
2058c 54 65 6d 70 52 65 67 5d 3b 0a 7d 0a 0a 2f 2a 0a TempReg];.}../*.
2058d 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 20 ** Deallocate a
2058e 72 65 67 69 73 74 65 72 2c 20 6d 61 6b 69 6e 67 register, making
2058f 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 available for r
20590 65 75 73 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 euse for some ot
20591 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 2e 0a her.** purpose..
20592 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 **.** If a regis
20593 74 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ter is currently
20594 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 being used by t
20595 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2c he column cache,
20596 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 6c then.** the dal
20597 6c 6f 63 61 74 69 6f 6e 20 69 73 20 64 65 66 65 location is defe
20598 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65 20 63 rred until the c
20599 6f 6c 75 6d 6e 20 63 61 63 68 65 20 6c 69 6e 65 olumn cache line
2059a 20 74 68 61 74 20 75 73 65 73 0a 2a 2a 20 74 68 that uses.** th
2059b 65 20 72 65 67 69 73 74 65 72 20 62 65 63 6f 6d e register becom
2059c 65 73 20 73 74 61 6c 65 2e 0a 2a 2f 0a 53 51 4c es stale..*/.SQL
2059d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
2059e 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
2059f 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 empReg(Parse *pP
205a0 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b arse, int iReg){
205a1 0a 20 20 69 66 28 20 69 52 65 67 20 26 26 20 70 . if( iReg && p
205a2 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c Parse->nTempReg<
205a3 41 72 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 ArraySize(pParse
205a4 2d 3e 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 ->aTempReg) ){.
205a5 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 74 int i;. st
205a6 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a ruct yColCache *
205a7 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 p;. for(i=0,
205a8 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
205a9 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
205aa 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
205ab 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ++){. if( p
205ac 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a ->iReg==iReg ){.
205ad 20 20 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 p->tempR
205ae 65 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 eg = 1;.
205af 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a return;. }.
205b0 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 }. pParse
205b1 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 73 ->aTempReg[pPars
205b2 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d e->nTempReg++] =
205b3 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a iReg;. }.}../*
205b4 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 .** Allocate or
205b5 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f deallocate a blo
205b6 63 6b 20 6f 66 20 6e 52 65 67 20 63 6f 6e 73 65 ck of nReg conse
205b7 63 75 74 69 76 65 20 72 65 67 69 73 74 65 72 73 cutive registers
205b8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
205b9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
205ba 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 tTempRange(Parse
205bb 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 52 *pParse, int nR
205bc 65 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b eg){. int i, n;
205bd 0a 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e 69 . i = pParse->i
205be 52 61 6e 67 65 52 65 67 3b 0a 20 20 6e 20 3d 20 RangeReg;. n =
205bf 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 pParse->nRangeRe
205c0 67 3b 0a 20 20 69 66 28 20 6e 52 65 67 3c 3d 6e g;. if( nReg<=n
205c1 20 26 26 20 21 75 73 65 64 41 73 43 6f 6c 75 6d && !usedAsColum
205c2 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69 nCache(pParse, i
205c3 2c 20 69 2b 6e 2d 31 29 20 29 7b 0a 20 20 20 20 , i+n-1) ){.
205c4 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 pParse->iRangeRe
205c5 67 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 g += nReg;. p
205c6 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 Parse->nRangeReg
205c7 20 2d 3d 20 6e 52 65 67 3b 0a 20 20 7d 65 6c 73 -= nReg;. }els
205c8 65 7b 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73 e{. i = pPars
205c9 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 e->nMem+1;. p
205ca 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e Parse->nMem += n
205cb 52 65 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Reg;. }. retur
205cc 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 n i;.}.SQLITE_PR
205cd 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
205ce 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e e3ReleaseTempRan
205cf 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ge(Parse *pParse
205d0 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 , int iReg, int
205d1 6e 52 65 67 29 7b 0a 20 20 69 66 28 20 6e 52 65 nReg){. if( nRe
205d2 67 3e 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 g>pParse->nRange
205d3 52 65 67 20 29 7b 0a 20 20 20 20 70 50 61 72 73 Reg ){. pPars
205d4 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 3d 20 6e e->nRangeReg = n
205d5 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73 65 2d Reg;. pParse-
205d6 3e 69 52 61 6e 67 65 52 65 67 20 3d 20 69 52 65 >iRangeReg = iRe
205d7 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a g;. }.}../*****
205d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
205d9 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a expr.c ********
205da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205dc 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
205dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
205de 66 69 6c 65 20 61 6c 74 65 72 2e 63 20 2a 2a 2a file alter.c ***
205df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205e1 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
205e2 32 30 30 35 20 46 65 62 72 75 61 72 79 20 31 35 2005 February 15
205e3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
205e4 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
205e5 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
205e6 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
205e7 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
205e8 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
205e9 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
205ea 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
205eb 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
205ec 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
205ed 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
205ee 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
205ef 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
205f0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
205f1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
205f2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
205f3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
205f4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
205f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
205f9 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
205fa 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 tains C code rou
205fb 74 69 6e 65 73 20 74 68 61 74 20 75 73 65 64 20 tines that used
205fc 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44 42 45 to generate VDBE
205fd 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 69 6d code.** that im
205fe 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 41 4c 54 plements the ALT
205ff 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 ER TABLE command
20600 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 6c 74 ..**.** $Id: alt
20601 65 72 2e 63 2c 76 20 31 2e 35 37 20 32 30 30 39 er.c,v 1.57 2009
20602 2f 30 34 2f 31 36 20 31 36 3a 33 30 3a 31 38 20 /04/16 16:30:18
20603 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
20604 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 .** The code in
20605 74 68 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 this file only e
20606 78 69 73 74 73 20 69 66 20 77 65 20 61 72 65 20 xists if we are
20607 6e 6f 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 not omitting the
20608 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 .** ALTER TABLE
20609 6c 6f 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62 logic from the b
2060a 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 uild..*/.#ifndef
2060b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 SQLITE_OMIT_ALT
2060c 45 52 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 ERTABLE.../*.**
2060d 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
2060e 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e used by SQL gen
2060f 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d erated to implem
20610 65 6e 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 ent the .** ALTE
20611 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e R TABLE command.
20612 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
20613 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20 ent is the text
20614 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42 4c of a CREATE TABL
20615 45 20 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 E or.** CREATE I
20616 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 NDEX command. Th
20617 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 e second is a ta
20618 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 ble name. The ta
20619 62 6c 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 ble name in .**
2061a 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
2061b 20 6f 72 20 43 52 45 41 54 45 20 49 4e 44 45 58 or CREATE INDEX
2061c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 statement is re
2061d 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20 placed with the
2061e 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e third.** argumen
2061f 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 t and the result
20620 20 72 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 returned. Examp
20621 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 les:.**.** sqlit
20622 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 e_rename_table('
20623 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63 CREATE TABLE abc
20624 28 61 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66 (a, b, c)', 'def
20625 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 ').** -> 'CR
20626 45 41 54 45 20 54 41 42 4c 45 20 64 65 66 28 61 EATE TABLE def(a
20627 2c 20 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 , b, c)'.**.** s
20628 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 qlite_rename_tab
20629 6c 65 28 27 43 52 45 41 54 45 20 49 4e 44 45 58 le('CREATE INDEX
2062a 20 69 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 i ON abc(a)', '
2062b 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 def').** ->
2062c 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 'CREATE INDEX i
2062d 4f 4e 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 ON def(a, b, c)'
2062e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
2062f 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 renameTableFunc(
20630 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
20631 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
20632 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
20633 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
20634 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 gv.){. unsigned
20635 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 char const *zSq
20636 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
20637 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
20638 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
20639 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 const *zTableNa
2063a 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c me = sqlite3_val
2063b 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 ue_text(argv[1])
2063c 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a ;.. int token;.
2063d 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 Token tname;.
2063e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 unsigned char c
2063f 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 onst *zCsr = zSq
20640 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 l;. int len = 0
20641 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a ;. char *zRet;.
20642 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20643 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
20644 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
20645 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 xt);.. UNUSED_P
20646 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
20647 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 );.. /* The pri
20648 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c nciple used to l
20649 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 ocate the table
2064a 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 name in the CREA
2064b 54 45 20 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 TE TABLE . ** s
2064c 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74 tatement is that
2064d 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
2064e 69 73 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e is the first non
2064f 2d 73 70 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 -space token tha
20650 74 0a 20 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 t. ** is immedi
20651 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 ately followed b
20652 79 20 61 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f y a TK_LP or TK_
20653 55 53 49 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a USING token.. *
20654 2f 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a /. if( zSql ){.
20655 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 do {. i
20656 66 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 f( !*zCsr ){.
20657 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 /* Ran out
20658 6f 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 of input before
20659 66 69 6e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 finding an openi
2065a 6e 67 20 62 72 61 63 6b 65 74 2e 20 52 65 74 75 ng bracket. Retu
2065b 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 rn NULL. */.
2065c 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
2065d 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 }.. /* St
2065e 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 ore the token th
2065f 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 at zCsr points t
20660 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 o in tname. */.
20661 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a tname.z = z
20662 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 Csr;. tname
20663 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 .n = len;..
20664 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72 /* Advance zCsr
20665 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b to the next tok
20666 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74 en. Store that t
20667 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f oken type in 'to
20668 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 ken',. ** a
20669 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69 6e nd its length in
2066a 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75 73 'len' (to be us
2066b 65 64 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f ed next iteratio
2066c 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e n of this loop).
2066d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
2066e 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 do {. zCs
2066f 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 r += len;.
20670 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 len = sqlite3G
20671 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 etToken(zCsr, &t
20672 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 oken);. } w
20673 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f hile( token==TK_
20674 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20 61 SPACE );. a
20675 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a ssert( len>0 );.
20676 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b } while( tok
20677 65 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b en!=TK_LP && tok
20678 65 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a en!=TK_USING );.
20679 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 . zRet = sqli
2067a 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 te3MPrintf(db, "
2067b 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 74 %.*s\"%w\"%s", t
2067c 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a name.z - zSql, z
2067d 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 Sql, . zTa
2067e 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a bleName, tname.z
2067f 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 +tname.n);. s
20680 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
20681 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 xt(context, zRet
20682 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e , -1, SQLITE_DYN
20683 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 AMIC);. }.}..#i
20684 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
20685 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 T_TRIGGER./* Thi
20686 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
20687 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61 ed by SQL genera
20688 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 ted to implement
20689 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 the.** ALTER TA
2068a 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 BLE command. The
2068b 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
2068c 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 is the text of a
2068d 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 CREATE TRIGGER
2068e 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 .** statement. T
2068f 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 he second is a t
20690 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 able name. The t
20691 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 able name in the
20692 20 43 52 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 CREATE .** TRIG
20693 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20 69 73 GER statement is
20694 20 72 65 70 6c 61 63 65 64 20 77 69 74 68 20 74 replaced with t
20695 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
20696 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 t and the result
20697 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 .** returned. T
20698 68 69 73 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 his is analagous
20699 20 74 6f 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 to renameTableF
2069a 75 6e 63 28 29 20 61 62 6f 76 65 2c 20 65 78 63 unc() above, exc
2069b 65 70 74 20 66 6f 72 20 43 52 45 41 54 45 0a 2a ept for CREATE.*
2069c 2a 20 54 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 * TRIGGER, not C
2069d 52 45 41 54 45 20 49 4e 44 45 58 20 61 6e 64 20 REATE INDEX and
2069e 43 52 45 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f CREATE TABLE..*/
2069f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e .static void ren
206a0 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 28 0a ameTriggerFunc(.
206a1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
206a2 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
206a3 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c t NotUsed,. sql
206a4 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
206a5 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 v.){. unsigned
206a6 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c char const *zSql
206a7 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
206a8 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a _text(argv[0]);.
206a9 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
206aa 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d const *zTableNam
206ab 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 e = sqlite3_valu
206ac 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b e_text(argv[1]);
206ad 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 .. int token;.
206ae 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 Token tname;.
206af 69 6e 74 20 64 69 73 74 20 3d 20 33 3b 0a 20 20 int dist = 3;.
206b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f unsigned char co
206b1 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c nst *zCsr = zSql
206b2 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b ;. int len = 0;
206b3 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20 . char *zRet;.
206b4 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 sqlite3 *db = s
206b5 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
206b6 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
206b7 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 );.. UNUSED_PAR
206b8 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
206b9 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 .. /* The princ
206ba 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 iple used to loc
206bb 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 ate the table na
206bc 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 me in the CREATE
206bd 20 54 52 49 47 47 45 52 20 0a 20 20 2a 2a 20 73 TRIGGER . ** s
206be 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74 tatement is that
206bf 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
206c0 69 73 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b is the first tok
206c1 65 6e 20 74 68 61 74 20 69 73 20 69 6d 6d 65 64 en that is immed
206c2 69 61 74 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65 iatedly. ** pre
206c3 63 65 64 65 64 20 62 79 20 65 69 74 68 65 72 20 ceded by either
206c4 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 TK_ON or TK_DOT
206c5 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 and immediatedly
206c6 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65 followed by one
206c7 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e . ** of TK_WHEN
206c8 2c 20 54 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b , TK_BEGIN or TK
206c9 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 _FOR.. */. if(
206ca 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 zSql ){. do
206cb 7b 0a 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a {.. if( !*z
206cc 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f Csr ){. /
206cd 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 * Ran out of inp
206ce 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e ut before findin
206cf 67 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 g the table name
206d0 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a . Return NULL. *
206d1 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
206d2 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
206d3 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f /* Store the to
206d4 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f ken that zCsr po
206d5 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 ints to in tname
206d6 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 . */. tname
206d7 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 .z = zCsr;.
206d8 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a tname.n = len;.
206d9 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 . /* Advanc
206da 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 e zCsr to the ne
206db 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 xt token. Store
206dc 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 that token type
206dd 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 in 'token',.
206de 20 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e ** and its len
206df 67 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f gth in 'len' (to
206e0 20 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 be used next it
206e1 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 eration of this
206e2 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a loop).. */.
206e3 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 do {.
206e4 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a zCsr += len;.
206e5 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 len = sq
206e6 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 lite3GetToken(zC
206e7 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 sr, &token);.
206e8 20 20 20 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e }while( token
206e9 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 ==TK_SPACE );.
206ea 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e assert( len>
206eb 30 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 56 0 );.. /* V
206ec 61 72 69 61 62 6c 65 20 27 64 69 73 74 27 20 73 ariable 'dist' s
206ed 74 6f 72 65 73 20 74 68 65 20 6e 75 6d 62 65 72 tores the number
206ee 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65 61 64 20 of tokens read
206ef 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 0a 20 since the most.
206f0 20 20 20 20 20 2a 2a 20 72 65 63 65 6e 74 20 54 ** recent T
206f1 4b 5f 44 4f 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20 K_DOT or TK_ON.
206f2 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 This means that
206f3 77 68 65 6e 20 61 20 57 48 45 4e 2c 20 46 4f 52 when a WHEN, FOR
206f4 20 6f 72 20 42 45 47 49 4e 20 0a 20 20 20 20 20 or BEGIN .
206f5 20 2a 2a 20 74 6f 6b 65 6e 20 69 73 20 72 65 61 ** token is rea
206f6 64 20 61 6e 64 20 27 64 69 73 74 27 20 65 71 75 d and 'dist' equ
206f7 61 6c 73 20 32 2c 20 74 68 65 20 63 6f 6e 64 69 als 2, the condi
206f8 74 69 6f 6e 20 73 74 61 74 65 64 20 61 62 6f 76 tion stated abov
206f9 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 e. ** to be
206fa 20 6d 65 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 met.. **.
206fb 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 ** Note tha
206fc 74 20 4f 4e 20 63 61 6e 6e 6f 74 20 62 65 20 61 t ON cannot be a
206fd 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
206fe 20 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c or column name,
206ff 20 73 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 so. ** the
20700 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
20701 20 77 6f 72 72 79 20 61 62 6f 75 74 20 73 79 6e worry about syn
20702 74 61 78 20 6c 69 6b 65 20 0a 20 20 20 20 20 20 tax like .
20703 2a 2a 20 22 43 52 45 41 54 45 20 54 52 49 47 47 ** "CREATE TRIGG
20704 45 52 20 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 ER ... ON ON.ON
20705 42 45 47 49 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a BEGIN ..." etc..
20706 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 */. d
20707 69 73 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 ist++;. if(
20708 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c token==TK_DOT |
20709 7c 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 | token==TK_ON )
2070a 7b 0a 20 20 20 20 20 20 20 20 64 69 73 74 20 3d {. dist =
2070b 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
2070c 7d 20 77 68 69 6c 65 28 20 64 69 73 74 21 3d 32 } while( dist!=2
2070d 20 7c 7c 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 || (token!=TK_W
2070e 48 45 4e 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b HEN && token!=TK
2070f 5f 46 4f 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54 _FOR && token!=T
20710 4b 5f 42 45 47 49 4e 29 20 29 3b 0a 0a 20 20 20 K_BEGIN) );..
20711 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6e 61 /* Variable tna
20712 6d 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 me now contains
20713 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 the token that i
20714 73 20 74 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d s the old table-
20715 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 name. ** in t
20716 68 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45 he CREATE TRIGGE
20717 52 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 R statement..
20718 20 2a 2f 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 */. zRet = s
20719 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
2071a 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 , "%.*s\"%w\"%s"
2071b 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c , tname.z - zSql
2071c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 , zSql, .
2071d 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d zTableName, tnam
2071e 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 e.z+tname.n);.
2071f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
20720 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a _text(context, z
20721 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f Ret, -1, SQLITE_
20722 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a DYNAMIC);. }.}.
20723 23 65 6e 64 69 66 20 20 20 2f 2a 20 21 53 51 4c #endif /* !SQL
20724 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
20725 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 */../*.** Regis
20726 74 65 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e ter built-in fun
20727 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 68 ctions used to h
20728 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c elp implement AL
20729 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c TER TABLE.*/.SQL
2072a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
2072b 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e sqlite3AlterFun
2072c 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a ctions(sqlite3 *
2072d 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 db){. sqlite3Cr
2072e 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 eateFunc(db, "sq
2072f 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c lite_rename_tabl
20730 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 e", 2, SQLITE_UT
20731 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 F8, 0,.
20732 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20733 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c renameTableFunc,
20734 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 0, 0);.#ifndef
20735 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
20736 47 45 52 0a 20 20 73 71 6c 69 74 65 33 43 72 65 GER. sqlite3Cre
20737 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c ateFunc(db, "sql
20738 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 ite_rename_trigg
20739 65 72 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 er", 2, SQLITE_U
2073a 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 TF8, 0,.
2073b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2073c 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75 renameTriggerFu
2073d 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 nc, 0, 0);.#endi
2073e 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 f.}../*.** Gener
2073f 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20 ate the text of
20740 61 20 57 48 45 52 45 20 65 78 70 72 65 73 73 69 a WHERE expressi
20741 6f 6e 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 on which can be
20742 75 73 65 64 20 74 6f 20 73 65 6c 65 63 74 20 61 used to select a
20743 6c 6c 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 ll.** temporary
20744 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c triggers on tabl
20745 65 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 e pTab from the
20746 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 sqlite_temp_mast
20747 65 72 20 74 61 62 6c 65 2e 20 49 66 0a 2a 2a 20 er table. If.**
20748 74 61 62 6c 65 20 70 54 61 62 20 68 61 73 20 6e table pTab has n
20749 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 o temporary trig
2074a 67 65 72 73 2c 20 6f 72 20 69 73 20 69 74 73 65 gers, or is itse
2074b 6c 66 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 lf stored in the
2074c 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 64 .** temporary d
2074d 61 74 61 62 61 73 65 2c 20 4e 55 4c 4c 20 69 73 atabase, NULL is
2074e 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
2074f 61 74 69 63 20 63 68 61 72 20 2a 77 68 65 72 65 atic char *where
20750 54 65 6d 70 54 72 69 67 67 65 72 73 28 50 61 72 TempTriggers(Par
20751 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c se *pParse, Tabl
20752 65 20 2a 70 54 61 62 29 7b 0a 20 20 54 72 69 67 e *pTab){. Trig
20753 67 65 72 20 2a 70 54 72 69 67 3b 0a 20 20 63 68 ger *pTrig;. ch
20754 61 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 0a ar *zWhere = 0;.
20755 20 20 63 68 61 72 20 2a 74 6d 70 20 3d 20 30 3b char *tmp = 0;
20756 0a 20 20 63 6f 6e 73 74 20 53 63 68 65 6d 61 20 . const Schema
20757 2a 70 54 65 6d 70 53 63 68 65 6d 61 20 3d 20 70 *pTempSchema = p
20758 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 Parse->db->aDb[1
20759 5d 2e 70 53 63 68 65 6d 61 3b 20 2f 2a 20 54 65 ].pSchema; /* Te
2075a 6d 70 20 64 62 20 73 63 68 65 6d 61 20 2a 2f 0a mp db schema */.
2075b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62 . /* If the tab
2075c 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 61 74 65 le is not locate
2075d 64 20 69 6e 20 74 68 65 20 74 65 6d 70 2d 64 62 d in the temp-db
2075e 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 (in which case
2075f 4e 55 4c 4c 20 69 73 20 0a 20 20 2a 2a 20 72 65 NULL is . ** re
20760 74 75 72 6e 65 64 2c 20 6c 6f 6f 70 20 74 68 72 turned, loop thr
20761 6f 75 67 68 20 74 68 65 20 74 61 62 6c 65 73 20 ough the tables
20762 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 list of triggers
20763 2e 20 46 6f 72 20 65 61 63 68 20 74 72 69 67 67 . For each trigg
20764 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 69 73 20 er. ** that is
20765 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 not part of the
20766 74 65 6d 70 2d 64 62 20 73 63 68 65 6d 61 2c 20 temp-db schema,
20767 61 64 64 20 61 20 63 6c 61 75 73 65 20 74 6f 20 add a clause to
20768 74 68 65 20 57 48 45 52 45 20 0a 20 20 2a 2a 20 the WHERE . **
20769 65 78 70 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 expression being
2076a 20 62 75 69 6c 74 20 75 70 20 69 6e 20 7a 57 68 built up in zWh
2076b 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ere.. */. if(
2076c 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 21 3d 70 pTab->pSchema!=p
2076d 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 TempSchema ){.
2076e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
2076f 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 pParse->db;.
20770 66 6f 72 28 70 54 72 69 67 3d 73 71 6c 69 74 65 for(pTrig=sqlite
20771 33 54 72 69 67 67 65 72 4c 69 73 74 28 70 50 61 3TriggerList(pPa
20772 72 73 65 2c 20 70 54 61 62 29 3b 20 70 54 72 69 rse, pTab); pTri
20773 67 3b 20 70 54 72 69 67 3d 70 54 72 69 67 2d 3e g; pTrig=pTrig->
20774 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 pNext){. if
20775 28 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 ( pTrig->pSchema
20776 3d 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29 7b ==pTempSchema ){
20777 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 57 . if( !zW
20778 68 65 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 here ){.
20779 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74 zWhere = sqlit
2077a 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e e3MPrintf(db, "n
2077b 61 6d 65 3d 25 51 22 2c 20 70 54 72 69 67 2d 3e ame=%Q", pTrig->
2077c 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7d name);. }
2077d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
2077e 74 6d 70 20 3d 20 7a 57 68 65 72 65 3b 0a 20 20 tmp = zWhere;.
2077f 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d zWhere =
20780 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
20781 64 62 2c 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d db, "%s OR name=
20782 25 51 22 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 %Q", zWhere, pTr
20783 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 ig->name);.
20784 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
20785 65 65 28 64 62 2c 20 74 6d 70 29 3b 0a 20 20 20 ee(db, tmp);.
20786 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
20787 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
20788 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a n zWhere;.}../*.
20789 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
2078a 20 74 6f 20 64 72 6f 70 20 61 6e 64 20 72 65 6c to drop and rel
2078b 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c oad the internal
2078c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
2078d 6f 66 20 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62 of table.** pTab
2078e 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
2078f 73 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 72 se, including tr
20790 69 67 67 65 72 73 20 61 6e 64 20 74 65 6d 70 6f iggers and tempo
20791 72 61 72 79 20 74 72 69 67 67 65 72 73 2e 0a 2a rary triggers..*
20792 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65 * Argument zName
20793 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
20794 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 the table in the
20795 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
20796 20 61 74 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 at.** the time
20797 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f the generated co
20798 64 65 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 de is executed.
20799 54 68 69 73 20 63 61 6e 20 62 65 20 64 69 66 66 This can be diff
2079a 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54 erent from.** pT
2079b 61 62 2d 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69 ab->zName if thi
2079c 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 s function is be
2079d 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f ing called to co
2079e 64 65 20 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a de part of an .*
2079f 2a 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 52 * "ALTER TABLE R
207a0 45 4e 41 4d 45 20 54 4f 22 20 73 74 61 74 65 6d ENAME TO" statem
207a1 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ent..*/.static v
207a2 6f 69 64 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 oid reloadTableS
207a3 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 chema(Parse *pPa
207a4 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 rse, Table *pTab
207a5 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
207a6 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b ame){. Vdbe *v;
207a7 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 3b . char *zWhere;
207a8 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
207a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
207aa 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 Index of databa
207ab 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 se containing pT
207ac 61 62 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ab */.#ifndef SQ
207ad 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
207ae 52 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 R. Trigger *pTr
207af 69 67 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 ig;.#endif.. v
207b0 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
207b1 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 (pParse);. if(
207b2 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 NEVER(v==0) ) re
207b3 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
207b4 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
207b5 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 sAllMutexes(pPar
207b6 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 se->db) );. iDb
207b7 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 = sqlite3Schema
207b8 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e ToIndex(pParse->
207b9 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d db, pTab->pSchem
207ba 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 a);. assert( iD
207bb 62 3e 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 b>=0 );..#ifndef
207bc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 SQLITE_OMIT_TRI
207bd 47 47 45 52 0a 20 20 2f 2a 20 44 72 6f 70 20 61 GGER. /* Drop a
207be 6e 79 20 74 61 62 6c 65 20 74 72 69 67 67 65 72 ny table trigger
207bf 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 s from the inter
207c0 6e 61 6c 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 nal schema. */.
207c1 20 66 6f 72 28 70 54 72 69 67 3d 73 71 6c 69 74 for(pTrig=sqlit
207c2 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70 50 e3TriggerList(pP
207c3 61 72 73 65 2c 20 70 54 61 62 29 3b 20 70 54 72 arse, pTab); pTr
207c4 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69 67 2d ig; pTrig=pTrig-
207c5 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 >pNext){. int
207c6 20 69 54 72 69 67 44 62 20 3d 20 73 71 6c 69 74 iTrigDb = sqlit
207c7 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
207c8 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 pParse->db, pTri
207c9 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 g->pSchema);.
207ca 20 61 73 73 65 72 74 28 20 69 54 72 69 67 44 62 assert( iTrigDb
207cb 3d 3d 69 44 62 20 7c 7c 20 69 54 72 69 67 44 62 ==iDb || iTrigDb
207cc 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ==1 );. sqlit
207cd 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
207ce 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 2c 20 OP_DropTrigger,
207cf 69 54 72 69 67 44 62 2c 20 30 2c 20 30 2c 20 70 iTrigDb, 0, 0, p
207d0 54 72 69 67 2d 3e 6e 61 6d 65 2c 20 30 29 3b 0a Trig->name, 0);.
207d1 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
207d2 20 44 72 6f 70 20 74 68 65 20 74 61 62 6c 65 20 Drop the table
207d3 61 6e 64 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 and index from t
207d4 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 he internal sche
207d5 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 ma */. sqlite3V
207d6 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
207d7 44 72 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 DropTable, iDb,
207d8 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 0, 0, pTab->zNam
207d9 65 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c e, 0);.. /* Rel
207da 6f 61 64 20 74 68 65 20 74 61 62 6c 65 2c 20 69 oad the table, i
207db 6e 64 65 78 20 61 6e 64 20 70 65 72 6d 61 6e 65 ndex and permane
207dc 6e 74 20 74 72 69 67 67 65 72 20 73 63 68 65 6d nt trigger schem
207dd 61 73 2e 20 2a 2f 0a 20 20 7a 57 68 65 72 65 20 as. */. zWhere
207de 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
207df 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 74 62 (pParse->db, "tb
207e0 6c 5f 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d l_name=%Q", zNam
207e1 65 29 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72 e);. if( !zWher
207e2 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 e ) return;. sq
207e3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
207e4 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d v, OP_ParseSchem
207e5 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 7a 57 a, iDb, 0, 0, zW
207e6 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 here, P4_DYNAMIC
207e7 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
207e8 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
207e9 20 20 2f 2a 20 4e 6f 77 2c 20 69 66 20 74 68 65 /* Now, if the
207ea 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 73 74 table is not st
207eb 6f 72 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 ored in the temp
207ec 20 64 61 74 61 62 61 73 65 2c 20 72 65 6c 6f 61 database, reloa
207ed 64 20 61 6e 79 20 74 65 6d 70 20 0a 20 20 2a 2a d any temp . **
207ee 20 74 72 69 67 67 65 72 73 2e 20 44 6f 6e 27 74 triggers. Don't
207ef 20 75 73 65 20 49 4e 28 2e 2e 2e 29 20 69 6e 20 use IN(...) in
207f0 63 61 73 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 case SQLITE_OMIT
207f1 5f 53 55 42 51 55 45 52 59 20 69 73 20 64 65 66 _SUBQUERY is def
207f2 69 6e 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 ined. . */. if
207f3 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65 54 ( (zWhere=whereT
207f4 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 61 72 empTriggers(pPar
207f5 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29 7b se, pTab))!=0 ){
207f6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
207f7 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 AddOp4(v, OP_Par
207f8 73 65 53 63 68 65 6d 61 2c 20 31 2c 20 30 2c 20 seSchema, 1, 0,
207f9 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 0, zWhere, P4_DY
207fa 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 NAMIC);. }.#end
207fb 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 if.}../*.** Gene
207fc 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 rate code to imp
207fd 6c 65 6d 65 6e 74 20 74 68 65 20 22 41 4c 54 45 lement the "ALTE
207fe 52 20 54 41 42 4c 45 20 78 78 78 20 52 45 4e 41 R TABLE xxx RENA
207ff 4d 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a 20 63 ME TO yyy" .** c
20800 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51 4c 49 ommand. .*/.SQLI
20801 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
20802 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 sqlite3AlterRena
20803 6d 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 meTable(. Parse
20804 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
20805 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 /* Parser c
20806 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53 72 63 ontext. */. Src
20807 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 List *pSrc,
20808 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 /* The ta
20809 62 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e 20 2a ble to rename. *
2080a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 /. Token *pName
2080b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2080c 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e The new table n
2080d 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ame. */.){. int
2080e 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 iDb;
2080f 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
20810 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 se that contains
20811 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
20812 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 char *zDb;
20813 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
20814 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 44 e of database iD
20815 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 b */. Table *pT
20816 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ab;
20817 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 /* Table being
20818 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63 68 61 renamed */. cha
20819 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 r *zName = 0;
2081a 20 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 /* NULL-t
2081b 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f erminated versio
2081c 6e 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 0a 20 n of pName */ .
2081d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
2081e 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 Parse->db; /* Da
2081f 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
20820 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 4e n */. int nTabN
20821 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
20822 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 54 /* Number of UT
20823 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 20 69 F-8 characters i
20824 6e 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a 20 20 n zTabName */.
20825 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
20826 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f 72 69 Name; /* Ori
20827 67 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20 74 68 ginal name of th
20828 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 e table */. Vdb
20829 65 20 2a 76 3b 0a 23 69 66 6e 64 65 66 20 53 51 e *v;.#ifndef SQ
2082a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
2082b 52 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 R. char *zWhere
2082c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
2082d 20 57 68 65 72 65 20 63 6c 61 75 73 65 20 74 6f Where clause to
2082e 20 6c 6f 63 61 74 65 20 74 65 6d 70 20 74 72 69 locate temp tri
2082f 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69 66 0a ggers */.#endif.
20830 20 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 52 int isVirtualR
20831 65 6e 61 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 54 ename = 0; /* T
20832 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 rue if this is a
20833 20 76 2d 74 61 62 6c 65 20 77 69 74 68 20 61 6e v-table with an
20834 20 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a 20 20 xRename() */.
20835 0a 20 20 69 66 28 20 4e 45 56 45 52 28 64 62 2d . if( NEVER(db-
20836 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 >mallocFailed) )
20837 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d goto exit_renam
20838 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 e_table;. asser
20839 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 t( pSrc->nSrc==1
2083a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
2083b 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 lite3BtreeHoldsA
2083c 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 llMutexes(pParse
2083d 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54 61 62 ->db) );.. pTab
2083e 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
2083f 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c Table(pParse, 0,
20840 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d pSrc->a[0].zNam
20841 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 e, pSrc->a[0].zD
20842 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 atabase);. if(
20843 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 !pTab ) goto exi
20844 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a t_rename_table;.
20845 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
20846 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 chemaToIndex(pPa
20847 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 rse->db, pTab->p
20848 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d Schema);. zDb =
20849 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e db->aDb[iDb].zN
2084a 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 ame;.. /* Get a
2084b 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 NULL terminated
2084c 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
2084d 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 new table name.
2084e 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c */. zName = sql
2084f 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
20850 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 n(db, pName);.
20851 69 66 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 if( !zName ) got
20852 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
20853 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ble;.. /* Check
20854 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72 that a table or
20855 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e index named 'zN
20856 61 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c ame' does not al
20857 72 65 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a ready exist. **
20858 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 in database iDb
20859 2e 20 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73 . If so, this is
2085a 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a an error.. */.
2085b 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e if( sqlite3Fin
2085c 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 dTable(db, zName
2085d 2c 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 , zDb) || sqlite
2085e 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 3FindIndex(db, z
2085f 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 Name, zDb) ){.
20860 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
20861 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 g(pParse, .
20862 20 20 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 "there is alr
20863 65 61 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62 eady another tab
20864 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68 le or index with
20865 20 74 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c this name: %s",
20866 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 zName);. got
20867 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 o exit_rename_ta
20868 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d ble;. }.. /* M
20869 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6e ake sure it is n
2086a 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c ot a system tabl
2086b 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c e being altered,
2086c 20 6f 72 20 61 20 72 65 73 65 72 76 65 64 20 6e or a reserved n
2086d 61 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 ame. ** that th
2086e 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 e table is being
2086f 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a renamed to.. *
20870 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 /. if( sqlite3S
20871 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e trlen30(pTab->zN
20872 61 6d 65 29 3e 36 20 0a 20 20 20 26 26 20 30 3d ame)>6 . && 0=
20873 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 =sqlite3StrNICmp
20874 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 (pTab->zName, "s
20875 71 6c 69 74 65 5f 22 2c 20 37 29 0a 20 20 29 7b qlite_", 7). ){
20876 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
20877 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 rMsg(pParse, "ta
20878 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 ble %s may not b
20879 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 e altered", pTab
2087a 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f ->zName);. go
2087b 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 to exit_rename_t
2087c 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 able;. }. if(
2087d 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 SQLITE_OK!=sqlit
2087e 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d e3CheckObjectNam
2087f 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 e(pParse, zName)
20880 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 ){. goto exi
20881 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a t_rename_table;.
20882 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c }..#ifndef SQL
20883 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 ITE_OMIT_VIEW.
20884 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 if( pTab->pSelec
20885 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
20886 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
20887 20 22 76 69 65 77 20 25 73 20 6d 61 79 20 6e 6f "view %s may no
20888 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70 t be altered", p
20889 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 Tab->zName);.
2088a 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d goto exit_renam
2088b 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e e_table;. }.#en
2088c 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
2088d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 ITE_OMIT_AUTHORI
2088e 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f ZATION. /* Invo
2088f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 ke the authoriza
20890 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a tion callback. *
20891 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 /. if( sqlite3A
20892 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c uthCheck(pParse,
20893 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 SQLITE_ALTER_TA
20894 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e BLE, zDb, pTab->
20895 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 zName, 0) ){.
20896 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d goto exit_renam
20897 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e e_table;. }.#en
20898 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
20899 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
2089a 54 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 TABLE. if( sqli
2089b 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e te3ViewGetColumn
2089c 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 Names(pParse, pT
2089d 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ab) ){. goto
2089e 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
2089f 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 e;. }. if( IsV
208a0 69 72 74 75 61 6c 28 70 54 61 62 29 20 26 26 20 irtual(pTab) &&
208a1 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 pTab->pMod->pMod
208a2 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 7b 0a ule->xRename ){.
208a3 20 20 20 20 69 73 56 69 72 74 75 61 6c 52 65 6e isVirtualRen
208a4 61 6d 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e ame = 1;. }.#en
208a5 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 dif.. /* Begin
208a6 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e a transaction an
208a7 64 20 63 6f 64 65 20 74 68 65 20 56 65 72 69 66 d code the Verif
208a8 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64 61 74 61 yCookie for data
208a9 62 61 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 base iDb. . **
208aa 54 68 65 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 Then modify the
208ab 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 schema cookie (s
208ac 69 6e 63 65 20 74 68 65 20 41 4c 54 45 52 20 54 ince the ALTER T
208ad 41 42 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 68 ABLE modifies th
208ae 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 e. ** schema).
208af 4f 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 Open a statement
208b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 transaction if
208b1 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 the table is a v
208b2 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c irtual. ** tabl
208b3 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 e.. */. v = sq
208b4 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
208b5 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 rse);. if( v==0
208b6 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 ){. goto exi
208b7 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a t_rename_table;.
208b8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 65 67 }. sqlite3Beg
208b9 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e inWriteOperation
208ba 28 70 50 61 72 73 65 2c 20 69 73 56 69 72 74 75 (pParse, isVirtu
208bb 61 6c 52 65 6e 61 6d 65 2c 20 69 44 62 29 3b 0a alRename, iDb);.
208bc 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 sqlite3ChangeC
208bd 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 ookie(pParse, iD
208be 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 b);.. /* If thi
208bf 73 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 s is a virtual t
208c0 61 62 6c 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 able, invoke the
208c1 20 78 52 65 6e 61 6d 65 28 29 20 66 75 6e 63 74 xRename() funct
208c2 69 6f 6e 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 ion if. ** one
208c3 69 73 20 64 65 66 69 6e 65 64 2e 20 54 68 65 20 is defined. The
208c4 78 52 65 6e 61 6d 65 28 29 20 63 61 6c 6c 62 61 xRename() callba
208c5 63 6b 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 ck will modify t
208c6 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 he names. ** of
208c7 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 75 any resources u
208c8 73 65 64 20 62 79 20 74 68 65 20 76 2d 74 61 62 sed by the v-tab
208c9 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
208ca 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68 n (including oth
208cb 65 72 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 74 er. ** SQLite t
208cc 61 62 6c 65 73 29 20 74 68 61 74 20 61 72 65 20 ables) that are
208cd 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 identified by th
208ce 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69 e name of the vi
208cf 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a rtual table.. *
208d0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
208d1 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
208d2 4c 45 0a 20 20 69 66 28 20 69 73 56 69 72 74 75 LE. if( isVirtu
208d3 61 6c 52 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 alRename ){.
208d4 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 int i = ++pParse
208d5 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 ->nMem;. sqli
208d6 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
208d7 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 OP_String8, 0,
208d8 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b i, 0, zName, 0);
208d9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
208da 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 AddOp4(v, OP_VRe
208db 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 name, i, 0, 0,(c
208dc 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d onst char*)pTab-
208dd 3e 70 56 74 61 62 2c 20 50 34 5f 56 54 41 42 29 >pVtab, P4_VTAB)
208de 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
208df 2f 2a 20 66 69 67 75 72 65 20 6f 75 74 20 68 6f /* figure out ho
208e0 77 20 6d 61 6e 79 20 55 54 46 2d 38 20 63 68 61 w many UTF-8 cha
208e1 72 61 63 74 65 72 73 20 61 72 65 20 69 6e 20 7a racters are in z
208e2 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 Name */. zTabNa
208e3 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 me = pTab->zName
208e4 3b 0a 20 20 6e 54 61 62 4e 61 6d 65 20 3d 20 73 ;. nTabName = s
208e5 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 qlite3Utf8CharLe
208e6 6e 28 7a 54 61 62 4e 61 6d 65 2c 20 2d 31 29 3b n(zTabName, -1);
208e7 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 .. /* Modify th
208e8 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 e sqlite_master
208e9 74 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 65 table to use the
208ea 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e new table name.
208eb 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 */. sqlite3Nes
208ec 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c tedParse(pParse,
208ed 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 . "UPDATE %
208ee 51 2e 25 73 20 53 45 54 20 22 0a 23 69 66 64 65 Q.%s SET ".#ifde
208ef 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
208f0 49 47 47 45 52 0a 20 20 20 20 20 20 20 20 20 20 IGGER.
208f1 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 "sql = sqlite_re
208f2 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 name_table(sql,
208f3 25 51 29 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 %Q), ".#else.
208f4 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 43 41 "sql = CA
208f5 53 45 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 SE ".
208f6 20 22 57 48 45 4e 20 74 79 70 65 20 3d 20 27 74 "WHEN type = 't
208f7 72 69 67 67 65 72 27 20 54 48 45 4e 20 73 71 6c rigger' THEN sql
208f8 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 ite_rename_trigg
208f9 65 72 28 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 er(sql, %Q)".
208fa 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 73 "ELSE s
208fb 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 qlite_rename_tab
208fc 6c 65 28 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c le(sql, %Q) END,
208fd 20 22 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 ".#endif.
208fe 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 "tbl_name =
208ff 25 51 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 %Q, ".
20900 22 6e 61 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 "name = CASE ".
20901 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e "WHEN
20902 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 54 48 type='table' TH
20903 45 4e 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 EN %Q ".
20904 20 20 20 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c "WHEN name L
20905 49 4b 45 20 27 73 71 6c 69 74 65 5f 61 75 74 6f IKE 'sqlite_auto
20906 69 6e 64 65 78 25 25 27 20 41 4e 44 20 74 79 70 index%%' AND typ
20907 65 3d 27 69 6e 64 65 78 27 20 54 48 45 4e 20 22 e='index' THEN "
20908 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22 27 . "'
20909 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex
2090a 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 _' || %Q || subs
2090b 74 72 28 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22 tr(name,%d+18) "
2090c 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c . "EL
2090d 53 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 SE name END ".
2090e 20 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f 6e "WHERE tbl_n
2090f 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20 20 20 ame=%Q AND ".
20910 20 20 20 20 20 20 20 22 28 74 79 70 65 3d 27 74 "(type='t
20911 61 62 6c 65 27 20 4f 52 20 74 79 70 65 3d 27 69 able' OR type='i
20912 6e 64 65 78 27 20 4f 52 20 74 79 70 65 3d 27 74 ndex' OR type='t
20913 72 69 67 67 65 72 27 29 3b 22 2c 20 0a 20 20 20 rigger');", .
20914 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 zDb, SCHEMA_T
20915 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 ABLE(iDb), zName
20916 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 , zName, zName,
20917 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20918 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 OMIT_TRIGGER.
20919 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 zName,.#endif
2091a 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 . zName, nT
2091b 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 abName, zTabName
2091c 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 . );..#ifndef S
2091d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
2091e 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 NCREMENT. /* If
2091f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 the sqlite_sequ
20920 65 6e 63 65 20 74 61 62 6c 65 20 65 78 69 73 74 ence table exist
20921 73 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 s in this databa
20922 73 65 2c 20 74 68 65 6e 20 75 70 64 61 74 65 20 se, then update
20923 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 20 74 68 . ** it with th
20924 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 e new table name
20925 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c .. */. if( sql
20926 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 ite3FindTable(db
20927 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e , "sqlite_sequen
20928 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 ce", zDb) ){.
20929 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 sqlite3NestedPa
2092a 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 rse(pParse,.
2092b 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77 "UPDATE \"%w
2092c 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e \".sqlite_sequen
2092d 63 65 20 73 65 74 20 6e 61 6d 65 20 3d 20 25 51 ce set name = %Q
2092e 20 57 48 45 52 45 20 6e 61 6d 65 20 3d 20 25 51 WHERE name = %Q
2092f 22 2c 0a 20 20 20 20 20 20 20 20 7a 44 62 2c 20 ",. zDb,
20930 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 zName, pTab->zNa
20931 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a me);. }.#endif.
20932 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20933 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f OMIT_TRIGGER. /
20934 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 54 * If there are T
20935 45 4d 50 20 74 72 69 67 67 65 72 73 20 6f 6e 20 EMP triggers on
20936 74 68 69 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 this table, modi
20937 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 fy the sqlite_te
20938 6d 70 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 mp_master. ** t
20939 61 62 6c 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 able. Don't do t
2093a 68 69 73 20 69 66 20 74 68 65 20 74 61 62 6c 65 his if the table
2093b 20 62 65 69 6e 67 20 41 4c 54 45 52 65 64 20 69 being ALTERed i
2093c 73 20 69 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 s itself located
2093d 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d in. ** the tem
2093e 70 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f p database.. */
2093f 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77 . if( (zWhere=w
20940 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 hereTempTriggers
20941 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21 (pParse, pTab))!
20942 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
20943 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 3NestedParse(pPa
20944 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55 rse, . "U
20945 50 44 41 54 45 20 73 71 6c 69 74 65 5f 74 65 6d PDATE sqlite_tem
20946 70 5f 6d 61 73 74 65 72 20 53 45 54 20 22 0a 20 p_master SET ".
20947 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 "sql
20948 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f = sqlite_rename_
20949 74 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 trigger(sql, %Q)
2094a 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 , ".
2094b 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22 "tbl_name = %Q "
2094c 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48 . "WH
2094d 45 52 45 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c ERE %s;", zName,
2094e 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b zName, zWhere);
2094f 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
20950 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a ee(db, zWhere);.
20951 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
20952 20 44 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 Drop and reload
20953 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 61 the internal ta
20954 62 6c 65 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 ble schema. */.
20955 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 reloadTableSche
20956 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c ma(pParse, pTab,
20957 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 69 74 5f 72 zName);..exit_r
20958 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a 20 20 73 ename_table:. s
20959 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c qlite3SrcListDel
2095a 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 ete(db, pSrc);.
2095b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
2095c 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f b, zName);.}.../
2095d 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
2095e 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 on is called aft
2095f 65 72 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42 er an "ALTER TAB
20960 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 LE ... ADD" stat
20961 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 ement.** has bee
20962 6e 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65 n parsed. Argume
20963 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61 nt pColDef conta
20964 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 ins the text of
20965 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d the new.** colum
20966 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a n definition..**
20967 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74 .** The Table st
20968 72 75 63 74 75 72 65 20 70 50 61 72 73 65 2d 3e ructure pParse->
20969 70 4e 65 77 54 61 62 6c 65 20 77 61 73 20 65 78 pNewTable was ex
2096a 74 65 6e 64 65 64 20 74 6f 20 69 6e 63 6c 75 64 tended to includ
2096b 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c e.** the new col
2096c 75 6d 6e 20 64 75 72 69 6e 67 20 70 61 72 73 69 umn during parsi
2096d 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
2096e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
2096f 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 e3AlterFinishAdd
20970 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 Column(Parse *pP
20971 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f arse, Token *pCo
20972 6c 44 65 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a lDef){. Table *
20973 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 pNew;
20974 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 /* Copy of pP
20975 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 arse->pNewTable
20976 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
20977 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
20978 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 61 6c * Table being al
20979 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 tered */. int i
2097a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
2097b 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
2097c 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e number */. con
2097d 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 st char *zDb;
2097e 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
2097f 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e se name */. con
20980 73 74 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 st char *zTab;
20981 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 /* Table
20982 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a name */. char *
20983 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 zCol;
20984 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d /* Null-term
20985 69 6e 61 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65 inated column de
20986 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f finition */. Co
20987 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 lumn *pCol;
20988 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
20989 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 ew column */. E
2098a 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 xpr *pDflt;
2098b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 /* Defa
2098c 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ult value for th
2098d 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a e new column */.
2098e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
2098f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
20990 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
20991 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 ection; */.. db
20992 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
20993 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 if( pParse->nEr
20994 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 r || db->mallocF
20995 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a ailed ) return;.
20996 20 20 70 4e 65 77 20 3d 20 70 50 61 72 73 65 2d pNew = pParse-
20997 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 61 73 >pNewTable;. as
20998 73 65 72 74 28 20 70 4e 65 77 20 29 3b 0a 0a 20 sert( pNew );..
20999 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
2099a 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 BtreeHoldsAllMut
2099b 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 44 exes(db) );. iD
2099c 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
2099d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 4e 65 aToIndex(db, pNe
2099e 77 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a w->pSchema);. z
2099f 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 Db = db->aDb[iDb
209a0 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20 ].zName;. zTab
209a1 3d 20 26 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 = &pNew->zName[1
209a2 36 5d 3b 20 20 2f 2a 20 53 6b 69 70 20 74 68 65 6]; /* Skip the
209a3 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 "sqlite_alterta
209a4 62 5f 22 20 70 72 65 66 69 78 20 6f 6e 20 74 68 b_" prefix on th
209a5 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c e name */. pCol
209a6 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 = &pNew->aCol[p
209a7 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 New->nCol-1];.
209a8 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 pDflt = pCol->pD
209a9 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 flt;. pTab = sq
209aa 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 lite3FindTable(d
209ab 62 2c 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20 b, zTab, zDb);.
209ac 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29 3b assert( pTab );
209ad 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
209ae 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
209af 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 ION. /* Invoke
209b0 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f the authorizatio
209b1 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 n callback. */.
209b2 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 if( sqlite3Auth
209b3 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 Check(pParse, SQ
209b4 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 LITE_ALTER_TABLE
209b5 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 , zDb, pTab->zNa
209b6 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 me, 0) ){. re
209b7 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 turn;. }.#endif
209b8 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 .. /* If the de
209b9 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 fault value for
209ba 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 the new column w
209bb 61 73 20 73 70 65 63 69 66 69 65 64 20 77 69 74 as specified wit
209bc 68 20 61 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61 h a . ** litera
209bd 6c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 l NULL, then set
209be 20 70 44 66 6c 74 20 74 6f 20 30 2e 20 54 68 69 pDflt to 0. Thi
209bf 73 20 73 69 6d 70 6c 69 66 69 65 73 20 63 68 65 s simplifies che
209c0 63 6b 69 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61 cking. ** for a
209c1 6e 20 53 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75 n SQL NULL defau
209c2 6c 74 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 lt below.. */.
209c3 20 69 66 28 20 70 44 66 6c 74 20 26 26 20 70 44 if( pDflt && pD
209c4 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c flt->op==TK_NULL
209c5 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d 20 ){. pDflt =
209c6 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 0;. }.. /* Che
209c7 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 ck that the new
209c8 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70 column is not sp
209c9 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d 41 ecified as PRIMA
209ca 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 RY KEY or UNIQUE
209cb 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 .. ** If there
209cc 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f is a NOT NULL co
209cd 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20 74 nstraint, then t
209ce 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
209cf 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f for the. ** co
209d0 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 lumn must not be
209d1 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 NULL.. */. if
209d2 28 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 ( pCol->isPrimKe
209d3 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 y ){. sqlite3
209d4 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
209d5 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 50 "Cannot add a P
209d6 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d RIMARY KEY colum
209d7 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b n");. return;
209d8 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d . }. if( pNew-
209d9 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 >pIndex ){. s
209da 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
209db 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 Parse, "Cannot a
209dc 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f 6c 75 dd a UNIQUE colu
209dd 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e mn");. return
209de 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c ;. }. if( pCol
209df 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44 ->notNull && !pD
209e0 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 flt ){. sqlit
209e1 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
209e2 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e e, . "Can
209e3 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e 55 not add a NOT NU
209e4 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64 LL column with d
209e5 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c efault value NUL
209e6 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b L");. return;
209e7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 . }.. /* Ensur
209e8 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65 78 e the default ex
209e9 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65 pression is some
209ea 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69 74 thing that sqlit
209eb 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 e3ValueFromExpr(
209ec 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c ). ** can handl
209ed 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52 e (i.e. not CURR
209ee 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20 ENT_TIME etc.).
209ef 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20 */. if( pDflt
209f0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 ){. sqlite3_v
209f1 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20 alue *pVal;.
209f2 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75 65 if( sqlite3Value
209f3 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66 FromExpr(db, pDf
209f4 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c lt, SQLITE_UTF8,
209f5 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 SQLITE_AFF_NONE
209f6 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 , &pVal) ){.
209f7 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
209f8 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 ed = 1;. re
209f9 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 turn;. }.
209fa 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20 20 if( !pVal ){.
209fb 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
209fc 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e sg(pParse, "Cann
209fd 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 ot add a column
209fe 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e with non-constan
209ff 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20 20 t default");.
20a00 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
20a01 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 . sqlite3Valu
20a02 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d eFree(pVal);. }
20a03 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 .. /* Modify th
20a04 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 e CREATE TABLE s
20a05 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a tatement. */. z
20a06 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 Col = sqlite3DbS
20a07 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 trNDup(db, (char
20a08 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43 *)pColDef->z, pC
20a09 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28 olDef->n);. if(
20a0a 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61 zCol ){. cha
20a0b 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b r *zEnd = &zCol[
20a0c 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20 pColDef->n-1];.
20a0d 20 20 20 77 68 69 6c 65 28 20 7a 45 6e 64 3e 7a while( zEnd>z
20a0e 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64 3d 3d 27 Col && (*zEnd=='
20a0f 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73 ;' || sqlite3Iss
20a10 70 61 63 65 28 2a 7a 45 6e 64 29 29 20 29 7b 0a pace(*zEnd)) ){.
20a11 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20 *zEnd-- =
20a12 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 '\0';. }.
20a13 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 sqlite3NestedPar
20a14 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 se(pParse, .
20a15 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77 "UPDATE \"%w
20a16 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 20 20 \".%s SET ".
20a17 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 75 62 "sql = sub
20a18 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c str(sql,1,%d) ||
20a19 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73 ', ' || %Q || s
20a1a 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20 22 0a ubstr(sql,%d) ".
20a1b 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 74 "WHERE t
20a1c 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e ype = 'table' AN
20a1d 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20 D name = %Q", .
20a1e 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 zDb, SCHEMA
20a1f 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 4e 65 _TABLE(iDb), pNe
20a20 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c w->addColOffset,
20a21 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64 zCol, pNew->add
20a22 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20 ColOffset+1,.
20a23 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20 zTab. );.
20a24 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
20a25 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a (db, zCol);. }.
20a26 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66 . /* If the def
20a27 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68 ault value of th
20a28 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 e new column is
20a29 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 74 NULL, then set t
20a2a 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72 he file. ** for
20a2b 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74 68 65 mat to 2. If the
20a2c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f default value o
20a2d 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e f the new column
20a2e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 is not NULL,.
20a2f 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d ** the file form
20a30 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20 at becomes 3..
20a31 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69 6e 69 */. sqlite3Mini
20a32 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 70 50 mumFileFormat(pP
20a33 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66 6c 74 arse, iDb, pDflt
20a34 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a ? 3 : 2);.. /*
20a35 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65 Reload the sche
20a36 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69 66 69 ma of the modifi
20a37 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72 ed table. */. r
20a38 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 eloadTableSchema
20a39 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 (pParse, pTab, p
20a3a 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a Tab->zName);.}..
20a3b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
20a3c 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ion is called by
20a3d 20 74 68 65 20 70 61 72 73 65 72 20 61 66 74 65 the parser afte
20a3e 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 r the table-name
20a3f 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52 in.** an "ALTER
20a40 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 TABLE <table-na
20a41 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65 6d 65 me> ADD" stateme
20a42 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20 41 72 nt is parsed. Ar
20a43 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20 gument .** pSrc
20a44 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 is the full-name
20a45 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 of the table be
20a46 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a ing altered..**.
20a47 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
20a48 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69 61 6c makes a (partial
20a49 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 ) copy of the Ta
20a4a 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a ble structure.**
20a4b 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 62 for the table b
20a4c 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61 6e 64 eing altered and
20a4d 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e 65 77 sets Parse.pNew
20a4e 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a Table to point.*
20a4f 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65 * to it. Routine
20a50 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
20a51 70 61 72 73 65 72 20 61 73 20 74 68 65 20 63 6f parser as the co
20a52 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a lumn definition.
20a53 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28 69 2e ** is parsed (i.
20a54 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c e. sqlite3AddCol
20a55 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65 20 6e umn()) add the n
20a56 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74 ew Column data t
20a57 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20 o .** the copy.
20a58 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 The copy of the
20a59 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 Table structure
20a5a 69 73 20 64 65 6c 65 74 65 64 20 62 79 20 74 6f is deleted by to
20a5b 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 kenize.c .** aft
20a5c 65 72 20 70 61 72 73 69 6e 67 20 69 73 20 66 69 er parsing is fi
20a5d 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f nished..**.** Ro
20a5e 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41 6c 74 utine sqlite3Alt
20a5f 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d erFinishAddColum
20a60 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c n() will be call
20a61 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a ed to complete.*
20a62 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22 41 4c * coding the "AL
20a63 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 TER TABLE ... AD
20a64 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f D" statement..*/
20a65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20a66 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 void sqlite3Alte
20a67 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 rBeginAddColumn(
20a68 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 Parse *pParse, S
20a69 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 rcList *pSrc){.
20a6a 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 Table *pNew;.
20a6b 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 Table *pTab;. V
20a6c 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 dbe *v;. int iD
20a6d 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e b;. int i;. in
20a6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69 t nAlloc;. sqli
20a6f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
20a70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b ->db;.. /* Look
20a71 20 75 70 20 74 68 65 20 74 61 62 6c 65 20 62 65 up the table be
20a72 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a ing altered. */.
20a73 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
20a74 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 ->pNewTable==0 )
20a75 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
20a76 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c te3BtreeHoldsAll
20a77 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20 Mutexes(db) );.
20a78 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
20a79 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69 ailed ) goto exi
20a7a 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 t_begin_add_colu
20a7b 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c mn;. pTab = sql
20a7c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 ite3LocateTable(
20a7d 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63 2d pParse, 0, pSrc-
20a7e 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 >a[0].zName, pSr
20a7f 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 c->a[0].zDatabas
20a80 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 e);. if( !pTab
20a81 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 ) goto exit_begi
20a82 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 n_add_column;..#
20a83 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20a84 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
20a85 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 if( IsVirtual(
20a86 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c pTab) ){. sql
20a87 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
20a88 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61 rse, "virtual ta
20a89 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 bles may not be
20a8a 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20 20 67 altered");. g
20a8b 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 oto exit_begin_a
20a8c 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 dd_column;. }.#
20a8d 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 endif.. /* Make
20a8e 20 73 75 72 65 20 74 68 69 73 20 69 73 20 6e 6f sure this is no
20a8f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 t an attempt to
20a90 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20 2a 2f ALTER a view. */
20a91 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 . if( pTab->pSe
20a92 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 lect ){. sqli
20a93 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
20a94 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 se, "Cannot add
20a95 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69 a column to a vi
20a96 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 ew");. goto e
20a97 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f xit_begin_add_co
20a98 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 lumn;. }.. ass
20a99 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64 43 6f ert( pTab->addCo
20a9a 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20 20 69 lOffset>0 );. i
20a9b 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
20a9c 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 maToIndex(db, pT
20a9d 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 ab->pSchema);..
20a9e 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79 20 6f /* Put a copy o
20a9f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 f the Table stru
20aa0 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e 65 77 ct in Parse.pNew
20aa1 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a 20 20 Table for the.
20aa2 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c ** sqlite3AddCol
20aa3 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 umn() function a
20aa4 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f nd friends to mo
20aa5 64 69 66 79 2e 20 20 42 75 74 20 6d 6f 64 69 66 dify. But modif
20aa6 79 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 y. ** the name
20aa7 62 79 20 61 64 64 69 6e 67 20 61 6e 20 22 73 71 by adding an "sq
20aa8 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 lite_altertab_"
20aa9 70 72 65 66 69 78 2e 20 20 42 79 20 61 64 64 69 prefix. By addi
20aaa 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 ng this. ** pre
20aab 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65 20 74 fix, we insure t
20aac 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77 69 6c hat the name wil
20aad 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 l not collide wi
20aae 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 th an existing.
20aaf 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61 75 73 ** table becaus
20ab0 65 20 75 73 65 72 20 74 61 62 6c 65 20 61 72 65 e user table are
20ab1 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 not allowed to
20ab2 68 61 76 65 20 74 68 65 20 22 73 71 6c 69 74 65 have the "sqlite
20ab3 5f 22 0a 20 20 2a 2a 20 70 72 65 66 69 78 20 6f _". ** prefix o
20ab4 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20 n their name..
20ab5 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54 61 62 */. pNew = (Tab
20ab6 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c le*)sqlite3DbMal
20ab7 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 locZero(db, size
20ab8 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 of(Table));. if
20ab9 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f 20 65 ( !pNew ) goto e
20aba 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f xit_begin_add_co
20abb 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e lumn;. pParse->
20abc 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e 65 77 pNewTable = pNew
20abd 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d ;. pNew->nRef =
20abe 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65 1;. pNew->dbMe
20abf 6d 20 3d 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b m = pTab->dbMem;
20ac0 0a 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 . pNew->nCol =
20ac1 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 pTab->nCol;. as
20ac2 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c sert( pNew->nCol
20ac3 3e 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d >0 );. nAlloc =
20ac4 20 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 (((pNew->nCol-1
20ac5 29 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 )/8)*8)+8;. ass
20ac6 65 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 ert( nAlloc>=pNe
20ac7 77 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f w->nCol && nAllo
20ac8 63 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 c%8==0 && nAlloc
20ac9 2d 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b -pNew->nCol<8 );
20aca 0a 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 . pNew->aCol =
20acb 28 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 (Column*)sqlite3
20acc 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
20acd 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a sizeof(Column)*
20ace 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d nAlloc);. pNew-
20acf 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 >zName = sqlite3
20ad0 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c MPrintf(db, "sql
20ad1 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 25 73 22 ite_altertab_%s"
20ad2 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
20ad3 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f if( !pNew->aCo
20ad4 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d l || !pNew->zNam
20ad5 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c e ){. db->mal
20ad6 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
20ad7 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 goto exit_beg
20ad8 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 in_add_column;.
20ad9 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 }. memcpy(pNew
20ada 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 ->aCol, pTab->aC
20adb 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d ol, sizeof(Colum
20adc 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a n)*pNew->nCol);.
20add 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 for(i=0; i<pNe
20ade 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 w->nCol; i++){.
20adf 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 Column *pCol
20ae0 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d = &pNew->aCol[i]
20ae1 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d ;. pCol->zNam
20ae2 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 e = sqlite3DbStr
20ae3 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e Dup(db, pCol->zN
20ae4 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e ame);. pCol->
20ae5 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 zColl = 0;. p
20ae6 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a Col->zType = 0;.
20ae7 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 pCol->pDflt
20ae8 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d = 0;. }. pNew-
20ae9 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 >pSchema = db->a
20aea 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b Db[iDb].pSchema;
20aeb 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f . pNew->addColO
20aec 66 66 73 65 74 20 3d 20 70 54 61 62 2d 3e 61 64 ffset = pTab->ad
20aed 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e dColOffset;. pN
20aee 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20 ew->nRef = 1;..
20aef 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e /* Begin a tran
20af0 73 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72 saction and incr
20af1 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61 ement the schema
20af2 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73 cookie. */. s
20af3 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 qlite3BeginWrite
20af4 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 Operation(pParse
20af5 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d , 0, iDb);. v =
20af6 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
20af7 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 pParse);. if( !
20af8 76 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 v ) goto exit_be
20af9 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a gin_add_column;.
20afa 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 sqlite3ChangeC
20afb 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 ookie(pParse, iD
20afc 62 29 3b 0a 0a 65 78 69 74 5f 62 65 67 69 6e 5f b);..exit_begin_
20afd 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 73 71 add_column:. sq
20afe 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 lite3SrcListDele
20aff 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 te(db, pSrc);.
20b00 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 return;.}.#endif
20b01 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c 54 45 /* SQLITE_ALTE
20b02 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a R_TABLE */../***
20b03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
20b04 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a of alter.c *****
20b05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
20b08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
20b09 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a 65 2e 63 n file analyze.c
20b0a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
20b0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
20b0d 2a 20 32 30 30 35 20 4a 75 6c 79 20 38 0a 2a 2a * 2005 July 8.**
20b0e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
20b0f 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
20b10 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
20b11 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
20b12 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
20b13 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
20b14 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
20b15 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
20b16 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
20b17 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
20b18 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
20b19 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
20b1a 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
20b1b 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
20b1c 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
20b1d 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
20b1e 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
20b1f 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
20b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
20b24 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
20b25 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69 61 74 ns code associat
20b26 65 64 20 77 69 74 68 20 74 68 65 20 41 4e 41 4c ed with the ANAL
20b27 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a YZE command..**.
20b28 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 61 6e 61 ** @(#) $Id: ana
20b29 6c 79 7a 65 2e 63 2c 76 20 31 2e 35 32 20 32 30 lyze.c,v 1.52 20
20b2a 30 39 2f 30 34 2f 31 36 20 31 37 3a 34 35 3a 34 09/04/16 17:45:4
20b2b 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 8 drh Exp $.*/.#
20b2c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20b2d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a 0a 2a IT_ANALYZE../*.*
20b2e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 * This routine g
20b2f 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 68 enerates code th
20b30 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73 71 6c at opens the sql
20b31 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 ite_stat1 table
20b32 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 53 74 on cursor.** iSt
20b33 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 atCur..**.** If
20b34 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 the sqlite_stat1
20b35 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 tables does not
20b36 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 previously exis
20b37 74 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 t, it is created
20b38 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 ..** If it does
20b39 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 previously exist
20b3a 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20 61 73 , all entires as
20b3b 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 61 sociated with ta
20b3c 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20 61 72 ble zWhere.** ar
20b3d 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66 20 7a e removed. If z
20b3e 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20 61 6c Where==0 then al
20b3f 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20 72 65 l entries are re
20b40 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 moved..*/.static
20b41 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74 54 61 void openStatTa
20b42 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 ble(. Parse *pP
20b43 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f arse, /
20b44 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
20b45 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 t */. int iDb,
20b46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
20b47 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 77 * The database w
20b48 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 69 6e e are looking in
20b49 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 43 */. int iStatC
20b4a 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ur, /*
20b4b 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 Open the sqlite
20b4c 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f 6e 20 _stat1 table on
20b4d 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 this cursor */.
20b4e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 68 const char *zWh
20b4f 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 ere /* Dele
20b50 74 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 te entries assoc
20b51 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 iated with this
20b52 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 table */.){. sq
20b53 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
20b54 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 se->db;. Db *pD
20b55 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74 50 61 b;. int iRootPa
20b56 67 65 3b 0a 20 20 75 38 20 63 72 65 61 74 65 53 ge;. u8 createS
20b57 74 61 74 31 20 3d 20 30 3b 0a 20 20 54 61 62 6c tat1 = 0;. Tabl
20b58 65 20 2a 70 53 74 61 74 3b 0a 20 20 56 64 62 65 e *pStat;. Vdbe
20b59 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 *v = sqlite3Get
20b5a 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 0a 20 Vdbe(pParse);..
20b5b 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 if( v==0 ) retu
20b5c 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 rn;. assert( sq
20b5d 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 lite3BtreeHoldsA
20b5e 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b llMutexes(db) );
20b5f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
20b60 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64 62 20 e3VdbeDb(v)==db
20b61 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e );. pDb = &db->
20b62 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 28 20 aDb[iDb];. if(
20b63 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74 65 33 (pStat = sqlite3
20b64 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 FindTable(db, "s
20b65 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 70 44 qlite_stat1", pD
20b66 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 20 29 7b b->zName))==0 ){
20b67 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 . /* The sqli
20b68 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 73 20 te_stat1 tables
20b69 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 does not exist.
20b6a 20 43 72 65 61 74 65 20 69 74 2e 20 20 0a 20 20 Create it. .
20b6b 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 61 ** Note that a
20b6c 20 73 69 64 65 2d 65 66 66 65 63 74 20 6f 66 20 side-effect of
20b6d 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
20b6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 6f statement is to
20b6f 20 6c 65 61 76 65 0a 20 20 20 20 2a 2a 20 74 68 leave. ** th
20b70 65 20 72 6f 6f 74 70 61 67 65 20 6f 66 20 74 68 e rootpage of th
20b71 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 72 e new table in r
20b72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e egister pParse->
20b73 72 65 67 52 6f 6f 74 2e 20 20 54 68 69 73 20 69 regRoot. This i
20b74 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61 s. ** importa
20b75 6e 74 20 62 65 63 61 75 73 65 20 74 68 65 20 4f nt because the O
20b76 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64 65 20 penWrite opcode
20b77 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20 6e 65 below will be ne
20b78 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 20 eding it. */.
20b79 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 sqlite3NestedPa
20b7a 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 rse(pParse,.
20b7b 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 "CREATE TABLE
20b7c 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 28 %Q.sqlite_stat1(
20b7d 74 62 6c 2c 69 64 78 2c 73 74 61 74 29 22 2c 0a tbl,idx,stat)",.
20b7e 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 pDb->zName
20b7f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 52 6f 6f . );. iRoo
20b80 74 50 61 67 65 20 3d 20 70 50 61 72 73 65 2d 3e tPage = pParse->
20b81 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20 63 72 65 regRoot;. cre
20b82 61 74 65 53 74 61 74 31 20 3d 20 31 3b 20 20 2f ateStat1 = 1; /
20b83 2a 20 43 61 75 73 65 20 72 6f 6f 74 70 61 67 65 * Cause rootpage
20b84 20 74 6f 20 62 65 20 74 61 6b 65 6e 20 66 72 6f to be taken fro
20b85 6d 20 74 6f 70 20 6f 66 20 73 74 61 63 6b 20 2a m top of stack *
20b86 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 57 /. }else if( zW
20b87 68 65 72 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 here ){. /* T
20b88 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 he sqlite_stat1
20b89 74 61 62 6c 65 20 65 78 69 73 74 73 2e 20 20 44 table exists. D
20b8a 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72 69 65 elete all entrie
20b8b 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
20b8c 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61 62 h. ** the tab
20b8d 6c 65 20 7a 57 68 65 72 65 2e 20 2a 2f 0a 20 20 le zWhere. */.
20b8e 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
20b8f 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 arse(pParse,.
20b90 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d "DELETE FROM
20b91 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 %Q.sqlite_stat1
20b92 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 0a WHERE tbl=%Q",.
20b93 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d pDb->zNam
20b94 65 2c 20 7a 57 68 65 72 65 0a 20 20 20 20 29 3b e, zWhere. );
20b95 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d . iRootPage =
20b96 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 pStat->tnum;.
20b97 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
20b98 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 e sqlite_stat1 t
20b99 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 able already exi
20b9a 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c sts. Delete all
20b9b 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 69 52 rows. */. iR
20b9c 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 61 74 2d ootPage = pStat-
20b9d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 >tnum;. sqlit
20b9e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20b9f 4f 50 5f 43 6c 65 61 72 2c 20 70 53 74 61 74 2d OP_Clear, pStat-
20ba0 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d >tnum, iDb);. }
20ba1 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 .. /* Open the
20ba2 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 sqlite_stat1 tab
20ba3 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 le for writing.
20ba4 55 6e 6c 65 73 73 20 69 74 20 77 61 73 20 63 72 Unless it was cr
20ba5 65 61 74 65 64 0a 20 20 2a 2a 20 62 79 20 74 68 eated. ** by th
20ba6 69 73 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c is vdbe program,
20ba7 20 6c 6f 63 6b 20 69 74 20 66 6f 72 20 77 72 69 lock it for wri
20ba8 74 69 6e 67 20 61 74 20 74 68 65 20 73 68 61 72 ting at the shar
20ba9 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 ed-cache level.
20baa 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 76 64 . ** If this vd
20bab 62 65 20 64 69 64 20 63 72 65 61 74 65 20 74 68 be did create th
20bac 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 e sqlite_stat1 t
20bad 61 62 6c 65 2c 20 74 68 65 6e 20 69 74 20 6d 75 able, then it mu
20bae 73 74 20 68 61 76 65 20 0a 20 20 2a 2a 20 61 6c st have . ** al
20baf 72 65 61 64 79 20 6f 62 74 61 69 6e 65 64 20 61 ready obtained a
20bb0 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 2c 20 6d 61 schema-lock, ma
20bb1 6b 69 6e 67 20 74 68 65 20 77 72 69 74 65 2d 6c king the write-l
20bb2 6f 63 6b 20 72 65 64 75 6e 64 61 6e 74 2e 0a 20 ock redundant..
20bb3 20 2a 2f 0a 20 20 69 66 28 20 21 63 72 65 61 74 */. if( !creat
20bb4 65 53 74 61 74 31 20 29 7b 0a 20 20 20 20 73 71 eStat1 ){. sq
20bb5 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 lite3TableLock(p
20bb6 50 61 72 73 65 2c 20 69 44 62 2c 20 69 52 6f 6f Parse, iDb, iRoo
20bb7 74 50 61 67 65 2c 20 31 2c 20 22 73 71 6c 69 74 tPage, 1, "sqlit
20bb8 65 5f 73 74 61 74 31 22 29 3b 0a 20 20 7d 0a 20 e_stat1");. }.
20bb9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20bba 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 p3(v, OP_OpenWri
20bbb 74 65 2c 20 69 53 74 61 74 43 75 72 2c 20 69 52 te, iStatCur, iR
20bbc 6f 6f 74 50 61 67 65 2c 20 69 44 62 29 3b 0a 20 ootPage, iDb);.
20bbd 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
20bbe 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 geP4(v, -1, (cha
20bbf 72 20 2a 29 33 2c 20 50 34 5f 49 4e 54 33 32 29 r *)3, P4_INT32)
20bc0 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 ;. sqlite3VdbeC
20bc1 68 61 6e 67 65 50 35 28 76 2c 20 63 72 65 61 74 hangeP5(v, creat
20bc2 65 53 74 61 74 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eStat1);.}../*.*
20bc3 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
20bc4 74 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 to do an analysi
20bc5 73 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 s of all indices
20bc6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
20bc7 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 .** a single tab
20bc8 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
20bc9 69 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 id analyzeOneTab
20bca 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 le(. Parse *pPa
20bcb 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 rse, /* Parser
20bcc 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 context */. Ta
20bcd 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f ble *pTab, /
20bce 2a 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e * Table whose in
20bcf 64 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 dices are to be
20bd0 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e analyzed */. in
20bd1 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f t iStatCur, /
20bd2 2a 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43 * Index of VdbeC
20bd3 75 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65 ursor that write
20bd4 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 s the sqlite_sta
20bd5 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e t1 table */. in
20bd6 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f t iMem /
20bd7 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f * Available memo
20bd8 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 ry locations beg
20bd9 69 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 in here */.){.
20bda 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 Index *pIdx;
20bdb 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20 /* An index to
20bdc 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a being analyzed *
20bdd 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b /. int iIdxCur;
20bde 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
20bdf 20 56 64 62 65 43 75 72 73 6f 72 20 66 6f 72 20 VdbeCursor for
20be0 69 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c index being anal
20be1 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 yzed */. int nC
20be2 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ol; /* Nu
20be3 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
20be4 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a in the index */.
20be5 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 Vdbe *v;
20be6 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
20be7 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 l machine being
20be8 62 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e built up */. in
20be9 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f t i; /
20bea 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
20beb 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f /. int topOfLoo
20bec 70 3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 p; /* The top
20bed 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 of the loop */.
20bee 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 int endOfLoop;
20bef 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 /* The end of
20bf0 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e the loop */. in
20bf1 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f t addr; /
20bf2 2a 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 * The address of
20bf3 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 an instruction
20bf4 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 */. int iDb;
20bf5 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
20bf6 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 f database conta
20bf7 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20 ining pTab */..
20bf8 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 v = sqlite3GetV
20bf9 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 dbe(pParse);. i
20bfa 66 28 20 76 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 f( v==0 || NEVER
20bfb 28 70 54 61 62 3d 3d 30 29 20 7c 7c 20 70 54 61 (pTab==0) || pTa
20bfc 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a b->pIndex==0 ){.
20bfd 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61 /* Do no ana
20bfe 6c 79 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73 lysis for tables
20bff 20 74 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e that have no in
20c00 64 69 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74 dices */. ret
20c01 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 urn;. }. asser
20c02 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
20c03 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 oldsAllMutexes(p
20c04 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 Parse->db) );.
20c05 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
20c06 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 emaToIndex(pPars
20c07 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 e->db, pTab->pSc
20c08 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 hema);. assert(
20c09 20 69 44 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64 iDb>=0 );.#ifnd
20c0a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
20c0b 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 UTHORIZATION. i
20c0c 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 f( sqlite3AuthCh
20c0d 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 eck(pParse, SQLI
20c0e 54 45 5f 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62 TE_ANALYZE, pTab
20c0f 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 ->zName, 0,.
20c10 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 pParse->db->aD
20c11 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 b[iDb].zName ) )
20c12 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
20c13 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 }.#endif.. /* E
20c14 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 2d stablish a read-
20c15 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c lock on the tabl
20c16 65 20 61 74 20 74 68 65 20 73 68 61 72 65 64 2d e at the shared-
20c17 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a cache level. */.
20c18 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f sqlite3TableLo
20c19 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 ck(pParse, iDb,
20c1a 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 pTab->tnum, 0, p
20c1b 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 Tab->zName);..
20c1c 69 49 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 iIdxCur = pParse
20c1d 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 ->nTab++;. for(
20c1e 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 pIdx=pTab->pInde
20c1f 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 x; pIdx; pIdx=pI
20c20 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 dx->pNext){.
20c21 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 KeyInfo *pKey =
20c22 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 sqlite3IndexKeyi
20c23 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 nfo(pParse, pIdx
20c24 29 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 46 69 );. int regFi
20c25 65 6c 64 73 3b 20 20 20 20 2f 2a 20 52 65 67 69 elds; /* Regi
20c26 73 74 65 72 20 62 6c 6f 63 6b 20 66 6f 72 20 62 ster block for b
20c27 75 69 6c 64 69 6e 67 20 72 65 63 6f 72 64 73 20 uilding records
20c28 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65 */. int regRe
20c29 63 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 c; /* Regi
20c2a 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d ster holding com
20c2b 70 6c 65 74 65 64 20 72 65 63 6f 72 64 20 2a 2f pleted record */
20c2c 0a 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 . int regTemp
20c2d 3b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 ; /* Tempor
20c2e 61 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72 ary use register
20c2f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 43 */. int regC
20c30 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e ol; /* Con
20c31 74 65 6e 74 20 6f 66 20 61 20 63 6f 6c 75 6d 6e tent of a column
20c32 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 from the table
20c33 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a being analyzed *
20c34 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 /. int regRow
20c35 69 64 3b 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 id; /* Rowid
20c36 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72 74 65 for the inserte
20c37 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 d record */.
20c38 69 6e 74 20 72 65 67 46 32 3b 0a 0a 20 20 20 20 int regF2;..
20c39 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 /* Open a cursor
20c3a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f to the index to
20c3b 20 62 65 20 61 6e 61 6c 79 7a 65 64 0a 20 20 20 be analyzed.
20c3c 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
20c3d 69 44 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65 iDb==sqlite3Sche
20c3e 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 maToIndex(pParse
20c3f 2d 3e 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 ->db, pIdx->pSch
20c40 65 6d 61 29 20 29 3b 0a 20 20 20 20 6e 43 6f 6c ema) );. nCol
20c41 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e = pIdx->nColumn
20c42 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
20c43 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 eAddOp4(v, OP_Op
20c44 65 6e 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c enRead, iIdxCur,
20c45 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 pIdx->tnum, iDb
20c46 2c 0a 20 20 20 20 20 20 20 20 28 63 68 61 72 20 ,. (char
20c47 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e *)pKey, P4_KEYIN
20c48 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 FO_HANDOFF);.
20c49 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
20c4a 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 "%s", pIdx->zNa
20c4b 6d 65 29 29 3b 0a 20 20 20 20 72 65 67 46 69 65 me));. regFie
20c4c 6c 64 73 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a lds = iMem+nCol*
20c4d 32 3b 0a 20 20 20 20 72 65 67 54 65 6d 70 20 3d 2;. regTemp =
20c4e 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67 43 regRowid = regC
20c4f 6f 6c 20 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 ol = regFields+3
20c50 3b 0a 20 20 20 20 72 65 67 52 65 63 20 3d 20 72 ;. regRec = r
20c51 65 67 43 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 egCol+1;. if(
20c52 20 72 65 67 52 65 63 3e 70 50 61 72 73 65 2d 3e regRec>pParse->
20c53 6e 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 nMem ){. pP
20c54 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 arse->nMem = reg
20c55 52 65 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 Rec;. }..
20c56 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 /* Memory cells
20c57 61 72 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c are used as foll
20c58 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ows:. **.
20c59 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a ** mem[iMem]:
20c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 The
20c5b 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
20c5c 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 rows in the tab
20c5d 6c 65 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 le.. ** me
20c5e 6d 5b 69 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 m[iMem+1]:
20c5f 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 Number of d
20c60 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 20 69 istinct values i
20c61 6e 20 63 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a n column 1. *
20c62 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 * .... **
20c63 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c mem[iMem+nCol
20c64 5d 3a 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 ]: Number
20c65 20 6f 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c of distinct val
20c66 75 65 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a ues in column N.
20c67 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d ** mem[iM
20c68 65 6d 2b 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 em+nCol+1]
20c69 20 4c 61 73 74 20 6f 62 73 65 72 76 65 64 20 76 Last observed v
20c6a 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 alue of column 1
20c6b 0a 20 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 . ** ....
20c6c 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 ** mem[iMe
20c6d 6d 2b 6e 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 m+nCol+nCol]:
20c6e 4c 61 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 Last observed va
20c6f 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a lue of column N.
20c70 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 **. ** Ce
20c71 6c 6c 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 lls iMem through
20c72 20 69 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 iMem+nCol are i
20c73 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e nitialized to 0.
20c74 20 20 54 68 65 20 6f 74 68 65 72 73 0a 20 20 20 The others.
20c75 20 2a 2a 20 61 72 65 20 69 6e 69 74 69 61 6c 69 ** are initiali
20c76 7a 65 64 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 zed to NULL..
20c77 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b */. for(i=0;
20c78 20 69 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a i<=nCol; i++){.
20c79 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
20c7a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
20c7b 74 65 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 teger, 0, iMem+i
20c7c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
20c7d 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b (i=0; i<nCol; i+
20c7e 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
20c7f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
20c80 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b P_Null, 0, iMem+
20c81 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d nCol+i+1);. }
20c82 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 .. /* Do the
20c83 61 6e 61 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f analysis.. */
20c84 0a 20 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d . endOfLoop =
20c85 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
20c86 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 Label(v);. sq
20c87 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
20c88 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 v, OP_Rewind, iI
20c89 64 78 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 dxCur, endOfLoop
20c8a 29 3b 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 );. topOfLoop
20c8b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
20c8c 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 rrentAddr(v);.
20c8d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20c8e 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d Op2(v, OP_AddImm
20c8f 2c 20 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 , iMem, 1);.
20c90 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b for(i=0; i<nCol;
20c91 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c i++){. sql
20c92 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
20c93 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 , OP_Column, iId
20c94 78 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 xCur, i, regCol)
20c95 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
20c96 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
20c97 4e 65 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 Ne, regCol, 0, i
20c98 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 Mem+nCol+i+1);.
20c99 20 20 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a /**** TODO:
20c9a 20 20 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 add collating
20c9b 73 65 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a sequence *****/.
20c9c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
20c9d 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c eChangeP5(v, SQL
20c9e 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b ITE_JUMPIFNULL);
20c9f 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
20ca0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20ca1 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f OP_Goto, 0, endO
20ca2 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 fLoop);. for(
20ca3 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b i=0; i<nCol; i++
20ca4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
20ca5 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
20ca6 74 6f 70 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 topOfLoop + 2*(i
20ca7 20 2b 20 31 29 29 3b 0a 20 20 20 20 20 20 73 71 + 1));. sq
20ca8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
20ca9 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d v, OP_AddImm, iM
20caa 65 6d 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 em+i+1, 1);.
20cab 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20cac 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e Op3(v, OP_Column
20cad 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d , iIdxCur, i, iM
20cae 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 em+nCol+i+1);.
20caf 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
20cb0 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
20cb1 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 v, endOfLoop);.
20cb2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20cb3 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c dOp2(v, OP_Next,
20cb4 20 69 49 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c iIdxCur, topOfL
20cb5 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 oop);. sqlite
20cb6 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
20cb7 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 P_Close, iIdxCur
20cb8 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 );.. /* Store
20cb9 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 0a the results. .
20cba 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
20cbb 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 e result is a si
20cbc 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 ngle row of the
20cbd 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 sqlite_stat1 tab
20cbe 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20 le. The first.
20cbf 20 20 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e ** two column
20cc0 73 20 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20 s are the names
20cc1 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 of the table and
20cc2 20 69 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69 index. The thi
20cc3 72 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a rd column. **
20cc4 20 69 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d is a string com
20cc5 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20 posed of a list
20cc6 6f 66 20 69 6e 74 65 67 65 72 20 73 74 61 74 69 of integer stati
20cc7 73 74 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a stics about the.
20cc8 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 ** index. T
20cc9 68 65 20 66 69 72 73 74 20 69 6e 74 65 67 65 72 he first integer
20cca 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 in the list is
20ccb 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
20ccc 20 6f 66 20 65 6e 74 72 69 65 73 0a 20 20 20 20 of entries.
20ccd 2a 2a 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e ** in the index.
20cce 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61 There is one a
20ccf 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 dditional intege
20cd0 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f r in the list fo
20cd1 72 20 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f r each. ** co
20cd2 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c lumn of the tabl
20cd3 65 2e 20 20 54 68 69 73 20 61 64 64 69 74 69 6f e. This additio
20cd4 6e 61 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61 nal integer is a
20cd5 20 67 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 guess of how ma
20cd6 6e 79 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f ny. ** rows o
20cd7 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 f the table the
20cd8 69 6e 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 index will selec
20cd9 74 2e 20 20 49 66 20 44 20 69 73 20 74 68 65 20 t. If D is the
20cda 63 6f 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 count of distinc
20cdb 74 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 t. ** values
20cdc 61 6e 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74 and K is the tot
20cdd 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 al number of row
20cde 73 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65 s, then the inte
20cdf 67 65 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a ger is computed.
20ce0 20 20 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a ** as:. *
20ce1 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 *. **
20ce2 49 20 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 I = (K+D-1)/D.
20ce3 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b **. ** If K
20ce4 3d 3d 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 ==0 then no entr
20ce5 79 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 y is made into t
20ce6 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 he sqlite_stat1
20ce7 74 61 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 table. . **
20ce8 49 66 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 If K>0 then it i
20ce9 73 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 s always the cas
20cea 65 20 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76 e the D>0 so div
20ceb 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 ision by zero.
20cec 20 20 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f ** is never po
20ced 73 73 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 ssible.. */.
20cee 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 addr = sqlite
20cef 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
20cf0 50 5f 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a P_IfNot, iMem);.
20cf1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20cf2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 ddOp4(v, OP_Stri
20cf3 6e 67 38 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 ng8, 0, regField
20cf4 73 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d s, 0, pTab->zNam
20cf5 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 e, 0);. sqlit
20cf6 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
20cf7 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 OP_String8, 0, r
20cf8 65 67 46 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 egFields+1, 0, p
20cf9 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a Idx->zName, 0);.
20cfa 20 20 20 20 72 65 67 46 32 20 3d 20 72 65 67 46 regF2 = regF
20cfb 69 65 6c 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c ields+2;. sql
20cfc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
20cfd 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d , OP_SCopy, iMem
20cfe 2c 20 72 65 67 46 32 29 3b 0a 20 20 20 20 66 6f , regF2);. fo
20cff 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 r(i=0; i<nCol; i
20d00 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
20d01 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
20d02 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 OP_String8, 0, r
20d03 65 67 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 egTemp, 0, " ",
20d04 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0);. sqlite
20d05 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
20d06 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d P_Concat, regTem
20d07 70 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 p, regF2, regF2)
20d08 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
20d09 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
20d0a 41 64 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b Add, iMem, iMem+
20d0b 69 2b 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 i+1, regTemp);.
20d0c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20d0d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 AddOp2(v, OP_Add
20d0e 49 6d 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 Imm, regTemp, -1
20d0f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
20d10 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
20d11 5f 44 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b _Divide, iMem+i+
20d12 31 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 1, regTemp, regT
20d13 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 emp);. sqli
20d14 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
20d15 20 4f 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 OP_ToInt, regTe
20d16 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 mp);. sqlit
20d17 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
20d18 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 OP_Concat, regTe
20d19 6d 70 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32 mp, regF2, regF2
20d1a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
20d1b 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
20d1c 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c , OP_MakeRecord,
20d1d 20 72 65 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 regFields, 3, r
20d1e 65 67 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29 egRec, "aaa", 0)
20d1f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
20d20 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 eAddOp2(v, OP_Ne
20d21 77 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 wRowid, iStatCur
20d22 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 , regRowid);.
20d23 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20d24 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c p3(v, OP_Insert,
20d25 20 69 53 74 61 74 43 75 72 2c 20 72 65 67 52 65 iStatCur, regRe
20d26 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 c, regRowid);.
20d27 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
20d28 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f ngeP5(v, OPFLAG_
20d29 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c APPEND);. sql
20d2a 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
20d2b 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d (v, addr);. }.}
20d2c 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
20d2d 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 code that will
20d2e 63 61 75 73 65 20 74 68 65 20 6d 6f 73 74 20 72 cause the most r
20d2f 65 63 65 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c ecent index anal
20d30 79 73 69 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 ysis to.** be la
20d31 6f 64 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e oded into intern
20d32 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 20 77 al hash tables w
20d33 68 65 72 65 20 69 73 20 63 61 6e 20 62 65 20 75 here is can be u
20d34 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 sed..*/.static v
20d35 6f 69 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 oid loadAnalysis
20d36 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
20d37 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 int iDb){. Vdbe
20d38 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 *v = sqlite3Get
20d39 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
20d3a 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c if( v ){. sql
20d3b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
20d3c 2c 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 , OP_LoadAnalysi
20d3d 73 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a s, iDb);. }.}..
20d3e 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
20d3f 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f ode that will do
20d40 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 an analysis of
20d41 61 6e 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 an entire databa
20d42 73 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 se.*/.static voi
20d43 64 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 d analyzeDatabas
20d44 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
20d45 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c int iDb){. sql
20d46 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
20d47 65 2d 3e 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 e->db;. Schema
20d48 2a 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 *pSchema = db->a
20d49 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b Db[iDb].pSchema;
20d4a 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 /* Schema of
20d4b 20 64 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f database iDb */
20d4c 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a . HashElem *k;.
20d4d 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a int iStatCur;.
20d4e 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 int iMem;.. s
20d4f 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 qlite3BeginWrite
20d50 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 Operation(pParse
20d51 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 , 0, iDb);. iSt
20d52 61 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e atCur = pParse->
20d53 6e 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 nTab++;. openSt
20d54 61 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 atTable(pParse,
20d55 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30 iDb, iStatCur, 0
20d56 29 3b 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 );. iMem = pPar
20d57 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f se->nMem+1;. fo
20d58 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 r(k=sqliteHashFi
20d59 72 73 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 rst(&pSchema->tb
20d5a 6c 48 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c lHash); k; k=sql
20d5b 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b iteHashNext(k)){
20d5c 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 . Table *pTab
20d5d 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 = (Table*)sqlit
20d5e 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 eHashData(k);.
20d5f 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c analyzeOneTabl
20d60 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 e(pParse, pTab,
20d61 69 53 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b iStatCur, iMem);
20d62 0a 20 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 . }. loadAnaly
20d63 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 sis(pParse, iDb)
20d64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
20d65 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 ate code that wi
20d66 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 ll do an analysi
20d67 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 s of a single ta
20d68 62 6c 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 ble in.** a data
20d69 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 base..*/.static
20d6a 76 6f 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c void analyzeTabl
20d6b 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
20d6c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 Table *pTab){.
20d6d 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 int iDb;. int
20d6e 69 53 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 iStatCur;.. ass
20d6f 65 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a ert( pTab!=0 );.
20d70 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
20d71 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 3BtreeHoldsAllMu
20d72 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 texes(pParse->db
20d73 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c ) );. iDb = sql
20d74 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
20d75 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 x(pParse->db, pT
20d76 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 ab->pSchema);.
20d77 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
20d78 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 eOperation(pPars
20d79 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 e, 0, iDb);. iS
20d7a 74 61 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d tatCur = pParse-
20d7b 3e 6e 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 >nTab++;. openS
20d7c 74 61 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c tatTable(pParse,
20d7d 20 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 iDb, iStatCur,
20d7e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pTab->zName);.
20d7f 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 analyzeOneTable(
20d80 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 pParse, pTab, iS
20d81 74 61 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e tatCur, pParse->
20d82 6e 4d 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 nMem+1);. loadA
20d83 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 nalysis(pParse,
20d84 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 iDb);.}../*.** G
20d85 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
20d86 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d the ANALYZE com
20d87 6d 61 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 mand. The parse
20d88 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 r calls this rou
20d89 74 69 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 tine.** when it
20d8a 72 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e recognizes an AN
20d8b 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a ALYZE command..*
20d8c 2a 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c *.** ANAL
20d8d 59 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 YZE
20d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d -
20d8f 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e - 1.** AN
20d90 41 4c 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 ALYZE <database
20d91 3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 >
20d92 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 -- 2.**
20d93 41 4e 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 ANALYZE ?<datab
20d94 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 ase>.?<tablename
20d95 3e 20 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f > -- 3.**.** Fo
20d96 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 rm 1 causes all
20d97 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 indices in all a
20d98 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
20d99 73 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 s to be analyzed
20d9a 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c ..** Form 2 anal
20d9b 79 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 yzes all indices
20d9c 20 74 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 the single data
20d9d 62 61 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 base named..** F
20d9e 6f 72 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 orm 3 analyzes a
20d9f 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 ll indices assoc
20da0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e iated with the n
20da1 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 amed table..*/.S
20da2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
20da3 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a id sqlite3Analyz
20da4 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
20da5 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 Token *pName1,
20da6 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a Token *pName2){.
20da7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
20da8 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e pParse->db;. in
20da9 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a t iDb;. int i;.
20daa 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b char *z, *zDb;
20dab 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a . Table *pTab;.
20dac 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e Token *pTableN
20dad 61 6d 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 ame;.. /* Read
20dae 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
20daf 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ema. If an error
20db0 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 occurs, leave a
20db1 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a n error message.
20db2 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e ** and code in
20db3 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 pParse and retu
20db4 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 rn NULL. */. as
20db5 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
20db6 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
20db7 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b s(pParse->db) );
20db8 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
20db9 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 !=sqlite3ReadSch
20dba 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 ema(pParse) ){.
20dbb 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a return;. }..
20dbc 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 assert( pName2
20dbd 21 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 3d 3d 30 !=0 || pName1==0
20dbe 20 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 );. if( pName1
20dbf 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f ==0 ){. /* Fo
20dc0 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20 65 rm 1: Analyze e
20dc1 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20 20 verything */.
20dc2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
20dc3 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nDb; i++){.
20dc4 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 if( i==1 ) cont
20dc5 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 inue; /* Do not
20dc6 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45 4d analyze the TEM
20dc7 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 P database */.
20dc8 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 analyzeDatab
20dc9 61 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b 0a ase(pParse, i);.
20dca 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
20dcb 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 ( pName2->n==0 )
20dcc 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a {. /* Form 2:
20dcd 20 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61 Analyze the da
20dce 74 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 tabase or table
20dcf 6e 61 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62 named */. iDb
20dd0 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 = sqlite3FindDb
20dd1 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 (db, pName1);.
20dd2 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a if( iDb>=0 ){.
20dd3 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 analyzeDat
20dd4 61 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 44 abase(pParse, iD
20dd5 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 b);. }else{.
20dd6 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 z = sqlite3
20dd7 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
20dd8 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 , pName1);.
20dd9 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 if( z ){.
20dda 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 pTab = sqlite3
20ddb 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 LocateTable(pPar
20ddc 73 65 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20 20 se, 0, z, 0);.
20ddd 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
20dde 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 ree(db, z);.
20ddf 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a if( pTab ){.
20de0 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a analyz
20de1 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 eTable(pParse, p
20de2 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a Tab);. }.
20de3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
20de4 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f }else{. /* Fo
20de5 72 6d 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74 68 rm 3: Analyze th
20de6 65 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 e fully qualifie
20de7 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a d table name */.
20de8 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 iDb = sqlite
20de9 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 3TwoPartName(pPa
20dea 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 rse, pName1, pNa
20deb 6d 65 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d 65 me2, &pTableName
20dec 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d );. if( iDb>=
20ded 30 20 29 7b 0a 20 20 20 20 20 20 7a 44 62 20 3d 0 ){. zDb =
20dee 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e db->aDb[iDb].zN
20def 61 6d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 ame;. z = s
20df0 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
20df1 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 ken(db, pTableNa
20df2 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a me);. if( z
20df3 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 ){. pTab
20df4 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
20df5 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c Table(pParse, 0,
20df6 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 z, zDb);.
20df7 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
20df8 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 db, z);.
20df9 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 if( pTab ){.
20dfa 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 analyzeTab
20dfb 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 le(pParse, pTab)
20dfc 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
20dfd 20 20 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d }. } . }
20dfe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 .}../*.** Used t
20dff 6f 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 o pass informati
20e00 6f 6e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c on from the anal
20e01 79 7a 65 72 20 72 65 61 64 65 72 20 74 68 72 6f yzer reader thro
20e02 75 67 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 ugh to the.** ca
20e03 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a llback routine..
20e04 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
20e05 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61 t analysisInfo a
20e06 6e 61 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72 nalysisInfo;.str
20e07 75 63 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f uct analysisInfo
20e08 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
20e09 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
20e0a 7a 44 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f zDatabase;.};../
20e0b 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62 61 *.** This callba
20e0c 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e ck is invoked on
20e0d 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 ce for each inde
20e0e 78 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 74 x when reading t
20e0f 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 he.** sqlite_sta
20e10 74 31 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a t1 table. .**.*
20e11 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 * argv[0] =
20e12 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 name of the inde
20e13 78 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d x.** argv[1]
20e14 20 3d 20 72 65 73 75 6c 74 73 20 6f 66 20 61 6e = results of an
20e15 61 6c 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65 alysis - on inte
20e16 67 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c ger for each col
20e17 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e umn.*/.static in
20e18 74 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 t analysisLoader
20e19 28 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e (void *pData, in
20e1a 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 t argc, char **a
20e1b 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55 rgv, char **NotU
20e1c 73 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 sed){. analysis
20e1d 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 Info *pInfo = (a
20e1e 6e 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 nalysisInfo*)pDa
20e1f 74 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e ta;. Index *pIn
20e20 64 65 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b dex;. int i, c;
20e21 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
20e22 76 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 v;. const char
20e23 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 *z;.. assert( a
20e24 72 67 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55 53 rgc==2 );. UNUS
20e25 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
20e26 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a 20 tUsed, argc);..
20e27 20 69 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c 20 if( argv==0 ||
20e28 61 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72 argv[0]==0 || ar
20e29 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 gv[1]==0 ){.
20e2a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
20e2b 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 pIndex = sqlite3
20e2c 46 69 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f 2d FindIndex(pInfo-
20e2d 3e 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70 49 >db, argv[0], pI
20e2e 6e 66 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b nfo->zDatabase);
20e2f 0a 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 . if( pIndex==0
20e30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
20e31 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67 76 ;. }. z = argv
20e32 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 [1];. for(i=0;
20e33 2a 7a 20 26 26 20 69 3c 3d 70 49 6e 64 65 78 2d *z && i<=pIndex-
20e34 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a >nColumn; i++){.
20e35 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 77 v = 0;. w
20e36 68 69 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d hile( (c=z[0])>=
20e37 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b '0' && c<='9' ){
20e38 0a 20 20 20 20 20 20 76 20 3d 20 76 2a 31 30 20 . v = v*10
20e39 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 + c - '0';.
20e3a 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 z++;. }.
20e3b 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 pIndex->aiRowEst
20e3c 5b 69 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66 28 [i] = v;. if(
20e3d 20 2a 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a *z==' ' ) z++;.
20e3e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
20e3f 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 74 68 }../*.** Load th
20e40 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 e content of the
20e41 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 sqlite_stat1 ta
20e42 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 ble into the ind
20e43 65 78 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a ex hash tables..
20e44 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20e45 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 E int sqlite3Ana
20e46 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 lysisLoad(sqlite
20e47 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 3 *db, int iDb){
20e48 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 . analysisInfo
20e49 73 49 6e 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65 sInfo;. HashEle
20e4a 6d 20 2a 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 m *i;. char *zS
20e4b 71 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 ql;. int rc;..
20e4c 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
20e4d 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 && iDb<db->nDb )
20e4e 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e ;. assert( db->
20e4f 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 aDb[iDb].pBt!=0
20e50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
20e51 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
20e52 74 65 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d tex(db->aDb[iDb]
20e53 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 .pBt) );.. /* C
20e54 6c 65 61 72 20 61 6e 79 20 70 72 69 6f 72 20 73 lear any prior s
20e55 74 61 74 69 73 74 69 63 73 20 2a 2f 0a 20 20 66 tatistics */. f
20e56 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 or(i=sqliteHashF
20e57 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 irst(&db->aDb[iD
20e58 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 b].pSchema->idxH
20e59 61 73 68 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 ash);i;i=sqliteH
20e5a 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 ashNext(i)){.
20e5b 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73 Index *pIdx = s
20e5c 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29 qliteHashData(i)
20e5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 66 ;. sqlite3Def
20e5e 61 75 6c 74 52 6f 77 45 73 74 28 70 49 64 78 29 aultRowEst(pIdx)
20e5f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
20e60 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 k to make sure t
20e61 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 he sqlite_stat1
20e62 74 61 62 6c 65 20 65 78 69 73 74 73 73 20 2a 2f table existss */
20e63 0a 20 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64 62 . sInfo.db = db
20e64 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 ;. sInfo.zDatab
20e65 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 ase = db->aDb[iD
20e66 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 b].zName;. if(
20e67 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 sqlite3FindTable
20e68 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 (db, "sqlite_sta
20e69 74 31 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 t1", sInfo.zData
20e6a 62 61 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 base)==0 ){.
20e6b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
20e6c 52 52 4f 52 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a RROR;. }... /*
20e6d 20 4c 6f 61 64 20 6e 65 77 20 73 74 61 74 69 73 Load new statis
20e6e 74 69 63 73 20 6f 75 74 20 6f 66 20 74 68 65 20 tics out of the
20e6f 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 sqlite_stat1 tab
20e70 6c 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 le */. zSql = s
20e71 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
20e72 2c 20 22 53 45 4c 45 43 54 20 69 64 78 2c 20 73 , "SELECT idx, s
20e73 74 61 74 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 tat FROM %Q.sqli
20e74 74 65 5f 73 74 61 74 31 22 2c 0a 20 20 20 20 20 te_stat1",.
20e75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20e76 20 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 sInfo.zDataba
20e77 73 65 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d se);. if( zSql=
20e78 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
20e79 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
20e7a 65 6c 73 65 7b 0a 20 20 20 20 28 76 6f 69 64 29 else{. (void)
20e7b 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
20e7c 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 (db);. rc = s
20e7d 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 qlite3_exec(db,
20e7e 7a 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f zSql, analysisLo
20e7f 61 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 ader, &sInfo, 0)
20e80 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 ;. (void)sqli
20e81 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b te3SafetyOn(db);
20e82 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
20e83 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 ee(db, zSql);.
20e84 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
20e85 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c _NOMEM ) db->mal
20e86 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
20e87 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
20e88 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }...#endif /* SQ
20e89 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a LITE_OMIT_ANALYZ
20e8a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
20e8b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6e 61 ***** End of ana
20e8c 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a lyze.c *********
20e8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20e8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20e8f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
20e90 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
20e91 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a attach.c ******
20e92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20e94 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 ****/./*.** 2003
20e95 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 April 6.**.** T
20e96 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
20e97 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
20e98 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
20e99 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
20e9a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
20e9b 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
20e9c 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
20e9d 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
20e9e 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
20e9f 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
20ea0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
20ea1 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
20ea2 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
20ea3 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
20ea4 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
20ea5 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
20ea6 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
20ea7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ea9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20eaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20eab 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
20eac 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
20ead 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 de used to imple
20eae 6d 65 6e 74 20 74 68 65 20 41 54 54 41 43 48 20 ment the ATTACH
20eaf 61 6e 64 20 44 45 54 41 43 48 20 63 6f 6d 6d 61 and DETACH comma
20eb0 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 nds..**.** $Id:
20eb1 61 74 74 61 63 68 2e 63 2c 76 20 31 2e 39 30 20 attach.c,v 1.90
20eb2 32 30 30 39 2f 30 35 2f 30 31 20 30 36 3a 31 39 2009/05/01 06:19
20eb3 3a 32 31 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :21 danielk1977
20eb4 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 Exp $.*/..#ifnde
20eb5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 f SQLITE_OMIT_AT
20eb6 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c TACH./*.** Resol
20eb7 76 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e ve an expression
20eb8 20 74 68 61 74 20 77 61 73 20 70 61 72 74 20 6f that was part o
20eb9 66 20 61 6e 20 41 54 54 41 43 48 20 6f 72 20 44 f an ATTACH or D
20eba 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e ETACH statement.
20ebb 20 54 68 69 73 0a 2a 2a 20 69 73 20 73 6c 69 67 This.** is slig
20ebc 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 20 66 htly different f
20ebd 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67 20 61 20 rom resolving a
20ebe 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78 70 72 65 normal SQL expre
20ebf 73 73 69 6f 6e 2c 20 62 65 63 61 75 73 65 20 73 ssion, because s
20ec0 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e 74 69 66 imple.** identif
20ec1 69 65 72 73 20 61 72 65 20 74 72 65 61 74 65 64 iers are treated
20ec2 20 61 73 20 73 74 72 69 6e 67 73 2c 20 6e 6f 74 as strings, not
20ec3 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c 75 6d 6e possible column
20ec4 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69 61 73 65 names or aliase
20ec5 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66 s..**.** i.e. if
20ec6 20 74 68 65 20 70 61 72 73 65 72 20 73 65 65 73 the parser sees
20ec7 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 :.**.** ATTA
20ec8 43 48 20 44 41 54 41 42 41 53 45 20 61 62 63 20 CH DATABASE abc
20ec9 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20 AS def.**.** it
20eca 74 72 65 61 74 73 20 74 68 65 20 74 77 6f 20 65 treats the two e
20ecb 78 70 72 65 73 73 69 6f 6e 73 20 61 73 20 6c 69 xpressions as li
20ecc 74 65 72 61 6c 20 73 74 72 69 6e 67 73 20 27 61 teral strings 'a
20ecd 62 63 27 20 61 6e 64 20 27 64 65 66 27 20 69 6e bc' and 'def' in
20ece 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b stead of.** look
20ecf 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20 ing for columns
20ed0 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 of the same name
20ed1 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c ..**.** This onl
20ed2 79 20 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65 y applies to the
20ed3 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45 root node of pE
20ed4 78 70 72 2c 20 73 6f 20 74 68 65 20 73 74 61 74 xpr, so the stat
20ed5 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ement:.**.**
20ed6 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45 ATTACH DATABASE
20ed7 20 61 62 63 7c 7c 64 65 66 20 41 53 20 27 64 62 abc||def AS 'db
20ed8 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61 2'.**.** will fa
20ed9 69 6c 20 62 65 63 61 75 73 65 20 6e 65 69 74 68 il because neith
20eda 65 72 20 61 62 63 20 6f 72 20 64 65 66 20 63 61 er abc or def ca
20edb 6e 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a n be resolved..*
20edc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
20edd 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 4e olveAttachExpr(N
20ede 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 61 6d ameContext *pNam
20edf 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 0a e, Expr *pExpr).
20ee0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
20ee1 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 45 ITE_OK;. if( pE
20ee2 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 xpr ){. if( p
20ee3 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20 Expr->op!=TK_ID
20ee4 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
20ee5 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
20ee6 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20 70 45 78 Names(pName, pEx
20ee7 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 pr);. if( r
20ee8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
20ee9 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f !sqlite3ExprIsCo
20eea 6e 73 74 61 6e 74 28 70 45 78 70 72 29 20 29 7b nstant(pExpr) ){
20eeb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
20eec 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d 65 2d 3e ErrorMsg(pName->
20eed 70 50 61 72 73 65 2c 20 22 69 6e 76 61 6c 69 64 pParse, "invalid
20eee 20 6e 61 6d 65 3a 20 5c 22 25 54 5c 22 22 2c 20 name: \"%T\"",
20eef 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 &pExpr->span);.
20ef0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
20ef1 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
20ef2 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
20ef3 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d pExpr->op =
20ef4 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 TK_STRING;.
20ef5 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
20ef6 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 c;.}../*.** An S
20ef7 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e QL user-function
20ef8 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 registered to d
20ef9 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e o the work of an
20efa 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e ATTACH statemen
20efb 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 t. The.** three
20efc 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 arguments to the
20efd 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 function come d
20efe 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 6e 20 irectly from an
20eff 61 74 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 attach statement
20f00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 :.**.** ATTA
20f01 43 48 20 44 41 54 41 42 41 53 45 20 78 20 41 53 CH DATABASE x AS
20f02 20 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20 y KEY z.**.**
20f03 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 SELECT sqlite
20f04 5f 61 74 74 61 63 68 28 78 2c 20 79 2c 20 7a 29 _attach(x, y, z)
20f05 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 .**.** If the op
20f06 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20 73 tional "KEY z" s
20f07 79 6e 74 61 78 20 69 73 20 6f 6d 69 74 74 65 64 yntax is omitted
20f08 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69 73 , an SQL NULL is
20f09 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a passed as the.*
20f0a 2a 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 * third argument
20f0b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
20f0c 20 61 74 74 61 63 68 46 75 6e 63 28 0a 20 20 73 attachFunc(. s
20f0d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
20f0e 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e context,. int N
20f0f 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 otUsed,. sqlite
20f10 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
20f11 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
20f12 20 72 63 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 rc = 0;. sqlit
20f13 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
20f14 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
20f15 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 le(context);. c
20f16 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
20f17 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
20f18 7a 46 69 6c 65 3b 0a 20 20 44 62 20 2a 61 4e 65 zFile;. Db *aNe
20f19 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 44 w;. char *zErrD
20f1a 79 6e 20 3d 20 30 3b 0a 0a 20 20 55 4e 55 53 45 yn = 0;.. UNUSE
20f1b 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
20f1c 73 65 64 29 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d sed);.. zFile =
20f1d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 (const char *)s
20f1e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
20f1f 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e t(argv[0]);. zN
20f20 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 ame = (const cha
20f21 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 r *)sqlite3_valu
20f22 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b e_text(argv[1]);
20f23 0a 20 20 69 66 28 20 7a 46 69 6c 65 3d 3d 30 20 . if( zFile==0
20f24 29 20 7a 46 69 6c 65 20 3d 20 22 22 3b 0a 20 20 ) zFile = "";.
20f25 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a if( zName==0 ) z
20f26 4e 61 6d 65 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a Name = "";.. /*
20f27 20 43 68 65 63 6b 20 66 6f 72 20 74 68 65 20 66 Check for the f
20f28 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 3a ollowing errors:
20f29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 2a . **. ** *
20f2a 20 54 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 Too many attach
20f2b 65 64 20 64 61 74 61 62 61 73 65 73 2c 0a 20 20 ed databases,.
20f2c 2a 2a 20 20 20 20 20 2a 20 54 72 61 6e 73 61 63 ** * Transac
20f2d 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 6f tion currently o
20f2e 70 65 6e 0a 20 20 2a 2a 20 20 20 20 20 2a 20 53 pen. ** * S
20f2f 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73 pecified databas
20f30 65 20 6e 61 6d 65 20 61 6c 72 65 61 64 79 20 62 e name already b
20f31 65 69 6e 67 20 75 73 65 64 2e 0a 20 20 2a 2f 0a eing used.. */.
20f32 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 3d 64 if( db->nDb>=d
20f33 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
20f34 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d _LIMIT_ATTACHED]
20f35 2b 32 20 29 7b 0a 20 20 20 20 7a 45 72 72 44 79 +2 ){. zErrDy
20f36 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e n = sqlite3MPrin
20f37 74 66 28 64 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 tf(db, "too many
20f38 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 attached databa
20f39 73 65 73 20 2d 20 6d 61 78 20 25 64 22 2c 20 0a ses - max %d", .
20f3a 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 db->aLimit
20f3b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 [SQLITE_LIMIT_AT
20f3c 54 41 43 48 45 44 5d 0a 20 20 20 20 29 3b 0a 20 TACHED]. );.
20f3d 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 goto attach_e
20f3e 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rror;. }. if(
20f3f 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 !db->autoCommit
20f40 29 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 3d ){. zErrDyn =
20f41 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
20f42 64 62 2c 20 22 63 61 6e 6e 6f 74 20 41 54 54 41 db, "cannot ATTA
20f43 43 48 20 64 61 74 61 62 61 73 65 20 77 69 74 68 CH database with
20f44 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 in transaction")
20f45 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 ;. goto attac
20f46 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 66 h_error;. }. f
20f47 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
20f48 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 b; i++){. cha
20f49 72 20 2a 7a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 r *z = db->aDb[i
20f4a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 61 73 73 ].zName;. ass
20f4b 65 72 74 28 20 7a 20 26 26 20 7a 4e 61 6d 65 20 ert( z && zName
20f4c 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 );. if( sqlit
20f4d 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61 e3StrICmp(z, zNa
20f4e 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 me)==0 ){.
20f4f 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 zErrDyn = sqlite
20f50 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 64 61 3MPrintf(db, "da
20f51 74 61 62 61 73 65 20 25 73 20 69 73 20 61 6c 72 tabase %s is alr
20f52 65 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a 4e eady in use", zN
20f53 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f ame);. goto
20f54 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
20f55 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
20f56 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e 65 77 20 llocate the new
20f57 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 62 2d entry in the db-
20f58 3e 61 44 62 5b 5d 20 61 72 72 61 79 20 61 6e 64 >aDb[] array and
20f59 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20 initialise the
20f5a 73 63 68 65 6d 61 0a 20 20 2a 2a 20 68 61 73 68 schema. ** hash
20f5b 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 tables.. */.
20f5c 69 66 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d if( db->aDb==db-
20f5d 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20 >aDbStatic ){.
20f5e 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 aNew = sqlite3
20f5f 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
20f60 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 sizeof(db->aDb[0
20f61 5d 29 2a 33 20 29 3b 0a 20 20 20 20 69 66 28 20 ])*3 );. if(
20f62 61 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e aNew==0 ) return
20f63 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 4e 65 ;. memcpy(aNe
20f64 77 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 w, db->aDb, size
20f65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 32 of(db->aDb[0])*2
20f66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
20f67 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 aNew = sqlite3Db
20f68 52 65 61 6c 6c 6f 63 28 64 62 2c 20 64 62 2d 3e Realloc(db, db->
20f69 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e aDb, sizeof(db->
20f6a 61 44 62 5b 30 5d 29 2a 28 64 62 2d 3e 6e 44 62 aDb[0])*(db->nDb
20f6b 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20 61 +1) );. if( a
20f6c 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b New==0 ) return;
20f6d 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 20 3d . }. db->aDb =
20f6e 20 61 4e 65 77 3b 0a 20 20 61 4e 65 77 20 3d 20 aNew;. aNew =
20f6f 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 &db->aDb[db->nDb
20f70 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 4e 65 77 ];. memset(aNew
20f71 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 61 4e 65 , 0, sizeof(*aNe
20f72 77 29 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 w));.. /* Open
20f73 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
20f74 65 2e 20 49 66 20 74 68 65 20 62 74 72 65 65 20 e. If the btree
20f75 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 is successfully
20f76 6f 70 65 6e 65 64 2c 20 75 73 65 0a 20 20 2a 2a opened, use. **
20f77 20 69 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 it to obtain th
20f78 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
20f79 61 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 a. At this point
20f7a 20 74 68 65 20 73 63 68 65 6d 61 20 6d 61 79 0a the schema may.
20f7b 20 20 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 ** or may not
20f7c 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a be initialised..
20f7d 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
20f7e 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 te3BtreeFactory(
20f7f 64 62 2c 20 7a 46 69 6c 65 2c 20 30 2c 20 53 51 db, zFile, 0, SQ
20f80 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 LITE_DEFAULT_CAC
20f81 48 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20 20 20 HE_SIZE,.
20f82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20f83 20 20 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 db->openFlag
20f84 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s | SQLITE_OPEN_
20f85 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20 20 MAIN_DB,.
20f86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20f87 20 20 20 20 26 61 4e 65 77 2d 3e 70 42 74 29 3b &aNew->pBt);
20f88 0a 20 20 64 62 2d 3e 6e 44 62 2b 2b 3b 0a 20 20 . db->nDb++;.
20f89 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 if( rc==SQLITE_C
20f8a 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a 20 20 20 ONSTRAINT ){.
20f8b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
20f8c 4f 52 3b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 OR;. zErrDyn
20f8d 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
20f8e 28 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 69 (db, "database i
20f8f 73 20 61 6c 72 65 61 64 79 20 61 74 74 61 63 68 s already attach
20f90 65 64 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ed");. }else if
20f91 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
20f92 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 ){. Pager *pP
20f93 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e ager;. aNew->
20f94 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 pSchema = sqlite
20f95 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61 3SchemaGet(db, a
20f96 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 New->pBt);. i
20f97 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d f( !aNew->pSchem
20f98 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 a ){. rc =
20f99 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
20f9a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77 }else if( aNew
20f9b 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f ->pSchema->file_
20f9c 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e format && aNew->
20f9d 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e pSchema->enc!=EN
20f9e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 7a C(db) ){. z
20f9f 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 ErrDyn = sqlite3
20fa0 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 MPrintf(db, .
20fa1 20 20 20 20 20 22 61 74 74 61 63 68 65 64 20 64 "attached d
20fa2 61 74 61 62 61 73 65 73 20 6d 75 73 74 20 75 73 atabases must us
20fa3 65 20 74 68 65 20 73 61 6d 65 20 74 65 78 74 20 e the same text
20fa4 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e encoding as main
20fa5 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 database");.
20fa6 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
20fa7 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 RROR;. }.
20fa8 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 pPager = sqlite3
20fa9 42 74 72 65 65 50 61 67 65 72 28 61 4e 65 77 2d BtreePager(aNew-
20faa 3e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 >pBt);. sqlit
20fab 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f e3PagerLockingMo
20fac 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 de(pPager, db->d
20fad 66 6c 74 4c 6f 63 6b 4d 6f 64 65 29 3b 0a 20 20 fltLockMode);.
20fae 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f sqlite3PagerJo
20faf 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72 urnalMode(pPager
20fb0 2c 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 , db->dfltJourna
20fb1 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 61 4e lMode);. }. aN
20fb2 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 ew->zName = sqli
20fb3 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
20fb4 7a 4e 61 6d 65 29 3b 0a 20 20 61 4e 65 77 2d 3e zName);. aNew->
20fb5 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 safety_level = 3
20fb6 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 ;..#if SQLITE_HA
20fb7 53 5f 43 4f 44 45 43 0a 20 20 7b 0a 20 20 20 20 S_CODEC. {.
20fb8 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 extern int sqlit
20fb9 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 73 71 e3CodecAttach(sq
20fba 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e lite3*, int, con
20fbb 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a st void*, int);.
20fbc 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 extern void
20fbd 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b sqlite3CodecGetK
20fbe 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ey(sqlite3*, int
20fbf 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b , void**, int*);
20fc0 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 . int nKey;.
20fc1 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 char *zKey;.
20fc2 20 20 20 69 6e 74 20 74 20 3d 20 73 71 6c 69 74 int t = sqlit
20fc3 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
20fc4 67 76 5b 32 5d 29 3b 0a 20 20 20 20 73 77 69 74 gv[2]);. swit
20fc5 63 68 28 20 74 20 29 7b 0a 20 20 20 20 20 20 63 ch( t ){. c
20fc6 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 ase SQLITE_INTEG
20fc7 45 52 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53 ER:. case S
20fc8 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 0a 20 20 20 QLITE_FLOAT:.
20fc9 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 zErrDyn = s
20fca 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
20fcb 62 2c 20 22 49 6e 76 61 6c 69 64 20 6b 65 79 20 b, "Invalid key
20fcc 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20 value");.
20fcd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
20fce 4f 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 OR;. brea
20fcf 6b 3b 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 k;. .
20fd0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 case SQLITE_TE
20fd1 58 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53 XT:. case S
20fd2 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 QLITE_BLOB:.
20fd3 20 20 20 20 6e 4b 65 79 20 3d 20 73 71 6c 69 74 nKey = sqlit
20fd4 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
20fd5 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 rgv[2]);.
20fd6 20 7a 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29 zKey = (char *)
20fd7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c sqlite3_value_bl
20fd8 6f 62 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 ob(argv[2]);.
20fd9 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 sqlite3Code
20fda 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d 3e cAttach(db, db->
20fdb 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65 nDb-1, zKey, nKe
20fdc 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 y);. brea
20fdd 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73 65 20 53 k;.. case S
20fde 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0a 20 20 20 20 QLITE_NULL:.
20fdf 20 20 20 20 2f 2a 20 4e 6f 20 6b 65 79 20 73 70 /* No key sp
20fe0 65 63 69 66 69 65 64 2e 20 20 55 73 65 20 74 68 ecified. Use th
20fe1 65 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 6d e key from the m
20fe2 61 69 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a ain database */.
20fe3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 sqlite3C
20fe4 6f 64 65 63 47 65 74 4b 65 79 28 64 62 2c 20 30 odecGetKey(db, 0
20fe5 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b 65 79 2c , (void**)&zKey,
20fe6 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 &nKey);.
20fe7 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 sqlite3CodecAtt
20fe8 61 63 68 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d ach(db, db->nDb-
20fe9 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 1, zKey, nKey);.
20fea 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
20feb 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
20fec 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c . /* If the fil
20fed 65 20 77 61 73 20 6f 70 65 6e 65 64 20 73 75 63 e was opened suc
20fee 63 65 73 73 66 75 6c 6c 79 2c 20 72 65 61 64 20 cessfully, read
20fef 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 the schema for t
20ff0 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 2e he new database.
20ff1 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 66 61 . ** If this fa
20ff2 69 6c 73 2c 20 6f 72 20 69 66 20 6f 70 65 6e 69 ils, or if openi
20ff3 6e 67 20 74 68 65 20 66 69 6c 65 20 66 61 69 6c ng the file fail
20ff4 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 ed, then close t
20ff5 68 65 20 66 69 6c 65 20 61 6e 64 20 0a 20 20 2a he file and . *
20ff6 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 * remove the ent
20ff7 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e ry from the db->
20ff8 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 69 2e 65 aDb[] array. i.e
20ff9 2e 20 70 75 74 20 65 76 65 72 79 74 68 69 6e 67 . put everything
20ffa 20 62 61 63 6b 20 74 68 65 20 77 61 79 0a 20 20 back the way.
20ffb 2a 2a 20 77 65 20 66 6f 75 6e 64 20 69 74 2e 0a ** we found it..
20ffc 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 */. if( rc==S
20ffd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
20ffe 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
20fff 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 etyOn(db);. s
21000 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
21001 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 72 63 20 All(db);. rc
21002 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 = sqlite3Init(db
21003 2c 20 26 7a 45 72 72 44 79 6e 29 3b 0a 20 20 20 , &zErrDyn);.
21004 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
21005 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 28 veAll(db);. (
21006 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
21007 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 7d 0a 20 tyOff(db);. }.
21008 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69 if( rc ){. i
21009 6e 74 20 69 44 62 20 3d 20 64 62 2d 3e 6e 44 62 nt iDb = db->nDb
2100a 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 - 1;. assert
2100b 28 20 69 44 62 3e 3d 32 20 29 3b 0a 20 20 20 20 ( iDb>=2 );.
2100c 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d if( db->aDb[iDb]
2100d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 73 71 .pBt ){. sq
2100e 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 lite3BtreeClose(
2100f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 db->aDb[iDb].pBt
21010 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 );. db->aDb
21011 5b 69 44 62 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 [iDb].pBt = 0;.
21012 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 db->aDb[iDb
21013 5d 2e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 ].pSchema = 0;.
21014 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
21015 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 ResetInternalSch
21016 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 ema(db, 0);.
21017 64 62 2d 3e 6e 44 62 20 3d 20 69 44 62 3b 0a 20 db->nDb = iDb;.
21018 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
21019 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 E_NOMEM || rc==S
2101a 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
2101b 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d M ){. db->m
2101c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
2101d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
2101e 46 72 65 65 28 64 62 2c 20 7a 45 72 72 44 79 6e Free(db, zErrDyn
2101f 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 44 79 6e );. zErrDyn
21020 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
21021 66 28 64 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 f(db, "out of me
21022 6d 6f 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 mory");. }els
21023 65 20 69 66 28 20 7a 45 72 72 44 79 6e 3d 3d 30 e if( zErrDyn==0
21024 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 44 79 ){. zErrDy
21025 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e n = sqlite3MPrin
21026 74 66 28 64 62 2c 20 22 75 6e 61 62 6c 65 20 74 tf(db, "unable t
21027 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 3a o open database:
21028 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 %s", zFile);.
21029 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 }. goto att
2102a 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 ach_error;. }.
2102b 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 61 74 74 . return;..att
2102c 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a 20 ach_error:. /*
2102d 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
2102e 69 66 20 77 65 20 67 65 74 20 68 65 72 65 20 2a if we get here *
2102f 2f 0a 20 20 69 66 28 20 7a 45 72 72 44 79 6e 20 /. if( zErrDyn
21030 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 ){. sqlite3_r
21031 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 esult_error(cont
21032 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c 20 2d 31 ext, zErrDyn, -1
21033 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
21034 46 72 65 65 28 64 62 2c 20 7a 45 72 72 44 79 6e Free(db, zErrDyn
21035 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 );. }. if( rc
21036 29 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 ) sqlite3_result
21037 5f 65 72 72 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 _error_code(cont
21038 65 78 74 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a ext, rc);.}../*.
21039 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 ** An SQL user-f
2103a 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 unction register
2103b 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 ed to do the wor
2103c 6b 20 6f 66 20 61 6e 20 44 45 54 41 43 48 20 73 k of an DETACH s
2103d 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a tatement. The.**
2103e 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 three arguments
2103f 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e to the function
21040 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 come directly f
21041 72 6f 6d 20 61 20 64 65 74 61 63 68 20 73 74 61 rom a detach sta
21042 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 tement:.**.**
21043 20 20 44 45 54 41 43 48 20 44 41 54 41 42 41 53 DETACH DATABAS
21044 45 20 78 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 E x.**.** SE
21045 4c 45 43 54 20 73 71 6c 69 74 65 5f 64 65 74 61 LECT sqlite_deta
21046 63 68 28 78 29 0a 2a 2f 0a 73 74 61 74 69 63 20 ch(x).*/.static
21047 76 6f 69 64 20 64 65 74 61 63 68 46 75 6e 63 28 void detachFunc(
21048 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
21049 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
2104a 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
2104b 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
2104c 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 gv.){. const ch
2104d 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e ar *zName = (con
2104e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 st char *)sqlite
2104f 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
21050 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 v[0]);. sqlite3
21051 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 *db = sqlite3_c
21052 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
21053 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 6e 74 (context);. int
21054 20 69 3b 0a 20 20 44 62 20 2a 70 44 62 20 3d 20 i;. Db *pDb =
21055 30 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 0;. char zErr[1
21056 32 38 5d 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 28];.. UNUSED_P
21057 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
21058 29 3b 0a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d );.. if( zName=
21059 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b =0 ) zName = "";
2105a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
2105b 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
2105c 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b pDb = &db->aDb[
2105d 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d i];. if( pDb-
2105e 3e 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e >pBt==0 ) contin
2105f 75 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ue;. if( sqli
21060 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e te3StrICmp(pDb->
21061 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 zName, zName)==0
21062 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 ) break;. }..
21063 20 69 66 28 20 69 3e 3d 64 62 2d 3e 6e 44 62 20 if( i>=db->nDb
21064 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
21065 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
21066 45 72 72 29 2c 7a 45 72 72 2c 20 22 6e 6f 20 73 Err),zErr, "no s
21067 75 63 68 20 64 61 74 61 62 61 73 65 3a 20 25 73 uch database: %s
21068 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 ", zName);. g
21069 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 oto detach_error
2106a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 32 20 ;. }. if( i<2
2106b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
2106c 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
2106d 45 72 72 29 2c 7a 45 72 72 2c 20 22 63 61 6e 6e Err),zErr, "cann
2106e 6f 74 20 64 65 74 61 63 68 20 64 61 74 61 62 61 ot detach databa
2106f 73 65 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a se %s", zName);.
21070 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f goto detach_
21071 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 error;. }. if(
21072 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 !db->autoCommit
21073 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
21074 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
21075 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 zErr), zErr,.
21076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21077 20 20 22 63 61 6e 6e 6f 74 20 44 45 54 41 43 48 "cannot DETACH
21078 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e database within
21079 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a transaction");.
2107a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f goto detach_
2107b 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 error;. }. if(
2107c 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
2107d 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e nReadTrans(pDb->
2107e 70 42 74 29 20 7c 7c 20 73 71 6c 69 74 65 33 42 pBt) || sqlite3B
2107f 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 70 treeIsInBackup(p
21080 44 62 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 Db->pBt) ){.
21081 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
21082 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 (sizeof(zErr),zE
21083 72 72 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 rr, "database %s
21084 20 69 73 20 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 is locked", zNa
21085 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 me);. goto de
21086 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a tach_error;. }.
21087 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 . sqlite3BtreeC
21088 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a lose(pDb->pBt);.
21089 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a pDb->pBt = 0;.
2108a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d pDb->pSchema =
2108b 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 0;. sqlite3Res
2108c 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 etInternalSchema
2108d 28 64 62 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 (db, 0);. retur
2108e 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 72 72 6f 72 n;..detach_error
2108f 3a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 :. sqlite3_resu
21090 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 lt_error(context
21091 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a , zErr, -1);.}..
21092 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 /*.** This proce
21093 64 75 72 65 20 67 65 6e 65 72 61 74 65 73 20 56 dure generates V
21094 44 42 45 20 63 6f 64 65 20 66 6f 72 20 61 20 73 DBE code for a s
21095 69 6e 67 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e ingle invocation
21096 20 6f 66 20 65 69 74 68 65 72 20 74 68 65 0a 2a of either the.*
21097 2a 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28 * sqlite_detach(
21098 29 20 6f 72 20 73 71 6c 69 74 65 5f 61 74 74 61 ) or sqlite_atta
21099 63 68 28 29 20 53 51 4c 20 75 73 65 72 20 66 75 ch() SQL user fu
2109a 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 nctions..*/.stat
2109b 69 63 20 76 6f 69 64 20 63 6f 64 65 41 74 74 61 ic void codeAtta
2109c 63 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ch(. Parse *pPa
2109d 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 rse, /* Th
2109e 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 e parser context
2109f 20 2a 2f 0a 20 20 69 6e 74 20 74 79 70 65 2c 20 */. int type,
210a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69 /* Ei
210a1 74 68 65 72 20 53 51 4c 49 54 45 5f 41 54 54 41 ther SQLITE_ATTA
210a2 43 48 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 54 CH or SQLITE_DET
210a3 41 43 48 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 ACH */. FuncDef
210a4 20 2a 70 46 75 6e 63 2c 20 20 20 20 20 20 2f 2a *pFunc, /*
210a5 20 46 75 6e 63 44 65 66 20 77 72 61 70 70 65 72 FuncDef wrapper
210a6 20 66 6f 72 20 64 65 74 61 63 68 46 75 6e 63 28 for detachFunc(
210a7 29 20 6f 72 20 61 74 74 61 63 68 46 75 6e 63 28 ) or attachFunc(
210a8 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41 75 ) */. Expr *pAu
210a9 74 68 41 72 67 2c 20 20 20 20 20 20 2f 2a 20 45 thArg, /* E
210aa 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 73 xpression to pas
210ab 73 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 s to authorizati
210ac 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 on callback */.
210ad 20 45 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d 65 Expr *pFilename
210ae 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 , /* Name of
210af 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
210b0 2f 0a 20 20 45 78 70 72 20 2a 70 44 62 6e 61 6d /. Expr *pDbnam
210b1 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 e, /* Name
210b2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
210b3 20 74 6f 20 75 73 65 20 69 6e 74 65 72 6e 61 6c to use internal
210b4 6c 79 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4b ly */. Expr *pK
210b5 65 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ey /*
210b6 44 61 74 61 62 61 73 65 20 6b 65 79 20 66 6f 72 Database key for
210b7 20 65 6e 63 72 79 70 74 69 6f 6e 20 65 78 74 65 encryption exte
210b8 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e nsion */.){. in
210b9 74 20 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 t rc;. NameCont
210ba 65 78 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64 62 ext sName;. Vdb
210bb 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 2a e *v;. sqlite3*
210bc 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
210bd 3b 0a 20 20 69 6e 74 20 72 65 67 41 72 67 73 3b ;. int regArgs;
210be 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
210bf 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
210c0 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 64 62 ION. assert( db
210c1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c ->mallocFailed |
210c2 7c 20 70 41 75 74 68 41 72 67 20 29 3b 0a 20 20 | pAuthArg );.
210c3 69 66 28 20 70 41 75 74 68 41 72 67 20 29 7b 0a if( pAuthArg ){.
210c4 20 20 20 20 63 68 61 72 20 2a 7a 41 75 74 68 41 char *zAuthA
210c5 72 67 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 rg = sqlite3Name
210c6 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70 FromToken(db, &p
210c7 41 75 74 68 41 72 67 2d 3e 73 70 61 6e 29 3b 0a AuthArg->span);.
210c8 20 20 20 20 69 66 28 20 21 7a 41 75 74 68 41 72 if( !zAuthAr
210c9 67 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 g ){. goto
210ca 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 attach_end;.
210cb 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
210cc 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
210cd 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74 68 41 se, type, zAuthA
210ce 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 rg, 0, 0);. s
210cf 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
210d0 20 7a 41 75 74 68 41 72 67 29 3b 0a 20 20 20 20 zAuthArg);.
210d1 69 66 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b if(rc!=SQLITE_OK
210d2 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 ){. goto a
210d3 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 7d ttach_end;. }
210d4 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
210d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
210d6 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 6d RIZATION */.. m
210d7 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20 30 2c emset(&sName, 0,
210d8 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74 sizeof(NameCont
210d9 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e 70 ext));. sName.p
210da 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
210db 0a 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 . if( . SQ
210dc 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
210dd 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 esolveAttachExpr
210de 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e 61 (&sName, pFilena
210df 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 me)) ||. SQ
210e0 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
210e1 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 esolveAttachExpr
210e2 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d 65 (&sName, pDbname
210e3 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49 )) ||. SQLI
210e4 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 TE_OK!=(rc = res
210e5 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26 olveAttachExpr(&
210e6 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20 20 sName, pKey)).
210e7 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e ){. pParse->n
210e8 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 Err++;. goto
210e9 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d 0a attach_end;. }.
210ea 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 . v = sqlite3Ge
210eb 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
210ec 20 72 65 67 41 72 67 73 20 3d 20 73 71 6c 69 74 regArgs = sqlit
210ed 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 e3GetTempRange(p
210ee 50 61 72 73 65 2c 20 34 29 3b 0a 20 20 73 71 6c Parse, 4);. sql
210ef 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
210f0 72 73 65 2c 20 70 46 69 6c 65 6e 61 6d 65 2c 20 rse, pFilename,
210f1 72 65 67 41 72 67 73 29 3b 0a 20 20 73 71 6c 69 regArgs);. sqli
210f2 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 te3ExprCode(pPar
210f3 73 65 2c 20 70 44 62 6e 61 6d 65 2c 20 72 65 67 se, pDbname, reg
210f4 41 72 67 73 2b 31 29 3b 0a 20 20 73 71 6c 69 74 Args+1);. sqlit
210f5 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 e3ExprCode(pPars
210f6 65 2c 20 70 4b 65 79 2c 20 72 65 67 41 72 67 73 e, pKey, regArgs
210f7 2b 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 +2);.. assert(
210f8 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 v || db->mallocF
210f9 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 76 ailed );. if( v
210fa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
210fb 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
210fc 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65 67 Function, 0, reg
210fd 41 72 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e 41 Args+3-pFunc->nA
210fe 72 67 2c 20 72 65 67 41 72 67 73 2b 33 29 3b 0a rg, regArgs+3);.
210ff 20 20 20 20 61 73 73 65 72 74 28 20 70 46 75 6e assert( pFun
21100 63 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 28 c->nArg==-1 || (
21101 70 46 75 6e 63 2d 3e 6e 41 72 67 26 30 78 66 66 pFunc->nArg&0xff
21102 29 3d 3d 70 46 75 6e 63 2d 3e 6e 41 72 67 20 29 )==pFunc->nArg )
21103 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21104 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 eChangeP5(v, (u8
21105 29 28 70 46 75 6e 63 2d 3e 6e 41 72 67 29 29 3b )(pFunc->nArg));
21106 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21107 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 ChangeP4(v, -1,
21108 28 63 68 61 72 20 2a 29 70 46 75 6e 63 2c 20 50 (char *)pFunc, P
21109 34 5f 46 55 4e 43 44 45 46 29 3b 0a 0a 20 20 20 4_FUNCDEF);..
2110a 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 /* Code an OP_E
2110b 78 70 69 72 65 2e 20 46 6f 72 20 61 6e 20 41 54 xpire. For an AT
2110c 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2c 20 TACH statement,
2110d 73 65 74 20 50 31 20 74 6f 20 74 72 75 65 20 28 set P1 to true (
2110e 65 78 70 69 72 65 20 74 68 69 73 0a 20 20 20 20 expire this.
2110f 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c ** statement onl
21110 79 29 2e 20 46 6f 72 20 44 45 54 41 43 48 2c 20 y). For DETACH,
21111 73 65 74 20 69 74 20 74 6f 20 66 61 6c 73 65 20 set it to false
21112 28 65 78 70 69 72 65 20 61 6c 6c 20 65 78 69 73 (expire all exis
21113 74 69 6e 67 0a 20 20 20 20 2a 2a 20 73 74 61 74 ting. ** stat
21114 65 6d 65 6e 74 73 29 2e 0a 20 20 20 20 2a 2f 0a ements).. */.
21115 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21116 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69 ddOp1(v, OP_Expi
21117 72 65 2c 20 28 74 79 70 65 3d 3d 53 51 4c 49 54 re, (type==SQLIT
21118 45 5f 41 54 54 41 43 48 29 29 3b 0a 20 20 7d 0a E_ATTACH));. }.
21119 20 20 0a 61 74 74 61 63 68 5f 65 6e 64 3a 0a 20 .attach_end:.
2111a 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
2111b 74 65 28 64 62 2c 20 70 46 69 6c 65 6e 61 6d 65 te(db, pFilename
2111c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
2111d 44 65 6c 65 74 65 28 64 62 2c 20 70 44 62 6e 61 Delete(db, pDbna
2111e 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 me);. sqlite3Ex
2111f 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4b 65 prDelete(db, pKe
21120 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c y);.}../*.** Cal
21121 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 led by the parse
21122 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 20 44 r to compile a D
21123 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e ETACH statement.
21124 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 .**.** DETAC
21125 48 20 70 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c H pDbname.*/.SQL
21126 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21127 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 sqlite3Detach(P
21128 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
21129 70 72 20 2a 70 44 62 6e 61 6d 65 29 7b 0a 20 20 pr *pDbname){.
2112a 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20 64 static FuncDef d
2112b 65 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 etach_func = {.
2112c 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 1,
2112d 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a /* nArg */.
2112e 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c SQLITE_UTF8,
2112f 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e /* iPrefEn
21130 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 c */. 0,
21131 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c /* fl
21132 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ags */. 0,
21133 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21134 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 20 20 pUserData */.
21135 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
21136 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 /* pNext */.
21137 20 20 20 64 65 74 61 63 68 46 75 6e 63 2c 20 20 detachFunc,
21138 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f /* xFunc */
21139 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
2113a 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 70 20 /* xStep
2113b 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
2113c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e /* xFin
2113d 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73 71 alize */. "sq
2113e 6c 69 74 65 5f 64 65 74 61 63 68 22 2c 20 20 2f lite_detach", /
2113f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 * zName */. 0
21140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21141 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20 7d /* pHash */. }
21142 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28 70 ;. codeAttach(p
21143 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 Parse, SQLITE_DE
21144 54 41 43 48 2c 20 26 64 65 74 61 63 68 5f 66 75 TACH, &detach_fu
21145 6e 63 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c 20 nc, pDbname, 0,
21146 30 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 0, pDbname);.}..
21147 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 /*.** Called by
21148 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f the parser to co
21149 6d 70 69 6c 65 20 61 6e 20 41 54 54 41 43 48 20 mpile an ATTACH
2114a 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a statement..**.**
2114b 20 20 20 20 20 41 54 54 41 43 48 20 70 20 41 53 ATTACH p AS
2114c 20 70 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b 65 pDbname KEY pKe
2114d 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 y.*/.SQLITE_PRIV
2114e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
2114f 41 74 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 Attach(Parse *pP
21150 61 72 73 65 2c 20 45 78 70 72 20 2a 70 2c 20 45 arse, Expr *p, E
21151 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45 78 xpr *pDbname, Ex
21152 70 72 20 2a 70 4b 65 79 29 7b 0a 20 20 73 74 61 pr *pKey){. sta
21153 74 69 63 20 46 75 6e 63 44 65 66 20 61 74 74 61 tic FuncDef atta
21154 63 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20 ch_func = {.
21155 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3,
21156 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20 /* nArg */.
21157 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 SQLITE_UTF8,
21158 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a /* iPrefEnc *
21159 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
2115a 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 /* flags
2115b 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 */. 0,
2115c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 /* pUs
2115d 65 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c erData */. 0,
2115e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2115f 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 /* pNext */.
21160 61 74 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20 attachFunc,
21161 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20 /* xFunc */.
21162 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
21163 20 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a /* xStep */.
21164 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 0,
21165 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 /* xFinali
21166 7a 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74 ze */. "sqlit
21167 65 5f 61 74 74 61 63 68 22 2c 20 20 2f 2a 20 7a e_attach", /* z
21168 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20 20 Name */. 0
21169 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2116a 20 70 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 pHash */. };.
2116b 20 63 6f 64 65 41 74 74 61 63 68 28 70 50 61 72 codeAttach(pPar
2116c 73 65 2c 20 53 51 4c 49 54 45 5f 41 54 54 41 43 se, SQLITE_ATTAC
2116d 48 2c 20 26 61 74 74 61 63 68 5f 66 75 6e 63 2c H, &attach_func,
2116e 20 70 2c 20 70 2c 20 70 44 62 6e 61 6d 65 2c 20 p, p, pDbname,
2116f 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 pKey);.}.#endif
21170 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 /* SQLITE_OMIT_A
21171 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 TTACH */../*.**
21172 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 44 62 46 Initialize a DbF
21173 69 78 65 72 20 73 74 72 75 63 74 75 72 65 2e 20 ixer structure.
21174 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 This routine mu
21175 73 74 20 62 65 20 63 61 6c 6c 65 64 20 70 72 69 st be called pri
21176 6f 72 0a 2a 2a 20 74 6f 20 70 61 73 73 69 6e 67 or.** to passing
21177 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 74 the structure t
21178 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 71 6c o one of the sql
21179 69 74 65 46 69 78 41 41 41 41 28 29 20 72 6f 75 iteFixAAAA() rou
2117a 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 0a tines below..**.
2117b 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 ** The return va
2117c 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 77 68 lue indicates wh
2117d 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 66 69 78 ether or not fix
2117e 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 ation is require
2117f 64 2e 20 20 54 52 55 45 0a 2a 2a 20 6d 65 61 6e d. TRUE.** mean
21180 73 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 s we do need to
21181 66 69 78 20 74 68 65 20 64 61 74 61 62 61 73 65 fix the database
21182 20 72 65 66 65 72 65 6e 63 65 73 2c 20 46 41 4c references, FAL
21183 53 45 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e SE means we do n
21184 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ot..*/.SQLITE_PR
21185 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21186 33 46 69 78 49 6e 69 74 28 0a 20 20 44 62 46 69 3FixInit(. DbFi
21187 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 xer *pFix,
21188 2f 2a 20 54 68 65 20 66 69 78 65 72 20 74 6f 20 /* The fixer to
21189 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a be initialized *
2118a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 /. Parse *pPars
2118b 65 2c 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 e, /* Error
2118c 20 6d 65 73 73 61 67 65 73 20 77 69 6c 6c 20 62 messages will b
2118d 65 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a e written here *
2118e 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 /. int iDb,
2118f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
21190 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 is the database
21191 74 68 61 74 20 6d 75 73 74 20 62 65 20 75 73 65 that must be use
21192 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 d */. const cha
21193 72 20 2a 7a 54 79 70 65 2c 20 20 2f 2a 20 22 76 r *zType, /* "v
21194 69 65 77 22 2c 20 22 74 72 69 67 67 65 72 22 2c iew", "trigger",
21195 20 6f 72 20 22 69 6e 64 65 78 22 20 2a 2f 0a 20 or "index" */.
21196 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e const Token *pN
21197 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 ame /* Name of
21198 74 68 65 20 76 69 65 77 2c 20 74 72 69 67 67 65 the view, trigge
21199 72 2c 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 29 r, or index */.)
2119a 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
2119b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 44 .. if( NEVER(iD
2119c 62 3c 30 29 20 7c 7c 20 69 44 62 3d 3d 31 20 29 b<0) || iDb==1 )
2119d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 return 0;. db
2119e 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
2119f 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e assert( db->nDb>
211a0 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 iDb );. pFix->p
211a1 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
211a2 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 pFix->zDb = db
211a3 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
211a4 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 ;. pFix->zType
211a5 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d = zType;. pFix-
211a6 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a >pName = pName;.
211a7 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f return 1;.}../
211a8 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
211a9 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e ng set of routin
211aa 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 es walk through
211ab 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 61 the parse tree a
211ac 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 nd assign.** a s
211ad 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65 pecific database
211ae 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 to all table re
211af 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20 74 ferences where t
211b0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 he database name
211b1 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73 .** was left uns
211b2 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 pecified in the
211b3 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 original SQL sta
211b4 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69 tement. The pFi
211b5 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d x structure.** m
211b6 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e ust have been in
211b7 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70 itialized by a p
211b8 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c rior call to sql
211b9 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a ite3FixInit()..*
211ba 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
211bb 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 nes are used to
211bc 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 make sure that a
211bd 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72 n index, trigger
211be 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 , or.** view in
211bf 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 one database doe
211c0 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f s not refer to o
211c1 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66 66 bjects in a diff
211c2 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a erent database..
211c3 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 ** (Exception: i
211c4 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72 73 ndices, triggers
211c5 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74 , and views in t
211c6 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
211c7 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 are.** allowed
211c8 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74 to refer to anyt
211c9 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66 hing.) If a ref
211ca 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63 erence is explic
211cb 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 itly made.** to
211cc 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 an object in a d
211cd 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 ifferent databas
211ce 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 e, an error mess
211cf 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a age is added to.
211d0 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d ** pParse->zErrM
211d1 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75 sg and these rou
211d2 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e tines return non
211d3 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79 -zero. If every
211d4 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 thing.** checks
211d5 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69 out, these routi
211d6 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f nes return 0..*/
211d7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
211d8 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 int sqlite3FixSr
211d9 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 cList(. DbFixer
211da 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a *pFix, /*
211db 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 Context of the
211dc 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 fixation */. Sr
211dd 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 cList *pList
211de 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 /* The Source
211df 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61 list to check a
211e0 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a nd modify */.){.
211e1 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 int i;. const
211e2 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 char *zDb;. st
211e3 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
211e4 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 m *pItem;.. if(
211e5 20 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 NEVER(pList==0)
211e6 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a ) return 0;. z
211e7 44 62 20 3d 20 70 46 69 78 2d 3e 7a 44 62 3b 0a Db = pFix->zDb;.
211e8 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d for(i=0, pItem
211e9 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 =pList->a; i<pLi
211ea 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 st->nSrc; i++, p
211eb 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 Item++){. if(
211ec 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 pItem->zDatabas
211ed 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 e==0 ){. pI
211ee 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d tem->zDatabase =
211ef 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
211f0 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2d 3e 64 (pFix->pParse->d
211f1 62 2c 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65 6c b, zDb);. }el
211f2 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 se if( sqlite3St
211f3 72 49 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44 61 rICmp(pItem->zDa
211f4 74 61 62 61 73 65 2c 7a 44 62 29 21 3d 30 20 29 tabase,zDb)!=0 )
211f5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
211f6 72 72 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70 50 rrorMsg(pFix->pP
211f7 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 22 arse,. "
211f8 25 73 20 25 54 20 63 61 6e 6e 6f 74 20 72 65 66 %s %T cannot ref
211f9 65 72 65 6e 63 65 20 6f 62 6a 65 63 74 73 20 69 erence objects i
211fa 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 0a n database %s",.
211fb 20 20 20 20 20 20 20 20 20 70 46 69 78 2d 3e 7a pFix->z
211fc 54 79 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61 6d Type, pFix->pNam
211fd 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 e, pItem->zDatab
211fe 61 73 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ase);. retu
211ff 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 69 66 20 rn 1;. }.#if
21200 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
21201 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 OMIT_VIEW) || !d
21202 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
21203 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 IT_TRIGGER).
21204 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 if( sqlite3FixSe
21205 6c 65 63 74 28 70 46 69 78 2c 20 70 49 74 65 6d lect(pFix, pItem
21206 2d 3e 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 ->pSelect) ) ret
21207 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 73 urn 1;. if( s
21208 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 qlite3FixExpr(pF
21209 69 78 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 20 ix, pItem->pOn)
2120a 29 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 ) return 1;.#end
2120b 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 if. }. return
2120c 30 3b 0a 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 0;.}.#if !define
2120d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
2120e 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 EW) || !defined(
2120f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
21210 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 GER).SQLITE_PRIV
21211 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
21212 69 78 53 65 6c 65 63 74 28 0a 20 20 44 62 46 69 ixSelect(. DbFi
21213 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 xer *pFix,
21214 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 /* Context of t
21215 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 he fixation */.
21216 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
21217 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
21218 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f ECT statement to
21219 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 be fixed to one
2121a 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a database */.){.
2121b 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 while( pSelect
2121c 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
2121d 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 te3FixExprList(p
2121e 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 Fix, pSelect->pE
2121f 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 List) ){. r
21220 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
21221 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
21222 78 53 72 63 4c 69 73 74 28 70 46 69 78 2c 20 70 xSrcList(pFix, p
21223 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 20 29 7b Select->pSrc) ){
21224 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
21225 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
21226 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 qlite3FixExpr(pF
21227 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 ix, pSelect->pWh
21228 65 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ere) ){. re
21229 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
2122a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 if( sqlite3Fix
2122b 45 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 Expr(pFix, pSele
2122c 63 74 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b 0a ct->pHaving) ){.
2122d 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
2122e 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 }. pSelec
2122f 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 t = pSelect->pPr
21230 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ior;. }. retur
21231 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 n 0;.}.SQLITE_PR
21232 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
21233 33 46 69 78 45 78 70 72 28 0a 20 20 44 62 46 69 3FixExpr(. DbFi
21234 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f xer *pFix, /
21235 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 * Context of the
21236 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 fixation */. E
21237 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 xpr *pExpr
21238 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 /* The express
21239 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 ion to be fixed
2123a 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 to one database
2123b 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 */.){. while( p
2123c 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 Expr ){. if(
2123d 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
2123e 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b ty(pExpr, EP_Tok
2123f 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f enOnly|EP_SpanTo
21240 6b 65 6e 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 ken) ) break;.
21241 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
21242 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21243 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
21244 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 if( sqlite3F
21245 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 ixSelect(pFix, p
21246 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 Expr->x.pSelect)
21247 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 ) return 1;.
21248 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 }else{. if
21249 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 ( sqlite3FixExpr
2124a 4c 69 73 74 28 70 46 69 78 2c 20 70 45 78 70 72 List(pFix, pExpr
2124b 2d 3e 78 2e 70 4c 69 73 74 29 20 29 20 72 65 74 ->x.pList) ) ret
2124c 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
2124d 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 if( sqlite3FixE
2124e 78 70 72 28 70 46 69 78 2c 20 70 45 78 70 72 2d xpr(pFix, pExpr-
2124f 3e 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 >pRight) ){.
21250 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
21251 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 45 }. pExpr = pE
21252 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a xpr->pLeft;. }.
21253 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 return 0;.}.SQ
21254 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
21255 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c sqlite3FixExprL
21256 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a ist(. DbFixer *
21257 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e pFix, /* Con
21258 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 text of the fixa
21259 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 tion */. ExprLi
2125a 73 74 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a 20 st *pList /*
2125b 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 The expression t
2125c 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e o be fixed to on
2125d 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b e database */.){
2125e 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
2125f 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
21260 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 *pItem;. if( p
21261 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e List==0 ) return
21262 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 0;. for(i=0, p
21263 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 Item=pList->a; i
21264 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 <pList->nExpr; i
21265 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
21266 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 if( sqlite3Fix
21267 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d Expr(pFix, pItem
21268 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 ->pExpr) ){.
21269 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
2126a 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
2126b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ;.}.#endif..#ifn
2126c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
2126d 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50 TRIGGER.SQLITE_P
2126e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
2126f 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70 e3FixTriggerStep
21270 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 (. DbFixer *pFi
21271 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 x, /* Contex
21272 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f t of the fixatio
21273 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 n */. TriggerSt
21274 65 70 20 2a 70 53 74 65 70 20 2f 2a 20 54 68 65 ep *pStep /* The
21275 20 74 72 69 67 67 65 72 20 73 74 65 70 20 62 65 trigger step be
21276 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 fixed to one da
21277 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 tabase */.){. w
21278 68 69 6c 65 28 20 70 53 74 65 70 20 29 7b 0a 20 hile( pStep ){.
21279 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 if( sqlite3Fi
2127a 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 53 xSelect(pFix, pS
2127b 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b tep->pSelect) ){
2127c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
2127d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
2127e 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 qlite3FixExpr(pF
2127f 69 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 ix, pStep->pWher
21280 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 e) ){. retu
21281 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
21282 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 if( sqlite3FixEx
21283 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53 74 prList(pFix, pSt
21284 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 20 29 ep->pExprList) )
21285 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
21286 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74 65 ;. }. pSte
21287 70 20 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78 74 p = pStep->pNext
21288 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
21289 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a ;.}.#endif../***
2128a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
2128b 6f 66 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a of attach.c ****
2128c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2128d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2128e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
2128f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
21290 6e 20 66 69 6c 65 20 61 75 74 68 2e 63 20 2a 2a n file auth.c **
21291 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21292 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21293 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
21294 2a 20 32 30 30 33 20 4a 61 6e 75 61 72 79 20 31 * 2003 January 1
21295 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 1.**.** The auth
21296 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
21297 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
21298 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
21299 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
2129a 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
2129b 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
2129c 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
2129d 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
2129e 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
2129f 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
212a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
212a1 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
212a2 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
212a3 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
212a4 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
212a5 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
212a6 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
212a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
212ab 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
212ac 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 ntains code used
212ad 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
212ae 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 e sqlite3_set_au
212af 74 68 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41 50 thorizer().** AP
212b0 49 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69 74 I. This facilit
212b1 79 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c y is an optional
212b2 20 66 65 61 74 75 72 65 20 6f 66 20 74 68 65 20 feature of the
212b3 6c 69 62 72 61 72 79 2e 20 20 45 6d 62 65 64 64 library. Embedd
212b4 65 64 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74 68 ed.** systems th
212b5 61 74 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 at do not need t
212b6 68 69 73 20 66 61 63 69 6c 69 74 79 20 6d 61 79 his facility may
212b7 20 6f 6d 69 74 20 69 74 20 62 79 20 72 65 63 6f omit it by reco
212b8 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6c mpiling.** the l
212b9 69 62 72 61 72 79 20 77 69 74 68 20 2d 44 53 51 ibrary with -DSQ
212ba 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
212bb 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a 20 IZATION=1.**.**
212bc 24 49 64 3a 20 61 75 74 68 2e 63 2c 76 20 31 2e $Id: auth.c,v 1.
212bd 33 31 20 32 30 30 39 2f 30 35 2f 30 34 20 31 38 31 2009/05/04 18
212be 3a 30 31 3a 34 30 20 64 72 68 20 45 78 70 20 24 :01:40 drh Exp $
212bf 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f .*/../*.** All o
212c0 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 f the code in th
212c1 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f is file may be o
212c2 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e 69 mitted by defini
212c3 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d ng a single.** m
212c4 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 acro..*/.#ifndef
212c5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
212c6 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a 2a HORIZATION../*.*
212c7 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20 74 * Set or clear t
212c8 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 he access author
212c9 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ization function
212ca 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63 65 ..**.** The acce
212cb 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e ss authorization
212cc 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 20 function is be
212cd 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68 called during th
212ce 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a 2a e compilation.**
212cf 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66 79 phase to verify
212d0 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20 68 that the user h
212d1 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20 77 as read and/or w
212d2 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72 6d rite access perm
212d3 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 72 ission on.** var
212d4 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20 74 ious fields of t
212d5 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
212d6 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
212d7 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e to the auth fun
212d8 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63 6f ction.** is a co
212d9 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61 72 py of the 3rd ar
212da 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 72 gument to this r
212db 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65 63 outine. The sec
212dc 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 ond argument.**
212dd 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 to the auth func
212de 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20 74 tion is one of t
212df 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a hese constants:.
212e0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 **.** SQLI
212e1 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 0a TE_CREATE_INDEX.
212e2 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
212e3 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a 20 CREATE_TABLE.**
212e4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 SQLITE_CRE
212e5 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a ATE_TEMP_INDEX.*
212e6 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 * SQLITE_C
212e7 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 REATE_TEMP_TABLE
212e8 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
212e9 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 _CREATE_TEMP_TRI
212ea 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 GGER.** SQ
212eb 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 LITE_CREATE_TEMP
212ec 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 _VIEW.** S
212ed 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 QLITE_CREATE_TRI
212ee 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 GGER.** SQ
212ef 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 LITE_CREATE_VIEW
212f0 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
212f1 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 20 _DELETE.**
212f2 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 SQLITE_DROP_IND
212f3 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 EX.** SQLI
212f4 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a 2a TE_DROP_TABLE.**
212f5 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 SQLITE_DR
212f6 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a OP_TEMP_INDEX.**
212f7 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 SQLITE_DR
212f8 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a OP_TEMP_TABLE.**
212f9 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 SQLITE_DR
212fa 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a OP_TEMP_TRIGGER.
212fb 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
212fc 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a 2a DROP_TEMP_VIEW.*
212fd 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 * SQLITE_D
212fe 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 ROP_TRIGGER.**
212ff 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 SQLITE_DROP
21300 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 _VIEW.** S
21301 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a 20 QLITE_INSERT.**
21302 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52 41 SQLITE_PRA
21303 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c GMA.** SQL
21304 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20 20 ITE_READ.**
21305 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 0a SQLITE_SELECT.
21306 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f ** SQLITE_
21307 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 TRANSACTION.**
21308 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44 41 SQLITE_UPDA
21309 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 TE.**.** The thi
2130a 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 rd and fourth ar
2130b 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 61 guments to the a
2130c 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 uth function are
2130d 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 the name of.**
2130e 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68 the table and th
2130f 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 72 e column that ar
21310 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 64 e being accessed
21311 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e 63 . The auth func
21312 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 tion.** should r
21313 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c eturn either SQL
21314 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 44 ITE_OK, SQLITE_D
21315 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f 49 ENY, or SQLITE_I
21316 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53 51 GNORE. If.** SQ
21317 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
21318 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 ned, it means th
21319 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c 6c at access is all
2131a 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44 45 owed. SQLITE_DE
2131b 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 NY.** means that
2131c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
2131d 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72 75 nt will never-ru
2131e 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 5f n - the sqlite3_
2131f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20 77 exec() call.** w
21320 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68 20 ill return with
21321 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54 an error. SQLIT
21322 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 E_IGNORE means t
21323 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 hat the SQL stat
21324 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 ement.** should
21325 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74 73 run but attempts
21326 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70 65 to read the spe
21327 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77 69 cified column wi
21328 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a ll return NULL.*
21329 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20 74 * and attempts t
2132a 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c 75 o write the colu
2132b 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f 72 mn will be ignor
2132c 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e ed..**.** Settin
2132d 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 g the auth funct
2132e 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61 ion to NULL disa
2132f 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e 20 bles this hook.
21330 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 The default.**
21331 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 61 setting of the a
21332 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 uth function is
21333 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f NULL..*/.SQLITE_
21334 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
21335 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a set_authorizer(.
21336 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 sqlite3 *db,.
21337 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f int (*xAuth)(vo
21338 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 id*,int,const ch
21339 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
2133a 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
2133b 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 t char*),. void
2133c 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69 *pArg.){. sqli
2133d 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
2133e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 db->mutex);. db
2133f 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b ->xAuth = xAuth;
21340 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67 20 . db->pAuthArg
21341 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 = pArg;. sqlite
21342 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 3ExpirePreparedS
21343 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 tatements(db);.
21344 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
21345 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
21346 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
21347 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 _OK;.}../*.** Wr
21348 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ite an error mes
21349 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73 65 sage into pParse
2134a 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20 65 ->zErrMsg that e
2134b 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68 65 xplains that the
2134c 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69 65 .** user-supplie
2134d 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 d authorization
2134e 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 function returne
2134f 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c d an illegal val
21350 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ue..*/.static vo
21351 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 id sqliteAuthBad
21352 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73 65 ReturnCode(Parse
21353 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c *pParse){. sql
21354 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
21355 72 73 65 2c 20 22 61 75 74 68 6f 72 69 7a 65 72 rse, "authorizer
21356 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 22 29 3b 0a malfunction");.
21357 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 pParse->rc = S
21358 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a QLITE_ERROR;.}..
21359 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20 /*.** The pExpr
2135a 73 68 6f 75 6c 64 20 62 65 20 61 20 54 4b 5f 43 should be a TK_C
2135b 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e OLUMN expression
2135c 2e 20 20 54 68 65 20 74 61 62 6c 65 20 72 65 66 . The table ref
2135d 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69 73 20 69 erred to.** is i
2135e 6e 20 70 54 61 62 4c 69 73 74 20 6f 72 20 65 6c n pTabList or el
2135f 73 65 20 69 74 20 69 73 20 74 68 65 20 4e 45 57 se it is the NEW
21360 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66 or OLD table of
21361 20 61 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2a a trigger. .**
21362 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
21363 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 72 65 61 it is OK to rea
21364 64 20 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 d this particula
21365 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 r column..**.**
21366 49 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 If the auth func
21367 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c tion returns SQL
21368 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61 6e ITE_IGNORE, chan
21369 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e ge the TK_COLUMN
2136a 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e .** instruction
2136b 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c 2e into a TK_NULL.
2136c 20 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75 If the auth fu
2136d 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 nction returns S
2136e 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a 20 74 QLITE_DENY,.** t
2136f 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 hen generate an
21370 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 error..*/.SQLITE
21371 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
21372 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 0a 20 lite3AuthRead(.
21373 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
21374 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
21375 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a rser context */.
21376 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 Expr *pExpr,
21377 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
21378 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 63 68 65 xpression to che
21379 63 6b 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e ck authorization
2137a 20 6f 6e 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 on */. Schema
2137b 2a 70 53 63 68 65 6d 61 2c 20 20 20 20 20 20 2f *pSchema, /
2137c 2a 20 54 68 65 20 73 63 68 65 6d 61 20 6f 66 20 * The schema of
2137d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a the expression *
2137e 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 /. SrcList *pTa
2137f 62 4c 69 73 74 20 20 20 20 20 2f 2a 20 41 6c 6c bList /* All
21380 20 74 61 62 6c 65 20 74 68 61 74 20 70 45 78 70 table that pExp
21381 72 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f r might refer to
21382 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
21383 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
21384 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 b;. int rc;. T
21385 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 able *pTab = 0;
21386 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c /* The tabl
21387 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a e being read */.
21388 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
21389 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ol; /* Name
2138a 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 of the column of
2138b 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
2138c 69 6e 74 20 69 53 72 63 3b 20 20 20 20 20 20 20 int iSrc;
2138d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
2138e 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 20 n pTabList->a[]
2138f 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 of table being r
21390 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ead */. const c
21391 68 61 72 20 2a 7a 44 42 61 73 65 3b 20 20 20 2f har *zDBase; /
21392 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 * Name of databa
21393 73 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 se being accesse
21394 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 d */. TriggerSt
21395 61 63 6b 20 2a 70 53 74 61 63 6b 3b 20 2f 2a 20 ack *pStack; /*
21396 54 68 65 20 73 74 61 63 6b 20 6f 66 20 63 75 72 The stack of cur
21397 72 65 6e 74 20 74 72 69 67 67 65 72 73 20 2a 2f rent triggers */
21398 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
21399 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
2139a 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
2139b 61 62 61 73 65 20 74 68 65 20 65 78 70 72 65 73 abase the expres
2139c 73 69 6f 6e 20 72 65 66 65 72 73 20 74 6f 20 2a sion refers to *
2139d 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 /.. if( db->xAu
2139e 74 68 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a th==0 ) return;.
2139f 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
213a0 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 >op==TK_COLUMN )
213a1 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 ;. iDb = sqlite
213a2 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 3SchemaToIndex(p
213a3 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 63 68 65 Parse->db, pSche
213a4 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 ma);. if( iDb<0
213a5 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74 ){. /* An at
213a6 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20 tempt to read a
213a7 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20 column out of a
213a8 73 75 62 71 75 65 72 79 20 6f 72 20 6f 74 68 65 subquery or othe
213a9 72 0a 20 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61 r. ** tempora
213aa 72 79 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 ry table. */.
213ab 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
213ac 66 28 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 f( pTabList ){.
213ad 20 20 20 66 6f 72 28 69 53 72 63 3d 30 3b 20 41 for(iSrc=0; A
213ae 4c 57 41 59 53 28 69 53 72 63 3c 70 54 61 62 4c LWAYS(iSrc<pTabL
213af 69 73 74 2d 3e 6e 53 72 63 29 3b 20 69 53 72 63 ist->nSrc); iSrc
213b0 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ++){. if( p
213b1 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 54 Expr->iTable==pT
213b2 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e abList->a[iSrc].
213b3 69 43 75 72 73 6f 72 20 29 20 62 72 65 61 6b 3b iCursor ) break;
213b4 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
213b5 74 28 20 69 53 72 63 3c 70 54 61 62 4c 69 73 74 t( iSrc<pTabList
213b6 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 70 54 ->nSrc );. pT
213b7 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 ab = pTabList->a
213b8 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a 20 20 7d [iSrc].pTab;. }
213b9 65 6c 73 65 7b 0a 20 20 20 20 70 53 74 61 63 6b else{. pStack
213ba 20 3d 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 = pParse->trigS
213bb 74 61 63 6b 3b 0a 20 20 20 20 69 66 28 20 41 4c tack;. if( AL
213bc 57 41 59 53 28 70 53 74 61 63 6b 29 20 29 7b 0a WAYS(pStack) ){.
213bd 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6d 75 /* This mu
213be 73 74 20 62 65 20 61 6e 20 61 74 74 65 6d 70 74 st be an attempt
213bf 20 74 6f 20 72 65 61 64 20 74 68 65 20 4e 45 57 to read the NEW
213c0 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 or OLD pseudo-t
213c1 61 62 6c 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f ables. ** o
213c2 66 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20 20 f a trigger..
213c3 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 */. asse
213c4 72 74 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c rt( pExpr->iTabl
213c5 65 3d 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49 64 e==pStack->newId
213c6 78 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 x || pExpr->iTab
213c7 6c 65 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64 49 le==pStack->oldI
213c8 64 78 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 dx );. pTab
213c9 20 3d 20 70 53 74 61 63 6b 2d 3e 70 54 61 62 3b = pStack->pTab;
213ca 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
213cb 20 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 NEVER(pTab==0)
213cc 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
213cd 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d pExpr->iColumn>=
213ce 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
213cf 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c pExpr->iColumn<
213d0 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 pTab->nCol );.
213d1 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 zCol = pTab->a
213d2 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 Col[pExpr->iColu
213d3 6d 6e 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c mn].zName;. }el
213d4 73 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b se if( pTab->iPK
213d5 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 ey>=0 ){. ass
213d6 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 ert( pTab->iPKey
213d7 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 <pTab->nCol );.
213d8 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e zCol = pTab->
213d9 61 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 aCol[pTab->iPKey
213da 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 ].zName;. }else
213db 7b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f {. zCol = "RO
213dc 57 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65 WID";. }. asse
213dd 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 rt( iDb>=0 && iD
213de 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a b<db->nDb );. z
213df 44 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b DBase = db->aDb[
213e0 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 iDb].zName;. rc
213e1 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d = db->xAuth(db-
213e2 3e 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54 >pAuthArg, SQLIT
213e3 45 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e E_READ, pTab->zN
213e4 61 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73 ame, zCol, zDBas
213e5 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 e, .
213e6 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 pParse->zAu
213e7 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 thContext);. if
213e8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e ( rc==SQLITE_IGN
213e9 4f 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72 ORE ){. pExpr
213ea 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a ->op = TK_NULL;.
213eb 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d }else if( rc==
213ec 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 SQLITE_DENY ){.
213ed 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32 if( db->nDb>2
213ee 20 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20 || iDb!=0 ){.
213ef 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
213f0 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63 Msg(pParse, "acc
213f1 65 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20 ess to %s.%s.%s
213f2 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20 is prohibited",
213f3 0a 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65 . zDBase
213f4 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a , pTab->zName, z
213f5 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Col);. }else{
213f6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
213f7 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
213f8 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20 access to %s.%s
213f9 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70 is prohibited",p
213fa 54 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29 Tab->zName,zCol)
213fb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 ;. }. pPar
213fc 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
213fd 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 AUTH;. }else if
213fe 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
213ff 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 ){. sqliteAut
21400 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 hBadReturnCode(p
21401 50 61 72 73 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f Parse);. }.}../
21402 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68 6f *.** Do an autho
21403 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20 75 rization check u
21404 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61 6e sing the code an
21405 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76 65 d arguments give
21406 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65 69 n. Return.** ei
21407 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 ther SQLITE_OK (
21408 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45 5f zero) or SQLITE_
21409 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54 45 IGNORE or SQLITE
2140a 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49 54 _DENY. If SQLIT
2140b 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65 74 E_DENY.** is ret
2140c 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 urned, then the
2140d 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64 20 error count and
2140e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
2140f 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a 20 6d pParse are.** m
21410 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70 72 69 odified appropri
21411 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ately..*/.SQLITE
21412 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
21413 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 0a 20 ite3AuthCheck(.
21414 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a Parse *pParse,.
21415 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20 63 6f int code,. co
21416 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 31 2c nst char *zArg1,
21417 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
21418 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20 63 68 Arg2,. const ch
21419 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20 20 73 ar *zArg3.){. s
2141a 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
2141b 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 rse->db;. int r
2141c 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 64 c;.. /* Don't d
2141d 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a 61 74 o any authorizat
2141e 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68 ion checks if th
2141f 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e e database is in
21420 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a 2a 20 itialising. **
21421 6f 72 20 69 66 20 74 68 65 20 70 61 72 73 65 72 or if the parser
21422 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b 65 is being invoke
21423 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 73 71 d from within sq
21424 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 lite3_declare_vt
21425 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 ab.. */. if( d
21426 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 b->init.busy ||
21427 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
21428 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
21429 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
2142a 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 if( db->xAuth==0
2142b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
2142c 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
2142d 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 rc = db->xAuth(d
2142e 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63 6f 64 b->pAuthArg, cod
2142f 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67 32 2c e, zArg1, zArg2,
21430 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65 2d 3e zArg3, pParse->
21431 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 zAuthContext);.
21432 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
21433 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71 6c 69 DENY ){. sqli
21434 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
21435 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 se, "not authori
21436 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61 72 73 zed");. pPars
21437 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 e->rc = SQLITE_A
21438 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 UTH;. }else if(
21439 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc!=SQLITE_OK &
2143a 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e & rc!=SQLITE_IGN
2143b 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ORE ){. rc =
2143c 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20 20 SQLITE_DENY;.
2143d 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 sqliteAuthBadRe
2143e 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65 29 turnCode(pParse)
2143f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
21440 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68 c;.}../*.** Push
21441 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f an authorizatio
21442 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65 n context. Afte
21443 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 r this routine i
21444 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a s called, the.**
21445 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20 zArg3 argument
21446 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e to authorization
21447 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 callbacks will
21448 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69 be zContext unti
21449 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72 l.** popped. Or
2144a 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74 if pParse==0, t
2144b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
2144c 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 no-op..*/.SQLIT
2144d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
2144e 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 qlite3AuthContex
2144f 74 50 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a tPush(. Parse *
21450 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f pParse,. AuthCo
21451 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c ntext *pContext,
21452 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a . const char *
21453 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 61 73 zContext.){. as
21454 73 65 72 74 28 20 70 50 61 72 73 65 20 29 3b 0a sert( pParse );.
21455 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 pContext->pPar
21456 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 se = pParse;. p
21457 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f Context->zAuthCo
21458 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e ntext = pParse->
21459 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 zAuthContext;.
2145a 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e pParse->zAuthCon
2145b 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b text = zContext;
2145c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 61 6e .}../*.** Pop an
2145d 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 authorization c
2145e 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61 73 20 ontext that was
2145f 70 72 65 76 69 6f 75 73 6c 79 20 70 75 73 68 65 previously pushe
21460 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 41 d.** by sqlite3A
21461 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 0a 2a uthContextPush.*
21462 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21463 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 void sqlite3Aut
21464 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68 hContextPop(Auth
21465 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 Context *pContex
21466 74 29 7b 0a 20 20 69 66 28 20 70 43 6f 6e 74 65 t){. if( pConte
21467 78 74 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 xt->pParse ){.
21468 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 pContext->pPar
21469 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 se->zAuthContext
2146a 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 = pContext->zAu
2146b 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 thContext;. p
2146c 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 Context->pParse
2146d 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 = 0;. }.}..#end
2146e 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
2146f 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 T_AUTHORIZATION
21470 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
21471 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75 74 68 2e *** End of auth.
21472 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
21473 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21474 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21475 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
21476 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
21477 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a uild.c *********
21478 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21479 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2147a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
2147b 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
2147c 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
2147d 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
2147e 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
2147f 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
21480 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
21481 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
21482 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
21483 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
21484 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
21485 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
21486 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
21487 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
21488 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
21489 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
2148a 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
2148b 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
2148c 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
2148d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
2148e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2148f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
21491 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
21492 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
21493 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 C code routines
21494 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 that are called
21495 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 70 by the SQLite p
21496 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e 20 73 79 arser.** when sy
21497 6e 74 61 78 20 72 75 6c 65 73 20 61 72 65 20 72 ntax rules are r
21498 65 64 75 63 65 64 2e 20 20 54 68 65 20 72 6f 75 educed. The rou
21499 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
2149a 6c 65 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a le handle the.**
2149b 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e 64 73 following kinds
2149c 20 6f 66 20 53 51 4c 20 73 79 6e 74 61 78 3a 0a of SQL syntax:.
2149d 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 **.** CREATE
2149e 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 44 52 TABLE.** DR
2149f 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 OP TABLE.**
214a0 43 52 45 41 54 45 20 49 4e 44 45 58 0a 2a 2a 20 CREATE INDEX.**
214a1 20 20 20 20 44 52 4f 50 20 49 4e 44 45 58 0a 2a DROP INDEX.*
214a2 2a 20 20 20 20 20 63 72 65 61 74 69 6e 67 20 49 * creating I
214a3 44 20 6c 69 73 74 73 0a 2a 2a 20 20 20 20 20 42 D lists.** B
214a4 45 47 49 4e 20 54 52 41 4e 53 41 43 54 49 4f 4e EGIN TRANSACTION
214a5 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d 49 54 0a 2a .** COMMIT.*
214a6 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 0a 2a * ROLLBACK.*
214a7 2a 0a 2a 2a 20 24 49 64 3a 20 62 75 69 6c 64 2e *.** $Id: build.
214a8 63 2c 76 20 31 2e 35 33 37 20 32 30 30 39 2f 30 c,v 1.537 2009/0
214a9 35 2f 30 36 20 31 38 3a 34 32 3a 32 31 20 64 72 5/06 18:42:21 dr
214aa 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a h Exp $.*/../*.*
214ab 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
214ac 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 s called when a
214ad 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e new SQL statemen
214ae 74 20 69 73 20 62 65 67 69 6e 6e 69 6e 67 20 74 t is beginning t
214af 6f 0a 2a 2a 20 62 65 20 70 61 72 73 65 64 2e 20 o.** be parsed.
214b0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
214b1 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 pParse structure
214b2 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 as needed..*/.S
214b3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
214b4 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 id sqlite3BeginP
214b5 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 arse(Parse *pPar
214b6 73 65 2c 20 69 6e 74 20 65 78 70 6c 61 69 6e 46 se, int explainF
214b7 6c 61 67 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e lag){. pParse->
214b8 65 78 70 6c 61 69 6e 20 3d 20 28 75 38 29 65 78 explain = (u8)ex
214b9 70 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 plainFlag;. pPa
214ba 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d rse->nVar = 0;.}
214bb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
214bc 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
214bd 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 HE./*.** The Tab
214be 6c 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65 leLock structure
214bf 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 is only used by
214c0 20 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c the sqlite3Tabl
214c1 65 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 eLock() and.** c
214c2 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 odeTableLocks()
214c3 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 functions..*/.st
214c4 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b ruct TableLock {
214c5 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
214c6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
214c7 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 atabase containi
214c8 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 ng the table to
214c9 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 be locked */. i
214ca 6e 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20 nt iTab;
214cb 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 /* The root
214cc 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c page of the tabl
214cd 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a e to be locked *
214ce 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f /. u8 isWriteLo
214cf 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ck; /* True
214d0 20 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e for write lock.
214d1 20 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65 False for a re
214d2 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e ad lock */. con
214d3 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 st char *zName;
214d4 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
214d5 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a table */.};../*
214d6 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 .** Record the f
214d7 61 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74 act that we want
214d8 20 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 to lock a table
214d9 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a at run-time. .
214da 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
214db 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 to be locked has
214dc 20 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20 root page iTab
214dd 61 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 and is found in
214de 64 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a database iDb..**
214df 20 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69 A read or a wri
214e0 74 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 te lock can be t
214e1 61 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f aken depending o
214e2 6e 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a n isWritelock..*
214e3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
214e4 65 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74 e just records t
214e5 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
214e6 20 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64 lock is desired
214e7 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 . The.** code t
214e8 6f 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 o make the lock
214e9 6f 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74 occur is generat
214ea 65 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61 ed by a later ca
214eb 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 ll to.** codeTab
214ec 6c 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20 leLocks() which
214ed 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71 occurs during sq
214ee 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e lite3FinishCodin
214ef 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 g()..*/.SQLITE_P
214f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
214f1 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 te3TableLock(.
214f2 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
214f3 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
214f4 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntext */. int i
214f5 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a Db, /*
214f6 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 Index of the da
214f7 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e tabase containin
214f8 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c g the table to l
214f9 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 ock */. int iTa
214fa 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 b, /* R
214fb 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 oot page number
214fc 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 of the table to
214fd 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 be locked */. u
214fe 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 8 isWriteLock,
214ff 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
21500 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 write lock */.
21501 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
21502 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 e /* Name of th
21503 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f e table to be lo
21504 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 cked */.){. int
21505 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 i;. int nBytes
21506 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 ;. TableLock *p
21507 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 ;.. if( iDb<0 )
21508 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
21509 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }.. for(i=0; i<
2150a 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f pParse->nTableLo
2150b 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 ck; i++){. p
2150c 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c = &pParse->aTabl
2150d 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 eLock[i];. if
2150e 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26 ( p->iDb==iDb &&
2150f 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 29 p->iTab==iTab )
21510 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69 {. p->isWri
21511 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57 teLock = (p->isW
21512 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 riteLock || isWr
21513 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 iteLock);.
21514 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
21515 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 69 }.. nBytes = si
21516 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20 zeof(TableLock)
21517 2a 20 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c * (pParse->nTabl
21518 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72 eLock+1);. pPar
21519 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d se->aTableLock =
2151a 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 . sqlite3D
2151b 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 bReallocOrFree(p
2151c 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 Parse->db, pPars
2151d 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e e->aTableLock, n
2151e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 50 Bytes);. if( pP
2151f 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b arse->aTableLock
21520 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 ){. p = &pPa
21521 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b rse->aTableLock[
21522 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f pParse->nTableLo
21523 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 ck++];. p->iD
21524 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e b = iDb;. p->
21525 69 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20 iTab = iTab;.
21526 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 p->isWriteLock
21527 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 = isWriteLock;.
21528 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e p->zName = zN
21529 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ame;. }else{.
2152a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 pParse->nTable
2152b 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 Lock = 0;. pP
2152c 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
2152d 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a Failed = 1;. }.
2152e 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e }../*.** Code an
2152f 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e OP_TableLock in
21530 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 struction for ea
21531 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 ch table locked
21532 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d by the.** statem
21533 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20 ent (configured
21534 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 by calls to sqli
21535 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e te3TableLock()).
21536 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
21537 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50 codeTableLocks(P
21538 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 arse *pParse){.
21539 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a int i;. Vdbe *
2153a 70 56 64 62 65 3b 20 0a 0a 20 20 69 66 28 20 30 pVdbe; .. if( 0
2153b 3d 3d 28 70 56 64 62 65 20 3d 20 73 71 6c 69 74 ==(pVdbe = sqlit
2153c 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
2153d 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e )) ){. return
2153e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 ;. }.. for(i=0
2153f 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 ; i<pParse->nTab
21540 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 leLock; i++){.
21541 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d TableLock *p =
21542 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 &pParse->aTable
21543 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 Lock[i];. int
21544 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20 p1 = p->iDb;.
21545 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21546 4f 70 34 28 70 56 64 62 65 2c 20 4f 50 5f 54 61 Op4(pVdbe, OP_Ta
21547 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e bleLock, p1, p->
21548 69 54 61 62 2c 20 70 2d 3e 69 73 57 72 69 74 65 iTab, p->isWrite
21549 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 Lock,.
2154a 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a p->z
2154b 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 Name, P4_STATIC)
2154c 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.}.#else.
2154d 23 64 65 66 69 6e 65 20 63 6f 64 65 54 61 62 6c #define codeTabl
2154e 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e 64 69 66 eLocks(x).#endif
2154f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
21550 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61 tine is called a
21551 66 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51 fter a single SQ
21552 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 L statement has
21553 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61 been.** parsed a
21554 6e 64 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 nd a VDBE progra
21555 6d 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 61 m to execute tha
21556 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 t statement has
21557 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64 been.** prepared
21558 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
21559 70 75 74 73 20 74 68 65 20 66 69 6e 69 73 68 69 puts the finishi
2155a 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68 ng touches on th
2155b 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61 e.** VDBE progra
2155c 6d 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65 m and resets the
2155d 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 pParse structur
2155e 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a e for the next.*
2155f 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e * parse..**.** N
21560 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 20 65 ote that if an e
21561 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69 rror occurred, i
21562 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 t might be the c
21563 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 ase that.** no V
21564 44 42 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e DBE code was gen
21565 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 erated..*/.SQLIT
21566 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21567 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 qlite3FinishCodi
21568 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ng(Parse *pParse
21569 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
2156a 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 ;. Vdbe *v;..
2156b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
2156c 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
2156d 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e cFailed ) return
2156e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
2156f 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e 3b nested ) return;
21570 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e . if( pParse->n
21571 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 Err ) return;..
21572 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 67 65 6e /* Begin by gen
21573 65 72 61 74 69 6e 67 20 73 6f 6d 65 20 74 65 72 erating some ter
21574 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 20 61 74 mination code at
21575 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a the end of the.
21576 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 61 ** vdbe progra
21577 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c m. */. v = sql
21578 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
21579 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a se);. if( v ){.
2157a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2157b 64 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74 ddOp0(v, OP_Halt
2157c 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 );.. /* The c
2157d 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 ookie mask conta
2157e 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 ins one bit for
2157f 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69 each database fi
21580 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 le open.. **
21581 28 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61 (Bit 0 is for ma
21582 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72 in, bit 1 is for
21583 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f temp, and so fo
21584 72 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a rth.) Bits are.
21585 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65 ** set for e
21586 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61 ach database tha
21587 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65 t is used. Gene
21588 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61 rate code to sta
21589 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e rt a. ** tran
2158a 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20 saction on each
2158b 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61 6e used database an
2158c 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 20 d to verify the
2158d 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 schema cookie.
2158e 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65 ** on each use
2158f 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 d database..
21590 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 */. if( pPars
21591 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 e->cookieGoto>0
21592 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73 ){. u32 mas
21593 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 k;. int iDb
21594 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
21595 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 dbeJumpHere(v, p
21596 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 Parse->cookieGot
21597 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 o-1);. for(
21598 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 iDb=0, mask=1; i
21599 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b Db<db->nDb; mask
2159a 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 <<=1, iDb++){.
2159b 20 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20 if( (mask
2159c 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 & pParse->cookie
2159d 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 Mask)==0 ) conti
2159e 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c nue;. sql
2159f 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
215a0 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 e(v, iDb);.
215a1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
215a2 64 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61 dOp2(v,OP_Transa
215a3 63 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 ction, iDb, (mas
215a4 6b 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74 k & pParse->writ
215a5 65 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20 eMask)!=0);.
215a6 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 if( db->init
215a7 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 .busy==0 ){.
215a8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
215a9 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65 72 eAddOp2(v,OP_Ver
215aa 69 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 ifyCookie, iDb,
215ab 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 pParse->cookieVa
215ac 6c 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20 20 lue[iDb]);.
215ad 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 }. }.#if
215ae 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
215af 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
215b0 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e {. in
215b1 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 t i;. for
215b2 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e (i=0; i<pParse->
215b3 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b nVtabLock; i++){
215b4 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 . char
215b5 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29 *vtab = (char *)
215b6 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f pParse->apVtabLo
215b7 63 6b 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20 20 ck[i]->pVtab;.
215b8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
215b9 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
215ba 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c VBegin, 0, 0, 0,
215bb 20 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b vtab, P4_VTAB);
215bc 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
215bd 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 pParse->nVtab
215be 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 Lock = 0;.
215bf 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 }.#endif..
215c0 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 /* Once all the
215c1 63 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65 cookies have bee
215c2 6e 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 74 n verified and t
215c3 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e ransactions open
215c4 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62 ed, . ** ob
215c5 74 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 65 tain the require
215c6 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54 d table-locks. T
215c7 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 his is a no-op u
215c8 6e 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 20 nless the .
215c9 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 ** shared-cache
215ca 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 feature is enab
215cb 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 led.. */.
215cc 20 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 codeTableLoc
215cd 6b 73 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 ks(pParse);.
215ce 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
215cf 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 Op2(v, OP_Goto,
215d0 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 0, pParse->cooki
215d1 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 eGoto);. }.
215d2 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 }... /* Get the
215d3 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72 65 VDBE program re
215d4 61 64 79 20 66 6f 72 20 65 78 65 63 75 74 69 6f ady for executio
215d5 6e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 26 n. */. if( v &
215d6 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d & pParse->nErr==
215d7 30 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 0 && !db->malloc
215d8 46 61 69 6c 65 64 20 29 7b 0a 23 69 66 64 65 66 Failed ){.#ifdef
215d9 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
215da 20 20 46 49 4c 45 20 2a 74 72 61 63 65 20 3d 20 FILE *trace =
215db 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c (db->flags & SQL
215dc 49 54 45 5f 56 64 62 65 54 72 61 63 65 29 21 3d ITE_VdbeTrace)!=
215dd 30 20 3f 20 73 74 64 6f 75 74 20 3a 20 30 3b 0a 0 ? stdout : 0;.
215de 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 54 sqlite3VdbeT
215df 72 61 63 65 28 76 2c 20 74 72 61 63 65 29 3b 0a race(v, trace);.
215e0 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 #endif. asser
215e1 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 t( pParse->iCach
215e2 65 4c 65 76 65 6c 3d 3d 30 20 29 3b 20 20 2f 2a eLevel==0 ); /*
215e3 20 44 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65 Disables and re
215e4 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a -enables match *
215e5 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 /. sqlite3Vdb
215e6 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50 eMakeReady(v, pP
215e7 61 72 73 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 arse->nVar, pPar
215e8 73 65 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20 se->nMem,.
215e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
215ea 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2c pParse->nTab,
215eb 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e pParse->explain
215ec 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 );. pParse->r
215ed 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
215ee 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c . pParse->col
215ef 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20 20 NamesSet = 0;.
215f0 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 65 }else if( pParse
215f1 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
215f2 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 ){. pParse->r
215f3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
215f4 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e ;. }. pParse->
215f5 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61 72 nTab = 0;. pPar
215f6 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20 20 se->nMem = 0;.
215f7 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 30 pParse->nSet = 0
215f8 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 ;. pParse->nVar
215f9 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e = 0;. pParse->
215fa 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b 0a cookieMask = 0;.
215fb 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 pParse->cookie
215fc 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a Goto = 0;.}../*.
215fd 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 ** Run the parse
215fe 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 r and code gener
215ff 61 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c 79 ator recursively
21600 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e in order to gen
21601 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f erate.** code fo
21602 72 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d r the SQL statem
21603 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 74 ent given onto t
21604 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 50 he end of the pP
21605 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 arse context.**
21606 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 currently under
21607 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 57 construction. W
21608 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 69 hen the parser i
21609 73 20 72 75 6e 20 72 65 63 75 72 73 69 76 65 6c s run recursivel
2160a 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 74 y.** this way, t
2160b 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 he final OP_Halt
2160c 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 64 is not appended
2160d 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 69 and other initi
2160e 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 alization.** and
2160f 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 74 finalization st
21610 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 eps are omitted
21611 62 65 63 61 75 73 65 20 74 68 6f 73 65 20 61 72 because those ar
21612 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68 e handling by th
21613 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 70 e.** outermost p
21614 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 arser..**.** Not
21615 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 6e everything is n
21616 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20 66 estable. This f
21617 61 63 69 6c 69 74 79 20 69 73 20 64 65 73 69 67 acility is desig
21618 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a 2a ned to permit.**
21619 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
2161a 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72 and DELETE oper
2161b 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53 ations against S
2161c 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55 QLITE_MASTER. U
2161d 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f se.** care if yo
2161e 75 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 20 u decide to try
2161f 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 to use this rout
21620 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 ine for some oth
21621 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a er purposes..*/.
21622 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
21623 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 oid sqlite3Neste
21624 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 dParse(Parse *pP
21625 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 arse, const char
21626 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
21627 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
21628 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 63 char *zSql;. c
21629 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 har *zErrMsg = 0
2162a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
2162b 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 20 = pParse->db;.#
2162c 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20 20 define SAVE_SZ
2162d 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20 2d (sizeof(Parse) -
2162e 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65 2c offsetof(Parse,
2162f 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73 61 nVar)). char sa
21630 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b 0a veBuf[SAVE_SZ];.
21631 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e . if( pParse->n
21632 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 Err ) return;.
21633 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
21634 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f 2a nested<10 ); /*
21635 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64 20 Nesting should
21636 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69 74 only be of limit
21637 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76 61 ed depth */. va
21638 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
21639 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 at);. zSql = sq
2163a 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
2163b 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
2163c 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
2163d 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 if( zSql==0 ){.
2163e 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20 return; /*
2163f 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 A malloc must ha
21640 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d ve failed */. }
21641 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 . pParse->neste
21642 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73 61 d++;. memcpy(sa
21643 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d 3e veBuf, &pParse->
21644 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b 0a nVar, SAVE_SZ);.
21645 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 memset(&pParse
21646 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45 5f ->nVar, 0, SAVE_
21647 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 75 SZ);. sqlite3Ru
21648 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 nParser(pParse,
21649 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b zSql, &zErrMsg);
2164a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
2164b 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 (db, zErrMsg);.
2164c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
2164d 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d 63 b, zSql);. memc
2164e 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 py(&pParse->nVar
2164f 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45 5f , saveBuf, SAVE_
21650 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e SZ);. pParse->n
21651 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a ested--;.}../*.*
21652 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d * Locate the in-
21653 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 memory structure
21654 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
21655 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 a particular dat
21656 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 abase.** table g
21657 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 iven the name of
21658 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20 that table and
21659 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 (optionally) the
2165a 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 name of the.**
2165b 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
2165c 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 ing the table.
2165d 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e Return NULL if n
2165e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 ot found..**.**
2165f 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 If zDatabase is
21660 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0, all databases
21661 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f are searched fo
21662 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 r the table and
21663 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61 74 the.** first mat
21664 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20 72 ching table is r
21665 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 eturned. (No ch
21666 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c 69 ecking for dupli
21667 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e 61 cate table.** na
21668 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 mes is done.) T
21669 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 he search order
2166a 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 is TEMP first, t
2166b 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 hen MAIN, then a
2166c 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 ny.** auxiliary
2166d 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 20 databases added
2166e 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 using the ATTACH
2166f 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 command..**.**
21670 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 See also sqlite3
21671 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a 2a LocateTable()..*
21672 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21673 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 Table *sqlite3F
21674 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 indTable(sqlite3
21675 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
21676 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 *zName, const c
21677 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29 7b har *zDatabase){
21678 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30 3b . Table *p = 0;
21679 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
2167a 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 nName;. assert(
2167b 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 6e zName!=0 );. n
2167c 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 Name = sqlite3St
2167d 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 rlen30(zName);.
2167e 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 for(i=OMIT_TEMP
2167f 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 DB; i<db->nDb; i
21680 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d ++){. int j =
21681 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 (i<2) ? i^1 : i
21682 3b 20 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45 ; /* Search TE
21683 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a MP before MAIN *
21684 2f 0a 20 20 20 20 69 66 28 20 7a 44 61 74 61 62 /. if( zDatab
21685 61 73 65 21 3d 30 20 26 26 20 73 71 6c 69 74 65 ase!=0 && sqlite
21686 33 53 74 72 49 43 6d 70 28 7a 44 61 74 61 62 61 3StrICmp(zDataba
21687 73 65 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a se, db->aDb[j].z
21688 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 Name) ) continue
21689 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 ;. p = sqlite
2168a 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 3HashFind(&db->a
2168b 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 Db[j].pSchema->t
2168c 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e blHash, zName, n
2168d 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 Name);. if( p
2168e 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 ) break;. }.
2168f 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
21690 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e ** Locate the in
21691 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 -memory structur
21692 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 e that describes
21693 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 a particular da
21694 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 tabase.** table
21695 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f given the name o
21696 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 f that table and
21697 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 (optionally) th
21698 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a e name of the.**
21699 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
2169a 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 ning the table.
2169b 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
2169c 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 41 6c 73 6f not found. Also
2169d 20 6c 65 61 76 65 20 61 6e 0a 2a 2a 20 65 72 72 leave an.** err
2169e 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 or message in pP
2169f 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a arse->zErrMsg..*
216a0 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 *.** The differe
216a1 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 nce between this
216a2 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 73 71 6c routine and sql
216a3 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20 ite3FindTable()
216a4 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 is that this.**
216a5 72 6f 75 74 69 6e 65 20 6c 65 61 76 65 73 20 61 routine leaves a
216a6 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
216a7 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d in pParse->zErrM
216a8 73 67 20 77 68 65 72 65 0a 2a 2a 20 73 71 6c 69 sg where.** sqli
216a9 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20 64 te3FindTable() d
216aa 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 oes not..*/.SQLI
216ab 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 TE_PRIVATE Table
216ac 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 *sqlite3LocateT
216ad 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 able(. Parse *p
216ae 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f Parse, /
216af 2a 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 * context in whi
216b0 63 68 20 74 6f 20 72 65 70 6f 72 74 20 65 72 72 ch to report err
216b1 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 ors */. int isV
216b2 69 65 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 iew,
216b3 2f 2a 20 54 72 75 65 20 69 66 20 6c 6f 6f 6b 69 /* True if looki
216b4 6e 67 20 66 6f 72 20 61 20 56 49 45 57 20 72 61 ng for a VIEW ra
216b5 74 68 65 72 20 74 68 61 6e 20 61 20 54 41 42 4c ther than a TABL
216b6 45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 E */. const cha
216b7 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 2f 2a r *zName, /*
216b8 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
216b9 6c 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e le we are lookin
216ba 67 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 g for */. const
216bb 20 63 68 61 72 20 2a 7a 44 62 61 73 65 20 20 20 char *zDbase
216bc 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
216bd 20 64 61 74 61 62 61 73 65 2e 20 20 4d 69 67 68 database. Migh
216be 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a t be NULL */.){.
216bf 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 20 2f Table *p;.. /
216c0 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61 62 * Read the datab
216c1 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 ase schema. If a
216c2 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
216c3 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d leave an error m
216c4 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 essage. ** and
216c5 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 code in pParse a
216c6 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 nd return NULL.
216c7 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f */. if( SQLITE_
216c8 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 OK!=sqlite3ReadS
216c9 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b chema(pParse) ){
216ca 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
216cb 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 }.. p = sqlite
216cc 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72 73 3FindTable(pPars
216cd 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 e->db, zName, zD
216ce 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d base);. if( p==
216cf 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 0 ){. const c
216d0 68 61 72 20 2a 7a 4d 73 67 20 3d 20 69 73 56 69 har *zMsg = isVi
216d1 65 77 20 3f 20 22 6e 6f 20 73 75 63 68 20 76 69 ew ? "no such vi
216d2 65 77 22 20 3a 20 22 6e 6f 20 73 75 63 68 20 74 ew" : "no such t
216d3 61 62 6c 65 22 3b 0a 20 20 20 20 69 66 28 20 7a able";. if( z
216d4 44 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73 Dbase ){. s
216d5 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
216d6 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 Parse, "%s: %s.%
216d7 73 22 2c 20 7a 4d 73 67 2c 20 7a 44 62 61 73 65 s", zMsg, zDbase
216d8 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 , zName);. }e
216d9 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
216da 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
216db 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d 73 e, "%s: %s", zMs
216dc 67 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d g, zName);. }
216dd 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65 . pParse->che
216de 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20 ckSchema = 1;.
216df 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
216e0 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 ./*.** Locate th
216e1 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 e in-memory stru
216e2 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 cture that descr
216e3 69 62 65 73 20 0a 2a 2a 20 61 20 70 61 72 74 69 ibes .** a parti
216e4 63 75 6c 61 72 20 69 6e 64 65 78 20 67 69 76 65 cular index give
216e5 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 n the name of th
216e6 61 74 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 at index.** and
216e7 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
216e8 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 6f database that co
216e9 6e 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 ntains the index
216ea 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c ..** Return NULL
216eb 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a if not found..*
216ec 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 *.** If zDatabas
216ed 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 e is 0, all data
216ee 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 bases are search
216ef 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74 61 ed for the.** ta
216f0 62 6c 65 20 61 6e 64 20 74 68 65 20 66 69 72 73 ble and the firs
216f1 74 20 6d 61 74 63 68 69 6e 67 20 69 6e 64 65 78 t matching index
216f2 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 is returned. (
216f3 4e 6f 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 66 No checking.** f
216f4 6f 72 20 64 75 70 6c 69 63 61 74 65 20 69 6e 64 or duplicate ind
216f5 65 78 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 ex names is done
216f6 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f .) The search o
216f7 72 64 65 72 20 69 73 0a 2a 2a 20 54 45 4d 50 20 rder is.** TEMP
216f8 66 69 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e first, then MAIN
216f9 2c 20 74 68 65 6e 20 61 6e 79 20 61 75 78 69 6c , then any auxil
216fa 69 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61 iary databases a
216fb 64 64 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 dded.** using th
216fc 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 e ATTACH command
216fd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
216fe 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 ATE Index *sqlit
216ff 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 e3FindIndex(sqli
21700 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 te3 *db, const c
21701 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 har *zName, cons
21702 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 t char *zDb){.
21703 49 6e 64 65 78 20 2a 70 20 3d 20 30 3b 0a 20 20 Index *p = 0;.
21704 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e 61 int i;. int nNa
21705 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c me = sqlite3Strl
21706 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 en30(zName);. f
21707 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 or(i=OMIT_TEMPDB
21708 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
21709 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 ){. int j = (
2170a 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 i<2) ? i^1 : i;
2170b 20 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 /* Search TEMP
2170c 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 before MAIN */.
2170d 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 Schema *pSche
2170e 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e ma = db->aDb[j].
2170f 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 69 66 28 pSchema;. if(
21710 20 7a 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 zDb && sqlite3S
21711 74 72 49 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e trICmp(zDb, db->
21712 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 aDb[j].zName) )
21713 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 continue;. as
21714 73 65 72 74 28 20 70 53 63 68 65 6d 61 20 7c 7c sert( pSchema ||
21715 20 28 6a 3d 3d 31 20 26 26 20 21 64 62 2d 3e 61 (j==1 && !db->a
21716 44 62 5b 31 5d 2e 70 42 74 29 20 29 3b 0a 20 20 Db[1].pBt) );.
21717 20 20 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b if( pSchema ){
21718 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 . p = sqlit
21719 65 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 e3HashFind(&pSch
2171a 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e ema->idxHash, zN
2171b 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 ame, nName);.
2171c 20 7d 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 }. if( p ) b
2171d 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 reak;. }. retu
2171e 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn p;.}../*.** R
2171f 65 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 eclaim the memor
21720 79 20 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64 y used by an ind
21721 65 78 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ex.*/.static voi
21722 64 20 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65 d freeIndex(Inde
21723 78 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 x *p){. sqlite3
21724 20 2a 64 62 20 3d 20 70 2d 3e 70 54 61 62 6c 65 *db = p->pTable
21725 2d 3e 64 62 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 ->dbMem;. sqlit
21726 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
21727 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 zColAff);. sqli
21728 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 te3DbFree(db, p)
21729 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 ;.}../*.** Remov
2172a 65 20 74 68 65 20 67 69 76 65 6e 20 69 6e 64 65 e the given inde
2172b 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 x from the index
2172c 20 68 61 73 68 20 74 61 62 6c 65 2c 20 61 6e 64 hash table, and
2172d 20 66 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d free.** its mem
2172e 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 2e 0a ory structures..
2172f 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 **.** The index
21730 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 is removed from
21731 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 the database has
21732 68 20 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20 h tables but.**
21733 69 74 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b it is not unlink
21734 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c ed from the Tabl
21735 65 20 74 68 61 74 20 69 74 20 69 6e 64 65 78 65 e that it indexe
21736 73 2e 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 s..** Unlinking
21737 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 6d from the Table m
21738 75 73 74 20 62 65 20 64 6f 6e 65 20 62 79 20 74 ust be done by t
21739 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
2173a 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
2173b 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 oid sqlite3Delet
2173c 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 eIndex(Index *p)
2173d 7b 0a 20 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b {. Index *pOld;
2173e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2173f 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b Name = p->zName;
21740 0a 0a 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 .. pOld = sqlit
21741 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d e3HashInsert(&p-
21742 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 >pSchema->idxHas
21743 68 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 h, zName,.
21744 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21745 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c sqlite3Strl
21746 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b en30(zName), 0);
21747 0a 20 20 61 73 73 65 72 74 28 20 70 4f 6c 64 3d . assert( pOld=
21748 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b =0 || pOld==p );
21749 0a 20 20 66 72 65 65 49 6e 64 65 78 28 70 29 3b . freeIndex(p);
2174a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 .}../*.** For th
2174b 65 20 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a e index called z
2174c 49 64 78 4e 61 6d 65 20 77 68 69 63 68 20 69 73 IdxName which is
2174d 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 found in the da
2174e 74 61 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20 75 tabase iDb,.** u
2174f 6e 6c 69 6b 65 20 74 68 61 74 20 69 6e 64 65 78 nlike that index
21750 20 66 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 20 from its Table
21751 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 then remove the
21752 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 index from.** th
21753 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 e index hash tab
21754 6c 65 20 61 6e 64 20 66 72 65 65 20 61 6c 6c 20 le and free all
21755 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 memory structure
21756 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 s associated.**
21757 77 69 74 68 20 74 68 65 20 69 6e 64 65 78 2e 0a with the index..
21758 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21759 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e E void sqlite3Un
2175a 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 linkAndDeleteInd
2175b 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ex(sqlite3 *db,
2175c 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 int iDb, const c
2175d 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a har *zIdxName){.
2175e 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b Index *pIndex;
2175f 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 48 61 . int len;. Ha
21760 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62 2d sh *pHash = &db-
21761 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d >aDb[iDb].pSchem
21762 61 2d 3e 69 64 78 48 61 73 68 3b 0a 0a 20 20 6c a->idxHash;.. l
21763 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c en = sqlite3Strl
21764 65 6e 33 30 28 7a 49 64 78 4e 61 6d 65 29 3b 0a en30(zIdxName);.
21765 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 pIndex = sqlit
21766 65 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 e3HashInsert(pHa
21767 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c 20 6c 65 sh, zIdxName, le
21768 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 49 6e n, 0);. if( pIn
21769 64 65 78 20 29 7b 0a 20 20 20 20 69 66 28 20 70 dex ){. if( p
2176a 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 Index->pTable->p
2176b 49 6e 64 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b Index==pIndex ){
2176c 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 . pIndex->p
2176d 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 Table->pIndex =
2176e 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 pIndex->pNext;.
2176f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
21770 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 Index *p;.
21771 66 6f 72 28 70 3d 70 49 6e 64 65 78 2d 3e 70 54 for(p=pIndex->pT
21772 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 20 able->pIndex; p
21773 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 49 6e && p->pNext!=pIn
21774 64 65 78 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 dex; p=p->pNext)
21775 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 70 20 26 {}. if( p &
21776 26 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 & p->pNext==pInd
21777 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d ex ){. p-
21778 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 2d >pNext = pIndex-
21779 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a >pNext;. }.
2177a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 49 6e }. freeIn
2177b 64 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d dex(pIndex);. }
2177c 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 . db->flags |=
2177d 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
2177e 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 nges;.}../*.** E
2177f 72 61 73 65 20 61 6c 6c 20 73 63 68 65 6d 61 20 rase all schema
21780 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d information from
21781 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 the in-memory h
21782 61 73 68 20 74 61 62 6c 65 73 20 6f 66 0a 2a 2a ash tables of.**
21783 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 a single databa
21784 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e se. This routin
21785 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 e is called to r
21786 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a eclaim memory.**
21787 20 62 65 66 6f 72 65 20 74 68 65 20 64 61 74 61 before the data
21788 62 61 73 65 20 63 6c 6f 73 65 73 2e 20 20 49 74 base closes. It
21789 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20 is also called
2178a 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 during a rollbac
2178b 6b 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 77 65 k.** if there we
2178c 72 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 re schema change
2178d 73 20 64 75 72 69 6e 67 20 74 68 65 20 74 72 61 s during the tra
2178e 6e 73 61 63 74 69 6f 6e 20 6f 72 20 69 66 20 61 nsaction or if a
2178f 0a 2a 2a 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 .** schema-cooki
21790 65 20 6d 69 73 6d 61 74 63 68 20 6f 63 63 75 72 e mismatch occur
21791 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3d s..**.** If iDb=
21792 3d 30 20 74 68 65 6e 20 72 65 73 65 74 20 74 68 =0 then reset th
21793 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d e internal schem
21794 61 20 74 61 62 6c 65 73 20 66 6f 72 20 61 6c 6c a tables for all
21795 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
21796 65 73 2e 20 20 49 66 20 69 44 62 3e 3d 31 20 74 es. If iDb>=1 t
21797 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69 6e hen reset the in
21798 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 66 6f ternal schema fo
21799 72 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 73 69 r only the.** si
2179a 6e 67 6c 65 20 66 69 6c 65 20 69 6e 64 69 63 61 ngle file indica
2179b 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
2179c 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
2179d 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c te3ResetInternal
2179e 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 20 2a Schema(sqlite3 *
2179f 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 db, int iDb){.
217a0 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 73 73 65 int i, j;. asse
217a1 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 rt( iDb>=0 && iD
217a2 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 b<db->nDb );..
217a3 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 if( iDb==0 ){.
217a4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
217a5 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a terAll(db);. }.
217a6 20 20 66 6f 72 28 69 3d 69 44 62 3b 20 69 3c 64 for(i=iDb; i<d
217a7 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
217a8 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d Db *pDb = &db-
217a9 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 >aDb[i];. if(
217aa 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b pDb->pSchema ){
217ab 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 69 3d . assert(i=
217ac 3d 31 20 7c 7c 20 28 70 44 62 2d 3e 70 42 74 20 =1 || (pDb->pBt
217ad 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 && sqlite3BtreeH
217ae 6f 6c 64 73 4d 75 74 65 78 28 70 44 62 2d 3e 70 oldsMutex(pDb->p
217af 42 74 29 29 29 3b 0a 20 20 20 20 20 20 73 71 6c Bt)));. sql
217b0 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 70 ite3SchemaFree(p
217b1 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 Db->pSchema);.
217b2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 44 62 3e }. if( iDb>
217b3 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0 ) return;. }.
217b4 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 30 assert( iDb==0
217b5 20 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 );. db->flags
217b6 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 &= ~SQLITE_Inter
217b7 6e 43 68 61 6e 67 65 73 3b 0a 20 20 73 71 6c 69 nChanges;. sqli
217b8 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c te3BtreeLeaveAll
217b9 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f (db);.. /* If o
217ba 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
217bb 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 e auxiliary data
217bc 62 61 73 65 20 66 69 6c 65 73 20 68 61 73 20 62 base files has b
217bd 65 65 6e 20 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a een closed,. **
217be 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 then remove the
217bf 6d 20 66 72 6f 6d 20 74 68 65 20 61 75 78 69 6c m from the auxil
217c0 69 61 72 79 20 64 61 74 61 62 61 73 65 20 6c 69 iary database li
217c1 73 74 2e 20 20 57 65 20 74 61 6b 65 20 74 68 65 st. We take the
217c2 0a 20 20 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 . ** opportunit
217c3 79 20 74 6f 20 64 6f 20 74 68 69 73 20 68 65 72 y to do this her
217c4 65 20 73 69 6e 63 65 20 77 65 20 68 61 76 65 20 e since we have
217c5 6a 75 73 74 20 64 65 6c 65 74 65 64 20 61 6c 6c just deleted all
217c6 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 of the. ** sch
217c7 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73 20 ema hash tables
217c8 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 64 6f and therefore do
217c9 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6d 61 6b not have to mak
217ca 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 20 20 e any changes.
217cb 2a 2a 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f ** to any of tho
217cc 73 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a se tables.. */.
217cd 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
217ce 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
217cf 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d struct Db *pDb =
217d0 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 &db->aDb[i];.
217d1 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d if( pDb->pBt==
217d2 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
217d3 44 62 2d 3e 70 41 75 78 20 26 26 20 70 44 62 2d Db->pAux && pDb-
217d4 3e 78 46 72 65 65 41 75 78 20 29 20 70 44 62 2d >xFreeAux ) pDb-
217d5 3e 78 46 72 65 65 41 75 78 28 70 44 62 2d 3e 70 >xFreeAux(pDb->p
217d6 41 75 78 29 3b 0a 20 20 20 20 20 20 70 44 62 2d Aux);. pDb-
217d7 3e 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d >pAux = 0;. }
217d8 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 . }. for(i=j=2
217d9 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
217da 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 ){. struct Db
217db 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 *pDb = &db->aDb
217dc 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 [i];. if( pDb
217dd 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ->pBt==0 ){.
217de 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
217df 64 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b db, pDb->zName);
217e0 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d . pDb->zNam
217e1 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e e = 0;. con
217e2 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 tinue;. }.
217e3 20 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20 if( j<i ){.
217e4 20 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 db->aDb[j] = d
217e5 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d b->aDb[i];. }
217e6 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 . j++;. }.
217e7 6d 65 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b memset(&db->aDb[
217e8 6a 5d 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d j], 0, (db->nDb-
217e9 6a 29 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 j)*sizeof(db->aD
217ea 62 5b 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 b[j]));. db->nD
217eb 62 20 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d b = j;. if( db-
217ec 3e 6e 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61 >nDb<=2 && db->a
217ed 44 62 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 Db!=db->aDbStati
217ee 63 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 c ){. memcpy(
217ef 64 62 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64 db->aDbStatic, d
217f0 62 2d 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66 b->aDb, 2*sizeof
217f1 28 64 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 (db->aDb[0]));.
217f2 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
217f3 28 64 62 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20 (db, db->aDb);.
217f4 20 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d db->aDb = db-
217f5 3e 61 44 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a >aDbStatic;. }.
217f6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
217f7 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
217f8 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63 when a commit oc
217f9 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f curs..*/.SQLITE_
217fa 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
217fb 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e ite3CommitIntern
217fc 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 alChanges(sqlite
217fd 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c 3 *db){. db->fl
217fe 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49 ags &= ~SQLITE_I
217ff 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a nternChanges;.}.
21800 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
21801 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 column names fr
21802 6f 6d 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69 om a table or vi
21803 65 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ew..*/.static vo
21804 69 64 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f id sqliteResetCo
21805 6c 75 6d 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20 lumnNames(Table
21806 2a 70 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20 *pTable){. int
21807 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f i;. Column *pCo
21808 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 l;. sqlite3 *db
21809 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d = pTable->dbMem
2180a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 ;. assert( pTab
2180b 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 le!=0 );. if( (
2180c 70 43 6f 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61 pCol = pTable->a
2180d 43 6f 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 66 Col)!=0 ){. f
2180e 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65 or(i=0; i<pTable
2180f 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f ->nCol; i++, pCo
21810 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 l++){. sqli
21811 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 te3DbFree(db, pC
21812 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ol->zName);.
21813 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
21814 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 ete(db, pCol->pD
21815 66 6c 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 flt);. sqli
21816 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 te3DbFree(db, pC
21817 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 20 20 20 ol->zType);.
21818 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
21819 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 db, pCol->zColl)
2181a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
2181b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 te3DbFree(db, pT
2181c 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a 20 20 7d able->aCol);. }
2181d 0a 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 . pTable->aCol
2181e 3d 20 30 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e = 0;. pTable->n
2181f 43 6f 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Col = 0;.}../*.*
21820 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 6d 65 6d * Remove the mem
21821 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75 ory data structu
21822 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 res associated w
21823 69 74 68 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a ith the given.**
21824 20 54 61 62 6c 65 2e 20 20 4e 6f 20 63 68 61 6e Table. No chan
21825 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 ges are made to
21826 64 69 73 6b 20 62 79 20 74 68 69 73 20 72 6f 75 disk by this rou
21827 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tine..**.** This
21828 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 64 65 routine just de
21829 6c 65 74 65 73 20 74 68 65 20 64 61 74 61 20 73 letes the data s
2182a 74 72 75 63 74 75 72 65 2e 20 20 49 74 20 64 6f tructure. It do
2182b 65 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 0a 2a 2a es not unlink.**
2182c 20 74 68 65 20 74 61 62 6c 65 20 64 61 74 61 20 the table data
2182d 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 structure from t
2182e 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 he hash table.
2182f 42 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74 But it does dest
21830 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74 roy.** memory st
21831 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20 ructures of the
21832 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 indices and fore
21833 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 ign keys associa
21834 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 ted with .** the
21835 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 table..*/.SQLIT
21836 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21837 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c qlite3DeleteTabl
21838 65 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 e(Table *pTable)
21839 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 {. Index *pInde
2183a 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 46 4b 65 x, *pNext;. FKe
2183b 79 20 2a 70 46 4b 65 79 2c 20 2a 70 4e 65 78 74 y *pFKey, *pNext
2183c 46 4b 65 79 3b 0a 20 20 73 71 6c 69 74 65 33 20 FKey;. sqlite3
2183d 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 *db;.. if( pTab
2183e 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a le==0 ) return;.
2183f 20 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64 db = pTable->d
21840 62 4d 65 6d 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e bMem;.. /* Do n
21841 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 74 61 ot delete the ta
21842 62 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 72 65 ble until the re
21843 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 ference count re
21844 61 63 68 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 aches zero. */.
21845 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b pTable->nRef--;
21846 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e . if( pTable->n
21847 52 65 66 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 Ref>0 ){. ret
21848 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 urn;. }. asser
21849 74 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3d t( pTable->nRef=
2184a 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 =0 );.. /* Dele
2184b 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 te all indices a
2184c 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
2184d 68 69 73 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 his table. */.
2184e 20 66 6f 72 28 70 49 6e 64 65 78 20 3d 20 70 54 for(pIndex = pT
2184f 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49 able->pIndex; pI
21850 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65 ndex; pIndex=pNe
21851 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d xt){. pNext =
21852 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a pIndex->pNext;.
21853 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64 assert( pInd
21854 65 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 ex->pSchema==pTa
21855 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a ble->pSchema );.
21856 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 sqlite3Delet
21857 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 3b 0a eIndex(pIndex);.
21858 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c }..#ifndef SQL
21859 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e ITE_OMIT_FOREIGN
2185a 5f 4b 45 59 0a 20 20 2f 2a 20 44 65 6c 65 74 65 _KEY. /* Delete
2185b 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 all foreign key
2185c 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
2185d 68 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f h this table. */
2185e 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 . for(pFKey=pTa
2185f 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 ble->pFKey; pFKe
21860 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 46 4b y; pFKey=pNextFK
21861 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74 46 4b ey){. pNextFK
21862 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78 ey = pFKey->pNex
21863 74 46 72 6f 6d 3b 0a 20 20 20 20 73 71 6c 69 74 tFrom;. sqlit
21864 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b e3DbFree(db, pFK
21865 65 79 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ey);. }.#endif.
21866 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 . /* Delete the
21867 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 Table structure
21868 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 itself.. */.
21869 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d sqliteResetColum
2186a 6e 4e 61 6d 65 73 28 70 54 61 62 6c 65 29 3b 0a nNames(pTable);.
2186b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
2186c 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d db, pTable->zNam
2186d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 e);. sqlite3DbF
2186e 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e ree(db, pTable->
2186f 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 zColAff);. sqli
21870 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 te3SelectDelete(
21871 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c db, pTable->pSel
21872 65 63 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ect);.#ifndef SQ
21873 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a LITE_OMIT_CHECK.
21874 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
21875 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e ete(db, pTable->
21876 70 43 68 65 63 6b 29 3b 0a 23 65 6e 64 69 66 0a pCheck);.#endif.
21877 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 sqlite3VtabCle
21878 61 72 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 ar(pTable);. sq
21879 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
2187a 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pTable);.}../*.*
2187b 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 67 69 76 * Unlink the giv
2187c 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 en table from th
2187d 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e e hash tables an
2187e 64 20 74 68 65 20 64 65 6c 65 74 65 20 74 68 65 d the delete the
2187f 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72 75 63 74 .** table struct
21880 75 72 65 20 77 69 74 68 20 61 6c 6c 20 69 74 73 ure with all its
21881 20 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 indices and for
21882 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 53 51 eign keys..*/.SQ
21883 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
21884 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 d sqlite3UnlinkA
21885 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 ndDeleteTable(sq
21886 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 lite3 *db, int i
21887 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a Db, const char *
21888 7a 54 61 62 4e 61 6d 65 29 7b 0a 20 20 54 61 62 zTabName){. Tab
21889 6c 65 20 2a 70 3b 0a 20 20 44 62 20 2a 70 44 62 le *p;. Db *pDb
2188a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 ;.. assert( db!
2188b 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
2188c 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 iDb>=0 && iDb<db
2188d 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
2188e 74 28 20 7a 54 61 62 4e 61 6d 65 20 26 26 20 7a t( zTabName && z
2188f 54 61 62 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 TabName[0] );.
21890 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 pDb = &db->aDb[i
21891 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 Db];. p = sqlit
21892 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 44 e3HashInsert(&pD
21893 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 b->pSchema->tblH
21894 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 2c 0a 20 ash, zTabName,.
21895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21896 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
21897 72 6c 65 6e 33 30 28 7a 54 61 62 4e 61 6d 65 29 rlen30(zTabName)
21898 2c 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 ,0);. sqlite3De
21899 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20 leteTable(p);.
2189a 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c db->flags |= SQL
2189b 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
2189c 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 s;.}../*.** Give
2189d 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72 n a token, retur
2189e 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 n a string that
2189f 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 consists of the
218a0 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20 text of that.**
218a1 74 6f 6b 65 6e 2e 20 20 53 70 61 63 65 20 74 6f token. Space to
218a2 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
218a3 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 ed string.** is
218a4 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
218a5 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 liteMalloc() and
218a6 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 must be freed b
218a7 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a y the calling.**
218a8 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
218a9 20 41 6e 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d Any quotation m
218aa 61 72 6b 73 20 28 65 78 3a 20 20 22 6e 61 6d 65 arks (ex: "name
218ab 22 2c 20 27 6e 61 6d 65 27 2c 20 5b 6e 61 6d 65 ", 'name', [name
218ac 5d 2c 20 6f 72 20 60 6e 61 6d 65 60 29 20 74 68 ], or `name`) th
218ad 61 74 0a 2a 2a 20 73 75 72 72 6f 75 6e 64 20 74 at.** surround t
218ae 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 74 he body of the t
218af 6f 6b 65 6e 20 61 72 65 20 72 65 6d 6f 76 65 64 oken are removed
218b0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 ..**.** Tokens a
218b1 72 65 20 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f re often just po
218b2 69 6e 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20 inters into the
218b3 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 original SQL tex
218b4 74 20 61 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20 t and so.** are
218b5 6e 6f 74 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 not \000 termina
218b6 74 65 64 20 61 6e 64 20 61 72 65 20 6e 6f 74 20 ted and are not
218b7 70 65 72 73 69 73 74 65 6e 74 2e 20 20 54 68 65 persistent. The
218b8 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 returned string
218b9 0a 2a 2a 20 69 73 20 5c 30 30 30 20 74 65 72 6d .** is \000 term
218ba 69 6e 61 74 65 64 20 61 6e 64 20 69 73 20 70 65 inated and is pe
218bb 72 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c rsistent..*/.SQL
218bc 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
218bd 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f *sqlite3NameFro
218be 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a mToken(sqlite3 *
218bf 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 db, Token *pName
218c0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 ){. char *zName
218c1 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b ;. if( pName ){
218c2 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c . zName = sql
218c3 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 ite3DbStrNDup(db
218c4 2c 20 28 63 68 61 72 2a 29 70 4e 61 6d 65 2d 3e , (char*)pName->
218c5 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 z, pName->n);.
218c6 20 20 69 66 28 20 70 4e 61 6d 65 2d 3e 71 75 6f if( pName->quo
218c7 74 65 64 20 29 20 73 71 6c 69 74 65 33 44 65 71 ted ) sqlite3Deq
218c8 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 7d uote(zName);. }
218c9 65 6c 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 else{. zName
218ca 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
218cb 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a n zName;.}../*.*
218cc 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 * Open the sqlit
218cd 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 73 e_master table s
218ce 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 tored in databas
218cf 65 20 6e 75 6d 62 65 72 20 69 44 62 20 66 6f 72 e number iDb for
218d0 0a 2a 2a 20 77 72 69 74 69 6e 67 2e 20 54 68 65 .** writing. The
218d1 20 74 61 62 6c 65 20 69 73 20 6f 70 65 6e 65 64 table is opened
218d2 20 75 73 69 6e 67 20 63 75 72 73 6f 72 20 30 2e using cursor 0.
218d3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
218d4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
218d5 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 penMasterTable(P
218d6 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69 44 62 arse *p, int iDb
218d7 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 ){. Vdbe *v = s
218d8 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 qlite3GetVdbe(p)
218d9 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 ;. sqlite3Table
218da 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41 53 Lock(p, iDb, MAS
218db 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43 48 TER_ROOT, 1, SCH
218dc 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 29 3b EMA_TABLE(iDb));
218dd 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
218de 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 dOp3(v, OP_OpenW
218df 72 69 74 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f rite, 0, MASTER_
218e0 52 4f 4f 54 2c 20 69 44 62 29 3b 0a 20 20 73 71 ROOT, iDb);. sq
218e1 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
218e2 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 4(v, -1, (char *
218e3 29 35 2c 20 50 34 5f 49 4e 54 33 32 29 3b 20 20 )5, P4_INT32);
218e4 2f 2a 20 35 20 63 6f 6c 75 6d 6e 20 74 61 62 6c /* 5 column tabl
218e5 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 54 e */. if( p->nT
218e6 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e ab==0 ){. p->
218e7 6e 54 61 62 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a nTab = 1;. }.}.
218e8 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 ./*.** Parameter
218e9 20 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f zName points to
218ea 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 a nul-terminate
218eb 64 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e d buffer contain
218ec 69 6e 67 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 ing the name.**
218ed 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 28 22 of a database ("
218ee 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f 72 main", "temp" or
218ef 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 the name of an
218f0 61 74 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 attached db). Th
218f1 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 is.** function r
218f2 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 eturns the index
218f3 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 of the named da
218f4 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 tabase in db->aD
218f5 62 5b 5d 2c 20 6f 72 0a 2a 2a 20 2d 31 20 69 66 b[], or.** -1 if
218f6 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20 63 61 the named db ca
218f7 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a nnot be found..*
218f8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
218f9 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 int sqlite3Find
218fa 44 62 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a DbName(sqlite3 *
218fb 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a db, const char *
218fc 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 zName){. int i
218fd 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a = -1; /*
218fe 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 Database number
218ff 20 2a 2f 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 */. if( zName
21900 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 3b 0a ){. Db *pDb;.
21901 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 int n = sqli
21902 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d te3Strlen30(zNam
21903 65 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 28 64 e);. for(i=(d
21904 62 2d 3e 6e 44 62 2d 31 29 2c 20 70 44 62 3d 26 b->nDb-1), pDb=&
21905 64 62 2d 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d 30 db->aDb[i]; i>=0
21906 3b 20 69 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20 ; i--, pDb--){.
21907 20 20 20 20 20 69 66 28 20 28 21 4f 4d 49 54 5f if( (!OMIT_
21908 54 45 4d 50 44 42 20 7c 7c 20 69 21 3d 31 20 29 TEMPDB || i!=1 )
21909 20 26 26 20 6e 3d 3d 73 71 6c 69 74 65 33 53 74 && n==sqlite3St
2190a 72 6c 65 6e 33 30 28 70 44 62 2d 3e 7a 4e 61 6d rlen30(pDb->zNam
2190b 65 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 e) && .
2190c 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 0==sqlite3StrIC
2190d 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a mp(pDb->zName, z
2190e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 Name) ){.
2190f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
21910 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
21911 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn i;.}../*.** T
21912 68 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 he token *pName
21913 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d contains the nam
21914 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 e of a database
21915 28 65 69 74 68 65 72 20 22 6d 61 69 6e 22 20 6f (either "main" o
21916 72 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20 74 r.** "temp" or t
21917 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 he name of an at
21918 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 69 73 tached db). This
21919 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
2191a 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 the.** index of
2191b 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 the named datab
2191c 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d ase in db->aDb[]
2191d 2c 20 6f 72 20 2d 31 20 69 66 20 74 68 65 20 6e , or -1 if the n
2191e 61 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73 amed db .** does
2191f 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 not exist..*/.S
21920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
21921 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 t sqlite3FindDb(
21922 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b sqlite3 *db, Tok
21923 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e en *pName){. in
21924 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
21925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21926 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e /* Database n
21927 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 umber */. char
21928 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
21929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2192a 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 65 20 73 /* Name we are s
2192b 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a earching for */.
2192c 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 zName = sqlite
2192d 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
2192e 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 20 3d b, pName);. i =
2192f 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 sqlite3FindDbNa
21930 6d 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 me(db, zName);.
21931 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
21932 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 b, zName);. ret
21933 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 urn i;.}../* The
21934 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6f table or view o
21935 72 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 r trigger name i
21936 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 s passed to this
21937 20 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b routine via tok
21938 65 6e 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e ens.** pName1 an
21939 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 d pName2. If the
2193a 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 table name was
2193b 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c fully qualified,
2193c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a for example:.**
2193d 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 .** CREATE TABLE
2193e 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a xxx.yyy (...);.
2193f 2a 2a 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d ** .** Then pNam
21940 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 e1 is set to "xx
21941 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 x" and pName2 "y
21942 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 yy". On the othe
21943 72 20 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65 r hand if.** the
21944 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e table name is n
21945 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 ot fully qualifi
21946 65 64 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 ed, i.e.:.**.**
21947 43 52 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 CREATE TABLE yyy
21948 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 (...);.**.** The
21949 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 n pName1 is set
2194a 74 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 to "yyy" and pNa
2194b 6d 65 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a me2 is ""..**.**
2194c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
2194d 74 73 20 74 68 65 20 2a 70 70 55 6e 71 75 61 6c ts the *ppUnqual
2194e 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e pointer to poin
2194f 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 t at the token (
21950 70 4e 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 pName1 or.** pNa
21951 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 me2) that stores
21952 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 the unqualified
21953 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68 table name. Th
21954 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a e index of the.*
21955 2a 20 64 61 74 61 62 61 73 65 20 22 78 78 78 22 * database "xxx"
21956 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
21957 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21958 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 int sqlite3TwoPa
21959 72 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 rtName(. Parse
2195a 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a *pParse, /*
2195b 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 Parsing and cod
2195c 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
2195d 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 text */. Token
2195e 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a *pName1, /*
2195f 20 54 68 65 20 22 78 78 78 22 20 69 6e 20 74 68 The "xxx" in th
21960 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 e name "xxx.yyy"
21961 20 6f 72 20 22 78 78 78 22 20 2a 2f 0a 20 20 54 or "xxx" */. T
21962 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 oken *pName2,
21963 20 20 20 2f 2a 20 54 68 65 20 22 79 79 79 22 20 /* The "yyy"
21964 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 in the name "xxx
21965 2e 79 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e .yyy" */. Token
21966 20 2a 2a 70 55 6e 71 75 61 6c 20 20 20 20 20 2f **pUnqual /
21967 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e 71 75 * Write the unqu
21968 61 6c 69 66 69 65 64 20 6f 62 6a 65 63 74 20 6e alified object n
21969 61 6d 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ame here */.){.
2196a 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
2196b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2196c 44 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 Database holding
2196d 20 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 the object */.
2196e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
2196f 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 Parse->db;.. if
21970 28 20 70 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d ( pName2 && pNam
21971 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 69 e2->n>0 ){. i
21972 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 f( db->init.busy
21973 20 29 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) {. sqlit
21974 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
21975 65 2c 20 22 63 6f 72 72 75 70 74 20 64 61 74 61 e, "corrupt data
21976 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 70 50 base");. pP
21977 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 arse->nErr++;.
21978 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 return -1;.
21979 20 20 20 7d 0a 20 20 20 20 2a 70 55 6e 71 75 61 }. *pUnqua
2197a 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20 20 l = pName2;.
2197b 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e iDb = sqlite3Fin
2197c 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b dDb(db, pName1);
2197d 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29 . if( iDb<0 )
2197e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
2197f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
21980 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 "unknown databas
21981 65 20 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b 0a e %T", pName1);.
21982 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 pParse->nE
21983 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 rr++;. retu
21984 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d rn -1;. }. }
21985 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
21986 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d ( db->init.iDb==
21987 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 0 || db->init.bu
21988 73 79 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 sy );. iDb =
21989 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 20 db->init.iDb;.
2198a 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 *pUnqual = pNa
2198b 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 me1;. }. retur
2198c 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n iDb;.}../*.**
2198d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
2198e 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 used to check if
2198f 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e the UTF-8 strin
21990 67 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65 67 g zName is a leg
21991 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69 65 al.** unqualifie
21992 64 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65 77 d name for a new
21993 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 28 schema object (
21994 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76 69 table, index, vi
21995 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72 ew or.** trigger
21996 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72 65 ). All names are
21997 20 6c 65 67 61 6c 20 65 78 63 65 70 74 20 74 68 legal except th
21998 6f 73 65 20 74 68 61 74 20 62 65 67 69 6e 20 77 ose that begin w
21999 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 0a 2a ith the string.*
2199a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28 69 6e 20 * "sqlite_" (in
2199b 75 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72 20 upper, lower or
2199c 6d 69 78 65 64 20 63 61 73 65 29 2e 20 54 68 69 mixed case). Thi
2199d 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 s portion of the
2199e 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69 73 namespace.** is
2199f 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e reserved for in
219a0 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a 53 ternal use..*/.S
219a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
219a2 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 t sqlite3CheckOb
219a3 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a jectName(Parse *
219a4 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 pParse, const ch
219a5 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 66 ar *zName){. if
219a6 28 20 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e 69 ( !pParse->db->i
219a7 6e 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72 nit.busy && pPar
219a8 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 0a 20 se->nested==0 .
219a9 20 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61 && (pPa
219aa 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 rse->db->flags &
219ab 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 SQLITE_WriteSch
219ac 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 ema)==0.
219ad 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 && 0==sqlite3S
219ae 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 22 trNICmp(zName, "
219af 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a sqlite_", 7) ){.
219b0 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
219b1 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 62 6a Msg(pParse, "obj
219b2 65 63 74 20 6e 61 6d 65 20 72 65 73 65 72 76 65 ect name reserve
219b3 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 d for internal u
219b4 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b se: %s", zName);
219b5 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
219b6 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
219b7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
219b8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e ;.}../*.** Begin
219b9 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20 constructing a
219ba 6e 65 77 20 74 61 62 6c 65 20 72 65 70 72 65 73 new table repres
219bb 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f entation in memo
219bc 72 79 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 ry. This is.**
219bd 74 68 65 20 66 69 72 73 74 20 6f 66 20 73 65 76 the first of sev
219be 65 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75 74 eral action rout
219bf 69 6e 65 73 20 74 68 61 74 20 67 65 74 20 63 61 ines that get ca
219c0 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 lled in response
219c1 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41 54 45 20 .** to a CREATE
219c2 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e TABLE statement.
219c3 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c In particular,
219c4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
219c5 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 65 72 called.** after
219c6 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73 20 22 seeing tokens "
219c7 43 52 45 41 54 45 22 20 61 6e 64 20 22 54 41 42 CREATE" and "TAB
219c8 4c 45 22 20 61 6e 64 20 74 68 65 20 74 61 62 6c LE" and the tabl
219c9 65 20 6e 61 6d 65 2e 20 54 68 65 20 69 73 54 65 e name. The isTe
219ca 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72 mp.** flag is tr
219cb 75 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 ue if the table
219cc 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64 should be stored
219cd 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 in the auxiliar
219ce 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 y database.** fi
219cf 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e le instead of in
219d0 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
219d1 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 se file. This i
219d2 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 20 63 s normally the c
219d3 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ase.** when the
219d4 22 54 45 4d 50 22 20 6f 72 20 22 54 45 4d 50 4f "TEMP" or "TEMPO
219d5 52 41 52 59 22 20 6b 65 79 77 6f 72 64 20 6f 63 RARY" keyword oc
219d6 63 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 0a curs in between.
219d7 2a 2a 20 43 52 45 41 54 45 20 61 6e 64 20 54 41 ** CREATE and TA
219d8 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e BLE..**.** The n
219d9 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 ew table record
219da 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 is initialized a
219db 6e 64 20 70 75 74 20 69 6e 20 70 50 61 72 73 65 nd put in pParse
219dc 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a 2a 20 ->pNewTable..**
219dd 41 73 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 43 As more of the C
219de 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 REATE TABLE stat
219df 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c ement is parsed,
219e0 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 63 74 69 additional acti
219e1 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 on.** routines w
219e2 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f ill be called to
219e3 20 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72 6d add more inform
219e4 61 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 65 ation to this re
219e5 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68 65 20 cord..** At the
219e6 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54 end of the CREAT
219e7 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e E TABLE statemen
219e8 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33 45 6e t, the sqlite3En
219e9 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 dTable() routine
219ea 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f .** is called to
219eb 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 63 6f complete the co
219ec 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 nstruction of th
219ed 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f e new table reco
219ee 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 rd..*/.SQLITE_PR
219ef 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
219f0 65 33 53 74 61 72 74 54 61 62 6c 65 28 0a 20 20 e3StartTable(.
219f1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
219f2 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 /* Parser conte
219f3 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 xt */. Token *p
219f4 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46 69 72 73 Name1, /* Firs
219f5 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 t part of the na
219f6 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
219f7 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b or view */. Tok
219f8 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 2f 2a en *pName2, /*
219f9 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 Second part of
219fa 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
219fb 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f table or view */
219fc 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 . int isTemp,
219fd 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
219fe 68 69 73 20 69 73 20 61 20 54 45 4d 50 20 74 61 his is a TEMP ta
219ff 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 ble */. int isV
21a00 69 65 77 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 iew, /* Tru
21a01 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56 e if this is a V
21a02 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 IEW */. int isV
21a03 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20 54 72 75 irtual, /* Tru
21a04 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56 e if this is a V
21a05 49 52 54 55 41 4c 20 74 61 62 6c 65 20 2a 2f 0a IRTUAL table */.
21a06 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 int noErr
21a07 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 /* Do nothing
21a08 20 69 66 20 74 61 62 6c 65 20 61 6c 72 65 61 64 if table alread
21a09 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 y exists */.){.
21a0a 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 0a Table *pTable;.
21a0b 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 char *zName =
21a0c 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 0; /* The name o
21a0d 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 f the new table
21a0e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
21a0f 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
21a10 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 Vdbe *v;. int
21a11 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 iDb; /*
21a12 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 Database number
21a13 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61 to create the ta
21a14 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 ble in */. Toke
21a15 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 n *pName; /*
21a16 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 Unqualified name
21a17 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
21a18 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a create */.. /*
21a19 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 The table or vi
21a1a 65 77 20 6e 61 6d 65 20 74 6f 20 63 72 65 61 74 ew name to creat
21a1b 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 e is passed to t
21a1c 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20 his routine via
21a1d 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d tokens. ** pNam
21a1e 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 e1 and pName2. I
21a1f 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 f the table name
21a20 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 was fully quali
21a21 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c fied, for exampl
21a22 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 e:. **. ** CRE
21a23 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 ATE TABLE xxx.yy
21a24 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20 y (...);. ** .
21a25 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 ** Then pName1
21a26 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 is set to "xxx"
21a27 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 and pName2 "yyy"
21a28 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 . On the other h
21a29 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 and if. ** the
21a2a 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f table name is no
21a2b 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 t fully qualifie
21a2c 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20 d, i.e.:. **.
21a2d 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 ** CREATE TABLE
21a2e 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 yyy(...);. **.
21a2f 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 ** Then pName1
21a30 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 is set to "yyy"
21a31 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 and pName2 is ""
21a32 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 .. **. ** The
21a33 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73 20 call below sets
21a34 74 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74 65 the pName pointe
21a35 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 r to point at th
21a36 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 e token (pName1
21a37 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29 20 or. ** pName2)
21a38 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 that stores the
21a39 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c unqualified tabl
21a3a 65 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 72 69 e name. The vari
21a3b 61 62 6c 65 20 69 44 62 20 69 73 0a 20 20 2a 2a able iDb is. **
21a3c 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 set to the inde
21a3d 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 x of the databas
21a3e 65 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 e that the table
21a3f 20 6f 72 20 76 69 65 77 20 69 73 20 74 6f 20 62 or view is to b
21a40 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 e. ** created i
21a41 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 20 3d 20 n.. */. iDb =
21a42 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 sqlite3TwoPartNa
21a43 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 me(pParse, pName
21a44 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 1, pName2, &pNam
21a45 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 e);. if( iDb<0
21a46 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
21a47 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 !OMIT_TEMPDB &&
21a48 69 73 54 65 6d 70 20 26 26 20 69 44 62 3e 31 20 isTemp && iDb>1
21a49 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 63 72 65 ){. /* If cre
21a4a 61 74 69 6e 67 20 61 20 74 65 6d 70 20 74 61 62 ating a temp tab
21a4b 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61 79 le, the name may
21a4c 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 not be qualifie
21a4d 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 d */. sqlite3
21a4e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
21a4f 20 22 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c "temporary tabl
21a50 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75 e name must be u
21a51 6e 71 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 20 nqualified");.
21a52 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
21a53 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 if( !OMIT_TEMPDB
21a54 20 26 26 20 69 73 54 65 6d 70 20 29 20 69 44 62 && isTemp ) iDb
21a55 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 2d = 1;.. pParse-
21a56 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70 >sNameToken = *p
21a57 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 Name;. zName =
21a58 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
21a59 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b oken(db, pName);
21a5a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 . if( zName==0
21a5b 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
21a5c 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 SQLITE_OK!=sqlit
21a5d 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d e3CheckObjectNam
21a5e 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 e(pParse, zName)
21a5f 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 ){. goto beg
21a60 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a in_table_error;.
21a61 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e }. if( db->in
21a62 69 74 2e 69 44 62 3d 3d 31 20 29 20 69 73 54 65 it.iDb==1 ) isTe
21a63 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 mp = 1;.#ifndef
21a64 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
21a65 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 ORIZATION. asse
21a66 72 74 28 20 28 69 73 54 65 6d 70 20 26 20 31 29 rt( (isTemp & 1)
21a67 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b 0a ==isTemp );. {.
21a68 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 int code;.
21a69 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 char *zDb = db
21a6a 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
21a6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
21a6c 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 3AuthCheck(pPars
21a6d 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 e, SQLITE_INSERT
21a6e 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 , SCHEMA_TABLE(i
21a6f 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20 sTemp), 0, zDb)
21a70 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 ){. goto be
21a71 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b gin_table_error;
21a72 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 . }. if( i
21a73 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69 sView ){. i
21a74 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 f( !OMIT_TEMPDB
21a75 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 && isTemp ){.
21a76 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 code = SQLI
21a77 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 TE_CREATE_TEMP_V
21a78 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 IEW;. }else
21a79 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d {. code =
21a7a 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 SQLITE_CREATE_V
21a7b 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 IEW;. }.
21a7c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 }else{. if
21a7d 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 ( !OMIT_TEMPDB &
21a7e 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 & isTemp ){.
21a7f 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 code = SQLIT
21a80 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 E_CREATE_TEMP_TA
21a81 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 BLE;. }else
21a82 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d {. code =
21a83 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
21a84 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ABLE;. }.
21a85 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 56 }. if( !isV
21a86 69 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65 irtual && sqlite
21a87 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 3AuthCheck(pPars
21a88 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 e, code, zName,
21a89 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 0, zDb) ){.
21a8a 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
21a8b 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 e_error;. }.
21a8c 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
21a8d 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e 65 Make sure the ne
21a8e 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f 65 w table name doe
21a8f 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 s not collide wi
21a90 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 th an existing.
21a91 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62 ** index or tab
21a92 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 le name in the s
21a93 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 ame database. I
21a94 73 73 75 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 ssue an error me
21a95 73 73 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 74 ssage if. ** it
21a96 20 64 6f 65 73 2e 20 54 68 65 20 65 78 63 65 70 does. The excep
21a97 74 69 6f 6e 20 69 73 20 69 66 20 74 68 65 20 73 tion is if the s
21a98 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 70 tatement being p
21a99 61 72 73 65 64 20 77 61 73 20 70 61 73 73 65 64 arsed was passed
21a9a 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c 69 . ** to an sqli
21a9b 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 te3_declare_vtab
21a9c 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 74 () call. In that
21a9d 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 63 case only the c
21a9e 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a olumn names. **
21a9f 20 61 6e 64 20 74 79 70 65 73 20 77 69 6c 6c 20 and types will
21aa0 62 65 20 75 73 65 64 2c 20 73 6f 20 74 68 65 72 be used, so ther
21aa1 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 e is no need to
21aa2 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 61 test for namespa
21aa3 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f ce. ** collisio
21aa4 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 ns.. */. if( !
21aa5 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
21aa6 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 ){. if( SQLIT
21aa7 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 E_OK!=sqlite3Rea
21aa8 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 dSchema(pParse)
21aa9 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 ){. goto be
21aaa 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b gin_table_error;
21aab 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 6c . }. pTabl
21aac 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 e = sqlite3FindT
21aad 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 able(db, zName,
21aae 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 db->aDb[iDb].zNa
21aaf 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 me);. if( pTa
21ab0 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ble ){. if(
21ab1 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 !noErr ){.
21ab2 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
21ab3 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c sg(pParse, "tabl
21ab4 65 20 25 54 20 61 6c 72 65 61 64 79 20 65 78 69 e %T already exi
21ab5 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 sts", pName);.
21ab6 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f }. goto
21ab7 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
21ab8 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 or;. }. if
21ab9 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 ( sqlite3FindInd
21aba 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 ex(db, zName, 0)
21abb 21 3d 30 20 26 26 20 28 69 44 62 3d 3d 30 20 7c !=0 && (iDb==0 |
21abc 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 | !db->init.busy
21abd 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) ){. sqlit
21abe 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
21abf 65 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 e, "there is alr
21ac0 65 61 64 79 20 61 6e 20 69 6e 64 65 78 20 6e 61 eady an index na
21ac1 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b med %s", zName);
21ac2 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 . goto begi
21ac3 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 n_table_error;.
21ac4 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 54 61 62 }. }.. pTab
21ac5 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 le = sqlite3DbMa
21ac6 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
21ac7 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 eof(Table));. i
21ac8 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 7b 0a f( pTable==0 ){.
21ac9 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
21aca 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 70 50 iled = 1;. pP
21acb 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 arse->rc = SQLIT
21acc 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70 50 61 E_NOMEM;. pPa
21acd 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 rse->nErr++;.
21ace 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c goto begin_tabl
21acf 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 e_error;. }. p
21ad0 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a Table->zName = z
21ad1 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65 2d 3e Name;. pTable->
21ad2 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 70 54 iPKey = -1;. pT
21ad3 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d 20 able->pSchema =
21ad4 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 db->aDb[iDb].pSc
21ad5 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65 2d 3e hema;. pTable->
21ad6 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62 nRef = 1;. pTab
21ad7 6c 65 2d 3e 64 62 4d 65 6d 20 3d 20 64 62 2d 3e le->dbMem = db->
21ad8 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c lookaside.bEnabl
21ad9 65 64 20 3f 20 64 62 20 3a 20 30 3b 0a 20 20 69 ed ? db : 0;. i
21ada 66 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 f( pParse->pNewT
21adb 61 62 6c 65 20 29 20 73 71 6c 69 74 65 33 44 65 able ) sqlite3De
21adc 6c 65 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 leteTable(pParse
21add 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 ->pNewTable);.
21ade 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
21adf 65 20 3d 20 70 54 61 62 6c 65 3b 0a 0a 20 20 2f e = pTable;.. /
21ae0 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 * If this is the
21ae1 20 6d 61 67 69 63 20 73 71 6c 69 74 65 5f 73 65 magic sqlite_se
21ae2 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 quence table use
21ae3 64 20 62 79 20 61 75 74 6f 69 6e 63 72 65 6d 65 d by autoincreme
21ae4 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 nt,. ** then re
21ae5 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 72 20 74 cord a pointer t
21ae6 6f 20 74 68 69 73 20 74 61 62 6c 65 20 69 6e 20 o this table in
21ae7 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
21ae8 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a e structure. **
21ae9 20 73 6f 20 74 68 61 74 20 49 4e 53 45 52 54 20 so that INSERT
21aea 63 61 6e 20 66 69 6e 64 20 74 68 65 20 74 61 62 can find the tab
21aeb 6c 65 20 65 61 73 69 6c 79 2e 0a 20 20 2a 2f 0a le easily.. */.
21aec 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21aed 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e MIT_AUTOINCREMEN
21aee 54 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d T. if( !pParse-
21aef 3e 6e 65 73 74 65 64 20 26 26 20 73 74 72 63 6d >nested && strcm
21af0 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 p(zName, "sqlite
21af1 5f 73 65 71 75 65 6e 63 65 22 29 3d 3d 30 20 29 _sequence")==0 )
21af2 7b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 {. pTable->pS
21af3 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 20 3d chema->pSeqTab =
21af4 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e pTable;. }.#en
21af5 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 dif.. /* Begin
21af6 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 generating the c
21af7 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e ode that will in
21af8 73 65 72 74 20 74 68 65 20 74 61 62 6c 65 20 72 sert the table r
21af9 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20 2a 2a 20 ecord into. **
21afa 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 the SQLITE_MASTE
21afb 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 20 69 R table. Note i
21afc 6e 20 70 61 72 74 69 63 75 6c 61 72 20 74 68 61 n particular tha
21afd 74 20 77 65 20 6d 75 73 74 20 67 6f 20 61 68 65 t we must go ahe
21afe 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61 6c 6c 6f ad. ** and allo
21aff 63 61 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 cate the record
21b00 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 number for the t
21b01 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f 77 2e 20 able entry now.
21b02 20 42 65 66 6f 72 65 20 61 6e 79 0a 20 20 2a 2a Before any. **
21b03 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 PRIMARY KEY or
21b04 55 4e 49 51 55 45 20 6b 65 79 77 6f 72 64 73 20 UNIQUE keywords
21b05 61 72 65 20 70 61 72 73 65 64 2e 20 20 54 68 6f are parsed. Tho
21b06 73 65 20 6b 65 79 77 6f 72 64 73 20 77 69 6c 6c se keywords will
21b07 20 63 61 75 73 65 0a 20 20 2a 2a 20 69 6e 64 69 cause. ** indi
21b08 63 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 ces to be create
21b09 64 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 d and the table
21b0a 72 65 63 6f 72 64 20 6d 75 73 74 20 63 6f 6d 65 record must come
21b0b 20 62 65 66 6f 72 65 20 74 68 65 20 0a 20 20 2a before the . *
21b0c 2a 20 69 6e 64 69 63 65 73 2e 20 20 48 65 6e 63 * indices. Henc
21b0d 65 2c 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 e, the record nu
21b0e 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 mber for the tab
21b0f 6c 65 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 le must be alloc
21b10 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77 2e 0a 20 ated. ** now..
21b11 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 */. if( !db->i
21b12 6e 69 74 2e 62 75 73 79 20 26 26 20 28 76 20 3d nit.busy && (v =
21b13 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
21b14 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20 pParse))!=0 ){.
21b15 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69 int j1;. i
21b16 6e 74 20 66 69 6c 65 46 6f 72 6d 61 74 3b 0a 20 nt fileFormat;.
21b17 20 20 20 69 6e 74 20 72 65 67 31 2c 20 72 65 67 int reg1, reg
21b18 32 2c 20 72 65 67 33 3b 0a 20 20 20 20 73 71 6c 2, reg3;. sql
21b19 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 ite3BeginWriteOp
21b1a 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 eration(pParse,
21b1b 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 0, iDb);..#ifnde
21b1c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
21b1d 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 RTUALTABLE. i
21b1e 66 28 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a f( isVirtual ){.
21b1f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21b20 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 eAddOp0(v, OP_VB
21b21 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e egin);. }.#en
21b22 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 dif.. /* If t
21b23 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 he file format a
21b24 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69 6e 20 74 nd encoding in t
21b25 68 65 20 64 61 74 61 62 61 73 65 20 68 61 76 65 he database have
21b26 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 0a not been set, .
21b27 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 6d 20 ** set them
21b28 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 now.. */.
21b29 72 65 67 31 20 3d 20 70 50 61 72 73 65 2d 3e 72 reg1 = pParse->r
21b2a 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 egRowid = ++pPar
21b2b 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 se->nMem;. re
21b2c 67 32 20 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 g2 = pParse->reg
21b2d 52 6f 6f 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d Root = ++pParse-
21b2e 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 33 20 >nMem;. reg3
21b2f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
21b30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21b31 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 eAddOp3(v, OP_Re
21b32 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72 adCookie, iDb, r
21b33 65 67 33 2c 20 31 29 3b 20 20 20 2f 2a 20 66 69 eg3, 1); /* fi
21b34 6c 65 5f 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 le_format */.
21b35 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 sqlite3VdbeUses
21b36 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 Btree(v, iDb);.
21b37 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 j1 = sqlite3V
21b38 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
21b39 49 66 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 66 If, reg3);. f
21b3a 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28 64 62 2d ileFormat = (db-
21b3b 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
21b3c 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 29 21 3d LegacyFileFmt)!=
21b3d 30 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 0 ?.
21b3e 20 20 20 20 20 20 31 20 3a 20 53 51 4c 49 54 45 1 : SQLITE
21b3f 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 _MAX_FILE_FORMAT
21b40 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
21b41 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
21b42 74 65 67 65 72 2c 20 66 69 6c 65 46 6f 72 6d 61 teger, fileForma
21b43 74 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 t, reg3);. sq
21b44 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21b45 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c v, OP_SetCookie,
21b46 20 69 44 62 2c 20 31 2c 20 72 65 67 33 29 3b 0a iDb, 1, reg3);.
21b47 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21b48 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
21b49 67 65 72 2c 20 45 4e 43 28 64 62 29 2c 20 72 65 ger, ENC(db), re
21b4a 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g3);. sqlite3
21b4b 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21b4c 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c _SetCookie, iDb,
21b4d 20 34 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 4, reg3);. s
21b4e 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
21b4f 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 re(v, j1);..
21b50 2f 2a 20 54 68 69 73 20 6a 75 73 74 20 63 72 65 /* This just cre
21b51 61 74 65 73 20 61 20 70 6c 61 63 65 2d 68 6f 6c ates a place-hol
21b52 64 65 72 20 72 65 63 6f 72 64 20 69 6e 20 74 68 der record in th
21b53 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 e sqlite_master
21b54 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68 table.. ** Th
21b55 65 20 72 65 63 6f 72 64 20 63 72 65 61 74 65 64 e record created
21b56 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 does not contai
21b57 6e 20 61 6e 79 74 68 69 6e 67 20 79 65 74 2e 20 n anything yet.
21b58 20 49 74 20 77 69 6c 6c 20 62 65 20 72 65 70 6c It will be repl
21b59 61 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 aced. ** by t
21b5a 68 65 20 72 65 61 6c 20 65 6e 74 72 79 20 69 6e he real entry in
21b5b 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 code generated
21b5c 61 74 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 at sqlite3EndTab
21b5d 6c 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 le().. **.
21b5e 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 66 6f ** The rowid fo
21b5f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 r the new entry
21b60 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69 73 is left in regis
21b61 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 ter pParse->regR
21b62 6f 77 69 64 2e 0a 20 20 20 20 2a 2a 20 54 68 65 owid.. ** The
21b63 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 root page numbe
21b64 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 r of the new tab
21b65 6c 65 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65 le is left in re
21b66 67 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f g pParse->regRoo
21b67 74 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f t.. ** The ro
21b68 77 69 64 20 61 6e 64 20 72 6f 6f 74 20 70 61 67 wid and root pag
21b69 65 20 6e 75 6d 62 65 72 20 76 61 6c 75 65 73 20 e number values
21b6a 61 72 65 20 6e 65 65 64 65 64 20 62 79 20 74 68 are needed by th
21b6b 65 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20 e code that.
21b6c 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 ** sqlite3EndTab
21b6d 6c 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 le will generate
21b6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21 64 65 .. */.#if !de
21b6f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
21b70 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 T_VIEW) || !defi
21b71 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
21b72 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20 VIRTUALTABLE).
21b73 20 20 69 66 28 20 69 73 56 69 65 77 20 7c 7c 20 if( isView ||
21b74 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 isVirtual ){.
21b75 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21b76 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
21b77 65 72 2c 20 30 2c 20 72 65 67 32 29 3b 0a 20 20 er, 0, reg2);.
21b78 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 }else.#endif.
21b79 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
21b7a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21b7b 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c 20 OP_CreateTable,
21b7c 69 44 62 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 iDb, reg2);.
21b7d 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 }. sqlite3Ope
21b7e 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50 61 nMasterTable(pPa
21b7f 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 rse, iDb);. s
21b80 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21b81 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c (v, OP_NewRowid,
21b82 20 30 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73 0, reg1);. s
21b83 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21b84 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
21b85 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 reg3);. sqlit
21b86 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
21b87 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 72 65 OP_Insert, 0, re
21b88 67 33 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73 g3, reg1);. s
21b89 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
21b8a 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 P5(v, OPFLAG_APP
21b8b 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 END);. sqlite
21b8c 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 3VdbeAddOp0(v, O
21b8d 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 0a 20 P_Close);. }..
21b8e 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f 6e 2d /* Normal (non-
21b8f 65 72 72 6f 72 29 20 72 65 74 75 72 6e 2e 20 2a error) return. *
21b90 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f /. return;.. /
21b91 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
21b92 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 20 68 65 curs, we jump he
21b93 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61 62 6c re */.begin_tabl
21b94 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 e_error:. sqlit
21b95 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 e3DbFree(db, zNa
21b96 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d me);. return;.}
21b97 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 ../*.** This mac
21b98 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f ro is used to co
21b99 6d 70 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 mpare two string
21b9a 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e 73 65 s in a case-inse
21b9b 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 72 2e 0a nsitive manner..
21b9c 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 68 74 6c ** It is slightl
21b9d 79 20 66 61 73 74 65 72 20 74 68 61 6e 20 63 61 y faster than ca
21b9e 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 53 74 72 lling sqlite3Str
21b9f 49 43 6d 70 28 29 20 64 69 72 65 63 74 6c 79 2c ICmp() directly,
21ba0 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63 65 73 but.** produces
21ba1 20 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a 2a 2a larger code..**
21ba2 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54 68 69 .** WARNING: Thi
21ba3 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 63 s macro is not c
21ba4 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74 ompatible with t
21ba5 68 65 20 73 74 72 63 6d 70 28 29 20 66 61 6d 69 he strcmp() fami
21ba6 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75 72 6e ly. It.** return
21ba7 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74 77 s true if the tw
21ba8 6f 20 73 74 72 69 6e 67 73 20 61 72 65 20 65 71 o strings are eq
21ba9 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 65 20 66 ual, otherwise f
21baa 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 alse..*/.#define
21bab 20 53 54 52 49 43 4d 50 28 78 2c 20 79 29 20 28 STRICMP(x, y) (
21bac 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f \.sqlite3UpperTo
21bad 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64 Lower[*(unsigned
21bae 20 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d 20 20 char *)(x)]==
21baf 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54 \.sqlite3UpperT
21bb0 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 oLower[*(unsigne
21bb1 64 20 63 68 61 72 20 2a 29 28 79 29 5d 20 20 20 d char *)(y)]
21bb2 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 33 53 74 \.&& sqlite3St
21bb3 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 79 29 2b rICmp((x)+1,(y)+
21bb4 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41 1)==0 )../*.** A
21bb5 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 dd a new column
21bb6 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 to the table cur
21bb7 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e rently being con
21bb8 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 structed..**.**
21bb9 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 The parser calls
21bba 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e this routine on
21bbb 63 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 ce for each colu
21bbc 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a mn declaration.*
21bbd 2a 20 69 6e 20 61 20 43 52 45 41 54 45 20 54 41 * in a CREATE TA
21bbe 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 BLE statement.
21bbf 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c sqlite3StartTabl
21bc0 65 28 29 20 67 65 74 73 20 63 61 6c 6c 65 64 0a e() gets called.
21bc1 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 65 74 20 ** first to get
21bc2 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 20 54 things going. T
21bc3 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
21bc4 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 65 is called for e
21bc5 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a ach.** column..*
21bc6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21bc7 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
21bc8 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 Column(Parse *pP
21bc9 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 arse, Token *pNa
21bca 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b me){. Table *p;
21bcb 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 . int i;. char
21bcc 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 *z;. Column *p
21bcd 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a Col;. sqlite3 *
21bce 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
21bcf 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 . if( (p = pPar
21bd0 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d se->pNewTable)==
21bd1 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 20 0 ) return;.#if
21bd2 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d SQLITE_MAX_COLUM
21bd3 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b N. if( p->nCol+
21bd4 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 1>db->aLimit[SQL
21bd5 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e ITE_LIMIT_COLUMN
21bd6 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ] ){. sqlite3
21bd7 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
21bd8 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d "too many colum
21bd9 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e ns on %s", p->zN
21bda 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
21bdb 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7a ;. }.#endif. z
21bdc 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
21bdd 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d omToken(db, pNam
21bde 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 e);. if( z==0 )
21bdf 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 return;. for(i
21be0 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 =0; i<p->nCol; i
21be1 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 54 52 ++){. if( STR
21be2 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b ICMP(z, p->aCol[
21be3 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 i].zName) ){.
21be4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
21be5 73 67 28 70 50 61 72 73 65 2c 20 22 64 75 70 6c sg(pParse, "dupl
21be6 69 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d icate column nam
21be7 65 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 e: %s", z);.
21be8 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
21be9 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 db, z);. re
21bea 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a turn;. }. }.
21beb 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c 20 26 if( (p->nCol &
21bec 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 0x7)==0 ){.
21bed 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20 Column *aNew;.
21bee 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 aNew = sqlite3
21bef 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e DbRealloc(db,p->
21bf0 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29 aCol,(p->nCol+8)
21bf1 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b *sizeof(p->aCol[
21bf2 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 0]));. if( aN
21bf3 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 ew==0 ){. s
21bf4 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
21bf5 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 z);. retur
21bf6 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e n;. }. p->
21bf7 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d aCol = aNew;. }
21bf8 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 . pCol = &p->aC
21bf9 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d ol[p->nCol];. m
21bfa 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73 emset(pCol, 0, s
21bfb 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d izeof(p->aCol[0]
21bfc 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d ));. pCol->zNam
21bfd 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66 e = z;. . /* If
21bfe 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74 79 70 there is no typ
21bff 65 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f 6c e specified, col
21c00 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20 64 65 umns have the de
21c01 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79 0a 20 fault affinity.
21c02 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49 66 20 74 ** 'NONE'. If t
21c03 68 65 72 65 20 69 73 20 61 20 74 79 70 65 20 73 here is a type s
21c04 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 73 pecified, then s
21c05 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 qlite3AddColumnT
21c06 79 70 65 28 29 20 77 69 6c 6c 0a 20 20 2a 2a 20 ype() will. **
21c07 62 65 20 63 61 6c 6c 65 64 20 6e 65 78 74 20 74 be called next t
21c08 6f 20 73 65 74 20 70 43 6f 6c 2d 3e 61 66 66 69 o set pCol->affi
21c09 6e 69 74 79 20 63 6f 72 72 65 63 74 6c 79 2e 0a nity correctly..
21c0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 */. pCol->aff
21c0b 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
21c0c 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43 FF_NONE;. p->nC
21c0d 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ol++;.}../*.** T
21c0e 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
21c0f 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 alled by the par
21c10 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 ser while in the
21c11 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 middle of.** pa
21c12 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 rsing a CREATE T
21c13 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 ABLE statement.
21c14 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f A "NOT NULL" co
21c15 6e 73 74 72 61 69 6e 74 20 68 61 73 0a 2a 2a 20 nstraint has.**
21c16 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61 20 63 been seen on a c
21c17 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72 6f 75 olumn. This rou
21c18 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 6e 6f tine sets the no
21c19 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a tNull flag on.**
21c1a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75 72 72 the column curr
21c1b 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 ently under cons
21c1c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c truction..*/.SQL
21c1d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21c1e 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 sqlite3AddNotNu
21c1f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ll(Parse *pParse
21c20 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a , int onError){.
21c21 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e Table *p;. in
21c22 74 20 69 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 t i;. if( (p =
21c23 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
21c24 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a e)==0 ) return;.
21c25 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b i = p->nCol-1;
21c26 0a 20 20 69 66 28 20 69 3e 3d 30 20 29 20 70 2d . if( i>=0 ) p-
21c27 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c >aCol[i].notNull
21c28 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a = (u8)onError;.
21c29 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 }../*.** Scan th
21c2a 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61 e column type na
21c2b 6d 65 20 7a 54 79 70 65 20 28 6c 65 6e 67 74 68 me zType (length
21c2c 20 6e 54 79 70 65 29 20 61 6e 64 20 72 65 74 75 nType) and retu
21c2d 72 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69 rn the.** associ
21c2e 61 74 65 64 20 61 66 66 69 6e 69 74 79 20 74 79 ated affinity ty
21c2f 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 pe..**.** This r
21c30 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63 61 outine does a ca
21c31 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 se-independent s
21c32 65 61 72 63 68 20 6f 66 20 7a 54 79 70 65 20 66 earch of zType f
21c33 6f 72 20 74 68 65 20 0a 2a 2a 20 73 75 62 73 74 or the .** subst
21c34 72 69 6e 67 73 20 69 6e 20 74 68 65 20 66 6f 6c rings in the fol
21c35 6c 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20 49 66 lowing table. If
21c36 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 one of the subs
21c37 74 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75 trings is.** fou
21c38 6e 64 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f nd, the correspo
21c39 6e 64 69 6e 67 20 61 66 66 69 6e 69 74 79 20 69 nding affinity i
21c3a 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 7a s returned. If z
21c3b 54 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a Type contains.**
21c3c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f more than one o
21c3d 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 f the substrings
21c3e 2c 20 65 6e 74 72 69 65 73 20 74 6f 77 61 72 64 , entries toward
21c3f 20 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 the top of .**
21c40 74 68 65 20 74 61 62 6c 65 20 74 61 6b 65 20 70 the table take p
21c41 72 69 6f 72 69 74 79 2e 20 46 6f 72 20 65 78 61 riority. For exa
21c42 6d 70 6c 65 2c 20 69 66 20 7a 54 79 70 65 20 69 mple, if zType i
21c43 73 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a s 'BLOBINT', .**
21c44 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 SQLITE_AFF_INTE
21c45 47 45 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e GER is returned.
21c46 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67 .**.** Substring
21c47 20 20 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0a | Affinity.
21c48 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -------------
21c49 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
21c4a 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20 ---.** 'INT'
21c4b 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 | SQLITE_AF
21c4c 46 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48 F_INTEGER.** 'CH
21c4d 41 52 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c AR' | SQL
21c4e 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 ITE_AFF_TEXT.**
21c4f 27 43 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 'CLOB' |
21c50 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a SQLITE_AFF_TEXT.
21c51 2a 2a 20 27 54 45 58 54 27 20 20 20 20 20 20 20 ** 'TEXT'
21c52 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 | SQLITE_AFF_TE
21c53 58 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20 XT.** 'BLOB'
21c54 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 | SQLITE_AFF
21c55 5f 4e 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c 27 20 _NONE.** 'REAL'
21c56 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f | SQLITE_
21c57 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f AFF_REAL.** 'FLO
21c58 41 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 A' | SQLI
21c59 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 TE_AFF_REAL.** '
21c5a 44 4f 55 42 27 20 20 20 20 20 20 20 20 7c 20 53 DOUB' | S
21c5b 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a QLITE_AFF_REAL.*
21c5c 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 *.** If none of
21c5d 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 the substrings i
21c5e 6e 20 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c n the above tabl
21c5f 65 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 e are found,.**
21c60 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
21c61 49 43 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a IC is returned..
21c62 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21c63 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 E char sqlite3Af
21c64 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 finityType(const
21c65 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a Token *pType){.
21c66 20 20 75 33 32 20 68 20 3d 20 30 3b 0a 20 20 63 u32 h = 0;. c
21c67 68 61 72 20 61 66 66 20 3d 20 53 51 4c 49 54 45 har aff = SQLITE
21c68 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 _AFF_NUMERIC;.
21c69 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
21c6a 68 61 72 20 2a 7a 49 6e 20 3d 20 70 54 79 70 65 har *zIn = pType
21c6b 2d 3e 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 ->z;. const uns
21c6c 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 6e 64 igned char *zEnd
21c6d 20 3d 20 26 70 54 79 70 65 2d 3e 7a 5b 70 54 79 = &pType->z[pTy
21c6e 70 65 2d 3e 6e 5d 3b 0a 0a 20 20 77 68 69 6c 65 pe->n];.. while
21c6f 28 20 7a 49 6e 21 3d 7a 45 6e 64 20 29 7b 0a 20 ( zIn!=zEnd ){.
21c70 20 20 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b 20 h = (h<<8) +
21c71 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
21c72 77 65 72 5b 2a 7a 49 6e 5d 3b 0a 20 20 20 20 7a wer[*zIn];. z
21c73 49 6e 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3d In++;. if( h=
21c74 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 68 27 =(('c'<<24)+('h'
21c75 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 <<16)+('a'<<8)+'
21c76 72 27 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 r') ){
21c77 20 20 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20 20 /* CHAR */.
21c78 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
21c79 5f 41 46 46 5f 54 45 58 54 3b 20 0a 20 20 20 20 _AFF_TEXT; .
21c7a 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 }else if( h==(('
21c7b 63 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 c'<<24)+('l'<<16
21c7c 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 )+('o'<<8)+'b')
21c7d 29 7b 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f 42 ){ /* CLOB
21c7e 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 */. aff =
21c7f 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b SQLITE_AFF_TEXT;
21c80 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 . }else if( h
21c81 3d 3d 28 28 27 74 27 3c 3c 32 34 29 2b 28 27 65 ==(('t'<<24)+('e
21c82 27 3c 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29 2b '<<16)+('x'<<8)+
21c83 27 74 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 't') ){ /*
21c84 20 54 45 58 54 20 2a 2f 0a 20 20 20 20 20 20 61 TEXT */. a
21c85 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f ff = SQLITE_AFF_
21c86 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 TEXT;. }else
21c87 69 66 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32 34 if( h==(('b'<<24
21c88 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 )+('l'<<16)+('o'
21c89 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 <<8)+'b')
21c8a 20 20 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 /* BLOB */.
21c8b 20 20 20 20 20 20 26 26 20 28 61 66 66 3d 3d 53 && (aff==S
21c8c 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
21c8d 43 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 C || aff==SQLITE
21c8e 5f 41 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20 20 _AFF_REAL) ){.
21c8f 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
21c90 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 _AFF_NONE;.#ifnd
21c91 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
21c92 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
21c93 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 }else if( h==(
21c94 28 27 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c ('r'<<24)+('e'<<
21c95 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27 16)+('a'<<8)+'l'
21c96 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 ) /* RE
21c97 41 4c 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 AL */. &&
21c98 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 aff==SQLITE_AFF
21c99 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 _NUMERIC ){.
21c9a 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 aff = SQLITE_A
21c9b 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c FF_REAL;. }el
21c9c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 66 27 3c se if( h==(('f'<
21c9d 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 <24)+('l'<<16)+(
21c9e 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 20 'o'<<8)+'a')
21c9f 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f /* FLOA */
21ca0 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d . && aff=
21ca1 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 =SQLITE_AFF_NUME
21ca2 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 RIC ){. aff
21ca3 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 = SQLITE_AFF_RE
21ca4 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 AL;. }else if
21ca5 28 20 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 2b ( h==(('d'<<24)+
21ca6 28 27 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c 3c ('o'<<16)+('u'<<
21ca7 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 8)+'b')
21ca8 20 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 20 /* DOUB */.
21ca9 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 && aff==SQLI
21caa 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 TE_AFF_NUMERIC )
21cab 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 {. aff = SQ
21cac 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 LITE_AFF_REAL;.#
21cad 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 endif. }else
21cae 69 66 28 20 28 68 26 30 78 30 30 46 46 46 46 46 if( (h&0x00FFFFF
21caf 46 29 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b 28 F)==(('i'<<16)+(
21cb0 27 6e 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 'n'<<8)+'t') ){
21cb1 20 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 /* INT */.
21cb2 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f aff = SQLITE_
21cb3 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
21cb4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21cb5 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 66 }.. return af
21cb6 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 f;.}../*.** This
21cb7 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
21cb8 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 ed by the parser
21cb9 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 while in the mi
21cba 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 ddle of.** parsi
21cbb 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c ng a CREATE TABL
21cbc 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 E statement. Th
21cbd 65 20 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69 e pFirst token i
21cbe 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 s the first.** t
21cbf 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75 oken in the sequ
21cc0 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74 ence of tokens t
21cc1 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 hat describe the
21cc2 20 74 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20 type of the.**
21cc3 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 column currently
21cc4 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 under construct
21cc5 69 6f 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20 ion. pLast is
21cc6 74 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a the last token.*
21cc7 2a 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 * in the sequenc
21cc8 65 2e 20 20 55 73 65 20 74 68 69 73 20 69 6e 66 e. Use this inf
21cc9 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 ormation to cons
21cca 74 72 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a truct a string.*
21ccb 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 * that contains
21ccc 74 68 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20 the typename of
21ccd 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 the column and s
21cce 74 6f 72 65 20 74 68 61 74 20 73 74 72 69 6e 67 tore that string
21ccf 0a 2a 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f .** in zType..*/
21cd0 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 .SQLITE_PRIVATE
21cd1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
21cd2 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 ColumnType(Parse
21cd3 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 *pParse, Token
21cd4 2a 70 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65 *pType){. Table
21cd5 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 *p;. int i;.
21cd6 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 Column *pCol;.
21cd7 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 sqlite3 *db;..
21cd8 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d if( (p = pParse-
21cd9 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 >pNewTable)==0 )
21cda 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 return;. i = p
21cdb 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 ->nCol-1;. if(
21cdc 69 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 i<0 ) return;.
21cdd 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b pCol = &p->aCol[
21cde 69 5d 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 i];. db = pPars
21cdf 65 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 e->db;. sqlite3
21ce0 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d DbFree(db, pCol-
21ce1 3e 7a 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d >zType);. pCol-
21ce2 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 >zType = sqlite3
21ce3 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
21ce4 2c 20 70 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c , pType);. pCol
21ce5 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c ->affinity = sql
21ce6 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 ite3AffinityType
21ce7 28 70 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a (pType);.}../*.*
21ce8 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
21ce9 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 is the default
21cea 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6d 6f value for the mo
21ceb 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 st recently adde
21cec 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 d column.** of t
21ced 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 he table current
21cee 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 ly under constru
21cef 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 ction..**.** Def
21cf0 61 75 6c 74 20 76 61 6c 75 65 20 65 78 70 72 65 ault value expre
21cf1 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 63 ssions must be c
21cf2 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 73 65 20 onstant. Raise
21cf3 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66 20 an exception if
21cf4 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 this.** is not t
21cf5 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 he case..**.** T
21cf6 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
21cf7 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 alled by the par
21cf8 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 ser while in the
21cf9 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 middle of.** pa
21cfa 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 rsing a CREATE T
21cfb 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a ABLE statement..
21cfc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21cfd 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 E void sqlite3Ad
21cfe 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61 dDefaultValue(Pa
21cff 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
21d00 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 54 61 62 r *pExpr){. Tab
21d01 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 le *p;. Column
21d02 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 *pCol;. sqlite3
21d03 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
21d04 62 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 b;. if( (p = pP
21d05 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 arse->pNewTable)
21d06 21 3d 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 20 !=0 ){. pCol
21d07 3d 20 26 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e = &(p->aCol[p->n
21d08 43 6f 6c 2d 31 5d 29 3b 0a 20 20 20 20 69 66 28 Col-1]);. if(
21d09 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 !sqlite3ExprIsC
21d0a 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f onstantOrFunctio
21d0b 6e 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 n(pExpr) ){.
21d0c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
21d0d 67 28 70 50 61 72 73 65 2c 20 22 64 65 66 61 75 g(pParse, "defau
21d0e 6c 74 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 lt value of colu
21d0f 6d 6e 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63 mn [%s] is not c
21d10 6f 6e 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 20 onstant",.
21d11 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 pCol->zName)
21d12 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
21d13 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 20 /* A copy of
21d14 70 45 78 70 72 20 69 73 20 75 73 65 64 20 69 6e pExpr is used in
21d15 73 74 65 61 64 20 6f 66 20 74 68 65 20 6f 72 69 stead of the ori
21d16 67 69 6e 61 6c 2c 20 61 73 20 70 45 78 70 72 20 ginal, as pExpr
21d17 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a contains. *
21d18 2a 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 70 6f * tokens that po
21d19 69 6e 74 20 74 6f 20 76 6f 6c 61 74 69 6c 65 20 int to volatile
21d1a 6d 65 6d 6f 72 79 2e 20 54 68 65 20 27 73 70 61 memory. The 'spa
21d1b 6e 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 n' of the expres
21d1c 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 sion. ** is
21d1d 20 72 65 71 75 69 72 65 64 20 62 79 20 70 72 61 required by pra
21d1e 67 6d 61 20 74 61 62 6c 65 5f 69 6e 66 6f 2e 0a gma table_info..
21d1f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
21d20 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
21d21 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 (db, pCol->pDflt
21d22 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 );. pCol->p
21d23 44 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 45 78 Dflt = sqlite3Ex
21d24 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2c prDup(db, pExpr,
21d25 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 7c EXPRDUP_REDUCE|
21d26 45 58 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a 20 EXPRDUP_SPAN);.
21d27 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
21d28 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
21d29 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pExpr);.}../*.*
21d2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74 68 65 20 * Designate the
21d2b 50 52 49 4d 41 52 59 20 4b 45 59 20 66 6f 72 20 PRIMARY KEY for
21d2c 74 68 65 20 74 61 62 6c 65 2e 20 20 70 4c 69 73 the table. pLis
21d2d 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6e t is a list of n
21d2e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75 ames .** of colu
21d2f 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68 mns that form th
21d30 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 e primary key.
21d31 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c If pList is NULL
21d32 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f , then the.** mo
21d33 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 st recently adde
21d34 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 d column of the
21d35 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 72 69 table is the pri
21d36 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 mary key..**.**
21d37 41 20 74 61 62 6c 65 20 63 61 6e 20 68 61 76 65 A table can have
21d38 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69 at most one pri
21d39 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20 74 68 mary key. If th
21d3a 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 e table already
21d3b 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79 has.** a primary
21d3c 20 6b 65 79 20 28 61 6e 64 20 74 68 69 73 20 69 key (and this i
21d3d 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 69 s the second pri
21d3e 6d 61 72 79 20 6b 65 79 29 20 74 68 65 6e 20 63 mary key) then c
21d3f 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f reate an.** erro
21d40 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
21d41 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 6f PRIMARY KEY is o
21d42 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d n a single colum
21d43 6e 20 77 68 6f 73 65 20 64 61 74 61 74 79 70 65 n whose datatype
21d44 20 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20 is INTEGER,.**
21d45 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 74 72 79 then we will try
21d46 20 74 6f 20 75 73 65 20 74 68 61 74 20 63 6f 6c to use that col
21d47 75 6d 6e 20 61 73 20 74 68 65 20 72 6f 77 69 64 umn as the rowid
21d48 2e 20 20 53 65 74 20 74 68 65 20 54 61 62 6c 65 . Set the Table
21d49 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20 .iPKey.** field
21d4a 6f 66 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 of the table und
21d4b 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 er construction
21d4c 74 6f 20 62 65 20 74 68 65 20 69 6e 64 65 78 20 to be the index
21d4d 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45 of the.** INTEGE
21d4e 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f R PRIMARY KEY co
21d4f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b lumn. Table.iPK
21d50 65 79 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20 ey is set to -1
21d51 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e if there is.** n
21d52 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 o INTEGER PRIMAR
21d53 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 Y KEY..**.** If
21d54 74 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 61 the key is not a
21d55 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 n INTEGER PRIMAR
21d56 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 72 65 61 Y KEY, then crea
21d57 74 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69 te a unique.** i
21d58 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6b 65 79 ndex for the key
21d59 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 73 20 63 . No index is c
21d5a 72 65 61 74 65 64 20 66 6f 72 20 49 4e 54 45 47 reated for INTEG
21d5b 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 73 2e ER PRIMARY KEYs.
21d5c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
21d5d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
21d5e 64 64 50 72 69 6d 61 72 79 4b 65 79 28 0a 20 20 ddPrimaryKey(.
21d5f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
21d60 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
21d61 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 text */. ExprLi
21d62 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c st *pList, /* L
21d63 69 73 74 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d ist of field nam
21d64 65 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 es to be indexed
21d65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f */. int onErro
21d66 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 r, /* What
21d67 74 6f 20 64 6f 20 77 69 74 68 20 61 20 75 6e 69 to do with a uni
21d68 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 queness conflict
21d69 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e */. int autoIn
21d6a 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 c, /* True
21d6b 69 66 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45 if the AUTOINCRE
21d6c 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 69 73 20 MENT keyword is
21d6d 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 present */. int
21d6e 20 73 6f 72 74 4f 72 64 65 72 20 20 20 20 20 2f sortOrder /
21d6f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 * SQLITE_SO_ASC
21d70 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 or SQLITE_SO_DES
21d71 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 C */.){. Table
21d72 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e *pTab = pParse->
21d73 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 63 68 61 pNewTable;. cha
21d74 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 r *zType = 0;.
21d75 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69 int iCol = -1, i
21d76 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 ;. if( pTab==0
21d77 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 || IN_DECLARE_VT
21d78 41 42 20 29 20 67 6f 74 6f 20 70 72 69 6d 61 72 AB ) goto primar
21d79 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 69 66 y_key_exit;. if
21d7a 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 ( pTab->tabFlags
21d7b 20 26 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 & TF_HasPrimary
21d7c 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Key ){. sqlit
21d7d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
21d7e 65 2c 20 0a 20 20 20 20 20 20 22 74 61 62 6c 65 e, . "table
21d7f 20 5c 22 25 73 5c 22 20 68 61 73 20 6d 6f 72 65 \"%s\" has more
21d80 20 74 68 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72 than one primar
21d81 79 20 6b 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e y key", pTab->zN
21d82 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70 ame);. goto p
21d83 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b rimary_key_exit;
21d84 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62 . }. pTab->tab
21d85 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 73 50 Flags |= TF_HasP
21d86 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20 69 66 28 rimaryKey;. if(
21d87 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
21d88 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 iCol = pTab->nC
21d89 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70 54 61 62 ol - 1;. pTab
21d8a 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 ->aCol[iCol].isP
21d8b 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 7d 65 rimKey = 1;. }e
21d8c 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 lse{. for(i=0
21d8d 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 ; i<pList->nExpr
21d8e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f ; i++){. fo
21d8f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 r(iCol=0; iCol<p
21d90 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b Tab->nCol; iCol+
21d91 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +){. if(
21d92 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 sqlite3StrICmp(p
21d93 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 List->a[i].zName
21d94 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f , pTab->aCol[iCo
21d95 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a l].zName)==0 ){.
21d96 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
21d97 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
21d98 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f }. if( iCo
21d99 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a l<pTab->nCol ){.
21d9a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 pTab->aC
21d9b 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b ol[iCol].isPrimK
21d9c 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a ey = 1;. }.
21d9d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c }. if( pL
21d9e 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20 29 20 69 ist->nExpr>1 ) i
21d9f 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 Col = -1;. }.
21da0 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 if( iCol>=0 && i
21da1 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 Col<pTab->nCol )
21da2 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54 {. zType = pT
21da3 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a ab->aCol[iCol].z
21da4 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Type;. }. if(
21da5 7a 54 79 70 65 20 26 26 20 73 71 6c 69 74 65 33 zType && sqlite3
21da6 53 74 72 49 43 6d 70 28 7a 54 79 70 65 2c 20 22 StrICmp(zType, "
21da7 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a 20 20 20 INTEGER")==0.
21da8 20 20 20 20 20 26 26 20 73 6f 72 74 4f 72 64 65 && sortOrde
21da9 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 r==SQLITE_SO_ASC
21daa 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 69 50 ){. pTab->iP
21dab 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 Key = iCol;.
21dac 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20 pTab->keyConf =
21dad 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 (u8)onError;.
21dae 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 6e 63 assert( autoInc
21daf 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 3d 3d ==0 || autoInc==
21db0 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e 74 1 );. pTab->t
21db1 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 6f 49 abFlags |= autoI
21db2 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d nc*TF_Autoincrem
21db3 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ent;. }else if(
21db4 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 66 6e autoInc ){.#ifn
21db5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
21db6 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 AUTOINCREMENT.
21db7 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
21db8 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 4f 49 g(pParse, "AUTOI
21db9 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e 6c 79 NCREMENT is only
21dba 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e 20 22 allowed on an "
21dbb 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 52 . "INTEGER
21dbc 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b 0a PRIMARY KEY");.
21dbd 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a #endif. }else{.
21dbe 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 sqlite3Creat
21dbf 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 30 eIndex(pParse, 0
21dc0 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 20 6f , 0, 0, pList, o
21dc1 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20 73 6f nError, 0, 0, so
21dc2 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20 20 20 rtOrder, 0);.
21dc3 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d 0a pList = 0;. }.
21dc4 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 .primary_key_exi
21dc5 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 t:. sqlite3Expr
21dc6 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 ListDelete(pPars
21dc7 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 e->db, pList);.
21dc8 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a return;.}../*.*
21dc9 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45 43 * Add a new CHEC
21dca 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 K constraint to
21dcb 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e the table curren
21dcc 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 tly under constr
21dcd 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 uction..*/.SQLIT
21dce 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21dcf 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f qlite3AddCheckCo
21dd0 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72 73 nstraint(. Pars
21dd1 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a e *pParse, /*
21dd2 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
21dd3 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68 65 */. Expr *pChe
21dd4 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20 63 ckExpr /* The c
21dd5 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e 20 heck expression
21dd6 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 */.){. sqlite3
21dd7 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
21dd8 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
21dd9 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54 61 _OMIT_CHECK. Ta
21dda 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 ble *pTab = pPar
21ddb 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 se->pNewTable;.
21ddc 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49 4e if( pTab && !IN
21ddd 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b _DECLARE_VTAB ){
21dde 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 48 45 43 . /* The CHEC
21ddf 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 K expression mus
21de0 74 20 62 65 20 64 75 70 6c 69 63 61 74 65 64 20 t be duplicated
21de1 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e 73 20 72 so that tokens r
21de2 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20 6d efer. ** to m
21de3 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 61 6e alloced space an
21de4 64 20 6e 6f 74 20 74 68 65 20 28 65 70 68 65 6d d not the (ephem
21de5 65 72 61 6c 29 20 74 65 78 74 20 6f 66 20 74 68 eral) text of th
21de6 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a 20 e CREATE TABLE.
21de7 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 ** statement
21de8 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e 70 43 68 */. pTab->pCh
21de9 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 eck = sqlite3Exp
21dea 72 41 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e 70 rAnd(db, pTab->p
21deb 43 68 65 63 6b 2c 20 0a 20 20 20 20 20 20 20 20 Check, .
21dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ded 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21dee 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 68 3ExprDup(db, pCh
21def 65 63 6b 45 78 70 72 2c 20 30 29 29 3b 0a 20 20 eckExpr, 0));.
21df0 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 }.#endif. sqlit
21df1 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
21df2 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 7d 0a pCheckExpr);.}.
21df3 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 ./*.** Set the c
21df4 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f ollation functio
21df5 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 n of the most re
21df6 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61 cently parsed ta
21df7 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f ble column.** to
21df8 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76 the CollSeq giv
21df9 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 en..*/.SQLITE_PR
21dfa 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
21dfb 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 e3AddCollateType
21dfc 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21dfd 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a Token *pToken){.
21dfe 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e Table *p;. in
21dff 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f t i;. char *zCo
21e00 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
21e01 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d /* Dequoted nam
21e02 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 e of collation s
21e03 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c equence */. sql
21e04 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 ite3 *db;.. if(
21e05 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e (p = pParse->pN
21e06 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 ewTable)==0 ) re
21e07 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e turn;. i = p->n
21e08 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50 Col-1;. db = pP
21e09 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c arse->db;. zCol
21e0a 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 l = sqlite3NameF
21e0b 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f romToken(db, pTo
21e0c 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f ken);. if( !zCo
21e0d 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 ll ) return;..
21e0e 69 66 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 if( sqlite3Locat
21e0f 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
21e10 20 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 zColl, -1) ){.
21e11 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a Index *pIdx;.
21e12 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a p->aCol[i].z
21e13 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 Coll = zColl;.
21e14 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 . /* If the c
21e15 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 olumn is declare
21e16 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 d as "<name> PRI
21e17 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 MARY KEY COLLATE
21e18 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a <type>",. **
21e19 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d then an index m
21e1a 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 ay have been cre
21e1b 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c ated on this col
21e1c 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 umn before the.
21e1d 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 ** collation
21e1e 74 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 type was added.
21e1f 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20 Correct this if
21e20 69 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a it is the case..
21e21 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 */. for(p
21e22 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 Idx=p->pIndex; p
21e23 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
21e24 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 pNext){. as
21e25 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c sert( pIdx->nCol
21e26 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 umn==1 );.
21e27 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 if( pIdx->aiColu
21e28 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 mn[0]==i ){.
21e29 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c pIdx->azColl
21e2a 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d [0] = p->aCol[i]
21e2b 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a .zColl;. }.
21e2c 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
21e2d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
21e2e 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d (db, zColl);. }
21e2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
21e30 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
21e31 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
21e32 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 quence for datab
21e33 61 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a ase native text.
21e34 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e ** encoding iden
21e35 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74 tified by the st
21e36 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 ring zName, leng
21e37 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 th nName..**.**
21e38 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
21e39 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
21e3a 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c nce is not avail
21e3b 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 able, or not ava
21e3c 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 ilable.** in the
21e3d 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 database native
21e3e 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 encoding, the c
21e3f 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 ollation factory
21e40 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a is invoked to.*
21e41 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 * request it. If
21e42 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 the collation f
21e43 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 actory does not
21e44 73 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 supply such a se
21e45 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 quence,.** and t
21e46 68 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 he sequence is a
21e47 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 vailable in anot
21e48 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e her text encodin
21e49 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a g, then that is.
21e4a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 ** returned inst
21e4b 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f ead..**.** If no
21e4c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 versions of the
21e4d 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 requested colla
21e4e 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 tions sequence a
21e4f 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 re available, or
21e50 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f .** another erro
21e51 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 r occurs, NULL i
21e52 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 s returned and a
21e53 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
21e54 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 written into.**
21e55 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 pParse..**.** Th
21e56 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
21e57 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 wrapper around s
21e58 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 qlite3FindCollSe
21e59 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 q(). This routi
21e5a 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 ne.** invokes th
21e5b 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 e collation fact
21e5c 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 ory if the named
21e5d 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f collation canno
21e5e 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e t be found.** an
21e5f 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 d generates an e
21e60 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f rror message..*/
21e61 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21e62 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
21e63 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 LocateCollSeq(Pa
21e64 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e rse *pParse, con
21e65 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
21e66 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 73 71 int nName){. sq
21e67 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
21e68 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 se->db;. u8 enc
21e69 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 = ENC(db);. u8
21e6a 20 69 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e initbusy = db->
21e6b 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c init.busy;. Col
21e6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 lSeq *pColl;..
21e6d 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 pColl = sqlite3F
21e6e 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 indCollSeq(db, e
21e6f 6e 63 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 nc, zName, nName
21e70 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69 , initbusy);. i
21e71 66 28 20 21 69 6e 69 74 62 75 73 79 20 26 26 20 f( !initbusy &&
21e72 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c (!pColl || !pCol
21e73 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20 l->xCmp) ){.
21e74 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 pColl = sqlite3G
21e75 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 43 etCollSeq(db, pC
21e76 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d oll, zName, nNam
21e77 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f e);. if( !pCo
21e78 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
21e79 6e 4e 61 6d 65 3c 30 20 29 7b 0a 20 20 20 20 20 nName<0 ){.
21e7a 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 nName = sqlit
21e7b 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 e3Strlen30(zName
21e7c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
21e7d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
21e7e 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 (pParse, "no suc
21e7f 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 h collation sequ
21e80 65 6e 63 65 3a 20 25 2e 2a 73 22 2c 20 6e 4e 61 ence: %.*s"